The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { PROVIDER_SEND_TURN_MAX_ATTACHMENTS } from "@t3tools/contracts"; |
| 2 | import { msg } from "gt-react-native"; |
| 3 | import { m } from "../i18n/messages"; |
| 4 | import { scopedThreadKey } from "../lib/scopedEntities"; |
| 5 | import { appAtomRegistry } from "./atom-registry"; |
| 6 | import { |
| 7 | confirmThreadOutboxMessageQueued, |
| 8 | threadOutboxRevision, |
| 9 | type QueuedThreadMessage, |
| 10 | } from "./thread-outbox"; |
| 11 | import { removeThreadOutboxMessage } from "./thread-outbox-removal"; |
| 12 | import { |
| 13 | flushComposerDrafts, |
| 14 | getComposerDraftSnapshot, |
| 15 | mergeComposerDraftContent, |
| 16 | undoComposerDraftMerge, |
| 17 | updateComposerDraftSettings, |
| 18 | waitForComposerDraftsLoaded, |
| 19 | } from "./use-composer-drafts"; |
| 20 | import { |
| 21 | dispatchingQueuedMessageIdsAtom, |
| 22 | editingQueuedMessageIdsAtom, |
| 23 | holdEditingQueuedMessage, |
| 24 | releaseEditingQueuedMessage, |
| 25 | } from "./use-thread-outbox"; |
| 26 | |
| 27 | /** Take delivery ownership before any await; the durable draft then takes ownership of the files. */ |
| 28 | export async function editPendingThreadMessage(message: QueuedThreadMessage): Promise<boolean> { |
| 29 | if ( |
| 30 | message.creation || |
| 31 | appAtomRegistry.get(dispatchingQueuedMessageIdsAtom)[message.messageId] || |
| 32 | appAtomRegistry.get(editingQueuedMessageIdsAtom)[message.messageId] |
| 33 | ) { |
| 34 | return false; |
| 35 | } |
| 36 | holdEditingQueuedMessage(message.messageId); |
| 37 | const draftKey = scopedThreadKey(message.environmentId, message.threadId); |
| 38 | let rollback: { |
| 39 | snapshot: ReturnType<typeof getComposerDraftSnapshot>; |
| 40 | merged: ReturnType<typeof getComposerDraftSnapshot>; |
| 41 | } | null = null; |
| 42 | try { |
| 43 | if (!(await confirmThreadOutboxMessageQueued(message))) return false; |
| 44 | const revision = threadOutboxRevision(message.messageId); |
| 45 | await waitForComposerDraftsLoaded(); |
| 46 | const snapshot = getComposerDraftSnapshot(draftKey); |
| 47 | const attachmentIds = new Set(snapshot.attachments.map((attachment) => attachment.id)); |
| 48 | for (const attachment of message.attachments) attachmentIds.add(attachment.id); |
| 49 | if (attachmentIds.size > PROVIDER_SEND_TURN_MAX_ATTACHMENTS) { |
| 50 | throw new Error(m(msg("Remove attachments from the composer before editing this message."))); |
| 51 | } |
| 52 | try { |
| 53 | await mergeComposerDraftContent(draftKey, message); |
| 54 | } finally { |
| 55 | rollback = { snapshot, merged: getComposerDraftSnapshot(draftKey) }; |
| 56 | } |
| 57 | updateComposerDraftSettings(draftKey, { |
| 58 | ...(message.modelSelection ? { modelSelection: message.modelSelection } : {}), |
| 59 | ...(message.runtimeMode ? { runtimeMode: message.runtimeMode } : {}), |
| 60 | ...(message.interactionMode ? { interactionMode: message.interactionMode } : {}), |
| 61 | }); |
| 62 | rollback = { snapshot, merged: getComposerDraftSnapshot(draftKey) }; |
| 63 | await flushComposerDrafts(); |
| 64 | if (!(await removeThreadOutboxMessage(message, revision))) return false; |
| 65 | rollback = null; |
| 66 | return true; |
| 67 | } finally { |
| 68 | try { |
| 69 | if (rollback) await undoComposerDraftMerge(draftKey, rollback.snapshot, rollback.merged); |
| 70 | } finally { |
| 71 | releaseEditingQueuedMessage(message.messageId); |
| 72 | } |
| 73 | } |
| 74 | } |