import { ModelPolicyService } from '../../../../core/llm/models/index.js';
/**
 * Provider-owned codec for translating between Heddle LLM port types and the
 * OpenAI Responses API payloads/events.
 */
export class OpenAiCodec {
    static parseStreamedToolCalls(streamedToolCalls) {
        const toolCalls = [];
        for (const entry of streamedToolCalls.values()) {
            if (!entry.argumentsText.trim()) {
                continue;
            }
            toolCalls.push({
                id: entry.id,
                tool: entry.tool,
                input: JSON.parse(entry.argumentsText),
            });
        }
        return toolCalls;
    }
    static readReasoningSummaryDeltaText(delta) {
        if (typeof delta === 'string') {
            return delta;
        }
        if (!delta || typeof delta !== 'object' || Array.isArray(delta)) {
            return undefined;
        }
        const text = delta.text;
        return typeof text === 'string' ? text : undefined;
    }
    static extractAssistantContent(response, preferToolCalls) {
        const text = response.output_text?.trim();
        if (text) {
            return text;
        }
        if (preferToolCalls) {
            return undefined;
        }
        const output = Array.isArray(response.output) ? response.output : [];
        for (const item of output) {
            if (item.type !== 'message') {
                continue;
            }
            const content = Array.isArray(item.content) ? item.content : [];
            const segment = content.find((part) => part.type === 'output_text');
            if (segment?.text?.trim()) {
                return segment.text.trim();
            }
        }
        return undefined;
    }
    static extractAssistantDiagnostics(response, preferToolCalls) {
        const output = Array.isArray(response.output) ? response.output : [];
        const reasoning = output.find((item) => item.type === 'reasoning');
        if (!reasoning || !Array.isArray(reasoning.summary)) {
            return undefined;
        }
        const rationale = reasoning.summary
            .map((summary) => summary.type === 'summary_text' ? summary.text.trim() : '')
            .find(Boolean);
        if (!rationale) {
            return undefined;
        }
        if (preferToolCalls) {
            return { rationale };
        }
        return {
            rationale,
        };
    }
    static extractUsage(response) {
        const usage = response.usage;
        if (!usage) {
            return undefined;
        }
        return {
            inputTokens: usage.input_tokens,
            outputTokens: usage.output_tokens,
            totalTokens: usage.total_tokens,
            cachedInputTokens: usage.input_tokens_details?.cached_tokens,
            reasoningTokens: usage.output_tokens_details?.reasoning_tokens,
            requests: 1,
        };
    }
    static buildResponsesRequest(messages, options) {
        const systemMessages = options.oauthMode ? messages.filter((message) => message.role === 'system') : [];
        const inputMessages = options.oauthMode ? messages.filter((message) => message.role !== 'system') : messages;
        const instructions = options.oauthMode ?
            systemMessages.map((message) => message.content.trim()).filter(Boolean).join('\n\n')
            : undefined;
        const reasoningEffort = OpenAiCodec.resolveRequestReasoningEffort({
            model: options.model,
            explicitEffort: options.reasoningEffort,
        });
        return {
            model: options.model,
            input: OpenAiCodec.toResponseInput(inputMessages),
            tools: options.tools.length > 0 ? options.tools.map((tool) => OpenAiCodec.toResponseTool(tool)) : undefined,
            store: false,
            reasoning: {
                summary: options.oauthMode ? 'auto' : 'detailed',
                ...(reasoningEffort ? { effort: reasoningEffort } : {}),
            },
            ...(instructions ? { instructions } : {}),
        };
    }
    static resolveRequestReasoningEffort(args) {
        const effectiveEffort = args.explicitEffort ?? ModelPolicyService.resolveDefaultReasoningEffort(args.model);
        if (!effectiveEffort) {
            return undefined;
        }
        if (!ModelPolicyService.supportsOpenAiRequestReasoningEffortLevel(args.model, effectiveEffort)) {
            if (args.explicitEffort) {
                throw new Error(`Reasoning effort "${effectiveEffort}" is not supported for OpenAI model ${args.model}.`);
            }
            return undefined;
        }
        return OpenAiCodec.toOpenAiReasoningEffort(effectiveEffort);
    }
    static toOpenAiReasoningEffort(value) {
        return value;
    }
    static toResponseInput(messages) {
        return messages.flatMap((message) => OpenAiCodec.toResponseInputItems(message));
    }
    static toResponseInputItems(msg) {
        switch (msg.role) {
            case 'system':
                return [{ type: 'message', role: 'system', content: msg.content }];
            case 'user':
                return [{ type: 'message', role: 'user', content: msg.content }];
            case 'assistant': {
                const items = [];
                if (msg.content) {
                    items.push({ type: 'message', role: 'assistant', content: msg.content });
                }
                if (msg.toolCalls) {
                    for (const call of msg.toolCalls) {
                        items.push({
                            type: 'function_call',
                            call_id: call.id,
                            name: call.tool,
                            arguments: JSON.stringify(call.input),
                        });
                    }
                }
                return items;
            }
            case 'tool':
                return [
                    {
                        type: 'function_call_output',
                        call_id: msg.toolCallId,
                        output: msg.content,
                    },
                ];
        }
    }
    static toResponseTool(tool) {
        return {
            type: 'function',
            name: tool.name,
            description: tool.description,
            parameters: tool.parameters,
            strict: false,
        };
    }
}
//# sourceMappingURL=openai-codec.js.map