"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var agent_exports = {};
__export(agent_exports, {
  Agent: () => Agent,
  AgentTask: () => AgentTask,
  STOP_RESPONSE_SYMBOL: () => STOP_RESPONSE_SYMBOL,
  StopResponse: () => StopResponse,
  _getActivityTaskInfo: () => _getActivityTaskInfo,
  _setActivityTaskInfo: () => _setActivityTaskInfo,
  functionCallStorage: () => functionCallStorage,
  isStopResponse: () => isStopResponse,
  speechHandleStorage: () => speechHandleStorage
});
module.exports = __toCommonJS(agent_exports);
var import_node_async_hooks = require("node:async_hooks");
var import_web = require("node:stream/web");
var import_inference = require("../inference/index.cjs");
var import_chat_context = require("../llm/chat_context.cjs");
var import_llm = require("../llm/index.cjs");
var import_log = require("../log.cjs");
var import_stt = require("../stt/index.cjs");
var import_basic = require("../tokenize/basic/index.cjs");
var import_tts = require("../tts/index.cjs");
var import_types = require("../types.cjs");
var import_utils = require("../utils.cjs");
var import_agent_activity = require("./agent_activity.cjs");
var import_utils2 = require("./turn_config/utils.cjs");
const functionCallStorage = new import_node_async_hooks.AsyncLocalStorage();
const speechHandleStorage = new import_node_async_hooks.AsyncLocalStorage();
const activityTaskInfoStorage = /* @__PURE__ */ new WeakMap();
function _setActivityTaskInfo(task, options) {
  const info = activityTaskInfoStorage.get(task) ?? {
    functionCall: null,
    speechHandle: null,
    inlineTask: false
  };
  if (Object.hasOwn(options, "functionCall")) {
    info.functionCall = options.functionCall ?? null;
  }
  if (Object.hasOwn(options, "speechHandle")) {
    info.speechHandle = options.speechHandle ?? null;
  }
  if (Object.hasOwn(options, "inlineTask")) {
    info.inlineTask = options.inlineTask ?? false;
  }
  activityTaskInfoStorage.set(task, info);
}
function _getActivityTaskInfo(task) {
  return activityTaskInfoStorage.get(task);
}
const STOP_RESPONSE_SYMBOL = Symbol("StopResponse");
class StopResponse extends Error {
  constructor() {
    super();
    this.name = "StopResponse";
    Object.defineProperty(this, STOP_RESPONSE_SYMBOL, {
      value: true
    });
  }
}
function isStopResponse(value) {
  return value !== void 0 && value !== null && typeof value === "object" && STOP_RESPONSE_SYMBOL in value;
}
class Agent {
  _id;
  _stt;
  _vad;
  _llm;
  _tts;
  _turnHandling;
  _minConsecutiveSpeechDelay;
  _useTtsAlignedTranscript;
  /** @internal */
  _agentActivity;
  /** @internal */
  _chatCtx;
  /** @internal */
  _instructions;
  /** @internal */
  _tools;
  constructor({
    id,
    instructions,
    chatCtx,
    tools,
    turnDetection,
    stt,
    vad,
    llm,
    tts,
    allowInterruptions,
    turnHandling,
    minConsecutiveSpeechDelay,
    useTtsAlignedTranscript
  }) {
    if (id) {
      this._id = id;
    } else {
      const className = this.constructor.name;
      if (className === "Agent") {
        this._id = "default_agent";
      } else {
        this._id = className.replace(/([A-Z])/g, "_$1").toLowerCase().replace(/^_/, "");
      }
    }
    this._instructions = instructions;
    this._tools = { ...tools };
    this._chatCtx = chatCtx ? chatCtx.copy({
      toolCtx: this._tools
    }) : import_llm.ChatContext.empty();
    const resolvedTurnHandling = (0, import_utils2.migrateTurnHandling)({
      turnDetection,
      allowInterruptions,
      turnHandling
    });
    this._turnHandling = Object.keys(resolvedTurnHandling).length > 0 ? resolvedTurnHandling : void 0;
    this._vad = vad;
    if (typeof stt === "string") {
      this._stt = import_inference.STT.fromModelString(stt);
    } else {
      this._stt = stt;
    }
    if (typeof llm === "string") {
      this._llm = import_inference.LLM.fromModelString(llm);
    } else {
      this._llm = llm;
    }
    if (typeof tts === "string") {
      this._tts = import_inference.TTS.fromModelString(tts);
    } else {
      this._tts = tts;
    }
    this._minConsecutiveSpeechDelay = minConsecutiveSpeechDelay;
    this._useTtsAlignedTranscript = useTtsAlignedTranscript;
    this._agentActivity = void 0;
  }
  get vad() {
    return this._vad;
  }
  get stt() {
    return this._stt;
  }
  get llm() {
    return this._llm;
  }
  get tts() {
    return this._tts;
  }
  get useTtsAlignedTranscript() {
    return this._useTtsAlignedTranscript;
  }
  get chatCtx() {
    return new import_chat_context.ReadonlyChatContext(this._chatCtx.items);
  }
  get id() {
    return this._id;
  }
  get instructions() {
    return this._instructions;
  }
  get toolCtx() {
    return { ...this._tools };
  }
  get session() {
    return this.getActivityOrThrow().agentSession;
  }
  get turnHandling() {
    return this._turnHandling;
  }
  get minConsecutiveSpeechDelay() {
    return this._minConsecutiveSpeechDelay;
  }
  async onEnter() {
  }
  async onExit() {
  }
  async transcriptionNode(text, modelSettings) {
    return Agent.default.transcriptionNode(this, text, modelSettings);
  }
  async onUserTurnCompleted(_chatCtx, _newMessage) {
  }
  async onUserTurnExceeded(ev) {
    await this.session.generateReply({
      userInput: ev.transcript,
      instructions: "The user has been speaking too long without giving a chance to reply. Politely cut in with a short reply or notice. Keep it short since the user cannot interrupt it.",
      allowInterruptions: false,
      toolChoice: "none",
      inputModality: "audio"
    });
  }
  async sttNode(audio, modelSettings) {
    return Agent.default.sttNode(this, audio, modelSettings);
  }
  async llmNode(chatCtx, toolCtx, modelSettings) {
    return Agent.default.llmNode(this, chatCtx, toolCtx, modelSettings);
  }
  async ttsNode(text, modelSettings) {
    return Agent.default.ttsNode(this, text, modelSettings);
  }
  async realtimeAudioOutputNode(audio, modelSettings) {
    return Agent.default.realtimeAudioOutputNode(this, audio, modelSettings);
  }
  // realtime_audio_output_node
  getActivityOrThrow() {
    if (!this._agentActivity) {
      throw new Error("Agent activity not found");
    }
    return this._agentActivity;
  }
  async updateChatCtx(chatCtx) {
    if (!this._agentActivity) {
      this._chatCtx = chatCtx.copy({ toolCtx: this.toolCtx });
      return;
    }
    this._agentActivity.updateChatCtx(chatCtx);
  }
  // TODO: Add when AgentConfigUpdate is ported to ChatContext.
  async updateTools(tools) {
    if (!this._agentActivity) {
      this._tools = { ...tools };
      this._chatCtx = this._chatCtx.copy({ toolCtx: this._tools });
      return;
    }
    await this._agentActivity.updateTools(tools);
  }
  static default = {
    async sttNode(agent, audio, _modelSettings) {
      var _a;
      const activity = agent.getActivityOrThrow();
      if (!activity.stt) {
        throw new Error("sttNode called but no STT node is available");
      }
      let wrappedStt = activity.stt;
      if (!wrappedStt.capabilities.streaming) {
        const vad = agent.vad || activity.vad;
        if (!vad) {
          throw new Error(
            "STT does not support streaming, add a VAD to the AgentTask/VoiceAgent to enable streaming"
          );
        }
        wrappedStt = new import_stt.StreamAdapter(wrappedStt, vad);
      }
      const connOptions = activity.agentSession.connOptions.sttConnOptions;
      const stream = wrappedStt.stream({ connOptions });
      const audioInputStartedAt = activity.inputStartedAt ?? // Use input started at proxied from AudioRecognition if available
      ((_a = activity.agentSession._recorderIO) == null ? void 0 : _a.recordingStartedAt) ?? // Fallback to recording start time if available
      activity.agentSession._startedAt ?? // Fallback to session start time
      Date.now();
      stream.startTimeOffset = (Date.now() - audioInputStartedAt) / 1e3;
      stream.updateInputStream(audio);
      let cleaned = false;
      const cleanup = () => {
        if (cleaned) return;
        cleaned = true;
        stream.detachInputStream();
        stream.close();
      };
      return new import_web.ReadableStream({
        async start(controller) {
          try {
            for await (const event of stream) {
              controller.enqueue(event);
            }
            controller.close();
          } finally {
            cleanup();
          }
        },
        cancel() {
          cleanup();
        }
      });
    },
    async llmNode(agent, chatCtx, toolCtx, modelSettings) {
      const activity = agent.getActivityOrThrow();
      if (!activity.llm) {
        throw new Error("llmNode called but no LLM node is available");
      }
      if (!(activity.llm instanceof import_llm.LLM)) {
        throw new Error(
          "llmNode should only be used with LLM (non-multimodal/realtime APIs) nodes"
        );
      }
      const { toolChoice } = modelSettings;
      const connOptions = activity.agentSession.connOptions.llmConnOptions;
      const stream = activity.llm.chat({
        chatCtx,
        toolCtx,
        toolChoice,
        connOptions
      });
      let cleaned = false;
      const cleanup = () => {
        if (cleaned) return;
        cleaned = true;
        stream.close();
      };
      return new import_web.ReadableStream({
        async start(controller) {
          try {
            for await (const chunk of stream) {
              controller.enqueue(chunk);
            }
            controller.close();
          } finally {
            cleanup();
          }
        },
        cancel() {
          cleanup();
        }
      });
    },
    async ttsNode(agent, text, _modelSettings) {
      const activity = agent.getActivityOrThrow();
      if (!activity.tts) {
        throw new Error("ttsNode called but no TTS node is available");
      }
      let wrappedTts = activity.tts;
      if (!activity.tts.capabilities.streaming) {
        wrappedTts = new import_tts.StreamAdapter(wrappedTts, new import_basic.SentenceTokenizer());
      }
      const connOptions = activity.agentSession.connOptions.ttsConnOptions;
      const stream = wrappedTts.stream({ connOptions });
      stream.updateInputStream(text);
      let cleaned = false;
      const cleanup = async () => {
        if (cleaned) return;
        cleaned = true;
        stream.close();
        await text.cancel("tts node cleanup").catch(() => {
        });
        if (wrappedTts !== activity.tts) {
          await wrappedTts.close();
        }
      };
      return new import_web.ReadableStream({
        async start(controller) {
          try {
            for await (const chunk of stream) {
              if (chunk === import_tts.SynthesizeStream.END_OF_STREAM) {
                break;
              }
              if (chunk.timedTranscripts && chunk.timedTranscripts.length > 0) {
                chunk.frame.userdata[import_types.USERDATA_TIMED_TRANSCRIPT] = chunk.timedTranscripts;
              }
              controller.enqueue(chunk.frame);
            }
            controller.close();
          } finally {
            await cleanup();
          }
        },
        cancel() {
          return cleanup();
        }
      });
    },
    async transcriptionNode(agent, text, _modelSettings) {
      return text;
    },
    async realtimeAudioOutputNode(_agent, audio, _modelSettings) {
      return audio;
    }
  };
}
class AgentTask extends Agent {
  started = false;
  future = new import_utils.Future();
  _preserveFunctionCallHistory;
  #logger = (0, import_log.log)();
  constructor(options) {
    const { preserveFunctionCallHistory = false, ...rest } = options;
    super(rest);
    this._preserveFunctionCallHistory = preserveFunctionCallHistory;
  }
  get done() {
    return this.future.done;
  }
  complete(result) {
    if (this.future.done) {
      throw new Error(`${this.constructor.name} is already done`);
    }
    if (result instanceof Error) {
      this.future.reject(result);
    } else {
      this.future.resolve(result);
    }
    const speechHandle = speechHandleStorage.getStore();
    if (speechHandle) {
      speechHandle._maybeRunFinalOutput = result;
    }
  }
  async run() {
    if (this.started) {
      throw new Error(
        `Task ${this.constructor.name} has already started and cannot be awaited multiple times`
      );
    }
    this.started = true;
    const currentTask = import_utils.Task.current();
    if (!currentTask) {
      throw new Error(`${this.constructor.name} must be executed inside a Task context`);
    }
    const taskInfo = _getActivityTaskInfo(currentTask);
    if (!taskInfo || !taskInfo.inlineTask) {
      throw new Error(
        `${this.constructor.name} should only be awaited inside function tools or the onEnter/onExit methods of an Agent`
      );
    }
    const speechHandle = speechHandleStorage.getStore();
    const oldActivity = import_agent_activity.agentActivityStorage.getStore();
    if (!oldActivity) {
      throw new Error(`${this.constructor.name} must be executed inside an AgentActivity context`);
    }
    currentTask.addDoneCallback(() => {
      if (this.future.done) return;
      this.#logger.error(`The Task finished before ${this.constructor.name} was completed.`);
      this.complete(new Error(`The Task finished before ${this.constructor.name} was completed.`));
    });
    const oldAgent = oldActivity.agent;
    const session = oldActivity.agentSession;
    const blockedTasks = [currentTask];
    const onEnterTask = oldActivity._onEnterTask;
    if (onEnterTask && !onEnterTask.done && onEnterTask !== currentTask) {
      blockedTasks.push(onEnterTask);
    }
    if (taskInfo.functionCall && oldActivity.llm instanceof import_llm.RealtimeModel && !oldActivity.llm.capabilities.manualFunctionCalls) {
      this.#logger.error(
        `Realtime model does not support resuming function calls from chat context, using AgentTask inside a function tool may have unexpected behavior.`
      );
    }
    await session._updateActivity(this, {
      previousActivity: "pause",
      newActivity: "start",
      blockedTasks
    });
    let runState = session._globalRunState;
    if (speechHandle && runState && !runState.done()) {
      if (runState._watchedHandleCount() > 1) {
        runState._unwatchHandle(speechHandle);
      }
      runState._markDoneIfNeeded();
    }
    try {
      return await this.future.await;
    } finally {
      runState = session._globalRunState;
      if (session.currentAgent !== this) {
        this.#logger.warn(
          `${this.constructor.name} completed, but the agent has changed in the meantime. Ignoring handoff to the previous agent, likely due to AgentSession.updateAgent being invoked.`
        );
        await oldActivity.close();
      } else {
        if (speechHandle && runState && !runState.done()) {
          runState._watchHandle(speechHandle);
        }
        const mergedChatCtx = oldAgent._chatCtx.merge(this._chatCtx, {
          excludeFunctionCall: !this._preserveFunctionCallHistory,
          excludeInstructions: true
        });
        oldAgent._chatCtx.items = mergedChatCtx.items;
        await session._updateActivity(oldAgent, {
          previousActivity: "close",
          newActivity: "resume",
          waitOnEnter: false
        });
      }
    }
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  Agent,
  AgentTask,
  STOP_RESPONSE_SYMBOL,
  StopResponse,
  _getActivityTaskInfo,
  _setActivityTaskInfo,
  functionCallStorage,
  isStopResponse,
  speechHandleStorage
});
//# sourceMappingURL=agent.cjs.map