llmz 0.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/README.md +41 -0
- package/dist/chunk-3CGLDS5T.cjs +3059 -0
- package/dist/chunk-3T465BEW.js +96 -0
- package/dist/chunk-6QDFMQHA.js +98 -0
- package/dist/chunk-7FIPXMZR.js +33 -0
- package/dist/chunk-7S3UY52U.js +3059 -0
- package/dist/chunk-A7BDFLAE.cjs +30 -0
- package/dist/chunk-BKGPO722.cjs +3079 -0
- package/dist/chunk-BOLDQGES.cjs +33 -0
- package/dist/chunk-C2TSWGYC.cjs +91 -0
- package/dist/chunk-DFZ6GX5C.js +220 -0
- package/dist/chunk-DILHJIMP.js +180 -0
- package/dist/chunk-E2B5DRIC.cjs +419 -0
- package/dist/chunk-E5W3P7EO.cjs +96 -0
- package/dist/chunk-ENFB45AI.cjs +220 -0
- package/dist/chunk-MSTBEKOQ.js +28 -0
- package/dist/chunk-PQN6HW7A.cjs +28 -0
- package/dist/chunk-Q4DEJGXD.js +344 -0
- package/dist/chunk-QPSUMPLP.js +419 -0
- package/dist/chunk-QZGFCB4T.cjs +344 -0
- package/dist/chunk-R2N2UUKC.cjs +98 -0
- package/dist/chunk-RB2L33ZR.js +3079 -0
- package/dist/chunk-RJHDQL4C.js +161 -0
- package/dist/chunk-UG2GCPHF.js +91 -0
- package/dist/chunk-UVSOAH2H.js +152 -0
- package/dist/chunk-VP6WWAE2.cjs +152 -0
- package/dist/chunk-YPOOKE3A.cjs +180 -0
- package/dist/chunk-YSQDPG26.js +30 -0
- package/dist/chunk-YUDUY7XW.cjs +284 -0
- package/dist/chunk-ZSRC3TN4.js +284 -0
- package/dist/chunk-ZUAFUOUR.cjs +161 -0
- package/dist/compiler/compiler.d.ts +28 -0
- package/dist/compiler/index.d.ts +2 -0
- package/dist/compiler/plugins/async-iterator.d.ts +4 -0
- package/dist/compiler/plugins/braces-tsx.d.ts +3 -0
- package/dist/compiler/plugins/jsx-preserve-newlines.d.ts +5 -0
- package/dist/compiler/plugins/line-tracking.d.ts +5 -0
- package/dist/compiler/plugins/replace-comment.d.ts +5 -0
- package/dist/compiler/plugins/return-async.d.ts +4 -0
- package/dist/compiler/plugins/track-tool-calls.d.ts +16 -0
- package/dist/compiler/plugins/variable-extraction.d.ts +5 -0
- package/dist/component-DWBRHLEN.cjs +9 -0
- package/dist/component-R3ZAVXUC.js +9 -0
- package/dist/component.d.ts +51 -0
- package/dist/component.default.d.ts +10 -0
- package/dist/context.d.ts +167 -0
- package/dist/dual-modes-2VDEOJ5D.js +13 -0
- package/dist/dual-modes-3MH5N6RY.cjs +13 -0
- package/dist/errors.d.ts +64 -0
- package/dist/exit-GYMXZZ4I.js +9 -0
- package/dist/exit-PQHMJEOG.cjs +9 -0
- package/dist/exit.d.ts +20 -0
- package/dist/formatting.d.ts +5 -0
- package/dist/getter.d.ts +2 -0
- package/dist/handlers.d.ts +10 -0
- package/dist/hoist.d.ts +2 -0
- package/dist/index.cjs +811 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +811 -0
- package/dist/inspect.d.ts +6 -0
- package/dist/jsx-CJ2RBODH.js +14 -0
- package/dist/jsx-PPLE5YTB.cjs +14 -0
- package/dist/jsx.d.ts +11 -0
- package/dist/llmz-EGTGPSTX.cjs +1049 -0
- package/dist/llmz-WY74UVE3.js +1049 -0
- package/dist/llmz.d.ts +41 -0
- package/dist/objects.d.ts +24 -0
- package/dist/openai.d.ts +8 -0
- package/dist/prompts/chat-mode/system.md.d.ts +2 -0
- package/dist/prompts/chat-mode/user.md.d.ts +2 -0
- package/dist/prompts/common.d.ts +6 -0
- package/dist/prompts/dual-modes.d.ts +2 -0
- package/dist/prompts/prompt.d.ts +50 -0
- package/dist/prompts/worker-mode/system.md.d.ts +2 -0
- package/dist/prompts/worker-mode/user.md.d.ts +2 -0
- package/dist/snapshots.d.ts +61 -0
- package/dist/stack-traces.d.ts +1 -0
- package/dist/tool-AY47ML65.cjs +12 -0
- package/dist/tool-WVIPESKN.js +12 -0
- package/dist/tool.d.ts +30 -0
- package/dist/transcript.d.ts +9 -0
- package/dist/truncator-MTONQESU.js +11 -0
- package/dist/truncator-ZKPOW2I3.cjs +11 -0
- package/dist/truncator.d.ts +21 -0
- package/dist/types.d.ts +131 -0
- package/dist/typings-HTYLRNIC.js +11 -0
- package/dist/typings-QRJGGNUM.cjs +11 -0
- package/dist/typings.d.ts +5 -0
- package/dist/utils-TS23YJPE.js +38 -0
- package/dist/utils-UR4IMUHC.cjs +38 -0
- package/dist/utils.d.ts +21 -0
- package/dist/vm-4JFSZAMR.cjs +13 -0
- package/dist/vm-SMMA664M.js +13 -0
- package/dist/vm.d.ts +3 -0
- package/package.json +85 -0
- package/patches/source-map-js@1.2.1.patch +16 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
var _chunk3CGLDS5Tcjs = require('./chunk-3CGLDS5T.cjs');
|
|
5
|
+
|
|
6
|
+
// src/jsx.ts
|
|
7
|
+
var _zui = require('@bpinternal/zui');
|
|
8
|
+
function isAnyJsxComponent(component) {
|
|
9
|
+
return typeof component === "object" && component !== null && "__jsx" in component && component.__jsx === true && typeof component.type === "string";
|
|
10
|
+
}
|
|
11
|
+
function isJsxComponent(type, component) {
|
|
12
|
+
return typeof component === "object" && component !== null && "__jsx" in component && component.__jsx === true && typeof component.type === "string" && typeof type === "string" && component.type.toUpperCase().trim() === type.toUpperCase().trim();
|
|
13
|
+
}
|
|
14
|
+
var createJsxComponent = (props) => ({
|
|
15
|
+
__jsx: true,
|
|
16
|
+
type: _chunk3CGLDS5Tcjs.isString_default.call(void 0, props.type) ? props.type.toUpperCase() : "__unknown__",
|
|
17
|
+
children: Array.isArray(props.children) ? props.children : props.children ? [props.children] : [],
|
|
18
|
+
props: _chunk3CGLDS5Tcjs.isPlainObject_default.call(void 0, props.props) ? props.props : {}
|
|
19
|
+
});
|
|
20
|
+
var Jsx = _zui.z.custom(
|
|
21
|
+
(value) => isAnyJsxComponent(value),
|
|
22
|
+
(value) => ({
|
|
23
|
+
params: { value },
|
|
24
|
+
message: `Invalid JSX component: ${value}`
|
|
25
|
+
})
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
exports.isAnyJsxComponent = isAnyJsxComponent; exports.isJsxComponent = isJsxComponent; exports.createJsxComponent = createJsxComponent; exports.Jsx = Jsx;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
var _chunkQZGFCB4Tcjs = require('./chunk-QZGFCB4T.cjs');
|
|
5
|
+
|
|
6
|
+
// src/snapshots.ts
|
|
7
|
+
var _ulid = require('ulid');
|
|
8
|
+
var MAX_SNAPSHOT_SIZE_BYTES = 4e3;
|
|
9
|
+
var Snapshot = class _Snapshot {
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
#status;
|
|
16
|
+
get status() {
|
|
17
|
+
return Object.freeze({ ...this.#status });
|
|
18
|
+
}
|
|
19
|
+
constructor(props) {
|
|
20
|
+
this.id = props.id;
|
|
21
|
+
this.stack = props.stack;
|
|
22
|
+
this.reason = props.reason;
|
|
23
|
+
this.variables = props.variables;
|
|
24
|
+
this.toolCall = props.toolCall;
|
|
25
|
+
this.#status = props.status;
|
|
26
|
+
}
|
|
27
|
+
static fromSignal(signal) {
|
|
28
|
+
const variables = Object.entries(signal.variables).map(([name, value]) => {
|
|
29
|
+
const type = _chunkQZGFCB4Tcjs.extractType.call(void 0, value);
|
|
30
|
+
const bytes = JSON.stringify(value || "").length;
|
|
31
|
+
const truncated = bytes > MAX_SNAPSHOT_SIZE_BYTES;
|
|
32
|
+
return truncated ? { name, type, bytes, truncated: true, preview: _nullishCoalesce(_chunkQZGFCB4Tcjs.inspect.call(void 0, value, name), () => ( "N/A")) } : { name, type, bytes, truncated: false, value };
|
|
33
|
+
});
|
|
34
|
+
return new _Snapshot({
|
|
35
|
+
id: "snapshot_" + _ulid.ulid.call(void 0, ),
|
|
36
|
+
reason: signal.message,
|
|
37
|
+
stack: signal.truncatedCode,
|
|
38
|
+
variables,
|
|
39
|
+
toolCall: signal.toolCall,
|
|
40
|
+
status: { type: "pending" }
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
toJSON() {
|
|
44
|
+
return {
|
|
45
|
+
id: this.id,
|
|
46
|
+
reason: this.reason,
|
|
47
|
+
stack: this.stack,
|
|
48
|
+
variables: this.variables,
|
|
49
|
+
toolCall: this.toolCall
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
static fromJSON(json) {
|
|
53
|
+
return new _Snapshot({
|
|
54
|
+
id: json.id,
|
|
55
|
+
reason: json.reason,
|
|
56
|
+
stack: json.stack,
|
|
57
|
+
variables: json.variables,
|
|
58
|
+
toolCall: json.toolCall,
|
|
59
|
+
status: json.status
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
clone() {
|
|
63
|
+
return new _Snapshot({
|
|
64
|
+
id: this.id,
|
|
65
|
+
reason: this.reason,
|
|
66
|
+
stack: this.stack,
|
|
67
|
+
variables: this.variables,
|
|
68
|
+
toolCall: this.toolCall,
|
|
69
|
+
status: this.#status
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
reset() {
|
|
73
|
+
this.#status = { type: "pending" };
|
|
74
|
+
}
|
|
75
|
+
resolve(value) {
|
|
76
|
+
if (this.#status.type !== "pending") {
|
|
77
|
+
throw new Error(`Cannot resolve snapshot because it is already settled: ${this.#status.type}`);
|
|
78
|
+
}
|
|
79
|
+
this.#status = { type: "resolved", value };
|
|
80
|
+
}
|
|
81
|
+
reject(error) {
|
|
82
|
+
if (this.#status.type !== "pending") {
|
|
83
|
+
throw new Error(`Cannot reject snapshot because it is already settled: ${this.#status.type}`);
|
|
84
|
+
}
|
|
85
|
+
this.#status = { type: "rejected", error };
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
exports.Snapshot = Snapshot;
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import {
|
|
2
|
+
camelCase_default,
|
|
3
|
+
deburr_default,
|
|
4
|
+
isPlainObject_default,
|
|
5
|
+
pickBy_default,
|
|
6
|
+
startCase_default
|
|
7
|
+
} from "./chunk-7S3UY52U.js";
|
|
8
|
+
|
|
9
|
+
// src/utils.ts
|
|
10
|
+
import { getWasmTokenizer } from "@bpinternal/thicktoken";
|
|
11
|
+
import { z } from "@bpinternal/zui";
|
|
12
|
+
var tokenizer = null;
|
|
13
|
+
var init = async () => {
|
|
14
|
+
if (tokenizer) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
while (typeof getWasmTokenizer !== "function") {
|
|
18
|
+
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
19
|
+
}
|
|
20
|
+
tokenizer = await getWasmTokenizer();
|
|
21
|
+
};
|
|
22
|
+
var getTokenizer = () => {
|
|
23
|
+
if (!tokenizer) {
|
|
24
|
+
throw new Error("Tokenizer not initialized, make sure to call init() first and await it");
|
|
25
|
+
}
|
|
26
|
+
return tokenizer;
|
|
27
|
+
};
|
|
28
|
+
var Tokens = (min, max) => {
|
|
29
|
+
return z.string().superRefine((value, ctx) => {
|
|
30
|
+
const tokens = tokenizer.count(value);
|
|
31
|
+
if (value.length < min) {
|
|
32
|
+
ctx.addIssue({
|
|
33
|
+
code: "too_small",
|
|
34
|
+
minimum: min,
|
|
35
|
+
type: "string",
|
|
36
|
+
message: `Text is ${tokens} but expected at least ${min} tokens`,
|
|
37
|
+
inclusive: true
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
if (value.length > max) {
|
|
41
|
+
ctx.addIssue({
|
|
42
|
+
code: "too_big",
|
|
43
|
+
maximum: max,
|
|
44
|
+
type: "string",
|
|
45
|
+
message: `Text is ${tokens} tokens but expected at most ${max} tokens`,
|
|
46
|
+
inclusive: true
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
return value;
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
var Identifier = z.string().min(1).max(100).regex(
|
|
53
|
+
/^([a-zA-Z_][a-zA-Z0-9_]*)$/,
|
|
54
|
+
"Identifiers must start with a letter and can only contain letters, numbers and underscores"
|
|
55
|
+
);
|
|
56
|
+
function escapeString(str) {
|
|
57
|
+
if (typeof str !== "string") {
|
|
58
|
+
return "";
|
|
59
|
+
}
|
|
60
|
+
const rawStr = String.raw`${str}`;
|
|
61
|
+
const escapedStr = rawStr.replace(/\\/g, "\\\\").replace(/\n/g, "\\n").replace(/"/g, '\\"').replace(/'/g, "\\'");
|
|
62
|
+
if (escapedStr.includes("`")) {
|
|
63
|
+
return `"${escapedStr}"`;
|
|
64
|
+
} else if (escapedStr.includes("'")) {
|
|
65
|
+
return `"${escapedStr}"`;
|
|
66
|
+
} else {
|
|
67
|
+
return `'${escapedStr}'`;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
var toPropertyKey = (key) => {
|
|
71
|
+
if (/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(key)) {
|
|
72
|
+
return key;
|
|
73
|
+
}
|
|
74
|
+
return escapeString(key);
|
|
75
|
+
};
|
|
76
|
+
var trimEmptyLines = (lines) => {
|
|
77
|
+
var _a;
|
|
78
|
+
while (lines.length && !((_a = lines[0]) == null ? void 0 : _a.trim())) {
|
|
79
|
+
lines.shift();
|
|
80
|
+
}
|
|
81
|
+
while (lines.length && !lines[lines.length - 1].trim()) {
|
|
82
|
+
lines.pop();
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
var getMultilineComment = (description) => {
|
|
86
|
+
description = description == null ? void 0 : description.replace(/(\n(\s*)?){3,}/g, "\n\n");
|
|
87
|
+
const ensureLineStartsWithAsterisk = (line) => line.startsWith("* ") ? ` ${line}` : ` * ${line}`;
|
|
88
|
+
const escapeCommentEnd = (line) => line.replace(/\*\//g, "*\\/");
|
|
89
|
+
const descLines = (description ?? "").split("\n").map((line) => line.trim());
|
|
90
|
+
trimEmptyLines(descLines);
|
|
91
|
+
if (descLines.length) {
|
|
92
|
+
descLines[0] = descLines[0].replace(/^\/\*\*?/, "");
|
|
93
|
+
descLines[descLines.length - 1] = descLines[descLines.length - 1].replace(/\*\/$/, "");
|
|
94
|
+
}
|
|
95
|
+
trimEmptyLines(descLines);
|
|
96
|
+
return descLines.length === 0 ? "" : descLines.length === 1 ? `/** ${escapeCommentEnd(descLines[0])} */` : `/**
|
|
97
|
+
${descLines.map(ensureLineStartsWithAsterisk).map(escapeCommentEnd).join("\n")}
|
|
98
|
+
*/`;
|
|
99
|
+
};
|
|
100
|
+
var toValidFunctionName = (str) => {
|
|
101
|
+
let name = deburr_default(str);
|
|
102
|
+
name = name.replace(/[^a-zA-Z0-9_$]/g, "");
|
|
103
|
+
if (!/^[a-zA-Z_$]/.test(name)) {
|
|
104
|
+
name = `_${name}`;
|
|
105
|
+
}
|
|
106
|
+
return camelCase_default(name);
|
|
107
|
+
};
|
|
108
|
+
var awaitObject = async (obj) => {
|
|
109
|
+
const newObj = { ...obj };
|
|
110
|
+
const promises = [];
|
|
111
|
+
const setProp = async (key, value) => {
|
|
112
|
+
newObj[key] = await value;
|
|
113
|
+
};
|
|
114
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
115
|
+
if (value instanceof Promise) {
|
|
116
|
+
promises.push(setProp(key, value));
|
|
117
|
+
} else if (isPlainObject_default(value)) {
|
|
118
|
+
promises.push(setProp(key, await awaitObject(value)));
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
await Promise.all(promises);
|
|
122
|
+
return newObj;
|
|
123
|
+
};
|
|
124
|
+
var toValidObjectName = (str) => {
|
|
125
|
+
let name = deburr_default(str);
|
|
126
|
+
name = name.split(/[^a-zA-Z0-9_$]/g).map(startCase_default).join("");
|
|
127
|
+
if (!/^[a-zA-Z_$]/.test(name)) {
|
|
128
|
+
name = `_${name}`;
|
|
129
|
+
}
|
|
130
|
+
return name.replaceAll(" ", "");
|
|
131
|
+
};
|
|
132
|
+
var stripInvalidIdentifiers = (object) => {
|
|
133
|
+
if (typeof object !== "object") {
|
|
134
|
+
return object;
|
|
135
|
+
}
|
|
136
|
+
if (Array.isArray(object)) {
|
|
137
|
+
return object.map(stripInvalidIdentifiers);
|
|
138
|
+
}
|
|
139
|
+
return pickBy_default(object, (__, key) => Identifier.safeParse(key).success);
|
|
140
|
+
};
|
|
141
|
+
var convertObjectToZuiLiterals = (obj, nested = false) => {
|
|
142
|
+
if (typeof obj === "string") {
|
|
143
|
+
return z.literal(obj).catch(() => obj);
|
|
144
|
+
}
|
|
145
|
+
if (typeof obj === "number") {
|
|
146
|
+
return z.literal(obj).catch(() => obj);
|
|
147
|
+
}
|
|
148
|
+
if (typeof obj === "boolean") {
|
|
149
|
+
return z.literal(obj).catch(() => obj);
|
|
150
|
+
}
|
|
151
|
+
if (Array.isArray(obj)) {
|
|
152
|
+
if (obj.length === 0) {
|
|
153
|
+
return z.tuple([]).catch(() => obj);
|
|
154
|
+
}
|
|
155
|
+
return z.tuple([
|
|
156
|
+
// the tuple needs to have at least one element
|
|
157
|
+
convertObjectToZuiLiterals(obj[0]),
|
|
158
|
+
...obj.slice(1).map((child) => convertObjectToZuiLiterals(child))
|
|
159
|
+
]).catch(() => obj);
|
|
160
|
+
}
|
|
161
|
+
if (obj !== null && typeof obj === "object") {
|
|
162
|
+
const shape = {};
|
|
163
|
+
for (const key in obj) {
|
|
164
|
+
shape[key] = convertObjectToZuiLiterals(obj[key], true);
|
|
165
|
+
}
|
|
166
|
+
if (nested) {
|
|
167
|
+
return z.object(shape).catch(() => shape);
|
|
168
|
+
}
|
|
169
|
+
return shape;
|
|
170
|
+
}
|
|
171
|
+
if (obj === void 0) {
|
|
172
|
+
return z.undefined().catch(() => void 0);
|
|
173
|
+
}
|
|
174
|
+
if (obj === null) {
|
|
175
|
+
return z.null().catch(() => null);
|
|
176
|
+
}
|
|
177
|
+
throw new Error(`Unsupported object type ${typeof obj}, ${obj})`);
|
|
178
|
+
};
|
|
179
|
+
var isValidSchema = (schema) => {
|
|
180
|
+
try {
|
|
181
|
+
z.fromJsonSchema(schema);
|
|
182
|
+
return typeof schema.type === "string";
|
|
183
|
+
} catch {
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
function isJsonSchema(schema) {
|
|
188
|
+
return !!schema && typeof schema === "object" && ("$schema" in schema || "type" in schema || "properties" in schema);
|
|
189
|
+
}
|
|
190
|
+
function isValidIdentifier(name) {
|
|
191
|
+
if (typeof name !== "string") {
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
return /^[A-Z]{1,}[A-Z0-9_]{0,50}$/i.test(name);
|
|
195
|
+
}
|
|
196
|
+
function isValidMessageName(name) {
|
|
197
|
+
if (typeof name !== "string") {
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
return /^[A-Z]{1,50}$/i.test(name);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export {
|
|
204
|
+
init,
|
|
205
|
+
getTokenizer,
|
|
206
|
+
Tokens,
|
|
207
|
+
Identifier,
|
|
208
|
+
escapeString,
|
|
209
|
+
toPropertyKey,
|
|
210
|
+
getMultilineComment,
|
|
211
|
+
toValidFunctionName,
|
|
212
|
+
awaitObject,
|
|
213
|
+
toValidObjectName,
|
|
214
|
+
stripInvalidIdentifiers,
|
|
215
|
+
convertObjectToZuiLiterals,
|
|
216
|
+
isValidSchema,
|
|
217
|
+
isJsonSchema,
|
|
218
|
+
isValidIdentifier,
|
|
219
|
+
isValidMessageName
|
|
220
|
+
};
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getTypings
|
|
3
|
+
} from "./chunk-ZSRC3TN4.js";
|
|
4
|
+
import {
|
|
5
|
+
convertObjectToZuiLiterals,
|
|
6
|
+
isJsonSchema,
|
|
7
|
+
isValidIdentifier
|
|
8
|
+
} from "./chunk-DFZ6GX5C.js";
|
|
9
|
+
import {
|
|
10
|
+
isEmpty_default,
|
|
11
|
+
uniq_default
|
|
12
|
+
} from "./chunk-7S3UY52U.js";
|
|
13
|
+
|
|
14
|
+
// src/tool.ts
|
|
15
|
+
import { z } from "@bpinternal/zui";
|
|
16
|
+
var Tool = class _Tool {
|
|
17
|
+
_staticInputValues;
|
|
18
|
+
name;
|
|
19
|
+
aliases = [];
|
|
20
|
+
description;
|
|
21
|
+
metadata;
|
|
22
|
+
input;
|
|
23
|
+
output;
|
|
24
|
+
setStaticInputValues(values) {
|
|
25
|
+
if (values === null || values === void 0) {
|
|
26
|
+
this._staticInputValues = void 0;
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
const input = this.input ? z.fromJsonSchema(this.input) : z.any();
|
|
30
|
+
if (input instanceof z.ZodObject && typeof values !== "object") {
|
|
31
|
+
throw new Error(
|
|
32
|
+
`Invalid static input values for tool ${this.name}. Expected an object, but got type "${typeof values}"`
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
if (input instanceof z.ZodArray && !Array.isArray(values)) {
|
|
36
|
+
throw new Error(
|
|
37
|
+
`Invalid static input values for tool ${this.name}. Expected an array, but got type "${typeof values}"`
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
this._staticInputValues = values;
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
get zInput() {
|
|
44
|
+
let input = this.input ? z.fromJsonSchema(this.input) : z.any();
|
|
45
|
+
if (!isEmpty_default(this._staticInputValues)) {
|
|
46
|
+
const inputExtensions = convertObjectToZuiLiterals(this._staticInputValues);
|
|
47
|
+
if (input instanceof z.ZodObject) {
|
|
48
|
+
input = input.extend(inputExtensions);
|
|
49
|
+
} else if (input instanceof z.ZodArray) {
|
|
50
|
+
input = z.array(input.element.extend(inputExtensions));
|
|
51
|
+
} else {
|
|
52
|
+
input = inputExtensions;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return input;
|
|
56
|
+
}
|
|
57
|
+
get zOutput() {
|
|
58
|
+
return this.output ? z.fromJsonSchema(this.output) : z.void();
|
|
59
|
+
}
|
|
60
|
+
rename(name) {
|
|
61
|
+
const before = this.name;
|
|
62
|
+
if (!isValidIdentifier(name)) {
|
|
63
|
+
throw new Error(
|
|
64
|
+
`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.`
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
this.name = name;
|
|
68
|
+
this.aliases = uniq_default([name, ...this.aliases.map((alias) => alias === before ? name : alias)]);
|
|
69
|
+
return this;
|
|
70
|
+
}
|
|
71
|
+
clone() {
|
|
72
|
+
try {
|
|
73
|
+
return new _Tool({
|
|
74
|
+
name: this.name,
|
|
75
|
+
aliases: [...this.aliases],
|
|
76
|
+
description: this.description,
|
|
77
|
+
metadata: JSON.parse(JSON.stringify(this.metadata)),
|
|
78
|
+
input: this.input ? z.fromJsonSchema(this.input) : void 0,
|
|
79
|
+
output: this.output ? z.fromJsonSchema(this.output) : void 0,
|
|
80
|
+
handler: this._handler
|
|
81
|
+
}).setStaticInputValues(this._staticInputValues);
|
|
82
|
+
} catch (e) {
|
|
83
|
+
throw new Error(`Failed to clone tool "${this.name}": ${e}`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
_handler;
|
|
87
|
+
constructor(props) {
|
|
88
|
+
if (!isValidIdentifier(props.name)) {
|
|
89
|
+
throw new Error(
|
|
90
|
+
`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.`
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
if (props.description !== void 0 && typeof props.description !== "string") {
|
|
94
|
+
throw new Error(
|
|
95
|
+
`Invalid description for tool ${props.name}. Expected a string, but got type "${typeof props.description}"`
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
if (props.metadata !== void 0 && typeof props.metadata !== "object") {
|
|
99
|
+
throw new Error(
|
|
100
|
+
`Invalid metadata for tool ${props.name}. Expected an object, but got type "${typeof props.metadata}"`
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
if (typeof props.handler !== "function") {
|
|
104
|
+
throw new Error(
|
|
105
|
+
`Invalid handler for tool ${props.name}. Expected a function, but got type "${typeof props.handler}"`
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
if (props.aliases !== void 0 && !Array.isArray(props.aliases)) {
|
|
109
|
+
throw new Error(
|
|
110
|
+
`Invalid aliases for tool ${props.name}. Expected an array, but got type "${typeof props.aliases}"`
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
if (props.aliases && props.aliases.some((alias) => !isValidIdentifier(alias))) {
|
|
114
|
+
throw new Error(`Invalid aliases for tool ${props.name}. Expected an array of valid identifiers.`);
|
|
115
|
+
}
|
|
116
|
+
if (typeof props.input !== "undefined") {
|
|
117
|
+
if (props.input && "toJsonSchema" in props.input && typeof props.input.toJsonSchema === "function") {
|
|
118
|
+
this.input = props.input.toJsonSchema();
|
|
119
|
+
} else if (isJsonSchema(props.input)) {
|
|
120
|
+
this.input = props.input;
|
|
121
|
+
} else {
|
|
122
|
+
throw new Error(
|
|
123
|
+
`Invalid input schema for tool ${props.name}. Expected a ZodType or JSONSchema, but got type "${typeof props.input}"`
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (typeof props.output !== "undefined") {
|
|
128
|
+
if (props.output && "toJsonSchema" in props.output && typeof props.output.toJsonSchema === "function") {
|
|
129
|
+
this.output = props.output.toJsonSchema();
|
|
130
|
+
} else if (isJsonSchema(props.output)) {
|
|
131
|
+
this.output = props.output;
|
|
132
|
+
} else {
|
|
133
|
+
throw new Error(
|
|
134
|
+
`Invalid output schema for tool ${props.name}. Expected a ZodType or JSONSchema, but got type "${typeof props.output}"`
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
this.name = props.name;
|
|
139
|
+
this.aliases = uniq_default([props.name, ...props.aliases ?? []]);
|
|
140
|
+
this.description = props.description;
|
|
141
|
+
this.metadata = props.metadata ?? {};
|
|
142
|
+
this._handler = props.handler;
|
|
143
|
+
this.setStaticInputValues(props.staticInputValues);
|
|
144
|
+
}
|
|
145
|
+
async execute(input) {
|
|
146
|
+
const pInput = this.zInput.safeParse(input);
|
|
147
|
+
if (!pInput.success) {
|
|
148
|
+
throw new Error(`Tool "${this.name}" received invalid input: ${pInput.error.message}`);
|
|
149
|
+
}
|
|
150
|
+
const result = await this._handler(pInput.data);
|
|
151
|
+
const pOutput = this.zOutput.safeParse(result);
|
|
152
|
+
return pOutput.success ? pOutput.data : result;
|
|
153
|
+
}
|
|
154
|
+
async getTypings() {
|
|
155
|
+
const input = this.input ? z.fromJsonSchema(this.input) : void 0;
|
|
156
|
+
const output = this.output ? z.fromJsonSchema(this.output) : z.void();
|
|
157
|
+
const fnType = z.function(input, z.promise(output)).title(this.name).describe(this.description ?? "");
|
|
158
|
+
return getTypings(fnType, {
|
|
159
|
+
declaration: true
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
static withUniqueNames = (tools) => {
|
|
163
|
+
const names = /* @__PURE__ */ new Set();
|
|
164
|
+
return tools.map((tool) => {
|
|
165
|
+
if (tools.filter((t) => t.name === tool.name).length === 1) {
|
|
166
|
+
return tool;
|
|
167
|
+
}
|
|
168
|
+
let counter = 1;
|
|
169
|
+
let toolName = tool.name + counter;
|
|
170
|
+
while (names.has(toolName)) {
|
|
171
|
+
toolName = `${tool.name}${++counter}`;
|
|
172
|
+
}
|
|
173
|
+
return tool.rename(toolName);
|
|
174
|
+
});
|
|
175
|
+
};
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
export {
|
|
179
|
+
Tool
|
|
180
|
+
};
|