UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 2.43 kB
import{i as listAgentIds}from"./agent-roster-4XrgUgPK.mjs";import{A as tryResolveLegacyCompatibilityAgentId}from"./agent-scope-config-CG9CFpGX.mjs";import{h as readRuntimeConfig}from"./runtime-snapshot-Bg5K4JNu.mjs";import"./agent-scope-Dw5XRSEh.mjs";import{o as READ_SCOPE}from"./operator-scopes-bEvB7E5X.mjs";import"./io-Di_yuX3t.mjs";import{n as operatorScopeSatisfied}from"./operator-scope-compat-TmgAKmD2.mjs";import{t as sendJson}from"./http-json-g_k96dZ_.mjs";import{d as sendMethodNotAllowed,f as sendMissingScopeForbidden,h as sendUnauthorized,u as sendInvalidRequest}from"./http-common-kI7-c_LU.mjs";import{r as authorizeGatewayHttpRequestOrReply,u as resolveSharedSecretHttpOperatorScopes}from"./http-auth-utils-DoyTE-rC.mjs";import{i as canonicalizeOpenClawAgentModelId,n as OPENCLAW_MODEL_ID,t as OPENCLAW_DEFAULT_MODEL_ID}from"./http-utils-C2sazj48.mjs"; //!src/gateway/models-http.ts function toOpenAiModel(e){return{id:e,object:`model`,created:0,owned_by:`openclaw`,permission:[]}}function loadAgentModelIds(){const e=readRuntimeConfig();const t=/* @__PURE__ */ new Set([OPENCLAW_MODEL_ID,OPENCLAW_DEFAULT_MODEL_ID]);const n=tryResolveLegacyCompatibilityAgentId(e);if(n)t.add(`openclaw/${n}`);for(const n of listAgentIds(e))t.add(`openclaw/${n}`);return Array.from(t)}async function handleOpenAiModelsHttpRequest(e,t,n){const r=new URL(e.url??`/`,`http://localhost`).pathname;if(r!==`/v1/models`&&!r.startsWith(`/v1/models/`))return false;if(e.method!==`GET`){sendMethodNotAllowed(t,`GET`);return true}const i=await authorizeGatewayHttpRequestOrReply({...n,req:e,res:t});if(!i)return true;if(!i.hasCurrentClientAuthority()){sendUnauthorized(t);return true}const a=resolveSharedSecretHttpOperatorScopes(e,i);if(!operatorScopeSatisfied(`operator.read`,a)){sendMissingScopeForbidden(t,READ_SCOPE);return true}const o=loadAgentModelIds();if(r===`/v1/models`){sendJson(t,200,{object:`list`,data:o.map(toOpenAiModel)});return true}const s=r.slice(11);if(!s){sendInvalidRequest(t,`Missing model id.`);return true}let c;try{c=decodeURIComponent(s)}catch{sendInvalidRequest(t,`Invalid model id encoding.`);return true}const l=canonicalizeOpenClawAgentModelId(c);if(!l){sendInvalidRequest(t,`Invalid model id.`);return true}if(!o.includes(l)){sendJson(t,404,{error:{message:`Model '${c}' not found.`,type:`invalid_request_error`}});return true}sendJson(t,200,toOpenAiModel(l));return true}export{handleOpenAiModelsHttpRequest};