llmz 0.0.8 → 0.0.10

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 (92) hide show
  1. package/package.json +3 -3
  2. package/dist/chat.d.ts +0 -17
  3. package/dist/chunk-276Q6EWP.cjs +0 -224
  4. package/dist/chunk-4L6D2A6O.cjs +0 -283
  5. package/dist/chunk-4MNIJGK6.js +0 -224
  6. package/dist/chunk-7WRN4E42.js +0 -3087
  7. package/dist/chunk-BEPRLBPK.cjs +0 -3123
  8. package/dist/chunk-C6WNNTEV.cjs +0 -212
  9. package/dist/chunk-D3ESDRLH.js +0 -3123
  10. package/dist/chunk-GGWM6X2K.js +0 -184
  11. package/dist/chunk-GWFYZDUR.cjs +0 -105
  12. package/dist/chunk-HJKOSEH2.cjs +0 -722
  13. package/dist/chunk-IH2WQFO5.js +0 -283
  14. package/dist/chunk-IKSIOIIP.cjs +0 -28
  15. package/dist/chunk-JAGB2AOU.js +0 -212
  16. package/dist/chunk-JDABP4SD.cjs +0 -161
  17. package/dist/chunk-JGVAZO4X.cjs +0 -152
  18. package/dist/chunk-JKVVQN2P.js +0 -161
  19. package/dist/chunk-JMSZKB4T.js +0 -105
  20. package/dist/chunk-JQBT7UWN.js +0 -28
  21. package/dist/chunk-KH6JQYQA.js +0 -796
  22. package/dist/chunk-KMZDFWYZ.cjs +0 -33
  23. package/dist/chunk-ORQP26SZ.js +0 -33
  24. package/dist/chunk-PRVFVXT4.js +0 -722
  25. package/dist/chunk-SHJDRZF5.cjs +0 -796
  26. package/dist/chunk-SNDVQU5A.js +0 -152
  27. package/dist/chunk-UQOBUJIQ.cjs +0 -3087
  28. package/dist/chunk-ZRCU35UV.cjs +0 -184
  29. package/dist/citations.d.ts +0 -63
  30. package/dist/compiler/compiler.d.ts +0 -28
  31. package/dist/compiler/index.d.ts +0 -2
  32. package/dist/compiler/plugins/async-iterator.d.ts +0 -4
  33. package/dist/compiler/plugins/braces-tsx.d.ts +0 -3
  34. package/dist/compiler/plugins/jsx-preserve-newlines.d.ts +0 -5
  35. package/dist/compiler/plugins/line-tracking.d.ts +0 -5
  36. package/dist/compiler/plugins/replace-comment.d.ts +0 -5
  37. package/dist/compiler/plugins/return-async.d.ts +0 -4
  38. package/dist/compiler/plugins/track-tool-calls.d.ts +0 -16
  39. package/dist/compiler/plugins/variable-extraction.d.ts +0 -5
  40. package/dist/component-R4WTW6DZ.cjs +0 -18
  41. package/dist/component-WFVDVSDK.js +0 -18
  42. package/dist/component.d.ts +0 -66
  43. package/dist/component.default.d.ts +0 -316
  44. package/dist/context.d.ts +0 -176
  45. package/dist/dual-modes-T53P72CH.js +0 -12
  46. package/dist/dual-modes-VLIGPIHX.cjs +0 -12
  47. package/dist/errors.d.ts +0 -64
  48. package/dist/exit-TRXEU4OU.cjs +0 -8
  49. package/dist/exit-YORW76T3.js +0 -8
  50. package/dist/exit.d.ts +0 -26
  51. package/dist/formatting.d.ts +0 -5
  52. package/dist/getter.d.ts +0 -2
  53. package/dist/handlers.d.ts +0 -10
  54. package/dist/hoist.d.ts +0 -2
  55. package/dist/index.cjs +0 -921
  56. package/dist/index.d.ts +0 -23
  57. package/dist/index.js +0 -921
  58. package/dist/inspect.d.ts +0 -6
  59. package/dist/jsx-AEHVFB3L.js +0 -13
  60. package/dist/jsx-AJAXBWFE.cjs +0 -13
  61. package/dist/jsx.d.ts +0 -11
  62. package/dist/llmz-QLZBDG2Z.cjs +0 -576
  63. package/dist/llmz-ROOX7RYI.js +0 -576
  64. package/dist/llmz.d.ts +0 -40
  65. package/dist/objects.d.ts +0 -24
  66. package/dist/prompts/chat-mode/system.md.d.ts +0 -2
  67. package/dist/prompts/chat-mode/user.md.d.ts +0 -2
  68. package/dist/prompts/common.d.ts +0 -6
  69. package/dist/prompts/dual-modes.d.ts +0 -2
  70. package/dist/prompts/prompt.d.ts +0 -55
  71. package/dist/prompts/worker-mode/system.md.d.ts +0 -2
  72. package/dist/prompts/worker-mode/user.md.d.ts +0 -2
  73. package/dist/result.d.ts +0 -33
  74. package/dist/snapshots.d.ts +0 -69
  75. package/dist/stack-traces.d.ts +0 -1
  76. package/dist/tool-N6ODRRGH.js +0 -11
  77. package/dist/tool-QP4MVRWI.cjs +0 -11
  78. package/dist/tool.d.ts +0 -56
  79. package/dist/transcript.d.ts +0 -36
  80. package/dist/truncator-DUMWEGQO.cjs +0 -10
  81. package/dist/truncator-IY2MXOMC.js +0 -10
  82. package/dist/truncator.d.ts +0 -20
  83. package/dist/types.d.ts +0 -105
  84. package/dist/typings-2CPHOFDN.cjs +0 -10
  85. package/dist/typings-GDMY6VY2.js +0 -10
  86. package/dist/typings.d.ts +0 -5
  87. package/dist/utils-A7WNEFTA.cjs +0 -39
  88. package/dist/utils-N24IHDFA.js +0 -39
  89. package/dist/utils.d.ts +0 -23
  90. package/dist/vm-2DLG7V4G.cjs +0 -12
  91. package/dist/vm-FLBMZUA2.js +0 -12
  92. package/dist/vm.d.ts +0 -3
@@ -1,722 +0,0 @@
1
- import {
2
- DualModePrompt,
3
- extractType,
4
- inspect
5
- } from "./chunk-KH6JQYQA.js";
6
- import {
7
- Tool
8
- } from "./chunk-JAGB2AOU.js";
9
- import {
10
- LoopExceededError
11
- } from "./chunk-JKVVQN2P.js";
12
- import {
13
- Exit
14
- } from "./chunk-JMSZKB4T.js";
15
- import {
16
- assertValidComponent
17
- } from "./chunk-GGWM6X2K.js";
18
- import {
19
- wrapContent
20
- } from "./chunk-SNDVQU5A.js";
21
- import {
22
- cloneDeep_default,
23
- isPlainObject_default
24
- } from "./chunk-7WRN4E42.js";
25
-
26
- // src/snapshots.ts
27
- import { ulid } from "ulid";
28
- var MAX_SNAPSHOT_SIZE_BYTES = 4e3;
29
- var Snapshot = class _Snapshot {
30
- id;
31
- reason;
32
- stack;
33
- variables;
34
- toolCall;
35
- #status;
36
- get status() {
37
- return Object.freeze({ ...this.#status });
38
- }
39
- constructor(props) {
40
- this.id = props.id;
41
- this.stack = props.stack;
42
- this.reason = props.reason;
43
- this.variables = props.variables;
44
- this.toolCall = props.toolCall;
45
- this.#status = props.status;
46
- }
47
- static fromSignal(signal) {
48
- const variables = Object.entries(signal.variables).map(([name, value]) => {
49
- const type = extractType(value);
50
- const bytes = JSON.stringify(value || "").length;
51
- const truncated = bytes > MAX_SNAPSHOT_SIZE_BYTES;
52
- return truncated ? { name, type, bytes, truncated: true, preview: inspect(value, name) ?? "N/A" } : { name, type, bytes, truncated: false, value };
53
- });
54
- return new _Snapshot({
55
- id: "snapshot_" + ulid(),
56
- reason: signal.message,
57
- stack: signal.truncatedCode,
58
- variables,
59
- toolCall: signal.toolCall,
60
- status: { type: "pending" }
61
- });
62
- }
63
- toJSON() {
64
- return {
65
- id: this.id,
66
- reason: this.reason,
67
- stack: this.stack,
68
- variables: this.variables,
69
- toolCall: this.toolCall,
70
- status: this.#status
71
- };
72
- }
73
- static fromJSON(json) {
74
- return new _Snapshot({
75
- id: json.id,
76
- reason: json.reason,
77
- stack: json.stack,
78
- variables: json.variables,
79
- toolCall: json.toolCall,
80
- status: json.status
81
- });
82
- }
83
- clone() {
84
- return new _Snapshot({
85
- id: this.id,
86
- reason: this.reason,
87
- stack: this.stack,
88
- variables: this.variables,
89
- toolCall: this.toolCall,
90
- status: this.#status
91
- });
92
- }
93
- reset() {
94
- this.#status = { type: "pending" };
95
- }
96
- resolve(value) {
97
- if (this.#status.type !== "pending") {
98
- throw new Error(`Cannot resolve snapshot because it is already settled: ${this.#status.type}`);
99
- }
100
- this.#status = { type: "resolved", value };
101
- }
102
- reject(error) {
103
- if (this.#status.type !== "pending") {
104
- throw new Error(`Cannot reject snapshot because it is already settled: ${this.#status.type}`);
105
- }
106
- this.#status = { type: "rejected", error };
107
- }
108
- };
109
-
110
- // src/transcript.ts
111
- var MAX_MESSAGE_LENGTH = 5e3;
112
- function getMessagePreview(message) {
113
- if (message.role === "assistant" || message.role === "user" || message.role === "summary") {
114
- return message.content;
115
- }
116
- if (message.role === "event") {
117
- return inspect(message.payload, message.name, { tokens: 1e3 });
118
- }
119
- return inspect(message, void 0, { tokens: 1e3 });
120
- }
121
- function getMessageType(message) {
122
- if (message.role === "assistant" || message.role === "user" || message.role === "summary") {
123
- return message.role;
124
- }
125
- if (message.role === "event") {
126
- return `event:${message.name}`;
127
- }
128
- return "unknown";
129
- }
130
- var TranscriptArray = class extends Array {
131
- constructor(items = []) {
132
- items = Array.isArray(items) ? items : [];
133
- super(...items);
134
- items.forEach((item) => {
135
- if (!["user", "assistant"].includes(item.role)) {
136
- throw new Error(`Invalid role "${item.role}" in transcript message`);
137
- }
138
- if ("name" in item && item.name && typeof item.name !== "string") {
139
- throw new Error(`Invalid name for transcript message. Expected a string, but got type "${typeof item.name}"`);
140
- }
141
- if ("content" in item && typeof item.content !== "string") {
142
- throw new Error(
143
- `Invalid content for transcript message. Expected a string, but got type "${typeof item.content}"`
144
- );
145
- }
146
- });
147
- Object.setPrototypeOf(this, new.target.prototype);
148
- }
149
- toString() {
150
- if (!this.length) {
151
- return "";
152
- }
153
- return this.map((item, idx) => {
154
- var _a, _b;
155
- const msgIdx = getMessageType(item) + "-" + String(idx + 1).padStart(3, "0");
156
- let preview = getMessagePreview(item);
157
- if (preview.length > MAX_MESSAGE_LENGTH) {
158
- preview = preview.slice(0, MAX_MESSAGE_LENGTH) + "\n... (truncated)";
159
- }
160
- const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
161
- if ((item.role === "event" || item.role === "user") && ((_a = item.attachments) == null ? void 0 : _a.length)) {
162
- for (let i = 0; i < item.attachments.length; i++) {
163
- const attachmentIdx = alphabet[i % alphabet.length];
164
- preview += `
165
- [Attachment ${msgIdx}-${attachmentIdx}]`;
166
- }
167
- }
168
- const tags = [];
169
- tags.push({ key: "role", value: item.role });
170
- if ("name" in item && ((_b = item.name) == null ? void 0 : _b.length)) {
171
- tags.push({ key: "name", value: item.name });
172
- }
173
- const tagsString = tags.map(({ key, value }) => `${key}="${value}"`).join(" ");
174
- return `<${msgIdx} ${tagsString}>
175
- ${preview.trim()}
176
- </${msgIdx}>`;
177
- }).join("\n");
178
- }
179
- };
180
-
181
- // src/result.ts
182
- var ExecutionResult = class {
183
- status;
184
- context;
185
- constructor(status, context) {
186
- this.status = status;
187
- this.context = context;
188
- }
189
- isSuccess() {
190
- return this.status === "success" && this instanceof SuccessExecutionResult;
191
- }
192
- isError() {
193
- return this.status === "error" && this instanceof ErrorExecutionResult;
194
- }
195
- isInterrupted() {
196
- return this.status === "interrupted" && this instanceof PartialExecutionResult;
197
- }
198
- is(exit) {
199
- return this.status === "success" && this instanceof SuccessExecutionResult && this.result.exit === exit;
200
- }
201
- get output() {
202
- return this.isSuccess() ? this.result.result : null;
203
- }
204
- get iteration() {
205
- return this.context.iterations.at(-1) || null;
206
- }
207
- get iterations() {
208
- return this.context.iterations ?? [];
209
- }
210
- };
211
- var SuccessExecutionResult = class extends ExecutionResult {
212
- result;
213
- constructor(context, result) {
214
- super("success", context);
215
- this.result = result;
216
- }
217
- get output() {
218
- return this.result.result;
219
- }
220
- get iteration() {
221
- return this.context.iterations.at(-1);
222
- }
223
- };
224
- var ErrorExecutionResult = class extends ExecutionResult {
225
- error;
226
- constructor(context, error) {
227
- super("error", context);
228
- this.error = error;
229
- }
230
- get output() {
231
- return null;
232
- }
233
- };
234
- var PartialExecutionResult = class extends ExecutionResult {
235
- signal;
236
- snapshot;
237
- constructor(context, signal, snapshot) {
238
- super("interrupted", context);
239
- this.signal = signal;
240
- this.snapshot = snapshot;
241
- }
242
- get output() {
243
- return null;
244
- }
245
- };
246
-
247
- // src/getter.ts
248
- var getValue = async (valueOrGetter, ctx) => {
249
- if (typeof valueOrGetter === "function") {
250
- try {
251
- return await valueOrGetter(ctx);
252
- } catch (e) {
253
- throw new Error(`Error while getting value for ${valueOrGetter}: ${e}`);
254
- }
255
- } else {
256
- return valueOrGetter;
257
- }
258
- };
259
-
260
- // src/context.ts
261
- import { z } from "@bpinternal/zui";
262
- import { ulid as ulid2 } from "ulid";
263
-
264
- // src/handlers.ts
265
- var Emitter = class {
266
- _handlers = [];
267
- subscribe = (fn) => {
268
- this._handlers.push(fn);
269
- return () => {
270
- this._handlers = this._handlers.filter((handler) => handler !== fn);
271
- };
272
- };
273
- emit(event) {
274
- this._handlers.forEach((handler) => handler(event));
275
- }
276
- };
277
- var HookedArray = class extends Array {
278
- #listeners = new Emitter();
279
- constructor(...items) {
280
- super(...items);
281
- Object.setPrototypeOf(this, new.target.prototype);
282
- }
283
- push(...items) {
284
- try {
285
- this.#listeners.emit(items);
286
- } finally {
287
- return super.push(...items);
288
- }
289
- }
290
- onPush(fn) {
291
- return this.#listeners.subscribe(fn);
292
- }
293
- };
294
-
295
- // src/context.ts
296
- var ThinkExit = new Exit({
297
- name: "think",
298
- description: "Think about the current situation and provide a response"
299
- });
300
- var ListenExit = new Exit({
301
- name: "listen",
302
- description: "Listen to the user and provide a response"
303
- });
304
- var DefaultExit = new Exit({
305
- name: "done",
306
- description: "When the execution is sucessfully completed or when error recovery is not possible",
307
- schema: z.discriminatedUnion("success", [
308
- z.object({
309
- success: z.literal(true),
310
- result: z.any().describe("The result of the execution")
311
- }),
312
- z.object({
313
- success: z.literal(false),
314
- error: z.string().describe("The error message if the execution failed")
315
- })
316
- ])
317
- });
318
- var Iteration = class {
319
- id;
320
- messages;
321
- code;
322
- traces;
323
- variables;
324
- started_ts;
325
- ended_ts;
326
- status;
327
- _mutations;
328
- get mutations() {
329
- return [...this._mutations.values()];
330
- }
331
- trackMutation(mutation) {
332
- this._mutations.set(`${mutation.object ?? "global"}:${mutation.property}`, mutation);
333
- }
334
- _parameters;
335
- get transcript() {
336
- return this._parameters.transcript;
337
- }
338
- get tools() {
339
- return this._parameters.tools;
340
- }
341
- get objects() {
342
- return this._parameters.objects;
343
- }
344
- get exits() {
345
- const exits = [...this._parameters.exits, ThinkExit];
346
- if (this.isChatEnabled) {
347
- exits.push(ListenExit);
348
- }
349
- return exits;
350
- }
351
- get instructions() {
352
- return this._parameters.instructions;
353
- }
354
- llm;
355
- hasExited() {
356
- return ["exit_success"].includes(this.status.type);
357
- }
358
- hasExitedWith(exit) {
359
- return this.status.type === "exit_success" && this.status.exit_success.exit_name === exit.name;
360
- }
361
- isSuccessful() {
362
- return ["callback_requested", "exit_success", "thinking_requested"].includes(
363
- this.status.type
364
- );
365
- }
366
- isFailed() {
367
- return [
368
- "generation_error",
369
- "invalid_code_error",
370
- "execution_error",
371
- "exit_error",
372
- "aborted"
373
- ].includes(this.status.type);
374
- }
375
- get duration() {
376
- const ms = (this.ended_ts ?? Date.now()) - this.started_ts;
377
- const trailing = this.ended_ts ? "" : " (still running)";
378
- return ms.toLocaleString("en-US", { style: "unit", unit: "millisecond" }) + trailing;
379
- }
380
- get error() {
381
- if (this.status.type === "generation_error") {
382
- return `CodeGenerationError: ${this.status.generation_error.message}`;
383
- }
384
- if (this.status.type === "invalid_code_error") {
385
- return `InvalidCodeError: ${this.status.invalid_code_error.message}`;
386
- }
387
- if (this.status.type === "execution_error") {
388
- return `CodeExecutionError: ${this.status.execution_error.message}`;
389
- }
390
- if (this.status.type === "exit_error") {
391
- return `ExitError: ${this.status.exit_error.message}`;
392
- }
393
- if (this.status.type === "aborted") {
394
- return `Aborted: ${this.status.aborted.reason}`;
395
- }
396
- return null;
397
- }
398
- get isChatEnabled() {
399
- return this._parameters.tools.find((x) => x.name.toLowerCase() === "message") !== void 0;
400
- }
401
- constructor(props) {
402
- this.id = props.id;
403
- this.status = { type: "pending" };
404
- this.traces = new HookedArray();
405
- this._mutations = /* @__PURE__ */ new Map();
406
- this.messages = props.messages;
407
- this.variables = props.variables;
408
- this._parameters = props.parameters;
409
- this.started_ts = Date.now();
410
- }
411
- end(status) {
412
- if (this.status.type !== "pending") {
413
- throw new Error(`Iteration ${this.id} has already ended with status ${this.status.type}`);
414
- }
415
- this.ended_ts = Date.now();
416
- this.status = status;
417
- }
418
- };
419
- var Context = class {
420
- id;
421
- chat;
422
- instructions;
423
- objects;
424
- tools;
425
- exits;
426
- version = DualModePrompt;
427
- timeout = 6e4;
428
- // Default timeout of 60 seconds
429
- loop;
430
- temperature;
431
- model;
432
- metadata;
433
- snapshot;
434
- iteration = 0;
435
- iterations;
436
- async nextIteration() {
437
- if (this.iterations.length >= this.loop) {
438
- throw new LoopExceededError();
439
- }
440
- if (this.snapshot && this.snapshot.status.type === "pending") {
441
- throw new Error(
442
- `Cannot resume execution from a snapshot that is still pending: ${this.snapshot.id}. Please resolve() or reject() it first.`
443
- );
444
- }
445
- const parameters = await this._refreshIterationParameters();
446
- const messages = await this._getIterationMessages(parameters);
447
- const iteration = new Iteration({
448
- id: `${this.id}_${this.iterations.length + 1}`,
449
- variables: this._getIterationVariables(),
450
- parameters,
451
- messages
452
- });
453
- this.iterations.push(iteration);
454
- this.iteration = this.iterations.length;
455
- this.snapshot = void 0;
456
- return iteration;
457
- }
458
- _getIterationVariables() {
459
- const lastIteration = this.iterations.at(-1);
460
- const variables = {};
461
- if ((lastIteration == null ? void 0 : lastIteration.status.type) === "thinking_requested") {
462
- const lastThinkingVariables = lastIteration.status.thinking_requested.variables;
463
- if (isPlainObject_default(lastThinkingVariables)) {
464
- Object.assign(variables, cloneDeep_default(lastThinkingVariables));
465
- }
466
- }
467
- if (isPlainObject_default(lastIteration == null ? void 0 : lastIteration.variables)) {
468
- Object.assign(variables, cloneDeep_default((lastIteration == null ? void 0 : lastIteration.variables) ?? {}));
469
- }
470
- return variables;
471
- }
472
- async _getIterationMessages(parameters) {
473
- var _a, _b, _c, _d, _e, _f;
474
- const lastIteration = this.iterations.at(-1);
475
- if (((_a = this.snapshot) == null ? void 0 : _a.status.type) === "resolved") {
476
- return [
477
- await this.version.getSystemMessage({
478
- globalTools: parameters.tools,
479
- objects: parameters.objects,
480
- instructions: parameters.instructions,
481
- transcript: parameters.transcript,
482
- exits: parameters.exits,
483
- components: parameters.components
484
- }),
485
- this.version.getSnapshotResolvedMessage({
486
- snapshot: this.snapshot
487
- })
488
- ];
489
- }
490
- if (((_b = this.snapshot) == null ? void 0 : _b.status.type) === "rejected") {
491
- return [
492
- await this.version.getSystemMessage({
493
- globalTools: parameters.tools,
494
- objects: parameters.objects,
495
- instructions: parameters.instructions,
496
- transcript: parameters.transcript,
497
- exits: parameters.exits,
498
- components: parameters.components
499
- }),
500
- this.version.getSnapshotRejectedMessage({
501
- snapshot: this.snapshot
502
- })
503
- ];
504
- }
505
- if (!lastIteration) {
506
- return [
507
- await this.version.getSystemMessage({
508
- globalTools: parameters.tools,
509
- objects: parameters.objects,
510
- instructions: parameters.instructions,
511
- transcript: parameters.transcript,
512
- exits: parameters.exits,
513
- components: parameters.components
514
- }),
515
- await this.version.getInitialUserMessage({
516
- globalTools: parameters.tools,
517
- objects: parameters.objects,
518
- instructions: parameters.instructions,
519
- transcript: parameters.transcript,
520
- exits: parameters.exits,
521
- components: parameters.components
522
- })
523
- ];
524
- }
525
- const lastIterationMessages = [
526
- await this.version.getSystemMessage({
527
- globalTools: parameters.tools,
528
- objects: parameters.objects,
529
- instructions: parameters.instructions,
530
- transcript: parameters.transcript,
531
- exits: parameters.exits,
532
- components: parameters.components
533
- }),
534
- ...lastIteration.messages.filter((x) => x.role !== "system")
535
- ];
536
- if ((lastIteration == null ? void 0 : lastIteration.status.type) === "thinking_requested") {
537
- return [
538
- ...lastIterationMessages,
539
- {
540
- role: "assistant",
541
- content: wrapContent(((_c = lastIteration.llm) == null ? void 0 : _c.output) ?? "", { preserve: "top", flex: 4, minTokens: 25 })
542
- },
543
- await this.version.getThinkingMessage({
544
- reason: lastIteration.status.thinking_requested.reason,
545
- variables: lastIteration.status.thinking_requested.variables
546
- })
547
- ];
548
- }
549
- if ((lastIteration == null ? void 0 : lastIteration.status.type) === "exit_error") {
550
- return [
551
- ...lastIterationMessages,
552
- {
553
- role: "assistant",
554
- content: wrapContent(((_d = lastIteration.llm) == null ? void 0 : _d.output) ?? "", { preserve: "top", flex: 4, minTokens: 25 })
555
- },
556
- await this.version.getInvalidCodeMessage({
557
- code: lastIteration.code ?? "// No code generated",
558
- message: `Invalid return statement (action: ${lastIteration.status.exit_error.exit}): ${lastIteration.status.exit_error.message}`
559
- })
560
- ];
561
- }
562
- if ((lastIteration == null ? void 0 : lastIteration.status.type) === "invalid_code_error") {
563
- return [
564
- ...lastIterationMessages,
565
- {
566
- role: "assistant",
567
- content: wrapContent(((_e = lastIteration.llm) == null ? void 0 : _e.output) ?? "", { preserve: "top", flex: 4, minTokens: 25 })
568
- },
569
- await this.version.getInvalidCodeMessage({
570
- code: lastIteration.code ?? "// No code generated",
571
- message: lastIteration.status.invalid_code_error.message
572
- })
573
- ];
574
- }
575
- if ((lastIteration == null ? void 0 : lastIteration.status.type) === "execution_error") {
576
- return [
577
- ...lastIterationMessages,
578
- {
579
- role: "assistant",
580
- content: wrapContent(((_f = lastIteration.llm) == null ? void 0 : _f.output) ?? "", { preserve: "top", flex: 4, minTokens: 25 })
581
- },
582
- await this.version.getCodeExecutionErrorMessage({
583
- message: lastIteration.status.execution_error.message,
584
- stacktrace: lastIteration.status.execution_error.stack
585
- })
586
- ];
587
- }
588
- throw new Error(
589
- `Unexpected iteration status: ${lastIteration == null ? void 0 : lastIteration.status.type}. This is likely a bug, please report it.`
590
- );
591
- }
592
- async _refreshIterationParameters() {
593
- var _a, _b;
594
- const instructions = await getValue(this.instructions, this);
595
- const transcript = new TranscriptArray(await getValue(((_a = this.chat) == null ? void 0 : _a.transcript) ?? [], this));
596
- const tools = Tool.withUniqueNames(await getValue(this.tools, this) ?? []);
597
- const objects = await getValue(this.objects, this) ?? [];
598
- const exits = await getValue(this.exits, this) ?? [];
599
- const components = await getValue(((_b = this.chat) == null ? void 0 : _b.components) ?? [], this);
600
- if (objects && objects.length > 100) {
601
- throw new Error("Too many objects. Expected at most 100 objects.");
602
- }
603
- if (tools && tools.length > 100) {
604
- throw new Error("Too many tools. Expected at most 100 tools.");
605
- }
606
- for (const component of components) {
607
- assertValidComponent(component.definition);
608
- }
609
- const ReservedToolNames = [
610
- "think",
611
- "listen",
612
- "return",
613
- "exit",
614
- "action",
615
- "function",
616
- "callback",
617
- "code",
618
- "execute",
619
- "jsx",
620
- "object",
621
- "string",
622
- "number",
623
- "boolean",
624
- "array"
625
- ];
626
- const MessageTool = this.chat && components.length ? new Tool({
627
- name: "Message",
628
- description: "Send a message to the user",
629
- aliases: Array.from(
630
- /* @__PURE__ */ new Set(["message", ...components.flatMap((x) => [x.definition.name, ...x.definition.aliases ?? []])])
631
- ),
632
- handler: async (message) => {
633
- var _a2, _b2;
634
- return await ((_b2 = (_a2 = this.chat) == null ? void 0 : _a2.handler) == null ? void 0 : _b2.call(_a2, message));
635
- }
636
- }) : null;
637
- const allTools = MessageTool ? [MessageTool, ...tools] : tools;
638
- for (const tool of tools) {
639
- for (let name of [...tool.aliases, tool.name]) {
640
- name = name.toLowerCase();
641
- if (ReservedToolNames.includes(name)) {
642
- throw new Error(`Tool name "${name}" (${tool.name}) is reserved. Please choose a different name.`);
643
- }
644
- if (components.find(
645
- (x) => {
646
- var _a2;
647
- return x.definition.name.toLowerCase() === name || ((_a2 = x.definition.aliases) == null ? void 0 : _a2.map((x2) => x2.toLowerCase()).includes(name));
648
- }
649
- )) {
650
- throw new Error(
651
- `Tool name "${name}" (${tool.name}) is already used by a component. Please choose a different name.`
652
- );
653
- }
654
- if (exits.find((x) => x.name.toLowerCase() === name) || exits.find((x) => {
655
- var _a2;
656
- return (_a2 = x.aliases) == null ? void 0 : _a2.map((x2) => x2.toLowerCase()).includes(name);
657
- })) {
658
- throw new Error(
659
- `Tool name "${name}" (${tool.name}) is already used by an exit. Please choose a different name.`
660
- );
661
- }
662
- }
663
- }
664
- if (exits && exits.length > 100) {
665
- throw new Error("Too many exits. Expected at most 100 exits.");
666
- }
667
- if (components && components.length > 100) {
668
- throw new Error("Too many components. Expected at most 100 components.");
669
- }
670
- if (instructions && instructions.length > 1e6) {
671
- throw new Error("Instructions are too long. Expected at most 1,000,000 characters.");
672
- }
673
- if (transcript && transcript.length > 250) {
674
- throw new Error("Too many transcript messages. Expected at most 250 messages.");
675
- }
676
- if (!components.length && !exits.length) {
677
- exits.push(DefaultExit);
678
- }
679
- return {
680
- transcript,
681
- tools: allTools,
682
- objects,
683
- exits,
684
- instructions,
685
- components
686
- };
687
- }
688
- constructor(props) {
689
- this.id = `llmz_${ulid2()}`;
690
- this.instructions = props.instructions;
691
- this.objects = props.objects;
692
- this.tools = props.tools;
693
- this.exits = props.exits;
694
- this.chat = props.chat;
695
- this.timeout = Math.min(999999999, Math.max(0, props.timeout ?? 6e4));
696
- this.loop = props.loop ?? 3;
697
- this.temperature = props.temperature ?? 0.7;
698
- this.model = props.model;
699
- this.iterations = [];
700
- this.metadata = props.metadata ?? {};
701
- this.snapshot = props.snapshot;
702
- if (this.loop < 1 || this.loop > 100) {
703
- throw new Error("Invalid loop. Expected a number between 1 and 100.");
704
- }
705
- if (this.temperature < 0 || this.temperature > 2) {
706
- throw new Error("Invalid temperature. Expected a number between 0 and 2.");
707
- }
708
- }
709
- };
710
-
711
- export {
712
- Snapshot,
713
- ExecutionResult,
714
- SuccessExecutionResult,
715
- ErrorExecutionResult,
716
- PartialExecutionResult,
717
- getValue,
718
- ThinkExit,
719
- ListenExit,
720
- DefaultExit,
721
- Context
722
- };