"use strict";
var import_vitest = require("vitest");
var import_log = require("../log.cjs");
var import_types = require("../types.cjs");
var import_utils = require("../utils.cjs");
var import_chat_context = require("./chat_context.cjs");
var import_llm = require("./llm.cjs");
class MockLLMStream extends import_llm.LLMStream {
  constructor(llm, opts, chunks) {
    super(llm, opts);
    this.chunks = chunks;
  }
  async run() {
    for (const chunk of this.chunks) {
      this.queue.put(chunk);
      await (0, import_utils.delay)(1);
    }
  }
}
class MockLLM extends import_llm.LLM {
  constructor(chunks) {
    super();
    this.chunks = chunks;
  }
  label() {
    return "mock-llm";
  }
  chat(opts) {
    return new MockLLMStream(
      this,
      {
        chatCtx: opts.chatCtx,
        toolCtx: opts.toolCtx,
        connOptions: opts.connOptions ?? import_types.DEFAULT_API_CONNECT_OPTIONS
      },
      this.chunks
    );
  }
}
(0, import_vitest.describe)("LLMStream.collect", () => {
  (0, import_vitest.beforeAll)(() => {
    (0, import_log.initializeLogger)({ pretty: false });
    process.on("unhandledRejection", () => {
    });
  });
  (0, import_vitest.it)("joins content parts and trims surrounding whitespace", async () => {
    const llm = new MockLLM([
      { id: "1", delta: { role: "assistant", content: "  Hello" } },
      { id: "1", delta: { role: "assistant", content: ", " } },
      { id: "1", delta: { role: "assistant", content: "world!  " } }
    ]);
    const response = await llm.chat({ chatCtx: new import_chat_context.ChatContext() }).collect();
    (0, import_vitest.expect)(response.text).toBe("Hello, world!");
    (0, import_vitest.expect)(response.toolCalls).toHaveLength(0);
    (0, import_vitest.expect)(response.usage).toBeUndefined();
    (0, import_vitest.expect)(response.extra).toEqual({});
  });
  (0, import_vitest.it)("accumulates tool calls across chunks", async () => {
    const callA = new import_chat_context.FunctionCall({
      callId: "call_a",
      name: "get_weather",
      args: '{"city":"SF"}'
    });
    const callB = new import_chat_context.FunctionCall({
      callId: "call_b",
      name: "play_song",
      args: '{"name":"x"}'
    });
    const llm = new MockLLM([
      { id: "1", delta: { role: "assistant", toolCalls: [callA] } },
      { id: "1", delta: { role: "assistant", toolCalls: [callB] } }
    ]);
    const response = await llm.chat({ chatCtx: new import_chat_context.ChatContext() }).collect();
    (0, import_vitest.expect)(response.text).toBe("");
    (0, import_vitest.expect)(response.toolCalls).toHaveLength(2);
    (0, import_vitest.expect)(response.toolCalls[0].callId).toBe("call_a");
    (0, import_vitest.expect)(response.toolCalls[1].callId).toBe("call_b");
  });
  (0, import_vitest.it)("captures the latest usage and merges extra data", async () => {
    var _a, _b;
    const llm = new MockLLM([
      { id: "1", delta: { role: "assistant", content: "hi", extra: { a: 1 } } },
      {
        id: "1",
        delta: { role: "assistant", content: " there", extra: { b: 2 } },
        usage: {
          completionTokens: 2,
          promptTokens: 5,
          promptCachedTokens: 0,
          totalTokens: 7
        }
      },
      {
        id: "1",
        usage: {
          completionTokens: 3,
          promptTokens: 5,
          promptCachedTokens: 0,
          totalTokens: 8
        }
      }
    ]);
    const response = await llm.chat({ chatCtx: new import_chat_context.ChatContext() }).collect();
    (0, import_vitest.expect)(response.text).toBe("hi there");
    (0, import_vitest.expect)((_a = response.usage) == null ? void 0 : _a.completionTokens).toBe(3);
    (0, import_vitest.expect)((_b = response.usage) == null ? void 0 : _b.totalTokens).toBe(8);
    (0, import_vitest.expect)(response.extra).toEqual({ a: 1, b: 2 });
  });
  (0, import_vitest.it)("returns empty response for an empty stream", async () => {
    const llm = new MockLLM([]);
    const response = await llm.chat({ chatCtx: new import_chat_context.ChatContext() }).collect();
    (0, import_vitest.expect)(response.text).toBe("");
    (0, import_vitest.expect)(response.toolCalls).toHaveLength(0);
    (0, import_vitest.expect)(response.usage).toBeUndefined();
    (0, import_vitest.expect)(response.extra).toEqual({});
  });
});
//# sourceMappingURL=llm.test.cjs.map