llmz 0.7.1 → 0.8.0

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 (47) hide show
  1. package/dist/{_baseForOwn-buOZDFbK.cjs → _baseForOwn-BfWDTL8L.cjs} +0 -50
  2. package/dist/common-DdArNQhl.cjs +643 -0
  3. package/dist/common-pgJjFu2Y.js +596 -0
  4. package/dist/compiler-C-OI9sim.cjs +547 -0
  5. package/dist/compiler-CW1VjQsn.js +473 -0
  6. package/dist/{component-BwixNVH8.cjs → component-BXS8vAwO.cjs} +3 -3
  7. package/dist/{component-DGG7d_Mz.js → component-Dq7ZLdvy.js} +1 -1
  8. package/dist/component.d.ts +4 -4
  9. package/dist/component.default.d.ts +60 -87
  10. package/dist/component.default.test.d.ts +1 -0
  11. package/dist/context.d.ts +10 -0
  12. package/dist/{custom-client-CMuy7fXU.js → custom-client-2V9lmzUf.js} +119 -24
  13. package/dist/{custom-client-Bp1TgTKk.cjs → custom-client-HkM1Sdzv.cjs} +124 -29
  14. package/dist/{dual-modes-CfqTk9M8.cjs → dual-modes-Cb7ThvoV.cjs} +1268 -1062
  15. package/dist/{dual-modes-mNyMThtf.js → dual-modes-Cy0_W4O2.js} +1242 -1036
  16. package/dist/example.d.ts +45 -0
  17. package/dist/example.test.d.ts +1 -0
  18. package/dist/{execute-DP_17lYZ.js → execute-Aw4cbIx-.js} +23 -10
  19. package/dist/{execute-msui51b2.cjs → execute-COIHOIKR.cjs} +30 -18
  20. package/dist/{exit-BGi0Kk0y.cjs → exit-DUM96DN1.cjs} +3 -3
  21. package/dist/exit-parser.d.ts +2 -0
  22. package/dist/index.cjs +33 -356
  23. package/dist/index.d.ts +2 -0
  24. package/dist/index.js +13 -337
  25. package/dist/index.workerd.js +9981 -9388
  26. package/dist/{jsx-CIgdGGVs.js → jsx-B0iYyvfr.js} +50 -2
  27. package/dist/{jsx-BWvT6ukg.cjs → jsx-D5Y2cPo-.cjs} +58 -4
  28. package/dist/message-stream/parser.d.ts +3 -0
  29. package/dist/message-stream/types.d.ts +1 -1
  30. package/dist/prompts/chat-mode/system.d.ts +2 -0
  31. package/dist/prompts/chat-mode/user.d.ts +2 -0
  32. package/dist/prompts/execution-history.d.ts +6 -0
  33. package/dist/prompts/execution-state.d.ts +3 -0
  34. package/dist/prompts/prompt.d.ts +26 -0
  35. package/dist/prompts/protocol.test.d.ts +1 -0
  36. package/dist/prompts/worker-mode/system.d.ts +2 -0
  37. package/dist/prompts/worker-mode/user.d.ts +2 -0
  38. package/dist/runtime/types.d.ts +9 -0
  39. package/dist/{tool-ReA9gxMp.cjs → tool-Dy6AJpJH.cjs} +4 -4
  40. package/dist/{truncator-B6aaJaN1.cjs → truncator-CLkouTCO.cjs} +3 -3
  41. package/dist/{typings-BwAz_TM9.cjs → typings-3lsqCbkn.cjs} +3 -3
  42. package/dist/{utils-ZpJm00ed.cjs → utils-3ESwP3yg.cjs} +2 -2
  43. package/dist/{vm-C5vf1LH3.js → vm-BLsHw1da.js} +3 -462
  44. package/dist/{vm-BjkJqSQH.cjs → vm-D6s1zF6M.cjs} +28 -489
  45. package/package.json +1 -1
  46. package/dist/isPlainObject-CSxXVKCk.cjs +0 -56
  47. package/dist/isPlainObject-D39AzPO2.js +0 -51
@@ -1,5 +1,25 @@
1
1
  import { z } from '@bpinternal/zui';
2
- import { Component } from './component.js';
2
+ import { Component, type ContainerComponentDefinition, type RenderedComponent } from './component.js';
3
+ declare const carouselProps: z.ZodObject<{
4
+ cards: z.ZodArray<z.ZodObject<{
5
+ title: z.ZodString;
6
+ subtitle: z.ZodOptional<z.ZodString>;
7
+ body: z.ZodOptional<z.ZodString>;
8
+ image: z.ZodOptional<z.ZodObject<{
9
+ url: z.ZodString;
10
+ alt: z.ZodOptional<z.ZodString>;
11
+ }, "strip">>;
12
+ buttons: z.ZodOptional<z.ZodArray<z.ZodObject<{
13
+ action: z.ZodDefault<z.ZodEnum<["say", "url", "postback"]>>;
14
+ label: z.ZodString;
15
+ value: z.ZodOptional<z.ZodString>;
16
+ url: z.ZodOptional<z.ZodString>;
17
+ }, "strip">, "many">>;
18
+ }, "strip">, "many">;
19
+ }, "strip">;
20
+ declare class CarouselComponent extends Component<ContainerComponentDefinition<typeof carouselProps>> {
21
+ render(props: Component['propsType'], children?: any[]): RenderedComponent;
22
+ }
3
23
  export declare const DefaultComponents: {
4
24
  Button: Component<{
5
25
  type: "leaf";
@@ -8,13 +28,28 @@ export declare const DefaultComponents: {
8
28
  aliases: string[];
9
29
  generation: {
10
30
  usage: string;
11
- examples: {
31
+ examples: ({
32
+ props: {
33
+ action: string;
34
+ label: string;
35
+ url?: undefined;
36
+ value?: undefined;
37
+ };
38
+ } | {
12
39
  props: {
40
+ action: string;
41
+ label: string;
42
+ url: string;
43
+ value?: undefined;
44
+ };
45
+ } | {
46
+ props: {
47
+ url?: undefined;
13
48
  action: string;
14
49
  label: string;
15
50
  value: string;
16
51
  };
17
- }[];
52
+ })[];
18
53
  };
19
54
  leaf: {
20
55
  props: z.ZodObject<{
@@ -31,6 +66,7 @@ export declare const DefaultComponents: {
31
66
  description: string;
32
67
  aliases: never[];
33
68
  generation: {
69
+ usage: string;
34
70
  examples: {
35
71
  props: {
36
72
  url: string;
@@ -111,6 +147,7 @@ export declare const DefaultComponents: {
111
147
  description: string;
112
148
  aliases: never[];
113
149
  generation: {
150
+ usage: string;
114
151
  examples: {
115
152
  props: {
116
153
  title: string;
@@ -132,6 +169,7 @@ export declare const DefaultComponents: {
132
169
  description: string;
133
170
  aliases: never[];
134
171
  generation: {
172
+ usage: string;
135
173
  examples: {
136
174
  props: {
137
175
  url: string;
@@ -155,13 +193,28 @@ export declare const DefaultComponents: {
155
193
  aliases: string[];
156
194
  generation: {
157
195
  usage: string;
158
- examples: {
196
+ examples: ({
197
+ props: {
198
+ action: string;
199
+ label: string;
200
+ url?: undefined;
201
+ value?: undefined;
202
+ };
203
+ } | {
159
204
  props: {
205
+ action: string;
206
+ label: string;
207
+ url: string;
208
+ value?: undefined;
209
+ };
210
+ } | {
211
+ props: {
212
+ url?: undefined;
160
213
  action: string;
161
214
  label: string;
162
215
  value: string;
163
216
  };
164
- }[];
217
+ })[];
165
218
  };
166
219
  leaf: {
167
220
  props: z.ZodObject<{
@@ -175,88 +228,7 @@ export declare const DefaultComponents: {
175
228
  })[];
176
229
  };
177
230
  }>;
178
- Carousel: Component<{
179
- type: "container";
180
- name: string;
181
- description: string;
182
- aliases: never[];
183
- container: {
184
- props: z.ZodObject<{}, "strip">;
185
- children: {
186
- description: string;
187
- component: {
188
- type: "container";
189
- name: string;
190
- description: string;
191
- aliases: never[];
192
- generation: {
193
- examples: {
194
- props: {
195
- title: string;
196
- subtitle: string;
197
- };
198
- body: string;
199
- }[];
200
- };
201
- container: {
202
- props: z.ZodObject<{
203
- title: z.ZodString;
204
- subtitle: z.ZodOptional<z.ZodString>;
205
- }, "strip">;
206
- children: ({
207
- description: string;
208
- component: {
209
- type: "leaf";
210
- name: string;
211
- description: string;
212
- aliases: never[];
213
- generation: {
214
- examples: {
215
- props: {
216
- url: string;
217
- alt: string;
218
- };
219
- }[];
220
- };
221
- leaf: {
222
- props: z.ZodObject<{
223
- url: z.ZodString;
224
- alt: z.ZodOptional<z.ZodString>;
225
- }, "strip">;
226
- };
227
- };
228
- } | {
229
- description: string;
230
- component: {
231
- type: "leaf";
232
- description: string;
233
- name: string;
234
- aliases: string[];
235
- generation: {
236
- usage: string;
237
- examples: {
238
- props: {
239
- action: string;
240
- label: string;
241
- value: string;
242
- };
243
- }[];
244
- };
245
- leaf: {
246
- props: z.ZodObject<{
247
- action: z.ZodDefault<z.ZodEnum<["say", "url", "postback"]>>;
248
- label: z.ZodString;
249
- value: z.ZodOptional<z.ZodString>;
250
- url: z.ZodOptional<z.ZodString>;
251
- }, "strip">;
252
- };
253
- };
254
- })[];
255
- };
256
- };
257
- }[];
258
- };
259
- }>;
231
+ Carousel: CarouselComponent;
260
232
  Text: Component<{
261
233
  type: "default";
262
234
  name: string;
@@ -293,3 +265,4 @@ export declare const DefaultComponents: {
293
265
  };
294
266
  }>;
295
267
  };
268
+ export {};
@@ -0,0 +1 @@
1
+ export {};
package/dist/context.d.ts CHANGED
@@ -3,6 +3,7 @@ import { z } from '@bpinternal/zui';
3
3
  import { Chat } from './chat.js';
4
4
  import { Component } from './component.js';
5
5
  import { SnapshotSignal } from './errors.js';
6
+ import type { Example } from './example.js';
6
7
  import { Exit } from './exit.js';
7
8
  import { ValueOrGetter } from './getter.js';
8
9
  import { HookedArray } from './handlers.js';
@@ -30,6 +31,8 @@ export type ContextTokens = {
30
31
  transcript: number;
31
32
  /** The ■ protocol reference documenting components and exits. */
32
33
  protocol: number;
34
+ /** Consumer few-shot demonstrations, separate from the live transcript. */
35
+ examples: number;
33
36
  /** Messages carried over from previous iterations (assistant responses, execution results, errors). */
34
37
  iterations: number;
35
38
  };
@@ -57,6 +60,7 @@ export type IterationParameters = {
57
60
  objects: ObjectInstance[];
58
61
  exits: Exit[];
59
62
  instructions?: string;
63
+ examples?: Example[];
60
64
  components: Component[];
61
65
  model: Models | Models[];
62
66
  temperature: number;
@@ -93,6 +97,8 @@ export declare namespace IterationStatuses {
93
97
  /** The value returned by the executed code (or the context provided by a ThinkSignal). */
94
98
  variables: unknown;
95
99
  metadata?: Record<string, unknown>;
100
+ /** A tool paused execution; it did not finish or return normally. */
101
+ interrupted?: boolean;
96
102
  };
97
103
  };
98
104
  type Callback = {
@@ -405,6 +411,7 @@ export declare class Iteration implements Serializable<Iteration.JSON> {
405
411
  get mutations(): ObjectMutation[];
406
412
  trackMutation(mutation: ObjectMutation): void;
407
413
  private _parameters;
414
+ get components(): Component[];
408
415
  get transcript(): TranscriptArray;
409
416
  get tools(): Tool<z.ZodType<any, z.ZodTypeDef, any>, z.ZodType<any, z.ZodTypeDef, any>>[];
410
417
  get objects(): ObjectInstance[];
@@ -551,6 +558,7 @@ export declare class Context implements Serializable<Context.JSON> {
551
558
  id: string;
552
559
  chat?: Chat;
553
560
  instructions?: ValueOrGetter<string, Context>;
561
+ examples?: ValueOrGetter<Example[], Context>;
554
562
  objects?: ValueOrGetter<ObjectInstance[], Context>;
555
563
  tools?: ValueOrGetter<Tool[], Context>;
556
564
  exits?: ValueOrGetter<Exit[], Context>;
@@ -588,6 +596,7 @@ export declare class Context implements Serializable<Context.JSON> {
588
596
  transcriptionModel?: SttModels;
589
597
  metadata: Record<string, any>;
590
598
  snapshot?: Snapshot;
599
+ private _lastMessageWithoutExecutionState?;
591
600
  iteration: number;
592
601
  iterations: Iteration[];
593
602
  nextIteration(): Promise<Iteration>;
@@ -605,6 +614,7 @@ export declare class Context implements Serializable<Context.JSON> {
605
614
  constructor(props: {
606
615
  chat?: Chat;
607
616
  instructions?: ValueOrGetter<string, Context>;
617
+ examples?: ValueOrGetter<Example[], Context>;
608
618
  objects?: ValueOrGetter<ObjectInstance[], Context>;
609
619
  tools?: ValueOrGetter<Tool[], Context>;
610
620
  exits?: ValueOrGetter<Exit[], Context>;
@@ -1,13 +1,13 @@
1
+ import { d as baseCreate, i as inspect, l as flatRest, r as extractType, t as DualModePrompt, u as copyArray } from "./dual-modes-Cy0_W4O2.js";
1
2
  import { a as Symbol, n as isArray, o as root, r as isObjectLike } from "./_overArg-CRYbj1NH.js";
2
3
  import { D as isBuffer, E as baseUnary, M as baseAssignValue, R as isObject, T as nodeUtil, _ as baseGet, c as _getTag_default, f as getSymbols, h as Stack, k as isPrototype, s as Uint8Array, u as getAllKeys, v as toKey, x as keys, y as castPath, z as arrayMap } from "./_baseIteratee-P_firB7b.js";
3
- import { f as flatRest, l as extractType, m as baseCreate, p as copyArray, t as DualModePrompt, u as inspect } from "./dual-modes-mNyMThtf.js";
4
- import { _ as getSymbolsIn, b as keysIn, g as getAllKeysIn, o as getTokenizer, x as assignValue, y as baseSlice } from "./utils-UBeAFeEI.js";
4
+ import { _ as getSymbolsIn, b as keysIn, g as getAllKeysIn, i as getErrorMessage, o as getTokenizer, x as assignValue, y as baseSlice } from "./utils-UBeAFeEI.js";
5
5
  import { t as getPrototype } from "./_getPrototype-CG7Dxrrz.js";
6
- import { t as isPlainObject } from "./isPlainObject-D39AzPO2.js";
6
+ import { a as isPlainObject } from "./jsx-B0iYyvfr.js";
7
+ import { n as assertValidComponent } from "./component-Dq7ZLdvy.js";
7
8
  import { t as Tool } from "./tool-Bv8N4IPj.js";
8
- import { o as LoopExceededError } from "./errors-Cg1G0Awu.js";
9
+ import { l as ThinkSignal, o as LoopExceededError } from "./errors-Cg1G0Awu.js";
9
10
  import { t as Exit } from "./exit-CDdxDBRT.js";
10
- import { n as assertValidComponent } from "./component-DGG7d_Mz.js";
11
11
  import { i as wrapContent, t as stripTruncationTags } from "./truncator-DZqEqnpD.js";
12
12
  import { z } from "@bpinternal/zui";
13
13
  import { ulid } from "ulid";
@@ -819,7 +819,8 @@ function parseExit(returnValue, exits) {
819
819
  }
820
820
  const schema = returnExit.zSchema;
821
821
  const schemaType = schema._def.typeName;
822
- let parsed = schema.safeParse(valueToValidate);
822
+ const initialParse = schema.safeParse(valueToValidate);
823
+ let parsed = initialParse;
823
824
  if (!parsed.success && alternativeValue !== void 0 && alternativeValue !== valueToValidate) {
824
825
  const altParsed = schema.safeParse(alternativeValue);
825
826
  if (altParsed.success) {
@@ -833,6 +834,7 @@ function parseExit(returnValue, exits) {
833
834
  parsed = result.parsed;
834
835
  }
835
836
  if (!parsed.success) {
837
+ var _initialParse$error;
836
838
  const getValueTypeDescription = (val) => {
837
839
  if (val === null) return "null";
838
840
  if (val === void 0) return "undefined";
@@ -894,6 +896,7 @@ function parseExit(returnValue, exits) {
894
896
  "But expected one of:",
895
897
  ...expectedStatements.map((s) => ` ${s}`)
896
898
  ].join("\n"),
899
+ validationErrors: (((_initialParse$error = initialParse.error) === null || _initialParse$error === void 0 ? void 0 : _initialParse$error.issues) ?? []).map((issue) => `${issue.path.join(".") || "value"}: ${issue.message}`),
897
900
  returnValue
898
901
  };
899
902
  }
@@ -1788,6 +1791,49 @@ var HookedArray = class extends Array {
1788
1791
  }
1789
1792
  };
1790
1793
  //#endregion
1794
+ //#region src/prompts/execution-history.ts
1795
+ function describeOutcome(status) {
1796
+ switch (status.type) {
1797
+ case "thinking_requested": return status.thinking_requested.interrupted ? "a tool paused execution" : "code completed and returned control";
1798
+ case "execution_error": return "code execution failed";
1799
+ case "invalid_code_error": return "invalid code or protocol";
1800
+ case "exit_error": return "exit validation failed";
1801
+ default: return status.type;
1802
+ }
1803
+ }
1804
+ /** Summarize observed effects, including failed calls and retracted deliveries. */
1805
+ function summarizeIterations(iterations, canTalk) {
1806
+ const history = [];
1807
+ const attempts = /* @__PURE__ */ new Map();
1808
+ const deliveredMessages = [];
1809
+ for (const [index, iteration] of iterations.entries()) {
1810
+ const calls = [];
1811
+ const lastRestart = iteration.traces.reduce((last, trace, traceIndex) => trace.type === "llm_call_restarted" ? traceIndex : last, -1);
1812
+ for (const [traceIndex, trace] of iteration.traces.entries()) {
1813
+ if (trace.type === "tool_call") {
1814
+ const name = trace.object ? `${trace.object}.${trace.tool_name}` : trace.tool_name;
1815
+ let outcome = "failed or paused";
1816
+ if (trace.success) outcome = trace.output instanceof ThinkSignal ? "paused for attention" : "succeeded";
1817
+ attempts.set(name, (attempts.get(name) ?? 0) + 1);
1818
+ calls.push(`${name}: ${outcome}`);
1819
+ }
1820
+ if (canTalk && trace.type === "yield") deliveredMessages.push({
1821
+ iteration: index + 1,
1822
+ content: trace.value,
1823
+ ...traceIndex < lastRestart ? { retracted: true } : {}
1824
+ });
1825
+ }
1826
+ const outcome = describeOutcome(iteration.status);
1827
+ const toolSummary = calls.length ? ` Tool attempts: ${calls.join("; ")}.` : "";
1828
+ history.push(`Iteration ${index + 1}: ${outcome}.${toolSummary}`);
1829
+ }
1830
+ return {
1831
+ history,
1832
+ toolAttempts: Object.fromEntries(attempts),
1833
+ deliveredMessages: canTalk ? deliveredMessages : void 0
1834
+ };
1835
+ }
1836
+ //#endregion
1791
1837
  //#region src/transcript.ts
1792
1838
  const MAX_MESSAGE_LENGTH = 5e3;
1793
1839
  function getMessagePreview(message) {
@@ -1991,7 +2037,7 @@ const ThinkExit = new Exit({
1991
2037
  */
1992
2038
  const ListenExit = new Exit({
1993
2039
  name: "listen",
1994
- description: "Listen to the user and provide a response"
2040
+ description: "Stop talking and wait for the user to talk next."
1995
2041
  });
1996
2042
  /**
1997
2043
  * Default exit used when no custom exits are provided.
@@ -2117,6 +2163,9 @@ var Iteration = class {
2117
2163
  this._mutations.set(`${mutation.object ?? "global"}:${mutation.property}`, mutation);
2118
2164
  }
2119
2165
  _parameters;
2166
+ get components() {
2167
+ return this._parameters.components;
2168
+ }
2120
2169
  get transcript() {
2121
2170
  return this._parameters.transcript;
2122
2171
  }
@@ -2234,6 +2283,7 @@ var Context = class {
2234
2283
  id;
2235
2284
  chat;
2236
2285
  instructions;
2286
+ examples;
2237
2287
  objects;
2238
2288
  tools;
2239
2289
  exits;
@@ -2271,6 +2321,7 @@ var Context = class {
2271
2321
  transcriptionModel;
2272
2322
  metadata;
2273
2323
  snapshot;
2324
+ _lastMessageWithoutExecutionState;
2274
2325
  iteration = 0;
2275
2326
  iterations;
2276
2327
  async nextIteration() {
@@ -2329,18 +2380,20 @@ var Context = class {
2329
2380
  const tools = countText(parts.tools);
2330
2381
  const transcript = countText(parts.transcript);
2331
2382
  const protocol = countText(parts.protocol);
2383
+ const examples = countText(parts.examples);
2332
2384
  const systemTokens = messages.filter((x) => x.role === "system").reduce((acc, x) => acc + countMessage(x), 0);
2333
2385
  const otherTokens = messages.filter((x) => x.role !== "system").reduce((acc, x) => acc + countMessage(x), 0);
2334
2386
  const isFirstIteration = this.iterations.length === 0 && !this.snapshot;
2335
- const framework = Math.max(0, systemTokens - (instructions + tools + transcript + protocol));
2387
+ const framework = Math.max(0, systemTokens - (instructions + tools + transcript + protocol + examples));
2336
2388
  const iterations = isFirstIteration ? 0 : otherTokens;
2337
2389
  return {
2338
- total: framework + instructions + tools + transcript + protocol + iterations + (isFirstIteration ? otherTokens : 0),
2390
+ total: framework + instructions + tools + transcript + protocol + examples + iterations + (isFirstIteration ? otherTokens : 0),
2339
2391
  framework: framework + (isFirstIteration ? otherTokens : 0),
2340
2392
  instructions,
2341
2393
  tools,
2342
2394
  transcript,
2343
2395
  protocol,
2396
+ examples,
2344
2397
  iterations
2345
2398
  };
2346
2399
  }
@@ -2348,24 +2401,50 @@ var Context = class {
2348
2401
  var _this$snapshot2, _this$snapshot3;
2349
2402
  const lastIteration = this.iterations.at(-1);
2350
2403
  const promptProps = {
2404
+ iteration: {
2405
+ current: this.iterations.length + 1,
2406
+ limit: this.loop,
2407
+ resumed: !!this.snapshot,
2408
+ ...summarizeIterations(this.iterations, parameters.components.length > 0)
2409
+ },
2351
2410
  globalTools: parameters.tools,
2352
2411
  objects: parameters.objects,
2353
2412
  instructions: parameters.instructions,
2413
+ examples: parameters.examples,
2354
2414
  transcript: parameters.transcript,
2355
2415
  exits: parameters.components.length ? [...parameters.exits, ListenExit] : parameters.exits,
2356
2416
  components: parameters.components
2357
2417
  };
2358
2418
  const { message: systemMessage, parts } = await this.version.getSystemMessage(promptProps);
2359
- const withParts = (messages) => ({
2360
- messages,
2361
- parts
2362
- });
2419
+ const previousLastMessage = this._lastMessageWithoutExecutionState;
2420
+ const withParts = (messages) => {
2421
+ var _this$version$getExec, _this$version;
2422
+ const last = messages.at(-1);
2423
+ this._lastMessageWithoutExecutionState = last;
2424
+ const state = ((_this$version$getExec = (_this$version = this.version).getExecutionState) === null || _this$version$getExec === void 0 ? void 0 : _this$version$getExec.call(_this$version, promptProps)) ?? "";
2425
+ if (state) messages[messages.length - 1] = Array.isArray(last.content) ? {
2426
+ ...last,
2427
+ content: [...last.content, {
2428
+ type: "text",
2429
+ text: state.trim()
2430
+ }]
2431
+ } : {
2432
+ ...last,
2433
+ content: (last.content ?? "") + state
2434
+ };
2435
+ return {
2436
+ messages,
2437
+ parts
2438
+ };
2439
+ };
2363
2440
  if (((_this$snapshot2 = this.snapshot) === null || _this$snapshot2 === void 0 ? void 0 : _this$snapshot2.status.type) === "resolved") return withParts([systemMessage, this.version.getSnapshotResolvedMessage({ snapshot: this.snapshot })]);
2364
2441
  if (((_this$snapshot3 = this.snapshot) === null || _this$snapshot3 === void 0 ? void 0 : _this$snapshot3.status.type) === "rejected") return withParts([systemMessage, this.version.getSnapshotRejectedMessage({ snapshot: this.snapshot })]);
2365
2442
  if (!lastIteration) return withParts([systemMessage, await this.version.getInitialUserMessage(promptProps)]);
2366
- const lastIterationMessages = [systemMessage, ...lastIteration.messages.filter((x) => x.role !== "system")];
2443
+ const history = lastIteration.messages.slice();
2444
+ if (previousLastMessage) history[history.length - 1] = previousLastMessage;
2445
+ const lastIterationMessages = [systemMessage, ...history.filter((x) => x.role !== "system")];
2367
2446
  if ((lastIteration === null || lastIteration === void 0 ? void 0 : lastIteration.status.type) === "thinking_requested") {
2368
- var _lastIteration$llm;
2447
+ var _lastIteration$llm, _lastIteration$llm2;
2369
2448
  return withParts([
2370
2449
  ...lastIterationMessages,
2371
2450
  {
@@ -2377,60 +2456,73 @@ var Context = class {
2377
2456
  })
2378
2457
  },
2379
2458
  await this.version.getThinkingMessage({
2459
+ isChatEnabled: parameters.components.length > 0,
2380
2460
  reason: lastIteration.status.thinking_requested.reason,
2381
- variables: lastIteration.status.thinking_requested.variables
2461
+ variables: lastIteration.status.thinking_requested.variables,
2462
+ interrupted: lastIteration.status.thinking_requested.interrupted,
2463
+ discardedMessages: (_lastIteration$llm2 = lastIteration.llm) === null || _lastIteration$llm2 === void 0 || (_lastIteration$llm2 = _lastIteration$llm2.diagnostics) === null || _lastIteration$llm2 === void 0 ? void 0 : _lastIteration$llm2.some((diagnostic) => diagnostic.code === "send-after-run")
2382
2464
  })
2383
2465
  ]);
2384
2466
  }
2385
2467
  if ((lastIteration === null || lastIteration === void 0 ? void 0 : lastIteration.status.type) === "exit_error") {
2386
- var _lastIteration$llm2;
2468
+ var _lastIteration$llm3;
2387
2469
  return withParts([
2388
2470
  ...lastIterationMessages,
2389
2471
  {
2390
2472
  role: "assistant",
2391
- content: wrapContent(((_lastIteration$llm2 = lastIteration.llm) === null || _lastIteration$llm2 === void 0 ? void 0 : _lastIteration$llm2.output) ?? "", {
2473
+ content: wrapContent(((_lastIteration$llm3 = lastIteration.llm) === null || _lastIteration$llm3 === void 0 ? void 0 : _lastIteration$llm3.output) ?? "", {
2392
2474
  preserve: "top",
2393
2475
  flex: 4,
2394
2476
  minTokens: 25
2395
2477
  })
2396
2478
  },
2397
2479
  await this.version.getInvalidCodeMessage({
2398
- code: lastIteration.code ?? "// No code generated",
2399
- message: `Invalid return statement (action: ${lastIteration.status.exit_error.exit}): ${lastIteration.status.exit_error.message}`
2480
+ isChatEnabled: parameters.components.length > 0,
2481
+ code: lastIteration.next ? `■next=${lastIteration.next.name} ${JSON.stringify(lastIteration.next.props)}` : lastIteration.code ?? "// No code generated",
2482
+ message: `Invalid ■next block (${lastIteration.status.exit_error.exit}): ${lastIteration.status.exit_error.message}`
2400
2483
  })
2401
2484
  ]);
2402
2485
  }
2403
2486
  if ((lastIteration === null || lastIteration === void 0 ? void 0 : lastIteration.status.type) === "invalid_code_error") {
2404
- var _lastIteration$llm3;
2487
+ var _lastIteration$llm4;
2405
2488
  return withParts([
2406
2489
  ...lastIterationMessages,
2407
2490
  {
2408
2491
  role: "assistant",
2409
- content: wrapContent(((_lastIteration$llm3 = lastIteration.llm) === null || _lastIteration$llm3 === void 0 ? void 0 : _lastIteration$llm3.output) ?? "", {
2492
+ content: wrapContent(((_lastIteration$llm4 = lastIteration.llm) === null || _lastIteration$llm4 === void 0 ? void 0 : _lastIteration$llm4.output) ?? "", {
2410
2493
  preserve: "top",
2411
2494
  flex: 4,
2412
2495
  minTokens: 25
2413
2496
  })
2414
2497
  },
2415
2498
  await this.version.getInvalidCodeMessage({
2499
+ isChatEnabled: parameters.components.length > 0,
2416
2500
  code: lastIteration.code ?? "// No code generated",
2417
2501
  message: lastIteration.status.invalid_code_error.message
2418
2502
  })
2419
2503
  ]);
2420
2504
  }
2421
2505
  if ((lastIteration === null || lastIteration === void 0 ? void 0 : lastIteration.status.type) === "execution_error") {
2422
- var _lastIteration$llm4;
2506
+ var _lastIteration$llm5;
2423
2507
  return withParts([
2424
2508
  ...lastIterationMessages,
2425
2509
  {
2426
2510
  role: "assistant",
2427
- content: wrapContent(((_lastIteration$llm4 = lastIteration.llm) === null || _lastIteration$llm4 === void 0 ? void 0 : _lastIteration$llm4.output) ?? "", {
2511
+ content: wrapContent(((_lastIteration$llm5 = lastIteration.llm) === null || _lastIteration$llm5 === void 0 ? void 0 : _lastIteration$llm5.output) ?? "", {
2428
2512
  preserve: "top",
2429
2513
  flex: 4,
2430
2514
  minTokens: 25
2431
2515
  })
2432
2516
  },
2433
2517
  await this.version.getCodeExecutionErrorMessage({
2518
+ isChatEnabled: parameters.components.length > 0,
2519
+ variables: lastIteration.variables,
2520
+ toolCalls: lastIteration.traces.filter((trace) => trace.type === "tool_call").map((trace) => ({
2521
+ tool: trace.tool_name,
2522
+ input: trace.input,
2523
+ success: trace.success,
2524
+ ...trace.success ? { output: trace.output } : { error: getErrorMessage(trace.error) }
2525
+ })),
2434
2526
  message: lastIteration.status.execution_error.message,
2435
2527
  stacktrace: lastIteration.status.execution_error.stack
2436
2528
  })
@@ -2441,6 +2533,7 @@ var Context = class {
2441
2533
  async _refreshIterationParameters() {
2442
2534
  var _this$chat, _this$chat2;
2443
2535
  const instructions = await getValue(this.instructions, this);
2536
+ const examples = await getValue(this.examples, this);
2444
2537
  const transcript = new TranscriptArray(await getValue(((_this$chat = this.chat) === null || _this$chat === void 0 ? void 0 : _this$chat.transcript) ?? [], this));
2445
2538
  const tools = Tool.withUniqueNames(await getValue(this.tools, this) ?? []);
2446
2539
  const objects = await getValue(this.objects, this) ?? [];
@@ -2497,6 +2590,7 @@ var Context = class {
2497
2590
  objects,
2498
2591
  exits,
2499
2592
  instructions,
2593
+ examples,
2500
2594
  components,
2501
2595
  model,
2502
2596
  temperature,
@@ -2506,6 +2600,7 @@ var Context = class {
2506
2600
  constructor(props) {
2507
2601
  this.id = `llmz_${ulid()}`;
2508
2602
  this.instructions = props.instructions;
2603
+ this.examples = props.examples;
2509
2604
  this.objects = props.objects;
2510
2605
  this.tools = props.tools;
2511
2606
  this.exits = props.exits;