@litert-lm/core 0.16.0 → 0.17.1
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/README.md +164 -0
- package/dist/cleanup.d.ts +25 -0
- package/dist/cleanup.d.ts.map +1 -0
- package/dist/cleanup.js +36 -0
- package/dist/cleanup.js.map +1 -0
- package/dist/conversation.d.ts +51 -0
- package/dist/conversation.d.ts.map +1 -0
- package/dist/conversation.js +171 -0
- package/dist/conversation.js.map +1 -0
- package/dist/conversation_config.d.ts +157 -0
- package/dist/conversation_config.d.ts.map +1 -0
- package/dist/conversation_config.js +42 -0
- package/dist/conversation_config.js.map +1 -0
- package/dist/embedding_engine.d.ts +51 -0
- package/dist/embedding_engine.d.ts.map +1 -0
- package/dist/embedding_engine.js +198 -0
- package/dist/embedding_engine.js.map +1 -0
- package/dist/embedding_engine_settings.d.ts +39 -0
- package/dist/embedding_engine_settings.d.ts.map +1 -0
- package/dist/embedding_engine_settings.js +35 -0
- package/dist/embedding_engine_settings.js.map +1 -0
- package/dist/engine.d.ts +37 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +203 -0
- package/dist/engine.js.map +1 -0
- package/dist/engine_settings.d.ts +57 -0
- package/dist/engine_settings.d.ts.map +1 -0
- package/dist/engine_settings.js +114 -0
- package/dist/engine_settings.js.map +1 -0
- package/dist/global_litertlm.d.ts +66 -0
- package/dist/global_litertlm.d.ts.map +1 -0
- package/dist/global_litertlm.js +85 -0
- package/dist/global_litertlm.js.map +1 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +33 -0
- package/dist/index.js.map +1 -0
- package/dist/litertlm_web.d.ts +32 -0
- package/dist/litertlm_web.d.ts.map +1 -0
- package/dist/litertlm_web.js +75 -0
- package/dist/litertlm_web.js.map +1 -0
- package/dist/load.d.ts +28 -0
- package/dist/load.d.ts.map +1 -0
- package/dist/load.js +50 -0
- package/dist/load.js.map +1 -0
- package/dist/load_litertlm.d.ts +58 -0
- package/dist/load_litertlm.d.ts.map +1 -0
- package/dist/load_litertlm.js +87 -0
- package/dist/load_litertlm.js.map +1 -0
- package/dist/mutex.d.ts +45 -0
- package/dist/mutex.d.ts.map +1 -0
- package/dist/mutex.js +62 -0
- package/dist/mutex.js.map +1 -0
- package/dist/orchestration/auto_tool_chat.d.ts +82 -0
- package/dist/orchestration/auto_tool_chat.d.ts.map +1 -0
- package/dist/orchestration/auto_tool_chat.js +304 -0
- package/dist/orchestration/auto_tool_chat.js.map +1 -0
- package/dist/orchestration/chat_interface.d.ts +30 -0
- package/dist/orchestration/chat_interface.d.ts.map +1 -0
- package/dist/orchestration/chat_interface.js +17 -0
- package/dist/orchestration/chat_interface.js.map +1 -0
- package/dist/orchestration/webmcp_tool.d.ts +41 -0
- package/dist/orchestration/webmcp_tool.d.ts.map +1 -0
- package/dist/orchestration/webmcp_tool.js +32 -0
- package/dist/orchestration/webmcp_tool.js.map +1 -0
- package/dist/readable_stream_data_stream_wrapper.d.ts +44 -0
- package/dist/readable_stream_data_stream_wrapper.d.ts.map +1 -0
- package/dist/readable_stream_data_stream_wrapper.js +315 -0
- package/dist/readable_stream_data_stream_wrapper.js.map +1 -0
- package/dist/session.d.ts +51 -0
- package/dist/session.d.ts.map +1 -0
- package/dist/session.js +107 -0
- package/dist/session.js.map +1 -0
- package/dist/session_config.d.ts +60 -0
- package/dist/session_config.d.ts.map +1 -0
- package/dist/session_config.js +109 -0
- package/dist/session_config.js.map +1 -0
- package/dist/stream_utils.d.ts +25 -0
- package/dist/stream_utils.d.ts.map +1 -0
- package/dist/stream_utils.js +95 -0
- package/dist/stream_utils.js.map +1 -0
- package/dist/testing/fakes.d.ts +67 -0
- package/dist/testing/fakes.d.ts.map +1 -0
- package/dist/testing/fakes.js +125 -0
- package/dist/testing/fakes.js.map +1 -0
- package/dist/testing/index.d.ts +17 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +17 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/types.d.ts +42 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +17 -0
- package/dist/types.js.map +1 -0
- package/dist/url_path_utils.d.ts +28 -0
- package/dist/url_path_utils.d.ts.map +1 -0
- package/dist/url_path_utils.js +34 -0
- package/dist/url_path_utils.js.map +1 -0
- package/dist/wasm_binding_types.d.ts +473 -0
- package/dist/wasm_binding_types.d.ts.map +1 -0
- package/dist/wasm_binding_types.js +47 -0
- package/dist/wasm_binding_types.js.map +1 -0
- package/dist/wasm_feature_detect.d.ts +45 -0
- package/dist/wasm_feature_detect.d.ts.map +1 -0
- package/dist/wasm_feature_detect.js +139 -0
- package/dist/wasm_feature_detect.js.map +1 -0
- package/dist/wasm_utils.d.ts +30 -0
- package/dist/wasm_utils.d.ts.map +1 -0
- package/dist/wasm_utils.js +41 -0
- package/dist/wasm_utils.js.map +1 -0
- package/package.json +3 -2
- package/wasm/litertlm_wasm_asyncify_internal.js +8749 -0
- package/wasm/litertlm_wasm_asyncify_internal.wasm +0 -0
- package/wasm/litertlm_wasm_compat_asyncify_internal.js +8758 -0
- package/wasm/litertlm_wasm_compat_asyncify_internal.wasm +0 -0
- package/wasm/litertlm_wasm_compat_internal.js +8543 -0
- package/wasm/litertlm_wasm_compat_internal.wasm +0 -0
- package/wasm/litertlm_wasm_internal.js +8532 -0
- package/wasm/litertlm_wasm_internal.wasm +0 -0
|
@@ -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.js';
|
|
17
|
+
import { LoadOptions } from './load.js';
|
|
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(path?: UrlString): 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,mBAAmB,CAAC;AAC3C,OAAO,EAAO,WAAW,EAAC,MAAM,WAAW,CAAC;AAE5C,KAAK,SAAS,GAAG,MAAM,CAAC;AAGxB;;;;;;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,CAarC;AAED;;GAEG;AACH,wBAAgB,uBAAuB,CAAC,IAAI,CAAC,EAAE,SAAS,GAAG,OAAO,CAAC,QAAQ,CAAC,CAa3E"}
|
|
@@ -0,0 +1,87 @@
|
|
|
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.js';
|
|
18
|
+
import { LiteRtLm } from './litertlm_web.js';
|
|
19
|
+
import { load } from './load.js';
|
|
20
|
+
let globalLiteRtLmPath = undefined;
|
|
21
|
+
/**
|
|
22
|
+
* Load LiteRT-LM Wasm files from the given URL. This needs to be called before
|
|
23
|
+
* calling any other LiteRT-LM functions.
|
|
24
|
+
*
|
|
25
|
+
* The URL can be:
|
|
26
|
+
*
|
|
27
|
+
* - A directory containing the LiteRT Wasm files (e.g. `.../wasm/`), or
|
|
28
|
+
* - The LiteRT-LM Wasm's js file (e.g. `.../litertlm_wasm_internal.js`)
|
|
29
|
+
*
|
|
30
|
+
* If the URL is to a directory, LiteRT-LM will detect what WASM features are
|
|
31
|
+
* available in the browser and load the compatible WASM file. If the URL is
|
|
32
|
+
* to a file, it will be loaded as is.
|
|
33
|
+
*
|
|
34
|
+
* @param path The path to the directory containing the LiteRT-LM Wasm files, or
|
|
35
|
+
* the full URL of the LiteRT-LM Wasm .js file.
|
|
36
|
+
*/
|
|
37
|
+
export function loadLiteRtLm(path, options) {
|
|
38
|
+
if (hasGlobalLiteRtLmPromise()) {
|
|
39
|
+
throw new Error('LiteRT-LM is already loading / loaded.');
|
|
40
|
+
}
|
|
41
|
+
setGlobalLiteRtLmPromise(load(path, options)
|
|
42
|
+
.then(async (liteRtLm) => {
|
|
43
|
+
setGlobalLiteRtLm(liteRtLm);
|
|
44
|
+
return liteRtLm;
|
|
45
|
+
})
|
|
46
|
+
.catch(error => {
|
|
47
|
+
setGlobalLiteRtLmPromise(undefined);
|
|
48
|
+
throw error;
|
|
49
|
+
}));
|
|
50
|
+
return getGlobalLiteRtLmPromise();
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Unload the LiteRT-LM WASM module.
|
|
54
|
+
*
|
|
55
|
+
* This deletes the global LiteRT-LM instance and invalidate any models
|
|
56
|
+
* associated with it. You will need to call loadLiteRtLm() again to reload the
|
|
57
|
+
* module.
|
|
58
|
+
*/
|
|
59
|
+
export function unloadLiteRtLm() {
|
|
60
|
+
if (hasGlobalLiteRtLmPromise() && !hasGlobalLiteRtLm()) {
|
|
61
|
+
throw new Error('LiteRT-LM is loading and can not be unloaded or canceled ' +
|
|
62
|
+
'until it is finished loading.');
|
|
63
|
+
}
|
|
64
|
+
if (hasGlobalLiteRtLm()) {
|
|
65
|
+
getGlobalLiteRtLm().delete();
|
|
66
|
+
setGlobalLiteRtLm(undefined);
|
|
67
|
+
globalLiteRtLmPath = undefined;
|
|
68
|
+
}
|
|
69
|
+
setGlobalLiteRtLmPromise(undefined);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Get the global LiteRT-LM instance, or load it if it hasn't been loaded yet.
|
|
73
|
+
*/
|
|
74
|
+
export function getOrLoadGlobalLiteRtLm(path) {
|
|
75
|
+
const liteRtLmPromise = getGlobalLiteRtLmPromise();
|
|
76
|
+
if (liteRtLmPromise) {
|
|
77
|
+
if (path && globalLiteRtLmPath !== path) {
|
|
78
|
+
throw new Error(`LiteRT-LM is already loading / loaded with a different path ` +
|
|
79
|
+
`${globalLiteRtLmPath} but was requested to load ${path}. To ` +
|
|
80
|
+
`reload with a different path, call unloadLiteRtLm() first.`);
|
|
81
|
+
}
|
|
82
|
+
return liteRtLmPromise;
|
|
83
|
+
}
|
|
84
|
+
globalLiteRtLmPath = path ?? LiteRtLm.DEFAULT_WASM_PATH;
|
|
85
|
+
return liteRtLmPromise ?? loadLiteRtLm(globalLiteRtLmPath);
|
|
86
|
+
}
|
|
87
|
+
//# 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,sBAAsB,CAAC;AAC3K,OAAO,EAAC,QAAQ,EAAC,MAAM,mBAAmB,CAAC;AAC3C,OAAO,EAAC,IAAI,EAAc,MAAM,WAAW,CAAC;AAG5C,IAAI,kBAAkB,GAAwB,SAAS,CAAC;AAWxD;;;;;;;;;;;;;;;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;QAC7B,kBAAkB,GAAG,SAAS,CAAC;IACjC,CAAC;IACD,wBAAwB,CAAC,SAAS,CAAC,CAAC;AACtC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,uBAAuB,CAAC,IAAgB;IACtD,MAAM,eAAe,GAAG,wBAAwB,EAAE,CAAC;IACnD,IAAI,eAAe,EAAE,CAAC;QACpB,IAAI,IAAI,IAAI,kBAAkB,KAAK,IAAI,EAAE,CAAC;YACxC,MAAM,IAAI,KAAK,CACX,8DAA8D;gBAC9D,GAAG,kBAAkB,8BAA8B,IAAI,OAAO;gBAC9D,4DAA4D,CAAC,CAAC;QACpE,CAAC;QACD,OAAO,eAAe,CAAC;IACzB,CAAC;IACD,kBAAkB,GAAG,IAAI,IAAI,QAAQ,CAAC,iBAAiB,CAAC;IACxD,OAAO,eAAe,IAAI,YAAY,CAAC,kBAAkB,CAAC,CAAC;AAC7D,CAAC"}
|
package/dist/mutex.d.ts
ADDED
|
@@ -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,82 @@
|
|
|
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
|
+
import { ConversationConfig, Message, MessageLike, Tool } from '../conversation_config.js';
|
|
17
|
+
import { Engine } from '../engine.js';
|
|
18
|
+
import { JsonValue } from '../types.js';
|
|
19
|
+
import { BenchmarkInfo } from '../wasm_binding_types.js';
|
|
20
|
+
import { ChatInterface } from './chat_interface.js';
|
|
21
|
+
import { WebMcpTool } from './webmcp_tool.js';
|
|
22
|
+
type ToolExecute = (args: Record<string, JsonValue>) => JsonValue | Promise<JsonValue>;
|
|
23
|
+
/**
|
|
24
|
+
* A tool with an `execute` function implementation.
|
|
25
|
+
*/
|
|
26
|
+
export type ToolWithImplementation = Tool & {
|
|
27
|
+
execute: ToolExecute;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Event detail representing the execution progress of a single tool.
|
|
31
|
+
*/
|
|
32
|
+
export interface ToolProgressEvent {
|
|
33
|
+
id: number;
|
|
34
|
+
name: string;
|
|
35
|
+
arguments: Record<string, JsonValue>;
|
|
36
|
+
status: 'started' | 'completed' | 'error';
|
|
37
|
+
result?: unknown;
|
|
38
|
+
error?: string;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Options for configuring an AutoToolChat.
|
|
42
|
+
*/
|
|
43
|
+
export interface AutoToolChatOptions {
|
|
44
|
+
engine: Engine;
|
|
45
|
+
config?: ConversationConfig;
|
|
46
|
+
tools?: Array<ToolWithImplementation | WebMcpTool>;
|
|
47
|
+
recurringToolCallLimit?: number;
|
|
48
|
+
onToolProgress?: (event: ToolProgressEvent) => void;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Wraps a conversation and automatically executes tools for the model.
|
|
52
|
+
*
|
|
53
|
+
* Tools are called in parallel, but the model waits for all of them to
|
|
54
|
+
* finish before being woken up again.
|
|
55
|
+
*/
|
|
56
|
+
export declare class AutoToolChat implements ChatInterface {
|
|
57
|
+
private readonly options;
|
|
58
|
+
private baseConversation;
|
|
59
|
+
private isBusy;
|
|
60
|
+
private isCancelled;
|
|
61
|
+
private readonly tools;
|
|
62
|
+
private nextToolCallId;
|
|
63
|
+
private currentBatch;
|
|
64
|
+
constructor(options: AutoToolChatOptions);
|
|
65
|
+
private getConversation;
|
|
66
|
+
sendMessage(message: MessageLike | MessageLike[]): Promise<Message>;
|
|
67
|
+
sendMessageStreaming(message: MessageLike | MessageLike[]): ReadableStream<Message>;
|
|
68
|
+
/**
|
|
69
|
+
* Cancels the current generation.
|
|
70
|
+
* Note: This does not abort any in-progress tool calls natively. Tools that
|
|
71
|
+
* affect state may still complete in the background eventually, but their
|
|
72
|
+
* results will be discarded and not reported back to the model or the user.
|
|
73
|
+
*/
|
|
74
|
+
cancel(): void;
|
|
75
|
+
getHistory(): Promise<Message[]>;
|
|
76
|
+
getTokenCount(): Promise<number>;
|
|
77
|
+
getBenchmarkInfo(): Promise<BenchmarkInfo>;
|
|
78
|
+
delete(): Promise<void>;
|
|
79
|
+
private executeToolCalls;
|
|
80
|
+
}
|
|
81
|
+
export {};
|
|
82
|
+
//# sourceMappingURL=auto_tool_chat.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auto_tool_chat.d.ts","sourceRoot":"","sources":["../../src/orchestration/auto_tool_chat.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,OAAO,EAAc,kBAAkB,EAAgB,OAAO,EAAE,WAAW,EAAE,IAAI,EAAW,MAAM,2BAA2B,CAAC;AAC9H,OAAO,EAAC,MAAM,EAAC,MAAM,cAAc,CAAC;AACpC,OAAO,EAAC,SAAS,EAAC,MAAM,aAAa,CAAC;AACtC,OAAO,EAAC,aAAa,EAAC,MAAM,0BAA0B,CAAC;AAEvD,OAAO,EAAC,aAAa,EAAC,MAAM,qBAAqB,CAAC;AAClD,OAAO,EAAuB,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAMlE,KAAK,WAAW,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,KAC/C,SAAS,GAAC,OAAO,CAAC,SAAS,CAAC,CAAC;AAEjC;;GAEG;AACH,MAAM,MAAM,sBAAsB,GAAG,IAAI,GAAC;IAAC,OAAO,EAAE,WAAW,CAAA;CAAC,CAAC;AAEjE;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IACrC,MAAM,EAAE,SAAS,GAAC,WAAW,GAAC,OAAO,CAAC;IACtC,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,kBAAkB,CAAC;IAC5B,KAAK,CAAC,EAAE,KAAK,CAAC,sBAAsB,GAAC,UAAU,CAAC,CAAC;IACjD,sBAAsB,CAAC,EAAE,MAAM,CAAC;IAChC,cAAc,CAAC,EAAE,CAAC,KAAK,EAAE,iBAAiB,KAAK,IAAI,CAAC;CACrD;AAED;;;;;GAKG;AACH,qBAAa,YAAa,YAAW,aAAa;IAQpC,OAAO,CAAC,QAAQ,CAAC,OAAO;IAPpC,OAAO,CAAC,gBAAgB,CAA2B;IACnD,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,QAAQ,CAAC,KAAK,CAA6C;IACnE,OAAO,CAAC,cAAc,CAAK;IAC3B,OAAO,CAAC,YAAY,CAA4B;gBAEnB,OAAO,EAAE,mBAAmB;YA+B3C,eAAe;IAsBvB,WAAW,CAAC,OAAO,EAAE,WAAW,GAAC,WAAW,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC;IAmCvE,oBAAoB,CAAC,OAAO,EAAE,WAAW,GAAC,WAAW,EAAE,GACnD,cAAc,CAAC,OAAO,CAAC;IAmF3B;;;;;OAKG;IACH,MAAM;IAUA,UAAU,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;IAKhC,aAAa,IAAI,OAAO,CAAC,MAAM,CAAC;IAKhC,gBAAgB,IAAI,OAAO,CAAC,aAAa,CAAC;IAK1C,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;YAOf,gBAAgB;CAU/B"}
|
|
@@ -0,0 +1,304 @@
|
|
|
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
|
+
import { isWebMcpTool, toTool } from './webmcp_tool.js';
|
|
17
|
+
const BUSY_MESSAGE = 'Conversation is busy. A generation is already in progress.';
|
|
18
|
+
/**
|
|
19
|
+
* Wraps a conversation and automatically executes tools for the model.
|
|
20
|
+
*
|
|
21
|
+
* Tools are called in parallel, but the model waits for all of them to
|
|
22
|
+
* finish before being woken up again.
|
|
23
|
+
*/
|
|
24
|
+
export class AutoToolChat {
|
|
25
|
+
options;
|
|
26
|
+
baseConversation = null;
|
|
27
|
+
isBusy = false;
|
|
28
|
+
isCancelled = false;
|
|
29
|
+
tools = new Map();
|
|
30
|
+
nextToolCallId = 1;
|
|
31
|
+
currentBatch = null;
|
|
32
|
+
constructor(options) {
|
|
33
|
+
this.options = options;
|
|
34
|
+
if (options.config?.preface?.tools &&
|
|
35
|
+
options.config.preface.tools.length > 0) {
|
|
36
|
+
throw new Error('AutoToolChat handles tools itself. Do not provide tools via ConversationConfig.preface.tools.');
|
|
37
|
+
}
|
|
38
|
+
const activeTools = options.tools || [];
|
|
39
|
+
for (let t of activeTools) {
|
|
40
|
+
let tool;
|
|
41
|
+
if (isWebMcpTool(t)) {
|
|
42
|
+
tool = { ...toTool(t), execute: t.execute };
|
|
43
|
+
}
|
|
44
|
+
else if (!('function' in t)) {
|
|
45
|
+
const { execute, ...declaration } = t;
|
|
46
|
+
tool = { type: 'function', function: declaration, execute };
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
tool = t;
|
|
50
|
+
}
|
|
51
|
+
const name = tool.function.name;
|
|
52
|
+
if (!name)
|
|
53
|
+
throw new Error('Tool must have a name.');
|
|
54
|
+
if (!tool.execute) {
|
|
55
|
+
throw new Error(`Tool ${name} must have an execute function.`);
|
|
56
|
+
}
|
|
57
|
+
if (this.tools.has(name)) {
|
|
58
|
+
throw new Error(`Duplicate tool name ${name} provided.`);
|
|
59
|
+
}
|
|
60
|
+
this.tools.set(name, tool);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
async getConversation() {
|
|
64
|
+
if (!this.baseConversation) {
|
|
65
|
+
const config = this.options.config ? { ...this.options.config } : {};
|
|
66
|
+
if (this.tools.size > 0) {
|
|
67
|
+
if (!config.preface) {
|
|
68
|
+
config.preface = {};
|
|
69
|
+
}
|
|
70
|
+
// Extract the explicit tool definitions (e.g. name, description,
|
|
71
|
+
// parameters) to pass to createConversation without the execute
|
|
72
|
+
// implementation.
|
|
73
|
+
config.preface.tools = Array.from(this.tools.values()).map(t => {
|
|
74
|
+
const { execute, ...rest } = t;
|
|
75
|
+
return rest;
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
this.baseConversation =
|
|
79
|
+
await this.options.engine.createConversation(config);
|
|
80
|
+
}
|
|
81
|
+
return this.baseConversation;
|
|
82
|
+
}
|
|
83
|
+
async sendMessage(message) {
|
|
84
|
+
if (this.isBusy) {
|
|
85
|
+
throw new Error(BUSY_MESSAGE);
|
|
86
|
+
}
|
|
87
|
+
this.isBusy = true;
|
|
88
|
+
this.isCancelled = false;
|
|
89
|
+
try {
|
|
90
|
+
const conversation = await this.getConversation();
|
|
91
|
+
let currentMessage = message;
|
|
92
|
+
const recurringToolCallLimit = this.options.recurringToolCallLimit ?? 25;
|
|
93
|
+
let iteration = 0;
|
|
94
|
+
while (iteration < recurringToolCallLimit) {
|
|
95
|
+
iteration++;
|
|
96
|
+
const responseMsg = await conversation.sendMessage(currentMessage);
|
|
97
|
+
if (responseMsg.tool_calls && responseMsg.tool_calls.length > 0) {
|
|
98
|
+
const toolResponses = await this.executeToolCalls(responseMsg.tool_calls);
|
|
99
|
+
if (this.isCancelled) {
|
|
100
|
+
throw new Error('Conversation cancelled');
|
|
101
|
+
}
|
|
102
|
+
// Feed back as a tool message
|
|
103
|
+
currentMessage = { role: 'tool', content: toolResponses };
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
return responseMsg;
|
|
107
|
+
}
|
|
108
|
+
throw new Error('Tool calling exceeded the recurring limit');
|
|
109
|
+
}
|
|
110
|
+
finally {
|
|
111
|
+
this.isBusy = false;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
sendMessageStreaming(message) {
|
|
115
|
+
if (this.isBusy) {
|
|
116
|
+
throw new Error(BUSY_MESSAGE);
|
|
117
|
+
}
|
|
118
|
+
this.isBusy = true;
|
|
119
|
+
this.isCancelled = false;
|
|
120
|
+
let isStreamCancelled = false;
|
|
121
|
+
return new ReadableStream({
|
|
122
|
+
start: (controller) => {
|
|
123
|
+
let currentMessage = message;
|
|
124
|
+
let iteration = 0;
|
|
125
|
+
const recurringToolCallLimit = this.options.recurringToolCallLimit ?? 25;
|
|
126
|
+
const executeIteration = async () => {
|
|
127
|
+
if (iteration >= recurringToolCallLimit) {
|
|
128
|
+
this.isBusy = false;
|
|
129
|
+
controller.error(new Error('Tool calling exceeded the recurring limit'));
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
iteration++;
|
|
133
|
+
let toolCalls = [];
|
|
134
|
+
try {
|
|
135
|
+
const conversation = await this.getConversation();
|
|
136
|
+
const stream = conversation.sendMessageStreaming(currentMessage);
|
|
137
|
+
for await (const chunk of stream) {
|
|
138
|
+
if (isStreamCancelled || this.isCancelled) {
|
|
139
|
+
// Because we break out of the loop early, the underlying
|
|
140
|
+
// iterator automatically invokes stream.cancel()
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
if (chunk.tool_calls && chunk.tool_calls.length > 0) {
|
|
144
|
+
toolCalls = toolCalls.concat(chunk.tool_calls);
|
|
145
|
+
// Omit the tool_calls property before enqueuing to the user
|
|
146
|
+
const cleanValue = { ...chunk };
|
|
147
|
+
delete cleanValue.tool_calls;
|
|
148
|
+
if (cleanValue.content) { // only enqueue if there's content
|
|
149
|
+
// besides tool_calls
|
|
150
|
+
controller.enqueue(cleanValue);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
controller.enqueue(chunk);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
catch (e) {
|
|
159
|
+
this.isBusy = false;
|
|
160
|
+
controller.error(e);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
if (isStreamCancelled || this.isCancelled)
|
|
164
|
+
return;
|
|
165
|
+
if (toolCalls.length > 0) {
|
|
166
|
+
const toolResponses = await this.executeToolCalls(toolCalls);
|
|
167
|
+
if (isStreamCancelled || this.isCancelled)
|
|
168
|
+
return;
|
|
169
|
+
currentMessage = { role: 'tool', content: toolResponses };
|
|
170
|
+
await executeIteration();
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
this.isBusy = false;
|
|
174
|
+
controller.close();
|
|
175
|
+
};
|
|
176
|
+
executeIteration().catch((e) => {
|
|
177
|
+
if (isStreamCancelled || this.isCancelled)
|
|
178
|
+
return;
|
|
179
|
+
this.isBusy = false;
|
|
180
|
+
controller.error(e);
|
|
181
|
+
});
|
|
182
|
+
},
|
|
183
|
+
cancel: () => {
|
|
184
|
+
isStreamCancelled = true;
|
|
185
|
+
this.cancel();
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Cancels the current generation.
|
|
191
|
+
* Note: This does not abort any in-progress tool calls natively. Tools that
|
|
192
|
+
* affect state may still complete in the background eventually, but their
|
|
193
|
+
* results will be discarded and not reported back to the model or the user.
|
|
194
|
+
*/
|
|
195
|
+
cancel() {
|
|
196
|
+
this.isCancelled = true;
|
|
197
|
+
if (this.currentBatch) {
|
|
198
|
+
this.currentBatch.cancel();
|
|
199
|
+
}
|
|
200
|
+
if (this.baseConversation) {
|
|
201
|
+
this.baseConversation.cancel();
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
async getHistory() {
|
|
205
|
+
const conversation = await this.getConversation();
|
|
206
|
+
return conversation.getHistory();
|
|
207
|
+
}
|
|
208
|
+
async getTokenCount() {
|
|
209
|
+
const conversation = await this.getConversation();
|
|
210
|
+
return conversation.getTokenCount();
|
|
211
|
+
}
|
|
212
|
+
async getBenchmarkInfo() {
|
|
213
|
+
const conversation = await this.getConversation();
|
|
214
|
+
return conversation.getBenchmarkInfo();
|
|
215
|
+
}
|
|
216
|
+
async delete() {
|
|
217
|
+
if (this.baseConversation) {
|
|
218
|
+
await this.baseConversation.delete();
|
|
219
|
+
this.baseConversation = null;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
async executeToolCalls(toolCalls) {
|
|
223
|
+
this.currentBatch = new ToolCallBatch(this.tools, this.options.onToolProgress, () => this.nextToolCallId++);
|
|
224
|
+
try {
|
|
225
|
+
return await this.currentBatch.executeAll(toolCalls);
|
|
226
|
+
}
|
|
227
|
+
finally {
|
|
228
|
+
this.currentBatch = null;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
class ToolCallBatch {
|
|
233
|
+
tools;
|
|
234
|
+
onToolProgress;
|
|
235
|
+
getNextId;
|
|
236
|
+
isCancelled = false;
|
|
237
|
+
constructor(tools, onToolProgress, getNextId) {
|
|
238
|
+
this.tools = tools;
|
|
239
|
+
this.onToolProgress = onToolProgress;
|
|
240
|
+
this.getNextId = getNextId;
|
|
241
|
+
}
|
|
242
|
+
cancel() {
|
|
243
|
+
this.isCancelled = true;
|
|
244
|
+
}
|
|
245
|
+
async executeAll(toolCalls) {
|
|
246
|
+
return Promise.all(toolCalls.map(async (tc) => {
|
|
247
|
+
const tool = this.tools.get(tc.function.name);
|
|
248
|
+
const callId = this.getNextId();
|
|
249
|
+
let result;
|
|
250
|
+
if (this.onToolProgress) {
|
|
251
|
+
this.onToolProgress({
|
|
252
|
+
id: callId,
|
|
253
|
+
name: tc.function.name,
|
|
254
|
+
arguments: tc.function.arguments,
|
|
255
|
+
status: 'started'
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
if (!tool) {
|
|
259
|
+
const errorMsg = `Tool ${tc.function.name} not found.`;
|
|
260
|
+
result = { error: errorMsg };
|
|
261
|
+
if (!this.isCancelled && this.onToolProgress) {
|
|
262
|
+
this.onToolProgress({
|
|
263
|
+
id: callId,
|
|
264
|
+
name: tc.function.name,
|
|
265
|
+
arguments: tc.function.arguments,
|
|
266
|
+
status: 'error',
|
|
267
|
+
error: errorMsg
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
else {
|
|
272
|
+
try {
|
|
273
|
+
result = await tool.execute(tc.function.arguments);
|
|
274
|
+
if (!this.isCancelled && this.onToolProgress) {
|
|
275
|
+
this.onToolProgress({
|
|
276
|
+
id: callId,
|
|
277
|
+
name: tc.function.name,
|
|
278
|
+
arguments: tc.function.arguments,
|
|
279
|
+
status: 'completed',
|
|
280
|
+
result
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
catch (e) {
|
|
285
|
+
const errorMsg = String(e);
|
|
286
|
+
result = {
|
|
287
|
+
error: `Error executing tool ${tc.function.name}: ${errorMsg}`
|
|
288
|
+
};
|
|
289
|
+
if (!this.isCancelled && this.onToolProgress) {
|
|
290
|
+
this.onToolProgress({
|
|
291
|
+
id: callId,
|
|
292
|
+
name: tc.function.name,
|
|
293
|
+
arguments: tc.function.arguments,
|
|
294
|
+
status: 'error',
|
|
295
|
+
error: errorMsg
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return { type: 'tool_response', name: tc.function.name, response: result };
|
|
301
|
+
}));
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
//# sourceMappingURL=auto_tool_chat.js.map
|