openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
90 lines (89 loc) • 2.9 kB
JavaScript
import { i as listAgentIds } from "./agent-roster-t1ooUdXp.mjs";
import { A as tryResolveLegacyCompatibilityAgentId } from "./agent-scope-config-DmXgFGKP.mjs";
import { h as readRuntimeConfig } from "./runtime-snapshot-WbwbY5b7.mjs";
import "./agent-scope-BwB8YH6y.mjs";
import { a as READ_SCOPE } from "./operator-scopes-CULmYfyY.mjs";
import "./io-4QsrsWTJ.mjs";
import { n as operatorScopeSatisfied } from "./operator-scope-compat-CpFHu-FC.mjs";
import { t as sendJson } from "./http-json-ddGJz5CA.mjs";
import { d as sendMethodNotAllowed, f as sendMissingScopeForbidden, h as sendUnauthorized, u as sendInvalidRequest } from "./http-common-FfhIIwOW.mjs";
import { r as authorizeGatewayHttpRequestOrReply, u as resolveSharedSecretHttpOperatorScopes } from "./http-auth-utils-B0FI4CE4.mjs";
import { i as canonicalizeOpenClawAgentModelId, n as OPENCLAW_MODEL_ID, t as OPENCLAW_DEFAULT_MODEL_ID } from "./http-utils-B5V9Okzh.mjs";
//#region 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);
}
/** Handle OpenAI-compatible model list/detail requests, returning false for unrelated paths. */
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;
}
//#endregion
export { handleOpenAiModelsHttpRequest };