import { ReadableStream } from "node:stream/web";
import { readStream, toStream } from "../utils.js";
function createAgentV2(AgentBase, options) {
  class AgentV2 extends AgentBase {
    hookAdapter;
    constructor({
      onEnter,
      onExit,
      onUserTurnCompleted,
      sttNode,
      llmNode,
      ttsNode,
      realtimeAudioOutputNode,
      transcriptionNode,
      ...agentOptions
    }) {
      super({
        ...agentOptions,
        id: agentOptions.id ?? "default_agent"
      });
      this.hookAdapter = new AgentHookAdapter(
        {
          onEnter,
          onExit,
          onUserTurnCompleted,
          sttNode,
          llmNode,
          ttsNode,
          realtimeAudioOutputNode,
          transcriptionNode
        },
        new AgentHookContext(this)
      );
    }
    async onEnter() {
      return this.hookAdapter.onEnter(() => super.onEnter());
    }
    async onExit() {
      return this.hookAdapter.onExit(() => super.onExit());
    }
    async onUserTurnCompleted(chatCtx, newMessage) {
      return this.hookAdapter.onUserTurnCompleted(
        chatCtx,
        newMessage,
        () => super.onUserTurnCompleted(chatCtx, newMessage)
      );
    }
    async sttNode(audio, modelSettings) {
      return this.hookAdapter.sttNode(
        audio,
        modelSettings,
        () => super.sttNode(audio, modelSettings)
      );
    }
    async llmNode(chatCtx, toolCtx, modelSettings) {
      return this.hookAdapter.llmNode(
        chatCtx,
        toolCtx,
        modelSettings,
        () => super.llmNode(chatCtx, toolCtx, modelSettings)
      );
    }
    async ttsNode(text, modelSettings) {
      return this.hookAdapter.ttsNode(
        text,
        modelSettings,
        () => super.ttsNode(text, modelSettings)
      );
    }
    async realtimeAudioOutputNode(audio, modelSettings) {
      return this.hookAdapter.realtimeAudioOutputNode(
        audio,
        modelSettings,
        () => super.realtimeAudioOutputNode(audio, modelSettings)
      );
    }
    async transcriptionNode(text, modelSettings) {
      return this.hookAdapter.transcriptionNode(
        text,
        modelSettings,
        () => super.transcriptionNode(text, modelSettings)
      );
    }
  }
  return new AgentV2(options);
}
function createAgentTaskV2(AgentTaskBase, options) {
  class AgentTaskV2 extends AgentTaskBase {
    hookAdapter;
    constructor({
      onEnter,
      onExit,
      onUserTurnCompleted,
      sttNode,
      llmNode,
      ttsNode,
      realtimeAudioOutputNode,
      transcriptionNode,
      ...taskOptions
    }) {
      super({
        ...taskOptions,
        id: taskOptions.id ?? "default_agent"
      });
      this.hookAdapter = new AgentHookAdapter(
        {
          onEnter,
          onExit,
          onUserTurnCompleted,
          sttNode,
          llmNode,
          ttsNode,
          realtimeAudioOutputNode,
          transcriptionNode
        },
        new AgentTaskHookContext(this)
      );
    }
    async onEnter() {
      return this.hookAdapter.onEnter(() => super.onEnter());
    }
    async onExit() {
      return this.hookAdapter.onExit(() => super.onExit());
    }
    async onUserTurnCompleted(chatCtx, newMessage) {
      return this.hookAdapter.onUserTurnCompleted(
        chatCtx,
        newMessage,
        () => super.onUserTurnCompleted(chatCtx, newMessage)
      );
    }
    async sttNode(audio, modelSettings) {
      return this.hookAdapter.sttNode(
        audio,
        modelSettings,
        () => super.sttNode(audio, modelSettings)
      );
    }
    async llmNode(chatCtx, toolCtx, modelSettings) {
      return this.hookAdapter.llmNode(
        chatCtx,
        toolCtx,
        modelSettings,
        () => super.llmNode(chatCtx, toolCtx, modelSettings)
      );
    }
    async ttsNode(text, modelSettings) {
      return this.hookAdapter.ttsNode(
        text,
        modelSettings,
        () => super.ttsNode(text, modelSettings)
      );
    }
    async realtimeAudioOutputNode(audio, modelSettings) {
      return this.hookAdapter.realtimeAudioOutputNode(
        audio,
        modelSettings,
        () => super.realtimeAudioOutputNode(audio, modelSettings)
      );
    }
    async transcriptionNode(text, modelSettings) {
      return this.hookAdapter.transcriptionNode(
        text,
        modelSettings,
        () => super.transcriptionNode(text, modelSettings)
      );
    }
  }
  return new AgentTaskV2(options);
}
class AgentHookAdapter {
  constructor(hooks, context) {
    this.hooks = hooks;
    this.context = context;
  }
  async onEnter(fallback) {
    if (!this.hooks.onEnter) {
      return fallback();
    }
    return this.hooks.onEnter(this.context);
  }
  async onExit(fallback) {
    if (!this.hooks.onExit) {
      return fallback();
    }
    return this.hooks.onExit(this.context);
  }
  async onUserTurnCompleted(chatCtx, newMessage, fallback) {
    if (!this.hooks.onUserTurnCompleted) {
      return fallback();
    }
    return this.hooks.onUserTurnCompleted(this.context, chatCtx, newMessage);
  }
  async sttNode(audio, modelSettings, fallback) {
    if (!this.hooks.sttNode) {
      return fallback();
    }
    const input = audio instanceof ReadableStream ? readStream(audio) : audio;
    const result = await this.hooks.sttNode(this.context, input, modelSettings);
    return result === null ? null : toStream(result);
  }
  async llmNode(chatCtx, toolCtx, modelSettings, fallback) {
    if (!this.hooks.llmNode) {
      return fallback();
    }
    const result = await this.hooks.llmNode(
      this.context,
      chatCtx,
      toolCtx,
      modelSettings
    );
    return result === null ? null : toStream(result);
  }
  async ttsNode(text, modelSettings, fallback) {
    if (!this.hooks.ttsNode) {
      return fallback();
    }
    const input = text instanceof ReadableStream ? readStream(text) : text;
    const result = await this.hooks.ttsNode(this.context, input, modelSettings);
    return result === null ? null : toStream(result);
  }
  async realtimeAudioOutputNode(audio, modelSettings, fallback) {
    if (!this.hooks.realtimeAudioOutputNode) {
      return fallback();
    }
    const input = audio instanceof ReadableStream ? readStream(audio) : audio;
    const result = await this.hooks.realtimeAudioOutputNode(this.context, input, modelSettings);
    return result === null ? null : toStream(result);
  }
  async transcriptionNode(text, modelSettings, fallback) {
    if (!this.hooks.transcriptionNode) {
      return fallback();
    }
    const input = text instanceof ReadableStream ? readStream(text) : text;
    const result = await this.hooks.transcriptionNode(this.context, input, modelSettings);
    return result === null ? null : toStream(result);
  }
}
class AgentHookContext {
  constructor(agent) {
    this.agent = agent;
  }
  get vad() {
    return this.agent.vad;
  }
  get stt() {
    return this.agent.stt;
  }
  get llm() {
    return this.agent.llm;
  }
  get tts() {
    return this.agent.tts;
  }
  get useTtsAlignedTranscript() {
    return this.agent.useTtsAlignedTranscript;
  }
  get chatCtx() {
    return this.agent.chatCtx;
  }
  get id() {
    return this.agent.id;
  }
  get instructions() {
    return this.agent.instructions;
  }
  get toolCtx() {
    return this.agent.toolCtx;
  }
  get session() {
    return this.agent.session;
  }
  get turnHandling() {
    return this.agent.turnHandling;
  }
  get minConsecutiveSpeechDelay() {
    return this.agent.minConsecutiveSpeechDelay;
  }
}
class AgentTaskHookContext extends AgentHookContext {
  constructor(agent) {
    super(agent);
  }
  complete(result) {
    this.agent.complete(result);
  }
}
export {
  createAgentTaskV2,
  createAgentV2
};
//# sourceMappingURL=agent_v2.js.map