@mailwoman/sentencepiece-wasm 10.0.0 → 10.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.
Files changed (4) hide show
  1. package/binding.cpp +2 -2
  2. package/build.sh +11 -11
  3. package/index.d.ts +30 -15
  4. package/package.json +1 -15
package/binding.cpp CHANGED
@@ -58,7 +58,7 @@ public:
58
58
  /**
59
59
  * Encode with native offsets. Returns a JS object:
60
60
  * { pieces: string[], ids: number[], begins: number[], ends: number[] } — begins/ends are byte
61
- * offsets into the UTF-8 encoding of `text` (the exact bytes embind marshalled in).
61
+ * offsets into the UTF-8 encoding of `text` (the exact bytes embind marshaled in).
62
62
  */
63
63
  emscripten::val encodeWithOffsets(const std::string& text) const {
64
64
  sentencepiece::SentencePieceText spt;
@@ -94,7 +94,7 @@ public:
94
94
 
95
95
  /**
96
96
  * Detokenize ids back to text. Invalid ids surface as the empty string plus the error field on
97
- * the wrapper's contract being violated upstream — the TS layer validates lengths.
97
+ * the wrapper's interface being violated upstream — the TS layer validates lengths.
98
98
  */
99
99
  std::string decodeIDs(const std::vector<int>& ids) const {
100
100
  std::string out;
package/build.sh CHANGED
@@ -4,17 +4,17 @@
4
4
  # @license AGPL-3.0
5
5
  # @author Teffen Ellis, et al.
6
6
  #
7
- # Build the committed `sentencepiece.mjs` artifact: google/sentencepiece at the PINNED tag below,
8
- # compiled to WASM with emscripten and linked against binding.cpp (the embind wrapper exposing
7
+ # Build the committed `sentencepiece.mjs` artifact: google/sentencepiece at the pinned tag below,
8
+ # compiled to wasm with emscripten and linked against binding.cpp (the embind wrapper exposing
9
9
  # native per-piece byte offsets — task #26).
10
10
  #
11
- # The artifact is COMMITTED (like core/data's reference blobs): consumers import
11
+ # The artifact is committed (like core/data's reference blobs): consumers import
12
12
  # `@mailwoman/sentencepiece-wasm` as a plain dependency and never need emscripten. Re-run this
13
13
  # script only to bump the sentencepiece tag or change the binding; commit the regenerated .mjs in
14
14
  # the same PR and note the tag bump in the header of index.d.ts.
15
15
  #
16
16
  # Prereqs: emsdk activated (`source ~/tools/emsdk/emsdk_env.sh`), cmake, git. The sentencepiece
17
- # checkout is cached OUTSIDE the repo (a toolchain input, not a repo artifact).
17
+ # checkout is cached outside the repo (a toolchain input, not a repo artifact).
18
18
  #
19
19
  # Single-file output on purpose: -sSINGLE_FILE=1 embeds the wasm as base64 in the .mjs, matching
20
20
  # how the previous runtime (@sctg/sentencepiece-js) shipped — no separate .wasm delivery path to
@@ -23,7 +23,7 @@
23
23
 
24
24
  set -euo pipefail
25
25
 
26
- # The pin. corpus-python trains with `sentencepiece>=0.2.2` — the runtime MUST tokenize
26
+ # The pin. corpus-python trains with `sentencepiece>=0.2.2` — the runtime must tokenize
27
27
  # byte-identically to the trainer, so the tags travel together (see project-tokenizer-mismatch).
28
28
  SP_TAG="v0.2.2"
29
29
 
@@ -43,8 +43,8 @@ fi
43
43
  git -C "$SP_SRC" fetch --tags
44
44
  git -C "$SP_SRC" checkout "$SP_TAG"
45
45
 
46
- # Static library build. SPM_ENABLE_SHARED=OFF is required under emscripten; the TCMalloc probe is
47
- # meaningless in WASM.
46
+ # Static library build. SPM_ENABLE_SHARED=off is required under emscripten; the TCMalloc probe is
47
+ # meaningless in wasm.
48
48
  emcmake cmake -S "$SP_SRC" -B "$BUILD_DIR" \
49
49
  -DCMAKE_BUILD_TYPE=Release \
50
50
  -DSPM_ENABLE_SHARED=OFF \
@@ -59,13 +59,13 @@ cmake --build "$BUILD_DIR" -j "$(nproc)" > /dev/null
59
59
  ABSL_LIBS=$(find "$BUILD_DIR/third_party/abseil-cpp" -name "*.a" | sort)
60
60
 
61
61
  # Link the binding. Notes on the flags:
62
- # - MODULARIZE + EXPORT_ES6: the artifact default-exports an async factory (createSentencePiece).
62
+ # - modularize + EXPORT_ES6: the artifact default-exports an async factory (createSentencePiece).
63
63
  # - SINGLE_FILE: wasm embedded as base64 (see header).
64
- # - ENVIRONMENT=web,webview,worker,node: every consumer surface (browser demo, workers, Node CLI).
64
+ # - environment=web,webview,worker,node: every consumer surface (browser demo, workers, Node CLI).
65
65
  # - ALLOW_MEMORY_GROWTH: tokenizer.model is ~1.6MB and encode allocations scale with input length.
66
66
  # - DISABLE_EXCEPTION_CATCHING=0: protobuf-lite + sentencepiece throw in cold error paths; without
67
- # catching enabled a bad model file ABORTS the runtime instead of returning a status.
68
- # - FILESYSTEM=0: only LoadFromSerializedProto is bound — no FS use anywhere.
67
+ # catching enabled a bad model file aborts the runtime instead of returning a status.
68
+ # - filesystem=0: only LoadFromSerializedProto is bound — no FS use anywhere.
69
69
  emcc "$SCRIPT_DIR/binding.cpp" \
70
70
  -I "$SP_SRC/src" -I "$SP_SRC" -I "$SP_SRC/third_party/abseil-cpp" -I "$SP_SRC/third_party/protobuf-lite" -I "$SP_SRC/src/builtin_pb" -I "$BUILD_DIR" \
71
71
  "$BUILD_DIR/src/libsentencepiece.a" \
package/index.d.ts CHANGED
@@ -3,15 +3,19 @@
3
3
  * @license AGPL-3.0
4
4
  * @author Teffen Ellis, et al.
5
5
  *
6
- * Type surface of the committed `sentencepiece.mjs` artifact — google/sentencepiece **v0.2.2**
7
- * compiled to WASM with the native-offsets embind wrapper (`binding.cpp`, task #26). Rebuilt only
8
- * by `build.sh`; bump the tag there and note it here.
6
+ * Type surface of the committed `sentencepiece.mjs` artifact, google/sentencepiece **v0.2.2**
7
+ * compiled to wasm with the native-offsets embind wrapper (`binding.cpp`). Rebuilt only by
8
+ * `build.sh`. Bump the tag there and note it here.
9
9
  */
10
10
 
11
11
  /**
12
- * One encode result. `begins`/`ends` are UTF-8 BYTE offsets into the encoded input with the upstream invariant
13
- * `utf8(text).slice(begins[i], ends[i])` = the piece's surface, and contiguity between consecutive pieces. The TS
14
- * tokenizer layer owns byte→UTF-16 conversion.
12
+ * One encode result.
13
+ *
14
+ * `begins` and `ends` are UTF-8 byte offsets into the encoded input.
15
+ * The upstream invariant is `utf8(text).slice(begins[i], ends[i])` equals the piece's surface.
16
+ *
17
+ * Consecutive pieces also occupy contiguous byte ranges.
18
+ * The TS tokenizer layer owns byte→UTF-16 conversion.
15
19
  */
16
20
  export interface EncodeWithOffsetsResult {
17
21
  pieces: string[]
@@ -19,33 +23,43 @@ export interface EncodeWithOffsetsResult {
19
23
  begins: number[]
20
24
  ends: number[]
21
25
  /**
22
- * Present INSTEAD of the arrays when encoding failed (errors cross the boundary as values).
26
+ * Present instead of the arrays when encoding failed (errors cross the boundary as values).
23
27
  */
24
28
  error?: string
25
29
  }
26
30
 
27
31
  /**
28
- * The bound processor. Construct via the module factory, then `loadFromSerializedProto` once.
32
+ * The bound processor.
33
+ *
34
+ * Construct via the module factory, then `loadFromSerializedProto` once.
29
35
  */
30
36
  export declare class SentencePieceProcessor {
31
37
  constructor()
32
38
  /**
33
- * Load a `tokenizer.model` from its serialized-proto bytes. Takes a `Uint8Array` — the binding deliberately does NOT
34
- * accept a string (embind marshals JS strings to `std::string` as UTF-8, which corrupts arbitrary binary). Returns
35
- * `""` on success, the sentencepiece status message on failure.
39
+ * Load a `tokenizer.model` from its serialized-proto bytes.
40
+ *
41
+ * Takes serialized model bytes as a `Uint8Array`.
42
+ * Embind marshals JS strings to `std::string` as UTF-8, corrupting arbitrary binary bytes.
43
+ *
44
+ * Returns `""` on success, the sentencepiece status message on failure.
36
45
  */
37
46
  loadFromSerializedProto(serialized: Uint8Array): string
38
47
  encodeWithOffsets(text: string): EncodeWithOffsetsResult
39
48
  decodeIDs(ids: IntVector): string
40
49
  /**
41
- * Embind object lifetime: the processor owns WASM-heap memory — call when done (long-lived singletons in practice
42
- * never do).
50
+ * Embind object lifetime.
51
+ *
52
+ * The processor owns wasm-heap memory, so call `delete()` when done.
53
+ * Long-lived singletons in practice never do.
43
54
  */
44
55
  delete(): void
45
56
  }
46
57
 
47
58
  /**
48
- * Embind-registered `std::vector<int>` — build with `module.IntVector`, `push_back` ids, and `delete()` after use.
59
+ * Embind-registered `std::vector<int>`.
60
+ *
61
+ * Build with `module.IntVector` and add ids with `push_back`.
62
+ * Call `delete()` after use.
49
63
  */
50
64
  export declare class IntVector {
51
65
  constructor()
@@ -61,7 +75,8 @@ export interface SentencePieceModule {
61
75
  }
62
76
 
63
77
  /**
64
- * The emscripten MODULARIZE factory — resolves once the embedded WASM is instantiated.
78
+ * The emscripten modularize factory.
79
+ * It resolves once the embedded wasm is instantiated.
65
80
  */
66
81
  declare function createSentencePiece(): Promise<SentencePieceModule>
67
82
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mailwoman/sentencepiece-wasm",
3
- "version": "10.0.0",
3
+ "version": "10.2.0",
4
4
  "description": "google/sentencepiece v0.2.2 compiled to WASM with a native-offsets binding — the tokenizer core for @mailwoman/neural. Single-file ESM (wasm embedded), no runtime deps; per-piece byte offsets come from SentencePiece itself instead of TS-side reconstruction.",
5
5
  "license": "AGPL-3.0-only OR LicenseRef-Commercial",
6
6
  "repository": {
@@ -16,13 +16,6 @@
16
16
  "!test/**"
17
17
  ],
18
18
  "type": "module",
19
- "imports": {
20
- "#*": {
21
- "types": "./out/*.d.ts",
22
- "node": "./out/*.js",
23
- "default": "./out/*.js"
24
- }
25
- },
26
19
  "exports": {
27
20
  "./package.json": "./package.json",
28
21
  ".": {
@@ -37,13 +30,6 @@
37
30
  "types": "./index.d.ts",
38
31
  "default": "./sentencepiece.mjs"
39
32
  }
40
- },
41
- "imports": {
42
- "#*": {
43
- "types": "./out/*.d.ts",
44
- "node": "./out/*.js",
45
- "default": "./out/*.js"
46
- }
47
33
  }
48
34
  }
49
35
  }