From 8d7778d1d6c56c85311d5d8de5a2658a181e0083 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 8 Mar 2026 19:40:17 +0000 Subject: [PATCH] refactor: dedupe plugin runtime stores --- extensions/bluebubbles/src/runtime.ts | 19 +++++++---------- extensions/discord/src/runtime.ts | 16 ++++----------- extensions/feishu/src/runtime.ts | 16 ++++----------- extensions/googlechat/src/runtime.ts | 16 ++++----------- extensions/imessage/src/runtime.ts | 16 ++++----------- extensions/irc/src/runtime.ts | 16 ++++----------- extensions/line/src/runtime.ts | 16 ++++----------- extensions/matrix/src/runtime.ts | 16 ++++----------- extensions/mattermost/src/runtime.ts | 16 ++++----------- extensions/msteams/src/runtime.ts | 16 ++++----------- extensions/nextcloud-talk/src/runtime.ts | 16 ++++----------- extensions/nostr/src/runtime.ts | 16 ++++----------- extensions/signal/src/runtime.ts | 16 ++++----------- extensions/slack/src/runtime.ts | 16 ++++----------- extensions/synology-chat/src/runtime.ts | 24 ++++++---------------- extensions/telegram/src/runtime.ts | 16 ++++----------- extensions/tlon/src/runtime.ts | 16 ++++----------- extensions/twitch/src/runtime.ts | 16 ++++----------- extensions/whatsapp/src/runtime.ts | 16 ++++----------- extensions/zalo/src/runtime.ts | 16 ++++----------- extensions/zalouser/src/runtime.ts | 16 ++++----------- src/plugin-sdk/index.ts | 1 + src/plugin-sdk/runtime-store.ts | 26 ++++++++++++++++++++++++ 23 files changed, 116 insertions(+), 258 deletions(-) create mode 100644 src/plugin-sdk/runtime-store.ts diff --git a/extensions/bluebubbles/src/runtime.ts b/extensions/bluebubbles/src/runtime.ts index 89ee04cf8a4..e1c0254e1c0 100644 --- a/extensions/bluebubbles/src/runtime.ts +++ b/extensions/bluebubbles/src/runtime.ts @@ -1,31 +1,26 @@ +import { createPluginRuntimeStore } from "openclaw/plugin-sdk"; import type { PluginRuntime } from "openclaw/plugin-sdk/bluebubbles"; -let runtime: PluginRuntime | null = null; +const runtimeStore = createPluginRuntimeStore("BlueBubbles runtime not initialized"); type LegacyRuntimeLogShape = { log?: (message: string) => void }; - -export function setBlueBubblesRuntime(next: PluginRuntime): void { - runtime = next; -} +export const setBlueBubblesRuntime = runtimeStore.setRuntime; export function clearBlueBubblesRuntime(): void { - runtime = null; + runtimeStore.clearRuntime(); } export function tryGetBlueBubblesRuntime(): PluginRuntime | null { - return runtime; + return runtimeStore.tryGetRuntime(); } export function getBlueBubblesRuntime(): PluginRuntime { - if (!runtime) { - throw new Error("BlueBubbles runtime not initialized"); - } - return runtime; + return runtimeStore.getRuntime(); } export function warnBlueBubbles(message: string): void { const formatted = `[bluebubbles] ${message}`; // Backward-compatible with tests/legacy injections that pass { log }. - const log = (runtime as unknown as LegacyRuntimeLogShape | null)?.log; + const log = (runtimeStore.tryGetRuntime() as unknown as LegacyRuntimeLogShape | null)?.log; if (typeof log === "function") { log(formatted); return; diff --git a/extensions/discord/src/runtime.ts b/extensions/discord/src/runtime.ts index 506a81085ee..9a23266edda 100644 --- a/extensions/discord/src/runtime.ts +++ b/extensions/discord/src/runtime.ts @@ -1,14 +1,6 @@ +import { createPluginRuntimeStore } from "openclaw/plugin-sdk"; import type { PluginRuntime } from "openclaw/plugin-sdk/discord"; -let runtime: PluginRuntime | null = null; - -export function setDiscordRuntime(next: PluginRuntime) { - runtime = next; -} - -export function getDiscordRuntime(): PluginRuntime { - if (!runtime) { - throw new Error("Discord runtime not initialized"); - } - return runtime; -} +const { setRuntime: setDiscordRuntime, getRuntime: getDiscordRuntime } = + createPluginRuntimeStore("Discord runtime not initialized"); +export { getDiscordRuntime, setDiscordRuntime }; diff --git a/extensions/feishu/src/runtime.ts b/extensions/feishu/src/runtime.ts index b66579e8775..c1a4b65c50a 100644 --- a/extensions/feishu/src/runtime.ts +++ b/extensions/feishu/src/runtime.ts @@ -1,14 +1,6 @@ +import { createPluginRuntimeStore } from "openclaw/plugin-sdk"; import type { PluginRuntime } from "openclaw/plugin-sdk/feishu"; -let runtime: PluginRuntime | null = null; - -export function setFeishuRuntime(next: PluginRuntime) { - runtime = next; -} - -export function getFeishuRuntime(): PluginRuntime { - if (!runtime) { - throw new Error("Feishu runtime not initialized"); - } - return runtime; -} +const { setRuntime: setFeishuRuntime, getRuntime: getFeishuRuntime } = + createPluginRuntimeStore("Feishu runtime not initialized"); +export { getFeishuRuntime, setFeishuRuntime }; diff --git a/extensions/googlechat/src/runtime.ts b/extensions/googlechat/src/runtime.ts index 55af03db04d..2276eb7dcfa 100644 --- a/extensions/googlechat/src/runtime.ts +++ b/extensions/googlechat/src/runtime.ts @@ -1,14 +1,6 @@ +import { createPluginRuntimeStore } from "openclaw/plugin-sdk"; import type { PluginRuntime } from "openclaw/plugin-sdk/googlechat"; -let runtime: PluginRuntime | null = null; - -export function setGoogleChatRuntime(next: PluginRuntime) { - runtime = next; -} - -export function getGoogleChatRuntime(): PluginRuntime { - if (!runtime) { - throw new Error("Google Chat runtime not initialized"); - } - return runtime; -} +const { setRuntime: setGoogleChatRuntime, getRuntime: getGoogleChatRuntime } = + createPluginRuntimeStore("Google Chat runtime not initialized"); +export { getGoogleChatRuntime, setGoogleChatRuntime }; diff --git a/extensions/imessage/src/runtime.ts b/extensions/imessage/src/runtime.ts index 866d9c8d380..a4b2f1a98de 100644 --- a/extensions/imessage/src/runtime.ts +++ b/extensions/imessage/src/runtime.ts @@ -1,14 +1,6 @@ +import { createPluginRuntimeStore } from "openclaw/plugin-sdk"; import type { PluginRuntime } from "openclaw/plugin-sdk/imessage"; -let runtime: PluginRuntime | null = null; - -export function setIMessageRuntime(next: PluginRuntime) { - runtime = next; -} - -export function getIMessageRuntime(): PluginRuntime { - if (!runtime) { - throw new Error("iMessage runtime not initialized"); - } - return runtime; -} +const { setRuntime: setIMessageRuntime, getRuntime: getIMessageRuntime } = + createPluginRuntimeStore("iMessage runtime not initialized"); +export { getIMessageRuntime, setIMessageRuntime }; diff --git a/extensions/irc/src/runtime.ts b/extensions/irc/src/runtime.ts index 51fcdd7c454..b5597236b7a 100644 --- a/extensions/irc/src/runtime.ts +++ b/extensions/irc/src/runtime.ts @@ -1,14 +1,6 @@ +import { createPluginRuntimeStore } from "openclaw/plugin-sdk"; import type { PluginRuntime } from "openclaw/plugin-sdk/irc"; -let runtime: PluginRuntime | null = null; - -export function setIrcRuntime(next: PluginRuntime) { - runtime = next; -} - -export function getIrcRuntime(): PluginRuntime { - if (!runtime) { - throw new Error("IRC runtime not initialized"); - } - return runtime; -} +const { setRuntime: setIrcRuntime, getRuntime: getIrcRuntime } = + createPluginRuntimeStore("IRC runtime not initialized"); +export { getIrcRuntime, setIrcRuntime }; diff --git a/extensions/line/src/runtime.ts b/extensions/line/src/runtime.ts index 4f1a4fc121a..38ed57e7875 100644 --- a/extensions/line/src/runtime.ts +++ b/extensions/line/src/runtime.ts @@ -1,14 +1,6 @@ +import { createPluginRuntimeStore } from "openclaw/plugin-sdk"; import type { PluginRuntime } from "openclaw/plugin-sdk/line"; -let runtime: PluginRuntime | null = null; - -export function setLineRuntime(r: PluginRuntime): void { - runtime = r; -} - -export function getLineRuntime(): PluginRuntime { - if (!runtime) { - throw new Error("LINE runtime not initialized - plugin not registered"); - } - return runtime; -} +const { setRuntime: setLineRuntime, getRuntime: getLineRuntime } = + createPluginRuntimeStore("LINE runtime not initialized - plugin not registered"); +export { getLineRuntime, setLineRuntime }; diff --git a/extensions/matrix/src/runtime.ts b/extensions/matrix/src/runtime.ts index 4d94aacf99d..90fe7d1f8e9 100644 --- a/extensions/matrix/src/runtime.ts +++ b/extensions/matrix/src/runtime.ts @@ -1,14 +1,6 @@ +import { createPluginRuntimeStore } from "openclaw/plugin-sdk"; import type { PluginRuntime } from "openclaw/plugin-sdk/matrix"; -let runtime: PluginRuntime | null = null; - -export function setMatrixRuntime(next: PluginRuntime) { - runtime = next; -} - -export function getMatrixRuntime(): PluginRuntime { - if (!runtime) { - throw new Error("Matrix runtime not initialized"); - } - return runtime; -} +const { setRuntime: setMatrixRuntime, getRuntime: getMatrixRuntime } = + createPluginRuntimeStore("Matrix runtime not initialized"); +export { getMatrixRuntime, setMatrixRuntime }; diff --git a/extensions/mattermost/src/runtime.ts b/extensions/mattermost/src/runtime.ts index f6e5e83f270..8fe131f2335 100644 --- a/extensions/mattermost/src/runtime.ts +++ b/extensions/mattermost/src/runtime.ts @@ -1,14 +1,6 @@ +import { createPluginRuntimeStore } from "openclaw/plugin-sdk"; import type { PluginRuntime } from "openclaw/plugin-sdk/mattermost"; -let runtime: PluginRuntime | null = null; - -export function setMattermostRuntime(next: PluginRuntime) { - runtime = next; -} - -export function getMattermostRuntime(): PluginRuntime { - if (!runtime) { - throw new Error("Mattermost runtime not initialized"); - } - return runtime; -} +const { setRuntime: setMattermostRuntime, getRuntime: getMattermostRuntime } = + createPluginRuntimeStore("Mattermost runtime not initialized"); +export { getMattermostRuntime, setMattermostRuntime }; diff --git a/extensions/msteams/src/runtime.ts b/extensions/msteams/src/runtime.ts index 97d2272c101..04444a29fc1 100644 --- a/extensions/msteams/src/runtime.ts +++ b/extensions/msteams/src/runtime.ts @@ -1,14 +1,6 @@ +import { createPluginRuntimeStore } from "openclaw/plugin-sdk"; import type { PluginRuntime } from "openclaw/plugin-sdk/msteams"; -let runtime: PluginRuntime | null = null; - -export function setMSTeamsRuntime(next: PluginRuntime) { - runtime = next; -} - -export function getMSTeamsRuntime(): PluginRuntime { - if (!runtime) { - throw new Error("MSTeams runtime not initialized"); - } - return runtime; -} +const { setRuntime: setMSTeamsRuntime, getRuntime: getMSTeamsRuntime } = + createPluginRuntimeStore("MSTeams runtime not initialized"); +export { getMSTeamsRuntime, setMSTeamsRuntime }; diff --git a/extensions/nextcloud-talk/src/runtime.ts b/extensions/nextcloud-talk/src/runtime.ts index 2a7718e1661..d4870a74839 100644 --- a/extensions/nextcloud-talk/src/runtime.ts +++ b/extensions/nextcloud-talk/src/runtime.ts @@ -1,14 +1,6 @@ +import { createPluginRuntimeStore } from "openclaw/plugin-sdk"; import type { PluginRuntime } from "openclaw/plugin-sdk/nextcloud-talk"; -let runtime: PluginRuntime | null = null; - -export function setNextcloudTalkRuntime(next: PluginRuntime) { - runtime = next; -} - -export function getNextcloudTalkRuntime(): PluginRuntime { - if (!runtime) { - throw new Error("Nextcloud Talk runtime not initialized"); - } - return runtime; -} +const { setRuntime: setNextcloudTalkRuntime, getRuntime: getNextcloudTalkRuntime } = + createPluginRuntimeStore("Nextcloud Talk runtime not initialized"); +export { getNextcloudTalkRuntime, setNextcloudTalkRuntime }; diff --git a/extensions/nostr/src/runtime.ts b/extensions/nostr/src/runtime.ts index dbcffde4979..1063bd8d6d3 100644 --- a/extensions/nostr/src/runtime.ts +++ b/extensions/nostr/src/runtime.ts @@ -1,14 +1,6 @@ +import { createPluginRuntimeStore } from "openclaw/plugin-sdk"; import type { PluginRuntime } from "openclaw/plugin-sdk/nostr"; -let runtime: PluginRuntime | null = null; - -export function setNostrRuntime(next: PluginRuntime): void { - runtime = next; -} - -export function getNostrRuntime(): PluginRuntime { - if (!runtime) { - throw new Error("Nostr runtime not initialized"); - } - return runtime; -} +const { setRuntime: setNostrRuntime, getRuntime: getNostrRuntime } = + createPluginRuntimeStore("Nostr runtime not initialized"); +export { getNostrRuntime, setNostrRuntime }; diff --git a/extensions/signal/src/runtime.ts b/extensions/signal/src/runtime.ts index 21f90071ad8..fd6c5fbdae6 100644 --- a/extensions/signal/src/runtime.ts +++ b/extensions/signal/src/runtime.ts @@ -1,14 +1,6 @@ +import { createPluginRuntimeStore } from "openclaw/plugin-sdk"; import type { PluginRuntime } from "openclaw/plugin-sdk/signal"; -let runtime: PluginRuntime | null = null; - -export function setSignalRuntime(next: PluginRuntime) { - runtime = next; -} - -export function getSignalRuntime(): PluginRuntime { - if (!runtime) { - throw new Error("Signal runtime not initialized"); - } - return runtime; -} +const { setRuntime: setSignalRuntime, getRuntime: getSignalRuntime } = + createPluginRuntimeStore("Signal runtime not initialized"); +export { getSignalRuntime, setSignalRuntime }; diff --git a/extensions/slack/src/runtime.ts b/extensions/slack/src/runtime.ts index 02222d2b073..9ba83fcb4c8 100644 --- a/extensions/slack/src/runtime.ts +++ b/extensions/slack/src/runtime.ts @@ -1,14 +1,6 @@ +import { createPluginRuntimeStore } from "openclaw/plugin-sdk"; import type { PluginRuntime } from "openclaw/plugin-sdk/slack"; -let runtime: PluginRuntime | null = null; - -export function setSlackRuntime(next: PluginRuntime) { - runtime = next; -} - -export function getSlackRuntime(): PluginRuntime { - if (!runtime) { - throw new Error("Slack runtime not initialized"); - } - return runtime; -} +const { setRuntime: setSlackRuntime, getRuntime: getSlackRuntime } = + createPluginRuntimeStore("Slack runtime not initialized"); +export { getSlackRuntime, setSlackRuntime }; diff --git a/extensions/synology-chat/src/runtime.ts b/extensions/synology-chat/src/runtime.ts index f7ef39ff65f..6abb71d8188 100644 --- a/extensions/synology-chat/src/runtime.ts +++ b/extensions/synology-chat/src/runtime.ts @@ -1,20 +1,8 @@ -/** - * Plugin runtime singleton. - * Stores the PluginRuntime from api.runtime (set during register()). - * Used by channel.ts to access dispatch functions. - */ - +import { createPluginRuntimeStore } from "openclaw/plugin-sdk"; import type { PluginRuntime } from "openclaw/plugin-sdk/synology-chat"; -let runtime: PluginRuntime | null = null; - -export function setSynologyRuntime(r: PluginRuntime): void { - runtime = r; -} - -export function getSynologyRuntime(): PluginRuntime { - if (!runtime) { - throw new Error("Synology Chat runtime not initialized - plugin not registered"); - } - return runtime; -} +const { setRuntime: setSynologyRuntime, getRuntime: getSynologyRuntime } = + createPluginRuntimeStore( + "Synology Chat runtime not initialized - plugin not registered", + ); +export { getSynologyRuntime, setSynologyRuntime }; diff --git a/extensions/telegram/src/runtime.ts b/extensions/telegram/src/runtime.ts index dd1e3f9f2b8..4effcb7b5bf 100644 --- a/extensions/telegram/src/runtime.ts +++ b/extensions/telegram/src/runtime.ts @@ -1,14 +1,6 @@ +import { createPluginRuntimeStore } from "openclaw/plugin-sdk"; import type { PluginRuntime } from "openclaw/plugin-sdk/telegram"; -let runtime: PluginRuntime | null = null; - -export function setTelegramRuntime(next: PluginRuntime) { - runtime = next; -} - -export function getTelegramRuntime(): PluginRuntime { - if (!runtime) { - throw new Error("Telegram runtime not initialized"); - } - return runtime; -} +const { setRuntime: setTelegramRuntime, getRuntime: getTelegramRuntime } = + createPluginRuntimeStore("Telegram runtime not initialized"); +export { getTelegramRuntime, setTelegramRuntime }; diff --git a/extensions/tlon/src/runtime.ts b/extensions/tlon/src/runtime.ts index 0400d636b57..1551ea38f3f 100644 --- a/extensions/tlon/src/runtime.ts +++ b/extensions/tlon/src/runtime.ts @@ -1,14 +1,6 @@ +import { createPluginRuntimeStore } from "openclaw/plugin-sdk"; import type { PluginRuntime } from "openclaw/plugin-sdk/tlon"; -let runtime: PluginRuntime | null = null; - -export function setTlonRuntime(next: PluginRuntime) { - runtime = next; -} - -export function getTlonRuntime(): PluginRuntime { - if (!runtime) { - throw new Error("Tlon runtime not initialized"); - } - return runtime; -} +const { setRuntime: setTlonRuntime, getRuntime: getTlonRuntime } = + createPluginRuntimeStore("Tlon runtime not initialized"); +export { getTlonRuntime, setTlonRuntime }; diff --git a/extensions/twitch/src/runtime.ts b/extensions/twitch/src/runtime.ts index 5dfdd225c4c..f82e4313f81 100644 --- a/extensions/twitch/src/runtime.ts +++ b/extensions/twitch/src/runtime.ts @@ -1,14 +1,6 @@ +import { createPluginRuntimeStore } from "openclaw/plugin-sdk"; import type { PluginRuntime } from "openclaw/plugin-sdk/twitch"; -let runtime: PluginRuntime | null = null; - -export function setTwitchRuntime(next: PluginRuntime) { - runtime = next; -} - -export function getTwitchRuntime(): PluginRuntime { - if (!runtime) { - throw new Error("Twitch runtime not initialized"); - } - return runtime; -} +const { setRuntime: setTwitchRuntime, getRuntime: getTwitchRuntime } = + createPluginRuntimeStore("Twitch runtime not initialized"); +export { getTwitchRuntime, setTwitchRuntime }; diff --git a/extensions/whatsapp/src/runtime.ts b/extensions/whatsapp/src/runtime.ts index 490c7873219..c5044db6a29 100644 --- a/extensions/whatsapp/src/runtime.ts +++ b/extensions/whatsapp/src/runtime.ts @@ -1,14 +1,6 @@ +import { createPluginRuntimeStore } from "openclaw/plugin-sdk"; import type { PluginRuntime } from "openclaw/plugin-sdk/whatsapp"; -let runtime: PluginRuntime | null = null; - -export function setWhatsAppRuntime(next: PluginRuntime) { - runtime = next; -} - -export function getWhatsAppRuntime(): PluginRuntime { - if (!runtime) { - throw new Error("WhatsApp runtime not initialized"); - } - return runtime; -} +const { setRuntime: setWhatsAppRuntime, getRuntime: getWhatsAppRuntime } = + createPluginRuntimeStore("WhatsApp runtime not initialized"); +export { getWhatsAppRuntime, setWhatsAppRuntime }; diff --git a/extensions/zalo/src/runtime.ts b/extensions/zalo/src/runtime.ts index 5d96660a7d3..74542043913 100644 --- a/extensions/zalo/src/runtime.ts +++ b/extensions/zalo/src/runtime.ts @@ -1,14 +1,6 @@ +import { createPluginRuntimeStore } from "openclaw/plugin-sdk"; import type { PluginRuntime } from "openclaw/plugin-sdk/zalo"; -let runtime: PluginRuntime | null = null; - -export function setZaloRuntime(next: PluginRuntime): void { - runtime = next; -} - -export function getZaloRuntime(): PluginRuntime { - if (!runtime) { - throw new Error("Zalo runtime not initialized"); - } - return runtime; -} +const { setRuntime: setZaloRuntime, getRuntime: getZaloRuntime } = + createPluginRuntimeStore("Zalo runtime not initialized"); +export { getZaloRuntime, setZaloRuntime }; diff --git a/extensions/zalouser/src/runtime.ts b/extensions/zalouser/src/runtime.ts index 42cb9def444..473df2b8fbe 100644 --- a/extensions/zalouser/src/runtime.ts +++ b/extensions/zalouser/src/runtime.ts @@ -1,14 +1,6 @@ +import { createPluginRuntimeStore } from "openclaw/plugin-sdk"; import type { PluginRuntime } from "openclaw/plugin-sdk/zalouser"; -let runtime: PluginRuntime | null = null; - -export function setZalouserRuntime(next: PluginRuntime): void { - runtime = next; -} - -export function getZalouserRuntime(): PluginRuntime { - if (!runtime) { - throw new Error("Zalouser runtime not initialized"); - } - return runtime; -} +const { setRuntime: setZalouserRuntime, getRuntime: getZalouserRuntime } = + createPluginRuntimeStore("Zalouser runtime not initialized"); +export { getZalouserRuntime, setZalouserRuntime }; diff --git a/src/plugin-sdk/index.ts b/src/plugin-sdk/index.ts index ca3f54a479b..816d644cd99 100644 --- a/src/plugin-sdk/index.ts +++ b/src/plugin-sdk/index.ts @@ -194,6 +194,7 @@ export { buildOauthProviderAuthResult } from "./provider-auth-result.js"; export { formatResolvedUnresolvedNote } from "./resolution-notes.js"; export { buildChannelSendResult } from "./channel-send-result.js"; export type { ChannelSendRawResult } from "./channel-send-result.js"; +export { createPluginRuntimeStore } from "./runtime-store.js"; export type { ChannelDock } from "../channels/dock.js"; export { getChatChannelMeta } from "../channels/registry.js"; export { resolveAllowlistMatchByCandidates } from "../channels/allowlist-match.js"; diff --git a/src/plugin-sdk/runtime-store.ts b/src/plugin-sdk/runtime-store.ts new file mode 100644 index 00000000000..de0d84131e1 --- /dev/null +++ b/src/plugin-sdk/runtime-store.ts @@ -0,0 +1,26 @@ +export function createPluginRuntimeStore(errorMessage: string): { + setRuntime: (next: T) => void; + clearRuntime: () => void; + tryGetRuntime: () => T | null; + getRuntime: () => T; +} { + let runtime: T | null = null; + + return { + setRuntime(next: T) { + runtime = next; + }, + clearRuntime() { + runtime = null; + }, + tryGetRuntime() { + return runtime; + }, + getRuntime() { + if (!runtime) { + throw new Error(errorMessage); + } + return runtime; + }, + }; +}