import { errInfo } from "../lib/errInfo.js";
import { logError } from "../lib/logger.js";
import { requestPromptBuilderChat } from "../lib/promptBuilder/client.js";
import { requireRuntimeContext } from "../lib/runtimeContext.js";
export function registerPromptBuilderRoutes(app, ctxRaw) {
    const ctx = requireRuntimeContext(ctxRaw);
    app.post("/api/prompt-builder/chat", async (req, res) => {
        try {
            const result = await requestPromptBuilderChat(ctx, req.body);
            res.json(result);
        }
        catch (error) {
            const info = errInfo(error);
            const code = typeof info.code === "string" ? info.code : "PROMPT_BUILDER_UNKNOWN";
            const status = typeof info.status === "number" && info.status >= 400
                ? info.status
                : 500;
            logError("prompt-builder", code, {
                message: info.message,
                status,
            });
            res.status(status).json({
                error: { code, message: info.message },
            });
        }
    });
}
