1import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
2import { Atom } from "effect/reactivity";
3import { appAtomRegistry } from "./atom-registry";
4
5export function questionAttachmentDraftPrefix(
6 environmentId: EnvironmentId,
7 threadId: ThreadId,
8): string {
9 return `${environmentId}:question-${encodeURIComponent(JSON.stringify(threadId))}-`;
10}
11export function questionAttachmentDraftKey(
12 environmentId: EnvironmentId,
13 threadId: ThreadId,
14 requestId: string,
15 questionId: string,
16): string {
17 return `${questionAttachmentDraftPrefix(environmentId, threadId)}${encodeURIComponent(JSON.stringify([requestId, questionId]))}`;
18}
19export const questionAttachmentPreparationAtom = Atom.make<Record<string, number>>({}).pipe(
20 Atom.keepAlive,
21);
22export function changeQuestionAttachmentPreparation(key: string, delta: number): void {
23 const counts = appAtomRegistry.get(questionAttachmentPreparationAtom);
24 if (delta < 0 && !(key in counts)) return;
25 appAtomRegistry.set(questionAttachmentPreparationAtom, {
26 ...counts,
27 [key]: Math.max(0, (counts[key] ?? 0) + delta),
28 });
29}