@@ -175,6 +175,7 @@ export async function buildTelegramInboundContextPayload(params: {
175175 topicConfig ?: TelegramTopicConfig ;
176176 stickerCacheHit : boolean ;
177177 effectiveWasMentioned : boolean ;
178+ hasControlCommand : boolean ;
178179 audioTranscribedMediaIndex ?: number ;
179180 commandAuthorized : boolean ;
180181 locationData ?: NormalizedLocation ;
@@ -223,6 +224,7 @@ export async function buildTelegramInboundContextPayload(params: {
223224 topicConfig,
224225 stickerCacheHit,
225226 effectiveWasMentioned,
227+ hasControlCommand,
226228 audioTranscribedMediaIndex,
227229 commandAuthorized,
228230 locationData,
@@ -368,9 +370,9 @@ export async function buildTelegramInboundContextPayload(params: {
368370 previousTimestamp,
369371 envelope : envelopeOptions ,
370372 } ) ;
373+ const channelHistory = createChannelHistoryWindow ( { historyMap : groupHistories } ) ;
371374 let combinedBody = body ;
372375 if ( isGroup && historyKey && historyLimit > 0 ) {
373- const channelHistory = createChannelHistoryWindow ( { historyMap : groupHistories } ) ;
374376 combinedBody = channelHistory . buildPendingContext ( {
375377 historyKey,
376378 limit : historyLimit ,
@@ -397,7 +399,7 @@ export async function buildTelegramInboundContextPayload(params: {
397399 } ) ;
398400 const inboundHistory =
399401 isGroup && historyKey && historyLimit > 0
400- ? createChannelHistoryWindow ( { historyMap : groupHistories } ) . buildInboundHistory ( {
402+ ? channelHistory . buildInboundHistory ( {
401403 historyKey,
402404 limit : historyLimit ,
403405 } )
@@ -411,6 +413,10 @@ export async function buildTelegramInboundContextPayload(params: {
411413 const telegramTo = `telegram:${ chatId } ` ;
412414 const locationContext = locationData ? toLocationContext ( locationData ) : undefined ;
413415 const commandSource = options ?. commandSource ;
416+ const inboundTurnKind =
417+ isGroup && ! effectiveWasMentioned && ! hasControlCommand && commandSource !== "native"
418+ ? "room_event"
419+ : "user_request" ;
414420 const ctxPayload = sessionRuntime . buildChannelTurnContext ( {
415421 channel : "telegram" ,
416422 accountId : route . accountId ,
@@ -447,6 +453,7 @@ export async function buildTelegramInboundContextPayload(params: {
447453 messageThreadId : threadSpec . id ,
448454 } ,
449455 message : {
456+ inboundTurnKind,
450457 body : combinedBody ,
451458 rawBody,
452459 bodyForAgent : bodyText ,
@@ -538,6 +545,18 @@ export async function buildTelegramInboundContextPayload(params: {
538545 TopicName : isForum && topicName ? topicName : undefined ,
539546 } ,
540547 } satisfies BuildChannelTurnContextParams ) ;
548+ if ( inboundTurnKind === "room_event" && historyKey ) {
549+ channelHistory . record ( {
550+ historyKey,
551+ limit : historyLimit ,
552+ entry : {
553+ sender : buildSenderLabel ( msg , senderId || chatId ) ,
554+ body : rawBody ,
555+ timestamp : msg . date ? msg . date * 1000 : undefined ,
556+ messageId : typeof msg . message_id === "number" ? String ( msg . message_id ) : undefined ,
557+ } ,
558+ } ) ;
559+ }
541560
542561 const pinnedMainDmOwner = ! isGroup
543562 ? sessionRuntime . resolvePinnedMainDmOwnerFromAllowlist ( {
0 commit comments