"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const clients_1 = require("../../../src/clients");
const types_1 = require("../../../src/types");
const OUTPUT_DIR = path.join(__dirname, "outputs", "streaming");
function ensureOutputDir() {
    fs.mkdirSync(OUTPUT_DIR, { recursive: true });
}
describe("Streaming Text Generation", () => {
    let client;
    beforeAll(() => {
        ensureOutputDir();
        client = new clients_1.AIClient();
    });
    test("1. OpenAI GPT-4o – streaming text generation", async () => {
        const outputPath = path.join(OUTPUT_DIR, "openai-streaming.txt");
        if (fs.existsSync(outputPath)) {
            console.log(`Skipping: output already exists at ${outputPath}`);
            return;
        }
        if (!process.env.OPENAI_KEY) {
            console.log("Skipping: OPENAI_KEY not set");
            return;
        }
        const prompt = "Count from 1 to 10, one number per line.";
        const openaiClient = client.clients.openai;
        if (!openaiClient) {
            console.log("Skipping: OpenAI client not available");
            return;
        }
        const stream = await openaiClient.client.chat.completions.create({
            model: types_1.Models.openai.GPT_4o_Mini,
            messages: [{ role: "user", content: prompt }],
            stream: true,
            max_tokens: 100,
        });
        let fullText = "";
        const chunks = [];
        for await (const chunk of stream) {
            const content = chunk.choices[0]?.delta?.content || "";
            if (content) {
                fullText += content;
                chunks.push(content);
            }
        }
        fs.writeFileSync(outputPath, `Full text:\n${fullText}\n\nChunks (${chunks.length}):\n${chunks.join(" | ")}`);
        console.log(`✅ OpenAI streaming completed`);
        console.log(`   Output saved to: ${outputPath}`);
        console.log(`   Total chunks: ${chunks.length}`);
        console.log(`   Full text: ${fullText.substring(0, 100)}...`);
        expect(fullText).toBeTruthy();
        expect(chunks.length).toBeGreaterThan(0);
        expect(fullText).toContain("1");
        expect(fullText).toContain("10");
    }, 60000);
    test("2. Anthropic Claude – streaming text generation", async () => {
        const outputPath = path.join(OUTPUT_DIR, "anthropic-streaming.txt");
        if (fs.existsSync(outputPath)) {
            console.log(`Skipping: output already exists at ${outputPath}`);
            return;
        }
        if (!process.env.ANTHROPIC_API_KEY) {
            console.log("Skipping: ANTHROPIC_API_KEY not set");
            return;
        }
        const prompt = "Count from 1 to 10, one number per line.";
        const anthropicClient = client.clients.anthropic;
        if (!anthropicClient) {
            console.log("Skipping: Anthropic client not available");
            return;
        }
        const stream = await anthropicClient.client.messages.create({
            model: types_1.Models.anthropic.Haiku4_5,
            max_tokens: 100,
            messages: [{ role: "user", content: prompt }],
            stream: true,
        });
        let fullText = "";
        const chunks = [];
        for await (const chunk of stream) {
            if (chunk.type === "content_block_delta" &&
                chunk.delta.type === "text_delta") {
                const content = chunk.delta.text;
                if (content) {
                    fullText += content;
                    chunks.push(content);
                }
            }
        }
        fs.writeFileSync(outputPath, `Full text:\n${fullText}\n\nChunks (${chunks.length}):\n${chunks.join(" | ")}`);
        console.log(`✅ Anthropic streaming completed`);
        console.log(`   Output saved to: ${outputPath}`);
        console.log(`   Total chunks: ${chunks.length}`);
        console.log(`   Full text: ${fullText.substring(0, 100)}...`);
        expect(fullText).toBeTruthy();
        expect(chunks.length).toBeGreaterThan(0);
        expect(fullText).toContain("1");
        expect(fullText).toContain("10");
    }, 60000);
    test("3. Google Gemini – streaming text generation", async () => {
        const outputPath = path.join(OUTPUT_DIR, "google-streaming.txt");
        if (fs.existsSync(outputPath)) {
            console.log(`Skipping: output already exists at ${outputPath}`);
            return;
        }
        if (!process.env.GEMINI_API_KEY) {
            console.log("Skipping: GEMINI_API_KEY not set");
            return;
        }
        const prompt = "Count from 1 to 10, one number per line.";
        const geminiClient = client.clients.google;
        if (!geminiClient) {
            console.log("Skipping: Google client not available");
            return;
        }
        const result = await geminiClient.client.models.generateContentStream({
            model: types_1.Models.google.Gemini_20_Flash,
            contents: [{ role: 'user', parts: [{ text: prompt }] }]
        });
        let fullText = "";
        const chunks = [];
        for await (const chunk of result) {
            const chunkText = chunk.candidates?.[0]?.content?.parts?.[0]?.text || '';
            if (chunkText) {
                fullText += chunkText;
                chunks.push(chunkText);
            }
        }
        fs.writeFileSync(outputPath, `Full text:\n${fullText}\n\nChunks (${chunks.length}):\n${chunks.join(" | ")}`);
        console.log(`✅ Google Gemini streaming completed`);
        console.log(`   Output saved to: ${outputPath}`);
        console.log(`   Total chunks: ${chunks.length}`);
        console.log(`   Full text: ${fullText.substring(0, 100)}...`);
        expect(fullText).toBeTruthy();
        expect(chunks.length).toBeGreaterThan(0);
        expect(fullText).toContain("1");
        expect(fullText).toContain("10");
    }, 60000);
    test("4. XAI Grok – streaming text generation", async () => {
        const outputPath = path.join(OUTPUT_DIR, "xai-streaming.txt");
        if (fs.existsSync(outputPath)) {
            console.log(`Skipping: output already exists at ${outputPath}`);
            return;
        }
        if (!process.env.XAI_API_KEY) {
            console.log("Skipping: XAI_API_KEY not set");
            return;
        }
        const prompt = "Count from 1 to 10, one number per line.";
        const xaiClient = client.clients.xai;
        if (!xaiClient) {
            console.log("Skipping: XAI client not available");
            return;
        }
        const stream = await xaiClient.client.chat.completions.create({
            model: types_1.Models.xai.Grok3MiniFastBeta,
            messages: [{ role: "user", content: prompt }],
            stream: true,
            max_tokens: 100,
        });
        let fullText = "";
        const chunks = [];
        for await (const chunk of stream) {
            const content = chunk.choices[0]?.delta?.content || "";
            if (content) {
                fullText += content;
                chunks.push(content);
            }
        }
        fs.writeFileSync(outputPath, `Full text:\n${fullText}\n\nChunks (${chunks.length}):\n${chunks.join(" | ")}`);
        console.log(`✅ XAI Grok streaming completed`);
        console.log(`   Output saved to: ${outputPath}`);
        console.log(`   Total chunks: ${chunks.length}`);
        console.log(`   Full text: ${fullText.substring(0, 100)}...`);
        expect(fullText).toBeTruthy();
        expect(chunks.length).toBeGreaterThan(0);
        expect(fullText).toContain("1");
        expect(fullText).toContain("10");
    }, 60000);
});
//# sourceMappingURL=streaming.test.js.map