openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
456 lines (455 loc) • 21.5 kB
JavaScript
import { c as normalizeOptionalLowercaseString, l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { v as normalizeUniqueTrimmedStringList } from "./string-normalization-DLmfYZXI.mjs";
import { t as BUNDLED_OFFICIAL_EXTERNAL_PLUGIN_CATALOG_ENTRIES } from "./official-external-plugin-bundled-catalogs-BXkCPIh3.mjs";
import { n as normalizeClawHubSha256Integrity } from "./clawhub-integrity-DTNmm7ZL.mjs";
import { i as isExactSemverVersion, l as resolveOpenClawReleaseCohortVersion, s as parseRegistryNpmSpec } from "./npm-registry-spec-HKkAvl6N.mjs";
import { f as selectNpmChannelVersion } from "./update-channels-CDc4OFaS.mjs";
import { r as isUnavailableClawHubTarget, t as CLAWHUB_INSTALL_ERROR_CODE } from "./clawhub-error-codes-5kIjtf6h.mjs";
import { n as isUnavailableNpmTarget, t as PLUGIN_INSTALL_ERROR_CODE } from "./install-types-BBbGKapR.mjs";
import { a as getFeedEntryInstallCandidateRecords, g as resolveOfficialExternalPluginId, h as resolveOfficialExternalPluginCatalogProfileConfig, m as resolveOfficialExternalPluginCatalogEntryKey, o as getOfficialExternalPluginCatalogManifest, s as hasKnownCatalogSourceRef, t as DEFAULT_OFFICIAL_EXTERNAL_PLUGIN_CATALOG_PROFILE_CONFIG } from "./official-external-plugin-catalog-source-4o5oPhVN.mjs";
//#region src/infra/clawhub-spec.ts
/** Parses explicit `clawhub:<name>[@version]` package specs for ClawHub installs. */
function parseClawHubPluginSpec(e) {
const t = e.trim();
if (!t.toLowerCase().startsWith("clawhub:")) return null;
const n = t.slice(8).trim();
if (!n) return null;
const r = n.lastIndexOf("@");
if (r <= 0) return { name: n };
if (r >= n.length - 1) return null;
const i = n.slice(0, r).trim();
const a = n.slice(r + 1).trim();
if (!i || !a) return null;
return {
name: i,
version: a
};
}
//#endregion
//#region src/plugins/plugin-install-default-choice.ts
function normalizePluginInstallDefaultChoice(e) {
return e === "clawhub" || e === "npm" || e === "local" ? e : void 0;
}
//#endregion
//#region src/plugins/install-channel-specs.ts
/** Only declared identities participate; a ClawHub slug never implies an npm package. */
function resolvePluginInstallSources(e, t) {
const n = [];
for (const r of ["npm", "clawhub"]) {
const i = (r === "npm" ? e.npmSpec : e.clawhubSpec)?.trim();
if (!i || t && r !== t) continue;
const a = e.npmSpec?.trim() ? "npm" : "clawhub";
n.push({
source: r,
spec: i,
...r === a && e.expectedIntegrity ? { expectedIntegrity: e.expectedIntegrity } : {}
});
}
return n;
}
function isUnavailablePluginSource(e, t) {
if (t.ok) return false;
return e === "npm" ? t.code === PLUGIN_INSTALL_ERROR_CODE.RELEASE_COHORT_UNAVAILABLE || isUnavailableNpmTarget({
ok: false,
code: t.code
}) : isUnavailableClawHubTarget({
ok: false,
code: t.code
}) || t.code === CLAWHUB_INSTALL_ERROR_CODE.ARTIFACT_UNAVAILABLE || t.code === CLAWHUB_INSTALL_ERROR_CODE.ARTIFACT_DOWNLOAD_UNAVAILABLE;
}
/** Availability alone permits a declared secondary; every attempt owns its artifact review. */
async function installWithSourceFallback(e) {
for (const [t, n] of e.sources.entries()) {
const r = await e.install(n);
const i = e.sources[t + 1];
if (!i || !isUnavailablePluginSource(n.source, e.result(r))) return {
attempt: r,
source: n
};
await e.onFallback(`${n.spec} unavailable; using ${i.spec} instead.`);
}
throw new Error("Plugin has no declared remote install source.");
}
var NpmChannelResolutionError = class extends Error {
constructor(...e) {
super(...e);
this.code = PLUGIN_INSTALL_ERROR_CODE.NPM_METADATA_FAILURE;
}
};
/** Bare specs and latest retain default intent while following the active release channel. */
function resolveDefaultNpmSpec(e) {
const t = parseRegistryNpmSpec(e);
if (!t) return null;
if (t.selectorKind === "none" || t.selectorKind === "tag" && t.selector?.toLowerCase() === "latest") return t;
return null;
}
function resolveCoreBoundNpmSpec(e, t = false) {
if (e.updateChannel === "extended-stable" || e.updateChannel === "stable" && (e.versionBoundToCore || t)) {
const t = resolveDefaultNpmSpec(e.spec);
if (t && e.officialPackageName === t.name) {
const n = e.coreVersion?.trim();
if (!n || !isExactSemverVersion(n)) {
const n = e.updateChannel === "extended-stable" ? "Extended-stable" : "Version-bound";
throw new Error(`${n} plugin resolution for ${t.name} requires an exact core version.`);
}
const r = e.versionBoundToCore ? resolveOpenClawReleaseCohortVersion(n) : n;
return `${t.name}@${r}`;
}
}
}
async function resolveNpmInstallSpecsForUpdateChannel(e) {
const t = e.installSpecOverride ?? resolveCoreBoundNpmSpec(e);
const n = parseRegistryNpmSpec(e.spec);
const r = n?.selector?.toLowerCase();
if (t || e.updateChannel !== "beta" || !n || n.selectorKind !== "none" && !(n.selectorKind === "tag" && (r === "latest" || r === "beta"))) return {
installSpec: t ?? e.spec,
recordSpec: e.spec
};
const { resolveNpmSpecMetadata: i } = await import("./install-source-utils-DacTIwQ7.mjs");
const resolveTag = async (t) => {
const r = await i({
spec: `${n.name}@${t}`,
timeoutMs: e.timeoutMs
});
if (!r.ok) {
if (r.category === "metadata-env") throw new NpmChannelResolutionError(`Could not resolve ${n.name}@${t}: ${r.error}`);
return {
version: null,
metadata: void 0
};
}
if (r.metadata.name !== n.name || !isExactSemverVersion(r.metadata.version ?? "")) throw new NpmChannelResolutionError(`Invalid npm channel metadata for ${n.name}@${t}.`);
return {
version: r.metadata.version ?? null,
metadata: r.metadata,
tag: t
};
};
const [a, o] = await Promise.all([resolveTag("beta"), resolveTag("latest")]);
const s = selectNpmChannelVersion(a, o);
if (!s.version || !s.metadata) return {
installSpec: `${n.name}@latest`,
recordSpec: e.spec
};
return {
installSpec: `${n.name}@${s.version}`,
recordSpec: e.spec,
npmResolution: s.metadata,
channelTag: s.tag,
...s === o && a.version ? { channelReason: "tag-behind-latest" } : {}
};
}
function resolveClawHubInstallSpecsForUpdateChannel(e) {
const t = parseClawHubPluginSpec(e.spec);
if (t && e.officialPackageName === t.name && (e.updateChannel === "extended-stable" || e.updateChannel === "stable" && (e.versionBoundToCore || e.preferCoreVersion))) {
const n = resolveCoreBoundNpmSpec({
...e,
spec: `${t.name}${t.version ? `@${t.version}` : ""}`
}, e.preferCoreVersion);
const r = n ? `clawhub:${n}` : e.spec;
return {
installSpec: r,
recordSpec: e.spec,
...n && e.updateChannel === "stable" && e.preferCoreVersion ? {
fallbackSpec: e.spec,
fallbackLabel: r
} : {}
};
}
if (e.updateChannel !== "beta" || !t || t.version && t.version.toLowerCase() !== "latest") return {
installSpec: e.spec,
recordSpec: e.spec
};
const n = `clawhub:${t.name}@beta`;
return {
installSpec: n,
recordSpec: e.spec,
fallbackSpec: e.spec,
fallbackLabel: n
};
}
/**
* Installs the channel-resolved spec, widening to the operator's own selector
* when that release has no published artifact. The degrade is announced rather
* than silent, because it changes which build the operator ends up running.
*/
async function installWithChannelFallback(e) {
const t = await e.install(e.installSpec);
const { fallbackSpec: n } = e;
if (!n || n === e.installSpec || !e.isRetryable(t)) return t;
await e.onFallback(`No ${e.installSpec} release is published; installing ${n} instead.`);
return await e.install(n);
}
//#endregion
//#region src/plugins/official-external-plugin-catalog.ts
/** Reads official external plugin/channel/provider catalogs into manifest-like metadata. */
function getFeedEntryInstallCandidates(e) {
return normalizeOptionalString(e.state) === "available" && normalizeOptionalString(e.publisher?.trust) === "official" ? getFeedEntryInstallCandidateRecords(e) : [];
}
const BUNDLED_CATALOG_SOURCE_REFS = new Set(Object.keys(DEFAULT_OFFICIAL_EXTERNAL_PLUGIN_CATALOG_PROFILE_CONFIG.sources ?? {}));
function prepareBundledOfficialExternalPluginCatalog() {
const e = /* @__PURE__ */ new Set();
const t = [];
const n = /* @__PURE__ */ new Map();
const r = /* @__PURE__ */ new Map();
const i = /* @__PURE__ */ new Set();
for (const a of BUNDLED_OFFICIAL_EXTERNAL_PLUGIN_CATALOG_ENTRIES) {
if (getFeedEntryInstallCandidateRecords(a).some((e) => !hasKnownCatalogSourceRef(e, BUNDLED_CATALOG_SOURCE_REFS))) continue;
const o = resolveOfficialExternalPluginCatalogEntryKey(a);
if (o && !e.has(o)) {
e.add(o);
t.push(a);
for (const e of resolveOfficialExternalPluginLookupIds(a)) if (!n.has(e)) n.set(e, a);
const s = normalizeOptionalString(a.name);
if (s && !r.has(s)) r.set(s, a);
const c = resolveOfficialExternalPluginId(a)?.toLowerCase();
if (c) i.add(c);
}
}
return {
entries: t,
byId: n,
byPackage: r,
canonicalIds: i
};
}
let bundledCatalog;
function getBundledOfficialExternalPluginCatalog() {
return bundledCatalog ??= prepareBundledOfficialExternalPluginCatalog();
}
function formatFeedInstallCandidateSpec(e) {
const t = normalizeOptionalString(e.package);
if (!t) return;
const n = normalizeOptionalString(e.version);
if (!n || t.endsWith(`@${n}`)) return t;
return `${t}@${n}`;
}
function getFeedEntryCandidateSourceType(e, t) {
const n = normalizeOptionalString(e.sourceRef);
if (!n) return;
return resolveOfficialExternalPluginCatalogProfileConfig(t).sources[n]?.type;
}
function resolveFeedEntryInstallSources(e) {
const t = getFeedEntryInstallCandidates(e.entry);
return ["npm", "clawhub"].flatMap((n) => {
const r = t.find((t) => getFeedEntryCandidateSourceType(t, e.catalogConfig) === n && Boolean(normalizeOptionalString(t.package)));
const i = r && formatFeedInstallCandidateSpec(r);
if (!r || !i) return [];
const a = n === "npm" ? normalizeNpmExpectedIntegrity(r.integrity) : normalizeClawHubSha256ExpectedIntegrity(r.integrity);
return [{
source: n,
spec: n === "clawhub" ? `clawhub:${i}` : i,
...a ? { expectedIntegrity: a } : {}
}];
});
}
function resolveFeedEntryInstallCandidate(e) {
const t = resolveFeedEntryInstallSources(e)[0];
return t ? {
...t.source === "npm" ? { npmSpec: t.spec } : { clawhubSpec: t.spec },
defaultChoice: t.source,
...t.expectedIntegrity ? { expectedIntegrity: t.expectedIntegrity } : {}
} : null;
}
/** Source-specific catalog pins stay attached to the artifact they authenticate. */
function resolveOfficialExternalPluginInstallSources(e, t) {
const n = t?.resolvedInstall === void 0 ? resolveOfficialExternalPluginInstall(e) : t.resolvedInstall;
if (!n) return [];
const r = resolveFeedEntryInstallSources({ entry: e });
return r.length > 0 ? r : resolvePluginInstallSources(n);
}
function normalizeClawHubSha256ExpectedIntegrity(e) {
const t = normalizeOptionalString(e);
return t ? normalizeClawHubSha256Integrity(t) ?? void 0 : void 0;
}
function normalizeNpmExpectedIntegrity(e) {
const t = normalizeOptionalString(e);
if (!t || !/^[a-z0-9]+-[A-Za-z0-9+/=]+$/i.test(t)) return;
return t;
}
/** Returns legacy plugin ids used only for trusted update migrations. */
function resolveOfficialExternalPluginLegacyIds(e) {
return normalizeUniqueTrimmedStringList(getOfficialExternalPluginCatalogManifest(e)?.legacyPluginIds);
}
/** Returns former npm package names accepted only for trusted update migrations. */
function resolveOfficialExternalPluginLegacyNpmPackageNames(e) {
return normalizeUniqueTrimmedStringList(getOfficialExternalPluginCatalogManifest(e)?.legacyNpmPackageNames);
}
/** Returns the host-owned setup migration selected for an external channel cutover. */
function resolveOfficialExternalChannelCompatibilityMigration(e) {
const t = getOfficialExternalPluginCatalogEntry(e);
return normalizeOptionalString(getOfficialExternalPluginCatalogManifest(t ?? {})?.channelHostConfig?.compatibilityMigration);
}
function resolveOfficialExternalPluginLookupIds(e) {
const t = getOfficialExternalPluginCatalogManifest(e);
const n = [t?.plugin?.id, t?.channel?.id];
for (const e of t?.providers ?? []) n.push(e.id, ...e.aliases ?? []);
return normalizeUniqueTrimmedStringList(n);
}
function resolveOfficialExternalPluginLabel(e) {
const t = getOfficialExternalPluginCatalogManifest(e);
return normalizeOptionalString(t?.plugin?.label) ?? normalizeOptionalString(t?.channel?.label) ?? normalizeOptionalString(t?.providers?.[0]?.name) ?? normalizeOptionalString(e.title) ?? normalizeOptionalString(e.name) ?? resolveOfficialExternalPluginId(e) ?? "plugin";
}
function resolveOfficialExternalPluginInstall(e, t) {
const n = normalizeOptionalString(e.state);
const r = normalizeOptionalString(e.publisher?.trust);
if ((n || r) && (n !== "available" || r !== "official")) return null;
const i = getOfficialExternalPluginCatalogManifest(e)?.install;
const a = normalizeOptionalString(i?.clawhubSpec);
const o = normalizeOptionalString(i?.npmSpec);
const s = normalizeOptionalString(i?.localPath);
const c = resolveFeedEntryInstallCandidate({
entry: e,
catalogConfig: t?.catalogConfig
});
if (c) return {
...c,
...i?.minHostVersion ? { minHostVersion: i.minHostVersion } : {},
...i?.allowInvalidConfigRecovery === true ? { allowInvalidConfigRecovery: true } : {}
};
const l = getFeedEntryInstallCandidateRecords(e).length > 0;
const u = o ?? (l || a ? void 0 : normalizeOptionalString(e.name));
const d = normalizePluginInstallDefaultChoice(i?.defaultChoice) ?? (u ? "npm" : a ? "clawhub" : s ? "local" : void 0);
if (!a && !u && !s) return null;
return {
...a ? { clawhubSpec: a } : {},
...u ? { npmSpec: u } : {},
...s ? { localPath: s } : {},
...d ? { defaultChoice: d } : {},
...i?.minHostVersion ? { minHostVersion: i.minHostVersion } : {},
...i?.expectedIntegrity ? { expectedIntegrity: i.expectedIntegrity } : {},
...i?.allowInvalidConfigRecovery === true ? { allowInvalidConfigRecovery: true } : {}
};
}
async function loadConfiguredHostedOfficialExternalPluginCatalogEntries(e) {
const { loadHostedOfficialExternalPluginCatalogEntries: t } = await import("./official-external-plugin-catalog-hosted-BzHP8COA.mjs");
return await t(e);
}
function listOfficialExternalPluginCatalogEntries() {
return [...getBundledOfficialExternalPluginCatalog().entries];
}
/** Returns whether an id is the canonical id of an official external plugin. */
function isOfficialExternalPluginId(e) {
const t = normalizeOptionalString(e)?.toLowerCase();
if (!t) return false;
return getBundledOfficialExternalPluginCatalog().canonicalIds.has(t);
}
function resolveOfficialExternalPluginOwners(e) {
const t = /* @__PURE__ */ new Set();
for (const n of getBundledOfficialExternalPluginCatalog().entries) {
const r = resolveOfficialExternalPluginId(n);
const i = getOfficialExternalPluginCatalogManifest(n);
if (r && i && e(i)) t.add(r);
}
return [...t].toSorted((e, t) => e.localeCompare(t));
}
function normalizedProviderIdSet(e) {
return new Set(e.map(normalizeOptionalLowercaseString).filter((e) => Boolean(e)));
}
/** Resolves official external plugin owners for configured capability provider ids. */
function resolveOfficialExternalProviderContractPluginIds(e) {
const t = normalizedProviderIdSet([...e.providerIds]);
if (t.size === 0) return [];
return resolveOfficialExternalPluginOwners((n) => n.contracts?.[e.contract]?.some((e) => {
const n = normalizeOptionalString(e)?.toLowerCase();
return n ? t.has(n) : false;
}));
}
/** Resolves official web provider owners from matching documented environment credentials. */
function resolveOfficialExternalWebProviderContractPluginIdsForEnv(e) {
return resolveOfficialExternalPluginOwners((t) => {
const n = normalizedProviderIdSet(t.contracts?.webFetchProviders ?? []);
return n.size > 0 && t.webSearchProviders?.some((t) => {
const r = normalizeOptionalString(t.id)?.toLowerCase();
return r !== void 0 && n.has(r) && t.envVars?.some((t) => Boolean(e.env[t]?.trim()));
});
});
}
/** Resolves official external plugin owners for configured model provider ids. */
function resolveOfficialExternalProviderPluginIds(e) {
const t = normalizedProviderIdSet([...e.providerIds]);
if (t.size === 0) return [];
return resolveOfficialExternalPluginOwners((e) => e.providers?.some((e) => [e.id, ...e.aliases ?? []].some((e) => {
const n = normalizeOptionalString(e)?.toLowerCase();
return n ? t.has(n) : false;
})));
}
/** Resolves official external provider owners with configured environment credentials. */
function resolveOfficialExternalProviderPluginIdsForEnv(e) {
return resolveOfficialExternalPluginOwners((t) => t.providers?.some((t) => t.envVars?.some((t) => Boolean(e[t]?.trim()))));
}
function listOfficialExternalChannelCatalogEntries() {
return listOfficialExternalPluginCatalogEntries().filter((e) => Boolean(getOfficialExternalPluginCatalogManifest(e)?.channel));
}
function listOfficialExternalChannelEnvVars() {
return listOfficialExternalChannelCatalogEntries().flatMap((e) => {
const t = getOfficialExternalPluginCatalogManifest(e)?.channel;
const n = normalizeOptionalString(t?.id)?.toLowerCase();
const r = normalizeUniqueTrimmedStringList([
...t?.envVars ?? [],
...t?.configuredState?.env?.allOf ?? [],
...t?.configuredState?.env?.anyOf ?? []
]);
return n && r.length > 0 ? [{
channelId: n,
envVars: r
}] : [];
});
}
const CHANNEL_SECRET_FIELD_PATTERN = /^[A-Za-z][A-Za-z0-9]*$/;
const CHANNEL_SECRET_ENV_PATTERN = /^[A-Z][A-Z0-9_]*$/;
function findOfficialExternalChannelManifest(e) {
const t = listOfficialExternalChannelCatalogEntries().find((t) => normalizeOptionalLowercaseString(getOfficialExternalPluginCatalogManifest(t)?.channel?.id) === e);
return getOfficialExternalPluginCatalogManifest(t ?? {});
}
/** Returns a validated host fallback secret contract for one external channel. */
function getOfficialExternalChannelSecretContract(e) {
const t = normalizeOptionalString(e)?.toLowerCase();
if (!t) return;
const n = findOfficialExternalChannelManifest(t)?.channelSecrets?.fields;
if (!n) return;
const r = n.flatMap((e) => {
const t = normalizeOptionalString(e.field);
const n = normalizeOptionalString(e.activationField);
const r = normalizeOptionalString(e.activationEnv);
if (!t || !CHANNEL_SECRET_FIELD_PATTERN.test(t) || n !== void 0 && !CHANNEL_SECRET_FIELD_PATTERN.test(n) || r !== void 0 && !CHANNEL_SECRET_ENV_PATTERN.test(r)) return [];
return [{
field: t,
...n ? { activationField: n } : {},
...r ? { activationEnv: r } : {}
}];
});
return r.length > 0 ? {
channelId: t,
fields: r
} : void 0;
}
/** Returns trusted host validation clauses for one official external channel. */
function getOfficialExternalChannelHostSchemaAllOf(e) {
const t = normalizeOptionalString(e)?.toLowerCase();
if (!t) return [];
const n = findOfficialExternalChannelManifest(t)?.channelHostConfig?.schemaAllOf;
return Array.isArray(n) ? n.filter(isRecord) : [];
}
function listOfficialExternalProviderCatalogEntries() {
return listOfficialExternalPluginCatalogEntries().filter((e) => (getOfficialExternalPluginCatalogManifest(e)?.providers?.length ?? 0) > 0);
}
function getOfficialExternalPluginCatalogEntry(e) {
const t = e.trim();
if (!t) return;
return getBundledOfficialExternalPluginCatalog().byId.get(t);
}
function getOfficialExternalPluginCatalogEntryForPackage(e) {
const t = e?.trim();
if (!t) return;
return getBundledOfficialExternalPluginCatalog().byPackage.get(t);
}
/** Source discovery alone does not make an external package part of the core distribution. */
function isExternallyDistributedPlugin(e) {
if (e.packageBuild?.bundledDist === true) return false;
const t = getOfficialExternalPluginCatalogEntryForPackage(e.packageName);
return e.packageBuild?.bundledDist === false || t !== void 0 && resolveOfficialExternalPluginId(t) === e.pluginId && getOfficialExternalPluginCatalogManifest(t)?.build?.bundledDist !== true;
}
//#endregion
export { resolvePluginInstallSources as A, NpmChannelResolutionError as C, resolveClawHubInstallSpecsForUpdateChannel as D, isUnavailablePluginSource as E, parseClawHubPluginSpec as M, resolveDefaultNpmSpec as O, resolveOfficialExternalWebProviderContractPluginIdsForEnv as S, installWithSourceFallback as T, resolveOfficialExternalPluginLegacyNpmPackageNames as _, isExternallyDistributedPlugin as a, resolveOfficialExternalProviderPluginIds as b, listOfficialExternalChannelEnvVars as c, loadConfiguredHostedOfficialExternalPluginCatalogEntries as d, resolveOfficialExternalChannelCompatibilityMigration as f, resolveOfficialExternalPluginLegacyIds as g, resolveOfficialExternalPluginLabel as h, getOfficialExternalPluginCatalogEntryForPackage as i, normalizePluginInstallDefaultChoice as j, resolveNpmInstallSpecsForUpdateChannel as k, listOfficialExternalPluginCatalogEntries as l, resolveOfficialExternalPluginInstallSources as m, getOfficialExternalChannelSecretContract as n, isOfficialExternalPluginId as o, resolveOfficialExternalPluginInstall as p, getOfficialExternalPluginCatalogEntry as r, listOfficialExternalChannelCatalogEntries as s, getOfficialExternalChannelHostSchemaAllOf as t, listOfficialExternalProviderCatalogEntries as u, resolveOfficialExternalPluginLookupIds as v, installWithChannelFallback as w, resolveOfficialExternalProviderPluginIdsForEnv as x, resolveOfficialExternalProviderContractPluginIds as y };