// src/utils/logger.ts
var Logger = class _Logger {
  debugEnabled;
  prefix;
  scope;
  onLog;
  constructor(config = {}) {
    this.debugEnabled = config.debug ?? false;
    this.prefix = config.prefix ?? "[PostHog Agent]";
    this.scope = config.scope ?? "agent";
    this.onLog = config.onLog;
  }
  formatMessage(level, message, data) {
    const timestamp = (/* @__PURE__ */ new Date()).toISOString();
    const base = `${timestamp} ${this.prefix} [${level}] ${message}`;
    if (data !== void 0) {
      return `${base} ${JSON.stringify(data, null, 2)}`;
    }
    return base;
  }
  emitLog(level, message, data) {
    if (this.onLog) {
      this.onLog(level, this.scope, message, data);
      return;
    }
    const shouldLog = this.debugEnabled || level === "error";
    if (shouldLog) {
      console[level](this.formatMessage(level.toLowerCase(), message, data));
    }
  }
  error(message, error) {
    const data = error instanceof Error ? { message: error.message, stack: error.stack } : error;
    this.emitLog("error", message, data);
  }
  warn(message, data) {
    this.emitLog("warn", message, data);
  }
  info(message, data) {
    this.emitLog("info", message, data);
  }
  debug(message, data) {
    this.emitLog("debug", message, data);
  }
  child(childPrefix) {
    return new _Logger({
      debug: this.debugEnabled,
      prefix: `${this.prefix} [${childPrefix}]`,
      scope: `${this.scope}:${childPrefix}`,
      onLog: this.onLog
    });
  }
};

// src/adapters/claude/mcp/tool-metadata.ts
var mcpToolMetadataCache = /* @__PURE__ */ new Map();
var PENDING_RETRY_INTERVAL_MS = 1e3;
var PENDING_MAX_RETRIES = 10;
function sanitizeMcpServerName(name) {
  return name.replace(/[^a-zA-Z0-9_-]/g, "_");
}
function buildToolKey(serverName, toolName) {
  return `mcp__${serverName}__${toolName}`;
}
function delay(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}
async function fetchMcpToolMetadata(q, logger = new Logger({ debug: false, prefix: "[McpToolMetadata]" })) {
  let retries = 0;
  while (retries <= PENDING_MAX_RETRIES) {
    let statuses;
    try {
      statuses = await q.mcpServerStatus();
    } catch (error) {
      logger.error("Failed to fetch MCP server status", {
        error: error instanceof Error ? error.message : String(error)
      });
      return;
    }
    const pendingServers = statuses.filter((s) => s.status === "pending");
    for (const server of statuses) {
      if (server.status !== "connected" || !server.tools) {
        continue;
      }
      let readOnlyCount = 0;
      for (const tool of server.tools) {
        const toolKey = buildToolKey(server.name, tool.name);
        const readOnly = tool.annotations?.readOnly === true;
        const existing = mcpToolMetadataCache.get(toolKey);
        mcpToolMetadataCache.set(toolKey, {
          readOnly,
          name: tool.name,
          description: tool.description,
          approvalState: existing?.approvalState
        });
        if (readOnly) readOnlyCount++;
      }
      logger.info("Fetched MCP tool metadata", {
        serverName: server.name,
        toolCount: server.tools.length,
        readOnlyCount
      });
    }
    if (pendingServers.length === 0) {
      return;
    }
    retries++;
    if (retries > PENDING_MAX_RETRIES) {
      logger.warn("Gave up waiting for pending MCP servers", {
        pendingServers: pendingServers.map((s) => s.name)
      });
      return;
    }
    logger.info("Waiting for pending MCP servers", {
      pendingServers: pendingServers.map((s) => s.name),
      retry: retries
    });
    await delay(PENDING_RETRY_INTERVAL_MS);
  }
}
function getMcpToolMetadata(toolName) {
  return mcpToolMetadataCache.get(toolName);
}
function isMcpToolReadOnly(toolName) {
  const metadata = mcpToolMetadataCache.get(toolName);
  return metadata?.readOnly === true;
}
function getConnectedMcpServerNames() {
  const names = /* @__PURE__ */ new Set();
  for (const key of mcpToolMetadataCache.keys()) {
    const parts = key.split("__");
    if (parts.length >= 3) names.add(parts[1]);
  }
  return [...names];
}
function getMcpToolApprovalState(toolName) {
  return mcpToolMetadataCache.get(toolName)?.approvalState;
}
function setMcpToolApprovalStates(approvals) {
  for (const [toolKey, approvalState] of Object.entries(approvals)) {
    const existing = mcpToolMetadataCache.get(toolKey);
    if (existing) {
      existing.approvalState = approvalState;
    } else {
      mcpToolMetadataCache.set(toolKey, {
        readOnly: false,
        name: toolKey,
        approvalState
      });
    }
  }
}
function clearMcpToolMetadataCache() {
  mcpToolMetadataCache.clear();
}
export {
  clearMcpToolMetadataCache,
  fetchMcpToolMetadata,
  getConnectedMcpServerNames,
  getMcpToolApprovalState,
  getMcpToolMetadata,
  isMcpToolReadOnly,
  sanitizeMcpServerName,
  setMcpToolApprovalStates
};
//# sourceMappingURL=tool-metadata.js.map