The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { EnvironmentId } from "@t3tools/contracts"; |
| 2 | |
| 3 | import { appAtomRegistry } from "./atom-registry"; |
| 4 | import { threadOutboxManager } from "./thread-outbox"; |
| 5 | import type { QueuedThreadMessage } from "./thread-outbox-model"; |
| 6 | import { |
| 7 | clearComposerDraft, |
| 8 | composerDraftsAtom, |
| 9 | flushComposerDrafts, |
| 10 | scheduleUnusedComposerAttachmentCleanup, |
| 11 | waitForComposerDraftsLoaded, |
| 12 | } from "./use-composer-drafts"; |
| 13 | |
| 14 | async function cleanUpRemovedMessages( |
| 15 | removedMessages: ReadonlyArray<QueuedThreadMessage>, |
| 16 | ): Promise<void> { |
| 17 | const attachments = removedMessages.flatMap((message) => message.attachments); |
| 18 | const removedCreations = removedMessages.filter((message) => message.creation !== undefined); |
| 19 | if (removedCreations.length === 0) { |
| 20 | scheduleUnusedComposerAttachmentCleanup(attachments); |
| 21 | return; |
| 22 | } |
| 23 | |
| 24 | try { |
| 25 | await waitForComposerDraftsLoaded(); |
| 26 | const liveMessageIds = new Set( |
| 27 | Object.values(appAtomRegistry.get(threadOutboxManager.queuedMessagesByThreadKeyAtom)) |
| 28 | .flat() |
| 29 | .map((message) => message.messageId), |
| 30 | ); |
| 31 | const drafts = appAtomRegistry.get(composerDraftsAtom); |
| 32 | let clearedDraft = false; |
| 33 | for (const message of removedCreations) { |
| 34 | if (liveMessageIds.has(message.messageId)) { |
| 35 | continue; |
| 36 | } |
| 37 | const draftKey = `pending-task:${message.messageId}`; |
| 38 | const draft = drafts[draftKey]; |
| 39 | if (draft === undefined) { |
| 40 | continue; |
| 41 | } |
| 42 | attachments.push(...draft.attachments); |
| 43 | clearComposerDraft(draftKey, { deferAttachmentCleanup: true }); |
| 44 | clearedDraft = true; |
| 45 | } |
| 46 | if (clearedDraft) { |
| 47 | await flushComposerDrafts(); |
| 48 | } |
| 49 | } catch (error) { |
| 50 | // Removal is already durable; retain files and report secondary cleanup failure without changing success. |
| 51 | console.warn("[thread-outbox] failed to clean up removed pending task drafts", error); |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | scheduleUnusedComposerAttachmentCleanup(attachments); |
| 56 | } |
| 57 | |
| 58 | /** Returns false without releasing files if expectedRevision changed or canRemove rejects current ownership. */ |
| 59 | export async function removeThreadOutboxMessage( |
| 60 | message: QueuedThreadMessage, |
| 61 | expectedRevision?: number, |
| 62 | canRemove?: () => boolean, |
| 63 | ): Promise<boolean> { |
| 64 | const removed = await threadOutboxManager.remove(message, expectedRevision, canRemove); |
| 65 | if (removed === null) { |
| 66 | return false; |
| 67 | } |
| 68 | // Clean the removed payload; accepted updates may contain files absent from the caller's snapshot. |
| 69 | await cleanUpRemovedMessages([removed]); |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | export async function clearThreadOutboxEnvironment(environmentId: EnvironmentId): Promise<void> { |
| 74 | // Use the actual removed set, including persisted messages missed by an earlier failed hydration. |
| 75 | const removed = await threadOutboxManager.clearEnvironment(environmentId); |
| 76 | await cleanUpRemovedMessages(removed); |
| 77 | } |