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

queued-run-edit.ts

122 lines4.3 KB

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 { abortComposerDraftOperations } from "./composerDraftOperations";
14import { queuedEditDraftKey } from "./queued-edit-draft-key";
15import {
16 clearComposerDraft,
17 setComposerDraftContext,
18 setComposerDraftText,
19} from "./use-composer-drafts";
20​
21export interface QueuedRunEdit {
22 readonly runId: RunId;
23 readonly messageId: MessageId;
24 readonly originalText: string;
25 readonly existingAttachments: ReadonlyArray<ChatAttachment>;
26 readonly context?: OrchestrationMessageContext;
28​
29export const queuedRunEditsAtom = Atom.make<Readonly<Record<string, QueuedRunEdit>>>({}).pipe(
30 Atom.keepAlive,
31 Atom.withLabel("mobile-queued-run-edits"),
32);
33​
34export { isQueuedEditDraftKey, queuedEditDraftKey } from "./queued-edit-draft-key";
35​
36export function getQueuedRunEdit(threadKey: string | null): QueuedRunEdit | null {
37 if (threadKey === null) return null;
38 return appAtomRegistry.get(queuedRunEditsAtom)[threadKey] ?? null;
40​
41export function useQueuedRunEdit(threadKey: string | null): QueuedRunEdit | null {
42 const edits = useAtomValue(queuedRunEditsAtom);
43 return threadKey === null ? null : (edits[threadKey] ?? null);
45​
46function setQueuedRunEdit(threadKey: string, edit: QueuedRunEdit | null): void {
47 const current = appAtomRegistry.get(queuedRunEditsAtom);
48 if (edit === null) {
49 if (!current[threadKey]) return;
50 const next = { ...current };
51 delete next[threadKey];
52 appAtomRegistry.set(queuedRunEditsAtom, next);
53 return;
54 }
55 appAtomRegistry.set(queuedRunEditsAtom, { ...current, [threadKey]: edit });
57​
58export function beginQueuedRunEdit(threadKey: string, edit: QueuedRunEdit): void {
59 const previous = getQueuedRunEdit(threadKey);
60 if (previous !== null && previous.runId !== edit.runId) {
61 clearComposerDraft(queuedEditDraftKey(threadKey, previous.runId));
62 }
63 // The composer moves off the thread draft, so a paste or pick still running for it is dropped.
64 abortComposerDraftOperations(threadKey);
65 const draftKey = queuedEditDraftKey(threadKey, edit.runId);
66 clearComposerDraft(draftKey);
67 setComposerDraftText(draftKey, edit.originalText);
68 setComposerDraftContext(draftKey, edit.context);
69 setQueuedRunEdit(threadKey, edit);
71​
72export function removeQueuedRunEditAttachment(threadKey: string, attachmentId: string): void {
73 const edit = getQueuedRunEdit(threadKey);
74 if (edit === null) return;
75 setQueuedRunEdit(threadKey, {
76 ...edit,
77 existingAttachments: edit.existingAttachments.filter(
78 (attachment) => attachment.id !== attachmentId,
79 ),
80 });
82​
83export function endQueuedRunEdit(
84 threadKey: string,
85 options?: { readonly deferAttachmentCleanup?: boolean },
86): void {
87 const edit = getQueuedRunEdit(threadKey);
88 if (edit === null) return;
89 clearComposerDraft(queuedEditDraftKey(threadKey, edit.runId), options);
90 setQueuedRunEdit(threadKey, null);
92​
93/** Attachments replace wholesale; resend kept server attachments with uploads in composer order. */
94export function resolveQueuedEditPayload(input: {
95 readonly edit: QueuedRunEdit;
96 readonly draftContext: OrchestrationMessageContext | undefined;
97 readonly draftAttachments: ReadonlyArray<{ readonly id: string }>;
98 readonly uploaded: ReadonlyArray<ChatAttachment | UploadChatAttachment>;
99}): {
100 readonly attachments: ReadonlyArray<ChatAttachment | UploadChatAttachment>;
101 readonly context: OrchestrationMessageContext | undefined;
102} {
103 const attachments = [...input.edit.existingAttachments, ...input.uploaded];
104 const rebound = uploadedComposerContext(
105 input.draftContext,
106 input.draftAttachments,
107 input.uploaded,
108 );
109 if (rebound === undefined) return { attachments, context: undefined };
110 const liveIds = new Set(
111 attachments.flatMap((attachment) =>
112 "id" in attachment && attachment.id ? [attachment.id] : [],
113 ),
114 );
115 const records = rebound.records.filter(
116 (record) => !("attachmentId" in record) || liveIds.has(record.attachmentId),
117 );
118 return {
119 attachments,
120 context: records.length > 0 ? { version: 1, records } : undefined,
121 };