|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Qwen Team |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * Browser-safe subset of @qwen-code/qwen-code-core tokenLimits. |
| 9 | + * |
| 10 | + * The webview bundle (IIFE, platform: browser) cannot `require` Node.js |
| 11 | + * packages. This module replicates the constants and logic the webview |
| 12 | + * actually uses so that the core package never needs to be pulled into the |
| 13 | + * browser bundle. |
| 14 | + * |
| 15 | + * Keep this file in sync with: |
| 16 | + * packages/core/src/core/tokenLimits.ts |
| 17 | + */ |
| 18 | + |
| 19 | +type TokenCount = number; |
| 20 | + |
| 21 | +// --------------------------------------------------------------------------- |
| 22 | +// Public constants |
| 23 | +// --------------------------------------------------------------------------- |
| 24 | + |
| 25 | +/** Default input context window size: 128 K tokens (power-of-two). */ |
| 26 | +export const DEFAULT_TOKEN_LIMIT: TokenCount = 131_072; |
| 27 | + |
| 28 | +// --------------------------------------------------------------------------- |
| 29 | +// Token limit types |
| 30 | +// --------------------------------------------------------------------------- |
| 31 | + |
| 32 | +export type TokenLimitType = 'input' | 'output'; |
| 33 | + |
| 34 | +// --------------------------------------------------------------------------- |
| 35 | +// Internal constants |
| 36 | +// --------------------------------------------------------------------------- |
| 37 | + |
| 38 | +const LIMITS = { |
| 39 | + '32k': 32_768, |
| 40 | + '64k': 65_536, |
| 41 | + '128k': 131_072, |
| 42 | + '192k': 196_608, |
| 43 | + '200k': 200_000, |
| 44 | + '256k': 262_144, |
| 45 | + '272k': 272_000, |
| 46 | + '400k': 400_000, |
| 47 | + '512k': 524_288, |
| 48 | + '1m': 1_000_000, |
| 49 | + '4k': 4_096, |
| 50 | + '8k': 8_192, |
| 51 | + '16k': 16_384, |
| 52 | +} as const; |
| 53 | + |
| 54 | +const DEFAULT_OUTPUT_TOKEN_LIMIT: TokenCount = 32_000; |
| 55 | + |
| 56 | +// --------------------------------------------------------------------------- |
| 57 | +// Model name normaliser |
| 58 | +// --------------------------------------------------------------------------- |
| 59 | + |
| 60 | +/** |
| 61 | + * Robust normaliser: strips provider prefixes, pipes/colons, date/version |
| 62 | + * suffixes, quantisation markers, etc. |
| 63 | + * @param model - Raw model identifier string |
| 64 | + * @returns Normalised lowercase model name |
| 65 | + */ |
| 66 | +function normalize(model: string): string { |
| 67 | + let s = (model ?? '').toLowerCase().trim(); |
| 68 | + |
| 69 | + s = s.replace(/^.*\//, ''); |
| 70 | + s = s.split('|').pop() ?? s; |
| 71 | + s = s.split(':').pop() ?? s; |
| 72 | + s = s.replace(/\s+/g, '-'); |
| 73 | + s = s.replace(/-preview/g, ''); |
| 74 | + |
| 75 | + if ( |
| 76 | + !s.match(/^qwen-(?:plus|flash|vl-max)-latest$/) && |
| 77 | + !s.match(/^kimi-k2-\d{4}$/) |
| 78 | + ) { |
| 79 | + s = s.replace( |
| 80 | + /-(?:\d{4,}|\d+x\d+b|v\d+(?:\.\d+)*|(?<=-[^-]+-)\d+(?:\.\d+)+|latest|exp)$/g, |
| 81 | + '', |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + s = s.replace(/-(?:\d?bit|int[48]|bf16|fp16|q[45]|quantized)$/g, ''); |
| 86 | + return s; |
| 87 | +} |
| 88 | + |
| 89 | +// --------------------------------------------------------------------------- |
| 90 | +// Input context-window patterns (most specific → most general) |
| 91 | +// --------------------------------------------------------------------------- |
| 92 | + |
| 93 | +const INPUT_PATTERNS: Array<[RegExp, TokenCount]> = [ |
| 94 | + // Google Gemini |
| 95 | + [/^gemini-3/, LIMITS['1m']], |
| 96 | + [/^gemini-/, LIMITS['1m']], |
| 97 | + |
| 98 | + // OpenAI |
| 99 | + [/^gpt-5/, LIMITS['272k']], |
| 100 | + [/^gpt-/, LIMITS['128k']], |
| 101 | + [/^o\d/, LIMITS['200k']], |
| 102 | + |
| 103 | + // Anthropic Claude |
| 104 | + [/^claude-/, LIMITS['200k']], |
| 105 | + |
| 106 | + // Alibaba / Qwen |
| 107 | + [/^qwen3-coder-plus/, LIMITS['1m']], |
| 108 | + [/^qwen3-coder-flash/, LIMITS['1m']], |
| 109 | + [/^qwen3\.\d/, LIMITS['1m']], |
| 110 | + [/^qwen-plus-latest$/, LIMITS['1m']], |
| 111 | + [/^qwen-flash-latest$/, LIMITS['1m']], |
| 112 | + [/^coder-model$/, LIMITS['1m']], |
| 113 | + [/^qwen3-max/, LIMITS['256k']], |
| 114 | + [/^qwen3-coder-/, LIMITS['256k']], |
| 115 | + [/^qwen/, LIMITS['256k']], |
| 116 | + |
| 117 | + // DeepSeek |
| 118 | + [/^deepseek/, LIMITS['128k']], |
| 119 | + |
| 120 | + // Zhipu GLM |
| 121 | + [/^glm-5/, 202_752 as TokenCount], |
| 122 | + [/^glm-/, 202_752 as TokenCount], |
| 123 | + |
| 124 | + // MiniMax |
| 125 | + [/^minimax-m2\.5/i, LIMITS['192k']], |
| 126 | + [/^minimax-/i, LIMITS['200k']], |
| 127 | + |
| 128 | + // Moonshot / Kimi |
| 129 | + [/^kimi-/, LIMITS['256k']], |
| 130 | + |
| 131 | + // ByteDance Seed-OSS |
| 132 | + [/^seed-oss/, LIMITS['512k']], |
| 133 | +]; |
| 134 | + |
| 135 | +// --------------------------------------------------------------------------- |
| 136 | +// Output token-limit patterns |
| 137 | +// --------------------------------------------------------------------------- |
| 138 | + |
| 139 | +const OUTPUT_PATTERNS: Array<[RegExp, TokenCount]> = [ |
| 140 | + [/^gemini-3/, LIMITS['64k']], |
| 141 | + [/^gemini-/, LIMITS['8k']], |
| 142 | + |
| 143 | + [/^gpt-5/, LIMITS['128k']], |
| 144 | + [/^gpt-/, LIMITS['16k']], |
| 145 | + [/^o\d/, LIMITS['128k']], |
| 146 | + |
| 147 | + [/^claude-opus-4-6/, LIMITS['128k']], |
| 148 | + [/^claude-sonnet-4-6/, LIMITS['64k']], |
| 149 | + [/^claude-/, LIMITS['64k']], |
| 150 | + |
| 151 | + [/^qwen3\.\d/, LIMITS['64k']], |
| 152 | + [/^coder-model$/, LIMITS['64k']], |
| 153 | + [/^qwen/, LIMITS['32k']], |
| 154 | + |
| 155 | + [/^deepseek-reasoner/, LIMITS['64k']], |
| 156 | + [/^deepseek-r1/, LIMITS['64k']], |
| 157 | + [/^deepseek-chat/, LIMITS['8k']], |
| 158 | + |
| 159 | + [/^glm-5/, LIMITS['16k']], |
| 160 | + [/^glm-4\.7/, LIMITS['16k']], |
| 161 | + |
| 162 | + [/^minimax-m2\.5/i, LIMITS['64k']], |
| 163 | + |
| 164 | + [/^kimi-k2\.5/, LIMITS['32k']], |
| 165 | +]; |
| 166 | + |
| 167 | +// --------------------------------------------------------------------------- |
| 168 | +// Public API |
| 169 | +// --------------------------------------------------------------------------- |
| 170 | + |
| 171 | +/** |
| 172 | + * Return the token limit for a given model name. |
| 173 | + * |
| 174 | + * This is a browser-safe mirror of `tokenLimit()` in |
| 175 | + * `@qwen-code/qwen-code-core`. The webview only calls this as a fallback |
| 176 | + * when `modelInfo._meta.contextLimit` is unavailable. |
| 177 | + * |
| 178 | + * @param model - The model identifier string |
| 179 | + * @param type - 'input' for context window, 'output' for generation limit |
| 180 | + * @returns Maximum token count for the model and type |
| 181 | + */ |
| 182 | +export function tokenLimit( |
| 183 | + model: string, |
| 184 | + type: TokenLimitType = 'input', |
| 185 | +): TokenCount { |
| 186 | + const norm = normalize(model); |
| 187 | + const patterns = type === 'output' ? OUTPUT_PATTERNS : INPUT_PATTERNS; |
| 188 | + |
| 189 | + for (const [regex, limit] of patterns) { |
| 190 | + if (regex.test(norm)) { |
| 191 | + return limit; |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + return type === 'output' ? DEFAULT_OUTPUT_TOKEN_LIMIT : DEFAULT_TOKEN_LIMIT; |
| 196 | +} |
0 commit comments