perf(memory): trim telegram command runtime imports

This commit is contained in:
Vincent Koc
2026-04-02 14:10:57 +09:00
parent 1a037ff6cd
commit 5b952836e3
3 changed files with 61 additions and 34 deletions

View File

@@ -0,0 +1,11 @@
export {
ensureConfiguredBindingRouteReady,
recordInboundSessionMetaSafe,
} from "openclaw/plugin-sdk/conversation-runtime";
export { getAgentScopedMediaLocalRoots } from "openclaw/plugin-sdk/media-runtime";
export { executePluginCommand, matchPluginCommand } from "openclaw/plugin-sdk/plugin-runtime";
export {
finalizeInboundContext,
resolveChunkMode,
} from "openclaw/plugin-sdk/reply-dispatch-runtime";
export { resolveThreadSessionKeys } from "openclaw/plugin-sdk/routing";

View File

@@ -15,13 +15,6 @@ import {
} from "openclaw/plugin-sdk/command-auth-native";
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-runtime";
import type { ChannelGroupPolicy } from "openclaw/plugin-sdk/config-runtime";
import { resolveMarkdownTableMode } from "openclaw/plugin-sdk/markdown-table-runtime";
import { getRuntimeConfigSnapshot } from "openclaw/plugin-sdk/runtime-config-snapshot";
import {
normalizeTelegramCommandName,
resolveTelegramCustomCommands,
TELEGRAM_COMMAND_NAME_PATTERN,
} from "openclaw/plugin-sdk/telegram-command-config";
import type {
ReplyToMode,
TelegramAccountConfig,
@@ -29,25 +22,18 @@ import type {
TelegramGroupConfig,
TelegramTopicConfig,
} from "openclaw/plugin-sdk/config-runtime";
import {
ensureConfiguredBindingRouteReady,
recordInboundSessionMetaSafe,
} from "openclaw/plugin-sdk/conversation-runtime";
import { getAgentScopedMediaLocalRoots } from "openclaw/plugin-sdk/media-runtime";
import {
executePluginCommand,
getPluginCommandSpecs,
matchPluginCommand,
} from "openclaw/plugin-sdk/plugin-runtime";
import {
finalizeInboundContext,
resolveChunkMode,
} from "openclaw/plugin-sdk/reply-dispatch-runtime";
import { resolveMarkdownTableMode } from "openclaw/plugin-sdk/markdown-table-runtime";
import { getPluginCommandSpecs } from "openclaw/plugin-sdk/plugin-runtime";
import { resolveAgentRoute } from "openclaw/plugin-sdk/routing";
import { resolveThreadSessionKeys } from "openclaw/plugin-sdk/routing";
import { getRuntimeConfigSnapshot } from "openclaw/plugin-sdk/runtime-config-snapshot";
import { danger, logVerbose } from "openclaw/plugin-sdk/runtime-env";
import { getChildLogger } from "openclaw/plugin-sdk/runtime-env";
import type { RuntimeEnv } from "openclaw/plugin-sdk/runtime-env";
import {
normalizeTelegramCommandName,
resolveTelegramCustomCommands,
TELEGRAM_COMMAND_NAME_PATTERN,
} from "openclaw/plugin-sdk/telegram-command-config";
import { resolveTelegramAccount } from "./accounts.js";
import { withTelegramApiErrorLogging } from "./api-logging.js";
import { isSenderAllowed, normalizeDmAllowFromWithStore } from "./bot-access.js";
@@ -89,6 +75,9 @@ const EMPTY_RESPONSE_FALLBACK = "No response generated. Please try again.";
const TELEGRAM_NATIVE_COMMAND_CALLBACK_PREFIX = "tgcmd:";
type TelegramNativeCommandContext = Context & { match?: string };
type TelegramChunkMode = ReturnType<
typeof import("openclaw/plugin-sdk/reply-dispatch-runtime").resolveChunkMode
>;
type TelegramCommandAuthResult = {
chatId: number;
@@ -112,6 +101,15 @@ async function loadTelegramNativeCommandDeliveryRuntime() {
return await telegramNativeCommandDeliveryRuntimePromise;
}
let telegramNativeCommandRuntimePromise:
| Promise<typeof import("./bot-native-commands.runtime.js")>
| undefined;
async function loadTelegramNativeCommandRuntime() {
telegramNativeCommandRuntimePromise ??= import("./bot-native-commands.runtime.js");
return await telegramNativeCommandRuntimePromise;
}
export type RegisterTelegramHandlerParams = {
cfg: OpenClawConfig;
accountId: string;
@@ -537,7 +535,7 @@ export const registerTelegramNativeCommands = ({
route: ReturnType<typeof resolveTelegramConversationRoute>["route"];
mediaLocalRoots: readonly string[] | undefined;
tableMode: ReturnType<typeof resolveMarkdownTableMode>;
chunkMode: ReturnType<typeof resolveChunkMode>;
chunkMode: TelegramChunkMode;
} | null> => {
const { msg, runtimeCfg, isGroup, isForum, resolvedThreadId, senderId, topicAgentId } = params;
const chatId = msg.chat.id;
@@ -557,8 +555,9 @@ export const registerTelegramNativeCommands = ({
senderId,
topicAgentId,
});
const nativeCommandRuntime = await loadTelegramNativeCommandRuntime();
if (configuredBinding) {
const ensured = await ensureConfiguredBindingRouteReady({
const ensured = await nativeCommandRuntime.ensureConfiguredBindingRouteReady({
cfg: runtimeCfg,
bindingResolution: configuredBinding,
});
@@ -579,13 +578,20 @@ export const registerTelegramNativeCommands = ({
return null;
}
}
const mediaLocalRoots = getAgentScopedMediaLocalRoots(runtimeCfg, route.agentId);
const mediaLocalRoots = nativeCommandRuntime.getAgentScopedMediaLocalRoots(
runtimeCfg,
route.agentId,
);
const tableMode = resolveMarkdownTableMode({
cfg: runtimeCfg,
channel: "telegram",
accountId: route.accountId,
});
const chunkMode = resolveChunkMode(runtimeCfg, "telegram", route.accountId);
const chunkMode = nativeCommandRuntime.resolveChunkMode(
runtimeCfg,
"telegram",
route.accountId,
);
return { chatId, threadSpec, route, mediaLocalRoots, tableMode, chunkMode };
};
const buildCommandDeliveryBaseOptions = (params: {
@@ -597,7 +603,7 @@ export const registerTelegramNativeCommands = ({
mediaLocalRoots?: readonly string[];
threadSpec: ReturnType<typeof resolveTelegramThreadSpec>;
tableMode: ReturnType<typeof resolveMarkdownTableMode>;
chunkMode: ReturnType<typeof resolveChunkMode>;
chunkMode: TelegramChunkMode;
linkPreview?: boolean;
}) => ({
chatId: String(params.chatId),
@@ -736,9 +742,10 @@ export const registerTelegramNativeCommands = ({
});
// DMs: use raw messageThreadId for thread sessions (not resolvedThreadId which is for forums)
const dmThreadId = threadSpec.scope === "dm" ? threadSpec.id : undefined;
const nativeCommandRuntime = await loadTelegramNativeCommandRuntime();
const threadKeys =
dmThreadId != null
? resolveThreadSessionKeys({
? nativeCommandRuntime.resolveThreadSessionKeys({
baseSessionKey,
threadId: `${chatId}:${dmThreadId}`,
})
@@ -772,7 +779,7 @@ export const registerTelegramNativeCommands = ({
? `${msg.chat.title} id:${chatId}`
: `group:${chatId}`
: (buildSenderName(msg) ?? String(senderId || chatId));
const ctxPayload = finalizeInboundContext({
const ctxPayload = nativeCommandRuntime.finalizeInboundContext({
Body: prompt,
BodyForAgent: prompt,
RawBody: prompt,
@@ -804,7 +811,7 @@ export const registerTelegramNativeCommands = ({
OriginatingTo: originatingTo,
});
await recordInboundSessionMetaSafe({
await nativeCommandRuntime.recordInboundSessionMetaSafe({
cfg: executionCfg,
agentId: route.agentId,
sessionKey: ctxPayload.SessionKey ?? route.sessionKey,
@@ -894,7 +901,8 @@ export const registerTelegramNativeCommands = ({
const runtimeTelegramCfg = resolveFreshTelegramConfig(runtimeCfg);
const rawText = ctx.match?.trim() ?? "";
const commandBody = `/${pluginCommand.command}${rawText ? ` ${rawText}` : ""}`;
const match = matchPluginCommand(commandBody);
const nativeCommandRuntime = await loadTelegramNativeCommandRuntime();
const match = nativeCommandRuntime.matchPluginCommand(commandBody);
if (!match) {
await withTelegramApiErrorLogging({
operation: "sendMessage",
@@ -950,7 +958,7 @@ export const registerTelegramNativeCommands = ({
const to = `telegram:${chatId}`;
const { deliverReplies } = await loadTelegramNativeCommandDeliveryRuntime();
const result = await executePluginCommand({
const result = await nativeCommandRuntime.executePluginCommand({
command: match.command,
args: match.args,
senderId,

View File

@@ -71,6 +71,14 @@ const isGrammyHttpError = (err: unknown): boolean => {
return (err as { name?: string }).name === "HttpError";
};
type TelegramMonitorPollingRuntime = typeof import("./monitor-polling.runtime.js");
type TelegramPollingSessionInstance = InstanceType<
TelegramMonitorPollingRuntime["TelegramPollingSession"]
>;
type TelegramExecApprovalHandlerInstance = InstanceType<
TelegramMonitorPollingRuntime["TelegramExecApprovalHandler"]
>;
let telegramMonitorPollingRuntimePromise:
| Promise<typeof import("./monitor-polling.runtime.js")>
| undefined;
@@ -91,8 +99,8 @@ async function loadTelegramMonitorWebhookRuntime() {
export async function monitorTelegramProvider(opts: MonitorTelegramOpts = {}) {
const log = opts.runtime?.error ?? console.error;
let pollingSession: TelegramPollingSession | undefined;
let execApprovalsHandler: TelegramExecApprovalHandler | undefined;
let pollingSession: TelegramPollingSessionInstance | undefined;
let execApprovalsHandler: TelegramExecApprovalHandlerInstance | undefined;
const unregisterHandler = registerUnhandledRejectionHandler((err) => {
const isNetworkError = isRecoverableTelegramNetworkError(err, { context: "polling" });