@@ -117,7 +117,10 @@ import {
117117 type LaneName ,
118118} from "./lane-delivery.js" ;
119119import { TELEGRAM_TEXT_CHUNK_LIMIT } from "./outbound-adapter.js" ;
120- import { recordOutboundMessageForPromptContext } from "./outbound-message-context.js" ;
120+ import {
121+ recordOutboundMessageForPromptContext ,
122+ withTelegramPromptContextTimestampMs ,
123+ } from "./outbound-message-context.js" ;
121124import {
122125 createTelegramReasoningStepState ,
123126 splitTelegramReasoningText ,
@@ -244,6 +247,7 @@ export type TelegramDispatchResult =
244247type TelegramReasoningLevel = "off" | "on" | "stream" ;
245248
246249type TelegramTranscriptMirrorPayload = { text ?: string ; mediaUrls ?: string [ ] } ;
250+ type CurrentTurnTranscriptFinal = { text : string ; timestamp : number } ;
247251type TelegramScopedTranscriptSession = { sessionId : string ; storePath : string } ;
248252type FreshTelegramSessionEntryLoader = ( (
249253 agentId : string ,
@@ -1459,10 +1463,16 @@ export const dispatchTelegramMessage = async ({
14591463 const sessionKey = ctxPayload . SessionKey ;
14601464 let transcriptMirrorSequence = 0 ;
14611465 const transcriptMirrorTurnId = `${ chatId } :${ ctxPayload . MessageSid ?? msg . message_id ?? dispatchStartedAt } ` ;
1462- const resolveCurrentTurnTranscriptFinalText = async ( ) : Promise < string | undefined > => {
1466+ let currentTurnTranscriptFinal : CurrentTurnTranscriptFinal | undefined ;
1467+ const resolveCurrentTurnTranscriptFinal = async ( ) : Promise <
1468+ CurrentTurnTranscriptFinal | undefined
1469+ > => {
14631470 if ( ! sessionKey ) {
14641471 return undefined ;
14651472 }
1473+ if ( currentTurnTranscriptFinal ) {
1474+ return currentTurnTranscriptFinal ;
1475+ }
14661476 try {
14671477 const { entry : sessionEntry , storePath } = loadFreshSessionEntry ( route . agentId , sessionKey ) ;
14681478 if ( ! sessionEntry ?. sessionId ) {
@@ -1477,12 +1487,25 @@ export const dispatchTelegramMessage = async ({
14771487 if ( ! latest ?. timestamp || latest . timestamp < dispatchStartedAt ) {
14781488 return undefined ;
14791489 }
1480- return latest . text ;
1490+ currentTurnTranscriptFinal = {
1491+ text : latest . text ,
1492+ timestamp : latest . timestamp ,
1493+ } ;
1494+ return currentTurnTranscriptFinal ;
14811495 } catch ( err ) {
14821496 logVerbose ( `telegram transcript final candidate lookup failed: ${ formatErrorMessage ( err ) } ` ) ;
14831497 return undefined ;
14841498 }
14851499 } ;
1500+ const resolveCurrentTurnTranscriptFinalText = async ( ) : Promise < string | undefined > =>
1501+ ( await resolveCurrentTurnTranscriptFinal ( ) ) ?. text ;
1502+ const resolvePromptContextTimestampMs = async ( text : string ) : Promise < number | undefined > => {
1503+ const final = await resolveCurrentTurnTranscriptFinal ( ) ;
1504+ if ( final ?. text . trim ( ) !== text . trim ( ) ) {
1505+ return undefined ;
1506+ }
1507+ return final . timestamp ;
1508+ } ;
14861509 const deliveryBaseOptions = {
14871510 chatId : String ( chatId ) ,
14881511 accountId : route . accountId ,
@@ -1631,6 +1654,14 @@ export const dispatchTelegramMessage = async ({
16311654 return false ;
16321655 }
16331656 const deliverablePayload = applyQuoteReplyTarget ( payload ) ;
1657+ const promptContextTimestampMs =
1658+ options ?. durable && deliverablePayload . text
1659+ ? await resolvePromptContextTimestampMs ( deliverablePayload . text )
1660+ : undefined ;
1661+ const effectivePayload = withTelegramPromptContextTimestampMs (
1662+ deliverablePayload ,
1663+ promptContextTimestampMs ,
1664+ ) ;
16341665 const silent = options ?. silent ?? ( silentErrorReplies && payload . isError === true ) ;
16351666 const durableDelivery = telegramDeps . deliverInboundReplyWithMessageSendContext ;
16361667 if ( options ?. durable && durableDelivery ) {
@@ -1641,7 +1672,7 @@ export const dispatchTelegramMessage = async ({
16411672 accountId : route . accountId ,
16421673 agentId : route . agentId ,
16431674 ctxPayload,
1644- payload : deliverablePayload ,
1675+ payload : effectivePayload ,
16451676 info : { kind : "final" } ,
16461677 replyToMode,
16471678 threadId : threadSpec . id ,
@@ -1652,13 +1683,13 @@ export const dispatchTelegramMessage = async ({
16521683 } ,
16531684 silent,
16541685 requiredCapabilities : deriveDurableFinalDeliveryRequirements ( {
1655- payload : deliverablePayload ,
1656- replyToId : deliverablePayload . replyToId ,
1686+ payload : effectivePayload ,
1687+ replyToId : effectivePayload . replyToId ,
16571688 threadId : threadSpec . id ,
16581689 silent,
16591690 payloadTransport : true ,
16601691 extraCapabilities : {
1661- nativeQuote : usesNativeTelegramQuote ( deliverablePayload ) ,
1692+ nativeQuote : usesNativeTelegramQuote ( effectivePayload ) ,
16621693 } ,
16631694 } ) ,
16641695 } ) ;
@@ -1676,7 +1707,7 @@ export const dispatchTelegramMessage = async ({
16761707 const result = await ( telegramDeps . deliverReplies ?? deliverReplies ) ( {
16771708 ...deliveryBaseOptions ,
16781709 transcriptMirror : options ?. durable ? deliveryBaseOptions . transcriptMirror : undefined ,
1679- replies : [ deliverablePayload ] ,
1710+ replies : [ effectivePayload ] ,
16801711 onVoiceRecording : sendRecordVoice ,
16811712 silent,
16821713 mediaLoader : telegramDeps . loadWebMedia ,
@@ -1701,6 +1732,10 @@ export const dispatchTelegramMessage = async ({
17011732 groupId : deliveryBaseOptions . mirrorGroupId ,
17021733 } ) ;
17031734 try {
1735+ const promptContextContent =
1736+ result . delivery . promptContextContent ?? result . delivery . content ;
1737+ const promptContextTimestampMs =
1738+ await resolvePromptContextTimestampMs ( promptContextContent ) ;
17041739 await (
17051740 telegramDeps . recordOutboundMessageForPromptContext ??
17061741 recordOutboundMessageForPromptContext
@@ -1710,7 +1745,8 @@ export const dispatchTelegramMessage = async ({
17101745 chatId : deliveryBaseOptions . chatId ,
17111746 message : { message_id : result . delivery . messageId } ,
17121747 messageId : result . delivery . messageId ,
1713- text : result . delivery . promptContextContent ?? result . delivery . content ,
1748+ text : promptContextContent ,
1749+ ...( promptContextTimestampMs !== undefined ? { promptContextTimestampMs } : { } ) ,
17141750 ...( threadSpec . id !== undefined ? { messageThreadId : threadSpec . id } : { } ) ,
17151751 } ) ;
17161752 } catch ( error ) {
@@ -1868,11 +1904,13 @@ export const dispatchTelegramMessage = async ({
18681904 markProgressFinalDelivered ( ) ;
18691905 return { kind : "sent" } ;
18701906 } ;
1871- const resolveTranscriptBackedFinalText = async ( text : string ) : Promise < string > =>
1872- await resolveTranscriptBackedChannelFinalText ( {
1907+ const resolveTranscriptBackedFinalText = async ( text : string ) : Promise < string > => {
1908+ const candidate = await resolveCurrentTurnTranscriptFinal ( ) ;
1909+ return await resolveTranscriptBackedChannelFinalText ( {
18731910 finalText : text ,
1874- resolveCandidateText : resolveCurrentTurnTranscriptFinalText ,
1911+ resolveCandidateText : async ( ) => candidate ?. text ,
18751912 } ) ;
1913+ } ;
18761914
18771915 if ( isDmTopic ) {
18781916 try {
0 commit comments