llmz 0.6.0 → 0.6.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 (110) hide show
  1. package/DOCS.md +1 -3
  2. package/dist/_baseForOwn-CFhWe_W6.js +48 -0
  3. package/dist/_baseForOwn-buOZDFbK.cjs +103 -0
  4. package/dist/_baseIteratee-CF4qP2a8.cjs +2377 -0
  5. package/dist/_baseIteratee-P_firB7b.js +2138 -0
  6. package/dist/_getPrototype-CG7Dxrrz.js +6 -0
  7. package/dist/_getPrototype-DQDlNVVM.cjs +10 -0
  8. package/dist/_overArg-C5Hy7oCi.cjs +196 -0
  9. package/dist/_overArg-CRYbj1NH.js +155 -0
  10. package/dist/citations.d.ts +11 -11
  11. package/dist/component-BwixNVH8.cjs +73 -0
  12. package/dist/component-DGG7d_Mz.js +44 -0
  13. package/dist/context.d.ts +8 -8
  14. package/dist/custom-client-BPmEoWKu.cjs +2618 -0
  15. package/dist/custom-client-NUoxrFQh.js +2547 -0
  16. package/dist/dual-modes-D_p-jedw.js +2750 -0
  17. package/dist/dual-modes-wAOAlaBS.cjs +2840 -0
  18. package/dist/errors-C962uJdS.cjs +220 -0
  19. package/dist/errors-Cg1G0Awu.js +161 -0
  20. package/dist/execute-BEaq3jkV.cjs +1003 -0
  21. package/dist/execute-DYvFRs54.js +1001 -0
  22. package/dist/exit-BGi0Kk0y.cjs +428 -0
  23. package/dist/exit-CDdxDBRT.js +417 -0
  24. package/dist/index.cjs +1786 -1489
  25. package/dist/index.d.ts +10 -9
  26. package/dist/index.js +1721 -1479
  27. package/dist/index.workerd.js +17534 -16875
  28. package/dist/isEmpty-BhJRCqIF.js +52 -0
  29. package/dist/isEmpty-LY533Wmh.cjs +57 -0
  30. package/dist/isPlainObject-CSxXVKCk.cjs +56 -0
  31. package/dist/isPlainObject-D39AzPO2.js +51 -0
  32. package/dist/jsx-BWvT6ukg.cjs +75 -0
  33. package/dist/jsx-CIgdGGVs.js +52 -0
  34. package/dist/jsx.d.ts +2 -2
  35. package/dist/prompts/chat-mode/system.md.d.ts +1 -1
  36. package/dist/prompts/chat-mode/user.md.d.ts +1 -1
  37. package/dist/prompts/worker-mode/system.md.d.ts +1 -1
  38. package/dist/prompts/worker-mode/user.md.d.ts +1 -1
  39. package/dist/quickjs-variant-BYhMBpwU.js +45 -0
  40. package/dist/quickjs-variant-CsIwTVFC.cjs +56 -0
  41. package/dist/quickjs-variant.workerd.d.ts +1 -1
  42. package/dist/result.d.ts +12 -12
  43. package/dist/rolldown-runtime-EAsCxrDo.js +14 -0
  44. package/dist/runtime/interpret-result.d.ts +1 -1
  45. package/dist/snapshots.d.ts +5 -5
  46. package/dist/stack-traces-BIE_5Xwf.js +21 -0
  47. package/dist/stack-traces-ntw_qhoy.cjs +26 -0
  48. package/dist/tool-CAo-BQHs.js +643 -0
  49. package/dist/tool-DdPZb0JW.cjs +654 -0
  50. package/dist/tool.d.ts +3 -3
  51. package/dist/truncator-B6aaJaN1.cjs +276 -0
  52. package/dist/truncator-DZqEqnpD.js +253 -0
  53. package/dist/typings-BwAz_TM9.cjs +784 -0
  54. package/dist/typings-CrBa7Tyy.js +743 -0
  55. package/dist/uniq-BYQ7GWwb.js +183 -0
  56. package/dist/uniq-G872RWmE.cjs +200 -0
  57. package/dist/utils-UBeAFeEI.js +950 -0
  58. package/dist/utils-ZpJm00ed.cjs +1075 -0
  59. package/dist/vm/errors.d.ts +2 -2
  60. package/dist/vm-BjkJqSQH.cjs +3438 -0
  61. package/dist/vm-C5vf1LH3.js +3436 -0
  62. package/package.json +7 -7
  63. package/dist/chunk-34GJUS6U.js +0 -3266
  64. package/dist/chunk-3UZKLXAD.cjs +0 -3249
  65. package/dist/chunk-4POZJQXN.js +0 -1530
  66. package/dist/chunk-5WBNTV42.cjs +0 -1530
  67. package/dist/chunk-5ZF6NTYI.cjs +0 -33
  68. package/dist/chunk-72ZWBNZV.js +0 -173
  69. package/dist/chunk-DDSJTR6Q.cjs +0 -257
  70. package/dist/chunk-GDJTKI7J.js +0 -792
  71. package/dist/chunk-GLXADNTR.js +0 -199
  72. package/dist/chunk-GOWPACSK.cjs +0 -199
  73. package/dist/chunk-HB2G32AK.js +0 -257
  74. package/dist/chunk-HC2QZTVT.cjs +0 -3266
  75. package/dist/chunk-HEAQAEFZ.cjs +0 -173
  76. package/dist/chunk-HMMM32RG.cjs +0 -792
  77. package/dist/chunk-I56MPOVG.cjs +0 -62
  78. package/dist/chunk-KIN7Y247.cjs +0 -32
  79. package/dist/chunk-KTDBAJBB.cjs +0 -1685
  80. package/dist/chunk-KWQTYXAT.js +0 -62
  81. package/dist/chunk-LOX7GOR6.js +0 -1685
  82. package/dist/chunk-PK72FAKD.cjs +0 -175
  83. package/dist/chunk-PZE6PIPG.cjs +0 -621
  84. package/dist/chunk-RGUG3UMF.cjs +0 -26
  85. package/dist/chunk-TDALRNGL.js +0 -621
  86. package/dist/chunk-UGHFNCPD.js +0 -26
  87. package/dist/chunk-WSVDMGMR.js +0 -175
  88. package/dist/chunk-XTEHT2TK.js +0 -33
  89. package/dist/chunk-YEAWWJSJ.js +0 -32
  90. package/dist/chunk-YRS7KRKP.js +0 -3249
  91. package/dist/component-2NYFY6CW.cjs +0 -14
  92. package/dist/component-ASNZHQUR.js +0 -14
  93. package/dist/dual-modes-S5TJ5JVJ.js +0 -10
  94. package/dist/dual-modes-TCMWBZIH.cjs +0 -10
  95. package/dist/execute-FF7D46TY.js +0 -1109
  96. package/dist/execute-IK6O4SU2.cjs +0 -1109
  97. package/dist/exit-MX56VN2C.cjs +0 -8
  98. package/dist/exit-UQ3EUX5D.js +0 -8
  99. package/dist/jsx-DDZ63UUF.js +0 -13
  100. package/dist/jsx-ULPWX5TW.cjs +0 -13
  101. package/dist/tool-KD7FL45V.js +0 -11
  102. package/dist/tool-VE2YHIY2.cjs +0 -11
  103. package/dist/truncator-73RCL52Y.js +0 -12
  104. package/dist/truncator-FZNLYSDL.cjs +0 -12
  105. package/dist/typings-E2RROCLT.js +0 -10
  106. package/dist/typings-LQOBZXSO.cjs +0 -10
  107. package/dist/utils-IATCUNKF.js +0 -43
  108. package/dist/utils-ITB4NEL7.cjs +0 -43
  109. package/dist/vm-6D3IIIU7.cjs +0 -12
  110. package/dist/vm-QO5FSGPN.js +0 -12
@@ -0,0 +1,428 @@
1
+ const require__baseForOwn = require("./_baseForOwn-buOZDFbK.cjs");
2
+ const require_uniq = require("./uniq-G872RWmE.cjs");
3
+ const require_utils = require("./utils-ZpJm00ed.cjs");
4
+ let _bpinternal_zui = require("@bpinternal/zui");
5
+ //#region src/exit.ts
6
+ var exit_exports = /* @__PURE__ */ require__baseForOwn.__exportAll({ Exit: () => Exit });
7
+ /**
8
+ * Defines how LLMz agent execution can terminate.
9
+ *
10
+ * Exits are the primary mechanism for controlling how and when agent execution completes.
11
+ * They define the possible outcomes of an execution and provide type-safe result handling.
12
+ * When an agent calls `return { action: 'exit_name', ...data }`, the execution terminates
13
+ * with the corresponding Exit.
14
+ *
15
+ * ## Core Concepts
16
+ *
17
+ * **Termination Control**: Exits define the valid ways an agent execution can end.
18
+ * Unlike traditional functions that just return values, LLMz agents must explicitly
19
+ * exit through predefined exits, ensuring controlled and predictable termination.
20
+ *
21
+ * **Type Safety**: Each exit can define a Zod/Zui schema that validates the return data,
22
+ * providing compile-time and runtime type safety for execution results.
23
+ *
24
+ * **Flow Control**: Different exits allow for different execution paths and result
25
+ * handling, enabling complex decision-making and branching logic.
26
+ *
27
+ * **Built-in vs Custom**: LLMz provides built-in exits (ThinkExit, ListenExit, DefaultExit)
28
+ * for common patterns, while custom exits enable domain-specific termination logic.
29
+ *
30
+ * ## Usage Patterns
31
+ *
32
+ * ### Simple Exit (No Data)
33
+ *
34
+ * For basic flow control without additional data:
35
+ *
36
+ * ```typescript
37
+ * const exit = new Exit({
38
+ * name: 'exit',
39
+ * description: 'When the user wants to exit the program',
40
+ * })
41
+ *
42
+ * // Agent usage: return { action: 'exit' }
43
+ *
44
+ * // Result handling
45
+ * if (result.is(exit)) {
46
+ * console.log('User chose to exit')
47
+ * process.exit(0)
48
+ * }
49
+ * ```
50
+ *
51
+ * ### Exit with Schema (Typed Data)
52
+ *
53
+ * For structured data extraction with validation:
54
+ *
55
+ * ```typescript
56
+ * const escalation = new Exit({
57
+ * name: 'escalation',
58
+ * description: 'Escalate the issue to a human agent',
59
+ * schema: z.object({
60
+ * reason: z.enum(['Frustrated user', 'Technical issue', 'Sensitive topic', 'Other']),
61
+ * priority: z.enum(['low', 'medium', 'high']).default('medium'),
62
+ * details: z.string(),
63
+ * }),
64
+ * })
65
+ *
66
+ * // Agent usage: return { action: 'escalation', reason: 'Technical issue', details: '...' }
67
+ *
68
+ * // Type-safe result handling
69
+ * if (result.is(escalation)) {
70
+ * console.log(`Escalation: ${result.output.reason}`)
71
+ * console.log(`Priority: ${result.output.priority}`)
72
+ * console.log(`Details: ${result.output.details}`)
73
+ * }
74
+ * ```
75
+ *
76
+ * ### Multiple Exits for Decision Making
77
+ *
78
+ * Enabling different execution paths:
79
+ *
80
+ * ```typescript
81
+ * const approved = new Exit({
82
+ * name: 'approved',
83
+ * description: 'Request was approved',
84
+ * schema: z.object({
85
+ * amount: z.number(),
86
+ * reference: z.string(),
87
+ * }),
88
+ * })
89
+ *
90
+ * const rejected = new Exit({
91
+ * name: 'rejected',
92
+ * description: 'Request was rejected',
93
+ * schema: z.object({
94
+ * reason: z.string(),
95
+ * }),
96
+ * })
97
+ *
98
+ * const result = await execute({
99
+ * instructions: 'Process the loan application',
100
+ * exits: [approved, rejected],
101
+ * tools: [reviewTool, creditCheckTool],
102
+ * client,
103
+ * })
104
+ *
105
+ * if (result.is(approved)) {
106
+ * console.log(`Approved: $${result.output.amount} (${result.output.reference})`)
107
+ * } else if (result.is(rejected)) {
108
+ * console.log(`Rejected: ${result.output.reason}`)
109
+ * }
110
+ * ```
111
+ *
112
+ * ## Advanced Features
113
+ *
114
+ * ### Exit Aliases
115
+ *
116
+ * Multiple names for the same exit:
117
+ *
118
+ * ```typescript
119
+ * const exit = new Exit({
120
+ * name: 'complete',
121
+ * aliases: ['done', 'finished', 'end'],
122
+ * description: 'Task completed successfully',
123
+ * })
124
+ *
125
+ * // Agent can use any alias: return { action: 'done' } or { action: 'finished' }
126
+ * ```
127
+ *
128
+ * ### Exit Metadata for Orchestration
129
+ *
130
+ * Additional data for complex systems:
131
+ *
132
+ * ```typescript
133
+ * const handoff = new Exit({
134
+ * name: 'handoff_sales',
135
+ * description: 'Handoff to sales agent',
136
+ * metadata: {
137
+ * type: 'handoff',
138
+ * agent: 'sales',
139
+ * department: 'customer_service',
140
+ * },
141
+ * schema: z.object({
142
+ * reason: z.string(),
143
+ * context: z.record(z.any()),
144
+ * }),
145
+ * })
146
+ *
147
+ * // Use metadata in exit callbacks
148
+ * onExit: async (result) => {
149
+ * if (result.exit.metadata?.type === 'handoff') {
150
+ * await routeToAgent(result.exit.metadata.agent, result.result)
151
+ * }
152
+ * }
153
+ * ```
154
+ *
155
+ * ### Exit Callbacks and Hooks
156
+ *
157
+ * Custom logic when exits are triggered:
158
+ *
159
+ * ```typescript
160
+ * const result = await execute({
161
+ * instructions: 'Process customer request',
162
+ * exits: [approved, rejected],
163
+ * onExit: async (exitResult) => {
164
+ * // Called before execution completes
165
+ * await logOutcome(exitResult.exit.name, exitResult.result)
166
+ *
167
+ * // Can throw to prevent exit and force retry
168
+ * if (needsManagerApproval(exitResult)) {
169
+ * throw new Error('Manager approval required')
170
+ * }
171
+ * },
172
+ * client,
173
+ * })
174
+ * ```
175
+ *
176
+ * ## Exit Cloning and Modification
177
+ *
178
+ * Create variations of existing exits:
179
+ *
180
+ * ```typescript
181
+ * const baseExit = new Exit({
182
+ * name: 'base',
183
+ * description: 'Base exit',
184
+ * schema: z.object({ status: z.string() }),
185
+ * })
186
+ *
187
+ * const customExit = baseExit.clone().rename('custom')
188
+ * // Creates independent copy with new name
189
+ * ```
190
+ *
191
+ * ## Best Practices
192
+ *
193
+ * 1. **Descriptive Names**: Use clear, action-oriented names that describe the outcome
194
+ * 2. **Comprehensive Schemas**: Define complete data structures with descriptions
195
+ * 3. **Multiple Exits**: Design multiple exits for different execution paths
196
+ * 4. **Type Safety**: Always use `result.is(exit)` for type-safe result handling
197
+ * 5. **Documentation**: Provide clear descriptions for both the exit and schema fields
198
+ * 6. **Validation**: Use Zod's validation features (enums, constraints, defaults)
199
+ *
200
+ * @template T - The type of data this exit returns (inferred from schema)
201
+ *
202
+ * @see {@link ExecutionResult} For result handling
203
+ * @see {@link ThinkExit} Built-in thinking exit
204
+ * @see {@link ListenExit} Built-in chat listening exit
205
+ * @see {@link DefaultExit} Built-in completion exit
206
+ */
207
+ var Exit = class Exit {
208
+ /** The primary name of the exit (used in return statements) */
209
+ name;
210
+ /** Alternative names that can be used to reference this exit */
211
+ aliases = [];
212
+ /** Human-readable description of when this exit should be used */
213
+ description;
214
+ /** Additional metadata for orchestration and custom logic */
215
+ metadata;
216
+ /** JSON Schema for validating exit result data */
217
+ schema;
218
+ /**
219
+ * Returns the Zod schema equivalent of the JSON schema (if available).
220
+ * Used internally for validation and type inference.
221
+ */
222
+ get zSchema() {
223
+ return this.schema ? require_utils.fromJSONSchemaCompat(this.schema) : void 0;
224
+ }
225
+ /**
226
+ * Renames the exit and updates aliases accordingly.
227
+ *
228
+ * @param name - The new name for the exit (must be a valid identifier)
229
+ * @returns This exit instance for chaining
230
+ *
231
+ * @example
232
+ * ```typescript
233
+ * const exit = new Exit({ name: 'old_name', description: 'Test exit' })
234
+ * exit.rename('new_name')
235
+ * console.log(exit.name) // 'new_name'
236
+ * ```
237
+ */
238
+ rename(name) {
239
+ const before = this.name;
240
+ if (!require_utils.isValidIdentifier(name)) throw new Error(`Invalid name for exit ${name}. An exit name must start with a letter and contain only letters, numbers, and underscores. It must be 1-50 characters long.`);
241
+ this.name = name;
242
+ this.aliases = require_uniq.uniq([name, ...this.aliases.map((alias) => alias === before ? name : alias)]);
243
+ return this;
244
+ }
245
+ /**
246
+ * Creates a deep copy of this exit.
247
+ *
248
+ * The clone is completely independent and can be modified without affecting
249
+ * the original exit. This is useful for creating variations of existing exits.
250
+ *
251
+ * @returns A new Exit instance with the same configuration
252
+ *
253
+ * @example
254
+ * ```typescript
255
+ * const originalExit = new Exit({
256
+ * name: 'base',
257
+ * description: 'Base exit',
258
+ * schema: z.object({ status: z.string() }),
259
+ * })
260
+ *
261
+ * const customExit = originalExit.clone().rename('custom')
262
+ * // customExit is independent of originalExit
263
+ * ```
264
+ */
265
+ clone() {
266
+ return new Exit({
267
+ name: this.name,
268
+ aliases: [...this.aliases],
269
+ description: this.description,
270
+ metadata: JSON.parse(JSON.stringify(this.metadata)),
271
+ schema: this.zSchema
272
+ });
273
+ }
274
+ /**
275
+ * Type guard to check if this exit matches another exit by name.
276
+ *
277
+ * Used internally for type narrowing and exit comparison.
278
+ *
279
+ * @param exit - The exit to compare against
280
+ * @returns True if the exits have the same name
281
+ */
282
+ is(exit) {
283
+ return this.name === exit.name;
284
+ }
285
+ /**
286
+ * Type guard to check if an ExitResult matches this exit.
287
+ *
288
+ * @param result - The exit result to check
289
+ * @returns True if the result was created by this exit
290
+ */
291
+ match(result) {
292
+ return result.exit instanceof Exit && this.name === result.exit.name;
293
+ }
294
+ /**
295
+ * Serializes this exit to a JSON-compatible object.
296
+ *
297
+ * @returns JSON representation of the exit
298
+ *
299
+ * @example
300
+ * ```typescript
301
+ * const exit = new Exit({
302
+ * name: 'complete',
303
+ * description: 'Task completed successfully',
304
+ * })
305
+ *
306
+ * console.log(exit.toJSON())
307
+ * // { name: 'complete', aliases: [], description: 'Task completed successfully', metadata: {}, schema: undefined }
308
+ * ```
309
+ */
310
+ toJSON() {
311
+ return {
312
+ name: this.name,
313
+ aliases: [...this.aliases],
314
+ description: this.description,
315
+ metadata: { ...this.metadata },
316
+ schema: this.schema
317
+ };
318
+ }
319
+ /**
320
+ * Creates a new Exit instance.
321
+ *
322
+ * @param props - Exit configuration
323
+ * @param props.name - Primary name for the exit (must be valid identifier)
324
+ * @param props.description - Human-readable description of the exit's purpose
325
+ * @param props.aliases - Alternative names that can be used to reference this exit
326
+ * @param props.metadata - Additional data for orchestration and custom logic
327
+ * @param props.schema - Zod schema for validating exit result data
328
+ *
329
+ * @example
330
+ * ```typescript
331
+ * // Simple exit without data validation
332
+ * const exit = new Exit({
333
+ * name: 'complete',
334
+ * description: 'Task completed successfully',
335
+ * })
336
+ * ```
337
+ *
338
+ * @example
339
+ * ```typescript
340
+ * // Exit with typed result data
341
+ * const approval = new Exit({
342
+ * name: 'approved',
343
+ * description: 'Request approved by system',
344
+ * schema: z.object({
345
+ * amount: z.number().positive(),
346
+ * reference: z.string().min(1),
347
+ * timestamp: z.date().default(() => new Date()),
348
+ * }),
349
+ * })
350
+ * ```
351
+ *
352
+ * @example
353
+ * ```typescript
354
+ * // Exit with aliases and metadata
355
+ * const handoff = new Exit({
356
+ * name: 'handoff_support',
357
+ * aliases: ['escalate', 'transfer'],
358
+ * description: 'Transfer to human support agent',
359
+ * metadata: {
360
+ * department: 'customer_service',
361
+ * priority: 'high',
362
+ * },
363
+ * schema: z.object({
364
+ * reason: z.string(),
365
+ * customerData: z.record(z.any()),
366
+ * }),
367
+ * })
368
+ * ```
369
+ */
370
+ constructor(props) {
371
+ if (!require_utils.isValidIdentifier(props.name)) throw new Error(`Invalid name for exit ${props.name}. A exit name must start with a letter and contain only letters, numbers, and underscores. It must be 1-50 characters long.`);
372
+ if (typeof props.description !== "string" || props.description.trim().length === 0) throw new Error(`Invalid description for exit ${props.name}. Expected a non-empty string, but got type "${typeof props.description}"`);
373
+ if (props.metadata !== void 0 && typeof props.metadata !== "object") throw new Error(`Invalid metadata for exit ${props.name}. Expected an object, but got type "${typeof props.metadata}"`);
374
+ if (props.aliases !== void 0 && !Array.isArray(props.aliases)) throw new Error(`Invalid aliases for exit ${props.name}. Expected an array, but got type "${typeof props.aliases}"`);
375
+ if (props.aliases && props.aliases.some((alias) => !require_utils.isValidIdentifier(alias))) throw new Error(`Invalid aliases for exit ${props.name}. Expected an array of valid identifiers.`);
376
+ if (typeof props.schema !== "undefined") {
377
+ if (require_utils.isZuiSchema(props.schema)) this.schema = _bpinternal_zui.transforms.toJSONSchemaLegacy(props.schema);
378
+ else if (require_utils.isJsonSchema(props.schema)) this.schema = props.schema;
379
+ else throw new Error(`Invalid input schema for exit ${props.name}. Expected a ZodType or JSONSchema, but got type "${typeof props.schema}"`);
380
+ }
381
+ this.name = props.name;
382
+ this.aliases = require_uniq.uniq([props.name, ...props.aliases ?? []]);
383
+ this.description = props.description;
384
+ this.metadata = props.metadata ?? {};
385
+ }
386
+ /**
387
+ * Ensures all exits in an array have unique names by renaming duplicates.
388
+ *
389
+ * When multiple exits have the same name, this method appends numbers to
390
+ * create unique names (e.g., 'exit1', 'exit2'). This prevents naming conflicts
391
+ * in execution contexts with multiple exits.
392
+ *
393
+ * @param exits - Array of exits that may have duplicate names
394
+ * @returns Array of exits with guaranteed unique names
395
+ *
396
+ * @example
397
+ * ```typescript
398
+ * const exit1 = new Exit({ name: 'done', description: 'First done' })
399
+ * const exit2 = new Exit({ name: 'done', description: 'Second done' })
400
+ *
401
+ * const uniqueExits = Exit.withUniqueNames([exit1, exit2])
402
+ * // Result: [{ name: 'done' }, { name: 'done1' }]
403
+ * ```
404
+ */
405
+ static withUniqueNames = (exits) => {
406
+ const names = /* @__PURE__ */ new Set();
407
+ return exits.map((exit) => {
408
+ if (exits.filter((t) => t.name === exit.name).length === 1) return exit;
409
+ let counter = 1;
410
+ let exitName = exit.name + counter;
411
+ while (names.has(exitName)) exitName = `${exit.name}${++counter}`;
412
+ return exit.rename(exitName);
413
+ });
414
+ };
415
+ };
416
+ //#endregion
417
+ Object.defineProperty(exports, "Exit", {
418
+ enumerable: true,
419
+ get: function() {
420
+ return Exit;
421
+ }
422
+ });
423
+ Object.defineProperty(exports, "exit_exports", {
424
+ enumerable: true,
425
+ get: function() {
426
+ return exit_exports;
427
+ }
428
+ });