diff --git a/apps/server/src/fork/shownHistory/ShownHistory.test.ts b/apps/server/src/fork/shownHistory/ShownHistory.test.ts index 495df729b6d68..4686c5ab2a43b 100644 --- a/apps/server/src/fork/shownHistory/ShownHistory.test.ts +++ b/apps/server/src/fork/shownHistory/ShownHistory.test.ts @@ -36,6 +36,7 @@ import { } from "@t3tools/shared/displayModes"; import { boundedSnapshotProjection } from "@t3tools/shared/orchestrationV2BoundedSnapshot"; import { resolveT3McpToolId } from "@t3tools/shared/t3McpToolPresentation"; +import { htmlRenderFromToolItem, mcpAppFromToolItem } from "@t3tools/shared/toolOutput"; import * as NodeHttpPlatform from "@effect/platform-node/NodeHttpPlatform"; import * as NodeServices from "@effect/platform-node/NodeServices"; import * as DateTime from "effect/DateTime"; @@ -90,6 +91,7 @@ const UNANSWERED = ThreadId.make("thread:shown:unanswered"); const LARGE = ThreadId.make("thread:shown:large"); const REWRITES = ThreadId.make("thread:shown:rewrites"); const HUGE = ThreadId.make("thread:shown:huge"); +const SPLIT = ThreadId.make("thread:shown:split"); const runIdOf = (threadId: ThreadId, run: number) => RunId.make(`run:${threadId}:${run}`); const nodeIdOf = (threadId: ThreadId, run: number) => NodeId.make(`node:${threadId}:${run}`); @@ -330,7 +332,88 @@ function botThread(): OrchestrationV2TurnItem[] { return items; } -/** A typed message, then 300 sends in 30 bot runs, three hidden rows after each. */ +const render = (threadId: ThreadId, run: number, k: number): OrchestrationV2TurnItem => ({ + ...base(threadId, run, k), + type: "dynamic_tool", + toolName: "mcp__t3-code__html_render", + input: { title: "Chart", height: 420 }, + output: { + structuredContent: { + htmlRender: { attachmentId: `chart-${k}.html`, title: "Chart", height: 420 }, + }, + content: [], + }, +}); + +const mcpApp = (threadId: ThreadId, run: number, k: number): OrchestrationV2TurnItem => ({ + ...base(threadId, run, k), + type: "dynamic_tool", + toolName: "weather.get_weather", + input: {}, + output: { + t3McpApp: { + attachmentId: `weather-${k}.html`, + server: "weather", + tool: "get_weather", + resourceUri: "ui://weather/dashboard", + }, + result: { content: [] }, + }, +}); + +const mailApp = (threadId: ThreadId, run: number, k: number): OrchestrationV2TurnItem => { + const app = mcpApp(threadId, run, k); + return app.type === "dynamic_tool" + ? { + ...app, + toolName: "mail.send_to_user", + output: { + t3McpApp: { + attachmentId: `mail-${k}.html`, + server: "mail", + tool: "send_to_user", + resourceUri: "ui://mail/compose", + }, + result: { content: [] }, + }, + } + : app; +}; + +/** Inline work output that is no card: a subagent's result. */ +const delegated = (threadId: ThreadId, run: number, k: number): OrchestrationV2TurnItem => ({ + ...base(threadId, run, k), + type: "dynamic_tool", + toolName: "mcp__t3-code__delegate_task", + input: { task: "check" }, + output: { content: [{ type: "text", text: "done" }] }, +}); + +/** A link card the agent did not wait on, so it can wait for the user long after its run. */ +const linkCard = ( + threadId: ThreadId, + run: number, + k: number, + status: "waiting" | "cancelled", +): OrchestrationV2TurnItem => ({ + ...base(threadId, run, k, status), + type: "link_request", + reason: "Run it on the vps", + linkStatus: status === "waiting" ? "pending" : "declined", +}); + +const secretCard = (threadId: ThreadId, run: number, k: number): OrchestrationV2TurnItem => ({ + ...base(threadId, run, k, "waiting"), + type: "secret_request", + label: "API token", + reason: "Deploy needs it", + secretStatus: "pending", +}); + +/** + * A typed message, then 300 sends in 30 bot runs, three hidden rows after each, and cards in runs 5 + * and 30. + */ function pagedThread(): OrchestrationV2TurnItem[] { const items = [userMessage(PAGED, 1, 1, "Start the long job.")]; for (const run of range(2, 31)) { @@ -341,6 +424,17 @@ function pagedThread(): OrchestrationV2TurnItem[] { items.push(send(PAGED, run, 2 + 4 * j, `Step ${run}.${j} done.`)); items.push(...range(3 + 4 * j, 5 + 4 * j).map((k) => hidden(PAGED, run, k))); } + if (run === 5 || run === 30) + items.push(render(PAGED, run, 42), mcpApp(PAGED, run, 43), delegated(PAGED, run, 44)); + // An app whose tool name holds send_to_user, which the agent's message view must still skip. + if (run === 30) items.push(mailApp(PAGED, run, 49)); + if (run === 5) + items.push( + linkCard(PAGED, run, 45, "waiting"), + linkCard(PAGED, run, 46, "cancelled"), + { ...mcpApp(PAGED, run, 47), status: "failed" }, + secretCard(PAGED, run, 48), + ); } return items; } @@ -528,10 +622,30 @@ const rewritesThread = () => [ const LARGE_TEXT = "A full report line. ".repeat(512); -/** A typed message and 150 sends of about 10 KiB each, one per run. */ +/** + * A typed message, then 150 runs of a typed request, a send of about 10 KiB and a waiting card, so + * the byte cap falls between a card and the message its run answers. + */ const largeThread = () => [ userMessage(LARGE, 1, 1, "Send the full reports."), - ...range(2, 151).map((run) => send(LARGE, run, 1, `${LARGE_TEXT}${run}`)), + ...range(2, 151).flatMap((run) => [ + userMessage(LARGE, run, 1, `Send report ${run}.`), + send(LARGE, run, 2, `${LARGE_TEXT}${run}`), + secretCard(LARGE, run, 3), + ]), +]; + +const SPLIT_FIRST = itemIdOf(SPLIT, 1, 1); + +/** + * An agent's first message; a later run with a reply under it and a card; then 127 newer sends in + * one run, so the window has its messages right after the card, before the reply's run ends. + */ +const splitThread = () => [ + send(SPLIT, 1, 1, "The nightly job is done."), + userMessage(SPLIT, 2, 1, "Which one?", TYPED, String(SPLIT_FIRST)), + secretCard(SPLIT, 2, 2), + ...range(1, 127).map((k) => send(SPLIT, 3, k, `Note ${k}.`)), ]; const HUGE_TEXT = "x".repeat(5 * 1_048_576); @@ -579,6 +693,7 @@ const seed = Effect.gen(function* () { [LARGE, largeThread(), threadCreated(LARGE)], [REWRITES, rewritesThread(), threadCreated(REWRITES)], [HUGE, hugeThread(), threadCreated(HUGE)], + [SPLIT, splitThread(), threadCreated(SPLIT)], ]; for (const [threadId, items, created] of threads) { yield* projections.apply(created); @@ -727,15 +842,52 @@ const allPages = (threadId: ThreadId, cursor: string | null | undefined) => const visibleIds = (projection: OrchestrationV2ThreadProjection) => projection.visibleTurnItems.map((row) => String(row.sourceItemId)); -const isShownItem = (item: OrchestrationV2TurnItem) => { +const isShownMessage = (item: OrchestrationV2TurnItem) => { const event = displayEventFromTurnItem(item); return event?.type === "user" || event?.type === "sent" || event?.type === "rewrite"; }; +/** A message, or a card the screen draws beside the messages. */ +const isShownItem = (item: OrchestrationV2TurnItem) => + isShownMessage(item) || + ((item.type === "link_request" || item.type === "secret_request") && item.status === "waiting") || + (item.type === "dynamic_tool" && + ((item.status === "completed" && htmlRenderFromToolItem(item) !== undefined) || + mcpAppFromToolItem(item) !== undefined)); + /** The ids of `items` that a display mode shows, oldest first. */ const shownIdsOf = (items: ReadonlyArray) => items.filter(isShownItem).map((item) => String(item.id)); +/** + * The screen places a card by the latest message the user typed in its run, so that message comes + * no later than the card: on its page or a newer one. `pages` runs newest first. + */ +function assertCardsPlaced( + pages: ReadonlyArray< + ReadonlyArray<{ readonly item: OrchestrationV2TurnItem; readonly sourceItemId: unknown }> + >, + items: ReadonlyArray, +) { + const typedIn = new Map( + items.flatMap((item) => + displayEventFromTurnItem(item)?.type === "user" && item.runId !== null + ? [[String(item.runId), String(item.id)] as const] + : [], + ), + ); + const pageOf = new Map( + pages.flatMap((rows, index) => rows.map((row) => [String(row.sourceItemId), index] as const)), + ); + for (const [index, rows] of pages.entries()) { + for (const { item, sourceItemId } of rows) { + const typed = isShownMessage(item) ? undefined : typedIn.get(String(item.runId)); + if (typed === undefined) continue; + assert.isAtMost(pageOf.get(typed) ?? Infinity, index, `${sourceItemId} comes with ${typed}`); + } + } +} + it.layer(TestLayer)("shown-only history", (it) => { it.effect("a shown subscription opens a bot-heavy thread on its messages", () => Effect.gen(function* () { @@ -767,7 +919,11 @@ it.layer(TestLayer)("shown-only history", (it) => { assert.isTrue(page.items.every((row) => isShownItem(row.item))); } for (const page of pages.slice(0, -1)) { - assert.isAtLeast(page.items.length, 100, "a page that is not the last holds 100 messages"); + assert.isAtLeast( + page.items.filter((row) => isShownMessage(row.item)).length, + 100, + "a page that is not the last holds 100 messages", + ); } assert.isFalse(pages.at(-1)!.hasMoreHistory); assert.isNull(pages.at(-1)!.nextCursor); @@ -775,6 +931,12 @@ it.layer(TestLayer)("shown-only history", (it) => { String(row.sourceItemId), ); assert.deepStrictEqual(loaded, shownIdsOf(pagedThread())); + assertCardsPlaced([window, ...pages.map((page) => page.items)], pagedThread()); + assert.notInclude( + visibleIds(boundedSnapshotProjection(snapshot)), + String(itemIdOf(PAGED, 30, 44)), + "inline work output that is no card", + ); }), ); @@ -793,6 +955,10 @@ it.layer(TestLayer)("shown-only history", (it) => { assert.strictEqual(replies.length, 3); assert.isTrue(replies.every((core) => core.first === FIRST)); assert.isTrue(loaded.has(FIRST)); + + // A reply that the window takes in to finish a card's run brings its first message too. + const split = visibleIds(boundedSnapshotProjection(yield* shownSnapshot(SPLIT))); + assert.includeMembers(split, [String(itemIdOf(SPLIT, 2, 2)), String(SPLIT_FIRST)]); }), ); @@ -932,6 +1098,7 @@ it.layer(TestLayer)("shown-only history", (it) => { ), shown, ); + assertCardsPlaced([window, ...pages.map((page) => page.items)], largeThread()); }), ); @@ -942,7 +1109,10 @@ it.layer(TestLayer)("shown-only history", (it) => { selectShownTail(sql, threadId, ProjectionStore.inheritedRunFork, minCores); const ids = (rows: ReadonlyArray<{ readonly itemId: TurnItemId }>) => rows.map((row) => String(row.itemId)); - const paged = shownIdsOf(pagedThread()); + // The agent's message view reads no cards. + const paged = pagedThread() + .filter(isShownMessage) + .map((item) => String(item.id)); const enough = yield* tail(PAGED, 240); assert.strictEqual(enough?.stop, "enough"); diff --git a/apps/server/src/fork/shownHistory/ShownHistory.ts b/apps/server/src/fork/shownHistory/ShownHistory.ts index 11a74aad58d36..dd143f39daf95 100644 --- a/apps/server/src/fork/shownHistory/ShownHistory.ts +++ b/apps/server/src/fork/shownHistory/ShownHistory.ts @@ -17,6 +17,7 @@ import { SEND_TO_USER_TOOL_NAME, SUB_THREAD_WINDOW_CORES, } from "@t3tools/shared/displayModes"; +import { htmlRenderFromToolItem, mcpAppFromToolItem } from "@t3tools/shared/toolOutput"; import * as Effect from "effect/Effect"; import * as Result from "effect/Result"; import * as Schema from "effect/Schema"; @@ -40,11 +41,11 @@ export interface ShownKey { readonly itemId: TurnItemId; } -/** A row that a display mode shows, and what it shows. */ +/** A row that a display mode shows, and what it shows: a message, or a card (`other`). */ interface ShownRow extends ShownKey { readonly rowid: number; readonly item: OrchestrationV2TurnItem; - readonly event: Exclude; + readonly event: DisplayEvent; } /** The rows of a window or page, newest first, and the newest shown row older than them. */ @@ -75,12 +76,13 @@ interface ShownTarget { readonly itemId: string; } -/** A walk stops once it holds `minCores` messages, or at a cap. */ +/** A walk stops once it holds `minCores` messages, or at a cap. The screen also reads `cards`. */ interface ShownLimits { readonly minCores: number; readonly maxRows: number; readonly maxBytes: number; readonly maxRuns: number; + readonly cards: boolean; } const WINDOW_LIMITS: ShownLimits = { @@ -90,12 +92,14 @@ const WINDOW_LIMITS: ShownLimits = { maxBytes: 512 * 1024, // A run's records come along outside the byte cap; Normal stops at as many turns. maxRuns: THREAD_HISTORY_MAX_RAW_TURNS, + cards: true, }; const PAGE_LIMITS: ShownLimits = { minCores: 100, maxRows: 1_000, maxBytes: 1_048_576, maxRuns: Number.POSITIVE_INFINITY, + cards: true, }; const TARGETED_PAGE_LIMITS: ShownLimits = { ...PAGE_LIMITS, maxRows: 2_000, maxBytes: 2_097_152 }; /** A tail read's caps; it asks for its own `minCores`. */ @@ -165,15 +169,26 @@ export const sendCallSql = (sql: SqlClient.SqlClient) => sql`(item.type = 'dynamic_tool' AND json_extract(item.payload_json, '$.toolName') LIKE ${`%${SEND_TO_USER_TOOL_NAME}%`})`; -/** The rows a display mode can show: messages the user typed (isUserTypedMessage) and send calls. */ -const shownCandidateSql = (sql: SqlClient.SqlClient) => sql`( +/** + * The rows a display mode can show: messages the user typed (isUserTypedMessage) and send calls. + * With `cards`, also waiting secret and link cards, and tool calls with inline output, a superset + * of the agent's renders and apps. + */ +const shownCandidateSql = (sql: SqlClient.SqlClient, cards: boolean) => sql`( (item.type = 'user_message' AND json_extract(item.payload_json, '$.createdBy') = 'user' AND json_extract(item.payload_json, '$.creationSource') IN ('web', 'mobile') AND json_extract(item.payload_json, '$.scheduledTaskId') IS NULL AND json_extract(item.payload_json, '$.senderThreadId') IS NULL AND json_extract(item.payload_json, '$.idleCompaction') IS NULL) - OR ${sendCallSql(sql)})`; + OR ${sendCallSql(sql)} + ${ + cards + ? sql`OR (item.type IN ('secret_request', 'link_request') AND item.status = 'waiting') + OR (item.type = 'dynamic_tool' AND item.inline_work_output = 1 + AND (item.payload_json LIKE '%htmlRender%' OR item.payload_json LIKE '%t3McpApp%'))` + : sql.literal("") + })`; // A thread's own rows follow the eligible rule of ProjectionStore.retainedWindowRows; an inherited // stretch only the fork cutoff. @@ -193,7 +208,12 @@ interface CandidateRow { readonly payload_json: string; } -const readCandidates = (sql: SqlClient.SqlClient, segment: ShownSegment, from: ShownBound) => +const readCandidates = ( + sql: SqlClient.SqlClient, + segment: ShownSegment, + from: ShownBound, + cards: boolean, +) => sql` SELECT item.rowid AS item_rowid, item.turn_item_id, item.ordinal, item.payload_json FROM orchestration_v2_projection_turn_items AS item @@ -202,7 +222,7 @@ const readCandidates = (sql: SqlClient.SqlClient, segment: ShownSegment, from: S AND item.ordinal <= ${from.ordinal} AND (item.ordinal < ${from.ordinal} OR item.turn_item_id ${sql.literal(from.inclusive ? "<=" : "<")} ${from.itemId}) - AND ${shownCandidateSql(sql)} + AND ${shownCandidateSql(sql, cards)} AND ${segmentVisible(sql, segment)} ORDER BY item.ordinal DESC, item.turn_item_id DESC LIMIT ${BATCH_ROWS} @@ -340,21 +360,34 @@ const rewriteTarget = (event: Extract +/** A card the web timeline draws beside the messages: a waiting request, a render or an app. */ +const isShownCard = (item: OrchestrationV2TurnItem) => + item.type === "secret_request" || item.type === "link_request" + ? item.status === "waiting" + : item.type === "dynamic_tool" && + ((item.status === "completed" && htmlRenderFromToolItem(item) !== undefined) || + mcpAppFromToolItem(item) !== undefined); + +/** A candidate that shows nothing, such as a refused send or a subagent's result, is null. */ +const decodeShownRow = (segment: ShownSegment, row: CandidateRow, cards: boolean) => decodeTurnItem(row.payload_json).pipe( Effect.map((item): ShownRow | null => { - const event = displayEventFromTurnItem(item); - return event === null || event.type === "other" - ? null - : { ...keyOf(segment, row), rowid: row.item_rowid, item, event }; + const message = displayEventFromTurnItem(item); + const event: DisplayEvent | null = + message !== null && message.type !== "other" + ? message + : cards && isShownCard(item) + ? { type: "other" } + : null; + return event === null ? null : { ...keyOf(segment, row), rowid: row.item_rowid, item, event }; }), ); /** * Walks `segments` newest first over the shown rows only, in batches, until it has passed `target` - * and holds the messages `limits` asks for, checked after each batch. A cap ends it at any row. - * `next` is the newest shown row the walk left out, which the next page starts at. + * and holds the messages `limits` asks for, checked after each batch, and the screen's cards hold + * their run's typed message. A cap ends it at any row, or before a card's run it would cut, unless + * that run is all the walk holds. `next` is the newest shown row the walk left out. */ const walkShownRows = Effect.fn("ShownHistory.walk")(function* ( sql: SqlClient.SqlClient, @@ -368,12 +401,23 @@ const walkShownRows = Effect.fn("ShownHistory.walk")(function* ( const rewritten = new Set(); let bytes = 0; let met = false; + let runOfLast: string | null = null; + let runStart = 0; + let grewAfterMet = false; + // Runs whose card is loaded but not the message the user typed in the run, which places it. + const unplaced = new Set(); + const typed = new Set(); for (const [index, segment] of segments.entries()) { let from: ShownBound | null = segment.from; while (from !== null) { - const batch: ReadonlyArray = yield* readCandidates(sql, segment, from); + const batch: ReadonlyArray = yield* readCandidates( + sql, + segment, + from, + limits.cards, + ); for (const candidate of batch) { - const row = yield* decodeShownRow(segment, candidate); + const row = yield* decodeShownRow(segment, candidate, limits.cards); if (row === null) continue; // A newer rewrite of the same message already sets its text. const rewrite = row.event.type === "rewrite" ? rewriteTarget(row.event) : undefined; @@ -381,17 +425,36 @@ const walkShownRows = Effect.fn("ShownHistory.walk")(function* ( if (rewrite !== undefined) rewritten.add(rewrite); const cost = rowBytes(row); const newRun = row.item.runId !== null && !runs.has(row.item.runId); - if ( - met || - (rows.length > 0 && - (rows.length >= limits.maxRows || - bytes + cost > limits.maxBytes || - (newRun && runs.size >= limits.maxRuns))) - ) + // The screen places a run's cards by the run's typed message, so a page reads on to it. + const sameRun = + runOfLast !== null && row.item.runId === runOfLast && unplaced.has(runOfLast); + // Finishing a run can add a reply whose first message is older. + if (met && !sameRun && grewAfterMet) met = shownNeedsMet(rows, limits.minCores); + grewAfterMet &&= sameRun; + const capped = + rows.length > 0 && + (rows.length >= limits.maxRows || + bytes + cost > limits.maxBytes || + (newRun && runs.size >= limits.maxRuns)); + if (capped && sameRun && runStart > 0) { + const { threadId, ordinal, itemId } = rows[runStart]!; + return { rows: rows.slice(0, runStart), next: { threadId, ordinal, itemId } }; + } + if ((met && !sameRun) || capped) return { rows, next: keyOf(segment, candidate) } satisfies ShownWalk; + if (!sameRun) runStart = rows.length; rows.push(row); + grewAfterMet ||= met; bytes += cost; - if (row.item.runId !== null) runs.add(row.item.runId); + runOfLast = row.item.runId; + if (row.item.runId !== null) { + runs.add(row.item.runId); + if (row.event.type === "user") { + typed.add(row.item.runId); + unplaced.delete(row.item.runId); + } else if (row.event.type === "other" && !typed.has(row.item.runId)) + unplaced.add(row.item.runId); + } } const last = batch.at(-1); from = @@ -455,7 +518,7 @@ export const selectShownTail = Effect.fn("ShownHistory.selectTail")(function* ( const walk = yield* walkShownRows( sql, segments, - { ...SHOWN_TAIL_LIMITS, minCores }, + { ...SHOWN_TAIL_LIMITS, minCores, cards: false }, // The rows stay in memory whole; a wire row would summarize a send's input. (row) => Buffer.byteLength(JSON.stringify(row.item), "utf8"), null, diff --git a/apps/web/src/fork/displayMode/displayModeTimeline.ts b/apps/web/src/fork/displayMode/displayModeTimeline.ts index 08ca5b51615ab..7b58249063a01 100644 --- a/apps/web/src/fork/displayMode/displayModeTimeline.ts +++ b/apps/web/src/fork/displayMode/displayModeTimeline.ts @@ -1,4 +1,4 @@ -import { MessageId } from "@t3tools/contracts"; +import { MessageId, type RunId } from "@t3tools/contracts"; import { type DisplayEvent, type DisplayMode, @@ -234,11 +234,48 @@ export function rowShownIn( return core.first === subThread ? "here" : "elsewhere"; } +/** + * The run of a row a mode with sub-threads draws beside the messages: the agent's render or app, or + * a secret or link card that waits for this thread's user. Undefined for any other row. + */ +function cardRun(entry: TimelineEntry): RunId | null | undefined { + if (entry.kind === "html-render" || entry.kind === "mcp-app") return entry.runId; + if (entry.kind !== "event") return undefined; + const { item, visibility } = entry.projectedItem; + return (item.type === "secret_request" || item.type === "link_request") && + item.status === "waiting" && + visibility === "local" + ? item.runId + : undefined; +} + +/** + * Where each run's cards show, as its answer does: the first message of the sub-thread of the + * latest message the user typed in the run. A run with none, such as one a task or another agent + * started, shows its cards in the main list, as does a sub-thread whose first message is not loaded. + */ +function cardPlaces( + entries: ReadonlyArray, + cores: ReadonlyArray, +): ReadonlyMap { + const coreById = new Map(cores.map((core) => [core.id, core])); + const places = new Map(); + for (const entry of entries) { + const core = coreById.get(entry.id); + const runId = entry.kind === "message" ? entry.message.runId : null; + if (core?.from !== "user" || runId == null) continue; + if (core.reply && core.first !== null && coreById.has(core.first)) + places.set(runId, core.first); + else places.delete(runId); + } + return places; +} + /** * Returns the rows that a display mode draws, or `entries` itself when no row changes. A completed * send is a reply with its latest text, but in Normal a Normal-mode send keeps upstream's row. A - * replace call has no row. A mode without sub-threads keeps all other rows. A mode with - * sub-threads draws the first messages, or with `subThread`, that first message and its replies. + * replace call has no row. Other rows stay, except in a mode with sub-threads: it draws the first + * messages, or with `subThread`, that first message and its replies, with their runs' cards. */ export function applyDisplayMode( mode: DisplayMode, @@ -254,8 +291,15 @@ export function applyDisplayMode( .map((core) => core.id), ) : null; + let places: ReadonlyMap | undefined; + const cardHere = (entry: TimelineEntry) => { + const runId = cardRun(entry); + if (runId === undefined) return false; + places ??= cardPlaces(entries, cores); + return (runId === null ? null : (places.get(runId) ?? null)) === subThread; + }; const rows = entries.flatMap((entry, index): TimelineEntry[] => { - if (shownIds !== null && !shownIds.has(entry.id)) return []; + if (shownIds !== null && !shownIds.has(entry.id)) return cardHere(entry) ? [entry] : []; const item = entry.kind === "work" ? entry.entry.structuredPayload : undefined; if (item !== undefined && isReplaceSendCall(item)) return []; const event = events[index]; @@ -271,7 +315,7 @@ export function applyDisplayMode( /** * A mode that chooses its replies draws no Working or Thinking row, and marks an agent message - * drawn right after another one, with no user message between, so a rule can part the two. + * drawn right after another one, with no user message or card between, so a rule can part the two. */ export function displayModeRows( rows: MessagesTimelineRow[], @@ -281,6 +325,8 @@ export function displayModeRows( let afterAgent = false; return rows.flatMap((row): MessagesTimelineRow[] => { if (row.kind === "working" || row.kind === "thinking") return []; + if (row.kind === "event" || row.kind === "html-render" || row.kind === "mcp-app") + afterAgent = false; if (row.kind !== "message") return [row]; const fromAgent = row.message.role === "assistant"; const follows = fromAgent && afterAgent; diff --git a/apps/web/src/fork/subThreads/subThreadTimeline.test.ts b/apps/web/src/fork/subThreads/subThreadTimeline.test.ts index 47c1b1e8794c8..2bfa9fffc3286 100644 --- a/apps/web/src/fork/subThreads/subThreadTimeline.test.ts +++ b/apps/web/src/fork/subThreads/subThreadTimeline.test.ts @@ -102,11 +102,11 @@ const rewrite = (id: string, second: number, done: number, input: Record) => +const entriesOf = (items: ReadonlyArray, inherited?: string) => deriveTimelineEntriesFromVisibleTurnItems({ visibleTurnItems: items.map((item, position): OrchestrationV2ProjectedTurnItem => ({ position, - visibility: "local", + visibility: item.id === inherited ? "inherited" : "local", sourceThreadId: threadId, sourceItemId: item.id, item, @@ -153,6 +153,134 @@ describe("Chat mode rows", () => { ]); }); + it("draws a waiting card, a render and an app where the message their run answers is", () => { + const inRun = (item: T, run: string): T => ({ + ...item, + runId: RunId.make(run), + }); + const asking = { + ...base("asking", 8), + status: "running" as const, + completedAt: null, + type: "dynamic_tool" as const, + toolName: "mcp__t3-code__request_secret", + input: { label: "API token" }, + } satisfies OrchestrationV2TurnItem; + const secret = { + ...base("secret", 9), + status: "waiting" as const, + completedAt: null, + type: "secret_request" as const, + label: "API token", + reason: "Deploy needs it", + secretStatus: "pending" as const, + } satisfies OrchestrationV2TurnItem; + const link = { + ...base("link", 10), + status: "waiting" as const, + completedAt: null, + type: "link_request" as const, + reason: "Run it on the vps", + linkStatus: "pending" as const, + } satisfies OrchestrationV2TurnItem; + const chart = { + ...base("chart", 11), + type: "dynamic_tool" as const, + toolName: "mcp__t3-code__html_render", + input: { title: "Chart", height: 420 }, + output: { + structuredContent: { + htmlRender: { attachmentId: "chart.html", title: "Chart", height: 420 }, + }, + content: [], + }, + } satisfies OrchestrationV2TurnItem; + const app = { + ...base("app", 12), + type: "dynamic_tool" as const, + toolName: "weather.get_weather", + input: {}, + output: { + t3McpApp: { + attachmentId: "weather-app.html", + server: "weather", + tool: "get_weather", + resourceUri: "ui://weather/dashboard", + }, + result: { content: [] }, + }, + } satisfies OrchestrationV2TurnItem; + const ask = typed("ask", 7, "Deploy it."); + const shown = [ask, asking, secret, link, chart, app].map((item) => inRun(item, "deploy")); + shown.unshift(...items); + const mainList = (rows: ReadonlyArray, inherited?: string) => + ids(applyDisplayMode(CHAT_REPLIES_DISPLAY_MODE, entriesOf(rows, inherited))); + expect(mainList(shown)).toEqual([ + "q1", + "news", + "a1", + "ask", + "secret", + "link", + "chart", + "mcp-app:app", + ]); + expect(ids(applyDisplayMode(CHAT_REPLIES_DISPLAY_MODE, entriesOf(shown), "q1"))).toEqual([ + "q1", + "q2", + "a2", + ]); + // A card after a reply shows in that reply's sub-thread only, also after the agent's answer. + const tint = inRun(typed("tint", 13, "Which color?", "news"), "tint"); + const answer = inRun( + sent("teal", 14, { text: "Teal.", reply_to: shortId("tint"), user_message: "x" }), + "tint", + ); + const swatch = inRun({ ...chart, id: TurnItemId.make("swatch"), ordinal: 15 }, "tint"); + const asked = [...shown, tint, answer, swatch]; + expect(mainList(asked)).toEqual(mainList(shown)); + expect(ids(applyDisplayMode(CHAT_REPLIES_DISPLAY_MODE, entriesOf(asked), "news"))).toEqual([ + "news", + "why", + "tint", + "teal", + "swatch", + ]); + // A run the user typed nothing in, such as one a task started, shows its cards in the main list. + const report = inRun({ ...chart, id: TurnItemId.make("report"), ordinal: 16 }, "task"); + expect(mainList([...asked, report])).toEqual([...mainList(shown), "report"]); + // A message an agent wrote under a first message places nothing. + const relay = inRun( + { + ...typed("relay", 16, "Check the logs.", "news"), + createdBy: "agent", + creationSource: "mcp", + }, + "relay", + ); + const logs = inRun({ ...chart, id: TurnItemId.make("logs"), ordinal: 17 }, "relay"); + expect(mainList([...asked, relay, logs])).toEqual([...mainList(shown), "logs"]); + // The latest message the user typed in the run places its cards, here a steer in the main list. + const steer = inRun(typed("steer", 14, "Use blue instead."), "tint"); + const steered = [...shown, tint, steer, swatch]; + expect(mainList(steered)).toEqual([...mainList(shown), "steer", "swatch"]); + expect(ids(applyDisplayMode(CHAT_REPLIES_DISPLAY_MODE, entriesOf(steered), "news"))).toEqual([ + "news", + "why", + "tint", + ]); + // A reply under a first message that is not loaded opens no sub-thread, so its card stays in view. + const lost = inRun(typed("lost", 13, "Still there?", "gone"), "tint"); + expect(mainList([...shown, lost, swatch])).toEqual([...mainList(shown), "swatch"]); + // An answered card has nothing left to ask, and a fork's inherited card is answered in its own thread. + const answered = [ + { ...secret, status: "completed" as const, secretStatus: "saved" as const }, + { ...link, status: "cancelled" as const, linkStatus: "declined" as const }, + ]; + expect(mainList([...items, ...answered])).toEqual(["q1", "news", "a1"]); + expect(mainList([...items, inRun(secret, "deploy")], "secret")).toEqual(["q1", "news", "a1"]); + }); + it("marks a rewritten message with the time its rewrite completed, a first message too", () => { const editNews = rewrite("w1", 8, 9, { sub_thread: shortId("The deploy is done."), @@ -321,6 +449,51 @@ describe("displayModeRows", () => { ).toEqual([false, false, false, true, false, false]); expect(marked[1]).toBe(rows[1]); }); + + it("does not mark an agent message that a card parts from the one before it", () => { + const chart: MessagesTimelineRow = { + kind: "html-render", + id: "chart", + createdAt: time(0), + htmlRender: { attachmentId: "chart.html", title: "Chart", height: 420 }, + }; + const app: MessagesTimelineRow = { + kind: "mcp-app", + id: "mcp-app:app", + createdAt: time(0), + sourceThreadId: threadId, + itemId: TurnItemId.make("app"), + revision: "1", + mcpApp: { + attachmentId: "weather-app.html", + server: "weather", + tool: "get_weather", + resourceUri: "ui://weather/dashboard", + }, + }; + const secret = entriesOf([ + { + ...base("secret", 0), + status: "waiting", + completedAt: null, + type: "secret_request", + label: "API token", + reason: "Deploy needs it", + secretStatus: "pending", + }, + ])[0]!; + if (secret.kind !== "event") throw new Error("a secret card is an event row"); + const card: MessagesTimelineRow = { ...secret, kind: "event" }; + for (const parting of [chart, app, card]) { + const marked = displayModeRows( + [row("u1", "user"), row("a1", "assistant"), parting, row("a2", "assistant")], + true, + ); + expect( + marked.map((each) => each.kind === "message" && each.followsAgentMessage === true), + ).toEqual([false, false, false, false]); + } + }); }); describe("withSubThreadRecord", () => {