UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 5.6 kB
import"./model-ref-shared-T-aCqInj.mjs";import{r as normalizeProviderId}from"./provider-id-A-8WTtLA.mjs";import{b as resolveNativeModelPrimary,f as resolveAgentWorkspaceDir}from"./agent-scope-config-CG9CFpGX.mjs";import{$c as validateModelsProbeParams}from"./src-BDZMFmKa.mjs";import{i as DEFAULT_PROVIDER,n as DEFAULT_MODEL}from"./defaults-k51tX6yK.mjs";import"./agent-scope-Dw5XRSEh.mjs";import{b as readUtilityModelSetting,h as resolveConfiguredModelRef,i as buildModelAliasIndex,y as resolveModelRefFromString}from"./model-selection-shared-CIOFxJ36.mjs";import{r as modelKey}from"./provider-registry-index-fv5AHL27.mjs";import{n as resolveConfiguredModelFallbacks}from"./model-selection-resolve-BoMeDqc_.mjs";import{i as ErrorCodes}from"./missing-scope-error-CkgBgh6d.mjs";import{p as errorShape}from"./error-codes-CQNGhXT-.mjs";import{t as formatForLog}from"./ws-log-DRCSDKLv.mjs";import{t as assertValidParams}from"./validation-CM41YclJ.mjs";import{t as redactStatusSecrets}from"./status-redaction-C7GJukcz.mjs";import{r as runAuthProbes}from"./list.probe-B_USeMnp.mjs";import{t as resolveModelAuthAgentScope}from"./model-auth-agent-scope-dS6ga2Rp.mjs"; //!src/gateway/server-methods/models-probe.ts const DEFAULT_TIMEOUT_MS=2e4;const MIN_TIMEOUT_MS=5e3;const MAX_TIMEOUT_MS=6e4;const PROBE_CONCURRENCY=2;const FAILURE_PRIORITY=[`auth`,`billing`,`rate_limit`,`timeout`,`format`,`no_model`,`unknown`];const PROBE_ERROR_MESSAGES={auth:`Authentication failed.`,rate_limit:`The provider rate limit was reached.`,billing:`The provider reported a billing problem.`,timeout:`The connection timed out.`,format:`The provider rejected the model or request format.`,unknown:`The connection check failed.`,no_model:`No model is available for this provider.`};function safeProbeError(e){return e===`ok`?void 0:PROBE_ERROR_MESSAGES[e]}const PROBE_REASON_MESSAGES={excluded_by_auth_order:`This profile is excluded by the provider auth order. Update the order or choose another profile, then retry.`,missing_credential:`This credential is missing. Add it or remove the stale configuration, then retry.`,expired:`This credential has expired. Sign in again or replace it, then retry.`,refresh_failed:`This sign-in could not be refreshed. Sign in again from Models, then retry.`,invalid_expires:`This credential has invalid expiry metadata. Sign in again or replace it, then retry.`,unresolved_ref:`The configured credential could not be resolved. Update or remove it, then retry.`,ineligible_profile:`This profile is not compatible with the provider configuration. Choose another profile, then retry.`,no_model:`No model is available for this provider. Configure a model, then retry.`};function safeProbeTargetLabel(e){const t=e.source===`profile`?`Profile ${e.label}`:e.source===`models.json`?e.label===`config`?`Configured credential`:`Provider configuration`:`Environment credential (${e.label})`;return e.model?`${t} · ${e.model}`:t}function safeProbeTargetError(e){if(e.status===`ok`)return;return(e.reasonCode&&PROBE_REASON_MESSAGES[e.reasonCode])??safeProbeError(e.status)}function modelCandidatesFromConfig(e,t){const n={cfg:e,agentId:t,defaultProvider:DEFAULT_PROVIDER,allowPluginNormalization:false};const r=buildModelAliasIndex(n);const i=resolveNativeModelPrimary(e,t)?resolveConfiguredModelRef({...n,defaultModel:DEFAULT_MODEL}):void 0;const a=readUtilityModelSetting(e,t);const o=[...resolveConfiguredModelFallbacks(n),...a.kind===`explicit`?[a.modelRef]:[],...a.kind!==`disabled`?a.fallbacks:[]].flatMap(e=>{const t=resolveModelRefFromString({...n,raw:e,aliasIndex:r});return t?[modelKey(t.ref.provider,t.ref.model)]:[]});return i?[modelKey(i.provider,i.model),...o]:o}function selectRollupStatus(e){if(e.some(e=>e.status===`ok`))return`ok`;return FAILURE_PRIORITY.find(t=>e.some(e=>e.status===t))??`unknown`}function mapProbeResult(e,t){const n=selectRollupStatus(t);const r=t.filter(e=>e.status===n).map(e=>e.latencyMs).filter(e=>typeof e===`number`).toSorted((e,t)=>e-t)[0];const i=safeProbeError(n);return{provider:e,status:n,...r!==void 0?{latencyMs:r}:{},...i?{error:i}:{},results:t.map(e=>{const t=safeProbeTargetError(e);return{...e.profileId?{profileId:e.profileId}:{},label:safeProbeTargetLabel(e),status:e.status,...e.latencyMs!==void 0?{latencyMs:e.latencyMs}:{},...t?{error:t}:{}}})}}const modelsProbeHandlers={"models.probe":async({params:e,respond:t,context:n})=>{if(!assertValidParams(e,validateModelsProbeParams,`models.probe`,t))return;const r=normalizeProviderId(e.provider);const i=e.profileId?.trim();if(!r||e.profileId!==void 0&&!i){t(false,void 0,errorShape(ErrorCodes.INVALID_REQUEST,`provider and profileId must not be blank`));return}const a=Math.min(MAX_TIMEOUT_MS,Math.max(MIN_TIMEOUT_MS,e.timeoutMs??DEFAULT_TIMEOUT_MS));try{const o=n.getRuntimeConfig();const s=resolveModelAuthAgentScope(o,e.agentId);if(!s.ok){t(false,void 0,s.error);return}const c=resolveAgentWorkspaceDir(o,s.agentId);const l=mapProbeResult(r,(await runAuthProbes({cfg:o,agentId:s.agentId,agentDir:s.agentDir,workspaceDir:c,providers:[r],modelCandidates:modelCandidatesFromConfig(o,s.agentId),options:{provider:r,...i?{profileIds:[i]}:{},...!i?{includeDirectKeys:true}:{},timeoutMs:a,concurrency:PROBE_CONCURRENCY}})).results);if(l.results.length===0)l.error=`No check targets are available for this provider.`;t(true,l,void 0)}catch(e){n.logGateway.warn(`Model connection check failed.`,{event:`models_probe_failed`,provider:r,timeoutMs:a,error:redactStatusSecrets(formatForLog(e))});t(false,void 0,errorShape(ErrorCodes.UNAVAILABLE,`Connection check failed.`))}}};export{modelsProbeHandlers};