llmz 0.0.13 → 0.0.15

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 (52) hide show
  1. package/CLAUDE.md +363 -0
  2. package/README.md +61 -34
  3. package/dist/abort-signal.d.ts +40 -0
  4. package/dist/chat.d.ts +325 -0
  5. package/dist/{chunk-KH6JQYQA.js → chunk-2D2DE7CD.js} +2 -2
  6. package/dist/chunk-3G3BS5IA.cjs +256 -0
  7. package/dist/{chunk-SNDVQU5A.js → chunk-3JYCCI4S.js} +1 -1
  8. package/dist/chunk-A7QHWVD7.js +493 -0
  9. package/dist/{chunk-IH2WQFO5.js → chunk-EE6NVDID.js} +1 -1
  10. package/dist/{chunk-4L6D2A6O.cjs → chunk-FZJHYLM2.cjs} +14 -14
  11. package/dist/{chunk-JGVAZO4X.cjs → chunk-GZPN7RGH.cjs} +2 -2
  12. package/dist/{chunk-SHJDRZF5.cjs → chunk-PIDLNYIP.cjs} +25 -25
  13. package/dist/{chunk-PRVFVXT4.js → chunk-RBRTK37G.js} +383 -4
  14. package/dist/{chunk-HJKOSEH2.cjs → chunk-TCRRSS44.cjs} +397 -18
  15. package/dist/chunk-VPTFUOIK.js +256 -0
  16. package/dist/{chunk-276Q6EWP.cjs → chunk-WHNOR4ZU.cjs} +3 -0
  17. package/dist/chunk-XGJOEQMW.cjs +493 -0
  18. package/dist/{chunk-4MNIJGK6.js → chunk-ZORRILUV.js} +3 -0
  19. package/dist/context.d.ts +412 -4
  20. package/dist/{dual-modes-T53P72CH.js → dual-modes-7FI4T35O.js} +3 -3
  21. package/dist/{dual-modes-VLIGPIHX.cjs → dual-modes-OFHV2C3X.cjs} +4 -4
  22. package/dist/exit-XAYKJ6TR.cjs +8 -0
  23. package/dist/{exit-YORW76T3.js → exit-YLO7BY7Z.js} +2 -2
  24. package/dist/exit.d.ts +369 -2
  25. package/dist/index.cjs +253 -28
  26. package/dist/index.d.ts +71 -1
  27. package/dist/index.js +242 -17
  28. package/dist/{llmz-ROOX7RYI.js → llmz-67EZPJ4E.js} +113 -39
  29. package/dist/{llmz-QLZBDG2Z.cjs → llmz-WVNKAMCP.cjs} +123 -49
  30. package/dist/llmz.d.ts +142 -5
  31. package/dist/objects.d.ts +350 -1
  32. package/dist/result.d.ts +809 -6
  33. package/dist/snapshots.d.ts +181 -1
  34. package/dist/{tool-QP4MVRWI.cjs → tool-O4SFRIE4.cjs} +4 -4
  35. package/dist/{tool-N6ODRRGH.js → tool-PCOYOCRH.js} +3 -3
  36. package/dist/tool.d.ts +470 -2
  37. package/dist/{truncator-IY2MXOMC.js → truncator-BSP6PQPC.js} +2 -2
  38. package/dist/truncator-W3NXBLYJ.cjs +10 -0
  39. package/dist/types.d.ts +3 -0
  40. package/dist/{typings-GDMY6VY2.js → typings-WYHEFCYB.js} +2 -2
  41. package/dist/{typings-2CPHOFDN.cjs → typings-Y45GMPZT.cjs} +3 -3
  42. package/dist/utils-L5QAQXV2.cjs +39 -0
  43. package/dist/{utils-N24IHDFA.js → utils-RQHQ2KOG.js} +1 -1
  44. package/docs/TODO.md +919 -0
  45. package/package.json +3 -3
  46. package/dist/chunk-C6WNNTEV.cjs +0 -212
  47. package/dist/chunk-GWFYZDUR.cjs +0 -105
  48. package/dist/chunk-JAGB2AOU.js +0 -212
  49. package/dist/chunk-JMSZKB4T.js +0 -105
  50. package/dist/exit-TRXEU4OU.cjs +0 -8
  51. package/dist/truncator-DUMWEGQO.cjs +0 -10
  52. package/dist/utils-A7WNEFTA.cjs +0 -39
package/dist/context.d.ts CHANGED
@@ -9,8 +9,8 @@ import { ObjectInstance } from './objects.js';
9
9
  import { LLMzPrompts, Prompt } from './prompts/prompt.js';
10
10
  import { Snapshot } from './snapshots.js';
11
11
  import { Tool } from './tool.js';
12
- import { TranscriptArray } from './transcript.js';
13
- import { ObjectMutation, Trace } from './types.js';
12
+ import { Transcript, TranscriptArray } from './transcript.js';
13
+ import { ObjectMutation, Serializable, Trace } from './types.js';
14
14
  type Model = Parameters<InstanceType<typeof Cognitive>['generateContent']>[0]['model'];
15
15
  export type IterationParameters = {
16
16
  transcript: TranscriptArray;
@@ -79,8 +79,220 @@ export declare namespace IterationStatuses {
79
79
  };
80
80
  };
81
81
  }
82
+ /**
83
+ * Built-in exit for requesting thinking time during agent execution.
84
+ *
85
+ * The ThinkExit allows agents to pause execution and reflect on the current situation,
86
+ * variables, and context before continuing. There are two ways to trigger thinking:
87
+ *
88
+ * 1. **Agent-initiated**: Agent calls `return { action: 'think' }` to pause and reflect
89
+ * 2. **Tool/Hook-initiated**: Tools or hooks throw `ThinkSignal` to force agent reflection
90
+ *
91
+ * This exit is automatically available in all LLMz executions and is commonly used for:
92
+ * - Complex decision making that requires analysis
93
+ * - Debugging and understanding current variable state
94
+ * - Planning multi-step operations
95
+ * - Tool feedback and result processing
96
+ * - Reflecting on previous iterations and results
97
+ *
98
+ * @example
99
+ * ```typescript
100
+ * // Agent retrieves web search results and decides to think about them
101
+ * const results = await searchWeb(query)
102
+ *
103
+ * // Agent decides it needs to think (look at the search results) before responding
104
+ * return { action: 'think', results }
105
+ * ```
106
+ *
107
+ * Sometimes, as the author of the tool, you may want to always force the agent to think about the results.
108
+ * In this case, you can throw a `ThinkSignal` from the tool handler to trigger thinking.
109
+ *
110
+ * @example
111
+ * ```typescript
112
+ * // Tool-initiated thinking using ThinkSignal
113
+ * import { ThinkSignal } from 'llmz'
114
+ *
115
+ * const searchTool = new Tool({
116
+ * name: 'search',
117
+ * handler: async ({ query }) => {
118
+ * const results = await performSearch(query)
119
+ *
120
+ * if (!results.length) {
121
+ * // Force agent to think about alternative approaches
122
+ * throw new ThinkSignal(
123
+ * 'No search results found',
124
+ * 'No results were found. Consider rephrasing the query or using a different approach.'
125
+ * )
126
+ * }
127
+ *
128
+ * // Provide context for agent to process results
129
+ * throw new ThinkSignal(
130
+ * 'Search completed with results',
131
+ * `Found ${results.length} results. Process them carefully and provide citations.`
132
+ * )
133
+ * }
134
+ * })
135
+ * ```
136
+ * When an iteration ends with ThinkExit, the agent will automatically loop and start a new iteration to continue processing, unless iteration limit is reached.
137
+ *
138
+ * The thinking process helps agents:
139
+ * - Avoid rushing into incorrect solutions
140
+ * - Better understand complex problems and tool results
141
+ * - Maintain variable state across iterations
142
+ * - Process feedback from tools and hooks
143
+ * - Provide more thoughtful and accurate responses
144
+ */
82
145
  export declare const ThinkExit: Exit<unknown>;
146
+ /**
147
+ * Built-in exit for waiting for user input in chat mode.
148
+ *
149
+ * The ListenExit is automatically available when chat mode is enabled (when a Chat
150
+ * instance is provided to execute()). When an agent calls `return { action: 'listen' }`,
151
+ * the execution pauses and waits for user input before continuing the conversation.
152
+ *
153
+ * This exit is essential for interactive conversational agents and is used to:
154
+ * - Wait for user responses in chat interfaces
155
+ * - Pause execution until user provides input
156
+ * - Enable back-and-forth conversation flow
157
+ * - Allow users to guide the conversation direction
158
+ *
159
+ * The ListenExit is only available in chat mode - it will not be present in
160
+ * worker mode executions where no chat interface is provided.
161
+ *
162
+ * @example
163
+ * ```typescript
164
+ * // Agent generated code using ListenExit in chat mode
165
+ * yield <Message>What would you like me to help you with today?</Message>
166
+ * yield <Button action="postback" label="Get Weather" value="weather" />
167
+ * yield <Button action="postback" label="Set Reminder" value="reminder" />
168
+ *
169
+ * // Wait for user to respond
170
+ * return { action: 'listen' }
171
+ * ```
172
+ *
173
+ * @example
174
+ * ```typescript
175
+ * // Standard chat interaction pattern
176
+ * const calculation = 2 + 8
177
+ * yield <Message>The result of `2 + 8` is **{calculation}**.</Message>
178
+ * return { action: 'listen' }
179
+ * ```
180
+ *
181
+ * @example
182
+ * ```typescript
183
+ * // CLI chat example with ListenExit handling
184
+ * const chat = new CLIChat()
185
+ *
186
+ * while (chat.iterate()) {
187
+ * const result = await execute({
188
+ * instructions: 'Help the user with their questions',
189
+ * chat,
190
+ * client,
191
+ * })
192
+ *
193
+ * if (result.is(ListenExit)) {
194
+ * // CLIChat handles prompting user automatically
195
+ * continue
196
+ * } else {
197
+ * console.log('Conversation ended')
198
+ * break
199
+ * }
200
+ * }
201
+ * ```
202
+ *
203
+ * The ListenExit enables natural conversation flow where:
204
+ * - Agent sends messages and waits for responses
205
+ * - User provides input to guide the conversation
206
+ * - Conversation continues iteratively until completion
207
+ * - Chat interface manages the input/output cycle
208
+ */
83
209
  export declare const ListenExit: Exit<unknown>;
210
+ /**
211
+ * Default exit used when no custom exits are provided.
212
+ *
213
+ * The DefaultExit is automatically used in worker mode when no custom exits are defined.
214
+ * It provides a standard way to complete execution with either success or failure outcomes.
215
+ * The exit uses a discriminated union schema to ensure type-safe handling of both success
216
+ * and error cases.
217
+ *
218
+ * This exit is commonly used for:
219
+ * - Simple worker mode executions without custom completion logic
220
+ * - Standardized success/failure reporting
221
+ * - Basic task completion with result or error information
222
+ * - Default fallback when no specific exit behavior is needed
223
+ *
224
+ * @example
225
+ * ```typescript
226
+ * // Agent generated code using DefaultExit for successful completion
227
+ * const data = await fetchUserData(userId)
228
+ * const processedResult = processData(data)
229
+ *
230
+ * return {
231
+ * action: 'done',
232
+ * success: true,
233
+ * result: processedResult
234
+ * }
235
+ * ```
236
+ *
237
+ * @example
238
+ * ```typescript
239
+ * // Agent generated code using DefaultExit for error cases
240
+ * try {
241
+ * const result = await riskyOperation()
242
+ * return { action: 'done', success: true, result }
243
+ * } catch (error) {
244
+ * return {
245
+ * action: 'done',
246
+ * success: false,
247
+ * error: `Operation failed: ${error.message}`
248
+ * }
249
+ * }
250
+ * ```
251
+ *
252
+ * @example
253
+ * ```typescript
254
+ * import { execute, DefaultExit } from 'llmz'
255
+ *
256
+ * // Handling DefaultExit in execution results
257
+ * const result = await execute({
258
+ * instructions: 'Process the user data and return results',
259
+ * // No custom exits provided - DefaultExit will be used
260
+ * client,
261
+ * })
262
+ *
263
+ * if (result.is(DefaultExit)) {
264
+ * if (result.output.success) {
265
+ * console.log('Success:', result.output.result)
266
+ * } else {
267
+ * console.error('Error:', result.output.error)
268
+ * }
269
+ * }
270
+ * ```
271
+ *
272
+ * @example
273
+ * ```typescript
274
+ * // Worker mode execution with automatic DefaultExit
275
+ * const result = await execute({
276
+ * instructions: 'Calculate fibonacci numbers up to 100',
277
+ * tools: [mathTools],
278
+ * client,
279
+ * // No chat provided = worker mode
280
+ * // No exits provided = DefaultExit automatically added
281
+ * })
282
+ *
283
+ * // Result will use DefaultExit for completion
284
+ * if (result.isSuccess() && result.is(DefaultExit)) {
285
+ * const { success, result: data, error } = result.output
286
+ * // Handle success/failure cases
287
+ * }
288
+ * ```
289
+ *
290
+ * The DefaultExit provides a consistent interface for:
291
+ * - Type-safe success/failure handling
292
+ * - Standardized result reporting across different executions
293
+ * - Automatic fallback behavior when no custom exits are defined
294
+ * - Clear separation between successful results and error conditions
295
+ */
84
296
  export declare const DefaultExit: Exit<{
85
297
  success: true;
86
298
  result?: any;
@@ -88,7 +300,38 @@ export declare const DefaultExit: Exit<{
88
300
  error: string;
89
301
  success: false;
90
302
  }>;
91
- export declare class Iteration {
303
+ export declare namespace Iteration {
304
+ type JSON = {
305
+ id: string;
306
+ messages: LLMzPrompts.Message[];
307
+ code?: string;
308
+ traces: Trace[];
309
+ variables: Record<string, any>;
310
+ started_ts: number;
311
+ ended_ts?: number;
312
+ status: IterationStatus;
313
+ mutations: ObjectMutation[];
314
+ llm?: {
315
+ started_at: number;
316
+ ended_at: number;
317
+ status: 'success' | 'error';
318
+ cached: boolean;
319
+ tokens: number;
320
+ spend: number;
321
+ output: string;
322
+ model: string;
323
+ };
324
+ transcript: Transcript.Message[];
325
+ tools: Tool.JSON[];
326
+ objects: ObjectInstance.JSON[];
327
+ exits: Exit.JSON[];
328
+ instructions?: string;
329
+ duration?: string;
330
+ error?: string | null;
331
+ isChatEnabled?: boolean;
332
+ };
333
+ }
334
+ export declare class Iteration implements Serializable<Iteration.JSON> {
92
335
  id: string;
93
336
  messages: LLMzPrompts.Message[];
94
337
  code?: string;
@@ -138,8 +381,82 @@ export declare class Iteration {
138
381
  variables: Record<string, any>;
139
382
  });
140
383
  end(status: IterationStatus): void;
384
+ toJSON(): {
385
+ id: string;
386
+ messages: LLMzPrompts.Message[];
387
+ code: string | undefined;
388
+ traces: import("./types.js").Traces.Trace[];
389
+ variables: Record<string, any>;
390
+ started_ts: number;
391
+ ended_ts: number | undefined;
392
+ status: IterationStatus;
393
+ mutations: ObjectMutation[];
394
+ llm: {
395
+ started_at: number;
396
+ ended_at: number;
397
+ status: "success" | "error";
398
+ cached: boolean;
399
+ tokens: number;
400
+ spend: number;
401
+ output: string;
402
+ model: string;
403
+ } | undefined;
404
+ transcript: Transcript.Message[];
405
+ tools: {
406
+ name: string;
407
+ aliases: string[];
408
+ description: string | undefined;
409
+ metadata: Record<string, unknown>;
410
+ input: import("json-schema").JSONSchema7 | undefined;
411
+ output: import("json-schema").JSONSchema7 | undefined;
412
+ staticInputValues: unknown;
413
+ maxRetries: number;
414
+ }[];
415
+ objects: {
416
+ name: string;
417
+ description: string | undefined;
418
+ properties: import("./objects.js").ObjectProperty[] | undefined;
419
+ tools: {
420
+ name: string;
421
+ aliases: string[];
422
+ description: string | undefined;
423
+ metadata: Record<string, unknown>;
424
+ input: import("json-schema").JSONSchema7 | undefined;
425
+ output: import("json-schema").JSONSchema7 | undefined;
426
+ staticInputValues: unknown;
427
+ maxRetries: number;
428
+ }[];
429
+ metadata: Record<string, unknown> | undefined;
430
+ }[];
431
+ exits: {
432
+ name: string;
433
+ aliases: string[];
434
+ description: string;
435
+ metadata: {
436
+ [x: string]: unknown;
437
+ };
438
+ schema: import("json-schema").JSONSchema7 | undefined;
439
+ }[];
440
+ instructions: string | undefined;
441
+ duration: string;
442
+ error: string | null;
443
+ isChatEnabled: boolean;
444
+ };
445
+ }
446
+ export declare namespace Context {
447
+ type JSON = {
448
+ id: string;
449
+ iterations: Iteration.JSON[];
450
+ iteration: number;
451
+ timeout: number;
452
+ loop: number;
453
+ temperature: number;
454
+ model?: Model;
455
+ metadata: Record<string, any>;
456
+ snapshot?: Snapshot.JSON;
457
+ };
141
458
  }
142
- export declare class Context {
459
+ export declare class Context implements Serializable<Context.JSON> {
143
460
  id: string;
144
461
  chat?: Chat;
145
462
  instructions?: ValueOrGetter<string, Context>;
@@ -172,5 +489,96 @@ export declare class Context {
172
489
  snapshot?: Snapshot;
173
490
  timeout?: number;
174
491
  });
492
+ toJSON(): {
493
+ id: string;
494
+ iterations: {
495
+ id: string;
496
+ messages: LLMzPrompts.Message[];
497
+ code: string | undefined;
498
+ traces: import("./types.js").Traces.Trace[];
499
+ variables: Record<string, any>;
500
+ started_ts: number;
501
+ ended_ts: number | undefined;
502
+ status: IterationStatus;
503
+ mutations: ObjectMutation[];
504
+ llm: {
505
+ started_at: number;
506
+ ended_at: number;
507
+ status: "success" | "error";
508
+ cached: boolean;
509
+ tokens: number;
510
+ spend: number;
511
+ output: string;
512
+ model: string;
513
+ } | undefined;
514
+ transcript: Transcript.Message[];
515
+ tools: {
516
+ name: string;
517
+ aliases: string[];
518
+ description: string | undefined;
519
+ metadata: Record<string, unknown>;
520
+ input: import("json-schema").JSONSchema7 | undefined;
521
+ output: import("json-schema").JSONSchema7 | undefined;
522
+ staticInputValues: unknown;
523
+ maxRetries: number;
524
+ }[];
525
+ objects: {
526
+ name: string;
527
+ description: string | undefined;
528
+ properties: import("./objects.js").ObjectProperty[] | undefined;
529
+ tools: {
530
+ name: string;
531
+ aliases: string[];
532
+ description: string | undefined;
533
+ metadata: Record<string, unknown>;
534
+ input: import("json-schema").JSONSchema7 | undefined;
535
+ output: import("json-schema").JSONSchema7 | undefined;
536
+ staticInputValues: unknown;
537
+ maxRetries: number;
538
+ }[];
539
+ metadata: Record<string, unknown> | undefined;
540
+ }[];
541
+ exits: {
542
+ name: string;
543
+ aliases: string[];
544
+ description: string;
545
+ metadata: {
546
+ [x: string]: unknown;
547
+ };
548
+ schema: import("json-schema").JSONSchema7 | undefined;
549
+ }[];
550
+ instructions: string | undefined;
551
+ duration: string;
552
+ error: string | null;
553
+ isChatEnabled: boolean;
554
+ }[];
555
+ iteration: number;
556
+ timeout: number;
557
+ loop: number;
558
+ temperature: number;
559
+ model: "best" | "fast" | `${string}:${string}` | undefined;
560
+ metadata: Record<string, any>;
561
+ snapshot: {
562
+ id: string;
563
+ reason: string | undefined;
564
+ stack: string;
565
+ variables: ({
566
+ name: string;
567
+ type: string;
568
+ bytes: number;
569
+ preview?: string;
570
+ value?: unknown;
571
+ truncated: boolean;
572
+ } & ({
573
+ truncated: true;
574
+ preview: string;
575
+ } | {
576
+ truncated: false;
577
+ value: unknown;
578
+ }))[];
579
+ toolCall: import("./errors.js").ToolCall | undefined;
580
+ status: import("./snapshots.js").SnapshotStatus;
581
+ } | undefined;
582
+ };
175
583
  }
176
584
  export {};
@@ -1,11 +1,11 @@
1
1
  import {
2
2
  DualModePrompt
3
- } from "./chunk-KH6JQYQA.js";
3
+ } from "./chunk-2D2DE7CD.js";
4
4
  import "./chunk-JQBT7UWN.js";
5
5
  import "./chunk-GGWM6X2K.js";
6
6
  import "./chunk-ORQP26SZ.js";
7
- import "./chunk-SNDVQU5A.js";
8
- import "./chunk-4MNIJGK6.js";
7
+ import "./chunk-3JYCCI4S.js";
8
+ import "./chunk-ZORRILUV.js";
9
9
  import "./chunk-7WRN4E42.js";
10
10
  export {
11
11
  DualModePrompt
@@ -1,12 +1,12 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
- var _chunkSHJDRZF5cjs = require('./chunk-SHJDRZF5.cjs');
3
+ var _chunkPIDLNYIPcjs = require('./chunk-PIDLNYIP.cjs');
4
4
  require('./chunk-IKSIOIIP.cjs');
5
5
  require('./chunk-ZRCU35UV.cjs');
6
6
  require('./chunk-KMZDFWYZ.cjs');
7
- require('./chunk-JGVAZO4X.cjs');
8
- require('./chunk-276Q6EWP.cjs');
7
+ require('./chunk-GZPN7RGH.cjs');
8
+ require('./chunk-WHNOR4ZU.cjs');
9
9
  require('./chunk-UQOBUJIQ.cjs');
10
10
 
11
11
 
12
- exports.DualModePrompt = _chunkSHJDRZF5cjs.DualModePrompt;
12
+ exports.DualModePrompt = _chunkPIDLNYIPcjs.DualModePrompt;
@@ -0,0 +1,8 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
+
3
+ var _chunk3G3BS5IAcjs = require('./chunk-3G3BS5IA.cjs');
4
+ require('./chunk-WHNOR4ZU.cjs');
5
+ require('./chunk-UQOBUJIQ.cjs');
6
+
7
+
8
+ exports.Exit = _chunk3G3BS5IAcjs.Exit;
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  Exit
3
- } from "./chunk-JMSZKB4T.js";
4
- import "./chunk-4MNIJGK6.js";
3
+ } from "./chunk-VPTFUOIK.js";
4
+ import "./chunk-ZORRILUV.js";
5
5
  import "./chunk-7WRN4E42.js";
6
6
  export {
7
7
  Exit