@meritt/tokenmeter-wasm 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Alexey Simonenko, alexey@simonenko.dev, https://simonenko.xyz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/package.json CHANGED
@@ -1,6 +1,54 @@
1
1
  {
2
2
  "name": "@meritt/tokenmeter-wasm",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.2.0",
4
+ "description": "Counts the tokens of text in o200k_base with tiktoken's algorithm and splits it into token groups on grapheme boundaries, in WebAssembly",
5
+ "keywords": [
6
+ "gpt",
7
+ "o200k_base",
8
+ "openai",
9
+ "tiktoken",
10
+ "tokenizer",
11
+ "tokens",
12
+ "wasm",
13
+ "webassembly"
14
+ ],
15
+ "license": "MIT",
16
+ "author": {
17
+ "name": "Alexey Simonenko",
18
+ "email": "alexey@simonenko.dev",
19
+ "url": "https://simonenko.xyz"
20
+ },
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/meritt/tokenmeter.rs.git",
24
+ "directory": "packages/npm"
25
+ },
26
+ "type": "module",
27
+ "engines": {
28
+ "node": ">=26.13.1"
29
+ },
30
+ "sideEffects": false,
31
+ "exports": {
32
+ ".": "./tokenmeter.js",
33
+ "./tokenmeter.wasm": "./tokenmeter.wasm"
34
+ },
35
+ "files": [
36
+ "tokenmeter.js",
37
+ "tokenmeter.wasm"
38
+ ],
39
+ "publishConfig": {
40
+ "access": "public"
41
+ },
42
+ "devEngines": {
43
+ "packageManager": {
44
+ "name": "pnpm",
45
+ "version": "12.25.1",
46
+ "onFail": "download"
47
+ },
48
+ "runtime": {
49
+ "name": "node",
50
+ "version": "26.14.0",
51
+ "onFail": "error"
52
+ }
53
+ }
6
54
  }
package/tokenmeter.js ADDED
@@ -0,0 +1,151 @@
1
+ const VERSION = '0.2.0';
2
+ const STATUS = ['ok', 'invalid_utf8', 'invalid_argument', 'internal', 'input_too_large', 'tokenization'];
3
+ const MAX_BYTES = 1048576;
4
+ const GROUPS = { size: 20, status: 0, tokens: 4, truncated: 8, pairs: 12, length: 16 };
5
+ const encoder = new TextEncoder();
6
+ const decoder = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true });
7
+
8
+ export class TokenMeterError extends Error {
9
+ constructor(code) {
10
+ super(code);
11
+ this.name = 'TokenMeterError';
12
+ this.code = code;
13
+ }
14
+ }
15
+
16
+ export async function load(module) {
17
+ let exports;
18
+ try {
19
+ exports = WebAssembly.Module.exports(module);
20
+ } catch {
21
+ throw new TokenMeterError('invalid_argument');
22
+ }
23
+ if (!exports.some(item => item.name === 'tokenmeter_version' && item.kind === 'function')) {
24
+ throw new TokenMeterError('version');
25
+ }
26
+ let instance;
27
+ try {
28
+ instance = await WebAssembly.instantiate(module, {});
29
+ } catch {
30
+ throw new TokenMeterError('internal');
31
+ }
32
+ return new TokenMeter(instance.exports);
33
+ }
34
+
35
+ function encode(text) {
36
+ if (typeof text !== 'string') throw new TokenMeterError('invalid_argument');
37
+ if (text.length > MAX_BYTES) throw new TokenMeterError('input_too_large');
38
+ if (!text.isWellFormed()) throw new TokenMeterError('invalid_utf8');
39
+ const bytes = encoder.encode(text);
40
+ if (bytes.length > MAX_BYTES) throw new TokenMeterError('input_too_large');
41
+ return bytes;
42
+ }
43
+
44
+ class TokenMeter {
45
+ #exports;
46
+
47
+ constructor(exports) {
48
+ this.#exports = exports;
49
+ this.#run(() => {
50
+ if (this.#cString('tokenmeter_version') !== VERSION) throw new TokenMeterError('version');
51
+ this.version = VERSION;
52
+ this.encoding = this.#cString('tokenmeter_encoding');
53
+ this.#check(this.#call('tokenmeter_prepare'));
54
+ });
55
+ }
56
+
57
+ count(text) {
58
+ return this.#run(() => this.#withText(text, 4, (bytes, input, out) => {
59
+ this.#check(this.#call('tokenmeter_count', input, bytes.length, out));
60
+ return new DataView(this.#exports.memory.buffer, out, 4).getUint32(0, true);
61
+ }));
62
+ }
63
+
64
+ split(text, limit = 200) {
65
+ return this.#run(() => {
66
+ if (!Number.isInteger(limit) || limit < 0 || limit > 0xffffffff) throw new TokenMeterError('invalid_argument');
67
+ return this.#withText(text, GROUPS.size, (bytes, input, out) => {
68
+ this.#call('tokenmeter_split', input, bytes.length, limit, out);
69
+ try {
70
+ return this.#groups(bytes, out);
71
+ } finally {
72
+ if (this.#exports) this.#call('tokenmeter_split_free', out);
73
+ }
74
+ });
75
+ });
76
+ }
77
+
78
+ #groups(bytes, out) {
79
+ const buffer = this.#exports.memory.buffer;
80
+ const view = new DataView(buffer, out, GROUPS.size);
81
+ this.#check(view.getUint32(GROUPS.status, true));
82
+ const pairs = new Uint32Array(buffer, view.getUint32(GROUPS.pairs, true), view.getUint32(GROUPS.length, true) * 2);
83
+ const groups = [];
84
+ let start = 0;
85
+ for (let i = 0; i < pairs.length; i += 2) {
86
+ groups.push({ text: decoder.decode(bytes.subarray(start, pairs[i])), tokens: pairs[i + 1] });
87
+ start = pairs[i];
88
+ }
89
+ return {
90
+ tokens: view.getUint32(GROUPS.tokens, true),
91
+ groups,
92
+ truncated: view.getUint32(GROUPS.truncated, true) !== 0,
93
+ };
94
+ }
95
+
96
+ #withText(text, size, use) {
97
+ const bytes = encode(text);
98
+ return this.#buffer(size, out => this.#buffer(bytes.length, input => {
99
+ new Uint8Array(this.#exports.memory.buffer, input, bytes.length).set(bytes);
100
+ return use(bytes, input, out);
101
+ }));
102
+ }
103
+
104
+ #buffer(size, use) {
105
+ const p = this.#call('tokenmeter_alloc', size) >>> 0;
106
+ if (!p && size) this.#fatal();
107
+ try {
108
+ return use(p);
109
+ } finally {
110
+ if (this.#exports && p) this.#call('tokenmeter_dealloc', p, size);
111
+ }
112
+ }
113
+
114
+ #cString(name) {
115
+ const p = this.#call(name) >>> 0;
116
+ const bytes = new Uint8Array(this.#exports.memory.buffer);
117
+ const end = bytes.indexOf(0, p);
118
+ if (end < 0) this.#fatal();
119
+ return decoder.decode(bytes.subarray(p, end));
120
+ }
121
+
122
+ #check(status) {
123
+ if (status === 0) return;
124
+ const code = STATUS[status];
125
+ if (!code || code === 'internal') this.#fatal();
126
+ throw new TokenMeterError(code);
127
+ }
128
+
129
+ #call(name, ...args) {
130
+ try {
131
+ return this.#exports[name](...args);
132
+ } catch {
133
+ this.#fatal();
134
+ }
135
+ }
136
+
137
+ #run(use) {
138
+ if (!this.#exports) throw new TokenMeterError('internal');
139
+ try {
140
+ return use();
141
+ } catch (error) {
142
+ if (error instanceof TokenMeterError) throw error;
143
+ this.#fatal();
144
+ }
145
+ }
146
+
147
+ #fatal() {
148
+ this.#exports = null;
149
+ throw new TokenMeterError('internal');
150
+ }
151
+ }
Binary file
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.