@devries-cameron20 / t3code Mirror · read-only / commits / 599d21060a
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 23, 03:00 UTC

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

Co-authored-by: SAPHID <saphid@users.noreply.github.com> Co-authored-by: GPT-6.1 Sol <noreply@openai.com>

Alex Southwell committed Oct 19, 2026 at 07:04 UTC 599d21060a85a8534ab4a858a36a2447c510b146
3 files changed +130 -10
apps/web/src/components/chat/ChatComposer.tsx
+18 -10
@@ -1,5 +1,6 @@
1 import { formatProviderSkillDisplayName } from "@t3tools/shared/inlineSkills";
2 import { useComposerTypingGuard } from "./useComposerTypingGuard";
3 +import { insertAttachmentReferencesForDraft } from "./composerAttachmentReferences";
4 import { DESKTOP_PASTE_AS_TEXT_EVENT } from "../../lib/desktopPasteAsText";
5 import { runtimeModeConfig, runtimeModeOptions as runtimeModes } from "./runtimeModeConfig";
6 import { isLocalEnvironmentDisabled } from "../../localEnvironment";
@@ -5538,16 +5539,23 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
5539 // Question answers carry attachments beside the answer, never as chips. Falling back to
5540 // the thread prompt here would hide the file behind a reference the question never shows.
5541 if (questionAttachmentTarget) return false;
5541 - if (selection) {
5542 - const edit = inlineContextReferenceReplacement(promptRef.current, selection, references);
5543 - return applyPromptReplacement(edit.start, edit.end, edit.text);
5544 - }
5545 - const text = references.map(formatInlineContextReference).join(" ");
5546 - const inserted = insertComposerText(`${text} `, "cursor", { ensureLeadingBoundary: true });
5547 - if (!inserted) {
5548 - setPrompt(ensureInlineContextReferences(promptRef.current, references));
5549 - }
5550 - return true;
5542 + return insertAttachmentReferencesForDraft({
5543 + target: composerDraftTarget,
5544 + liveAttachmentTargetKey: attachmentTargetKeyRef.current,
5545 + references,
5546 + insertReferences: () => {
5547 + if (selection) {
5548 + const edit = inlineContextReferenceReplacement(promptRef.current, selection, references);
5549 + return applyPromptReplacement(edit.start, edit.end, edit.text);
5550 + }
5551 + const text = references.map(formatInlineContextReference).join(" ");
5552 + const inserted = insertComposerText(`${text} `, "cursor", { ensureLeadingBoundary: true });
5553 + if (!inserted) {
5554 + setPrompt(ensureInlineContextReferences(promptRef.current, references));
5555 + }
5556 + return true;
5557 + },
5558 + });
5559 };
5560
5561 const removeComposerImage = (imageId: string) => {
apps/web/src/components/chat/composerAttachmentReferences.test.ts new
+86
@@ -0,0 +1,86 @@
1 +import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2 +import { EnvironmentId, ThreadId } from "@t3tools/contracts";
3 +import { afterEach, describe, expect, it, vi } from "vite-plus/test";
4 +
5 +import {
6 + composerTargetKey,
7 + type ComposerImageAttachment,
8 + useComposerDraftStore,
9 +} from "../../composerDraftStore";
10 +import { imageContextReference } from "../../lib/composerContextRecords";
11 +import { ensureInlineContextReferences } from "../../lib/composerContextReferences";
12 +import { insertAttachmentReferencesForDraft } from "./composerAttachmentReferences";
13 +
14 +afterEach(() => {
15 + useComposerDraftStore.setState({ draftsByThreadKey: {} });
16 +});
17 +
18 +describe("async attachment references", () => {
19 + it.each(["thread", "environment"])(
20 + "keeps a deferred image in its original draft after a %s switch",
21 + async (switchKind) => {
22 + const targetA = scopeThreadRef(EnvironmentId.make("env-A"), ThreadId.make("thread-A"));
23 + const targetB = scopeThreadRef(
24 + EnvironmentId.make(switchKind === "environment" ? "env-B" : "env-A"),
25 + ThreadId.make(switchKind === "environment" ? "thread-A" : "thread-B"),
26 + );
27 + const store = useComposerDraftStore.getState();
28 + store.setPrompt(targetA, "Draft A original");
29 + store.setPrompt(targetB, "Draft B original");
30 + let liveTargetKey = composerTargetKey(targetA);
31 + const promptRef = { current: "Draft A original" };
32 + const editor = { text: promptRef.current, caret: 4 };
33 + const insertReferences = vi.fn(() => {
34 + const next = ensureInlineContextReferences(promptRef.current, [
35 + imageContextReference(image),
36 + ]);
37 + store.setPrompt(targetA, next);
38 + promptRef.current = next;
39 + editor.text = next;
40 + editor.caret = next.length;
41 + return true;
42 + });
43 + const image: ComposerImageAttachment = {
44 + type: "image",
45 + id: "image-1",
46 + name: "picture.png",
47 + mimeType: "image/png",
48 + sizeBytes: 3,
49 + file: new File(["png"], "picture.png", { type: "image/png" }),
50 + previewUrl: "blob:picture",
51 + };
52 + let finishCompression!: (image: ComposerImageAttachment) => void;
53 + const compression = new Promise<ComposerImageAttachment>((resolve) => {
54 + finishCompression = resolve;
55 + });
56 + const pending = compression.then((compressed) => {
57 + const accepted = store.addImages(targetA, [compressed]);
58 + expect(accepted).toEqual([image.id]);
59 + return insertAttachmentReferencesForDraft({
60 + target: targetA,
61 + liveAttachmentTargetKey: liveTargetKey,
62 + references: [imageContextReference(compressed)],
63 + insertReferences,
64 + });
65 + });
66 +
67 + liveTargetKey = composerTargetKey(targetB);
68 + promptRef.current = "Draft B original";
69 + editor.text = promptRef.current;
70 + // Use the latest stored A prompt, including edits from elsewhere during compression.
71 + store.setPrompt(targetA, "Draft A original updated");
72 + finishCompression(image);
73 + expect(await pending).toBe(true);
74 +
75 + expect(store.getComposerDraft(targetA)?.prompt).toBe(
76 + ensureInlineContextReferences("Draft A original updated", [imageContextReference(image)]),
77 + );
78 + expect(store.getComposerDraft(targetA)?.images).toEqual([image]);
79 + expect(store.getComposerDraft(targetB)?.prompt).toBe("Draft B original");
80 + expect(store.getComposerDraft(targetB)?.images).toEqual([]);
81 + expect(promptRef.current).toBe("Draft B original");
82 + expect(editor).toEqual({ text: "Draft B original", caret: 4 });
83 + expect(insertReferences).not.toHaveBeenCalled();
84 + },
85 + );
86 +});
apps/web/src/components/chat/composerAttachmentReferences.ts new
+26
@@ -0,0 +1,26 @@
1 +import {
2 + composerTargetKey,
3 + type ComposerThreadTarget,
4 + useComposerDraftStore,
5 +} from "../../composerDraftStore";
6 +import {
7 + type ComposerContextReference,
8 + ensureInlineContextReferences,
9 +} from "../../lib/composerContextReferences";
10 +
11 +/** Route references that finish preparing asynchronously to the draft that accepted them. */
12 +export function insertAttachmentReferencesForDraft(input: {
13 + target: ComposerThreadTarget;
14 + liveAttachmentTargetKey: string;
15 + references: ReadonlyArray<ComposerContextReference>;
16 + insertReferences: () => boolean;
17 +}): boolean {
18 + if (composerTargetKey(input.target) !== input.liveAttachmentTargetKey) {
19 + // The shared editor and prompt ref now belong to another draft or question.
20 + const store = useComposerDraftStore.getState();
21 + const prompt = store.getComposerDraft(input.target)?.prompt ?? "";
22 + store.setPrompt(input.target, ensureInlineContextReferences(prompt, input.references));
23 + return true;
24 + }
25 + return input.insertReferences();
26 +}