openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
269 lines (268 loc) • 11 kB
JavaScript
import "./model-ref-shared-DqE2RqZv.mjs";
import { r as normalizeProviderId } from "./provider-id-b5XGSf9C.mjs";
import { g as resolveDefaultAgentDir } from "./agent-scope-config-DmXgFGKP.mjs";
import "./agent-scope-BwB8YH6y.mjs";
import { n as modelTransportRoutesMatch } from "./model-compat-catalog-bqzqqcXc.mjs";
import { n as withPluginRuntimeGenerationScope } from "./generation-scope-Cq7RLFex.mjs";
import { r as modelKey } from "./provider-registry-index-DhPtwFh_.mjs";
import { t as findNormalizedProviderValue } from "./model-selection-normalize-DIZCRGVF.mjs";
import "./model-selection-C2A4Wyix.mjs";
import { a as resolveLegacyInheritedAuthDir } from "./legacy-inherited-auth-dir-1Y-Cs3kS.mjs";
import { h as loadPreparedModelRuntimeSnapshot, j as readCapturedPreparedModelRuntimeCatalog, m as getPreparedModelRuntimeSnapshot } from "./prepared-model-runtime-Bbo9uBdt.mjs";
import { g as providerOwnsDynamicModelPreparation } from "./provider-runtime-BddjXkmv.mjs";
import { i as resolveModelWorkspaceDir, t as ModelRegistry } from "./model-registry.core-DcVMaqxb.mjs";
import { t as AuthStorage } from "./auth-storage.core-C5ytUL3b.mjs";
import { n as buildSuppressedBuiltInModelError } from "./model-suppression-D6ogCgZv.mjs";
import { a as resolveBundledStaticCatalogModel, i as resolveBundledProviderStaticCatalogModel } from "./model.static-catalog-BYAE0w4W.mjs";
import { i as resolveConfiguredProviderConfig, s as mergeModelMediaInput } from "./model.configured-overrides-ACgGOHL7.mjs";
import { a as resolveModelWithPreparedRegistry, c as resolveRuntimeHooks, i as resolveExplicitModelWithRegistry, n as normalizeProviderModelRef, o as resolveRuntimePreferredSuppressedModel, r as resolveDynamicModelAuthProfile, s as shouldCompareProviderRuntimeResolvedModel, t as normalizeConfiguredProviderModel } from "./model.registry-resolution-CV_hSCul.mjs";
//#region src/agents/embedded-agent-runner/model.ts
/** Creates isolated model/auth stores for harnesses that own model discovery themselves. */
function createEmptyAgentDiscoveryStores() {
const e = AuthStorage.inMemory({});
return {
authStorage: e,
modelRegistry: ModelRegistry.inMemory(e)
};
}
async function resolveModelAsync(e, t, n, r, i) {
i?.assertCurrent?.();
const a = n ?? resolveDefaultAgentDir(r ?? {});
const o = resolveModelWorkspaceDir(r, i?.workspaceDir, i?.agentId);
let s = i?.preparedModelRuntime;
if (!s && !i?.skipAgentDiscovery && (!i?.authStorage || !i?.modelRegistry)) {
const e = {
...i?.agentId ? { agentId: i.agentId } : {},
agentDir: a,
config: r ?? {},
inheritedAuthDir: resolveLegacyInheritedAuthDir(r ?? {})
};
s = getPreparedModelRuntimeSnapshot({
...e,
...i?.workspaceDir ? { workspaceDir: i.workspaceDir } : {}
});
if (!s && !i?.workspaceDir && o) s = getPreparedModelRuntimeSnapshot({
...e,
workspaceDir: o
});
s ??= await loadPreparedModelRuntimeSnapshot({
...e,
...o ? { workspaceDir: o } : {}
});
}
const resolve = async () => {
i?.assertCurrent?.();
const n = i?.workspaceDir ?? s?.workspaceDir ?? o;
const c = normalizeProviderModelRef({
provider: e,
modelId: t,
cfg: r,
workspaceDir: n,
modelIdSource: i?.modelIdSource
});
const l = {
provider: c.provider,
model: c.model
};
let { authStorage: u, modelRegistry: d } = i ?? {};
if (!u || !d) {
const e = s?.createStores() ?? createEmptyAgentDiscoveryStores();
u ??= e.authStorage;
d ??= i?.authStorage ? e.modelRegistry.fork(u) : e.modelRegistry;
}
const matchesSelectedModel = (e) => e.provider === c.provider && e.id === c.model;
const f = s?.modelCatalog.entries.find(matchesSelectedModel);
if (((s && readCapturedPreparedModelRuntimeCatalog(s)?.entries.find((e) => matchesSelectedModel(e) && modelTransportRoutesMatch(e, f ?? {})))?.inference?.chat ?? f?.inference?.chat) === false) return {
error: `Model ${c.provider}/${c.model} does not support chat inference`,
authStorage: u,
modelRegistry: d
};
const p = resolveRuntimeHooks(i);
let m = false;
let h;
const getManifestStaticCatalogModel = () => {
if (!m) {
m = true;
h = s?.findConfiguredRuntimeModel(c.provider, c.model) ?? resolveBundledStaticCatalogModel({
provider: c.provider,
modelId: c.model,
cfg: r,
workspaceDir: n,
includeRuntimeDiscovery: true,
...s ? { metadataSnapshot: s.metadataSnapshot } : {}
});
}
return h;
};
const g = {
abortSignal: i?.abortSignal,
assertCurrent: i?.assertCurrent,
provider: c.provider,
modelId: c.model,
modelRegistry: d,
cfg: r,
agentDir: a,
...i?.agentRuntimeId ? { agentRuntimeId: i.agentRuntimeId } : {},
manifestAlias: c.manifestAlias,
workspaceDir: n,
authProfileId: i?.authProfileId,
authProfileMode: i?.authProfileMode,
preferredProfile: i?.preferredProfile,
runtimeHooks: p,
getStaticCatalogModel: getManifestStaticCatalogModel
};
const _ = resolveExplicitModelWithRegistry({
...g,
preparedInlineProviderModels: r === s?.config ? s?.inlineProviderModels : void 0
});
if (_ && _.kind !== "resolved") {
const e = _.kind === "suppressed" ? await resolveRuntimePreferredSuppressedModel(g) : void 0;
i?.assertCurrent?.();
if (e) return {
model: e,
logicalRef: l,
authStorage: u,
modelRegistry: d
};
return {
error: _.error ?? buildUnknownModelError(g),
authStorage: u,
modelRegistry: d
};
}
const v = resolveConfiguredProviderConfig(r, c.provider);
const y = s?.metadataSnapshot;
let b;
const resolveStaticCatalogModel = async () => {
if (!i?.allowBundledStaticCatalogFallback) return;
return getManifestStaticCatalogModel() ?? await (b ??= resolveBundledProviderStaticCatalogModel({
provider: c.provider,
modelId: c.model,
cfg: r,
workspaceDir: n,
...y ? { metadataSnapshot: y } : {}
}));
};
const resolveStaticCatalogFallbackModel = async () => {
const e = await resolveStaticCatalogModel();
i?.assertCurrent?.();
if (!e) return;
return normalizeConfiguredProviderModel({
...g,
discoveredModel: e,
providerConfig: v,
providerMetadataOwners: y?.owners,
preferDiscoveredModelMetadata: true,
preferDiscoveredTransport: i?.preferBundledStaticCatalogTransport,
staticCatalogModel: e
});
};
const resolveDynamicAttempt = async () => {
const e = await resolveDynamicModelAuthProfile(g);
i?.assertCurrent?.();
const t = i?.deferProviderDynamicModelPreparation ? void 0 : await p.prepareProviderDynamicModel({
provider: c.provider,
config: r,
workspaceDir: n,
context: {
config: r,
agentDir: a,
...i?.agentRuntimeId ? { agentRuntimeId: i.agentRuntimeId } : {},
workspaceDir: n,
provider: c.provider,
modelId: c.model,
modelRegistry: d,
providerConfig: v,
...e
}
});
i?.assertCurrent?.();
return resolveModelWithPreparedRegistry({
...g,
preparedAuthProfile: e,
...t ? { preparedDynamicModel: t } : {},
...i?.allowBundledStaticCatalogFallback ? { skipConfiguredFallback: true } : {}
});
};
const x = shouldCompareProviderRuntimeResolvedModel(g);
let S = _?.kind === "resolved" && !x ? _.model : void 0;
S ??= await resolveDynamicAttempt();
i?.assertCurrent?.();
if (!S && !_ && i?.allowBundledStaticCatalogFallback) {
S = await resolveStaticCatalogFallbackModel();
i?.assertCurrent?.();
S ??= normalizeConfiguredProviderModel({
...g,
providerMetadataOwners: y?.owners
});
}
if (S && i?.allowBundledStaticCatalogFallback) {
const e = (await resolveStaticCatalogModel())?.mediaInput;
i?.assertCurrent?.();
const t = S.mediaInput;
const n = mergeModelMediaInput(e, t);
if (n) S = {
...S,
mediaInput: n
};
}
if (S) return {
model: S,
logicalRef: l,
authStorage: u,
modelRegistry: d
};
return {
error: buildUnknownModelError(g),
...i?.deferProviderDynamicModelPreparation && providerOwnsDynamicModelPreparation({
provider: c.provider,
config: r,
workspaceDir: n
}) ? { deferred: "provider-dynamic-model" } : {},
authStorage: u,
modelRegistry: d
};
};
return s ? await withPluginRuntimeGenerationScope(s, resolve) : await resolve();
}
function buildUnknownModelError(e) {
const t = buildSuppressedBuiltInModelError({
provider: e.provider,
id: e.modelId,
...e.cfg ? { config: e.cfg } : {},
...e.workspaceDir ? { workspaceDir: e.workspaceDir } : {}
});
if (t) return t;
const n = `Unknown model: ${e.provider}/${e.modelId}`;
const r = buildMissingProviderModelRegistrationHint(e);
if (r) return `${n}. ${r}`;
const i = (e.runtimeHooks ?? resolveRuntimeHooks()).buildProviderUnknownModelHintWithPlugin({
provider: e.provider,
config: e.cfg,
workspaceDir: e.workspaceDir,
env: process.env,
context: {
config: e.cfg,
agentDir: e.agentDir,
workspaceDir: e.workspaceDir,
env: process.env,
provider: e.provider,
modelId: e.modelId
}
});
return i ? `${n}. ${i}` : `${n}. Run \`openclaw models list --refresh --provider ${e.provider}\` to inspect this provider's model choices, then retry with a model supported by your account.`;
}
function buildMissingProviderModelRegistrationHint(e) {
if (normalizeProviderId(e.provider) === "openai-codex") return `"openai-codex" is a legacy provider ID. Run \`openclaw doctor --fix\` to migrate legacy model and provider config to the current OpenAI format. If the provider has no authenticated profile, run \`openclaw models status\` to check provider auth and re-authenticate if needed. See https://docs.openclaw.ai/concepts/model-providers.`;
const t = e.cfg?.agents?.defaults?.models;
if (!t) return;
const n = modelKey(e.provider, e.modelId);
const r = t[n] ?? t[`${e.provider}/${e.modelId}`];
if (!r) return;
const i = r.agentRuntime?.id;
if (i) return `Found agents.defaults.models["${n}"] bound to the "${i}" agent runtime. Models served by an agent runtime come from that runtime and its linked account, not from models.providers["${e.provider}"].models[] — registering it there will not make it usable. Confirm "${e.modelId}" is still offered by the "${i}" runtime and switch agents.defaults.model.primary to a currently available model (run \`openclaw models list --refresh --provider ${e.provider}\` to list them). See https://docs.openclaw.ai/concepts/model-providers.`;
if (findNormalizedProviderValue(e.cfg?.models?.providers, e.provider)?.models?.some((t) => t.id === e.modelId)) return;
return `Found agents.defaults.models["${n}"], but no matching models.providers["${e.provider}"].models[] entry. Add { "id": "${e.modelId}", "name": "${e.modelId}" } to models.providers["${e.provider}"].models[] to register this provider model. For custom or proxy providers, also set api and baseUrl so requests route to the intended endpoint. See https://docs.openclaw.ai/concepts/model-providers.`;
}
//#endregion
export { resolveModelAsync as n, createEmptyAgentDiscoveryStores as t };