Open History to see the last commit
1import { resolveQueuedEditExit } from "@t3tools/client-runtime/queued-run-edit";
2import type { ChatAttachment, ResponseContextRecord } from "@t3tools/contracts";
3import * as Equal from "effect/Equal";
4import {
5 composerDraftHasUserContent,
6 useComposerDraftStore,
7 type ComposerFileAttachment,
8 type ComposerImageAttachment,
9 type ComposerThreadTarget,
10} from "../../composerDraftStore";
11import { msg } from "gt-react";
12import { m } from "../../i18n/messages";
13import { releaseDraftAttachments } from "../../lib/attachmentUploadQueue";
14
15/** Staged attachments upload as soon as they are added, so a dropped edit must release them. */
16export function discardQueuedEditDraft(target: ComposerThreadTarget): void {
17 const store = useComposerDraftStore.getState();
18 const draft = store.getComposerDraft(target);
19 if (draft) {
20 releaseDraftAttachments([...draft.images, ...draft.files]);
21 for (const image of draft.images) {
22 if (image.previewUrl.startsWith("blob:")) URL.revokeObjectURL(image.previewUrl);
23 }
24 }
25 store.clearComposerContent(target);
26}
27
28export function recoverQueuedMessageEdit(input: {
29 readonly editTarget: ComposerThreadTarget;
30 readonly threadTarget: ComposerThreadTarget;
31 readonly originalText: string;
32 readonly originalReplyTo: ResponseContextRecord | null;
33 readonly removedExistingAttachment?: boolean;
34}): "kept" | "discarded" | "clean" {
35 const store = useComposerDraftStore.getState();
36 const draft = store.getComposerDraft(input.editTarget);
37 const recovery = resolveQueuedEditExit({
38 editChanged:
39 input.removedExistingAttachment === true ||
40 !Equal.equals(draft?.replyTo ?? null, input.originalReplyTo) ||
41 (draft !== null &&
42 (draft.prompt !== input.originalText || draft.images.length > 0 || draft.files.length > 0)),
43 // A cleared edit has nothing to put back, so it can only be discarded.
44 threadDraftEmpty:
45 composerDraftHasUserContent(draft) &&
46 !composerDraftHasUserContent(store.getComposerDraft(input.threadTarget)),
47 });
48 if (recovery === "kept") {
49 store.moveComposerContent(input.editTarget, input.threadTarget);
50 store.clearComposerContent(input.editTarget);
51 } else {
52 discardQueuedEditDraft(input.editTarget);
53 }
54 return recovery;
55}
56
57export async function prepareQueuedEditAttachments(input: {
58 readonly existingAttachments: ReadonlyArray<ChatAttachment>;
59 readonly images: ReadonlyArray<ComposerImageAttachment>;
60 readonly files: ReadonlyArray<ComposerFileAttachment>;
61 readonly uploadFiles: (
62 files: ReadonlyArray<ComposerFileAttachment>,
63 ) => Promise<ReadonlyArray<ChatAttachment>>;
64 readonly readImage: (file: File) => Promise<string>;
65}) {
66 const files = input.files.length === 0 ? [] : await input.uploadFiles(input.files);
67 if (files.length !== input.files.length)
68 throw new Error(m(msg("Retry or remove failed uploads before saving.")));
69 const images = await Promise.all(
70 input.images.map(async (image) => ({
71 type: "image" as const,
72 id: image.id,
73 name: image.name,
74 mimeType: image.mimeType,
75 sizeBytes: image.sizeBytes,
76 dataUrl: await input.readImage(image.file),
77 ...(image.source ? { source: image.source } : {}),
78 })),
79 );
80 return [...input.existingAttachments, ...images, ...files];
81}