@positronic/core 0.0.45 → 0.0.47

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.
@@ -273,9 +273,10 @@ function _ts_values(o) {
273
273
  };
274
274
  throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
275
275
  }
276
- import { BRAIN_EVENTS, STATUS } from './constants.js';
276
+ import { BRAIN_EVENTS } from './constants.js';
277
277
  import { applyPatches } from './json-patch.js';
278
278
  import { reconstructLoopContext } from './loop-messages.js';
279
+ import { createBrainExecutionMachine, sendEvent, sendAction, BRAIN_ACTIONS } from './brain-state-machine.js';
279
280
  import { DEFAULT_ENV } from './brain.js';
280
281
  export var BrainRunner = /*#__PURE__*/ function() {
281
282
  "use strict";
@@ -330,7 +331,7 @@ export var BrainRunner = /*#__PURE__*/ function() {
330
331
  key: "run",
331
332
  value: function run(_0) {
332
333
  return _async_to_generator(function(brain) {
333
- var _ref, _ref_initialState, initialState, options, initialCompletedSteps, brainRunId, endAfter, signal, response, loopEvents, _this_options, adapters, client, resources, pages, env, resolvedEnv, currentState, stepNumber, loopResumeContext, brainRun, _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _loop, _iterator, _step, _ret, err, error, cancelledEvent;
334
+ var _ref, _ref_initialState, initialState, options, initialCompletedSteps, brainRunId, endAfter, signal, response, loopEvents, _this_options, adapters, client, resources, pages, env, resolvedEnv, machineInitialState, initialStepCount, machine, loopResumeContext, brainRun, _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _loop, _iterator, _step, _ret, err, error, cancelledEvent;
334
335
  var _arguments = arguments;
335
336
  return _ts_generator(this, function(_state) {
336
337
  switch(_state.label){
@@ -338,20 +339,23 @@ export var BrainRunner = /*#__PURE__*/ function() {
338
339
  _ref = _arguments.length > 1 && _arguments[1] !== void 0 ? _arguments[1] : {}, _ref_initialState = _ref.initialState, initialState = _ref_initialState === void 0 ? {} : _ref_initialState, options = _ref.options, initialCompletedSteps = _ref.initialCompletedSteps, brainRunId = _ref.brainRunId, endAfter = _ref.endAfter, signal = _ref.signal, response = _ref.response, loopEvents = _ref.loopEvents;
339
340
  _this_options = this.options, adapters = _this_options.adapters, client = _this_options.client, resources = _this_options.resources, pages = _this_options.pages, env = _this_options.env;
340
341
  resolvedEnv = env !== null && env !== void 0 ? env : DEFAULT_ENV;
341
- currentState = initialState !== null && initialState !== void 0 ? initialState : {};
342
- stepNumber = 1;
343
- // Apply any patches from completed steps
344
- // to the initial state so that the brain
345
- // starts with a state that reflects all of the completed steps.
346
- // Need to do this when a brain is restarted with completed steps.
342
+ // Apply any patches from completed steps to get the initial state
343
+ // for the state machine. The machine will then track all subsequent state changes.
344
+ machineInitialState = initialState !== null && initialState !== void 0 ? initialState : {};
345
+ initialStepCount = 0;
347
346
  initialCompletedSteps === null || initialCompletedSteps === void 0 ? void 0 : initialCompletedSteps.forEach(function(step) {
348
347
  if (step.patch) {
349
- currentState = applyPatches(currentState, [
348
+ machineInitialState = applyPatches(machineInitialState, [
350
349
  step.patch
351
350
  ]);
352
- stepNumber++;
351
+ initialStepCount++;
353
352
  }
354
353
  });
354
+ // Create state machine with pre-populated state
355
+ // The machine tracks: currentState, isTopLevel, topLevelStepCount, etc.
356
+ machine = createBrainExecutionMachine({
357
+ initialState: machineInitialState
358
+ });
355
359
  // If loopEvents and response are provided, reconstruct loop context
356
360
  loopResumeContext = loopEvents && response ? reconstructLoopContext(loopEvents, response) : null;
357
361
  brainRun = brainRunId && initialCompletedSteps ? brain.run({
@@ -392,7 +396,7 @@ export var BrainRunner = /*#__PURE__*/ function() {
392
396
  14
393
397
  ]);
394
398
  _loop = function() {
395
- var _value, event, cancelledEvent;
399
+ var _value, event, cancelledEvent, totalSteps;
396
400
  return _ts_generator(this, function(_state) {
397
401
  switch(_state.label){
398
402
  case 0:
@@ -402,15 +406,9 @@ export var BrainRunner = /*#__PURE__*/ function() {
402
406
  3,
403
407
  2
404
408
  ];
405
- // Emit a cancelled event
406
- cancelledEvent = {
407
- type: BRAIN_EVENTS.CANCELLED,
408
- status: STATUS.CANCELLED,
409
- brainTitle: brain.title,
410
- brainDescription: brain.structure.description,
411
- brainRunId: brainRunId || event.brainRunId,
412
- options: event.options
413
- };
409
+ // Use state machine to create cancelled event
410
+ sendAction(machine, BRAIN_ACTIONS.CANCEL_BRAIN, {});
411
+ cancelledEvent = machine.context.currentEvent;
414
412
  return [
415
413
  4,
416
414
  Promise.all(adapters.map(function(adapter) {
@@ -419,13 +417,16 @@ export var BrainRunner = /*#__PURE__*/ function() {
419
417
  ];
420
418
  case 1:
421
419
  _state.sent();
420
+ // Cast is safe: state started as TState and patches maintain the structure
422
421
  return [
423
422
  2,
424
423
  {
425
- v: currentState
424
+ v: machine.context.currentState
426
425
  }
427
426
  ];
428
427
  case 2:
428
+ // Feed event to state machine - this updates currentState, isTopLevel, stepCount, etc.
429
+ sendEvent(machine, event);
429
430
  // Dispatch event to all adapters
430
431
  return [
431
432
  4,
@@ -435,30 +436,28 @@ export var BrainRunner = /*#__PURE__*/ function() {
435
436
  ];
436
437
  case 3:
437
438
  _state.sent();
438
- // Update current state when steps complete
439
- if (event.type === BRAIN_EVENTS.STEP_COMPLETE) {
440
- if (event.patch) {
441
- currentState = applyPatches(currentState, [
442
- event.patch
443
- ]);
444
- }
445
- // Check if we should stop after this step
446
- if (endAfter && stepNumber >= endAfter) {
439
+ // Check if we should stop after this step (only for top-level steps)
440
+ // The machine's topLevelStepCount tracks steps since this run started,
441
+ // so we add initialStepCount to get the total step count.
442
+ if (event.type === BRAIN_EVENTS.STEP_COMPLETE && machine.context.isTopLevel) {
443
+ totalSteps = machine.context.topLevelStepCount + initialStepCount;
444
+ if (endAfter && totalSteps >= endAfter) {
445
+ // Cast is safe: state started as TState and patches maintain the structure
447
446
  return [
448
447
  2,
449
448
  {
450
- v: currentState
449
+ v: machine.context.currentState
451
450
  }
452
451
  ];
453
452
  }
454
- stepNumber++;
455
453
  }
456
- // Stop execution when webhook event is encountered
457
- if (event.type === BRAIN_EVENTS.WEBHOOK) {
454
+ // Stop execution when machine enters paused state (webhook)
455
+ if (machine.context.isPaused) {
456
+ // Cast is safe: state started as TState and patches maintain the structure
458
457
  return [
459
458
  2,
460
459
  {
461
- v: currentState
460
+ v: machine.context.currentState
462
461
  }
463
462
  ];
464
463
  }
@@ -555,14 +554,8 @@ export var BrainRunner = /*#__PURE__*/ function() {
555
554
  3,
556
555
  17
557
556
  ];
558
- cancelledEvent = {
559
- type: BRAIN_EVENTS.CANCELLED,
560
- status: STATUS.CANCELLED,
561
- brainTitle: brain.title,
562
- brainDescription: brain.structure.description,
563
- brainRunId: brainRunId || '',
564
- options: options || {}
565
- };
557
+ sendAction(machine, BRAIN_ACTIONS.CANCEL_BRAIN, {});
558
+ cancelledEvent = machine.context.currentEvent;
566
559
  return [
567
560
  4,
568
561
  Promise.all(adapters.map(function(adapter) {
@@ -571,16 +564,18 @@ export var BrainRunner = /*#__PURE__*/ function() {
571
564
  ];
572
565
  case 16:
573
566
  _state.sent();
567
+ // Cast is safe: state started as TState and patches maintain the structure
574
568
  return [
575
569
  2,
576
- currentState
570
+ machine.context.currentState
577
571
  ];
578
572
  case 17:
579
573
  throw error;
580
574
  case 18:
575
+ // Cast is safe: state started as TState and patches maintain the structure
581
576
  return [
582
577
  2,
583
- currentState
578
+ machine.context.currentState
584
579
  ];
585
580
  }
586
581
  });