Skip to content

Commit 807055a

Browse files
saphidcodex
andcommitted
fix(web): keep compressed image chips in their original draft
Co-Authored-By: GPT-6.1 Sol <[email protected]>
1 parent 83fbbc1 commit 807055a

3 files changed

Lines changed: 130 additions & 10 deletions

File tree

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { formatProviderSkillDisplayName } from "@t3tools/shared/inlineSkills";
22
import { useComposerTypingGuard } from "./useComposerTypingGuard";
3+
import { insertAttachmentReferencesForDraft } from "./composerAttachmentReferences";
34
import { DESKTOP_PASTE_AS_TEXT_EVENT } from "../../lib/desktopPasteAsText";
45
import { runtimeModeConfig, runtimeModeOptions as runtimeModes } from "./runtimeModeConfig";
56
import { isLocalEnvironmentDisabled } from "../../localEnvironment";
@@ -6028,16 +6029,23 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
60286029
// Question answers carry attachments beside the answer, never as chips. Falling back to
60296030
// the thread prompt here would hide the file behind a reference the question never shows.
60306031
if (questionAttachmentTarget) return false;
6031-
if (selection) {
6032-
const edit = inlineContextReferenceReplacement(promptRef.current, selection, references);
6033-
return applyPromptReplacement(edit.start, edit.end, edit.text);
6034-
}
6035-
const text = references.map(formatInlineContextReference).join(" ");
6036-
const inserted = insertComposerText(`${text} `, "cursor", { ensureLeadingBoundary: true });
6037-
if (!inserted) {
6038-
setPrompt(ensureInlineContextReferences(promptRef.current, references));
6039-
}
6040-
return true;
6032+
return insertAttachmentReferencesForDraft({
6033+
target: composerDraftTarget,
6034+
liveAttachmentTargetKey: attachmentTargetKeyRef.current,
6035+
references,
6036+
insertReferences: () => {
6037+
if (selection) {
6038+
const edit = inlineContextReferenceReplacement(promptRef.current, selection, references);
6039+
return applyPromptReplacement(edit.start, edit.end, edit.text);
6040+
}
6041+
const text = references.map(formatInlineContextReference).join(" ");
6042+
const inserted = insertComposerText(`${text} `, "cursor", { ensureLeadingBoundary: true });
6043+
if (!inserted) {
6044+
setPrompt(ensureInlineContextReferences(promptRef.current, references));
6045+
}
6046+
return true;
6047+
},
6048+
});
60416049
};
60426050

60436051
const removeComposerImage = (imageId: string) => {
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

0 commit comments

Comments
 (0)