openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
175 lines (174 loc) • 6.53 kB
JavaScript
import { o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CCIiB4H7.mjs";
import "./src-H3RMXyFL.mjs";
import { r as normalizeProviderId, t as findNormalizedProviderKey } from "./provider-id-b5XGSf9C.mjs";
import { i as getCurrentPluginMetadataSnapshot } from "./current-plugin-metadata-snapshot-H0TP3PFt.mjs";
import { a as normalizeOptionalAgentRuntimeId } from "./agent-runtime-id-D8s1CaYB.mjs";
import { n as isManifestPluginAvailableForControlPlane, s as loadManifestMetadataSnapshot } from "./manifest-contract-eligibility-DoGGge-2.mjs";
import { r as modelKey } from "./provider-registry-index-DhPtwFh_.mjs";
import { t as RUNTIME_MODEL_VISIBILITY_NORMALIZATION } from "./model-visibility-policy-CH4gPfvH.mjs";
import { t as resolveAgentHarnessPolicy } from "./policy-DzaOuofI.mjs";
import "./model-selection-C2A4Wyix.mjs";
import { c as resolveCliRuntimeExecutionProvider } from "./model-runtime-aliases-DZy9Ea2c.mjs";
import { o as resolveEffectiveAgentRuntime } from "./thinking-runtime-BKWl4CUs.mjs";
import { n as resolveModelRuntimeDirective, t as applyModelRuntimeDirective } from "./directive-handling.model-runtime-BKpt-WsQ.mjs";
import { t as workerInferenceMetadata } from "./inference-placement-DTrb-mxJ.mjs";
//#region src/auto-reply/reply/model-runtime-normalization.ts
function normalizeRuntimeChoiceId(e) {
const t = normalizeLowercaseStringOrEmpty(e);
return !t || t === "auto" || t === "default" ? "openclaw" : t;
}
/** Carries the Gateway-owned metadata snapshot through one model-selection run. */
function resolveRuntimeNormalization(e) {
return {
...RUNTIME_MODEL_VISIBILITY_NORMALIZATION,
manifestPlugins: getCurrentPluginMetadataSnapshot({
config: e,
allowWorkspaceScopedSnapshot: true
})
};
}
function findSelectedCatalogEntry(e) {
const t = normalizeProviderId(e.provider);
const n = modelKey(t, e.model);
return e.catalog?.find((n) => normalizeProviderId(n.provider) === t && n.id.trim() === e.model.trim()) ?? e.catalog?.find((e) => modelKey(e.provider, e.id) === n);
}
/** Provider identity comes from authored routes or prepared/plugin metadata, not model inventory. */
function isKnownModelSelectionProvider(e) {
const t = normalizeProviderId(e.provider);
if (findNormalizedProviderKey(e.cfg.models?.providers, t) || e.catalog.some((e) => normalizeProviderId(e.provider) === t)) return true;
const n = loadManifestMetadataSnapshot({ config: e.cfg });
return n.plugins.some((r) => r.providers.some((e) => normalizeProviderId(e) === t) && isManifestPluginAvailableForControlPlane({
snapshot: n,
plugin: r,
config: e.cfg
}));
}
/** Prepare runtime and capabilities for the selected route before any session mutation. */
async function prepareModelSelectionRuntime(e) {
const t = e.profileOverride ? {
...e.sessionEntry,
providerOverride: e.provider,
modelProvider: e.provider,
authProfileOverride: e.profileOverride,
authProfileOverrideSource: "user"
} : e.sessionEntry;
let n = resolveModelRuntimeDirective(e);
if (n.kind === "invalid") return {
status: "rejected",
reason: "invalid-runtime",
message: n.errorText
};
const r = findSelectedCatalogEntry(e);
if (!isKnownModelSelectionProvider(e)) return {
status: "rejected",
reason: "unknown-provider",
message: `Unknown provider "${e.provider}". Use /models to list providers.`
};
let i;
let a;
let o;
let s = n.kind === "set";
const c = {
agentId: e.agentId,
provider: e.provider,
modelId: e.model,
modelApi: r?.api,
modelBaseUrl: r?.baseUrl
};
if (!e.rawRuntime) {
const i = resolveAgentHarnessPolicy({
...c,
config: e.cfg
});
const a = resolveEffectiveAgentRuntime({
...c,
cfg: e.cfg,
sessionEntry: t
});
if (n.kind === "clear" || !t?.agentRuntimeOverride) o = resolveCliRuntimeExecutionProvider({
cfg: e.cfg,
agentId: e.agentId,
provider: e.provider,
modelId: e.model,
authProfileId: t?.authProfileOverride
});
s = Boolean(r?.nativeRuntime || a !== "openclaw" || o || i.forcedByEnvironment && i.runtime !== "openclaw");
}
if (n.kind === "set" && n.runtime === "openclaw" && !t?.authProfileOverride) {
const t = e.cfg.cloudWorkers?.requiredProfile;
const n = t ? e.cfg.cloudWorkers?.profiles?.[t] : void 0;
if (n && workerInferenceMetadata({
providerId: n.provider,
profileSnapshot: n
}).inference) s = false;
}
if (s) {
const { preparePublishedModelRuntimeChoice: r } = await import("./model-runtime-choice-DDfgqzCb.mjs");
const s = await r({
...e,
sessionEntry: t,
runtimeId: n.kind === "set" ? n.runtime : void 0,
preferredRuntimeId: (n.kind === "unchanged" ? normalizeOptionalAgentRuntimeId(t?.agentRuntimeOverride) : void 0) ?? o
});
if (s.kind === "unavailable") return {
status: "rejected",
reason: "invalid-runtime",
message: s.message
};
i = s.validate;
a = s.harness;
n = {
kind: "set",
runtime: s.runtimeId
};
}
const l = { ...t };
applyModelRuntimeDirective(l, n);
const u = n.kind === "set" ? n.runtime : resolveEffectiveAgentRuntime({
cfg: e.cfg,
...c,
sessionEntry: l
});
let d;
const f = e.thinkingPolicyRequired !== false || u !== "openclaw" || !r || r.nativeRuntime !== void 0 && r.nativeRuntime !== "openclaw";
if (e.hydrateThinkingCatalog !== false && e.cfg.plugins?.enabled !== false && f) {
const { loadProviderScopedThinkingCatalog: t } = await import("./agents/model-catalog.runtime.js");
const n = await t({
config: e.cfg,
agentId: e.agentId,
provider: e.provider,
model: e.model,
agentRuntime: u,
workspaceDir: e.workspaceDir
});
d = findSelectedCatalogEntry({
...e,
catalog: n
});
}
return {
status: "ready",
runtime: n,
validateRuntimeSelection: i,
harness: a,
catalog: d ? [d, ...e.catalog.filter((e) => e !== r)] : [...e.catalog]
};
}
function modelCatalogEntryKey(e) {
return JSON.stringify([e.provider.trim(), e.id.trim()]);
}
/** Retain prepared-only models while overlaying matching configured model metadata. */
function mergePreparedConfiguredCatalog(e) {
if (!e.prepared?.length) return e.configured;
const t = new Map(e.configured.map((e) => [modelCatalogEntryKey(e), e]));
for (const n of e.prepared) {
const e = modelCatalogEntryKey(n);
t.set(e, {
...t.get(e),
...n
});
}
return [...t.values()];
}
//#endregion
export { prepareModelSelectionRuntime as a, normalizeRuntimeChoiceId as i, isKnownModelSelectionProvider as n, resolveRuntimeNormalization as o, mergePreparedConfiguredCatalog as r, findSelectedCatalogEntry as t };