perf(memory): lazy-load telegram monitor runtime graphs

This commit is contained in:
Vincent Koc
2026-04-02 14:07:35 +09:00
parent fcfb9ddb1d
commit 4309dc6d5e
3 changed files with 58 additions and 23 deletions

View File

@@ -0,0 +1,3 @@
export { TelegramExecApprovalHandler } from "./exec-approvals-handler.js";
export { TelegramPollingSession } from "./polling-session.js";
export { readTelegramUpdateOffset, writeTelegramUpdateOffset } from "./update-offset-store.js";

View File

@@ -0,0 +1,2 @@
export { TelegramExecApprovalHandler } from "./exec-approvals-handler.js";
export { startTelegramWebhook } from "./webhook.js";

View File

@@ -8,16 +8,12 @@ import type { RuntimeEnv } from "openclaw/plugin-sdk/runtime-env";
import { formatErrorMessage } from "openclaw/plugin-sdk/ssrf-runtime";
import { resolveTelegramAccount } from "./accounts.js";
import { resolveTelegramAllowedUpdates } from "./allowed-updates.js";
import { TelegramExecApprovalHandler } from "./exec-approvals-handler.js";
import { resolveTelegramTransport } from "./fetch.js";
import {
isRecoverableTelegramNetworkError,
isTelegramPollingNetworkError,
} from "./network-errors.js";
import { TelegramPollingSession } from "./polling-session.js";
import { makeProxyFetch } from "./proxy.js";
import { readTelegramUpdateOffset, writeTelegramUpdateOffset } from "./update-offset-store.js";
import { startTelegramWebhook } from "./webhook.js";
export type MonitorTelegramOpts = {
token?: string;
@@ -75,6 +71,24 @@ const isGrammyHttpError = (err: unknown): boolean => {
return (err as { name?: string }).name === "HttpError";
};
let telegramMonitorPollingRuntimePromise:
| Promise<typeof import("./monitor-polling.runtime.js")>
| undefined;
async function loadTelegramMonitorPollingRuntime() {
telegramMonitorPollingRuntimePromise ??= import("./monitor-polling.runtime.js");
return await telegramMonitorPollingRuntimePromise;
}
let telegramMonitorWebhookRuntimePromise:
| Promise<typeof import("./monitor-webhook.runtime.js")>
| undefined;
async function loadTelegramMonitorWebhookRuntime() {
telegramMonitorWebhookRuntimePromise ??= import("./monitor-webhook.runtime.js");
return await telegramMonitorWebhookRuntimePromise;
}
export async function monitorTelegramProvider(opts: MonitorTelegramOpts = {}) {
const log = opts.runtime?.error ?? console.error;
let pollingSession: TelegramPollingSession | undefined;
@@ -120,6 +134,41 @@ export async function monitorTelegramProvider(opts: MonitorTelegramOpts = {}) {
const proxyFetch =
opts.proxyFetch ?? (account.config.proxy ? makeProxyFetch(account.config.proxy) : undefined);
if (opts.useWebhook) {
const { TelegramExecApprovalHandler, startTelegramWebhook } =
await loadTelegramMonitorWebhookRuntime();
execApprovalsHandler = new TelegramExecApprovalHandler({
token,
accountId: account.accountId,
cfg,
runtime: opts.runtime,
});
await execApprovalsHandler.start();
await startTelegramWebhook({
token,
accountId: account.accountId,
config: cfg,
path: opts.webhookPath,
port: opts.webhookPort,
secret: opts.webhookSecret ?? account.config.webhookSecret,
host: opts.webhookHost ?? account.config.webhookHost,
runtime: opts.runtime as RuntimeEnv,
fetch: proxyFetch,
abortSignal: opts.abortSignal,
publicUrl: opts.webhookUrl,
webhookCertPath: opts.webhookCertPath,
});
await waitForAbortSignal(opts.abortSignal);
return;
}
const {
TelegramExecApprovalHandler,
TelegramPollingSession,
readTelegramUpdateOffset,
writeTelegramUpdateOffset,
} = await loadTelegramMonitorPollingRuntime();
execApprovalsHandler = new TelegramExecApprovalHandler({
token,
accountId: account.accountId,
@@ -162,25 +211,6 @@ export async function monitorTelegramProvider(opts: MonitorTelegramOpts = {}) {
}
};
if (opts.useWebhook) {
await startTelegramWebhook({
token,
accountId: account.accountId,
config: cfg,
path: opts.webhookPath,
port: opts.webhookPort,
secret: opts.webhookSecret ?? account.config.webhookSecret,
host: opts.webhookHost ?? account.config.webhookHost,
runtime: opts.runtime as RuntimeEnv,
fetch: proxyFetch,
abortSignal: opts.abortSignal,
publicUrl: opts.webhookUrl,
webhookCertPath: opts.webhookCertPath,
});
await waitForAbortSignal(opts.abortSignal);
return;
}
// Preserve sticky IPv4 fallback state across clean/conflict restarts.
// Dirty polling cycles rebuild transport inside TelegramPollingSession.
const createTelegramTransportForPolling = () =>