@@ -2,10 +2,7 @@ import { createHash } from "node:crypto";
22import fs from "node:fs" ;
33import type { Message } from "grammy/types" ;
44import { formatLocationText } from "openclaw/plugin-sdk/channel-inbound" ;
5- import {
6- parseStrictNonNegativeInteger ,
7- parseStrictPositiveInteger ,
8- } from "openclaw/plugin-sdk/number-runtime" ;
5+ import { parseStrictPositiveInteger } from "openclaw/plugin-sdk/number-runtime" ;
96import type { MsgContext } from "openclaw/plugin-sdk/reply-runtime" ;
107import { logVerbose } from "openclaw/plugin-sdk/runtime-env" ;
118import { appendRegularFileSync , replaceFileAtomicSync } from "openclaw/plugin-sdk/security-runtime" ;
@@ -17,6 +14,7 @@ import {
1714 getTelegramTextParts ,
1815 normalizeForwardedContext ,
1916} from "./bot/helpers.js" ;
17+ import { parseTelegramMessageThreadId } from "./outbound-params.js" ;
2018import { getOptionalTelegramRuntime } from "./runtime.js" ;
2119
2220export type TelegramReplyChainEntry = NonNullable < MsgContext [ "ReplyChain" ] > [ number ] ;
@@ -166,6 +164,7 @@ function normalizeMessageNode(
166164 const forwardedFrom = normalizeForwardedContext ( msg ) ;
167165 const replyMessage = resolveReplyMessage ( msg ) ;
168166 const body = resolveMessageBody ( msg ) ;
167+ const threadId = normalizeTelegramCacheThreadId ( params . threadId ) ;
169168 return {
170169 sourceMessage : msg ,
171170 messageId : String ( msg . message_id ) ,
@@ -181,7 +180,7 @@ function normalizeMessageNode(
181180 ...( forwardedFrom ?. fromId ? { forwardedFromId : forwardedFrom . fromId } : { } ) ,
182181 ...( forwardedFrom ?. fromUsername ? { forwardedFromUsername : forwardedFrom . fromUsername } : { } ) ,
183182 ...( forwardedFrom ?. date ? { forwardedDate : forwardedFrom . date * 1000 } : { } ) ,
184- ...( params . threadId != null ? { threadId : String ( params . threadId ) } : { } ) ,
183+ ...( threadId !== undefined ? { threadId : String ( threadId ) } : { } ) ,
185184 } ;
186185}
187186
@@ -198,9 +197,7 @@ function normalizeRequiredMessageNode(
198197
199198function resolveMessageThreadId ( msg : Message ) : number | undefined {
200199 const threadId = ( msg as { message_thread_id ?: unknown } ) . message_thread_id ;
201- return typeof threadId === "number" && Number . isFinite ( threadId )
202- ? Math . trunc ( threadId )
203- : undefined ;
200+ return normalizeTelegramCacheThreadId ( threadId ) ;
204201}
205202
206203function normalizeMessageNodes (
@@ -246,7 +243,11 @@ function parseSafeMessageId(value: string | undefined): number | undefined {
246243}
247244
248245function parseCachedThreadId ( value : unknown ) : number | undefined {
249- return parseStrictNonNegativeInteger ( value ) ;
246+ return normalizeTelegramCacheThreadId ( value ) ;
247+ }
248+
249+ function normalizeTelegramCacheThreadId ( value : unknown ) : number | undefined {
250+ return parseTelegramMessageThreadId ( value ) ;
250251}
251252
252253function isTelegramSourceMessage ( value : unknown ) : value is Message {
@@ -754,7 +755,11 @@ export function createTelegramMessageCache(params?: {
754755 } ) => {
755756 await hydrateMessageCacheBucket ( bucket , maxMessages , scopeKey ) ;
756757 const prefix = telegramMessageCacheKeyPrefix ( { scopeKey, ...params } ) ;
757- const threadId = params . threadId != null ? String ( params . threadId ) : undefined ;
758+ const normalizedThreadId = normalizeTelegramCacheThreadId ( params . threadId ) ;
759+ if ( params . threadId != null && normalizedThreadId === undefined ) {
760+ return [ ] ;
761+ }
762+ const threadId = normalizedThreadId !== undefined ? String ( normalizedThreadId ) : undefined ;
758763 return Array . from ( messages , ( [ key , node ] ) => ( { key, node } ) )
759764 . filter ( ( { key, node } ) => {
760765 if ( ! key . startsWith ( prefix ) ) {
0 commit comments