openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
247 lines (246 loc) • 8.9 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { c as formatErrorStackWithMessage } from "./error-coercion-hDrHOSGy.mjs";
import { b as uniqueStrings } from "./string-normalization-DLmfYZXI.mjs";
import { r as defaultRuntime } from "./runtime-B-SLYP-m.mjs";
import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs";
import { t as getPreparedMessageToolCatalog } from "./prepared-message-tool-catalog-BVSCBqBK.mjs";
import { t as normalizeAnyChannelId } from "./registry-normalize-guW5_4yo.mjs";
import "./registry-BB86xKJa.mjs";
import { n as getLoadedChannelPlugin, r as listChannelPlugins, t as getChannelPlugin } from "./registry-ByqVE5S3.mjs";
import "./plugins-BoMiRtiY.mjs";
import { t as loadOptionalBundledChannelPublicArtifact } from "./optional-public-artifact-tFJCkvSC.mjs";
import { Type } from "typebox";
//#region src/channels/plugins/message-tool-api.ts
/**
* Resolves a bundled channel's message-tool discovery adapter without loading the full plugin.
*/
function resolveBundledChannelMessageToolDiscoveryAdapter(e) {
const t = loadOptionalBundledChannelPublicArtifact({
channelId: e,
artifactBasename: "message-tool-api.js"
});
const n = t?.describeMessageTool;
if (typeof n !== "function") return;
return {
describeMessageTool: n,
describeMessageToolAsync: t?.describeMessageToolAsync
};
}
//#endregion
//#region src/channels/plugins/message-action-discovery.ts
/** Lists message-action adapters from the caller's exact prepared registry. */
const listMessageActionDiscoveryChannels = (e) => (e ?? getPreparedMessageToolCatalog())?.channels ?? listChannelPlugins();
const loggedMessageActionErrors = /* @__PURE__ */ new Set();
function resolveMessageActionDiscoveryChannelId(e) {
return normalizeAnyChannelId(e) ?? normalizeOptionalString(e);
}
function createMessageActionDiscoveryContext(e) {
const t = resolveMessageActionDiscoveryChannelId(e.channel ?? e.currentChannelProvider);
return {
cfg: e.cfg ?? {},
...e.chatType ? { chatType: e.chatType } : {},
currentChannelId: e.currentChannelId,
currentChannelProvider: t,
currentThreadTs: e.currentThreadTs,
currentMessageId: e.currentMessageId,
accountId: e.accountId,
sessionKey: e.sessionKey,
sessionId: e.sessionId,
agentId: e.agentId,
requesterSenderId: e.requesterSenderId,
senderIsOwner: e.senderIsOwner
};
}
function describeMessageToolSafely(e) {
try {
return e.actions.describeMessageTool(e.context) ?? null;
} catch (t) {
reportDiscoveryError(e.pluginId, t);
return null;
}
}
function reportDiscoveryError(e, t) {
const n = formatErrorMessage(t);
const r = `${e}:describeMessageTool:${n}`;
if (!loggedMessageActionErrors.has(r)) {
loggedMessageActionErrors.add(r);
const i = t instanceof Error && t.stack ? formatErrorStackWithMessage(t) : n;
defaultRuntime.error(`[message-action-discovery] ${e}.actions.describeMessageTool failed: ${i}`);
}
}
function normalizeMessageToolMediaSourceParams(e, t) {
if (Array.isArray(e)) return e;
if (!e || typeof e !== "object") return [];
const n = e;
if (t) {
const e = n[t];
return Array.isArray(e) ? e : [];
}
return Object.values(n).flatMap((e) => Array.isArray(e) ? e : []);
}
function resolveCurrentChannelMessageToolDiscoveryAdapter(e, t) {
const n = resolveMessageActionDiscoveryChannelId(e);
if (!n) return null;
const r = t ?? getPreparedMessageToolCatalog();
const i = r?.getChannel(n);
if (i?.actions) return {
pluginId: i.id,
actions: i.actions
};
if (!r) {
const e = getLoadedChannelPlugin(n);
if (e?.actions) return {
pluginId: e.id,
actions: e.actions
};
}
const a = resolveBundledChannelMessageToolDiscoveryAdapter(n);
if (a) return {
pluginId: n,
actions: a
};
const o = r ? void 0 : getChannelPlugin(n);
return o?.actions ? {
pluginId: o.id,
actions: o.actions
} : null;
}
function runMessageActionDiscovery(e) {
let t = e.next();
while (!t.done) t = e.next(resolveMessageActionDiscoveryForPlugin(t.value));
return t.value;
}
async function runMessageActionDiscoveryAsync(e) {
const t = /* @__PURE__ */ new Map();
let n = e.next();
while (!n.done) {
const r = n.value;
const { cfg: i, ...a } = r.context;
const o = JSON.stringify(Object.entries(a).filter(([, e]) => e !== void 0).toSorted(([e], [t]) => e.localeCompare(t)));
const s = r.actions ? t.get(r.actions) ?? [] : [];
let c = s.find((e) => e.cfg === i && e.contextKey === o);
if (!c) {
c = {
cfg: i,
contextKey: o,
described: await describeMessageToolAsync(r)
};
if (r.actions) {
s.push(c);
t.set(r.actions, s);
}
}
n = e.next(projectMessageActionDiscovery(r, c.described));
}
return n.value;
}
function resolveMessageActionDiscoveryForPlugin(e) {
const t = e.actions;
return projectMessageActionDiscovery(e, t ? describeMessageToolSafely({
pluginId: e.pluginId,
context: e.context,
actions: t
}) : null);
}
async function resolveMessageActionDiscoveryForPluginAsync(e) {
return projectMessageActionDiscovery(e, await describeMessageToolAsync(e));
}
async function describeMessageToolAsync(e) {
if (!e.actions) return null;
if (!e.actions.describeMessageToolAsync) return describeMessageToolSafely({
...e,
actions: e.actions
});
try {
return await e.actions.describeMessageToolAsync(e.context);
} catch (t) {
reportDiscoveryError(e.pluginId, t);
return null;
}
}
function projectMessageActionDiscovery(e, t) {
const n = e.includeSchema ? t?.schema : void 0;
return {
actions: e.includeActions && Array.isArray(t?.actions) ? [...t.actions] : [],
capabilities: e.includeCapabilities && Array.isArray(t?.capabilities) ? t.capabilities : [],
schemaContributions: n ? Array.isArray(n) ? n : [n] : [],
mediaSourceParams: normalizeMessageToolMediaSourceParams(t?.mediaSourceParams, e.action)
};
}
function* listCrossChannelSchemaSupportedMessageActionsSteps(e) {
const t = yield* resolveCurrentMessageActionDiscoverySteps(e, {
includeActions: true,
includeSchema: true
});
if (!t) return [];
const n = /* @__PURE__ */ new Set();
for (const e of t.schemaContributions) {
if ((e.visibility ?? "current-channel") !== "current-channel") continue;
if (!Object.hasOwn(e, "actions")) return [];
const t = e.actions;
if (!Array.isArray(t)) return [];
for (const e of t) n.add(e);
}
return t.actions.filter((e) => !n.has(e));
}
/**
* Merges schema properties while preserving the first plugin to define a key.
*/
function mergeToolSchemaProperties(e, t) {
if (!t) return;
for (const [n, r] of Object.entries(t)) {
if (n in e) continue;
e[n] = Type.IsOptional(r) ? r : Type.Optional(r);
}
}
function* resolveChannelMessageToolSchemaPropertiesSteps(e) {
const t = {};
const n = resolveMessageActionDiscoveryChannelId(e.channel);
const r = createMessageActionDiscoveryContext(e);
const contextForPlugin = (t) => {
const i = !n || resolveMessageActionDiscoveryChannelId(t) === n ? e.accountId : void 0;
return {
...r,
accountId: e.resolveAccountIdForChannel ? e.resolveAccountIdForChannel(t, i) : i
};
};
const i = /* @__PURE__ */ new Set();
function* mergePluginSchema(e, r) {
const i = yield {
pluginId: e,
actions: r,
context: contextForPlugin(e),
includeSchema: true
};
for (const r of i.schemaContributions) {
const i = r.visibility ?? "current-channel";
if (!n || i === "all-configured" || e === n) mergeToolSchemaProperties(t, r.properties);
}
}
const a = e.channels ?? listMessageActionDiscoveryChannels(e.preparedMessageToolCatalog);
for (const e of a) {
if (!e.actions) continue;
i.add(e.id);
yield* mergePluginSchema(e.id, e.actions);
}
if (n && !i.has(n)) {
const t = resolveCurrentChannelMessageToolDiscoveryAdapter(n, e.preparedMessageToolCatalog);
if (t?.actions) yield* mergePluginSchema(t.pluginId, t.actions);
}
return t;
}
function* resolveCurrentMessageActionDiscoverySteps(e, t = {}) {
const n = resolveCurrentChannelMessageToolDiscoveryAdapter(e.channel, e.preparedMessageToolCatalog);
return n ? yield {
...n,
context: createMessageActionDiscoveryContext(e),
action: e.action,
...t
} : null;
}
async function resolveChannelMessageToolMediaSourceParamKeysAsync(e) {
const t = await runMessageActionDiscoveryAsync(resolveCurrentMessageActionDiscoverySteps(e));
return uniqueStrings(t?.mediaSourceParams ?? []);
}
//#endregion
export { resolveChannelMessageToolSchemaPropertiesSteps as a, resolveMessageActionDiscoveryChannelId as c, runMessageActionDiscoveryAsync as d, resolveChannelMessageToolMediaSourceParamKeysAsync as i, resolveMessageActionDiscoveryForPluginAsync as l, listCrossChannelSchemaSupportedMessageActionsSteps as n, resolveCurrentChannelMessageToolDiscoveryAdapter as o, listMessageActionDiscoveryChannels as r, resolveCurrentMessageActionDiscoverySteps as s, createMessageActionDiscoveryContext as t, runMessageActionDiscovery as u };