From cad97812bff118e5efde000a502a76e564b4bf5f Mon Sep 17 00:00:00 2001 From: Renfei Zhou Date: Sun, 22 Nov 2026 02:17:03 +0000 Subject: [PATCH] feat(fork): Chat mode answers an earlier message in its sub-thread When the user sends a second main-list message while the agent is still on the first, the agent may now answer the first one separately. That answer shows in the first message's sub-thread; answers to the latest main-list message still show in the main list. - foldCores places an answer to a main-list message in that message's sub-thread when a later main-list user message comes before it. - send_to_user no longer refuses such an answer. It refuses once when a later main-list message is a steer that no provider has confirmed reading (pending, cancelled, or never past the time it was stored), and lists it; the retry goes through. - The result text, the Chat mode rules (chat-replies@4), the per-message line and the reply_to description state the new placement, with same-topic versus separate-topic guidance. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- .../DisplayModeCompactionNote.test.ts | 2 +- .../DisplayModeWrap.integration.test.ts | 2 +- .../displayModeWrap/DisplayModeWrap.test.ts | 2 +- .../fork/displayModeWrap/DisplayModeWrap.ts | 14 +- .../src/fork/userMessageTools/SendToUser.ts | 136 +++++++++++--- .../SendToUserSubThreads.test.ts | 171 ++++++++++++++++-- .../fork/userMessageTools/replyStage.test.ts | 48 +++++ .../src/fork/userMessageTools/replyStage.ts | 20 ++ .../src/fork/userView/userViewText.test.ts | 13 +- apps/server/src/fork/userView/userViewText.ts | 3 +- .../src/provider/agentContextBudget.json | 2 +- .../fork/subThreads/subThreadTimeline.test.ts | 15 +- .../fork/displayModes/displayModes.test.ts | 81 ++++++++- .../src/fork/displayModes/displayModes.ts | 44 +++-- 15 files changed, 474 insertions(+), 81 deletions(-) create mode 100644 apps/server/src/fork/userMessageTools/replyStage.test.ts diff --git a/README.md b/README.md index d30eeeb0e6f8d..d2f920aa50eeb 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ This is a fork of T3 Code that integrates a few additional features, including: - **Better support for manually switching accounts mid-session:** - After hitting the usage limit of an account, the thread now retries after 2 minutes and 30 minutes in addition to the default retry behavior of T3 Code. This allows the thread to recover earlier if the user manually switches the account. - New Codex/Antigravity logins are picked up mid-session without a full restart. -- **Display mode per thread:** Right-click a thread and set Display mode to "Chat mode" to hide the agent's work and keep the chat simple: you see just your messages and the replies the agent sends to you with its `send_to_user` tool, and the agent answers each message where you wrote it, so a question in the main list gets its answer in the main list. "Normal mode" shows everything again. The mode applies in the web and desktop apps, not in the mobile app. +- **Display mode per thread:** Right-click a thread and set Display mode to "Chat mode" to hide the agent's work and keep the chat simple: you see just your messages and the replies the agent sends to you with its `send_to_user` tool, and the agent answers each message where you wrote it, so a question in the main list gets its answer in the main list. If you send another message in the main list before the agent answers, a separate answer to the earlier message shows in that message's replies, which open from the reply line under it. "Normal mode" shows everything again. The mode applies in the web and desktop apps, not in the mobile app. - To follow up on one of the agent's messages without filling the main list, point at it and choose Reply. The replies show in a Replies tab in the right panel (+ → Replies, or the reply line under the message), which has its own reply box, and the agent answers you there. Under a message with replies you see how many it has, how many are new, and a few generated words on what they are about. The agent names the message it answers by its id, with `send_to_user`. A Cite in the main list on a message that already has replies opens those replies and puts the quote in their reply box, unless the main box or the open reply box already has text; then the quote goes there, the main box first. A Cite on any other message goes into the main box. An agent reads the thread as you see it with its `t3_thread_user_view` tool, and to brief another agent in your own words it passes on the result's last line, which shows that agent the same messages and says if they changed since. - In Chat mode the agent sends one reply per message you type, and before a long task it may first send a short "on it" note; a thin line parts two agent messages in a row. To say more, it rewrites its reply in place with its `replace_send_to_user` tool. A rewritten reply shows a small "Edited" mark, and in a sub-thread it counts as new. - While the agent works, Chat mode shows "Working for" and the time just above the message box, in the sub-thread where you last wrote to it in that run, or else in the main list. diff --git a/apps/server/src/fork/displayModeWrap/DisplayModeCompactionNote.test.ts b/apps/server/src/fork/displayModeWrap/DisplayModeCompactionNote.test.ts index 43c633e8a49ec..e4bf3a8945b6f 100644 --- a/apps/server/src/fork/displayModeWrap/DisplayModeCompactionNote.test.ts +++ b/apps/server/src/fork/displayModeWrap/DisplayModeCompactionNote.test.ts @@ -60,7 +60,7 @@ it.layer(notes)("DisplayModeCompactionNote", (it) => { assert.deepEqual( (yield* toldRows).map((row) => [row.thread_id, row.mode_id]), [ - [chatId, "chat-replies@3"], + [chatId, "chat-replies@4"], [dashboardId, "normal+dashboard@2"], [toldNormalId, "normal"], [unknownToldId, "normal"], diff --git a/apps/server/src/fork/displayModeWrap/DisplayModeWrap.integration.test.ts b/apps/server/src/fork/displayModeWrap/DisplayModeWrap.integration.test.ts index 7149b5b882cda..f1fc65c6bceb4 100644 --- a/apps/server/src/fork/displayModeWrap/DisplayModeWrap.integration.test.ts +++ b/apps/server/src/fork/displayModeWrap/DisplayModeWrap.integration.test.ts @@ -32,7 +32,7 @@ const inMainList = (messageId: string, text: string) => [ "", `Message "${shortId(messageId)}" from the user in the main list.`, - `To answer, call send_to_user with reply_to "${shortId(messageId)}" and the user's words below as user_message. Your answer shows in the main list.`, + `To answer, call send_to_user with reply_to "${shortId(messageId)}" and the user's words below as user_message. Your answer shows in the main list, or in this message's sub-thread if the user sends a later main-list message before you answer.`, "", text, ].join("\n"); diff --git a/apps/server/src/fork/displayModeWrap/DisplayModeWrap.test.ts b/apps/server/src/fork/displayModeWrap/DisplayModeWrap.test.ts index 872196106e253..aca3ec1a92f87 100644 --- a/apps/server/src/fork/displayModeWrap/DisplayModeWrap.test.ts +++ b/apps/server/src/fork/displayModeWrap/DisplayModeWrap.test.ts @@ -316,7 +316,7 @@ const inMainList = (id: string, text: string) => [ "", `Message "${shortId(id)}" from the user in the main list.`, - `To answer, call send_to_user with reply_to "${shortId(id)}" and the user's words below as user_message. Your answer shows in the main list.`, + `To answer, call send_to_user with reply_to "${shortId(id)}" and the user's words below as user_message. Your answer shows in the main list, or in this message's sub-thread if the user sends a later main-list message before you answer.`, "", text, ].join("\n"); diff --git a/apps/server/src/fork/displayModeWrap/DisplayModeWrap.ts b/apps/server/src/fork/displayModeWrap/DisplayModeWrap.ts index df818a71fb691..44427ff888626 100644 --- a/apps/server/src/fork/displayModeWrap/DisplayModeWrap.ts +++ b/apps/server/src/fork/displayModeWrap/DisplayModeWrap.ts @@ -9,6 +9,7 @@ import { } from "@t3tools/contracts"; import { displayEventFromTurnItem, + displayEventsOf, displayNote, isMainListOnItsOwn, isWebTypedMessage, @@ -452,16 +453,9 @@ export const layer: Layer.Layer< const events = under === undefined ? [] - : (yield* shownItems( - threadId, - message.id, - undefined, - SUB_THREAD_WINDOW_CORES, - false, - )).flatMap((item) => { - const event = displayEventFromTurnItem(item); - return event === null ? [] : [event]; - }); + : displayEventsOf( + yield* shownItems(threadId, message.id, undefined, SUB_THREAD_WINDOW_CORES, false), + ); const wrapped = mode.wrap === null ? text : mode.wrap(events, text, { id: message.id, under }); if (under !== undefined) { diff --git a/apps/server/src/fork/userMessageTools/SendToUser.ts b/apps/server/src/fork/userMessageTools/SendToUser.ts index 957745f09200f..185d2f1f019a5 100644 --- a/apps/server/src/fork/userMessageTools/SendToUser.ts +++ b/apps/server/src/fork/userMessageTools/SendToUser.ts @@ -7,6 +7,7 @@ import { expandAssistantCitationsForProvider } from "@t3tools/shared/assistantCi import { ACK_MAX_BYTES, displayEventFromTurnItem, + displayEventsOf, foldCores, freshFirstPointer, isMainListOnItsOwn, @@ -42,6 +43,7 @@ import * as SubThreads from "../subThreads/SubThreads.ts"; import { judgeReplace, judgeSend, + isUnread, makeReplyHolds, type ReplyCall, replyCallOf, @@ -88,6 +90,7 @@ const MatchInput = (description: string) => /** How long an accepted send counts while its call is not yet in the history, for calls not named. */ const UNNAMED_SEND_HOLD_MS = 10_000; +const UNREAD_TOLD_MS = 10 * 60_000; const MIN_PERCENT = Math.round(TEXT_MATCH_MIN_RATIO * 100); const DONE = "If you are done, end your turn with no text."; const SHOWN = { ok: true as const, message: `Shown to the user. ${DONE}` }; @@ -114,7 +117,7 @@ const SendToUserTool = Tool.make(SEND_TO_USER_TOOL_NAME, { parameters: Schema.Struct({ text: SendText, reply_to: Pointer( - "Chat mode only. The id of the user's message that this text answers, as the block before that message gave it. The answer shows where that message is: in the main list or in its sub-thread.", + "Chat mode only. The id of the user's message that this text answers, as the block before that message gave it. The answer shows where that message is, in the main list or in its sub-thread. If the user sent a later main-list message, an answer to an earlier one shows in the earlier message's sub-thread.", ), user_message: Schema.optional( Schema.NullOr( @@ -198,12 +201,11 @@ function itemsBefore(shown: ReadonlyArray, ownItem: str } const coresOf = (items: ReadonlyArray) => - foldCores( - items.flatMap((item) => { - const event = displayEventFromTurnItem(item); - return event === null ? [] : [event]; - }), - ); + foldCores(displayEventsOf(items)); + +const sweep = (expiries: Map, now: number) => { + for (const [key, until] of expiries) if (until <= now) expiries.delete(key); +}; /** The calls toward the answer to the user message at `at`: those after it that `concerns` keeps. */ function replyCallsAfter( @@ -441,6 +443,11 @@ export const layerHandlers = McpToolAccess.toLayer( // few seconds instead, while the provider has not yet stored it, for that user message only. /** The ids given to first messages that may not be in the history yet, each with its hold's end. */ const givenIds = new Map(); + /** + * Unread user messages that a refusal already showed, by thread and message id, each with its + * expiry. + */ + const toldUnread = new Map(); const holds = makeReplyHolds(UNNAMED_SEND_HOLD_MS); const holdKey = (threadId: ThreadId, place: string) => `${threadId}\n${place}`; @@ -456,11 +463,13 @@ export const layerHandlers = McpToolAccess.toLayer( .latestShownItems(threadId, toolUseId, SUB_THREAD_WINDOW_CORES, toMainListMessage) .pipe(Effect.mapError(orchestrationError)); const items = itemsBefore(shown, toolUseId); - // `cores` is what the agent saw when it made the call. `coresNow` has what the user typed since. + const events = displayEventsOf(items); + // `cores` is what the agent saw when it made the call. `shown` has what the user typed since. return { items, - cores: coresOf(items), - coresNow: coresOf(shown), + events, + cores: foldCores(events), + shown, named: toolUseId !== undefined, }; }); @@ -482,7 +491,7 @@ export const layerHandlers = McpToolAccess.toLayer( }, ) { const replyTo = messagePointer(input.reply_to); - const { items, cores, named } = yield* readThreadCores( + const { items, events, cores, named } = yield* readThreadCores( threadId, replyTo === undefined && input.new_sub_thread === true && input.ack !== true, ); @@ -526,7 +535,7 @@ export const layerHandlers = McpToolAccess.toLayer( holds.record(mainKey, named, "answer", "answered", now); // A user message to the main list between two first messages can come before the first is // in the history, so the ids given for a while count as taken. - for (const [id, until] of givenIds) if (until <= now) givenIds.delete(id); + sweep(givenIds, now); const id = freshFirstPointer( new Set([ ...cores.map((core) => core.shortId), @@ -566,12 +575,14 @@ export const layerHandlers = McpToolAccess.toLayer( } // `/compact` is never the latest message, except when it is the one being answered. const counts = owesAnswer(target) ? owesAnswer : (core: VisibleCore) => core.from === "user"; - const latest = cores.findLast((core) => counts(core) && samePlace(core, target)); - if (latest !== target) { + const later = cores + .slice(cores.indexOf(target) + 1) + .filter((core) => counts(core) && samePlace(core, target)); + if (target.reply && later.length > 0) { return yield* placeRefusal( "invalid_request", [ - `the user sent a later message, "${latest?.shortId}", ${target.reply ? "in this sub-thread" : "in the main list"}. Answer that one.`, + `the user sent a later message, "${later.at(-1)?.shortId}", in this sub-thread. Answer that one.`, ], cores, target, @@ -591,20 +602,90 @@ export const layerHandlers = McpToolAccess.toLayer( }); if (!verdict.ok) return yield* placeRefusal("invalid_request", [verdict.reason], cores, target, []); - holds.record(key, named, input.ack === true ? "ack" : "answer", verdict.next, now); - if (input.ack === true) return ACKED; - if (target.reply && target.first !== null) - yield* subThreadChanged( - threadId, + sweep(toldUnread, now); + const userItems = new Map( + items.flatMap((item) => + item.type === "user_message" ? [[String(item.messageId), item] as const] : [], + ), + ); + // An answer or ack to a later message shows that the agent read it. + const unread = later.filter((core) => { + const item = userItems.get(core.id); + return ( + item !== undefined && + isUnread(item) && + !toldUnread.has(`${threadId}\n${core.id}`) && + !cores + .slice(cores.indexOf(core) + 1) + .some((reply) => reply.from === "agent" && reply.replyTo === core.shortId) + ); + }); + if (unread.length > 0) { + for (const core of unread) toldUnread.set(`${threadId}\n${core.id}`, now + UNREAD_TOLD_MS); + return yield* placeRefusal( + "invalid_request", + [ + `your answer was not shown because the user wrote in the main list after "${replyTo}", and you may not have read what they sent yet.`, + ], + cores, + undefined, [ - ...subThreadCores(cores, target.first), - { id: "", from: "agent", text: input.text, first: target.first, reply: true }, + "Later main-list messages from the user, by id:", + ...unread.map((core) => `"${core.shortId}": ${recapText(core.text)}`), + `Read what the user sent first. If it is on the same topic, give one combined answer to the latest message only. If it is a separate topic, call send_to_user again with reply_to "${replyTo}", and your answer shows in that message's sub-thread.`, ], - target.first, ); + } + const shown = foldCores([ + ...events, + { + type: "sent", + id: "", + text: input.text, + replyTo, + ...(input.ack === true && { ack: true }), + }, + ]); + const placed = shown.at(-1)!; + const late = placed.reply && !target.reply; + // A late ack opens the sub-thread, so the user may already have written there. + const replied = late + ? cores.findLast( + (core) => + owesAnswer(core) && core.reply && core.first === placed.first && core !== target, + ) + : undefined; + if (replied !== undefined) { + return yield* placeRefusal( + "invalid_request", + [ + `the user sent a later message, "${replied.shortId}", in this sub-thread. Answer that one.`, + ], + cores, + replied, + [], + ); + } + holds.record(key, named, input.ack === true ? "ack" : "answer", verdict.next, now); + if (input.ack === true) + return late + ? { + ok: true as const, + message: `Shown to the user as an ack for "${replyTo}", in that message's sub-thread, because the user wrote later in the main list. Send your answer when it is ready.`, + } + : ACKED; + if (placed.reply && placed.first !== null) + yield* subThreadChanged(threadId, subThreadCores(shown, placed.first), placed.first); + if (!late) + return { + ok: true as const, + message: `Shown to the user as your answer to "${replyTo}", ${placed.reply ? "in its sub-thread" : "in the main list"}. ${DONE}`, + }; + const open = unansweredLines(shown); + const sent = `Shown to the user as your answer to "${replyTo}", in that message's sub-thread, because the user wrote later in the main list.`; return { ok: true as const, - message: `Shown to the user as your answer to "${replyTo}", ${target.reply ? "in its sub-thread" : "in the main list"}. ${DONE}`, + message: open.length === 0 ? `${sent} ${DONE}` : [sent, ...open].join("\n"), }; }); @@ -618,7 +699,8 @@ export const layerHandlers = McpToolAccess.toLayer( readonly new_text: string; }, ) { - const { items, cores, coresNow, named } = yield* readThreadCores(threadId, false); + const { items, cores, shown, named } = yield* readThreadCores(threadId, false); + const coresNow = coresOf(shown); const replyTo = messagePointer(input.reply_to); const subThread = messagePointer(input.sub_thread); if ((replyTo === undefined) === (subThread === undefined)) { @@ -681,7 +763,9 @@ export const layerHandlers = McpToolAccess.toLayer( // judgeSend still takes the answer after an ack that the user replied under. answerNow.ack && later.first === answerNow.id ? `the user has written after your ack for message "${replyTo}", so it stays as it is. Send your answer to "${replyTo}" with send_to_user and reply_to "${replyTo}".` - : `the user has written after your ${answerNow.ack ? "ack for" : "answer to"} message "${replyTo}", so it stays as it is. Answer their latest message with send_to_user and reply_to "${later.shortId}".`, + : answerNow.ack && !later.reply + ? `the user has written after your ack for message "${replyTo}", so it stays as it is. Answer their latest message with send_to_user and reply_to "${later.shortId}". You may still send your answer to "${replyTo}" with send_to_user and reply_to "${replyTo}". It shows in that message's sub-thread.` + : `the user has written after your ${answerNow.ack ? "ack for" : "answer to"} message "${replyTo}", so it stays as it is. Answer their latest message with send_to_user and reply_to "${later.shortId}".`, ], coresNow, later, diff --git a/apps/server/src/fork/userMessageTools/SendToUserSubThreads.test.ts b/apps/server/src/fork/userMessageTools/SendToUserSubThreads.test.ts index 2ab36f67666f0..61ef67836fd08 100644 --- a/apps/server/src/fork/userMessageTools/SendToUserSubThreads.test.ts +++ b/apps/server/src/fork/userMessageTools/SendToUserSubThreads.test.ts @@ -9,10 +9,18 @@ import { ThreadId, TurnItemId, } from "@t3tools/contracts"; -import { recapLine, shortId, SUB_THREAD_RECORD_KIND } from "@t3tools/shared/displayModes"; +import { + displayEventsOf, + foldCores, + recapLine, + shortId, + SUB_THREAD_RECORD_KIND, +} from "@t3tools/shared/displayModes"; import * as DateTime from "effect/DateTime"; +import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; +import * as TestClock from "effect/testing/TestClock"; import { McpSchema, McpServer } from "effect/ai"; import * as McpHttpServer from "../../mcp/McpHttpServer.ts"; @@ -111,6 +119,15 @@ const answer = (id: string, text: string, to: string) => const ackOf = (id: string, text: string, to: string) => sent(id, { text, reply_to: shortId(to), user_message: "", ack: true }); const news = (id: string, text: string) => sent(id, { text, new_sub_thread: true }); +const steered = (id: string, text: string, fields: Record = {}) => + ({ + ...typed(id, text), + inputIntent: "steer", + completedAt: DateTime.add(time, { seconds: 1 }), + ...fields, + }) as OrchestrationV2TurnItem; +const coresOf = (items: ReadonlyArray) => + foldCores(displayEventsOf(items)); let history: ReadonlyArray = []; let changes: Array<{ readonly firstId: string; readonly lines: ReadonlyArray }> = []; @@ -480,16 +497,126 @@ it.effect("refuses words unlike the named message, with its place to check", () }).pipe(Effect.provide(layer)), ); -it.effect("refuses an answer to a message that the user followed up in the same place", () => +it.effect("answers an earlier main-list message in its sub-thread, after one unread refusal", () => Effect.gen(function* () { const q1 = { text: "Yes.", reply_to: shortId("q1"), user_message: "Is the build green?" }; - setup([typed("q1", "Is the build green?"), typed("q2", "And the lint job?")]); - expect(refusalLines(yield* call("send_to_user", q1))).toEqual([ - `Refused: the user sent a later message, "${shortId("q2")}", in the main list. Answer that one.`, - "The main list shows:", + const lateText = (id: string) => + `Shown to the user as your answer to "${shortId(id)}", in that message's sub-thread, because the user wrote later in the main list.`; + setup([typed("q1", "Is the build green?"), steered("q2", "And the lint job?")]); + expect((yield* call("send_to_user", q1)).structuredContent).toEqual({ + ok: true, + message: [ + lateText("q1"), + "Messages from the user with no answer yet, by id:", + `"${shortId("q2")}": And the lint job?`, + ].join("\n"), + }); + expect(changes).toEqual([ + { firstId: "q1", lines: ["[user] Is the build green?", "[you] Yes."] }, + ]); + expect( + coresOf([...history, answer("a1", "Yes.", "q1")]).find((core) => core.id === "a1"), + ).toMatchObject({ reply: true, first: "q1" }); + + const unreadRefusal = (target: string, later: string, text = "And the lint job?") => [ + `Refused: your answer was not shown because the user wrote in the main list after "${shortId(target)}", and you may not have read what they sent yet.`, + "Later main-list messages from the user, by id:", + `"${shortId(later)}": ${text}`, + `Read what the user sent first. If it is on the same topic, give one combined answer to the latest message only. If it is a separate topic, call send_to_user again with reply_to "${shortId(target)}", and your answer shows in that message's sub-thread.`, + ]; + const pending = { ...q1, reply_to: shortId("p1") }; + setup([ + typed("p1", "Is the build green?"), + steered("p2", "One more thing:\nAnd the lint job?", { status: "pending", completedAt: null }), + ]); + expect(refusalLines(yield* call("send_to_user", pending))).toEqual( + unreadRefusal("p1", "p2", "One more thing:\nAnd the lint job?").flatMap((line) => + line.split("\n"), + ), + ); + expect((yield* call("send_to_user", pending)).structuredContent).toMatchObject({ + message: expect.stringContaining(lateText("p1")), + }); + // A second answer is refused as one, not as an unread steer that a retry could not pass. + setup([ + typed("d1", "Is the build green?"), + answer("d3", "Yes.", "d1"), + steered("d2", "And the lint job?", { status: "pending", completedAt: null }), + ]); + expect( + refusalLines(yield* call("send_to_user", { ...q1, reply_to: shortId("d1") }))[0], + ).toContain("you already answered"); + const stored = { ...q1, reply_to: shortId("u1") }; + setup([ + typed("u1", "Is the build green?"), + steered("u2", "And the lint job?", { completedAt: time }), + ]); + expect(refusalLines(yield* call("send_to_user", stored))).toEqual(unreadRefusal("u1", "u2")); + setup([...history, { ...answer("r1", "Yes.", "u1"), output: { isError: true } }]); + expect((yield* call("send_to_user", stored)).structuredContent).toMatchObject({ + message: expect.stringContaining(lateText("u1")), + }); + + const expiring = { ...q1, reply_to: shortId("x1") }; + setup([ + typed("x1", "Is the build green?"), + steered("x2", "And the lint job?", { completedAt: time }), + ]); + expect(refusalLines(yield* call("send_to_user", expiring))).toEqual(unreadRefusal("x1", "x2")); + yield* TestClock.adjust(Duration.minutes(10)); + expect(refusalLines(yield* call("send_to_user", expiring))).toEqual(unreadRefusal("x1", "x2")); + // An answer to the later message shows that the agent read it. + setup([ + typed("w1", "Is the build green?"), + steered("w2", "And the lint job?", { completedAt: time }), + answer("w3", "Lint passes.", "w2"), + ]); + expect( + (yield* call("send_to_user", { ...q1, reply_to: shortId("w1") })).structuredContent, + ).toEqual(shown(lateText("w1"))); + setup([typed("l1", "Is the build green?"), typed("l2", "And the lint job?")]); + const lateAck = { ...q1, text: "Checking.", reply_to: shortId("l1"), ack: true }; + expect((yield* call("send_to_user", lateAck)).structuredContent).toEqual({ + ok: true, + message: `Shown to the user as an ack for "${shortId("l1")}", in that message's sub-thread, because the user wrote later in the main list. Send your answer when it is ready.`, + }); + expect(changes).toEqual([]); + setup([...history, ackOf("k5", "Checking.", "l1"), typed("l3", "Also the deploy?", "l1")]); + expect(refusalLines(yield* call("send_to_user", { ...q1, reply_to: shortId("l1") }))).toEqual([ + `Refused: the user sent a later message, "${shortId("l3")}", in this sub-thread. Answer that one.`, + "That message's sub-thread shows:", "[user] Is the build green?", - "[user] And the lint job?", + "[you] Checking.", + "[user] Also the deploy?", ]); + + const acked = { ...q1, reply_to: shortId("c1") }; + setup([ + typed("c1", "Is the build green?"), + ackOf("k1", "Checking.", "c1"), + typed("c2", "And the lint job?"), + answer("e2", "Lint passes.", "c2"), + ]); + expect((yield* call("send_to_user", acked)).structuredContent).toEqual(shown(lateText("c1"))); + expect(changes).toEqual([ + { firstId: "c1", lines: ["[user] Is the build green?", "[you] Yes."] }, + ]); + const replaceArgs = { + reply_to: shortId("c1"), + user_message: "Is the build green?", + old_text: "Yes.", + new_text: "Yes, all checks pass.", + }; + setup([...history, answer("c3", "Yes.", "c1")]); + expect((yield* call("replace_send_to_user", replaceArgs)).isError).not.toBe(true); + expect(changes).toEqual([ + { firstId: "c1", lines: ["[user] Is the build green?", "[you] Yes, all checks pass."] }, + ]); + setup([...history, typed("c4", "Why?", "c1")]); + expect(refusalLines(yield* call("replace_send_to_user", replaceArgs))[0]).toBe( + `Refused: the user has written after your answer to message "${shortId("c1")}", so it stays as it is. Answer their latest message with send_to_user and reply_to "${shortId("c4")}".`, + ); + setup([ news("n1", "The deploy is done."), typed("why", "Which build?", "n1"), @@ -511,8 +638,10 @@ it.effect("refuses an answer to a message that the user followed up in the same "[user] And when?", ]); // A reply that old history put under the question is in another place. - setup([typed("q1", "Is the build green?"), typed("q2", "And the lint job?", "q1")]); - expect((yield* call("send_to_user", q1)).isError).not.toBe(true); + setup([typed("o1", "Is the build green?"), typed("o2", "And the lint job?", "o1")]); + expect( + (yield* call("send_to_user", { ...q1, reply_to: shortId("o1") })).structuredContent, + ).toEqual(shown(`Shown to the user as your answer to "${shortId("o1")}", in the main list.`)); // `/compact` is not a later message. setup([ typed("q3", "Is the build green?"), @@ -703,6 +832,15 @@ it.effect("replaces the answer to the named message only", () => expect(refusalLines(yield* call("replace_send_to_user", acked))[0]).toBe( `Refused: the user has written after your ack for message "${shortId("q4")}", so it stays as it is. Send your answer to "${shortId("q4")}" with send_to_user and reply_to "${shortId("q4")}".`, ); + setup([ + typed("q6", "Is the build green?"), + ackOf("k6", "Checking.", "q6"), + typed("q7", "And the lint job?"), + ]); + const frozen = { ...q1, reply_to: shortId("q6"), old_text: "Checking." }; + expect(refusalLines(yield* call("replace_send_to_user", frozen))[0]).toBe( + `Refused: the user has written after your ack for message "${shortId("q6")}", so it stays as it is. Answer their latest message with send_to_user and reply_to "${shortId("q7")}". You may still send your answer to "${shortId("q6")}" with send_to_user and reply_to "${shortId("q6")}". It shows in that message's sub-thread.`, + ); // `/compact` is not a later message: the ack still becomes the answer. setup([ typed("q5", "Is the build green?"), @@ -841,12 +979,23 @@ it.effect("judges a named call by the history before it, and a replace by messag reply_to: shortId("q1"), user_message: "Is the build green?", }; + const unread = { status: "pending", completedAt: null }; setup([ typed("q1", "Is the build green?"), running("s1", "send_to_user", sendArgs), - typed("q2", "And the lint job?"), + steered("q2", "And the lint job?", unread), ]); - expect((yield* call("send_to_user", sendArgs, repliesThread, "s1")).isError).not.toBe(true); + expect((yield* call("send_to_user", sendArgs, repliesThread, "s1")).structuredContent).toEqual( + shown(`Shown to the user as your answer to "${shortId("q1")}", in the main list.`), + ); + setup([ + typed("q1", "Is the build green?"), + steered("q2", "And the lint job?", unread), + running("s1", "send_to_user", sendArgs), + ]); + expect(refusalLines(yield* call("send_to_user", sendArgs, repliesThread, "s1"))[0]).toContain( + "you may not have read what they sent yet", + ); // A replace is refused: the user's message was written after the answer as it was. const replaceArgs = { reply_to: shortId("q1"), diff --git a/apps/server/src/fork/userMessageTools/replyStage.test.ts b/apps/server/src/fork/userMessageTools/replyStage.test.ts new file mode 100644 index 0000000000000..8da9af9f6707d --- /dev/null +++ b/apps/server/src/fork/userMessageTools/replyStage.test.ts @@ -0,0 +1,48 @@ +import { MessageId, ThreadId, TurnItemId } from "@t3tools/contracts"; +import * as DateTime from "effect/DateTime"; +import { describe, expect, it } from "vite-plus/test"; + +import { isUnread } from "./replyStage.ts"; + +type UserMessage = Parameters[0]; + +const stored = DateTime.makeUnsafe("2026-11-22T00:00:00.000Z"); +const read = DateTime.makeUnsafe("2026-11-22T00:00:03.000Z"); + +const message = (id: string, fields: Partial = {}): UserMessage => ({ + id: TurnItemId.make(id), + threadId: ThreadId.make("reply-stage"), + runId: null, + nodeId: null, + providerThreadId: null, + providerTurnId: null, + nativeItemRef: null, + parentItemId: null, + ordinal: 0, + status: "completed", + title: null, + startedAt: stored, + completedAt: stored, + updatedAt: stored, + type: "user_message", + messageId: MessageId.make(id), + inputIntent: "steer", + text: "And the lint job?", + attachments: [], + createdBy: "user", + creationSource: "web", + ...fields, +}); + +describe("isUnread", () => { + it.each]>([ + ["a pending steer", true, { status: "pending", completedAt: null }], + ["a cancelled steer", true, { status: "cancelled" }], + ["a steer that no provider moved past the time it was stored", true, {}], + ["a promoted steer that no provider moved", true, { inputIntent: "promoted_queued_to_steer" }], + ["a steer that a provider read later", false, { completedAt: read }], + ["a message that started a turn", false, { inputIntent: "turn_start" }], + ])("%s is unread: %s", (_name, unread, fields) => { + expect(isUnread(message("m", fields))).toBe(unread); + }); +}); diff --git a/apps/server/src/fork/userMessageTools/replyStage.ts b/apps/server/src/fork/userMessageTools/replyStage.ts index 1acd67fd2fb65..aeb6705b7e431 100644 --- a/apps/server/src/fork/userMessageTools/replyStage.ts +++ b/apps/server/src/fork/userMessageTools/replyStage.ts @@ -6,6 +6,7 @@ import { SEND_TO_USER_TOOL_NAME, } from "@t3tools/shared/displayModes"; import { resolveT3McpToolId } from "@t3tools/shared/t3McpToolPresentation"; +import * as DateTime from "effect/DateTime"; /** How far the agent's answer to one user message has come. It only moves forward. */ export type ReplyStage = "open" | "acked" | "answered"; @@ -95,6 +96,25 @@ export function judgeSend(input: { : { ok: false, reason: REFUSALS[step.refused](input.subject) }; } +type UserMessageItem = Extract; + +const STEERS: ReadonlySet = new Set([ + "steer", + "promoted_queued_to_steer", +]); + +const epochMillis = (at: DateTime.Utc | null) => (at === null ? null : DateTime.toEpochMillis(at)); + +/** + * Whether `item` is a steer that no provider has confirmed reading, because it is pending, was + * cancelled, or never moved past the time it was stored. + */ +export const isUnread = (item: UserMessageItem): boolean => + STEERS.has(item.inputIntent) && + (item.status === "pending" || + item.status === "cancelled" || + epochMillis(item.completedAt) === epochMillis(item.startedAt)); + /** * Whether a replace may go out now. It checks its old text against the delivered messages, so it * waits while an accepted send is not delivered yet. A replace always leaves an answer. diff --git a/apps/server/src/fork/userView/userViewText.test.ts b/apps/server/src/fork/userView/userViewText.test.ts index 98ef97bc2a243..d759cd4a0cb4c 100644 --- a/apps/server/src/fork/userView/userViewText.test.ts +++ b/apps/server/src/fork/userView/userViewText.test.ts @@ -429,7 +429,7 @@ describe("userViewText", () => { expect(textOf(view(conversation(), { subThread: S5 }))).not.toContain("replies]"); }); - it("shows replies typed under a user's own message, as threads from before in-place answers have", () => { + it("shows replies under a user's own message: a late answer, or replies from before in-place answers", () => { const q = shortId("message:1"); const r = shortId("message:3"); const items = [ @@ -444,6 +444,17 @@ describe("userViewText", () => { `Sub-thread under [user ${q}, 2026-11-15T16:48:00.000Z]:\n\n[user ${r}, 2026-11-15T16:48:20.000Z]\n In a microwave?\n\n[agent, answer to ${r}, 2026-11-15T16:48:30.000Z]\n About 15 seconds.\n`, ); expect(textOf(view(items, { subThread: r }))).toBe(thread); + const late = [ + user(1, 0, "How long to toast nori?"), + user(2, 10, "And in a pan?"), + send(3, 20, "A few seconds a side.", { reply_to: q }), + ]; + const main = textOf(view(late, {})); + expect(main).toContain(`[user ${q}, 2026-11-15T16:48:00.000Z, 1 reply]`); + expect(main).not.toContain(`answer to ${q}`); + expect(textOf(view(late, { subThread: q }))).toContain( + `Sub-thread under [user ${q}, 2026-11-15T16:48:00.000Z]:\n\n[agent, answer to ${q}, 2026-11-15T16:48:20.000Z]\n A few seconds a side.\n`, + ); }); it("names the messages before the main-list window in an Earlier line that skips none", () => { diff --git a/apps/server/src/fork/userView/userViewText.ts b/apps/server/src/fork/userView/userViewText.ts index 4805524e934ff..d84c782d809a4 100644 --- a/apps/server/src/fork/userView/userViewText.ts +++ b/apps/server/src/fork/userView/userViewText.ts @@ -392,7 +392,8 @@ function rootOf( if (first === undefined) return complete ? nowhere : { type: "more" }; return first.reply ? nowhere : first; } - // Threads from before answers moved in place have replies under the user's own messages. + // A user message has replies when the agent answered it late, or in threads from before answers + // moved in place. if (named.from === "agent" || cores.some((core) => core.reply && core.first === named.id)) return named; const answers = cores.filter( diff --git a/apps/server/src/provider/agentContextBudget.json b/apps/server/src/provider/agentContextBudget.json index 99ee9f1c10f34..19722f2d64269 100644 --- a/apps/server/src/provider/agentContextBudget.json +++ b/apps/server/src/provider/agentContextBudget.json @@ -1,5 +1,5 @@ { "claudeSystemAppendBytes": 5945, "codexAdditionalContextBytes": 7339, - "mcpToolListJsonBytes": 64573 + "mcpToolListJsonBytes": 64688 } diff --git a/apps/web/src/fork/subThreads/subThreadTimeline.test.ts b/apps/web/src/fork/subThreads/subThreadTimeline.test.ts index 2bfa9fffc3286..c88c813c439f7 100644 --- a/apps/web/src/fork/subThreads/subThreadTimeline.test.ts +++ b/apps/web/src/fork/subThreads/subThreadTimeline.test.ts @@ -132,8 +132,21 @@ const readAt = (at: string | null) => new Map([["q1", { firstId: "q1", readAt: at, summary: null }]]); describe("Chat mode rows", () => { - it("draws the main list with each answer where its question is", () => { + it("draws the main list with each answer where its question is, or under it if answered late", () => { expect(ids(applyDisplayMode(CHAT_REPLIES_DISPLAY_MODE, entries))).toEqual(["q1", "news", "a1"]); + const late = entriesOf([ + typed("t", 0, "Is the build green?"), + typed("n", 1, "And the lint job?"), + sent("a", 2, { text: "Yes.", reply_to: shortId("t"), user_message: "x" }), + ]); + expect(ids(applyDisplayMode(CHAT_REPLIES_DISPLAY_MODE, late))).toEqual(["t", "n"]); + expect(ids(applyDisplayMode(CHAT_REPLIES_DISPLAY_MODE, late, "t"))).toEqual(["t", "a"]); + expect(timelineSubThreads(late, null).get("t")).toMatchObject({ + firstFrom: "user", + replies: 1, + lastFrom: "agent", + lastText: "Yes.", + }); }); it("draws a sub-thread as its first message and its replies", () => { diff --git a/packages/shared/src/fork/displayModes/displayModes.test.ts b/packages/shared/src/fork/displayModes/displayModes.test.ts index 32bcdaa7f840b..c2becae9539d2 100644 --- a/packages/shared/src/fork/displayModes/displayModes.test.ts +++ b/packages/shared/src/fork/displayModes/displayModes.test.ts @@ -35,6 +35,7 @@ import { subThreadCores, threadDisplay, toldKey, + type VisibleCore, withoutRemovedDisplayModes, } from "./displayModes.ts"; @@ -369,7 +370,7 @@ describe("sub-threads", () => { answer("A2", "Q2"), ]; - it("puts an answer where the message it answers is", () => { + it("puts an answer where the message it answers is, or under it after a later main-list message", () => { const cores = foldCores([...events, replyUnder("More?", "sent:A1"), answer("Sure", "More?")]); expect(cores.filter((core) => !core.reply).map((core) => core.text)).toEqual([ "Q1", @@ -387,6 +388,68 @@ describe("sub-threads", () => { "More?", "Sure", ]); + + const textsOf = (shown: ReadonlyArray) => shown.map((core) => core.text); + const compact: DisplayEvent = { + type: "user", + id: "user:/compact", + text: "/compact", + compaction: true, + }; + const ack: DisplayEvent = { + type: "sent", + id: "sent:On it.", + text: "On it.", + replyTo: shortId("user:T"), + ack: true, + }; + const rewritten: DisplayEvent = { + type: "rewrite", + id: "rewrite:A2", + text: "A2", + replyTo: shortId("user:T"), + }; + const rows: ReadonlyArray< + readonly [ReadonlyArray, ReadonlyArray, ReadonlyArray] + > = [ + [ + [user("T"), user("N"), answer("A", "T")], + ["T", "N"], + ["T", "A"], + ], + [[user("T"), answer("A", "T"), user("N")], ["T", "A", "N"], ["T"]], + [[user("T"), compact, answer("A", "T")], ["T", "/compact", "A"], ["T"]], + [ + [user("T"), sent("News"), replyUnder("X", "sent:News"), answer("A", "T")], + ["T", "News", "A"], + ["T"], + ], + [ + [user("T"), ack, user("N"), answer("A", "T"), rewritten], + ["T", "On it.", "N"], + ["T", "A2"], + ], + [ + [user("T"), user("N"), answer("A", "T"), replyUnder("R", "user:T"), answer("AR", "R")], + ["T", "N"], + ["T", "A", "R", "AR"], + ], + ]; + const before = [user("P"), sent("Old news"), answer("AP", "P"), user("Q")]; + const placesFromT = (shown: ReadonlyArray) => + shown + .slice(shown.findIndex((core) => core.id === "user:T")) + .map(({ id, first, reply }) => ({ id, first, reply })); + for (const [row, main, underT] of rows) { + const shown = foldCores(row); + expect(textsOf(shown.filter((core) => !core.reply))).toEqual(main); + expect(textsOf(subThreadCores(shown, "user:T"))).toEqual(underT); + const history = [...before, ...row]; + for (let start = 0; start <= before.length; start++) + expect(placesFromT(foldCores(history.slice(start))), `window from ${start}`).toEqual( + placesFromT(foldCores(history)), + ); + } }); it("gives an answer no sub-thread when the message it answers is not among the events", () => { @@ -461,7 +524,7 @@ describe("sub-threads", () => { [ "", `Message "${shortId("user:Q3")}" from the user in the main list.`, - `To answer, call send_to_user with reply_to "${shortId("user:Q3")}" and the user's words below as user_message. Your answer shows in the main list.`, + `To answer, call send_to_user with reply_to "${shortId("user:Q3")}" and the user's words below as user_message. Your answer shows in the main list, or in this message's sub-thread if the user sends a later main-list message before you answer.`, "", "Q3", ].join("\n"), @@ -615,18 +678,18 @@ describe("displayNote", () => { const CHAT = CHAT_REPLIES_DISPLAY_MODE; it.each | null]>([ - ["chat-replies@3+dashboard@2", "chat-replies", true, null], + ["chat-replies@4+dashboard@2", "chat-replies", true, null], ["normal", "chat-replies", false, [CHAT.notice]], ["normal", "normal", true, [ON]], - ["chat-replies@3+dashboard@2", "chat-replies", false, [OFF]], + ["chat-replies@4+dashboard@2", "chat-replies", false, [OFF]], ["normal", "chat-replies", true, [CHAT.notice, ON]], - ["chat-replies@3", "normal", true, [NORMAL.notice, ON]], + ["chat-replies@4", "normal", true, [NORMAL.notice, ON]], ["unknown", "chat-replies", true, [CHAT.stateNotice, ON_STATE]], ["messages-dashboard", "chat-replies", true, [CHAT.stateNotice, ON_STATE]], ["messages-dashboard", "chat-replies", false, [CHAT.stateNotice, OFF_STATE]], - ["chat-replies@2", "chat-replies", false, [CHAT.stateNotice, OFF_STATE]], - ["chat-replies@3+dashboard", "chat-replies", true, [CHAT.stateNotice, ON_STATE]], - ["chat-replies@3+dashboard", "chat-replies", false, [CHAT.stateNotice, OFF_STATE]], + ["chat-replies@3", "chat-replies", false, [CHAT.stateNotice, OFF_STATE]], + ["chat-replies@4+dashboard", "chat-replies", true, [CHAT.stateNotice, ON_STATE]], + ["chat-replies@4+dashboard", "chat-replies", false, [CHAT.stateNotice, OFF_STATE]], ])("brings an agent told %s up to %s with the Dashboard on: %s", (told, modeId, on, notes) => { expect(displayNote(told, displayOf(resolveDisplayMode(modeId), on))).toBe( notes?.join("\n") ?? null, @@ -641,7 +704,7 @@ describe("displayNote", () => { it("records what the agent was told in one key per display", () => { expect([NORMAL_DISPLAY, displayOf(CHAT, true)].map(toldKey)).toEqual([ "normal", - "chat-replies@3+dashboard@2", + "chat-replies@4+dashboard@2", ]); }); }); diff --git a/packages/shared/src/fork/displayModes/displayModes.ts b/packages/shared/src/fork/displayModes/displayModes.ts index f44c119b5ac41..ff8dc67a10820 100644 --- a/packages/shared/src/fork/displayModes/displayModes.ts +++ b/packages/shared/src/fork/displayModes/displayModes.ts @@ -103,9 +103,10 @@ export interface VisibleCore { */ readonly first: string | null; /** - * Whether the core is not in the main list: it shows in the sub-thread of `first`, or nowhere when + * Whether the core is not in the main list. It shows in the sub-thread of `first`, or nowhere when * `first` is null. A user message is a reply when it carries a `fork-reply` record. An answer is a - * reply when the message it answers is one. + * reply when the message it answers is one, or when the user sent a later main-list message before + * the answer. */ readonly reply: boolean; /** @@ -160,10 +161,10 @@ export interface DisplayMode { | ((events: ReadonlyArray, text: string, message: WrappedMessage) => string) | null; /** - * Whether the user can reply under one of the agent's main-list messages, in its sub-thread. The - * main list shows the cores that are not replies. The agent answers each message that the user - * types once, by its short id, after at most one ack, and the answer shows where that message is. - * To say more, it rewrites that answer with `replace_send_to_user`. + * Whether the user can reply in a main-list message's sub-thread. The main list shows the cores that + * are not replies. The agent answers each message the user types once, by its short id, after at most + * one ack. The answer shows where that message is, or in its sub-thread when the user sent a later + * main-list message first. To say more, the agent rewrites the answer with `replace_send_to_user`. */ readonly subThreads: boolean; } @@ -208,6 +209,7 @@ export function foldCores( const answerByReplyTo = new Map(); const agentFirstByShortId = new Map(); const unplacedAnswers = new Map(); + let latestMainUser = -1; for (const event of events) { switch (event.type) { case "user": { @@ -216,8 +218,7 @@ export function foldCores( if (root !== undefined) cores[root] = { ...cores[root]!, first: event.under!, reply: false }; const id = shortId(event.id); - userByShortId.set( - id, + const at = cores.push({ id: event.id, from: "user", @@ -226,8 +227,9 @@ export function foldCores( reply: event.under !== undefined, shortId: id, ...(event.compaction && { compaction: event.compaction }), - }) - 1, - ); + }) - 1; + userByShortId.set(id, at); + if (isMainListOnItsOwn(event)) latestMainUser = at; break; } case "sent": @@ -247,10 +249,12 @@ export function foldCores( } else { const answered = userByShortId.get(event.replyTo); const question = answered === undefined ? undefined : cores[answered]!; - // An answer shows where the message it answers is. With that message not among the - // events, its place is unknown and it shows nowhere until a reply under it places it; - // ShownHistory loads that message with it. - const inMainList = question !== undefined && !question.reply; + // An answer shows where the message it answers is. If a later main-list user message came + // first, an answer to an earlier main-list message shows in the answered message's + // sub-thread. With the answered message not among the events, its place is unknown and it + // shows nowhere until a reply under it places it; ShownHistory loads that message with it. + const inMainList = + question !== undefined && !question.reply && latestMainUser <= answered!; latestSent = cores.push({ id: event.id, @@ -357,18 +361,18 @@ function chatRepliesWrap( ? `Message "${id}" from the user, a reply in a sub-thread.` : `Message "${id}" from the user, a reply in the sub-thread that showed:`, ...shown.map(recapLine), - `To answer, call send_to_user with reply_to "${id}" and the user's words below as user_message. Your answer shows ${message.under === undefined ? "in the main list" : "in this sub-thread"}.`, + `To answer, call send_to_user with reply_to "${id}" and the user's words below as user_message. Your answer shows ${message.under === undefined ? "in the main list, or in this message's sub-thread if the user sends a later main-list message before you answer" : "in this sub-thread"}.`, "", text, ].join("\n"); } const CHAT_REPLIES_RULES = - "They see only their own messages and your send_to_user messages. The main list is one chat. The user can also choose Reply on one of your messages there and talk with you in its sub-thread. Each message from the user comes with a short id. To answer one, call send_to_user with reply_to set to its id and user_message set to its words; your answer shows where the user wrote it, in the main list or in that sub-thread. Answer only the user's latest message in each place, once. To change an answer, call replace_send_to_user with the same reply_to, until the user writes after it there or replies under it. To say something that answers no message, call send_to_user with new_sub_thread true: it shows in the main list, one until the user writes there again. To change that message, call replace_send_to_user with sub_thread set to the id that the send returned, until the user writes after it or replies under it."; + "They see only their own messages and your send_to_user messages. The main list is one chat. The user can also choose Reply on one of your messages there and talk with you in its sub-thread. Each message from the user comes with a short id. To answer one, call send_to_user with reply_to set to its id and user_message set to its words. Your answer shows where the user wrote it, in the main list or in that sub-thread. If the user sent a later main-list message, an answer to an earlier one shows in the earlier message's sub-thread. If that later message is on the same topic, give one combined answer to the latest message only. Otherwise, you may answer the earlier message in its sub-thread and the latest one in the main list. Answer each message at most once. In a sub-thread, answer only the latest message. To change an answer, call replace_send_to_user with the same reply_to, until the user writes after it where it shows or replies under it. To say something that answers no message, call send_to_user with new_sub_thread true. It shows in the main list, one until the user writes there again. To change that message, call replace_send_to_user with sub_thread set to the id the send returned, until the user writes after it or replies under it."; export const CHAT_REPLIES_DISPLAY_MODE: DisplayMode = { id: "chat-replies", - toldId: "chat-replies@3", + toldId: "chat-replies@4", label: "Chat mode", notice: `The user switched this thread to Chat mode. ${CHAT_REPLIES_RULES}`, stateNotice: `This thread is in Chat mode. ${CHAT_REPLIES_RULES}`, @@ -669,6 +673,12 @@ export function displayEventFromTurnItem(item: OrchestrationV2TurnItem): Display } } +export const displayEventsOf = (items: Iterable): DisplayEvent[] => + Array.from(items).flatMap((item) => { + const event = displayEventFromTurnItem(item); + return event === null ? [] : [event]; + }); + /** The text of a completed `send_to_user` call. Any other item has none. */ export function sentMessageText(item: OrchestrationV2TurnItem): string | null { const event = displayEventFromTurnItem(item);