@litert-lm/core 0.12.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 (82) hide show
  1. package/README.md +145 -0
  2. package/dist/cleanup.d.ts +25 -0
  3. package/dist/cleanup.d.ts.map +1 -0
  4. package/dist/cleanup.js +36 -0
  5. package/dist/cleanup.js.map +1 -0
  6. package/dist/conversation.d.ts +43 -0
  7. package/dist/conversation.d.ts.map +1 -0
  8. package/dist/conversation.js +153 -0
  9. package/dist/conversation.js.map +1 -0
  10. package/dist/conversation_config.d.ts +107 -0
  11. package/dist/conversation_config.d.ts.map +1 -0
  12. package/dist/conversation_config.js +17 -0
  13. package/dist/conversation_config.js.map +1 -0
  14. package/dist/engine.d.ts +37 -0
  15. package/dist/engine.d.ts.map +1 -0
  16. package/dist/engine.js +200 -0
  17. package/dist/engine.js.map +1 -0
  18. package/dist/engine_settings.d.ts +56 -0
  19. package/dist/engine_settings.d.ts.map +1 -0
  20. package/dist/engine_settings.js +110 -0
  21. package/dist/engine_settings.js.map +1 -0
  22. package/dist/global_litertlm.d.ts +66 -0
  23. package/dist/global_litertlm.d.ts.map +1 -0
  24. package/dist/global_litertlm.js +85 -0
  25. package/dist/global_litertlm.js.map +1 -0
  26. package/dist/index.d.ts +27 -0
  27. package/dist/index.d.ts.map +1 -0
  28. package/dist/index.js +27 -0
  29. package/dist/index.js.map +1 -0
  30. package/dist/litertlm_web.d.ts +32 -0
  31. package/dist/litertlm_web.d.ts.map +1 -0
  32. package/dist/litertlm_web.js +75 -0
  33. package/dist/litertlm_web.js.map +1 -0
  34. package/dist/load.d.ts +28 -0
  35. package/dist/load.d.ts.map +1 -0
  36. package/dist/load.js +43 -0
  37. package/dist/load.js.map +1 -0
  38. package/dist/load_litertlm.d.ts +58 -0
  39. package/dist/load_litertlm.d.ts.map +1 -0
  40. package/dist/load_litertlm.js +75 -0
  41. package/dist/load_litertlm.js.map +1 -0
  42. package/dist/mutex.d.ts +45 -0
  43. package/dist/mutex.d.ts.map +1 -0
  44. package/dist/mutex.js +62 -0
  45. package/dist/mutex.js.map +1 -0
  46. package/dist/readable_stream_data_stream_wrapper.d.ts +44 -0
  47. package/dist/readable_stream_data_stream_wrapper.d.ts.map +1 -0
  48. package/dist/readable_stream_data_stream_wrapper.js +315 -0
  49. package/dist/readable_stream_data_stream_wrapper.js.map +1 -0
  50. package/dist/session.d.ts +46 -0
  51. package/dist/session.d.ts.map +1 -0
  52. package/dist/session.js +88 -0
  53. package/dist/session.js.map +1 -0
  54. package/dist/session_config.d.ts +55 -0
  55. package/dist/session_config.d.ts.map +1 -0
  56. package/dist/session_config.js +109 -0
  57. package/dist/session_config.js.map +1 -0
  58. package/dist/types.d.ts +42 -0
  59. package/dist/types.d.ts.map +1 -0
  60. package/dist/types.js +17 -0
  61. package/dist/types.js.map +1 -0
  62. package/dist/url_path_utils.d.ts +28 -0
  63. package/dist/url_path_utils.d.ts.map +1 -0
  64. package/dist/url_path_utils.js +34 -0
  65. package/dist/url_path_utils.js.map +1 -0
  66. package/dist/wasm_binding_types.d.ts +365 -0
  67. package/dist/wasm_binding_types.d.ts.map +1 -0
  68. package/dist/wasm_binding_types.js +39 -0
  69. package/dist/wasm_binding_types.js.map +1 -0
  70. package/dist/wasm_feature_detect.d.ts +42 -0
  71. package/dist/wasm_feature_detect.d.ts.map +1 -0
  72. package/dist/wasm_feature_detect.js +117 -0
  73. package/dist/wasm_feature_detect.js.map +1 -0
  74. package/dist/wasm_utils.d.ts +30 -0
  75. package/dist/wasm_utils.d.ts.map +1 -0
  76. package/dist/wasm_utils.js +41 -0
  77. package/dist/wasm_utils.js.map +1 -0
  78. package/package.json +46 -0
  79. package/wasm/litertlm_wasm_compat_internal.js +8400 -0
  80. package/wasm/litertlm_wasm_compat_internal.wasm +0 -0
  81. package/wasm/litertlm_wasm_internal.js +8389 -0
  82. package/wasm/litertlm_wasm_internal.wasm +0 -0
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Copyright 2026 Google LLC
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ // Placeholder for internal dependency on trusted resource url
17
+ import { getGlobalLiteRtLm } from './global_litertlm';
18
+ /**
19
+ * Set up the default WebGPU device for the LiteRT LM Wasm module.
20
+ */
21
+ export async function setupDefaultWebGpuDevice() {
22
+ await getGlobalLiteRtLm().setupDefaultWebGpuDevice();
23
+ }
24
+ /**
25
+ * Run LiteRt LM models in the browser.
26
+ */
27
+ export class LiteRtLm {
28
+ liteRtLmWasm;
29
+ static DEFAULT_WASM_PATH = 'https://cdn.jsdelivr.net/npm/@litert-lm/core@0.12.0/wasm';
30
+ constructor(wasmModule) {
31
+ this.liteRtLmWasm = wasmModule;
32
+ this.liteRtLmWasm.setupLogging();
33
+ }
34
+ async setupDefaultWebGpuDevice() {
35
+ if (this.liteRtLmWasm.preinitializedWebGPUDevice) {
36
+ return;
37
+ }
38
+ this.liteRtLmWasm.preinitializedWebGPUDevice =
39
+ await createDefaultWebGpuDevice();
40
+ }
41
+ delete() {
42
+ // no op for now.
43
+ }
44
+ }
45
+ // TODO: b/475939320 - Use a shared implementation with LiteRT.
46
+ const DESIRED_WEBGPU_FEATURES = [
47
+ 'shader-f16',
48
+ 'subgroups',
49
+ ];
50
+ async function createDefaultWebGpuDevice() {
51
+ const adapterDescriptor = {
52
+ powerPreference: 'high-performance',
53
+ };
54
+ const adapter = await navigator.gpu.requestAdapter(adapterDescriptor);
55
+ if (!adapter) {
56
+ throw new Error('No GPU adapter found.');
57
+ }
58
+ const requiredLimits = {
59
+ maxBufferSize: adapter.limits.maxBufferSize,
60
+ maxStorageBufferBindingSize: adapter.limits.maxStorageBufferBindingSize,
61
+ maxStorageBuffersPerShaderStage: adapter.limits.maxStorageBuffersPerShaderStage,
62
+ maxTextureDimension2D: adapter.limits.maxTextureDimension2D,
63
+ };
64
+ const requiredFeatures = [];
65
+ for (const feature of DESIRED_WEBGPU_FEATURES) {
66
+ if (adapter.features.has(feature)) {
67
+ requiredFeatures.push(feature);
68
+ }
69
+ }
70
+ return await adapter.requestDevice({
71
+ requiredFeatures,
72
+ requiredLimits,
73
+ });
74
+ }
75
+ //# sourceMappingURL=litertlm_web.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"litertlm_web.js","sourceRoot":"","sources":["../src/litertlm_web.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,8DAA8D;AAE9D,OAAO,EAAC,iBAAiB,EAAC,MAAM,mBAAmB,CAAC;AAGpD;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,wBAAwB;IAC5C,MAAM,iBAAiB,EAAE,CAAC,wBAAwB,EAAE,CAAC;AACvD,CAAC;AAED;;GAEG;AACH,MAAM,OAAO,QAAQ;IACV,YAAY,CAAe;IACpC,MAAM,CAAC,iBAAiB,GAAG,0DAA0D,CAAC;IAEtF,YAAY,UAAsB;QAChC,IAAI,CAAC,YAAY,GAAG,UAA0B,CAAC;QAC/C,IAAI,CAAC,YAAY,CAAC,YAAY,EAAE,CAAC;IACnC,CAAC;IAED,KAAK,CAAC,wBAAwB;QAC5B,IAAI,IAAI,CAAC,YAAY,CAAC,0BAA0B,EAAE,CAAC;YACjD,OAAO;QACT,CAAC;QACD,IAAI,CAAC,YAAY,CAAC,0BAA0B;YACxC,MAAM,yBAAyB,EAAE,CAAC;IACxC,CAAC;IAED,MAAM;QACJ,iBAAiB;IACnB,CAAC;;AAGH,+DAA+D;AAC/D,MAAM,uBAAuB,GAAG;IAC9B,YAAY;IACZ,WAA6B;CACH,CAAC;AAE7B,KAAK,UAAU,yBAAyB;IACtC,MAAM,iBAAiB,GAA6B;QAClD,eAAe,EAAE,kBAAkB;KACpC,CAAC;IACF,MAAM,OAAO,GAAG,MAAM,SAAS,CAAC,GAAG,CAAC,cAAc,CAAC,iBAAiB,CAAC,CAAC;IACtE,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAC;IAC3C,CAAC;IAED,MAAM,cAAc,GAAG;QACrB,aAAa,EAAE,OAAO,CAAC,MAAM,CAAC,aAAa;QAC3C,2BAA2B,EAAE,OAAO,CAAC,MAAM,CAAC,2BAA2B;QACvE,+BAA+B,EAC3B,OAAO,CAAC,MAAM,CAAC,+BAA+B;QAClD,qBAAqB,EAAE,OAAO,CAAC,MAAM,CAAC,qBAAqB;KAC5D,CAAC;IAEF,MAAM,gBAAgB,GAAqB,EAAE,CAAC;IAC9C,KAAK,MAAM,OAAO,IAAI,uBAAuB,EAAE,CAAC;QAC9C,IAAI,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC;YAClC,gBAAgB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACjC,CAAC;IACH,CAAC;IAED,OAAO,MAAM,OAAO,CAAC,aAAa,CAAC;QACjC,gBAAgB;QAChB,cAAc;KACf,CAAC,CAAC;AACL,CAAC"}
package/dist/load.d.ts ADDED
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Copyright 2025 Google LLC
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import { LiteRtLm } from './litertlm_web';
17
+ import { UrlPath } from './url_path_utils';
18
+ /**
19
+ * Options for loading LiteRT-LM's Wasm module.
20
+ **/
21
+ export interface LoadOptions {
22
+ }
23
+ /**
24
+ * Load the LiteRtLm library with WASM from the given URL. Does not set the
25
+ * global LiteRtLm instance.
26
+ */
27
+ export declare function load(path: UrlPath, options?: LoadOptions): Promise<LiteRtLm>;
28
+ //# sourceMappingURL=load.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"load.d.ts","sourceRoot":"","sources":["../src/load.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAIH,OAAO,EAAC,QAAQ,EAAC,MAAM,gBAAgB,CAAC;AACxC,OAAO,EAAkC,OAAO,EAAC,MAAM,kBAAkB,CAAC;AAM1E;;IAEI;AACJ,MAAM,WAAW,WAAW;CAAG;AAE/B;;;GAGG;AACH,wBAAsB,IAAI,CACtB,IAAI,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,QAAQ,CAAC,CAkB3D"}
package/dist/load.js ADDED
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Copyright 2025 Google LLC
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import { createWasmLib } from '@litertjs/wasm-utils';
17
+ import { LiteRtLm } from './litertlm_web';
18
+ import { appendPathSegment, pathToString } from './url_path_utils';
19
+ import { supportsFeature } from './wasm_feature_detect';
20
+ const WASM_JS_FILE_NAME = 'litertlm_wasm_internal.js';
21
+ const WASM_JS_COMPAT_FILE_NAME = 'litertlm_wasm_compat_internal.js';
22
+ /**
23
+ * Load the LiteRtLm library with WASM from the given URL. Does not set the
24
+ * global LiteRtLm instance.
25
+ */
26
+ export async function load(path, options) {
27
+ const pathString = pathToString(path);
28
+ const relaxedSimd = await supportsFeature('relaxedSimd');
29
+ let fileName = WASM_JS_COMPAT_FILE_NAME;
30
+ if (relaxedSimd) {
31
+ fileName = WASM_JS_FILE_NAME;
32
+ }
33
+ let jsFilePath = path;
34
+ if (pathString.endsWith('.wasm')) {
35
+ throw new Error('Please load the `.js` file corresponding to the `.wasm` file, or ' +
36
+ 'load the directory containing it.');
37
+ }
38
+ else if (!pathString.endsWith('.js')) {
39
+ jsFilePath = appendPathSegment(path, fileName);
40
+ }
41
+ return createWasmLib(LiteRtLm, jsFilePath);
42
+ }
43
+ //# sourceMappingURL=load.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"load.js","sourceRoot":"","sources":["../src/load.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAC,aAAa,EAAC,MAAM,sBAAsB,CAAC;AAEnD,OAAO,EAAC,QAAQ,EAAC,MAAM,gBAAgB,CAAC;AACxC,OAAO,EAAC,iBAAiB,EAAE,YAAY,EAAU,MAAM,kBAAkB,CAAC;AAC1E,OAAO,EAAC,eAAe,EAAC,MAAM,uBAAuB,CAAC;AAEtD,MAAM,iBAAiB,GAAG,2BAA2B,CAAC;AACtD,MAAM,wBAAwB,GAAG,kCAAkC,CAAC;AAOpE;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,IAAI,CACtB,IAAa,EAAE,OAAqB;IACtC,MAAM,UAAU,GAAG,YAAY,CAAC,IAAI,CAAC,CAAC;IACtC,MAAM,WAAW,GAAG,MAAM,eAAe,CAAC,aAAa,CAAC,CAAC;IACzD,IAAI,QAAQ,GAAG,wBAAwB,CAAC;IACxC,IAAI,WAAW,EAAE,CAAC;QAChB,QAAQ,GAAG,iBAAiB,CAAC;IAC/B,CAAC;IAED,IAAI,UAAU,GAAG,IAAI,CAAC;IACtB,IAAI,UAAU,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;QACjC,MAAM,IAAI,KAAK,CACX,mEAAmE;YACnE,mCAAmC,CAAC,CAAC;IAC3C,CAAC;SAAM,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;QACvC,UAAU,GAAG,iBAAiB,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;IACjD,CAAC;IAED,OAAO,aAAa,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC;AAC7C,CAAC"}
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Copyright 2025 Google LLC
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import { LiteRtLm } from './litertlm_web';
17
+ import { LoadOptions } from './load';
18
+ type UrlString = string;
19
+ /**
20
+ * Options for loading LiteRT-LM.
21
+ *
22
+ * @property threads Whether to load the threaded version of the Wasm module.
23
+ * Defaults to false. Unused when specifying a .js file directly instead of
24
+ * a directory containing the Wasm files.
25
+ **/
26
+ export interface LoadLiteRtLmOptions extends LoadOptions {
27
+ }
28
+ /**
29
+ * Load LiteRT-LM Wasm files from the given URL. This needs to be called before
30
+ * calling any other LiteRT-LM functions.
31
+ *
32
+ * The URL can be:
33
+ *
34
+ * - A directory containing the LiteRT Wasm files (e.g. `.../wasm/`), or
35
+ * - The LiteRT-LM Wasm's js file (e.g. `.../litertlm_wasm_internal.js`)
36
+ *
37
+ * If the URL is to a directory, LiteRT-LM will detect what WASM features are
38
+ * available in the browser and load the compatible WASM file. If the URL is
39
+ * to a file, it will be loaded as is.
40
+ *
41
+ * @param path The path to the directory containing the LiteRT-LM Wasm files, or
42
+ * the full URL of the LiteRT-LM Wasm .js file.
43
+ */
44
+ export declare function loadLiteRtLm(path: UrlString, options?: LoadLiteRtLmOptions): Promise<LiteRtLm>;
45
+ /**
46
+ * Unload the LiteRT-LM WASM module.
47
+ *
48
+ * This deletes the global LiteRT-LM instance and invalidate any models
49
+ * associated with it. You will need to call loadLiteRtLm() again to reload the
50
+ * module.
51
+ */
52
+ export declare function unloadLiteRtLm(): void;
53
+ /**
54
+ * Get the global LiteRT-LM instance, or load it if it hasn't been loaded yet.
55
+ */
56
+ export declare function getOrLoadGlobalLiteRtLm(): Promise<LiteRtLm>;
57
+ export {};
58
+ //# sourceMappingURL=load_litertlm.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"load_litertlm.d.ts","sourceRoot":"","sources":["../src/load_litertlm.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAKH,OAAO,EAAC,QAAQ,EAAC,MAAM,gBAAgB,CAAC;AACxC,OAAO,EAAO,WAAW,EAAC,MAAM,QAAQ,CAAC;AAEzC,KAAK,SAAS,GAAG,MAAM,CAAC;AAExB;;;;;;IAMI;AACJ,MAAM,WAAW,mBAAoB,SAAQ,WAAW;CAAG;AAE3D;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,YAAY,CACxB,IAAI,EAAE,SAAS,EAAE,OAAO,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,QAAQ,CAAC,CAcrE;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,IAAI,IAAI,CAYrC;AAED;;GAEG;AACH,wBAAgB,uBAAuB,IAAI,OAAO,CAAC,QAAQ,CAAC,CAE3D"}
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Copyright 2025 Google LLC
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ // Placeholder for internal dependency on trusted resource url type
17
+ import { getGlobalLiteRtLm, getGlobalLiteRtLmPromise, hasGlobalLiteRtLm, hasGlobalLiteRtLmPromise, setGlobalLiteRtLm, setGlobalLiteRtLmPromise } from './global_litertlm';
18
+ import { LiteRtLm } from './litertlm_web';
19
+ import { load } from './load';
20
+ /**
21
+ * Load LiteRT-LM Wasm files from the given URL. This needs to be called before
22
+ * calling any other LiteRT-LM functions.
23
+ *
24
+ * The URL can be:
25
+ *
26
+ * - A directory containing the LiteRT Wasm files (e.g. `.../wasm/`), or
27
+ * - The LiteRT-LM Wasm's js file (e.g. `.../litertlm_wasm_internal.js`)
28
+ *
29
+ * If the URL is to a directory, LiteRT-LM will detect what WASM features are
30
+ * available in the browser and load the compatible WASM file. If the URL is
31
+ * to a file, it will be loaded as is.
32
+ *
33
+ * @param path The path to the directory containing the LiteRT-LM Wasm files, or
34
+ * the full URL of the LiteRT-LM Wasm .js file.
35
+ */
36
+ export function loadLiteRtLm(path, options) {
37
+ if (hasGlobalLiteRtLmPromise()) {
38
+ throw new Error('LiteRT-LM is already loading / loaded.');
39
+ }
40
+ setGlobalLiteRtLmPromise(load(path, options)
41
+ .then(async (liteRtLm) => {
42
+ setGlobalLiteRtLm(liteRtLm);
43
+ return liteRtLm;
44
+ })
45
+ .catch(error => {
46
+ setGlobalLiteRtLmPromise(undefined);
47
+ throw error;
48
+ }));
49
+ return getGlobalLiteRtLmPromise();
50
+ }
51
+ /**
52
+ * Unload the LiteRT-LM WASM module.
53
+ *
54
+ * This deletes the global LiteRT-LM instance and invalidate any models
55
+ * associated with it. You will need to call loadLiteRtLm() again to reload the
56
+ * module.
57
+ */
58
+ export function unloadLiteRtLm() {
59
+ if (hasGlobalLiteRtLmPromise() && !hasGlobalLiteRtLm()) {
60
+ throw new Error('LiteRT-LM is loading and can not be unloaded or canceled ' +
61
+ 'until it is finished loading.');
62
+ }
63
+ if (hasGlobalLiteRtLm()) {
64
+ getGlobalLiteRtLm().delete();
65
+ setGlobalLiteRtLm(undefined);
66
+ }
67
+ setGlobalLiteRtLmPromise(undefined);
68
+ }
69
+ /**
70
+ * Get the global LiteRT-LM instance, or load it if it hasn't been loaded yet.
71
+ */
72
+ export function getOrLoadGlobalLiteRtLm() {
73
+ return getGlobalLiteRtLmPromise() ?? loadLiteRtLm(LiteRtLm.DEFAULT_WASM_PATH);
74
+ }
75
+ //# sourceMappingURL=load_litertlm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"load_litertlm.js","sourceRoot":"","sources":["../src/load_litertlm.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,mEAAmE;AAEnE,OAAO,EAAC,iBAAiB,EAAE,wBAAwB,EAAE,iBAAiB,EAAE,wBAAwB,EAAE,iBAAiB,EAAE,wBAAwB,EAAC,MAAM,mBAAmB,CAAC;AACxK,OAAO,EAAC,QAAQ,EAAC,MAAM,gBAAgB,CAAC;AACxC,OAAO,EAAC,IAAI,EAAc,MAAM,QAAQ,CAAC;AAazC;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,YAAY,CACxB,IAAe,EAAE,OAA6B;IAChD,IAAI,wBAAwB,EAAE,EAAE,CAAC;QAC/B,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;IAC5D,CAAC;IACD,wBAAwB,CAAC,IAAI,CAAC,IAAI,EAAE,OAAO,CAAC;SACd,IAAI,CAAC,KAAK,EAAC,QAAQ,EAAC,EAAE;QACrB,iBAAiB,CAAC,QAAQ,CAAC,CAAC;QAC5B,OAAO,QAAQ,CAAC;IAClB,CAAC,CAAC;SACD,KAAK,CAAC,KAAK,CAAC,EAAE;QACb,wBAAwB,CAAC,SAAS,CAAC,CAAC;QACpC,MAAM,KAAK,CAAC;IACd,CAAC,CAAC,CAAC,CAAC;IACjC,OAAO,wBAAwB,EAAG,CAAC;AACrC,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,cAAc;IAC5B,IAAI,wBAAwB,EAAE,IAAI,CAAC,iBAAiB,EAAE,EAAE,CAAC;QACvD,MAAM,IAAI,KAAK,CACX,2DAA2D;YAC3D,+BAA+B,CAAC,CAAC;IACvC,CAAC;IAED,IAAI,iBAAiB,EAAE,EAAE,CAAC;QACxB,iBAAiB,EAAE,CAAC,MAAM,EAAE,CAAC;QAC7B,iBAAiB,CAAC,SAAS,CAAC,CAAC;IAC/B,CAAC;IACD,wBAAwB,CAAC,SAAS,CAAC,CAAC;AACtC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,uBAAuB;IACrC,OAAO,wBAAwB,EAAE,IAAI,YAAY,CAAC,QAAQ,CAAC,iBAAiB,CAAC,CAAC;AAChF,CAAC"}
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Copyright 2026 The ODML Authors.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /**
17
+ * A mutual exclusion lock for managing asynchronous operations.
18
+ *
19
+ * In an Emscripten WebAssembly environment using Asyncify or JSPI, C++
20
+ * execution can be paused during asynchronous operations (such as waiting for
21
+ * WebGPU execution). While Wasm is paused, the JS event loop continues running.
22
+ * If JS invokes another Wasm binding function that attempts to acquire the same
23
+ * C++ mutex held by the paused stack frame, single-threaded C++ cannot yield
24
+ * and triggers an unrecoverable deadlock.
25
+ *
26
+ * This class prevents re-entrancy deadlocks by serializing all entry points
27
+ * from JavaScript into Wasm that interact with underlying C++ mutexes. Even
28
+ * seemingly synchronous tasks (like creating or deleting a session) must
29
+ * asynchronously wait for this mutex to ensure any paused C++ tasks have fully
30
+ * completed and released their locks.
31
+ */
32
+ export declare class Mutex {
33
+ private mutexPromise;
34
+ private queueCount;
35
+ /**
36
+ * Whether the mutex is currently locked.
37
+ */
38
+ get isLocked(): boolean;
39
+ /**
40
+ * Executes the provided function once the mutex is acquired, and releases
41
+ * the mutex when the function completes.
42
+ */
43
+ acquireAndRun<T>(fn: () => Promise<T> | T): Promise<T>;
44
+ }
45
+ //# sourceMappingURL=mutex.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mutex.d.ts","sourceRoot":"","sources":["../src/mutex.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,KAAK;IAChB,OAAO,CAAC,YAAY,CAAqB;IACzC,OAAO,CAAC,UAAU,CAAK;IAEvB;;OAEG;IACH,IAAI,QAAQ,IAAI,OAAO,CAEtB;IAED;;;OAGG;IACG,aAAa,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAE,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;CAe5D"}
package/dist/mutex.js ADDED
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Copyright 2026 The ODML Authors.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /**
17
+ * A mutual exclusion lock for managing asynchronous operations.
18
+ *
19
+ * In an Emscripten WebAssembly environment using Asyncify or JSPI, C++
20
+ * execution can be paused during asynchronous operations (such as waiting for
21
+ * WebGPU execution). While Wasm is paused, the JS event loop continues running.
22
+ * If JS invokes another Wasm binding function that attempts to acquire the same
23
+ * C++ mutex held by the paused stack frame, single-threaded C++ cannot yield
24
+ * and triggers an unrecoverable deadlock.
25
+ *
26
+ * This class prevents re-entrancy deadlocks by serializing all entry points
27
+ * from JavaScript into Wasm that interact with underlying C++ mutexes. Even
28
+ * seemingly synchronous tasks (like creating or deleting a session) must
29
+ * asynchronously wait for this mutex to ensure any paused C++ tasks have fully
30
+ * completed and released their locks.
31
+ */
32
+ export class Mutex {
33
+ mutexPromise = Promise.resolve();
34
+ queueCount = 0;
35
+ /**
36
+ * Whether the mutex is currently locked.
37
+ */
38
+ get isLocked() {
39
+ return this.queueCount > 0;
40
+ }
41
+ /**
42
+ * Executes the provided function once the mutex is acquired, and releases
43
+ * the mutex when the function completes.
44
+ */
45
+ async acquireAndRun(fn) {
46
+ this.queueCount++;
47
+ const previousPromise = this.mutexPromise;
48
+ let resolveMutex;
49
+ this.mutexPromise = new Promise((resolve) => {
50
+ resolveMutex = resolve;
51
+ });
52
+ try {
53
+ await previousPromise;
54
+ return await fn();
55
+ }
56
+ finally {
57
+ this.queueCount--;
58
+ resolveMutex();
59
+ }
60
+ }
61
+ }
62
+ //# sourceMappingURL=mutex.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mutex.js","sourceRoot":"","sources":["../src/mutex.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;;;;;;;;;;;GAeG;AACH,MAAM,OAAO,KAAK;IACR,YAAY,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;IACjC,UAAU,GAAG,CAAC,CAAC;IAEvB;;OAEG;IACH,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,UAAU,GAAG,CAAC,CAAC;IAC7B,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,aAAa,CAAI,EAAuB;QAC5C,IAAI,CAAC,UAAU,EAAE,CAAC;QAClB,MAAM,eAAe,GAAG,IAAI,CAAC,YAAY,CAAC;QAC1C,IAAI,YAAyB,CAAC;QAC9B,IAAI,CAAC,YAAY,GAAG,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE;YAChD,YAAY,GAAG,OAAO,CAAC;QACzB,CAAC,CAAC,CAAC;QACH,IAAI,CAAC;YACH,MAAM,eAAe,CAAC;YACtB,OAAO,MAAM,EAAE,EAAE,CAAC;QACpB,CAAC;gBAAS,CAAC;YACT,IAAI,CAAC,UAAU,EAAE,CAAC;YAClB,YAAY,EAAE,CAAC;QACjB,CAAC;IACH,CAAC;CACF"}
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Copyright 2026 The ODML Authors.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /**
17
+ * Wraps a ReadableStream for use in LiteRT LM C++ streamed model loading.
18
+ */
19
+ export declare class ReadableStreamDataStreamWrapper {
20
+ private readonly stream;
21
+ private readonly getWasmHeap;
22
+ readonly bytesPerSlice: number;
23
+ private slices;
24
+ private streamDone;
25
+ private globalPosition;
26
+ private reader;
27
+ private leftoverData?;
28
+ private isReading;
29
+ constructor(stream: ReadableStream<Uint8Array>, getWasmHeap: () => Uint8Array, bytesPerSlice?: number);
30
+ private getViews;
31
+ private readInternal;
32
+ private fillBufferFromStream;
33
+ private doDiscard;
34
+ readAndDiscard(destAddress: number, offset: number | bigint, count: number | bigint): Promise<{
35
+ error?: Error;
36
+ }>;
37
+ readAndPreserve(destAddress: number, offset: number | bigint, count: number | bigint): Promise<{
38
+ error?: Error;
39
+ }>;
40
+ discard(offset: number | bigint, count: number | bigint): Promise<{
41
+ error?: Error;
42
+ }>;
43
+ }
44
+ //# sourceMappingURL=readable_stream_data_stream_wrapper.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"readable_stream_data_stream_wrapper.d.ts","sourceRoot":"","sources":["../src/readable_stream_data_stream_wrapper.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAiEH;;GAEG;AACH,qBAAa,+BAA+B;IActC,OAAO,CAAC,QAAQ,CAAC,MAAM;IAEvB,OAAO,CAAC,QAAQ,CAAC,WAAW;IAC5B,QAAQ,CAAC,aAAa;IAhB1B,OAAO,CAAC,MAAM,CAAe;IAC7B,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,cAAc,CAAK;IAE3B,OAAO,CAAC,MAAM,CAIR;IACN,OAAO,CAAC,YAAY,CAAC,CAAa;IAClC,OAAO,CAAC,SAAS,CAAS;gBAGL,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,EAElC,WAAW,EAAE,MAAM,UAAU,EACrC,aAAa,SAAa;IAQvC,OAAO,CAAG,QAAQ;YAoFJ,YAAY;YAoDZ,oBAAoB;IA8ElC,OAAO,CAAC,SAAS;IAMX,cAAc,CAChB,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAC,MAAM,EAC1C,KAAK,EAAE,MAAM,GAAC,MAAM,GAAG,OAAO,CAAC;QAAC,KAAK,CAAC,EAAE,KAAK,CAAA;KAAC,CAAC;IAU7C,eAAe,CACjB,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAC,MAAM,EAC1C,KAAK,EAAE,MAAM,GAAC,MAAM,GAAG,OAAO,CAAC;QAAC,KAAK,CAAC,EAAE,KAAK,CAAA;KAAC,CAAC;IAU7C,OAAO,CAAC,MAAM,EAAE,MAAM,GAAC,MAAM,EAAE,KAAK,EAAE,MAAM,GAAC,MAAM,GACrD,OAAO,CAAC;QAAC,KAAK,CAAC,EAAE,KAAK,CAAA;KAAC,CAAC;CAQ7B"}