Skip to content

Commit 599d210

Browse files
saphidcodex
andauthored
fix(web): keep compressed image chips in their original draft (pingdotgg#18393)
Co-authored-by: SAPHID <[email protected]> Co-authored-by: GPT-6.1 Sol <[email protected]>
1 parent 1114c38 commit 599d210

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";
@@ -5538,16 +5539,23 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
55385539
// Question answers carry attachments beside the answer, never as chips. Falling back to
55395540
// the thread prompt here would hide the file behind a reference the question never shows.
55405541
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+
});
55515559
};
55525560

55535561
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)