UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 8.31 kB
import{l as normalizeOptionalString}from"./string-coerce-CpTFcSwY.mjs";import{a as asOptionalRecord,l as isRecord}from"./record-coerce-BTbqdJ3-.mjs";import{_ as resolveDefaultAgentId}from"./agent-scope-config-CG9CFpGX.mjs";import{g as setAgentEffectiveModelPrimary,o as resolveAgentEffectiveModelPrimary}from"./agent-scope-Dw5XRSEh.mjs";import"./string-coerce-runtime-BYDH9Quw.mjs";import"./agent-runtime-SZFAv6hc.mjs";import{_ as resolveMigrationConfigRuntime,d as markMigrationItemSkipped,l as markMigrationItemConflict,u as markMigrationItemError}from"./migration-B8ObiJfa.mjs";import{a as HERMES_REASON_DEFAULT_MODEL_CONFIGURED,i as HERMES_REASON_CONFIG_RUNTIME_UNAVAILABLE,p as readHermesModelDetails,t as HERMES_REASON_ALREADY_CONFIGURED}from"./items-CStDNBS4.mjs"; //!extensions/migrate-hermes/model.ts const HERMES_PROVIDER_ALIASES={alibaba:`qwen`,"alibaba-cloud":`qwen`,"alibaba-coding":`qwen`,"alibaba-coding-plan":`qwen`,alibaba_coding:`qwen`,alibaba_coding_plan:`qwen`,aliyun:`qwen`,"azure-foundry":`microsoft-foundry`,bedrock:`amazon-bedrock`,claude:`anthropic`,"claude-code":`anthropic`,copilot:`github-copilot`,gemini:`google`,github:`github-copilot`,"github-model":`github-copilot`,"github-models":`github-copilot`,glm:`zai`,"google-ai-studio":`google`,"google-gemini":`google`,grok:`xai`,kilo:`kilocode`,"kilo-code":`kilocode`,"kilo-gateway":`kilocode`,"kimi-cn":`moonshot`,"kimi-for-coding":`kimi`,"kimi-coding":`kimi`,"kimi-coding-cn":`moonshot`,"moonshot-cn":`moonshot`,"minimax-global":`minimax-portal`,"minimax-cn":`minimax`,"minimax-oauth":`minimax-portal`,minimax_oauth:`minimax-portal`,"opencode-zen":`opencode`,"openai-api":`openai`,"openai-codex":`openai`,dashscope:`qwen`,"qwen-cli":`qwen`,"qwen-oauth":`qwen`,"qwen-portal":`qwen`,"x-ai":`xai`,"x-ai-oauth":`xai`,"x.ai":`xai`,"xai-grok-oauth":`xai`,"xai-oauth":`xai`,"grok-oauth":`xai`,"z-ai":`zai`,"z.ai":`zai`,zen:`opencode`,zhipu:`zai`,vertex:`google-vertex`};const HERMES_CANONICAL_PROVIDER_IDS=/* @__PURE__ */ new Set([`alibaba`,`alibaba-coding-plan`,`anthropic`,`arcee`,`azure-foundry`,`bedrock`,`copilot`,`copilot-acp`,`deepseek`,`fireworks`,`github-copilot`,`gemini`,`gmi`,`huggingface`,`kilo`,`kilocode`,`kimi-coding`,`kimi-coding-cn`,`kimi-for-coding`,`lmstudio`,`minimax`,`minimax-cn`,`minimax-oauth`,`moa`,`nous`,`novita`,`nvidia`,`ollama-cloud`,`openai-api`,`openai-codex`,`opencode`,`opencode-go`,`opencode-zen`,`openrouter`,`stepfun`,`tencent-tokenhub`,`xai`,`xai-oauth`,`xiaomi`,`zai`,`vertex`]);const HERMES_DYNAMIC_KIMI_PROVIDER_IDS=/* @__PURE__ */ new Set([`kimi`,`kimi-coding`,`kimi-for-coding`,`moonshot`]);const HERMES_RETIRED_QWEN_PROVIDER_IDS=/* @__PURE__ */ new Set([`qwen-cli`,`qwen-oauth`,`qwen-portal`]);function normalizeHermesProviderId(e){const t=normalizeHermesCustomProviderId(e);return Object.hasOwn(HERMES_PROVIDER_ALIASES,t)?HERMES_PROVIDER_ALIASES[t]??t:t}function normalizeHermesCustomProviderId(e){const t=e.trim().toLowerCase();return(t.startsWith(`custom:`)?t.slice(7):t).replaceAll(` `,`-`)}function isRetiredHermesQwenProviderValue(e){const t=e.indexOf(`/`);const n=t>0?e.slice(0,t):e;return HERMES_RETIRED_QWEN_PROVIDER_IDS.has(normalizeHermesCustomProviderId(n))}function usesRetiredHermesQwenProvider(e){const t=asOptionalRecord(e.model);return[normalizeOptionalString(e.provider),typeof e.model===`string`?e.model:void 0,normalizeOptionalString(t?.provider),normalizeOptionalString(t?.default),normalizeOptionalString(t?.model),normalizeOptionalString(e.default_model),normalizeOptionalString(e.model_name)].some(e=>e!==void 0&&isRetiredHermesQwenProviderValue(e))}function readHermesBaseUrl(e){return e?normalizeOptionalString(e.base_url)??normalizeOptionalString(e.baseUrl)??normalizeOptionalString(e.url)??normalizeOptionalString(e.api):void 0}function readKimiBaseUrl(e,t,n){const r=asOptionalRecord(e.model);const i=normalizeOptionalString(r?.provider)??normalizeOptionalString(e.provider);if(i&&HERMES_DYNAMIC_KIMI_PROVIDER_IDS.has(normalizeHermesCustomProviderId(i))){const e=readHermesBaseUrl(r);if(e)return e}const a=asOptionalRecord(e.providers);for(const[e,n]of Object.entries(a??{}))if(normalizeHermesCustomProviderId(e)===normalizeHermesCustomProviderId(t)){const e=readHermesBaseUrl(asOptionalRecord(n));if(e)return e}return n.KIMI_BASE_URL?.trim()||void 0}function resolveHermesKimiProviderId(e,t,n){const r=normalizeHermesCustomProviderId(t);if(!HERMES_DYNAMIC_KIMI_PROVIDER_IDS.has(r))return;const i=readKimiBaseUrl(e,r,n);if(i){try{const e=new URL(i);const t=e.hostname.toLowerCase();const n=e.pathname.toLowerCase().replace(/\/+$/u,``);if(t===`api.kimi.com`&&(n===`/coding`||n===`/coding/v1`))return`kimi`;if(t===`api.moonshot.ai`)return`moonshot`}catch{}return normalizeHermesProviderId(r)===`moonshot`?`moonshot`:`kimi`}return(n.KIMI_API_KEY?.trim()||n.KIMI_CODING_API_KEY?.trim())?.startsWith(`sk-kimi-`)?`kimi`:`moonshot`}function hasExplicitHermesProvider(e,t){const n=normalizeHermesCustomProviderId(t);if(HERMES_CANONICAL_PROVIDER_IDS.has(n)||HERMES_RETIRED_QWEN_PROVIDER_IDS.has(n))return false;const r=e.providers;if(isRecord(r)&&Object.keys(r).some(e=>normalizeHermesCustomProviderId(e)===n))return true;return Array.isArray(e.custom_providers)&&e.custom_providers.some(e=>{if(!isRecord(e))return false;const t=normalizeOptionalString(e.name)??normalizeOptionalString(e.id);return t?normalizeHermesCustomProviderId(t)===n:false})}function resolveHermesConfiguredProviderId(e,t,n={}){if(hasExplicitHermesProvider(e,t))return normalizeHermesCustomProviderId(t);return resolveHermesKimiProviderId(e,t,n)??normalizeHermesProviderId(t)}function joinHermesProviderModel(e,t,n,r){if(!t){const e=n.indexOf(`/`);if(e>0&&isRetiredHermesQwenProviderValue(n))return`qwen/${n.slice(e+1)}`;return n}if(t.trim().toLowerCase()===`auto`){const t=n.indexOf(`/`);return t>0?`${resolveHermesConfiguredProviderId(e,n.slice(0,t),r)}/${n.slice(t+1)}`:n}const i=hasExplicitHermesProvider(e,t);const a=resolveHermesConfiguredProviderId(e,t,r);const o=n.indexOf(`/`);if(o>0){if((i?normalizeHermesCustomProviderId(n.slice(0,o)):resolveHermesConfiguredProviderId(e,n.slice(0,o),r))===a)return`${a}/${n.slice(o+1)}`}return n.startsWith(`${a}/`)?n:`${a}/${n}`}function resolveHermesModelRef(e,t={}){const n=e.model;if(typeof n===`string`&&n.trim()){const r=n.trim();return joinHermesProviderModel(e,normalizeOptionalString(e.provider),r,t)}if(isRecord(n)){const r=normalizeOptionalString(n.default)??normalizeOptionalString(n.model);const i=Boolean(normalizeOptionalString(n.base_url)??normalizeOptionalString(n.baseUrl));const a=normalizeOptionalString(n.provider)??(i?`custom`:void 0);return r?joinHermesProviderModel(e,a,r,t):void 0}const r=normalizeOptionalString(e.default_model)??normalizeOptionalString(e.model_name);const i=normalizeOptionalString(e.provider);return r?joinHermesProviderModel(e,i,r,t):void 0}function resolveCurrentModelRef(e){return resolveAgentEffectiveModelPrimary(e.config,e.targetAgentId??resolveDefaultAgentId(e.config))}var ModelApplyAbortError=class extends Error{constructor(e,t){super(t);this.status=e;this.reason=t;this.name=`ModelApplyAbortError`}};async function applyModelItem(e,t){const n=readHermesModelDetails(t);if(!n)return t;try{const r=resolveMigrationConfigRuntime(e);if(!r?.current||!r.mutateConfigFile)return markMigrationItemError(t,HERMES_REASON_CONFIG_RUNTIME_UNAVAILABLE);const i=e.targetAgentId??resolveDefaultAgentId(e.config);await r.mutateConfigFile({base:`runtime`,afterWrite:{mode:`auto`},mutate(t){const r=resolveAgentEffectiveModelPrimary(t,i);if(r===n.model)throw new ModelApplyAbortError(`skipped`,HERMES_REASON_ALREADY_CONFIGURED);if(r&&!e.overwrite)throw new ModelApplyAbortError(`conflict`,HERMES_REASON_DEFAULT_MODEL_CONFIGURED);setAgentEffectiveModelPrimary(t,i,n.model,{forceAgent:Boolean(e.targetAgentId)})}});return{...t,status:`migrated`}}catch(e){if(e instanceof ModelApplyAbortError)return e.status===`conflict`?markMigrationItemConflict(t,e.reason):markMigrationItemSkipped(t,e.reason);return markMigrationItemError(t,e instanceof Error?e.message:String(e))}}export{resolveCurrentModelRef as a,usesRetiredHermesQwenProvider as c,readHermesBaseUrl as i,normalizeHermesCustomProviderId as n,resolveHermesConfiguredProviderId as o,normalizeHermesProviderId as r,resolveHermesModelRef as s,applyModelItem as t};