//#region src/agent/image/image-model-fallback.ts
function collectModelRefs(params) {
	const refs = [];
	const add = (raw) => {
		const t = raw?.trim();
		if (t?.includes("/")) refs.push(t);
	};
	add(params.modelOverride);
	add(params.toolConfig.primary);
	for (const f of params.toolConfig.fallbacks ?? []) add(f);
	const seen = /* @__PURE__ */ new Set();
	const out = [];
	for (const r of refs) if (!seen.has(r)) {
		seen.add(r);
		out.push(r);
	}
	return out;
}
async function runWithImageModelFallback(params) {
	const refs = collectModelRefs({
		toolConfig: params.toolConfig,
		modelOverride: params.modelOverride
	});
	if (refs.length === 0) throw new Error("No image model configured. Set agents.defaults.imageModel.primary or add fallbacks.");
	const attempts = [];
	let lastErr;
	for (const modelRef of refs) try {
		return {
			result: await params.run(modelRef),
			attempts
		};
	} catch (err) {
		lastErr = err;
		const msg = err instanceof Error ? err.message : String(err);
		const slash = modelRef.indexOf("/");
		attempts.push({
			provider: slash > 0 ? modelRef.slice(0, slash) : modelRef,
			model: slash > 0 ? modelRef.slice(slash + 1) : "",
			error: msg
		});
	}
	const summary = attempts.map((a) => `${a.provider}/${a.model}: ${a.error}`).join(" | ");
	throw new Error(`All image models failed (${attempts.length}): ${summary || (lastErr instanceof Error ? lastErr.message : String(lastErr))}`);
}
//#endregion
export { runWithImageModelFallback };

//# sourceMappingURL=image-model-fallback.js.map