llmz 0.7.0 → 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 +2 -2
  46. package/dist/isPlainObject-CSxXVKCk.cjs +0 -56
  47. package/dist/isPlainObject-D39AzPO2.js +0 -51
@@ -1,14 +1,14 @@
1
+ const require_dual_modes = require("./dual-modes-Cb7ThvoV.cjs");
1
2
  const require__overArg = require("./_overArg-C5Hy7oCi.cjs");
2
3
  const require__baseIteratee = require("./_baseIteratee-CF4qP2a8.cjs");
3
- const require_dual_modes = require("./dual-modes-CfqTk9M8.cjs");
4
- const require_utils = require("./utils-ZpJm00ed.cjs");
4
+ const require_utils = require("./utils-3ESwP3yg.cjs");
5
5
  const require__getPrototype = require("./_getPrototype-DQDlNVVM.cjs");
6
- const require_isPlainObject = require("./isPlainObject-CSxXVKCk.cjs");
7
- const require_tool = require("./tool-ReA9gxMp.cjs");
6
+ const require_jsx = require("./jsx-D5Y2cPo-.cjs");
7
+ const require_component = require("./component-BXS8vAwO.cjs");
8
+ const require_tool = require("./tool-Dy6AJpJH.cjs");
8
9
  const require_errors = require("./errors-C962uJdS.cjs");
9
- const require_exit = require("./exit-BGi0Kk0y.cjs");
10
- const require_component = require("./component-BwixNVH8.cjs");
11
- const require_truncator = require("./truncator-B6aaJaN1.cjs");
10
+ const require_exit = require("./exit-DUM96DN1.cjs");
11
+ const require_truncator = require("./truncator-CLkouTCO.cjs");
12
12
  let _bpinternal_zui = require("@bpinternal/zui");
13
13
  let ulid = require("ulid");
14
14
  //#region ../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_arrayEach.js
@@ -539,7 +539,7 @@ function baseUnset(object, path) {
539
539
  * @returns {*} Returns the uncloned value or `undefined` to defer cloning to `_.cloneDeep`.
540
540
  */
541
541
  function customOmitClone(value) {
542
- return require_isPlainObject.isPlainObject(value) ? void 0 : value;
542
+ return require_jsx.isPlainObject(value) ? void 0 : value;
543
543
  }
544
544
  //#endregion
545
545
  //#region ../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/omit.js
@@ -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 require_errors.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 require_exit.Exit({
1991
2037
  */
1992
2038
  const ListenExit = new require_exit.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() {
@@ -2302,9 +2353,9 @@ var Context = class {
2302
2353
  const variables = {};
2303
2354
  if ((lastIteration === null || lastIteration === void 0 ? void 0 : lastIteration.status.type) === "thinking_requested") {
2304
2355
  const lastThinkingVariables = lastIteration.status.thinking_requested.variables;
2305
- if (require_isPlainObject.isPlainObject(lastThinkingVariables)) Object.assign(variables, cloneDeep(lastThinkingVariables));
2356
+ if (require_jsx.isPlainObject(lastThinkingVariables)) Object.assign(variables, cloneDeep(lastThinkingVariables));
2306
2357
  }
2307
- if (require_isPlainObject.isPlainObject(lastIteration === null || lastIteration === void 0 ? void 0 : lastIteration.variables)) Object.assign(variables, cloneDeep((lastIteration === null || lastIteration === void 0 ? void 0 : lastIteration.variables) ?? {}));
2358
+ if (require_jsx.isPlainObject(lastIteration === null || lastIteration === void 0 ? void 0 : lastIteration.variables)) Object.assign(variables, cloneDeep((lastIteration === null || lastIteration === void 0 ? void 0 : lastIteration.variables) ?? {}));
2308
2359
  if (((_this$snapshot = this.snapshot) === null || _this$snapshot === void 0 ? void 0 : _this$snapshot.status.type) === "resolved") {
2309
2360
  for (const v of this.snapshot.variables) if (!v.truncated && v.value !== void 0) variables[v.name] = v.value;
2310
2361
  }
@@ -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: require_truncator.wrapContent(((_lastIteration$llm2 = lastIteration.llm) === null || _lastIteration$llm2 === void 0 ? void 0 : _lastIteration$llm2.output) ?? "", {
2473
+ content: require_truncator.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: require_truncator.wrapContent(((_lastIteration$llm3 = lastIteration.llm) === null || _lastIteration$llm3 === void 0 ? void 0 : _lastIteration$llm3.output) ?? "", {
2492
+ content: require_truncator.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: require_truncator.wrapContent(((_lastIteration$llm4 = lastIteration.llm) === null || _lastIteration$llm4 === void 0 ? void 0 : _lastIteration$llm4.output) ?? "", {
2511
+ content: require_truncator.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: require_utils.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 = require_tool.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_${(0, ulid.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;