@@ -15,39 +15,25 @@ import {
1515} from "openclaw/plugin-sdk/command-auth-native" ;
1616import type { OpenClawConfig } from "openclaw/plugin-sdk/config-runtime" ;
1717import type { ChannelGroupPolicy } from "openclaw/plugin-sdk/config-runtime" ;
18- import { resolveMarkdownTableMode } from "openclaw/plugin-sdk/markdown-table-runtime" ;
19- import { getRuntimeConfigSnapshot } from "openclaw/plugin-sdk/runtime-config-snapshot" ;
20- import {
21- normalizeTelegramCommandName ,
22- resolveTelegramCustomCommands ,
23- TELEGRAM_COMMAND_NAME_PATTERN ,
24- } from "openclaw/plugin-sdk/telegram-command-config" ;
2518import type {
2619 ReplyToMode ,
2720 TelegramAccountConfig ,
2821 TelegramDirectConfig ,
2922 TelegramGroupConfig ,
3023 TelegramTopicConfig ,
3124} from "openclaw/plugin-sdk/config-runtime" ;
32- import {
33- ensureConfiguredBindingRouteReady ,
34- recordInboundSessionMetaSafe ,
35- } from "openclaw/plugin-sdk/conversation-runtime" ;
36- import { getAgentScopedMediaLocalRoots } from "openclaw/plugin-sdk/media-runtime" ;
37- import {
38- executePluginCommand ,
39- getPluginCommandSpecs ,
40- matchPluginCommand ,
41- } from "openclaw/plugin-sdk/plugin-runtime" ;
42- import {
43- finalizeInboundContext ,
44- resolveChunkMode ,
45- } from "openclaw/plugin-sdk/reply-dispatch-runtime" ;
25+ import { resolveMarkdownTableMode } from "openclaw/plugin-sdk/markdown-table-runtime" ;
26+ import { getPluginCommandSpecs } from "openclaw/plugin-sdk/plugin-runtime" ;
4627import { resolveAgentRoute } from "openclaw/plugin-sdk/routing" ;
47- import { resolveThreadSessionKeys } from "openclaw/plugin-sdk/routing " ;
28+ import { getRuntimeConfigSnapshot } from "openclaw/plugin-sdk/runtime-config-snapshot " ;
4829import { danger , logVerbose } from "openclaw/plugin-sdk/runtime-env" ;
4930import { getChildLogger } from "openclaw/plugin-sdk/runtime-env" ;
5031import type { RuntimeEnv } from "openclaw/plugin-sdk/runtime-env" ;
32+ import {
33+ normalizeTelegramCommandName ,
34+ resolveTelegramCustomCommands ,
35+ TELEGRAM_COMMAND_NAME_PATTERN ,
36+ } from "openclaw/plugin-sdk/telegram-command-config" ;
5137import { resolveTelegramAccount } from "./accounts.js" ;
5238import { withTelegramApiErrorLogging } from "./api-logging.js" ;
5339import { isSenderAllowed , normalizeDmAllowFromWithStore } from "./bot-access.js" ;
@@ -89,6 +75,9 @@ const EMPTY_RESPONSE_FALLBACK = "No response generated. Please try again.";
8975const TELEGRAM_NATIVE_COMMAND_CALLBACK_PREFIX = "tgcmd:" ;
9076
9177type TelegramNativeCommandContext = Context & { match ?: string } ;
78+ type TelegramChunkMode = ReturnType <
79+ typeof import ( "openclaw/plugin-sdk/reply-dispatch-runtime" ) . resolveChunkMode
80+ > ;
9281
9382type TelegramCommandAuthResult = {
9483 chatId : number ;
@@ -112,6 +101,15 @@ async function loadTelegramNativeCommandDeliveryRuntime() {
112101 return await telegramNativeCommandDeliveryRuntimePromise ;
113102}
114103
104+ let telegramNativeCommandRuntimePromise :
105+ | Promise < typeof import ( "./bot-native-commands.runtime.js" ) >
106+ | undefined ;
107+
108+ async function loadTelegramNativeCommandRuntime ( ) {
109+ telegramNativeCommandRuntimePromise ??= import ( "./bot-native-commands.runtime.js" ) ;
110+ return await telegramNativeCommandRuntimePromise ;
111+ }
112+
115113export type RegisterTelegramHandlerParams = {
116114 cfg : OpenClawConfig ;
117115 accountId : string ;
@@ -537,7 +535,7 @@ export const registerTelegramNativeCommands = ({
537535 route : ReturnType < typeof resolveTelegramConversationRoute > [ "route" ] ;
538536 mediaLocalRoots : readonly string [ ] | undefined ;
539537 tableMode : ReturnType < typeof resolveMarkdownTableMode > ;
540- chunkMode : ReturnType < typeof resolveChunkMode > ;
538+ chunkMode : TelegramChunkMode ;
541539 } | null > => {
542540 const { msg, runtimeCfg, isGroup, isForum, resolvedThreadId, senderId, topicAgentId } = params ;
543541 const chatId = msg . chat . id ;
@@ -557,8 +555,9 @@ export const registerTelegramNativeCommands = ({
557555 senderId,
558556 topicAgentId,
559557 } ) ;
558+ const nativeCommandRuntime = await loadTelegramNativeCommandRuntime ( ) ;
560559 if ( configuredBinding ) {
561- const ensured = await ensureConfiguredBindingRouteReady ( {
560+ const ensured = await nativeCommandRuntime . ensureConfiguredBindingRouteReady ( {
562561 cfg : runtimeCfg ,
563562 bindingResolution : configuredBinding ,
564563 } ) ;
@@ -579,13 +578,20 @@ export const registerTelegramNativeCommands = ({
579578 return null ;
580579 }
581580 }
582- const mediaLocalRoots = getAgentScopedMediaLocalRoots ( runtimeCfg , route . agentId ) ;
581+ const mediaLocalRoots = nativeCommandRuntime . getAgentScopedMediaLocalRoots (
582+ runtimeCfg ,
583+ route . agentId ,
584+ ) ;
583585 const tableMode = resolveMarkdownTableMode ( {
584586 cfg : runtimeCfg ,
585587 channel : "telegram" ,
586588 accountId : route . accountId ,
587589 } ) ;
588- const chunkMode = resolveChunkMode ( runtimeCfg , "telegram" , route . accountId ) ;
590+ const chunkMode = nativeCommandRuntime . resolveChunkMode (
591+ runtimeCfg ,
592+ "telegram" ,
593+ route . accountId ,
594+ ) ;
589595 return { chatId, threadSpec, route, mediaLocalRoots, tableMode, chunkMode } ;
590596 } ;
591597 const buildCommandDeliveryBaseOptions = ( params : {
@@ -597,7 +603,7 @@ export const registerTelegramNativeCommands = ({
597603 mediaLocalRoots ?: readonly string [ ] ;
598604 threadSpec : ReturnType < typeof resolveTelegramThreadSpec > ;
599605 tableMode : ReturnType < typeof resolveMarkdownTableMode > ;
600- chunkMode : ReturnType < typeof resolveChunkMode > ;
606+ chunkMode : TelegramChunkMode ;
601607 linkPreview ?: boolean ;
602608 } ) => ( {
603609 chatId : String ( params . chatId ) ,
@@ -736,9 +742,10 @@ export const registerTelegramNativeCommands = ({
736742 } ) ;
737743 // DMs: use raw messageThreadId for thread sessions (not resolvedThreadId which is for forums)
738744 const dmThreadId = threadSpec . scope === "dm" ? threadSpec . id : undefined ;
745+ const nativeCommandRuntime = await loadTelegramNativeCommandRuntime ( ) ;
739746 const threadKeys =
740747 dmThreadId != null
741- ? resolveThreadSessionKeys ( {
748+ ? nativeCommandRuntime . resolveThreadSessionKeys ( {
742749 baseSessionKey,
743750 threadId : `${ chatId } :${ dmThreadId } ` ,
744751 } )
@@ -772,7 +779,7 @@ export const registerTelegramNativeCommands = ({
772779 ? `${ msg . chat . title } id:${ chatId } `
773780 : `group:${ chatId } `
774781 : ( buildSenderName ( msg ) ?? String ( senderId || chatId ) ) ;
775- const ctxPayload = finalizeInboundContext ( {
782+ const ctxPayload = nativeCommandRuntime . finalizeInboundContext ( {
776783 Body : prompt ,
777784 BodyForAgent : prompt ,
778785 RawBody : prompt ,
@@ -804,7 +811,7 @@ export const registerTelegramNativeCommands = ({
804811 OriginatingTo : originatingTo ,
805812 } ) ;
806813
807- await recordInboundSessionMetaSafe ( {
814+ await nativeCommandRuntime . recordInboundSessionMetaSafe ( {
808815 cfg : executionCfg ,
809816 agentId : route . agentId ,
810817 sessionKey : ctxPayload . SessionKey ?? route . sessionKey ,
@@ -894,7 +901,8 @@ export const registerTelegramNativeCommands = ({
894901 const runtimeTelegramCfg = resolveFreshTelegramConfig ( runtimeCfg ) ;
895902 const rawText = ctx . match ?. trim ( ) ?? "" ;
896903 const commandBody = `/${ pluginCommand . command } ${ rawText ? ` ${ rawText } ` : "" } ` ;
897- const match = matchPluginCommand ( commandBody ) ;
904+ const nativeCommandRuntime = await loadTelegramNativeCommandRuntime ( ) ;
905+ const match = nativeCommandRuntime . matchPluginCommand ( commandBody ) ;
898906 if ( ! match ) {
899907 await withTelegramApiErrorLogging ( {
900908 operation : "sendMessage" ,
@@ -950,7 +958,7 @@ export const registerTelegramNativeCommands = ({
950958 const to = `telegram:${ chatId } ` ;
951959 const { deliverReplies } = await loadTelegramNativeCommandDeliveryRuntime ( ) ;
952960
953- const result = await executePluginCommand ( {
961+ const result = await nativeCommandRuntime . executePluginCommand ( {
954962 command : match . command ,
955963 args : match . args ,
956964 senderId,
0 commit comments