import { init_agent_scope, normalizeAgentId } from "../../../agent/agent-scope.js";
import { init_schema, parseModelRef } from "../../../config/schema.js";
import { init_providers, isProviderConfigured, resolveModel } from "../../../providers/index.js";
import { getVoiceModelsConfig } from "../../../config/voice.js";
import { agentModelFallbacksToArray, agentModelRefToString } from "../lib/agent-model.js";
import { deleteAgentAvatarFile, finalizeCreateAgentDirs, listAgentProfileFiles, listGatewayAgents, prepareCreateAgent, prepareCreateAgentsBatch, prepareDeleteAgent, prepareUpdateAgent, readAgentAvatarFile, readAgentProfileFile, runAfterDeletePurge, writeAgentAvatarFromBase64, writeAgentProfileFile } from "../../agents-admin.js";
import { resolveImageGenerationCapabilities, resolveImageUnderstandingCapabilities } from "../../image-capabilities.js";
//#region src/gateway/hono/routes/agents.ts
init_schema();
init_providers();
init_agent_scope();
function parseProfileFiles(raw) {
	if (raw === void 0) return;
	if (raw === null || typeof raw !== "object" || Array.isArray(raw)) return { error: "profileFiles must be an object" };
	const profileFiles = {};
	for (const [name, content] of Object.entries(raw)) {
		if (typeof content !== "string") return { error: `profileFiles["${name}"] must be a string` };
		profileFiles[name] = content;
	}
	return profileFiles;
}
function isParseError(value) {
	return typeof value === "object" && value !== null && "error" in value && typeof value.error === "string";
}
function parseCreateAgentBody(raw) {
	if (raw === null || typeof raw !== "object" || Array.isArray(raw)) return { error: "each agent must be an object" };
	const body = raw;
	const name = typeof body.name === "string" ? body.name : "";
	const workspace = typeof body.workspace === "string" ? body.workspace : "";
	const model = typeof body.model === "string" ? body.model : void 0;
	const agentDir = typeof body.agentDir === "string" ? body.agentDir : void 0;
	const description = typeof body.description === "string" ? body.description : void 0;
	const id = typeof body.id === "string" ? body.id : void 0;
	const toolsDisable = Array.isArray(body.toolsDisable) ? body.toolsDisable.map((x) => String(x).trim()).filter(Boolean) : void 0;
	let profileFiles;
	if (Object.hasOwn(body, "profileFiles")) {
		const parsed = parseProfileFiles(body.profileFiles);
		if (isParseError(parsed)) return parsed;
		profileFiles = parsed;
	}
	return {
		name,
		workspace,
		...model !== void 0 ? { model } : {},
		...agentDir !== void 0 ? { agentDir } : {},
		...id !== void 0 ? { id } : {},
		...description !== void 0 ? { description } : {},
		...toolsDisable !== void 0 ? { toolsDisable } : {},
		...profileFiles !== void 0 ? { profileFiles } : {}
	};
}
function registerAgentsRoutes(authenticated, deps) {
	const { service, strictRateLimitMiddleware } = deps;
	authenticated.get("/api/agents", async (c) => {
		const cfg = service.currentConfig;
		const payload = await listGatewayAgents(cfg);
		return c.json({
			ok: true,
			payload
		});
	});
	authenticated.post("/api/agents/batch", strictRateLimitMiddleware, async (c) => {
		let body = {};
		try {
			body = await c.req.json();
		} catch {
			return c.json({
				ok: false,
				error: { message: "Invalid JSON" }
			}, 400);
		}
		const rawAgents = body.agents;
		if (!Array.isArray(rawAgents)) return c.json({
			ok: false,
			error: { message: "agents must be an array" }
		}, 400);
		const parsedAgents = [];
		for (const raw of rawAgents) {
			const parsed = parseCreateAgentBody(raw);
			if ("error" in parsed) return c.json({
				ok: false,
				error: { message: parsed.error }
			}, 400);
			parsedAgents.push(parsed);
		}
		const prep = prepareCreateAgentsBatch(service.currentConfig, parsedAgents);
		if (prep.ok === false) return c.json({
			ok: false,
			error: { message: prep.error }
		}, prep.status ?? 400);
		const { nextConfig, created } = prep.data;
		const save = await service.saveConfig(nextConfig);
		if (!save.saved) return c.json({
			ok: false,
			error: { message: save.error ?? "save failed" }
		}, 500);
		const cfg = service.currentConfig;
		const agentIds = [];
		for (const item of created) {
			const finalized = await finalizeCreateAgentDirs(cfg, item.agentId, { ...item.profileFiles !== void 0 ? { profileFiles: item.profileFiles } : {} });
			if (finalized.ok === false) return c.json({
				ok: false,
				error: { message: finalized.error }
			}, finalized.status ?? 400);
			agentIds.push(item.agentId);
		}
		const agentsPayload = await listGatewayAgents(cfg);
		return c.json({
			ok: true,
			payload: {
				agentIds,
				agents: agentsPayload
			}
		});
	});
	authenticated.post("/api/agents", strictRateLimitMiddleware, async (c) => {
		let body = {};
		try {
			body = await c.req.json();
		} catch {
			return c.json({
				ok: false,
				error: { message: "Invalid JSON" }
			}, 400);
		}
		const parsed = parseCreateAgentBody(body);
		if ("error" in parsed) return c.json({
			ok: false,
			error: { message: parsed.error }
		}, 400);
		const prep = prepareCreateAgent(service.currentConfig, parsed);
		if (prep.ok === false) return c.json({
			ok: false,
			error: { message: prep.error }
		}, prep.status ?? 400);
		const { nextConfig, agentId } = prep.data;
		const save = await service.saveConfig(nextConfig);
		if (!save.saved) return c.json({
			ok: false,
			error: { message: save.error ?? "save failed" }
		}, 500);
		const finalized = await finalizeCreateAgentDirs(service.currentConfig, agentId, { ...parsed.profileFiles !== void 0 ? { profileFiles: parsed.profileFiles } : {} });
		if (finalized.ok === false) return c.json({
			ok: false,
			error: { message: finalized.error }
		}, finalized.status ?? 400);
		const agentsPayload = await listGatewayAgents(service.currentConfig);
		return c.json({
			ok: true,
			payload: {
				agentId,
				agents: agentsPayload
			}
		});
	});
	authenticated.patch("/api/agents/:id", strictRateLimitMiddleware, async (c) => {
		const id = normalizeAgentId(c.req.param("id") ?? "");
		let body = {};
		try {
			body = await c.req.json();
		} catch {
			return c.json({
				ok: false,
				error: { message: "Invalid JSON" }
			}, 400);
		}
		const skillsPatch = body.skills === null ? null : Array.isArray(body.skills) ? body.skills.map((x) => String(x).trim()).filter(Boolean) : void 0;
		const toolsDisablePatch = body.toolsDisable === null ? null : Array.isArray(body.toolsDisable) ? body.toolsDisable.map((x) => String(x).trim()).filter(Boolean) : void 0;
		const descriptionPatch = Object.hasOwn(body, "description") ? body.description === null ? null : typeof body.description === "string" ? body.description : void 0 : void 0;
		const prep = prepareUpdateAgent(service.currentConfig, id, {
			name: typeof body.name === "string" ? body.name : void 0,
			...descriptionPatch !== void 0 ? { description: descriptionPatch } : {},
			workspace: typeof body.workspace === "string" ? body.workspace : void 0,
			model: body.model === null ? null : typeof body.model === "string" ? body.model : void 0,
			agentDir: body.agentDir === null ? null : typeof body.agentDir === "string" ? body.agentDir : void 0,
			setDefault: body.setDefault === true,
			...skillsPatch !== void 0 ? { skills: skillsPatch } : {},
			...toolsDisablePatch !== void 0 ? { toolsDisable: toolsDisablePatch } : {}
		});
		if (prep.ok === false) return c.json({
			ok: false,
			error: { message: prep.error }
		}, prep.status ?? 400);
		const save = await service.saveConfig(prep.data.nextConfig);
		if (!save.saved) return c.json({
			ok: false,
			error: { message: save.error ?? "save failed" }
		}, 500);
		const agentsPayload = await listGatewayAgents(service.currentConfig);
		return c.json({
			ok: true,
			payload: agentsPayload
		});
	});
	authenticated.delete("/api/agents/:id", strictRateLimitMiddleware, async (c) => {
		const id = normalizeAgentId(c.req.param("id") ?? "");
		const purge = c.req.query("purge") === "1" || c.req.query("purge") === "true";
		const prep = prepareDeleteAgent(service.currentConfig, id);
		if (prep.ok === false) return c.json({
			ok: false,
			error: { message: prep.error }
		}, prep.status ?? 400);
		const { nextConfig, agentId } = prep.data;
		const save = await service.saveConfig(nextConfig);
		if (!save.saved) return c.json({
			ok: false,
			error: { message: save.error ?? "save failed" }
		}, 500);
		if (purge) await runAfterDeletePurge(service.currentConfig, agentId);
		const agentsPayload = await listGatewayAgents(service.currentConfig);
		return c.json({
			ok: true,
			payload: {
				agentId,
				purged: purge,
				agents: agentsPayload
			}
		});
	});
	authenticated.get("/api/agents/:id/avatar", async (c) => {
		const id = normalizeAgentId(c.req.param("id") ?? "");
		const res = await readAgentAvatarFile(service.currentConfig, id);
		if (res.ok === false) return c.json({
			ok: false,
			error: { message: res.error }
		}, res.status ?? 400);
		return new Response(res.data.buffer, {
			status: 200,
			headers: {
				"Content-Type": res.data.contentType,
				"Cache-Control": "private, max-age=3600"
			}
		});
	});
	authenticated.put("/api/agents/:id/avatar", strictRateLimitMiddleware, async (c) => {
		const id = normalizeAgentId(c.req.param("id") ?? "");
		let body = {};
		try {
			body = await c.req.json();
		} catch {
			return c.json({
				ok: false,
				error: { message: "Invalid JSON" }
			}, 400);
		}
		const base64 = typeof body.base64 === "string" ? body.base64 : "";
		const mimeType = typeof body.mimeType === "string" ? body.mimeType : "";
		const res = await writeAgentAvatarFromBase64(service.currentConfig, id, base64, mimeType);
		if (res.ok === false) return c.json({
			ok: false,
			error: { message: res.error }
		}, res.status ?? 400);
		return c.json({
			ok: true,
			payload: { agentId: res.data.agentId }
		});
	});
	authenticated.delete("/api/agents/:id/avatar", strictRateLimitMiddleware, async (c) => {
		const id = normalizeAgentId(c.req.param("id") ?? "");
		const res = await deleteAgentAvatarFile(service.currentConfig, id);
		if (res.ok === false) return c.json({
			ok: false,
			error: { message: res.error }
		}, res.status ?? 400);
		return c.json({
			ok: true,
			payload: { agentId: res.data.agentId }
		});
	});
	authenticated.get("/api/agents/:id/files", async (c) => {
		const id = normalizeAgentId(c.req.param("id") ?? "");
		const res = await listAgentProfileFiles(service.currentConfig, id);
		if (res.ok === false) return c.json({
			ok: false,
			error: { message: res.error }
		}, res.status ?? 400);
		return c.json({
			ok: true,
			payload: res.data
		});
	});
	authenticated.get("/api/agents/:id/files/:name", async (c) => {
		const id = normalizeAgentId(c.req.param("id") ?? "");
		const name = decodeURIComponent(c.req.param("name") ?? "");
		const res = await readAgentProfileFile(service.currentConfig, id, name);
		if (res.ok === false) return c.json({
			ok: false,
			error: { message: res.error }
		}, res.status ?? 400);
		return c.json({
			ok: true,
			payload: {
				agentId: res.data.agentId,
				name,
				content: res.data.content
			}
		});
	});
	authenticated.put("/api/agents/:id/files/:name", strictRateLimitMiddleware, async (c) => {
		const id = normalizeAgentId(c.req.param("id") ?? "");
		const name = decodeURIComponent(c.req.param("name") ?? "");
		let content = "";
		try {
			const body = await c.req.json();
			content = typeof body.content === "string" ? body.content : "";
		} catch {
			return c.json({
				ok: false,
				error: { message: "Invalid JSON" }
			}, 400);
		}
		const res = await writeAgentProfileFile(service.currentConfig, id, name, content);
		if (res.ok === false) return c.json({
			ok: false,
			error: { message: res.error }
		}, res.status ?? 400);
		return c.json({
			ok: true,
			payload: {
				agentId: res.data.agentId,
				name
			}
		});
	});
	authenticated.get("/api/voice/models", (c) => {
		const models = getVoiceModelsConfig();
		return c.json({
			ok: true,
			payload: { models }
		});
	});
	authenticated.get("/api/image/capabilities", async (c) => {
		const config = service.currentConfig;
		const imageGenerationProviders = await resolveImageGenerationCapabilities(config);
		const imageUnderstandingProviders = await resolveImageUnderstandingCapabilities(config);
		return c.json({
			ok: true,
			payload: {
				current: {
					imageModel: agentModelRefToString(config.agents?.defaults?.imageModel) ?? null,
					imageModelFallbacks: agentModelFallbacksToArray(config.agents?.defaults?.imageModel),
					imageGenerationModel: agentModelRefToString(config.agents?.defaults?.imageGenerationModel) ?? null,
					imageGenerationModelFallbacks: agentModelFallbacksToArray(config.agents?.defaults?.imageGenerationModel),
					mediaMaxMb: config.agents?.defaults?.mediaMaxMb ?? null
				},
				imageGeneration: { providers: imageGenerationProviders },
				imageUnderstanding: { providers: imageUnderstandingProviders }
			}
		});
	});
	authenticated.post("/api/image/validate-model", strictRateLimitMiddleware, async (c) => {
		let body;
		try {
			body = await c.req.json();
		} catch {
			return c.json({
				ok: false,
				error: "Invalid JSON"
			}, 400);
		}
		const modelRef = body.modelRef;
		if (!modelRef || typeof modelRef !== "string") return c.json({
			ok: false,
			error: "modelRef is required"
		}, 400);
		const parsed = parseModelRef(modelRef);
		if (!parsed) return c.json({
			ok: true,
			payload: {
				valid: false,
				reason: "invalid_format",
				message: "Model reference must be in \"provider/model\" format"
			}
		});
		if (!await isProviderConfigured(parsed.provider)) return c.json({
			ok: true,
			payload: {
				valid: false,
				reason: "provider_not_configured",
				message: `Provider "${parsed.provider}" is not configured. Set the API key first.`,
				provider: parsed.provider
			}
		});
		try {
			resolveModel(modelRef);
		} catch {
			return c.json({
				ok: true,
				payload: {
					valid: false,
					reason: "model_not_found",
					message: `Model not found in registry: ${modelRef}`,
					provider: parsed.provider,
					model: parsed.model
				}
			});
		}
		return c.json({
			ok: true,
			payload: {
				valid: true,
				provider: parsed.provider,
				model: parsed.model
			}
		});
	});
}
//#endregion
export { registerAgentsRoutes };

//# sourceMappingURL=agents.js.map