@m4trix/core 0.14.0 → 0.15.1

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.
@@ -226,10 +226,7 @@ var runSubscriber = (agent, publishesTo, dequeue, plane, emitQueue, channelName)
226
226
  if (listensTo.length > 0 && !listensTo.includes(envelope.name)) {
227
227
  return;
228
228
  }
229
- const runEvents = plane.getRunEvents(
230
- envelope.meta.runId,
231
- envelope.meta.contextId
232
- );
229
+ const runEvents = plane.getRunEvents(envelope.meta.runId, envelope.meta.contextId);
233
230
  const contextEvents = plane.getContextEvents(envelope.meta.contextId);
234
231
  yield* effect.Effect.withSpan("agent.listen", {
235
232
  attributes: {
@@ -264,9 +261,7 @@ var runSubscriber = (agent, publishesTo, dequeue, plane, emitQueue, channelName)
264
261
  ).catch(() => {
265
262
  });
266
263
  } else {
267
- effect.Effect.runFork(
268
- plane.publishToChannels(publishesTo, fullEnvelope)
269
- );
264
+ effect.Effect.runFork(plane.publishToChannels(publishesTo, fullEnvelope));
270
265
  }
271
266
  },
272
267
  runEvents,
@@ -292,14 +287,7 @@ var run = (network, plane, options) => effect.Effect.gen(function* () {
292
287
  for (const reg of registrations.values()) {
293
288
  for (const channel of reg.subscribedTo) {
294
289
  const dequeue = yield* plane.subscribe(channel.name);
295
- yield* runSubscriber(
296
- reg.agent,
297
- reg.publishesTo,
298
- dequeue,
299
- plane,
300
- emitQueue,
301
- channel.name
302
- );
290
+ yield* runSubscriber(reg.agent, reg.publishesTo, dequeue, plane, emitQueue, channel.name);
303
291
  }
304
292
  }
305
293
  yield* effect.Effect.never;
@@ -367,14 +355,7 @@ function streamFromDequeue(take, signal, eventFilter) {
367
355
  };
368
356
  }
369
357
  function expose(network, options) {
370
- const {
371
- auth,
372
- select,
373
- plane: providedPlane,
374
- onRequest,
375
- triggerEvents,
376
- tracingLayer
377
- } = options;
358
+ const { auth, select, plane: providedPlane, onRequest, triggerEvents, tracingLayer } = options;
378
359
  const triggerEventDef = triggerEvents?.[0];
379
360
  const triggerEventName = triggerEventDef?.name ?? "request";
380
361
  const channels = resolveChannels(network, select);
@@ -421,10 +402,8 @@ function expose(network, options) {
421
402
  meta,
422
403
  payload: opts.event.payload
423
404
  };
424
- effect.Effect.runPromise(plane.publish(targetChannel, envelope)).catch(
425
- () => {
426
- }
427
- );
405
+ effect.Effect.runPromise(plane.publish(targetChannel, envelope)).catch(() => {
406
+ });
428
407
  };
429
408
  const dequeue = yield* plane.subscribe(channels[0]);
430
409
  if (onRequest) {
@@ -464,10 +443,7 @@ function expose(network, options) {
464
443
  if (auth) {
465
444
  const result = await auth(req);
466
445
  if (!result.allowed) {
467
- throw new ExposeAuthError(
468
- result.message ?? "Unauthorized",
469
- result.status ?? 401
470
- );
446
+ throw new ExposeAuthError(result.message ?? "Unauthorized", result.status ?? 401);
471
447
  }
472
448
  }
473
449
  return consumer ? createStream(req, consumer) : createStream(req);
@@ -502,6 +478,7 @@ var AgentNetwork = class _AgentNetwork {
502
478
  createChannel: (name) => network.addChannel(name),
503
479
  sink: Sink,
504
480
  registerAgent: (agent) => network.registerAgentInternal(agent),
481
+ registerAggregator: (aggregator) => network.registerAggregatorInternal(aggregator),
505
482
  spawner: (factory) => network.createSpawnerInternal(factory)
506
483
  };
507
484
  callback(ctx);
@@ -536,6 +513,9 @@ var AgentNetwork = class _AgentNetwork {
536
513
  };
537
514
  return binding;
538
515
  }
516
+ registerAggregatorInternal(aggregator) {
517
+ return this.registerAgentInternal(aggregator);
518
+ }
539
519
  createSpawnerInternal(factoryClass) {
540
520
  const reg = {
541
521
  factoryClass,
@@ -640,7 +620,9 @@ var AgentNetworkEvent = {
640
620
  const makeBound = (meta, payload2) => effect.Effect.runSync(
641
621
  decodeEnvelope({ name, meta, payload: payload2 })
642
622
  );
643
- const makeEffect = (payload2) => decodePayload(payload2).pipe(effect.Effect.map((p) => ({ name, payload: p })));
623
+ const makeEffect = (payload2) => decodePayload(payload2).pipe(
624
+ effect.Effect.map((p) => ({ name, payload: p }))
625
+ );
644
626
  const makeBoundEffect = (meta, payload2) => decodeEnvelope({ name, meta, payload: payload2 });
645
627
  const is = effect.Schema.is(envelopeSchema);
646
628
  return {
@@ -657,20 +639,110 @@ var AgentNetworkEvent = {
657
639
  };
658
640
  }
659
641
  };
660
- var _params, _logic, _id, _listensTo;
642
+ var EventAggregator = class _EventAggregator {
643
+ constructor({
644
+ listensTo = [],
645
+ emits = [],
646
+ emitWhen
647
+ }) {
648
+ this._listensTo = listensTo;
649
+ this._emits = emits;
650
+ this._emitWhen = emitWhen;
651
+ }
652
+ static listensTo(events) {
653
+ return new _EventAggregator({ listensTo: [...events] });
654
+ }
655
+ emits(events) {
656
+ return new _EventAggregator({
657
+ listensTo: this._listensTo,
658
+ emits: [...this._emits, ...events],
659
+ emitWhen: this._emitWhen
660
+ });
661
+ }
662
+ emitWhen(fn) {
663
+ return new _EventAggregator({
664
+ listensTo: this._listensTo,
665
+ emits: this._emits,
666
+ emitWhen: fn
667
+ });
668
+ }
669
+ mapToEmit(fn) {
670
+ return new EventAggregatorInstance({
671
+ listensTo: this._listensTo.map((eventDef) => eventDef.name),
672
+ emitWhen: this._emitWhen ?? (() => true),
673
+ mapToEmit: fn
674
+ });
675
+ }
676
+ };
677
+ var _id, _listensTo, _emitWhen, _mapToEmit;
678
+ var EventAggregatorInstance = class {
679
+ constructor({
680
+ listensTo,
681
+ emitWhen,
682
+ mapToEmit
683
+ }) {
684
+ __privateAdd(this, _id, void 0);
685
+ __privateAdd(this, _listensTo, void 0);
686
+ __privateAdd(this, _emitWhen, void 0);
687
+ __privateAdd(this, _mapToEmit, void 0);
688
+ __privateSet(this, _id, `event-aggregator-${crypto$1.randomUUID()}`);
689
+ __privateSet(this, _listensTo, listensTo);
690
+ __privateSet(this, _emitWhen, emitWhen);
691
+ __privateSet(this, _mapToEmit, mapToEmit);
692
+ }
693
+ getId() {
694
+ return __privateGet(this, _id);
695
+ }
696
+ getListensTo() {
697
+ return __privateGet(this, _listensTo);
698
+ }
699
+ async invoke(options) {
700
+ const { triggerEvent, emit, runEvents, contextEvents } = options ?? {};
701
+ if (triggerEvent == null) {
702
+ return;
703
+ }
704
+ const emitFn = emit ?? ((_event) => {
705
+ });
706
+ const runEventsValue = runEvents ?? [];
707
+ const contextEventsValue = contextEvents ?? {
708
+ all: [],
709
+ byRun: () => [],
710
+ map: /* @__PURE__ */ new Map()
711
+ };
712
+ const shouldEmit = await __privateGet(this, _emitWhen).call(this, {
713
+ triggerEvent,
714
+ runEvents: runEventsValue,
715
+ contextEvents: contextEventsValue
716
+ });
717
+ if (!shouldEmit) {
718
+ return;
719
+ }
720
+ await __privateGet(this, _mapToEmit).call(this, {
721
+ triggerEvent,
722
+ emit: emitFn,
723
+ runEvents: runEventsValue,
724
+ contextEvents: contextEventsValue
725
+ });
726
+ }
727
+ };
728
+ _id = new WeakMap();
729
+ _listensTo = new WeakMap();
730
+ _emitWhen = new WeakMap();
731
+ _mapToEmit = new WeakMap();
732
+ var _params, _logic, _id2, _listensTo2;
661
733
  var Agent = class {
662
734
  constructor(logic, params, listensTo) {
663
735
  __privateAdd(this, _params, void 0);
664
736
  __privateAdd(this, _logic, void 0);
665
- __privateAdd(this, _id, void 0);
666
- __privateAdd(this, _listensTo, void 0);
737
+ __privateAdd(this, _id2, void 0);
738
+ __privateAdd(this, _listensTo2, void 0);
667
739
  __privateSet(this, _logic, logic);
668
740
  __privateSet(this, _params, params);
669
- __privateSet(this, _id, `agent-${crypto$1.randomUUID()}`);
670
- __privateSet(this, _listensTo, listensTo ?? []);
741
+ __privateSet(this, _id2, `agent-${crypto$1.randomUUID()}`);
742
+ __privateSet(this, _listensTo2, listensTo ?? []);
671
743
  }
672
744
  getListensTo() {
673
- return __privateGet(this, _listensTo);
745
+ return __privateGet(this, _listensTo2);
674
746
  }
675
747
  async invoke(options) {
676
748
  const { triggerEvent, emit, runEvents, contextEvents } = options ?? {};
@@ -689,13 +761,13 @@ var Agent = class {
689
761
  });
690
762
  }
691
763
  getId() {
692
- return __privateGet(this, _id);
764
+ return __privateGet(this, _id2);
693
765
  }
694
766
  };
695
767
  _params = new WeakMap();
696
768
  _logic = new WeakMap();
697
- _id = new WeakMap();
698
- _listensTo = new WeakMap();
769
+ _id2 = new WeakMap();
770
+ _listensTo2 = new WeakMap();
699
771
 
700
772
  // src/matrix/agent-factory.ts
701
773
  var AgentFactory = class _AgentFactory {
@@ -880,9 +952,7 @@ var Skill = class _Skill {
880
952
  const layersObj = runtime?.layers ?? {};
881
953
  const chunks = [];
882
954
  const emit = (chunk) => {
883
- const decoded = effect.Effect.runSync(
884
- decodeChunk(chunk)
885
- );
955
+ const decoded = effect.Effect.runSync(decodeChunk(chunk));
886
956
  chunks.push(decoded);
887
957
  };
888
958
  const done = await defineFn({
@@ -890,9 +960,7 @@ var Skill = class _Skill {
890
960
  emit,
891
961
  layers: layersObj
892
962
  });
893
- const decodedDone = effect.Effect.runSync(
894
- decodeDone(done)
895
- );
963
+ const decodedDone = effect.Effect.runSync(decodeDone(done));
896
964
  return { chunks, done: decodedDone };
897
965
  };
898
966
  return {
@@ -903,9 +971,7 @@ var Skill = class _Skill {
903
971
  const layersObj = runtime?.layers ?? {};
904
972
  const chunks = [];
905
973
  const emit = (chunk) => {
906
- const decoded = effect.Effect.runSync(
907
- decodeChunk(chunk)
908
- );
974
+ const decoded = effect.Effect.runSync(decodeChunk(chunk));
909
975
  chunks.push(decoded);
910
976
  };
911
977
  const done = await defineFn({
@@ -913,9 +979,7 @@ var Skill = class _Skill {
913
979
  emit,
914
980
  layers: layersObj
915
981
  });
916
- const decodedDone = effect.Effect.runSync(
917
- decodeDone(done)
918
- );
982
+ const decodedDone = effect.Effect.runSync(decodeDone(done));
919
983
  for (const c of chunks) {
920
984
  yield c;
921
985
  }
@@ -1020,9 +1084,7 @@ var NextEndpoint = {
1020
1084
  var ExpressEndpoint = {
1021
1085
  from(api, options) {
1022
1086
  if (api.protocol !== "sse") {
1023
- throw new Error(
1024
- `ExpressEndpoint: unsupported protocol "${api.protocol}"`
1025
- );
1087
+ throw new Error(`ExpressEndpoint: unsupported protocol "${api.protocol}"`);
1026
1088
  }
1027
1089
  const { requestToContextId, requestToRunId } = options;
1028
1090
  return {
@@ -1125,20 +1187,10 @@ function getDepth(parent) {
1125
1187
  return 1 + getDepth(p.parent);
1126
1188
  }
1127
1189
  var consoleTracer = effect.Tracer.make({
1128
- span: (name, parent, context, links, startTime, kind) => new ConsoleSpan(
1129
- name,
1130
- parent,
1131
- context,
1132
- links,
1133
- startTime,
1134
- kind,
1135
- getDepth(parent)
1136
- ),
1190
+ span: (name, parent, context, links, startTime, kind) => new ConsoleSpan(name, parent, context, links, startTime, kind, getDepth(parent)),
1137
1191
  context: (f) => f()
1138
1192
  });
1139
- var consoleTracerLayer = effect.Layer.setTracer(
1140
- consoleTracer
1141
- );
1193
+ var consoleTracerLayer = effect.Layer.setTracer(consoleTracer);
1142
1194
 
1143
1195
  Object.defineProperty(exports, 'S', {
1144
1196
  enumerable: true,
@@ -1153,6 +1205,8 @@ exports.ChannelName = ChannelName;
1153
1205
  exports.ConfiguredChannel = ConfiguredChannel;
1154
1206
  exports.DepedencyLayer = DepedencyLayer;
1155
1207
  exports.Done = Done;
1208
+ exports.EventAggregator = EventAggregator;
1209
+ exports.EventAggregatorInstance = EventAggregatorInstance;
1156
1210
  exports.EventMetaSchema = EventMetaSchema;
1157
1211
  exports.ExposeAuthError = ExposeAuthError;
1158
1212
  exports.ExpressEndpoint = ExpressEndpoint;