// @bun
// src/utils/build-constants.ts
var CLI_VERSION = "1.19.0-beta.13";
var EXPECTED_RUNTIME_VERSION = "1.19.0-beta.13";
var MIN_SUPPORTED_RUNTIME = "1.17.0";
var IS_RELEASE_BUILD = false;

// ../analytics/src/constants.ts
var POSTHOG_API_KEY = typeof process !== "undefined" && process.env?.POSTHOG_API_KEY || "phc_i782iVFulXCgIMKtyOL5ns4IR1w4oW9lA5sUzwuY4wj";
var POSTHOG_HOST = "https://us.i.posthog.com";

// ../analytics/src/sanitize.ts
function sanitizeErrorMessage(message) {
  let sanitized = message;
  sanitized = sanitized.replace(/\/[^\s"'`]+(\/[^\s"'`]+)+/g, "<path>");
  sanitized = sanitized.replace(/[A-Z]:\\[\w\\]+/g, "<path>");
  sanitized = sanitized.replace(/bp_pat_[A-Za-z0-9]+/g, "<token>");
  sanitized = sanitized.replace(/Bearer\s+[A-Za-z0-9._-]+/g, "Bearer <token>");
  sanitized = sanitized.replace(/phc_[A-Za-z0-9]+/g, "<posthog_key>");
  sanitized = sanitized.replace(/([A-Z_]{2,})=\S+/g, "$1=<redacted>");
  sanitized = sanitized.replace(/https?:\/\/[^:]+:[^@]+@/g, "https://<redacted>@");
  if (sanitized.length > 500) {
    sanitized = sanitized.slice(0, 497) + "...";
  }
  return sanitized;
}
function sanitizeStackTrace(stack) {
  let sanitized = stack;
  sanitized = sanitized.replace(/\/(Users|home)\/[^/\s)]+/g, "/$1/<user>");
  sanitized = sanitized.replace(/([A-Z]:\\Users\\)[^\\\s)]+/gi, "$1<user>");
  sanitized = sanitized.replace(/bp_pat_[A-Za-z0-9]+/g, "<token>");
  sanitized = sanitized.replace(/Bearer\s+[A-Za-z0-9._-]+/g, "Bearer <token>");
  sanitized = sanitized.replace(/phc_[A-Za-z0-9]+/g, "<posthog_key>");
  return sanitized;
}
function toSanitizedError(error) {
  if (!(error instanceof Error)) {
    return new Error(sanitizeErrorMessage(String(error)));
  }
  const message = sanitizeErrorMessage(error.message);
  const sanitized = new Error(message);
  sanitized.name = error.name;
  if (error.stack) {
    const lines = error.stack.split(`
`);
    let frameStart = lines.length;
    while (frameStart > 0 && /^\s+at /.test(lines[frameStart - 1]))
      frameStart--;
    const frames = lines.slice(frameStart);
    sanitized.stack = [`${error.name}: ${message}`, ...frames.map(sanitizeStackTrace)].join(`
`);
  } else {
    sanitized.stack = `${error.name}: ${message}`;
  }
  return sanitized;
}

// ../analytics/src/errors.ts
class AdkError extends Error {
  static __IS_ADK_BASE_ERROR = true;
  code;
  expected;
  details;
  suggestion;
  constructor(opts) {
    super(opts.message, opts.cause !== undefined ? { cause: opts.cause } : undefined);
    this.name = this.constructor.name;
    this.code = opts.code;
    this.expected = opts.expected ?? false;
    if (opts.details !== undefined) {
      this.details = opts.details;
    }
    if (opts.suggestion !== undefined) {
      this.suggestion = opts.suggestion;
    }
  }
}
function isAdkError(error) {
  if (error instanceof AdkError)
    return true;
  if (!(error instanceof Error))
    return false;
  return error.constructor?.__IS_ADK_BASE_ERROR === true;
}
// ../analytics/src/node.ts
import { homedir } from "os";
import { join } from "path";
var TELEMETRY_CONFIG_DIR = join(homedir(), ".botpress");
var TELEMETRY_CONFIG_FILE = join(TELEMETRY_CONFIG_DIR, "telemetry.json");

// src/utils/telemetry-spool.ts
import { appendFileSync, existsSync, mkdirSync, readFileSync, statSync } from "fs";
import { dirname, join as join2 } from "path";
var FATAL_SPOOL_FILE = join2(TELEMETRY_CONFIG_DIR, "pending-exceptions.jsonl");
var FATAL_SPOOL_MAX_BYTES = 64 * 1024;
function isTelemetryEnabled() {
  if (process.env.DO_NOT_TRACK === "1" || process.env.DO_NOT_TRACK === "true")
    return false;
  try {
    if (existsSync(TELEMETRY_CONFIG_FILE)) {
      const config = JSON.parse(readFileSync(TELEMETRY_CONFIG_FILE, "utf-8"));
      return config.enabled !== false;
    }
  } catch {}
  return true;
}
function spoolExceptionBeforeExit(error, properties = {}, spoolFile = FATAL_SPOOL_FILE) {
  if (false)
    ;
  if (!isTelemetryEnabled())
    return;
  try {
    const sanitized = toSanitizedError(error);
    const record = {
      name: sanitized.name,
      message: sanitized.message,
      ...sanitized.stack ? { stack: sanitized.stack } : {},
      properties: {
        ...properties,
        error_type: error instanceof Error ? error.constructor.name : "UnknownError",
        ...isAdkError(error) ? { error_code: error.code, expected: error.expected } : {},
        adk_version: CLI_VERSION,
        timestamp: new Date().toISOString()
      }
    };
    const dir = dirname(spoolFile);
    if (!existsSync(dir))
      mkdirSync(dir, { recursive: true });
    if (existsSync(spoolFile) && statSync(spoolFile).size > FATAL_SPOOL_MAX_BYTES)
      return;
    appendFileSync(spoolFile, `${JSON.stringify(record)}
`);
  } catch {}
}

export { CLI_VERSION, EXPECTED_RUNTIME_VERSION, MIN_SUPPORTED_RUNTIME, IS_RELEASE_BUILD, POSTHOG_API_KEY, POSTHOG_HOST, sanitizeErrorMessage, toSanitizedError, isAdkError, TELEMETRY_CONFIG_DIR, TELEMETRY_CONFIG_FILE, FATAL_SPOOL_FILE, spoolExceptionBeforeExit };
