Skip to content

Commit 7a09255

Browse files
committed
Runtime: lazy-load channel runtime singletons
1 parent 7c2863d commit 7a09255

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

src/auto-reply/reply/commands-session.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ const SESSION_COMMAND_PREFIX = "/session";
2222
const SESSION_DURATION_OFF_VALUES = new Set(["off", "disable", "disabled", "none", "0"]);
2323
const SESSION_ACTION_IDLE = "idle";
2424
const SESSION_ACTION_MAX_AGE = "max-age";
25-
const channelRuntime = createPluginRuntime().channel;
25+
let cachedChannelRuntime: ReturnType<typeof createPluginRuntime>["channel"] | undefined;
26+
27+
function getChannelRuntime() {
28+
cachedChannelRuntime ??= createPluginRuntime().channel;
29+
return cachedChannelRuntime;
30+
}
2631

2732
function resolveSessionCommandUsage() {
2833
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
373378
const threadId =
374379
params.ctx.MessageThreadId != null ? String(params.ctx.MessageThreadId).trim() : "";
375380
const telegramConversationId = onTelegram ? resolveTelegramConversationId(params) : undefined;
381+
const channelRuntime = getChannelRuntime();
376382

377383
const discordManager = onDiscord
378384
? channelRuntime.discord.threadBindings.getManager(accountId)

src/gateway/session-reset-service.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ import {
3131
} from "./session-utils.js";
3232

3333
const ACP_RUNTIME_CLEANUP_TIMEOUT_MS = 15_000;
34-
const channelRuntime = createPluginRuntime().channel;
34+
let cachedChannelRuntime: ReturnType<typeof createPluginRuntime>["channel"] | undefined;
35+
36+
function getChannelRuntime() {
37+
cachedChannelRuntime ??= createPluginRuntime().channel;
38+
return cachedChannelRuntime;
39+
}
3540

3641
function stripRuntimeModelState(entry?: SessionEntry): SessionEntry | undefined {
3742
if (!entry) {
@@ -71,6 +76,7 @@ export async function emitSessionUnboundLifecycleEvent(params: {
7176
emitHooks?: boolean;
7277
}) {
7378
const targetKind = isSubagentSessionKey(params.targetSessionKey) ? "subagent" : "acp";
79+
const channelRuntime = getChannelRuntime();
7480
channelRuntime.discord.threadBindings.unbindBySessionKey({
7581
targetSessionKey: params.targetSessionKey,
7682
targetKind,

0 commit comments

Comments
 (0)