Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

queuedMessageEdit.ts

56 lines2.1 KB

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