"use strict";
var import_rtc_node = require("@livekit/rtc-node");
var import_web = require("node:stream/web");
var import_vitest = require("vitest");
var import_log = require("../log.cjs");
var import_agent = require("./agent.cjs");
var import_agent_session = require("./agent_session.cjs");
var import_events = require("./events.cjs");
var import_io = require("./io.cjs");
var import_fake_llm = require("./testing/fake_llm.cjs");
function frame(durationMs = 20, sampleRate = 24e3) {
  const samples = Math.floor(sampleRate * durationMs / 1e3);
  return new import_rtc_node.AudioFrame(new Int16Array(samples), sampleRate, 1, samples);
}
class InterruptibleOutput extends import_io.AudioOutput {
  onFirstFrame;
  started = false;
  constructor() {
    super(24e3);
  }
  async captureFrame(f) {
    var _a;
    await super.captureFrame(f);
    if (!this.started) {
      this.started = true;
      this.onPlaybackStarted(Date.now());
      (_a = this.onFirstFrame) == null ? void 0 : _a.call(this);
    }
  }
  flush() {
    super.flush();
  }
  clearBuffer() {
    this.onPlaybackFinished({ playbackPosition: 0.02, interrupted: true });
  }
}
class FrameAgent extends import_agent.Agent {
  constructor() {
    super({ instructions: "test" });
  }
  async ttsNode() {
    return new import_web.ReadableStream({
      start(controller) {
        for (let i = 0; i < 10; i++) controller.enqueue(frame());
        controller.close();
      }
    });
  }
}
(0, import_vitest.describe)("AgentActivity interrupted-speech commit", () => {
  (0, import_log.initializeLogger)({ pretty: false, level: "silent" });
  (0, import_vitest.it)("commits an interrupted reply to chat ctx when no synchronized transcript is available", async () => {
    const session = new import_agent_session.AgentSession({
      llm: new import_fake_llm.FakeLLM([{ input: "hello", content: "A fairly long spoken reply." }])
    });
    const audioOut = new InterruptibleOutput();
    session.output.audio = audioOut;
    audioOut.onFirstFrame = () => session.interrupt({ force: true });
    const assistantMessages = [];
    session.on(import_events.AgentSessionEventTypes.ConversationItemAdded, (ev) => {
      if (ev.item.type === "message" && ev.item.role === "assistant") {
        assistantMessages.push({
          content: ev.item.textContent ?? "",
          interrupted: !!ev.item.interrupted
        });
      }
    });
    await session.start({ agent: new FrameAgent() });
    try {
      const handle = session.generateReply({ userInput: "hello" });
      await handle.waitForPlayout();
      await import_vitest.vi.waitFor(() => (0, import_vitest.expect)(assistantMessages.length).toBeGreaterThan(0));
      const msg = assistantMessages[0];
      (0, import_vitest.expect)(msg.interrupted).toBe(true);
      (0, import_vitest.expect)(msg.content.length).toBeGreaterThan(0);
      (0, import_vitest.expect)("A fairly long spoken reply.".startsWith(msg.content)).toBe(true);
    } finally {
      await session.close();
    }
  });
});
//# sourceMappingURL=agent_activity_interrupted_commit.test.cjs.map