@positronic/core 0.0.62 → 0.0.64

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/src/dsl/brain-state-machine.js +1 -5
  2. package/dist/src/dsl/builder/brain.js +23 -8
  3. package/dist/src/dsl/constants.js +0 -1
  4. package/dist/src/dsl/example-webhook.js +8 -9
  5. package/dist/src/dsl/execution/constants.js +0 -3
  6. package/dist/src/dsl/execution/event-stream.js +249 -146
  7. package/dist/src/dsl/webhook.js +3 -2
  8. package/dist/src/index.js +5 -0
  9. package/dist/src/tools/index.js +11 -6
  10. package/dist/src/ui/generate-page-html.js +15 -3
  11. package/dist/src/ui/parse-form-data.js +102 -0
  12. package/dist/src/validate-webhook-token.js +17 -0
  13. package/dist/types/clients/types.d.ts +0 -5
  14. package/dist/types/clients/types.d.ts.map +1 -1
  15. package/dist/types/dsl/brain-state-machine.d.ts +0 -6
  16. package/dist/types/dsl/brain-state-machine.d.ts.map +1 -1
  17. package/dist/types/dsl/brain.d.ts +2 -2
  18. package/dist/types/dsl/brain.d.ts.map +1 -1
  19. package/dist/types/dsl/builder/brain.d.ts +9 -16
  20. package/dist/types/dsl/builder/brain.d.ts.map +1 -1
  21. package/dist/types/dsl/constants.d.ts +0 -1
  22. package/dist/types/dsl/constants.d.ts.map +1 -1
  23. package/dist/types/dsl/definitions/blocks.d.ts +19 -12
  24. package/dist/types/dsl/definitions/blocks.d.ts.map +1 -1
  25. package/dist/types/dsl/definitions/events.d.ts +1 -8
  26. package/dist/types/dsl/definitions/events.d.ts.map +1 -1
  27. package/dist/types/dsl/definitions/steps.d.ts +1 -1
  28. package/dist/types/dsl/definitions/steps.d.ts.map +1 -1
  29. package/dist/types/dsl/example-webhook.d.ts +1 -1
  30. package/dist/types/dsl/example-webhook.d.ts.map +1 -1
  31. package/dist/types/dsl/execution/constants.d.ts +0 -4
  32. package/dist/types/dsl/execution/constants.d.ts.map +1 -1
  33. package/dist/types/dsl/execution/event-stream.d.ts +3 -1
  34. package/dist/types/dsl/execution/event-stream.d.ts.map +1 -1
  35. package/dist/types/dsl/webhook.d.ts +4 -1
  36. package/dist/types/dsl/webhook.d.ts.map +1 -1
  37. package/dist/types/index.d.ts +4 -1
  38. package/dist/types/index.d.ts.map +1 -1
  39. package/dist/types/tools/index.d.ts +6 -0
  40. package/dist/types/tools/index.d.ts.map +1 -1
  41. package/dist/types/ui/generate-page-html.d.ts +13 -0
  42. package/dist/types/ui/generate-page-html.d.ts.map +1 -1
  43. package/dist/types/ui/parse-form-data.d.ts +12 -0
  44. package/dist/types/ui/parse-form-data.d.ts.map +1 -0
  45. package/dist/types/validate-webhook-token.d.ts +10 -0
  46. package/dist/types/validate-webhook-token.d.ts.map +1 -0
  47. package/package.json +1 -1
@@ -469,11 +469,50 @@ import { generateUI } from '../../ui/generate-ui.js';
469
469
  import { generatePageHtml } from '../../ui/generate-page-html.js';
470
470
  import { createScopedMemory } from '../../memory/scoped-memory.js';
471
471
  import { Step } from '../builder/step.js';
472
- import { DEFAULT_ENV, DEFAULT_AGENT_SYSTEM_PROMPT, MAX_RETRIES } from './constants.js';
472
+ import { DEFAULT_ENV, DEFAULT_AGENT_SYSTEM_PROMPT } from './constants.js';
473
473
  import { defaultDoneSchema } from '../../tools/index.js';
474
474
  var clone = function(value) {
475
475
  return structuredClone(value);
476
476
  };
477
+ function createSemaphore(limit) {
478
+ var running = 0;
479
+ var queue = [];
480
+ return {
481
+ acquire: function acquire() {
482
+ return _async_to_generator(function() {
483
+ return _ts_generator(this, function(_state) {
484
+ if (running < limit) {
485
+ running++;
486
+ return [
487
+ 2,
488
+ function() {
489
+ running--;
490
+ if (queue.length > 0) {
491
+ running++;
492
+ queue.shift()();
493
+ }
494
+ }
495
+ ];
496
+ }
497
+ return [
498
+ 2,
499
+ new Promise(function(resolve) {
500
+ queue.push(function() {
501
+ return resolve(function() {
502
+ running--;
503
+ if (queue.length > 0) {
504
+ running++;
505
+ queue.shift()();
506
+ }
507
+ });
508
+ });
509
+ })
510
+ ];
511
+ });
512
+ })();
513
+ }
514
+ };
515
+ }
477
516
  export var BrainEventStream = /*#__PURE__*/ function() {
478
517
  "use strict";
479
518
  function BrainEventStream(params) {
@@ -499,6 +538,7 @@ export var BrainEventStream = /*#__PURE__*/ function() {
499
538
  _define_property(this, "memoryProvider", void 0);
500
539
  _define_property(this, "scopedMemory", void 0);
501
540
  _define_property(this, "guards", new Map());
541
+ _define_property(this, "waits", new Map());
502
542
  _define_property(this, "stopped", false);
503
543
  var blocks = params.blocks, title = params.title, description = params.description, providedBrainRunId = params.brainRunId, _params_options = params.options, options = _params_options === void 0 ? {} : _params_options, client = params.client, services = params.services, _params_resources = params.resources, resources = _params_resources === void 0 ? {} : _params_resources, pages = params.pages, env = params.env, components = params.components, defaultTools = params.defaultTools, signalProvider = params.signalProvider, memoryProvider = params.memoryProvider;
504
544
  // Check if this is a resume run or fresh start
@@ -522,7 +562,7 @@ export var BrainEventStream = /*#__PURE__*/ function() {
522
562
  if (memoryProvider) {
523
563
  this.scopedMemory = createScopedMemory(memoryProvider, title);
524
564
  }
525
- // Initialize steps - track guard blocks by index
565
+ // Initialize steps - track guard and wait blocks by index
526
566
  this.steps = [];
527
567
  var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
528
568
  try {
@@ -531,6 +571,9 @@ export var BrainEventStream = /*#__PURE__*/ function() {
531
571
  if (block.type === 'guard') {
532
572
  var guardBlock = block;
533
573
  this.guards.set(this.steps.length, guardBlock);
574
+ } else if (block.type === 'wait') {
575
+ var waitBlock = block;
576
+ this.waits.set(this.steps.length, waitBlock);
534
577
  }
535
578
  this.steps.push(new Step(block));
536
579
  }
@@ -586,7 +629,7 @@ export var BrainEventStream = /*#__PURE__*/ function() {
586
629
  key: "next",
587
630
  value: function next() {
588
631
  return _wrap_async_generator(function() {
589
- var _this, steps, brainTitle, brainDescription, currentState, options, brainRunId, webhookResponse, signals, webhookSignal, deepest, signals1, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, signal, err, step, guard, err1, error, currentStep;
632
+ var _this, steps, brainTitle, brainDescription, currentState, options, brainRunId, webhookResponse, signals, webhookSignal, deepest, signals1, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, signal, err, step, guard, waitBlock, err1, error, currentStep;
590
633
  return _ts_generator(this, function(_state) {
591
634
  switch(_state.label){
592
635
  case 0:
@@ -595,9 +638,9 @@ export var BrainEventStream = /*#__PURE__*/ function() {
595
638
  case 1:
596
639
  _state.trys.push([
597
640
  1,
598
- 31,
641
+ 33,
599
642
  ,
600
- 34
643
+ 36
601
644
  ]);
602
645
  if (!!this.resumeContext) return [
603
646
  3,
@@ -717,7 +760,7 @@ export var BrainEventStream = /*#__PURE__*/ function() {
717
760
  case 11:
718
761
  if (!(this.currentStepIndex < steps.length)) return [
719
762
  3,
720
- 29
763
+ 31
721
764
  ];
722
765
  if (!this.signalProvider) return [
723
766
  3,
@@ -846,6 +889,24 @@ export var BrainEventStream = /*#__PURE__*/ function() {
846
889
  11
847
890
  ];
848
891
  case 24:
892
+ // Handle wait blocks
893
+ waitBlock = this.waits.get(this.currentStepIndex);
894
+ if (!waitBlock) return [
895
+ 3,
896
+ 26
897
+ ];
898
+ return [
899
+ 5,
900
+ _ts_values(_async_generator_delegate(_async_iterator(this.executeWait(step, waitBlock))))
901
+ ];
902
+ case 25:
903
+ _state.sent();
904
+ this.currentStepIndex++;
905
+ return [
906
+ 3,
907
+ 11
908
+ ];
909
+ case 26:
849
910
  // Step start event
850
911
  return [
851
912
  4,
@@ -859,7 +920,7 @@ export var BrainEventStream = /*#__PURE__*/ function() {
859
920
  brainRunId: brainRunId
860
921
  }
861
922
  ];
862
- case 25:
923
+ case 27:
863
924
  _state.sent();
864
925
  step.withStatus(STATUS.RUNNING);
865
926
  // Step Status Event to indicate that the step is running
@@ -877,7 +938,7 @@ export var BrainEventStream = /*#__PURE__*/ function() {
877
938
  brainRunId: brainRunId
878
939
  }
879
940
  ];
880
- case 26:
941
+ case 28:
881
942
  _state.sent();
882
943
  // Execute step and yield the STEP_COMPLETE event and
883
944
  // all events from inner brains if any
@@ -885,7 +946,7 @@ export var BrainEventStream = /*#__PURE__*/ function() {
885
946
  5,
886
947
  _ts_values(_async_generator_delegate(_async_iterator(this.executeStep(step))))
887
948
  ];
888
- case 27:
949
+ case 29:
889
950
  _state.sent();
890
951
  // Backend requested a stop (e.g. batch chunk pause for DO restart)
891
952
  if (this.stopped) {
@@ -908,14 +969,14 @@ export var BrainEventStream = /*#__PURE__*/ function() {
908
969
  brainRunId: brainRunId
909
970
  }
910
971
  ];
911
- case 28:
972
+ case 30:
912
973
  _state.sent();
913
974
  this.currentStepIndex++;
914
975
  return [
915
976
  3,
916
977
  11
917
978
  ];
918
- case 29:
979
+ case 31:
919
980
  return [
920
981
  4,
921
982
  {
@@ -927,13 +988,13 @@ export var BrainEventStream = /*#__PURE__*/ function() {
927
988
  options: options
928
989
  }
929
990
  ];
930
- case 30:
991
+ case 32:
931
992
  _state.sent();
932
993
  return [
933
994
  3,
934
- 34
995
+ 36
935
996
  ];
936
- case 31:
997
+ case 33:
937
998
  err1 = _state.sent();
938
999
  error = err1;
939
1000
  currentStep = steps[this.currentStepIndex];
@@ -954,7 +1015,7 @@ export var BrainEventStream = /*#__PURE__*/ function() {
954
1015
  options: options
955
1016
  }
956
1017
  ];
957
- case 32:
1018
+ case 34:
958
1019
  _state.sent();
959
1020
  // Step Status Event
960
1021
  return [
@@ -971,10 +1032,10 @@ export var BrainEventStream = /*#__PURE__*/ function() {
971
1032
  brainRunId: brainRunId
972
1033
  }
973
1034
  ];
974
- case 33:
1035
+ case 35:
975
1036
  _state.sent();
976
1037
  throw error;
977
- case 34:
1038
+ case 36:
978
1039
  return [
979
1040
  2
980
1041
  ];
@@ -987,7 +1048,7 @@ export var BrainEventStream = /*#__PURE__*/ function() {
987
1048
  key: "executeStep",
988
1049
  value: function executeStep(step) {
989
1050
  return _wrap_async_generator(function() {
990
- var block, stepBlock, _this_resumeContext, brainBlock, initialState, innerResumeContext, patches, innerBrainPaused, _this_options, _this_options1, innerRun, _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, event, err, _innerResumeContext_state, baseState, innerState, prevState, _, prevState1, stepBlock1, retries, result, _this_options2, actionPromise, error, _this_options3, serializedWaitFor;
1051
+ var block, stepBlock, _this_resumeContext, brainBlock, initialState, innerResumeContext, patches, innerBrainPaused, _this_options, _this_options1, innerRun, _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, event, err, _innerResumeContext_state, baseState, innerState, prevState, _, prevState1, stepBlock1, _this_options2, result;
991
1052
  return _ts_generator(this, function(_state) {
992
1053
  switch(_state.label){
993
1054
  case 0:
@@ -1181,7 +1242,7 @@ export var BrainEventStream = /*#__PURE__*/ function() {
1181
1242
  _state.sent();
1182
1243
  return [
1183
1244
  3,
1184
- 35
1245
+ 25
1185
1246
  ];
1186
1247
  case 20:
1187
1248
  if (!(block.type === 'agent')) return [
@@ -1196,93 +1257,30 @@ export var BrainEventStream = /*#__PURE__*/ function() {
1196
1257
  _state.sent();
1197
1258
  return [
1198
1259
  3,
1199
- 35
1260
+ 25
1200
1261
  ];
1201
1262
  case 22:
1202
1263
  // Get previous state before action
1203
1264
  prevState1 = this.currentState;
1204
1265
  stepBlock1 = block;
1205
- // Execute step with automatic retry on failure
1206
- retries = 0;
1207
- _state.label = 23;
1208
- case 23:
1209
- if (!true) return [
1210
- 3,
1211
- 31
1212
- ];
1213
- _state.label = 24;
1214
- case 24:
1215
- _state.trys.push([
1216
- 24,
1217
- 26,
1218
- ,
1219
- 30
1220
- ]);
1221
- actionPromise = Promise.resolve(stepBlock1.action(_object_spread({
1222
- state: this.currentState,
1223
- options: (_this_options2 = this.options) !== null && _this_options2 !== void 0 ? _this_options2 : {},
1224
- client: this.client,
1225
- resources: this.resources,
1226
- response: this.currentResponse,
1227
- page: this.currentPage,
1228
- pages: this.pages,
1229
- env: this.env,
1230
- memory: this.scopedMemory
1231
- }, this.services)));
1232
1266
  return [
1233
1267
  4,
1234
- _await_async_generator(actionPromise)
1268
+ _await_async_generator(Promise.resolve(stepBlock1.action(_object_spread({
1269
+ state: this.currentState,
1270
+ options: (_this_options2 = this.options) !== null && _this_options2 !== void 0 ? _this_options2 : {},
1271
+ client: this.client,
1272
+ resources: this.resources,
1273
+ response: this.currentResponse,
1274
+ page: this.currentPage,
1275
+ pages: this.pages,
1276
+ env: this.env,
1277
+ memory: this.scopedMemory
1278
+ }, this.services))))
1235
1279
  ];
1236
- case 25:
1280
+ case 23:
1237
1281
  result = _state.sent();
1238
- return [
1239
- 3,
1240
- 31
1241
- ]; // Success
1242
- case 26:
1243
- error = _state.sent();
1244
- if (!(retries < MAX_RETRIES)) return [
1245
- 3,
1246
- 28
1247
- ];
1248
- retries++;
1249
- return [
1250
- 4,
1251
- {
1252
- type: BRAIN_EVENTS.STEP_RETRY,
1253
- stepTitle: step.block.title,
1254
- stepId: step.id,
1255
- error: {
1256
- name: error.name,
1257
- message: error.message,
1258
- stack: error.stack
1259
- },
1260
- attempt: retries,
1261
- options: (_this_options3 = this.options) !== null && _this_options3 !== void 0 ? _this_options3 : {},
1262
- brainRunId: this.brainRunId
1263
- }
1264
- ];
1265
- case 27:
1266
- _state.sent();
1267
- return [
1268
- 3,
1269
- 29
1270
- ];
1271
- case 28:
1272
- throw error;
1273
- case 29:
1274
- return [
1275
- 3,
1276
- 30
1277
- ];
1278
- case 30:
1279
- return [
1280
- 3,
1281
- 23
1282
- ];
1283
- case 31:
1284
- // Extract state from result (handles waitFor and promptResponse cases)
1285
- if (result && (typeof result === "undefined" ? "undefined" : _type_of(result)) === 'object' && ('waitFor' in result || 'promptResponse' in result)) {
1282
+ // Extract state from result (handles promptResponse case)
1283
+ if (result && (typeof result === "undefined" ? "undefined" : _type_of(result)) === 'object' && 'promptResponse' in result) {
1286
1284
  this.currentState = result.state;
1287
1285
  } else {
1288
1286
  this.currentState = result;
@@ -1291,40 +1289,16 @@ export var BrainEventStream = /*#__PURE__*/ function() {
1291
1289
  5,
1292
1290
  _ts_values(_async_generator_delegate(_async_iterator(this.completeStep(step, prevState1))))
1293
1291
  ];
1294
- case 32:
1295
- _state.sent();
1296
- if (!(result && (typeof result === "undefined" ? "undefined" : _type_of(result)) === 'object' && 'waitFor' in result)) return [
1297
- 3,
1298
- 34
1299
- ];
1300
- // Serialize webhook registrations (remove Zod schemas for event serializability)
1301
- serializedWaitFor = result.waitFor.map(function(registration) {
1302
- return {
1303
- slug: registration.slug,
1304
- identifier: registration.identifier
1305
- };
1306
- });
1307
- return [
1308
- 4,
1309
- {
1310
- type: BRAIN_EVENTS.WEBHOOK,
1311
- waitFor: serializedWaitFor,
1312
- options: this.options,
1313
- brainRunId: this.brainRunId
1314
- }
1315
- ];
1316
- case 33:
1292
+ case 24:
1317
1293
  _state.sent();
1318
- _state.label = 34;
1319
- case 34:
1320
1294
  // Handle promptResponse - set currentResponse for next step
1321
1295
  if (result && (typeof result === "undefined" ? "undefined" : _type_of(result)) === 'object' && 'promptResponse' in result) {
1322
1296
  this.currentResponse = result.promptResponse;
1323
1297
  }
1324
1298
  // Reset currentPage after step consumes it (page is ephemeral)
1325
1299
  this.currentPage = undefined;
1326
- _state.label = 35;
1327
- case 35:
1300
+ _state.label = 25;
1301
+ case 25:
1328
1302
  return [
1329
1303
  2
1330
1304
  ];
@@ -1736,7 +1710,7 @@ export var BrainEventStream = /*#__PURE__*/ function() {
1736
1710
  var desc = comp.description.split('\n')[0]; // First line only
1737
1711
  return "- ".concat(compName, ": ").concat(desc);
1738
1712
  }).join('\n');
1739
- description = "Generate a web page for displaying rich content or collecting user input.\n\nSometimes you need more than simple notifications to communicate with users. This tool creates web pages that can display formatted content, dashboards, or forms to collect information.\n\nAVAILABLE COMPONENTS:\n".concat(componentList, "\n\nRETURNS: { url: string, webhook: { slug: string, identifier: string } | null }\n- url: The page URL\n- webhook: For forms (hasForm=true), contains slug and identifier that can be passed to waitForWebhook to pause execution until the user submits the form\n\nIMPORTANT: Users have no way to discover the page URL on their own. After generating a page, you must tell them the URL using whatever communication tools are available.");
1713
+ description = "Generate a web page for displaying rich content or collecting user input.\n\nSometimes you need more than simple notifications to communicate with users. This tool creates web pages that can display formatted content, dashboards, or forms to collect information.\n\nAVAILABLE COMPONENTS:\n".concat(componentList, "\n\nRETURNS: { url: string, webhook: { slug: string, identifier: string, token: string } | null }\n- url: The page URL\n- webhook: For forms (hasForm=true), contains slug, identifier, and token that must all be passed to waitForWebhook to pause execution until the user submits the form\n\nIMPORTANT: Users have no way to discover the page URL on their own. After generating a page, you must tell them the URL using whatever communication tools are available.");
1740
1714
  }
1741
1715
  toolsForClient[name1] = {
1742
1716
  description: description,
@@ -1997,7 +1971,8 @@ export var BrainEventStream = /*#__PURE__*/ function() {
1997
1971
  webhooks: webhooks.map(function(w) {
1998
1972
  return {
1999
1973
  slug: w.slug,
2000
- identifier: w.identifier
1974
+ identifier: w.identifier,
1975
+ token: w.token
2001
1976
  };
2002
1977
  })
2003
1978
  };
@@ -2169,7 +2144,7 @@ export var BrainEventStream = /*#__PURE__*/ function() {
2169
2144
  * Cloudflare backends can restart the DO to reclaim memory.
2170
2145
  */ function executeBatchPrompt(step) {
2171
2146
  return _wrap_async_generator(function() {
2172
- var _this, _this_resumeContext, block, batchConfig, prevState, _batchConfig_client, client, items, totalItems, _batchConfig_chunkSize, chunkSize, batchProgress, _batchProgress_processedCount, startIndex, results, chunkStart, signals, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, signal, _this_options, err, chunkEnd, chunk, chunkResults, i, _this_options1, finalResults;
2147
+ var _this, _this_resumeContext, block, batchConfig, prevState, _batchConfig_client, client, items, totalItems, _batchConfig_concurrency, concurrency, semaphore, batchProgress, _batchProgress_processedCount, startIndex, results, chunkStart, signals, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, signal, _this_options, err, chunkEnd, chunk, chunkResults, i, _this_options1, finalResults;
2173
2148
  return _ts_generator(this, function(_state) {
2174
2149
  switch(_state.label){
2175
2150
  case 0:
@@ -2180,7 +2155,8 @@ export var BrainEventStream = /*#__PURE__*/ function() {
2180
2155
  client = (_batchConfig_client = batchConfig.client) !== null && _batchConfig_client !== void 0 ? _batchConfig_client : this.client;
2181
2156
  items = batchConfig.over(this.currentState);
2182
2157
  totalItems = items.length;
2183
- chunkSize = (_batchConfig_chunkSize = batchConfig.chunkSize) !== null && _batchConfig_chunkSize !== void 0 ? _batchConfig_chunkSize : 10;
2158
+ concurrency = (_batchConfig_concurrency = batchConfig.concurrency) !== null && _batchConfig_concurrency !== void 0 ? _batchConfig_concurrency : 10;
2159
+ semaphore = createSemaphore(concurrency);
2184
2160
  // Resume support: pick up from where we left off
2185
2161
  batchProgress = (_this_resumeContext = this.resumeContext) === null || _this_resumeContext === void 0 ? void 0 : _this_resumeContext.batchProgress;
2186
2162
  startIndex = (_batchProgress_processedCount = batchProgress === null || batchProgress === void 0 ? void 0 : batchProgress.processedCount) !== null && _batchProgress_processedCount !== void 0 ? _batchProgress_processedCount : 0;
@@ -2287,39 +2263,45 @@ export var BrainEventStream = /*#__PURE__*/ function() {
2287
2263
  7
2288
2264
  ];
2289
2265
  case 11:
2290
- chunkEnd = Math.min(chunkStart + chunkSize, totalItems);
2266
+ chunkEnd = Math.min(chunkStart + concurrency, totalItems);
2291
2267
  chunk = items.slice(chunkStart, chunkEnd);
2292
2268
  return [
2293
2269
  4,
2294
2270
  _await_async_generator(Promise.all(chunk.map(function(item) {
2295
2271
  return _async_to_generator(function() {
2296
- var promptText, output, error, fallback;
2272
+ var release, promptText, output, error, fallback;
2297
2273
  return _ts_generator(this, function(_state) {
2298
2274
  switch(_state.label){
2299
2275
  case 0:
2276
+ return [
2277
+ 4,
2278
+ semaphore.acquire()
2279
+ ];
2280
+ case 1:
2281
+ release = _state.sent();
2282
+ _state.label = 2;
2283
+ case 2:
2300
2284
  _state.trys.push([
2301
- 0,
2302
- 3,
2303
- ,
2304
- 4
2285
+ 2,
2286
+ 5,
2287
+ 6,
2288
+ 7
2305
2289
  ]);
2306
2290
  return [
2307
2291
  4,
2308
2292
  batchConfig.template(item, this.resources)
2309
2293
  ];
2310
- case 1:
2294
+ case 3:
2311
2295
  promptText = _state.sent();
2312
2296
  return [
2313
2297
  4,
2314
- client.generateObject(_object_spread({
2298
+ client.generateObject({
2315
2299
  schema: batchConfig.schema,
2316
2300
  schemaName: batchConfig.schemaName,
2317
2301
  prompt: promptText
2318
- }, batchConfig.maxRetries !== undefined && {
2319
- maxRetries: batchConfig.maxRetries
2320
- }))
2302
+ })
2321
2303
  ];
2322
- case 2:
2304
+ case 4:
2323
2305
  output = _state.sent();
2324
2306
  return [
2325
2307
  2,
@@ -2328,7 +2310,7 @@ export var BrainEventStream = /*#__PURE__*/ function() {
2328
2310
  output
2329
2311
  ]
2330
2312
  ];
2331
- case 3:
2313
+ case 5:
2332
2314
  error = _state.sent();
2333
2315
  if (batchConfig.error) {
2334
2316
  fallback = batchConfig.error(item, error);
@@ -2341,7 +2323,12 @@ export var BrainEventStream = /*#__PURE__*/ function() {
2341
2323
  ];
2342
2324
  }
2343
2325
  throw error;
2344
- case 4:
2326
+ case 6:
2327
+ release();
2328
+ return [
2329
+ 7
2330
+ ];
2331
+ case 7:
2345
2332
  return [
2346
2333
  2
2347
2334
  ];
@@ -2376,7 +2363,7 @@ export var BrainEventStream = /*#__PURE__*/ function() {
2376
2363
  _state.sent();
2377
2364
  _state.label = 14;
2378
2365
  case 14:
2379
- chunkStart += chunkSize;
2366
+ chunkStart += concurrency;
2380
2367
  return [
2381
2368
  3,
2382
2369
  1
@@ -2408,7 +2395,7 @@ export var BrainEventStream = /*#__PURE__*/ function() {
2408
2395
  * Generates UI components, renders to HTML, creates page, and sets up webhook.
2409
2396
  */ function executeUIStep(step, stepBlock) {
2410
2397
  return _wrap_async_generator(function() {
2411
- var prevState, uiConfig, prompt, uiResult, placementCount, placementInfo, _uiResult_text, webhookIdentifier, formAction, html, page, _uiConfig_responseSchema, webhook;
2398
+ var prevState, uiConfig, prompt, uiResult, placementCount, placementInfo, _uiResult_text, webhookIdentifier, formToken, formAction, html, page, _uiConfig_responseSchema, webhook;
2412
2399
  return _ts_generator(this, function(_state) {
2413
2400
  switch(_state.label){
2414
2401
  case 0:
@@ -2455,6 +2442,8 @@ export var BrainEventStream = /*#__PURE__*/ function() {
2455
2442
  }
2456
2443
  // Create unique identifier for this form submission webhook
2457
2444
  webhookIdentifier = "".concat(this.brainRunId, "-").concat(step.id);
2445
+ // Generate CSRF token for form submission validation
2446
+ formToken = crypto.randomUUID();
2458
2447
  // Construct form action URL for the webhook
2459
2448
  formAction = "".concat(this.env.origin, "/webhooks/system/ui-form?identifier=").concat(encodeURIComponent(webhookIdentifier));
2460
2449
  // Generate HTML page
@@ -2463,7 +2452,8 @@ export var BrainEventStream = /*#__PURE__*/ function() {
2463
2452
  rootId: uiResult.rootId,
2464
2453
  data: this.currentState,
2465
2454
  title: stepBlock.title,
2466
- formAction: formAction
2455
+ formAction: formAction,
2456
+ formToken: formToken
2467
2457
  });
2468
2458
  return [
2469
2459
  4,
@@ -2476,7 +2466,8 @@ export var BrainEventStream = /*#__PURE__*/ function() {
2476
2466
  webhook = {
2477
2467
  slug: 'ui-form',
2478
2468
  identifier: webhookIdentifier,
2479
- schema: (_uiConfig_responseSchema = uiConfig.responseSchema) !== null && _uiConfig_responseSchema !== void 0 ? _uiConfig_responseSchema : z.record(z.unknown())
2469
+ schema: (_uiConfig_responseSchema = uiConfig.responseSchema) !== null && _uiConfig_responseSchema !== void 0 ? _uiConfig_responseSchema : z.record(z.unknown()),
2470
+ token: formToken
2480
2471
  };
2481
2472
  // Set currentPage for the next step to access
2482
2473
  this.currentPage = {
@@ -2499,6 +2490,118 @@ export var BrainEventStream = /*#__PURE__*/ function() {
2499
2490
  }).call(this);
2500
2491
  }
2501
2492
  },
2493
+ {
2494
+ key: "executeWait",
2495
+ value: function executeWait(step, waitBlock) {
2496
+ return _wrap_async_generator(function() {
2497
+ var _this, steps, options, brainRunId, _this_options, result, webhooks, serializedWaitFor;
2498
+ return _ts_generator(this, function(_state) {
2499
+ switch(_state.label){
2500
+ case 0:
2501
+ _this = this, steps = _this.steps, options = _this.options, brainRunId = _this.brainRunId;
2502
+ // Emit STEP_START for the wait block
2503
+ return [
2504
+ 4,
2505
+ {
2506
+ type: BRAIN_EVENTS.STEP_START,
2507
+ status: STATUS.RUNNING,
2508
+ stepTitle: step.block.title,
2509
+ stepId: step.id,
2510
+ stepIndex: this.currentStepIndex,
2511
+ options: options,
2512
+ brainRunId: brainRunId
2513
+ }
2514
+ ];
2515
+ case 1:
2516
+ _state.sent();
2517
+ step.withStatus(STATUS.RUNNING);
2518
+ return [
2519
+ 4,
2520
+ {
2521
+ type: BRAIN_EVENTS.STEP_STATUS,
2522
+ steps: steps.map(function(s) {
2523
+ var _s_serialized = s.serialized, patch = _s_serialized.patch, rest = _object_without_properties(_s_serialized, [
2524
+ "patch"
2525
+ ]);
2526
+ return rest;
2527
+ }),
2528
+ options: options,
2529
+ brainRunId: brainRunId
2530
+ }
2531
+ ];
2532
+ case 2:
2533
+ _state.sent();
2534
+ return [
2535
+ 4,
2536
+ _await_async_generator(waitBlock.action(_object_spread({
2537
+ state: this.currentState,
2538
+ options: (_this_options = this.options) !== null && _this_options !== void 0 ? _this_options : {},
2539
+ client: this.client,
2540
+ resources: this.resources,
2541
+ page: this.currentPage,
2542
+ pages: this.pages,
2543
+ env: this.env
2544
+ }, this.services)))
2545
+ ];
2546
+ case 3:
2547
+ result = _state.sent();
2548
+ // Complete step (state unchanged, generates empty patch)
2549
+ return [
2550
+ 5,
2551
+ _ts_values(_async_generator_delegate(_async_iterator(this.completeStep(step, this.currentState))))
2552
+ ];
2553
+ case 4:
2554
+ _state.sent();
2555
+ return [
2556
+ 4,
2557
+ {
2558
+ type: BRAIN_EVENTS.STEP_STATUS,
2559
+ steps: steps.map(function(s) {
2560
+ var _s_serialized = s.serialized, patch = _s_serialized.patch, rest = _object_without_properties(_s_serialized, [
2561
+ "patch"
2562
+ ]);
2563
+ return rest;
2564
+ }),
2565
+ options: options,
2566
+ brainRunId: brainRunId
2567
+ }
2568
+ ];
2569
+ case 5:
2570
+ _state.sent();
2571
+ // Normalize result to array (handle single webhook case)
2572
+ webhooks = Array.isArray(result) ? result : [
2573
+ result
2574
+ ];
2575
+ // Serialize webhooks (strip Zod schemas)
2576
+ serializedWaitFor = webhooks.map(function(registration) {
2577
+ return {
2578
+ slug: registration.slug,
2579
+ identifier: registration.identifier,
2580
+ token: registration.token
2581
+ };
2582
+ });
2583
+ // Emit WEBHOOK event
2584
+ return [
2585
+ 4,
2586
+ {
2587
+ type: BRAIN_EVENTS.WEBHOOK,
2588
+ waitFor: serializedWaitFor,
2589
+ options: this.options,
2590
+ brainRunId: this.brainRunId
2591
+ }
2592
+ ];
2593
+ case 6:
2594
+ _state.sent();
2595
+ // Reset currentPage after wait consumes it (page is ephemeral)
2596
+ this.currentPage = undefined;
2597
+ return [
2598
+ 2
2599
+ ];
2600
+ }
2601
+ });
2602
+ }).call(this);
2603
+ }
2604
+ },
2502
2605
  {
2503
2606
  key: "executeGuard",
2504
2607
  value: function executeGuard(step, guard) {