openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 9.84 kB
JavaScript
import"./model-ref-shared-T-aCqInj.mjs";import{r as normalizeProviderId}from"./provider-id-A-8WTtLA.mjs";import{g as resolveDefaultAgentDir}from"./agent-scope-config-CG9CFpGX.mjs";import"./agent-scope-Dw5XRSEh.mjs";import{n as modelTransportRoutesMatch}from"./model-compat-catalog-yXiNopuo.mjs";import{n as withPluginRuntimeGenerationScope}from"./generation-scope-CxHnQlew.mjs";import{r as modelKey}from"./provider-registry-index-fv5AHL27.mjs";import{t as findNormalizedProviderValue}from"./model-selection-normalize-DU37Sfvg.mjs";import{d as assertModelSupportsChat}from"./error-oegzzI05.mjs";import"./model-selection-CNHinoVN.mjs";import{n as buildSuppressedBuiltInModelError}from"./model-suppression--37qUzP4.mjs";import{m as providerOwnsDynamicModelPreparation}from"./provider-runtime-B6YUXwCw.mjs";import{a as resolveBundledProviderStaticCatalogModel,o as resolveBundledStaticCatalogModel}from"./model.static-catalog-DJ0BQBuH.mjs";import{a as resolveLegacyInheritedAuthDir}from"./legacy-inherited-auth-dir-9LCjV4lR.mjs";import{s as resolveModelCostConfig}from"./usage-format-u8ZA4MCp.mjs";import{r as resolveModelWorkspaceDir,t as ModelRegistry}from"./model-registry.core-BiwWlhq6.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-D0F2tm5v.mjs";import{t as AuthStorage}from"./auth-storage.core-CJFKhJwL.mjs";import{i as resolveConfiguredProviderConfig,s as mergeModelMediaInput}from"./model.configured-overrides-Dqnw5TO7.mjs";import{A as readCapturedPreparedModelRuntimeCatalog,h as loadPreparedModelRuntimeSnapshot,m as getPreparedModelRuntimeSnapshot}from"./prepared-model-runtime-Bq1P9DhS.mjs";
//!src/agents/embedded-agent-runner/model.ts
async function withResolvedPricing(e,t,n){const r=await resolveModelCostConfig({provider:e.provider,model:e.id,config:t,agentDir:n,runtimeCost:e.cost});return r&&r!==e.cost?{...e,cost:r}:e}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(void 0,i?.selectedModelStores?{provider:c.provider,modelId:c.model}:void 0)??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);const p=s&&readCapturedPreparedModelRuntimeCatalog(s)?.entries.find(e=>matchesSelectedModel(e)&&modelTransportRoutesMatch(e,f??{}));assertModelSupportsChat(p?.inference?.chat??f?.inference?.chat,c);const m=resolveRuntimeHooks(i);let h=false;let g;const getManifestStaticCatalogModel=()=>{if(!h){h=true;g=s?.findConfiguredRuntimeModel(c.provider,c.model)??resolveBundledStaticCatalogModel({provider:c.provider,modelId:c.model,cfg:r,workspaceDir:n,includeRuntimeDiscovery:true,...s?{metadataSnapshot:s.metadataSnapshot}:{}})}return g};const _={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:m,getStaticCatalogModel:getManifestStaticCatalogModel};const v=resolveExplicitModelWithRegistry({..._,preparedInlineProviderModels:r===s?.config?s?.inlineProviderModels:void 0});if(v&&v.kind!==`resolved`){const e=v.kind===`suppressed`?await resolveRuntimePreferredSuppressedModel(_):void 0;i?.assertCurrent?.();if(e){const t=await withResolvedPricing(e,r,a);i?.assertCurrent?.();return{model:t,logicalRef:l,authStorage:u,modelRegistry:d}}return{error:v.error??buildUnknownModelError(_),authStorage:u,modelRegistry:d}}const y=resolveConfiguredProviderConfig(r,c.provider);const b=s?.metadataSnapshot;let x;const resolveStaticCatalogModel=async()=>{if(!i?.allowBundledStaticCatalogFallback)return;return getManifestStaticCatalogModel()??await(x??=resolveBundledProviderStaticCatalogModel({provider:c.provider,modelId:c.model,cfg:r,workspaceDir:n,...b?{metadataSnapshot:b}:{}}))};const resolveStaticCatalogFallbackModel=async()=>{const e=await resolveStaticCatalogModel();i?.assertCurrent?.();if(!e)return;return normalizeConfiguredProviderModel({..._,discoveredModel:e,providerConfig:y,providerMetadataOwners:b?.owners,preferDiscoveredModelMetadata:true,preferDiscoveredTransport:i?.preferBundledStaticCatalogTransport,staticCatalogModel:e})};const resolveDynamicAttempt=async()=>{const e=await resolveDynamicModelAuthProfile(_);i?.assertCurrent?.();const t=i?.deferProviderDynamicModelPreparation?void 0:await m.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:y,...e}});i?.assertCurrent?.();return resolveModelWithPreparedRegistry({..._,preparedAuthProfile:e,...t?{preparedDynamicModel:t}:{},...i?.allowBundledStaticCatalogFallback?{skipConfiguredFallback:true}:{}})};const S=shouldCompareProviderRuntimeResolvedModel(_);let C=v?.kind===`resolved`&&!S?v.model:void 0;C??=await resolveDynamicAttempt();i?.assertCurrent?.();if(!C&&!v&&i?.allowBundledStaticCatalogFallback){C=await resolveStaticCatalogFallbackModel();i?.assertCurrent?.();C??=normalizeConfiguredProviderModel({..._,providerMetadataOwners:b?.owners})}if(C&&i?.allowBundledStaticCatalogFallback){const e=(await resolveStaticCatalogModel())?.mediaInput;i?.assertCurrent?.();const t=C.mediaInput;const n=mergeModelMediaInput(e,t);if(n)C={...C,mediaInput:n}}if(C){const e=await withResolvedPricing(C,r,a);i?.assertCurrent?.();return{model:e,logicalRef:l,authStorage:u,modelRegistry:d}}return{error:buildUnknownModelError(_),...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.`}export{resolveModelAsync as n,createEmptyAgentDiscoveryStores as t};