diff --git a/packages/app/e2e/regression/btw-sidebar.spec.ts b/packages/app/e2e/regression/btw-sidebar.spec.ts index 0472b77b2ba..9fab3463316 100644 --- a/packages/app/e2e/regression/btw-sidebar.spec.ts +++ b/packages/app/e2e/regression/btw-sidebar.spec.ts @@ -66,7 +66,7 @@ test("keeps separate one-shot /btw answers and restores them after reload", asyn expect(prompts).toEqual([]) }) -test("isolates concurrent side questions and makes an interrupted reload retryable", async ({ page }) => { +test("isolates concurrent side questions and makes interrupted and blank answers retryable", async ({ page }) => { const held = Promise.withResolvers() const attempts: string[] = [] const prompts: unknown[] = [] @@ -83,6 +83,14 @@ test("isolates concurrent side questions and makes an interrupted reload retryab return { text: "Abandoned answer" } } + // A reply with only a tool call or reasoning reaches the panel as blank text. + if (input.prompt.includes("blank question")) { + return { + text: + attempts.filter((item) => item.includes("blank question")).length === 1 ? " \n " : "Recovered answer", + } + } + return { text: input.prompt.includes("fast question") ? "Fast answer" : "Retried answer" } }, }) @@ -114,6 +122,17 @@ test("isolates concurrent side questions and makes an interrupted reload retryab await expect(panel.getByText("Retried answer", { exact: true })).toBeVisible() await expect(panel.getByText("Abandoned answer", { exact: true })).toHaveCount(0) await expect(panel.getByRole("textbox")).toHaveCount(0) + + await editor.fill("/btw blank question") + await editor.press("Enter") + await expect(page.getByRole("tab", { name: "blank question", exact: true })).toHaveAttribute("data-selected", "") + await expect(panel.getByText("Couldn’t answer that question", { exact: true })).toBeVisible() + await expect(panel.getByText("The model replied without any text.", { exact: true })).toBeVisible() + await expect(panel.getByRole("status")).toHaveCount(0) + await panel.getByRole("button", { name: "Retry", exact: true }).click() + await expect(panel.getByText("Recovered answer", { exact: true })).toBeVisible() + await expect(panel.getByText("Couldn’t answer that question", { exact: true })).toHaveCount(0) + expect(attempts.filter((item) => item.includes("blank question"))).toHaveLength(2) expect(prompts).toEqual([]) }) diff --git a/packages/gui-plugins/src/btw/i18n/en.ts b/packages/gui-plugins/src/btw/i18n/en.ts index 5da51e18208..d5d51911a03 100644 --- a/packages/gui-plugins/src/btw/i18n/en.ts +++ b/packages/gui-plugins/src/btw/i18n/en.ts @@ -4,6 +4,8 @@ export default { "tab.title": "/btw", "question.required": "Add a question after /btw", error: "Couldn’t answer that question", + "error.empty": "The model replied without any text.", + "error.timeout": "The model took too long to reply.", retry: "Retry", copy: "Copy answer", } diff --git a/packages/gui-plugins/src/btw/machine.test.ts b/packages/gui-plugins/src/btw/machine.test.ts new file mode 100644 index 00000000000..944e1b6955b --- /dev/null +++ b/packages/gui-plugins/src/btw/machine.test.ts @@ -0,0 +1,292 @@ +import { describe, expect, test } from "bun:test" +import { + BTW_GENERATE_TIMEOUT_MS, + btwChatView, + btwTabView, + createBtwState, + transitionBtw, + type BtwEffect, + type BtwEvent, + type BtwSavedChat, + type BtwState, +} from "./machine" + +const model = { providerID: "anthropic", id: "claude" } + +const asked: BtwEvent = { + type: "chat.asked", + sessionKey: "s1", + id: "c1", + question: " Why? ", + created: 1_000, + agent: "build", + model, +} + +// Replays events through the pure machine and collects every effect it asked for, in order. +function play(state: BtwState, ...events: BtwEvent[]) { + return events.reduce<{ state: BtwState; effects: readonly BtwEffect[] }>( + (result, event) => { + const transition = transitionBtw(result.state, event) + + return { state: transition.state, effects: [...result.effects, ...transition.effects] } + }, + { state, effects: [] }, + ) +} + +function succeeded(attempt: number, text: string, now = 1_450): BtwEvent { + return { type: "generate.succeeded", sessionKey: "s1", id: "c1", attempt, text, now } +} + +function retried(saved?: BtwSavedChat): BtwEvent { + return { type: "chat.retried", sessionKey: "s1", id: "c1", now: 2_000, saved } +} + +describe("bug replays", () => { + test.each([" \n\t ", ""])( + "a reply without visible text (%j) fails with Retry instead of a blank panel", + (text) => { + const empty = play(createBtwState(), asked, succeeded(1, text)) + + expect(btwChatView(empty.state, [], "s1", "c1")).toEqual({ + phase: "failed", + question: "Why?", + created: 1_000, + agent: "build", + model, + failure: { reason: "empty" }, + }) + expect(transitionBtw(play(createBtwState(), asked).state, succeeded(1, text)).effects).toEqual([ + { type: "abort", attempt: 1 }, + ]) + + const recovered = play(empty.state, retried(), succeeded(2, "Because.", 2_300)) + + expect(recovered.effects[0]).toEqual({ + type: "generate", + id: "c1", + attempt: 2, + question: "Why?", + timeout: BTW_GENERATE_TIMEOUT_MS, + }) + expect(btwChatView(recovered.state, [], "s1", "c1")).toMatchObject({ + phase: "answered", + answer: "Because.", + duration: 300, + }) + }, + ) + + test("a blank answer stored by an older build is a retryable failure", () => { + const stored = { id: "c1", question: "Why?", answer: " " } + + expect(btwChatView(createBtwState(), [stored], "s1", "c1")).toEqual({ + phase: "failed", + question: "Why?", + created: undefined, + agent: undefined, + model: undefined, + failure: { reason: "empty" }, + }) + expect(transitionBtw(createBtwState(), retried(stored)).effects).toEqual([ + { type: "generate", id: "c1", attempt: 1, question: "Why?", timeout: BTW_GENERATE_TIMEOUT_MS }, + ]) + }) + + test("a question asked before the desktop store loads shows, fails and retries without it", () => { + const running = play(createBtwState(), asked).state + + expect(btwTabView(running, undefined, "s1", "c1")).toEqual({ status: "ready", question: "Why?" }) + expect(btwChatView(running, undefined, "s1", "c1")).toMatchObject({ phase: "running", question: "Why?" }) + + const failed = transitionBtw(running, { + type: "generate.failed", + sessionKey: "s1", + id: "c1", + attempt: 1, + message: "Offline", + }).state + + expect(btwChatView(failed, undefined, "s1", "c1")).toMatchObject({ + phase: "failed", + question: "Why?", + failure: { reason: "error", message: "Offline" }, + }) + expect(transitionBtw(failed, retried()).effects).toEqual([ + { type: "generate", id: "c1", attempt: 2, question: "Why?", timeout: BTW_GENERATE_TIMEOUT_MS }, + ]) + }) + + test("a restored tab waits for the store, then shows what it holds", () => { + const state = createBtwState() + + const saved = [ + { id: "answered", question: "Q1", answer: "A1", duration: 5 }, + { id: "interrupted", question: "Q2" }, + ] + + expect(btwTabView(state, undefined, "s1", "answered")).toEqual({ status: "loading" }) + expect(btwChatView(state, undefined, "s1", "answered")).toEqual({ phase: "loading" }) + expect(btwTabView(state, saved, "s1", "gone")).toEqual({ status: "missing" }) + expect(btwChatView(state, saved, "s1", "gone")).toEqual({ phase: "missing" }) + expect(btwTabView(state, saved, "s1", "answered")).toEqual({ status: "ready", question: "Q1" }) + expect(btwChatView(state, saved, "s1", "answered")).toMatchObject({ phase: "answered", answer: "A1", duration: 5 }) + expect(btwChatView(state, saved, "s1", "interrupted")).toMatchObject({ + phase: "failed", + failure: { reason: "interrupted" }, + }) + }) + + test("a stalled request times out with Retry and ignores its late reply", () => { + const timedOut = transitionBtw(play(createBtwState(), asked).state, { + type: "generate.timed-out", + sessionKey: "s1", + id: "c1", + attempt: 1, + }) + + expect(timedOut.effects).toEqual([{ type: "abort", attempt: 1 }]) + expect(btwChatView(timedOut.state, [], "s1", "c1")).toMatchObject({ + phase: "failed", + failure: { reason: "timeout" }, + }) + + const late = transitionBtw(timedOut.state, succeeded(1, "Too late")) + + expect(late.state).toBe(timedOut.state) + expect(late.effects).toEqual([]) + }) +}) + +describe("requests", () => { + test("an answer is trimmed, timed from the question and stored", () => { + const result = transitionBtw(play(createBtwState(), asked).state, succeeded(1, " Direct answer ")) + + expect(btwChatView(result.state, undefined, "s1", "c1")).toMatchObject({ + phase: "answered", + answer: "Direct answer", + duration: 450, + }) + expect(result.effects).toEqual([ + { type: "abort", attempt: 1 }, + { type: "persist-answer", id: "c1", answer: "Direct answer", duration: 450, agent: "build", model }, + ]) + expect( + btwChatView(transitionBtw(play(createBtwState(), asked).state, succeeded(1, "x", 10)).state, [], "s1", "c1"), + ).toMatchObject({ duration: 0 }) + }) + + test("asking stores the question, opens its tab and starts one attempt", () => { + expect(transitionBtw(createBtwState(), asked).effects).toEqual([ + { type: "persist-question", id: "c1", question: "Why?", created: 1_000, agent: "build", model }, + { type: "open-tab", id: "c1" }, + { type: "generate", id: "c1", attempt: 1, question: "Why?", timeout: BTW_GENERATE_TIMEOUT_MS }, + ]) + + const running = play(createBtwState(), asked).state + + expect(transitionBtw(running, asked).state).toBe(running) + expect(transitionBtw(createBtwState(), { ...asked, question: " " }).effects).toEqual([]) + }) + + test("retry starts only from a failure", () => { + const running = play(createBtwState(), asked).state + const answered = transitionBtw(running, succeeded(1, "A")).state + + expect(transitionBtw(running, retried()).state).toBe(running) + expect(transitionBtw(answered, retried()).state).toBe(answered) + expect(transitionBtw(createBtwState(), retried()).effects).toEqual([]) + expect(transitionBtw(createBtwState(), retried({ id: "c1", question: "Why?", answer: "A" })).effects).toEqual([]) + }) + + test("results of an earlier attempt never settle a retry", () => { + const retrying = play( + createBtwState(), + asked, + { type: "generate.timed-out", sessionKey: "s1", id: "c1", attempt: 1 }, + retried(), + ).state + + const stale: BtwEvent[] = [ + succeeded(1, "Old"), + { type: "generate.failed", sessionKey: "s1", id: "c1", attempt: 1, message: "Old" }, + { type: "generate.timed-out", sessionKey: "s1", id: "c1", attempt: 1 }, + ] + + stale.forEach((event) => expect(transitionBtw(retrying, event).state).toBe(retrying)) + }) + + test("a server error keeps its message until the retry, and a blank message reads as none", () => { + const failed = play(createBtwState(), asked, { + type: "generate.failed", + sessionKey: "s1", + id: "c1", + attempt: 1, + message: " 429 Too Many Requests ", + }).state + + expect(btwChatView(failed, [], "s1", "c1")).toMatchObject({ + failure: { reason: "error", message: "429 Too Many Requests" }, + }) + expect(btwChatView(transitionBtw(failed, retried()).state, [], "s1", "c1").phase).toBe("running") + + const blank = play(createBtwState(), asked, { + type: "generate.failed", + sessionKey: "s1", + id: "c1", + attempt: 1, + message: " ", + }).state + + expect(btwChatView(blank, [], "s1", "c1")).toMatchObject({ failure: { reason: "error", message: undefined } }) + }) +}) + +describe("exits", () => { + test("closing a running chat's tab ends its request and forgets it", () => { + const removed = transitionBtw(play(createBtwState(), asked).state, { + type: "chat.removed", + sessionKey: "s1", + id: "c1", + }) + + expect(removed.effects).toEqual([ + { type: "abort", attempt: 1 }, + { type: "delete-chat", id: "c1" }, + ]) + expect(removed.state).toEqual({ status: "active", issued: 1, sessions: new Map() }) + expect(transitionBtw(removed.state, succeeded(1, "Late")).state).toBe(removed.state) + + const restored = createBtwState() + const forgotten = transitionBtw(restored, { type: "chat.removed", sessionKey: "s1", id: "c1" }) + + expect(forgotten.state).toBe(restored) + expect(forgotten.effects).toEqual([{ type: "delete-chat", id: "c1" }]) + }) + + test("closing a session ends only its requests", () => { + const both = play(createBtwState(), asked, { ...asked, sessionKey: "s2", id: "c2" }).state + const pruned = transitionBtw(both, { type: "sessions.pruned", keep: new Set(["s2"]) }) + + expect(pruned.effects).toEqual([{ type: "abort", attempt: 1 }]) + expect(btwChatView(pruned.state, undefined, "s1", "c1")).toEqual({ phase: "loading" }) + expect(btwChatView(pruned.state, undefined, "s2", "c2").phase).toBe("running") + expect(transitionBtw(pruned.state, { type: "sessions.pruned", keep: new Set(["s2"]) }).state).toBe(pruned.state) + }) + + test("disposing ends every request and ignores everything after", () => { + const disposed = transitionBtw(play(createBtwState(), asked, { ...asked, sessionKey: "s2", id: "c2" }).state, { + type: "disposed", + }) + + expect(disposed.state).toEqual({ status: "disposed" }) + expect(disposed.effects).toEqual([ + { type: "abort", attempt: 1 }, + { type: "abort", attempt: 2 }, + ]) + const later: BtwEvent[] = [asked, retried(), succeeded(1, "Late"), { type: "disposed" }] + + later.forEach((event) => expect(transitionBtw(disposed.state, event).state).toBe(disposed.state)) + }) +}) diff --git a/packages/gui-plugins/src/btw/machine.ts b/packages/gui-plugins/src/btw/machine.ts new file mode 100644 index 00000000000..9ba3b9a2725 --- /dev/null +++ b/packages/gui-plugins/src/btw/machine.ts @@ -0,0 +1,341 @@ +/** Longest one answer may take before it fails with Retry; a stalled connection never settles on its own. */ +export const BTW_GENERATE_TIMEOUT_MS = 300_000 + +export type BtwChatModel = { + readonly providerID: string + readonly id: string + readonly variant?: string +} + +/** One chat as the session store keeps it. */ +export type BtwSavedChat = { + readonly id: string + readonly question: string + readonly answer?: string + readonly agent?: string + readonly model?: BtwChatModel + readonly created?: number + readonly duration?: number +} + +type ChatInfo = { + readonly question: string + readonly created?: number + readonly agent?: string + readonly model?: BtwChatModel +} + +/** + * Why a chat has no answer. `interrupted` is a stored chat without an answer: a reload, closed session or disposed + * plugin abandoned its request. `empty` is a reply without visible text (a tool call or reasoning only). + */ +export type BtwFailure = + | { readonly reason: "interrupted" } + | { readonly reason: "empty" } + | { readonly reason: "timeout" } + | { readonly reason: "error"; readonly message?: string } + +/** + * A chat this plugin instance asked or retried. Chats it never touched are read from the session store, so a chat + * shows its question, failure and Retry before the store loads. + */ +export type BtwChat = + | (ChatInfo & { readonly status: "running"; readonly attempt: number; readonly started: number }) + | (ChatInfo & { readonly status: "answered"; readonly answer: string; readonly duration: number }) + | (ChatInfo & { readonly status: "failed"; readonly failure: BtwFailure }) + +/** `issued` is the last attempt id handed out; chats are keyed by session key, then chat id. */ +export type BtwState = + | { + readonly status: "active" + readonly issued: number + readonly sessions: ReadonlyMap> + } + | { readonly status: "disposed" } + +type ActiveState = Extract + +export type BtwEvent = + | { + readonly type: "chat.asked" + readonly sessionKey: string + readonly id: string + readonly question: string + readonly created: number + readonly agent?: string + readonly model?: BtwChatModel + } + | { + readonly type: "chat.retried" + readonly sessionKey: string + readonly id: string + readonly now: number + /** The stored chat, once the session store has loaded. */ + readonly saved?: BtwSavedChat + readonly agent?: string + readonly model?: BtwChatModel + } + | { + readonly type: "generate.succeeded" + readonly sessionKey: string + readonly id: string + readonly attempt: number + readonly text: string + readonly now: number + } + | { + readonly type: "generate.failed" + readonly sessionKey: string + readonly id: string + readonly attempt: number + readonly message?: string + } + | { readonly type: "generate.timed-out"; readonly sessionKey: string; readonly id: string; readonly attempt: number } + | { readonly type: "chat.removed"; readonly sessionKey: string; readonly id: string } + | { readonly type: "sessions.pruned"; readonly keep: ReadonlySet } + | { readonly type: "disposed" } + +/** Effects act on the session of the event that caused them. */ +export type BtwEffect = + | { + readonly type: "persist-question" + readonly id: string + readonly question: string + readonly created: number + readonly agent?: string + readonly model?: BtwChatModel + } + | { readonly type: "open-tab"; readonly id: string } + | { + readonly type: "generate" + readonly id: string + readonly attempt: number + readonly question: string + readonly timeout: number + } + | { + readonly type: "persist-answer" + readonly id: string + readonly answer: string + readonly duration: number + readonly agent?: string + readonly model?: BtwChatModel + } + | { readonly type: "delete-chat"; readonly id: string } + /** Ends the attempt's request, if it still runs, and clears its timeout. */ + | { readonly type: "abort"; readonly attempt: number } + +export type BtwTransition = { + readonly state: BtwState + readonly effects: readonly BtwEffect[] +} + +export function createBtwState(): BtwState { + return { status: "active", issued: 0, sessions: new Map() } +} + +export function transitionBtw(state: BtwState, event: BtwEvent): BtwTransition { + if (state.status === "disposed") return next(state) + + if (event.type === "disposed") { + return next({ status: "disposed" }, [...state.sessions.values()].flatMap(aborts)) + } + + if (event.type === "chat.asked") return asked(state, event) + + if (event.type === "chat.retried") return retried(state, event) + + if (event.type === "chat.removed") return removed(state, event.sessionKey, event.id) + + if (event.type === "sessions.pruned") return pruned(state, event.keep) + + const chat = state.sessions.get(event.sessionKey)?.get(event.id) + + if (chat?.status !== "running" || chat.attempt !== event.attempt) return next(state) + + const info = { question: chat.question, created: chat.created, agent: chat.agent, model: chat.model } + const abort: BtwEffect = { type: "abort", attempt: chat.attempt } + + if (event.type === "generate.timed-out") { + return next(put(state, event.sessionKey, event.id, { ...info, status: "failed", failure: { reason: "timeout" } }), [ + abort, + ]) + } + + if (event.type === "generate.failed") { + const failure: BtwFailure = { reason: "error", message: event.message?.trim() || undefined } + + return next(put(state, event.sessionKey, event.id, { ...info, status: "failed", failure }), [abort]) + } + + const answer = event.text.trim() + + // A reply without visible text is a failure the user can retry, not an empty answer. + if (!answer) { + return next(put(state, event.sessionKey, event.id, { ...info, status: "failed", failure: { reason: "empty" } }), [ + abort, + ]) + } + + const duration = Math.max(0, event.now - chat.started) + + return next(put(state, event.sessionKey, event.id, { ...info, status: "answered", answer, duration }), [ + abort, + { type: "persist-answer", id: event.id, answer, duration, agent: chat.agent, model: chat.model }, + ]) +} + +export type BtwTabView = + | { readonly status: "loading" } + | { readonly status: "missing" } + | { readonly status: "ready"; readonly question: string } + +export type BtwChatView = + | { readonly phase: "loading" } + | { readonly phase: "missing" } + | (ChatInfo & { readonly phase: "running" }) + | (ChatInfo & { readonly phase: "failed"; readonly failure: BtwFailure }) + | (ChatInfo & { readonly phase: "answered"; readonly answer: string; readonly duration?: number }) + +/** What a chat's tab shows. `saved` is the session store's chats, undefined until it loads. */ +export function btwTabView( + state: BtwState, + saved: readonly BtwSavedChat[] | undefined, + sessionKey: string, + id: string, +): BtwTabView { + const chat = liveChat(state, sessionKey, id) ?? saved?.find((item) => item.id === id) + + if (chat) return { status: "ready", question: chat.question } + + return saved ? { status: "missing" } : { status: "loading" } +} + +/** What a chat's panel shows. `saved` is the session store's chats, undefined until it loads. */ +export function btwChatView( + state: BtwState, + saved: readonly BtwSavedChat[] | undefined, + sessionKey: string, + id: string, +): BtwChatView { + const live = liveChat(state, sessionKey, id) + + if (live) { + const info = { question: live.question, created: live.created, agent: live.agent, model: live.model } + + if (live.status === "running") return { ...info, phase: "running" } + + if (live.status === "failed") return { ...info, phase: "failed", failure: live.failure } + + return { ...info, phase: "answered", answer: live.answer, duration: live.duration } + } + + if (!saved) return { phase: "loading" } + const stored = saved.find((item) => item.id === id) + + if (!stored) return { phase: "missing" } + const info = { question: stored.question, created: stored.created, agent: stored.agent, model: stored.model } + const answer = stored.answer?.trim() + + if (answer) return { ...info, phase: "answered", answer, duration: stored.duration } + + // Builds before this machine stored a blank reply as the answer. + return { ...info, phase: "failed", failure: { reason: stored.answer === undefined ? "interrupted" : "empty" } } +} + +function asked(state: ActiveState, event: Extract) { + const question = event.question.trim() + + if (!question || state.sessions.get(event.sessionKey)?.has(event.id)) return next(state) + const attempt = state.issued + 1 + const info = { question, created: event.created, agent: event.agent, model: event.model } + + return next( + put({ ...state, issued: attempt }, event.sessionKey, event.id, { + ...info, + status: "running", + attempt, + started: event.created, + }), + [ + { type: "persist-question", id: event.id, ...info }, + { type: "open-tab", id: event.id }, + { type: "generate", id: event.id, attempt, question, timeout: BTW_GENERATE_TIMEOUT_MS }, + ], + ) +} + +// A stored chat this instance never asked (one restored after a reload) is retryable once the store has loaded. +function retried(state: ActiveState, event: Extract) { + const live = state.sessions.get(event.sessionKey)?.get(event.id) + const chat = live ?? (event.saved?.answer?.trim() ? undefined : event.saved) + + if (!chat || (live && live.status !== "failed")) return next(state) + const attempt = state.issued + 1 + + return next( + put({ ...state, issued: attempt }, event.sessionKey, event.id, { + status: "running", + attempt, + started: event.now, + question: chat.question, + created: chat.created, + agent: event.agent ?? chat.agent, + model: event.model ?? chat.model, + }), + [{ type: "generate", id: event.id, attempt, question: chat.question, timeout: BTW_GENERATE_TIMEOUT_MS }], + ) +} + +function removed(state: ActiveState, sessionKey: string, id: string) { + const chats = state.sessions.get(sessionKey) + const chat = chats?.get(id) + const remove: BtwEffect = { type: "delete-chat", id } + + if (!chats || !chat) return next(state, [remove]) + const rest = new Map(chats) + rest.delete(id) + const sessions = new Map(state.sessions) + + if (rest.size === 0) sessions.delete(sessionKey) + + if (rest.size > 0) sessions.set(sessionKey, rest) + + return next( + { ...state, sessions }, + chat.status === "running" ? [{ type: "abort", attempt: chat.attempt }, remove] : [remove], + ) +} + +// A closed session's requests end; its stored chats without an answer read as interrupted when it opens again. +function pruned(state: ActiveState, keep: ReadonlySet) { + const gone = [...state.sessions.keys()].filter((key) => !keep.has(key)) + + if (gone.length === 0) return next(state) + + return next( + { ...state, sessions: new Map([...state.sessions].filter(([key]) => keep.has(key))) }, + gone.flatMap((key) => aborts(state.sessions.get(key) ?? new Map())), + ) +} + +function aborts(chats: ReadonlyMap): BtwEffect[] { + return [...chats.values()].flatMap((chat): BtwEffect[] => + chat.status === "running" ? [{ type: "abort", attempt: chat.attempt }] : [], + ) +} + +function liveChat(state: BtwState, sessionKey: string, id: string) { + return state.status === "active" ? state.sessions.get(sessionKey)?.get(id) : undefined +} + +function put(state: ActiveState, sessionKey: string, id: string, chat: BtwChat): ActiveState { + const sessions = new Map(state.sessions) + sessions.set(sessionKey, new Map(state.sessions.get(sessionKey)).set(id, chat)) + + return { ...state, sessions } +} + +function next(state: BtwState, effects: readonly BtwEffect[] = []): BtwTransition { + return { state, effects } +} diff --git a/packages/gui-plugins/src/btw/model.ts b/packages/gui-plugins/src/btw/model.ts index a7ea84b214c..21a148cf72d 100644 --- a/packages/gui-plugins/src/btw/model.ts +++ b/packages/gui-plugins/src/btw/model.ts @@ -1,9 +1,9 @@ -import { batch, onCleanup } from "solid-js" -import { createStore } from "solid-js/store" +import { batch, createSignal, onCleanup, untrack } from "solid-js" import { showToast } from "@opencode/ui/toast" import { uuid } from "@opencode/util/uuid" import type { MountedSession, SetupContext } from "@opencode/plugin/gui" import type Btw from "./index" +import { btwChatView, btwTabView, createBtwState, transitionBtw, type BtwEffect, type BtwEvent } from "./machine" const instructions = [ "The user is asking a quick side question about the conversation so far.", @@ -13,22 +13,124 @@ const instructions = [ /** One-shot side questions per session, one tab each, stored until the tab closes. */ export function createBtw(ctx: SetupContext) { - const controllers = new Map() - const [requests, setRequests] = createStore<{ pending: string[] }>({ pending: [] }) - const saved = (session: MountedSession) => ctx.stores.chats(session) + const [state, setState] = createSignal(createBtwState()) + // Driver handles only: the machine decides which attempt runs, these end its request and timeout. + const inFlight = new Map }>() - const stop = (id: string) => { - controllers.get(id)?.abort() - controllers.delete(id) - setRequests("pending", (list) => list.filter((item) => item !== id)) + const saved = (session: MountedSession) => { + const store = ctx.stores.chats(session) + + return store.ready() ? (store.value?.chats ?? []) : undefined + } + + // Effects act on `session`, the session of the action or request that caused the event. + const dispatch = (event: BtwEvent, session?: MountedSession) => { + const previous = untrack(state) + const result = transitionBtw(previous, event) + + batch(() => { + if (result.state !== previous) setState(result.state) + result.effects.forEach((effect) => run(effect, session)) + }) + } + + const run = (effect: BtwEffect, session: MountedSession | undefined) => { + if (effect.type === "abort") { + const handle = inFlight.get(effect.attempt) + inFlight.delete(effect.attempt) + + if (!handle) return + clearTimeout(handle.timer) + handle.controller.abort() + + return + } + + // SAFETY: only `disposed`, `sessions.pruned` and request results without an answer dispatch without a session, + // and the machine answers those with `abort` alone. + if (!session) return + const store = ctx.stores.chats(session) + + if (effect.type === "persist-question") { + // Desktop storage loads asynchronously; a write before load queues and lands once it loads, while the machine + // already shows the chat. The session's store lives while its tab stays open. + store.update((draft) => { + draft.chats.push({ + id: effect.id, + question: effect.question, + created: effect.created, + agent: effect.agent, + model: effect.model, + }) + }) + + return + } + + if (effect.type === "open-tab") { + ctx.layout.open(`${ctx.id}:${effect.id}`, session, { tab: "select" }) + + return + } + + if (effect.type === "persist-answer") { + store.update((draft) => { + const chat = draft.chats.find((item) => item.id === effect.id) + + if (!chat) return + chat.answer = effect.answer + chat.duration = effect.duration + + if (effect.agent) chat.agent = effect.agent + + if (effect.model) chat.model = effect.model + }) + + return + } + + if (effect.type === "delete-chat") { + store.update((draft) => { + draft.chats = draft.chats.filter((item) => item.id !== effect.id) + }) + + return + } + + generate(session, effect) } // In-flight questions keep running when the user leaves their session and answer into its store; only a reload, - // closing the question's tab or disposing the plugin abandons one, which leaves it retryable. - onCleanup(() => Array.from(controllers.keys()).forEach(stop)) + // closing the question's tab or session, or disposing the plugin abandons one, which leaves it retryable. + const generate = (session: MountedSession, effect: Extract) => { + const key = session.key + const result = { sessionKey: key, id: effect.id, attempt: effect.attempt } + const controller = new AbortController() - const entry = (session: MountedSession, id: string) => saved(session).value?.chats.find((item) => item.id === id) - const pending = (id: string) => requests.pending.includes(id) + inFlight.set(effect.attempt, { + controller, + timer: setTimeout(() => dispatch({ type: "generate.timed-out", ...result }), effect.timeout), + }) + + session.server.client.session + .generate( + { sessionID: session.id, prompt: [instructions, effect.question].join("\n\n") }, + { signal: AbortSignal.any([ctx.signal, controller.signal]) }, + ) + .then( + (reply) => { + if (ctx.signal.aborted) return + dispatch({ type: "generate.succeeded", ...result, text: reply.text, now: Date.now() }, session) + }, + (cause) => { + if (ctx.signal.aborted) return + const message = cause instanceof Error ? cause.message : String(cause) + dispatch({ type: "generate.failed", ...result, message }) + }, + ) + } + + onCleanup(() => dispatch({ type: "disposed" })) const selection = (session: MountedSession) => { const info = session.id ? session.server.data.session.get(session.id) : undefined @@ -51,47 +153,6 @@ export function createBtw(ctx: SetupContext) { } } - // Takes the question rather than reading it back, because a new chat's write may still wait for the store to load. - const generate = (session: MountedSession, id: string, question: string) => { - if (pending(id)) return - - const store = saved(session) - const controller = new AbortController() - const started = Date.now() - const current = selection(session) - controllers.set(id, controller) - setRequests("pending", (list) => [...list, id]) - - return ( - session.server.client.session - .generate( - { sessionID: session.id, prompt: [instructions, question].join("\n\n") }, - { signal: controller.signal }, - ) - .then((result) => { - if (ctx.signal.aborted || controller.signal.aborted) return - const duration = Math.max(0, Date.now() - started) - - store.update((draft) => { - const chat = draft.chats.find((item) => item.id === id) - - if (!chat) return - chat.answer = result.text.trim() - chat.duration = duration - - if (current.agent) chat.agent = current.agent - - if (current.model) chat.model = current.model - }) - }) - // A missing answer is the retry state; no transient error flag is stored. - .catch(() => undefined) - .finally(() => { - if (controllers.get(id) === controller) stop(id) - }) - ) - } - const ask = (value?: string) => { const question = value?.trim() @@ -105,52 +166,34 @@ export function createBtw(ctx: SetupContext) { if (!session?.id) return - const id = uuid() - const created = Date.now() - const current = selection(session) - - // Desktop storage loads asynchronously. A write before load queues and lands in the same batch that marks the - // store ready, and the panel keeps a tab hidden until then, so the chat and its tab need not wait. The session's - // store lives while its tab stays open, so leaving the session first still records the chat. - batch(() => { - saved(session).update((draft) => { - draft.chats.push({ - id, - question, - created, - agent: current.agent, - model: current.model, - }) - }) - ctx.layout.open(`${ctx.id}:${id}`, session, { tab: "select" }) - }) - void generate(session, id, question) + dispatch( + { type: "chat.asked", sessionKey: session.key, id: uuid(), question, created: Date.now(), ...selection(session) }, + session, + ) } return { ask, - saved, - pending, - entry, - question: (session: MountedSession, id: string) => entry(session, id)?.question, - answer: (session: MountedSession, id: string) => entry(session, id)?.answer, - error: (session: MountedSession, id: string) => { - const item = entry(session, id) - - return !!item && item.answer === undefined && !pending(id) - }, + tab: (session: MountedSession, id: string) => btwTabView(state(), saved(session), session.key, id), + view: (session: MountedSession, id: string) => btwChatView(state(), saved(session), session.key, id), retry: (session: MountedSession, id: string) => { - const question = entry(session, id)?.question - - if (question) void generate(session, id, question) + dispatch( + { + type: "chat.retried", + sessionKey: session.key, + id, + now: Date.now(), + saved: saved(session)?.find((item) => item.id === id), + ...selection(session), + }, + session, + ) }, /** Closing a tab forgets its question and answer. */ - remove: (session: MountedSession, id: string) => { - stop(id) - saved(session).update((draft) => { - draft.chats = draft.chats.filter((item) => item.id !== id) - }) - }, + remove: (session: MountedSession, id: string) => + dispatch({ type: "chat.removed", sessionKey: session.key, id }, session), + /** Ends the requests of sessions whose shell tab closed and forgets their chats. */ + prune: (keep: ReadonlySet) => dispatch({ type: "sessions.pruned", keep }), } } diff --git a/packages/gui-plugins/src/btw/panel.tsx b/packages/gui-plugins/src/btw/panel.tsx index cd5bdaa261b..3b9529daba7 100644 --- a/packages/gui-plugins/src/btw/panel.tsx +++ b/packages/gui-plugins/src/btw/panel.tsx @@ -9,15 +9,48 @@ import { Tooltip } from "@opencode/ui/tooltip" import { showToast } from "@opencode/ui/toast" import { Markdown } from "@opencode/session-ui/markdown" import { usePlugin, type MountedSession } from "@opencode/plugin/gui" +import type { BtwFailure } from "./machine" import type { BtwModel } from "./model" export default function SessionBtwPanel(props: { btw: BtwModel; session: MountedSession; id: string }) { const ctx = usePlugin() const i18n = useI18n() const system = ctx.system - const chat = () => props.btw.entry(props.session, props.id) + const view = createMemo(() => props.btw.view(props.session, props.id)) + + const chat = createMemo(() => { + const current = view() + + return current.phase === "loading" || current.phase === "missing" ? undefined : current + }) + + const running = createMemo(() => view().phase === "running") + + const failure = createMemo(() => { + const current = view() + + return current.phase === "failed" ? current.failure : undefined + }) + + const answered = createMemo(() => { + const current = view() + + return current.phase === "answered" ? current : undefined + }) + const question = () => chat()?.question - const answer = () => chat()?.answer + const answer = () => answered()?.answer + + const failureDetail = (value: BtwFailure) => { + if (value.reason === "empty") return ctx.t("error.empty") + + if (value.reason === "timeout") return ctx.t("error.timeout") + + if (value.reason === "error") return value.message + + return undefined + } + // New primitive tokens clear the copied mark when text changes without being proxied by the store. const shownQuestion = createMemo(on(question, () => Symbol())) const shownAnswer = createMemo(on(answer, () => Symbol())) @@ -63,7 +96,7 @@ export default function SessionBtwPanel(props: { btw: BtwModel; session: Mounted }) const durationLabel = createMemo(() => { - const ms = chat()?.duration + const ms = answered()?.duration if (ms === undefined || !(ms >= 0)) return "" const total = Math.round(ms / 1000) @@ -147,7 +180,7 @@ export default function SessionBtwPanel(props: { btw: BtwModel; session: Mounted - +
- -
-
{ctx.t("error")}
- -
+ + {(current) => ( +
+
+
{ctx.t("error")}
+ + {(detail) =>
{detail()}
} +
+
+ +
+ )}
- - {(text) => ( + + {(reply) => (
- +
= (ctx) => { @@ -16,15 +17,22 @@ const setup: Plugin.Setup = (ctx) => { // Tab objects per session, reused so strip updates and session switches do not rebuild a trigger. const tabs = new Map>() + const title = (view: BtwTabView) => (view.status === "ready" ? view.question : ctx.t("tab.title")) - // Drops the tab objects of sessions whose shell tab closed. + // Drops the tab objects and chats of sessions whose shell tab closed. createKeyed( () => [...sessions.list().map((session) => session.key), sessions.current()?.key].join("\u0000"), () => { - const keep = new Set([...sessions.list().map((session) => session.key), sessions.current()?.key]) + const keep = new Set( + [...sessions.list().map((session) => session.key), sessions.current()?.key].filter( + (key): key is string => key !== undefined, + ), + ) + tabs.forEach((_cache, key) => { if (!keep.has(key)) tabs.delete(key) }) + btw.prune(keep) }, ) @@ -50,17 +58,17 @@ const setup: Plugin.Setup = (ctx) => { transient: true, // Layouts saved before plugins store the tab as "btw"; as a panel key it leaves like any unlisted transient tab. legacy: { btw: "main" }, - // One tab per stored question. Until the session's store loads, its tabs stay listed but hidden so restore keeps - // them; afterwards a tab without a stored question leaves the strip. + // One tab per question. A restored tab stays listed but hidden until the session's store loads so restore keeps + // it; afterwards a tab without a stored question leaves the strip. A question asked here shows at once. list: (input) => { if (input.open.length === 0) return [] - const saved = btw.saved(input.session) - const cache = tabs.get(input.session.key) ?? new Map() - tabs.set(input.session.key, cache) + const session = input.session + const cache = tabs.get(session.key) ?? new Map() + tabs.set(session.key, cache) return input.open.flatMap((id) => { - if (saved.ready() && !saved.value?.chats.some((item) => item.id === id)) return [] + if (btw.tab(session, id).status === "missing") return [] const existing = cache.get(id) @@ -69,15 +77,15 @@ const setup: Plugin.Setup = (ctx) => { const tab: PanelTab = { id, get title() { - return saved.value?.chats.find((item) => item.id === id)?.question ?? ctx.t("tab.title") + return title(btw.tab(session, id)) }, get hidden() { - return !saved.ready() + return btw.tab(session, id).status === "loading" }, label: (state) => (
- {btw.question(state.session, id) ?? ctx.t("tab.title")} + {title(btw.tab(state.session, id))}
), }