"use strict";
/**
 * Cursor Protobuf Main Module
 * Exports encoder/decoder functions and builds complete requests
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractTextFromResponse = exports.parseConnectRPCFrame = exports.decodeMessage = exports.decodeField = exports.decodeVarint = exports.wrapConnectRPCFrame = exports.encodeMcpTool = exports.encodeMessageId = exports.encodeMetadata = exports.encodeCursorSetting = exports.encodeModel = exports.encodeInstruction = exports.encodeMessage = exports.encodeField = exports.encodeVarint = exports.generateCursorBody = exports.buildChatRequest = exports.encodeRequest = void 0;
const crypto_1 = require("crypto");
const cursor_protobuf_schema_js_1 = require("./cursor-protobuf-schema.js");
const cursor_protobuf_encoder_js_1 = require("./cursor-protobuf-encoder.js");
Object.defineProperty(exports, "encodeField", { enumerable: true, get: function () { return cursor_protobuf_encoder_js_1.encodeField; } });
Object.defineProperty(exports, "encodeVarint", { enumerable: true, get: function () { return cursor_protobuf_encoder_js_1.encodeVarint; } });
Object.defineProperty(exports, "encodeMessage", { enumerable: true, get: function () { return cursor_protobuf_encoder_js_1.encodeMessage; } });
Object.defineProperty(exports, "encodeInstruction", { enumerable: true, get: function () { return cursor_protobuf_encoder_js_1.encodeInstruction; } });
Object.defineProperty(exports, "encodeModel", { enumerable: true, get: function () { return cursor_protobuf_encoder_js_1.encodeModel; } });
Object.defineProperty(exports, "encodeCursorSetting", { enumerable: true, get: function () { return cursor_protobuf_encoder_js_1.encodeCursorSetting; } });
Object.defineProperty(exports, "encodeMetadata", { enumerable: true, get: function () { return cursor_protobuf_encoder_js_1.encodeMetadata; } });
Object.defineProperty(exports, "encodeMessageId", { enumerable: true, get: function () { return cursor_protobuf_encoder_js_1.encodeMessageId; } });
Object.defineProperty(exports, "encodeMcpTool", { enumerable: true, get: function () { return cursor_protobuf_encoder_js_1.encodeMcpTool; } });
Object.defineProperty(exports, "wrapConnectRPCFrame", { enumerable: true, get: function () { return cursor_protobuf_encoder_js_1.wrapConnectRPCFrame; } });
const cursor_protobuf_decoder_js_1 = require("./cursor-protobuf-decoder.js");
Object.defineProperty(exports, "decodeVarint", { enumerable: true, get: function () { return cursor_protobuf_decoder_js_1.decodeVarint; } });
Object.defineProperty(exports, "decodeField", { enumerable: true, get: function () { return cursor_protobuf_decoder_js_1.decodeField; } });
Object.defineProperty(exports, "decodeMessage", { enumerable: true, get: function () { return cursor_protobuf_decoder_js_1.decodeMessage; } });
Object.defineProperty(exports, "parseConnectRPCFrame", { enumerable: true, get: function () { return cursor_protobuf_decoder_js_1.parseConnectRPCFrame; } });
Object.defineProperty(exports, "extractTextFromResponse", { enumerable: true, get: function () { return cursor_protobuf_decoder_js_1.extractTextFromResponse; } });
const cursor_protobuf_schema_js_2 = require("./cursor-protobuf-schema.js");
/**
 * Build complete chat request protobuf
 */
function encodeRequest(messages, modelName, tools = [], reasoningEffort = null) {
    if (messages.length === 0) {
        throw new Error('Messages array must not be empty');
    }
    const hasTools = tools?.length > 0;
    const isAgentic = hasTools;
    const formattedMessages = [];
    const messageIds = [];
    // Prepare messages
    for (let i = 0; i < messages.length; i++) {
        const msg = messages[i];
        const role = msg.role === 'user' ? cursor_protobuf_schema_js_1.ROLE.USER : cursor_protobuf_schema_js_1.ROLE.ASSISTANT;
        const msgId = (0, crypto_1.randomUUID)();
        const isLast = i === messages.length - 1;
        formattedMessages.push({
            content: msg.content,
            role,
            messageId: msgId,
            isLast,
            hasTools,
            toolResults: msg.tool_results || [],
        });
        messageIds.push({ messageId: msgId, role });
    }
    // Map reasoning effort to thinking level
    let thinkingLevel = cursor_protobuf_schema_js_1.THINKING_LEVEL.UNSPECIFIED;
    if (reasoningEffort === 'medium')
        thinkingLevel = cursor_protobuf_schema_js_1.THINKING_LEVEL.MEDIUM;
    else if (reasoningEffort === 'high')
        thinkingLevel = cursor_protobuf_schema_js_1.THINKING_LEVEL.HIGH;
    // Build arrays for messages and tools
    const messageFields = formattedMessages.map((fm) => (0, cursor_protobuf_encoder_js_1.encodeField)(cursor_protobuf_schema_js_1.FIELD.Chat.MESSAGES, cursor_protobuf_schema_js_2.WIRE_TYPE.LEN, (0, cursor_protobuf_encoder_js_1.encodeMessage)(fm.content, fm.role, fm.messageId, fm.isLast, fm.hasTools, fm.toolResults)));
    const messageIdFields = messageIds.map((mid) => (0, cursor_protobuf_encoder_js_1.encodeField)(cursor_protobuf_schema_js_1.FIELD.Chat.MESSAGE_IDS, cursor_protobuf_schema_js_2.WIRE_TYPE.LEN, (0, cursor_protobuf_encoder_js_1.encodeMessageId)(mid.messageId, mid.role)));
    const toolFields = tools?.length > 0
        ? tools.map((tool) => (0, cursor_protobuf_encoder_js_1.encodeField)(cursor_protobuf_schema_js_1.FIELD.Chat.MCP_TOOLS, cursor_protobuf_schema_js_2.WIRE_TYPE.LEN, (0, cursor_protobuf_encoder_js_1.encodeMcpTool)(tool)))
        : [];
    const supportedToolsField = isAgentic
        ? [(0, cursor_protobuf_encoder_js_1.encodeField)(cursor_protobuf_schema_js_1.FIELD.Chat.SUPPORTED_TOOLS, cursor_protobuf_schema_js_2.WIRE_TYPE.LEN, (0, cursor_protobuf_encoder_js_1.encodeVarint)(1))]
        : [];
    // Concatenate all parts
    const parts = [
        ...messageFields,
        (0, cursor_protobuf_encoder_js_1.encodeField)(cursor_protobuf_schema_js_1.FIELD.Chat.UNKNOWN_2, cursor_protobuf_schema_js_2.WIRE_TYPE.VARINT, 1),
        (0, cursor_protobuf_encoder_js_1.encodeField)(cursor_protobuf_schema_js_1.FIELD.Chat.INSTRUCTION, cursor_protobuf_schema_js_2.WIRE_TYPE.LEN, (0, cursor_protobuf_encoder_js_1.encodeInstruction)('')),
        (0, cursor_protobuf_encoder_js_1.encodeField)(cursor_protobuf_schema_js_1.FIELD.Chat.UNKNOWN_4, cursor_protobuf_schema_js_2.WIRE_TYPE.VARINT, 1),
        (0, cursor_protobuf_encoder_js_1.encodeField)(cursor_protobuf_schema_js_1.FIELD.Chat.MODEL, cursor_protobuf_schema_js_2.WIRE_TYPE.LEN, (0, cursor_protobuf_encoder_js_1.encodeModel)(modelName)),
        (0, cursor_protobuf_encoder_js_1.encodeField)(cursor_protobuf_schema_js_1.FIELD.Chat.WEB_TOOL, cursor_protobuf_schema_js_2.WIRE_TYPE.LEN, ''),
        (0, cursor_protobuf_encoder_js_1.encodeField)(cursor_protobuf_schema_js_1.FIELD.Chat.UNKNOWN_13, cursor_protobuf_schema_js_2.WIRE_TYPE.VARINT, 1),
        (0, cursor_protobuf_encoder_js_1.encodeField)(cursor_protobuf_schema_js_1.FIELD.Chat.CURSOR_SETTING, cursor_protobuf_schema_js_2.WIRE_TYPE.LEN, (0, cursor_protobuf_encoder_js_1.encodeCursorSetting)()),
        (0, cursor_protobuf_encoder_js_1.encodeField)(cursor_protobuf_schema_js_1.FIELD.Chat.UNKNOWN_19, cursor_protobuf_schema_js_2.WIRE_TYPE.VARINT, 1),
        (0, cursor_protobuf_encoder_js_1.encodeField)(cursor_protobuf_schema_js_1.FIELD.Chat.CONVERSATION_ID, cursor_protobuf_schema_js_2.WIRE_TYPE.LEN, (0, crypto_1.randomUUID)()),
        (0, cursor_protobuf_encoder_js_1.encodeField)(cursor_protobuf_schema_js_1.FIELD.Chat.METADATA, cursor_protobuf_schema_js_2.WIRE_TYPE.LEN, (0, cursor_protobuf_encoder_js_1.encodeMetadata)()),
        (0, cursor_protobuf_encoder_js_1.encodeField)(cursor_protobuf_schema_js_1.FIELD.Chat.IS_AGENTIC, cursor_protobuf_schema_js_2.WIRE_TYPE.VARINT, isAgentic ? 1 : 0),
        ...supportedToolsField,
        ...messageIdFields,
        ...toolFields,
        (0, cursor_protobuf_encoder_js_1.encodeField)(cursor_protobuf_schema_js_1.FIELD.Chat.LARGE_CONTEXT, cursor_protobuf_schema_js_2.WIRE_TYPE.VARINT, 0),
        (0, cursor_protobuf_encoder_js_1.encodeField)(cursor_protobuf_schema_js_1.FIELD.Chat.UNKNOWN_38, cursor_protobuf_schema_js_2.WIRE_TYPE.VARINT, 0),
        (0, cursor_protobuf_encoder_js_1.encodeField)(cursor_protobuf_schema_js_1.FIELD.Chat.UNIFIED_MODE, cursor_protobuf_schema_js_2.WIRE_TYPE.VARINT, isAgentic ? cursor_protobuf_schema_js_1.UNIFIED_MODE.AGENT : cursor_protobuf_schema_js_1.UNIFIED_MODE.CHAT),
        (0, cursor_protobuf_encoder_js_1.encodeField)(cursor_protobuf_schema_js_1.FIELD.Chat.UNKNOWN_47, cursor_protobuf_schema_js_2.WIRE_TYPE.LEN, ''),
        (0, cursor_protobuf_encoder_js_1.encodeField)(cursor_protobuf_schema_js_1.FIELD.Chat.SHOULD_DISABLE_TOOLS, cursor_protobuf_schema_js_2.WIRE_TYPE.VARINT, isAgentic ? 0 : 1),
        (0, cursor_protobuf_encoder_js_1.encodeField)(cursor_protobuf_schema_js_1.FIELD.Chat.THINKING_LEVEL, cursor_protobuf_schema_js_2.WIRE_TYPE.VARINT, thinkingLevel),
        (0, cursor_protobuf_encoder_js_1.encodeField)(cursor_protobuf_schema_js_1.FIELD.Chat.UNKNOWN_51, cursor_protobuf_schema_js_2.WIRE_TYPE.VARINT, 0),
        (0, cursor_protobuf_encoder_js_1.encodeField)(cursor_protobuf_schema_js_1.FIELD.Chat.UNKNOWN_53, cursor_protobuf_schema_js_2.WIRE_TYPE.VARINT, 1),
        (0, cursor_protobuf_encoder_js_1.encodeField)(cursor_protobuf_schema_js_1.FIELD.Chat.UNIFIED_MODE_NAME, cursor_protobuf_schema_js_2.WIRE_TYPE.LEN, isAgentic ? 'Agent' : 'Ask'),
    ];
    return (0, cursor_protobuf_encoder_js_1.concatArrays)(...parts);
}
exports.encodeRequest = encodeRequest;
/**
 * Build chat request wrapped in top-level message
 */
function buildChatRequest(messages, modelName, tools = [], reasoningEffort = null) {
    return (0, cursor_protobuf_encoder_js_1.encodeField)(cursor_protobuf_schema_js_1.FIELD.Request.REQUEST, cursor_protobuf_schema_js_2.WIRE_TYPE.LEN, encodeRequest(messages, modelName, tools, reasoningEffort));
}
exports.buildChatRequest = buildChatRequest;
/**
 * Generate the raw top-level protobuf request body expected by Cursor upstream.
 */
function generateCursorBody(messages, modelName, tools = [], reasoningEffort = null) {
    return buildChatRequest(messages, modelName, tools, reasoningEffort);
}
exports.generateCursorBody = generateCursorBody;
//# sourceMappingURL=cursor-protobuf.js.map