Skip to content
TypeScript

fix(web): keep compressed image chips in their original draft (#18393)

Alex Southwell599d21060a85
diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx
index 2fe74cfc430..2ddb10ae890 100644
--- a/apps/web/src/components/chat/ChatComposer.tsx
+++ b/apps/web/src/components/chat/ChatComposer.tsx
@@ -1,5 +1,6 @@
import { formatProviderSkillDisplayName } from "@t3tools/shared/inlineSkills";
import { useComposerTypingGuard } from "./useComposerTypingGuard";
+import { insertAttachmentReferencesForDraft } from "./composerAttachmentReferences";
import { DESKTOP_PASTE_AS_TEXT_EVENT } from "../../lib/desktopPasteAsText";
import { runtimeModeConfig, runtimeModeOptions as runtimeModes } from "./runtimeModeConfig";
import { isLocalEnvironmentDisabled } from "../../localEnvironment";
@@ -5538,16 +5539,23 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
// Question answers carry attachments beside the answer, never as chips. Falling back to
// the thread prompt here would hide the file behind a reference the question never shows.
if (questionAttachmentTarget) return false;
- if (selection) {
- const edit = inlineContextReferenceReplacement(promptRef.current, selection, references);
- return applyPromptReplacement(edit.start, edit.end, edit.text);
- }
- const text = references.map(formatInlineContextReference).join(" ");
- const inserted = insertComposerText(`${text} `, "cursor", { ensureLeadingBoundary: true });
- if (!inserted) {
- setPrompt(ensureInlineContextReferences(promptRef.current, references));
- }
- return true;
+ return insertAttachmentReferencesForDraft({
+ target: composerDraftTarget,
+ liveAttachmentTargetKey: attachmentTargetKeyRef.current,
+ references,
+ insertReferences: () => {
+ if (selection) {
+ const edit = inlineContextReferenceReplacement(promptRef.current, selection, references);
+ return applyPromptReplacement(edit.start, edit.end, edit.text);
+ }
+ const text = references.map(formatInlineContextReference).join(" ");
+ const inserted = insertComposerText(`${text} `, "cursor", { ensureLeadingBoundary: true });
+ if (!inserted) {
+ setPrompt(ensureInlineContextReferences(promptRef.current, references));
+ }
+ return true;
+ },
+ });
};
const removeComposerImage = (imageId: string) => {
diff --git a/apps/web/src/components/chat/composerAttachmentReferences.test.ts b/apps/web/src/components/chat/composerAttachmentReferences.test.ts
new file mode 100644
index 00000000000..c044ee2b405
--- /dev/null
+++ b/apps/web/src/components/chat/composerAttachmentReferences.test.ts
@@ -0,0 +1,86 @@
+import { scopeThreadRef } from "@t3tools/client-runtime/environment";
+import { EnvironmentId, ThreadId } from "@t3tools/contracts";
+import { afterEach, describe, expect, it, vi } from "vite-plus/test";
+
+import {
+ composerTargetKey,
+ type ComposerImageAttachment,
+ useComposerDraftStore,
+} from "../../composerDraftStore";
+import { imageContextReference } from "../../lib/composerContextRecords";
+import { ensureInlineContextReferences } from "../../lib/composerContextReferences";
+import { insertAttachmentReferencesForDraft } from "./composerAttachmentReferences";
+
+afterEach(() => {
+ useComposerDraftStore.setState({ draftsByThreadKey: {} });
+});
+
+describe("async attachment references", () => {
+ it.each(["thread", "environment"])(
+ "keeps a deferred image in its original draft after a %s switch",
+ async (switchKind) => {
+ const targetA = scopeThreadRef(EnvironmentId.make("env-A"), ThreadId.make("thread-A"));
+ const targetB = scopeThreadRef(
+ EnvironmentId.make(switchKind === "environment" ? "env-B" : "env-A"),
+ ThreadId.make(switchKind === "environment" ? "thread-A" : "thread-B"),
+ );
+ const store = useComposerDraftStore.getState();
+ store.setPrompt(targetA, "Draft A original");
+ store.setPrompt(targetB, "Draft B original");
+ let liveTargetKey = composerTargetKey(targetA);
+ const promptRef = { current: "Draft A original" };
+ const editor = { text: promptRef.current, caret: 4 };
+ const insertReferences = vi.fn(() => {
+ const next = ensureInlineContextReferences(promptRef.current, [
+ imageContextReference(image),
+ ]);
+ store.setPrompt(targetA, next);
+ promptRef.current = next;
+ editor.text = next;
+ editor.caret = next.length;
+ return true;
+ });
+ const image: ComposerImageAttachment = {
+ type: "image",
+ id: "image-1",
+ name: "picture.png",
+ mimeType: "image/png",
+ sizeBytes: 3,
+ file: new File(["png"], "picture.png", { type: "image/png" }),
+ previewUrl: "blob:picture",
+ };
+ let finishCompression!: (image: ComposerImageAttachment) => void;
+ const compression = new Promise<ComposerImageAttachment>((resolve) => {
+ finishCompression = resolve;
+ });
+ const pending = compression.then((compressed) => {
+ const accepted = store.addImages(targetA, [compressed]);
+ expect(accepted).toEqual([image.id]);
+ return insertAttachmentReferencesForDraft({
+ target: targetA,
+ liveAttachmentTargetKey: liveTargetKey,
+ references: [imageContextReference(compressed)],
+ insertReferences,
+ });
+ });
+
+ liveTargetKey = composerTargetKey(targetB);
+ promptRef.current = "Draft B original";
+ editor.text = promptRef.current;
+ // Use the latest stored A prompt, including edits from elsewhere during compression.
+ store.setPrompt(targetA, "Draft A original updated");
+ finishCompression(image);
+ expect(await pending).toBe(true);
+
+ expect(store.getComposerDraft(targetA)?.prompt).toBe(
+ ensureInlineContextReferences("Draft A original updated", [imageContextReference(image)]),
+ );
+ expect(store.getComposerDraft(targetA)?.images).toEqual([image]);
+ expect(store.getComposerDraft(targetB)?.prompt).toBe("Draft B original");
+ expect(store.getComposerDraft(targetB)?.images).toEqual([]);
+ expect(promptRef.current).toBe("Draft B original");
+ expect(editor).toEqual({ text: "Draft B original", caret: 4 });
+ expect(insertReferences).not.toHaveBeenCalled();
+ },
+ );
+});
diff --git a/apps/web/src/components/chat/composerAttachmentReferences.ts b/apps/web/src/components/chat/composerAttachmentReferences.ts
new file mode 100644
index 00000000000..13bd5565d9f
--- /dev/null
+++ b/apps/web/src/components/chat/composerAttachmentReferences.ts
@@ -0,0 +1,26 @@
+import {
+ composerTargetKey,
+ type ComposerThreadTarget,
+ useComposerDraftStore,
+} from "../../composerDraftStore";
+import {
+ type ComposerContextReference,
+ ensureInlineContextReferences,
+} from "../../lib/composerContextReferences";
+
+/** Route references that finish preparing asynchronously to the draft that accepted them. */
+export function insertAttachmentReferencesForDraft(input: {
+ target: ComposerThreadTarget;
+ liveAttachmentTargetKey: string;
+ references: ReadonlyArray<ComposerContextReference>;
+ insertReferences: () => boolean;
+}): boolean {
+ if (composerTargetKey(input.target) !== input.liveAttachmentTargetKey) {
+ // The shared editor and prompt ref now belong to another draft or question.
+ const store = useComposerDraftStore.getState();
+ const prompt = store.getComposerDraft(input.target)?.prompt ?? "";
+ store.setPrompt(input.target, ensureInlineContextReferences(prompt, input.references));
+ return true;
+ }
+ return input.insertReferences();
+}