import runDbClient_default from "../../../data/db/runDbClient.js";
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
import { TenantProjectIdParamsSchema, commonGetErrorResponses, createApiError, formatMessagesForLLMContext, getConversationHistory } from "@inkeep/agents-core";

//#region src/domains/manage/routes/conversations.ts
const app = new OpenAPIHono();
const ConversationQueryParamsSchema = z.object({
	limit: z.coerce.number().min(1).max(200).default(20).optional(),
	includeInternal: z.coerce.boolean().default(false).optional()
});
const ConversationWithFormattedMessagesResponse = z.object({ data: z.object({
	messages: z.array(z.any()),
	formatted: z.object({ llmContext: z.string() })
}) }).openapi("ConversationWithFormattedMessagesResponse");
app.openapi(createRoute({
	method: "get",
	path: "/{id}",
	summary: "Get Conversation",
	operationId: "get-conversation",
	tags: ["Conversations"],
	request: {
		params: TenantProjectIdParamsSchema,
		query: ConversationQueryParamsSchema
	},
	responses: {
		200: {
			description: "Conversation found with formatted messages for LLM use",
			content: { "application/json": { schema: ConversationWithFormattedMessagesResponse } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const { tenantId, projectId, id } = c.req.valid("param");
	const { limit = 20, includeInternal = true } = c.req.valid("query");
	const messages = await getConversationHistory(runDbClient_default)({
		scopes: {
			tenantId,
			projectId
		},
		conversationId: id,
		options: {
			limit,
			includeInternal
		}
	});
	if (!messages || messages.length === 0) throw createApiError({
		code: "not_found",
		message: "Conversation not found"
	});
	const llmContext = formatMessagesForLLMContext(messages);
	return c.json({ data: {
		messages,
		formatted: { llmContext }
	} });
});
var conversations_default = app;

//#endregion
export { conversations_default as default };