/**
 * Amazon SageMaker Provider Implementation (Simplified)
 *
 * This module provides a simplified SageMaker provider that extends BaseProvider
 * and integrates with the NeuroLink ecosystem using existing patterns.
 */
import { BaseProvider } from "../core/baseProvider.js";
import { logger } from "../utils/logger.js";
// SageMaker-specific imports
import { getDefaultSageMakerEndpoint, getSageMakerConfig, getSageMakerModel, getSageMakerModelConfig, } from "./sagemaker/config.js";
import { handleSageMakerError, SageMakerError } from "./sagemaker/errors.js";
import { SageMakerLanguageModel } from "./sagemaker/language-model.js";
/**
 * Amazon SageMaker Provider extending BaseProvider
 */
export class AmazonSageMakerProvider extends BaseProvider {
    sagemakerModel;
    sagemakerConfig;
    modelConfig;
    constructor(modelName, endpointName, region, neurolink) {
        super(modelName, "sagemaker", neurolink);
        try {
            // Load and validate configuration
            this.sagemakerConfig = getSageMakerConfig(region);
            this.modelConfig = getSageMakerModelConfig(endpointName || getDefaultSageMakerEndpoint());
            // Create the SageMaker LanguageModel implementation.
            // SageMakerLanguageModel implements SageMakerAsLanguageModel which is
            // structurally compatible with LanguageModelV2 (specificationVersion "v2",
            // modelId, provider, supportedUrls, doGenerate, doStream).
            const smModel = new SageMakerLanguageModel(this.modelName, this.sagemakerConfig, this.modelConfig);
            this.sagemakerModel = smModel;
            logger.debug("Amazon SageMaker Provider initialized", {
                modelName: this.modelName,
                endpointName: this.modelConfig.endpointName,
                region: this.sagemakerConfig.region,
                provider: this.providerName,
            });
        }
        catch (error) {
            logger.error("Failed to initialize SageMaker provider", {
                error: error instanceof Error ? error.message : String(error),
                modelName,
                endpointName,
            });
            throw handleSageMakerError(error);
        }
    }
    getProviderName() {
        return "sagemaker";
    }
    getDefaultModel() {
        return getSageMakerModel();
    }
    getAISDKModel() {
        return this.sagemakerModel;
    }
    async executeStream(_options, _analysisSchema) {
        try {
            // For now, throw an error indicating this is not yet implemented
            throw new SageMakerError("SageMaker streaming not yet fully implemented. Coming in next phase.", {
                code: "MODEL_ERROR",
                statusCode: 501,
                endpoint: this.modelConfig.endpointName,
            });
        }
        catch (error) {
            throw this.handleProviderError(error);
        }
    }
    formatProviderError(error) {
        if (error instanceof SageMakerError) {
            return error;
        }
        if (error instanceof Error && error.name === "TimeoutError") {
            return new SageMakerError(`SageMaker request timed out. Consider increasing timeout.`, {
                code: "NETWORK_ERROR",
                statusCode: 408,
                cause: error,
                endpoint: this.modelConfig.endpointName,
            });
        }
        return handleSageMakerError(error, this.modelConfig.endpointName);
    }
    /**
     * Get SageMaker-specific provider information
     */
    getSageMakerInfo() {
        return {
            endpointName: this.modelConfig.endpointName,
            modelType: this.modelConfig.modelType || "custom",
            region: this.sagemakerConfig.region,
            configured: !!(this.sagemakerConfig.accessKeyId && this.sagemakerConfig.secretAccessKey),
        };
    }
    /**
     * Test basic configuration
     */
    async testConnection() {
        try {
            // Basic validation test
            if (!this.sagemakerConfig.accessKeyId ||
                !this.sagemakerConfig.secretAccessKey) {
                return {
                    connected: false,
                    error: "AWS credentials not configured",
                };
            }
            if (!this.modelConfig.endpointName ||
                this.modelConfig.endpointName === "default-endpoint") {
                return {
                    connected: false,
                    error: "SageMaker endpoint not configured",
                };
            }
            // For now, just return that configuration looks valid
            return {
                connected: true,
            };
        }
        catch (error) {
            return {
                connected: false,
                error: error instanceof Error ? error.message : String(error),
            };
        }
    }
    /**
     * Public method to get the AI SDK model for CLI and external usage
     */
    async getModel() {
        return this.getAISDKModel();
    }
    /**
     * Test connectivity to the SageMaker endpoint
     */
    async testConnectivity() {
        const model = this.sagemakerModel;
        return model.testConnectivity
            ? await model.testConnectivity()
            : { success: false, error: "Test method not available" };
    }
    /**
     * Get model capabilities and information
     */
    getModelCapabilities() {
        const model = this.sagemakerModel;
        return model.getModelCapabilities
            ? model.getModelCapabilities()
            : {
                capabilities: {
                    streaming: true,
                    toolCalling: true,
                    structuredOutput: true,
                    batchInference: true,
                    supportedResponseFormats: ["text", "json_object"],
                    supportedToolTypes: ["function"],
                    maxBatchSize: 10,
                },
            };
    }
}
export default AmazonSageMakerProvider;
