Open History to see the last commit
1import type { QueuedThreadMessage } from "./thread-outbox-model";
2import { scopedThreadKey } from "../lib/scopedEntities";
3import { restoredNewTaskDraftKey } from "./new-task-draft-key";
4import {
5 appendComposerDraftAttachments,
6 clearComposerDraftContent,
7 flushComposerDrafts,
8 getComposerDraftSnapshot,
9 mergeComposerDraftContent,
10} from "./use-composer-drafts";
11
12export async function recoverFailedThreadDraft(message: QueuedThreadMessage): Promise<void> {
13 const sourceKey = scopedThreadKey(message.environmentId, message.threadId);
14 const targetKey = restoredNewTaskDraftKey(message.messageId);
15 const source = getComposerDraftSnapshot(sourceKey);
16 if (source.text.length === 0 && source.attachments.length === 0) return;
17
18 await mergeComposerDraftContent(targetKey, {
19 text: source.text,
20 context: source.context,
21 attachments: [],
22 });
23 const existingIds = new Set(
24 getComposerDraftSnapshot(targetKey).attachments.map((attachment) => attachment.id),
25 );
26 appendComposerDraftAttachments(
27 targetKey,
28 source.attachments.filter((attachment) => !existingIds.has(attachment.id)),
29 { allowOverflow: true },
30 );
31 // Recovery may exceed the send cap. Preserve every file and let the editor
32 // ask the user to remove extras; never discard them during a failed send.
33 await flushComposerDrafts();
34 clearComposerDraftContent(sourceKey);
35}