"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.submitPrompt = submitPrompt;
exports.queryResponse = queryResponse;
const i18n_1 = __importDefault(require("../../locales/i18n.js"));
const cn_ai_filter_1 = require("../../cn-ai-filter");
const ai_request_1 = require("../../service/ai-request.js");
/**
 * 提交 prompt
 * @param params 请求参数
 * @returns 响应结果
 */
async function submitPrompt(params, bizRequestConfig) {
    var _a, _b;
    try {
        const userId = (_a = (0, cn_ai_filter_1.getOperator)()) !== null && _a !== void 0 ? _a : params.userId;
        const data = await (0, ai_request_1.aiRequest)({
            url: (_b = bizRequestConfig === null || bizRequestConfig === void 0 ? void 0 : bizRequestConfig.agent) === null || _b === void 0 ? void 0 : _b.submitPromptUrl,
            path: '/aiTableAgent/submitPrompt.do',
            method: 'POST',
            data: { ...params, userId },
        });
        if (!data || !data.respMessageId || !data.conversationId) {
            console.warn('[submitPrompt] 返回数据格式异常:', data);
            return undefined;
        }
        return {
            respMessageId: data.respMessageId,
            conversationId: data.conversationId,
        };
    }
    catch (error) {
        console.error('[submitPrompt] 提交 prompt 失败:', error);
        throw error;
    }
}
/**
 * 查询 prompt 返回
 * @param params 请求参数
 * @returns 响应结果
 */
async function queryResponse(params, bizRequestConfig) {
    var _a;
    try {
        const data = await (0, ai_request_1.aiRequest)({
            url: (_a = bizRequestConfig === null || bizRequestConfig === void 0 ? void 0 : bizRequestConfig.agent) === null || _a === void 0 ? void 0 : _a.queryResponseUrl,
            path: '/aiTableAgent/queryResponse.do',
            method: 'POST',
            data: params,
        });
        if (!data || (!data.text && !data.id)) {
            return {
                id: -1,
                text: i18n_1.default.get({ id: 'GeneratingReplies', dm: '正在生成回复...' }),
                messageType: 'ASSISTANT',
                onGoing: true,
            };
        }
        return {
            id: data.id,
            text: data.text || '',
            messageType: data.messageType || 'ASSISTANT',
            onGoing: data.onGoing !== undefined ? data.onGoing : false,
            metaData: data.metaData,
        };
    }
    catch (error) {
        console.error('[queryResponse] 查询 prompt 返回失败:', error);
        return {
            id: -1,
            text: i18n_1.default.get({
                id: 'SorryThereWasAnErrorInTheQuery',
                dm: '抱歉，查询回复时出现错误，请稍后重试。',
            }),
            messageType: 'ASSISTANT',
            onGoing: false,
        };
    }
}
