diff --git a/web/scripts/transcript.test.mjs b/web/scripts/transcript.test.mjs index 0ce3f5918..b442183b6 100644 --- a/web/scripts/transcript.test.mjs +++ b/web/scripts/transcript.test.mjs @@ -911,3 +911,62 @@ test('user blocks keep mail sender metadata and do not carry user read receipts' assert.equal(agent.turnType, 'agent'); assert.equal(transcript.receipt, null); }); + +test('live stream notes render in place, ignore agent progress, and deduplicate by entry_id', async () => { + api.reset(conversation()); + const transcript = new Transcript('session'); + await transcript.load(); + transcript.attach(); + stream.emit({ + generation: 'g1', + cursor: 1, + events: [ + { + type: 'note', + sequence: 1, + data: { entry_id: 'n-agent', origin: 'agent', text: 'checking files' }, + }, + { + type: 'note', + sequence: 2, + data: { + entry_id: 'n-1', + origin: 'daemon', + text: 'python kernel reattached; its variables and jobs carried on', + }, + }, + { + type: 'note', + sequence: 3, + data: { + entry_id: 'n-1', + origin: 'daemon', + text: 'python kernel reattached; its variables and jobs carried on', + }, + }, + ], + }); + assert.deepEqual( + transcript.blocks.map((b) => b.kind), + ['user', 'assistant', 'end', 'note'], + ); + assert.equal( + transcript.blocks.at(-1).text, + 'python kernel reattached; its variables and jobs carried on', + ); + api.answerHistory(() => + Promise.resolve({ + items: [ + message('user', 3, 3, 'next', 'model-two'), + message('assistant', 4, 4, 'done', 'model-two'), + ], + newer: null, + }), + ); + await transcript.poll(); + assert.deepEqual( + transcript.blocks.map((b) => b.kind), + ['user', 'assistant', 'end', 'note', 'user', 'assistant', 'end'], + ); + transcript.detach(); +}); diff --git a/web/src/lib/transcript.svelte.ts b/web/src/lib/transcript.svelte.ts index 623569461..37257343e 100644 --- a/web/src/lib/transcript.svelte.ts +++ b/web/src/lib/transcript.svelte.ts @@ -155,6 +155,13 @@ interface Live { text: string; } +// stream notes stay where they arrived across later turns, and after is null until initial load finishes +interface LiveNote { + id: string; + text: string; + after: number | null; +} + /** A tool call running now, as its progress reports it. */ interface Progress { id: string; @@ -187,6 +194,7 @@ interface Read { speaker: string; called: Set; durable: Set; + notesFrom: number; } /** @@ -561,6 +569,7 @@ export class Transcript { compaction = $state(null); streaming = $state(false); live = $state.raw([]); + #notes = $state.raw([]); progress = $state.raw([]); shellCommands = $state.raw([]); /** What waits in the session's inbox, in the order it will be read. */ @@ -660,6 +669,12 @@ export class Transcript { } catch (error) { this.error = String((error as Error).message ?? error); } finally { + const tail = this.entries.at(-1)?.position ?? 0; + if (this.#notes.some((note) => note.after === null)) { + this.#notes = this.#notes.map((note) => + note.after === null ? { ...note, after: tail } : note, + ); + } this.loading = false; } this.#readFailures().catch(() => {}); @@ -689,7 +704,13 @@ export class Transcript { .filter((entry) => !this.#ids.has(entry.id)) .sort(byPosition); for (const entry of added) this.#ids.add(entry.id); - if (added.length) this.entries = joined(this.entries, added); + if (added.length) { + this.entries = joined(this.entries, added); + const addedIds = new Set(added.map((entry) => entry.id)); + if (this.#notes.some((note) => addedIds.has(note.id))) { + this.#notes = this.#notes.filter((note) => !addedIds.has(note.id)); + } + } // Initial hydration may finish after a reset that had no loaded rows yet. if (this.#stop && added.some((entry) => entry.kind === 'shell_command')) void this.#refreshShells(); @@ -890,13 +911,33 @@ export class Transcript { ); break; case 'message': + this.#commit(null); + break; case 'note': + this.#pushNote(data); this.#commit(null); break; } } } + #pushNote(data: any) { + const id = typeof data?.entry_id === 'string' ? data.entry_id : ''; + const origin = typeof data?.origin === 'string' ? data.origin : ''; + const text = typeof data?.text === 'string' ? data.text : ''; + if (origin === 'agent' || !text.trim()) return; + if (id && (this.#ids.has(id) || this.#notes.some((note) => note.id === id))) + return; + this.#notes = [ + ...this.#notes, + { + id: id || `note:${this.#notes.length}`, + text, + after: this.loading ? null : (this.entries.at(-1)?.position ?? 0), + }, + ]; + } + /** * Reads what committed, then drops the live output it replaces, so a reply * never blinks out between its last delta and its history entry. @@ -1330,6 +1371,16 @@ export class Transcript { const liveShells = new Map( this.shellCommands.map((command) => [command.id, command]), ); + let noteAt = 0; + const notes = this.#notes; + const flushNotes = (beforePosition: number) => { + while (noteAt < notes.length) { + const note = notes[noteAt]; + if (note.after === null || note.after >= beforePosition) break; + blocks.push({ kind: 'note', key: `note:${note.id}`, text: note.text }); + noteAt++; + } + }; for (const entry of this.entries) { // Mid-turn receipts belong to the surrounding turn. Loose idle receipts // still sign it off before shell time starts. @@ -1338,6 +1389,7 @@ export class Transcript { blocks.push(...this.#ending(turn)); turn = null; } + flushNotes(entry.position); speaker = 'you'; const part = entry.content.find( (item) => item.kind === 'json' && item.field === 'shell_command', @@ -1371,6 +1423,7 @@ export class Transcript { text: '', }; } + flushNotes(entry.position); turn.last = Math.max(turn.last, at(entry)); switch (entry.kind) { case 'user': { @@ -1484,7 +1537,7 @@ export class Transcript { block.kind === 'shell' ? [block.command.id] : [], ), ); - return { blocks, turn, speaker, called, durable }; + return { blocks, turn, speaker, called, durable, notesFrom: noteAt }; } /** A turn's sign-off, once the next one starts or the session rests: its failure, then a face. */ @@ -1517,12 +1570,14 @@ export class Transcript { } /** What follows history: the open turn's sign-off, live output, running calls and shell commands, and your sends. */ - #tail({ turn, speaker, called, durable }: Read): Block[] { + #tail({ turn, speaker, called, durable, notesFrom }: Read): Block[] { const blocks: Block[] = []; // Text still live has not committed yet: signing off now would put the // sign-off above the answer, timed without it. if (!this.running && !this.live.some((item) => item.text.trim())) blocks.push(...this.#ending(turn)); + for (const note of this.#notes.slice(notesFrom)) + blocks.push({ kind: 'note', key: `note:${note.id}`, text: note.text }); for (const item of this.live) { if (!item.text.trim()) continue; if (item.kind === 'thinking')