openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
80 lines (79 loc) • 3.85 kB
JavaScript
import { o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CCIiB4H7.mjs";
import { r as normalizeProviderId } from "./provider-id-b5XGSf9C.mjs";
import { n as estimateBase64DecodedBytes } from "./base64-CnKpkbff.mjs";
import { t as findNormalizedProviderValue } from "./model-selection-normalize-DIZCRGVF.mjs";
import "./model-selection-C2A4Wyix.mjs";
import { n as formatMediaSize } from "./store.shared-47VoRUGM.mjs";
import { n as isMinimaxVlmProvider } from "./minimax-vlm-KZwHpFfo.mjs";
import { r as coerceToolModelConfig } from "./model-config.helpers-FXb3KJo5.mjs";
//#region src/agents/tools/image-tool.helpers.ts
/** Decodes a base64 image data URL with optional decoded-size protection. */
function decodeDataUrl(e, t) {
const n = e.trim();
const r = /^data:([^;,]+)(?:;[^,]*)?;base64,/i.exec(n);
const i = r ? n.slice(r[0].length) : "";
if (!r || !i || /[^a-z0-9+/=\r\n]/i.test(i)) throw new Error("Invalid data URL (expected base64 data: URL).");
const a = normalizeLowercaseStringOrEmpty(r[1]);
if (!a.startsWith("image/")) throw new Error(`Unsupported data URL type: ${a || "unknown"}`);
const o = estimateBase64DecodedBytes(i);
if (typeof t?.maxBytes === "number" && o > t.maxBytes) throw new Error(`Invalid data URL: payload exceeds size limit (${formatMediaSize(o)}, limit ${formatMediaSize(t.maxBytes)}).`);
const s = Buffer.from(i, "base64");
if (s.length === 0) throw new Error("Invalid data URL: empty payload.");
return {
buffer: s,
mimeType: a,
kind: "image"
};
}
function coerceImageModelConfig(e) {
return coerceToolModelConfig(e?.agents?.defaults?.imageModel);
}
function resolveProviderlessConfiguredImageModelRef(e) {
const t = e.ref.trim();
if (!t || t.includes("/")) return t;
const n = e.cfg?.models?.providers;
const r = /* @__PURE__ */ new Set();
for (const [e, i] of Object.entries(n ?? {})) {
const n = normalizeProviderId(e);
if (!n || !Array.isArray(i?.models)) continue;
for (const e of i.models) {
const i = e?.id?.trim();
if (!i || !Array.isArray(e?.input) || !e.input.includes("image")) continue;
const a = [i];
const o = i.indexOf("/");
if (o > 0 && normalizeProviderId(i.slice(0, o)) === n) a.push(i.slice(o + 1));
const s = normalizeLowercaseStringOrEmpty(t);
if (!a.some((e) => normalizeLowercaseStringOrEmpty(e) === s)) continue;
r.add(o > 0 && normalizeProviderId(i.slice(0, o)) === n ? i : `${n}/${i}`);
}
}
const i = [...r];
if (i.length <= 1) return i.at(0) ?? t;
throw new Error(`Ambiguous image model "${t}". Configure a provider-prefixed ref such as ${i.map((e) => `"${e}"`).join(" or ")}.`);
}
function resolveConfiguredImageModelRefs(e) {
const t = e.imageModelConfig.primary?.trim();
const n = e.imageModelConfig.fallbacks?.map((t) => resolveProviderlessConfiguredImageModelRef({
cfg: e.cfg,
ref: t
})).filter((e) => e.length > 0);
return {
...e.imageModelConfig.primary !== void 0 ? { primary: t ? resolveProviderlessConfiguredImageModelRef({
cfg: e.cfg,
ref: t
}) : t } : {},
...n && n.length > 0 ? { fallbacks: n } : {},
...e.imageModelConfig.timeoutMs !== void 0 ? { timeoutMs: e.imageModelConfig.timeoutMs } : {}
};
}
function resolveProviderVisionModelFromConfig(e) {
if (isMinimaxVlmProvider(e.provider)) return null;
const t = ((findNormalizedProviderValue(e.cfg?.models?.providers, e.provider)?.models ?? []).find((e) => Boolean((e?.id ?? "").trim()) && e.input?.includes("image"))?.id ?? "").trim();
if (!t) return null;
const n = t.indexOf("/");
const r = n === -1 ? "" : normalizeLowercaseStringOrEmpty(t.slice(0, n));
const i = normalizeLowercaseStringOrEmpty(e.provider);
return r && r === i ? t : `${e.provider}/${t}`;
}
//#endregion
export { resolveProviderVisionModelFromConfig as i, decodeDataUrl as n, resolveConfiguredImageModelRefs as r, coerceImageModelConfig as t };