import { callClaudeTerminal } from '../claude-terminal/client.js';
import { resolveClaudeCliPath } from '../config/index.js';
import { USAGE_MISSING_REASONS } from '../../core/logging/contracts.js';
import { validateClaudeEffortCompatibility } from '../../core/workflow/claude-effort-compatibility.js';
import { AGENT_FAILURE_CATEGORIES } from '../../shared/types/agent-failure.js';
import { getErrorMessage } from '../../shared/utils/index.js';
function createProviderErrorResponse(agentName, options, message) {
    return {
        persona: agentName,
        status: 'error',
        content: message,
        timestamp: new Date(),
        sessionId: options.sessionId,
        error: message,
        failureCategory: AGENT_FAILURE_CATEGORIES.PROVIDER_ERROR,
        providerUsage: {
            usageMissing: true,
            reason: USAGE_MISSING_REASONS.NOT_SUPPORTED_BY_PROVIDER,
        },
    };
}
function createCaughtProviderErrorResponse(agentName, options, error) {
    return createProviderErrorResponse(agentName, options, `Claude terminal provider failed: ${getErrorMessage(error)}`);
}
function toTerminalOptions(options) {
    const claudeOptions = options.providerOptions?.claude;
    const terminalOptions = options.providerOptions?.claudeTerminal;
    validateClaudeEffortCompatibility(options.model, claudeOptions?.effort);
    return {
        cwd: options.cwd,
        abortSignal: options.abortSignal,
        sessionId: options.sessionId,
        model: options.model,
        effort: claudeOptions?.effort,
        allowedTools: options.allowedTools,
        mcpServers: options.mcpServers,
        ...(options.maxTurns !== undefined ? { maxTurns: options.maxTurns } : {}),
        permissionMode: options.permissionMode,
        bypassPermissions: options.bypassPermissions,
        backend: terminalOptions?.backend,
        timeoutMs: terminalOptions?.timeoutMs,
        keepSession: terminalOptions?.keepSession,
        transcriptPollIntervalMs: terminalOptions?.transcriptPollIntervalMs,
        onStream: options.onStream,
        onPermissionRequest: options.onPermissionRequest,
        onAskUserQuestion: options.onAskUserQuestion,
        outputSchema: options.outputSchema,
        pathToClaudeCodeExecutable: resolveClaudeCliPath() ?? undefined,
    };
}
export class ClaudeTerminalProvider {
    supportsStructuredOutput = true;
    setup(config) {
        const { name, systemPrompt } = config;
        return {
            call: async (prompt, options) => {
                try {
                    return await callClaudeTerminal(name, prompt, {
                        ...toTerminalOptions(options),
                        systemPrompt: systemPrompt ?? undefined,
                    });
                }
                catch (error) {
                    return createCaughtProviderErrorResponse(name, options, error);
                }
            },
        };
    }
}
//# sourceMappingURL=claude-terminal.js.map