openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
93 lines (92 loc) • 3.23 kB
JavaScript
import { r as normalizeProviderId } from "./provider-id-b5XGSf9C.mjs";
import { i as listAgentIds } from "./agent-roster-t1ooUdXp.mjs";
import { m as resolveAmbientOwnerAgentId } from "./agent-scope-config-DmXgFGKP.mjs";
import "./agent-scope-BwB8YH6y.mjs";
import { n as resolveSystemAgentConfiguredRouteFromConfig } from "./inference-route-CnmNvcun.mjs";
import { i as hasAvailableAuthForProvider } from "./model-auth-CEsyk_Yg.mjs";
import { a as verifySetupInference } from "./setup-inference-turn-C9QsFM4y.mjs";
import "./setup-inference-BaK6auaX.mjs";
//#region src/system-agent/inference-fallback.ts
const RETRYABLE_INFERENCE_STATUSES = /* @__PURE__ */ new Set([
"auth",
"rate_limit",
"billing",
"timeout",
"format",
"unavailable"
]);
const PROVIDER_WIDE_FAILURE_STATUSES = /* @__PURE__ */ new Set(["timeout", "unavailable"]);
/** Requester first. Other configured, authenticated providers: provider-id order. */
async function verifySystemAgentInferenceWithFallback(e) {
const { config: t, routePolicy: n } = e;
const r = t === void 0 ? await (await import("./config/config.js")).readConfigFileSnapshot() : void 0;
const i = t ?? (r?.exists && r.valid ? r.runtimeConfig ?? r.config : {});
const a = resolveAmbientOwnerAgentId(i, e.requestingAgentId);
const o = /* @__PURE__ */ new Set([a, ...listAgentIds(i)]);
const s = [];
for (const e of o) {
const t = await resolveSystemAgentConfiguredRouteFromConfig(i, e, {}, r);
if (!t) continue;
const n = normalizeProviderId(t.provider);
if (!n) continue;
s.push({
agentId: e,
provider: n,
route: t
});
}
const c = s.find((e) => e.agentId === a);
const l = [...c ? [c] : [], ...s.filter((e) => e !== c).toSorted((e, t) => e.provider.localeCompare(t.provider) || e.agentId.localeCompare(t.agentId))];
const u = n ? (await Promise.all(l.map(async ({ route: e }) => (await n.expand(e)).map((e) => ({
agentId: e.agentId,
provider: normalizeProviderId(e.provider),
route: e
}))))).flat() : l;
let d;
const f = /* @__PURE__ */ new Set();
const p = /* @__PURE__ */ new Set();
for (const t of u) {
if (f.has(t.provider)) continue;
const r = JSON.stringify([
t.provider,
t.route.authProfileId ?? null,
t.route.agentDir ?? null,
...n ? [t.route.model] : []
]);
if (p.has(r)) continue;
if (n && !await n.accept(t.route)) continue;
if (!n && t !== c && !await hasAvailableAuthForProvider({
provider: t.provider,
cfg: t.route.runConfig,
preferredProfile: t.route.authProfileId,
agentDir: t.route.agentDir,
modelId: t.route.model
})) continue;
p.add(r);
if (n) {
const e = await n.verify(t.route);
if (e.ok) return {
ok: true,
route: t.route
};
d = e;
} else {
const n = await verifySetupInference({
runtime: e.runtime,
bindSession: true,
agentId: t.agentId
});
if (n.ok) return n;
d = n;
}
if (!RETRYABLE_INFERENCE_STATUSES.has(d.status)) return d;
if (!n && PROVIDER_WIDE_FAILURE_STATUSES.has(d.status)) f.add(t.provider);
}
return d ?? {
ok: false,
status: "unknown",
error: "OpenClaw could not verify a usable inference route. Check model setup and try again."
};
}
//#endregion
export { verifySystemAgentInferenceWithFallback as t };