@@ -19,7 +19,7 @@ import {
1919 buildTelegramThreadParams ,
2020 getTelegramTextParts ,
2121 joinTelegramTextParts ,
22- resolveTelegramMessageThreadSpec ,
22+ type TelegramThreadSpec ,
2323} from "./bot/helpers.js" ;
2424import type { TelegramContext } from "./bot/types.js" ;
2525import type { TelegramMessageDispatchReplayClaim } from "./message-dispatch-dedupe.js" ;
@@ -35,7 +35,7 @@ export type TelegramDebounceEntry = {
3535 debounceKey : string | null ;
3636 debounceLane : TelegramDebounceLane ;
3737 botUsername ?: string ;
38- threadId ?: number ;
38+ threadSpec : TelegramThreadSpec ;
3939 promptContextMinTimestampMs ?: number ;
4040 promptContextAmbientWatermark ?: TelegramAmbientTranscriptWatermark ;
4141 dispatchDedupeClaims : TelegramMessageDispatchReplayClaim [ ] ;
@@ -47,6 +47,7 @@ type TextFragmentEntry = {
4747 key : string ;
4848 storeAllowFrom : string [ ] ;
4949 messages : Array < { msg : Message ; ctx : TelegramContext ; receivedAtMs : number } > ;
50+ threadSpec : TelegramThreadSpec ;
5051 promptContextMinTimestampMs ?: number ;
5152 promptContextAmbientWatermark ?: TelegramAmbientTranscriptWatermark ;
5253 dispatchDedupeClaims : TelegramMessageDispatchReplayClaim [ ] ;
@@ -59,8 +60,7 @@ type TelegramTextFragmentInput = {
5960 ctx : TelegramContext ;
6061 msg : Message ;
6162 chatId : number ;
62- resolvedThreadId ?: number ;
63- dmThreadId ?: number ;
63+ threadSpec : TelegramThreadSpec ;
6464 storeAllowFrom : string [ ] ;
6565 isAbortControlMessage : boolean ;
6666 isAuthorizedAbortControlMessage : ( ) => Promise < boolean > ;
@@ -164,6 +164,7 @@ export function createTelegramInboundBuffers({
164164 options : {
165165 receivedAtMs : last . receivedAtMs ,
166166 ingressBuffer : "inbound-debounce" ,
167+ threadSpec : last . threadSpec ,
167168 ...promptContextBoundaryOptions (
168169 last . promptContextMinTimestampMs ,
169170 last . promptContextAmbientWatermark ,
@@ -212,6 +213,7 @@ export function createTelegramInboundBuffers({
212213 ) ,
213214 receivedAtMs : first . receivedAtMs ,
214215 ingressBuffer : "inbound-debounce" ,
216+ threadSpec : first . threadSpec ,
215217 bufferedMessages : entries . map ( ( entry ) => entry . msg ) ,
216218 ...promptContextBoundaryOptions (
217219 latestPromptContextMinTimestampMs (
@@ -253,10 +255,7 @@ export function createTelegramInboundBuffers({
253255 }
254256 const chatId = items [ 0 ] ?. msg . chat . id ;
255257 if ( chatId != null ) {
256- const firstMessage = items [ 0 ] ?. msg ;
257- const threadParams = firstMessage
258- ? buildTelegramThreadParams ( resolveTelegramMessageThreadSpec ( firstMessage ) )
259- : undefined ;
258+ const threadParams = buildTelegramThreadParams ( items [ 0 ] ?. threadSpec ) ;
260259 void bot . api
261260 . sendMessage (
262261 chatId ,
@@ -326,6 +325,7 @@ export function createTelegramInboundBuffers({
326325 ambientTranscriptBody : formatTelegramAmbientTranscriptBody ( bufferedMessages ) ,
327326 receivedAtMs : first . receivedAtMs ,
328327 ingressBuffer : "text-fragment" ,
328+ threadSpec : entry . threadSpec ,
329329 bufferedMessages,
330330 ...promptContextBoundaryOptions (
331331 entry . promptContextMinTimestampMs ,
@@ -366,8 +366,7 @@ export function createTelegramInboundBuffers({
366366 ( entity ) => entity . type === "bot_command" && entity . offset === 0 ,
367367 ) ;
368368 const senderId = params . msg . from ?. id != null ? String ( params . msg . from . id ) : "unknown" ;
369- const threadId = params . resolvedThreadId ?? params . dmThreadId ;
370- const key = `text:${ params . chatId } :${ threadId ?? "main" } :${ senderId } ` ;
369+ const key = `text:${ params . chatId } :${ params . threadSpec . scope } :${ params . threadSpec . id ?? "main" } :${ senderId } ` ;
371370 if ( text && ! isCommand && ! params . isAbortControlMessage ) {
372371 const nowMs = Date . now ( ) ;
373372 const existing = textBuffer . get ( key ) ;
@@ -416,6 +415,7 @@ export function createTelegramInboundBuffers({
416415 const entry : TextFragmentEntry = {
417416 key,
418417 storeAllowFrom : params . storeAllowFrom ,
418+ threadSpec : params . threadSpec ,
419419 messages : [ { msg : params . msg , ctx : params . ctx , receivedAtMs : nowMs } ] ,
420420 dispatchDedupeClaims : params . dispatchDedupeClaims ,
421421 spooledReplayParticipants : participant ? [ participant ] : [ ] ,
0 commit comments