The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { appAtomRegistry } from "./atom-registry"; |
| 2 | import { createThreadOutboxManager } from "./thread-outbox-manager"; |
| 3 | import type { QueuedThreadMessage } from "./thread-outbox-model"; |
| 4 | import { expoThreadOutboxStorage, flushThreadOutboxWrites } from "./thread-outbox-storage"; |
| 5 | |
| 6 | export * from "./thread-outbox-model"; |
| 7 | |
| 8 | export const threadOutboxManager = createThreadOutboxManager({ |
| 9 | registry: appAtomRegistry, |
| 10 | storage: expoThreadOutboxStorage, |
| 11 | }); |
| 12 | |
| 13 | /** Drain queued mutations too; optimistic publication precedes the durable file write. */ |
| 14 | export async function flushThreadOutbox(): Promise<void> { |
| 15 | await threadOutboxManager.serialize(async () => {}); |
| 16 | await flushThreadOutboxWrites(); |
| 17 | } |
| 18 | |
| 19 | export function enqueueThreadOutboxMessage(message: QueuedThreadMessage): Promise<void> { |
| 20 | return threadOutboxManager.enqueue(message); |
| 21 | } |
| 22 | |
| 23 | export function confirmThreadOutboxMessageQueued(message: QueuedThreadMessage): Promise<boolean> { |
| 24 | return threadOutboxManager.confirmQueued(message); |
| 25 | } |
| 26 | |
| 27 | /** Returns false if removed or if expectedRevision differs from threadOutboxRevision; otherwise rewrites the message. */ |
| 28 | export function updateThreadOutboxMessage( |
| 29 | message: QueuedThreadMessage, |
| 30 | expectedRevision?: number, |
| 31 | ): Promise<boolean> { |
| 32 | return threadOutboxManager.update(message, expectedRevision); |
| 33 | } |
| 34 | |
| 35 | /** Changes the message as it is when its turn comes; false when it is gone or `change` returns null. */ |
| 36 | export function modifyThreadOutboxMessage( |
| 37 | messageId: QueuedThreadMessage["messageId"], |
| 38 | change: (message: QueuedThreadMessage) => QueuedThreadMessage | null, |
| 39 | ): Promise<boolean> { |
| 40 | return threadOutboxManager.modify(messageId, change); |
| 41 | } |
| 42 | |
| 43 | export function threadOutboxRevision(messageId: QueuedThreadMessage["messageId"]): number { |
| 44 | return threadOutboxManager.revisionOf(messageId); |
| 45 | } |
| 46 | |
| 47 | // Removal needs draft-state ownership to release files; keep that dependency outside this module. |