@positronic/core 0.0.56 → 0.0.57

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 (60) hide show
  1. package/dist/src/dsl/agent-messages.js +4 -75
  2. package/dist/src/dsl/brain-runner.js +131 -47
  3. package/dist/src/dsl/brain-state-machine.js +318 -482
  4. package/dist/src/dsl/builder/brain.js +35 -1
  5. package/dist/src/dsl/constants.js +14 -2
  6. package/dist/src/dsl/create-brain.js +4 -1
  7. package/dist/src/dsl/execution/constants.js +2 -2
  8. package/dist/src/dsl/execution/event-stream.js +812 -270
  9. package/dist/src/dsl/signal-validation.js +157 -0
  10. package/dist/src/dsl/types.js +2 -2
  11. package/dist/src/index.js +5 -2
  12. package/dist/src/memory/scoped-memory.js +176 -0
  13. package/dist/src/memory/types.js +12 -0
  14. package/dist/src/tools/index.js +90 -37
  15. package/dist/src/ui/generate-ui.js +6 -3
  16. package/dist/src/yaml/data-validator.js +195 -0
  17. package/dist/types/clients/types.d.ts +39 -2
  18. package/dist/types/clients/types.d.ts.map +1 -1
  19. package/dist/types/dsl/agent-messages.d.ts +8 -14
  20. package/dist/types/dsl/agent-messages.d.ts.map +1 -1
  21. package/dist/types/dsl/brain-runner.d.ts +27 -7
  22. package/dist/types/dsl/brain-runner.d.ts.map +1 -1
  23. package/dist/types/dsl/brain-state-machine.d.ts +92 -23
  24. package/dist/types/dsl/brain-state-machine.d.ts.map +1 -1
  25. package/dist/types/dsl/brain.d.ts +2 -2
  26. package/dist/types/dsl/brain.d.ts.map +1 -1
  27. package/dist/types/dsl/builder/brain.d.ts +51 -3
  28. package/dist/types/dsl/builder/brain.d.ts.map +1 -1
  29. package/dist/types/dsl/constants.d.ts +8 -0
  30. package/dist/types/dsl/constants.d.ts.map +1 -1
  31. package/dist/types/dsl/create-brain.d.ts +13 -1
  32. package/dist/types/dsl/create-brain.d.ts.map +1 -1
  33. package/dist/types/dsl/definitions/blocks.d.ts +3 -3
  34. package/dist/types/dsl/definitions/blocks.d.ts.map +1 -1
  35. package/dist/types/dsl/definitions/events.d.ts +40 -3
  36. package/dist/types/dsl/definitions/events.d.ts.map +1 -1
  37. package/dist/types/dsl/definitions/run-params.d.ts +17 -9
  38. package/dist/types/dsl/definitions/run-params.d.ts.map +1 -1
  39. package/dist/types/dsl/execution/constants.d.ts +2 -2
  40. package/dist/types/dsl/execution/constants.d.ts.map +1 -1
  41. package/dist/types/dsl/execution/event-stream.d.ts +12 -5
  42. package/dist/types/dsl/execution/event-stream.d.ts.map +1 -1
  43. package/dist/types/dsl/signal-validation.d.ts +36 -0
  44. package/dist/types/dsl/signal-validation.d.ts.map +1 -0
  45. package/dist/types/dsl/types.d.ts +57 -1
  46. package/dist/types/dsl/types.d.ts.map +1 -1
  47. package/dist/types/index.d.ts +12 -7
  48. package/dist/types/index.d.ts.map +1 -1
  49. package/dist/types/memory/scoped-memory.d.ts +22 -0
  50. package/dist/types/memory/scoped-memory.d.ts.map +1 -0
  51. package/dist/types/memory/types.d.ts +106 -0
  52. package/dist/types/memory/types.d.ts.map +1 -0
  53. package/dist/types/tools/index.d.ts +82 -15
  54. package/dist/types/tools/index.d.ts.map +1 -1
  55. package/dist/types/ui/generate-ui.d.ts.map +1 -1
  56. package/dist/types/yaml/data-validator.d.ts +27 -1
  57. package/dist/types/yaml/data-validator.d.ts.map +1 -1
  58. package/dist/types/yaml/types.d.ts +10 -0
  59. package/dist/types/yaml/types.d.ts.map +1 -1
  60. package/package.json +1 -1
@@ -1,76 +1,5 @@
1
- import { BRAIN_EVENTS } from './constants.js';
2
1
  /**
3
- * Reconstructs the agent context from stored events and a webhook response.
4
- * Returns null if this is not an agent resume (no AGENT_WEBHOOK event found).
5
- */ export function reconstructAgentContext(events, webhookResponse) {
6
- // Find AGENT_WEBHOOK event - if not present, this is not an agent resume
7
- var agentWebhookEvent = events.find(function(e) {
8
- return e.type === BRAIN_EVENTS.AGENT_WEBHOOK;
9
- });
10
- if (!agentWebhookEvent) {
11
- return null;
12
- }
13
- // Find AGENT_START to get the initial prompt and system
14
- var agentStartEvent = events.find(function(e) {
15
- return e.type === BRAIN_EVENTS.AGENT_START;
16
- });
17
- if (!agentStartEvent) {
18
- throw new Error('AGENT_START event not found but AGENT_WEBHOOK exists - invalid event sequence');
19
- }
20
- var messages = [];
21
- // Add initial user message from the prompt
22
- messages.push({
23
- role: 'user',
24
- content: agentStartEvent.prompt
25
- });
26
- var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
27
- try {
28
- // Process events in order to rebuild conversation
29
- for(var _iterator = events[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
30
- var event = _step.value;
31
- if (event.type === BRAIN_EVENTS.AGENT_ASSISTANT_MESSAGE) {
32
- var assistantEvent = event;
33
- messages.push({
34
- role: 'assistant',
35
- content: assistantEvent.content
36
- });
37
- } else if (event.type === BRAIN_EVENTS.AGENT_TOOL_RESULT) {
38
- var toolResultEvent = event;
39
- messages.push({
40
- role: 'tool',
41
- content: JSON.stringify(toolResultEvent.result),
42
- toolCallId: toolResultEvent.toolCallId,
43
- toolName: toolResultEvent.toolName
44
- });
45
- }
46
- }
47
- } catch (err) {
48
- _didIteratorError = true;
49
- _iteratorError = err;
50
- } finally{
51
- try {
52
- if (!_iteratorNormalCompletion && _iterator.return != null) {
53
- _iterator.return();
54
- }
55
- } finally{
56
- if (_didIteratorError) {
57
- throw _iteratorError;
58
- }
59
- }
60
- }
61
- // Add the webhook response as the pending tool's result
62
- messages.push({
63
- role: 'tool',
64
- content: JSON.stringify(webhookResponse),
65
- toolCallId: agentWebhookEvent.toolCallId,
66
- toolName: agentWebhookEvent.toolName
67
- });
68
- return {
69
- messages: messages,
70
- pendingToolCallId: agentWebhookEvent.toolCallId,
71
- pendingToolName: agentWebhookEvent.toolName,
72
- prompt: agentStartEvent.prompt,
73
- system: agentStartEvent.system,
74
- webhookResponse: webhookResponse
75
- };
76
- }
2
+ * Context for resuming an agent execution.
3
+ * Extends the state machine's AgentContext with an optional webhook response.
4
+ * The webhook response is only present when resuming from a webhook (vs a pause).
5
+ */ export { };
@@ -273,11 +273,52 @@ 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 } from './constants.js';
277
- import { applyPatches } from './json-patch.js';
278
- import { reconstructAgentContext } from './agent-messages.js';
276
+ import { BRAIN_EVENTS, STATUS } from './constants.js';
279
277
  import { createBrainExecutionMachine, sendEvent } from './brain-state-machine.js';
280
278
  import { DEFAULT_ENV } from './brain.js';
279
+ /**
280
+ * Convert execution stack to ResumeContext tree.
281
+ * Adds agent context to the deepest level.
282
+ * Webhook response is no longer passed here - it comes from signals during execution.
283
+ * This is internal to BrainRunner - external consumers don't need to know about ResumeContext.
284
+ */ function executionStackToResumeContext(stack, agentContext) {
285
+ if (stack.length === 0) {
286
+ throw new Error('Cannot convert empty execution stack to ResumeContext');
287
+ }
288
+ // Build from bottom of stack up (deepest to root)
289
+ var context;
290
+ for(var i = stack.length - 1; i >= 0; i--){
291
+ var entry = stack[i];
292
+ if (i === stack.length - 1) {
293
+ // Deepest level - add agent context (webhook response comes from signals now)
294
+ context = {
295
+ stepIndex: entry.stepIndex,
296
+ state: entry.state,
297
+ agentContext: agentContext !== null && agentContext !== void 0 ? agentContext : undefined
298
+ };
299
+ } else {
300
+ // Outer level - wrap inner context
301
+ context = {
302
+ stepIndex: entry.stepIndex,
303
+ state: entry.state,
304
+ innerResumeContext: context
305
+ };
306
+ }
307
+ }
308
+ return context;
309
+ }
310
+ /**
311
+ * Create a CANCELLED event for when the brain is aborted via signal.
312
+ * This is synthesized by the runner, not yielded from the brain's event stream.
313
+ */ function createCancelledEvent(brainRunId, options) {
314
+ return {
315
+ type: BRAIN_EVENTS.CANCELLED,
316
+ brainRunId: brainRunId,
317
+ brainTitle: '',
318
+ status: STATUS.CANCELLED,
319
+ options: options
320
+ };
321
+ }
281
322
  export var BrainRunner = /*#__PURE__*/ function() {
282
323
  "use strict";
283
324
  function BrainRunner(options) {
@@ -327,56 +368,106 @@ export var BrainRunner = /*#__PURE__*/ function() {
327
368
  }));
328
369
  }
329
370
  },
371
+ {
372
+ key: "withSignalProvider",
373
+ value: function withSignalProvider(signalProvider) {
374
+ return new BrainRunner(_object_spread_props(_object_spread({}, this.options), {
375
+ signalProvider: signalProvider
376
+ }));
377
+ }
378
+ },
330
379
  {
331
380
  key: "run",
332
- value: function run(_0) {
333
- return _async_to_generator(function(brain) {
334
- var _ref, _ref_initialState, initialState, options, initialCompletedSteps, brainRunId, endAfter, signal, response, agentEvents, _this_options, adapters, client, resources, pages, env, resolvedEnv, machineInitialState, initialStepCount, machine, agentResumeContext, brainRun, _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _loop, _iterator, _step, _ret, err, error, cancelledEvent;
335
- var _arguments = arguments;
381
+ value: /**
382
+ * Run a brain from the beginning with fresh state.
383
+ */ function run(brain, options) {
384
+ return _async_to_generator(function() {
385
+ var _ref, _ref_initialState, initialState, brainOptions, brainRunId, endAfter, signal;
386
+ return _ts_generator(this, function(_state) {
387
+ _ref = options !== null && options !== void 0 ? options : {}, _ref_initialState = _ref.initialState, initialState = _ref_initialState === void 0 ? {} : _ref_initialState, brainOptions = _ref.options, brainRunId = _ref.brainRunId, endAfter = _ref.endAfter, signal = _ref.signal;
388
+ return [
389
+ 2,
390
+ this.execute(brain, {
391
+ initialState: initialState,
392
+ options: brainOptions,
393
+ brainRunId: brainRunId,
394
+ endAfter: endAfter,
395
+ signal: signal,
396
+ initialStepCount: 0
397
+ })
398
+ ];
399
+ });
400
+ }).call(this);
401
+ }
402
+ },
403
+ {
404
+ key: "resume",
405
+ value: /**
406
+ * Resume a brain from a previous execution point.
407
+ * The machine should have historical events already replayed to reconstruct execution state.
408
+ * The BrainRunner will derive the ResumeContext from the machine's execution stack.
409
+ * Webhook response data comes from signals, not as a parameter.
410
+ */ function resume(brain, options) {
411
+ return _async_to_generator(function() {
412
+ var machine, brainRunId, brainOptions, endAfter, signal, _machine_context, executionStack, agentContext, resumeContext;
413
+ return _ts_generator(this, function(_state) {
414
+ machine = options.machine, brainRunId = options.brainRunId, brainOptions = options.options, endAfter = options.endAfter, signal = options.signal;
415
+ // Build ResumeContext from machine's execution stack
416
+ // Webhook response comes from signals during execution, not from resume parameters
417
+ _machine_context = machine.context, executionStack = _machine_context.executionStack, agentContext = _machine_context.agentContext;
418
+ resumeContext = executionStackToResumeContext(executionStack, agentContext);
419
+ return [
420
+ 2,
421
+ this.execute(brain, {
422
+ resumeContext: resumeContext,
423
+ machine: machine,
424
+ options: brainOptions,
425
+ brainRunId: brainRunId,
426
+ endAfter: endAfter,
427
+ signal: signal,
428
+ initialStepCount: resumeContext.stepIndex
429
+ })
430
+ ];
431
+ });
432
+ }).call(this);
433
+ }
434
+ },
435
+ {
436
+ key: "execute",
437
+ value: /**
438
+ * Internal execution method shared by run() and resume().
439
+ */ function execute(brain, params) {
440
+ return _async_to_generator(function() {
441
+ var _this_options, adapters, client, resources, pages, env, signalProvider, resolvedEnv, initialState, resumeContext, providedMachine, options, brainRunId, endAfter, signal, initialStepCount, _resumeContext_state, _ref, machine, brainRun, _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _loop, _iterator, _step, _ret, err, error, _machine_context_brainRunId, cancelledEvent;
336
442
  return _ts_generator(this, function(_state) {
337
443
  switch(_state.label){
338
444
  case 0:
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, agentEvents = _ref.agentEvents;
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;
445
+ _this_options = this.options, adapters = _this_options.adapters, client = _this_options.client, resources = _this_options.resources, pages = _this_options.pages, env = _this_options.env, signalProvider = _this_options.signalProvider;
341
446
  resolvedEnv = env !== null && env !== void 0 ? env : DEFAULT_ENV;
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;
346
- initialCompletedSteps === null || initialCompletedSteps === void 0 ? void 0 : initialCompletedSteps.forEach(function(step) {
347
- if (step.patch) {
348
- machineInitialState = applyPatches(machineInitialState, [
349
- step.patch
350
- ]);
351
- initialStepCount++;
352
- }
447
+ initialState = params.initialState, resumeContext = params.resumeContext, providedMachine = params.machine, options = params.options, brainRunId = params.brainRunId, endAfter = params.endAfter, signal = params.signal, initialStepCount = params.initialStepCount;
448
+ // Use provided state machine if available (for resumes with historical events),
449
+ // otherwise create a new one
450
+ machine = providedMachine !== null && providedMachine !== void 0 ? providedMachine : createBrainExecutionMachine({
451
+ initialState: (_ref = (_resumeContext_state = resumeContext === null || resumeContext === void 0 ? void 0 : resumeContext.state) !== null && _resumeContext_state !== void 0 ? _resumeContext_state : initialState) !== null && _ref !== void 0 ? _ref : {}
353
452
  });
354
- // Create state machine with pre-populated state
355
- // The machine tracks: currentState, isTopLevel, topLevelStepCount, etc.
356
- machine = createBrainExecutionMachine({
357
- initialState: machineInitialState
358
- });
359
- // If agentEvents and response are provided, reconstruct agent context
360
- agentResumeContext = agentEvents && response ? reconstructAgentContext(agentEvents, response) : null;
361
- brainRun = brainRunId && initialCompletedSteps ? brain.run({
362
- initialState: initialState,
363
- initialCompletedSteps: initialCompletedSteps,
453
+ brainRun = resumeContext ? brain.run({
454
+ resumeContext: resumeContext,
364
455
  brainRunId: brainRunId,
365
456
  options: options,
366
457
  client: client,
367
458
  resources: resources !== null && resources !== void 0 ? resources : {},
368
459
  pages: pages,
369
460
  env: resolvedEnv,
370
- response: response,
371
- agentResumeContext: agentResumeContext
461
+ signalProvider: signalProvider
372
462
  }) : brain.run({
373
- initialState: initialState,
463
+ initialState: initialState !== null && initialState !== void 0 ? initialState : {},
374
464
  options: options,
375
465
  client: client,
376
466
  brainRunId: brainRunId,
377
467
  resources: resources !== null && resources !== void 0 ? resources : {},
378
468
  pages: pages,
379
- env: resolvedEnv
469
+ env: resolvedEnv,
470
+ signalProvider: signalProvider
380
471
  });
381
472
  _state.label = 1;
382
473
  case 1:
@@ -396,7 +487,7 @@ export var BrainRunner = /*#__PURE__*/ function() {
396
487
  14
397
488
  ]);
398
489
  _loop = function() {
399
- var _value, event, cancelledEvent, totalSteps;
490
+ var _value, event, _machine_context_brainRunId, cancelledEvent, totalSteps;
400
491
  return _ts_generator(this, function(_state) {
401
492
  switch(_state.label){
402
493
  case 0:
@@ -406,11 +497,7 @@ export var BrainRunner = /*#__PURE__*/ function() {
406
497
  3,
407
498
  2
408
499
  ];
409
- // Use state machine to create cancelled event
410
- sendEvent(machine, {
411
- type: BRAIN_EVENTS.CANCELLED
412
- });
413
- cancelledEvent = machine.context.currentEvent;
500
+ cancelledEvent = createCancelledEvent((_machine_context_brainRunId = machine.context.brainRunId) !== null && _machine_context_brainRunId !== void 0 ? _machine_context_brainRunId : '', options !== null && options !== void 0 ? options : {});
414
501
  return [
415
502
  4,
416
503
  Promise.all(adapters.map(function(adapter) {
@@ -453,8 +540,8 @@ export var BrainRunner = /*#__PURE__*/ function() {
453
540
  ];
454
541
  }
455
542
  }
456
- // Stop execution when machine enters paused state (webhook)
457
- if (machine.context.isPaused) {
543
+ // Stop execution when machine enters paused or waiting state
544
+ if (machine.context.isPaused || machine.context.isWaiting) {
458
545
  // Cast is safe: state started as TState and patches maintain the structure
459
546
  return [
460
547
  2,
@@ -556,10 +643,7 @@ export var BrainRunner = /*#__PURE__*/ function() {
556
643
  3,
557
644
  17
558
645
  ];
559
- sendEvent(machine, {
560
- type: BRAIN_EVENTS.CANCELLED
561
- });
562
- cancelledEvent = machine.context.currentEvent;
646
+ cancelledEvent = createCancelledEvent((_machine_context_brainRunId = machine.context.brainRunId) !== null && _machine_context_brainRunId !== void 0 ? _machine_context_brainRunId : '', options !== null && options !== void 0 ? options : {});
563
647
  return [
564
648
  4,
565
649
  Promise.all(adapters.map(function(adapter) {
@@ -583,7 +667,7 @@ export var BrainRunner = /*#__PURE__*/ function() {
583
667
  ];
584
668
  }
585
669
  });
586
- }).apply(this, arguments);
670
+ }).call(this);
587
671
  }
588
672
  }
589
673
  ]);