UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

2 lines • 3.02 kB
//!packages/normalization-core/src/cjk-chars.ts const CHARS_PER_TOKEN_ESTIMATE=4;const DEFAULT_BUDGET_OPTIONS={};const TOKEN_PRESSURE_RUN_RE=/[A-Za-z0-9+/_=-]{16,}|\b[0-9]+\b|[{}[\]:,]/g;const TOKEN_PRESSURE_PART_RE=/[A-Za-z]+|[0-9]{1,3}|[^A-Za-z0-9]/y;const ASCII_RUN_OR_NON_ASCII_CODE_POINT_RE=/[\p{ASCII}]+|[^\p{ASCII}]/gu;const WEIGHTED_CJK_RANGES=[[[183,183],[12288,12703],[19968,40869],[44032,55215],[65281,65376]],[[4352,4607],[11904,12287],[12704,19967],[40870,40959],[40960,42239],[42752,42759],[43360,43391],[55216,55295],[63744,64255]],[[711,711],[713,715],[729,729],[746,747],[773,773],[803,803],[65040,65103],[65377,65500],[65504,65510]],[[119648,119665]],[[94176,94207],[94208,101647],[110576,110591],[110592,111359],[127488,127743],[131072,195103],[196608,210047]]];const WEIGHTED_CJK_RE=/* @__PURE__ */ (()=>new RegExp(`[${WEIGHTED_CJK_RANGES.flatMap(e=>e.map(([e,t])=>`\\u{${e.toString(16)}}-\\u{${t.toString(16)}}`)).join(``)}]`,`u`))();const CJK_CATEGORY=/* @__PURE__ */ (()=>{const e=Math.max(...WEIGHTED_CJK_RANGES.flatMap(e=>e.map(([,e])=>e)));const t=new Uint8Array(e+1);for(const[e,n]of WEIGHTED_CJK_RANGES.entries())for(const[r,i]of n)t.fill(e+1,r,i+1);return t})();function estimateStringChars(e){return estimateStringCharsWithMinimumRawWeight(e)}function estimateStringCharsWithMinimumRawWeight(e,t=DEFAULT_BUDGET_OPTIONS){const n=Math.max(1,t.minimumRawWeight??1);if(n!==1&&n!==2){let t=0;for(const r of e.matchAll(ASCII_RUN_OR_NON_ASCII_CODE_POINT_RE)){const e=r[0];const i=Math.ceil(e.length*n);t+=e.charCodeAt(0)<=127?i:Math.max(estimateStringChars(e),i)}return t}const r=e.search(WEIGHTED_CJK_RE);if(r<0)return e.length*n;let i=0;let a=0;let o=0;let s=0;let c=0;for(let t=r;t<e.length;t+=1){let n=e.charCodeAt(t);if(n<128)continue;if(n>=55296&&n<=56319){const r=e.charCodeAt(t+1);if(r>=56320&&r<=57343){n=65536+(n-55296)*1024+r-56320;t+=1}}switch(CJK_CATEGORY[n]){case 1:i+=1;break;case 2:a+=1;break;case 3:o+=1;break;case 4:s+=1;break;case 5:c+=1}}return e.length*n+i*(4-n)+a*(12-n)+o*(8-n)+s*(12-2*n)+c*(16-2*n)}function estimateTokensFromChars(e){return Math.ceil(Math.max(0,e)/4)}function estimateStringTokenPressure(e,t=4){let n=estimateStringChars(e)/t;TOKEN_PRESSURE_RUN_RE.lastIndex=0;let r=0;while(TOKEN_PRESSURE_RUN_RE.test(e)){const i=TOKEN_PRESSURE_RUN_RE.lastIndex;let a=i;let o=i;let s=false;let c=false;while(a>r){const t=e.charCodeAt(a-1);const n=t>=48&&t<=57;const r=t>=65&&t<=90||t>=97&&t<=122;if(!n&&!r&&t!==43&&t!==47&&t!==95&&t!==61&&t!==45)break;a--;s||=r;c||=n;if(n&&o===a+1)o=a}r=i;if(a===i)a--;else if(i-a<16)a=o;const l=i-a;let u=0;if(o===a)u=Math.ceil(l/3);else if(l===1)u=1;else{if(l<64&&!(s&&c))continue;TOKEN_PRESSURE_PART_RE.lastIndex=a;while(a<i&&TOKEN_PRESSURE_PART_RE.test(e)){const e=TOKEN_PRESSURE_PART_RE.lastIndex;u+=Math.ceil((e-a)/t);a=e}}n+=Math.max(0,u-l/t)}return Math.ceil(n)}export{estimateTokensFromChars as a,estimateStringTokenPressure as i,estimateStringChars as n,estimateStringCharsWithMinimumRawWeight as r,CHARS_PER_TOKEN_ESTIMATE as t};