Skip to content

Commit f7eca76

Browse files
committed
fix(composer): 保留排队编辑和恢复草稿中的原文
- 使用最终发送文本记录移动端排队编辑的回填内容,排除自动添加的附件提示。 - 移除引用时保留用户在引用前输入的空格。 - 恢复或合并草稿时保留原文记录,覆盖有无引用两种情况。 - 通过 440 个相关测试及 37 个包的类型检查。
1 parent 3f132a8 commit f7eca76

7 files changed

Lines changed: 103 additions & 34 deletions

File tree

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { describe, expect, it, vi } from "vite-plus/test";
22
import { Atom } from "effect/reactivity";
33
import {
44
ATTACHMENT_ONLY_BOOTSTRAP_PROMPT,
5-
restoreAttachmentOnlyBootstrapPrompt,
5+
recallComposerText,
66
} from "@t3tools/shared/composerContextReferences";
77
import {
88
ChatAttachmentId,
@@ -133,6 +133,25 @@ describe("resolveQueuedEditPayload", () => {
133133

134134
expect(payload.context).toEqual(recalled);
135135
});
136+
137+
it.each(["", "Ultrathink:"])(
138+
"omits the generated bootstrap when an attachment remains after editing to %j",
139+
(draftText) => {
140+
const payload = resolveQueuedEditPayload({
141+
edit: edit([image("kept")]),
142+
text: draftText,
143+
draftText,
144+
draftContext: context(["kept"]),
145+
draftAttachments: [],
146+
uploaded: [],
147+
});
148+
149+
expect(payload.text).toBe(
150+
(draftText ? `${draftText}\n` : "") + ATTACHMENT_ONLY_BOOTSTRAP_PROMPT,
151+
);
152+
expect(recallComposerText(payload.text, payload.context)).toBe(draftText);
153+
},
154+
);
136155
});
137156

138157
describe("beginQueuedRunEdit", () => {
@@ -152,19 +171,17 @@ describe("beginQueuedRunEdit", () => {
152171
beginQueuedRunEdit(threadKey, original, runs);
153172
const draft = getComposerDraftSnapshot(queuedEditDraftKey(threadKey, original.runId));
154173
expect(draft.text).toBe(prefix.trimEnd());
155-
expect(
156-
restoreAttachmentOnlyBootstrapPrompt(draft.text, original.existingAttachments.length > 0),
157-
).toBe(original.originalText);
158174
const editing = getQueuedRunEdit(threadKey);
159175
if (editing === null) throw new Error("Expected an open queued edit");
160176
const payload = resolveQueuedEditPayload({
161177
edit: editing,
162-
text: original.originalText,
178+
text: draft.text.trim(),
163179
draftText: draft.text,
164180
draftContext: draft.context,
165181
draftAttachments: [],
166182
uploaded: [],
167183
});
184+
expect(payload.text).toBe(original.originalText);
168185
expect(payload.context).toEqual(recall);
169186
appAtomRegistry.set(runs, [{ id: original.runId, status: "running" }]);
170187
expect(getQueuedRunEdit(threadKey)).toBeNull();

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ import type {
66
UploadChatAttachment,
77
} from "@t3tools/contracts";
88
import { useAtomValue } from "@effect/atom-react";
9-
import { stripAttachmentOnlyBootstrapPrompt } from "@t3tools/shared/composerContextReferences";
9+
import {
10+
composerRecallContext,
11+
restoreAttachmentOnlyBootstrapPrompt,
12+
stripAttachmentOnlyBootstrapPrompt,
13+
} from "@t3tools/shared/composerContextReferences";
1014
import {
1115
makeQueuedRunEditStore,
1216
resolveQueuedEditExit,
1317
type QueuedRunsAtom,
1418
} from "@t3tools/client-runtime/queued-run-edit";
1519

16-
import { composerRecallContext } from "@t3tools/shared/composerContextReferences";
1720
import { uploadedComposerContext } from "../lib/composerContext";
1821
import { appAtomRegistry } from "./atom-registry";
1922
import { abortComposerDraftOperations } from "./composerDraftOperations";
@@ -161,21 +164,23 @@ export function resolveQueuedEditPayload(input: {
161164
readonly draftAttachments: ReadonlyArray<{ readonly id: string }>;
162165
readonly uploaded: ReadonlyArray<ChatAttachment | UploadChatAttachment>;
163166
}): {
167+
readonly text: string;
164168
readonly attachments: ReadonlyArray<ChatAttachment | UploadChatAttachment>;
165169
readonly context: OrchestrationMessageContext;
166170
} {
167171
const attachments = [...input.edit.existingAttachments, ...input.uploaded];
172+
const text = restoreAttachmentOnlyBootstrapPrompt(input.text, attachments.length > 0);
168173
const rebound = uploadedComposerContext(
169174
input.draftContext,
170175
input.draftAttachments,
171176
input.uploaded,
172177
);
173178
const recall = (context: OrchestrationMessageContext | undefined) =>
174-
composerRecallContext(input.text, input.draftText, context, input.edit.originalText) ?? {
179+
composerRecallContext(text, input.draftText, context, input.edit.originalText) ?? {
175180
version: 1,
176181
records: [],
177182
};
178-
if (rebound === undefined) return { attachments, context: recall(undefined) };
183+
if (rebound === undefined) return { text, attachments, context: recall(undefined) };
179184
const liveIds = new Set(
180185
attachments.flatMap((attachment) =>
181186
"id" in attachment && attachment.id ? [attachment.id] : [],
@@ -185,6 +190,7 @@ export function resolveQueuedEditPayload(input: {
185190
(record) => !("attachmentId" in record) || liveIds.has(record.attachmentId),
186191
);
187192
return {
193+
text,
188194
attachments,
189195
context: recall({ ...rebound, records }),
190196
};

‎apps/mobile/src/state/use-composer-drafts.test.ts‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,22 @@ describe("mobile composer drafts", () => {
493493
schemaVersion: 1,
494494
drafts: { thread: { text, attachments: [], context } },
495495
}).drafts.thread!;
496-
appAtomRegistry.set(composerDraftsAtom, { thread: restored });
496+
const recovered = mergeComposerDraftContentState({}, "thread", restored);
497+
const existing = {
498+
text: "another draft",
499+
context: { version: 1, records: [], composerRecall: " another draft " } as const,
500+
attachments: [],
501+
};
502+
const combined = mergeComposerDraftContentState(
503+
{ thread: existing },
504+
"thread",
505+
restored,
506+
).thread!;
507+
const combinedMessage = serializeComposerMessageForServer(combined.text, combined.context);
508+
expect(recallComposerText(combinedMessage.text, combinedMessage.context)).toBe(
509+
" another draft \n\n original\n",
510+
);
511+
appAtomRegistry.set(composerDraftsAtom, recovered);
497512
setComposerDraftText("thread", text);
498513
const unchanged = getComposerDraftSnapshot("thread");
499514
expect(
@@ -504,8 +519,8 @@ describe("mobile composer drafts", () => {
504519
" original\n",
505520
);
506521

507-
const editedAuthored = " changed after recovery\n";
508-
setComposerDraftText("thread", `${editedAuthored}${references}`);
522+
const editedAuthored = " changed after recovery\n ";
523+
setComposerDraftText("thread", editedAuthored + referenceDraft.text);
509524
const edited = decodePersistedComposerState({
510525
schemaVersion: 1,
511526
drafts: { thread: getComposerDraftSnapshot("thread") },

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

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ import { createComposerContextHistory, referencedComposerContext } from "../lib/
3737
import { isQueuedEditDraftKey } from "./queued-edit-draft-key";
3838
import {
3939
collectComposerContextReferences,
40+
composerRecallContext,
4041
formatComposerContextReference,
42+
recallComposerText,
4143
sanitizeComposerContextLabel,
4244
replaceComposerContextReferences,
4345
} from "@t3tools/shared/composerContextReferences";
@@ -628,7 +630,18 @@ function restoreMissingComposerFileReferences(
628630
referenced.add(key);
629631
changed = true;
630632
}
631-
return changed ? { ...draft, text, context: { version: 1, records } } : draft;
633+
const composerRecall = draft.context?.composerRecall;
634+
return changed
635+
? {
636+
...draft,
637+
text,
638+
context: {
639+
version: 1,
640+
records,
641+
...(composerRecall === undefined ? {} : { composerRecall }),
642+
},
643+
}
644+
: draft;
632645
}
633646

634647
function normalizeDraft(draft: ComposerDraft | undefined): ComposerDraft {
@@ -1287,12 +1300,7 @@ export async function restoreCloudComposerDrafts(accountId: string): Promise<voi
12871300
? {
12881301
...draft,
12891302
...existing,
1290-
text: mergeComposerDraftText(existing.text, draft.text),
1291-
context: mergeReferencedComposerContext(
1292-
mergeComposerDraftText(existing.text, draft.text),
1293-
draft.context,
1294-
existing.context,
1295-
),
1303+
...mergeComposerDraftPrompt(existing, draft, { preferExistingRecords: true }),
12961304
// A concurrent import must not lose files, even above the send limit.
12971305
attachments: [
12981306
...existing.attachments,
@@ -1710,15 +1718,41 @@ function mergeComposerDraftText(existing: string, incoming: string): string {
17101718
return `${existing}\n\n${incoming}`;
17111719
}
17121720

1713-
function mergeReferencedComposerContext(
1714-
text: string,
1715-
first?: OrchestrationMessageContext,
1716-
second?: OrchestrationMessageContext,
1721+
function mergeComposerDraftPrompt(
1722+
existing: Pick<ComposerDraft, "text" | "context">,
1723+
incoming: Pick<ComposerDraftContent, "text" | "context">,
1724+
options?: { readonly preferExistingRecords?: boolean },
17171725
) {
1726+
const text = mergeComposerDraftText(existing.text, incoming.text);
1727+
const [first, second] = options?.preferExistingRecords
1728+
? [incoming.context, existing.context]
1729+
: [existing.context, incoming.context];
17181730
const records = new Map((first?.records ?? []).map((record) => [record.contextId, record]));
17191731
for (const record of second?.records ?? []) records.set(record.contextId, record);
1720-
if (records.size === 0) return undefined;
1721-
return referencedComposerContext(text, { version: 1, records: [...records.values()] });
1732+
const context =
1733+
records.size === 0
1734+
? undefined
1735+
: referencedComposerContext(text, { version: 1, records: [...records.values()] });
1736+
if (
1737+
existing.context?.composerRecall === undefined &&
1738+
incoming.context?.composerRecall === undefined
1739+
) {
1740+
return { text, context };
1741+
}
1742+
1743+
const previous = recallComposerText(existing.text, existing.context);
1744+
const added = recallComposerText(incoming.text, incoming.context);
1745+
// Keep recall aligned with the text merge, including a retried restoration of the same text.
1746+
let recalled = previous;
1747+
if (
1748+
text === incoming.text &&
1749+
(text !== existing.text || existing.context?.composerRecall === undefined)
1750+
) {
1751+
recalled = added;
1752+
} else if (text !== existing.text) {
1753+
recalled = [previous, added].filter((part) => part.length > 0).join("\n\n");
1754+
}
1755+
return { text, context: composerRecallContext(text, recalled, context) };
17221756
}
17231757

17241758
export function mergeComposerDraftContentState(
@@ -1742,8 +1776,7 @@ export function mergeComposerDraftContentState(
17421776
0,
17431777
PROVIDER_SEND_TURN_MAX_ATTACHMENTS,
17441778
);
1745-
const text = mergeComposerDraftText(existing.text, content.text);
1746-
const context = mergeReferencedComposerContext(text, existing.context, content.context);
1779+
const { text, context } = mergeComposerDraftPrompt(existing, content);
17471780
const importedShareIds = content.sourceShareId
17481781
? [...(existing.importedShareIds ?? []), content.sourceShareId]
17491782
: existing.importedShareIds;

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import {
4343
} from "@t3tools/client-runtime/state/threads";
4444
import { resolveThreadWorkingStartedAt } from "@t3tools/client-runtime/state/models";
4545
import { upgradeLegacyContextMessage } from "@t3tools/shared/composerContextLegacy";
46-
import { restoreAttachmentOnlyBootstrapPrompt } from "@t3tools/shared/composerContextReferences";
4746
import { composerContextSendBlockReason, reidentifyComposerContext } from "../lib/composerContext";
4847
import { uuidv4 } from "../lib/uuid";
4948
import { useNowMinuteMs } from "../lib/clock";
@@ -533,7 +532,7 @@ export function useThreadComposerState(input: {
533532
input: {
534533
threadId: thread.id,
535534
runId: edit.runId,
536-
text: restoreAttachmentOnlyBootstrapPrompt(text, payload.attachments.length > 0),
535+
text: payload.text,
537536
edit: {
538537
messageId: edit.messageId,
539538
attachments: payload.attachments,

‎packages/shared/src/composerContextReferences.test.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ describe("composer recall provenance", () => {
517517
expect(recallComposerText(sent)).toBe(sent);
518518
});
519519

520-
it("excludes only references backed by actual context records", () => {
520+
it.each([" ", ""])("keeps authored spacing around backed references (%j)", (suffix) => {
521521
const context: OrchestrationMessageContext = {
522522
version: 1,
523523
records: [
@@ -534,8 +534,7 @@ describe("composer recall provenance", () => {
534534
},
535535
],
536536
};
537-
const authored =
538-
"Look at [Terminal](t3-context://v1/terminal/term) please\n[literal](t3-context://v1/terminal/missing)";
537+
const authored = `Look at [Terminal](t3-context://v1/terminal/term)${suffix}please\n[literal](t3-context://v1/terminal/missing)`;
539538
expect(recallComposerText(authored, composerRecallContext(authored, authored, context))).toBe(
540539
"Look at please\n[literal](t3-context://v1/terminal/missing)",
541540
);

‎packages/shared/src/composerContextReferences.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,9 +474,9 @@ export function composerRecallContext(
474474
for (let index = references.length - 1; index >= 0; index--) {
475475
const reference = references[index]!;
476476
if (!ids.has(reference.contextId)) continue;
477-
let { start, end } = reference;
477+
const { start } = reference;
478+
let { end } = reference;
478479
if (text[end] === " ") end += 1;
479-
else if (text[start - 1] === " ") start -= 1;
480480
text = text.slice(0, start) + text.slice(end);
481481
}
482482
// Recompute rather than carrying provenance from a message being edited.

0 commit comments

Comments
 (0)