Open History to see the last commit
1import type {
2 ChatAttachment,
3 MessageId,
4 OrchestrationMessageContext,
5 RunId,
6 UploadChatAttachment,
7} from "@t3tools/contracts";
8import { Atom } from "effect/reactivity";
9import { useAtomValue } from "@effect/atom-react";
10
11import { uploadedComposerContext } from "../lib/composerContext";
12import { appAtomRegistry } from "./atom-registry";
13import { queuedEditDraftKey } from "./queued-edit-draft-key";
14import {
15 clearComposerDraft,
16 setComposerDraftContext,
17 setComposerDraftText,
18} from "./use-composer-drafts";
19
20export interface QueuedRunEdit {
21 readonly runId: RunId;
22 readonly messageId: MessageId;
23 readonly originalText: string;
24 readonly existingAttachments: ReadonlyArray<ChatAttachment>;
25 readonly context?: OrchestrationMessageContext;
26}
27
28export const queuedRunEditsAtom = Atom.make<Readonly<Record<string, QueuedRunEdit>>>({}).pipe(
29 Atom.keepAlive,
30 Atom.withLabel("mobile-queued-run-edits"),
31);
32
33export { isQueuedEditDraftKey, queuedEditDraftKey } from "./queued-edit-draft-key";
34
35export function getQueuedRunEdit(threadKey: string | null): QueuedRunEdit | null {
36 if (threadKey === null) return null;
37 return appAtomRegistry.get(queuedRunEditsAtom)[threadKey] ?? null;
38}
39
40export function useQueuedRunEdit(threadKey: string | null): QueuedRunEdit | null {
41 const edits = useAtomValue(queuedRunEditsAtom);
42 return threadKey === null ? null : (edits[threadKey] ?? null);
43}
44
45function setQueuedRunEdit(threadKey: string, edit: QueuedRunEdit | null): void {
46 const current = appAtomRegistry.get(queuedRunEditsAtom);
47 if (edit === null) {
48 if (!current[threadKey]) return;
49 const next = { ...current };
50 delete next[threadKey];
51 appAtomRegistry.set(queuedRunEditsAtom, next);
52 return;
53 }
54 appAtomRegistry.set(queuedRunEditsAtom, { ...current, [threadKey]: edit });
55}
56
57export function beginQueuedRunEdit(threadKey: string, edit: QueuedRunEdit): void {
58 const previous = getQueuedRunEdit(threadKey);
59 if (previous !== null && previous.runId !== edit.runId) {
60 clearComposerDraft(queuedEditDraftKey(threadKey, previous.runId));
61 }
62 const draftKey = queuedEditDraftKey(threadKey, edit.runId);
63 clearComposerDraft(draftKey);
64 setComposerDraftText(draftKey, edit.originalText);
65 setComposerDraftContext(draftKey, edit.context);
66 setQueuedRunEdit(threadKey, edit);
67}
68
69export function removeQueuedRunEditAttachment(threadKey: string, attachmentId: string): void {
70 const edit = getQueuedRunEdit(threadKey);
71 if (edit === null) return;
72 setQueuedRunEdit(threadKey, {
73 ...edit,
74 existingAttachments: edit.existingAttachments.filter(
75 (attachment) => attachment.id !== attachmentId,
76 ),
77 });
78}
79
80export function endQueuedRunEdit(
81 threadKey: string,
82 options?: { readonly deferAttachmentCleanup?: boolean },
83): void {
84 const edit = getQueuedRunEdit(threadKey);
85 if (edit === null) return;
86 clearComposerDraft(queuedEditDraftKey(threadKey, edit.runId), options);
87 setQueuedRunEdit(threadKey, null);
88}
89
90/** Attachments replace wholesale; resend kept server attachments with uploads in composer order. */
91export function resolveQueuedEditPayload(input: {
92 readonly edit: QueuedRunEdit;
93 readonly draftContext: OrchestrationMessageContext | undefined;
94 readonly draftAttachments: ReadonlyArray<{ readonly id: string }>;
95 readonly uploaded: ReadonlyArray<ChatAttachment | UploadChatAttachment>;
96}): {
97 readonly attachments: ReadonlyArray<ChatAttachment | UploadChatAttachment>;
98 readonly context: OrchestrationMessageContext | undefined;
99} {
100 const attachments = [...input.edit.existingAttachments, ...input.uploaded];
101 const rebound = uploadedComposerContext(
102 input.draftContext,
103 input.draftAttachments,
104 input.uploaded,
105 );
106 if (rebound === undefined) return { attachments, context: undefined };
107 const liveIds = new Set(
108 attachments.flatMap((attachment) =>
109 "id" in attachment && attachment.id ? [attachment.id] : [],
110 ),
111 );
112 const records = rebound.records.filter(
113 (record) => !("attachmentId" in record) || liveIds.has(record.attachmentId),
114 );
115 return {
116 attachments,
117 context: records.length > 0 ? { version: 1, records } : undefined,
118 };
119}