@blue-labs/document-processor 1.22.0 → 1.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/dist/BlueDocumentProcessor.d.ts +2 -6
  2. package/dist/BlueDocumentProcessor.d.ts.map +1 -1
  3. package/dist/config.d.ts.map +1 -1
  4. package/dist/index.js +3 -3
  5. package/dist/index.mjs +525 -427
  6. package/dist/processors/BaseChannelProcessor.d.ts +0 -5
  7. package/dist/processors/BaseChannelProcessor.d.ts.map +1 -1
  8. package/dist/processors/ChannelEventCheckpointProcessor.d.ts +0 -1
  9. package/dist/processors/ChannelEventCheckpointProcessor.d.ts.map +1 -1
  10. package/dist/processors/InitializedMarkerProcessor.d.ts +9 -0
  11. package/dist/processors/InitializedMarkerProcessor.d.ts.map +1 -0
  12. package/dist/processors/LifecycleEventChannelProcessor.d.ts +33 -0
  13. package/dist/processors/LifecycleEventChannelProcessor.d.ts.map +1 -0
  14. package/dist/processors/OperationProcessor.d.ts +0 -1
  15. package/dist/processors/OperationProcessor.d.ts.map +1 -1
  16. package/dist/processors/ProcessEmbeddedProcessor.d.ts +0 -1
  17. package/dist/processors/ProcessEmbeddedProcessor.d.ts.map +1 -1
  18. package/dist/processors/SequentialWorkflowProcessor/SequentialWorkflowProcessor.d.ts +0 -1
  19. package/dist/processors/SequentialWorkflowProcessor/SequentialWorkflowProcessor.d.ts.map +1 -1
  20. package/dist/processors/SequentialWorkflowProcessor/steps/UpdateDocumentExecutor.d.ts +2 -1
  21. package/dist/processors/SequentialWorkflowProcessor/steps/UpdateDocumentExecutor.d.ts.map +1 -1
  22. package/dist/processors/TimelineChannelProcessor.d.ts.map +1 -1
  23. package/dist/processors/index.d.ts +2 -0
  24. package/dist/processors/index.d.ts.map +1 -1
  25. package/dist/routing/EventRouter.d.ts.map +1 -1
  26. package/dist/testUtils.d.ts +15 -0
  27. package/dist/testUtils.d.ts.map +1 -0
  28. package/dist/types.d.ts +1 -2
  29. package/dist/types.d.ts.map +1 -1
  30. package/dist/utils/checkpoint.d.ts.map +1 -1
  31. package/dist/utils/eventFactories.d.ts +37 -0
  32. package/dist/utils/eventFactories.d.ts.map +1 -0
  33. package/dist/utils/initialized.d.ts +5 -0
  34. package/dist/utils/initialized.d.ts.map +1 -0
  35. package/package.json +5 -5
package/dist/index.mjs CHANGED
@@ -1,31 +1,31 @@
1
- var ae = Object.defineProperty;
2
- var se = (c, e, t) => e in c ? ae(c, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : c[e] = t;
3
- var l = (c, e, t) => se(c, typeof e != "symbol" ? e + "" : e, t);
4
- import { ProcessEmbeddedSchema as I, ChannelEventCheckpointSchema as ce, blueIds as g, CompositeTimelineChannelSchema as ie, DocumentUpdateChannelSchema as le, EmbeddedNodeChannelSchema as A, OperationSchema as ue, TimelineEntrySchema as B, OperationRequestSchema as he, UpdateDocumentSchema as k, TriggerEventSchema as N, JavaScriptCodeSchema as O, SequentialWorkflowSchema as de, SequentialWorkflowOperationSchema as pe, TimelineChannelSchema as me } from "@blue-repository/core-dev";
5
- import { applyBlueNodePatch as fe, BlueNodeTypeSchema as E, BlueNode as ye, isBigNumber as V } from "@blue-labs/language";
6
- import { deepFreeze as L, isNonNullable as M, deepContains as ge } from "@blue-labs/shared-utils";
7
- import { blueIds as H, MyOSTimelineEntrySchema as Ee, MyOSTimelineChannelSchema as we, MyOSAgentEventSchema as ve, MyOSAgentChannelSchema as Te } from "@blue-repository/myos-dev";
8
- const be = (c, e) => ({
1
+ var le = Object.defineProperty;
2
+ var ue = (c, e, t) => e in c ? le(c, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : c[e] = t;
3
+ var l = (c, e, t) => ue(c, typeof e != "symbol" ? e + "" : e, t);
4
+ import { ProcessEmbeddedSchema as M, ChannelEventCheckpointSchema as he, blueIds as y, InitializedMarkerSchema as de, CompositeTimelineChannelSchema as pe, DocumentUpdateChannelSchema as me, EmbeddedNodeChannelSchema as x, OperationSchema as fe, TimelineEntrySchema as $, OperationRequestSchema as ye, UpdateDocumentSchema as N, TriggerEventSchema as B, JavaScriptCodeSchema as O, SequentialWorkflowSchema as ge, SequentialWorkflowOperationSchema as Ee, TimelineChannelSchema as we } from "@blue-repository/core-dev";
5
+ import { applyBlueNodePatch as Q, BlueNodeTypeSchema as T, BlueNode as ve, isBigNumber as K } from "@blue-labs/language";
6
+ import { deepFreeze as k, isNonNullable as I, deepContains as X } from "@blue-labs/shared-utils";
7
+ import { blueIds as Y, MyOSTimelineEntrySchema as Te, MyOSTimelineChannelSchema as be, MyOSAgentEventSchema as Pe, MyOSAgentChannelSchema as Se } from "@blue-repository/myos-dev";
8
+ const Ce = (c, e) => ({
9
9
  on: (t, n) => ({ end: () => n(null) })
10
10
  });
11
- async function Se(c) {
11
+ async function ke(c) {
12
12
  return new Promise((e, t) => {
13
- be().on("error", t).end();
13
+ Ce().on("error", t).end();
14
14
  });
15
15
  }
16
- const T = (...c) => c.map((e, t) => {
16
+ const v = (...c) => c.map((e, t) => {
17
17
  if (typeof e != "string") return "";
18
18
  if (t === 0 && e === "/") return "/";
19
19
  const n = t > 0 ? e.replace(/^\/+/, "") : e;
20
20
  return t < c.length - 1 ? n.replace(/\/+$/, "") : n;
21
21
  }).filter(Boolean).join("/").replace(/\/{2,}/g, "/");
22
- class Q {
22
+ class Z {
23
23
  constructor(e, t, n, o) {
24
24
  l(this, "actions", []);
25
25
  this.getDocument = e, this.taskInfo = t, this.blue = n, this.onFlush = o;
26
26
  }
27
27
  get(e) {
28
- const t = this.getDocument(), n = T(this.taskInfo.nodePath, e);
28
+ const t = this.getDocument(), n = v(this.taskInfo.nodePath, e);
29
29
  return t.get(n);
30
30
  }
31
31
  addPatch(e) {
@@ -33,7 +33,7 @@ class Q {
33
33
  kind: "patch",
34
34
  patch: {
35
35
  ...e,
36
- path: T(this.taskInfo.nodePath, e.path)
36
+ path: v(this.taskInfo.nodePath, e.path)
37
37
  }
38
38
  });
39
39
  }
@@ -56,7 +56,7 @@ class Q {
56
56
  return this.taskInfo.nodePath;
57
57
  }
58
58
  resolvePath(e) {
59
- return T(this.taskInfo.nodePath, e);
59
+ return v(this.taskInfo.nodePath, e);
60
60
  }
61
61
  getTaskInfo() {
62
62
  return this.taskInfo;
@@ -68,76 +68,76 @@ class Q {
68
68
  loadExternalModule(e) {
69
69
  if (!/^https?:\/\//.test(e))
70
70
  throw new Error("Only http/https URLs are allowed");
71
- return Se();
71
+ return ke();
72
72
  }
73
73
  loadBlueContent(e) {
74
74
  throw new Error("Not implemented");
75
75
  }
76
76
  }
77
- class K extends Error {
77
+ class G extends Error {
78
78
  constructor(e, t) {
79
79
  super(`Cannot apply patch ${JSON.stringify(e)}`), this.patch = e, this.cause = t, this.name = "PatchApplicationError";
80
80
  }
81
81
  }
82
- class z extends Error {
82
+ class ee extends Error {
83
83
  constructor(e, t, n) {
84
84
  super(
85
85
  `Patch ${JSON.stringify(e)} touches "${e.op === "move" || e.op === "copy" ? `${e.from} → ${e.path}` : e.path}" which is inside embedded document "${t}" (Process Embedded @ "${n}")`
86
86
  ), this.patch = e, this.offendingPath = t, this.contractNodePath = n, this.name = "EmbeddedDocumentModificationError";
87
87
  }
88
88
  }
89
- class x extends Error {
89
+ class W extends Error {
90
90
  constructor(e, t) {
91
91
  super(`Failed to evaluate expression "${e}"`), this.code = e, this.cause = t, this.name = "ExpressionEvaluationError";
92
92
  }
93
93
  }
94
- class D extends Error {
94
+ class V extends Error {
95
95
  constructor(e, t) {
96
96
  super(`Failed to evaluate code block "${e}"`), this.code = e, this.cause = t, this.name = "CodeBlockEvaluationError";
97
97
  }
98
98
  }
99
- function q(c, e, t = "/", n = []) {
99
+ function D(c, e, t = "/", n = []) {
100
100
  const o = c.getContracts() ?? {};
101
- for (const [r, a] of Object.entries(o))
102
- if (E.isTypeOf(
103
- a,
104
- I
101
+ for (const [a, r] of Object.entries(o))
102
+ if (T.isTypeOf(
103
+ r,
104
+ M
105
105
  )) {
106
106
  const u = e.nodeToSchemaOutput(
107
- a,
108
- I
107
+ r,
108
+ M
109
109
  ).paths ?? [];
110
110
  for (const h of u)
111
111
  n.push({
112
- absPath: T(t, h),
113
- contractPath: T(t, `contracts/${r}`)
112
+ absPath: v(t, h),
113
+ contractPath: v(t, `contracts/${a}`)
114
114
  });
115
115
  }
116
- for (const [r, a] of Object.entries(c.getProperties() ?? {}))
117
- q(a, e, T(t, r), n);
116
+ for (const [a, r] of Object.entries(c.getProperties() ?? {}))
117
+ D(r, e, v(t, a), n);
118
118
  return n;
119
119
  }
120
- function W(c, e) {
120
+ function F(c, e) {
121
121
  return c === e || c.startsWith(e.endsWith("/") ? e : e + "/");
122
122
  }
123
- function j(c, e) {
123
+ function J(c, e) {
124
124
  if (!e.length) return c;
125
125
  let t = c.clone();
126
126
  for (const n of e)
127
127
  try {
128
- t = fe(t, n, !0);
128
+ t = Q(t, n, !0);
129
129
  } catch (o) {
130
- throw new K(n, o);
130
+ throw new G(n, o);
131
131
  }
132
- return L(t);
132
+ return k(t);
133
133
  }
134
134
  function f(c) {
135
135
  return c != null;
136
136
  }
137
- function C(c) {
138
- return c instanceof ye;
137
+ function P(c) {
138
+ return c instanceof ve;
139
139
  }
140
- class Pe {
140
+ class Ie {
141
141
  constructor(e = [], t = (n, o) => n < o ? -1 : n > o ? 1 : 0) {
142
142
  l(this, "data");
143
143
  l(this, "length");
@@ -159,31 +159,31 @@ class Pe {
159
159
  _up(e) {
160
160
  const { data: t, compare: n } = this, o = t[e];
161
161
  for (; e > 0; ) {
162
- const r = e - 1 >> 1, a = t[r];
163
- if (n(o, a) >= 0) break;
164
- t[e] = a, e = r;
162
+ const a = e - 1 >> 1, r = t[a];
163
+ if (n(o, r) >= 0) break;
164
+ t[e] = r, e = a;
165
165
  }
166
166
  t[e] = o;
167
167
  }
168
168
  _down(e) {
169
- const { data: t, compare: n } = this, o = this.length >> 1, r = t[e];
169
+ const { data: t, compare: n } = this, o = this.length >> 1, a = t[e];
170
170
  for (; e < o; ) {
171
- let a = (e << 1) + 1;
172
- const s = a + 1;
173
- if (s < this.length && n(t[s], t[a]) < 0 && (a = s), n(t[a], r) >= 0) break;
174
- t[e] = t[a], e = a;
171
+ let r = (e << 1) + 1;
172
+ const s = r + 1;
173
+ if (s < this.length && n(t[s], t[r]) < 0 && (r = s), n(t[r], a) >= 0) break;
174
+ t[e] = t[r], e = r;
175
175
  }
176
- t[e] = r;
176
+ t[e] = a;
177
177
  }
178
178
  }
179
- const Ce = (c, e, t, n, o, r) => [
179
+ const Ne = (c, e, t, n, o, a) => [
180
180
  -c,
181
181
  e,
182
182
  t,
183
183
  n,
184
184
  o,
185
- r
186
- ], ke = (c, e) => {
185
+ a
186
+ ], Be = (c, e) => {
187
187
  for (let t = 0; t < c.key.length; t++) {
188
188
  const n = c.key[t], o = e.key[t];
189
189
  if (n !== o)
@@ -191,13 +191,13 @@ const Ce = (c, e, t, n, o, r) => [
191
191
  }
192
192
  return 0;
193
193
  };
194
- class Ne {
194
+ class Oe {
195
195
  /**
196
196
  * Creates a new task queue with the task key comparator
197
197
  */
198
198
  constructor() {
199
199
  l(this, "queue");
200
- this.queue = new Pe([], ke);
200
+ this.queue = new Ie([], Be);
201
201
  }
202
202
  /**
203
203
  * Adds a task to the queue
@@ -222,7 +222,7 @@ class Ne {
222
222
  return this.queue.length;
223
223
  }
224
224
  }
225
- class Oe {
225
+ class Me {
226
226
  /**
227
227
  * Creates a new contract registry
228
228
  *
@@ -277,7 +277,7 @@ class Oe {
277
277
  return this.processors.values();
278
278
  }
279
279
  }
280
- class Ie {
280
+ class $e {
281
281
  constructor() {
282
282
  l(this, "isEnabled");
283
283
  this.isEnabled = process.env.TRACE_BLUE_ENABLED === "true";
@@ -344,8 +344,8 @@ class Ie {
344
344
  return this.isEnabled;
345
345
  }
346
346
  }
347
- const Be = 64;
348
- class Me {
347
+ const qe = 64;
348
+ class Ae {
349
349
  /**
350
350
  * Creates a new event router
351
351
  *
@@ -354,9 +354,9 @@ class Me {
354
354
  * @param getNextTaskId - Function to get the next task ID
355
355
  * @param getNextEventSeq - Function to get the next event sequence number
356
356
  */
357
- constructor(e, t, n, o, r) {
357
+ constructor(e, t, n, o, a) {
358
358
  l(this, "traceManager");
359
- this.blue = e, this.registry = t, this.queue = n, this.getNextTaskId = o, this.getNextEventSeq = r, this.traceManager = new Ie();
359
+ this.blue = e, this.registry = t, this.queue = n, this.getNextTaskId = o, this.getNextEventSeq = a, this.traceManager = new $e();
360
360
  }
361
361
  /**
362
362
  * Routes an event to matching contracts in the document
@@ -367,27 +367,27 @@ class Me {
367
367
  * @param afterTaskId - Minimum task ID to use
368
368
  * @param inlineDepth - Current adapter recursion depth
369
369
  */
370
- async route(e, t, n, o, r = 0) {
370
+ async route(e, t, n, o, a = 0) {
371
371
  var i;
372
372
  if (n.seq === void 0 && (n.seq = this.getNextEventSeq()), t.length === 0) {
373
373
  if (n.dispatchPath) {
374
374
  const u = n.dispatchPath.split("/").filter(Boolean), h = { ...n };
375
- return delete h.dispatchPath, this.route(e, u, h, o, r);
375
+ return delete h.dispatchPath, this.route(e, u, h, o, a);
376
376
  }
377
377
  if (n.source === "channel" && n.originNodePath && n.originNodePath !== "/") {
378
378
  const u = ((i = n.originNodePath) == null ? void 0 : i.split("/").filter(Boolean)) ?? [];
379
- return this.route(e, u, n, o, r);
379
+ return this.route(e, u, n, o, a);
380
380
  }
381
381
  }
382
- const a = T("/", t.join("/")), s = e.get(a);
383
- C(s) && await this.traverseContracts({
382
+ const r = v("/", t.join("/")), s = e.get(r);
383
+ P(s) && await this.traverseContracts({
384
384
  doc: e,
385
385
  node: s,
386
- nodePath: a,
386
+ nodePath: r,
387
387
  event: n,
388
388
  afterTaskId: o,
389
389
  pathSegments: t,
390
- inlineDepth: r
390
+ inlineDepth: a
391
391
  });
392
392
  }
393
393
  /**
@@ -398,12 +398,12 @@ class Me {
398
398
  doc: t,
399
399
  node: n,
400
400
  nodePath: o,
401
- event: r,
402
- afterTaskId: a,
401
+ event: a,
402
+ afterTaskId: r,
403
403
  pathSegments: s,
404
404
  inlineDepth: i
405
405
  } = e;
406
- if (!this.shouldSkipForChannel(r, o))
406
+ if (!this.shouldSkipForChannel(a, o))
407
407
  for (const [u, h] of Object.entries(
408
408
  n.getContracts() ?? {}
409
409
  )) {
@@ -417,19 +417,19 @@ class Me {
417
417
  nodePath: o,
418
418
  contractName: u,
419
419
  contractNode: h,
420
- event: r
421
- }, m = new Q(() => t, p, this.blue);
422
- if (d.supports(r, h, m, u))
420
+ event: a
421
+ }, m = new Z(() => t, p, this.blue);
422
+ if (d.supports(a, h, m, u))
423
423
  switch (d.role) {
424
424
  case "adapter":
425
425
  await this.processAdapter({
426
426
  cp: d,
427
- event: r,
427
+ event: a,
428
428
  contractNode: h,
429
429
  ctx: m,
430
430
  contractName: u,
431
431
  doc: t,
432
- afterTaskId: a,
432
+ afterTaskId: r,
433
433
  inlineDepth: i
434
434
  });
435
435
  break;
@@ -438,9 +438,9 @@ class Me {
438
438
  contractNode: h,
439
439
  contractName: u,
440
440
  nodePath: o,
441
- event: r,
441
+ event: a,
442
442
  depth: s.length,
443
- afterTaskId: a
443
+ afterTaskId: r
444
444
  });
445
445
  break;
446
446
  }
@@ -450,29 +450,29 @@ class Me {
450
450
  * Processes an adapter contract and routes any emitted events
451
451
  */
452
452
  async processAdapter(e) {
453
- var w;
453
+ var E;
454
454
  const {
455
455
  cp: t,
456
456
  event: n,
457
457
  contractNode: o,
458
- ctx: r,
459
- contractName: a,
458
+ ctx: a,
459
+ contractName: r,
460
460
  doc: s,
461
461
  afterTaskId: i,
462
462
  inlineDepth: u
463
463
  } = e;
464
- if (u >= Be)
464
+ if (u >= qe)
465
465
  throw new Error("Adapter recursion limit reached");
466
466
  const h = this.traceManager.addHop(
467
467
  n,
468
- ((w = r.getTaskInfo()) == null ? void 0 : w.nodePath) ?? "",
469
- a
468
+ ((E = a.getTaskInfo()) == null ? void 0 : E.nodePath) ?? "",
469
+ r
470
470
  );
471
- await t.handle(h, o, r, a);
472
- const d = await r.flush();
471
+ await t.handle(h, o, a, r);
472
+ const d = await a.flush();
473
473
  if (d.find((b) => b.kind === "patch"))
474
474
  throw new Error(
475
- `Contract "${a}" (adapter) attempted to patch the document`
475
+ `Contract "${r}" (adapter) attempted to patch the document`
476
476
  );
477
477
  const m = d.filter((b) => b.kind === "event");
478
478
  for (const b of m)
@@ -482,25 +482,25 @@ class Me {
482
482
  * Schedules a handler contract for future execution
483
483
  */
484
484
  scheduleHandler(e) {
485
- const { contractNode: t, contractName: n, nodePath: o, event: r, depth: a, afterTaskId: s } = e, i = t.getType();
485
+ const { contractNode: t, contractName: n, nodePath: o, event: a, depth: r, afterTaskId: s } = e, i = t.getType();
486
486
  if (!i) {
487
487
  console.warn(`Contract node type is not defined for: ${n}`);
488
488
  return;
489
489
  }
490
- const u = this.registry.orderOf(i), h = t.get("/order"), d = V(h) ? h.toNumber() : 0, p = this.getNextTaskId() + s, m = Ce(
491
- a,
492
- r.seq,
490
+ const u = this.registry.orderOf(i), h = t.get("/order"), d = K(h) ? h.toNumber() : 0, p = this.getNextTaskId() + s, m = Ne(
491
+ r,
492
+ a.seq,
493
493
  u,
494
494
  d,
495
495
  n,
496
496
  p
497
- ), w = this.traceManager.addHop(r, o, n);
497
+ ), E = this.traceManager.addHop(a, o, n);
498
498
  this.queue.push({
499
499
  key: m,
500
500
  nodePath: o,
501
501
  contractName: n,
502
502
  contractNode: t,
503
- event: w
503
+ event: E
504
504
  });
505
505
  }
506
506
  /**
@@ -510,41 +510,61 @@ class Me {
510
510
  return e.source === "channel" && !!e.originNodePath && e.originNodePath !== t;
511
511
  }
512
512
  }
513
- const qe = (c, e, t) => {
514
- (t instanceof K || t instanceof z) && console.error(
513
+ const De = (c, e, t) => {
514
+ (t instanceof G || t instanceof ee) && console.error(
515
515
  `[Blue] Failed to apply patches for contract "${c}" on event ${JSON.stringify(e)}`,
516
516
  t
517
517
  );
518
518
  };
519
- function F(c, e) {
519
+ function L(c, e) {
520
520
  const t = c.clone(), n = (o) => {
521
- if (!C(o)) return;
522
- const r = o.getContracts();
523
- r && (!r.checkpoint || !E.isTypeOf(
524
- r.checkpoint,
525
- ce
521
+ if (!P(o)) return;
522
+ const a = o.getContracts();
523
+ (!(a != null && a.checkpoint) || !T.isTypeOf(
524
+ a.checkpoint,
525
+ he
526
526
  )) && o.addContract(
527
527
  "checkpoint",
528
528
  e.jsonValueToNode({
529
529
  type: {
530
530
  name: "Channel Event Checkpoint",
531
- blueId: g["Channel Event Checkpoint"]
531
+ blueId: y["Channel Event Checkpoint"]
532
532
  },
533
533
  lastEvents: {}
534
534
  })
535
535
  );
536
536
  };
537
537
  n(t);
538
- for (const { absPath: o } of q(t, e)) {
539
- const r = t.get(o);
540
- C(r) && n(r);
538
+ for (const { absPath: o } of D(t, e)) {
539
+ const a = t.get(o);
540
+ P(a) && n(a);
541
541
  }
542
- return L(t);
542
+ return k(t);
543
543
  }
544
- class $e {
544
+ function je(c, e) {
545
+ const t = c.clone();
546
+ return P(t) ? (te(t, e) || t.addContract(
547
+ "initialized",
548
+ e.jsonValueToNode({
549
+ type: {
550
+ name: "Initialized Marker",
551
+ blueId: y["Initialized Marker"]
552
+ }
553
+ })
554
+ ), k(t)) : k(t);
555
+ }
556
+ function te(c, e) {
557
+ const t = c.getContracts();
558
+ return Object.values(t ?? {}).some(
559
+ (n) => e.isTypeOf(n, de, {
560
+ checkSchemaExtensions: !0
561
+ })
562
+ );
563
+ }
564
+ class xe {
545
565
  constructor(e) {
546
566
  l(this, "contractType", "Channel Event Checkpoint");
547
- l(this, "contractBlueId", g["Channel Event Checkpoint"]);
567
+ l(this, "contractBlueId", y["Channel Event Checkpoint"]);
548
568
  l(this, "role", "handler");
549
569
  this.cache = e;
550
570
  }
@@ -553,17 +573,14 @@ class $e {
553
573
  return e.source === "channel" && ((t = e.rootEvent) == null ? void 0 : t.payload) === e.payload && ((n = e.rootEvent) == null ? void 0 : n.source) === "external";
554
574
  }
555
575
  async handle(e, t, n) {
556
- var a;
557
- if (!e.channelName || !((a = e.rootEvent) != null && a.seq)) return;
558
- const o = await n.getBlue().calculateBlueId(e.rootEvent.payload), r = n.getNodePath().replace(/\/contracts\/checkpoint$/, "");
559
- this.cache.record(r, e, o);
560
- }
561
- init() {
562
- return [];
576
+ var r;
577
+ if (!e.channelName || !((r = e.rootEvent) != null && r.seq)) return;
578
+ const o = await n.getBlue().calculateBlueId(e.rootEvent.payload), a = n.getNodePath().replace(/\/contracts\/checkpoint$/, "");
579
+ this.cache.record(a, e, o);
563
580
  }
564
581
  }
565
- const U = (c, e) => c.get(e) !== void 0;
566
- class Ae {
582
+ const _ = (c, e) => c.get(e) !== void 0;
583
+ class We {
567
584
  constructor() {
568
585
  l(this, "firstSeen", /* @__PURE__ */ new Map());
569
586
  }
@@ -574,21 +591,21 @@ class Ae {
574
591
  /** Turn cached data into JSON-Patch ops */
575
592
  flush(e) {
576
593
  const t = [];
577
- for (const { docBase: n, event: o, eventBlueId: r } of this.firstSeen.values()) {
594
+ for (const { docBase: n, event: o, eventBlueId: a } of this.firstSeen.values()) {
578
595
  if (!o.channelName) continue;
579
- const a = T(
596
+ const r = v(
580
597
  n,
581
598
  "contracts/checkpoint/lastEvents",
582
599
  o.channelName
583
- ), s = `${a}/blueId`;
584
- U(e, a) ? t.push({
585
- op: U(e, s) ? "replace" : "add",
600
+ ), s = `${r}/blueId`;
601
+ _(e, r) ? t.push({
602
+ op: _(e, s) ? "replace" : "add",
586
603
  path: s,
587
- val: r
604
+ val: a
588
605
  }) : t.push({
589
606
  op: "add",
590
- path: a,
591
- val: { blueId: r }
607
+ path: r,
608
+ val: { blueId: a }
592
609
  });
593
610
  }
594
611
  return t;
@@ -597,7 +614,7 @@ class Ae {
597
614
  this.firstSeen.clear();
598
615
  }
599
616
  }
600
- class P {
617
+ class S {
601
618
  constructor() {
602
619
  l(this, "role", "adapter");
603
620
  }
@@ -608,121 +625,172 @@ class P {
608
625
  baseSupports(e) {
609
626
  return e.source !== "channel";
610
627
  }
611
- /**
612
- * Base implementation that returns empty array
613
- * Can be overridden by derived classes if needed
614
- */
615
- init() {
616
- return [];
617
- }
618
628
  }
619
- class xe extends P {
629
+ class Ve extends S {
620
630
  constructor() {
621
631
  super(...arguments);
622
632
  l(this, "contractType", "Composite Timeline Channel");
623
- l(this, "contractBlueId", g["Composite Timeline Channel"]);
633
+ l(this, "contractBlueId", y["Composite Timeline Channel"]);
624
634
  }
625
635
  supports(t, n, o) {
626
- const r = o.getBlue().nodeToSchemaOutput(n, ie);
627
- return !r.channels || !t.channelName ? !1 : r.channels.includes(t.channelName);
636
+ const a = o.getBlue().nodeToSchemaOutput(n, pe);
637
+ return !a.channels || !t.channelName ? !1 : a.channels.includes(t.channelName);
628
638
  }
629
- handle(t, n, o, r) {
639
+ handle(t, n, o, a) {
630
640
  o.emitEvent({
631
641
  payload: t.payload,
632
- channelName: r,
642
+ channelName: a,
633
643
  source: "channel"
634
644
  });
635
645
  }
636
646
  }
637
- class De extends P {
647
+ class Fe extends S {
638
648
  constructor() {
639
649
  super(...arguments);
640
650
  l(this, "contractType", "Document Update Channel");
641
- l(this, "contractBlueId", g["Document Update Channel"]);
651
+ l(this, "contractBlueId", y["Document Update Channel"]);
642
652
  }
643
- supports(t, n, o, r) {
653
+ supports(t, n, o, a) {
644
654
  if (!this.baseSupports(t)) return !1;
645
- const a = o.getBlue().nodeToSchemaOutput(n, le), s = t.payload.path;
646
- if (!s || t.channelName === r) return !1;
647
- const i = a.path;
648
- return M(i) && s === o.resolvePath(i);
649
- }
650
- handle(t, n, o, r) {
651
- const a = t.payload;
652
- a && o.emitEvent({
653
- payload: a,
654
- channelName: r,
655
+ const r = o.getBlue().nodeToSchemaOutput(n, me), s = t.payload.path;
656
+ if (!s || t.channelName === a) return !1;
657
+ const i = r.path;
658
+ return I(i) && s === o.resolvePath(i);
659
+ }
660
+ handle(t, n, o, a) {
661
+ const r = t.payload;
662
+ r && o.emitEvent({
663
+ payload: r,
664
+ channelName: a,
655
665
  source: "channel"
656
666
  });
657
667
  }
658
668
  }
659
- class We extends P {
669
+ class Je extends S {
660
670
  constructor() {
661
671
  super(...arguments);
662
672
  l(this, "contractType", "Embedded Node Channel");
663
- l(this, "contractBlueId", g["Embedded Node Channel"]);
673
+ l(this, "contractBlueId", y["Embedded Node Channel"]);
664
674
  }
665
675
  supports(t, n, o) {
666
676
  if (!this.baseSupports(t)) return !1;
667
- const r = o.getBlue().nodeToSchemaOutput(n, A);
668
- return f(t.originNodePath) && f(r.path) && t.originNodePath === o.resolvePath(r.path);
677
+ const a = o.getBlue().nodeToSchemaOutput(n, x);
678
+ return f(t.originNodePath) && f(a.path) && t.originNodePath === o.resolvePath(a.path);
669
679
  }
670
- handle(t, n, o, r) {
671
- const a = o.getBlue().nodeToSchemaOutput(n, A), { originNodePath: s, payload: i } = t;
672
- f(a.path) && s === o.resolvePath(a.path) && o.emitEvent({
680
+ handle(t, n, o, a) {
681
+ const r = o.getBlue().nodeToSchemaOutput(n, x), { originNodePath: s, payload: i } = t;
682
+ f(r.path) && s === o.resolvePath(r.path) && o.emitEvent({
673
683
  payload: i,
674
- channelName: r,
684
+ channelName: a,
675
685
  source: "channel"
676
686
  });
677
687
  }
678
688
  }
679
- const _ = (c) => c.payload.type === "Timeline Entry" || c.payload.type === "MyOS Timeline Entry";
680
- class je extends P {
689
+ class Le {
690
+ constructor() {
691
+ l(this, "contractType", "Initialized Marker");
692
+ l(this, "contractBlueId", y["Initialized Marker"]);
693
+ l(this, "role", "marker");
694
+ }
695
+ supports() {
696
+ return !1;
697
+ }
698
+ handle() {
699
+ }
700
+ }
701
+ const _e = /* @__PURE__ */ new Set([
702
+ "Document Processing Initiated"
703
+ // Add more lifecycle events here as needed
704
+ // 'Document Processing Completed',
705
+ // 'Document Processing Failed',
706
+ ]);
707
+ class Ue extends S {
708
+ constructor() {
709
+ super(...arguments);
710
+ l(this, "contractType", "Lifecycle Event Channel");
711
+ l(this, "contractBlueId", y["Lifecycle Event Channel"]);
712
+ }
713
+ supports(t, n, o) {
714
+ return !this.baseSupports(t) || !this.isLifecycleEvent(t) ? !1 : this.isEventPatternMatch(t, n, o);
715
+ }
716
+ handle(t, n, o, a) {
717
+ o.emitEvent({
718
+ payload: t.payload,
719
+ channelName: a,
720
+ source: "channel"
721
+ });
722
+ }
723
+ /**
724
+ * Checks if the event is a supported lifecycle event type
725
+ */
726
+ isLifecycleEvent(t) {
727
+ return _e.has(t.payload.type);
728
+ }
729
+ /**
730
+ * Checks if the event matches the channel's event pattern (if specified)
731
+ */
732
+ isEventPatternMatch(t, n, o) {
733
+ var r;
734
+ const a = (r = n.getProperties()) == null ? void 0 : r.event;
735
+ if (!a)
736
+ return !0;
737
+ try {
738
+ const s = o.getBlue(), i = s.nodeToJson(
739
+ s.jsonValueToNode(t.payload)
740
+ ), u = s.nodeToJson(a);
741
+ return X(i, u);
742
+ } catch (s) {
743
+ return console.warn("Error during lifecycle event pattern matching:", s), !1;
744
+ }
745
+ }
746
+ }
747
+ const U = (c) => c.payload.type === "Timeline Entry" || c.payload.type === "MyOS Timeline Entry";
748
+ class ze extends S {
681
749
  constructor() {
682
750
  super(...arguments);
683
751
  l(this, "contractType", "MyOS Timeline Channel");
684
- l(this, "contractBlueId", H["MyOS Timeline Channel"]);
752
+ l(this, "contractBlueId", Y["MyOS Timeline Channel"]);
685
753
  }
686
754
  supports(t, n, o) {
687
755
  var m;
688
- if (!this.baseSupports(t) || !_(t)) return !1;
689
- const r = o.getBlue(), a = r.jsonValueToNode(t.payload), s = r.nodeToSchemaOutput(
690
- a,
691
- Ee
692
- ), i = o.getBlue().nodeToSchemaOutput(n, we), u = (m = s.timeline) == null ? void 0 : m.getValue(), h = f(i.timelineId) && f(u), d = f(i.account) && f(s.account), p = f(i.email) && f(s.email);
756
+ if (!this.baseSupports(t) || !U(t)) return !1;
757
+ const a = o.getBlue(), r = a.jsonValueToNode(t.payload), s = a.nodeToSchemaOutput(
758
+ r,
759
+ Te
760
+ ), i = o.getBlue().nodeToSchemaOutput(n, be), u = (m = s.timeline) == null ? void 0 : m.timelineId, h = f(i.timelineId) && f(u), d = f(i.account) && f(s.account), p = f(i.email) && f(s.email);
693
761
  return h && u === i.timelineId || d && s.account === i.account || p && s.email === i.email;
694
762
  }
695
- handle(t, n, o, r) {
696
- _(t) && o.emitEvent({
763
+ handle(t, n, o, a) {
764
+ U(t) && o.emitEvent({
697
765
  payload: t.payload,
698
- channelName: r,
766
+ channelName: a,
699
767
  source: "channel"
700
768
  });
701
769
  }
702
770
  }
703
- class Fe extends P {
771
+ class He extends S {
704
772
  constructor() {
705
773
  super(...arguments);
706
774
  l(this, "contractType", "MyOS Agent Channel");
707
- l(this, "contractBlueId", H["MyOS Agent Channel"]);
775
+ l(this, "contractBlueId", Y["MyOS Agent Channel"]);
708
776
  }
709
777
  supports(t, n, o) {
710
778
  if (!this.baseSupports(t)) return !1;
711
779
  try {
712
- const { myosAgentEvent: r, myosAgentChannel: a } = this.parseEventAndChannel(
780
+ const { myosAgentEvent: a, myosAgentChannel: r } = this.parseEventAndChannel(
713
781
  t,
714
782
  n,
715
783
  o
716
784
  );
717
- return this.isAgentMatch(r, a) && this.isEventPatternMatch(r, a, o);
718
- } catch (r) {
719
- return console.warn("Error in MyOSAgentChannelProcessor.supports:", r), !1;
785
+ return this.isAgentMatch(a, r) && this.isEventPatternMatch(a, r, o);
786
+ } catch (a) {
787
+ return console.warn("Error in MyOSAgentChannelProcessor.supports:", a), !1;
720
788
  }
721
789
  }
722
- handle(t, n, o, r) {
790
+ handle(t, n, o, a) {
723
791
  o.emitEvent({
724
792
  payload: t.payload,
725
- channelName: r,
793
+ channelName: a,
726
794
  source: "channel"
727
795
  });
728
796
  }
@@ -731,12 +799,12 @@ class Fe extends P {
731
799
  * @throws {Error} If schema validation fails
732
800
  */
733
801
  parseEventAndChannel(t, n, o) {
734
- const r = o.getBlue(), a = r.jsonValueToNode(t.payload), s = r.nodeToSchemaOutput(
735
- a,
736
- ve
737
- ), i = r.nodeToSchemaOutput(
802
+ const a = o.getBlue(), r = a.jsonValueToNode(t.payload), s = a.nodeToSchemaOutput(
803
+ r,
804
+ Pe
805
+ ), i = a.nodeToSchemaOutput(
738
806
  n,
739
- Te
807
+ Se
740
808
  );
741
809
  return { myosAgentEvent: s, myosAgentChannel: i };
742
810
  }
@@ -747,9 +815,9 @@ class Fe extends P {
747
815
  * @returns true if both have valid agent IDs and they match
748
816
  */
749
817
  isAgentMatch(t, n) {
750
- var a;
751
- const o = t.agentId, r = (a = n.agent) == null ? void 0 : a.agentId;
752
- return f(o) && f(r) && o === r;
818
+ var r;
819
+ const o = t.agentId, a = (r = n.agent) == null ? void 0 : r.agentId;
820
+ return f(o) && f(a) && o === a;
753
821
  }
754
822
  /**
755
823
  * Checks if the event pattern matches the channel's event filter
@@ -765,29 +833,29 @@ class Fe extends P {
765
833
  * - Otherwise → uses deep containment matching (event must contain all channel pattern fields)
766
834
  */
767
835
  isEventPatternMatch(t, n, o) {
768
- const r = n.event;
769
- if (!r)
770
- return !0;
771
- const a = t.event;
836
+ const a = n.event;
772
837
  if (!a)
838
+ return !0;
839
+ const r = t.event;
840
+ if (!r)
773
841
  return !1;
774
842
  try {
775
- const s = o.getBlue(), i = s.nodeToJson(a), u = s.nodeToJson(r);
776
- return ge(i, u);
843
+ const s = o.getBlue(), i = s.nodeToJson(r), u = s.nodeToJson(a);
844
+ return X(i, u);
777
845
  } catch (s) {
778
846
  return console.warn("Error during event pattern matching:", s), !1;
779
847
  }
780
848
  }
781
849
  }
782
- class Ue {
850
+ class Re {
783
851
  constructor() {
784
852
  l(this, "contractType", "Operation");
785
- l(this, "contractBlueId", g.Operation);
853
+ l(this, "contractBlueId", y.Operation);
786
854
  l(this, "role", "adapter");
787
855
  }
788
856
  supports(e, t, n, o) {
789
- const a = n.getBlue().nodeToSchemaOutput(t, ue), s = this.parseEventPayload(e, n);
790
- return f(s == null ? void 0 : s.operation) && s.operation === o && e.source === "channel" && e.channelName === a.channel;
857
+ const r = n.getBlue().nodeToSchemaOutput(t, fe), s = this.parseEventPayload(e, n);
858
+ return f(s == null ? void 0 : s.operation) && s.operation === o && e.source === "channel" && e.channelName === r.channel;
791
859
  }
792
860
  async handle(e, t, n, o) {
793
861
  n.emitEvent({
@@ -796,58 +864,54 @@ class Ue {
796
864
  source: "channel"
797
865
  });
798
866
  }
799
- init() {
800
- return [];
801
- }
802
867
  parseEventPayload(e, t) {
803
868
  const n = t.getBlue(), o = n.jsonValueToNode(e.payload);
804
- if (E.isTypeOf(o, B)) {
805
- const r = n.nodeToSchemaOutput(
869
+ if (n.isTypeOf(o, $, {
870
+ checkSchemaExtensions: !0
871
+ })) {
872
+ const a = n.nodeToSchemaOutput(
806
873
  o,
807
- B
874
+ $
808
875
  );
809
- if (r.message)
876
+ if (a.message)
810
877
  return n.nodeToSchemaOutput(
811
- r.message,
812
- he
878
+ a.message,
879
+ ye
813
880
  );
814
881
  }
815
882
  return null;
816
883
  }
817
884
  }
818
- class _e {
885
+ class Qe {
819
886
  constructor() {
820
887
  l(this, "contractType", "Process Embedded");
821
888
  l(this, "role", "adapter");
822
- l(this, "contractBlueId", g["Process Embedded"]);
889
+ l(this, "contractBlueId", y["Process Embedded"]);
823
890
  }
824
891
  supports(e) {
825
892
  return e.source !== "channel";
826
893
  }
827
894
  handle(e, t, n) {
828
- const o = n.getBlue().nodeToSchemaOutput(t, I);
829
- for (const r of o.paths ?? [])
895
+ const o = n.getBlue().nodeToSchemaOutput(t, M);
896
+ for (const a of o.paths ?? [])
830
897
  n.emitEvent({
831
898
  ...e,
832
- dispatchPath: n.resolvePath(r)
899
+ dispatchPath: n.resolvePath(a)
833
900
  });
834
901
  }
835
- init() {
836
- return [];
837
- }
838
902
  }
839
- const X = !process.env.SKIP_ISOLATED_VM;
840
- let v = null;
841
- if (X)
903
+ const ne = !process.env.SKIP_ISOLATED_VM;
904
+ let w = null;
905
+ if (ne)
842
906
  try {
843
- v = require("isolated-vm");
907
+ w = require("isolated-vm");
844
908
  } catch {
845
909
  console.warn("isolated-vm not available, using fallback evaluation method");
846
910
  }
847
- function J(c) {
911
+ function z(c) {
848
912
  return /\bimport\s.+\sfrom\s+['"][^'"]+['"]/.test(c) || /\bexport\s+/.test(c);
849
913
  }
850
- class G {
914
+ class q {
851
915
  /**
852
916
  * Main evaluation method - chooses between secure and simple evaluation strategies
853
917
  */
@@ -857,14 +921,14 @@ class G {
857
921
  bindings: n = {},
858
922
  options: o = {}
859
923
  }) {
860
- return !v || !X ? this.evaluateSimple(e, n, o) : this.evaluateSecure(e, n, t, o);
924
+ return !w || !ne ? this.evaluateSimple(e, n, o) : this.evaluateSecure(e, n, t, o);
861
925
  }
862
926
  /**
863
927
  * Fallback evaluation using Node's Function constructor
864
928
  * Used when isolated-vm is not available
865
929
  */
866
930
  static async evaluateSimple(e, t, n = {}) {
867
- if (J(e))
931
+ if (z(e))
868
932
  throw new Error(
869
933
  "Static import/export syntax requires isolated-vm – start Node without SKIP_ISOLATED_VM."
870
934
  );
@@ -885,99 +949,99 @@ class G {
885
949
  `return ${e};`
886
950
  )(...Object.values(t));
887
951
  } catch (o) {
888
- throw n.isCodeBlock ? new D(e, o) : new x(e, o);
952
+ throw n.isCodeBlock ? new V(e, o) : new W(e, o);
889
953
  }
890
954
  }
891
955
  /**
892
956
  * Secure evaluation using isolated-vm with support for ES modules
893
957
  */
894
958
  static async evaluateSecure(e, t, n, o = {}) {
895
- if (!v) throw new Error("isolated-vm not available");
896
- const r = new v.Isolate({ memoryLimit: 32 }), a = await r.createContext(), s = a.global;
959
+ if (!w) throw new Error("isolated-vm not available");
960
+ const a = new w.Isolate({ memoryLimit: 32 }), r = await a.createContext(), s = r.global;
897
961
  try {
898
962
  await this.setupIsolateEnvironment(s, t);
899
963
  const i = /* @__PURE__ */ new Map(), u = this.createModuleResolver(
900
- r,
901
964
  a,
965
+ r,
902
966
  i,
903
967
  n
904
968
  );
905
- return J(e) ? await this.evaluateESModule(
906
- r,
969
+ return z(e) ? await this.evaluateESModule(
907
970
  a,
971
+ r,
908
972
  e,
909
973
  o,
910
974
  u
911
975
  ) : await this.evaluateSimpleScript(
912
- r,
913
976
  a,
977
+ r,
914
978
  e,
915
979
  t,
916
980
  o
917
981
  );
918
982
  } catch (i) {
919
- throw o.isCodeBlock ? new D(e, i) : new x(e, i);
983
+ throw o.isCodeBlock ? new V(e, i) : new W(e, i);
920
984
  } finally {
921
- a.release(), r.dispose();
985
+ r.release(), a.dispose();
922
986
  }
923
987
  }
924
988
  /**
925
989
  * Setup the isolated VM environment with necessary host functions and data
926
990
  */
927
991
  static async setupIsolateEnvironment(e, t) {
928
- if (!v) throw new Error("isolated-vm not available");
929
- const n = new v.Callback(
992
+ if (!w) throw new Error("isolated-vm not available");
993
+ const n = new w.Callback(
930
994
  (...o) => console.log(...o)
931
995
  );
932
996
  await e.set("log", n);
933
- for (const [o, r] of Object.entries(t))
934
- typeof r == "function" ? await e.set(
997
+ for (const [o, a] of Object.entries(t))
998
+ typeof a == "function" ? await e.set(
935
999
  o,
936
- new v.Callback(r)
937
- ) : await e.set(o, new v.ExternalCopy(r).copyInto());
1000
+ new w.Callback(a)
1001
+ ) : await e.set(o, new w.ExternalCopy(a).copyInto());
938
1002
  }
939
1003
  /**
940
1004
  * Create module resolver function for handling imports
941
1005
  */
942
1006
  static createModuleResolver(e, t, n, o) {
943
- return async (r) => {
944
- if (n.has(r)) return n.get(r);
945
- if (r.startsWith("blue:")) {
946
- const a = r.slice(5), s = o.loadBlueContent;
1007
+ return async (a) => {
1008
+ if (n.has(a)) return n.get(a);
1009
+ if (a.startsWith("blue:")) {
1010
+ const r = a.slice(5), s = o.loadBlueContent;
947
1011
  if (typeof s != "function")
948
1012
  throw new Error(
949
- `ProcessingContext is missing a loadBlueContent(blueId) implementation (needed for ${r})`
1013
+ `ProcessingContext is missing a loadBlueContent(blueId) implementation (needed for ${a})`
950
1014
  );
951
- const i = await s(a), u = await e.compileModule(i);
952
- return n.set(r, u), await u.instantiate(
1015
+ const i = await s(r), u = await e.compileModule(i);
1016
+ return n.set(a, u), await u.instantiate(
953
1017
  t,
954
1018
  this.createModuleResolver(e, t, n, o)
955
1019
  ), u;
956
1020
  }
957
- if (/^https?:\/\//.test(r)) {
958
- let a;
1021
+ if (/^https?:\/\//.test(a)) {
1022
+ let r;
959
1023
  if (typeof o.loadExternalModule == "function")
960
- a = await o.loadExternalModule(r);
1024
+ r = await o.loadExternalModule(a);
961
1025
  else
962
1026
  throw new Error(
963
- `ProcessingContext is missing a loadExternalModule(url) implementation (needed for ${r})`
1027
+ `ProcessingContext is missing a loadExternalModule(url) implementation (needed for ${a})`
964
1028
  );
965
- const s = await e.compileModule(a);
966
- return n.set(r, s), await s.instantiate(
1029
+ const s = await e.compileModule(r);
1030
+ return n.set(a, s), await s.instantiate(
967
1031
  t,
968
1032
  this.createModuleResolver(e, t, n, o)
969
1033
  ), s;
970
1034
  }
971
- throw new Error(`Unsupported module specifier "${r}"`);
1035
+ throw new Error(`Unsupported module specifier "${a}"`);
972
1036
  };
973
1037
  }
974
1038
  /**
975
1039
  * Evaluate code as a simple script (no imports/exports)
976
1040
  */
977
- static async evaluateSimpleScript(e, t, n, o, r) {
978
- const a = Object.keys(o).join(", "), s = Object.keys(o).map((h) => h), i = `(async (${a}) => { ${r.isCodeBlock ? n : `return (${n});`} })(${s.join(", ")})`;
1041
+ static async evaluateSimpleScript(e, t, n, o, a) {
1042
+ const r = Object.keys(o).join(", "), s = Object.keys(o).map((h) => h), i = `(async (${r}) => { ${a.isCodeBlock ? n : `return (${n});`} })(${s.join(", ")})`;
979
1043
  return await (await e.compileScript(i)).run(t, {
980
- timeout: r.timeout ?? 500,
1044
+ timeout: a.timeout ?? 500,
981
1045
  promise: !0,
982
1046
  copy: !0,
983
1047
  release: !0
@@ -986,14 +1050,14 @@ class G {
986
1050
  /**
987
1051
  * Evaluate code as an ES module with import/export support
988
1052
  */
989
- static async evaluateESModule(e, t, n, o, r) {
990
- let a = n;
1053
+ static async evaluateESModule(e, t, n, o, a) {
1054
+ let r = n;
991
1055
  if (o.isCodeBlock) {
992
1056
  const u = /^\s*(import\s.+?;|export\s.+?;)/gm, h = (n.match(u) || []).join(
993
1057
  `
994
1058
  `
995
1059
  ), d = n.replace(u, "").trim();
996
- a = `
1060
+ r = `
997
1061
  ${h}
998
1062
  const run = function() {
999
1063
  ${d}
@@ -1001,8 +1065,8 @@ class G {
1001
1065
  export default run();
1002
1066
  `;
1003
1067
  }
1004
- const s = await e.compileModule(a);
1005
- return await s.instantiate(t, r), await s.evaluate({
1068
+ const s = await e.compileModule(r);
1069
+ return await s.instantiate(t, a), await s.evaluate({
1006
1070
  timeout: o.timeout ?? 500,
1007
1071
  promise: !0,
1008
1072
  reference: !0,
@@ -1015,121 +1079,162 @@ class G {
1015
1079
  });
1016
1080
  }
1017
1081
  }
1018
- class Y {
1082
+ class A {
1019
1083
  /**
1020
1084
  * Creates standard bindings for workflow step execution
1021
1085
  */
1022
1086
  static createStandardBindings(e, t, n) {
1023
1087
  const o = e.getBlue();
1024
1088
  return {
1025
- document: (r) => {
1026
- const a = e.get(r);
1027
- return V(a) ? a.toNumber() : C(a) ? o.nodeToJson(a, "original") : a;
1089
+ document: (a) => {
1090
+ const r = e.get(a);
1091
+ return K(r) ? r.toNumber() : P(r) ? o.nodeToJson(r, "original") : r;
1028
1092
  },
1029
1093
  event: t.payload,
1030
1094
  steps: n
1031
1095
  };
1032
1096
  }
1033
1097
  }
1034
- class Je {
1098
+ function H(c) {
1099
+ const { op: e, path: t, val: n, from: o } = c;
1100
+ if ((e === "move" || e === "copy") && !o)
1101
+ throw new Error(`${e} operation requires 'from' path`);
1102
+ if ((e === "add" || e === "replace") && n === void 0)
1103
+ throw new Error(`${e} operation requires 'val' property`);
1104
+ const a = { type: "Document Update", op: e, path: t };
1105
+ return n !== void 0 && (a.val = n), o !== void 0 && (a.from = o), a;
1106
+ }
1107
+ function Ke() {
1108
+ return {
1109
+ type: "Document Processing Initiated"
1110
+ };
1111
+ }
1112
+ class Xe {
1035
1113
  constructor() {
1036
1114
  l(this, "stepType", "Update Document");
1037
1115
  }
1038
1116
  supports(e) {
1039
- return E.isTypeOf(e, k);
1117
+ return T.isTypeOf(e, N);
1118
+ }
1119
+ async execute(e, t, n, o, a) {
1120
+ const r = n.getBlue();
1121
+ if (!T.isTypeOf(e, N)) return;
1122
+ const s = await this.evaluateChangeset(
1123
+ e.get("/changeset"),
1124
+ n,
1125
+ t,
1126
+ a
1127
+ ), i = Q(e, {
1128
+ op: "replace",
1129
+ path: "/changeset",
1130
+ val: s
1131
+ }), u = n.getBlue().nodeToSchemaOutput(i, N);
1132
+ for (const h of u.changeset ?? []) {
1133
+ if (!h.path) continue;
1134
+ const d = h.val;
1135
+ if ((h.op === "replace" || h.op === "add") && I(d)) {
1136
+ const p = await this.evaluateChangeValue(
1137
+ d,
1138
+ n,
1139
+ t,
1140
+ a
1141
+ );
1142
+ n.addPatch({
1143
+ op: h.op,
1144
+ path: h.path,
1145
+ val: p
1146
+ }), n.emitEvent({
1147
+ payload: H({
1148
+ op: h.op,
1149
+ path: n.resolvePath(h.path),
1150
+ val: r.nodeToJson(p, "original")
1151
+ })
1152
+ });
1153
+ }
1154
+ h.op === "remove" && (n.addPatch({ op: h.op, path: h.path }), n.emitEvent({
1155
+ payload: H({
1156
+ op: h.op,
1157
+ path: n.resolvePath(h.path),
1158
+ val: null
1159
+ })
1160
+ }));
1161
+ }
1162
+ }
1163
+ async evaluateChangeset(e, t, n, o) {
1164
+ const a = t.getBlue();
1165
+ if (typeof e == "string" && e.startsWith("${") && e.endsWith("}")) {
1166
+ const r = e.slice(2, -1), s = await q.evaluate({
1167
+ code: r,
1168
+ ctx: t,
1169
+ bindings: A.createStandardBindings(
1170
+ t,
1171
+ n,
1172
+ o
1173
+ )
1174
+ });
1175
+ return a.jsonValueToNode(s ?? null);
1176
+ }
1177
+ if (P(e))
1178
+ return e;
1179
+ throw new Error("Invalid changeset: expected a string or document node");
1040
1180
  }
1041
1181
  async evaluateChangeValue(e, t, n, o) {
1042
- const r = e == null ? void 0 : e.getValue();
1043
- if (typeof r == "string" && r.startsWith("${") && r.endsWith("}")) {
1044
- const a = r.slice(2, -1);
1045
- return await G.evaluate({
1046
- code: a,
1182
+ const a = e.getValue(), r = t.getBlue();
1183
+ if (typeof a == "string" && a.startsWith("${") && a.endsWith("}")) {
1184
+ const s = a.slice(2, -1), i = await q.evaluate({
1185
+ code: s,
1047
1186
  ctx: t,
1048
- bindings: Y.createStandardBindings(
1187
+ bindings: A.createStandardBindings(
1049
1188
  t,
1050
1189
  n,
1051
1190
  o
1052
1191
  )
1053
1192
  });
1193
+ return r.jsonValueToNode(i ?? null);
1054
1194
  }
1055
1195
  return e;
1056
1196
  }
1057
- async execute(e, t, n, o, r) {
1058
- if (!E.isTypeOf(e, k)) return;
1059
- const a = n.getBlue().nodeToSchemaOutput(e, k);
1060
- for (const s of a.changeset ?? [])
1061
- if (s.path) {
1062
- if (s.op === "replace" || s.op === "add") {
1063
- const i = await this.evaluateChangeValue(
1064
- s.val,
1065
- n,
1066
- t,
1067
- r
1068
- );
1069
- n.addPatch({
1070
- op: s.op,
1071
- path: s.path,
1072
- val: i
1073
- }), n.emitEvent({
1074
- payload: {
1075
- type: "Document Update",
1076
- op: s.op,
1077
- path: n.resolvePath(s.path),
1078
- val: i
1079
- }
1080
- });
1081
- }
1082
- s.op === "remove" && (n.addPatch({ op: s.op, path: s.path }), n.emitEvent({
1083
- payload: {
1084
- type: "Document Update",
1085
- op: s.op,
1086
- path: n.resolvePath(s.path),
1087
- val: null
1088
- }
1089
- }));
1090
- }
1091
- }
1092
1197
  }
1093
- class Re {
1198
+ class Ye {
1094
1199
  constructor() {
1095
1200
  l(this, "stepType", "Trigger Event");
1096
1201
  }
1097
1202
  supports(e) {
1098
- return E.isTypeOf(e, N);
1203
+ return T.isTypeOf(e, B);
1099
1204
  }
1100
1205
  async execute(e, t, n) {
1101
1206
  const o = n.getBlue();
1102
- if (!E.isTypeOf(e, N)) return;
1103
- const r = o.nodeToSchemaOutput(e, N);
1104
- if (!r.event) return;
1105
- const a = o.nodeToJson(
1106
- r.event,
1207
+ if (!T.isTypeOf(e, B)) return;
1208
+ const a = o.nodeToSchemaOutput(e, B);
1209
+ if (!a.event) return;
1210
+ const r = o.nodeToJson(
1211
+ a.event,
1107
1212
  "original"
1108
1213
  );
1109
1214
  n.emitEvent({
1110
- payload: a
1215
+ payload: r
1111
1216
  });
1112
1217
  }
1113
1218
  }
1114
- class Ve {
1219
+ class Ze {
1115
1220
  constructor() {
1116
1221
  l(this, "stepType", "JavaScript Code");
1117
1222
  }
1118
1223
  supports(e) {
1119
- return E.isTypeOf(e, O);
1224
+ return T.isTypeOf(e, O);
1120
1225
  }
1121
- async execute(e, t, n, o, r) {
1122
- if (!E.isTypeOf(e, O)) return;
1226
+ async execute(e, t, n, o, a) {
1227
+ if (!T.isTypeOf(e, O)) return;
1123
1228
  const s = n.getBlue().nodeToSchemaOutput(
1124
1229
  e,
1125
1230
  O
1126
1231
  );
1127
1232
  if (!s.code)
1128
1233
  throw new Error("JavaScript code is required");
1129
- const i = await G.evaluate({
1234
+ const i = await q.evaluate({
1130
1235
  code: s.code,
1131
1236
  ctx: n,
1132
- bindings: Y.createStandardBindings(n, t, r),
1237
+ bindings: A.createStandardBindings(n, t, a),
1133
1238
  options: {
1134
1239
  isCodeBlock: !0,
1135
1240
  timeout: 500
@@ -1146,15 +1251,15 @@ class Ve {
1146
1251
  return i;
1147
1252
  }
1148
1253
  }
1149
- const Le = [
1150
- new Je(),
1151
- new Re(),
1152
- new Ve()
1254
+ const Ge = [
1255
+ new Xe(),
1256
+ new Ye(),
1257
+ new Ze()
1153
1258
  ];
1154
- class Z {
1155
- constructor(e = Le) {
1259
+ class oe {
1260
+ constructor(e = Ge) {
1156
1261
  l(this, "contractType", "Sequential Workflow");
1157
- l(this, "contractBlueId", g["Sequential Workflow"]);
1262
+ l(this, "contractBlueId", y["Sequential Workflow"]);
1158
1263
  l(this, "role", "handler");
1159
1264
  l(this, "executors", []);
1160
1265
  this.executors = e;
@@ -1164,16 +1269,16 @@ class Z {
1164
1269
  this.executors.push(e);
1165
1270
  }
1166
1271
  supports(e, t, n) {
1167
- const a = n.getBlue().nodeToSchemaOutput(
1272
+ const r = n.getBlue().nodeToSchemaOutput(
1168
1273
  t,
1169
- de
1274
+ ge
1170
1275
  ).channel;
1171
- return e.source === "channel" && e.channelName === a;
1276
+ return e.source === "channel" && e.channelName === r;
1172
1277
  }
1173
1278
  async handle(e, t, n, o) {
1174
1279
  var s;
1175
- const r = {}, a = (s = t.getProperties()) == null ? void 0 : s.steps.getItems();
1176
- for (const [i, u] of (a ?? []).entries()) {
1280
+ const a = {}, r = (s = t.getProperties()) == null ? void 0 : s.steps.getItems();
1281
+ for (const [i, u] of (r ?? []).entries()) {
1177
1282
  const h = this.executors.find((p) => p.supports(u));
1178
1283
  if (!h)
1179
1284
  throw new Error(`Unsupported workflow step type "${u.getType()}"`);
@@ -1182,104 +1287,104 @@ class Z {
1182
1287
  e,
1183
1288
  n,
1184
1289
  o,
1185
- r
1290
+ a
1186
1291
  );
1187
1292
  if (d !== void 0) {
1188
1293
  const p = u.getName(), m = typeof p == "string" ? p : `Step${i + 1}`;
1189
- r[m] = d;
1294
+ a[m] = d;
1190
1295
  }
1191
1296
  await n.flush();
1192
1297
  }
1193
1298
  }
1194
- init() {
1195
- return [];
1196
- }
1197
1299
  }
1198
- class He {
1300
+ class et {
1199
1301
  constructor(e) {
1200
1302
  l(this, "contractType", "Sequential Workflow Operation");
1201
- l(this, "contractBlueId", g["Sequential Workflow Operation"]);
1303
+ l(this, "contractBlueId", y["Sequential Workflow Operation"]);
1202
1304
  l(this, "role", "handler");
1203
1305
  l(this, "sequentialWorkflowProcessor");
1204
- this.sequentialWorkflowProcessor = e || new Z();
1306
+ this.sequentialWorkflowProcessor = e || new oe();
1205
1307
  }
1206
1308
  supports(e, t, n) {
1207
- const a = n.getBlue().nodeToSchemaOutput(
1309
+ const r = n.getBlue().nodeToSchemaOutput(
1208
1310
  t,
1209
- pe
1311
+ Ee
1210
1312
  ).operation, s = e.channelName;
1211
- return e.source === "channel" && M(s) && M(a) && s === a;
1313
+ return e.source === "channel" && I(s) && I(r) && s === r;
1212
1314
  }
1213
1315
  async handle(e, t, n, o) {
1214
1316
  try {
1215
1317
  await this.sequentialWorkflowProcessor.handle(e, t, n, o);
1216
- } catch (r) {
1318
+ } catch (a) {
1217
1319
  throw console.error(
1218
1320
  "Error in SequentialWorkflowOperationProcessor.handle:",
1219
- r
1220
- ), r;
1321
+ a
1322
+ ), a;
1221
1323
  }
1222
1324
  }
1223
1325
  }
1224
1326
  const R = (c) => c.payload.type === "Timeline Entry";
1225
- class Qe extends P {
1327
+ class tt extends S {
1226
1328
  constructor() {
1227
1329
  super(...arguments);
1228
1330
  l(this, "contractType", "Timeline Channel");
1229
- l(this, "contractBlueId", g["Timeline Channel"]);
1331
+ l(this, "contractBlueId", y["Timeline Channel"]);
1230
1332
  }
1231
1333
  supports(t, n, o) {
1232
1334
  var h;
1233
1335
  if (!this.baseSupports(t) || !R(t)) return !1;
1234
- const r = o.getBlue(), a = r.nodeToSchemaOutput(
1235
- r.jsonValueToNode(t.payload),
1236
- B
1237
- ), s = o.getBlue().nodeToSchemaOutput(n, me), i = (h = a.timeline) == null ? void 0 : h.getValue();
1336
+ const a = o.getBlue(), r = a.nodeToSchemaOutput(
1337
+ a.jsonValueToNode(t.payload),
1338
+ $
1339
+ ), s = o.getBlue().nodeToSchemaOutput(n, we), i = (h = r.timeline) == null ? void 0 : h.timelineId;
1238
1340
  return f(s.timelineId) && f(i) && i === s.timelineId;
1239
1341
  }
1240
- handle(t, n, o, r) {
1342
+ handle(t, n, o, a) {
1241
1343
  R(t) && o.emitEvent({
1242
1344
  payload: t.payload,
1243
- channelName: r,
1345
+ channelName: a,
1244
1346
  source: "channel"
1245
1347
  });
1246
1348
  }
1247
1349
  }
1248
- const Ke = [
1249
- new _e(),
1250
- // channels
1251
- new We(),
1252
- new De(),
1350
+ const nt = [
1253
1351
  new Qe(),
1254
- new je(),
1352
+ // channels
1353
+ new Je(),
1255
1354
  new Fe(),
1256
- new xe(),
1355
+ new tt(),
1356
+ new ze(),
1357
+ new He(),
1358
+ new Ve(),
1257
1359
  new Ue(),
1360
+ new Re(),
1258
1361
  // sequential workflows
1259
- new Z(),
1260
- new He()
1362
+ new oe(),
1363
+ new et(),
1364
+ // markers
1365
+ new Le()
1261
1366
  ];
1262
- class tt {
1367
+ class lt {
1263
1368
  /**
1264
1369
  * Creates a new document processor
1265
1370
  *
1266
1371
  * @param processors - Initial list of processors to register
1267
1372
  */
1268
- constructor(e, t = Ke) {
1373
+ constructor(e, t = nt) {
1269
1374
  l(this, "taskCounter", 0);
1270
1375
  l(this, "eventCounter", 0);
1271
1376
  l(this, "registry");
1272
1377
  l(this, "queue");
1273
1378
  l(this, "router");
1274
- l(this, "checkpointCache", new Ae());
1275
- this.blue = e, this.registry = new Oe(t), this.queue = new Ne(), this.router = new Me(
1379
+ l(this, "checkpointCache", new We());
1380
+ this.blue = e, this.registry = new Me(t), this.queue = new Oe(), this.router = new Ae(
1276
1381
  this.blue,
1277
1382
  this.registry,
1278
1383
  this.queue,
1279
1384
  () => ++this.taskCounter,
1280
1385
  () => ++this.eventCounter
1281
1386
  ), this.register(
1282
- new $e(this.checkpointCache),
1387
+ new xe(this.checkpointCache),
1283
1388
  9999
1284
1389
  );
1285
1390
  }
@@ -1293,17 +1398,19 @@ class tt {
1293
1398
  this.registry.register(e, t);
1294
1399
  }
1295
1400
  /**
1296
- * Initializes a document and runs all init events
1401
+ * Initializes a document by emitting a Document Processing Initiated event
1297
1402
  *
1298
1403
  * @param document - The document to initialize
1299
1404
  * @returns Processing result with final state and emitted events
1300
1405
  */
1301
- async initialise(e) {
1302
- e = F(e, this.blue);
1303
- const t = await this.bootstrapContracts(e);
1304
- for (const n of t)
1305
- await this.router.route(e, [], n, 0);
1306
- return this.drainQueue(e);
1406
+ async initialize(e) {
1407
+ let t = L(e, this.blue);
1408
+ const n = {
1409
+ payload: Ke()
1410
+ }, o = [n.payload];
1411
+ await this.router.route(t, [], n, 0);
1412
+ const a = await this.drainQueue(t);
1413
+ return t = a.state, o.push(...a.emitted), t = je(t, this.blue), { state: t, emitted: o };
1307
1414
  }
1308
1415
  /**
1309
1416
  * Processes a batch of events against the document
@@ -1313,98 +1420,89 @@ class tt {
1313
1420
  * @returns Processing result with final state and emitted events
1314
1421
  */
1315
1422
  async processEvents(e, t) {
1316
- let n = F(e, this.blue);
1423
+ let n = L(e, this.blue);
1317
1424
  const o = [];
1318
- for (const r of t)
1425
+ if (!te(n, this.blue))
1426
+ throw new Error("Document is not initialized");
1427
+ for (const a of t)
1319
1428
  try {
1320
- const a = { payload: r, source: "external" };
1321
- await this.router.route(n, [], a, 0);
1429
+ const r = { payload: a, source: "external" };
1430
+ await this.router.route(n, [], r, 0);
1322
1431
  const s = await this.drainQueue(n);
1323
1432
  n = s.state, o.push(...s.emitted);
1324
1433
  const i = this.checkpointCache.flush(n);
1325
- i.length && (n = j(n, i));
1434
+ i.length && (n = J(n, i));
1326
1435
  } finally {
1327
1436
  this.checkpointCache.clear();
1328
1437
  }
1329
1438
  return { state: n, emitted: o };
1330
1439
  }
1331
- /**
1332
- * Collects initialization events from contract processors
1333
- */
1334
- async bootstrapContracts(e) {
1335
- const t = [], n = e.getContracts();
1336
- if (!n)
1337
- return t;
1338
- for (const [, o] of Object.entries(n)) {
1339
- const r = this.registry.get(o.getType());
1340
- r != null && r.init && t.push(...await r.init(o));
1341
- }
1342
- return t;
1343
- }
1344
1440
  /**
1345
1441
  * Drains the task queue and applies all actions
1346
1442
  */
1347
1443
  async drainQueue(e) {
1348
- var a;
1444
+ var r;
1349
1445
  let t = e;
1350
1446
  const n = [], o = 1e4;
1351
- let r = 0;
1447
+ let a = 0;
1352
1448
  for (; this.queue.length; ) {
1353
- if (++r > o)
1449
+ if (++a > o)
1354
1450
  throw new Error("Possible cycle – too many iterations");
1355
1451
  const s = this.queue.pop(), { nodePath: i, contractName: u, contractNode: h, event: d } = s, p = t.get(i);
1356
- if (!C(p) || !((a = p.getContracts()) != null && a[u]) || !h.getType()) continue;
1452
+ if (!P(p) || !((r = p.getContracts()) != null && r[u]) || !h.getType()) continue;
1357
1453
  const m = this.registry.get(h.getType());
1358
1454
  if (!m) {
1359
1455
  console.warn(`No processor registered for contract: ${u}`);
1360
1456
  continue;
1361
1457
  }
1362
- const w = new Q(
1458
+ const E = new Z(
1363
1459
  () => t,
1364
1460
  s,
1365
1461
  this.blue,
1366
1462
  async (b) => {
1367
- for (const y of b)
1368
- if (y.kind === "patch") {
1369
- const ee = q(t, this.blue);
1370
- for (const S of ee) {
1371
- const te = y.patch.op === "move" || y.patch.op === "copy" ? [y.patch.from, y.patch.path] : [y.patch.path], $ = w.getNodePath(), ne = te.some(
1372
- (re) => W(re, S.absPath)
1373
- ), oe = W(
1374
- $,
1375
- S.absPath
1463
+ for (const g of b)
1464
+ if (g.kind === "patch") {
1465
+ const ae = D(t, this.blue);
1466
+ for (const C of ae) {
1467
+ const re = g.patch.op === "move" || g.patch.op === "copy" ? [g.patch.from, g.patch.path] : [g.patch.path], j = E.getNodePath(), se = re.some(
1468
+ (ie) => F(ie, C.absPath)
1469
+ ), ce = F(
1470
+ j,
1471
+ C.absPath
1376
1472
  );
1377
- if (ne && !oe)
1378
- throw new z(
1379
- y.patch,
1380
- S.absPath,
1381
- $
1473
+ if (se && !ce)
1474
+ throw new ee(
1475
+ g.patch,
1476
+ C.absPath,
1477
+ j
1382
1478
  );
1383
1479
  }
1384
1480
  try {
1385
- t = j(t, [y.patch]);
1386
- } catch (S) {
1387
- throw qe(u, d, S), S;
1481
+ t = J(t, [g.patch]);
1482
+ } catch (C) {
1483
+ throw De(u, d, C), C;
1388
1484
  }
1389
- } else y.kind === "event" && (n.push(y.event.payload), await this.router.route(t, [], y.event, s.key[5]));
1485
+ } else g.kind === "event" && (n.push(g.event.payload), await this.router.route(t, [], g.event, s.key[5]));
1390
1486
  }
1391
1487
  );
1392
- await m.handle(d, h, w, u), await w.flush();
1488
+ await m.handle(d, h, E, u), await E.flush();
1393
1489
  }
1394
1490
  return { state: t, emitted: n };
1395
1491
  }
1396
1492
  }
1397
1493
  export {
1398
- tt as BlueDocumentProcessor,
1399
- $e as ChannelEventCheckpointProcessor,
1400
- xe as CompositeTimelineChannelProcessor,
1401
- De as DocumentUpdateChannelProcessor,
1402
- We as EmbeddedNodeChannelProcessor,
1403
- Fe as MyOSAgentChannelProcessor,
1404
- je as MyOSTimelineChannelProcessor,
1405
- Ue as OperationProcessor,
1406
- _e as ProcessEmbeddedProcessor,
1407
- He as SequentialWorkflowOperationProcessor,
1408
- Z as SequentialWorkflowProcessor,
1409
- Qe as TimelineChannelProcessor
1494
+ lt as BlueDocumentProcessor,
1495
+ xe as ChannelEventCheckpointProcessor,
1496
+ Ve as CompositeTimelineChannelProcessor,
1497
+ Fe as DocumentUpdateChannelProcessor,
1498
+ Je as EmbeddedNodeChannelProcessor,
1499
+ Le as InitializedMarkerProcessor,
1500
+ Ue as LifecycleEventChannelProcessor,
1501
+ He as MyOSAgentChannelProcessor,
1502
+ ze as MyOSTimelineChannelProcessor,
1503
+ Re as OperationProcessor,
1504
+ Qe as ProcessEmbeddedProcessor,
1505
+ et as SequentialWorkflowOperationProcessor,
1506
+ oe as SequentialWorkflowProcessor,
1507
+ tt as TimelineChannelProcessor
1410
1508
  };