/**
 * OwlCoda Native Response Parser
 *
 * Parses Anthropic Messages API responses into structured AssistantResponse.
 */
/** Parse a non-streaming Anthropic Messages API response. */
export function parseResponse(raw) {
    const thinkingBlocks = [];
    const textBlocks = [];
    const toolUseBlocks = [];
    for (const block of raw.content) {
        const type = block.type;
        if (type === 'text') {
            textBlocks.push(block);
        }
        else if (type === 'tool_use') {
            toolUseBlocks.push(block);
        }
        else if (type === 'thinking') {
            // Thinking-model providers (e.g. kimi-for-coding) require the block
            // to round-trip on future turns. Keep it or the next request 400s
            // with "reasoning_content is missing".
            thinkingBlocks.push(block);
        }
    }
    const text = textBlocks.map((b) => b.text).join('');
    return {
        thinkingBlocks,
        textBlocks,
        toolUseBlocks,
        stopReason: raw.stop_reason,
        usage: {
            inputTokens: raw.usage?.input_tokens ?? 0,
            outputTokens: raw.usage?.output_tokens ?? 0,
        },
        hasToolUse: toolUseBlocks.length > 0,
        text,
    };
}
/** Parse a raw JSON response body (handles error responses). */
export function parseResponseBody(body) {
    if (!body || typeof body !== 'object') {
        return { error: 'Invalid response body' };
    }
    const obj = body;
    // Check for error response
    if (obj['type'] === 'error') {
        const errObj = obj['error'];
        return { error: errObj?.['message'] ?? 'Unknown API error' };
    }
    // Must be a messages response
    if (obj['type'] !== 'message' || !Array.isArray(obj['content'])) {
        return { error: 'Unexpected response format' };
    }
    return parseResponse(obj);
}
/** Type guard for error results. */
export function isErrorResult(result) {
    return 'error' in result;
}
//# sourceMappingURL=response.js.map