Skip to content

Commit f797e17

Browse files
committed
fix(composer): 保留历史回填中的用户原始输入
- 在发送前记录文本来源,避免按前缀或标签误删用户内容,并以范围减少长提示词复制。 - 覆盖网页、移动端、计划发送和排队编辑,服务端清除旧客户端编辑后过期的回填信息。 - 通过 203 项相关测试和变更检查,并在隔离客户端验证修复前后及生成上下文对照。
1 parent c58d558 commit f797e17

19 files changed

Lines changed: 322 additions & 200 deletions

‎apps/mobile/src/features/threads/new-task-flow-provider.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { composerRecallContext } from "@t3tools/shared/composerContextReferences";
12
import { ActiveNewTaskContext } from "../../state/active-new-task";
23
import * as Dialog from "../../components/ui/Dialog";
34
import { resolveFilesystemReadAccess } from "@t3tools/client-runtime/state/filesystem";
@@ -1037,7 +1038,7 @@ export function NewTaskFlowProvider(
10371038
commandId: CommandId.make(metadata.commandId),
10381039
text,
10391040
attachments: draft.attachments,
1040-
context: draft.context,
1041+
context: composerRecallContext(text, draft.text, draft.context),
10411042
modelSelection: draftModelSelection,
10421043
runtimeMode: draft.runtimeMode ?? defaultRuntimeMode,
10431044
interactionMode: resolvePendingTaskInteractionMode({

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

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import { upgradeLegacyContextMessage } from "@t3tools/shared/composerContextLegacy";
2+
import {
3+
composerRecallContext,
4+
recallComposerText,
5+
} from "@t3tools/shared/composerContextReferences";
26
import { buildProjectThreadStartTurnInput } from "./projectThreadStartTurn";
37
import {
48
ProjectId,
@@ -269,18 +273,35 @@ describe("mobile composer context", () => {
269273
});
270274

271275
describe("host context compatibility", () => {
276+
it("keeps captured whitespace through outbox serialization and attachment rebinding", () => {
277+
const authored = " Ultrathink:\nThis prefix is literal\n";
278+
const sentText = authored.trim();
279+
const queuedContext = composerRecallContext(sentText, authored, {
280+
version: 1,
281+
records: [image],
282+
});
283+
const rebound = uploadedComposerContext(
284+
queuedContext,
285+
[{ id: "local-image" }],
286+
[{ id: "uploaded-image" }],
287+
);
288+
const sent = serializeComposerMessageForServer(sentText, rebound, true);
289+
expect(recallComposerText(sent.text, sent.context)).toBe(authored);
290+
expect(sent.context?.records).toEqual([{ ...image, attachmentId: "uploaded-image" }]);
291+
expect(serializeComposerMessageForServer(sentText, rebound, false).context).toBeUndefined();
292+
});
272293
it("sends an image-only prompt as the attachment-only prompt and keeps the image", () => {
273294
const context: OrchestrationMessageContext = { version: 1, records: [image] };
274295
const reference = formatComposerContextReference(image);
275296
expect(serializeComposerMessageForServer(reference, context, true)).toEqual({
276297
text: ATTACHMENT_ONLY_BOOTSTRAP_PROMPT,
277-
context,
298+
context: { ...context, composerRecall: "" },
278299
});
279300
const withProse = `${reference} what is this?`;
280-
expect(serializeComposerMessageForServer(withProse, context, true)).toEqual({
281-
text: withProse,
282-
context,
283-
});
301+
const sent = serializeComposerMessageForServer(withProse, context, true);
302+
expect(sent.text).toBe(withProse);
303+
expect(sent.context?.records).toEqual(context.records);
304+
expect(recallComposerText(sent.text, sent.context)).toBe("what is this?");
284305
});
285306

286307
it("carries the current PR diff file within the composer wire limits", () => {
@@ -374,7 +395,10 @@ describe("host context compatibility", () => {
374395
expect(message.text).toContain(review.text);
375396
expect(message.text).toContain(review.diff);
376397
expect(message.text).toContain(pr.pullRequest!.url);
377-
expect(serializeComposerMessageForServer(text, context, true)).toEqual({ text, context });
398+
expect(serializeComposerMessageForServer(text, context, true)).toEqual({
399+
text,
400+
context: { ...context, composerRecall: "" },
401+
});
378402
expect(context.records).toEqual([terminal, review, pr]);
379403
});
380404
});

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { isProviderSkillUserInvocable } from "@t3tools/client-runtime/providerSkills";
22
import { serializeLegacyContextMessage } from "@t3tools/shared/composerContextLegacySend";
3+
import {
4+
composerRecallContext,
5+
recallComposerText,
6+
} from "@t3tools/shared/composerContextReferences";
37
import { filePreviewKind } from "@t3tools/shared/filePreview";
48
import { videoMimeType } from "@t3tools/shared/video";
59
import {
@@ -209,7 +213,7 @@ export function uploadedComposerContext(
209213
if (!context) return undefined;
210214
const ids = new Map(drafts.map((draft, index) => [draft.id, uploaded[index]?.id]));
211215
return {
212-
version: 1,
216+
...context,
213217
records: context.records.map((record) =>
214218
"attachmentId" in record
215219
? { ...record, attachmentId: ids.get(record.attachmentId) ?? record.attachmentId }
@@ -263,7 +267,12 @@ export function serializeComposerMessageForServer(
263267
const text = isImageReferencesOnly(composerText)
264268
? ATTACHMENT_ONLY_BOOTSTRAP_PROMPT
265269
: composerText;
270+
const recallContext = composerRecallContext(
271+
text,
272+
recallComposerText(composerText, context),
273+
context,
274+
);
266275
return supportsInlineMessageContext
267-
? { text, ...(context ? { context } : {}) }
276+
? { text, ...(recallContext ? { context: recallContext } : {}) }
268277
: { text: serializeLegacyContextMessage({ text, records: context?.records ?? [] }) };
269278
}

‎apps/mobile/src/state/use-thread-composer-state.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { composerRecallContext } from "@t3tools/shared/composerContextReferences";
12
import { useComposerAttachmentActions } from "./use-composer-attachment-actions";
23
import * as Dialog from "../components/ui/Dialog";
34
import { AuthOrchestrationOperateScope } from "@t3tools/contracts";
@@ -507,7 +508,10 @@ export function useThreadComposerState() {
507508
edit: {
508509
messageId: edit.messageId,
509510
attachments: payload.attachments,
510-
...(payload.context ? { context: payload.context } : {}),
511+
context: composerRecallContext(text, draft.text, payload.context) ?? {
512+
version: 1,
513+
records: [],
514+
},
511515
},
512516
},
513517
});
@@ -706,7 +710,7 @@ export function useThreadComposerState() {
706710
commandId: CommandId.make(metadata.commandId),
707711
text,
708712
attachments,
709-
context: draft.context,
713+
context: composerRecallContext(text, draft.text, draft.context),
710714
modelSelection,
711715
runtimeMode: draft.runtimeMode ?? thread.runtimeMode,
712716
interactionMode: resolveProviderInteractionMode(

‎apps/mobile/src/state/use-thread-outbox-drain.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ export function useThreadOutboxDrain(): void {
10211021
createdAt: queuedMessage.createdAt,
10221022
titleText: queuedMessage.text,
10231023
...serializeComposerMessageForServer(
1024-
queuedMessage.text.trim(),
1024+
queuedMessage.text,
10251025
uploadedComposerContext(
10261026
queuedMessage.context,
10271027
queuedMessage.attachments,

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8385,6 +8385,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
83858385
const emitEvent = emit(events, command);
83868386
const editedAttachments =
83878387
command.attachments === undefined ? {} : { attachments: command.attachments };
8388+
// An older client or MCP caller can edit without capturing new recall provenance.
8389+
// Keep its context records, but never recall the previous draft after a text edit.
8390+
const editedContext =
8391+
command.context ??
8392+
(command.text === queuedMessage.text || queuedMessage.context === undefined
8393+
? queuedMessage.context
8394+
: { version: 1 as const, records: queuedMessage.context.records });
83888395
yield* emitEvent({
83898396
type: "message.updated",
83908397
threadId: command.threadId,
@@ -8396,7 +8403,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
83968403
...queuedMessage,
83978404
text: command.text,
83988405
...editedAttachments,
8399-
...(command.context ? { context: command.context } : {}),
8406+
...(editedContext ? { context: editedContext } : {}),
84008407
updatedAt: now,
84018408
},
84028409
});
@@ -8412,7 +8419,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
84128419
...queuedTurnItem,
84138420
text: command.text,
84148421
...editedAttachments,
8415-
...(command.context ? { context: command.context } : {}),
8422+
...(editedContext ? { context: editedContext } : {}),
84168423
updatedAt: now,
84178424
},
84188425
});

‎apps/server/src/orchestration-v2/ThreadLaunchService.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,6 +1737,7 @@ it.effect("shared intake preserves durable attachment bytes after a lost launch
17371737
text: "First [file](t3-context://v1/file/intake-file)",
17381738
context: {
17391739
version: 1 as const,
1740+
composerRecall: "First",
17401741
records: [
17411742
{
17421743
version: 1 as const,
@@ -1779,6 +1780,7 @@ it.effect("shared intake preserves durable attachment bytes after a lost launch
17791780
);
17801781
assert.isDefined(stored);
17811782
assert.notEqual(stored.attachments[0]?.id, attachment.id);
1783+
assert.equal(stored.context?.composerRecall, "First");
17821784
assert.equal(
17831785
(stored.context?.records[0] as { attachmentId: string }).attachmentId,
17841786
stored.attachments[0]?.id,
@@ -1844,6 +1846,7 @@ it.effect("shared intake preserves durable attachment bytes after a lost launch
18441846
threadId: input.threadId,
18451847
messageId: MessageId.make("intake-second"),
18461848
text: "Second",
1849+
context: { version: 1, records: [], composerRecall: "Original draft" },
18471850
attachments: [attachment],
18481851
dispatchMode: { type: "queue_after_active" },
18491852
createdBy: "user",
@@ -1861,6 +1864,19 @@ it.effect("shared intake preserves durable attachment bytes after a lost launch
18611864
(message) => message.id === queuedRun.userMessageId,
18621865
);
18631866
assert.isDefined(queuedMessage);
1867+
yield* ThreadMessageIntake.dispatchCommand({
1868+
type: "queued-run.edit",
1869+
commandId: CommandId.make("intake-unchanged-text"),
1870+
threadId: input.threadId,
1871+
runId: queuedRun.id,
1872+
text: queuedMessage.text,
1873+
});
1874+
assert.equal(
1875+
(yield* threads.getThreadProjection(input.threadId)).messages.find(
1876+
(message) => message.id === queuedMessage.id,
1877+
)?.context?.composerRecall,
1878+
"Original draft",
1879+
);
18641880
const file: ChatAttachment = {
18651881
type: "file",
18661882
id: ChatAttachmentId.make(createPendingAttachmentId("pdf")),
@@ -1889,6 +1905,21 @@ it.effect("shared intake preserves durable attachment bytes after a lost launch
18891905
(message) => message.id === queuedRun.userMessageId,
18901906
);
18911907
assert.isDefined(editedMessage);
1908+
assert.isUndefined(editedMessage.context?.composerRecall);
1909+
yield* ThreadMessageIntake.dispatchCommand({
1910+
type: "queued-run.edit",
1911+
commandId: CommandId.make("intake-new-provenance"),
1912+
threadId: input.threadId,
1913+
runId: queuedRun.id,
1914+
text: "Ultrathink:\nEdited with a file",
1915+
context: { version: 1, records: [], composerRecall: "Edited with a file" },
1916+
});
1917+
const recaptured = yield* threads.getThreadProjection(input.threadId);
1918+
assert.equal(
1919+
recaptured.messages.find((message) => message.id === editedMessage.id)?.context
1920+
?.composerRecall,
1921+
"Edited with a file",
1922+
);
18921923
assert.equal(editedMessage.attachments.length, 2);
18931924
assert.deepEqual(editedMessage.attachments[0], queuedMessage.attachments[0]);
18941925
assert.notEqual(editedMessage.attachments[1]?.id, file.id);

‎apps/web/src/components/ChatView.logic.ts‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,6 +1500,13 @@ export function snapshotComposerSend(contents: ComposerSendContents) {
15001500
const images = [...contents.images];
15011501
const files = [...contents.files];
15021502
const terminalContexts = [...contents.sendableTerminalContexts];
1503+
const composerText = contents.terminalContexts
1504+
.filter((context) => !terminalContexts.includes(context))
1505+
.reduce(
1506+
(text, context) =>
1507+
removeInlineContextReference(text, terminalContextReference(context).contextId).prompt,
1508+
contents.prompt,
1509+
);
15031510
return {
15041511
images,
15051512
files,
@@ -1508,14 +1515,8 @@ export function snapshotComposerSend(contents: ComposerSendContents) {
15081515
previewAnnotations: [...contents.previewAnnotations],
15091516
reviewComments: [...contents.reviewComments],
15101517
threadContexts: [...contents.threadContexts],
1511-
messageText: contents.terminalContexts
1512-
.filter((context) => !terminalContexts.includes(context))
1513-
.reduce(
1514-
(text, context) =>
1515-
removeInlineContextReference(text, terminalContextReference(context).contextId).prompt,
1516-
contents.prompt,
1517-
)
1518-
.trim(),
1518+
composerText,
1519+
messageText: composerText.trim(),
15191520
};
15201521
}
15211522

‎apps/web/src/components/chat/composerPromptHistory.test.ts‎

Lines changed: 26 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -25,64 +25,35 @@ function forward(position: ComposerPromptHistoryPosition | null, currentPrompt:
2525

2626
describe("recallableComposerPrompt", () => {
2727
it.each([
28-
"Render <preview_annotation>",
29-
"Render\n<preview_annotation>\nhere",
30-
"Render &lt;preview_annotation&gt;",
31-
])("strips an annotation whose comment contains a literal opening tag: %s", (comment) => {
32-
expect(
33-
recallableComposerPrompt(
34-
`Prompt\n<preview_annotation>\nComment: ${comment}\n</preview_annotation>`,
35-
),
36-
).toBe("Prompt");
37-
});
38-
39-
it("preserves a malformed annotation containing a nested opening tag", () => {
40-
const text =
41-
"Prompt\n<preview_annotation>\nouter literal\n<preview_annotation>\ninner\n</preview_annotation>";
28+
"Ultrathink:\nKeep this prefix in my example",
29+
'Keep this example\n<review_comment filePath="a.ts">literal</review_comment>',
30+
" Preserve surrounding whitespace\n",
31+
"PLEASE IMPLEMENT THIS PLAN:\nThis is a literal example",
32+
])("preserves authored text when its provenance is unknown: %s", (text) => {
4233
expect(recallableComposerPrompt(text)).toBe(text);
4334
});
44-
it("does not strip a terminal label embedded in ordinary prose", () => {
35+
it("uses origin metadata and skips generated-only messages", () => {
36+
const text = "Ultrathink:\nKeep this prefix";
4537
expect(
46-
recallableComposerPrompt(
47-
"email@build:7\n<terminal_context>\n- Build line 7:\n output\n</terminal_context>",
48-
),
49-
).toBe("email@build:7");
50-
});
51-
it("strips legacy send-time context blocks and the ultrathink prefix", () => {
52-
const sent =
53-
"Ultrathink:\nInvestigate this\n\n<terminal_context>\n- Terminal 1 lines 12-13:\n 12 | git status\n 13 | On branch main\n</terminal_context>\n\n<element_context>\n- <button>:\n url: https://example.com\n</element_context>";
54-
expect(recallableComposerPrompt(sent)).toBe("Investigate this");
55-
});
56-
57-
it.each([
58-
["Look at @terminal-1:4 please", "Look at please"],
59-
[
60-
"@terminal-1:4 typed twice: @terminal-1:4\n indented code",
61-
"typed twice: @terminal-1:4\n indented code",
62-
],
63-
[
64-
"see @terminal-1:40 and @terminal-1:4-12 then @terminal-1:4",
65-
"see @terminal-1:40 and @terminal-1:4-12 then",
66-
],
67-
])("removes only the matching legacy terminal label from %s", (typed, expected) => {
68-
const sent =
69-
typed + "\n\n<terminal_context>\n- Terminal 1 line 4:\n 4 | ls\n</terminal_context>";
70-
expect(recallableComposerPrompt(sent)).toBe(expected);
71-
});
72-
73-
it("strips only review comments appended at the end", () => {
74-
const block = '<review_comment filePath="src/app.ts">Keep this configurable.</review_comment>';
75-
expect(recallableComposerPrompt("Please update this.\n\n" + block)).toBe("Please update this.");
76-
const midPrompt = "Before\n\n" + block + "\n\nAfter";
77-
expect(recallableComposerPrompt(midPrompt)).toBe(midPrompt);
78-
expect(recallableComposerPrompt(midPrompt + "\n\n" + block)).toBe(midPrompt);
79-
});
80-
81-
it("removes canonical references without restoring dangling chips", () => {
82-
const sent =
83-
"Look at [Terminal](t3-context://v1/terminal/term-1) please\n indented code\n![image](t3-context://v1/image/img-1)";
84-
expect(recallableComposerPrompt(sent)).toBe("Look at please\n indented code");
85-
expect(recallableComposerPrompt("![image](t3-context://v1/image/img-1)")).toBe("");
38+
buildComposerPromptHistoryEntries([
39+
{ id: "literal", role: "user", text },
40+
{
41+
id: "generated",
42+
role: "user",
43+
text,
44+
context: { version: 1, records: [], composerRecall: [12, text.length] },
45+
},
46+
{
47+
id: "plan",
48+
role: "user",
49+
text: "PLEASE IMPLEMENT THIS PLAN:\nplan",
50+
context: { version: 1, records: [], composerRecall: "" },
51+
},
52+
]),
53+
).toEqual([
54+
{ id: "literal", prompt: text },
55+
{ id: "generated", prompt: "Keep this prefix" },
56+
]);
8657
});
8758
});
8859

0 commit comments

Comments
 (0)