var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { z } from "zod";
import { createSseEventEmitter } from "../surfaces/web-sse/createSseEventEmitter.js";
const agentResponseBodySchema = z.object({
    message: z.string(),
    sessionId: z.string(),
    mode: z.string().nullish(),
    timeZone: z.string().optional(),
    currentPage: z.custom().optional(),
}).strict();
const agentSpeechResponseBodySchema = agentResponseBodySchema.omit({ message: true });
export function setupCoreEndpoints(ctx, server) {
    server.endpoint({
        method: 'POST',
        path: `/agent/get-placeholder-messages`,
        handler: (_a) => __awaiter(this, [_a], void 0, function* ({ headers, adminUser }) {
            if (!ctx.options.placeholderMessages) {
                return {
                    messages: [],
                };
            }
            const messages = yield ctx.options.placeholderMessages({
                adminUser: adminUser,
                headers,
            });
            return {
                messages,
            };
        })
    });
    server.endpoint({
        method: 'POST',
        path: `/agent/response`,
        handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, adminUser, response, _raw_express_res, abortSignal }) {
            var _b;
            const data = ctx.parseBody(agentResponseBodySchema, body, response);
            if (!data)
                return;
            const emit = createSseEventEmitter(_raw_express_res, {
                vercelAiUiMessageStream: true,
                closeActiveBlockOnToolStart: true,
            });
            yield ctx.handleTurn({
                prompt: data.message,
                sessionId: data.sessionId,
                modeName: data.mode,
                userTimeZone: (_b = data.timeZone) !== null && _b !== void 0 ? _b : 'UTC',
                currentPage: data.currentPage,
                abortSignal,
                adminUser: adminUser,
                emit,
                failureLogMessage: "Agent response streaming failed",
                abortLogMessage: "Agent response streaming aborted by the client",
            });
            return null;
        })
    });
    server.endpoint({
        method: 'POST',
        path: `/agent/speech-response`,
        target: 'upload',
        handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, adminUser, response, _raw_express_req, _raw_express_res, abortSignal }) {
            var _b;
            const req = _raw_express_req;
            const audioAdapter = ctx.options.audioAdapter;
            if (!audioAdapter) {
                response.setStatus(400, "Audio adapter is not configured for AdminForth Agent");
                return { error: "Audio adapter is not configured for AdminForth Agent" };
            }
            const data = ctx.parseBody(agentSpeechResponseBodySchema, body, response);
            if (!data)
                return;
            if (!req.file) {
                response.setStatus(400, "Audio file is required");
                return { error: "Audio file is required" };
            }
            const emit = createSseEventEmitter(_raw_express_res);
            yield ctx.handleSpeechTurn({
                audioAdapter,
                audio: {
                    buffer: req.file.buffer,
                    filename: req.file.originalname,
                    mimeType: req.file.mimetype,
                },
                sessionId: data.sessionId,
                modeName: data.mode,
                userTimeZone: (_b = data.timeZone) !== null && _b !== void 0 ? _b : 'UTC',
                currentPage: data.currentPage,
                abortSignal,
                adminUser: adminUser,
                emit,
                failureLogMessage: "Agent speech response failed",
                abortLogMessage: "Agent speech response aborted by the client",
            });
            return null;
        })
    });
}
