llmz 0.8.0 → 1.0.1

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 (224) hide show
  1. package/DOCS.md +230 -1796
  2. package/ERRORS.md +98 -0
  3. package/README.md +96 -548
  4. package/dist/chat/chat.d.ts +63 -0
  5. package/dist/chat/component.d.ts +31 -0
  6. package/dist/chat/component.default.d.ts +58 -0
  7. package/dist/chat/response.d.ts +15 -0
  8. package/dist/compiler/compiler.d.ts +2 -7
  9. package/dist/compiler/plugins/dynamic-code.d.ts +7 -0
  10. package/dist/compiler/plugins/replace-comment.d.ts +2 -3
  11. package/dist/compiler/plugins/return-async.d.ts +5 -4
  12. package/dist/compiler/plugins/termination.d.ts +8 -0
  13. package/dist/compiler/plugins/tool-resolution.d.ts +4 -0
  14. package/dist/compiler/plugins/variable-extraction.d.ts +2 -8
  15. package/dist/component-BpMVZYOa.cjs +649 -0
  16. package/dist/component-C27qu6Zv.js +614 -0
  17. package/dist/context.d.ts +74 -385
  18. package/dist/custom-client-BDkgBR2w.js +1631 -0
  19. package/dist/custom-client-MytucGI-.cjs +1708 -0
  20. package/dist/custom-client.d.ts +12 -2
  21. package/dist/errors/catalog.d.ts +287 -0
  22. package/dist/errors/hooks.d.ts +2 -0
  23. package/dist/errors-H4JJFH3S.js +620 -0
  24. package/dist/errors-IhnTKUp7.cjs +871 -0
  25. package/dist/errors.d.ts +18 -36
  26. package/dist/execute-BYI_U5sR.js +1943 -0
  27. package/dist/execute-DLas4V2n.cjs +1944 -0
  28. package/dist/{exit-DUM96DN1.cjs → exit-BUgJ5qi5.cjs} +38 -28
  29. package/dist/{exit-CDdxDBRT.js → exit-CPNk3vbz.js} +37 -27
  30. package/dist/exit.d.ts +19 -16
  31. package/dist/index.cjs +181 -999
  32. package/dist/index.d.ts +25 -28
  33. package/dist/index.js +141 -974
  34. package/dist/index.workerd.js +7725 -9531
  35. package/dist/inspect.d.ts +19 -3
  36. package/dist/inspection.d.ts +38 -0
  37. package/dist/native-Bm6L9zOb.cjs +212 -0
  38. package/dist/native-DvTH9vFe.js +177 -0
  39. package/dist/objects.d.ts +2 -57
  40. package/dist/prompts/native.d.ts +2 -0
  41. package/dist/prompts/prompt.d.ts +10 -98
  42. package/dist/quickjs-variant-DJwYDSsb.cjs +1073 -0
  43. package/dist/quickjs-variant-DlPi9-Sx.js +984 -0
  44. package/dist/result.d.ts +30 -861
  45. package/dist/runtime/execute.d.ts +3 -2
  46. package/dist/runtime/execution-activity.d.ts +11 -0
  47. package/dist/runtime/execution-activity.test.d.ts +1 -0
  48. package/dist/runtime/execution-diagnostics.d.ts +11 -0
  49. package/dist/runtime/execution-diagnostics.test.d.ts +1 -0
  50. package/dist/runtime/execution-report.d.ts +48 -0
  51. package/dist/runtime/execution-report.test.d.ts +1 -0
  52. package/dist/runtime/execution-safety.test.d.ts +1 -0
  53. package/dist/runtime/exit-control-flow.test.d.ts +1 -0
  54. package/dist/runtime/fixtures/chat.d.ts +8 -0
  55. package/dist/runtime/fixtures/long-search.d.ts +19 -0
  56. package/dist/runtime/fixtures/long-search.test.d.ts +1 -0
  57. package/dist/runtime/fixtures/native-client.d.ts +33 -0
  58. package/dist/runtime/fixtures/protocol-languages.d.ts +74 -0
  59. package/dist/runtime/generate.d.ts +23 -27
  60. package/dist/runtime/generate.test.d.ts +1 -0
  61. package/dist/runtime/hook-provenance.test.d.ts +1 -0
  62. package/dist/runtime/inspection-hook.test.d.ts +1 -0
  63. package/dist/runtime/inspection-values.d.ts +14 -0
  64. package/dist/runtime/inspection-values.test.d.ts +1 -0
  65. package/dist/runtime/interruption-control-flow.test.d.ts +1 -0
  66. package/dist/runtime/interruption-settings.test.d.ts +1 -0
  67. package/dist/runtime/javascript-api.d.ts +59 -0
  68. package/dist/runtime/javascript-api.test.d.ts +1 -0
  69. package/dist/runtime/native-review.test.d.ts +1 -0
  70. package/dist/runtime/native-tools.d.ts +18 -0
  71. package/dist/runtime/native-tools.test.d.ts +1 -0
  72. package/dist/runtime/object-memory.test.d.ts +1 -0
  73. package/dist/runtime/protocol-adherence.test.d.ts +1 -0
  74. package/dist/runtime/single-tool.test.d.ts +1 -0
  75. package/dist/runtime/token-budget.d.ts +22 -0
  76. package/dist/runtime/token-budget.test.d.ts +1 -0
  77. package/dist/runtime/tool-wrapper.d.ts +4 -5
  78. package/dist/runtime/traces.test.d.ts +1 -0
  79. package/dist/runtime/truncation.test.d.ts +1 -0
  80. package/dist/runtime/types.d.ts +41 -36
  81. package/dist/runtime/variable-recovery.test.d.ts +1 -0
  82. package/dist/runtime/vm-context.d.ts +6 -1
  83. package/dist/runtime/worker-completion.test.d.ts +1 -0
  84. package/dist/runtime-names.d.ts +4 -0
  85. package/dist/schema-Aer_W7Al.js +45 -0
  86. package/dist/schema-DBkjo0b7.cjs +68 -0
  87. package/dist/schema.d.ts +10 -0
  88. package/dist/schema.test.d.ts +1 -0
  89. package/dist/session/compactor.d.ts +51 -0
  90. package/dist/session/compactor.test.d.ts +1 -0
  91. package/dist/session/history.d.ts +36 -0
  92. package/dist/session/history.test.d.ts +1 -0
  93. package/dist/session/json.d.ts +3 -0
  94. package/dist/session/json.test.d.ts +1 -0
  95. package/dist/session/memory-codec.d.ts +9 -0
  96. package/dist/session/memory-codec.test.d.ts +1 -0
  97. package/dist/session/memory-render.d.ts +15 -0
  98. package/dist/session/memory-render.test.d.ts +1 -0
  99. package/dist/session/memory.d.ts +112 -0
  100. package/dist/session/memory.test.d.ts +1 -0
  101. package/dist/session/messages.d.ts +17 -0
  102. package/dist/session/messages.test.d.ts +1 -0
  103. package/dist/session/serialization.d.ts +34 -0
  104. package/dist/session/serialization.test.d.ts +1 -0
  105. package/dist/session/session-memory.test.d.ts +1 -0
  106. package/dist/session/session.d.ts +117 -0
  107. package/dist/session/session.test.d.ts +1 -0
  108. package/dist/{transcript.d.ts → session/transcript.d.ts} +4 -8
  109. package/dist/session/transcript.test.d.ts +1 -0
  110. package/dist/{tool-Bv8N4IPj.js → tool--gK91eez.js} +144 -92
  111. package/dist/{tool-Dy6AJpJH.cjs → tool-Da_F-N-5.cjs} +146 -94
  112. package/dist/tool.d.ts +25 -17
  113. package/dist/truncate-BSoH2-nY.js +79 -0
  114. package/dist/truncate-nV3Mxr6-.cjs +102 -0
  115. package/dist/truncate.d.ts +24 -0
  116. package/dist/truncate.test.d.ts +1 -0
  117. package/dist/types.d.ts +27 -9
  118. package/dist/{typings-3lsqCbkn.cjs → typings-C1ihexWE.cjs} +38 -193
  119. package/dist/{typings-CrBa7Tyy.js → typings-D9_aC8aq.js} +38 -175
  120. package/dist/typings-formatter.d.ts +1 -1
  121. package/dist/typings.d.ts +3 -0
  122. package/dist/{uniq-BYQ7GWwb.js → uniq-DfUeSNaX.js} +2 -2
  123. package/dist/{uniq-G872RWmE.cjs → uniq-DiXgysNV.cjs} +6 -18
  124. package/dist/{_baseIteratee-P_firB7b.js → utils-BOFzb8Ok.js} +1134 -16
  125. package/dist/{_baseIteratee-CF4qP2a8.cjs → utils-CTlsXvdj.cjs} +1281 -84
  126. package/dist/vm/drivers/node.d.ts +1 -1
  127. package/dist/vm/drivers/quickjs.d.ts +1 -1
  128. package/dist/vm/errors.d.ts +4 -4
  129. package/dist/vm/index.d.ts +3 -4
  130. package/dist/vm/instrument.d.ts +14 -6
  131. package/dist/vm/member-proxy.d.ts +4 -0
  132. package/dist/vm/member-proxy.test.d.ts +1 -0
  133. package/dist/vm/memory.test.d.ts +1 -0
  134. package/dist/vm/return-semantics.test.d.ts +1 -0
  135. package/dist/vm/types.d.ts +18 -3
  136. package/dist/{vm-BLsHw1da.js → vm-8jxt8PTK.js} +1162 -180
  137. package/dist/{vm-D6s1zF6M.cjs → vm-XuVPuW3Y.cjs} +1195 -207
  138. package/package.json +13 -6
  139. package/dist/_baseForOwn-BfWDTL8L.cjs +0 -53
  140. package/dist/_baseForOwn-CFhWe_W6.js +0 -48
  141. package/dist/_getPrototype-CG7Dxrrz.js +0 -6
  142. package/dist/_getPrototype-DQDlNVVM.cjs +0 -10
  143. package/dist/_overArg-C5Hy7oCi.cjs +0 -196
  144. package/dist/_overArg-CRYbj1NH.js +0 -155
  145. package/dist/chat.d.ts +0 -401
  146. package/dist/common-DdArNQhl.cjs +0 -643
  147. package/dist/common-pgJjFu2Y.js +0 -596
  148. package/dist/compiler/plugins/track-tool-calls.d.ts +0 -33
  149. package/dist/compiler-C-OI9sim.cjs +0 -547
  150. package/dist/compiler-CW1VjQsn.js +0 -473
  151. package/dist/component-BXS8vAwO.cjs +0 -73
  152. package/dist/component-Dq7ZLdvy.js +0 -44
  153. package/dist/component.d.ts +0 -88
  154. package/dist/component.default.d.ts +0 -268
  155. package/dist/custom-client-2V9lmzUf.js +0 -2655
  156. package/dist/custom-client-HkM1Sdzv.cjs +0 -2726
  157. package/dist/dual-modes-Cb7ThvoV.cjs +0 -3020
  158. package/dist/dual-modes-Cy0_W4O2.js +0 -2930
  159. package/dist/errors-C962uJdS.cjs +0 -220
  160. package/dist/errors-Cg1G0Awu.js +0 -161
  161. package/dist/example.d.ts +0 -45
  162. package/dist/execute-Aw4cbIx-.js +0 -1080
  163. package/dist/execute-COIHOIKR.cjs +0 -1081
  164. package/dist/exit-parser.d.ts +0 -39
  165. package/dist/handlers.d.ts +0 -10
  166. package/dist/hoist.d.ts +0 -2
  167. package/dist/isEmpty-BhJRCqIF.js +0 -52
  168. package/dist/isEmpty-LY533Wmh.cjs +0 -57
  169. package/dist/jsx-B0iYyvfr.js +0 -100
  170. package/dist/jsx-D5Y2cPo-.cjs +0 -129
  171. package/dist/jsx.d.ts +0 -11
  172. package/dist/message-stream/index.d.ts +0 -9
  173. package/dist/message-stream/instructions.d.ts +0 -22
  174. package/dist/message-stream/normalizer.d.ts +0 -21
  175. package/dist/message-stream/parser.d.ts +0 -83
  176. package/dist/message-stream/registry.d.ts +0 -14
  177. package/dist/message-stream/replayable.d.ts +0 -27
  178. package/dist/message-stream/sanitize.d.ts +0 -10
  179. package/dist/message-stream/stream.d.ts +0 -54
  180. package/dist/message-stream/types.d.ts +0 -96
  181. package/dist/message-stream/validator.d.ts +0 -23
  182. package/dist/prompts/chat-mode/system.d.ts +0 -2
  183. package/dist/prompts/chat-mode/system.md.d.ts +0 -2
  184. package/dist/prompts/chat-mode/user.d.ts +0 -2
  185. package/dist/prompts/chat-mode/user.md.d.ts +0 -2
  186. package/dist/prompts/common.d.ts +0 -6
  187. package/dist/prompts/dual-modes.d.ts +0 -2
  188. package/dist/prompts/execution-history.d.ts +0 -6
  189. package/dist/prompts/execution-state.d.ts +0 -3
  190. package/dist/prompts/protocol.d.ts +0 -18
  191. package/dist/prompts/worker-mode/system.d.ts +0 -2
  192. package/dist/prompts/worker-mode/system.md.d.ts +0 -2
  193. package/dist/prompts/worker-mode/user.d.ts +0 -2
  194. package/dist/prompts/worker-mode/user.md.d.ts +0 -2
  195. package/dist/quickjs-variant-BYhMBpwU.js +0 -45
  196. package/dist/quickjs-variant-CsIwTVFC.cjs +0 -56
  197. package/dist/rolldown-runtime-EAsCxrDo.js +0 -14
  198. package/dist/runtime/interpret-result.d.ts +0 -22
  199. package/dist/snapshots.d.ts +0 -249
  200. package/dist/stack-traces-BIE_5Xwf.js +0 -21
  201. package/dist/stack-traces-ntw_qhoy.cjs +0 -26
  202. package/dist/truncator-CLkouTCO.cjs +0 -276
  203. package/dist/truncator-DZqEqnpD.js +0 -253
  204. package/dist/truncator.d.ts +0 -118
  205. package/dist/utils-3ESwP3yg.cjs +0 -1075
  206. package/dist/utils-UBeAFeEI.js +0 -950
  207. /package/dist/{citations.d.ts → chat/citations.d.ts} +0 -0
  208. /package/dist/{citations.test.d.ts → chat/citations.test.d.ts} +0 -0
  209. /package/dist/{component.default.test.d.ts → chat/component.default.test.d.ts} +0 -0
  210. /package/dist/{compiler/plugins/track-tool-calls.test.d.ts → chat/component.test.d.ts} +0 -0
  211. /package/dist/{example.test.d.ts → chat/response.test.d.ts} +0 -0
  212. /package/dist/{exit-parser.test.d.ts → compiler/plugins/dynamic-code.test.d.ts} +0 -0
  213. /package/dist/{handlers.test.d.ts → compiler/plugins/tool-resolution.test.d.ts} +0 -0
  214. /package/dist/{hoist.test.d.ts → errors/catalog.test.d.ts} +0 -0
  215. /package/dist/{message-stream/instructions.test.d.ts → exit.test.d.ts} +0 -0
  216. /package/dist/{message-stream/normalizer.test.d.ts → inspection.test.d.ts} +0 -0
  217. /package/dist/{message-stream/parser.test.d.ts → prompts/native.test.d.ts} +0 -0
  218. /package/dist/{message-stream/stream.test.d.ts → result.test.d.ts} +0 -0
  219. /package/dist/{message-stream/validator.test.d.ts → runtime/chat-delivery.test.d.ts} +0 -0
  220. /package/dist/{prompts/common.test.d.ts → runtime/citations.test.d.ts} +0 -0
  221. /package/dist/{prompts/protocol.test.d.ts → runtime/compatibility-regressions.test.d.ts} +0 -0
  222. /package/dist/runtime/{interpret-result.test.d.ts → context-registration.test.d.ts} +0 -0
  223. /package/dist/{transcript.test.d.ts → runtime/error-recovery.test.d.ts} +0 -0
  224. /package/dist/{truncator.test.d.ts → runtime/errors.test.d.ts} +0 -0
package/dist/context.d.ts CHANGED
@@ -1,39 +1,35 @@
1
1
  import { Models, SttModels } from '@botpress/cognitive';
2
2
  import { z } from '@bpinternal/zui';
3
- import { Chat } from './chat.js';
4
- import { Component } from './component.js';
5
- import { SnapshotSignal } from './errors.js';
6
- import type { Example } from './example.js';
3
+ import { Chat } from './chat/chat.js';
4
+ import { type ComponentRegistry } from './chat/component.js';
5
+ import { type ResolvedResponse } from './chat/response.js';
6
+ import { type ErrorDetails, type LLMzFailure } from './errors.js';
7
7
  import { Exit } from './exit.js';
8
8
  import { ValueOrGetter } from './getter.js';
9
- import { HookedArray } from './handlers.js';
10
- import type { Diagnostic } from './message-stream/types.js';
9
+ import { type Inspector, type OnInspect } from './inspection.js';
11
10
  import { ObjectInstance } from './objects.js';
12
- import { LLMzPrompts, ParsedNext, ParsedSend, Prompt } from './prompts/prompt.js';
13
- import { Snapshot } from './snapshots.js';
11
+ import { LLMzPrompts } from './prompts/prompt.js';
12
+ import { Session } from './session/session.js';
14
13
  import { Tool } from './tool.js';
15
- import { Transcript, TranscriptArray } from './transcript.js';
16
14
  import { ObjectMutation, Serializable, Trace } from './types.js';
17
15
  /**
18
- * Tokenizer-measured size of each part of the prompt, before truncation.
19
- * Useful to understand what is eating up the context window.
16
+ * Tokenizer estimate of the final request after compaction, grouped by purpose.
17
+ * Categories sum to the measured request size; provider-reported usage is separate.
18
+ * Media transport URLs and encoded bytes are excluded; media token usage is
19
+ * provider-specific and is not available until the provider reports usage.
20
20
  */
21
21
  export type ContextTokens = {
22
- /** Total measured size of the prompt (sum of all the parts below). */
22
+ /** Total measured request size (sum of all the parts below). */
23
23
  total: number;
24
- /** Static prompt scaffolding: response format, VM rules, security guidelines, recap. */
24
+ /** System scaffolding and structured-message overhead not attributed below. */
25
25
  framework: number;
26
26
  /** The identity / instructions section. */
27
27
  instructions: number;
28
- /** tools.d.ts — tools, objects and variable typings. */
28
+ /** Callable JavaScript declarations and native tool schemas. */
29
29
  tools: number;
30
- /** The conversation transcript. */
31
- transcript: number;
32
- /** The ■ protocol reference documenting components and exits. */
30
+ /** Native execution rules documenting components and exits. */
33
31
  protocol: number;
34
- /** Consumer few-shot demonstrations, separate from the live transcript. */
35
- examples: number;
36
- /** Messages carried over from previous iterations (assistant responses, execution results, errors). */
32
+ /** Current input, retained native history, tool results, and the memory overview. */
37
33
  iterations: number;
38
34
  };
39
35
  /** Token usage of a single iteration's LLM call. */
@@ -46,27 +42,27 @@ export type TokenUsage = {
46
42
  total: number;
47
43
  /**
48
44
  * The effective context window limit of this call, in tokens:
49
- * `min(options.maxTokens, model's max input tokens)`. Use it to compute the
45
+ * `min(options.maxTokens, smallest configured model input limit)`. Use it to compute the
50
46
  * percentage of context used (e.g. `context.total / limit`).
51
47
  * Undefined until the LLM call starts.
52
48
  */
53
49
  limit?: number;
54
- /** Measured context size by part of the prompt (pre-truncation). */
50
+ /** Measured context size by part of the request after compaction. */
55
51
  context: ContextTokens;
56
52
  };
57
53
  export type IterationParameters = {
58
- transcript: TranscriptArray;
54
+ chatEnabled: boolean;
59
55
  tools: Tool[];
60
56
  objects: ObjectInstance[];
61
57
  exits: Exit[];
62
58
  instructions?: string;
63
- examples?: Example[];
64
- components: Component[];
59
+ components: ComponentRegistry;
60
+ response?: ResolvedResponse;
65
61
  model: Models | Models[];
66
62
  temperature: number;
67
63
  reasoningEffort?: 'low' | 'medium' | 'high' | 'dynamic' | 'none';
68
64
  };
69
- export type IterationStatus = IterationStatuses.Pending | IterationStatuses.GenerationError | IterationStatuses.ExecutionError | IterationStatuses.InvalidCodeError | IterationStatuses.Thinking | IterationStatuses.Callback | IterationStatuses.ExitSuccess | IterationStatuses.ExitError | IterationStatuses.Aborted;
65
+ export type IterationStatus = IterationStatuses.Pending | IterationStatuses.GenerationError | IterationStatuses.ExecutionError | IterationStatuses.InvalidCodeError | IterationStatuses.Thinking | IterationStatuses.ExitSuccess | IterationStatuses.ExitError | IterationStatuses.Aborted;
70
66
  export declare namespace IterationStatuses {
71
67
  type Pending = {
72
68
  type: 'pending';
@@ -101,12 +97,6 @@ export declare namespace IterationStatuses {
101
97
  interrupted?: boolean;
102
98
  };
103
99
  };
104
- type Callback = {
105
- type: 'callback_requested';
106
- callback_requested: {
107
- signal: SnapshotSignal;
108
- };
109
- };
110
100
  type ExitSuccess<T = unknown> = {
111
101
  type: 'exit_success';
112
102
  exit_success: {
@@ -130,218 +120,14 @@ export declare namespace IterationStatuses {
130
120
  };
131
121
  }
132
122
  /**
133
- * Built-in exit for requesting thinking time during agent execution.
134
- *
135
- * The ThinkExit allows agents to pause execution and reflect on the current situation,
136
- * variables, and context before continuing. There are two ways to trigger thinking:
137
- *
138
- * 1. **Agent-initiated**: Agent calls `return { action: 'think' }` to pause and reflect
139
- * 2. **Tool/Hook-initiated**: Tools or hooks throw `ThinkSignal` to force agent reflection
140
- *
141
- * This exit is automatically available in all LLMz executions and is commonly used for:
142
- * - Complex decision making that requires analysis
143
- * - Debugging and understanding current variable state
144
- * - Planning multi-step operations
145
- * - Tool feedback and result processing
146
- * - Reflecting on previous iterations and results
147
- *
148
- * @example
149
- * ```typescript
150
- * // Agent retrieves web search results and decides to think about them
151
- * const results = await searchWeb(query)
152
- *
153
- * // Agent decides it needs to think (look at the search results) before responding
154
- * return { action: 'think', results }
155
- * ```
156
- *
157
- * Sometimes, as the author of the tool, you may want to always force the agent to think about the results.
158
- * In this case, you can throw a `ThinkSignal` from the tool handler to trigger thinking.
159
- *
160
- * @example
161
- * ```typescript
162
- * // Tool-initiated thinking using ThinkSignal
163
- * import { ThinkSignal } from 'llmz'
164
- *
165
- * const searchTool = new Tool({
166
- * name: 'search',
167
- * handler: async ({ query }) => {
168
- * const results = await performSearch(query)
169
- *
170
- * if (!results.length) {
171
- * // Force agent to think about alternative approaches
172
- * throw new ThinkSignal(
173
- * 'No search results found',
174
- * 'No results were found. Consider rephrasing the query or using a different approach.'
175
- * )
176
- * }
177
- *
178
- * // Provide context for agent to process results
179
- * throw new ThinkSignal(
180
- * 'Search completed with results',
181
- * `Found ${results.length} results. Process them carefully and provide citations.`
182
- * )
183
- * }
184
- * })
185
- * ```
186
- * When an iteration ends with ThinkExit, the agent will automatically loop and start a new iteration to continue processing, unless iteration limit is reached.
187
- *
188
- * The thinking process helps agents:
189
- * - Avoid rushing into incorrect solutions
190
- * - Better understand complex problems and tool results
191
- * - Maintain variable state across iterations
192
- * - Process feedback from tools and hooks
193
- * - Provide more thoughtful and accurate responses
194
- */
195
- export declare const ThinkExit: Exit<unknown>;
196
- /**
197
- * Built-in exit for waiting for user input in chat mode.
198
- *
199
- * The ListenExit is automatically available when chat mode is enabled (when a Chat
200
- * instance is provided to execute()). When an agent calls `return { action: 'listen' }`,
201
- * the execution pauses and waits for user input before continuing the conversation.
202
- *
203
- * This exit is essential for interactive conversational agents and is used to:
204
- * - Wait for user responses in chat interfaces
205
- * - Pause execution until user provides input
206
- * - Enable back-and-forth conversation flow
207
- * - Allow users to guide the conversation direction
208
- *
209
- * The ListenExit is only available in chat mode - it will not be present in
210
- * worker mode executions where no chat interface is provided.
211
- *
212
- * @example
213
- * ```typescript
214
- * // Agent generated code using ListenExit in chat mode
215
- * yield <Message>What would you like me to help you with today?</Message>
216
- * yield <Button action="postback" label="Get Weather" value="weather" />
217
- * yield <Button action="postback" label="Set Reminder" value="reminder" />
218
- *
219
- * // Wait for user to respond
220
- * return { action: 'listen' }
221
- * ```
222
- *
223
- * @example
224
- * ```typescript
225
- * // Standard chat interaction pattern
226
- * const calculation = 2 + 8
227
- * yield <Message>The result of `2 + 8` is **{calculation}**.</Message>
228
- * return { action: 'listen' }
229
- * ```
230
- *
231
- * @example
232
- * ```typescript
233
- * // CLI chat example with ListenExit handling
234
- * const chat = new CLIChat()
235
- *
236
- * while (chat.iterate()) {
237
- * const result = await execute({
238
- * instructions: 'Help the user with their questions',
239
- * chat,
240
- * client,
241
- * })
242
- *
243
- * if (result.is(ListenExit)) {
244
- * // CLIChat handles prompting user automatically
245
- * continue
246
- * } else {
247
- * console.log('Conversation ended')
248
- * break
249
- * }
250
- * }
251
- * ```
252
- *
253
- * The ListenExit enables natural conversation flow where:
254
- * - Agent sends messages and waits for responses
255
- * - User provides input to guide the conversation
256
- * - Conversation continues iteratively until completion
257
- * - Chat interface manages the input/output cycle
123
+ * Chat completion. JavaScript uses `return exit('listen')`
124
+ * to wait for user input. A plain assistant answer also implies this exit.
258
125
  */
259
126
  export declare const ListenExit: Exit<unknown>;
260
127
  /**
261
- * Default exit used when no custom exits are provided.
262
- *
263
- * The DefaultExit is automatically used in worker mode when no custom exits are defined.
264
- * It provides a standard way to complete execution with either success or failure outcomes.
265
- * The exit uses a discriminated union schema to ensure type-safe handling of both success
266
- * and error cases.
267
- *
268
- * This exit is commonly used for:
269
- * - Simple worker mode executions without custom completion logic
270
- * - Standardized success/failure reporting
271
- * - Basic task completion with result or error information
272
- * - Default fallback when no specific exit behavior is needed
273
- *
274
- * @example
275
- * ```typescript
276
- * // Agent generated code using DefaultExit for successful completion
277
- * const data = await fetchUserData(userId)
278
- * const processedResult = processData(data)
279
- *
280
- * return {
281
- * action: 'done',
282
- * success: true,
283
- * result: processedResult
284
- * }
285
- * ```
286
- *
287
- * @example
288
- * ```typescript
289
- * // Agent generated code using DefaultExit for error cases
290
- * try {
291
- * const result = await riskyOperation()
292
- * return { action: 'done', success: true, result }
293
- * } catch (error) {
294
- * return {
295
- * action: 'done',
296
- * success: false,
297
- * error: `Operation failed: ${error.message}`
298
- * }
299
- * }
300
- * ```
301
- *
302
- * @example
303
- * ```typescript
304
- * import { execute, DefaultExit } from 'llmz'
305
- *
306
- * // Handling DefaultExit in execution results
307
- * const result = await execute({
308
- * instructions: 'Process the user data and return results',
309
- * // No custom exits provided - DefaultExit will be used
310
- * client,
311
- * })
312
- *
313
- * if (result.is(DefaultExit)) {
314
- * if (result.output.success) {
315
- * console.log('Success:', result.output.result)
316
- * } else {
317
- * console.error('Error:', result.output.error)
318
- * }
319
- * }
320
- * ```
321
- *
322
- * @example
323
- * ```typescript
324
- * // Worker mode execution with automatic DefaultExit
325
- * const result = await execute({
326
- * instructions: 'Calculate fibonacci numbers up to 100',
327
- * tools: [mathTools],
328
- * client,
329
- * // No chat provided = worker mode
330
- * // No exits provided = DefaultExit automatically added
331
- * })
332
- *
333
- * // Result will use DefaultExit for completion
334
- * if (result.isSuccess() && result.is(DefaultExit)) {
335
- * const { success, result: data, error } = result.output
336
- * // Handle success/failure cases
337
- * }
338
- * ```
339
- *
340
- * The DefaultExit provides a consistent interface for:
341
- * - Type-safe success/failure handling
342
- * - Standardized result reporting across different executions
343
- * - Automatic fallback behavior when no custom exits are defined
344
- * - Clear separation between successful results and error conditions
128
+ * Worker completion when exits are omitted. JavaScript returns
129
+ * `exit('done', { success: true, result })` or
130
+ * `exit('done', { success: false, error })`.
345
131
  */
346
132
  export declare const DefaultExit: Exit<{
347
133
  success: true;
@@ -353,13 +139,11 @@ export declare const DefaultExit: Exit<{
353
139
  export declare namespace Iteration {
354
140
  type JSON = {
355
141
  id: string;
356
- messages: LLMzPrompts.Message[];
357
142
  code?: string;
358
143
  traces: Trace[];
359
144
  model: Models | Models[];
360
145
  temperature: number;
361
146
  reasoningEffort?: 'low' | 'medium' | 'high' | 'dynamic' | 'none';
362
- variables: Record<string, any>;
363
147
  started_ts: number;
364
148
  ended_ts?: number;
365
149
  status: IterationStatus;
@@ -372,32 +156,35 @@ export declare namespace Iteration {
372
156
  tokens: number;
373
157
  spend: number;
374
158
  output: string;
375
- diagnostics?: Diagnostic[];
376
159
  model: string;
377
160
  time_to_first_token?: number;
378
161
  time_to_last_token?: number;
379
162
  };
380
163
  tokens?: TokenUsage;
381
- transcript: Transcript.Message[];
382
- tools: Tool.JSON[];
383
- objects: ObjectInstance.JSON[];
384
- exits: Exit.JSON[];
385
- instructions?: string;
386
164
  duration?: string;
387
165
  error?: string | null;
166
+ exception?: ErrorDetails;
167
+ errors: ErrorDetails[];
388
168
  isChatEnabled?: boolean;
389
169
  };
390
170
  }
391
171
  export declare class Iteration implements Serializable<Iteration.JSON> {
392
172
  id: string;
393
- messages: LLMzPrompts.Message[];
173
+ readonly systemMessage: LLMzPrompts.Message;
394
174
  code?: string;
395
- /** Messages (`■send` blocks) parsed from the assistant response, in order. */
396
- sends?: ParsedSend[];
397
- /** The `■next` exit parsed from the assistant response, if any. */
398
- next?: ParsedNext;
399
- traces: HookedArray<Trace>;
400
- variables: Record<string, any>;
175
+ sessionInfo?: {
176
+ id: string;
177
+ number: number;
178
+ turn: number;
179
+ turnId: string;
180
+ timestamp: number;
181
+ };
182
+ /** Outer native call that owns the current JavaScript execution. */
183
+ nativeCallId?: string;
184
+ traces: Trace[];
185
+ private readonly _onTrace?;
186
+ /** Record a runtime trace and notify the observer without changing execution on observer errors. */
187
+ recordTrace: (trace: Trace) => void;
401
188
  /**
402
189
  * Token usage of this iteration's LLM call. The `context` breakdown is measured
403
190
  * when the prompt is assembled; `input`/`output` are filled in once the LLM call
@@ -407,12 +194,17 @@ export declare class Iteration implements Serializable<Iteration.JSON> {
407
194
  started_ts: number;
408
195
  ended_ts?: number;
409
196
  status: IterationStatus;
197
+ /** Original failure for programmatic handling. Execution errors retain their typed cause. */
198
+ exception?: LLMzFailure;
199
+ /** All observed failures, including tool errors caught by generated code. */
200
+ readonly errors: LLMzFailure[];
201
+ recordError(value: unknown): LLMzFailure;
410
202
  private _mutations;
411
203
  get mutations(): ObjectMutation[];
412
204
  trackMutation(mutation: ObjectMutation): void;
413
205
  private _parameters;
414
- get components(): Component[];
415
- get transcript(): TranscriptArray;
206
+ get components(): ComponentRegistry;
207
+ get response(): ResolvedResponse | undefined;
416
208
  get tools(): Tool<z.ZodType<any, z.ZodTypeDef, any>, z.ZodType<any, z.ZodTypeDef, any>>[];
417
209
  get objects(): ObjectInstance[];
418
210
  get model(): Models | Models[];
@@ -429,8 +221,6 @@ export declare class Iteration implements Serializable<Iteration.JSON> {
429
221
  tokens: number;
430
222
  spend: number;
431
223
  output: string;
432
- /** Syntax diagnostics from the response parser; raw output remains available above. */
433
- diagnostics?: Diagnostic[];
434
224
  model: string;
435
225
  /** Milliseconds between the LLM call start and the first streamed token. Only set on streaming clients. */
436
226
  time_to_first_token?: number;
@@ -450,30 +240,29 @@ export declare class Iteration implements Serializable<Iteration.JSON> {
450
240
  status: IterationStatuses.ExitSuccess<R>;
451
241
  } & this;
452
242
  isSuccessful(this: this): this is this & {
453
- status: IterationStatuses.ExitSuccess | IterationStatuses.Callback | IterationStatuses.Thinking;
243
+ status: IterationStatuses.ExitSuccess | IterationStatuses.Thinking;
454
244
  };
455
245
  isFailed(this: this): this is this & {
456
246
  status: IterationStatuses.GenerationError | IterationStatuses.ExecutionError | IterationStatuses.InvalidCodeError | IterationStatuses.ExitError | IterationStatuses.Aborted;
457
247
  };
458
248
  get duration(): string;
249
+ /** Human-readable failure summary. Use exception for instanceof checks and structured details. */
459
250
  get error(): string | null;
460
251
  get isChatEnabled(): boolean;
461
252
  constructor(props: {
462
253
  id: string;
463
254
  parameters: IterationParameters;
464
- messages: LLMzPrompts.Message[];
465
- variables: Record<string, any>;
255
+ systemMessage: LLMzPrompts.Message;
256
+ onTrace?: (trace: Trace) => void;
466
257
  });
467
- end(status: IterationStatus): void;
258
+ end(status: IterationStatus, exception?: unknown): void;
468
259
  toJSON(): {
469
260
  id: string;
470
- messages: import("@botpress/cognitive").CognitiveMessage[];
471
261
  code: string | undefined;
472
262
  model: Models[] | Models;
473
263
  temperature: number;
474
264
  reasoningEffort: "dynamic" | "high" | "low" | "medium" | "none" | undefined;
475
265
  traces: import("./types.js").Traces.Trace[];
476
- variables: Record<string, any>;
477
266
  started_ts: number;
478
267
  ended_ts: number | undefined;
479
268
  status: IterationStatus;
@@ -486,8 +275,6 @@ export declare class Iteration implements Serializable<Iteration.JSON> {
486
275
  tokens: number;
487
276
  spend: number;
488
277
  output: string;
489
- /** Syntax diagnostics from the response parser; raw output remains available above. */
490
- diagnostics?: Diagnostic[];
491
278
  model: string;
492
279
  /** Milliseconds between the LLM call start and the first streamed token. Only set on streaming clients. */
493
280
  time_to_first_token?: number;
@@ -501,45 +288,10 @@ export declare class Iteration implements Serializable<Iteration.JSON> {
501
288
  };
502
289
  } | undefined;
503
290
  tokens: TokenUsage | undefined;
504
- transcript: Transcript.Message[];
505
- tools: {
506
- name: string;
507
- aliases: string[];
508
- description: string | undefined;
509
- metadata: Record<string, unknown>;
510
- input: import("json-schema").JSONSchema7 | undefined;
511
- output: import("json-schema").JSONSchema7 | undefined;
512
- staticInputValues: unknown;
513
- maxRetries: number;
514
- }[];
515
- objects: {
516
- name: string;
517
- description: string | undefined;
518
- properties: import("./objects.js").ObjectProperty[] | undefined;
519
- tools: {
520
- name: string;
521
- aliases: string[];
522
- description: string | undefined;
523
- metadata: Record<string, unknown>;
524
- input: import("json-schema").JSONSchema7 | undefined;
525
- output: import("json-schema").JSONSchema7 | undefined;
526
- staticInputValues: unknown;
527
- maxRetries: number;
528
- }[];
529
- metadata: Record<string, unknown> | undefined;
530
- }[];
531
- exits: {
532
- name: string;
533
- aliases: string[];
534
- description: string;
535
- metadata: {
536
- [x: string]: unknown;
537
- };
538
- schema: import("json-schema").JSONSchema7 | undefined;
539
- }[];
540
- instructions: string | undefined;
541
291
  duration: string;
542
292
  error: string | null;
293
+ exception: ErrorDetails | undefined;
294
+ errors: ErrorDetails[];
543
295
  isChatEnabled: boolean;
544
296
  };
545
297
  }
@@ -551,42 +303,40 @@ export declare namespace Context {
551
303
  timeout: number;
552
304
  loop: number;
553
305
  metadata: Record<string, any>;
554
- snapshot?: Snapshot.JSON;
306
+ sessionId: string;
555
307
  };
556
308
  }
557
309
  export declare class Context implements Serializable<Context.JSON> {
558
310
  id: string;
559
311
  chat?: Chat;
560
312
  instructions?: ValueOrGetter<string, Context>;
561
- examples?: ValueOrGetter<Example[], Context>;
562
313
  objects?: ValueOrGetter<ObjectInstance[], Context>;
563
314
  tools?: ValueOrGetter<Tool[], Context>;
564
315
  exits?: ValueOrGetter<Exit[], Context>;
565
316
  model?: ValueOrGetter<Models | Models[], Context>;
566
317
  temperature: ValueOrGetter<number, Context>;
567
318
  reasoningEffort?: ValueOrGetter<'low' | 'medium' | 'high' | 'dynamic' | 'none', Context>;
568
- version: Prompt;
319
+ session: Session;
320
+ readonly inspector: Inspector;
569
321
  timeout: number;
570
322
  loop: number;
571
323
  /**
572
324
  * Optional cap on the model's context window. The effective limit is
573
- * `min(maxTokens, model's max input tokens)`.
325
+ * `min(maxTokens, smallest configured model input limit)`.
574
326
  */
575
327
  maxTokens?: number;
328
+ /** Default display budget; explicitly wrapped tool results can override it. */
329
+ toolResultMaxTokens: number;
576
330
  /**
577
331
  * Maximum time to wait for the first streamed token, in milliseconds,
578
332
  * before the cognitive service falls back to the next model/provider.
579
333
  */
580
334
  maxTimeToFirstToken?: number;
581
335
  /**
582
- * Allow Cognitive to restart a failed stream on another model. With fallback
583
- * enabled, there is no buffered delivery and no final delivery queue: delta
584
- * text and completed Chat.handler sends stream immediately during generation.
585
- * A reset-only delta (restart: true) on Chat.onMessageDelta invalidates ALL
586
- * current-iteration messages, including completed sends — consumers must
587
- * retract/replace them, which is unsafe for irreversible external transports.
588
- * Only TOOL/CODE execution waits for a successful stream. Streaming-only;
589
- * defaults to false, which leaves progressive sending unchanged.
336
+ * Allow Cognitive to restart a failed stream on another model. Previews remain
337
+ * live and are retracted with a restart delta before replacement output.
338
+ * Completed sends and code always wait for a valid response and successful
339
+ * transport. Streaming-only; defaults to false.
590
340
  */
591
341
  midStreamFallback?: boolean;
592
342
  /**
@@ -595,12 +345,9 @@ export declare class Context implements Serializable<Context.JSON> {
595
345
  */
596
346
  transcriptionModel?: SttModels;
597
347
  metadata: Record<string, any>;
598
- snapshot?: Snapshot;
599
- private _lastMessageWithoutExecutionState?;
600
348
  iteration: number;
601
349
  iterations: Iteration[];
602
- nextIteration(): Promise<Iteration>;
603
- private _getIterationVariables;
350
+ nextIteration(onTrace?: (trace: Trace) => void): Promise<Iteration>;
604
351
  /**
605
352
  * Measures the token size of each part of the prompt (pre-truncation).
606
353
  * The named parts come from the system prompt; everything else in the system
@@ -614,7 +361,6 @@ export declare class Context implements Serializable<Context.JSON> {
614
361
  constructor(props: {
615
362
  chat?: Chat;
616
363
  instructions?: ValueOrGetter<string, Context>;
617
- examples?: ValueOrGetter<Example[], Context>;
618
364
  objects?: ValueOrGetter<ObjectInstance[], Context>;
619
365
  tools?: ValueOrGetter<Tool[], Context>;
620
366
  exits?: ValueOrGetter<Exit[], Context>;
@@ -623,9 +369,11 @@ export declare class Context implements Serializable<Context.JSON> {
623
369
  reasoningEffort?: ValueOrGetter<'low' | 'medium' | 'high' | 'dynamic' | 'none', Context>;
624
370
  model?: ValueOrGetter<Models | Models[], Context>;
625
371
  metadata?: Record<string, any>;
626
- snapshot?: Snapshot;
372
+ session?: Session;
373
+ onInspect?: OnInspect;
627
374
  timeout?: number;
628
375
  maxTokens?: number;
376
+ toolResultMaxTokens?: number;
629
377
  maxTimeToFirstToken?: number;
630
378
  midStreamFallback?: boolean;
631
379
  transcriptionModel?: SttModels;
@@ -634,13 +382,11 @@ export declare class Context implements Serializable<Context.JSON> {
634
382
  id: string;
635
383
  iterations: {
636
384
  id: string;
637
- messages: import("@botpress/cognitive").CognitiveMessage[];
638
385
  code: string | undefined;
639
386
  model: Models[] | Models;
640
387
  temperature: number;
641
388
  reasoningEffort: "dynamic" | "high" | "low" | "medium" | "none" | undefined;
642
389
  traces: import("./types.js").Traces.Trace[];
643
- variables: Record<string, any>;
644
390
  started_ts: number;
645
391
  ended_ts: number | undefined;
646
392
  status: IterationStatus;
@@ -653,8 +399,6 @@ export declare class Context implements Serializable<Context.JSON> {
653
399
  tokens: number;
654
400
  spend: number;
655
401
  output: string;
656
- /** Syntax diagnostics from the response parser; raw output remains available above. */
657
- diagnostics?: Diagnostic[];
658
402
  model: string;
659
403
  /** Milliseconds between the LLM call start and the first streamed token. Only set on streaming clients. */
660
404
  time_to_first_token?: number;
@@ -668,71 +412,16 @@ export declare class Context implements Serializable<Context.JSON> {
668
412
  };
669
413
  } | undefined;
670
414
  tokens: TokenUsage | undefined;
671
- transcript: Transcript.Message[];
672
- tools: {
673
- name: string;
674
- aliases: string[];
675
- description: string | undefined;
676
- metadata: Record<string, unknown>;
677
- input: import("json-schema").JSONSchema7 | undefined;
678
- output: import("json-schema").JSONSchema7 | undefined;
679
- staticInputValues: unknown;
680
- maxRetries: number;
681
- }[];
682
- objects: {
683
- name: string;
684
- description: string | undefined;
685
- properties: import("./objects.js").ObjectProperty[] | undefined;
686
- tools: {
687
- name: string;
688
- aliases: string[];
689
- description: string | undefined;
690
- metadata: Record<string, unknown>;
691
- input: import("json-schema").JSONSchema7 | undefined;
692
- output: import("json-schema").JSONSchema7 | undefined;
693
- staticInputValues: unknown;
694
- maxRetries: number;
695
- }[];
696
- metadata: Record<string, unknown> | undefined;
697
- }[];
698
- exits: {
699
- name: string;
700
- aliases: string[];
701
- description: string;
702
- metadata: {
703
- [x: string]: unknown;
704
- };
705
- schema: import("json-schema").JSONSchema7 | undefined;
706
- }[];
707
- instructions: string | undefined;
708
415
  duration: string;
709
416
  error: string | null;
417
+ exception: ErrorDetails | undefined;
418
+ errors: ErrorDetails[];
710
419
  isChatEnabled: boolean;
711
420
  }[];
712
421
  iteration: number;
713
422
  timeout: number;
714
423
  loop: number;
715
424
  metadata: Record<string, any>;
716
- snapshot: {
717
- id: string;
718
- reason: string | undefined;
719
- stack: string;
720
- variables: ({
721
- name: string;
722
- type: string;
723
- bytes: number;
724
- preview?: string;
725
- value?: unknown;
726
- truncated: boolean;
727
- } & ({
728
- truncated: true;
729
- preview: string;
730
- } | {
731
- truncated: false;
732
- value: unknown;
733
- }))[];
734
- toolCall: import("./errors.js").ToolCall | undefined;
735
- status: import("./snapshots.js").SnapshotStatus;
736
- } | undefined;
425
+ sessionId: string;
737
426
  };
738
427
  }