Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
import * as Schema from "effect/Schema";
import { Atom } from "effect/unstable/reactivity";
import { writeFileAtomically } from "../lib/atomic-file";
import { SerializedAsyncQueue } from "../lib/serialized-async-queue";
import { appAtomRegistry } from "./atom-registry";
import {
COMPOSER_DRAFTS_SCHEMA_VERSION,
type ComposerDraft,
decodePersistedComposerDrafts,
isEmptyDraft,
normalizeDraft,
} from "./composer-draft-schema";
const COMPOSER_DRAFTS_DIRECTORY = "composer-drafts";
const COMPOSER_DRAFTS_FILE = "drafts.json";
const PERSIST_DEBOUNCE_MS = 200;
export class ComposerDraftPersistenceError extends Schema.TaggedErrorClass<ComposerDraftPersistenceError>()(
"ComposerDraftPersistenceError",
{
operation: Schema.Literals(["open", "read", "decode", "encode", "write", "hydrate"]),
directory: Schema.String,
fileName: Schema.String,
cause: Schema.Defect(),
},
) {
override get message(): string {
return `Composer draft persistence operation ${this.operation} failed for ${this.directory}/${this.fileName}.`;
}
}
export const composerDraftsAtom = Atom.make<Record<string, ComposerDraft>>({}).pipe(
Atom.keepAlive,
Atom.withLabel("mobile:composer-drafts"),
);
let loadPromise: Promise<void> | null = null;
let persistTimer: ReturnType<typeof setTimeout> | null = null;
let persistRetryNeeded = false;
let persistRevision = 0;
const persistenceQueue = new SerializedAsyncQueue();
/** Resets module-level state between test runs. */
export function resetComposerDraftsLoadState(): void {
loadPromise = null;
persistRetryNeeded = false;
persistRevision = 0;
if (persistTimer !== null) {
clearTimeout(persistTimer);
persistTimer = null;
}
}
export function getComposerDraftSnapshot(draftKey: string): ComposerDraft {
return normalizeDraft(appAtomRegistry.get(composerDraftsAtom)[draftKey]);
}
async function getComposerDraftsFile() {
const { Directory, File, Paths } = await import("expo-file-system");
const directory = new Directory(Paths.document, COMPOSER_DRAFTS_DIRECTORY);
directory.create({ idempotent: true, intermediates: true });
return new File(directory, COMPOSER_DRAFTS_FILE);
}
async function loadPersistedComposerDrafts(): Promise<Record<string, ComposerDraft>> {
let operation: ComposerDraftPersistenceError["operation"] = "open";
try {
const file = await getComposerDraftsFile();
if (!file.exists) {
return {};
}
operation = "read";
const raw = await file.text();
operation = "decode";
return decodePersistedComposerDrafts(JSON.parse(raw));
} catch (cause) {
throw new ComposerDraftPersistenceError({
operation,
directory: COMPOSER_DRAFTS_DIRECTORY,
fileName: COMPOSER_DRAFTS_FILE,
cause,
});
}
}
async function writePersistedComposerDrafts(drafts: Record<string, ComposerDraft>): Promise<void> {
let operation: ComposerDraftPersistenceError["operation"] = "open";
try {
const file = await getComposerDraftsFile();
operation = "encode";
const nonEmptyDrafts = Object.fromEntries(
Object.entries(drafts).filter(([, draft]) => !isEmptyDraft(draft)),
);
const document = {
schemaVersion: COMPOSER_DRAFTS_SCHEMA_VERSION,
drafts: nonEmptyDrafts,
} as const;
const encoded = JSON.stringify(document);
operation = "write";
await writeFileAtomically(file, encoded);
} catch (cause) {
throw new ComposerDraftPersistenceError({
operation,
directory: COMPOSER_DRAFTS_DIRECTORY,
fileName: COMPOSER_DRAFTS_FILE,
cause,
});
}
}
/**
* Lands any debounced or in-flight draft write before the JS runtime is torn
* down (app update restart), so the freshest draft state survives it. A write
* failure propagates so the caller can decide whether the restart may proceed.
*/
export async function flushComposerDrafts(): Promise<void> {
// Never land a pre-hydration snapshot: persisted state must merge into the
// atoms first, or this write would clobber disk with partial data.
ensureComposerDraftsLoaded();
if (loadPromise !== null) {
await loadPromise;
}
// An edit during an awaited write schedules another debounced write, so
// keep landing snapshots until no debounce is pending after a queue drain.
for (;;) {
const revisionToFlush = persistRevision;
while (persistTimer !== null || persistRetryNeeded) {
if (persistTimer !== null) clearTimeout(persistTimer);
persistTimer = null;
persistRetryNeeded = false;
try {
await persistenceQueue.run(() =>
writePersistedComposerDrafts(appAtomRegistry.get(composerDraftsAtom)),
);
} catch (error) {
persistRetryNeeded = true;
throw error;
}
}
// Draining also waits for an already-fired debounce whose write is still
// gated behind its own hydration await inside the queue.
await persistenceQueue.run(() => Promise.resolve());
// An edit can schedule its debounce while the drain sentinel is waiting
// behind an older write. Its revision changes immediately, before that
// later write enters the queue, so repeat until that edit is durable too.
if (persistRevision === revisionToFlush && persistTimer === null && !persistRetryNeeded) {
return;
}
}
}
function schedulePersistComposerDrafts(): void {
persistRevision += 1;
if (persistTimer !== null) {
clearTimeout(persistTimer);
}
persistTimer = setTimeout(() => {
persistTimer = null;
// The write enters the serialization queue before waiting on hydration,
// so flushComposerDrafts' queue drain cannot resolve ahead of it.
void persistenceQueue.run(async () => {
try {
await waitForComposerDraftsLoaded();
await writePersistedComposerDrafts(appAtomRegistry.get(composerDraftsAtom));
persistRetryNeeded = false;
} catch (error) {
// A failed debounce has no timer left. A later final flush must retry
// these edits after persisted ownership can be read safely.
persistRetryNeeded = true;
console.warn("[composer-drafts] failed to persist drafts", error);
// Draft persistence is best-effort; in-memory drafts still keep working.
}
});
}, PERSIST_DEBOUNCE_MS);
}
export function ensureComposerDraftsLoaded(): void {
if (loadPromise !== null) {
return;
}
const loading = loadPersistedComposerDrafts().then((persistedDrafts) => {
if (Object.keys(persistedDrafts).length === 0) {
return;
}
const current = appAtomRegistry.get(composerDraftsAtom);
appAtomRegistry.set(composerDraftsAtom, {
...persistedDrafts,
...current,
});
});
loadPromise = loading;
// Handle fire-and-forget hook loads without swallowing failures from the
// write and cleanup callers that await this same promise. A later call retries.
void loading.catch((cause) => {
if (loadPromise === loading) loadPromise = null;
console.warn(
"[composer-drafts] failed to hydrate drafts",
cause instanceof ComposerDraftPersistenceError
? cause
: new ComposerDraftPersistenceError({
operation: "hydrate",
directory: COMPOSER_DRAFTS_DIRECTORY,
fileName: COMPOSER_DRAFTS_FILE,
cause,
}),
);
});
}
/** Wait until persisted drafts have been merged into the in-memory composer state. */
export async function waitForComposerDraftsLoaded(): Promise<void> {
ensureComposerDraftsLoaded();
if (loadPromise !== null) {
await loadPromise;
}
}
export function updateComposerDrafts(
update: (current: Record<string, ComposerDraft>) => Record<string, ComposerDraft>,
): void {
const current = appAtomRegistry.get(composerDraftsAtom);
const next = update(current);
if (next === current) {
return;
}
appAtomRegistry.set(composerDraftsAtom, next);
schedulePersistComposerDrafts();
}
/** Drops a pending debounced write; the caller is about to write through. */
export function cancelScheduledComposerDraftsPersist(): void {
if (persistTimer !== null) {
clearTimeout(persistTimer);
persistTimer = null;
}
}
/** Writes `drafts` now, serialized after any in-flight write. */
export function writeComposerDraftsNow(drafts: Record<string, ComposerDraft>): Promise<void> {
return persistenceQueue.run(() => writePersistedComposerDrafts(drafts));
}