|
| 1 | +import { useAtomValue } from "@effect/atom-react"; |
| 2 | +import { |
| 3 | + canonicalComposerDraftCommon, |
| 4 | + composerDraftCommonEquals, |
| 5 | + createComposerDraftEnvironmentAtoms, |
| 6 | + createComposerDraftSyncController, |
| 7 | + type ComposerDraftSyncController, |
| 8 | +} from "@t3tools/client-runtime/state/composer-drafts"; |
| 9 | +import type { |
| 10 | + ComposerDraftCommon, |
| 11 | + ComposerDraftSnapshot, |
| 12 | + ScopedThreadRef, |
| 13 | +} from "@t3tools/contracts"; |
| 14 | +import { AsyncResult, Atom } from "effect/unstable/reactivity"; |
| 15 | +import { useEffect, useRef } from "react"; |
| 16 | + |
| 17 | +import { connectionAtomRuntime } from "../connection/runtime"; |
| 18 | +import { scopedThreadKey } from "../lib/scopedEntities"; |
| 19 | +import { uuidv4 } from "../lib/uuid"; |
| 20 | +import { appAtomRegistry } from "./atom-registry"; |
| 21 | +import { serverEnvironment } from "./server"; |
| 22 | +import { |
| 23 | + applySyncedComposerDraftCommon, |
| 24 | + composerDraftsAtom, |
| 25 | + composerDraftsLoadedAtom, |
| 26 | + type ComposerDraft, |
| 27 | +} from "./use-composer-drafts"; |
| 28 | + |
| 29 | +export const composerDraftEnvironment = createComposerDraftEnvironmentAtoms(connectionAtomRuntime); |
| 30 | + |
| 31 | +const EMPTY_SYNC_ATOM = Atom.make<null>(null).pipe( |
| 32 | + Atom.withLabel("mobile:composer-draft-sync-disabled"), |
| 33 | +); |
| 34 | +const revisions = new Map<string, number>(); |
| 35 | +const suppressedPostSendCommon = new Map<string, ComposerDraftCommon | null>(); |
| 36 | + |
| 37 | +export function readComposerDraftRevision(threadRef: ScopedThreadRef): number | undefined { |
| 38 | + return revisions.get(scopedThreadKey(threadRef.environmentId, threadRef.threadId)); |
| 39 | +} |
| 40 | + |
| 41 | +/** Prevents retained selector settings from being mistaken for a new draft. */ |
| 42 | +export function markComposerDraftSent(threadRef: ScopedThreadRef): void { |
| 43 | + const key = scopedThreadKey(threadRef.environmentId, threadRef.threadId); |
| 44 | + const draft = appAtomRegistry.get(composerDraftsAtom)[key]; |
| 45 | + suppressedPostSendCommon.set(key, commonFromDraft(draft ?? { text: "", attachments: [] })); |
| 46 | +} |
| 47 | + |
| 48 | +function commonFromDraft(draft: ComposerDraft): ComposerDraftCommon | null { |
| 49 | + if (draft.attachments.length > 0) return null; |
| 50 | + return canonicalComposerDraftCommon({ |
| 51 | + text: draft.text, |
| 52 | + modelSelection: draft.modelSelection ?? null, |
| 53 | + runtimeMode: draft.runtimeMode ?? null, |
| 54 | + interactionMode: draft.interactionMode ?? null, |
| 55 | + }); |
| 56 | +} |
| 57 | + |
| 58 | +export function useServerComposerDraftSync(threadRef: ScopedThreadRef | null): void { |
| 59 | + const drafts = useAtomValue(composerDraftsAtom); |
| 60 | + const draftsLoaded = useAtomValue(composerDraftsLoadedAtom); |
| 61 | + const serverConfig = useAtomValue( |
| 62 | + threadRef === null |
| 63 | + ? EMPTY_SYNC_ATOM |
| 64 | + : serverEnvironment.configValueAtom(threadRef.environmentId), |
| 65 | + ); |
| 66 | + const enabled = |
| 67 | + draftsLoaded && |
| 68 | + threadRef !== null && |
| 69 | + serverConfig !== null && |
| 70 | + "environment" in serverConfig && |
| 71 | + serverConfig.environment.capabilities.composerDraftSync === true; |
| 72 | + const streamResult = useAtomValue( |
| 73 | + enabled && threadRef !== null |
| 74 | + ? composerDraftEnvironment.changes({ |
| 75 | + environmentId: threadRef.environmentId, |
| 76 | + input: { threadId: threadRef.threadId }, |
| 77 | + }) |
| 78 | + : EMPTY_SYNC_ATOM, |
| 79 | + ); |
| 80 | + const draftKey = |
| 81 | + threadRef === null ? null : scopedThreadKey(threadRef.environmentId, threadRef.threadId); |
| 82 | + const draft = draftKey === null ? null : (drafts[draftKey] ?? null); |
| 83 | + const draftRef = useRef<ComposerDraft | null>(draft); |
| 84 | + draftRef.current = draft; |
| 85 | + const controllerRef = useRef<ComposerDraftSyncController | null>(null); |
| 86 | + |
| 87 | + useEffect(() => { |
| 88 | + controllerRef.current?.dispose(); |
| 89 | + if (!enabled || threadRef === null || draftKey === null) { |
| 90 | + controllerRef.current = null; |
| 91 | + return; |
| 92 | + } |
| 93 | + const key = draftKey; |
| 94 | + const readLocal = () => { |
| 95 | + const common = commonFromDraft(draftRef.current ?? { text: "", attachments: [] }); |
| 96 | + if (!suppressedPostSendCommon.has(key)) return common; |
| 97 | + const baseline = suppressedPostSendCommon.get(key) ?? null; |
| 98 | + if (composerDraftCommonEquals(common, baseline)) return null; |
| 99 | + suppressedPostSendCommon.delete(key); |
| 100 | + return common; |
| 101 | + }; |
| 102 | + const controller = createComposerDraftSyncController({ |
| 103 | + threadId: threadRef.threadId, |
| 104 | + readLocal, |
| 105 | + canApplyRemote: () => (draftRef.current?.attachments.length ?? 0) === 0, |
| 106 | + applyRemote: (common) => applySyncedComposerDraftCommon(key, common), |
| 107 | + update: async (input) => { |
| 108 | + const result = await composerDraftEnvironment.update.run(appAtomRegistry, { |
| 109 | + environmentId: threadRef.environmentId, |
| 110 | + input, |
| 111 | + }); |
| 112 | + return AsyncResult.isSuccess(result) ? result.value : null; |
| 113 | + }, |
| 114 | + createMutationId: () => `mobile:${uuidv4()}`, |
| 115 | + scheduleTask: (task, delayMs) => { |
| 116 | + const timer = setTimeout(task, delayMs); |
| 117 | + return () => clearTimeout(timer); |
| 118 | + }, |
| 119 | + onRevisionChange: (snapshot: ComposerDraftSnapshot) => { |
| 120 | + revisions.set(key, snapshot.revision); |
| 121 | + }, |
| 122 | + }); |
| 123 | + controllerRef.current = controller; |
| 124 | + return () => { |
| 125 | + controller.dispose(); |
| 126 | + if (controllerRef.current === controller) controllerRef.current = null; |
| 127 | + revisions.delete(key); |
| 128 | + suppressedPostSendCommon.delete(key); |
| 129 | + }; |
| 130 | + }, [draftKey, enabled, threadRef?.environmentId, threadRef?.threadId]); |
| 131 | + |
| 132 | + useEffect(() => { |
| 133 | + if (streamResult !== null && AsyncResult.isSuccess(streamResult)) { |
| 134 | + controllerRef.current?.observeSnapshot(streamResult.value); |
| 135 | + } |
| 136 | + }, [streamResult]); |
| 137 | + |
| 138 | + useEffect(() => { |
| 139 | + controllerRef.current?.observeLocalChange(); |
| 140 | + }, [ |
| 141 | + draft?.attachments, |
| 142 | + draft?.interactionMode, |
| 143 | + draft?.modelSelection, |
| 144 | + draft?.runtimeMode, |
| 145 | + draft?.text, |
| 146 | + ]); |
| 147 | +} |
0 commit comments