From e108fdc79203b93ee42677640d2d9a7749b23be6 Mon Sep 17 00:00:00 2001 From: Mohamed Boudra Date: Wed, 25 Nov 2026 20:07:08 +0700 Subject: [PATCH] fix(app): keep the turn footer on plugin-transformed assistant replies (#10795) * fix(app): preserve turn footer on plugin-transformed assistant replies Retain sourceMessageId and sourceText when timeline projection replaces an assistant_message with plugin items, and recognize projected assistant replies across turn timing, footer layout, fork boundary resolution, and turn copy (deduplicating split cards by sourceId). No plugin SDK changes. Closes #10438 Co-Authored-By: Claude Code * refactor(app): keep the replaced row on plugin timeline cards Projected plugin rows copied fields from the row they replaced one at a time (sourceId, sourceKind, sourceMessageId, sourceText), and each copy missed a footer input: turn stats from #10496 were still dropped for a transformed reply. Projection now keeps the replaced row as `source`, and `getAssistantReply` reads the reply through it for turn folding, timing, copy, fork, and turn stats. Refs #10438 Co-authored-by: Brandon Elliott * docs(plugins): note that a replaced reply keeps its turn footer Refs #10438 * test(app): check the turn duration under a plugin-transformed reply Refs #10438 --------- Co-authored-by: Brandon Elliott Co-authored-by: Claude Code --- docs/plugins.md | 7 +++ .../app/e2e/browser/plugin-timeline.spec.ts | 29 ++++++++- packages/app/src/agent-stream/layout.test.ts | 38 ++++++++++++ packages/app/src/agent-stream/layout.ts | 8 +-- .../src/agent-stream/render-strategy.test.ts | 40 ++++++++++++- packages/app/src/agent-stream/strategy.ts | 10 +++- .../src/agent-stream/turn-boundary.test.ts | 59 +++++++++++++++++++ .../app/src/agent-stream/turn-boundary.ts | 14 ++--- .../agent-stream/turn-collapse/model.test.ts | 11 ++-- .../src/agent-stream/turn-collapse/model.ts | 14 ++--- .../agent-stream/turn-footer.browser.test.tsx | 41 +++++++++++++ packages/app/src/agent-stream/turn-footer.tsx | 4 +- .../src/plugins/timeline/projection.test.ts | 53 ++++++++++++++++- .../app/src/plugins/timeline/projection.ts | 3 +- packages/app/src/timeline/turn-time.test.ts | 34 +++++++++++ packages/app/src/timeline/turn-time.ts | 4 +- packages/app/src/types/stream.ts | 21 +++++-- 17 files changed, 345 insertions(+), 45 deletions(-) diff --git a/docs/plugins.md b/docs/plugins.md index 4fd8d84cc9..f3b16e98bc 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -883,6 +883,13 @@ its Zod schema, and mounts the component inside the normal plugin runtime and er optional output `id` distinguishes several stable replacements from the same source item; its output index is the default. +Each replacement keeps a reference to the source row (`source` on the projected stream item), and +turn logic reads a replaced reply through it. A reply split into several cards stays one reply: turn +folding keeps every card visible, and the turn footer hangs under the last card, copies the original +reply text once, forks at its message, and shows its duration and stats. Read the reply with +`getAssistantReply` (`packages/app/src/types/stream.ts`) instead of checking `kind === +"assistant_message"`. + Transformers run synchronously and must be deterministic. When several transformers match, the first one that returns a result owns that source item. Plugin and registration ordering is stable. See `plugin-examples/timeline-items` for the complete contract. diff --git a/packages/app/e2e/browser/plugin-timeline.spec.ts b/packages/app/e2e/browser/plugin-timeline.spec.ts index aacd7c187b..d3719df65f 100644 --- a/packages/app/e2e/browser/plugin-timeline.spec.ts +++ b/packages/app/e2e/browser/plugin-timeline.spec.ts @@ -1,6 +1,12 @@ -import { test } from "../support/fixtures"; +import { expect, test } from "../support/fixtures"; +import { + expectChatHistoryAttachment, + forkMostRecentAssistantTurnToNewTab, + observeForkAttachment, +} from "../support/helpers/assistant-fork"; import { installDaemonWebSocketGate } from "../support/helpers/daemon-websocket-gate"; import { + ASSISTANT_TEXT, withTimelinePlugin, viewInlineThinkingExample, requestPluginTimeline, @@ -55,3 +61,24 @@ test("inline-thinking example renders a streamed thought through host UI", async }, info) => { await viewInlineThinkingExample(page, info); }); + +test("plugin-transformed assistant reply keeps copy, fork, and turn duration in its footer", async ({ + page, +}, info) => { + const forkAttachment = observeForkAttachment(page); + await withTimelinePlugin(page, info, "assistant", async (agent) => { + await requestPluginTimeline(agent); + await agent.client.waitForFinish(agent.agentId, 30_000); + await expectWholeCompletedCard(page); + + const footer = page.getByTestId("turn-footer").last(); + await expect(footer).toBeVisible(); + await expect(footer.getByRole("button", { name: "Copy turn" })).toBeVisible(); + await expect(footer.getByRole("button", { name: "Fork chat from here" })).toBeVisible(); + await expect(footer.getByText(/^Worked for /)).toBeVisible(); + + await forkMostRecentAssistantTurnToNewTab(page); + await expectChatHistoryAttachment(page); + expect(await forkAttachment.waitForText()).toContain(ASSISTANT_TEXT); + }); +}); diff --git a/packages/app/src/agent-stream/layout.test.ts b/packages/app/src/agent-stream/layout.test.ts index e41c05dec9..fd2a35491d 100644 --- a/packages/app/src/agent-stream/layout.test.ts +++ b/packages/app/src/agent-stream/layout.test.ts @@ -1,5 +1,7 @@ import { describe, expect, it } from "vitest"; import type { TurnTiming } from "@/timeline/turn-time"; +import type { TimelineItemTransform } from "@/plugins/timeline"; +import { projectPluginTimelineItems } from "@/plugins/timeline/projection"; import type { StreamItem } from "@/types/stream"; import { orderHeadForStreamRenderStrategy, @@ -578,4 +580,40 @@ describe("layoutStream", () => { expect(footerOwners(layout)).toEqual([]); }, ); + + it.each(["web", "android"] as const)( + "attaches the turn footer to the final card of a plugin-split reply on %s", + (platform) => { + const metrics = [{ label: "Model", value: "opus" }]; + const first = { ...assistantMessage("a1", 2), turnMetrics: [{ label: "Steps", value: "1" }] }; + const latest = { ...assistantMessage("a2", 4), turnMetrics: metrics }; + const splitIntoCards: TimelineItemTransform = ({ item, sourceId }) => + item.type !== "assistant_message" + ? undefined + : ["text", "card"].map((part) => ({ + type: "plugin", + id: `${sourceId}/${part}`, + pluginId: "genui", + kind: `genui-${part}`, + version: 1, + data: {}, + })); + const tail = projectPluginTimelineItems( + [userMessage("u1", 1), first, userMessage("u2", 3), latest], + splitIntoCards, + ); + + const layout = layoutFor({ + platform, + tail, + timingIds: ["genui/a1/card", "genui/a2/card"], + }); + + expect(footerOwners(layout)).toEqual(["genui/a1/card", "genui/a2/card"]); + expect(findLayoutItem(layout, "genui/a1/card").completedFooter?.turnMetrics).toBe( + first.turnMetrics, + ); + expect(layout.auxiliaryTurnFooter?.turnMetrics).toBe(metrics); + }, + ); }); diff --git a/packages/app/src/agent-stream/layout.ts b/packages/app/src/agent-stream/layout.ts index e1918ddc4f..7b459cb9d2 100644 --- a/packages/app/src/agent-stream/layout.ts +++ b/packages/app/src/agent-stream/layout.ts @@ -1,6 +1,6 @@ import type { AgentMetric } from "@getpaseo/protocol/agent-types"; import type { TurnTiming } from "@/timeline/turn-time"; -import type { StreamItem } from "@/types/stream"; +import { getAssistantReply, type StreamItem } from "@/types/stream"; import { getAssistantMessageSpacing, getGapBetweenStreamItems } from "./spacing"; import type { StreamFrameChildOrder, StreamStrategy } from "./strategy"; import { continuesResponse, continuesTurn, isResponseBoundary } from "./turn-membership"; @@ -72,9 +72,9 @@ function createTurnFooterHost(input: { timingByItemId: Map; }): TurnFooterHost { const turnMetrics = - input.item.kind === "assistant_message" || input.item.kind === "tool_call" + input.item.kind === "tool_call" ? input.item.turnMetrics - : undefined; + : getAssistantReply(input.item)?.turnMetrics; return { itemId: input.item.id, items: input.items, @@ -109,7 +109,7 @@ function findTurnFooterSource(input: { if (!item || (laterItem && !continuesResponse(item, laterItem))) { return toolFallback; } - if (item.kind === "assistant_message") { + if (getAssistantReply(item)) { return { item, items, index }; } laterItem = item; diff --git a/packages/app/src/agent-stream/render-strategy.test.ts b/packages/app/src/agent-stream/render-strategy.test.ts index 5e5ab7f7a2..14a6418956 100644 --- a/packages/app/src/agent-stream/render-strategy.test.ts +++ b/packages/app/src/agent-stream/render-strategy.test.ts @@ -28,7 +28,11 @@ function userMessage(id: string, text: string, seed: number): StreamItem { }; } -function assistantMessage(id: string, text: string, seed: number): StreamItem { +function assistantMessage( + id: string, + text: string, + seed: number, +): Extract { return { kind: "assistant_message", id, @@ -243,4 +247,38 @@ describe("layout strategy edges", () => { "content-then-footer", ); }); + + it.each(PLATFORMS)("copies a reply split into plugin cards once on %s", (platform) => { + const strategy = strategyFor(platform); + const splitReply = assistantMessage("a2", 'Summary\n\n```ui\nCard("Hi", [])\n```', 3); + const card = (part: string): StreamItem => ({ + kind: "plugin", + id: `genui/a2/${part}`, + source: splitReply, + timestamp: splitReply.timestamp, + pluginId: "genui", + pluginItemId: `a2/${part}`, + itemKind: `genui-${part}`, + version: 1, + data: {}, + }); + const items = orderTailForStreamRenderStrategy({ + strategy, + streamItems: [ + userMessage("u1", "prompt", 1), + assistantMessage("a1", "Before card", 2), + card("text"), + card("card"), + ], + }); + const startIndex = items.findIndex((item) => item.id === "genui/a2/card"); + + expect( + collectAssistantResponseContentForStreamRenderStrategy({ + strategy, + items, + startIndex, + }), + ).toBe(`Before card\n\n${splitReply.text}`); + }); }); diff --git a/packages/app/src/agent-stream/strategy.ts b/packages/app/src/agent-stream/strategy.ts index 7507f3442b..6a5db45c05 100644 --- a/packages/app/src/agent-stream/strategy.ts +++ b/packages/app/src/agent-stream/strategy.ts @@ -1,6 +1,6 @@ import type { ComponentType, ReactElement, ReactNode, RefObject } from "react"; import type { StyleProp, ViewStyle } from "react-native"; -import type { StreamItem } from "@/types/stream"; +import { getAssistantReply, type StreamItem } from "@/types/stream"; import type { ReadingRowReplacements } from "./reading-anchor"; import { continuesResponse } from "./turn-membership"; import type { StreamHistoryBoundary, StreamRenderSegments } from "./model"; @@ -185,6 +185,7 @@ export function createStreamStrategy(config: StreamStrategyConfig): StreamStrate }, collectAssistantResponseContent: (items, startIndex) => { const messages: string[] = []; + const seenReplies = new Set(); let laterItem: StreamItem | null = null; for ( let index = startIndex; @@ -195,8 +196,11 @@ export function createStreamStrategy(config: StreamStrategyConfig): StreamStrate if (!currentItem || (laterItem && !continuesResponse(currentItem, laterItem))) { break; } - if (currentItem.kind === "assistant_message") { - messages.push(currentItem.text); + // A transformer can split one reply into several cards; copy the reply once. + const reply = getAssistantReply(currentItem); + if (reply && !seenReplies.has(reply)) { + seenReplies.add(reply); + messages.push(reply.text); } laterItem = currentItem; } diff --git a/packages/app/src/agent-stream/turn-boundary.test.ts b/packages/app/src/agent-stream/turn-boundary.test.ts index 14e5ea776d..9cfc14a622 100644 --- a/packages/app/src/agent-stream/turn-boundary.test.ts +++ b/packages/app/src/agent-stream/turn-boundary.test.ts @@ -112,4 +112,63 @@ describe("resolveAssistantTurnForkBoundary", () => { }), ).toBeUndefined(); }); + + it("resolves a fork boundary from the reply a plugin card replaced", () => { + const reply = { + ...assistantMessage("assistant-1", 2, "msg-assistant-1"), + timelineCursor: { epoch: "timeline-1", seq: 42 }, + }; + const card: StreamItem = { + kind: "plugin", + id: "genui/assistant-1/1", + source: reply, + timelineCursor: reply.timelineCursor, + timestamp: reply.timestamp, + pluginId: "genui", + pluginItemId: "assistant-1/1", + itemKind: "genui-block", + version: 1, + data: {}, + }; + + expect( + resolveAssistantTurnForkBoundary({ + items: [userMessage("user-1", 1), card], + startIndex: 1, + supportsTimelineCursor: true, + }), + ).toEqual({ + boundaryCursor: { epoch: "timeline-1", seq: 42 }, + boundaryMessageId: "msg-assistant-1", + }); + expect( + resolveAssistantTurnForkBoundary({ + items: [userMessage("user-1", 1), card], + startIndex: 1, + supportsTimelineCursor: false, + }), + ).toEqual({ boundaryMessageId: "msg-assistant-1" }); + }); + + it("ignores plugin cards that did not replace a reply", () => { + const appended: StreamItem = { + kind: "plugin", + id: "plugin/card-1", + timelineCursor: { epoch: "timeline-1", seq: 42 }, + timestamp: timestamp(2), + pluginId: "plugin", + pluginItemId: "card-1", + itemKind: "card", + version: 1, + data: {}, + }; + + expect( + resolveAssistantTurnForkBoundary({ + items: [userMessage("user-1", 1), appended], + startIndex: 1, + supportsTimelineCursor: true, + }), + ).toBeUndefined(); + }); }); diff --git a/packages/app/src/agent-stream/turn-boundary.ts b/packages/app/src/agent-stream/turn-boundary.ts index f7cbe8c00f..659ff4a254 100644 --- a/packages/app/src/agent-stream/turn-boundary.ts +++ b/packages/app/src/agent-stream/turn-boundary.ts @@ -1,4 +1,4 @@ -import type { StreamItem, TimelinePosition } from "@/types/stream"; +import { getAssistantReply, type StreamItem, type TimelinePosition } from "@/types/stream"; export type AssistantTurnForkBoundary = | { boundaryCursor: TimelinePosition; boundaryMessageId?: string } @@ -9,15 +9,15 @@ export function resolveAssistantTurnForkBoundary(input: { startIndex: number; supportsTimelineCursor: boolean; }): AssistantTurnForkBoundary | undefined { - const item = input.items[input.startIndex]; - if (item?.kind !== "assistant_message") { + const reply = getAssistantReply(input.items[input.startIndex]); + if (!reply) { return undefined; } - if (input.supportsTimelineCursor && item.timelineCursor) { + if (input.supportsTimelineCursor && reply.timelineCursor) { return { - boundaryCursor: item.timelineCursor, - ...(item.messageId ? { boundaryMessageId: item.messageId } : {}), + boundaryCursor: reply.timelineCursor, + ...(reply.messageId ? { boundaryMessageId: reply.messageId } : {}), }; } - return item.messageId ? { boundaryMessageId: item.messageId } : undefined; + return reply.messageId ? { boundaryMessageId: reply.messageId } : undefined; } diff --git a/packages/app/src/agent-stream/turn-collapse/model.test.ts b/packages/app/src/agent-stream/turn-collapse/model.test.ts index 9c48f5695a..6438ca19d9 100644 --- a/packages/app/src/agent-stream/turn-collapse/model.test.ts +++ b/packages/app/src/agent-stream/turn-collapse/model.test.ts @@ -16,8 +16,7 @@ function pluginReply(sourceId: string, part = "0"): StreamItem { return { kind: "plugin", id: `math/${sourceId}/${part}`, - sourceId, - sourceKind: "assistant_message", + source: reply(sourceId), timestamp: at, pluginId: "math", pluginItemId: `${sourceId}/${part}`, @@ -99,10 +98,10 @@ describe("collapseTurns", () => { expect(result.hostIdByFoldedMessageId.get("intermediate")).toBe("prompt"); }); - it.each([undefined, "thought", "tool_call"] as const)( - "does not treat a plugin row with source kind %s as a reply", - (sourceKind) => { - const card = { ...pluginReply("card"), sourceKind }; + it.each([undefined, thought("card"), shell("card")])( + "does not treat a plugin row replacing %o as a reply", + (source) => { + const card = { ...pluginReply("card"), source }; const items = [user("prompt"), shell("call"), card]; expect(collapse(items).tail).toEqual(items); expect(collapse(items).summaryRowsByHostId.size).toBe(0); diff --git a/packages/app/src/agent-stream/turn-collapse/model.ts b/packages/app/src/agent-stream/turn-collapse/model.ts index fe931afed5..dae4fa8556 100644 --- a/packages/app/src/agent-stream/turn-collapse/model.ts +++ b/packages/app/src/agent-stream/turn-collapse/model.ts @@ -1,6 +1,7 @@ import { isTurnBoundary, isTurnTrigger } from "../turn-membership"; import { getStreamItemMessageId, + getAssistantReply, type StreamItem, type ThoughtItem, type TimelinePosition, @@ -103,22 +104,15 @@ function staysVisibleWhenCollapsed(item: StreamItem): boolean { return isTurnTrigger(item) || item.kind === "compaction"; } -function isAssistantReply(item: StreamItem): boolean { - return ( - item.kind === "assistant_message" || - (item.kind === "plugin" && item.sourceKind === "assistant_message") - ); -} - function findLastReplyIndex(tail: StreamItem[], start: number, end: number): number { for (let index = end - 1; index >= start; index -= 1) { const item = tail[index]!; - if (!isAssistantReply(item)) continue; + if (!getAssistantReply(item)) continue; const messageId = getStreamItemMessageId(item); // A transformer can split one reply into several cards; keep the whole reply visible. while (index > start) { const previous = tail[index - 1]!; - if (!isAssistantReply(previous) || getStreamItemMessageId(previous) !== messageId) break; + if (!getAssistantReply(previous) || getStreamItemMessageId(previous) !== messageId) break; index -= 1; } return index; @@ -156,7 +150,7 @@ export function collapseTurns(input: CollapseTurnsInput): TurnCollapse { } const folded = tail.slice(runStart, replyIndex); const tools = summarizeTurn(folded, input.getGroupedCalls); - if (!hasWork(tools) && !folded.some(isAssistantReply)) { + if (!hasWork(tools) && !folded.some((item) => getAssistantReply(item))) { output.push(...tail.slice(runStart, runEnd)); return; } diff --git a/packages/app/src/agent-stream/turn-footer.browser.test.tsx b/packages/app/src/agent-stream/turn-footer.browser.test.tsx index 46a51c0f34..3851c66fb8 100644 --- a/packages/app/src/agent-stream/turn-footer.browser.test.tsx +++ b/packages/app/src/agent-stream/turn-footer.browser.test.tsx @@ -150,3 +150,44 @@ describe("turn stats", () => { expect(host.getBoundingClientRect().height).toBe(without); }); }); + +const projectedReply: StreamItem = { + kind: "plugin", + id: "genui/reply/1", + source: { + kind: "assistant_message", + id: "reply", + messageId: "provider-reply-1", + text: 'Done.\n\n```ui\nCard("Summary", [])\n```', + timelineCursor: { epoch: "e1", seq: 5 }, + timestamp: FRIDAY_NIGHT, + }, + timelineCursor: { epoch: "e1", seq: 5 }, + timestamp: FRIDAY_NIGHT, + pluginId: "genui", + pluginItemId: "reply/1", + itemKind: "genui-block", + version: 1, + data: {}, +}; + +it("renders copy, fork, and duration for a plugin-projected assistant reply", () => { + const onFork = vi.fn(); + + act(() => + root.render( + , + ), + ); + + expect(host.querySelector('[aria-label="Copy turn"]')).not.toBeNull(); + expect(host.querySelector('[aria-label="Fork chat from here"]')).not.toBeNull(); + expect(host.textContent).toContain("Worked for 26s"); +}); diff --git a/packages/app/src/agent-stream/turn-footer.tsx b/packages/app/src/agent-stream/turn-footer.tsx index 93574a61d2..4d9d87e97f 100644 --- a/packages/app/src/agent-stream/turn-footer.tsx +++ b/packages/app/src/agent-stream/turn-footer.tsx @@ -4,7 +4,7 @@ import { View } from "react-native"; import { StyleSheet, withUnistyles } from "react-native-unistyles"; import { SPACING, type Theme } from "@/styles/theme"; import type { TurnTiming } from "@/timeline/turn-time"; -import type { StreamItem } from "@/types/stream"; +import { getAssistantReply, type StreamItem } from "@/types/stream"; import { collectAssistantResponseContentForStreamRenderStrategy, type StreamStrategy, @@ -208,7 +208,7 @@ function CompletedTurnFooter({ return ( { { kind: "plugin", id: "inline-thinking/thought-1/0", - sourceId: "thought-1", - sourceKind: "thought", + source: { kind: "thought", id: "thought-1" }, data: { text: "First", phase: "streaming" }, }, ]); @@ -192,4 +191,54 @@ describe("plugin timeline projection", () => { ]); expect(filtered).toEqual([]); }); + + it("keeps the replaced reply on every card it was split into", () => { + const source: StreamItem = { + kind: "assistant_message", + id: "assistant-1", + messageId: "provider-msg-1", + timelineCursor: { epoch: "e1", seq: 7 }, + turnId: "turn-1", + text: 'Summary\n\n```ui\nCard("Hi", [])\n```', + timestamp: new Date("2026-01-01T00:00:02.000Z"), + }; + const split: TimelineItemTransform = ({ sourceId }) => [ + { + type: "plugin", + id: `${sourceId}/text`, + pluginId: "genui", + kind: "genui-text", + version: 1, + data: { part: "text" }, + }, + { + type: "plugin", + id: `${sourceId}/card`, + pluginId: "genui", + kind: "genui-block", + version: 1, + data: { part: "card" }, + }, + ]; + const projected = projectPluginTimelineItems([source], split); + + expect(projected).toMatchObject([ + { + kind: "plugin", + id: "genui/assistant-1/text", + timelineCursor: { epoch: "e1", seq: 7 }, + turnId: "turn-1", + }, + { + kind: "plugin", + id: "genui/assistant-1/card", + timelineCursor: { epoch: "e1", seq: 7 }, + turnId: "turn-1", + }, + ]); + expect(projected.map((item) => item.kind === "plugin" && item.source)).toEqual([ + source, + source, + ]); + }); }); diff --git a/packages/app/src/plugins/timeline/projection.ts b/packages/app/src/plugins/timeline/projection.ts index 4670191746..5836e75aeb 100644 --- a/packages/app/src/plugins/timeline/projection.ts +++ b/packages/app/src/plugins/timeline/projection.ts @@ -95,8 +95,7 @@ function transformSourceItem( const projected: PluginTimelineStreamItem = { kind: "plugin", id: `${pluginItem.pluginId}/${pluginItem.id}`, - sourceId: item.id, - sourceKind: item.kind, + source: item, timestamp: item.timestamp, pluginId: pluginItem.pluginId, pluginItemId: pluginItem.id, diff --git a/packages/app/src/timeline/turn-time.test.ts b/packages/app/src/timeline/turn-time.test.ts index f3e97b2e49..ab795c6e48 100644 --- a/packages/app/src/timeline/turn-time.test.ts +++ b/packages/app/src/timeline/turn-time.test.ts @@ -247,3 +247,37 @@ it("times a heartbeat that ends with tools and no assistant text", () => { }); assert.equal(timing.byItemId.get("last")?.durationMs, 5000); }); + +it("times a reply that a plugin split into cards", () => { + const userAt = new Date("2026-05-15T00:00:00.000Z"); + const replyAt = new Date("2026-05-15T00:00:06.000Z"); + const reply: StreamItem = { + kind: "assistant_message", + id: "a1", + text: "Intro", + timestamp: replyAt, + }; + const card = (part: string): StreamItem => ({ + kind: "plugin", + id: `genui/a1/${part}`, + source: reply, + timestamp: replyAt, + pluginId: "genui", + pluginItemId: `a1/${part}`, + itemKind: `genui-${part}`, + version: 1, + data: {}, + }); + + const timing = deriveStreamTurnTiming({ + isTurnActive: false, + activeTurnStartedAt: null, + tail: [user("u1", userAt), card("text"), card("card")], + head: [], + }); + + assert.deepEqual(timing.byItemId.get("genui/a1/card"), { + completedAt: replyAt, + durationMs: 6000, + }); +}); diff --git a/packages/app/src/timeline/turn-time.ts b/packages/app/src/timeline/turn-time.ts index 8d1aa00648..a6f11afcf2 100644 --- a/packages/app/src/timeline/turn-time.ts +++ b/packages/app/src/timeline/turn-time.ts @@ -1,4 +1,4 @@ -import type { StreamItem } from "@/types/stream"; +import { getAssistantReply, type StreamItem } from "@/types/stream"; import { startsNewTurn, isTurnTrigger } from "@/agent-stream/turn-membership"; export interface TurnTiming { @@ -70,7 +70,7 @@ function walkTurns( assistantIds = []; } lastItemAt = item.timestamp; - if (item.kind === "assistant_message") { + if (getAssistantReply(item)) { assistantIds.push(item.id); } previousItem = item; diff --git a/packages/app/src/types/stream.ts b/packages/app/src/types/stream.ts index 47138a2575..8f6a572c80 100644 --- a/packages/app/src/types/stream.ts +++ b/packages/app/src/types/stream.ts @@ -783,7 +783,7 @@ export type AgentToolCallItem = ToolCallItem & { /** Find and scrolling address a plugin row by the source it replaces. */ export function getStreamItemMessageId(item: StreamItem): string { - if (item.kind === "plugin") return item.sourceId ?? item.id; + if (item.kind === "plugin") return item.source?.id ?? item.id; return item.id; } @@ -791,6 +791,15 @@ export function isAgentToolCallItem(item: StreamItem): item is AgentToolCallItem return item.kind === "tool_call" && item.payload.source === "agent"; } +/** The assistant reply a row shows: the row itself, or the reply a timeline plugin replaced. */ +export function getAssistantReply( + item: StreamItem | null | undefined, +): AssistantMessageItem | null { + if (item?.kind === "assistant_message") return item; + if (item?.kind === "plugin" && item.source?.kind === "assistant_message") return item.source; + return null; +} + type NotificationLevel = "info" | "warning" | "error"; export interface NotificationItem { @@ -818,10 +827,12 @@ export interface CompactionItem { export interface PluginTimelineStreamItem { kind: "plugin"; id: string; - /** The id of the stream item a timeline transformer replaced; set only by projection. */ - sourceId?: string; - /** The original row kind, retained by projection for turn folding. */ - sourceKind?: StreamItem["kind"]; + /** + * The row a timeline transformer replaced; set only by projection. Turn folding, timing, and the + * turn footer read the replaced row through it, so a transformed reply keeps its copy, fork, + * duration, and stats. + */ + source?: StreamItem; timelineCursor?: TimelinePosition; turnId?: string; timestamp: Date;