Skip to content

Commit 5b95283

Browse files
committed
perf(memory): trim telegram command runtime imports
1 parent 1a037ff commit 5b95283

3 files changed

Lines changed: 61 additions & 34 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export {
2+
ensureConfiguredBindingRouteReady,
3+
recordInboundSessionMetaSafe,
4+
} from "openclaw/plugin-sdk/conversation-runtime";
5+
export { getAgentScopedMediaLocalRoots } from "openclaw/plugin-sdk/media-runtime";
6+
export { executePluginCommand, matchPluginCommand } from "openclaw/plugin-sdk/plugin-runtime";
7+
export {
8+
finalizeInboundContext,
9+
resolveChunkMode,
10+
} from "openclaw/plugin-sdk/reply-dispatch-runtime";
11+
export { resolveThreadSessionKeys } from "openclaw/plugin-sdk/routing";

extensions/telegram/src/bot-native-commands.ts

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,25 @@ import {
1515
} from "openclaw/plugin-sdk/command-auth-native";
1616
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-runtime";
1717
import 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";
2518
import 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";
4627
import { resolveAgentRoute } from "openclaw/plugin-sdk/routing";
47-
import { resolveThreadSessionKeys } from "openclaw/plugin-sdk/routing";
28+
import { getRuntimeConfigSnapshot } from "openclaw/plugin-sdk/runtime-config-snapshot";
4829
import { danger, logVerbose } from "openclaw/plugin-sdk/runtime-env";
4930
import { getChildLogger } from "openclaw/plugin-sdk/runtime-env";
5031
import 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";
5137
import { resolveTelegramAccount } from "./accounts.js";
5238
import { withTelegramApiErrorLogging } from "./api-logging.js";
5339
import { isSenderAllowed, normalizeDmAllowFromWithStore } from "./bot-access.js";
@@ -89,6 +75,9 @@ const EMPTY_RESPONSE_FALLBACK = "No response generated. Please try again.";
8975
const TELEGRAM_NATIVE_COMMAND_CALLBACK_PREFIX = "tgcmd:";
9076

9177
type TelegramNativeCommandContext = Context & { match?: string };
78+
type TelegramChunkMode = ReturnType<
79+
typeof import("openclaw/plugin-sdk/reply-dispatch-runtime").resolveChunkMode
80+
>;
9281

9382
type 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+
115113
export 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,

extensions/telegram/src/monitor.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ const isGrammyHttpError = (err: unknown): boolean => {
7171
return (err as { name?: string }).name === "HttpError";
7272
};
7373

74+
type TelegramMonitorPollingRuntime = typeof import("./monitor-polling.runtime.js");
75+
type TelegramPollingSessionInstance = InstanceType<
76+
TelegramMonitorPollingRuntime["TelegramPollingSession"]
77+
>;
78+
type TelegramExecApprovalHandlerInstance = InstanceType<
79+
TelegramMonitorPollingRuntime["TelegramExecApprovalHandler"]
80+
>;
81+
7482
let telegramMonitorPollingRuntimePromise:
7583
| Promise<typeof import("./monitor-polling.runtime.js")>
7684
| undefined;
@@ -91,8 +99,8 @@ async function loadTelegramMonitorWebhookRuntime() {
9199

92100
export async function monitorTelegramProvider(opts: MonitorTelegramOpts = {}) {
93101
const log = opts.runtime?.error ?? console.error;
94-
let pollingSession: TelegramPollingSession | undefined;
95-
let execApprovalsHandler: TelegramExecApprovalHandler | undefined;
102+
let pollingSession: TelegramPollingSessionInstance | undefined;
103+
let execApprovalsHandler: TelegramExecApprovalHandlerInstance | undefined;
96104

97105
const unregisterHandler = registerUnhandledRejectionHandler((err) => {
98106
const isNetworkError = isRecoverableTelegramNetworkError(err, { context: "polling" });

0 commit comments

Comments
 (0)