Skip to content

Commit 3f132a8

Browse files
committed
fix(composer): 保留仅附件排队消息的原文记录
- 排队编辑保留原始发送文本,区分编辑框隐藏的附件占位内容与用户原文。 - 未编辑文字时保留已有回填记录,附件变动后也不会把生成的努力级别前缀当作用户输入。 - Web 和移动端的四个回归用例先失败后通过,变更范围检查共通过 437 项测试。
1 parent 749a4f2 commit 3f132a8

6 files changed

Lines changed: 46 additions & 28 deletions

File tree

‎apps/mobile/src/state/queued-run-edit.test.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,11 @@ describe("beginQueuedRunEdit", () => {
140140
"opens attachment-only edits without the placeholder and treats them as untouched (%j)",
141141
(prefix) => {
142142
const threadKey = `env-exit:attachment-only-${prefix.length}`;
143+
const recall = { version: 1, records: [], composerRecall: " \n " } as const;
143144
const original = {
144145
...edit([image("kept")]),
145146
originalText: prefix + ATTACHMENT_ONLY_BOOTSTRAP_PROMPT,
147+
context: recall,
146148
};
147149
const runs = Atom.make<ReadonlyArray<{ readonly id: RunId; readonly status: string }> | null>(
148150
[{ id: original.runId, status: "queued" }],
@@ -153,7 +155,17 @@ describe("beginQueuedRunEdit", () => {
153155
expect(
154156
restoreAttachmentOnlyBootstrapPrompt(draft.text, original.existingAttachments.length > 0),
155157
).toBe(original.originalText);
156-
expect(getQueuedRunEdit(threadKey)).toEqual({ ...original, originalText: prefix.trimEnd() });
158+
const editing = getQueuedRunEdit(threadKey);
159+
if (editing === null) throw new Error("Expected an open queued edit");
160+
const payload = resolveQueuedEditPayload({
161+
edit: editing,
162+
text: original.originalText,
163+
draftText: draft.text,
164+
draftContext: draft.context,
165+
draftAttachments: [],
166+
uploaded: [],
167+
});
168+
expect(payload.context).toEqual(recall);
157169
appAtomRegistry.set(runs, [{ id: original.runId, status: "running" }]);
158170
expect(getQueuedRunEdit(threadKey)).toBeNull();
159171
expect(appAtomRegistry.get(threadComposerErrorsAtom)[threadKey]?.message).toBeUndefined();

‎apps/mobile/src/state/queued-run-edit.ts‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function exitQueuedRunEdit(threadKey: string, edit: QueuedRunEdit): void {
7878
const editRestorable = editDraft.text.trim().length > 0 || editDraft.attachments.length > 0;
7979
const recovery = resolveQueuedEditExit({
8080
editChanged:
81-
editDraft.text !== edit.originalText ||
81+
editDraft.text !== stripAttachmentOnlyBootstrapPrompt(edit.originalText) ||
8282
editDraft.attachments.length > 0 ||
8383
edit.removedExistingAttachment === true,
8484
// A cleared edit has nothing to put back, so it can only be discarded.
@@ -120,15 +120,10 @@ export function beginQueuedRunEdit(
120120
// The composer moves off the thread draft, so a paste or pick still running for it is dropped.
121121
abortComposerDraftOperations(threadKey);
122122
const draftKey = queuedEditDraftKey(threadKey, edit.runId);
123-
const originalText = stripAttachmentOnlyBootstrapPrompt(edit.originalText);
124123
clearComposerDraft(draftKey);
125-
setComposerDraftText(draftKey, originalText);
124+
setComposerDraftText(draftKey, stripAttachmentOnlyBootstrapPrompt(edit.originalText));
126125
setComposerDraftContext(draftKey, edit.context);
127-
queuedRunEdits.begin(
128-
threadKey,
129-
{ ...edit, originalText },
130-
{ runs, onRunLeftQueue: exitQueuedRunEdit },
131-
);
126+
queuedRunEdits.begin(threadKey, edit, { runs, onRunLeftQueue: exitQueuedRunEdit });
132127
}
133128

134129
export function removeQueuedRunEditAttachment(threadKey: string, attachmentId: string): void {

‎apps/web/src/components/chat/queuedMessageEdit.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import type { ChatAttachment, OrchestrationMessageContext } from "@t3tools/contracts";
2-
import { composerRecallContext } from "@t3tools/shared/composerContextReferences";
2+
import {
3+
composerRecallContext,
4+
stripAttachmentOnlyBootstrapPrompt,
5+
} from "@t3tools/shared/composerContextReferences";
36
import { resolveQueuedEditExit } from "@t3tools/client-runtime/queued-run-edit";
47
import {
58
composerDraftHasUserContent,
@@ -21,7 +24,9 @@ export function recoverQueuedMessageEdit(input: {
2124
editChanged:
2225
input.removedExistingAttachment === true ||
2326
(draft !== null &&
24-
(draft.prompt !== input.originalText || draft.images.length > 0 || draft.files.length > 0)),
27+
(draft.prompt !== stripAttachmentOnlyBootstrapPrompt(input.originalText) ||
28+
draft.images.length > 0 ||
29+
draft.files.length > 0)),
2530
// A cleared edit has nothing to put back, so it can only be discarded.
2631
threadDraftEmpty:
2732
composerDraftHasUserContent(draft) &&

‎apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ describe("useQueuedRunEdit", () => {
194194
async (prefix) => {
195195
const { hook } = setup();
196196
mocks.editQueuedRun.mockResolvedValue({ _tag: "Success" });
197-
await using screen = await render(view(THREAD_A, "queued", hook));
198197
using toast = vi.spyOn(toastManager, "add");
199198
const attachments = [
200199
{
@@ -211,6 +210,10 @@ describe("useQueuedRunEdit", () => {
211210
text: prefix + ATTACHMENT_ONLY_BOOTSTRAP_PROMPT,
212211
attachments,
213212
};
213+
const context = { version: 1, records: [], composerRecall: " \n " } as const;
214+
await using screen = await render(
215+
view(THREAD_A, "queued", hook, [{ id: request.messageId, text: request.text, context }]),
216+
);
214217
await act(() => requireHook(hook).beginEdit(request));
215218
expect(
216219
useComposerDraftStore.getState().getComposerDraft(requireHook(hook).draftTarget)?.prompt ??
@@ -237,7 +240,7 @@ describe("useQueuedRunEdit", () => {
237240
expect.objectContaining({
238241
input: expect.objectContaining({
239242
text: prefix + ATTACHMENT_ONLY_BOOTSTRAP_PROMPT,
240-
edit: expect.objectContaining({ attachments }),
243+
edit: expect.objectContaining({ attachments, context }),
241244
}),
242245
}),
243246
);
@@ -247,12 +250,17 @@ describe("useQueuedRunEdit", () => {
247250
it("saves an effort-only prompt without claiming removed attachments remain", async () => {
248251
const { hook } = setup();
249252
mocks.editQueuedRun.mockResolvedValue({ _tag: "Success" });
250-
await using screen = await render(view(THREAD_A, "queued", hook));
253+
const messageId = MessageId.make("message-1");
254+
const text = `Ultrathink:\n${ATTACHMENT_ONLY_BOOTSTRAP_PROMPT}`;
255+
const context = { version: 1, records: [], composerRecall: "" } as const;
256+
await using screen = await render(
257+
view(THREAD_A, "queued", hook, [{ id: messageId, text, context }]),
258+
);
251259
await act(() =>
252260
requireHook(hook).beginEdit({
253261
runId: RUN,
254-
messageId: MessageId.make("message-1"),
255-
text: `Ultrathink:\n${ATTACHMENT_ONLY_BOOTSTRAP_PROMPT}`,
262+
messageId,
263+
text,
256264
attachments: [
257265
{
258266
type: "image",
@@ -271,7 +279,7 @@ describe("useQueuedRunEdit", () => {
271279
expect.objectContaining({
272280
input: expect.objectContaining({
273281
text: "Ultrathink:",
274-
edit: expect.objectContaining({ attachments: [] }),
282+
edit: expect.objectContaining({ attachments: [], context }),
275283
}),
276284
}),
277285
);

‎apps/web/src/components/chat/useQueuedRunEdit.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ export function useQueuedRunEdit(input: {
253253
threadId: activeThreadId,
254254
runId: request.runId,
255255
messageId: request.messageId,
256-
originalText: text,
256+
originalText: request.text,
257257
existingAttachments: request.attachments,
258258
context,
259259
},

‎packages/shared/src/composerContextReferences.ts‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -459,19 +459,17 @@ export function composerRecallContext(
459459
sentText: string,
460460
composerText: string,
461461
context?: OrchestrationMessageContext,
462-
previousText?: string,
462+
previousSentText?: string,
463463
): OrchestrationMessageContext | undefined {
464-
// A queued editor can reopen the already-trimmed wire text. A no-op save
465-
// must not replace the captured original with that lossy representation.
466-
if (
464+
// The queued editor hides the attachment prompt. Keep captured text when
465+
// the editor's text is untouched, even if saving changes the attachments.
466+
const unchangedDraft =
467+
previousSentText !== undefined &&
467468
context?.composerRecall !== undefined &&
468-
composerText === previousText &&
469-
sentText === previousText
470-
) {
471-
return context;
472-
}
469+
composerText === stripAttachmentOnlyBootstrapPrompt(previousSentText);
470+
if (unchangedDraft && sentText === previousSentText) return context;
473471
const ids = new Set(context?.records.map((record) => record.contextId));
474-
let text = composerText;
472+
let text = unchangedDraft ? recallComposerText(previousSentText, context) : composerText;
475473
const references = collectComposerContextReferences(text);
476474
for (let index = references.length - 1; index >= 0; index--) {
477475
const reference = references[index]!;

0 commit comments

Comments
 (0)