/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
/**
 * Utility class for extracting content and tool calls from provider-specific streaming responses
 */
export class ProviderContentExtractor {
    /**
     * Extract text content from a streaming chunk based on provider format
     */
    extractContentFromChunk(chunk) {
        if (!chunk || typeof chunk !== 'object') {
            return '';
        }
        try {
            // Handle Gemini format
            if ('candidates' in chunk &&
                Array.isArray(chunk.candidates)) {
                return this.extractGeminiContent(chunk);
            }
            // Handle OpenAI format
            if ('choices' in chunk &&
                Array.isArray(chunk.choices)) {
                return this.extractOpenAIContent(chunk);
            }
            // Handle Anthropic format
            if ('type' in chunk) {
                return this.extractAnthropicContent(chunk);
            }
            // Fallback: try to extract any text content
            return this.extractGenericContent(chunk);
        }
        catch (error) {
            console.warn('Error extracting content from chunk:', error);
            return '';
        }
    }
    /**
     * Extract tool calls from a streaming chunk based on provider format
     */
    extractToolCallsFromChunk(chunk) {
        if (!chunk || typeof chunk !== 'object') {
            return [];
        }
        try {
            // Handle Gemini function calls
            if ('candidates' in chunk) {
                return this.extractGeminiToolCalls(chunk);
            }
            // Handle OpenAI function calls
            if ('choices' in chunk) {
                return this.extractOpenAIToolCalls(chunk);
            }
            // Handle Anthropic tool use
            if ('type' in chunk) {
                return this.extractAnthropicToolCalls(chunk);
            }
            return [];
        }
        catch (error) {
            console.warn('Error extracting tool calls from chunk:', error);
            return [];
        }
    }
    extractGeminiContent(chunk) {
        const candidates = chunk.candidates;
        const candidate = candidates?.[0];
        if (!candidate)
            return '';
        // Handle text content
        const content = candidate.content;
        const parts = content?.parts;
        if (parts) {
            const textParts = parts
                .filter((part) => part.text)
                .map((part) => part.text);
            return textParts.join('');
        }
        return '';
    }
    extractOpenAIContent(chunk) {
        const choices = chunk.choices;
        const choice = choices?.[0];
        if (!choice)
            return '';
        // Handle streaming content
        const delta = choice.delta;
        if (delta?.content) {
            return delta.content;
        }
        // Handle complete content
        const message = choice.message;
        if (message?.content) {
            return message.content;
        }
        return '';
    }
    extractAnthropicContent(chunk) {
        // Handle different Anthropic event types
        switch (chunk.type) {
            case 'content_block_delta': {
                const delta = chunk.delta;
                return delta?.text || '';
            }
            case 'content_block_start': {
                const contentBlock = chunk.content_block;
                return contentBlock?.text || '';
            }
            case 'message_delta': {
                const delta = chunk.delta;
                return delta?.text || '';
            }
            default:
                return '';
        }
    }
    extractGenericContent(chunk) {
        // Try common content patterns
        if (chunk.text)
            return chunk.text;
        if (chunk.content)
            return chunk.content;
        if (chunk.message)
            return chunk.message;
        const delta = chunk.delta;
        if (delta?.text)
            return delta.text;
        return '';
    }
    extractGeminiToolCalls(chunk) {
        const candidates = chunk.candidates;
        const candidate = candidates?.[0];
        if (!candidate)
            return [];
        const content = candidate.content;
        const parts = content?.parts;
        if (!parts)
            return [];
        return parts
            .filter((part) => part.functionCall)
            .map((part) => ({
            provider: 'gemini',
            name: part.functionCall.name,
            arguments: part.functionCall.args,
            id: part.functionCall.id ||
                this.generateToolCallId(),
        }));
    }
    extractOpenAIToolCalls(chunk) {
        const choices = chunk.choices;
        const choice = choices?.[0];
        if (!choice)
            return [];
        // Handle streaming tool calls
        const delta = choice.delta;
        const toolCalls = delta?.tool_calls;
        if (toolCalls) {
            return toolCalls.map((call) => ({
                provider: 'openai',
                name: call.function?.name,
                arguments: call.function?.arguments,
                id: call.id,
            }));
        }
        // Handle complete tool calls
        const message = choice.message;
        const messageToolCalls = message?.tool_calls;
        if (messageToolCalls) {
            return messageToolCalls.map((call) => ({
                provider: 'openai',
                name: call.function.name,
                arguments: JSON.parse(call.function.arguments ||
                    '{}'),
                id: call.id,
            }));
        }
        return [];
    }
    extractAnthropicToolCalls(chunk) {
        if (chunk.type === 'tool_use') {
            return [
                {
                    provider: 'anthropic',
                    name: chunk.name,
                    arguments: chunk.input,
                    id: chunk.id,
                },
            ];
        }
        return [];
    }
    generateToolCallId() {
        return `tc_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
    }
}
//# sourceMappingURL=ProviderContentExtractor.js.map