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