@@ -37,7 +37,9 @@ import { createComposerContextHistory, referencedComposerContext } from "../lib/
3737import { isQueuedEditDraftKey } from "./queued-edit-draft-key" ;
3838import {
3939 collectComposerContextReferences ,
40+ composerRecallContext ,
4041 formatComposerContextReference ,
42+ recallComposerText ,
4143 sanitizeComposerContextLabel ,
4244 replaceComposerContextReferences ,
4345} from "@t3tools/shared/composerContextReferences" ;
@@ -628,7 +630,18 @@ function restoreMissingComposerFileReferences(
628630 referenced . add ( key ) ;
629631 changed = true ;
630632 }
631- return changed ? { ...draft , text, context : { version : 1 , records } } : draft ;
633+ const composerRecall = draft . context ?. composerRecall ;
634+ return changed
635+ ? {
636+ ...draft ,
637+ text,
638+ context : {
639+ version : 1 ,
640+ records,
641+ ...( composerRecall === undefined ? { } : { composerRecall } ) ,
642+ } ,
643+ }
644+ : draft ;
632645}
633646
634647function normalizeDraft ( draft : ComposerDraft | undefined ) : ComposerDraft {
@@ -1287,12 +1300,7 @@ export async function restoreCloudComposerDrafts(accountId: string): Promise<voi
12871300 ? {
12881301 ...draft ,
12891302 ...existing ,
1290- text : mergeComposerDraftText ( existing . text , draft . text ) ,
1291- context : mergeReferencedComposerContext (
1292- mergeComposerDraftText ( existing . text , draft . text ) ,
1293- draft . context ,
1294- existing . context ,
1295- ) ,
1303+ ...mergeComposerDraftPrompt ( existing , draft , { preferExistingRecords : true } ) ,
12961304 // A concurrent import must not lose files, even above the send limit.
12971305 attachments : [
12981306 ...existing . attachments ,
@@ -1710,15 +1718,41 @@ function mergeComposerDraftText(existing: string, incoming: string): string {
17101718 return `${ existing } \n\n${ incoming } ` ;
17111719}
17121720
1713- function mergeReferencedComposerContext (
1714- text : string ,
1715- first ?: OrchestrationMessageContext ,
1716- second ?: OrchestrationMessageContext ,
1721+ function mergeComposerDraftPrompt (
1722+ existing : Pick < ComposerDraft , "text" | "context" > ,
1723+ incoming : Pick < ComposerDraftContent , "text" | "context" > ,
1724+ options ?: { readonly preferExistingRecords ?: boolean } ,
17171725) {
1726+ const text = mergeComposerDraftText ( existing . text , incoming . text ) ;
1727+ const [ first , second ] = options ?. preferExistingRecords
1728+ ? [ incoming . context , existing . context ]
1729+ : [ existing . context , incoming . context ] ;
17181730 const records = new Map ( ( first ?. records ?? [ ] ) . map ( ( record ) => [ record . contextId , record ] ) ) ;
17191731 for ( const record of second ?. records ?? [ ] ) records . set ( record . contextId , record ) ;
1720- if ( records . size === 0 ) return undefined ;
1721- return referencedComposerContext ( text , { version : 1 , records : [ ...records . values ( ) ] } ) ;
1732+ const context =
1733+ records . size === 0
1734+ ? undefined
1735+ : referencedComposerContext ( text , { version : 1 , records : [ ...records . values ( ) ] } ) ;
1736+ if (
1737+ existing . context ?. composerRecall === undefined &&
1738+ incoming . context ?. composerRecall === undefined
1739+ ) {
1740+ return { text, context } ;
1741+ }
1742+
1743+ const previous = recallComposerText ( existing . text , existing . context ) ;
1744+ const added = recallComposerText ( incoming . text , incoming . context ) ;
1745+ // Keep recall aligned with the text merge, including a retried restoration of the same text.
1746+ let recalled = previous ;
1747+ if (
1748+ text === incoming . text &&
1749+ ( text !== existing . text || existing . context ?. composerRecall === undefined )
1750+ ) {
1751+ recalled = added ;
1752+ } else if ( text !== existing . text ) {
1753+ recalled = [ previous , added ] . filter ( ( part ) => part . length > 0 ) . join ( "\n\n" ) ;
1754+ }
1755+ return { text, context : composerRecallContext ( text , recalled , context ) } ;
17221756}
17231757
17241758export function mergeComposerDraftContentState (
@@ -1742,8 +1776,7 @@ export function mergeComposerDraftContentState(
17421776 0 ,
17431777 PROVIDER_SEND_TURN_MAX_ATTACHMENTS ,
17441778 ) ;
1745- const text = mergeComposerDraftText ( existing . text , content . text ) ;
1746- const context = mergeReferencedComposerContext ( text , existing . context , content . context ) ;
1779+ const { text, context } = mergeComposerDraftPrompt ( existing , content ) ;
17471780 const importedShareIds = content . sourceShareId
17481781 ? [ ...( existing . importedShareIds ?? [ ] ) , content . sourceShareId ]
17491782 : existing . importedShareIds ;
0 commit comments