(text: string)
| 5 | * 估算规则:CJK 每字约 1 token,其余字符约 1/4 token。 |
| 6 | */ |
| 7 | export function estimateTokens(text: string): number { |
| 8 | let cjk = 0 |
| 9 | let other = 0 |
| 10 | for (const ch of text) { |
| 11 | if (/[\u3400-\u9fff\uf900-\ufaff]/u.test(ch)) cjk++ |
| 12 | else other++ |
| 13 | } |
| 14 | return cjk + Math.ceil(other / 4) |
| 15 | } |
no outgoing calls
no test coverage detected