// src/server/utils/async.ts
function createDeferred() {
  let resolve;
  let reject;
  const promise = new Promise((res, rej) => {
    resolve = res;
    reject = rej;
  });
  return { promise, resolve, reject };
}
var EventEmitter = class {
  listeners = /* @__PURE__ */ new Map();
  on(event, listener) {
    if (!this.listeners.has(event)) {
      this.listeners.set(event, /* @__PURE__ */ new Set());
    }
    const listeners = this.listeners.get(event);
    listeners.add(listener);
    return () => {
      listeners.delete(listener);
    };
  }
  emit(event, ...args) {
    const listeners = this.listeners.get(event);
    if (listeners) {
      for (const listener of listeners) {
        listener(...args);
      }
    }
  }
  removeAllListeners() {
    this.listeners.clear();
  }
};

// src/server/tools/ask.ts
var pendingQuestions = /* @__PURE__ */ new Map();
var askUserTool = {
  name: "ask_user",
  definition: {
    type: "function",
    function: {
      name: "ask_user",
      description: "Pause execution and ask the user a question. Use this when you need clarification or user input before proceeding.",
      parameters: {
        type: "object",
        properties: {
          question: {
            type: "string",
            description: "The question to ask the user"
          }
        },
        required: ["question"]
      }
    }
  },
  async execute(args, context) {
    const question = args["question"];
    const callId = crypto.randomUUID();
    const deferred = createDeferred();
    void deferred.promise.catch(() => {
    });
    pendingQuestions.set(callId, {
      promise: deferred.promise,
      resolve: deferred.resolve,
      reject: deferred.reject,
      sessionId: context.sessionId
    });
    throw new AskUserInterrupt(callId, question);
  }
};
var AskUserInterrupt = class extends Error {
  constructor(callId, question) {
    super("Ask user interrupt");
    this.callId = callId;
    this.question = question;
    this.name = "AskUserInterrupt";
  }
};
function provideAnswer(callId, answer) {
  const pending = pendingQuestions.get(callId);
  if (!pending) {
    return false;
  }
  pending.resolve(answer);
  pendingQuestions.delete(callId);
  return true;
}
function cancelQuestion(callId, reason) {
  const pending = pendingQuestions.get(callId);
  if (!pending) {
    return false;
  }
  pending.reject(new Error(reason));
  pendingQuestions.delete(callId);
  return true;
}
function cancelQuestionsForSession(sessionId, reason) {
  let cancelledCount = 0;
  for (const [callId, pending] of pendingQuestions.entries()) {
    if (pending.sessionId !== sessionId) {
      continue;
    }
    pending.reject(new Error(reason));
    pendingQuestions.delete(callId);
    cancelledCount += 1;
  }
  return cancelledCount;
}
function hasPendingQuestion(callId) {
  return pendingQuestions.has(callId);
}
function awaitAnswer(callId) {
  const pending = pendingQuestions.get(callId);
  return pending?.promise ?? null;
}

export {
  EventEmitter,
  askUserTool,
  AskUserInterrupt,
  provideAnswer,
  cancelQuestion,
  cancelQuestionsForSession,
  hasPendingQuestion,
  awaitAnswer
};
//# sourceMappingURL=chunk-BJYPTN5S.js.map