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

// packages/core/src/core/tokenLimits.ts
init_esbuild_shims();
var DEFAULT_TOKEN_LIMIT = 131072;
var DEFAULT_OUTPUT_TOKEN_LIMIT = 32e3;
var ESCALATED_MAX_TOKENS = 64e3;
function parsePositiveIntegerEnvValue(raw) {
  if (raw === void 0) return void 0;
  const trimmed = raw.trim();
  if (!/^\d+$/.test(trimmed)) return void 0;
  const parsed = Number(trimmed);
  if (!Number.isSafeInteger(parsed) || parsed <= 0) return void 0;
  return parsed;
}
__name(parsePositiveIntegerEnvValue, "parsePositiveIntegerEnvValue");
var LIMITS = {
  "32k": 32768,
  "64k": 65536,
  "128k": 131072,
  "192k": 196608,
  // MiniMax-M2.5 context window
  "200k": 2e5,
  // vendor-declared decimal, used by OpenAI, Anthropic, etc.
  "256k": 262144,
  "272k": 272e3,
  // vendor-declared decimal, GPT-5.x input (400K total - 128K output)
  "384k": 384e3,
  // vendor-declared decimal, DeepSeek V4 max output
  "400k": 4e5,
  // vendor-declared decimal, used by OpenAI GPT-5.x
  "512k": 524288,
  "1m": 1e6,
  // Output token limits (typically much smaller than input limits)
  "4k": 4096,
  "8k": 8192,
  "16k": 16384
};
function normalize(model) {
  let s = (model ?? "").toLowerCase().trim();
  s = s.replace(/^.*\//, "");
  s = s.split("|").pop() ?? s;
  s = s.split(":").pop() ?? s;
  s = s.replace(/\s+/g, "-");
  s = s.replace(/-preview/g, "");
  if (!s.match(/^qwen-(?:plus|flash|vl-max)-latest$/) && !s.match(/^kimi-k2-\d{4}$/)) {
    s = s.replace(
      /-(?:\d{4,}|\d+x\d+b|v\d+(?:\.\d+)*|(?<=-[^-]+-)\d+(?:\.\d+)+|latest|exp)$/g,
      ""
    );
  }
  s = s.replace(/-(?:\d?bit|int[48]|bf16|fp16|q[45]|quantized)$/g, "");
  return s;
}
__name(normalize, "normalize");
var PATTERNS = [
  // -------------------
  // Google Gemini
  // -------------------
  [/^gemini-3/, LIMITS["1m"]],
  // Gemini 3.x (Pro, Flash, 3.1, etc.): 1M
  [/^gemini-/, LIMITS["1m"]],
  // Gemini fallback (1.5, 2.x): 1M
  // -------------------
  // OpenAI
  // -------------------
  [/^gpt-5/, LIMITS["272k"]],
  // GPT-5.x: 272K input (400K total - 128K output)
  [/^gpt-/, LIMITS["128k"]],
  // GPT fallback (4o, 4.1, etc.): 128K
  [/^o\d/, LIMITS["200k"]],
  // o-series (o3, o4-mini, etc.): 200K
  // -------------------
  // Anthropic Claude
  // -------------------
  [/^claude-/, LIMITS["200k"]],
  // All Claude models: 200K
  // -------------------
  // Alibaba / Qwen
  // -------------------
  // Commercial API models (1,000,000 context)
  [/^qwen3-coder-plus/, LIMITS["1m"]],
  [/^qwen3-coder-flash/, LIMITS["1m"]],
  [/^qwen3\.\d/, LIMITS["1m"]],
  [/^qwen-plus-latest$/, LIMITS["1m"]],
  [/^qwen-flash-latest$/, LIMITS["1m"]],
  [/^coder-model$/, LIMITS["1m"]],
  // Commercial API models (256K context)
  [/^qwen3-max/, LIMITS["256k"]],
  // Open-source Qwen3 variants: 256K native
  [/^qwen3-coder-/, LIMITS["256k"]],
  // Qwen fallback (VL, turbo, plus, 2.5, etc.): 128K
  [/^qwen/, LIMITS["256k"]],
  // -------------------
  // DeepSeek
  // -------------------
  [/^deepseek-v4/, LIMITS["1m"]],
  // DeepSeek V4 (flash, pro): 1M
  [/^deepseek/, LIMITS["128k"]],
  // -------------------
  // Zhipu GLM
  // -------------------
  // 1M context is the forward default for new GLM releases (GLM-5.2+, GLM-6.x,
  // and beyond) so they need no future code change. Confirmed 200K families
  // (GLM-5 / 5.0 / 5.1, GLM-4.x and older) are pinned explicitly first.
  [/^glm-5(\.[01])?(-|$)/, 202752],
  // GLM-5 / 5.0 / 5.1: 200K
  [/^glm-(?:[5-9]|\d{2,})/, LIMITS["1m"]],
  // GLM-5.2+, 6.x..9.x, 10.x+: 1M
  [/^glm-/, 202752],
  // GLM <=4.x / non-numeric fallback: 200K
  // -------------------
  // MiniMax
  // -------------------
  [/^minimax-m3/i, LIMITS["1m"]],
  // MiniMax-M3: 1,000,000
  [/^minimax-m2\.5/i, LIMITS["192k"]],
  // MiniMax-M2.5: 196,608
  [/^minimax-/i, LIMITS["200k"]],
  // MiniMax fallback: 200K
  // -------------------
  // Moonshot / Kimi
  // -------------------
  [/^kimi-/, LIMITS["256k"]],
  // Kimi fallback: 256K
  // -------------------
  // ByteDance Seed-OSS (512K)
  // -------------------
  [/^seed-oss/, LIMITS["512k"]]
];
var OUTPUT_PATTERNS = [
  // Google Gemini
  [/^gemini-3/, LIMITS["64k"]],
  // Gemini 3.x: 64K
  [/^gemini-/, LIMITS["8k"]],
  // Gemini fallback: 8K
  // OpenAI
  [/^gpt-5/, LIMITS["128k"]],
  // GPT-5.x: 128K
  [/^gpt-/, LIMITS["16k"]],
  // GPT fallback: 16K
  [/^o\d/, LIMITS["128k"]],
  // o-series: 128K
  // Anthropic Claude
  [/^claude-opus-4-6/, LIMITS["128k"]],
  // Opus 4.6: 128K
  [/^claude-sonnet-4-6/, LIMITS["64k"]],
  // Sonnet 4.6: 64K
  [/^claude-/, LIMITS["64k"]],
  // Claude fallback: 64K
  // Alibaba / Qwen
  [/^qwen3\.\d/, LIMITS["64k"]],
  [/^coder-model$/, LIMITS["64k"]],
  [/^qwen/, LIMITS["32k"]],
  // Qwen fallback (VL, turbo, plus, etc.): 32K
  // DeepSeek
  [/^deepseek-v4/, LIMITS["384k"]],
  // DeepSeek V4 (flash, pro): 384K
  [/^deepseek-reasoner/, LIMITS["64k"]],
  [/^deepseek-r1/, LIMITS["64k"]],
  [/^deepseek-chat/, LIMITS["8k"]],
  // Zhipu GLM
  [/^glm-5(?:\.\d+)?(?:-|$)/, LIMITS["128k"]],
  [/^glm-4\.7/, LIMITS["16k"]],
  // MiniMax
  [/^minimax-m2\.5/i, LIMITS["64k"]],
  // Kimi
  [/^kimi-k2\.5/, LIMITS["32k"]]
];
function findTokenLimit(model, type = "input") {
  const norm = normalize(model);
  const patterns = type === "output" ? OUTPUT_PATTERNS : PATTERNS;
  for (const [regex, limit] of patterns) {
    if (regex.test(norm)) {
      return limit;
    }
  }
  return void 0;
}
__name(findTokenLimit, "findTokenLimit");
function hasExplicitOutputLimit(model) {
  const norm = normalize(model);
  return OUTPUT_PATTERNS.some(([regex]) => regex.test(norm));
}
__name(hasExplicitOutputLimit, "hasExplicitOutputLimit");
function knownTokenLimit(model, type = "input") {
  return findTokenLimit(model, type);
}
__name(knownTokenLimit, "knownTokenLimit");
function tokenLimit(model, type = "input") {
  return knownTokenLimit(model, type) ?? (type === "output" ? DEFAULT_OUTPUT_TOKEN_LIMIT : DEFAULT_TOKEN_LIMIT);
}
__name(tokenLimit, "tokenLimit");

// packages/core/src/core/openaiContentGenerator/constants.ts
init_esbuild_shims();
var DEFAULT_TIMEOUT = 12e4;
var DEFAULT_STREAM_IDLE_TIMEOUT_MS = 24e4;
var QWEN_STREAM_IDLE_TIMEOUT_MS_ENV = "QWEN_STREAM_IDLE_TIMEOUT_MS";
var MAX_STREAM_IDLE_TIMEOUT_MS = 2147483647;
var DEFAULT_MAX_RETRIES = 3;
var DEFAULT_OPENAI_BASE_URL = "https://api.openai.com/v1";
var DEFAULT_DASHSCOPE_BASE_URL = "https://dashscope.aliyuncs.com/compatible-mode/v1";
var DASHSCOPE_PROXY_BASE_URL = process.env["DASHSCOPE_PROXY_BASE_URL"];

// packages/core/src/utils/request-tokenizer/textTokenizer.ts
init_esbuild_shims();
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) {
    if (!text || text.length === 0) {
      return 0;
    }
    let asciiChars = 0;
    let nonAsciiChars = 0;
    for (let i = 0; i < text.length; i++) {
      const charCode = text.charCodeAt(i);
      if (charCode < 128) {
        asciiChars++;
      } else {
        nonAsciiChars++;
      }
    }
    const tokens = asciiChars / 4 + nonAsciiChars * 1.1;
    return Math.ceil(tokens);
  }
};

// packages/core/src/utils/request-tokenizer/supportedImageFormats.ts
init_esbuild_shims();
var SUPPORTED_IMAGE_MIME_TYPES = [
  "image/bmp",
  "image/gif",
  "image/jpeg",
  "image/jpg",
  // Alternative MIME type for JPEG
  "image/png",
  "image/tiff",
  "image/webp",
  "image/heic"
];
function isSupportedImageMimeType(mimeType) {
  return SUPPORTED_IMAGE_MIME_TYPES.includes(
    mimeType
  );
}
__name(isSupportedImageMimeType, "isSupportedImageMimeType");
function getSupportedImageFormatsString() {
  return SUPPORTED_IMAGE_MIME_TYPES.map(
    (type) => type.replace("image/", "").toUpperCase()
  ).join(", ");
}
__name(getSupportedImageFormatsString, "getSupportedImageFormatsString");
function getUnsupportedImageFormatWarning() {
  return `Only the following image formats are supported: ${getSupportedImageFormatsString()}. Other formats may not work as expected.`;
}
__name(getUnsupportedImageFormatWarning, "getUnsupportedImageFormatWarning");

export {
  DEFAULT_TOKEN_LIMIT,
  DEFAULT_OUTPUT_TOKEN_LIMIT,
  ESCALATED_MAX_TOKENS,
  parsePositiveIntegerEnvValue,
  normalize,
  hasExplicitOutputLimit,
  knownTokenLimit,
  tokenLimit,
  DEFAULT_TIMEOUT,
  DEFAULT_STREAM_IDLE_TIMEOUT_MS,
  QWEN_STREAM_IDLE_TIMEOUT_MS_ENV,
  MAX_STREAM_IDLE_TIMEOUT_MS,
  DEFAULT_MAX_RETRIES,
  DEFAULT_OPENAI_BASE_URL,
  DEFAULT_DASHSCOPE_BASE_URL,
  DASHSCOPE_PROXY_BASE_URL,
  TextTokenizer,
  SUPPORTED_IMAGE_MIME_TYPES,
  isSupportedImageMimeType,
  getSupportedImageFormatsString,
  getUnsupportedImageFormatWarning
};
/**
 * @license
 * Copyright 2025 Qwen
 * SPDX-License-Identifier: Apache-2.0
 */
