import { getLogger as getLogger$1 } from "../../../logger.js";
import { a2aHandler } from "../a2a/handlers.js";
import { getRegisteredAgent } from "../data/agents.js";
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
import { HeadersScopeSchema, createApiError } from "@inkeep/agents-core";

//#region src/domains/run/routes/agents.ts
const app = new OpenAPIHono();
const logger = getLogger$1("agents");
app.openapi(createRoute({
	method: "get",
	path: "/.well-known/agent.json",
	request: { headers: HeadersScopeSchema },
	tags: ["A2A"],
	security: [{ bearerAuth: [] }],
	responses: {
		200: {
			description: "Agent Card for A2A discovery",
			content: { "application/json": { schema: z.object({
				name: z.string(),
				description: z.string().optional(),
				url: z.string(),
				version: z.string(),
				defaultInputModes: z.array(z.string()),
				defaultOutputModes: z.array(z.string()),
				skills: z.array(z.any())
			}) } }
		},
		404: { description: "Agent not found" }
	}
}), async (c) => {
	const otelHeaders = {
		traceparent: c.req.header("traceparent"),
		tracestate: c.req.header("tracestate"),
		baggage: c.req.header("baggage")
	};
	logger.info({
		otelHeaders,
		path: c.req.path,
		method: c.req.method
	}, "OpenTelemetry headers: well-known agent.json");
	const executionContext = c.get("executionContext");
	const { tenantId, projectId, agentId, subAgentId } = executionContext;
	logger.info({
		message: "getRegisteredAgent (agent-level)",
		tenantId,
		projectId,
		agentId,
		subAgentId
	}, "agent-level well-known agent.json");
	const agent = await getRegisteredAgent({
		executionContext,
		credentialStoreRegistry: c.get("credentialStores"),
		sandboxConfig: c.get("sandboxConfig")
	});
	logger.info({ agent }, "agent registered: well-known agent.json");
	if (!agent) throw createApiError({
		code: "not_found",
		message: "Agent not found"
	});
	return c.json(agent.agentCard);
});
app.post("/a2a", async (c) => {
	const otelHeaders = {
		traceparent: c.req.header("traceparent"),
		tracestate: c.req.header("tracestate"),
		baggage: c.req.header("baggage")
	};
	logger.info({
		otelHeaders,
		path: c.req.path,
		method: c.req.method
	}, "OpenTelemetry headers: a2a");
	const executionContext = c.get("executionContext");
	const { tenantId, projectId, agentId, subAgentId, project } = executionContext;
	if (subAgentId) {
		logger.info({
			message: "a2a (agent-level)",
			tenantId,
			projectId,
			agentId,
			subAgentId
		}, "agent-level a2a endpoint");
		const agent$1 = await getRegisteredAgent({
			executionContext,
			credentialStoreRegistry: c.get("credentialStores"),
			sandboxConfig: c.get("sandboxConfig")
		});
		if (!agent$1) return c.json({
			jsonrpc: "2.0",
			error: {
				code: -32004,
				message: "Agent not found"
			},
			id: null
		}, 404);
		return a2aHandler(c, agent$1);
	}
	logger.info({
		message: "a2a (agent-level)",
		tenantId,
		projectId,
		agentId
	}, "agent-level a2a endpoint");
	const agent = project.agents[agentId];
	if (!agent) return c.json({
		jsonrpc: "2.0",
		error: {
			code: -32004,
			message: "Agent not found"
		},
		id: null
	}, 404);
	if (!agent.defaultSubAgentId) return c.json({
		jsonrpc: "2.0",
		error: {
			code: -32004,
			message: "Agent does not have a default agent configured"
		},
		id: null
	}, 400);
	executionContext.subAgentId = agent.defaultSubAgentId;
	const defaultSubAgent = await getRegisteredAgent({
		executionContext,
		credentialStoreRegistry: c.get("credentialStores"),
		sandboxConfig: c.get("sandboxConfig")
	});
	if (!defaultSubAgent) return c.json({
		jsonrpc: "2.0",
		error: {
			code: -32004,
			message: "Agent not found"
		},
		id: null
	}, 404);
	return a2aHandler(c, defaultSubAgent);
});
var agents_default = app;

//#endregion
export { agents_default as default };