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.
Files changed (96) hide show
  1. package/README.md +41 -0
  2. package/dist/chunk-3CGLDS5T.cjs +3059 -0
  3. package/dist/chunk-3T465BEW.js +96 -0
  4. package/dist/chunk-6QDFMQHA.js +98 -0
  5. package/dist/chunk-7FIPXMZR.js +33 -0
  6. package/dist/chunk-7S3UY52U.js +3059 -0
  7. package/dist/chunk-A7BDFLAE.cjs +30 -0
  8. package/dist/chunk-BKGPO722.cjs +3079 -0
  9. package/dist/chunk-BOLDQGES.cjs +33 -0
  10. package/dist/chunk-C2TSWGYC.cjs +91 -0
  11. package/dist/chunk-DFZ6GX5C.js +220 -0
  12. package/dist/chunk-DILHJIMP.js +180 -0
  13. package/dist/chunk-E2B5DRIC.cjs +419 -0
  14. package/dist/chunk-E5W3P7EO.cjs +96 -0
  15. package/dist/chunk-ENFB45AI.cjs +220 -0
  16. package/dist/chunk-MSTBEKOQ.js +28 -0
  17. package/dist/chunk-PQN6HW7A.cjs +28 -0
  18. package/dist/chunk-Q4DEJGXD.js +344 -0
  19. package/dist/chunk-QPSUMPLP.js +419 -0
  20. package/dist/chunk-QZGFCB4T.cjs +344 -0
  21. package/dist/chunk-R2N2UUKC.cjs +98 -0
  22. package/dist/chunk-RB2L33ZR.js +3079 -0
  23. package/dist/chunk-RJHDQL4C.js +161 -0
  24. package/dist/chunk-UG2GCPHF.js +91 -0
  25. package/dist/chunk-UVSOAH2H.js +152 -0
  26. package/dist/chunk-VP6WWAE2.cjs +152 -0
  27. package/dist/chunk-YPOOKE3A.cjs +180 -0
  28. package/dist/chunk-YSQDPG26.js +30 -0
  29. package/dist/chunk-YUDUY7XW.cjs +284 -0
  30. package/dist/chunk-ZSRC3TN4.js +284 -0
  31. package/dist/chunk-ZUAFUOUR.cjs +161 -0
  32. package/dist/compiler/compiler.d.ts +28 -0
  33. package/dist/compiler/index.d.ts +2 -0
  34. package/dist/compiler/plugins/async-iterator.d.ts +4 -0
  35. package/dist/compiler/plugins/braces-tsx.d.ts +3 -0
  36. package/dist/compiler/plugins/jsx-preserve-newlines.d.ts +5 -0
  37. package/dist/compiler/plugins/line-tracking.d.ts +5 -0
  38. package/dist/compiler/plugins/replace-comment.d.ts +5 -0
  39. package/dist/compiler/plugins/return-async.d.ts +4 -0
  40. package/dist/compiler/plugins/track-tool-calls.d.ts +16 -0
  41. package/dist/compiler/plugins/variable-extraction.d.ts +5 -0
  42. package/dist/component-DWBRHLEN.cjs +9 -0
  43. package/dist/component-R3ZAVXUC.js +9 -0
  44. package/dist/component.d.ts +51 -0
  45. package/dist/component.default.d.ts +10 -0
  46. package/dist/context.d.ts +167 -0
  47. package/dist/dual-modes-2VDEOJ5D.js +13 -0
  48. package/dist/dual-modes-3MH5N6RY.cjs +13 -0
  49. package/dist/errors.d.ts +64 -0
  50. package/dist/exit-GYMXZZ4I.js +9 -0
  51. package/dist/exit-PQHMJEOG.cjs +9 -0
  52. package/dist/exit.d.ts +20 -0
  53. package/dist/formatting.d.ts +5 -0
  54. package/dist/getter.d.ts +2 -0
  55. package/dist/handlers.d.ts +10 -0
  56. package/dist/hoist.d.ts +2 -0
  57. package/dist/index.cjs +811 -0
  58. package/dist/index.d.ts +18 -0
  59. package/dist/index.js +811 -0
  60. package/dist/inspect.d.ts +6 -0
  61. package/dist/jsx-CJ2RBODH.js +14 -0
  62. package/dist/jsx-PPLE5YTB.cjs +14 -0
  63. package/dist/jsx.d.ts +11 -0
  64. package/dist/llmz-EGTGPSTX.cjs +1049 -0
  65. package/dist/llmz-WY74UVE3.js +1049 -0
  66. package/dist/llmz.d.ts +41 -0
  67. package/dist/objects.d.ts +24 -0
  68. package/dist/openai.d.ts +8 -0
  69. package/dist/prompts/chat-mode/system.md.d.ts +2 -0
  70. package/dist/prompts/chat-mode/user.md.d.ts +2 -0
  71. package/dist/prompts/common.d.ts +6 -0
  72. package/dist/prompts/dual-modes.d.ts +2 -0
  73. package/dist/prompts/prompt.d.ts +50 -0
  74. package/dist/prompts/worker-mode/system.md.d.ts +2 -0
  75. package/dist/prompts/worker-mode/user.md.d.ts +2 -0
  76. package/dist/snapshots.d.ts +61 -0
  77. package/dist/stack-traces.d.ts +1 -0
  78. package/dist/tool-AY47ML65.cjs +12 -0
  79. package/dist/tool-WVIPESKN.js +12 -0
  80. package/dist/tool.d.ts +30 -0
  81. package/dist/transcript.d.ts +9 -0
  82. package/dist/truncator-MTONQESU.js +11 -0
  83. package/dist/truncator-ZKPOW2I3.cjs +11 -0
  84. package/dist/truncator.d.ts +21 -0
  85. package/dist/types.d.ts +131 -0
  86. package/dist/typings-HTYLRNIC.js +11 -0
  87. package/dist/typings-QRJGGNUM.cjs +11 -0
  88. package/dist/typings.d.ts +5 -0
  89. package/dist/utils-TS23YJPE.js +38 -0
  90. package/dist/utils-UR4IMUHC.cjs +38 -0
  91. package/dist/utils.d.ts +21 -0
  92. package/dist/vm-4JFSZAMR.cjs +13 -0
  93. package/dist/vm-SMMA664M.js +13 -0
  94. package/dist/vm.d.ts +3 -0
  95. package/package.json +85 -0
  96. package/patches/source-map-js@1.2.1.patch +16 -0
@@ -0,0 +1,1049 @@
1
+ import {
2
+ runAsyncFunction
3
+ } from "./chunk-RB2L33ZR.js";
4
+ import "./chunk-7FIPXMZR.js";
5
+ import {
6
+ Snapshot
7
+ } from "./chunk-UG2GCPHF.js";
8
+ import {
9
+ Tool
10
+ } from "./chunk-DILHJIMP.js";
11
+ import "./chunk-ZSRC3TN4.js";
12
+ import {
13
+ AssignmentError,
14
+ CodeExecutionError,
15
+ InvalidCodeError,
16
+ LoopExceededError,
17
+ Signals,
18
+ SnapshotSignal,
19
+ ThinkSignal,
20
+ VMSignal
21
+ } from "./chunk-RJHDQL4C.js";
22
+ import {
23
+ Exit
24
+ } from "./chunk-6QDFMQHA.js";
25
+ import {
26
+ DualModePrompt
27
+ } from "./chunk-QPSUMPLP.js";
28
+ import "./chunk-Q4DEJGXD.js";
29
+ import {
30
+ cleanStackTrace
31
+ } from "./chunk-MSTBEKOQ.js";
32
+ import {
33
+ assertValidComponent
34
+ } from "./chunk-3T465BEW.js";
35
+ import {
36
+ truncateWrappedContent,
37
+ wrapContent
38
+ } from "./chunk-UVSOAH2H.js";
39
+ import {
40
+ init,
41
+ stripInvalidIdentifiers
42
+ } from "./chunk-DFZ6GX5C.js";
43
+ import {
44
+ clamp_default,
45
+ cloneDeep_default,
46
+ isEqual_default,
47
+ isPlainObject_default,
48
+ omit_default
49
+ } from "./chunk-7S3UY52U.js";
50
+ import "./chunk-YSQDPG26.js";
51
+
52
+ // src/llmz.ts
53
+ import { Cognitive } from "@botpress/cognitive";
54
+ import { z } from "@bpinternal/zui";
55
+ import ms from "ms";
56
+
57
+ // src/context.ts
58
+ import { ulid } from "ulid";
59
+
60
+ // src/getter.ts
61
+ var getValue = async (valueOrGetter, ctx) => {
62
+ if (typeof valueOrGetter === "function") {
63
+ try {
64
+ return await valueOrGetter(ctx);
65
+ } catch (e) {
66
+ throw new Error(`Error while getting value for ${valueOrGetter}: ${e}`);
67
+ }
68
+ } else {
69
+ return valueOrGetter;
70
+ }
71
+ };
72
+
73
+ // src/handlers.ts
74
+ var Emitter = class {
75
+ handlers = [];
76
+ subscribe = (fn) => {
77
+ this.handlers.push(fn);
78
+ return () => {
79
+ this.handlers = this.handlers.filter((handler) => handler !== fn);
80
+ };
81
+ };
82
+ emit(event) {
83
+ this.handlers.forEach((handler) => handler(event));
84
+ }
85
+ };
86
+ var HookedArray = class extends Array {
87
+ #listeners = new Emitter();
88
+ constructor(...items) {
89
+ super(...items);
90
+ Object.setPrototypeOf(this, new.target.prototype);
91
+ }
92
+ push(...items) {
93
+ try {
94
+ this.#listeners.emit(items);
95
+ } finally {
96
+ return super.push(...items);
97
+ }
98
+ }
99
+ onPush(fn) {
100
+ return this.#listeners.subscribe(fn);
101
+ }
102
+ };
103
+
104
+ // src/transcript.ts
105
+ var MAX_MESSAGE_LENGTH = 5e3;
106
+ var TranscriptArray = class extends Array {
107
+ constructor(items = []) {
108
+ items = Array.isArray(items) ? items : [];
109
+ super(...items);
110
+ items.forEach((item) => {
111
+ if (!["user", "assistant"].includes(item.role)) {
112
+ throw new Error(`Invalid role "${item.role}" in transcript message`);
113
+ }
114
+ if (item.name && typeof item.name !== "string") {
115
+ throw new Error(`Invalid name for transcript message. Expected a string, but got type "${typeof item.name}"`);
116
+ }
117
+ if (typeof item.content !== "string") {
118
+ throw new Error(
119
+ `Invalid content for transcript message. Expected a string, but got type "${typeof item.content}"`
120
+ );
121
+ }
122
+ });
123
+ Object.setPrototypeOf(this, new.target.prototype);
124
+ }
125
+ toString() {
126
+ if (!this.length) {
127
+ return "";
128
+ }
129
+ return this.map((item, idx) => {
130
+ var _a;
131
+ const msgIdx = "message-" + String(idx + 1).padStart(3, "0");
132
+ const shouldTruncate = item.content.length > MAX_MESSAGE_LENGTH;
133
+ const preview = shouldTruncate ? item.content.slice(0, MAX_MESSAGE_LENGTH) + "\n... (truncated)" : item.content;
134
+ const tags = [];
135
+ tags.push({ key: "role", value: item.role });
136
+ if ((_a = item.name) == null ? void 0 : _a.length) {
137
+ tags.push({ key: "name", value: item.name });
138
+ }
139
+ const tagsString = tags.map(({ key, value }) => `${key}="${value}"`).join(" ");
140
+ return `<${msgIdx} ${tagsString}>
141
+ ${preview.trim()}
142
+ </${msgIdx}>`;
143
+ }).join("\n");
144
+ }
145
+ };
146
+
147
+ // src/context.ts
148
+ var ThinkExit = new Exit({
149
+ name: "think",
150
+ description: "Think about the current situation and provide a response"
151
+ });
152
+ var ListenExit = new Exit({
153
+ name: "listen",
154
+ description: "Listen to the user and provide a response"
155
+ });
156
+ var Iteration = class {
157
+ id;
158
+ messages;
159
+ code;
160
+ traces;
161
+ variables;
162
+ started_ts;
163
+ ended_ts;
164
+ status;
165
+ _mutations;
166
+ get mutations() {
167
+ return [...this._mutations.values()];
168
+ }
169
+ trackMutation(mutation) {
170
+ this._mutations.set(`${mutation.object ?? "global"}:${mutation.property}`, mutation);
171
+ }
172
+ _parameters;
173
+ get transcript() {
174
+ return this._parameters.transcript;
175
+ }
176
+ get tools() {
177
+ return this._parameters.tools;
178
+ }
179
+ get objects() {
180
+ return this._parameters.objects;
181
+ }
182
+ get exits() {
183
+ const exits = [...this._parameters.exits, ThinkExit];
184
+ if (this.isChatEnabled) {
185
+ exits.push(ListenExit);
186
+ }
187
+ return exits;
188
+ }
189
+ get instructions() {
190
+ return this._parameters.instructions;
191
+ }
192
+ llm;
193
+ hasExited() {
194
+ return ["exit_success"].includes(this.status.type);
195
+ }
196
+ hasExitedWith(exit) {
197
+ return this.status.type === "exit_success" && this.status.exit_success.exit_name === exit.name;
198
+ }
199
+ isSuccessful() {
200
+ return ["callback_requested", "exit_success", "thinking_requested"].includes(
201
+ this.status.type
202
+ );
203
+ }
204
+ isFailed() {
205
+ return [
206
+ "generation_error",
207
+ "invalid_code_error",
208
+ "execution_error",
209
+ "exit_error",
210
+ "aborted"
211
+ ].includes(this.status.type);
212
+ }
213
+ get duration() {
214
+ const ms2 = (this.ended_ts ?? Date.now()) - this.started_ts;
215
+ const trailing = this.ended_ts ? "" : " (still running)";
216
+ return ms2.toLocaleString("en-US", { style: "unit", unit: "millisecond" }) + trailing;
217
+ }
218
+ get error() {
219
+ if (this.status.type === "generation_error") {
220
+ return `CodeGenerationError: ${this.status.generation_error.message}`;
221
+ }
222
+ if (this.status.type === "invalid_code_error") {
223
+ return `InvalidCodeError: ${this.status.invalid_code_error.message}`;
224
+ }
225
+ if (this.status.type === "execution_error") {
226
+ return `CodeExecutionError: ${this.status.execution_error.message}`;
227
+ }
228
+ if (this.status.type === "exit_error") {
229
+ return `ExitError: ${this.status.exit_error.message}`;
230
+ }
231
+ if (this.status.type === "aborted") {
232
+ return `Aborted: ${this.status.aborted.reason}`;
233
+ }
234
+ return null;
235
+ }
236
+ get isChatEnabled() {
237
+ return this._parameters.tools.find((x) => x.name.toLowerCase() === "message") !== void 0;
238
+ }
239
+ constructor(props) {
240
+ this.id = props.id;
241
+ this.status = { type: "pending" };
242
+ this.traces = new HookedArray();
243
+ this._mutations = /* @__PURE__ */ new Map();
244
+ this.messages = props.messages;
245
+ this.variables = props.variables;
246
+ this._parameters = props.parameters;
247
+ this.started_ts = Date.now();
248
+ }
249
+ end(status) {
250
+ if (this.status.type !== "pending") {
251
+ throw new Error(`Iteration ${this.id} has already ended with status ${this.status.type}`);
252
+ }
253
+ this.ended_ts = Date.now();
254
+ this.status = status;
255
+ }
256
+ };
257
+ var Context = class {
258
+ id;
259
+ transcript;
260
+ instructions;
261
+ objects;
262
+ tools;
263
+ exits;
264
+ components;
265
+ version = DualModePrompt;
266
+ loop;
267
+ temperature;
268
+ model;
269
+ metadata;
270
+ snapshot;
271
+ iteration = 0;
272
+ iterations;
273
+ async nextIteration() {
274
+ if (this.iterations.length >= this.loop) {
275
+ throw new LoopExceededError();
276
+ }
277
+ if (this.snapshot && this.snapshot.status.type === "pending") {
278
+ throw new Error(
279
+ `Cannot resume execution from a snapshot that is still pending: ${this.snapshot.id}. Please resolve() or reject() it first.`
280
+ );
281
+ }
282
+ const parameters = await this._refreshIterationParameters();
283
+ const messages = await this._getIterationMessages(parameters);
284
+ const iteration = new Iteration({
285
+ id: `${this.id}_${this.iterations.length + 1}`,
286
+ variables: this._getIterationVariables(),
287
+ parameters,
288
+ messages
289
+ });
290
+ this.iterations.push(iteration);
291
+ this.iteration = this.iterations.length;
292
+ this.snapshot = void 0;
293
+ return iteration;
294
+ }
295
+ _getIterationVariables() {
296
+ const lastIteration = this.iterations.at(-1);
297
+ const variables = {};
298
+ if ((lastIteration == null ? void 0 : lastIteration.status.type) === "thinking_requested") {
299
+ const lastThinkingVariables = lastIteration.status.thinking_requested.variables;
300
+ if (isPlainObject_default(lastThinkingVariables)) {
301
+ Object.assign(variables, cloneDeep_default(lastThinkingVariables));
302
+ }
303
+ }
304
+ if (isPlainObject_default(lastIteration == null ? void 0 : lastIteration.variables)) {
305
+ Object.assign(variables, cloneDeep_default((lastIteration == null ? void 0 : lastIteration.variables) ?? {}));
306
+ }
307
+ return variables;
308
+ }
309
+ async _getIterationMessages(parameters) {
310
+ var _a, _b, _c, _d, _e, _f;
311
+ const lastIteration = this.iterations.at(-1);
312
+ if (((_a = this.snapshot) == null ? void 0 : _a.status.type) === "resolved") {
313
+ return [
314
+ await this.version.getSystemMessage({
315
+ globalTools: parameters.tools,
316
+ objects: parameters.objects,
317
+ instructions: parameters.instructions,
318
+ transcript: parameters.transcript,
319
+ exits: parameters.exits,
320
+ components: parameters.components
321
+ }),
322
+ await this.version.getSnapshotResolvedMessage({
323
+ snapshot: this.snapshot
324
+ })
325
+ ];
326
+ }
327
+ if (((_b = this.snapshot) == null ? void 0 : _b.status.type) === "rejected") {
328
+ return [
329
+ await this.version.getSystemMessage({
330
+ globalTools: parameters.tools,
331
+ objects: parameters.objects,
332
+ instructions: parameters.instructions,
333
+ transcript: parameters.transcript,
334
+ exits: parameters.exits,
335
+ components: parameters.components
336
+ }),
337
+ await this.version.getSnapshotRejectedMessage({
338
+ snapshot: this.snapshot
339
+ })
340
+ ];
341
+ }
342
+ if (!lastIteration) {
343
+ return [
344
+ await this.version.getSystemMessage({
345
+ globalTools: parameters.tools,
346
+ objects: parameters.objects,
347
+ instructions: parameters.instructions,
348
+ transcript: parameters.transcript,
349
+ exits: parameters.exits,
350
+ components: parameters.components
351
+ }),
352
+ await this.version.getInitialUserMessage({
353
+ globalTools: parameters.tools,
354
+ objects: parameters.objects,
355
+ instructions: parameters.instructions,
356
+ transcript: parameters.transcript,
357
+ exits: parameters.exits,
358
+ components: parameters.components
359
+ })
360
+ ];
361
+ }
362
+ const lastIterationMessages = [
363
+ await this.version.getSystemMessage({
364
+ globalTools: parameters.tools,
365
+ objects: parameters.objects,
366
+ instructions: parameters.instructions,
367
+ transcript: parameters.transcript,
368
+ exits: parameters.exits,
369
+ components: parameters.components
370
+ }),
371
+ ...lastIteration.messages.filter((x) => x.role !== "system")
372
+ ];
373
+ if ((lastIteration == null ? void 0 : lastIteration.status.type) === "thinking_requested") {
374
+ return [
375
+ ...lastIterationMessages,
376
+ {
377
+ role: "assistant",
378
+ content: wrapContent(((_c = lastIteration.llm) == null ? void 0 : _c.output) ?? "", { preserve: "top", flex: 4, minTokens: 25 })
379
+ },
380
+ await this.version.getThinkingMessage({
381
+ reason: lastIteration.status.thinking_requested.reason,
382
+ variables: lastIteration.status.thinking_requested.variables
383
+ })
384
+ ];
385
+ }
386
+ if ((lastIteration == null ? void 0 : lastIteration.status.type) === "exit_error") {
387
+ return [
388
+ ...lastIterationMessages,
389
+ {
390
+ role: "assistant",
391
+ content: wrapContent(((_d = lastIteration.llm) == null ? void 0 : _d.output) ?? "", { preserve: "top", flex: 4, minTokens: 25 })
392
+ },
393
+ await this.version.getInvalidCodeMessage({
394
+ code: lastIteration.code ?? "// No code generated",
395
+ message: `Invalid return statement (action: ${lastIteration.status.exit_error.exit}): ${lastIteration.status.exit_error.message}`
396
+ })
397
+ ];
398
+ }
399
+ if ((lastIteration == null ? void 0 : lastIteration.status.type) === "invalid_code_error") {
400
+ return [
401
+ ...lastIterationMessages,
402
+ {
403
+ role: "assistant",
404
+ content: wrapContent(((_e = lastIteration.llm) == null ? void 0 : _e.output) ?? "", { preserve: "top", flex: 4, minTokens: 25 })
405
+ },
406
+ await this.version.getInvalidCodeMessage({
407
+ code: lastIteration.code ?? "// No code generated",
408
+ message: lastIteration.status.invalid_code_error.message
409
+ })
410
+ ];
411
+ }
412
+ if ((lastIteration == null ? void 0 : lastIteration.status.type) === "execution_error") {
413
+ return [
414
+ ...lastIterationMessages,
415
+ {
416
+ role: "assistant",
417
+ content: wrapContent(((_f = lastIteration.llm) == null ? void 0 : _f.output) ?? "", { preserve: "top", flex: 4, minTokens: 25 })
418
+ },
419
+ await this.version.getCodeExecutionErrorMessage({
420
+ message: lastIteration.status.execution_error.message,
421
+ stacktrace: lastIteration.status.execution_error.stack
422
+ })
423
+ ];
424
+ }
425
+ throw new Error(
426
+ `Unexpected iteration status: ${lastIteration == null ? void 0 : lastIteration.status.type}. This is likely a bug, please report it.`
427
+ );
428
+ }
429
+ async _refreshIterationParameters() {
430
+ var _a;
431
+ const instructions = await getValue(this.instructions, this);
432
+ const transcript = new TranscriptArray(await getValue(this.transcript, this) ?? []);
433
+ const tools = Tool.withUniqueNames(await getValue(this.tools, this) ?? []);
434
+ const objects = await getValue(this.objects, this) ?? [];
435
+ const exits = await getValue(this.exits, this) ?? [];
436
+ const components = await getValue(this.components, this) ?? [];
437
+ if (objects && objects.length > 100) {
438
+ throw new Error("Too many objects. Expected at most 100 objects.");
439
+ }
440
+ if (tools && tools.length > 100) {
441
+ throw new Error("Too many tools. Expected at most 100 tools.");
442
+ }
443
+ for (const component of components) {
444
+ assertValidComponent(component);
445
+ }
446
+ const ReservedToolNames = [
447
+ "think",
448
+ "listen",
449
+ "return",
450
+ "exit",
451
+ "action",
452
+ "function",
453
+ "callback",
454
+ "code",
455
+ "execute",
456
+ "jsx",
457
+ "object",
458
+ "string",
459
+ "number",
460
+ "boolean",
461
+ "array"
462
+ ];
463
+ const MessageTool = (_a = tools.find((x) => {
464
+ var _a2;
465
+ return x.name.toLowerCase() === "message" || ((_a2 = x.aliases) == null ? void 0 : _a2.includes("message"));
466
+ })) == null ? void 0 : _a.clone();
467
+ if (MessageTool && !components.length) {
468
+ throw new Error("The Message tool is only available in chat mode. Please provide at least one component.");
469
+ }
470
+ if (!MessageTool && components.length) {
471
+ throw new Error('When using components, you need to provide a tool called "Message"');
472
+ }
473
+ if (MessageTool && components.length) {
474
+ MessageTool.aliases = Array.from(
475
+ /* @__PURE__ */ new Set([...MessageTool.aliases, ...components.flatMap((x) => [x.name, ...x.aliases ?? []])])
476
+ );
477
+ }
478
+ const nonMessageTools = tools.filter((x) => {
479
+ var _a2;
480
+ return x.name.toLowerCase() !== "message" && !((_a2 = x.aliases) == null ? void 0 : _a2.includes("message"));
481
+ });
482
+ const allTools = MessageTool ? [MessageTool, ...nonMessageTools] : nonMessageTools;
483
+ for (const tool of nonMessageTools) {
484
+ for (let name of [...tool.aliases, tool.name]) {
485
+ name = name.toLowerCase();
486
+ if (ReservedToolNames.includes(name)) {
487
+ throw new Error(`Tool name "${name}" (${tool.name}) is reserved. Please choose a different name.`);
488
+ }
489
+ if (components.find((x) => {
490
+ var _a2;
491
+ return x.name.toLowerCase() === name || ((_a2 = x.aliases) == null ? void 0 : _a2.map((x2) => x2.toLowerCase()).includes(name));
492
+ })) {
493
+ throw new Error(
494
+ `Tool name "${name}" (${tool.name}) is already used by a component. Please choose a different name.`
495
+ );
496
+ }
497
+ if (exits.find((x) => x.name.toLowerCase() === name) || exits.find((x) => {
498
+ var _a2;
499
+ return (_a2 = x.aliases) == null ? void 0 : _a2.map((x2) => x2.toLowerCase()).includes(name);
500
+ })) {
501
+ throw new Error(
502
+ `Tool name "${name}" (${tool.name}) is already used by an exit. Please choose a different name.`
503
+ );
504
+ }
505
+ }
506
+ }
507
+ if (exits && exits.length > 100) {
508
+ throw new Error("Too many exits. Expected at most 100 exits.");
509
+ }
510
+ if (components && components.length > 100) {
511
+ throw new Error("Too many components. Expected at most 100 components.");
512
+ }
513
+ if (instructions && instructions.length > 1e6) {
514
+ throw new Error("Instructions are too long. Expected at most 1,000,000 characters.");
515
+ }
516
+ if (transcript && transcript.length > 250) {
517
+ throw new Error("Too many transcript messages. Expected at most 250 messages.");
518
+ }
519
+ if (!components.length && !exits.length) {
520
+ throw new Error("When no components are provided, at least one exit is required.");
521
+ }
522
+ return {
523
+ transcript,
524
+ tools: allTools,
525
+ objects,
526
+ exits,
527
+ instructions,
528
+ components
529
+ };
530
+ }
531
+ constructor(props) {
532
+ this.id = `llmz_${ulid()}`;
533
+ this.transcript = props.transcript;
534
+ this.instructions = props.instructions;
535
+ this.objects = props.objects;
536
+ this.tools = props.tools;
537
+ this.exits = props.exits;
538
+ this.components = props.components;
539
+ this.loop = props.loop ?? 3;
540
+ this.temperature = props.temperature ?? 0.7;
541
+ this.model = props.model;
542
+ this.iterations = [];
543
+ this.metadata = props.metadata ?? {};
544
+ this.snapshot = props.snapshot;
545
+ if (this.loop < 1 || this.loop > 100) {
546
+ throw new Error("Invalid loop. Expected a number between 1 and 100.");
547
+ }
548
+ if (this.temperature < 0 || this.temperature > 2) {
549
+ throw new Error("Invalid temperature. Expected a number between 0 and 2.");
550
+ }
551
+ }
552
+ };
553
+
554
+ // src/llmz.ts
555
+ var getErrorMessage = (err) => err instanceof Error ? err.message : JSON.stringify(err);
556
+ var SLOW_TOOL_WARNING = ms("15s");
557
+ var RESPONSE_LENGTH_BUFFER = {
558
+ MIN_TOKENS: 1e3,
559
+ MAX_TOKENS: 16e3,
560
+ PERCENTAGE: 0.1
561
+ };
562
+ var getModelOutputLimit = (inputLength) => clamp_default(
563
+ RESPONSE_LENGTH_BUFFER.PERCENTAGE * inputLength,
564
+ RESPONSE_LENGTH_BUFFER.MIN_TOKENS,
565
+ RESPONSE_LENGTH_BUFFER.MAX_TOKENS
566
+ );
567
+ var executeContext = async (props) => {
568
+ var _a, _b, _c;
569
+ await init();
570
+ const { signal, onIterationEnd, onTrace, onExit } = props;
571
+ const cognitive = props.client instanceof Cognitive ? props.client : new Cognitive({ client: props.client });
572
+ const cleanups = [];
573
+ const ctx = new Context({
574
+ instructions: props.instructions,
575
+ objects: props.objects,
576
+ tools: props.tools,
577
+ loop: (_a = props.options) == null ? void 0 : _a.loop,
578
+ temperature: (_b = props.options) == null ? void 0 : _b.temperature,
579
+ model: (_c = props.options) == null ? void 0 : _c.model,
580
+ transcript: props.transcript,
581
+ exits: props.exits,
582
+ components: props.components,
583
+ snapshot: props.snapshot
584
+ });
585
+ try {
586
+ while (true) {
587
+ if (ctx.iterations.length >= ctx.loop) {
588
+ return {
589
+ status: "error",
590
+ context: ctx,
591
+ error: `Loop limit exceeded. Maximum allowed loops: ${ctx.loop}`,
592
+ iterations: ctx.iterations
593
+ };
594
+ }
595
+ const iteration = await ctx.nextIteration();
596
+ cleanups.push(
597
+ iteration.traces.onPush((traces) => {
598
+ for (const trace of traces) {
599
+ onTrace == null ? void 0 : onTrace({ trace, iteration: ctx.iterations.length });
600
+ }
601
+ })
602
+ );
603
+ try {
604
+ await executeIteration({
605
+ iteration,
606
+ ctx,
607
+ cognitive,
608
+ abortSignal: signal,
609
+ onExit
610
+ });
611
+ } catch (err) {
612
+ iteration.end({
613
+ type: "execution_error",
614
+ execution_error: {
615
+ stack: cleanStackTrace(err.stack ?? "No stack trace available"),
616
+ message: "An unexpected error occurred: " + getErrorMessage(err)
617
+ }
618
+ });
619
+ }
620
+ try {
621
+ await (onIterationEnd == null ? void 0 : onIterationEnd(iteration));
622
+ } catch (err) {
623
+ console.error(err);
624
+ }
625
+ if (iteration.status.type === "exit_success") {
626
+ return {
627
+ status: "success",
628
+ context: ctx,
629
+ iterations: ctx.iterations
630
+ };
631
+ }
632
+ if (iteration.status.type === "callback_requested") {
633
+ return {
634
+ status: "interrupted",
635
+ context: ctx,
636
+ iterations: ctx.iterations,
637
+ signal: iteration.status.callback_requested.signal,
638
+ snapshot: Snapshot.fromSignal(iteration.status.callback_requested.signal)
639
+ };
640
+ }
641
+ if (iteration.status.type === "thinking_requested" || iteration.status.type === "exit_error" || iteration.status.type === "execution_error" || iteration.status.type === "invalid_code_error") {
642
+ continue;
643
+ }
644
+ return {
645
+ context: ctx,
646
+ error: iteration.error ?? `Unknown error. Status: ${iteration.status.type}`,
647
+ status: "error",
648
+ iterations: ctx.iterations
649
+ };
650
+ }
651
+ } catch (error) {
652
+ return {
653
+ status: "error",
654
+ iterations: ctx.iterations,
655
+ context: ctx,
656
+ error: error instanceof Error ? error.message : (error == null ? void 0 : error.toString()) ?? "Unknown error"
657
+ };
658
+ } finally {
659
+ for (const cleanup of cleanups) {
660
+ try {
661
+ cleanup();
662
+ } catch {
663
+ }
664
+ }
665
+ }
666
+ };
667
+ var executeIteration = async ({
668
+ iteration,
669
+ ctx,
670
+ cognitive,
671
+ abortSignal,
672
+ onExit
673
+ }) => {
674
+ var _a, _b, _c, _d, _e;
675
+ let startedAt = Date.now();
676
+ const traces = iteration.traces;
677
+ const modelLimit = 128e3;
678
+ const responseLengthBuffer = getModelOutputLimit(modelLimit);
679
+ const messages = truncateWrappedContent({
680
+ messages: iteration.messages,
681
+ tokenLimit: modelLimit - responseLengthBuffer,
682
+ throwOnFailure: false
683
+ }).filter(
684
+ (x) => (
685
+ // Filter out empty messages, as they are not valid inputs for the LLM
686
+ // This can happen when a message is truncated and the content is empty
687
+ x.content.trim().length > 0
688
+ )
689
+ );
690
+ const output = await cognitive.generateContent({
691
+ signal: abortSignal,
692
+ systemPrompt: (_a = messages.find((x) => x.role === "system")) == null ? void 0 : _a.content,
693
+ model: ctx.model,
694
+ temperature: ctx.temperature,
695
+ responseFormat: "text",
696
+ messages: messages.filter((x) => x.role === "user" || x.role === "assistant").map(
697
+ (x) => ({
698
+ role: x.role === "user" ? "user" : "assistant",
699
+ type: "text",
700
+ content: x.content
701
+ })
702
+ ),
703
+ stopSequences: ctx.version.getStopTokens()
704
+ });
705
+ const out = ((_c = (_b = output.output.choices) == null ? void 0 : _b[0]) == null ? void 0 : _c.type) === "text" && typeof ((_d = output.output.choices) == null ? void 0 : _d[0].content) === "string" ? output.output.choices[0].content : null;
706
+ if (!out) {
707
+ throw new Error("No output from LLM");
708
+ }
709
+ const assistantResponse = ctx.version.parseAssistantResponse(out);
710
+ iteration.code = assistantResponse.code.trim();
711
+ iteration.llm = {
712
+ cached: output.meta.cached || false,
713
+ ended_at: Date.now(),
714
+ started_at: startedAt,
715
+ status: "success",
716
+ tokens: output.meta.tokens.input + output.meta.tokens.output,
717
+ spend: output.meta.cost.input + output.meta.cost.output,
718
+ output: assistantResponse.raw,
719
+ model: `${output.meta.model.integration}:${output.meta.model.model}`
720
+ };
721
+ traces.push({
722
+ type: "llm_call_success",
723
+ started_at: startedAt,
724
+ ended_at: iteration.llm.ended_at,
725
+ model: ctx.model ?? "",
726
+ code: iteration.code
727
+ });
728
+ const vmContext = { ...stripInvalidIdentifiers(iteration.variables) };
729
+ for (const obj of iteration.objects) {
730
+ const internalValues = {};
731
+ const instance = {};
732
+ for (const { name, value, writable, type } of obj.properties ?? []) {
733
+ internalValues[name] = value;
734
+ const initialValue = value;
735
+ const schema = type ?? z.any();
736
+ Object.defineProperty(instance, name, {
737
+ enumerable: true,
738
+ configurable: true,
739
+ get() {
740
+ return internalValues[name];
741
+ },
742
+ set(value2) {
743
+ if (isEqual_default(value2, internalValues[name])) {
744
+ return;
745
+ }
746
+ if (!writable) {
747
+ throw new AssignmentError(`Property ${obj.name}.${name} is read-only and cannot be modified`);
748
+ }
749
+ if (value2 === internalValues[name]) {
750
+ return;
751
+ }
752
+ const parsed = schema.safeParse(value2);
753
+ if (!parsed.success) {
754
+ throw new AssignmentError(
755
+ `Invalid value for Object property ${obj.name}.${name}: ${getErrorMessage(parsed.error)}`
756
+ );
757
+ }
758
+ internalValues[name] = parsed.data;
759
+ traces.push({
760
+ type: "property",
761
+ started_at: Date.now(),
762
+ object: obj.name,
763
+ property: name,
764
+ value: parsed.data
765
+ });
766
+ iteration.trackMutation({ object: obj.name, property: name, before: initialValue, after: parsed.data });
767
+ }
768
+ });
769
+ }
770
+ for (const tool of obj.tools ?? []) {
771
+ instance[tool.name] = wrapTool({ tool, traces, object: obj.name });
772
+ }
773
+ Object.preventExtensions(instance);
774
+ Object.seal(instance);
775
+ vmContext[obj.name] = instance;
776
+ }
777
+ for (const tool of iteration.tools) {
778
+ const wrapped = wrapTool({ tool, traces });
779
+ for (const key of [tool.name, ...tool.aliases ?? []]) {
780
+ vmContext[key] = wrapped;
781
+ }
782
+ }
783
+ if (abortSignal == null ? void 0 : abortSignal.aborted) {
784
+ traces.push({
785
+ type: "abort_signal",
786
+ started_at: Date.now(),
787
+ reason: "The operation was aborted by user."
788
+ });
789
+ return iteration.end({
790
+ type: "aborted",
791
+ aborted: {
792
+ reason: (abortSignal == null ? void 0 : abortSignal.reason) ?? "The operation was aborted"
793
+ }
794
+ });
795
+ }
796
+ startedAt = Date.now();
797
+ const result = await runAsyncFunction(vmContext, assistantResponse.code.trim(), traces, abortSignal).catch(
798
+ (err) => {
799
+ return {
800
+ success: false,
801
+ error: err,
802
+ lines_executed: [],
803
+ traces: [],
804
+ variables: {}
805
+ };
806
+ }
807
+ );
808
+ if (result.error && result.error instanceof InvalidCodeError) {
809
+ return iteration.end({
810
+ type: "invalid_code_error",
811
+ invalid_code_error: {
812
+ message: result.error.message
813
+ }
814
+ });
815
+ }
816
+ traces.push({
817
+ type: "code_execution",
818
+ lines_executed: result.lines_executed ?? 0,
819
+ started_at: startedAt,
820
+ ended_at: Date.now()
821
+ });
822
+ if (result.error && result.error instanceof CodeExecutionError) {
823
+ return iteration.end({
824
+ type: "execution_error",
825
+ execution_error: {
826
+ message: result.error.message,
827
+ stack: cleanStackTrace(result.error.stack ?? "No stack trace available")
828
+ }
829
+ });
830
+ }
831
+ if (abortSignal == null ? void 0 : abortSignal.aborted) {
832
+ return iteration.end({
833
+ type: "aborted",
834
+ aborted: {
835
+ reason: (abortSignal == null ? void 0 : abortSignal.reason) ?? "The operation was aborted"
836
+ }
837
+ });
838
+ }
839
+ if (!result.success) {
840
+ return iteration.end({
841
+ type: "execution_error",
842
+ execution_error: {
843
+ message: ((_e = result == null ? void 0 : result.error) == null ? void 0 : _e.message) ?? "Unknown error occurred",
844
+ stack: cleanStackTrace(result.error.stack ?? "No stack trace available")
845
+ }
846
+ });
847
+ }
848
+ if (result.signal instanceof ThinkSignal) {
849
+ return iteration.end({
850
+ type: "thinking_requested",
851
+ thinking_requested: {
852
+ variables: result.signal.variables,
853
+ reason: result.signal.reason
854
+ }
855
+ });
856
+ }
857
+ if (result.signal instanceof SnapshotSignal) {
858
+ return iteration.end({
859
+ type: "callback_requested",
860
+ callback_requested: {
861
+ signal: result.signal
862
+ }
863
+ });
864
+ }
865
+ const validActions = [...iteration.exits.map((x) => x.name.toLowerCase()), "think"];
866
+ let returnValue = result.success && result.return_value ? result.return_value : null;
867
+ const returnAction = returnValue == null ? void 0 : returnValue.action;
868
+ const returnExit = iteration.exits.find((x) => x.name.toLowerCase() === (returnAction == null ? void 0 : returnAction.toLowerCase())) ?? iteration.exits.find((x) => x.aliases.some((a) => a.toLowerCase() === (returnAction == null ? void 0 : returnAction.toLowerCase())));
869
+ if (returnAction === "think") {
870
+ const variables = omit_default(returnValue ?? {}, "action");
871
+ if (isPlainObject_default(variables) && Object.keys(variables).length > 0) {
872
+ return iteration.end({
873
+ type: "thinking_requested",
874
+ thinking_requested: {
875
+ variables,
876
+ reason: "Thinking requested"
877
+ }
878
+ });
879
+ }
880
+ return iteration.end({
881
+ type: "thinking_requested",
882
+ thinking_requested: {
883
+ reason: "Thinking requested",
884
+ variables: iteration.variables
885
+ }
886
+ });
887
+ }
888
+ if (!returnAction) {
889
+ return iteration.end({
890
+ type: "exit_error",
891
+ exit_error: {
892
+ exit: "n/a",
893
+ message: `Code did not return an action. Valid actions are: ${validActions.join(", ")}`,
894
+ return_value: returnValue
895
+ }
896
+ });
897
+ }
898
+ if (!returnExit) {
899
+ return iteration.end({
900
+ type: "exit_error",
901
+ exit_error: {
902
+ exit: returnAction,
903
+ message: `Exit "${returnAction}" not found. Valid actions are: ${validActions.join(", ")}`,
904
+ return_value: returnValue
905
+ }
906
+ });
907
+ }
908
+ if (returnExit.zSchema) {
909
+ const parsed = returnExit.zSchema.safeParse(returnValue == null ? void 0 : returnValue.value);
910
+ if (!parsed.success) {
911
+ return iteration.end({
912
+ type: "exit_error",
913
+ exit_error: {
914
+ exit: returnExit.name,
915
+ message: `Invalid return value for exit ${returnExit.name}: ${getErrorMessage(parsed.error)}`,
916
+ return_value: returnValue
917
+ }
918
+ });
919
+ }
920
+ returnValue = { action: returnExit.name, value: parsed.data };
921
+ }
922
+ try {
923
+ await (onExit == null ? void 0 : onExit(returnExit, returnValue == null ? void 0 : returnValue.value));
924
+ } catch (err) {
925
+ return iteration.end({
926
+ type: "exit_error",
927
+ exit_error: {
928
+ exit: returnExit.name,
929
+ message: `Error executing exit ${returnExit.name}: ${getErrorMessage(err)}`,
930
+ return_value: returnValue
931
+ }
932
+ });
933
+ }
934
+ return iteration.end({
935
+ type: "exit_success",
936
+ exit_success: {
937
+ exit_name: returnExit.name,
938
+ return_value: returnValue == null ? void 0 : returnValue.value
939
+ }
940
+ });
941
+ };
942
+ function wrapTool({ tool, traces, object }) {
943
+ const getToolInput = (input) => tool.zInput.safeParse(input).data ?? input;
944
+ return function(input) {
945
+ const alertSlowTool = setTimeout(
946
+ () => traces.push({
947
+ type: "tool_slow",
948
+ tool_name: tool.name,
949
+ started_at: Date.now(),
950
+ input: getToolInput(input),
951
+ object,
952
+ duration: SLOW_TOOL_WARNING
953
+ }),
954
+ SLOW_TOOL_WARNING
955
+ );
956
+ const cancelSlowTool = () => clearTimeout(alertSlowTool);
957
+ const toolStart = Date.now();
958
+ let output;
959
+ let error;
960
+ let success = true;
961
+ const handleSignals = (error2) => {
962
+ if (output === error2) {
963
+ return true;
964
+ }
965
+ if (error2 instanceof SnapshotSignal) {
966
+ error2.toolCall = {
967
+ name: tool.name,
968
+ inputSchema: tool.input,
969
+ outputSchema: tool.output,
970
+ input
971
+ };
972
+ error2.message = Signals.serializeError(error2);
973
+ }
974
+ if (error2 instanceof ThinkSignal) {
975
+ traces.push({
976
+ type: "think_signal",
977
+ started_at: Date.now(),
978
+ line: 0,
979
+ ended_at: Date.now()
980
+ });
981
+ success = true;
982
+ output = error2;
983
+ return true;
984
+ }
985
+ return false;
986
+ };
987
+ try {
988
+ const result = tool.execute(input);
989
+ if (result instanceof Promise || (result == null ? void 0 : result.then) && (result == null ? void 0 : result.catch)) {
990
+ return result.then((res) => {
991
+ output = res;
992
+ success = true;
993
+ return res;
994
+ }).catch((err) => {
995
+ if (!handleSignals(err)) {
996
+ success = false;
997
+ error = err;
998
+ }
999
+ throw err;
1000
+ }).finally(() => {
1001
+ cancelSlowTool();
1002
+ traces.push({
1003
+ type: "tool_call",
1004
+ started_at: toolStart,
1005
+ ended_at: Date.now(),
1006
+ tool_name: tool.name,
1007
+ object,
1008
+ input: getToolInput(input),
1009
+ output,
1010
+ error,
1011
+ success
1012
+ });
1013
+ });
1014
+ }
1015
+ success = true;
1016
+ output = result;
1017
+ } catch (err) {
1018
+ if (!handleSignals(err)) {
1019
+ success = false;
1020
+ error = err;
1021
+ }
1022
+ }
1023
+ cancelSlowTool();
1024
+ traces.push({
1025
+ type: "tool_call",
1026
+ started_at: toolStart,
1027
+ ended_at: Date.now(),
1028
+ tool_name: tool.name,
1029
+ object,
1030
+ input: getToolInput(input),
1031
+ output,
1032
+ error,
1033
+ success
1034
+ });
1035
+ if (!success) {
1036
+ throw error;
1037
+ }
1038
+ if (output instanceof VMSignal) {
1039
+ throw output;
1040
+ }
1041
+ return output;
1042
+ };
1043
+ }
1044
+ var llmz = {
1045
+ executeContext
1046
+ };
1047
+ export {
1048
+ llmz
1049
+ };