llmz 0.9.0 → 1.0.2
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/DOCS.md +232 -1794
- package/ERRORS.md +101 -0
- package/README.md +96 -582
- package/dist/chat/chat.d.ts +63 -0
- package/dist/chat/component.d.ts +31 -0
- package/dist/chat/component.default.d.ts +58 -0
- package/dist/chat/response.d.ts +15 -0
- package/dist/compiler/compiler.d.ts +2 -7
- package/dist/compiler/plugins/dynamic-code.d.ts +7 -0
- package/dist/compiler/plugins/replace-comment.d.ts +2 -3
- package/dist/compiler/plugins/return-async.d.ts +5 -4
- package/dist/compiler/plugins/termination.d.ts +8 -0
- package/dist/compiler/plugins/tool-resolution.d.ts +4 -0
- package/dist/compiler/plugins/variable-extraction.d.ts +2 -8
- package/dist/component-CMFoCHyu.cjs +649 -0
- package/dist/component-DncqP4fB.js +614 -0
- package/dist/context.d.ts +71 -378
- package/dist/custom-client-C-WXvLD9.js +1655 -0
- package/dist/custom-client-Cgt9_0r1.cjs +1732 -0
- package/dist/custom-client.d.ts +12 -2
- package/dist/errors/catalog.d.ts +294 -0
- package/dist/errors/hooks.d.ts +2 -0
- package/dist/errors-C6ppQUZX.cjs +893 -0
- package/dist/errors-Crk5P8jo.js +636 -0
- package/dist/errors.d.ts +24 -36
- package/dist/execute-CiYvPs5Q.js +2061 -0
- package/dist/execute-DTTfacsi.cjs +2062 -0
- package/dist/{exit-DUM96DN1.cjs → exit-BOtKWPuT.cjs} +38 -28
- package/dist/{exit-CDdxDBRT.js → exit-DMPwCSba.js} +37 -27
- package/dist/exit.d.ts +19 -16
- package/dist/index.cjs +177 -1237
- package/dist/index.d.ts +25 -28
- package/dist/index.js +137 -1213
- package/dist/index.workerd.js +8974 -11145
- package/dist/inspect.d.ts +18 -4
- package/dist/inspection.d.ts +40 -0
- package/dist/native-0bq5ATZM.cjs +212 -0
- package/dist/native-n_718Z-T.js +177 -0
- package/dist/objects.d.ts +2 -57
- package/dist/prompts/native.d.ts +2 -0
- package/dist/prompts/prompt.d.ts +10 -101
- package/dist/quickjs-variant-21IO97PF.cjs +1080 -0
- package/dist/quickjs-variant-CtuGagsb.js +985 -0
- package/dist/result.d.ts +30 -861
- package/dist/runtime/execute.d.ts +3 -2
- package/dist/runtime/execution-activity.d.ts +11 -0
- package/dist/runtime/execution-diagnostics.d.ts +11 -0
- package/dist/runtime/execution-report.d.ts +51 -0
- package/dist/runtime/fixtures/chat.d.ts +8 -0
- package/dist/runtime/fixtures/long-search.d.ts +1 -1
- package/dist/runtime/fixtures/native-client.d.ts +33 -0
- package/dist/runtime/fixtures/protocol-languages.d.ts +1 -1
- package/dist/runtime/forced-inspection.d.ts +13 -0
- package/dist/runtime/forced-inspection.test.d.ts +1 -0
- package/dist/runtime/generate.d.ts +23 -20
- package/dist/runtime/generate.test.d.ts +1 -0
- package/dist/runtime/hook-provenance.test.d.ts +1 -0
- package/dist/runtime/inspection-hook.test.d.ts +1 -0
- package/dist/runtime/inspection-values.d.ts +14 -0
- package/dist/runtime/inspection-values.test.d.ts +1 -0
- package/dist/runtime/interruption-control-flow.test.d.ts +1 -0
- package/dist/runtime/interruption-settings.test.d.ts +1 -0
- package/dist/runtime/javascript-api.d.ts +62 -0
- package/dist/runtime/javascript-api.test.d.ts +1 -0
- package/dist/runtime/native-review.test.d.ts +1 -0
- package/dist/runtime/native-tools.d.ts +18 -0
- package/dist/runtime/native-tools.test.d.ts +1 -0
- package/dist/runtime/object-memory.test.d.ts +1 -0
- package/dist/runtime/single-tool.test.d.ts +1 -0
- package/dist/runtime/think-signal.test.d.ts +1 -0
- package/dist/runtime/token-budget.d.ts +22 -0
- package/dist/runtime/token-budget.test.d.ts +1 -0
- package/dist/runtime/tool-wrapper.d.ts +6 -5
- package/dist/runtime/traces.test.d.ts +1 -0
- package/dist/runtime/truncation.test.d.ts +1 -0
- package/dist/runtime/types.d.ts +47 -31
- package/dist/runtime/variable-recovery.test.d.ts +1 -0
- package/dist/runtime/vm-context.d.ts +6 -1
- package/dist/runtime/worker-completion.test.d.ts +1 -0
- package/dist/runtime-names.d.ts +4 -0
- package/dist/schema-CXlnbb3n.cjs +68 -0
- package/dist/schema-GE4ISPCR.js +45 -0
- package/dist/schema.d.ts +10 -0
- package/dist/schema.test.d.ts +1 -0
- package/dist/session/compactor.d.ts +51 -0
- package/dist/session/compactor.test.d.ts +1 -0
- package/dist/session/history.d.ts +36 -0
- package/dist/session/history.test.d.ts +1 -0
- package/dist/session/json.d.ts +3 -0
- package/dist/session/json.test.d.ts +1 -0
- package/dist/session/memory-codec.d.ts +9 -0
- package/dist/session/memory-codec.test.d.ts +1 -0
- package/dist/session/memory-render.d.ts +15 -0
- package/dist/session/memory-render.test.d.ts +1 -0
- package/dist/session/memory.d.ts +112 -0
- package/dist/session/memory.test.d.ts +1 -0
- package/dist/session/messages.d.ts +19 -0
- package/dist/session/messages.test.d.ts +1 -0
- package/dist/session/serialization.d.ts +36 -0
- package/dist/session/serialization.test.d.ts +1 -0
- package/dist/session/session-memory.test.d.ts +1 -0
- package/dist/session/session.d.ts +121 -0
- package/dist/session/session.test.d.ts +1 -0
- package/dist/session/speaker.test.d.ts +1 -0
- package/dist/{transcript.d.ts → session/transcript.d.ts} +4 -8
- package/dist/session/transcript.test.d.ts +1 -0
- package/dist/{tool-Bv8N4IPj.js → tool-DstssiAs.js} +144 -92
- package/dist/{tool-Dy6AJpJH.cjs → tool-_wK9hQOb.cjs} +146 -94
- package/dist/tool.d.ts +25 -17
- package/dist/truncate-BEKbdo3t.js +79 -0
- package/dist/truncate-uomAMGPs.cjs +102 -0
- package/dist/truncate.d.ts +24 -0
- package/dist/truncate.test.d.ts +1 -0
- package/dist/types.d.ts +33 -9
- package/dist/{typings-3lsqCbkn.cjs → typings-BA_XxAcY.cjs} +38 -193
- package/dist/{typings-CrBa7Tyy.js → typings-BEVDvtuk.js} +38 -175
- package/dist/typings-formatter.d.ts +1 -1
- package/dist/typings.d.ts +3 -0
- package/dist/{uniq-BYQ7GWwb.js → uniq-DfUeSNaX.js} +2 -2
- package/dist/{uniq-G872RWmE.cjs → uniq-DiXgysNV.cjs} +6 -18
- package/dist/{_baseIteratee-P_firB7b.js → utils-BOFzb8Ok.js} +1134 -16
- package/dist/{_baseIteratee-CF4qP2a8.cjs → utils-CTlsXvdj.cjs} +1281 -84
- package/dist/vm/drivers/node.d.ts +1 -1
- package/dist/vm/drivers/quickjs.d.ts +1 -1
- package/dist/vm/errors.d.ts +4 -4
- package/dist/vm/index.d.ts +3 -4
- package/dist/vm/instrument.d.ts +14 -6
- package/dist/vm/member-proxy.d.ts +4 -0
- package/dist/vm/member-proxy.test.d.ts +1 -0
- package/dist/vm/memory.test.d.ts +1 -0
- package/dist/vm/return-semantics.test.d.ts +1 -0
- package/dist/vm/types.d.ts +20 -3
- package/dist/{vm-BLsHw1da.js → vm-C7SfutHn.js} +1187 -180
- package/dist/{vm-D6s1zF6M.cjs → vm-gmekHc0i.cjs} +1221 -207
- package/package.json +13 -6
- package/dist/_baseForOwn-BfWDTL8L.cjs +0 -53
- package/dist/_baseForOwn-CFhWe_W6.js +0 -48
- package/dist/_getPrototype-CG7Dxrrz.js +0 -6
- package/dist/_getPrototype-DQDlNVVM.cjs +0 -10
- package/dist/_overArg-C5Hy7oCi.cjs +0 -196
- package/dist/_overArg-CRYbj1NH.js +0 -155
- package/dist/chat.d.ts +0 -401
- package/dist/common-BlRFtdf2.js +0 -807
- package/dist/common-BnvrDnIT.cjs +0 -860
- package/dist/compiler/plugins/track-tool-calls.d.ts +0 -33
- package/dist/compiler-C-OI9sim.cjs +0 -547
- package/dist/compiler-CW1VjQsn.js +0 -473
- package/dist/component-C6Agl2ns.cjs +0 -73
- package/dist/component-DGG7d_Mz.js +0 -44
- package/dist/component.d.ts +0 -88
- package/dist/component.default.d.ts +0 -278
- package/dist/custom-client-ChuZ63zp.js +0 -2658
- package/dist/custom-client-DAsGRnYc.cjs +0 -2729
- package/dist/dual-modes-DDkoCp3R.js +0 -3006
- package/dist/dual-modes-DPXiuvNe.cjs +0 -3090
- package/dist/errors-C962uJdS.cjs +0 -220
- package/dist/errors-Cg1G0Awu.js +0 -161
- package/dist/example-format.d.ts +0 -8
- package/dist/example.d.ts +0 -45
- package/dist/execute-BWcdv9so.js +0 -1059
- package/dist/execute-DM2-7y86.cjs +0 -1060
- package/dist/exit-parser.d.ts +0 -39
- package/dist/handlers.d.ts +0 -10
- package/dist/hoist.d.ts +0 -2
- package/dist/isEmpty-BhJRCqIF.js +0 -52
- package/dist/isEmpty-LY533Wmh.cjs +0 -57
- package/dist/isPlainObject-CSxXVKCk.cjs +0 -56
- package/dist/isPlainObject-D39AzPO2.js +0 -51
- package/dist/jsx-CIgdGGVs.js +0 -52
- package/dist/jsx-owqxrquU.cjs +0 -75
- package/dist/jsx.d.ts +0 -11
- package/dist/message-stream/index.d.ts +0 -9
- package/dist/message-stream/instructions.d.ts +0 -30
- package/dist/message-stream/normalizer.d.ts +0 -21
- package/dist/message-stream/parser.d.ts +0 -89
- package/dist/message-stream/registry.d.ts +0 -14
- package/dist/message-stream/replayable.d.ts +0 -27
- package/dist/message-stream/response-parser.d.ts +0 -24
- package/dist/message-stream/sanitize.d.ts +0 -10
- package/dist/message-stream/stream.d.ts +0 -54
- package/dist/message-stream/types.d.ts +0 -97
- package/dist/message-stream/validator.d.ts +0 -23
- package/dist/prompts/chat-mode/protocol.d.ts +0 -2
- package/dist/prompts/chat-mode/system.d.ts +0 -2
- package/dist/prompts/chat-mode/system.md.d.ts +0 -2
- package/dist/prompts/chat-mode/user.d.ts +0 -2
- package/dist/prompts/chat-mode/user.md.d.ts +0 -2
- package/dist/prompts/code-examples.d.ts +0 -2
- package/dist/prompts/common.d.ts +0 -7
- package/dist/prompts/dual-modes.d.ts +0 -2
- package/dist/prompts/execution-history.d.ts +0 -6
- package/dist/prompts/execution-state.d.ts +0 -3
- package/dist/prompts/protocol-basics.d.ts +0 -5
- package/dist/prompts/protocol.d.ts +0 -31
- package/dist/prompts/worker-mode/protocol.d.ts +0 -2
- package/dist/prompts/worker-mode/system.d.ts +0 -2
- package/dist/prompts/worker-mode/system.md.d.ts +0 -2
- package/dist/prompts/worker-mode/user.d.ts +0 -2
- package/dist/prompts/worker-mode/user.md.d.ts +0 -2
- package/dist/quickjs-variant-BYhMBpwU.js +0 -45
- package/dist/quickjs-variant-CsIwTVFC.cjs +0 -56
- package/dist/rolldown-runtime-EAsCxrDo.js +0 -14
- package/dist/runtime/fixtures/qwen-protocol-failures.d.ts +0 -29
- package/dist/runtime/interpret-result.d.ts +0 -22
- package/dist/snapshots.d.ts +0 -249
- package/dist/stack-traces-BIE_5Xwf.js +0 -21
- package/dist/stack-traces-ntw_qhoy.cjs +0 -26
- package/dist/truncator-CLkouTCO.cjs +0 -276
- package/dist/truncator-DZqEqnpD.js +0 -253
- package/dist/truncator.d.ts +0 -118
- package/dist/utils-3ESwP3yg.cjs +0 -1075
- package/dist/utils-UBeAFeEI.js +0 -950
- /package/dist/{citations.d.ts → chat/citations.d.ts} +0 -0
- /package/dist/{citations.test.d.ts → chat/citations.test.d.ts} +0 -0
- /package/dist/{component.default.test.d.ts → chat/component.default.test.d.ts} +0 -0
- /package/dist/{compiler/plugins/track-tool-calls.test.d.ts → chat/component.test.d.ts} +0 -0
- /package/dist/{example.test.d.ts → chat/response.test.d.ts} +0 -0
- /package/dist/{exit-parser.test.d.ts → compiler/plugins/dynamic-code.test.d.ts} +0 -0
- /package/dist/{handlers.test.d.ts → compiler/plugins/tool-resolution.test.d.ts} +0 -0
- /package/dist/{hoist.test.d.ts → errors/catalog.test.d.ts} +0 -0
- /package/dist/{message-stream/instructions.test.d.ts → exit.test.d.ts} +0 -0
- /package/dist/{message-stream/normalizer.test.d.ts → inspection.test.d.ts} +0 -0
- /package/dist/{message-stream/parser.test.d.ts → prompts/native.test.d.ts} +0 -0
- /package/dist/{message-stream/response-parser.test.d.ts → result.test.d.ts} +0 -0
- /package/dist/{message-stream/stream.test.d.ts → runtime/chat-completion.test.d.ts} +0 -0
- /package/dist/{message-stream/validator.test.d.ts → runtime/chat-delivery.test.d.ts} +0 -0
- /package/dist/{prompts/adherence.test.d.ts → runtime/chat-last-speaker.test.d.ts} +0 -0
- /package/dist/{prompts/catalogue-examples.test.d.ts → runtime/compatibility-regressions.test.d.ts} +0 -0
- /package/dist/{prompts/code-examples.test.d.ts → runtime/context-registration.test.d.ts} +0 -0
- /package/dist/{prompts/common.test.d.ts → runtime/empty-completion.test.d.ts} +0 -0
- /package/dist/{prompts/concrete-patterns.test.d.ts → runtime/error-recovery.test.d.ts} +0 -0
- /package/dist/{prompts/long-search.test.d.ts → runtime/errors.test.d.ts} +0 -0
- /package/dist/{prompts/protocol-simple.test.d.ts → runtime/execution-activity.test.d.ts} +0 -0
- /package/dist/{prompts/protocol.test.d.ts → runtime/execution-diagnostics.test.d.ts} +0 -0
- /package/dist/{prompts/sections.test.d.ts → runtime/execution-report.test.d.ts} +0 -0
- /package/dist/runtime/{interpret-result.test.d.ts → execution-safety.test.d.ts} +0 -0
- /package/dist/{transcript.test.d.ts → runtime/exit-control-flow.test.d.ts} +0 -0
- /package/dist/{truncator.test.d.ts → runtime/fixtures/long-search.test.d.ts} +0 -0
|
@@ -1,9 +1,59 @@
|
|
|
1
|
-
const
|
|
2
|
-
const
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
const
|
|
1
|
+
const require_utils = require("./utils-CTlsXvdj.cjs");
|
|
2
|
+
const require_errors = require("./errors-C6ppQUZX.cjs");
|
|
3
|
+
const require_uniq = require("./uniq-DiXgysNV.cjs");
|
|
4
|
+
const require_schema = require("./schema-CXlnbb3n.cjs");
|
|
5
|
+
const require_truncate = require("./truncate-uomAMGPs.cjs");
|
|
6
|
+
const require_typings = require("./typings-BA_XxAcY.cjs");
|
|
6
7
|
let _bpinternal_zui = require("@bpinternal/zui");
|
|
8
|
+
//#region ../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/isEmpty.js
|
|
9
|
+
/** `Object#toString` result references. */
|
|
10
|
+
var mapTag = "[object Map]";
|
|
11
|
+
var setTag = "[object Set]";
|
|
12
|
+
/** Used to check objects for own properties. */
|
|
13
|
+
var hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
14
|
+
/**
|
|
15
|
+
* Checks if `value` is an empty object, collection, map, or set.
|
|
16
|
+
*
|
|
17
|
+
* Objects are considered empty if they have no own enumerable string keyed
|
|
18
|
+
* properties.
|
|
19
|
+
*
|
|
20
|
+
* Array-like values such as `arguments` objects, arrays, buffers, strings, or
|
|
21
|
+
* jQuery-like collections are considered empty if they have a `length` of `0`.
|
|
22
|
+
* Similarly, maps and sets are considered empty if they have a `size` of `0`.
|
|
23
|
+
*
|
|
24
|
+
* @static
|
|
25
|
+
* @memberOf _
|
|
26
|
+
* @since 0.1.0
|
|
27
|
+
* @category Lang
|
|
28
|
+
* @param {*} value The value to check.
|
|
29
|
+
* @returns {boolean} Returns `true` if `value` is empty, else `false`.
|
|
30
|
+
* @example
|
|
31
|
+
*
|
|
32
|
+
* _.isEmpty(null);
|
|
33
|
+
* // => true
|
|
34
|
+
*
|
|
35
|
+
* _.isEmpty(true);
|
|
36
|
+
* // => true
|
|
37
|
+
*
|
|
38
|
+
* _.isEmpty(1);
|
|
39
|
+
* // => true
|
|
40
|
+
*
|
|
41
|
+
* _.isEmpty([1, 2, 3]);
|
|
42
|
+
* // => false
|
|
43
|
+
*
|
|
44
|
+
* _.isEmpty({ 'a': 1 });
|
|
45
|
+
* // => false
|
|
46
|
+
*/
|
|
47
|
+
function isEmpty(value) {
|
|
48
|
+
if (value == null) return true;
|
|
49
|
+
if (require_utils.isArrayLike(value) && (require_utils.isArray(value) || typeof value == "string" || typeof value.splice == "function" || require_utils.isBuffer(value) || require_utils.isTypedArray(value) || require_utils.isArguments(value))) return !value.length;
|
|
50
|
+
var tag = require_utils._getTag_default(value);
|
|
51
|
+
if (tag == mapTag || tag == setTag) return !value.size;
|
|
52
|
+
if (require_utils.isPrototype(value)) return !require_utils.baseKeys(value).length;
|
|
53
|
+
for (var key in value) if (hasOwnProperty.call(value, key)) return false;
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
//#endregion
|
|
7
57
|
//#region src/convert.ts
|
|
8
58
|
function convertObjectToZuiLiterals(obj, nested = false) {
|
|
9
59
|
if (typeof obj === "string") return _bpinternal_zui.z.literal(obj).catch(() => obj);
|
|
@@ -27,7 +77,7 @@ function convertObjectToZuiLiterals(obj, nested = false) {
|
|
|
27
77
|
}
|
|
28
78
|
//#endregion
|
|
29
79
|
//#region src/tool.ts
|
|
30
|
-
var tool_exports = /* @__PURE__ */
|
|
80
|
+
var tool_exports = /* @__PURE__ */ require_utils.__exportAll({ Tool: () => Tool });
|
|
31
81
|
/**
|
|
32
82
|
* Tool represents a callable function that agents can use to interact with external systems.
|
|
33
83
|
*
|
|
@@ -37,7 +87,7 @@ var tool_exports = /* @__PURE__ */ require_compiler.__exportAll({ Tool: () => To
|
|
|
37
87
|
*
|
|
38
88
|
* ## Key Features
|
|
39
89
|
* - **Type Safety**: Full TypeScript inference with Zui/Zod schema validation
|
|
40
|
-
* - **Input
|
|
90
|
+
* - **Input Validation**: Parse inputs through their original schema; output schemas are documentation only
|
|
41
91
|
* - **Retry Logic**: Built-in retry mechanisms with custom retry functions
|
|
42
92
|
* - **Static Values**: Pre-configure parameters that won't change between calls
|
|
43
93
|
* - **Tool Cloning**: Create variations of existing tools with modified behavior
|
|
@@ -164,7 +214,7 @@ var tool_exports = /* @__PURE__ */ require_compiler.__exportAll({ Tool: () => To
|
|
|
164
214
|
*
|
|
165
215
|
* Tools automatically handle:
|
|
166
216
|
* - **Input validation**: Invalid inputs throw descriptive errors
|
|
167
|
-
* - **Output
|
|
217
|
+
* - **Output hints**: Schemas describe likely outputs; results are never parsed or rejected
|
|
168
218
|
* - **Handler errors**: Exceptions in handlers are caught and can trigger retries
|
|
169
219
|
* - **Type coercion**: Basic type coercion where possible
|
|
170
220
|
*
|
|
@@ -175,7 +225,9 @@ var Tool = class Tool {
|
|
|
175
225
|
aliases = [];
|
|
176
226
|
description;
|
|
177
227
|
metadata;
|
|
228
|
+
/** Model-facing input description; zInput retains the original validator. */
|
|
178
229
|
input;
|
|
230
|
+
/** Expected raw output shape, used for documentation only. */
|
|
179
231
|
output;
|
|
180
232
|
retry;
|
|
181
233
|
MAX_RETRIES = 1e3;
|
|
@@ -226,8 +278,8 @@ var Tool = class Tool {
|
|
|
226
278
|
return this;
|
|
227
279
|
}
|
|
228
280
|
const input = this.input ? require_utils.fromJSONSchemaCompat(this.input) : _bpinternal_zui.z.any();
|
|
229
|
-
if (_bpinternal_zui.z.is.zuiObject(input) && typeof values !== "object") throw new
|
|
230
|
-
if (_bpinternal_zui.z.is.zuiArray(input) && !Array.isArray(values)) throw new
|
|
281
|
+
if (_bpinternal_zui.z.is.zuiObject(input) && typeof values !== "object") throw new require_errors.InvalidToolError(`Invalid static input values for tool ${this.name}. Expected an object, but got type "${typeof values}"`);
|
|
282
|
+
if (_bpinternal_zui.z.is.zuiArray(input) && !Array.isArray(values)) throw new require_errors.InvalidToolError(`Invalid static input values for tool ${this.name}. Expected an array, but got type "${typeof values}"`);
|
|
231
283
|
this._staticInputValues = values;
|
|
232
284
|
return this;
|
|
233
285
|
}
|
|
@@ -241,28 +293,19 @@ var Tool = class Tool {
|
|
|
241
293
|
* @internal
|
|
242
294
|
*/
|
|
243
295
|
get zInput() {
|
|
244
|
-
|
|
245
|
-
if (this.
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
else
|
|
251
|
-
|
|
252
|
-
if (_bpinternal_zui.z.is.zuiObject(input)) {
|
|
253
|
-
const inputExtensions = convertObjectToZuiLiterals(this._staticInputValues);
|
|
254
|
-
input = input.extend(inputExtensions);
|
|
255
|
-
} else if (_bpinternal_zui.z.is.zuiArray(input)) {
|
|
256
|
-
const inputExtensions = convertObjectToZuiLiterals(this._staticInputValues);
|
|
257
|
-
input = _bpinternal_zui.z.array(input.element.extend(inputExtensions));
|
|
258
|
-
} else input = convertObjectToZuiLiterals(this._staticInputValues);
|
|
259
|
-
}
|
|
260
|
-
return input;
|
|
296
|
+
const input = this._inputSchema ?? (this.input ? require_utils.fromJSONSchemaCompat(this.input) : _bpinternal_zui.z.any());
|
|
297
|
+
if (isEmpty(this._staticInputValues)) return input;
|
|
298
|
+
const shape = require_schema.schemaInput(input);
|
|
299
|
+
let overrides;
|
|
300
|
+
if (_bpinternal_zui.z.is.zuiObject(shape)) overrides = _bpinternal_zui.z.object(convertObjectToZuiLiterals(this._staticInputValues)).passthrough();
|
|
301
|
+
else if (_bpinternal_zui.z.is.zuiArray(shape)) overrides = _bpinternal_zui.z.array(_bpinternal_zui.z.object(convertObjectToZuiLiterals(this._staticInputValues)).passthrough());
|
|
302
|
+
else overrides = convertObjectToZuiLiterals(this._staticInputValues);
|
|
303
|
+
return overrides.pipe(input);
|
|
261
304
|
}
|
|
262
305
|
/**
|
|
263
|
-
* Gets the output schema
|
|
306
|
+
* Gets the output description as a promise schema. It is never used to parse tool results.
|
|
264
307
|
*
|
|
265
|
-
* @returns The Zui schema
|
|
308
|
+
* @returns The Zui schema used to describe likely output
|
|
266
309
|
* @internal
|
|
267
310
|
*/
|
|
268
311
|
get zOutput() {
|
|
@@ -290,7 +333,7 @@ var Tool = class Tool {
|
|
|
290
333
|
*/
|
|
291
334
|
rename(name) {
|
|
292
335
|
const before = this.name;
|
|
293
|
-
if (!require_utils.isValidIdentifier(name)) throw new
|
|
336
|
+
if (!require_utils.isValidIdentifier(name)) throw new require_errors.InvalidToolError(`Invalid name for tool ${name}. A tool name must start with a letter and contain only letters, numbers, and underscores. It must be 1-50 characters long.`);
|
|
294
337
|
this.name = name;
|
|
295
338
|
this.aliases = require_uniq.uniq([name, ...this.aliases.map((alias) => alias === before ? name : alias)]);
|
|
296
339
|
return this;
|
|
@@ -353,33 +396,30 @@ var Tool = class Tool {
|
|
|
353
396
|
*/
|
|
354
397
|
clone(props = {}) {
|
|
355
398
|
try {
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
if (this.output) try {
|
|
365
|
-
zOutput = _bpinternal_zui.transforms.fromJSONSchema(this.output);
|
|
366
|
-
} catch {
|
|
367
|
-
zOutput = _bpinternal_zui.transforms.fromJSONSchemaLegacy(this.output);
|
|
368
|
-
}
|
|
399
|
+
const zInput = this._inputSchema ?? (this.input ? require_utils.fromJSONSchemaCompat(this.input) : void 0);
|
|
400
|
+
const zOutput = this._outputSchema ?? (this.output ? require_utils.fromJSONSchemaCompat(this.output) : void 0);
|
|
401
|
+
let input = zInput;
|
|
402
|
+
if (typeof props.input === "function") input = props.input(zInput);
|
|
403
|
+
else if (_bpinternal_zui.z.is.zuiType(props.input)) input = props.input;
|
|
404
|
+
let output = zOutput;
|
|
405
|
+
if (typeof props.output === "function") output = props.output(zOutput);
|
|
406
|
+
else if (_bpinternal_zui.z.is.zuiType(props.output)) output = props.output;
|
|
369
407
|
return new Tool({
|
|
370
408
|
name: props.name ?? this.name,
|
|
371
409
|
aliases: props.aliases ?? [...this.aliases],
|
|
372
410
|
description: props.description ?? this.description,
|
|
373
411
|
metadata: JSON.parse(JSON.stringify(props.metadata ?? this.metadata)),
|
|
374
|
-
input
|
|
375
|
-
output
|
|
412
|
+
input,
|
|
413
|
+
output,
|
|
376
414
|
handler: props.handler ?? this._handler,
|
|
377
415
|
retry: props.retry ?? this.retry
|
|
378
416
|
}).setStaticInputValues(props.staticInputValues ?? this._staticInputValues);
|
|
379
417
|
} catch (e) {
|
|
380
|
-
throw new
|
|
418
|
+
throw new require_errors.InvalidToolError(`Failed to clone tool "${this.name}": ${e}`);
|
|
381
419
|
}
|
|
382
420
|
}
|
|
421
|
+
_inputSchema;
|
|
422
|
+
_outputSchema;
|
|
383
423
|
_handler;
|
|
384
424
|
/**
|
|
385
425
|
* Creates a new Tool instance.
|
|
@@ -388,7 +428,7 @@ var Tool = class Tool {
|
|
|
388
428
|
* @param props.name - Unique tool name (must be valid TypeScript identifier)
|
|
389
429
|
* @param props.description - Human-readable description for the LLM
|
|
390
430
|
* @param props.input - Zui/Zod schema for input validation (optional)
|
|
391
|
-
* @param props.output -
|
|
431
|
+
* @param props.output - Schema describing likely raw output; never used for validation or normalization
|
|
392
432
|
* @param props.handler - Async function that implements the tool logic
|
|
393
433
|
* @param props.aliases - Alternative names for the tool (optional)
|
|
394
434
|
* @param props.metadata - Additional metadata for the tool (optional)
|
|
@@ -458,29 +498,32 @@ var Tool = class Tool {
|
|
|
458
498
|
* ```
|
|
459
499
|
*/
|
|
460
500
|
constructor(props) {
|
|
461
|
-
if (!
|
|
462
|
-
if (
|
|
463
|
-
if (props.
|
|
464
|
-
if (typeof props.
|
|
465
|
-
if (props.
|
|
466
|
-
if (props.aliases
|
|
501
|
+
if (!props || typeof props !== "object" || Array.isArray(props)) throw new require_errors.InvalidToolError("Tool definition must be an object.");
|
|
502
|
+
if (!require_utils.isValidIdentifier(props.name)) throw new require_errors.InvalidToolError(`Invalid name for tool ${props.name}. A tool name must start with a letter and contain only letters, numbers, and underscores. It must be 1-50 characters long.`);
|
|
503
|
+
if (props.description !== void 0 && typeof props.description !== "string") throw new require_errors.InvalidToolError(`Invalid description for tool ${props.name}. Expected a string, but got type "${typeof props.description}"`);
|
|
504
|
+
if (props.metadata !== void 0 && typeof props.metadata !== "object") throw new require_errors.InvalidToolError(`Invalid metadata for tool ${props.name}. Expected an object, but got type "${typeof props.metadata}"`);
|
|
505
|
+
if (typeof props.handler !== "function") throw new require_errors.InvalidToolError(`Invalid handler for tool ${props.name}. Expected a function, but got type "${typeof props.handler}"`);
|
|
506
|
+
if (props.aliases !== void 0 && !Array.isArray(props.aliases)) throw new require_errors.InvalidToolError(`Invalid aliases for tool ${props.name}. Expected an array, but got type "${typeof props.aliases}"`);
|
|
507
|
+
if (props.aliases && props.aliases.some((alias) => !require_utils.isValidIdentifier(alias))) throw new require_errors.InvalidToolError(`Invalid aliases for tool ${props.name}. Expected an array of valid identifiers.`);
|
|
467
508
|
if (typeof props.input !== "undefined") {
|
|
468
509
|
if (require_utils.isZuiSchema(props.input)) try {
|
|
469
|
-
this.input =
|
|
470
|
-
|
|
471
|
-
|
|
510
|
+
this.input = require_schema.toModelSchema(props.input);
|
|
511
|
+
this._inputSchema = props.input;
|
|
512
|
+
} catch (cause) {
|
|
513
|
+
throw new require_errors.InvalidToolError(`Cannot describe input schema for tool ${props.name}: ${String(cause)}`, { cause });
|
|
472
514
|
}
|
|
473
515
|
else if (require_utils.isJsonSchema(props.input)) this.input = props.input;
|
|
474
|
-
else throw new
|
|
516
|
+
else throw new require_errors.InvalidToolError(`Invalid input schema for tool ${props.name}. Expected a ZodType or JSONSchema, but got type "${typeof props.input}"`);
|
|
475
517
|
}
|
|
476
518
|
if (typeof props.output !== "undefined") {
|
|
477
519
|
if (require_utils.isZuiSchema(props.output)) try {
|
|
478
|
-
this.output =
|
|
479
|
-
|
|
480
|
-
|
|
520
|
+
this.output = require_schema.toModelSchema(props.output);
|
|
521
|
+
this._outputSchema = props.output;
|
|
522
|
+
} catch (cause) {
|
|
523
|
+
throw new require_errors.InvalidToolError(`Cannot describe output schema for tool ${props.name}: ${String(cause)}`, { cause });
|
|
481
524
|
}
|
|
482
525
|
else if (require_utils.isJsonSchema(props.output)) this.output = props.output;
|
|
483
|
-
else throw new
|
|
526
|
+
else throw new require_errors.InvalidToolError(`Invalid output schema for tool ${props.name}. Expected a ZodType or JSONSchema, but got type "${typeof props.output}"`);
|
|
484
527
|
}
|
|
485
528
|
this.name = props.name;
|
|
486
529
|
this.aliases = require_uniq.uniq([props.name, ...props.aliases ?? []]);
|
|
@@ -490,28 +533,10 @@ var Tool = class Tool {
|
|
|
490
533
|
this.setStaticInputValues(props.staticInputValues);
|
|
491
534
|
this.retry = props.retry;
|
|
492
535
|
}
|
|
493
|
-
async _executeHandler(input, ctx, chat, yieldedCount, setYieldedCount) {
|
|
494
|
-
const handler = this._handler(input, ctx);
|
|
495
|
-
if (!(typeof handler === "object" && handler !== null && "next" in handler)) return handler;
|
|
496
|
-
let yieldIndex = 0;
|
|
497
|
-
while (true) {
|
|
498
|
-
const { value, done } = await handler.next();
|
|
499
|
-
if (done) return value;
|
|
500
|
-
if (yieldIndex >= yieldedCount) {
|
|
501
|
-
var _chat$handler;
|
|
502
|
-
setYieldedCount(yieldIndex + 1);
|
|
503
|
-
await (chat === null || chat === void 0 || (_chat$handler = chat.handler) === null || _chat$handler === void 0 ? void 0 : _chat$handler.call(chat, value, {
|
|
504
|
-
iterationId: ctx.iterationId ?? ctx.callId,
|
|
505
|
-
id: `${ctx.callId}:yield-${yieldIndex}`
|
|
506
|
-
}));
|
|
507
|
-
}
|
|
508
|
-
yieldIndex++;
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
536
|
/**
|
|
512
537
|
* Executes the tool with the given input and context.
|
|
513
538
|
*
|
|
514
|
-
* This method handles
|
|
539
|
+
* This method parses inputs and handles retries. Output schemas are hints: results pass through unchanged.
|
|
515
540
|
* It's called internally by the LLMz execution engine when generated code calls the tool.
|
|
516
541
|
*
|
|
517
542
|
* @param input - Input data to pass to the tool handler
|
|
@@ -532,27 +557,43 @@ var Tool = class Tool {
|
|
|
532
557
|
*
|
|
533
558
|
* @internal This method is primarily used internally by the LLMz execution engine
|
|
534
559
|
*/
|
|
535
|
-
async execute(rawInput, ctx
|
|
536
|
-
|
|
537
|
-
const
|
|
538
|
-
|
|
560
|
+
async execute(rawInput, ctx) {
|
|
561
|
+
var _ctx$onInput;
|
|
562
|
+
const schema = this.zInput;
|
|
563
|
+
const input = _bpinternal_zui.z.is.zuiObject(require_schema.schemaInput(schema)) ? rawInput ?? {} : rawInput;
|
|
564
|
+
const pInput = await require_schema.parseSchemaAsync(schema, input, `Tool "${this.name}" input`);
|
|
565
|
+
if (!pInput.success) {
|
|
566
|
+
const expectedInput = await require_typings.getTypings(this._inputSchema ?? schema);
|
|
567
|
+
const issues = pInput.error.issues.map(({ path, message }) => ({
|
|
568
|
+
path,
|
|
569
|
+
message
|
|
570
|
+
}));
|
|
571
|
+
throw new require_errors.ToolInputError(this.name, issues, expectedInput);
|
|
572
|
+
}
|
|
573
|
+
(_ctx$onInput = ctx.onInput) === null || _ctx$onInput === void 0 || _ctx$onInput.call(ctx, pInput.data);
|
|
539
574
|
let attempt = 0;
|
|
540
|
-
let
|
|
575
|
+
let lastError;
|
|
541
576
|
while (attempt < this.MAX_RETRIES) try {
|
|
542
|
-
const result = await this.
|
|
543
|
-
|
|
577
|
+
const result = await this._handler(pInput.data, ctx);
|
|
578
|
+
const policy = require_truncate.isTruncated(result) ? result.$$truncate : void 0;
|
|
579
|
+
const output = require_truncate.unwrapTruncated(result);
|
|
580
|
+
if (!policy) return output;
|
|
581
|
+
return Promise.resolve(output).then((parsed) => {
|
|
582
|
+
var _ctx$onTruncation;
|
|
583
|
+
(_ctx$onTruncation = ctx.onTruncation) === null || _ctx$onTruncation === void 0 || _ctx$onTruncation.call(ctx, parsed, policy);
|
|
584
|
+
return parsed;
|
|
544
585
|
});
|
|
545
|
-
const pOutput = this.zOutput.safeParse(result);
|
|
546
|
-
return pOutput.success ? pOutput.data : result;
|
|
547
586
|
} catch (err) {
|
|
548
587
|
var _this$retry;
|
|
588
|
+
if (require_errors.isCriticalError(err) || require_errors.VMSignal.is(err)) throw err;
|
|
589
|
+
lastError = err;
|
|
549
590
|
if (!await ((_this$retry = this.retry) === null || _this$retry === void 0 ? void 0 : _this$retry.call(this, {
|
|
550
591
|
input: pInput.data,
|
|
551
592
|
attempt: ++attempt,
|
|
552
593
|
error: err
|
|
553
|
-
}))) throw err;
|
|
594
|
+
}))) throw require_errors.VMSignal.is(err) || require_errors.isLLMzError(err) ? err : new require_errors.ToolExecutionError(this.name, err);
|
|
554
595
|
}
|
|
555
|
-
throw new
|
|
596
|
+
throw new require_errors.ToolExecutionError(this.name, lastError);
|
|
556
597
|
}
|
|
557
598
|
/**
|
|
558
599
|
* Generates TypeScript type definitions for this tool.
|
|
@@ -570,6 +611,8 @@ var Tool = class Tool {
|
|
|
570
611
|
} catch {
|
|
571
612
|
input = _bpinternal_zui.transforms.fromJSONSchemaLegacy(this.input);
|
|
572
613
|
}
|
|
614
|
+
const originalInput = this._inputSchema && require_schema.schemaInput(this._inputSchema);
|
|
615
|
+
if (originalInput && (_bpinternal_zui.z.is.zuiVoid(originalInput) || _bpinternal_zui.z.is.zuiUndefined(originalInput))) input = originalInput;
|
|
573
616
|
let output;
|
|
574
617
|
if (!this.output || Object.keys(this.output).length === 0) output = _bpinternal_zui.z.void();
|
|
575
618
|
else try {
|
|
@@ -577,11 +620,20 @@ var Tool = class Tool {
|
|
|
577
620
|
} catch {
|
|
578
621
|
output = _bpinternal_zui.transforms.fromJSONSchemaLegacy(this.output);
|
|
579
622
|
}
|
|
580
|
-
if (input && _bpinternal_zui.z.is.zuiObject(input.naked()) && typeof this._staticInputValues === "object" && !
|
|
623
|
+
if (input && _bpinternal_zui.z.is.zuiObject(input.naked()) && typeof this._staticInputValues === "object" && !isEmpty(this._staticInputValues)) {
|
|
581
624
|
const inputExtensions = convertObjectToZuiLiterals(this._staticInputValues);
|
|
582
625
|
input = input.extend(inputExtensions);
|
|
583
626
|
} else if (this._staticInputValues !== void 0) input = convertObjectToZuiLiterals(this._staticInputValues);
|
|
584
|
-
const
|
|
627
|
+
const valueType = input === null || input === void 0 ? void 0 : input.naked();
|
|
628
|
+
let directType;
|
|
629
|
+
if (valueType) {
|
|
630
|
+
if (_bpinternal_zui.z.is.zuiString(valueType)) directType = "string";
|
|
631
|
+
else if (_bpinternal_zui.z.is.zuiNumber(valueType)) directType = "number";
|
|
632
|
+
else if (_bpinternal_zui.z.is.zuiBoolean(valueType)) directType = "boolean";
|
|
633
|
+
else if (_bpinternal_zui.z.is.zuiArray(valueType)) directType = "array";
|
|
634
|
+
}
|
|
635
|
+
const description = [this.description, directType && `Pass the ${directType} itself as the argument, not an object containing it.`].filter(Boolean).join("\n");
|
|
636
|
+
const fnType = _bpinternal_zui.z.function(input, _bpinternal_zui.z.promise(output)).title(this.name).describe(description);
|
|
585
637
|
return require_typings.getTypings(fnType, { declaration: true });
|
|
586
638
|
}
|
|
587
639
|
/**
|
|
@@ -625,7 +677,7 @@ var Tool = class Tool {
|
|
|
625
677
|
});
|
|
626
678
|
};
|
|
627
679
|
/**
|
|
628
|
-
*
|
|
680
|
+
* Serializes model-facing descriptions, not JavaScript validators. Use clone() to retain schema effects.
|
|
629
681
|
*
|
|
630
682
|
* @returns JSON representation of the Tool instance
|
|
631
683
|
*/
|
package/dist/tool.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { TypeOf, z, ZodType } from '@bpinternal/zui';
|
|
2
2
|
import { JSONSchema7 } from 'json-schema';
|
|
3
|
-
import {
|
|
4
|
-
import { RenderedComponent } from './component.js';
|
|
3
|
+
import { type TruncationPolicy } from './truncate.js';
|
|
5
4
|
import { Serializable } from './types.js';
|
|
6
5
|
/**
|
|
7
6
|
* Input parameters passed to tool retry functions.
|
|
@@ -49,8 +48,14 @@ type SmartPartial<T> = IsObject<T> extends true ? Partial<T> : T;
|
|
|
49
48
|
type ToolCallContext = {
|
|
50
49
|
/** Unique identifier for this specific tool call */
|
|
51
50
|
callId: string;
|
|
52
|
-
/**
|
|
51
|
+
/** Owning iteration, when run by LLMz. */
|
|
53
52
|
iterationId?: string;
|
|
53
|
+
/** Native run_javascript call owning this invocation, when run by LLMz. */
|
|
54
|
+
nativeCallId?: string;
|
|
55
|
+
/** @internal Report the parsed argument without evaluating its effects again for traces. */
|
|
56
|
+
onInput?: (input: unknown) => void;
|
|
57
|
+
/** @internal Capture display policy while keeping the returned value ordinary JavaScript data. */
|
|
58
|
+
onTruncation?: (value: unknown, policy: TruncationPolicy) => void;
|
|
54
59
|
};
|
|
55
60
|
export declare namespace Tool {
|
|
56
61
|
type JSON = {
|
|
@@ -73,7 +78,7 @@ export declare namespace Tool {
|
|
|
73
78
|
*
|
|
74
79
|
* ## Key Features
|
|
75
80
|
* - **Type Safety**: Full TypeScript inference with Zui/Zod schema validation
|
|
76
|
-
* - **Input
|
|
81
|
+
* - **Input Validation**: Parse inputs through their original schema; output schemas are documentation only
|
|
77
82
|
* - **Retry Logic**: Built-in retry mechanisms with custom retry functions
|
|
78
83
|
* - **Static Values**: Pre-configure parameters that won't change between calls
|
|
79
84
|
* - **Tool Cloning**: Create variations of existing tools with modified behavior
|
|
@@ -200,7 +205,7 @@ export declare namespace Tool {
|
|
|
200
205
|
*
|
|
201
206
|
* Tools automatically handle:
|
|
202
207
|
* - **Input validation**: Invalid inputs throw descriptive errors
|
|
203
|
-
* - **Output
|
|
208
|
+
* - **Output hints**: Schemas describe likely outputs; results are never parsed or rejected
|
|
204
209
|
* - **Handler errors**: Exceptions in handlers are caught and can trigger retries
|
|
205
210
|
* - **Type coercion**: Basic type coercion where possible
|
|
206
211
|
*
|
|
@@ -211,7 +216,9 @@ export declare class Tool<I extends z.ZodType = z.ZodType, O extends z.ZodType =
|
|
|
211
216
|
aliases: string[];
|
|
212
217
|
description?: string;
|
|
213
218
|
metadata: Record<string, unknown>;
|
|
219
|
+
/** Model-facing input description; zInput retains the original validator. */
|
|
214
220
|
input?: JSONSchema7;
|
|
221
|
+
/** Expected raw output shape, used for documentation only. */
|
|
215
222
|
output?: JSONSchema7;
|
|
216
223
|
retry?: ToolRetryFn<TypeOf<I>>;
|
|
217
224
|
MAX_RETRIES: number;
|
|
@@ -256,7 +263,7 @@ export declare class Tool<I extends z.ZodType = z.ZodType, O extends z.ZodType =
|
|
|
256
263
|
* const unrestricted = restrictedTool.setStaticInputValues(null)
|
|
257
264
|
* ```
|
|
258
265
|
*/
|
|
259
|
-
setStaticInputValues(values: SmartPartial<
|
|
266
|
+
setStaticInputValues(values: SmartPartial<z.input<I>>): this;
|
|
260
267
|
/**
|
|
261
268
|
* Gets the computed input schema with static values applied.
|
|
262
269
|
*
|
|
@@ -268,9 +275,9 @@ export declare class Tool<I extends z.ZodType = z.ZodType, O extends z.ZodType =
|
|
|
268
275
|
*/
|
|
269
276
|
get zInput(): ZodType<any, z.ZodTypeDef, any>;
|
|
270
277
|
/**
|
|
271
|
-
* Gets the output schema
|
|
278
|
+
* Gets the output description as a promise schema. It is never used to parse tool results.
|
|
272
279
|
*
|
|
273
|
-
* @returns The Zui schema
|
|
280
|
+
* @returns The Zui schema used to describe likely output
|
|
274
281
|
* @internal
|
|
275
282
|
*/
|
|
276
283
|
get zOutput(): z.ZodPromise<ZodType<any, z.ZodTypeDef, any>>;
|
|
@@ -352,10 +359,12 @@ export declare class Tool<I extends z.ZodType = z.ZodType, O extends z.ZodType =
|
|
|
352
359
|
metadata?: Record<string, unknown>;
|
|
353
360
|
input: IX | ((original: I | undefined) => IX);
|
|
354
361
|
output: OX | ((original: O | undefined) => OX);
|
|
355
|
-
staticInputValues?: SmartPartial<
|
|
356
|
-
handler: (args: TypeOf<IX>, ctx: ToolCallContext) =>
|
|
362
|
+
staticInputValues?: SmartPartial<z.input<IX>>;
|
|
363
|
+
handler: (args: TypeOf<IX>, ctx: ToolCallContext) => Promise<unknown>;
|
|
357
364
|
retry: ToolRetryFn<TypeOf<IX>>;
|
|
358
365
|
}>): Tool<IX, OX>;
|
|
366
|
+
private _inputSchema?;
|
|
367
|
+
private _outputSchema?;
|
|
359
368
|
private _handler;
|
|
360
369
|
/**
|
|
361
370
|
* Creates a new Tool instance.
|
|
@@ -364,7 +373,7 @@ export declare class Tool<I extends z.ZodType = z.ZodType, O extends z.ZodType =
|
|
|
364
373
|
* @param props.name - Unique tool name (must be valid TypeScript identifier)
|
|
365
374
|
* @param props.description - Human-readable description for the LLM
|
|
366
375
|
* @param props.input - Zui/Zod schema for input validation (optional)
|
|
367
|
-
* @param props.output -
|
|
376
|
+
* @param props.output - Schema describing likely raw output; never used for validation or normalization
|
|
368
377
|
* @param props.handler - Async function that implements the tool logic
|
|
369
378
|
* @param props.aliases - Alternative names for the tool (optional)
|
|
370
379
|
* @param props.metadata - Additional metadata for the tool (optional)
|
|
@@ -440,15 +449,14 @@ export declare class Tool<I extends z.ZodType = z.ZodType, O extends z.ZodType =
|
|
|
440
449
|
metadata?: Record<string, unknown>;
|
|
441
450
|
input?: I;
|
|
442
451
|
output?: O;
|
|
443
|
-
staticInputValues?:
|
|
444
|
-
handler: (args: TypeOf<I>, ctx: ToolCallContext) =>
|
|
452
|
+
staticInputValues?: SmartPartial<z.input<I>>;
|
|
453
|
+
handler: (args: TypeOf<I>, ctx: ToolCallContext) => Promise<unknown>;
|
|
445
454
|
retry?: ToolRetryFn<TypeOf<I>>;
|
|
446
455
|
});
|
|
447
|
-
private _executeHandler;
|
|
448
456
|
/**
|
|
449
457
|
* Executes the tool with the given input and context.
|
|
450
458
|
*
|
|
451
|
-
* This method handles
|
|
459
|
+
* This method parses inputs and handles retries. Output schemas are hints: results pass through unchanged.
|
|
452
460
|
* It's called internally by the LLMz execution engine when generated code calls the tool.
|
|
453
461
|
*
|
|
454
462
|
* @param input - Input data to pass to the tool handler
|
|
@@ -469,7 +477,7 @@ export declare class Tool<I extends z.ZodType = z.ZodType, O extends z.ZodType =
|
|
|
469
477
|
*
|
|
470
478
|
* @internal This method is primarily used internally by the LLMz execution engine
|
|
471
479
|
*/
|
|
472
|
-
execute(rawInput:
|
|
480
|
+
execute(rawInput: z.input<I>, ctx: ToolCallContext): Promise<z.input<O>>;
|
|
473
481
|
/**
|
|
474
482
|
* Generates TypeScript type definitions for this tool.
|
|
475
483
|
*
|
|
@@ -511,7 +519,7 @@ export declare class Tool<I extends z.ZodType = z.ZodType, O extends z.ZodType =
|
|
|
511
519
|
*/
|
|
512
520
|
static withUniqueNames: (tools: Tool<any, any>[]) => Tool<any, any>[];
|
|
513
521
|
/**
|
|
514
|
-
*
|
|
522
|
+
* Serializes model-facing descriptions, not JavaScript validators. Use clone() to retain schema effects.
|
|
515
523
|
*
|
|
516
524
|
* @returns JSON representation of the Tool instance
|
|
517
525
|
*/
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { b as InvalidConfigurationError } from "./errors-Crk5P8jo.js";
|
|
2
|
+
//#region src/truncate.ts
|
|
3
|
+
const TRUNCATE_SIGNATURE = "llmz.truncate.v1";
|
|
4
|
+
const DEFAULT_TOOL_RESULT_MAX_TOKENS = 2e3;
|
|
5
|
+
/** Sets a display budget without truncating or changing the underlying value. */
|
|
6
|
+
function truncate({ value, maxTokens, preserve = "top" }) {
|
|
7
|
+
if (!Number.isSafeInteger(maxTokens) || maxTokens < 0) throw new InvalidConfigurationError("maxTokens must be a finite nonnegative integer");
|
|
8
|
+
if (preserve !== "top" && preserve !== "bottom" && preserve !== "both") throw new InvalidConfigurationError("preserve must be \"top\", \"bottom\", or \"both\"");
|
|
9
|
+
return Object.freeze({
|
|
10
|
+
$$truncate: Object.freeze({
|
|
11
|
+
signature: TRUNCATE_SIGNATURE,
|
|
12
|
+
maxTokens,
|
|
13
|
+
preserve
|
|
14
|
+
}),
|
|
15
|
+
value
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
function ownValue(value, key) {
|
|
19
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
20
|
+
return descriptor && "value" in descriptor ? descriptor.value : void 0;
|
|
21
|
+
}
|
|
22
|
+
function isTruncated(value) {
|
|
23
|
+
if (!value || typeof value !== "object") return false;
|
|
24
|
+
const policy = ownValue(value, "$$truncate");
|
|
25
|
+
if (!policy || typeof policy !== "object" || !Object.hasOwn(value, "value")) return false;
|
|
26
|
+
const signature = ownValue(policy, "signature");
|
|
27
|
+
const maxTokens = ownValue(policy, "maxTokens");
|
|
28
|
+
const preserve = ownValue(policy, "preserve");
|
|
29
|
+
const valueDescriptor = Object.getOwnPropertyDescriptor(value, "value");
|
|
30
|
+
return signature === TRUNCATE_SIGNATURE && typeof maxTokens === "number" && Number.isSafeInteger(maxTokens) && maxTokens >= 0 && (preserve === "top" || preserve === "bottom" || preserve === "both") && !!valueDescriptor && "value" in valueDescriptor;
|
|
31
|
+
}
|
|
32
|
+
function isContainer(value) {
|
|
33
|
+
if (!value || typeof value !== "object") return false;
|
|
34
|
+
const prototype = Object.getPrototypeOf(value);
|
|
35
|
+
return Array.isArray(value) || prototype === Object.prototype || prototype === null;
|
|
36
|
+
}
|
|
37
|
+
function unwrapTruncated(value) {
|
|
38
|
+
let underlying = value;
|
|
39
|
+
const rootWrappers = /* @__PURE__ */ new WeakSet();
|
|
40
|
+
while (isTruncated(underlying)) {
|
|
41
|
+
if (rootWrappers.has(underlying)) throw new InvalidConfigurationError("Circular truncation wrappers cannot be unwrapped");
|
|
42
|
+
rootWrappers.add(underlying);
|
|
43
|
+
underlying = underlying.value;
|
|
44
|
+
}
|
|
45
|
+
const visited = /* @__PURE__ */ new WeakSet();
|
|
46
|
+
function containsWrapper(current) {
|
|
47
|
+
if (isTruncated(current)) return true;
|
|
48
|
+
if (!isContainer(current) || visited.has(current)) return false;
|
|
49
|
+
visited.add(current);
|
|
50
|
+
for (const key of Reflect.ownKeys(current)) if (containsWrapper(ownValue(current, key))) return true;
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
if (!containsWrapper(underlying)) return underlying;
|
|
54
|
+
const copies = /* @__PURE__ */ new WeakMap();
|
|
55
|
+
const wrappers = /* @__PURE__ */ new WeakSet();
|
|
56
|
+
function unwrap(current) {
|
|
57
|
+
if (current && typeof current === "object" && copies.has(current)) return copies.get(current);
|
|
58
|
+
if (isTruncated(current)) {
|
|
59
|
+
if (wrappers.has(current)) throw new InvalidConfigurationError("Circular truncation wrappers cannot be unwrapped");
|
|
60
|
+
wrappers.add(current);
|
|
61
|
+
const result = unwrap(current.value);
|
|
62
|
+
wrappers.delete(current);
|
|
63
|
+
copies.set(current, result);
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
if (!isContainer(current)) return current;
|
|
67
|
+
const copy = Array.isArray(current) ? new Array(current.length) : Object.create(Object.getPrototypeOf(current));
|
|
68
|
+
copies.set(current, copy);
|
|
69
|
+
for (const key of Reflect.ownKeys(current)) {
|
|
70
|
+
const descriptor = Object.getOwnPropertyDescriptor(current, key);
|
|
71
|
+
if ("value" in descriptor) descriptor.value = unwrap(descriptor.value);
|
|
72
|
+
Object.defineProperty(copy, key, descriptor);
|
|
73
|
+
}
|
|
74
|
+
return copy;
|
|
75
|
+
}
|
|
76
|
+
return unwrap(underlying);
|
|
77
|
+
}
|
|
78
|
+
//#endregion
|
|
79
|
+
export { unwrapTruncated as i, isTruncated as n, truncate as r, DEFAULT_TOOL_RESULT_MAX_TOKENS as t };
|