export * from "./service.js";
export * from "./protocol.js";
export * from "./websocket.js";
import { AIGatewayService, applyAIGatewayResponseSchema, getAIGatewayCompletionStructured, getAIGatewayCompletionTextResult, } from "./service.js";
import { getEnv, getServiceUrls } from '@agentuity/config';
import { createServiceAdapter, isLogger, resolveApiKey, resolveRegion, resolveServiceUrl, } from '@agentuity/client';
import { isCliApiKey } from "./protocol.js";
import { createAIGatewayWebSocketClient, AIGatewayWebSocketError, } from "./websocket.js";
import { z } from 'zod';
function normalizeOrgId(orgId) {
    const trimmed = orgId?.trim();
    return trimmed ? trimmed : undefined;
}
function resolveOrgId(orgId) {
    return (normalizeOrgId(orgId) ??
        normalizeOrgId(getEnv('AGENTUITY_ORGID')) ??
        normalizeOrgId(getEnv('AGENTUITY_ORG_ID')) ??
        normalizeOrgId(getEnv('AGENTUITY_CLOUD_ORG_ID')));
}
export const AIGatewayClientOptionsSchema = z.object({
    apiKey: z.string().optional().describe('API key for authentication'),
    url: z.string().optional().describe('Base URL for the AI Gateway API'),
    orgId: z.string().optional().describe('Organization ID for multi-tenant operations'),
    logger: z.custom(isLogger).optional().describe('Custom logger instance'),
});
export class AIGatewayClient {
    #service;
    #apiKey;
    #orgId;
    #url;
    constructor(options = {}) {
        const validatedOptions = AIGatewayClientOptionsSchema.parse(options);
        const apiKey = validatedOptions.apiKey || getEnv('AGENTUITY_AIGATEWAY_KEY') || resolveApiKey() || '';
        const serviceUrls = getServiceUrls(resolveRegion());
        const url = resolveServiceUrl({
            url: validatedOptions.url,
            envKey: 'AGENTUITY_AIGATEWAY_URL',
            fallback: serviceUrls.aigateway,
        });
        const orgId = resolveOrgId(validatedOptions.orgId);
        const { adapter } = createServiceAdapter({
            apiKey,
            orgId,
            logger: validatedOptions.logger,
        });
        this.#apiKey = apiKey;
        this.#orgId = orgId;
        this.#url = url;
        this.#service = new AIGatewayService(url, adapter);
    }
    createWebSocket(options = {}) {
        if (!this.#apiKey) {
            throw new AIGatewayWebSocketError({
                message: 'API key is required for AI Gateway WebSocket connections. Provide apiKey when constructing AIGatewayClient or set AGENTUITY_AIGATEWAY_KEY / AGENTUITY_SDK_KEY.',
                code: 'connection_error',
            });
        }
        if (isCliApiKey(this.#apiKey) && !this.#orgId) {
            throw new AIGatewayWebSocketError({
                message: 'Organization ID is required for AI Gateway WebSocket connections when using a CLI API key (ck_*). Provide orgId when constructing AIGatewayClient or set AGENTUITY_ORGID / AGENTUITY_ORG_ID / AGENTUITY_CLOUD_ORG_ID.',
                code: 'connection_error',
            });
        }
        return createAIGatewayWebSocketClient({
            apiKey: this.#apiKey,
            orgId: this.#orgId,
            ...options,
            url: options.url ?? this.#url,
        });
    }
    async listModels() {
        return this.#service.listModels();
    }
    async complete(params) {
        return this.#service.complete(params);
    }
    async completeText(params) {
        const completion = await this.#service.complete(params);
        const result = getAIGatewayCompletionTextResult(completion);
        return { ...result, completion };
    }
    async completeStructured(params) {
        const { family } = applyAIGatewayResponseSchema(params);
        const completion = await this.#service.complete(params);
        const data = getAIGatewayCompletionStructured(completion, family);
        return { data, completion, family };
    }
    async request(options) {
        return this.#service.request(options);
    }
    async streamRequest(options) {
        return this.#service.streamRequest(options);
    }
}
//# sourceMappingURL=index.js.map