Runtime: lazy-load channel runtime singletons

This commit is contained in:
Vincent Koc
2026-03-16 01:01:27 -07:00
parent 7c2863d401
commit 7a09255361
2 changed files with 14 additions and 2 deletions

View File

@@ -22,7 +22,12 @@ const SESSION_COMMAND_PREFIX = "/session";
const SESSION_DURATION_OFF_VALUES = new Set(["off", "disable", "disabled", "none", "0"]);
const SESSION_ACTION_IDLE = "idle";
const SESSION_ACTION_MAX_AGE = "max-age";
const channelRuntime = createPluginRuntime().channel;
let cachedChannelRuntime: ReturnType<typeof createPluginRuntime>["channel"] | undefined;
function getChannelRuntime() {
cachedChannelRuntime ??= createPluginRuntime().channel;
return cachedChannelRuntime;
}
function resolveSessionCommandUsage() {
return "Usage: /session idle <duration|off> | /session max-age <duration|off> (example: /session idle 24h)";
@@ -373,6 +378,7 @@ export const handleSessionCommand: CommandHandler = async (params, allowTextComm
const threadId =
params.ctx.MessageThreadId != null ? String(params.ctx.MessageThreadId).trim() : "";
const telegramConversationId = onTelegram ? resolveTelegramConversationId(params) : undefined;
const channelRuntime = getChannelRuntime();
const discordManager = onDiscord
? channelRuntime.discord.threadBindings.getManager(accountId)

View File

@@ -31,7 +31,12 @@ import {
} from "./session-utils.js";
const ACP_RUNTIME_CLEANUP_TIMEOUT_MS = 15_000;
const channelRuntime = createPluginRuntime().channel;
let cachedChannelRuntime: ReturnType<typeof createPluginRuntime>["channel"] | undefined;
function getChannelRuntime() {
cachedChannelRuntime ??= createPluginRuntime().channel;
return cachedChannelRuntime;
}
function stripRuntimeModelState(entry?: SessionEntry): SessionEntry | undefined {
if (!entry) {
@@ -71,6 +76,7 @@ export async function emitSessionUnboundLifecycleEvent(params: {
emitHooks?: boolean;
}) {
const targetKind = isSubagentSessionKey(params.targetSessionKey) ? "subagent" : "acp";
const channelRuntime = getChannelRuntime();
channelRuntime.discord.threadBindings.unbindBySessionKey({
targetSessionKey: params.targetSessionKey,
targetKind,