| 1 | import type { Item, State } from "./useController"; |
| 2 | import type { HistoryMessage, WireCompletionSummary } from "./types"; |
| 3 | import { t } from "./i18n"; |
| 4 | import { completionSummaryPresentation, normalizeCompletionSummary, sessionQualityFloor } from "./completionSummary"; |
| 5 | import { mergeTurnResult } from "./turnResult"; |
| 6 | |
| 7 | function lastUserIndex(items: Item[]): number { |
| 8 | for (let i = items.length - 1; i >= 0; i--) if (items[i].kind === "user") return i; |
| 9 | return -1; |
| 10 | } |
| 11 | |
| 12 | export function historicalResultNotice(message: HistoryMessage, id: string): Extract<Item, { kind: "notice" }> | undefined { |
| 13 | const summary = normalizeCompletionSummary(mergeTurnResult(message.completionSummary, message.completionReceipt, message.turnId, message.checkpointTurn)); |
| 14 | const presentation = completionSummaryPresentation(summary, "standard", t); |
| 15 | return presentation ? { kind: "notice", id, variant: "completion", action: "open_changes", level: presentation.level, title: presentation.title, text: presentation.body, completionSummary: summary } : undefined; |
| 16 | } |
| 17 | |
| 18 | /** Replace only the current user turn's result, keeping its mounted identity. */ |
| 19 | export function withTurnResult(s: State, summary: WireCompletionSummary): State { |
| 20 | const boundary = lastUserIndex(s.items); |
| 21 | const index = s.items.findIndex((item, i) => i > boundary && item.kind === "notice" && item.variant === "completion"); |
| 22 | const presentation = completionSummaryPresentation(summary, sessionQualityFloor(s.meta), t); |
| 23 | if (!presentation) return { ...s, completionSummary: summary, items: index < 0 ? s.items : s.items.filter((_, i) => i !== index) }; |
| 24 | const id = index < 0 ? `q${s.seq}` : s.items[index].id; |
| 25 | const notice: Item = { kind: "notice", id, variant: "completion", action: "open_changes", level: presentation.level, title: presentation.title, text: presentation.body, completionSummary: summary }; |
| 26 | const items = [...s.items]; |
| 27 | if (index < 0) items.push(notice); else items[index] = notice; |
| 28 | return { ...s, completionSummary: summary, items, seq: s.seq + Number(index < 0) }; |
| 29 | } |
| 30 | |
| 31 | export function withRunningChecks(s: State): State { |
| 32 | const boundary = lastUserIndex(s.items); |
| 33 | const tools = s.items.slice(boundary + 1).filter((item): item is Extract<Item, { kind: "tool" }> => item.kind === "tool" && item.verifying === true && item.status === "running"); |
| 34 | if (tools.length === 0 && !s.completionSummary?.checking) return s; |
| 35 | const liveChecks = tools.map(tool => { |
| 36 | let command = tool.name; |
| 37 | try { const args = JSON.parse(tool.args); command = typeof args.command === "string" ? args.command : command; } catch { /* incomplete legacy arguments */ } |
| 38 | return { toolCallId: tool.id, command, output: tool.output?.slice(-65536) }; |
| 39 | }); |
| 40 | const summary = mergeTurnResult(s.completionSummary, undefined, s.activeTurnId); |
| 41 | return withTurnResult(s, { ...summary, checking: tools.length > 0, liveChecks }); |
| 42 | } |
| 43 |