openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 7.15 kB
JavaScript
import{l as isRecord}from"./record-coerce-BTbqdJ3-.mjs";import{i as copyConfigResolutionFacts}from"./resolution-facts-DYKtoyes.mjs";import{t as captureRuntimeConfigWithSource}from"./runtime-config-capture-state-B3_P0bBA.mjs";import{n as normalizeAgentId}from"./agent-id-cOfqmfln.mjs";import"./session-key-BPgKfU2m.mjs";import{r as normalizeProviderId}from"./provider-id-A-8WTtLA.mjs";import{h as SYSTEM_AGENT_ID,n as listAgentEntries}from"./agent-roster-4XrgUgPK.mjs";import{E as toAgentEntriesRecord,m as resolveAmbientOwnerAgentId}from"./agent-scope-config-CG9CFpGX.mjs";import{n as createRuntimeConfigReader}from"./runtime-snapshot-Bg5K4JNu.mjs";import{s as resolveAgentModelPrimaryValue}from"./model-input-CJnF-FEy.mjs";import{n as projectConfigOntoRuntimeSourceSnapshot}from"./runtime-source-projection-DvPkX_nq.mjs";import{i as resolveConfiguredSetupModelForAgent}from"./utility-model-CDT0xFeg.mjs";import{n as cliBackendAcceptsAuthProfileForwarding,r as resolveCliExecutionAuthProfileId}from"./cli-execution-auth-C5lp_Ebz.mjs";import{isDeepStrictEqual}from"node:util";
//!src/system-agent/inference-route.ts
function projectSystemAgentExecutionConfig(e,t,n){const r=listAgentEntries(e);const i=r.find(e=>normalizeAgentId(e.id)===t);const a=[...r.filter(e=>normalizeAgentId(e.id)!==SYSTEM_AGENT_ID),...n&&!i&&t!==`openclaw`?[{id:t}]:[],{id:SYSTEM_AGENT_ID,...i?.params!==void 0?{params:structuredClone(i.params)}:{},...i?.tools!==void 0?{tools:structuredClone(i.tools)}:{}}];const o={...e,agents:{...e.agents,entries:toAgentEntriesRecord(a)}};copyConfigResolutionFacts(e,o);return captureRuntimeConfigWithSource(o,projectConfigOntoRuntimeSourceSnapshot(e))}async function resolveSystemAgentConfiguredRouteFromConfig(e,t,n={},r){const i=r&&e===(r.runtimeConfig??r.config)?r.sourceConfig:e;const a=createRuntimeConfigReader(i)();const o=a===i?e:a;const[s,c,l,u]=await Promise.all([import(`./model-selection-B0xBo4V2.mjs`),import(`./model-runtime-aliases-D6wnAoGn.mjs`),import(`./simple-completion-selection-D6YU9lfh.mjs`),import(`./policy-Chea1vPc.mjs`)]);const d=resolveAmbientOwnerAgentId(e,t);const f=resolveConfiguredSetupModelForAgent({cfg:e,agentId:d,modelTarget:n.modelTarget});if(!f)return null;const p=l.resolveSimpleCompletionSelectionForAgent({cfg:e,agentId:d,modelRef:f.implicitPrimary?void 0:f.modelRef,manifestPlugins:n.pluginMetadataPlugins});if(!p)return null;const m=n.pluginMetadataPlugins?{plugins:n.pluginMetadataPlugins}:void 0;const h=c.resolveCliRuntimeExecutionProvider({provider:p.provider,cfg:e,agentId:d,modelId:p.modelId,...p.profileId?{authProfileId:p.profileId}:{},...m?{metadataSnapshot:m}:{}})??p.runtimeProvider??p.provider;const g=s.isCliProvider(h,e);const _=g&&cliBackendAcceptsAuthProfileForwarding({provider:h,config:e,agentId:d});const v=_?await resolveCliExecutionAuthProfileId({cliExecutionProvider:h,authProfileProvider:p.provider,config:e,agentDir:p.agentDir,...p.profileId?{selected:{authProfileId:p.profileId,authProfileIdSource:`user`}}:{}}):void 0;const y=_?v:p.profileId;const b=projectSystemAgentExecutionConfig(o,d,n.modelTarget===`primary`);const x=s.resolveConfiguredRouteModelLabel({configuredRef:f.modelRef,resolved:{provider:p.provider,model:p.modelId},...p.profileId?{profileId:p.profileId}:{}});const S={...f.modelTarget?{modelTarget:f.modelTarget}:{},sourceConfig:e,runConfig:b,modelLabel:x,provider:h,model:p.modelId,agentDir:p.agentDir,agentId:d,...y?{authProfileId:y}:{}};if(g)return{runner:`cli`,...S};const C=u.resolveAgentHarnessPolicy({config:e,agentId:d,provider:p.provider,modelId:p.modelId});return{runner:`embedded`,...C.runtimeSource===`implicit`?{}:{agentHarnessRuntimeOverride:C.runtime},...S}}function projectRelevantModelMap(e){if(!e.models)return;const t=Object.fromEntries(Object.entries(e.models).filter(([t,n])=>{const r=t.indexOf(`/`);const i=r>0?normalizeProviderId(t.slice(0,r)):``;const a=r>0?t.slice(r+1):t;return e.providerIds.has(i)&&(a===e.modelId||a===`*`||t===e.rawModel)||n.alias?.trim()===e.rawModel}));return Object.keys(t).length>0?t:void 0}async function projectInferenceRoute(e,t,n={},r=e){const{resolveProviderIdForAuth:i}=await import(`./provider-auth-aliases-DG29l98l.mjs`);const a=resolveAmbientOwnerAgentId(e,t);const o=await resolveSystemAgentConfiguredRouteFromConfig(e,a,n);const s=listAgentEntries(e).find(e=>normalizeAgentId(e.id)===a);const c=listAgentEntries(o?.runConfig??{}).find(e=>normalizeAgentId(e.id)===SYSTEM_AGENT_ID);const l=e.agents?.defaults;const u=normalizeProviderId(o?.modelLabel.split(`/`,1)[0]??``);const d=new Set([u,normalizeProviderId(o?.provider??``)].filter(Boolean));const f=n.pluginMetadataPlugins?{plugins:n.pluginMetadataPlugins}:void 0;const p={config:e,...f?{metadataSnapshot:f}:{}};const m=new Set([...d].map(e=>i(e,p)));const h=Object.fromEntries(Object.entries(e.auth?.profiles??{}).filter(([,e])=>m.has(i(e.provider,{...p,storedCredential:true}))));const g=Object.fromEntries(Object.entries(e.auth?.order??{}).filter(([e])=>m.has(i(e,p))));const _=Object.fromEntries(Object.entries(e.models?.providers??{}).filter(([e])=>d.has(normalizeProviderId(e))).map(([e,t])=>[e,structuredClone(t)]));const v=o?.modelTarget===`utility`?resolveAgentModelPrimaryValue(s?.utilityModel)??resolveAgentModelPrimaryValue(l?.utilityModel):typeof s?.model===`string`?s.model:s?.model?.primary||(typeof l?.model===`string`?l.model:l?.model?.primary);const y=s?{model:structuredClone(s.model),...o?.modelTarget===`utility`?{utilityModel:structuredClone(s.utilityModel)}:{},params:structuredClone(s.params),tools:structuredClone(s.tools),models:projectRelevantModelMap({models:s.models,providerIds:d,modelId:o?.model,rawModel:v})}:void 0;const b=y!==void 0&&Object.values(y).some(e=>e!==void 0);let x=null;if(o){const{runConfig:e,sourceConfig:t,...n}=o;x=n}return{route:x,defaultSelection:{explicitIds:[a]},auth:{profiles:h,order:g},models:{mode:e.models?.mode,providers:_},defaults:{model:structuredClone(l?.model),...o?.modelTarget===`utility`?{utilityModel:structuredClone(l?.utilityModel)}:{},params:structuredClone(l?.params),models:projectRelevantModelMap({models:l?.models,providerIds:d,modelId:o?.model,rawModel:v})},...s&&b?{agent:{id:normalizeAgentId(s.id),...y}}:{},...c?{executionAgent:{id:SYSTEM_AGENT_ID,params:structuredClone(c.params),tools:structuredClone(c.tools)}}:{},env:structuredClone(e.env),secrets:structuredClone(e.secrets),plugins:structuredClone(r.plugins),tools:structuredClone(e.tools)}}function withoutAgentIdentity(e){const t=isRecord(e.agent)?{...e.agent,id:`<agent>`}:e.agent;return{...e,route:e.route?{...e.route,agentId:`<agent>`,agentDir:`<agent-dir>`}:null,defaultSelection:{explicitIds:[]},...t?{agent:t}:{}}}function sameSetupInferenceRoute(e,t,n){return n?isDeepStrictEqual(withoutAgentIdentity(e),withoutAgentIdentity(t)):isDeepStrictEqual(e,t)}function sameSetupConfiguredRoute(e,t,n){if(!n)return isDeepStrictEqual(e,t);const normalize=e=>e?{...e,agentId:`<agent>`,agentDir:`<agent-dir>`}:null;return isDeepStrictEqual(normalize(e),normalize(t))}export{sameSetupInferenceRoute as i,resolveSystemAgentConfiguredRouteFromConfig as n,sameSetupConfiguredRoute as r,projectInferenceRoute as t};