openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
71 lines (70 loc) • 3.3 kB
JavaScript
import { t as ADMIN_SCOPE } from "./operator-scopes-Dw7Gu2cA.js";
import { i as normalizePluginGatewayMethodScope } from "./descriptor-C8WchCC9.js";
import "./method-scopes-K6J_UQGL.js";
//#region src/gateway/methods/registry.ts
function normalizeMethodName(name) {
return name.trim();
}
function normalizeDescriptor(input) {
const name = normalizeMethodName(input.name);
if (!name) throw new Error("gateway method descriptor name must not be empty");
const normalizedScope = input.scope === "node" || input.scope === "dynamic" ? input.scope : input.owner.kind === "plugin" ? normalizePluginGatewayMethodScope(name, input.scope).scope : input.scope;
if (!normalizedScope) throw new Error(`gateway method descriptor is missing a scope: ${name}`);
return {
...input,
name,
scope: normalizedScope,
profileAccess: input.profileAccess ?? (input.owner.kind === "core" ? "independent" : "required"),
...input.startup === "unavailable-until-sidecars" ? { startup: "unavailable-until-sidecars" } : {},
...input.controlPlaneWrite === true ? { controlPlaneWrite: true } : {},
...input.advertise === false ? { advertise: false } : {}
};
}
/** Creates a read-only registry for gateway method lookup, listing, and policy metadata. */
function createGatewayMethodRegistry(inputs, pluginRegistry) {
const descriptors = inputs.map(normalizeDescriptor);
const byName = /* @__PURE__ */ new Map();
for (const descriptor of descriptors) {
if (byName.has(descriptor.name)) throw new Error(`gateway method already registered: ${descriptor.name}`);
byName.set(descriptor.name, descriptor);
}
return {
...pluginRegistry ? { pluginRegistry } : {},
getHandler: (name) => byName.get(name)?.handler,
listMethods: () => descriptors.map((descriptor) => descriptor.name),
listAdvertisedMethods: () => descriptors.filter((descriptor) => descriptor.advertise !== false).map((descriptor) => descriptor.name),
getScope: (name) => byName.get(name)?.scope,
isStartupUnavailable: (name) => byName.get(name)?.startup === "unavailable-until-sidecars",
isControlPlaneWrite: (name) => byName.get(name)?.controlPlaneWrite === true,
requiresAuthenticatedProfile: (name) => byName.get(name)?.profileAccess === "required",
descriptors: () => descriptors
};
}
/** Converts a plain handler map into scoped descriptors owned by one gateway surface. */
function createGatewayMethodDescriptorsFromHandlers(params) {
return Object.entries(params.handlers).map(([name, handler]) => {
const scope = params.scopes?.[name] ?? params.defaultScope;
if (!scope) throw new Error(`gateway method is missing a scope: ${name}`);
return {
name,
handler,
owner: params.owner,
scope
};
});
}
/** Resolves plugin method descriptors, including the legacy handler-only registry shape. */
function createPluginGatewayMethodDescriptors(registry) {
const descriptors = registry.gatewayMethodDescriptors ?? [];
if (descriptors.length > 0) return [...descriptors];
return createGatewayMethodDescriptorsFromHandlers({
handlers: registry.gatewayHandlers,
owner: {
kind: "plugin",
pluginId: "unknown"
},
defaultScope: ADMIN_SCOPE
});
}
//#endregion
export { createGatewayMethodRegistry as n, createPluginGatewayMethodDescriptors as r, createGatewayMethodDescriptorsFromHandlers as t };