openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
171 lines (170 loc) • 6.21 kB
JavaScript
import { h as readProviderJsonResponse } from "./provider-http-errors-Dtgw3x-5.mjs";
import { p as sanitizeConfiguredModelProviderRequest } from "./provider-request-config-CCij-nof.mjs";
import { r as fetchWithSsrFGuard } from "./fetch-guard-BLMw2n18.mjs";
import { n as validateDecisionResultV2, t as validateDecisionBatchV2 } from "./validation-v2-60LJzO-m.mjs";
import "./ssrf-runtime-B6q6NM4E.mjs";
import { h as resolveProviderHttpRequestConfig } from "./shared-CfcMLHIP.mjs";
import "./provider-http-LcawLneq.mjs";
import { n as encodeSystemOneQuestions, t as decodeSystemOneResponse } from "./decisions-Cw6RZ9do.mjs";
import { t as OPENROUTER_BASE_URL } from "./provider-defaults-ilQX6q3J.mjs";
import { a as resolveOpenRouterApiBaseUrl, o as resolveOpenRouterSsrfPolicy } from "./provider-catalog-DXTNVH5w.mjs";
import { n as resolveOpenRouterExtraParamsForTransport, t as resolveOpenRouterConfiguredExtraParams } from "./provider-routing-Djr4k-k-.mjs";
//#region extensions/openrouter/decision-provider.ts
function isOpenRouterDecisionModel(e) {
return e === "typesafe/jev-1.13" || e === "typesafe/jev-1.14" || e === "~typesafe/jev-latest";
}
function unsupported() {
return {
status: "unavailable",
reason: "unsupported-input"
};
}
function supportsBatch(e, t) {
if (!validateDecisionBatchV2(e)) return false;
if (e.state.type !== "image" && e.state.type !== "text" && (e.state.type !== "json" || e.state.value === null || typeof e.state.value !== "object" && typeof e.state.value !== "string")) return false;
if (e.state.type === "image" && !/^data:image\/(?:png|jpeg);base64,/.test(e.state.dataUri)) return false;
return Object.values(e.questions).every((e) => {
if (e.type === "sort" || e.type === "tags" || e.instructions == null) return false;
if (e.type === "box") return t !== "typesafe/jev-1.13";
if (e.type === "choice") return Object.keys(e.criteria).length <= 255;
if (e.type === "score") return e.criteria.length <= 10 && e.criteria.every((e) => e !== null);
return e.criteria === void 0 || e.criteria !== null && e.criteria.true != null && e.criteria.false != null;
});
}
function buildRequest(e, t) {
const n = resolveOpenRouterConfiguredExtraParams({
config: t.config,
agentId: t.agentId,
modelId: t.model.id
}) ?? {};
const r = resolveOpenRouterExtraParamsForTransport({
config: t.config,
provider: "openrouter",
model: t.model,
extraParams: n
})?.patch ?? n;
if (r && Object.keys(r).some((e) => e !== "provider")) return;
return {
model: t.model.id,
state: e.state.type === "text" ? e.state.text : e.state.type === "json" ? e.state.value : e.state.type === "image" ? [...e.state.text === void 0 ? [] : [e.state.text], {
type: "image_url",
image_url: { url: e.state.dataUri }
}] : void 0,
questions: encodeSystemOneQuestions(e),
...r?.provider !== void 0 ? { provider: r.provider } : {}
};
}
function buildOpenRouterDecisionProvider() {
return {
id: "openrouter",
contractVersion: 2,
async evaluate(e, t) {
t.signal.throwIfAborted();
if (!isOpenRouterDecisionModel(t.model.id) || t.reasoning !== void 0 && t.reasoning !== "auto" || !supportsBatch(e, t.model.id)) return unsupported();
const n = buildRequest(e, t);
if (!n) return unsupported();
if (!t.auth.apiKey && !t.model.headers) return {
status: "unavailable",
reason: "credentials-unavailable"
};
const r = t.config.models?.providers?.openrouter;
let i;
let a;
const assertActive = () => {
t.signal.throwIfAborted();
let e;
try {
e = t.isAdmissible();
} catch (e) {
a = { error: e };
throw e;
}
if (!e || performance.now() >= t.deadlineMonotonicMs) throw new Error("OpenRouter decision is no longer admitted.");
};
try {
const a = r?.request;
const o = sanitizeConfiguredModelProviderRequest(a ? {
proxy: a.proxy,
tls: a.tls,
allowPrivateNetwork: a.allowPrivateNetwork
} : void 0);
const s = resolveProviderHttpRequestConfig({
provider: "openrouter",
capability: "llm",
transport: "http",
baseUrl: resolveOpenRouterApiBaseUrl(t.model.baseUrl ?? r?.baseUrl),
defaultBaseUrl: OPENROUTER_BASE_URL,
defaultHeaders: {
"Content-Type": "application/json",
...t.auth.apiKey && (!a?.auth || a.auth.mode === "provider-default") ? { Authorization: `Bearer ${t.auth.apiKey}` } : {}
},
headers: t.model.headers,
request: o
});
const c = t.deadlineMonotonicMs - performance.now();
t.signal.throwIfAborted();
if (c <= 0) return {
status: "unavailable",
reason: "transport"
};
const l = await fetchWithSsrFGuard({
url: s.baseUrl + "/systemone",
init: {
method: "POST",
headers: s.headers,
body: JSON.stringify(n)
},
signal: t.signal,
timeoutMs: c,
beforeRequest: assertActive,
policy: resolveOpenRouterSsrfPolicy(s, a),
dispatcherPolicy: s.dispatcherPolicy,
auditContext: "openrouter-decisions"
});
i = l.release;
const u = l.response;
if (!u.ok) return {
status: "unavailable",
reason: u.status === 401 || u.status === 403 ? "authentication" : u.status === 429 || u.status === 529 ? "rate-limited" : [
400,
413,
422
].includes(u.status) ? "unsupported-input" : "transport"
};
let d;
try {
d = await readProviderJsonResponse(u, "OpenRouter decisions", {
maxBytes: 1048576,
timeoutMs: Math.max(1, t.deadlineMonotonicMs - performance.now())
});
} catch {
t.signal.throwIfAborted();
return {
status: "unavailable",
reason: "invalid-response"
};
}
t.signal.throwIfAborted();
const f = decodeSystemOneResponse(e, d);
return validateDecisionResultV2(e, f, t.model.inference?.decision) ? {
status: "ok",
result: f
} : {
status: "unavailable",
reason: "invalid-response"
};
} catch {
t.signal.throwIfAborted();
if (a) throw a.error;
return {
status: "unavailable",
reason: "transport"
};
} finally {
await i?.();
}
}
};
}
//#endregion
export { isOpenRouterDecisionModel as n, buildOpenRouterDecisionProvider as t };