openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
192 lines (191 loc) • 8.92 kB
JavaScript
import { l as normalizeOptionalString, o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CCIiB4H7.mjs";
import { i as getPluginRegistryForContext, o as getPluginRuntimeGatewayRequestScope } from "./gateway-request-scope-BvvDhs5D.mjs";
import { t as PLUGIN_REGISTRY_STATE } from "./runtime-state-key-Cno8k69C.mjs";
import { n as getPluginRuntimeLoadContextState } from "./load-context-state-Cj2HcQlf.mjs";
import { r as normalizeProviderId } from "./provider-id-b5XGSf9C.mjs";
import { a as normalizeStaticProviderModelIdWithPolicies, i as normalizeConfiguredProviderCatalogModelRef, n as normalizeBuiltInProviderModelId, s as stripSelfProviderModelPrefix, t as collectManifestModelIdNormalizationPolicies } from "./provider-model-id-normalization-Yii1-7Le.mjs";
import { n as getCurrentPluginMetadataSnapshotRuntime, r as resolvePluginMetadataSnapshotRuntime } from "./plugin-metadata-snapshot.runtime.js";
import { r as matchesDeclaredProviderOwner } from "./provider-owner-index-6ny9zfJF.mjs";
import { t as getPluginRuntimeGenerationRegistry } from "./generation-state-CbpjEoxA.mjs";
import { r as modelKey, t as getProviderRegistryIndex } from "./provider-registry-index-DhPtwFh_.mjs";
//#region src/plugins/runtime-workspace-state.ts
/** Reads the active plugin registry workspace directory from global runtime state. */
function getActivePluginRegistryWorkspaceDirFromStateCore() {
return globalThis[PLUGIN_REGISTRY_STATE]?.workspaceDir ?? void 0;
}
//#endregion
//#region src/plugins/manifest-model-id-normalization.ts
/** Applies manifest-declared model-id normalization policies to provider model refs. */
function resolveManifestModelIdNormalizationPolicies(e = {}) {
if (e.plugins) return "owners" in e.plugins ? e.plugins.owners.modelIdNormalizationPolicies : collectManifestModelIdNormalizationPolicies(e.plugins);
const t = process.env;
const n = getActivePluginRegistryWorkspaceDirFromStateCore();
const r = getCurrentPluginMetadataSnapshotRuntime({
env: t,
workspaceDir: n,
allowWorkspaceScopedSnapshot: true,
requireDefaultDiscoveryContext: true
});
if (r) return r.owners.modelIdNormalizationPolicies;
const i = resolvePluginMetadataSnapshotRuntime({
config: {},
env: t,
workspaceDir: n,
allowWorkspaceScopedCurrent: true
});
return i ? i.owners.modelIdNormalizationPolicies : /* @__PURE__ */ new Map();
}
//#endregion
//#region src/plugins/provider-registry-selection.ts
/** Selects the current provider registration without reading unrelated runtime fields. */
function findProviderRuntimeRegistrationInRegistry(e) {
const t = getPluginRuntimeGatewayRequestScope();
const n = (t?.pluginRegistry === e.registry ? t.declaredProviderOwners : void 0) ?? getPluginRuntimeLoadContextState(e.registry)?.declaredProviderOwners;
const r = e.isOwnerEligible ?? ((t) => matchesDeclaredProviderOwner(n, e.provider, t));
const i = normalizeLowercaseStringOrEmpty(e.provider);
const a = e.registry.providers;
const o = getProviderRegistryIndex(a);
const eligible = (e) => r(a[e].pluginId);
let s = o.ids.get(i)?.find(eligible);
if (s === void 0) s = (e.ownerRefs.length === 0 ? o.refs.get(i) : [.../* @__PURE__ */ new Set([...o.ids.get(i) ?? [], ...e.ownerRefs.flatMap((e) => o.refs.get(normalizeLowercaseStringOrEmpty(e)) ?? [])])].sort((e, t) => e - t))?.find(eligible);
return s === void 0 ? void 0 : a[s];
}
//#endregion
//#region src/agents/provider-model-normalization.runtime.ts
/** Reads prepared provider hooks without activating plugins during model-reference parsing. */
/** Refines an already statically normalized model id through its provider hook. */
function normalizeProviderModelIdWithRuntime(e) {
const t = getPluginRuntimeGenerationRegistry() ?? getPluginRegistryForContext();
if (!t) return;
const n = findProviderRuntimeRegistrationInRegistry({
registry: t,
provider: e.provider,
ownerRefs: []
});
if (!n || !Object.getOwnPropertyDescriptor(n.provider, "normalizeModelId")?.enumerable) return;
const r = n.provider.normalizeModelId;
if (!r) return;
const i = {
pluginId: n.pluginId,
normalizeModelId: r
};
let a;
const materialize = (e) => {
if (Object.hasOwn(i, e) || a?.has(e) || !Object.isExtensible(i)) return;
if (Object.getOwnPropertyDescriptor(n.provider, e)?.enumerable) Object.defineProperty(i, e, {
value: Reflect.get(n.provider, e),
writable: true,
enumerable: true,
configurable: true
});
};
const ownKeys = () => {
const e = Reflect.ownKeys(n.provider).filter((e) => !a?.has(e) && Object.getOwnPropertyDescriptor(n.provider, e)?.enumerable);
for (const t of e) materialize(t);
return [...e, ...Reflect.ownKeys(i).filter((t) => !e.includes(t))];
};
const o = new Proxy(i, {
get(e, t, n) {
materialize(t);
return Reflect.get(e, t, n);
},
has(e, t) {
materialize(t);
return Reflect.has(e, t);
},
getOwnPropertyDescriptor(e, t) {
materialize(t);
return Reflect.getOwnPropertyDescriptor(e, t);
},
ownKeys,
deleteProperty(e, t) {
if (!Reflect.deleteProperty(e, t)) return false;
(a ??= /* @__PURE__ */ new Set()).add(t);
return true;
},
preventExtensions(e) {
ownKeys();
return Reflect.preventExtensions(e);
}
});
return normalizeOptionalString(o.normalizeModelId(e.context));
}
//#endregion
//#region src/agents/model-ref-shared.ts
/** Normalize a static provider model ID with built-in and optional manifest policy. */
function normalizeStaticProviderModelId(e, t, n = {}) {
const r = normalizeProviderId(e);
if (n.allowManifestNormalization === false) return normalizeBuiltInProviderModelId(r, t);
return normalizeStaticProviderModelIdWithPolicies(r, t, resolveManifestModelIdNormalizationPolicies({ plugins: n.manifestPlugins }));
}
/**
* Captures manifest policies once for repeated static model-id comparisons.
* Lifecycle-prepared callers must not rediscover plugin metadata inside model loops.
*/
function createStaticProviderModelIdNormalizer(e = {}) {
if (e.allowManifestNormalization === false) return (e, t) => normalizeBuiltInProviderModelId(normalizeProviderId(e), t);
if (e.manifestPlugins) {
const t = resolveManifestModelIdNormalizationPolicies({ plugins: e.manifestPlugins });
return (e, n) => normalizeStaticProviderModelIdWithPolicies(normalizeProviderId(e), n, t);
}
return (t, n) => normalizeStaticProviderModelId(t, n, e);
}
/** Normalize a configured catalog model ID for comparisons against provider catalogs. */
function normalizeConfiguredProviderCatalogModelId(e, t, n = {}) {
return normalizeConfiguredProviderCatalogModelRef(normalizeStaticProviderModelId(e, t, n));
}
/** Reuses one manifest-policy view across configured model rows in an operation. */
function createConfiguredProviderCatalogModelIdNormalizer(e = {}) {
let t;
return (n, r) => normalizeConfiguredProviderCatalogModelRef((t ??= createStaticProviderModelIdNormalizer(e))(n, r));
}
function normalizeProviderModelId(e, t, n) {
const r = normalizeStaticProviderModelId(e, stripSelfProviderModelPrefix(e, t), n);
if (n?.allowPluginNormalization === false) return r;
return normalizeProviderModelIdWithRuntime({
provider: e,
context: {
provider: e,
modelId: r
}
}) ?? r;
}
/** Normalize a provider/model pair into a canonical model reference. */
function normalizeModelRef(e, t, n) {
const r = normalizeProviderId(e);
return {
provider: r,
model: normalizeProviderModelId(r, t.trim(), n)
};
}
/** Return the legacy raw key when it differs from the canonical key. */
function legacyModelKey(e, t) {
const n = e.trim();
const r = t.trim();
if (!n || !r) return null;
const i = `${n}/${r}`;
return i === modelKey(n, r) ? null : i;
}
/** Resolve an allowlist entry to a canonical provider/model key. */
function resolveStaticAllowlistModelKey(e, t) {
const n = e.trim();
if (!n) return null;
const r = n.indexOf("/");
const i = r === -1 ? t : n.slice(0, r).trim();
const a = r === -1 ? n : n.slice(r + 1).trim();
if (!i || !a) return null;
const o = normalizeProviderId(i);
return modelKey(o, normalizeStaticProviderModelId(o, a));
}
/** Preserve literal provider/model refs that already include a provider prefix twice. */
function formatLiteralProviderPrefixedModelRef(e, t) {
const n = normalizeProviderId(e);
const r = t.trim();
if (!n || !r) return r;
const i = normalizeLowercaseStringOrEmpty(r);
const a = `${n}/${n}/`;
if (i.startsWith(a)) return r;
return i.startsWith(`${n}/`) ? `${n}/${r}` : r;
}
//#endregion
export { normalizeConfiguredProviderCatalogModelId as a, resolveStaticAllowlistModelKey as c, getActivePluginRegistryWorkspaceDirFromStateCore as d, legacyModelKey as i, normalizeProviderModelIdWithRuntime as l, createStaticProviderModelIdNormalizer as n, normalizeModelRef as o, formatLiteralProviderPrefixedModelRef as r, normalizeStaticProviderModelId as s, createConfiguredProviderCatalogModelIdNormalizer as t, findProviderRuntimeRegistrationInRegistry as u };