@chonkiejs/chunk 0.9.1 → 0.9.3
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/index.js +22 -1
- package/package.json +1 -1
- package/pkg/chonkiejs_chunk.d.ts +24 -1
- package/pkg/chonkiejs_chunk.js +33 -0
- package/pkg/chonkiejs_chunk_bg.wasm +0 -0
- package/pkg/chonkiejs_chunk_bg.wasm.d.ts +2 -1
- package/pkg/package.json +1 -1
package/index.js
CHANGED
|
@@ -28,6 +28,7 @@ import initWasm, {
|
|
|
28
28
|
chunk_offsets_pattern as wasmChunkOffsetsPattern,
|
|
29
29
|
split_offsets as wasmSplitOffsets,
|
|
30
30
|
merge_splits as wasmMergeSplits,
|
|
31
|
+
initSync as initWasmSync,
|
|
31
32
|
} from './pkg/chonkiejs_chunk.js';
|
|
32
33
|
|
|
33
34
|
export { default_target_size, default_delimiters };
|
|
@@ -220,10 +221,30 @@ let initialized = false;
|
|
|
220
221
|
|
|
221
222
|
/**
|
|
222
223
|
* Initialize the WASM module. Must be called before using chunk functions.
|
|
224
|
+
* Automatically detects Node.js vs browser environment.
|
|
223
225
|
*/
|
|
224
226
|
export async function init() {
|
|
225
227
|
if (!initialized) {
|
|
226
|
-
|
|
228
|
+
// Check if we're in Node.js
|
|
229
|
+
const isNode = typeof process !== 'undefined' &&
|
|
230
|
+
process.versions != null &&
|
|
231
|
+
process.versions.node != null;
|
|
232
|
+
|
|
233
|
+
if (isNode) {
|
|
234
|
+
// Node.js: read the wasm file and use initSync
|
|
235
|
+
const { readFileSync } = await import('node:fs');
|
|
236
|
+
const { fileURLToPath } = await import('node:url');
|
|
237
|
+
const { dirname, join } = await import('node:path');
|
|
238
|
+
|
|
239
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
240
|
+
const __dirname = dirname(__filename);
|
|
241
|
+
const wasmPath = join(__dirname, 'pkg', 'chonkiejs_chunk_bg.wasm');
|
|
242
|
+
const wasmBytes = readFileSync(wasmPath);
|
|
243
|
+
initWasmSync({ module: wasmBytes });
|
|
244
|
+
} else {
|
|
245
|
+
// Browser: use fetch-based init
|
|
246
|
+
await initWasm();
|
|
247
|
+
}
|
|
227
248
|
initialized = true;
|
|
228
249
|
}
|
|
229
250
|
}
|
package/package.json
CHANGED
package/pkg/chonkiejs_chunk.d.ts
CHANGED
|
@@ -101,6 +101,28 @@ export function default_target_size(): number;
|
|
|
101
101
|
*/
|
|
102
102
|
export function find_merge_indices(token_counts: Uint32Array, chunk_size: number): Uint32Array;
|
|
103
103
|
|
|
104
|
+
/**
|
|
105
|
+
* Merge segments based on token counts, respecting chunk size limits.
|
|
106
|
+
* Returns a flat array [endIndex1, tokenCount1, endIndex2, tokenCount2, ...].
|
|
107
|
+
*
|
|
108
|
+
* This is the equivalent of Chonkie's `_merge_splits` function.
|
|
109
|
+
* Used by RecursiveChunker to merge small segments into larger chunks
|
|
110
|
+
* that fit within a token budget.
|
|
111
|
+
*
|
|
112
|
+
* @param token_counts - Array of token counts for each segment
|
|
113
|
+
* @param chunk_size - Maximum tokens per merged chunk
|
|
114
|
+
* @param combine_whitespace - If true, adds +1 token per join for whitespace
|
|
115
|
+
* @returns Flat array of [endIndex, tokenCount] pairs
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```javascript
|
|
119
|
+
* const result = merge_splits([1, 1, 1, 1, 1, 1, 1], 3, false);
|
|
120
|
+
* // result = [3, 3, 6, 3, 7, 1]
|
|
121
|
+
* // Meaning: chunk 0-3 has 3 tokens, chunk 3-6 has 3 tokens, chunk 6-7 has 1 token
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
export function merge_splits(token_counts: Uint32Array, chunk_size: number, combine_whitespace?: boolean | null): Uint32Array;
|
|
125
|
+
|
|
104
126
|
/**
|
|
105
127
|
* Split text at every delimiter occurrence, returning offsets.
|
|
106
128
|
* Unlike chunk_offsets which creates size-based chunks, this splits at
|
|
@@ -140,11 +162,12 @@ export interface InitOutput {
|
|
|
140
162
|
readonly default_delimiters: () => [number, number];
|
|
141
163
|
readonly default_target_size: () => number;
|
|
142
164
|
readonly find_merge_indices: (a: number, b: number, c: number) => [number, number];
|
|
165
|
+
readonly merge_splits: (a: number, b: number, c: number, d: number) => [number, number];
|
|
143
166
|
readonly split_offsets: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number];
|
|
144
167
|
readonly __wbindgen_externrefs: WebAssembly.Table;
|
|
145
168
|
readonly __wbindgen_malloc: (a: number, b: number) => number;
|
|
146
|
-
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
147
169
|
readonly __wbindgen_free: (a: number, b: number, c: number) => void;
|
|
170
|
+
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
148
171
|
readonly __wbindgen_start: () => void;
|
|
149
172
|
}
|
|
150
173
|
|
package/pkg/chonkiejs_chunk.js
CHANGED
|
@@ -352,6 +352,39 @@ export function find_merge_indices(token_counts, chunk_size) {
|
|
|
352
352
|
return v2;
|
|
353
353
|
}
|
|
354
354
|
|
|
355
|
+
/**
|
|
356
|
+
* Merge segments based on token counts, respecting chunk size limits.
|
|
357
|
+
* Returns a flat array [endIndex1, tokenCount1, endIndex2, tokenCount2, ...].
|
|
358
|
+
*
|
|
359
|
+
* This is the equivalent of Chonkie's `_merge_splits` function.
|
|
360
|
+
* Used by RecursiveChunker to merge small segments into larger chunks
|
|
361
|
+
* that fit within a token budget.
|
|
362
|
+
*
|
|
363
|
+
* @param token_counts - Array of token counts for each segment
|
|
364
|
+
* @param chunk_size - Maximum tokens per merged chunk
|
|
365
|
+
* @param combine_whitespace - If true, adds +1 token per join for whitespace
|
|
366
|
+
* @returns Flat array of [endIndex, tokenCount] pairs
|
|
367
|
+
*
|
|
368
|
+
* @example
|
|
369
|
+
* ```javascript
|
|
370
|
+
* const result = merge_splits([1, 1, 1, 1, 1, 1, 1], 3, false);
|
|
371
|
+
* // result = [3, 3, 6, 3, 7, 1]
|
|
372
|
+
* // Meaning: chunk 0-3 has 3 tokens, chunk 3-6 has 3 tokens, chunk 6-7 has 1 token
|
|
373
|
+
* ```
|
|
374
|
+
* @param {Uint32Array} token_counts
|
|
375
|
+
* @param {number} chunk_size
|
|
376
|
+
* @param {boolean | null} [combine_whitespace]
|
|
377
|
+
* @returns {Uint32Array}
|
|
378
|
+
*/
|
|
379
|
+
export function merge_splits(token_counts, chunk_size, combine_whitespace) {
|
|
380
|
+
const ptr0 = passArray32ToWasm0(token_counts, wasm.__wbindgen_malloc);
|
|
381
|
+
const len0 = WASM_VECTOR_LEN;
|
|
382
|
+
const ret = wasm.merge_splits(ptr0, len0, chunk_size, isLikeNone(combine_whitespace) ? 0xFFFFFF : combine_whitespace ? 1 : 0);
|
|
383
|
+
var v2 = getArrayU32FromWasm0(ret[0], ret[1]).slice();
|
|
384
|
+
wasm.__wbindgen_free(ret[0], ret[1] * 4, 4);
|
|
385
|
+
return v2;
|
|
386
|
+
}
|
|
387
|
+
|
|
355
388
|
/**
|
|
356
389
|
* Split text at every delimiter occurrence, returning offsets.
|
|
357
390
|
* Unlike chunk_offsets which creates size-based chunks, this splits at
|
|
Binary file
|
|
@@ -12,9 +12,10 @@ export const chunker_with_pattern: (a: number, b: number, c: number, d: number,
|
|
|
12
12
|
export const default_delimiters: () => [number, number];
|
|
13
13
|
export const default_target_size: () => number;
|
|
14
14
|
export const find_merge_indices: (a: number, b: number, c: number) => [number, number];
|
|
15
|
+
export const merge_splits: (a: number, b: number, c: number, d: number) => [number, number];
|
|
15
16
|
export const split_offsets: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number];
|
|
16
17
|
export const __wbindgen_externrefs: WebAssembly.Table;
|
|
17
18
|
export const __wbindgen_malloc: (a: number, b: number) => number;
|
|
18
|
-
export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
19
19
|
export const __wbindgen_free: (a: number, b: number, c: number) => void;
|
|
20
|
+
export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
20
21
|
export const __wbindgen_start: () => void;
|