MCPcopy Create free account
hub / github.com/ChatLab/ChatLab / estimateTokens

Function estimateTokens

packages/node-runtime/src/semantic-index/tokens.ts:7–15  ·  view source on GitHub ↗
(text: string)

Source from the content-addressed store, hash-verified

5 * 估算规则:CJK 每字约 1 token,其余字符约 1/4 token。
6 */
7export 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}

Callers 4

segmentParentsFunction · 0.90
buildChildDraftsFunction · 0.90
messagesTokensFunction · 0.90
evidence.test.tsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected