Open History to see the last commit
1import type { ChatAttachment } from "@t3tools/contracts";
2import {
3 composerDraftHasUserContent,
4 useComposerDraftStore,
5 type ComposerFileAttachment,
6 type ComposerImageAttachment,
7 type ComposerThreadTarget,
8} from "../../composerDraftStore";
9
10export function recoverQueuedMessageEdit(input: {
11 readonly editTarget: ComposerThreadTarget;
12 readonly threadTarget: ComposerThreadTarget;
13 readonly originalText: string;
14}): "kept" | "discarded" | "clean" {
15 const store = useComposerDraftStore.getState();
16 const draft = store.getComposerDraft(input.editTarget);
17 const dirty =
18 draft !== null &&
19 (draft.prompt !== input.originalText || draft.images.length > 0 || draft.files.length > 0);
20 if (dirty && !composerDraftHasUserContent(store.getComposerDraft(input.threadTarget))) {
21 store.moveComposerPromptAndImages(input.editTarget, input.threadTarget);
22 return "kept";
23 }
24 store.clearComposerContent(input.editTarget);
25 return dirty ? "discarded" : "clean";
26}
27
28export async function prepareQueuedEditAttachments(input: {
29 readonly existingAttachments: ReadonlyArray<ChatAttachment>;
30 readonly images: ReadonlyArray<ComposerImageAttachment>;
31 readonly files: ReadonlyArray<ComposerFileAttachment>;
32 readonly uploadFiles: (
33 files: ReadonlyArray<ComposerFileAttachment>,
34 ) => Promise<ReadonlyArray<ChatAttachment>>;
35 readonly readImage: (file: File) => Promise<string>;
36}) {
37 const files = input.files.length === 0 ? [] : await input.uploadFiles(input.files);
38 if (files.length !== input.files.length)
39 throw new Error("Retry or remove failed uploads before saving.");
40 const images = await Promise.all(
41 input.images.map(async (image) => ({
42 type: "image" as const,
43 id: image.id,
44 name: image.name,
45 mimeType: image.mimeType,
46 sizeBytes: image.sizeBytes,
47 dataUrl: await input.readImage(image.file),
48 ...(image.source ? { source: image.source } : {}),
49 })),
50 );
51 return [...input.existingAttachments, ...images, ...files];
52}