openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 7.97 kB
JavaScript
import{d as withPluginRuntimeRegistryScope}from"./gateway-request-scope-7deHmsb_.mjs";import{r as normalizeProviderId}from"./provider-id-A-8WTtLA.mjs";import{_ as resolveDefaultAgentId,b as resolveNativeModelPrimary,f as resolveAgentWorkspaceDir}from"./agent-scope-config-CG9CFpGX.mjs";import{t as resolveDefaultAgentWorkspaceDir}from"./workspace-default-path-ULYbn-EL.mjs";import{t as dedupeByKey}from"./dedupe-by-key-Bdyq9B5M.mjs";import{i as DEFAULT_PROVIDER}from"./defaults-k51tX6yK.mjs";import"./agent-scope-Dw5XRSEh.mjs";import{l as getActivePluginRegistry}from"./runtime-Cl6PvJHC.mjs";import{n as withPluginRuntimeGenerationScope}from"./generation-scope-CxHnQlew.mjs";import{a as normalizeOptionalAgentRuntimeId,r as isDefaultAgentRuntimeId}from"./agent-runtime-id-dWmvEQWJ.mjs";import{r as buildConfiguredModelCatalog,y as resolveModelRefFromString}from"./model-selection-shared-CIOFxJ36.mjs";import{n as enrichHarnessRows,r as modelCatalogRouteVariantKey}from"./model-catalog-entry-Cq7wJl_w.mjs";import{a as resolveModelCatalogIdentityKey,t as createModelCatalogIdentityKeyResolver}from"./openai-model-routes-DFmM1mZU.mjs";import{n as getRegisteredAgentHarness}from"./registry-BzjsVuM_.mjs";import{t as resolveAgentHarnessPolicy}from"./policy-B5H6zqjn.mjs";import{t as copyProviderCatalogOutcomes}from"./provider-catalog-result-rf43WLKm.mjs";import{n as resolveConfiguredModelEntries}from"./configured-model-entries-CeK5vxAX.mjs";import{n as collectPreparedModelRuntimeConfiguredRefs}from"./prepared-model-runtime.configured-BFM-g_AK.mjs";import{isDeepStrictEqual}from"node:util";
//!src/agents/harness/model-catalog.ts
function isCatalogRowList(e){return Array.isArray(e)}function replaceRuntimeScope(e,t,n,r){const i=e&&Object.hasOwn(e,t)?e[t]??[]:[];const a=[...i.filter(r),...n];if(isDeepStrictEqual(i,a))return e;const o={...e,[t]:a};if(!a.length)delete o[t];return o}async function augmentModelCatalogWithAgentHarness(e){const t=e.preparedSnapshot??e.snapshot;const n=e.normalizeProvider??normalizeProviderId;const r=/* @__PURE__ */ new Map;const addRuntime=(e,t)=>{const n=normalizeOptionalAgentRuntimeId(e);if(!n||isDefaultAgentRuntimeId(n)||n===`openclaw`)return;const i=r.get(n)??/* @__PURE__ */ new Set;i.add(t);r.set(n,i)};const i=e.defaultModel?.trim();const a=e.nativeSelection?{provider:e.nativeSelection.provider,model:e.nativeSelection.modelId}:i?resolveModelRefFromString({cfg:e.cfg,raw:i,defaultProvider:e.defaultProvider,allowManifestNormalization:true,allowPluginNormalization:true})?.ref:void 0;let o;if(a){const n=createModelCatalogIdentityKeyResolver();const r=n({provider:a.provider,id:a.model});const i=[...t.entries,...t.staticEntries??[]].find(e=>n(e)===r);o=e.nativeSelection?.runtime??e.agentRuntime??resolveAgentHarnessPolicy({provider:a.provider,modelId:a.model,modelApi:i?.api,modelBaseUrl:i?.baseUrl,config:e.cfg,agentId:e.agentId}).runtime;addRuntime(o,a.provider)}if(e.includePickerRuntimes)for(const t of resolveConfiguredModelEntries({cfg:e.cfg,agentId:e.agentId,defaultProvider:e.defaultProvider,defaultModel:e.defaultModel}).entries)for(const e of t.pickerRuntimes??[])addRuntime(e,t.ref.provider);const s=e.observationConfig?e.pluginRegistry:e.pluginRegistry??getActivePluginRegistry();if(!s)return e.snapshot;if(e.includePickerRuntimes){for(const{harness:e}of s.agentHarnesses)if(e.loadModelCatalog&&!r.has(e.id))r.set(e.id,/* @__PURE__ */ new Set)}if(r.size===0)return e.snapshot;let c;try{c=collectPreparedModelRuntimeConfiguredRefs(e.cfg,e.agentId).flatMap(({value:t})=>{const n=resolveModelRefFromString({cfg:e.cfg,agentId:e.agentId,raw:t,defaultProvider:e.defaultProvider,allowManifestNormalization:true,allowPluginNormalization:true})?.ref;return n?[n]:[]})}catch(t){e.onError?.(t);return e.snapshot}let l=e.snapshot;const u=[];let d=false;for(const[i,a]of r){const r=[...a].filter(t=>!e.includesProvider||e.includesProvider(t));if(a.size>0&&r.length===0)continue;const f=withPluginRuntimeRegistryScope(s,()=>getRegisteredAgentHarness(i)?.harness);if(!f?.loadModelCatalog)continue;for(const t of r)e.onDiscoveryStarted?.(t);const p=e.includesProvider;const outsideScope=({provider:e})=>p!==void 0&&!p(e);let m;let h=[];try{const t=await f.loadModelCatalog({config:e.observationConfig??e.cfg,agentId:e.agentId,agentDir:e.agentDir,workspaceDir:e.workspaceDir,configuredModelRefs:c});if(isCatalogRowList(t))m=t;else{m=t.entries;h=copyProviderCatalogOutcomes(t);for(const e of h)e.provider=n(e.provider)}}catch(t){const a=new Set([...r,...[...l.entries,...l.routeVariants].filter(e=>e.nativeRuntime===i&&!outsideScope(e)).map(e=>e.provider)].map(n));if(l===e.snapshot)l={...e.snapshot};l.nativeProviderOutcomes=replaceRuntimeScope(l.nativeProviderOutcomes,i,[...a].map(e=>({provider:e,status:`unavailable`})),outsideScope);e.onError?.(t,r);continue}const g=p?m.filter(e=>p(e.provider)):m;const _=replaceRuntimeScope(l.nativeProviderOutcomes,i,h.filter(e=>!outsideScope(e)),outsideScope);const v=new Set(h.filter(e=>e.status!==`ready`).map(({provider:e})=>e));const y=replaceRuntimeScope(l.nativeHostRows,i,g.filter(e=>!e.nativeRuntime&&!v.has(n(e.provider))),e=>outsideScope(e)||v.has(n(e.provider)));if(_!==l.nativeProviderOutcomes||y!==l.nativeHostRows){if(l===e.snapshot)l={...e.snapshot};l.nativeProviderOutcomes=_;l.nativeHostRows=y}u.push(...g);d=true;const b=enrichHarnessRows(g,t);const x=createModelCatalogIdentityKeyResolver();const S=/* @__PURE__ */ new Set([...c.map(({provider:e,model:t})=>x({provider:e,id:t})),...buildConfiguredModelCatalog({cfg:e.cfg,workspaceDir:e.workspaceDir}).map(x)]);const retain=e=>e.nativeRuntime!==i||v.has(n(e.provider))||S.has(resolveModelCatalogIdentityKey(e))||p!==void 0&&!p(e.provider);const C=l.entries.filter(retain);const w=l.routeVariants.filter(retain);if(b.length===0&&C.length===l.entries.length&&w.length===l.routeVariants.length)continue;if(l===e.snapshot)l={...e.snapshot};l.entries=dedupeByKey(i===o?[...b,...C]:[...C,...b],createModelCatalogIdentityKeyResolver());const T=createModelCatalogIdentityKeyResolver();l.routeVariants=dedupeByKey([...b,...w],e=>modelCatalogRouteVariantKey(e,T(e)))}if(d)e.onDiscoveryCompleted?.(u);return l}function preparedHarnessCatalogScope(e){const t=e.agentId??resolveDefaultAgentId(e.config);return{config:e.config,agentId:t,agentDir:e.agentDir,workspaceDir:e.workspaceDir??resolveAgentWorkspaceDir(e.config,t)??resolveDefaultAgentWorkspaceDir()}}function isPreparedNativeModelCatalogReady(e){const{selection:t,snapshot:n,pluginGeneration:r}=e;if(!t&&e.catalogAcquired!==true)return false;if(!t&&!n.routeVariants.some(e=>e.nativeRuntime))return true;let i;const readReady=t=>{const n=r.pluginRegistry?.agentHarnesses.find(e=>e.harness.id===t.nativeRuntime)?.harness;return!n?.readModelCatalogReadiness||n.readModelCatalogReadiness({...i??=preparedHarnessCatalogScope(e.input),provider:t.provider,modelId:t.id})!==void 0};return withPluginRuntimeGenerationScope({metadataSnapshot:r.pluginMetadataSnapshot,pluginRegistry:r.pluginRegistry},()=>{if(t){const matches=e=>e.provider===t.provider&&e.id===t.modelId&&e.nativeRuntime===t.runtime;const e=n.entries.find(matches)??n.routeVariants.find(matches);return e!==void 0&&readReady(e)}return n.routeVariants.every(e=>!e.nativeRuntime||n.nativeProviderOutcomes&&Object.hasOwn(n.nativeProviderOutcomes,e.nativeRuntime)&&n.nativeProviderOutcomes[e.nativeRuntime]?.some(t=>t.provider===e.provider&&t.status!==`ready`)||readReady(e))})}function augmentPreparedModelCatalogWithAgentHarness(e){const{input:t,...n}=e;const{config:r,agentId:i,agentDir:a,workspaceDir:o}=preparedHarnessCatalogScope(t);return augmentModelCatalogWithAgentHarness({...n,cfg:r,agentId:i,agentDir:a,workspaceDir:o,defaultProvider:DEFAULT_PROVIDER,defaultModel:resolveNativeModelPrimary(t.config,i),includePickerRuntimes:e.nativeSelection===void 0,observationConfig:t.config})}export{augmentPreparedModelCatalogWithAgentHarness as n,isPreparedNativeModelCatalogReady as r,augmentModelCatalogWithAgentHarness as t};