"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseJsonRpcResponse = parseJsonRpcResponse;
/** Parse a JSON-RPC response, handling both plain JSON and SSE-wrapped formats. Throws if no valid response is found. */
function parseJsonRpcResponse(text) {
    const trimmed = text.trim();
    try {
        return JSON.parse(trimmed);
    }
    catch {
        // Might be SSE format
    }
    const lines = trimmed.split('\n');
    for (let i = lines.length - 1; i >= 0; i--) {
        const line = lines[i];
        if (line.startsWith('data: ')) {
            try {
                return JSON.parse(line.slice(6));
            }
            catch {
                continue;
            }
        }
    }
    throw new Error(`Failed to parse JSON-RPC response: ${trimmed.slice(0, 200)}`);
}
//# sourceMappingURL=json-rpc.js.map