@positronic/core 0.0.77 → 0.0.78

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 (130) hide show
  1. package/dist/src/dsl/brain-runner.js +37 -182
  2. package/dist/src/dsl/brain-state-machine.js +58 -112
  3. package/dist/src/dsl/brain.js +1 -0
  4. package/dist/src/dsl/builder/brain.js +250 -412
  5. package/dist/src/dsl/builder/continuation.js +67 -0
  6. package/dist/src/dsl/constants.js +16 -17
  7. package/dist/src/dsl/create-brain.js +29 -45
  8. package/dist/src/dsl/example-webhook.js +1 -1
  9. package/dist/src/dsl/execution/constants.js +0 -4
  10. package/dist/src/dsl/execution/event-channel.js +77 -0
  11. package/dist/src/dsl/execution/event-stream.js +1625 -1474
  12. package/dist/src/dsl/signal-validation.js +0 -1
  13. package/dist/src/dsl/webhook.js +4 -1
  14. package/dist/src/files/event-wrapper.js +268 -0
  15. package/dist/src/files/index.js +1 -0
  16. package/dist/src/files/mime.js +31 -0
  17. package/dist/src/files/types.js +13 -0
  18. package/dist/src/index.js +10 -3
  19. package/dist/src/jsx-runtime.js +49 -0
  20. package/dist/src/memory/{scoped-memory.js → create-memory.js} +5 -5
  21. package/dist/src/plugins/collect-webhooks.js +49 -0
  22. package/dist/src/plugins/define-plugin.js +15 -0
  23. package/dist/src/plugins/index.js +1 -0
  24. package/dist/src/plugins/types.js +4 -0
  25. package/dist/src/store/types.js +2 -3
  26. package/dist/src/template/render-html.js +282 -0
  27. package/dist/src/template/render.js +357 -0
  28. package/dist/src/tools/index.js +88 -27
  29. package/dist/src/ui/generate-page-html.js +6 -6
  30. package/dist/src/ui/{generate-ui.js → generate-page.js} +24 -12
  31. package/dist/src/ui/parse-form-data.js +1 -11
  32. package/dist/src/ui/types.js +0 -123
  33. package/dist/src/yaml/data-validator.js +0 -27
  34. package/dist/src/yaml/type-inference.js +10 -9
  35. package/dist/types/clients/types.d.ts +15 -5
  36. package/dist/types/clients/types.d.ts.map +1 -1
  37. package/dist/types/dsl/brain-runner.d.ts +9 -14
  38. package/dist/types/dsl/brain-runner.d.ts.map +1 -1
  39. package/dist/types/dsl/brain-state-machine.d.ts +18 -28
  40. package/dist/types/dsl/brain-state-machine.d.ts.map +1 -1
  41. package/dist/types/dsl/brain.d.ts +3 -2
  42. package/dist/types/dsl/brain.d.ts.map +1 -1
  43. package/dist/types/dsl/builder/brain.d.ts +85 -228
  44. package/dist/types/dsl/builder/brain.d.ts.map +1 -1
  45. package/dist/types/dsl/builder/continuation.d.ts +16 -0
  46. package/dist/types/dsl/builder/continuation.d.ts.map +1 -0
  47. package/dist/types/dsl/constants.d.ts +12 -12
  48. package/dist/types/dsl/constants.d.ts.map +1 -1
  49. package/dist/types/dsl/create-brain.d.ts +17 -57
  50. package/dist/types/dsl/create-brain.d.ts.map +1 -1
  51. package/dist/types/dsl/definitions/blocks.d.ts +67 -66
  52. package/dist/types/dsl/definitions/blocks.d.ts.map +1 -1
  53. package/dist/types/dsl/definitions/brain-types.d.ts +8 -11
  54. package/dist/types/dsl/definitions/brain-types.d.ts.map +1 -1
  55. package/dist/types/dsl/definitions/events.d.ts +61 -56
  56. package/dist/types/dsl/definitions/events.d.ts.map +1 -1
  57. package/dist/types/dsl/definitions/run-params.d.ts +20 -21
  58. package/dist/types/dsl/definitions/run-params.d.ts.map +1 -1
  59. package/dist/types/dsl/definitions/steps.d.ts +1 -1
  60. package/dist/types/dsl/definitions/steps.d.ts.map +1 -1
  61. package/dist/types/dsl/example-webhook.d.ts +3 -3
  62. package/dist/types/dsl/example-webhook.d.ts.map +1 -1
  63. package/dist/types/dsl/execution/constants.d.ts +0 -5
  64. package/dist/types/dsl/execution/constants.d.ts.map +1 -1
  65. package/dist/types/dsl/execution/event-channel.d.ts +16 -0
  66. package/dist/types/dsl/execution/event-channel.d.ts.map +1 -0
  67. package/dist/types/dsl/execution/event-stream.d.ts +46 -34
  68. package/dist/types/dsl/execution/event-stream.d.ts.map +1 -1
  69. package/dist/types/dsl/signal-validation.d.ts.map +1 -1
  70. package/dist/types/dsl/types.d.ts +21 -83
  71. package/dist/types/dsl/types.d.ts.map +1 -1
  72. package/dist/types/dsl/webhook.d.ts +12 -2
  73. package/dist/types/dsl/webhook.d.ts.map +1 -1
  74. package/dist/types/files/event-wrapper.d.ts +20 -0
  75. package/dist/types/files/event-wrapper.d.ts.map +1 -0
  76. package/dist/types/files/index.d.ts +2 -0
  77. package/dist/types/files/index.d.ts.map +1 -0
  78. package/dist/types/files/mime.d.ts +2 -0
  79. package/dist/types/files/mime.d.ts.map +1 -0
  80. package/dist/types/files/types.d.ts +88 -0
  81. package/dist/types/files/types.d.ts.map +1 -0
  82. package/dist/types/index.d.ts +22 -13
  83. package/dist/types/index.d.ts.map +1 -1
  84. package/dist/types/jsx-runtime.d.ts +35 -0
  85. package/dist/types/jsx-runtime.d.ts.map +1 -0
  86. package/dist/types/memory/{scoped-memory.d.ts → create-memory.d.ts} +7 -7
  87. package/dist/types/memory/create-memory.d.ts.map +1 -0
  88. package/dist/types/memory/types.d.ts +4 -4
  89. package/dist/types/memory/types.d.ts.map +1 -1
  90. package/dist/types/plugins/collect-webhooks.d.ts +8 -0
  91. package/dist/types/plugins/collect-webhooks.d.ts.map +1 -0
  92. package/dist/types/plugins/define-plugin.d.ts +25 -0
  93. package/dist/types/plugins/define-plugin.d.ts.map +1 -0
  94. package/dist/types/plugins/index.d.ts +3 -0
  95. package/dist/types/plugins/index.d.ts.map +1 -0
  96. package/dist/types/plugins/types.d.ts +83 -0
  97. package/dist/types/plugins/types.d.ts.map +1 -0
  98. package/dist/types/store/index.d.ts +1 -1
  99. package/dist/types/store/index.d.ts.map +1 -1
  100. package/dist/types/store/types.d.ts +0 -8
  101. package/dist/types/store/types.d.ts.map +1 -1
  102. package/dist/types/template/render-html.d.ts +9 -0
  103. package/dist/types/template/render-html.d.ts.map +1 -0
  104. package/dist/types/template/render.d.ts +15 -0
  105. package/dist/types/template/render.d.ts.map +1 -0
  106. package/dist/types/tools/index.d.ts +39 -27
  107. package/dist/types/tools/index.d.ts.map +1 -1
  108. package/dist/types/ui/generate-page-html.d.ts +4 -6
  109. package/dist/types/ui/generate-page-html.d.ts.map +1 -1
  110. package/dist/types/ui/{generate-ui.d.ts → generate-page.d.ts} +6 -6
  111. package/dist/types/ui/generate-page.d.ts.map +1 -0
  112. package/dist/types/ui/parse-form-data.d.ts +1 -5
  113. package/dist/types/ui/parse-form-data.d.ts.map +1 -1
  114. package/dist/types/ui/types.d.ts +0 -49
  115. package/dist/types/ui/types.d.ts.map +1 -1
  116. package/dist/types/yaml/data-validator.d.ts +0 -5
  117. package/dist/types/yaml/data-validator.d.ts.map +1 -1
  118. package/dist/types/yaml/type-inference.d.ts +4 -2
  119. package/dist/types/yaml/type-inference.d.ts.map +1 -1
  120. package/dist/types/yaml/types.d.ts +0 -1
  121. package/dist/types/yaml/types.d.ts.map +1 -1
  122. package/package.json +5 -1
  123. package/dist/src/dsl/agent-messages.js +0 -5
  124. package/dist/src/ui/validate-form.js +0 -428
  125. package/dist/types/dsl/agent-messages.d.ts +0 -12
  126. package/dist/types/dsl/agent-messages.d.ts.map +0 -1
  127. package/dist/types/memory/scoped-memory.d.ts.map +0 -1
  128. package/dist/types/ui/generate-ui.d.ts.map +0 -1
  129. package/dist/types/ui/validate-form.d.ts +0 -45
  130. package/dist/types/ui/validate-form.d.ts.map +0 -1
@@ -1,3 +1,11 @@
1
+ function _array_like_to_array(arr, len) {
2
+ if (len == null || len > arr.length) len = arr.length;
3
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
4
+ return arr2;
5
+ }
6
+ function _array_without_holes(arr) {
7
+ if (Array.isArray(arr)) return _array_like_to_array(arr);
8
+ }
1
9
  function _async_generator(gen) {
2
10
  var front, back;
3
11
  function send(key, arg) {
@@ -216,6 +224,12 @@ function _define_property(obj, key, value) {
216
224
  }
217
225
  return obj;
218
226
  }
227
+ function _iterable_to_array(iter) {
228
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
229
+ }
230
+ function _non_iterable_spread() {
231
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
232
+ }
219
233
  function _object_spread(target) {
220
234
  for(var i = 1; i < arguments.length; i++){
221
235
  var source = arguments[i] != null ? arguments[i] : {};
@@ -255,13 +269,47 @@ function _object_spread_props(target, source) {
255
269
  }
256
270
  return target;
257
271
  }
272
+ function _object_without_properties(source, excluded) {
273
+ if (source == null) return {};
274
+ var target = _object_without_properties_loose(source, excluded);
275
+ var key, i;
276
+ if (Object.getOwnPropertySymbols) {
277
+ var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
278
+ for(i = 0; i < sourceSymbolKeys.length; i++){
279
+ key = sourceSymbolKeys[i];
280
+ if (excluded.indexOf(key) >= 0) continue;
281
+ if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
282
+ target[key] = source[key];
283
+ }
284
+ }
285
+ return target;
286
+ }
287
+ function _object_without_properties_loose(source, excluded) {
288
+ if (source == null) return {};
289
+ var target = {};
290
+ var sourceKeys = Object.keys(source);
291
+ var key, i;
292
+ for(i = 0; i < sourceKeys.length; i++){
293
+ key = sourceKeys[i];
294
+ if (excluded.indexOf(key) >= 0) continue;
295
+ target[key] = source[key];
296
+ }
297
+ return target;
298
+ }
258
299
  function _overload_yield(value, kind) {
259
300
  this.v = value;
260
301
  this.k = kind;
261
302
  }
262
- function _type_of(obj) {
263
- "@swc/helpers - typeof";
264
- return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
303
+ function _to_consumable_array(arr) {
304
+ return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
305
+ }
306
+ function _unsupported_iterable_to_array(o, minLen) {
307
+ if (!o) return;
308
+ if (typeof o === "string") return _array_like_to_array(o, minLen);
309
+ var n = Object.prototype.toString.call(o).slice(8, -1);
310
+ if (n === "Object" && o.constructor) n = o.constructor.name;
311
+ if (n === "Map" || n === "Set") return Array.from(n);
312
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
265
313
  }
266
314
  function _wrap_async_generator(fn) {
267
315
  return function() {
@@ -373,9 +421,59 @@ function _ts_values(o) {
373
421
  };
374
422
  throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
375
423
  }
376
- import { z } from 'zod';
424
+ import { Continuation } from './continuation.js';
377
425
  import { BrainEventStream } from '../execution/event-stream.js';
378
426
  import { parseDuration } from '../duration.js';
427
+ /**
428
+ * Merge parent and own plugin configs, call create() for each, separate adapters from injections.
429
+ */ function resolvePlugins(parentConfigs, ownConfigs, ctx) {
430
+ // Own configs override parent by plugin name
431
+ var merged = _to_consumable_array(parentConfigs.filter(function(p) {
432
+ return !ownConfigs.some(function(own) {
433
+ return own.__plugin.name === p.__plugin.name;
434
+ });
435
+ })).concat(_to_consumable_array(ownConfigs));
436
+ var injections = {};
437
+ var adapters = [];
438
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
439
+ try {
440
+ for(var _iterator = merged[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
441
+ var _ref = _step.value;
442
+ var configured = _ref;
443
+ var plugin = configured.__plugin, config = configured.__config;
444
+ var _plugin_create = plugin.create({
445
+ config: config,
446
+ brainTitle: ctx.brainTitle,
447
+ currentUser: ctx.currentUser,
448
+ brainRunId: ctx.brainRunId
449
+ }), adapter = _plugin_create.adapter, injection = _object_without_properties(_plugin_create, [
450
+ "adapter"
451
+ ]);
452
+ injections[plugin.name] = injection;
453
+ if (adapter) {
454
+ adapters.push(adapter);
455
+ }
456
+ }
457
+ } catch (err) {
458
+ _didIteratorError = true;
459
+ _iteratorError = err;
460
+ } finally{
461
+ try {
462
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
463
+ _iterator.return();
464
+ }
465
+ } finally{
466
+ if (_didIteratorError) {
467
+ throw _iteratorError;
468
+ }
469
+ }
470
+ }
471
+ return {
472
+ injections: injections,
473
+ adapters: adapters,
474
+ configs: merged
475
+ };
476
+ }
379
477
  export var Brain = /*#__PURE__*/ function() {
380
478
  "use strict";
381
479
  function Brain(title, description) {
@@ -384,18 +482,15 @@ export var Brain = /*#__PURE__*/ function() {
384
482
  _define_property(this, "description", void 0);
385
483
  _define_property(this, "blocks", void 0);
386
484
  _define_property(this, "type", void 0);
387
- _define_property(this, "services", void 0);
388
485
  _define_property(this, "optionsSchema", void 0);
389
486
  _define_property(this, "components", void 0);
390
- _define_property(this, "defaultTools", void 0);
391
- _define_property(this, "extraTools", void 0);
392
- _define_property(this, "memoryProvider", void 0);
393
487
  _define_property(this, "storeSchema", void 0);
488
+ _define_property(this, "pluginConfigs", void 0);
394
489
  this.title = title;
395
490
  this.description = description;
396
491
  this.blocks = [];
397
492
  this.type = 'brain';
398
- this.services = {};
493
+ this.pluginConfigs = [];
399
494
  }
400
495
  _create_class(Brain, [
401
496
  {
@@ -410,11 +505,6 @@ export var Brain = /*#__PURE__*/ function() {
410
505
  type: 'step',
411
506
  title: block.title
412
507
  };
413
- } else if (block.type === 'agent') {
414
- return {
415
- type: 'agent',
416
- title: block.title
417
- };
418
508
  } else if (block.type === 'guard') {
419
509
  return {
420
510
  type: 'guard',
@@ -425,6 +515,16 @@ export var Brain = /*#__PURE__*/ function() {
425
515
  type: 'wait',
426
516
  title: block.title
427
517
  };
518
+ } else if (block.type === 'map') {
519
+ return {
520
+ type: 'map',
521
+ title: block.title
522
+ };
523
+ } else if (block.type === 'prompt') {
524
+ return {
525
+ type: 'prompt',
526
+ title: block.title
527
+ };
428
528
  } else {
429
529
  // block.type === 'brain'
430
530
  return {
@@ -438,39 +538,17 @@ export var Brain = /*#__PURE__*/ function() {
438
538
  }
439
539
  },
440
540
  {
441
- // New method to add services
442
- key: "withServices",
443
- value: function withServices(services) {
444
- var nextBrain = new Brain(this.title, this.description).withBlocks(this.blocks);
445
- // Set services
446
- nextBrain.services = services;
447
- // Copy optionsSchema to maintain it through the chain
448
- nextBrain.optionsSchema = this.optionsSchema;
449
- nextBrain.components = this.components;
450
- nextBrain.defaultTools = this.defaultTools;
451
- nextBrain.extraTools = this.extraTools;
452
- nextBrain.memoryProvider = this.memoryProvider;
453
- nextBrain.storeSchema = this.storeSchema;
454
- return nextBrain;
455
- }
456
- },
457
- {
458
- key: "withOptionsSchema",
459
- value: function withOptionsSchema(schema) {
541
+ key: "withOptions",
542
+ value: function withOptions(schema) {
460
543
  var nextBrain = new Brain(this.title, this.description).withBlocks(this.blocks);
544
+ this.copyConfigTo(nextBrain);
461
545
  nextBrain.optionsSchema = schema;
462
- nextBrain.services = this.services;
463
- nextBrain.components = this.components;
464
- nextBrain.defaultTools = this.defaultTools;
465
- nextBrain.extraTools = this.extraTools;
466
- nextBrain.memoryProvider = this.memoryProvider;
467
- nextBrain.storeSchema = this.storeSchema;
468
546
  return nextBrain;
469
547
  }
470
548
  },
471
549
  {
472
550
  /**
473
- * Configure UI components for generative UI steps.
551
+ * Configure UI components for page generation steps.
474
552
  *
475
553
  * @param components - Record of component definitions
476
554
  *
@@ -480,102 +558,18 @@ export var Brain = /*#__PURE__*/ function() {
480
558
  *
481
559
  * const myBrain = brain('my-brain')
482
560
  * .withComponents(components)
483
- * .ui('Show Form', formPrompt);
561
+ * .page('Show Form', ({ state }) => ({ prompt: `Create a form for ${state.userName}` }));
484
562
  * ```
485
563
  */ key: "withComponents",
486
564
  value: function withComponents(components) {
487
565
  var nextBrain = new Brain(this.title, this.description).withBlocks(this.blocks);
488
- nextBrain.optionsSchema = this.optionsSchema;
489
- nextBrain.services = this.services;
566
+ this.copyConfigTo(nextBrain);
490
567
  nextBrain.components = components;
491
- nextBrain.defaultTools = this.defaultTools;
492
- nextBrain.extraTools = this.extraTools;
493
- nextBrain.memoryProvider = this.memoryProvider;
494
- nextBrain.storeSchema = this.storeSchema;
495
568
  return nextBrain;
496
569
  }
497
570
  },
498
571
  {
499
572
  /**
500
- * Configure default tools for agent steps.
501
- * These tools will be automatically available in all agent steps and can be
502
- * extended or overridden in individual step configurations.
503
- *
504
- * @param tools - Record of default tool definitions
505
- *
506
- * @example
507
- * ```typescript
508
- * import { defaultTools } from '@positronic/core';
509
- *
510
- * const myBrain = brain('my-brain')
511
- * .withTools(defaultTools)
512
- * .brain('agent', ({ tools }) => ({
513
- * system: 'You are helpful',
514
- * prompt: 'Do something',
515
- * tools // uses defaults
516
- * }));
517
- * ```
518
- */ key: "withTools",
519
- value: function withTools(tools) {
520
- var next = this.nextBrain();
521
- next.defaultTools = tools;
522
- return next;
523
- }
524
- },
525
- {
526
- /**
527
- * Add extra tools on top of whatever default tools are already configured.
528
- * Unlike `withTools()` which replaces defaults, this merges additively.
529
- *
530
- * @param tools - Record of additional tool definitions
531
- *
532
- * @example
533
- * ```typescript
534
- * const myBrain = createBrain({ defaultTools })
535
- * ('my-brain')
536
- * .withExtraTools({ myCustomTool })
537
- * .brain('agent', ({ tools }) => ({
538
- * system: 'You are helpful',
539
- * prompt: 'Do something',
540
- * tools // includes both defaults and myCustomTool
541
- * }));
542
- * ```
543
- */ key: "withExtraTools",
544
- value: function withExtraTools(tools) {
545
- var next = this.nextBrain();
546
- next.extraTools = tools;
547
- return next;
548
- }
549
- },
550
- {
551
- /**
552
- * Configure a memory provider for this brain.
553
- * When configured, steps receive a scoped memory instance in their context.
554
- *
555
- * @param provider - The memory provider to use
556
- *
557
- * @example
558
- * ```typescript
559
- * import { createMem0Provider } from '@positronic/mem0';
560
- *
561
- * const memory = createMem0Provider({ apiKey: process.env.MEM0_API_KEY });
562
- *
563
- * const myBrain = brain('my-brain')
564
- * .withMemory(memory)
565
- * .brain('agent', async ({ memory }) => {
566
- * const prefs = await memory.search('user preferences');
567
- * return { system: `User preferences: ${prefs}`, prompt: 'Help me' };
568
- * });
569
- * ```
570
- */ key: "withMemory",
571
- value: function withMemory(provider) {
572
- var next = this.nextBrain();
573
- next.memoryProvider = provider;
574
- return next;
575
- }
576
- },
577
- {
578
- /**
579
573
  * Configure a typed key-value store for this brain.
580
574
  * When configured, steps receive a `store` object in their context with typed get/set/delete/has.
581
575
  *
@@ -598,16 +592,29 @@ export var Brain = /*#__PURE__*/ function() {
598
592
  */ key: "withStore",
599
593
  value: function withStore(storeSchema) {
600
594
  var nextBrain = new Brain(this.title, this.description).withBlocks(this.blocks);
601
- nextBrain.optionsSchema = this.optionsSchema;
602
- nextBrain.services = this.services;
603
- nextBrain.components = this.components;
604
- nextBrain.defaultTools = this.defaultTools;
605
- nextBrain.extraTools = this.extraTools;
606
- nextBrain.memoryProvider = this.memoryProvider;
595
+ this.copyConfigTo(nextBrain);
607
596
  nextBrain.storeSchema = storeSchema;
608
597
  return nextBrain;
609
598
  }
610
599
  },
600
+ {
601
+ /**
602
+ * Add a plugin to this brain. The plugin's create() is called per brain run,
603
+ * and its return value is placed on StepContext under the plugin name.
604
+ *
605
+ * Replaces any existing plugin with the same name.
606
+ */ key: "withPlugin",
607
+ value: function withPlugin(plugin) {
608
+ var nextBrain = new Brain(this.title, this.description).withBlocks(this.blocks);
609
+ this.copyConfigTo(nextBrain);
610
+ nextBrain.pluginConfigs = _to_consumable_array(this.pluginConfigs.filter(function(p) {
611
+ return p.__plugin.name !== plugin.__plugin.name;
612
+ })).concat([
613
+ plugin
614
+ ]);
615
+ return nextBrain;
616
+ }
617
+ },
611
618
  {
612
619
  key: "step",
613
620
  value: function step(title, action) {
@@ -631,7 +638,7 @@ export var Brain = /*#__PURE__*/ function() {
631
638
  timeout: parseDuration(options.timeout)
632
639
  });
633
640
  this.blocks.push(waitBlock);
634
- return this.nextBrain();
641
+ return this.continuationCallbacks();
635
642
  }
636
643
  },
637
644
  {
@@ -647,272 +654,65 @@ export var Brain = /*#__PURE__*/ function() {
647
654
  }
648
655
  },
649
656
  {
650
- // Implementation
657
+ // Nested brain — spreads inner brain's final state onto outer state
651
658
  key: "brain",
652
- value: function brain(title, innerBrainOrConfig, actionOrIterateConfig, initialState) {
653
- // Detect iterate config: 3rd arg is an object with `over` property
654
- var isIterateConfig = actionOrIterateConfig && (typeof actionOrIterateConfig === "undefined" ? "undefined" : _type_of(actionOrIterateConfig)) === 'object' && 'over' in actionOrIterateConfig;
655
- // Case 1: Nested brain instance
656
- if (innerBrainOrConfig && (typeof innerBrainOrConfig === "undefined" ? "undefined" : _type_of(innerBrainOrConfig)) === 'object' && 'type' in innerBrainOrConfig && innerBrainOrConfig.type === 'brain') {
657
- if (isIterateConfig) {
658
- // Case 1b: Nested brain with iterate
659
- var iterateConfig = actionOrIterateConfig;
660
- var nestedBlock = {
661
- type: 'brain',
662
- title: title,
663
- innerBrain: innerBrainOrConfig,
664
- initialState: function() {
665
- return {};
666
- },
667
- action: function() {
668
- return {};
669
- },
670
- iterateConfig: iterateConfig
671
- };
672
- this.blocks.push(nestedBlock);
673
- return this.nextBrain();
674
- }
675
- // Case 1a: Single nested brain (existing behavior)
676
- var action = actionOrIterateConfig;
677
- var nestedBlock1 = {
678
- type: 'brain',
679
- title: title,
680
- innerBrain: innerBrainOrConfig,
681
- initialState: initialState || function() {
682
- return {};
683
- },
684
- action: function(outerState, innerState, services) {
685
- return action({
686
- state: outerState,
687
- brainState: innerState,
688
- services: services
689
- });
690
- }
691
- };
692
- this.blocks.push(nestedBlock1);
693
- return this.nextBrain();
694
- }
695
- // Case 2 & 3: Agent config (object or function)
696
- if (isIterateConfig) {
697
- // Agent config with iterate — configFn receives (item, params)
698
- var iterateConfig1 = actionOrIterateConfig;
699
- var configFn = innerBrainOrConfig;
700
- var agentBlock = {
701
- type: 'agent',
702
- title: title,
703
- configFn: configFn,
704
- iterateConfig: iterateConfig1
705
- };
706
- this.blocks.push(agentBlock);
707
- return this.nextBrain();
708
- }
709
- var configFn1 = typeof innerBrainOrConfig === 'function' ? innerBrainOrConfig : function() {
710
- return innerBrainOrConfig;
659
+ value: function brain(title, innerBrain, config) {
660
+ var nestedConfig = config !== null && config !== void 0 ? config : {};
661
+ var nestedBlock = {
662
+ type: 'brain',
663
+ title: title,
664
+ innerBrain: innerBrain,
665
+ initialState: nestedConfig.initialState,
666
+ options: nestedConfig.options
711
667
  };
712
- var agentBlock1 = {
713
- type: 'agent',
668
+ this.blocks.push(nestedBlock);
669
+ return this.nextBrain();
670
+ }
671
+ },
672
+ {
673
+ key: "prompt",
674
+ value: function prompt(title, configFn) {
675
+ var promptBlock = {
676
+ type: 'prompt',
714
677
  title: title,
715
- configFn: configFn1
678
+ configFn: configFn
716
679
  };
717
- this.blocks.push(agentBlock1);
680
+ this.blocks.push(promptBlock);
718
681
  return this.nextBrain();
719
682
  }
720
683
  },
721
684
  {
722
685
  // Implementation
723
- key: "prompt",
724
- value: function prompt(title, config, iterateConfig) {
725
- // Schema-less prompt - returns text response for next step
726
- if (!config.outputSchema) {
727
- var textSchema = z.object({
728
- text: z.string()
729
- });
730
- var promptBlock = {
731
- type: 'step',
732
- title: title,
733
- client: config.client,
734
- action: function(param) {
735
- var state = param.state, options = param.options, client = param.client, resources = param.resources;
736
- return _async_to_generator(function() {
737
- var prompt, result;
738
- return _ts_generator(this, function(_state) {
739
- switch(_state.label){
740
- case 0:
741
- return [
742
- 4,
743
- config.template({
744
- state: state,
745
- options: options,
746
- resources: resources
747
- })
748
- ];
749
- case 1:
750
- prompt = _state.sent();
751
- return [
752
- 4,
753
- client.generateObject({
754
- schema: textSchema,
755
- schemaName: 'TextResponse',
756
- prompt: prompt
757
- })
758
- ];
759
- case 2:
760
- result = _state.sent();
761
- return [
762
- 2,
763
- {
764
- state: state,
765
- promptResponse: result.object
766
- }
767
- ];
768
- }
769
- });
770
- })();
771
- }
772
- };
773
- this.blocks.push(promptBlock);
774
- return this.nextBrain();
775
- }
776
- // At this point, outputSchema is guaranteed to exist (schema-less case returned early)
777
- var outputSchema = config.outputSchema;
778
- if (iterateConfig) {
779
- // Iterate mode - store config on block for event-stream to execute with per-item events
780
- var promptBlock1 = {
781
- type: 'step',
782
- title: title,
783
- action: function(param) {
784
- var state = param.state;
785
- return _async_to_generator(function() {
786
- return _ts_generator(this, function(_state) {
787
- return [
788
- 2,
789
- state
790
- ];
791
- });
792
- })();
793
- },
794
- iterateConfig: {
795
- over: iterateConfig.over,
796
- error: iterateConfig.error,
797
- template: config.template,
798
- schema: outputSchema.schema,
799
- schemaName: outputSchema.name,
800
- client: config.client
801
- }
802
- };
803
- this.blocks.push(promptBlock1);
804
- return this.nextBrain();
805
- } else {
806
- // Single mode - run prompt once with current state
807
- var promptBlock2 = {
808
- type: 'step',
809
- title: title,
810
- client: config.client,
811
- action: function(param) {
812
- var state = param.state, options = param.options, client = param.client, resources = param.resources;
813
- return _async_to_generator(function() {
814
- var schema, schemaName, prompt, result;
815
- return _ts_generator(this, function(_state) {
816
- switch(_state.label){
817
- case 0:
818
- schema = outputSchema.schema, schemaName = outputSchema.name;
819
- return [
820
- 4,
821
- config.template({
822
- state: state,
823
- options: options,
824
- resources: resources
825
- })
826
- ];
827
- case 1:
828
- prompt = _state.sent();
829
- return [
830
- 4,
831
- client.generateObject({
832
- schema: schema,
833
- schemaName: schemaName,
834
- prompt: prompt
835
- })
836
- ];
837
- case 2:
838
- result = _state.sent();
839
- return [
840
- 2,
841
- _object_spread_props(_object_spread({}, state), _define_property({}, outputSchema.name, result.object))
842
- ];
843
- }
844
- });
845
- })();
846
- }
847
- };
848
- this.blocks.push(promptBlock2);
849
- return this.nextBrain();
850
- }
686
+ key: "map",
687
+ value: function map(title, stateKey, configFn) {
688
+ var mapBlock = {
689
+ type: 'map',
690
+ title: title,
691
+ stateKey: stateKey,
692
+ configFn: configFn
693
+ };
694
+ this.blocks.push(mapBlock);
695
+ return this.nextBrain();
851
696
  }
852
697
  },
853
698
  {
854
- /**
855
- * Add a UI generation step that creates an interactive page.
856
- *
857
- * The step:
858
- * 1. Calls an LLM agent to generate UI components based on the prompt
859
- * 2. Renders the components to an HTML page
860
- * 3. Stores the page and makes it available via URL
861
- * 4. Creates a webhook for form submissions (typed based on responseSchema)
862
- *
863
- * The next step receives a `page` object with:
864
- * - `url`: URL to the generated page
865
- * - `webhook`: Pre-configured WebhookRegistration for form submissions
866
- *
867
- * The brain author is responsible for notifying users about the page (via Slack,
868
- * email, etc.) and using `.wait()` to pause until the form is submitted.
869
- * Form data arrives in the `response` parameter of the step after `.wait()`.
870
- *
871
- * @example
872
- * ```typescript
873
- * brain('feedback-form')
874
- * .step('Initialize', () => ({ userName: 'John' }))
875
- * .ui('Create Form', {
876
- * template: ({ state }) => `Create a feedback form for ${state.userName}`,
877
- * responseSchema: z.object({
878
- * rating: z.number().min(1).max(5),
879
- * comments: z.string(),
880
- * }),
881
- * })
882
- * .step('Notify', async ({ state, page, slack }) => {
883
- * await slack.post('#general', `Please fill out: ${page.url}`);
884
- * return state;
885
- * })
886
- * .wait('Wait for submission', ({ page }) => page.webhook)
887
- * .step('Process Feedback', ({ state, response }) => ({
888
- * ...state,
889
- * // response is typed: { rating: number, comments: string }
890
- * rating: response.rating,
891
- * comments: response.comments,
892
- * }))
893
- * ```
894
- */ key: "ui",
895
- value: function ui(title, config) {
896
- var uiBlock = {
699
+ // Implementation
700
+ key: "page",
701
+ value: function page(title, configFn) {
702
+ var pageBlock = {
897
703
  type: 'step',
898
704
  title: title,
899
- isUIStep: true,
900
- uiConfig: {
901
- template: config.template,
902
- responseSchema: config.responseSchema
903
- },
904
- action: function(params) {
705
+ isPageStep: true,
706
+ pageConfigFn: configFn,
707
+ action: function() {
905
708
  return _async_to_generator(function() {
906
709
  return _ts_generator(this, function(_state) {
907
- // The actual UI generation is handled by BrainRunner/BrainEventStream
908
- // This action is a placeholder that gets replaced during execution
909
- // when the runner detects `isUIStep: true` and has components configured
910
- throw new Error('UI step "'.concat(title, '" requires components to be configured via BrainRunner.withComponents(). ') + "The UI generation is handled by the runner, not the step action directly.");
710
+ throw new Error('Page step "'.concat(title, '" requires components to be configured via brain.withComponents(). ') + "Page generation is handled by the runner, not the step action directly.");
911
711
  });
912
712
  })();
913
713
  }
914
714
  };
915
- this.blocks.push(uiBlock);
715
+ this.blocks.push(pageBlock);
916
716
  return this.nextBrain();
917
717
  }
918
718
  },
@@ -921,17 +721,25 @@ export var Brain = /*#__PURE__*/ function() {
921
721
  value: // Implementation signature
922
722
  function run(params) {
923
723
  return _wrap_async_generator(function() {
924
- var _this, title, description, blocks, store, stream;
724
+ var _this, title, description, blocks, _params_brainRunId, brainRunId, files, pages, store, _params_pluginConfigs, _resolvePlugins, pluginInjections, pluginAdapters, mergedPluginConfigs, stream;
925
725
  return _ts_generator(this, function(_state) {
926
726
  switch(_state.label){
927
727
  case 0:
928
728
  _this = this, title = _this.title, description = _this.description, blocks = _this.blocks;
929
- // Build store if withStore() was called and a store provider is given
729
+ brainRunId = 'resume' in params && params.resume ? params.brainRunId : (_params_brainRunId = params.brainRunId) !== null && _params_brainRunId !== void 0 ? _params_brainRunId : '';
730
+ // Platform services
731
+ files = params.files;
732
+ pages = params.pages;
930
733
  store = this.storeSchema && params.storeProvider ? params.storeProvider({
931
- schema: this.storeSchema,
932
- brainTitle: this.title,
933
- currentUser: params.currentUser
734
+ brainTitle: title,
735
+ currentUser: params.currentUser,
736
+ schema: this.storeSchema
934
737
  }) : undefined;
738
+ _resolvePlugins = resolvePlugins((_params_pluginConfigs = params.pluginConfigs) !== null && _params_pluginConfigs !== void 0 ? _params_pluginConfigs : [], this.pluginConfigs, {
739
+ brainTitle: title,
740
+ currentUser: params.currentUser,
741
+ brainRunId: brainRunId
742
+ }), pluginInjections = _resolvePlugins.injections, pluginAdapters = _resolvePlugins.adapters, mergedPluginConfigs = _resolvePlugins.configs;
935
743
  stream = new BrainEventStream(_object_spread_props(_object_spread({
936
744
  title: title,
937
745
  description: description,
@@ -939,12 +747,13 @@ export var Brain = /*#__PURE__*/ function() {
939
747
  }, params), {
940
748
  options: params.options || {},
941
749
  optionsSchema: this.optionsSchema,
942
- services: this.services,
943
750
  components: this.components,
944
- defaultTools: this.defaultTools,
945
- extraTools: this.extraTools,
946
- memoryProvider: this.memoryProvider,
947
- store: store
751
+ files: files,
752
+ pages: pages,
753
+ store: store,
754
+ pluginInjections: pluginInjections,
755
+ pluginAdapters: pluginAdapters,
756
+ pluginConfigs: mergedPluginConfigs
948
757
  }));
949
758
  return [
950
759
  5,
@@ -967,27 +776,36 @@ export var Brain = /*#__PURE__*/ function() {
967
776
  return this;
968
777
  }
969
778
  },
779
+ {
780
+ key: "copyConfigTo",
781
+ value: function copyConfigTo(target) {
782
+ target.optionsSchema = this.optionsSchema;
783
+ target.components = this.components;
784
+ target.storeSchema = this.storeSchema;
785
+ target.pluginConfigs = this.pluginConfigs;
786
+ }
787
+ },
970
788
  {
971
789
  key: "nextBrain",
972
790
  value: function nextBrain() {
973
- // Pass default options to the next brain
974
791
  var nextBrain = new Brain(this.title, this.description).withBlocks(this.blocks);
975
- // Copy services to the next brain
976
- nextBrain.services = this.services;
977
- // Copy optionsSchema to the next brain
978
- nextBrain.optionsSchema = this.optionsSchema;
979
- // Copy components to the next brain
980
- nextBrain.components = this.components;
981
- // Copy defaultTools to the next brain
982
- nextBrain.defaultTools = this.defaultTools;
983
- // Copy extraTools to the next brain
984
- nextBrain.extraTools = this.extraTools;
985
- // Copy memoryProvider to the next brain
986
- nextBrain.memoryProvider = this.memoryProvider;
987
- // Copy store schema to the next brain
988
- nextBrain.storeSchema = this.storeSchema;
792
+ this.copyConfigTo(nextBrain);
989
793
  return nextBrain;
990
794
  }
795
+ },
796
+ {
797
+ key: "continuationCallbacks",
798
+ value: function continuationCallbacks() {
799
+ var blocks = this.blocks;
800
+ var self = this;
801
+ return new Continuation(function(block) {
802
+ return blocks.push(block);
803
+ }, function() {
804
+ var next = new Brain(self.title, self.description).withBlocks(blocks);
805
+ self.copyConfigTo(next);
806
+ return next;
807
+ });
808
+ }
991
809
  }
992
810
  ]);
993
811
  return Brain;
@@ -1005,14 +823,34 @@ var brainNames = new Set();
1005
823
  }
1006
824
  }
1007
825
  // Implementation
1008
- export function brain(titleOrConfig, agentConfig) {
1009
- var title = typeof titleOrConfig === 'string' ? titleOrConfig : titleOrConfig.title;
1010
- var description = typeof titleOrConfig === 'string' ? undefined : titleOrConfig.description;
826
+ export function brain(titleOrConfig) {
827
+ var isString = typeof titleOrConfig === 'string';
828
+ var title = isString ? titleOrConfig : titleOrConfig.title;
829
+ var description = isString ? undefined : titleOrConfig.description;
830
+ var plugins = isString ? undefined : titleOrConfig.plugins;
1011
831
  registerBrainName(title);
1012
- var newBrain = new Brain(title, description);
1013
- // If agentConfig is provided, create a brain with a single 'main' agent step
1014
- if (agentConfig !== undefined) {
1015
- return newBrain.brain('main', agentConfig);
832
+ var b = new Brain(title, description);
833
+ if (plugins) {
834
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
835
+ try {
836
+ for(var _iterator = Object.values(plugins)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
837
+ var plugin = _step.value;
838
+ b = b.withPlugin(plugin);
839
+ }
840
+ } catch (err) {
841
+ _didIteratorError = true;
842
+ _iteratorError = err;
843
+ } finally{
844
+ try {
845
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
846
+ _iterator.return();
847
+ }
848
+ } finally{
849
+ if (_didIteratorError) {
850
+ throw _iteratorError;
851
+ }
852
+ }
853
+ }
1016
854
  }
1017
- return newBrain;
855
+ return b;
1018
856
  }