Skip to content

Commit 3fd4269

Browse files
committed
fix(chat): fold question answers into tool activity
1 parent b7b3ef1 commit 3fd4269

13 files changed

Lines changed: 608 additions & 38 deletions

File tree

‎apps/mobile/src/features/threads/QuestionAnswerHistory.tsx‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
UserInputAttachments,
55
} from "@t3tools/contracts";
66
import { Image, Linking, Pressable, View } from "react-native";
7+
import { getQuestionAnswerText } from "@t3tools/client-runtime/work-log/user-input";
78
import { AppText as Text } from "../../components/AppText";
89
import { useAssetUrl } from "../../state/assets";
910

@@ -41,6 +42,7 @@ export function QuestionAnswerHistory(props: {
4142
<View className="gap-2">
4243
{[
4344
...new Set([
45+
...Object.keys(props.answer.questionTextById ?? {}),
4446
...Object.keys(props.answer.answers),
4547
...Object.keys(props.answer.attachmentsByQuestionId),
4648
]),
@@ -52,10 +54,7 @@ export function QuestionAnswerHistory(props: {
5254
</Text>
5355
) : null}
5456
<Text className="text-sm text-foreground">
55-
{[props.answer.answers[questionId]]
56-
.flat()
57-
.filter((value): value is string => typeof value === "string")
58-
.join(", ")}
57+
{getQuestionAnswerText(props.answer.answers[questionId])}
5958
</Text>
6059
{(props.answer.attachmentsByQuestionId[questionId] ?? []).map((attachment) => (
6160
<AnswerFile

‎apps/mobile/src/features/threads/thread-work-log.tsx‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { QuestionAnswerHistory } from "./QuestionAnswerHistory";
2+
import { getQuestionAnswerPreview } from "@t3tools/client-runtime/work-log/user-input";
23
import * as Haptics from "expo-haptics";
34
import { Image } from "expo-image";
45
import { type AppSymbolName, SymbolView } from "../../components/AppSymbol";
@@ -818,6 +819,11 @@ const ThreadWorkLogRow = memo(function ThreadWorkLogRow(
818819
numberOfLines={expanded ? undefined : 1}
819820
>
820821
{displayText}
822+
{row.workEntry.questionAnswer ? (
823+
<Text className="text-foreground-subtle">
824+
{` ${getQuestionAnswerPreview(row.workEntry.questionAnswer)}`}
825+
</Text>
826+
) : null}
821827
</Text>
822828
</>
823829
)}

‎apps/mobile/src/lib/threadActivity.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,55 @@ function makeThread(
276276
}
277277

278278
describe("buildThreadFeed", () => {
279+
it("folds question answers into tool history and only hides the matching async reply", () => {
280+
const createdAt = "2026-04-01T00:00:01.000Z";
281+
const activities = [
282+
makeActivity({
283+
id: EventId.make("question-request"),
284+
kind: "user-input.requested",
285+
summary: "User input requested",
286+
createdAt,
287+
turnId: TurnId.make("question-turn"),
288+
payload: {
289+
requestId: "question-1",
290+
questions: [{ id: "scope", question: "Which scope?" }],
291+
},
292+
}),
293+
makeActivity({
294+
id: EventId.make("question-response"),
295+
kind: "user-input.resolved",
296+
summary: "User input submitted",
297+
createdAt: "2026-04-01T00:00:02.000Z",
298+
turnId: TurnId.make("question-turn"),
299+
payload: { requestId: "question-1", answers: { scope: "Web" } },
300+
}),
301+
];
302+
const messages = ["async-answer:question-1", "real-user-message", "async-answer:unloaded"].map(
303+
(id) => ({
304+
id: MessageId.make(id),
305+
role: "user" as const,
306+
text: "Web",
307+
turnId: TurnId.make("answer-turn"),
308+
createdAt: "2026-04-01T00:00:03.000Z",
309+
updatedAt: "2026-04-01T00:00:03.000Z",
310+
streaming: false,
311+
}),
312+
);
313+
const feed = buildThreadFeed({ messages, activities });
314+
const rows = feed.flatMap((entry) => (entry.type === "activity-group" ? entry.activities : []));
315+
expect(rows).toHaveLength(1);
316+
expect(rows[0]?.workEntry).toMatchObject({
317+
tone: "tool",
318+
label: "User input submitted",
319+
questionAnswer: { questionTextById: { scope: "Which scope?" }, answers: { scope: "Web" } },
320+
});
321+
expect(feed.flatMap((entry) => (entry.type === "message" ? [entry.message.id] : []))).toEqual([
322+
"real-user-message",
323+
"async-answer:unloaded",
324+
]);
325+
expect(buildThreadFeed({ messages, activities: [] })).toHaveLength(3);
326+
});
327+
279328
it("reuses unchanged feed and presentation rows during an assistant text update", () => {
280329
const completedTurnId = TurnId.make("completed-turn");
281330
const activeTurnId = TurnId.make("active-turn");

‎apps/mobile/src/lib/threadActivity.ts‎

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as Option from "effect/Option";
2+
import { foldUserInputActivities } from "@t3tools/client-runtime/work-log/user-input";
23
import * as Schema from "effect/Schema";
34
import {
45
requestKindFromRequestType,
@@ -405,7 +406,7 @@ function deriveWorkLogEntries(
405406
): DerivedWorkLogEntry[] {
406407
const ordered = Arr.sort(activities, activityOrder);
407408
const entries: DerivedWorkLogEntry[] = [];
408-
for (const activity of ordered) {
409+
for (const activity of foldUserInputActivities(ordered)) {
409410
if (activity.tone !== "error" && isWorktreeSetupActivity(activity.kind)) continue;
410411
if (activity.kind === "tool.started") continue;
411412
// Like web: an agent's task.started row anchors its batch. It has a fixed
@@ -2184,21 +2185,30 @@ export function buildThreadFeed(
21842185
: loadedMessages;
21852186
const oldestLoadedMessageCreatedAt =
21862187
options?.loadedMessages !== undefined ? (loadedMessages[0]?.createdAt ?? null) : null;
2187-
const activityEntries = getThreadFeedActivityEntries(thread.activities);
2188+
const activityEntries = getThreadFeedActivityEntries(thread.activities).filter(
2189+
(entry) =>
2190+
oldestLoadedMessageCreatedAt === null || entry.createdAt >= oldestLoadedMessageCreatedAt,
2191+
);
2192+
const foldedAnswerMessageIds = new Set(
2193+
activityEntries.flatMap((entry) =>
2194+
entry.activity.workEntry.questionAnswer
2195+
? [`async-answer:${entry.activity.workEntry.questionAnswer.requestId}`]
2196+
: [],
2197+
),
2198+
);
21882199
const entries = Arr.sortWith(
21892200
[
2190-
...messages.map((message) => {
2191-
let entry = messageEntriesCache.get(message);
2192-
if (!entry) {
2193-
entry = { type: "message", id: message.id, createdAt: message.createdAt, message };
2194-
messageEntriesCache.set(message, entry);
2195-
}
2196-
return entry;
2197-
}),
2198-
...activityEntries.filter(
2199-
(entry) =>
2200-
oldestLoadedMessageCreatedAt === null || entry.createdAt >= oldestLoadedMessageCreatedAt,
2201-
),
2201+
...messages
2202+
.filter((message) => message.role !== "user" || !foldedAnswerMessageIds.has(message.id))
2203+
.map((message) => {
2204+
let entry = messageEntriesCache.get(message);
2205+
if (!entry) {
2206+
entry = { type: "message", id: message.id, createdAt: message.createdAt, message };
2207+
messageEntriesCache.set(message, entry);
2208+
}
2209+
return entry;
2210+
}),
2211+
...activityEntries,
22022212
],
22032213
(s) => new Date(s.createdAt),
22042214
Order.Date,

‎apps/server/src/orchestration/ActivityPayloadProjection.test.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,58 @@ function activity(payload: Record<string, unknown>): OrchestrationThreadActivity
2121
* assertions are the tripwire.
2222
*/
2323
describe("projectActivityPayload", () => {
24+
it.each([
25+
{
26+
provider: "claude",
27+
itemType: "dynamic_tool_call",
28+
data: {
29+
toolName: "AskUserQuestion",
30+
input: { questions: [{ question: "Which scope?", options: [{ label: "Web" }] }] },
31+
},
32+
},
33+
{
34+
provider: "opencode",
35+
itemType: "dynamic_tool_call",
36+
data: { tool: "question", state: { input: { questions: [{ question: "Which scope?" }] } } },
37+
},
38+
{
39+
provider: "cursor",
40+
itemType: "dynamic_tool_call",
41+
title: "cursor/ask_question",
42+
data: { rawInput: { questions: [{ prompt: "Which scope?" }] } },
43+
},
44+
{
45+
provider: "codex async",
46+
itemType: "mcp_tool_call",
47+
data: {
48+
item: {
49+
tool: "request_user_input_async",
50+
arguments: { questions: [{ title: "Which scope?" }] },
51+
},
52+
},
53+
},
54+
{
55+
provider: "mcp",
56+
itemType: "mcp_tool_call",
57+
data: {
58+
toolName: "mcp__questions__request_user_input",
59+
input: { questions: [{ question_text: "Which scope?" }] },
60+
},
61+
},
62+
])(
63+
"preserves $provider question text for client folding",
64+
({ provider: _provider, ...payload }) => {
65+
const projected = projectActivityPayload(activity(payload));
66+
expect(projected.payload).toMatchObject({
67+
data: { input: { questions: [{ question: "Which scope?" }] } },
68+
});
69+
expect((projected.payload as { data: { input: unknown } }).data.input).toEqual({
70+
questions: [{ question: "Which scope?" }],
71+
});
72+
expect(projectActivityPayload(projected)).toEqual(projected);
73+
},
74+
);
75+
2476
it("preserves tool attribution (agentId/parentToolUseId) through data slimming", () => {
2577
const projected = projectActivityPayload(
2678
activity({

‎apps/server/src/orchestration/ActivityPayloadProjection.ts‎

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,45 @@ function projectAcpContent(value: unknown): Record<string, unknown> | undefined
352352
return summary ? { content: summary } : undefined;
353353
}
354354

355+
function projectQuestionToolInput(data: Record<string, unknown>, title: unknown) {
356+
const item = asRecord(data.item);
357+
const toolName = data.toolName ?? data.tool ?? item?.tool ?? title;
358+
if (typeof toolName !== "string") return {};
359+
const name = toolName
360+
.split(/__|[./]/)
361+
.at(-1)
362+
?.replace(/[_\s]/g, "")
363+
.toLowerCase();
364+
if (
365+
name !== "askuserquestion" &&
366+
name !== "requestuserinput" &&
367+
name !== "requestuserinputasync" &&
368+
name !== "askquestion" &&
369+
name !== "question"
370+
)
371+
return {};
372+
const input = asRecord(
373+
data.input ?? data.rawInput ?? asRecord(data.state)?.input ?? item?.arguments,
374+
);
375+
const questions = input?.questions ?? asRecord(input?.params)?.questions;
376+
if (!Array.isArray(questions)) return {};
377+
// Clients match native tools to the canonical question; choices and answers
378+
// already live on the user-input activities and need not cross the wire twice.
379+
return {
380+
toolName,
381+
input: {
382+
questions: questions.map((value) => {
383+
const question = asRecord(value);
384+
return {
385+
question: asTrimmedString(
386+
question?.question ?? question?.question_text ?? question?.prompt ?? question?.title,
387+
),
388+
};
389+
}),
390+
},
391+
};
392+
}
393+
355394
/**
356395
* Removes activity payload fields that no current client reads while retaining
357396
* the full payload in persistence and the event store.
@@ -370,18 +409,19 @@ export function projectActivityPayload(
370409
payload.status === "completed" && (itemStatus === "failed" || itemStatus === "declined")
371410
? { ...payload, status: itemStatus }
372411
: payload;
412+
const questionInput = projectQuestionToolInput(data, payload.title);
373413

374414
if (payload.itemType === "mcp_tool_call") {
375415
return {
376416
...activity,
377417
payload: {
378418
...projectedPayload,
379-
data: projectMcpToolCallData(data),
419+
data: { ...projectMcpToolCallData(data), ...questionInput },
380420
},
381421
};
382422
}
383423

384-
const projectedData: Record<string, unknown> = {};
424+
const projectedData: Record<string, unknown> = { ...questionInput };
385425
const item = projectCommandData(data);
386426
if (item) {
387427
projectedData.item = item;

‎apps/web/src/components/chat/MessagesTimeline.test.tsx‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,12 +348,20 @@ describe("MessagesTimeline", () => {
348348
});
349349
const toggle = renderer!.root.findByProps({ "aria-expanded": false });
350350
await act(() => toggle.props.onClick());
351+
const questionToggle = renderer!.root.findByProps({
352+
"aria-label": "Question answer submitted",
353+
"aria-expanded": false,
354+
});
355+
expect(JSON.stringify(renderer!.toJSON())).not.toContain("Provide a spec");
356+
await act(() => questionToggle.props.onClick());
351357
const markup = JSON.stringify(renderer!.toJSON());
352358
expect(markup.match(/Provide a spec/g)).toHaveLength(1);
353-
expect(markup.match(/spec\.txt/g)).toHaveLength(1);
359+
expect(markup).toContain("spec.txt");
354360
expect(markup).toContain("Provide a screenshot");
355361
expect(markup).toContain("shot.png");
356362
for (const answer of Object.values(answers)) expect(markup).toContain(answer);
363+
await act(() => questionToggle.props.onClick());
364+
expect(JSON.stringify(renderer!.toJSON())).not.toContain("Provide a spec");
357365
} finally {
358366
await act(() => renderer?.unmount());
359367
}

0 commit comments

Comments
 (0)