UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

68 lines (67 loc) • 2.25 kB
import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs"; import { r as assertOperatorModelAllowed, t as assertAdmittedRunOperatorAuthority } from "./admitted-run-context-D3Jvk_f_.mjs"; import { n as resolveImageFallbackCandidates } from "./model-fallback-candidates-CFJLuJlL.mjs"; import { f as runFallbackAttempt, m as throwFallbackFailureSummary } from "./model-fallback-attempt-DPZU4qST.mjs"; //#region src/agents/model-fallback-image.ts /** Resolve one canonical candidate set for preparation and image/PDF execution. */ function resolveAllowedImageFallbackCandidates(e) { const t = e.operatorAuthority; if (t) { assertAdmittedRunOperatorAuthority(t); t.assertCurrent(); } const n = resolveImageFallbackCandidates(e); if (e.modelOverride?.trim()) assertOperatorModelAllowed(t, n.find((e) => e.routeOrigin === "requested")); const r = t?.modelPolicy; if (!r) return n; const i = n.filter((e) => r.allows(e)); assertOperatorModelAllowed(t, i[0]); return i; } async function runWithImageModelFallback(e) { const t = resolveAllowedImageFallbackCandidates(e); if (t.length === 0) throw new Error("No image model configured. Set agents.defaults.imageModel.primary or agents.defaults.imageModel.fallbacks."); const n = []; let r; for (const [i, a] of t.entries()) { assertOperatorModelAllowed(e.operatorAuthority, a); const o = await runFallbackAttempt({ run: e.run, ...a, attempts: n, attempt: i + 1, total: t.length, abortSignal: e.abortSignal }).catch((t) => { e.operatorAuthority?.assertCurrent(); e.abortSignal?.throwIfAborted(); throw t; }); assertOperatorModelAllowed(e.operatorAuthority, a); if ("success" in o) return o.success; const s = o.error; r = s; n.push({ provider: a.provider, model: a.model, error: formatErrorMessage(s) }); await e.onError?.({ provider: a.provider, model: a.model, error: s, attempt: i + 1, total: t.length }); } return throwFallbackFailureSummary({ attempts: n, candidates: t, lastError: r, label: "image models", formatAttempt: (e) => `${e.provider}/${e.model}: ${e.error}`, cfg: e.cfg }); } //#endregion export { runWithImageModelFallback as n, resolveAllowedImageFallbackCandidates as t };