openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
61 lines (60 loc) • 1.76 kB
JavaScript
//#region src/infra/channel-runtime-context.ts
const NOOP_DISPOSE = () => {};
/** Registers a channel-scoped runtime context, returning null when no runtime registry exists. */
function registerChannelRuntimeContext(e) {
return e.channelRuntime?.runtimeContexts?.register({
channelId: e.channelId,
accountId: e.accountId,
capability: e.capability,
context: e.context,
abortSignal: e.abortSignal
}) ?? null;
}
/** Reads a channel-scoped runtime context from the current runtime registry. */
function getChannelRuntimeContext(e) {
return e.channelRuntime?.runtimeContexts?.get({
channelId: e.channelId,
accountId: e.accountId,
capability: e.capability
});
}
/** Watches context registration changes for one channel/account/capability key. */
function watchChannelRuntimeContexts(e) {
return e.channelRuntime?.runtimeContexts?.watch({
channelId: e.channelId,
accountId: e.accountId,
capability: e.capability,
onEvent: e.onEvent
}) ?? null;
}
/** Wraps a channel runtime so contexts registered during a task are disposed together. */
function createTaskScopedChannelRuntime(e) {
const t = e.channelRuntime;
if (!t) return {
channelRuntime: void 0,
dispose: NOOP_DISPOSE
};
const n = t.runtimeContexts;
const r = /* @__PURE__ */ new Set();
return {
channelRuntime: {
...t,
runtimeContexts: {
...n,
register: (e) => {
const t = n.register(e);
const i = { dispose: () => {
if (r.delete(i)) t.dispose();
} };
r.add(i);
return i;
}
}
},
dispose: () => {
for (const e of Array.from(r)) e.dispose();
}
};
}
//#endregion
export { watchChannelRuntimeContexts as i, getChannelRuntimeContext as n, registerChannelRuntimeContext as r, createTaskScopedChannelRuntime as t };