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

unconfirmedSideChatFork.ts

70 lines2.8 KB

1import { CommandId, type ScopedThreadRef, ThreadId } from "@t3tools/contracts";
2import * as Option from "effect/Option";
3import * as Schema from "effect/Schema";
4​
5const UnconfirmedSideChatFork = Schema.fromJsonString(
6 Schema.Struct({ targetThreadId: ThreadId, commandId: CommandId }),
7);
8export type UnconfirmedSideChatFork = typeof UnconfirmedSideChatFork.Type;
9const decode = Schema.decodeOption(UnconfirmedSideChatFork);
10const encode = Schema.encodeSync(UnconfirmedSideChatFork);
11​
12/** Synchronous key-value storage that outlives a reload or restart; any call may throw. */
13export interface UnconfirmedSideChatForkStorage {
14 readonly getItem: (key: string) => string | null;
15 readonly setItem: (key: string, value: string) => void;
16 readonly removeItem: (key: string) => void;
18​
19/** A side chat that has a run or a message already belongs to someone; it is not resent. */
20export const sideChatHasStarted = (shell: {
21 readonly latestRun: unknown;
22 readonly latestUserMessageAt: unknown;
23}) => shell.latestRun !== null || shell.latestUserMessageAt !== null;
24​
25/**
26 * Per parent thread, the fork a side chat start sent without learning whether it landed. The next
27 * start resends the same command, so a fork the server made is reused instead of doubled.
28 * Storage carries it across a reload; what this run wrote wins, so storage that fails partway
29 * cannot bring back an older record.
30 */
31export function makeUnconfirmedSideChatForks(storage: () => UnconfirmedSideChatForkStorage) {
32 const written = new Map<string, UnconfirmedSideChatFork | null>();
33 const keyOf = (parent: ScopedThreadRef) =>
34 `t3code.side-chat-unconfirmed-fork.${parent.environmentId}.${parent.threadId}`;
35​
36 const write = (parent: ScopedThreadRef, fork: UnconfirmedSideChatFork | null) => {
37 const key = keyOf(parent);
38 written.set(key, fork);
39 try {
40 if (fork === null) storage().removeItem(key);
41 else storage().setItem(key, encode(fork));
42 } catch {}
43 };
44​
45 const read = (parent: ScopedThreadRef): UnconfirmedSideChatFork | null => {
46 const key = keyOf(parent);
47 if (written.has(key)) return written.get(key) ?? null;
48 try {
49 const stored = storage().getItem(key);
50 return stored ? Option.getOrNull(decode(stored)) : null;
51 } catch {
52 return null;
53 }
54 };
55​
56 /** The stored fork, unless its side chat is known here to have started; that one is dropped. */
57 const readReusable = (
58 parent: ScopedThreadRef,
59 readShell: (targetThreadId: ThreadId) => Parameters<typeof sideChatHasStarted>[0] | null,
60 ) => {
61 const stored = read(parent);
62 if (stored === null) return null;
63 const shell = readShell(stored.targetThreadId);
64 if (shell === null || !sideChatHasStarted(shell)) return stored;
65 write(parent, null);
66 return null;
67 };
68​
69 return { write, readReusable };