import { init_schema, parseModelRef } from "../../../config/schema.js";
import { getImageUnderstandingProvider } from "./provider-registry.js";
import "./pi-ai-provider.js";
//#region src/agent/image/understanding/runtime.ts
init_schema();
async function describeImages(params) {
	const parsed = parseModelRef(params.modelRef);
	if (!parsed) throw new Error(`Invalid model reference: ${params.modelRef}`);
	const provider = getImageUnderstandingProvider(parsed.provider);
	if (!provider) throw new Error(`No image understanding provider registered for: ${parsed.provider}. Ensure the provider module is imported.`);
	return provider.describeImages(parsed.model, {
		images: params.images,
		prompt: params.prompt,
		maxTokens: params.maxTokens,
		timeoutMs: params.timeoutMs,
		signal: params.signal
	});
}
async function describeImagesWithFallback(params) {
	const modelRefs = [params.modelRef, ...params.fallbacks ?? []];
	const attempts = [];
	let lastError;
	for (const modelRef of modelRefs) {
		const parsed = parseModelRef(modelRef);
		if (!parsed) {
			attempts.push({
				provider: modelRef,
				model: "",
				error: `Invalid model reference: ${modelRef}`
			});
			continue;
		}
		const provider = getImageUnderstandingProvider(parsed.provider);
		if (!provider) {
			attempts.push({
				provider: parsed.provider,
				model: parsed.model,
				error: `No provider registered for: ${parsed.provider}`
			});
			continue;
		}
		try {
			return {
				...await provider.describeImages(parsed.model, {
					images: params.images,
					prompt: params.prompt,
					maxTokens: params.maxTokens,
					timeoutMs: params.timeoutMs,
					signal: params.signal
				}),
				attempts
			};
		} catch (err) {
			lastError = err;
			attempts.push({
				provider: parsed.provider,
				model: parsed.model,
				error: err instanceof Error ? err.message : String(err)
			});
		}
	}
	const summary = attempts.map((a) => `${a.provider}/${a.model}: ${a.error}`).join(" | ");
	throw new Error(`All image understanding models failed (${attempts.length}): ${summary}`, { cause: lastError instanceof Error ? lastError : void 0 });
}
//#endregion
export { describeImages, describeImagesWithFallback };

//# sourceMappingURL=runtime.js.map