import { createOpenAI } from "@ai-sdk/openai";
import { XaiModels } from "../constants/enums.js";
import { BaseProvider } from "../core/baseProvider.js";
import { DEFAULT_MAX_STEPS } from "../core/constants.js";
import { streamAnalyticsCollector } from "../core/streamAnalytics.js";
import { isNeuroLink } from "../neurolink.js";
import { createLoggingFetch } from "../utils/loggingFetch.js";
import { tracers, ATTR, withClientStreamSpan } from "../telemetry/index.js";
import { AuthenticationError, InvalidModelError, NetworkError, ProviderError, RateLimitError, } from "../types/index.js";
import { logger } from "../utils/logger.js";
import { createXaiConfig, getProviderModel, validateApiKey, } from "../utils/providerConfig.js";
import { composeAbortSignals, createTimeoutController, TimeoutError, } from "../utils/timeout.js";
import { emitToolEndFromStepFinish } from "../utils/toolEndEmitter.js";
import { resolveToolChoice } from "../utils/toolChoice.js";
import { toAnalyticsStreamResult } from "./providerTypeUtils.js";
import { stepCountIs } from "../utils/tool.js";
import { streamText } from "../utils/generation.js";
/**
 * Logging fetch wrapper — masks the proxy URL on non-2xx responses so
 * stack traces and log lines don't leak the upstream's internal token /
 * tenant id (mirrors the deepseek/groq/etc. providers).
 */
const XAI_DEFAULT_BASE_URL = "https://api.x.ai/v1";
const getXaiApiKey = () => validateApiKey(createXaiConfig());
const getDefaultXaiModel = () => getProviderModel("XAI_MODEL", XaiModels.GROK_3);
/**
 * xAI Grok Provider
 *
 * OpenAI-compatible chat completions at api.x.ai/v1. Supports the Grok
 * family: grok-2, grok-3, grok-3-mini, grok-2-vision-latest (multimodal),
 * and grok-beta. Streaming and tool calling supported.
 *
 * @see https://docs.x.ai/api
 */
export class XaiProvider extends BaseProvider {
    model;
    apiKey;
    baseURL;
    constructor(modelName, sdk, _region, credentials) {
        const validatedNeurolink = isNeuroLink(sdk) ? sdk : undefined;
        super(modelName, "xai", validatedNeurolink);
        const overrideApiKey = credentials?.apiKey?.trim();
        this.apiKey =
            overrideApiKey && overrideApiKey.length > 0
                ? overrideApiKey
                : getXaiApiKey();
        this.baseURL =
            credentials?.baseURL ?? process.env.XAI_BASE_URL ?? XAI_DEFAULT_BASE_URL;
        const xai = createOpenAI({
            apiKey: this.apiKey,
            baseURL: this.baseURL,
            fetch: createLoggingFetch("xai"),
        });
        this.model = xai.chat(this.modelName);
        logger.debug("xAI Provider initialized", {
            modelName: this.modelName,
            providerName: this.providerName,
            baseURL: this.baseURL,
        });
    }
    async executeStream(options, _analysisSchema) {
        return withClientStreamSpan({
            name: "neurolink.provider.stream",
            tracer: tracers.provider,
            attributes: {
                [ATTR.GEN_AI_SYSTEM]: "xai",
                [ATTR.GEN_AI_MODEL]: this.modelName,
                [ATTR.GEN_AI_OPERATION]: "stream",
                [ATTR.NL_STREAM_MODE]: true,
            },
        }, async () => this.executeStreamInner(options), (r) => r.stream, (r, wrapped) => ({ ...r, stream: wrapped }));
    }
    async executeStreamInner(options) {
        this.validateStreamOptions(options);
        const startTime = Date.now();
        const timeout = this.getTimeout(options);
        const timeoutController = createTimeoutController(timeout, this.providerName, "stream");
        try {
            const shouldUseTools = !options.disableTools && this.supportsTools();
            const tools = shouldUseTools
                ? options.tools || (await this.getAllTools())
                : {};
            const messages = await this.buildMessagesForStream(options);
            const model = await this.getAISDKModelWithMiddleware(options);
            const result = await streamText({
                model,
                messages,
                temperature: options.temperature,
                maxOutputTokens: options.maxTokens,
                tools,
                stopWhen: stepCountIs(options.maxSteps || DEFAULT_MAX_STEPS),
                toolChoice: resolveToolChoice(options, tools, shouldUseTools),
                abortSignal: composeAbortSignals(options.abortSignal, timeoutController?.controller.signal),
                experimental_telemetry: this.telemetryHandler.getTelemetryConfig(options),
                experimental_repairToolCall: this.getToolCallRepairFn(options),
                onStepFinish: ({ toolCalls, toolResults }) => {
                    emitToolEndFromStepFinish(this.neurolink?.getEventEmitter(), toolResults);
                    this.handleToolExecutionStorage(toolCalls, toolResults, options, new Date()).catch((error) => {
                        logger.warn("[XaiProvider] Failed to store tool executions", {
                            provider: this.providerName,
                            error: error instanceof Error ? error.message : String(error),
                        });
                    });
                },
            });
            timeoutController?.cleanup();
            const transformedStream = this.createTextStream(result);
            const analyticsPromise = streamAnalyticsCollector.createAnalytics(this.providerName, this.modelName, toAnalyticsStreamResult(result), Date.now() - startTime, {
                requestId: `xai-stream-${Date.now()}`,
                streamingMode: true,
            });
            return {
                stream: transformedStream,
                provider: this.providerName,
                model: this.modelName,
                analytics: analyticsPromise,
                metadata: { startTime, streamId: `xai-${Date.now()}` },
            };
        }
        catch (error) {
            timeoutController?.cleanup();
            throw this.handleProviderError(error);
        }
    }
    getProviderName() {
        return this.providerName;
    }
    getDefaultModel() {
        return getDefaultXaiModel();
    }
    getAISDKModel() {
        return this.model;
    }
    formatProviderError(error) {
        if (error instanceof TimeoutError) {
            return new NetworkError(`Request timed out: ${error.message}`, "xai");
        }
        const errorRecord = error;
        const message = typeof errorRecord?.message === "string"
            ? errorRecord.message
            : "Unknown error";
        if (message.includes("Invalid API key") ||
            message.includes("Authentication") ||
            message.includes("401") ||
            message.includes("invalid_api_key")) {
            return new AuthenticationError("Invalid xAI API key. Please check your XAI_API_KEY environment variable. Get one at https://console.x.ai/", "xai");
        }
        if (message.includes("rate limit") || message.includes("429")) {
            return new RateLimitError("xAI rate limit exceeded. Back off and retry.", "xai");
        }
        if (message.includes("model_not_found") || message.includes("404")) {
            return new InvalidModelError(`xAI model '${this.modelName}' not found. Use grok-2-latest, grok-3, grok-3-mini, grok-2-vision-latest, or grok-beta.`, "xai");
        }
        if (message.includes("insufficient_quota") ||
            message.includes("quota exceeded")) {
            return new ProviderError("xAI account has insufficient quota. Top up at https://console.x.ai/", "xai");
        }
        return new ProviderError(`xAI error: ${message}`, "xai");
    }
    async validateConfiguration() {
        return typeof this.apiKey === "string" && this.apiKey.trim().length > 0;
    }
    getConfiguration() {
        return {
            provider: this.providerName,
            model: this.modelName,
            defaultModel: getDefaultXaiModel(),
            baseURL: this.baseURL,
        };
    }
}
export default XaiProvider;
//# sourceMappingURL=xai.js.map