openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
4 lines • 5.41 kB
JavaScript
import{o as normalizeLowercaseStringOrEmpty}from"./string-coerce-CpTFcSwY.mjs";import"./model-ref-shared-T-aCqInj.mjs";import{r as normalizeProviderId}from"./provider-id-A-8WTtLA.mjs";import{y as resolveModelRefFromString}from"./model-selection-shared-CIOFxJ36.mjs";import{r as modelKey}from"./provider-registry-index-fv5AHL27.mjs";import{n as createModelVisibilityPolicy}from"./model-visibility-policy-Bd5YBCCk.mjs";import{t as levenshteinDistance}from"./levenshtein-distance-D9oM68DG.mjs";import{o as resolveRuntimeNormalization}from"./model-runtime-normalization-Dr-uyynt.mjs";
//!src/auto-reply/reply/model-selection-directive.ts
const FUZZY_VARIANT_TOKENS=[`lightning`,`preview`,`mini`,`fast`,`turbo`,`lite`,`beta`,`small`,`nano`];function resolveModelDirectiveSelection(e){const{raw:t,defaultProvider:n,defaultModel:r,aliasIndex:i,allowedModelKeys:a}=e;const o=e.modelPolicy??createModelVisibilityPolicy({cfg:e.cfg??{},catalog:[],defaultProvider:n,defaultModel:{provider:n,model:r},agentId:e.agentId});const s=t.trim();const allows=t=>o.allows(t)&&(e.operatorModelPolicy?.allows(t)??true);const buildSelection=({provider:e,model:t},i)=>({provider:e,model:t,isDefault:e===n&&t===r,...i?{alias:i}:void 0});const resolveFuzzy=e=>{const t=normalizeLowercaseStringOrEmpty(e.fragment);if(!t)return;const o=e.provider?normalizeProviderId(e.provider):void 0;const s=[];for(const e of a){const t=e.indexOf(`/`);if(t<=0)continue;const n=normalizeProviderId(e.slice(0,t));const r=e.slice(t+1);if(r.endsWith(`*`)||!allows({provider:n,model:r}))continue;if(o&&n!==o)continue;s.push({provider:n,model:r})}if(!e.provider)for(const[e,n]of i.byAlias.entries()){if(!e.includes(t)||!allows(n.ref))continue;if(!s.some(e=>e.provider===n.ref.provider&&e.model===n.ref.model))s.push({provider:n.ref.provider,model:n.ref.model})}const c=s.map(e=>{const a=normalizeProviderId(e.provider);const o=e.model;const s=normalizeLowercaseStringOrEmpty(o);const c=`${a}/${s}`;const l=modelKey(a,o);const scoreFragment=(e,n,r,i)=>{if(e===t)return n;if(e.startsWith(t))return r;return e.includes(t)?i:0};let u=scoreFragment(c,220,140,110)+scoreFragment(a,180,120,90)+scoreFragment(s,160,110,80);const d=levenshteinDistance(t,s,3);if(d!=null)u+=(3-d)*70;const f=i.byKey.get(l)??[];for(const e of f)u+=scoreFragment(normalizeLowercaseStringOrEmpty(e),140,90,60);if(s.startsWith(a))u+=30;const p=FUZZY_VARIANT_TOKENS.filter(e=>t.includes(e));const m=p.filter(e=>s.includes(e)).length;const h=FUZZY_VARIANT_TOKENS.filter(e=>s.includes(e)).length;if(p.length===0&&h>0)u-=h*30;else if(p.length>0)u+=m>0?m*40:-20;const g=a===normalizeProviderId(n)&&o===r;if(g)u+=20;return{provider:e.provider,model:o,score:u,isDefault:g,variantCount:h,variantMatchCount:m,modelLength:s.length,key:l}}).toSorted((e,t)=>t.score-e.score||Number(t.isDefault)-Number(e.isDefault)||t.variantMatchCount-e.variantMatchCount||e.variantCount-t.variantCount||e.modelLength-t.modelLength||e.key.localeCompare(t.key))[0];const l=o?90:120;return c&&c.score>=l?buildSelection(c,i.byKey.get(modelKey(c.provider,c.model))?.[0]):void 0};const c=resolveModelRefFromString({cfg:e.cfg,agentId:e.agentId,raw:s,defaultProvider:n,aliasIndex:i});if(!c){const e=resolveFuzzy({fragment:s});return e?{selection:e}:{error:`Unrecognized model "${s}". Use /models to list providers, or /models <provider> to list models.`}}const l=modelKey(c.ref.provider,c.ref.model);const u=s.includes(`/`)||c.alias||a.has(l);if(e.operatorModelPolicy&&!e.operatorModelPolicy.allows(c.ref)&&u)return{error:`Your operator role cannot use this model. Choose an allowed model or ask a gateway administrator to update your role's model policy.`};const d=buildSelection(c.ref,c.alias);const f=allows(c.ref);if(f&&u)return{selection:d};if(s.includes(`/`)){const e=s.indexOf(`/`);const t=resolveFuzzy({provider:normalizeProviderId(s.slice(0,e).trim()),fragment:s.slice(e+1).trim()});if(t)return{selection:t}}const p=resolveFuzzy({fragment:s});if(p)return{selection:p};if(e.requireKnownModel&&!u){const e=s.toLowerCase();if(!o.catalog.some(t=>t.id.toLowerCase()===e)&&!o.configuredCatalog.some(t=>t.id.toLowerCase()===e))return{}}if(f)return{selection:d};const m=`${c.ref.provider}/${c.ref.model}`;const h=e.rawRuntime?.trim();const g=h?`/model ${m} --runtime ${h}`:`/model ${m}`;const _=[`Model "${m}" is not allowed. Use /models to list providers, or /models <provider> to list models.`,`Add "${m}" or its provider wildcard to ${o.allowRepairConfigPath}.`,`Then retry: ${g}`];if(h&&normalizeProviderId(h)===`codex`)_.push(`If the Codex runtime is missing, run: openclaw plugins enable codex`);return{error:_.join(`
`)}}async function prepareInlineModelDirectiveMatcher(e){const{cfg:t,agentId:n,defaultProvider:r,defaultModel:i,aliasIndex:a}=e;const o=e.preparedModelCatalog??await(await import(`./agents/model-catalog.runtime.js`)).loadPreparedModelCatalogSnapshot({config:t,agentId:n,readOnly:true});const s=createModelVisibilityPolicy({cfg:t,agentId:n,catalog:o.entries,defaultProvider:r,defaultModel:{provider:r,model:i},...resolveRuntimeNormalization(t)});return{preparedModelCatalog:o,isKnownInlineModel:e=>{const o=resolveModelDirectiveSelection({raw:e,cfg:t,agentId:n,defaultProvider:r,defaultModel:i,aliasIndex:a,allowedModelKeys:s.allowedKeys,modelPolicy:s,requireKnownModel:true});return Boolean(o.selection||o.error)}}}export{resolveModelDirectiveSelection as n,prepareInlineModelDirectiveMatcher as t};