Repository navigation
fix(chat): keep a chat on screen and load its history again when the host cannot read it #9976
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
970c631
530029f
a6dbe68
bacc68b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -77,7 +77,9 @@ import { | |
| type WorkDrawerHandle, | ||
| } from "../WorkDrawer"; | ||
| import { ComposerChips } from "./components/ComposerChips"; | ||
| import { HistoryNotice } from "./components/HistoryNotice"; | ||
| import { SubagentNotice } from "./components/SubagentNotice"; | ||
| import { TranscriptLoadState } from "./components/TranscriptLoadState"; | ||
| import { | ||
| type SwitchAccount, | ||
| type SwitchableAgent, | ||
|
|
@@ -101,6 +103,7 @@ import { subagentsInTurn } from "./utils/subagentsInTurn"; | |
| const RESUME_FOLLOW_PX = 24; | ||
| const NO_COMMANDS: AvailableCommand[] = []; | ||
| const NO_CONFIG_OPTIONS: SessionConfigOption[] = []; | ||
| const NO_APPROVALS: ReturnType<typeof useApprovals> = []; | ||
|
|
||
| function contentText(content: UserContent[]): string { | ||
| return userMessageText({ content }); | ||
|
|
@@ -503,7 +506,7 @@ export function SessionView({ | |
| [sendPrompt], | ||
| ); | ||
| const onSend = useMainSend({ | ||
| approvals, | ||
| approvals: session.history === "loaded" ? approvals : NO_APPROVALS, | ||
| holdPrompt, | ||
| loaded, | ||
| onAnswerFailed, | ||
|
|
@@ -790,6 +793,12 @@ export function SessionView({ | |
| {setupEnvironment && ( | ||
| <SetupSessionHeader environmentId={setupEnvironment} /> | ||
| )} | ||
| {session.history !== "loaded" && ( | ||
| <HistoryNotice | ||
|
Comment on lines
+796
to
+797
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mount the history live region before the first failure message. When history first becomes unavailable, this branch inserts the
|
||
| failed={session.history === "failed"} | ||
| onRetry={session.retryHistory} | ||
| /> | ||
| )} | ||
| <MessageScroller.Provider | ||
| autoScroll | ||
| defaultScrollPosition="end" | ||
|
|
@@ -832,6 +841,13 @@ export function SessionView({ | |
| </NestedItemsProvider> | ||
| </TranscriptPluginsProvider> | ||
| </SessionModeProvider> | ||
| {!focusedSubagentId && ( | ||
| <TranscriptLoadState | ||
| empty={timeline.length === 0 && transcriptOutbox.length === 0} | ||
| loading={!loaded} | ||
| unreachable={session.unreachable} | ||
| /> | ||
| )} | ||
| {!focusedSubagentId && rail.length > 1 && ( | ||
| <ChatHistorySidebarScroller | ||
| className="absolute inset-y-0 left-0 my-auto hidden h-fit max-h-full flex-col pl-3 @[56rem]:flex" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { Trans } from "@lingui/react/macro"; | ||
| import { Button } from "@superset/ui/button"; | ||
| import { Spinner } from "@superset/ui/spinner"; | ||
|
|
||
| export function HistoryNotice({ | ||
| failed, | ||
| onRetry, | ||
| }: { | ||
| failed: boolean; | ||
| onRetry: () => void; | ||
| }) { | ||
| return ( | ||
| <div className="flex shrink-0 justify-center px-4 pt-2"> | ||
| <output className="flex items-center gap-2 rounded-md border px-3 py-1 text-muted-foreground text-xs"> | ||
| {failed ? ( | ||
| <> | ||
| <Trans>Couldn't load this chat's messages.</Trans> | ||
| <Button | ||
| className="h-6 px-2 text-xs" | ||
| onClick={onRetry} | ||
| size="sm" | ||
| variant="ghost" | ||
| > | ||
| <Trans>Retry</Trans> | ||
| </Button> | ||
| </> | ||
| ) : ( | ||
| <> | ||
| <Spinner className="size-3" /> | ||
| <Trans>Couldn't load this chat's messages. Retrying…</Trans> | ||
| </> | ||
| )} | ||
| </output> | ||
| </div> | ||
| ); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { HistoryNotice } from "./HistoryNotice"; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { Trans } from "@lingui/react/macro"; | ||
| import { Spinner } from "@superset/ui/spinner"; | ||
| import { useEffect, useState } from "react"; | ||
|
|
||
| const SLOW_LOAD_MS = 500; | ||
|
|
||
| export function TranscriptLoadState({ | ||
| empty, | ||
| loading, | ||
| unreachable, | ||
| }: { | ||
| empty: boolean; | ||
| loading: boolean; | ||
| unreachable: boolean; | ||
| }) { | ||
| const [slow, setSlow] = useState(false); | ||
| useEffect(() => { | ||
| if (!loading) { | ||
| setSlow(false); | ||
| return; | ||
| } | ||
| const timer = setTimeout(() => setSlow(true), SLOW_LOAD_MS); | ||
| return () => clearTimeout(timer); | ||
| }, [loading]); | ||
|
|
||
| if (!empty || !loading || (!unreachable && !slow)) return null; | ||
| return ( | ||
| <output className="pointer-events-none absolute inset-0 flex items-center justify-center gap-2 p-4 text-center text-muted-foreground text-xs"> | ||
| <Spinner className="size-3" /> | ||
| {unreachable ? ( | ||
| <Trans>Can't reach this workspace's host. Retrying…</Trans> | ||
| ) : ( | ||
| <Trans>Loading chat</Trans> | ||
| )} | ||
| </output> | ||
| ); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { TranscriptLoadState } from "./TranscriptLoadState"; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1007,6 +1007,152 @@ describe("useChatSession", () => { | |
| test("keeps the cached transcript when the journal cannot be read", async () => { | ||
| expect(await cacheAndFailLoad("journal_missing")).toBeGreaterThan(0); | ||
| }); | ||
|
|
||
| test("shows the cached transcript and loads the history again while the host cannot read it", async () => { | ||
| const stack = await startStack(); | ||
| const first = stack.makeClient(); | ||
| const firstView = render(<Probe client={first} />); | ||
| await domWaitFor(() => expect(session().status).toBe("ready")); | ||
| act(() => { | ||
| session().sendPrompt([{ type: "text", text: "hello" }]); | ||
| }); | ||
| await domWaitFor(() => | ||
| expect(session().snapshot.session?.status).toBe("idle"), | ||
| ); | ||
| const cachedItems = session().snapshot.items.size; | ||
| expect(cachedItems).toBeGreaterThan(0); | ||
| firstView.unmount(); | ||
| first.close(); | ||
|
|
||
| let readable = false; | ||
| const flaky: ChatTransport = new Proxy(stack.transport, { | ||
| get: (target, prop, receiver) => { | ||
| if ((prop === "getOutline" || prop === "getItems") && !readable) { | ||
| return async () => ({ ok: false, reset: "journal_missing" }); | ||
| } | ||
| return Reflect.get(target, prop, receiver); | ||
| }, | ||
| }); | ||
| const manual = createManualWait(); | ||
| const client = stack.makeClient({ transport: flaky, wait: manual.wait }); | ||
| const view = render(<Probe client={client} wait={manual.wait} />); | ||
|
|
||
| await domWaitFor(() => expect(session().history).toBe("retrying")); | ||
| expect(session().status).toBe("ready"); | ||
| expect(session().snapshot.items.size).toBe(cachedItems); | ||
|
|
||
| readable = true; | ||
| await domWaitFor(() => { | ||
| act(() => manual.flush()); | ||
| expect(session().history).toBe("loaded"); | ||
| }); | ||
| expect(session().snapshot.items.size).toBe(cachedItems); | ||
| view.unmount(); | ||
| client.close(); | ||
| await stack.runtime.dispose(); | ||
| }); | ||
|
|
||
| test("stops asking for an unreadable history once the chat closes", async () => { | ||
| const stack = await startStack(); | ||
| const unreadable: ChatTransport = new Proxy(stack.transport, { | ||
| get: (target, prop, receiver) => | ||
| prop === "getOutline" || prop === "getItems" | ||
| ? async () => ({ ok: false, reset: "journal_missing" }) | ||
| : Reflect.get(target, prop, receiver), | ||
| }); | ||
| const manual = createManualWait(); | ||
| const client = stack.makeClient({ | ||
| transport: unreadable, | ||
| wait: manual.wait, | ||
| }); | ||
| const view = render(<Probe client={client} wait={manual.wait} />); | ||
| await domWaitFor(() => expect(session().history).toBe("retrying")); | ||
| expect(session().snapshot.session?.harness).toBe(FAKE_HARNESS); | ||
|
|
||
| view.unmount(); | ||
| client.close(); | ||
| act(() => manual.flush()); | ||
| expect(manual.pendingCount()).toBe(0); | ||
| await stack.runtime.dispose(); | ||
| }); | ||
|
|
||
| test("loads the history again when a resync after a stream reset fails", async () => { | ||
| const stack = await startStack(); | ||
| let readable = true; | ||
| const flaky: ChatTransport = new Proxy(stack.transport, { | ||
| get: (target, prop, receiver) => { | ||
| if ((prop === "getOutline" || prop === "getItems") && !readable) { | ||
| return async () => ({ ok: false, reset: "journal_missing" }); | ||
| } | ||
| return Reflect.get(target, prop, receiver); | ||
| }, | ||
| }); | ||
| const manual = createManualWait(); | ||
| const client = stack.makeClient({ transport: flaky, wait: manual.wait }); | ||
| const view = render(<Probe client={client} wait={manual.wait} />); | ||
| await domWaitFor(() => expect(session().status).toBe("ready")); | ||
| expect(session().history).toBe("loaded"); | ||
|
|
||
| readable = false; | ||
| act(() => { | ||
| stack.runtime.subscriptions.publish({ | ||
| v: 1, | ||
| sessionId: stack.sessionId, | ||
| ts: Date.now(), | ||
| reset: { reason: "journal_missing" }, | ||
| }); | ||
| }); | ||
| await domWaitFor(() => expect(session().history).toBe("retrying")); | ||
|
|
||
| readable = true; | ||
| await domWaitFor(() => { | ||
| act(() => manual.flush()); | ||
| expect(session().history).toBe("loaded"); | ||
| }); | ||
| view.unmount(); | ||
| client.close(); | ||
| await stack.runtime.dispose(); | ||
| }); | ||
|
|
||
| test("gives up on an unreadable history after a while and loads it when asked again", async () => { | ||
| const stack = await startStack(); | ||
| let readable = false; | ||
| let reads = 0; | ||
| const flaky: ChatTransport = new Proxy(stack.transport, { | ||
| get: (target, prop, receiver) => { | ||
| if (prop !== "getOutline" && prop !== "getItems") { | ||
| return Reflect.get(target, prop, receiver); | ||
| } | ||
| return async (input: never) => { | ||
| reads += 1; | ||
| if (!readable) return { ok: false, reset: "journal_missing" }; | ||
| return prop === "getOutline" | ||
| ? target.getOutline(input) | ||
| : target.getItems(input); | ||
| }; | ||
| }, | ||
| }); | ||
| const manual = createManualWait(); | ||
| const client = stack.makeClient({ transport: flaky, wait: manual.wait }); | ||
| const view = render(<Probe client={client} wait={manual.wait} />); | ||
|
|
||
| await domWaitFor(() => { | ||
| act(() => manual.flush()); | ||
| expect(session().history).toBe("failed"); | ||
| }); | ||
| const readsWhenFailed = reads; | ||
| for (let i = 0; i < 5; i += 1) act(() => manual.flush()); | ||
| await new Promise((resolve) => setTimeout(resolve, 20)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check that retries stop without waiting on a real timer. The test already injects
|
||
| expect(reads).toBe(readsWhenFailed); | ||
|
|
||
| readable = true; | ||
| act(() => session().retryHistory()); | ||
| await domWaitFor(() => expect(session().history).toBe("loaded")); | ||
| expect(reads).toBeGreaterThan(readsWhenFailed); | ||
| view.unmount(); | ||
| client.close(); | ||
| await stack.runtime.dispose(); | ||
| }); | ||
| }); | ||
|
|
||
| test("a chat subscribes to background steps by default", () => { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep composer answers working for questions received on the live stream.
If the host sends a new yes/no question while history is retrying, the snapshot contains the question, but this branch passes
NO_APPROVALS. Typing “yes” sends a new prompt instead of answering the question. With the question present,useMainSendwould send a boolean form response throughrespondToApproval. (raw.githubusercontent.com)Exclude cached-only approvals until recovery, but keep approvals confirmed by the current stream eligible for composer answers.