// Force strict mode and setup for ESM
"use strict";
import {
  init_esbuild_shims
} from "./chunk-5O2XNYP6.js";
import {
  __name
} from "./chunk-J2S4EL5Y.js";

// packages/core/src/utils/request-tokenizer/textTokenizer.ts
init_esbuild_shims();
var NON_ASCII_RE = /[\u0080-\uffff]/;
var TOKEN_ESTIMATE_UNITS_PER_TOKEN = 20;
function estimateTextTokens(text) {
  if (!text || text.length === 0) {
    return 0;
  }
  if (!NON_ASCII_RE.test(text)) {
    return Math.ceil(text.length / 4);
  }
  const nonAsciiChars = countNonAsciiChars(text);
  const asciiChars = text.length - nonAsciiChars;
  return Math.ceil(asciiChars / 4 + nonAsciiChars * 1.1);
}
__name(estimateTextTokens, "estimateTextTokens");
function estimateTextTokenUnits(text) {
  if (!text || text.length === 0) {
    return 0;
  }
  if (!NON_ASCII_RE.test(text)) {
    return text.length * 5;
  }
  const nonAsciiChars = countNonAsciiChars(text);
  const asciiChars = text.length - nonAsciiChars;
  return asciiChars * 5 + nonAsciiChars * 22;
}
__name(estimateTextTokenUnits, "estimateTextTokenUnits");
function countNonAsciiChars(text) {
  let count = 0;
  for (let i = 0; i < text.length; i++) {
    if (text.charCodeAt(i) >= 128) {
      count++;
    }
  }
  return count;
}
__name(countNonAsciiChars, "countNonAsciiChars");
var TextTokenizer = class {
  static {
    __name(this, "TextTokenizer");
  }
  /**
   * Calculate tokens for text content
   *
   * @param text - The text to estimate tokens for
   * @returns The estimated token count
   */
  async calculateTokens(text) {
    return this.calculateTokensSync(text);
  }
  /**
   * Calculate tokens for multiple text strings
   *
   * @param texts - Array of text strings to estimate tokens for
   * @returns Array of token counts corresponding to each input text
   */
  async calculateTokensBatch(texts) {
    return texts.map((text) => this.calculateTokensSync(text));
  }
  calculateTokensSync(text) {
    return estimateTextTokens(text);
  }
};

// packages/core/src/utils/tool-name-utils.ts
init_esbuild_shims();
var MAX_TOOL_NAME_LENGTH = 63;
var PROVIDER_SAFE_TOOL_NAME = /^[A-Za-z][A-Za-z0-9_-]*$/;
function normalizeToolNameForProvider(name) {
  if (name.length <= MAX_TOOL_NAME_LENGTH && PROVIDER_SAFE_TOOL_NAME.test(name)) {
    return name;
  }
  const normalized = sanitizeToolNameForProvider(name);
  const suffix = `_${stableToolNameHash(name)}`;
  return `${normalized.slice(0, MAX_TOOL_NAME_LENGTH - suffix.length)}${suffix}`;
}
__name(normalizeToolNameForProvider, "normalizeToolNameForProvider");
function sanitizeToolNameForProvider(name) {
  const sanitized = name.replace(/[^A-Za-z0-9_-]/g, "_");
  return /^[A-Za-z]/.test(sanitized) ? sanitized : `tool_${sanitized}`;
}
__name(sanitizeToolNameForProvider, "sanitizeToolNameForProvider");
function normalizeMcpToolName(name) {
  return name.startsWith("mcp__") ? normalizeToolNameForProvider(name) : name;
}
__name(normalizeMcpToolName, "normalizeMcpToolName");
function generateLegacyMcpToolName(name) {
  let legacyName = name.replace(/[^A-Za-z0-9_.-]/g, "_");
  if (legacyName.length > MAX_TOOL_NAME_LENGTH) {
    legacyName = legacyName.slice(0, 28) + "___" + legacyName.slice(-32);
  }
  return legacyName;
}
__name(generateLegacyMcpToolName, "generateLegacyMcpToolName");
function stableToolNameHash(name) {
  let hash = 2166136261;
  for (let index = 0; index < name.length; index += 1) {
    hash = Math.imul(hash ^ name.charCodeAt(index), 16777619);
  }
  return (hash >>> 0).toString(36).padStart(7, "0");
}
__name(stableToolNameHash, "stableToolNameHash");

export {
  TOKEN_ESTIMATE_UNITS_PER_TOKEN,
  estimateTextTokens,
  estimateTextTokenUnits,
  TextTokenizer,
  normalizeToolNameForProvider,
  sanitizeToolNameForProvider,
  normalizeMcpToolName,
  generateLegacyMcpToolName
};
/**
 * @license
 * Copyright 2025 Qwen
 * SPDX-License-Identifier: Apache-2.0
 */
