openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
205 lines (204 loc) • 8.42 kB
JavaScript
import { c as normalizeOptionalLowercaseString } from "./string-coerce-CCIiB4H7.mjs";
import { r as normalizeProviderId } from "./provider-id-b5XGSf9C.mjs";
import { i as parseModelCatalogRef } from "./model-catalog-refs-Bk2MNoCv.mjs";
import { a as resolveAgentDir } from "./agent-scope-config-DmXgFGKP.mjs";
import { i as resolveProviderIdForAuth } from "./provider-auth-aliases-CgGlzANu.mjs";
import { a as listCliRuntimeProviderIds, c as resolveCliRuntimeCanonicalProvider, i as listCliRuntimeModelBackendBindings, l as resolveCliRuntimeModelBackendBinding, r as isCliRuntimeModelBackendForProvider, u as resolveCliRuntimeProfileAlias } from "./cli-backends-B8PrIwPC.mjs";
import { a as normalizeOptionalAgentRuntimeId } from "./agent-runtime-id-D8s1CaYB.mjs";
import { r as resolveProviderModelCatalogId } from "./provider-model-routes-CbP035Gi.mjs";
import { r as resolveModelRuntimePolicy } from "./model-runtime-policy-Cfudvy7O.mjs";
import { o as getPreparedRuntimeAuthProfileStoreSnapshotCore } from "./runtime-snapshots-1lyYKb7Y.mjs";
import { m as resolveExplicitAuthOrderSelection } from "./order-jOvee2J6.mjs";
import { a as resolveLegacyInheritedAuthDir } from "./legacy-inherited-auth-dir-1Y-Cs3kS.mjs";
//#region src/agents/model-runtime-aliases.ts
/**
* Resolves CLI runtime aliases to provider/model auth labels and execution ids.
*/
const RETIRED_MODEL_PICKER_PROVIDERS = /* @__PURE__ */ new Set(["codex", "codex-cli"]);
/** True for retired provider ids that should stay out of model selection surfaces. */
function isRetiredModelPickerProvider(e) {
return RETIRED_MODEL_PICKER_PROVIDERS.has(normalizeProviderId(e));
}
/** Creates a provider visibility predicate for model picker rendering. */
function createModelPickerVisibleProviderPredicate(e = {}) {
const t = new Set(listCliRuntimeProviderIds({
config: e.config,
env: e.env,
includeSetupRegistry: e.includeSetupRegistry ?? false
}));
return (e) => {
const n = normalizeProviderId(e);
return !isRetiredModelPickerProvider(n) && !t.has(n);
};
}
/** True for CLI runtime provider ids such as `claude-cli` and `google-gemini-cli`. */
function isCliRuntimeProvider(e, t = {}) {
return resolveCliRuntimeCanonicalProvider({
runtime: e,
config: t.config,
includeSetupRegistry: t.config !== void 0
}) !== void 0;
}
function isCliRuntimeAlias(e) {
const t = normalizeProviderId(e ?? "");
return t ? listCliRuntimeModelBackendBindings().some((e) => e.runtime === t && e.provider !== t) : false;
}
function isCliRuntimeAliasForProvider(e) {
return isCliRuntimeModelBackendForProvider({
provider: e.provider,
runtime: e.runtime,
config: e.cfg
});
}
function canonicalizeRuntimeAliasProvider(e, t = {}) {
return resolveCliRuntimeCanonicalProvider({
runtime: e,
config: t.config,
includeSetupRegistry: t.config !== void 0
}) ?? e;
}
function normalizeRuntimeModelRefForComparison(e, t = {}) {
const n = e.trim();
const r = parseModelCatalogRef(n);
if (!r) return normalizeProviderId(canonicalizeRuntimeAliasProvider(n, t));
const i = normalizeProviderId(canonicalizeRuntimeAliasProvider(r.provider, t));
return `${i}/${resolveProviderModelCatalogId({
provider: i,
modelId: r.modelId
}) ?? r.modelId}`;
}
function normalizeRuntimeModelRefWithoutAlias(e) {
const t = e.trim();
const n = parseModelCatalogRef(t);
if (!n) return normalizeProviderId(t);
return `${n.provider}/${n.modelId}`;
}
function areRuntimeModelRefsEquivalent(e, t, n = {}) {
if (normalizeRuntimeModelRefWithoutAlias(e) === normalizeRuntimeModelRefWithoutAlias(t)) return true;
return normalizeRuntimeModelRefForComparison(e, n) === normalizeRuntimeModelRefForComparison(t, n);
}
/**
* Resolves an entry's twin route during owner-scoped preparation; model-id equivalence is plugin
* policy, so reads must not recompute it. A runtime chosen only by a session override has no
* twin: choosing that canonical row can reset to a different configured route.
*/
function resolveCliRuntimeTwinRoute(e, t) {
const n = normalizeProviderId(e.provider);
const r = t.cliRuntimeBindings.find((e) => e.runtime === n && e.provider !== n)?.provider;
const i = r ? n : normalizeOptionalAgentRuntimeId(resolveModelRuntimePolicy({
config: t.config,
agentId: t.agentId,
provider: e.provider,
modelId: e.id
}).policy?.id);
if (!i || !t.cliRuntimeBindings.some((e) => e.runtime === i)) return;
const a = r ?? n;
return {
key: `${a}/${resolveProviderModelCatalogId({
provider: a,
modelId: e.id
}) ?? e.id}\0${i}`,
runtime: i,
alias: r !== void 0
};
}
/**
* Drops CLI runtime rows whose canonical twin is listed on the same runtime, so one model and
* route appear once. An alias stays when a selection policy (the agent's manual policy or a
* Gateway role) allows it but not its canonical twin.
*/
function omitCliRuntimeAliasTwins(e, t) {
const n = /* @__PURE__ */ new Map();
for (const { row: t, twin: r } of e) if (r && !r.alias && t.agentRuntime?.id === r.runtime) n.set(r.key, t);
return e.flatMap(({ row: e, twin: r }) => {
const i = r?.alias ? n.get(r.key) : void 0;
return i && t.every((t) => !t.allows({
provider: e.provider,
model: e.id
}) || t.allows({
provider: i.provider,
model: i.id
})) ? [] : [e];
});
}
function shouldPreferActiveRuntimeAliasAuthLabel(e) {
if (!e.runtimeAliasModelEquivalent) return false;
const t = normalizeOptionalLowercaseString(e.selectedAuthLabel);
const n = normalizeOptionalLowercaseString(e.activeAuthLabel);
if (!n || n === "unknown") return false;
return t === "unknown" || Boolean(t?.startsWith("api-key")) && (n.startsWith("oauth") || n.startsWith("token") || n.startsWith("native"));
}
function resolveRuntimeAuthProvider(e, t, n = false) {
return resolveProviderIdForAuth(e, {
config: t.cfg,
storedCredential: n,
...t.metadataSnapshot ? { metadataSnapshot: t.metadataSnapshot } : {}
});
}
function resolveCliRuntimeFromAuthProfile(e) {
const t = e.cfg?.auth?.profiles ?? {};
const n = e.preparedAuthDirectories?.env ?? process.env;
const r = e.preparedAuthStore ?? getPreparedRuntimeAuthProfileStoreSnapshotCore(e.preparedAuthDirectories?.agentDir ?? (e.agentId ? resolveAgentDir(e.cfg ?? {}, e.agentId) : void 0), resolveLegacyInheritedAuthDir(e.cfg ?? {}, n, () => e.preparedAuthDirectories?.inheritedAuthDir), n);
if (e.authProfileId?.trim()) {
const n = e.authProfileId.trim();
return resolveCliRuntimeProfileAlias({
...e,
provider: e.provider,
profileProvider: (t[n] ?? r?.profiles[n])?.provider
});
}
const i = normalizeProviderId(e.provider);
const a = resolveRuntimeAuthProvider(i, e);
const o = resolveExplicitAuthOrderSelection({
storeOrder: r?.order,
configuredOrder: e.cfg?.auth?.order,
providerKey: i,
providerAuthKey: a
});
for (const n of o.order ?? []) {
const o = t[n] ?? r?.profiles[n];
if (!o?.provider) continue;
if (resolveRuntimeAuthProvider(o.provider, e, true) !== a) continue;
return resolveCliRuntimeProfileAlias({
...e,
provider: i,
profileProvider: o.provider
});
}
if (o.order !== void 0 && (o.order.length === 0 || o.order.some((e) => r?.profiles[e] !== void 0))) return;
const s = Object.entries(t).filter(([, t]) => {
if (!t?.provider) return false;
return resolveRuntimeAuthProvider(t.provider, e) === a;
}).map(([e]) => e);
if (s.length !== 1) return;
const [c] = s;
return c ? resolveCliRuntimeProfileAlias({
...e,
provider: i,
profileProvider: t[c]?.provider
}) : void 0;
}
function resolveCliRuntimeExecutionProvider(e, t) {
const n = normalizeProviderId(e.provider);
const { policy: r, matchedProvider: i } = t ?? resolveModelRuntimePolicy({
config: e.cfg,
provider: n,
modelId: e.modelId,
agentId: e.agentId
});
const a = r?.id?.trim() || void 0;
if (a === "openclaw") return;
if (!a || a === "auto") return resolveCliRuntimeFromAuthProfile({
...e,
provider: n
});
const o = n || normalizeProviderId(i ?? "");
if (!o) return;
return resolveCliRuntimeModelBackendBinding({
config: e.cfg,
provider: o,
runtime: a
})?.runtime;
}
//#endregion
export { isCliRuntimeProvider as a, resolveCliRuntimeExecutionProvider as c, isCliRuntimeAliasForProvider as i, resolveCliRuntimeTwinRoute as l, createModelPickerVisibleProviderPredicate as n, isRetiredModelPickerProvider as o, isCliRuntimeAlias as r, omitCliRuntimeAliasTwins as s, areRuntimeModelRefsEquivalent as t, shouldPreferActiveRuntimeAliasAuthLabel as u };