fix(app): make blank /btw answers retryable (#61883)

This commit is contained in:
Luke Parker
2026-11-04 20:57:31 -06:00
committed by GitHub
Unverified
parent 66b25c2bfc
commit 6540d9b01c
7 changed files with 864 additions and 119 deletions
@@ -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<void>()
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([])
})
+2
View File
@@ -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",
}
@@ -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))
})
})
+341
View File
@@ -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<string, ReadonlyMap<string, BtwChat>>
}
| { readonly status: "disposed" }
type ActiveState = Extract<BtwState, { status: "active" }>
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<string> }
| { 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<BtwEvent, { type: "chat.asked" }>) {
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<BtwEvent, { type: "chat.retried" }>) {
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<string>) {
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<string, BtwChat>): 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 }
}
+136 -93
View File
@@ -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<typeof Btw>) {
const controllers = new Map<string, AbortController>()
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<number, { controller: AbortController; timer: ReturnType<typeof setTimeout> }>()
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<BtwEffect, { type: "generate" }>) => {
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<typeof Btw>) {
}
}
// 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<typeof Btw>) {
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<string>) => dispatch({ type: "sessions.pruned", keep }),
}
}
+54 -14
View File
@@ -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
</div>
<Switch>
<Match when={props.btw.pending(props.id)}>
<Match when={running()}>
<div
data-component="session-working"
role="status"
@@ -156,19 +189,26 @@ export default function SessionBtwPanel(props: { btw: BtwModel; session: Mounted
<TextShimmer text={ctx.t("session.timeline.working")} active />
</div>
</Match>
<Match when={props.btw.error(props.session, props.id)}>
<div class="flex flex-col items-start gap-3 pt-4">
<div class="text-13-regular text-text-weak">{ctx.t("error")}</div>
<Button size="small" variant="outline" onClick={() => props.btw.retry(props.session, props.id)}>
{ctx.t("retry")}
</Button>
</div>
<Match when={failure()}>
{(current) => (
<div class="flex flex-col items-start gap-3 pt-4">
<div class="flex flex-col gap-1">
<div class="text-13-regular text-text-weak">{ctx.t("error")}</div>
<Show when={failureDetail(current())}>
{(detail) => <div class="text-12-regular text-text-weak break-words">{detail()}</div>}
</Show>
</div>
<Button size="small" variant="outline" onClick={() => props.btw.retry(props.session, props.id)}>
{ctx.t("retry")}
</Button>
</div>
)}
</Match>
<Match when={answer()}>
{(text) => (
<Match when={answered()}>
{(reply) => (
<div data-component="text-part">
<div data-slot="text-part-body">
<Markdown text={text()} class="text-14-regular" />
<Markdown text={reply().answer} class="text-14-regular" />
</div>
<div data-slot="text-part-copy-wrapper">
<Tooltip
+19 -11
View File
@@ -3,6 +3,7 @@ import { Icon } from "@opencode/ui/icon"
import { Command, createKeyed, type PanelTab, type Plugin } from "@opencode/plugin/gui"
import { BuiltinPanel, onIdle } from "@opencode/plugin/gui/builtin"
import type Btw from "./index"
import type { BtwTabView } from "./machine"
import { createBtw } from "./model"
const setup: Plugin.Setup<typeof Btw> = (ctx) => {
@@ -16,15 +17,22 @@ const setup: Plugin.Setup<typeof Btw> = (ctx) => {
// Tab objects per session, reused so strip updates and session switches do not rebuild a trigger.
const tabs = new Map<string, Map<string, PanelTab>>()
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<typeof Btw> = (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<string, PanelTab>()
tabs.set(input.session.key, cache)
const session = input.session
const cache = tabs.get(session.key) ?? new Map<string, PanelTab>()
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<typeof Btw> = (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) => (
<div class="flex min-w-0 items-center gap-1.5">
<Icon name="comment" class="shrink-0" />
<span class="truncate">{btw.question(state.session, id) ?? ctx.t("tab.title")}</span>
<span class="truncate">{title(btw.tab(state.session, id))}</span>
</div>
),
}