|
| 1 | +/** |
| 2 | + * CJK-aware character weighting for Ollama usage fallback estimates. |
| 3 | + * |
| 4 | + * This stays plugin-private because exposing it through the Plugin SDK would |
| 5 | + * create a stable public contract for one provider-specific fallback. Keep |
| 6 | + * the weighting aligned with normalization-core's CJK budget heuristic. |
| 7 | + */ |
| 8 | + |
| 9 | +const CHARS_PER_TOKEN_ESTIMATE = 4; |
| 10 | + |
| 11 | +const NON_ASCII_RE = /[\u0080-\u{10FFFF}]/u; |
| 12 | +const COMMON_CJK_RE = /[\u00B7\u3000-\u319F\u4E00-\u9FA5\uAC00-\uD7AF\uFF01-\uFF60]/gu; |
| 13 | +const RARE_BMP_CJK_RE = |
| 14 | + /[\u1100-\u11FF\u2E80-\u2FFF\u31A0-\u4DFF\u9FA6-\u9FFF\uA000-\uA4FF\uA700-\uA707\uA960-\uA97F\uD7B0-\uD7FF\uF900-\uFAFF]/gu; |
| 15 | +const TWO_TOKEN_CJK_RE = |
| 16 | + /[\u{02C7}\u{02C9}-\u{02CB}\u{02D9}\u{02EA}-\u{02EB}\uFE10-\uFE4F\uFF61-\uFFDC\uFFE0-\uFFE6]|\u{0305}|\u{0323}/gu; |
| 17 | +const THREE_TOKEN_SUPPLEMENTARY_CJK_RE = /[\u{1D360}-\u{1D371}]/gu; |
| 18 | +const SUPPLEMENTARY_CJK_RE = |
| 19 | + /[\u{16FE0}-\u{16FFF}\u{1AFF0}-\u{1AFFF}\u{1B000}-\u{1B16F}\u{1F200}-\u{1F2FF}\u{20000}-\u{2FA1F}\u{30000}-\u{3347F}]/gu; |
| 20 | +const SPECIAL_CJK_RE = |
| 21 | + /[\u{02C7}\u{02C9}-\u{02CB}\u{02D9}\u{02EA}-\u{02EB}\u1100-\u11FF\u2E80-\u2FFF\u31A0-\u4DFF\u9FA6-\u9FFF\uA000-\uA4FF\uA700-\uA707\uA960-\uA97F\uD7B0-\uD7FF\uF900-\uFAFF\uFE10-\uFE4F\uFF61-\uFFDC\uFFE0-\uFFE6\u{16FE0}-\u{16FFF}\u{1AFF0}-\u{1AFFF}\u{1B000}-\u{1B16F}\u{1D360}-\u{1D371}\u{1F200}-\u{1F2FF}\u{20000}-\u{2FA1F}\u{30000}-\u{3347F}]|\u{0305}|\u{0323}/u; |
| 22 | + |
| 23 | +function countMatches(text: string, pattern: RegExp): number { |
| 24 | + return (text.match(pattern) ?? []).length; |
| 25 | +} |
| 26 | + |
| 27 | +export function estimateStringChars(text: string): number { |
| 28 | + if (!NON_ASCII_RE.test(text)) { |
| 29 | + return text.length; |
| 30 | + } |
| 31 | + const commonCjkCount = countMatches(text, COMMON_CJK_RE); |
| 32 | + const commonEstimate = text.length + commonCjkCount * (CHARS_PER_TOKEN_ESTIMATE - 1); |
| 33 | + if (!SPECIAL_CJK_RE.test(text)) { |
| 34 | + return commonEstimate; |
| 35 | + } |
| 36 | + const rareBmpCjkCount = countMatches(text, RARE_BMP_CJK_RE); |
| 37 | + const twoTokenCjkCount = countMatches(text, TWO_TOKEN_CJK_RE); |
| 38 | + const threeTokenSupplementaryCjkCount = countMatches(text, THREE_TOKEN_SUPPLEMENTARY_CJK_RE); |
| 39 | + const supplementaryCjkCount = countMatches(text, SUPPLEMENTARY_CJK_RE); |
| 40 | + return ( |
| 41 | + commonEstimate + |
| 42 | + rareBmpCjkCount * (CHARS_PER_TOKEN_ESTIMATE * 3 - 1) + |
| 43 | + twoTokenCjkCount * (CHARS_PER_TOKEN_ESTIMATE * 2 - 1) + |
| 44 | + threeTokenSupplementaryCjkCount * (CHARS_PER_TOKEN_ESTIMATE * 3 - 2) + |
| 45 | + supplementaryCjkCount * (CHARS_PER_TOKEN_ESTIMATE * 4 - 2) |
| 46 | + ); |
| 47 | +} |
0 commit comments