@@ -20,11 +20,7 @@ import type {
2020} from "openclaw/plugin-sdk/config-contracts" ;
2121import { resolveChannelContextVisibilityMode } from "openclaw/plugin-sdk/context-visibility-runtime" ;
2222import { timestampMsToIsoString } from "openclaw/plugin-sdk/number-runtime" ;
23- import {
24- buildHistoryContextFromEntries ,
25- createChannelHistoryWindow ,
26- type HistoryEntry ,
27- } from "openclaw/plugin-sdk/reply-history" ;
23+ import { createChannelHistoryWindow , type HistoryEntry } from "openclaw/plugin-sdk/reply-history" ;
2824import type { ResolvedAgentRoute } from "openclaw/plugin-sdk/routing" ;
2925import { logVerbose , shouldLogVerbose } from "openclaw/plugin-sdk/runtime-env" ;
3026import { evaluateSupplementalContextVisibility } from "openclaw/plugin-sdk/security-runtime" ;
@@ -56,6 +52,8 @@ import {
5652import type { TelegramContext } from "./bot/types.js" ;
5753import { resolveTelegramGroupPromptSettings } from "./group-config-helpers.js" ;
5854import {
55+ isTelegramChatWindowPromptContext ,
56+ mergeTelegramGroupHistoryPromptContext ,
5957 recordTelegramGroupHistoryEntry ,
6058 selectTelegramGroupHistoryAfterLastSelf ,
6159} from "./group-history-window.js" ;
@@ -116,10 +114,6 @@ export async function resolveTelegramMessageContextStorePath(params: {
116114 } ) ;
117115}
118116
119- function isTelegramChatWindowPromptContext ( entry : TelegramPromptContextEntry ) : boolean {
120- return entry . source === "telegram" && entry . type === "chat_window" ;
121- }
122-
123117function replyTargetToChainEntry ( replyTarget : TelegramReplyTarget ) : TelegramReplyChainEntry {
124118 return {
125119 ...( replyTarget . id ? { messageId : replyTarget . id } : { } ) ,
@@ -173,23 +167,6 @@ function formatReplyChainEntry(entry: TelegramReplyChainEntry, index: number): s
173167 return `[${ labels . join ( " " ) } ]\n${ bodyLines . join ( "\n" ) } ` ;
174168}
175169
176- function formatTelegramGroupHistoryEntry ( params : {
177- entry : HistoryEntry ;
178- groupLabel : string ;
179- chatId : number | string ;
180- envelopeOptions : ReturnType < typeof resolveEnvelopeFormatOptions > ;
181- } ) : string {
182- return formatInboundEnvelope ( {
183- channel : "Telegram" ,
184- from : params . groupLabel ,
185- timestamp : params . entry . timestamp ,
186- body : `${ params . entry . body } [id:${ params . entry . messageId ?? "unknown" } chat:${ params . chatId } ]` ,
187- chatType : "group" ,
188- senderLabel : params . entry . sender ,
189- envelope : params . envelopeOptions ,
190- } ) ;
191- }
192-
193170export async function buildTelegramInboundContextPayload ( params : {
194171 cfg : OpenClawConfig ;
195172 primaryCtx : TelegramContext ;
@@ -408,7 +385,7 @@ export async function buildTelegramInboundContextPayload(params: {
408385 ! visibleReplyTarget ;
409386 // Existing plain DMs already carry their history through the persistent
410387 // transcript. Keep chat windows for fresh DMs, topics, replies, and groups.
411- const visiblePromptContext = shouldSuppressPersistedDmChatWindowContext
388+ const baseVisiblePromptContext = shouldSuppressPersistedDmChatWindowContext
412389 ? promptContext . filter ( ( entry ) => ! isTelegramChatWindowPromptContext ( entry ) )
413390 : promptContext ;
414391 const body = formatInboundEnvelope ( {
@@ -449,29 +426,19 @@ export async function buildTelegramInboundContextPayload(params: {
449426 hasAbortRequest,
450427 commandSource,
451428 } ) ;
452- let combinedBody = body ;
453429 let watermarkedGroupHistoryEntries : HistoryEntry [ ] | undefined ;
430+ let groupHistoryPromptEntries : HistoryEntry [ ] = [ ] ;
454431 if ( hasGroupHistoryContext && historyKey && historyLimit > 0 ) {
455432 const fullGroupHistoryEntries = ( groupHistories . get ( historyKey ) ?? [ ] ) . slice ( - historyLimit ) ;
456433 watermarkedGroupHistoryEntries =
457434 selectTelegramGroupHistoryAfterLastSelf ( fullGroupHistoryEntries ) . slice ( - historyLimit ) ;
458- // User-request turns use a non-destructive self-entry watermark. Room events
459- // are not session-persisted, so clearing this window loses ambient context.
460- const bodyHistoryEntries =
435+ groupHistoryPromptEntries =
461436 inboundEventKind === "room_event" ? fullGroupHistoryEntries : watermarkedGroupHistoryEntries ;
462- combinedBody = buildHistoryContextFromEntries ( {
463- entries : bodyHistoryEntries ,
464- currentMessage : combinedBody ,
465- formatEntry : ( entry ) =>
466- formatTelegramGroupHistoryEntry ( {
467- entry,
468- groupLabel : groupLabel ?? `group:${ chatId } ` ,
469- chatId,
470- envelopeOptions,
471- } ) ,
472- excludeLast : false ,
473- } ) ;
474437 }
438+ const visiblePromptContext = mergeTelegramGroupHistoryPromptContext ( {
439+ promptContext : baseVisiblePromptContext ,
440+ entries : groupHistoryPromptEntries ,
441+ } ) ;
475442
476443 const { skillFilter, groupSystemPrompt } = resolveTelegramGroupPromptSettings ( {
477444 groupConfig,
@@ -543,7 +510,7 @@ export async function buildTelegramInboundContextPayload(params: {
543510 } ,
544511 message : {
545512 inboundEventKind,
546- body : combinedBody ,
513+ body,
547514 rawBody,
548515 bodyForAgent : bodyText ,
549516 commandBody,
0 commit comments