// 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/core/tokenLimits.ts
init_esbuild_shims();
var DEFAULT_TOKEN_LIMIT = 2e5;
var DEFAULT_OUTPUT_TOKEN_LIMIT = 32e3;
var ESCALATED_MAX_TOKENS = 64e3;
var OUTPUT_TOKEN_CEILING = ESCALATED_MAX_TOKENS;
var MIN_CLAMPED_OUTPUT_TOKENS = 4e3;
function outputClampMargin(contextWindowSize) {
  return Math.max(1e4, Math.round(0.05 * contextWindowSize));
}
__name(outputClampMargin, "outputClampMargin");
function clampOutputTokensToWindow(outputCeiling, contextWindowSize, promptTokens) {
  const room = contextWindowSize - promptTokens - outputClampMargin(contextWindowSize);
  return Math.min(outputCeiling, Math.max(MIN_CLAMPED_OUTPUT_TOKENS, room));
}
__name(clampOutputTokensToWindow, "clampOutputTokensToWindow");
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.replace(
    /:(?:free|beta|extended|thinking|online|nitro|floor|latest|\d+(?:\.\d+)?(?:x\d+)?b(?:-[\w.]+)*)$/,
    ""
  );
  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-opus-4-(?:6|7|8)/, LIMITS["1m"]],
  // Opus 4.6-4.8: 1M
  [/^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.): 256K
  [/^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-k3/, LIMITS["1m"]],
  // Kimi K3: 1M
  [/^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|7|8)/, 128e3],
  // Opus 4.6-4.8: 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-k3/, LIMITS["128k"]],
  // Kimi K3: 128K default max output (up to 1M configurable)
  [/^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");
function defaultOutputCeiling(model) {
  const outputLimit = tokenLimit(model, "output");
  if (/^claude-opus-4-(?:6|7|8)/.test(normalize(model))) {
    return outputLimit;
  }
  return Math.min(outputLimit, OUTPUT_TOKEN_CEILING);
}
__name(defaultOutputCeiling, "defaultOutputCeiling");
function reconcileMaxTokens(configMaxTokens, requestMaxTokens) {
  if (typeof configMaxTokens === "number" && typeof requestMaxTokens === "number") {
    return Math.min(configMaxTokens, requestMaxTokens);
  }
  return void 0;
}
__name(reconcileMaxTokens, "reconcileMaxTokens");

export {
  DEFAULT_TOKEN_LIMIT,
  OUTPUT_TOKEN_CEILING,
  clampOutputTokensToWindow,
  parsePositiveIntegerEnvValue,
  normalize,
  hasExplicitOutputLimit,
  knownTokenLimit,
  tokenLimit,
  defaultOutputCeiling,
  reconcileMaxTokens
};
