@m4trix/core 0.15.0 → 0.15.2
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.
- package/dist/index.cjs +367 -413
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +367 -413
- package/dist/index.js.map +1 -1
- package/dist/matrix/index.cjs +365 -405
- package/dist/matrix/index.cjs.map +1 -1
- package/dist/matrix/index.d.ts +103 -101
- package/dist/matrix/index.js +365 -405
- package/dist/matrix/index.js.map +1 -1
- package/package.json +3 -7
package/dist/matrix/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { randomUUID } from 'crypto';
|
|
1
2
|
import { Brand, Schema, Tracer, Layer, Effect, Exit, PubSub, Queue, Cause } from 'effect';
|
|
2
3
|
export { Schema as S } from 'effect';
|
|
3
|
-
import { randomUUID } from 'crypto';
|
|
4
4
|
|
|
5
5
|
var __accessCheck = (obj, member, msg) => {
|
|
6
6
|
if (!member.has(obj))
|
|
@@ -20,56 +20,114 @@ var __privateSet = (obj, member, value, setter) => {
|
|
|
20
20
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
21
21
|
return value;
|
|
22
22
|
};
|
|
23
|
-
var
|
|
24
|
-
var
|
|
25
|
-
(
|
|
26
|
-
|
|
27
|
-
);
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
23
|
+
var _params, _logic, _id, _listensTo;
|
|
24
|
+
var Agent = class {
|
|
25
|
+
constructor(logic, params, listensTo) {
|
|
26
|
+
__privateAdd(this, _params, void 0);
|
|
27
|
+
__privateAdd(this, _logic, void 0);
|
|
28
|
+
__privateAdd(this, _id, void 0);
|
|
29
|
+
__privateAdd(this, _listensTo, void 0);
|
|
30
|
+
__privateSet(this, _logic, logic);
|
|
31
|
+
__privateSet(this, _params, params);
|
|
32
|
+
__privateSet(this, _id, `agent-${randomUUID()}`);
|
|
33
|
+
__privateSet(this, _listensTo, listensTo ?? []);
|
|
34
|
+
}
|
|
35
|
+
getListensTo() {
|
|
36
|
+
return __privateGet(this, _listensTo);
|
|
37
|
+
}
|
|
38
|
+
async invoke(options) {
|
|
39
|
+
const { triggerEvent, emit, runEvents, contextEvents } = options ?? {};
|
|
40
|
+
const emitFn = emit ?? ((_event) => {
|
|
41
|
+
});
|
|
42
|
+
await __privateGet(this, _logic).call(this, {
|
|
43
|
+
params: __privateGet(this, _params),
|
|
44
|
+
triggerEvent: triggerEvent ?? void 0,
|
|
45
|
+
emit: emitFn,
|
|
46
|
+
runEvents: runEvents ?? [],
|
|
47
|
+
contextEvents: contextEvents ?? {
|
|
48
|
+
all: [],
|
|
49
|
+
byRun: () => [],
|
|
50
|
+
map: /* @__PURE__ */ new Map()
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
getId() {
|
|
55
|
+
return __privateGet(this, _id);
|
|
36
56
|
}
|
|
37
57
|
};
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
58
|
+
_params = new WeakMap();
|
|
59
|
+
_logic = new WeakMap();
|
|
60
|
+
_id = new WeakMap();
|
|
61
|
+
_listensTo = new WeakMap();
|
|
62
|
+
|
|
63
|
+
// src/matrix/agent-factory.ts
|
|
64
|
+
var AgentFactory = class _AgentFactory {
|
|
65
|
+
constructor({
|
|
66
|
+
logic,
|
|
67
|
+
paramsSchema,
|
|
68
|
+
listensTo = [],
|
|
69
|
+
emits = []
|
|
70
|
+
}) {
|
|
71
|
+
this._logic = logic;
|
|
72
|
+
this._paramsSchema = paramsSchema;
|
|
73
|
+
this._listensTo = listensTo;
|
|
74
|
+
this._emits = emits;
|
|
47
75
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
76
|
+
getConstructorState() {
|
|
77
|
+
return {
|
|
78
|
+
logic: this._logic,
|
|
79
|
+
paramsSchema: this._paramsSchema,
|
|
80
|
+
listensTo: this._listensTo,
|
|
81
|
+
emits: this._emits
|
|
82
|
+
};
|
|
51
83
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
return this;
|
|
84
|
+
/** Union of all event definitions this agent listens to */
|
|
85
|
+
getListensTo() {
|
|
86
|
+
return this._listensTo;
|
|
55
87
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
return this;
|
|
88
|
+
/** Union of all event definitions this agent can emit */
|
|
89
|
+
getEmits() {
|
|
90
|
+
return this._emits;
|
|
59
91
|
}
|
|
60
|
-
|
|
61
|
-
return this.
|
|
92
|
+
getLogic() {
|
|
93
|
+
return this._logic;
|
|
62
94
|
}
|
|
63
|
-
|
|
64
|
-
return
|
|
95
|
+
static run() {
|
|
96
|
+
return new _AgentFactory({});
|
|
65
97
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
};
|
|
98
|
+
params(params) {
|
|
99
|
+
const { logic, ...rest } = this.getConstructorState();
|
|
100
|
+
return new _AgentFactory({
|
|
101
|
+
...rest,
|
|
102
|
+
logic,
|
|
103
|
+
paramsSchema: params
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
listensTo(events) {
|
|
107
|
+
return new _AgentFactory({
|
|
108
|
+
...this.getConstructorState(),
|
|
109
|
+
listensTo: [...this._listensTo, ...events]
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
emits(events) {
|
|
113
|
+
return new _AgentFactory({
|
|
114
|
+
...this.getConstructorState(),
|
|
115
|
+
emits: [...this._emits, ...events]
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
logic(fn) {
|
|
119
|
+
return new _AgentFactory({
|
|
120
|
+
...this.getConstructorState(),
|
|
121
|
+
logic: fn
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
produce(params) {
|
|
125
|
+
const listensTo = this._listensTo.map((e) => e.name);
|
|
126
|
+
return new Agent(
|
|
127
|
+
this._logic,
|
|
128
|
+
params,
|
|
129
|
+
listensTo
|
|
130
|
+
);
|
|
73
131
|
}
|
|
74
132
|
};
|
|
75
133
|
|
|
@@ -225,10 +283,7 @@ var runSubscriber = (agent, publishesTo, dequeue, plane, emitQueue, channelName)
|
|
|
225
283
|
if (listensTo.length > 0 && !listensTo.includes(envelope.name)) {
|
|
226
284
|
return;
|
|
227
285
|
}
|
|
228
|
-
const runEvents = plane.getRunEvents(
|
|
229
|
-
envelope.meta.runId,
|
|
230
|
-
envelope.meta.contextId
|
|
231
|
-
);
|
|
286
|
+
const runEvents = plane.getRunEvents(envelope.meta.runId, envelope.meta.contextId);
|
|
232
287
|
const contextEvents = plane.getContextEvents(envelope.meta.contextId);
|
|
233
288
|
yield* Effect.withSpan("agent.listen", {
|
|
234
289
|
attributes: {
|
|
@@ -263,9 +318,7 @@ var runSubscriber = (agent, publishesTo, dequeue, plane, emitQueue, channelName)
|
|
|
263
318
|
).catch(() => {
|
|
264
319
|
});
|
|
265
320
|
} else {
|
|
266
|
-
Effect.runFork(
|
|
267
|
-
plane.publishToChannels(publishesTo, fullEnvelope)
|
|
268
|
-
);
|
|
321
|
+
Effect.runFork(plane.publishToChannels(publishesTo, fullEnvelope));
|
|
269
322
|
}
|
|
270
323
|
},
|
|
271
324
|
runEvents,
|
|
@@ -291,18 +344,65 @@ var run = (network, plane, options) => Effect.gen(function* () {
|
|
|
291
344
|
for (const reg of registrations.values()) {
|
|
292
345
|
for (const channel of reg.subscribedTo) {
|
|
293
346
|
const dequeue = yield* plane.subscribe(channel.name);
|
|
294
|
-
yield* runSubscriber(
|
|
295
|
-
reg.agent,
|
|
296
|
-
reg.publishesTo,
|
|
297
|
-
dequeue,
|
|
298
|
-
plane,
|
|
299
|
-
emitQueue,
|
|
300
|
-
channel.name
|
|
301
|
-
);
|
|
347
|
+
yield* runSubscriber(reg.agent, reg.publishesTo, dequeue, plane, emitQueue, channel.name);
|
|
302
348
|
}
|
|
303
349
|
}
|
|
304
350
|
yield* Effect.never;
|
|
305
351
|
});
|
|
352
|
+
var KEBAB_CASE_REGEX = /^[a-z0-9]+(-[a-z0-9]+)*$/;
|
|
353
|
+
var ChannelName = Brand.refined(
|
|
354
|
+
(s) => typeof s === "string" && KEBAB_CASE_REGEX.test(s),
|
|
355
|
+
(s) => Brand.error(`Expected kebab-case (e.g. my-channel-name), got: ${s}`)
|
|
356
|
+
);
|
|
357
|
+
|
|
358
|
+
// src/matrix/agent-network/channel.ts
|
|
359
|
+
var Sink = {
|
|
360
|
+
kafka(config) {
|
|
361
|
+
return { _tag: "SinkDef", type: "kafka", config };
|
|
362
|
+
},
|
|
363
|
+
httpStream() {
|
|
364
|
+
return { _tag: "SinkDef", type: "http-stream", config: {} };
|
|
365
|
+
}
|
|
366
|
+
};
|
|
367
|
+
function isHttpStreamSink(sink) {
|
|
368
|
+
return sink.type === "http-stream";
|
|
369
|
+
}
|
|
370
|
+
var ConfiguredChannel = class {
|
|
371
|
+
constructor(name) {
|
|
372
|
+
this._tag = "ConfiguredChannel";
|
|
373
|
+
this._events = [];
|
|
374
|
+
this._sinks = [];
|
|
375
|
+
this.name = name;
|
|
376
|
+
}
|
|
377
|
+
events(events) {
|
|
378
|
+
this._events = [...events];
|
|
379
|
+
return this;
|
|
380
|
+
}
|
|
381
|
+
sink(sink) {
|
|
382
|
+
this._sinks = [...this._sinks, sink];
|
|
383
|
+
return this;
|
|
384
|
+
}
|
|
385
|
+
sinks(sinks) {
|
|
386
|
+
this._sinks = [...sinks];
|
|
387
|
+
return this;
|
|
388
|
+
}
|
|
389
|
+
getEvents() {
|
|
390
|
+
return this._events;
|
|
391
|
+
}
|
|
392
|
+
getSinks() {
|
|
393
|
+
return this._sinks;
|
|
394
|
+
}
|
|
395
|
+
};
|
|
396
|
+
var Channel = {
|
|
397
|
+
of(name) {
|
|
398
|
+
return {
|
|
399
|
+
_tag: "ChannelDef",
|
|
400
|
+
name
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
};
|
|
404
|
+
|
|
405
|
+
// src/matrix/io/expose.ts
|
|
306
406
|
async function extractPayload(req) {
|
|
307
407
|
const webRequest = req.request;
|
|
308
408
|
if (webRequest?.method === "POST") {
|
|
@@ -366,14 +466,7 @@ function streamFromDequeue(take, signal, eventFilter) {
|
|
|
366
466
|
};
|
|
367
467
|
}
|
|
368
468
|
function expose(network, options) {
|
|
369
|
-
const {
|
|
370
|
-
auth,
|
|
371
|
-
select,
|
|
372
|
-
plane: providedPlane,
|
|
373
|
-
onRequest,
|
|
374
|
-
triggerEvents,
|
|
375
|
-
tracingLayer
|
|
376
|
-
} = options;
|
|
469
|
+
const { auth, select, plane: providedPlane, onRequest, triggerEvents, tracingLayer } = options;
|
|
377
470
|
const triggerEventDef = triggerEvents?.[0];
|
|
378
471
|
const triggerEventName = triggerEventDef?.name ?? "request";
|
|
379
472
|
const channels = resolveChannels(network, select);
|
|
@@ -420,10 +513,8 @@ function expose(network, options) {
|
|
|
420
513
|
meta,
|
|
421
514
|
payload: opts.event.payload
|
|
422
515
|
};
|
|
423
|
-
Effect.runPromise(plane.publish(targetChannel, envelope)).catch(
|
|
424
|
-
|
|
425
|
-
}
|
|
426
|
-
);
|
|
516
|
+
Effect.runPromise(plane.publish(targetChannel, envelope)).catch(() => {
|
|
517
|
+
});
|
|
427
518
|
};
|
|
428
519
|
const dequeue = yield* plane.subscribe(channels[0]);
|
|
429
520
|
if (onRequest) {
|
|
@@ -463,10 +554,7 @@ function expose(network, options) {
|
|
|
463
554
|
if (auth) {
|
|
464
555
|
const result = await auth(req);
|
|
465
556
|
if (!result.allowed) {
|
|
466
|
-
throw new ExposeAuthError(
|
|
467
|
-
result.message ?? "Unauthorized",
|
|
468
|
-
result.status ?? 401
|
|
469
|
-
);
|
|
557
|
+
throw new ExposeAuthError(result.message ?? "Unauthorized", result.status ?? 401);
|
|
470
558
|
}
|
|
471
559
|
}
|
|
472
560
|
return consumer ? createStream(req, consumer) : createStream(req);
|
|
@@ -643,7 +731,9 @@ var AgentNetworkEvent = {
|
|
|
643
731
|
const makeBound = (meta, payload2) => Effect.runSync(
|
|
644
732
|
decodeEnvelope({ name, meta, payload: payload2 })
|
|
645
733
|
);
|
|
646
|
-
const makeEffect = (payload2) => decodePayload(payload2).pipe(
|
|
734
|
+
const makeEffect = (payload2) => decodePayload(payload2).pipe(
|
|
735
|
+
Effect.map((p) => ({ name, payload: p }))
|
|
736
|
+
);
|
|
647
737
|
const makeBoundEffect = (meta, payload2) => decodeEnvelope({ name, meta, payload: payload2 });
|
|
648
738
|
const is = Schema.is(envelopeSchema);
|
|
649
739
|
return {
|
|
@@ -695,27 +785,27 @@ var EventAggregator = class _EventAggregator {
|
|
|
695
785
|
});
|
|
696
786
|
}
|
|
697
787
|
};
|
|
698
|
-
var
|
|
788
|
+
var _id2, _listensTo2, _emitWhen, _mapToEmit;
|
|
699
789
|
var EventAggregatorInstance = class {
|
|
700
790
|
constructor({
|
|
701
791
|
listensTo,
|
|
702
792
|
emitWhen,
|
|
703
793
|
mapToEmit
|
|
704
794
|
}) {
|
|
705
|
-
__privateAdd(this,
|
|
706
|
-
__privateAdd(this,
|
|
795
|
+
__privateAdd(this, _id2, void 0);
|
|
796
|
+
__privateAdd(this, _listensTo2, void 0);
|
|
707
797
|
__privateAdd(this, _emitWhen, void 0);
|
|
708
798
|
__privateAdd(this, _mapToEmit, void 0);
|
|
709
|
-
__privateSet(this,
|
|
710
|
-
__privateSet(this,
|
|
799
|
+
__privateSet(this, _id2, `event-aggregator-${randomUUID()}`);
|
|
800
|
+
__privateSet(this, _listensTo2, listensTo);
|
|
711
801
|
__privateSet(this, _emitWhen, emitWhen);
|
|
712
802
|
__privateSet(this, _mapToEmit, mapToEmit);
|
|
713
803
|
}
|
|
714
804
|
getId() {
|
|
715
|
-
return __privateGet(this,
|
|
805
|
+
return __privateGet(this, _id2);
|
|
716
806
|
}
|
|
717
807
|
getListensTo() {
|
|
718
|
-
return __privateGet(this,
|
|
808
|
+
return __privateGet(this, _listensTo2);
|
|
719
809
|
}
|
|
720
810
|
async invoke(options) {
|
|
721
811
|
const { triggerEvent, emit, runEvents, contextEvents } = options ?? {};
|
|
@@ -746,118 +836,205 @@ var EventAggregatorInstance = class {
|
|
|
746
836
|
});
|
|
747
837
|
}
|
|
748
838
|
};
|
|
749
|
-
|
|
750
|
-
|
|
839
|
+
_id2 = new WeakMap();
|
|
840
|
+
_listensTo2 = new WeakMap();
|
|
751
841
|
_emitWhen = new WeakMap();
|
|
752
842
|
_mapToEmit = new WeakMap();
|
|
753
|
-
var
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
__privateAdd(this, _id2, void 0);
|
|
759
|
-
__privateAdd(this, _listensTo2, void 0);
|
|
760
|
-
__privateSet(this, _logic, logic);
|
|
761
|
-
__privateSet(this, _params, params);
|
|
762
|
-
__privateSet(this, _id2, `agent-${randomUUID()}`);
|
|
763
|
-
__privateSet(this, _listensTo2, listensTo ?? []);
|
|
764
|
-
}
|
|
765
|
-
getListensTo() {
|
|
766
|
-
return __privateGet(this, _listensTo2);
|
|
767
|
-
}
|
|
768
|
-
async invoke(options) {
|
|
769
|
-
const { triggerEvent, emit, runEvents, contextEvents } = options ?? {};
|
|
770
|
-
const emitFn = emit ?? ((_event) => {
|
|
771
|
-
});
|
|
772
|
-
await __privateGet(this, _logic).call(this, {
|
|
773
|
-
params: __privateGet(this, _params),
|
|
774
|
-
triggerEvent: triggerEvent ?? void 0,
|
|
775
|
-
emit: emitFn,
|
|
776
|
-
runEvents: runEvents ?? [],
|
|
777
|
-
contextEvents: contextEvents ?? {
|
|
778
|
-
all: [],
|
|
779
|
-
byRun: () => [],
|
|
780
|
-
map: /* @__PURE__ */ new Map()
|
|
781
|
-
}
|
|
782
|
-
});
|
|
783
|
-
}
|
|
784
|
-
getId() {
|
|
785
|
-
return __privateGet(this, _id2);
|
|
843
|
+
var randomHexString = (length) => {
|
|
844
|
+
const chars = "abcdef0123456789";
|
|
845
|
+
let result = "";
|
|
846
|
+
for (let i = 0; i < length; i++) {
|
|
847
|
+
result += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
786
848
|
}
|
|
849
|
+
return result;
|
|
787
850
|
};
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
this.
|
|
802
|
-
this.
|
|
803
|
-
this.
|
|
804
|
-
this._emits = emits;
|
|
805
|
-
}
|
|
806
|
-
getConstructorState() {
|
|
807
|
-
return {
|
|
808
|
-
logic: this._logic,
|
|
809
|
-
paramsSchema: this._paramsSchema,
|
|
810
|
-
listensTo: this._listensTo,
|
|
811
|
-
emits: this._emits
|
|
812
|
-
};
|
|
813
|
-
}
|
|
814
|
-
/** Union of all event definitions this agent listens to */
|
|
815
|
-
getListensTo() {
|
|
816
|
-
return this._listensTo;
|
|
817
|
-
}
|
|
818
|
-
/** Union of all event definitions this agent can emit */
|
|
819
|
-
getEmits() {
|
|
820
|
-
return this._emits;
|
|
821
|
-
}
|
|
822
|
-
getLogic() {
|
|
823
|
-
return this._logic;
|
|
851
|
+
var ConsoleSpan = class {
|
|
852
|
+
constructor(name, parent, context, links, startTime, kind, depth) {
|
|
853
|
+
this.name = name;
|
|
854
|
+
this.parent = parent;
|
|
855
|
+
this.context = context;
|
|
856
|
+
this.startTime = startTime;
|
|
857
|
+
this.kind = kind;
|
|
858
|
+
this.depth = depth;
|
|
859
|
+
this._tag = "Span";
|
|
860
|
+
this.sampled = true;
|
|
861
|
+
this.attributes = /* @__PURE__ */ new Map();
|
|
862
|
+
this.links = [];
|
|
863
|
+
this.traceId = parent._tag === "Some" ? parent.value.traceId : randomHexString(32);
|
|
864
|
+
this.spanId = randomHexString(16);
|
|
865
|
+
this.links = Array.from(links);
|
|
866
|
+
this.status = { _tag: "Started", startTime };
|
|
824
867
|
}
|
|
825
|
-
|
|
826
|
-
|
|
868
|
+
end(endTime, exit) {
|
|
869
|
+
if (this.status._tag === "Ended")
|
|
870
|
+
return;
|
|
871
|
+
const startTime = this.status.startTime;
|
|
872
|
+
const durationNs = endTime - startTime;
|
|
873
|
+
const durationMs = Number(durationNs) / 1e6;
|
|
874
|
+
const indent = " ".repeat(this.depth);
|
|
875
|
+
const attrs = Object.fromEntries(this.attributes);
|
|
876
|
+
const status = Exit.isSuccess(exit) ? "ok" : "error";
|
|
877
|
+
console.log(
|
|
878
|
+
`${indent}[trace] ${this.name} ${durationMs.toFixed(2)}ms (${status})`,
|
|
879
|
+
Object.keys(attrs).length > 0 ? attrs : ""
|
|
880
|
+
);
|
|
881
|
+
this.status = { _tag: "Ended", startTime, endTime, exit };
|
|
827
882
|
}
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
return new _AgentFactory({
|
|
831
|
-
...rest,
|
|
832
|
-
logic,
|
|
833
|
-
paramsSchema: params
|
|
834
|
-
});
|
|
883
|
+
attribute(key, value) {
|
|
884
|
+
this.attributes.set(key, value);
|
|
835
885
|
}
|
|
836
|
-
|
|
837
|
-
return new _AgentFactory({
|
|
838
|
-
...this.getConstructorState(),
|
|
839
|
-
listensTo: [...this._listensTo, ...events]
|
|
840
|
-
});
|
|
886
|
+
event(_name, _startTime, _attributes) {
|
|
841
887
|
}
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
...this.getConstructorState(),
|
|
845
|
-
emits: [...this._emits, ...events]
|
|
846
|
-
});
|
|
888
|
+
addLinks(links) {
|
|
889
|
+
this.links.push(...links);
|
|
847
890
|
}
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
891
|
+
};
|
|
892
|
+
function getDepth(parent) {
|
|
893
|
+
if (parent._tag === "None")
|
|
894
|
+
return 0;
|
|
895
|
+
const p = parent.value;
|
|
896
|
+
if (p._tag === "ExternalSpan")
|
|
897
|
+
return 0;
|
|
898
|
+
return 1 + getDepth(p.parent);
|
|
899
|
+
}
|
|
900
|
+
var consoleTracer = Tracer.make({
|
|
901
|
+
span: (name, parent, context, links, startTime, kind) => new ConsoleSpan(name, parent, context, links, startTime, kind, getDepth(parent)),
|
|
902
|
+
context: (f) => f()
|
|
903
|
+
});
|
|
904
|
+
var consoleTracerLayer = Layer.setTracer(consoleTracer);
|
|
905
|
+
|
|
906
|
+
// src/matrix/io/protocols/sse.ts
|
|
907
|
+
function formatSSE(envelope) {
|
|
908
|
+
const data = JSON.stringify(envelope);
|
|
909
|
+
return `event: ${envelope.name}
|
|
910
|
+
data: ${data}
|
|
911
|
+
|
|
912
|
+
`;
|
|
913
|
+
}
|
|
914
|
+
function toSSEStream(source, signal) {
|
|
915
|
+
const encoder = new TextEncoder();
|
|
916
|
+
return new ReadableStream({
|
|
917
|
+
async start(controller) {
|
|
918
|
+
const onAbort = () => controller.close();
|
|
919
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
920
|
+
try {
|
|
921
|
+
for await (const envelope of source) {
|
|
922
|
+
if (signal?.aborted)
|
|
923
|
+
break;
|
|
924
|
+
controller.enqueue(encoder.encode(formatSSE(envelope)));
|
|
925
|
+
}
|
|
926
|
+
} finally {
|
|
927
|
+
signal?.removeEventListener("abort", onAbort);
|
|
928
|
+
controller.close();
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
});
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
// src/matrix/io/adapters/next-endpoint.ts
|
|
935
|
+
var NextEndpoint = {
|
|
936
|
+
from(api, options) {
|
|
937
|
+
if (api.protocol !== "sse") {
|
|
938
|
+
throw new Error(`NextEndpoint: unsupported protocol "${api.protocol}"`);
|
|
939
|
+
}
|
|
940
|
+
const { requestToContextId, requestToRunId } = options;
|
|
941
|
+
return {
|
|
942
|
+
handler() {
|
|
943
|
+
return async (request) => {
|
|
944
|
+
const req = {
|
|
945
|
+
request,
|
|
946
|
+
contextId: requestToContextId(request),
|
|
947
|
+
runId: requestToRunId(request)
|
|
948
|
+
};
|
|
949
|
+
try {
|
|
950
|
+
const encoder = new TextEncoder();
|
|
951
|
+
const { readable, writable } = new TransformStream();
|
|
952
|
+
let consumerStarted;
|
|
953
|
+
const started = new Promise((resolve) => {
|
|
954
|
+
consumerStarted = resolve;
|
|
955
|
+
});
|
|
956
|
+
const streamDone = api.createStream(req, async (stream) => {
|
|
957
|
+
consumerStarted();
|
|
958
|
+
const writer = writable.getWriter();
|
|
959
|
+
try {
|
|
960
|
+
for await (const envelope of stream) {
|
|
961
|
+
if (request.signal?.aborted)
|
|
962
|
+
break;
|
|
963
|
+
await writer.write(encoder.encode(formatSSE(envelope)));
|
|
964
|
+
}
|
|
965
|
+
} finally {
|
|
966
|
+
await writer.close();
|
|
967
|
+
}
|
|
968
|
+
});
|
|
969
|
+
await Promise.race([started, streamDone]);
|
|
970
|
+
streamDone.catch(() => {
|
|
971
|
+
});
|
|
972
|
+
return new Response(readable, {
|
|
973
|
+
headers: {
|
|
974
|
+
"Content-Type": "text/event-stream",
|
|
975
|
+
"Cache-Control": "no-cache",
|
|
976
|
+
Connection: "keep-alive"
|
|
977
|
+
}
|
|
978
|
+
});
|
|
979
|
+
} catch (e) {
|
|
980
|
+
if (e instanceof ExposeAuthError) {
|
|
981
|
+
return new Response(e.message, { status: e.status });
|
|
982
|
+
}
|
|
983
|
+
throw e;
|
|
984
|
+
}
|
|
985
|
+
};
|
|
986
|
+
}
|
|
987
|
+
};
|
|
853
988
|
}
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
989
|
+
};
|
|
990
|
+
|
|
991
|
+
// src/matrix/io/adapters/express-endpoint.ts
|
|
992
|
+
var ExpressEndpoint = {
|
|
993
|
+
from(api, options) {
|
|
994
|
+
if (api.protocol !== "sse") {
|
|
995
|
+
throw new Error(`ExpressEndpoint: unsupported protocol "${api.protocol}"`);
|
|
996
|
+
}
|
|
997
|
+
const { requestToContextId, requestToRunId } = options;
|
|
998
|
+
return {
|
|
999
|
+
handler() {
|
|
1000
|
+
return async (req, res) => {
|
|
1001
|
+
const controller = new AbortController();
|
|
1002
|
+
req.on("close", () => controller.abort());
|
|
1003
|
+
const exposeReq = {
|
|
1004
|
+
request: { signal: controller.signal },
|
|
1005
|
+
req,
|
|
1006
|
+
res,
|
|
1007
|
+
contextId: requestToContextId(req),
|
|
1008
|
+
runId: requestToRunId(req)
|
|
1009
|
+
};
|
|
1010
|
+
try {
|
|
1011
|
+
const encoder = new TextEncoder();
|
|
1012
|
+
await api.createStream(exposeReq, async (stream) => {
|
|
1013
|
+
res.setHeader("Content-Type", "text/event-stream");
|
|
1014
|
+
res.setHeader("Cache-Control", "no-cache");
|
|
1015
|
+
res.setHeader("Connection", "keep-alive");
|
|
1016
|
+
res.flushHeaders?.();
|
|
1017
|
+
try {
|
|
1018
|
+
for await (const envelope of stream) {
|
|
1019
|
+
if (controller.signal.aborted)
|
|
1020
|
+
break;
|
|
1021
|
+
res.write(encoder.encode(formatSSE(envelope)));
|
|
1022
|
+
res.flush?.();
|
|
1023
|
+
}
|
|
1024
|
+
} finally {
|
|
1025
|
+
res.end();
|
|
1026
|
+
}
|
|
1027
|
+
});
|
|
1028
|
+
} catch (e) {
|
|
1029
|
+
if (e instanceof ExposeAuthError) {
|
|
1030
|
+
res.status(e.status).send(e.message);
|
|
1031
|
+
return;
|
|
1032
|
+
}
|
|
1033
|
+
throw e;
|
|
1034
|
+
}
|
|
1035
|
+
};
|
|
1036
|
+
}
|
|
1037
|
+
};
|
|
861
1038
|
}
|
|
862
1039
|
};
|
|
863
1040
|
var CAMEL_CASE_REGEX = /^[a-z][a-zA-Z0-9]*$/;
|
|
@@ -973,9 +1150,7 @@ var Skill = class _Skill {
|
|
|
973
1150
|
const layersObj = runtime?.layers ?? {};
|
|
974
1151
|
const chunks = [];
|
|
975
1152
|
const emit = (chunk) => {
|
|
976
|
-
const decoded = Effect.runSync(
|
|
977
|
-
decodeChunk(chunk)
|
|
978
|
-
);
|
|
1153
|
+
const decoded = Effect.runSync(decodeChunk(chunk));
|
|
979
1154
|
chunks.push(decoded);
|
|
980
1155
|
};
|
|
981
1156
|
const done = await defineFn({
|
|
@@ -983,9 +1158,7 @@ var Skill = class _Skill {
|
|
|
983
1158
|
emit,
|
|
984
1159
|
layers: layersObj
|
|
985
1160
|
});
|
|
986
|
-
const decodedDone = Effect.runSync(
|
|
987
|
-
decodeDone(done)
|
|
988
|
-
);
|
|
1161
|
+
const decodedDone = Effect.runSync(decodeDone(done));
|
|
989
1162
|
return { chunks, done: decodedDone };
|
|
990
1163
|
};
|
|
991
1164
|
return {
|
|
@@ -996,9 +1169,7 @@ var Skill = class _Skill {
|
|
|
996
1169
|
const layersObj = runtime?.layers ?? {};
|
|
997
1170
|
const chunks = [];
|
|
998
1171
|
const emit = (chunk) => {
|
|
999
|
-
const decoded = Effect.runSync(
|
|
1000
|
-
decodeChunk(chunk)
|
|
1001
|
-
);
|
|
1172
|
+
const decoded = Effect.runSync(decodeChunk(chunk));
|
|
1002
1173
|
chunks.push(decoded);
|
|
1003
1174
|
};
|
|
1004
1175
|
const done = await defineFn({
|
|
@@ -1006,9 +1177,7 @@ var Skill = class _Skill {
|
|
|
1006
1177
|
emit,
|
|
1007
1178
|
layers: layersObj
|
|
1008
1179
|
});
|
|
1009
|
-
const decodedDone = Effect.runSync(
|
|
1010
|
-
decodeDone(done)
|
|
1011
|
-
);
|
|
1180
|
+
const decodedDone = Effect.runSync(decodeDone(done));
|
|
1012
1181
|
for (const c of chunks) {
|
|
1013
1182
|
yield c;
|
|
1014
1183
|
}
|
|
@@ -1024,215 +1193,6 @@ var Skill = class _Skill {
|
|
|
1024
1193
|
}
|
|
1025
1194
|
};
|
|
1026
1195
|
|
|
1027
|
-
// src/matrix/io/protocols/sse.ts
|
|
1028
|
-
function formatSSE(envelope) {
|
|
1029
|
-
const data = JSON.stringify(envelope);
|
|
1030
|
-
return `event: ${envelope.name}
|
|
1031
|
-
data: ${data}
|
|
1032
|
-
|
|
1033
|
-
`;
|
|
1034
|
-
}
|
|
1035
|
-
function toSSEStream(source, signal) {
|
|
1036
|
-
const encoder = new TextEncoder();
|
|
1037
|
-
return new ReadableStream({
|
|
1038
|
-
async start(controller) {
|
|
1039
|
-
const onAbort = () => controller.close();
|
|
1040
|
-
signal?.addEventListener("abort", onAbort, { once: true });
|
|
1041
|
-
try {
|
|
1042
|
-
for await (const envelope of source) {
|
|
1043
|
-
if (signal?.aborted)
|
|
1044
|
-
break;
|
|
1045
|
-
controller.enqueue(encoder.encode(formatSSE(envelope)));
|
|
1046
|
-
}
|
|
1047
|
-
} finally {
|
|
1048
|
-
signal?.removeEventListener("abort", onAbort);
|
|
1049
|
-
controller.close();
|
|
1050
|
-
}
|
|
1051
|
-
}
|
|
1052
|
-
});
|
|
1053
|
-
}
|
|
1054
|
-
|
|
1055
|
-
// src/matrix/io/adapters/next-endpoint.ts
|
|
1056
|
-
var NextEndpoint = {
|
|
1057
|
-
from(api, options) {
|
|
1058
|
-
if (api.protocol !== "sse") {
|
|
1059
|
-
throw new Error(`NextEndpoint: unsupported protocol "${api.protocol}"`);
|
|
1060
|
-
}
|
|
1061
|
-
const { requestToContextId, requestToRunId } = options;
|
|
1062
|
-
return {
|
|
1063
|
-
handler() {
|
|
1064
|
-
return async (request) => {
|
|
1065
|
-
const req = {
|
|
1066
|
-
request,
|
|
1067
|
-
contextId: requestToContextId(request),
|
|
1068
|
-
runId: requestToRunId(request)
|
|
1069
|
-
};
|
|
1070
|
-
try {
|
|
1071
|
-
const encoder = new TextEncoder();
|
|
1072
|
-
const { readable, writable } = new TransformStream();
|
|
1073
|
-
let consumerStarted;
|
|
1074
|
-
const started = new Promise((resolve) => {
|
|
1075
|
-
consumerStarted = resolve;
|
|
1076
|
-
});
|
|
1077
|
-
const streamDone = api.createStream(req, async (stream) => {
|
|
1078
|
-
consumerStarted();
|
|
1079
|
-
const writer = writable.getWriter();
|
|
1080
|
-
try {
|
|
1081
|
-
for await (const envelope of stream) {
|
|
1082
|
-
if (request.signal?.aborted)
|
|
1083
|
-
break;
|
|
1084
|
-
await writer.write(encoder.encode(formatSSE(envelope)));
|
|
1085
|
-
}
|
|
1086
|
-
} finally {
|
|
1087
|
-
await writer.close();
|
|
1088
|
-
}
|
|
1089
|
-
});
|
|
1090
|
-
await Promise.race([started, streamDone]);
|
|
1091
|
-
streamDone.catch(() => {
|
|
1092
|
-
});
|
|
1093
|
-
return new Response(readable, {
|
|
1094
|
-
headers: {
|
|
1095
|
-
"Content-Type": "text/event-stream",
|
|
1096
|
-
"Cache-Control": "no-cache",
|
|
1097
|
-
Connection: "keep-alive"
|
|
1098
|
-
}
|
|
1099
|
-
});
|
|
1100
|
-
} catch (e) {
|
|
1101
|
-
if (e instanceof ExposeAuthError) {
|
|
1102
|
-
return new Response(e.message, { status: e.status });
|
|
1103
|
-
}
|
|
1104
|
-
throw e;
|
|
1105
|
-
}
|
|
1106
|
-
};
|
|
1107
|
-
}
|
|
1108
|
-
};
|
|
1109
|
-
}
|
|
1110
|
-
};
|
|
1111
|
-
|
|
1112
|
-
// src/matrix/io/adapters/express-endpoint.ts
|
|
1113
|
-
var ExpressEndpoint = {
|
|
1114
|
-
from(api, options) {
|
|
1115
|
-
if (api.protocol !== "sse") {
|
|
1116
|
-
throw new Error(
|
|
1117
|
-
`ExpressEndpoint: unsupported protocol "${api.protocol}"`
|
|
1118
|
-
);
|
|
1119
|
-
}
|
|
1120
|
-
const { requestToContextId, requestToRunId } = options;
|
|
1121
|
-
return {
|
|
1122
|
-
handler() {
|
|
1123
|
-
return async (req, res) => {
|
|
1124
|
-
const controller = new AbortController();
|
|
1125
|
-
req.on("close", () => controller.abort());
|
|
1126
|
-
const exposeReq = {
|
|
1127
|
-
request: { signal: controller.signal },
|
|
1128
|
-
req,
|
|
1129
|
-
res,
|
|
1130
|
-
contextId: requestToContextId(req),
|
|
1131
|
-
runId: requestToRunId(req)
|
|
1132
|
-
};
|
|
1133
|
-
try {
|
|
1134
|
-
const encoder = new TextEncoder();
|
|
1135
|
-
await api.createStream(exposeReq, async (stream) => {
|
|
1136
|
-
res.setHeader("Content-Type", "text/event-stream");
|
|
1137
|
-
res.setHeader("Cache-Control", "no-cache");
|
|
1138
|
-
res.setHeader("Connection", "keep-alive");
|
|
1139
|
-
res.flushHeaders?.();
|
|
1140
|
-
try {
|
|
1141
|
-
for await (const envelope of stream) {
|
|
1142
|
-
if (controller.signal.aborted)
|
|
1143
|
-
break;
|
|
1144
|
-
res.write(encoder.encode(formatSSE(envelope)));
|
|
1145
|
-
res.flush?.();
|
|
1146
|
-
}
|
|
1147
|
-
} finally {
|
|
1148
|
-
res.end();
|
|
1149
|
-
}
|
|
1150
|
-
});
|
|
1151
|
-
} catch (e) {
|
|
1152
|
-
if (e instanceof ExposeAuthError) {
|
|
1153
|
-
res.status(e.status).send(e.message);
|
|
1154
|
-
return;
|
|
1155
|
-
}
|
|
1156
|
-
throw e;
|
|
1157
|
-
}
|
|
1158
|
-
};
|
|
1159
|
-
}
|
|
1160
|
-
};
|
|
1161
|
-
}
|
|
1162
|
-
};
|
|
1163
|
-
var randomHexString = (length) => {
|
|
1164
|
-
const chars = "abcdef0123456789";
|
|
1165
|
-
let result = "";
|
|
1166
|
-
for (let i = 0; i < length; i++) {
|
|
1167
|
-
result += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
1168
|
-
}
|
|
1169
|
-
return result;
|
|
1170
|
-
};
|
|
1171
|
-
var ConsoleSpan = class {
|
|
1172
|
-
constructor(name, parent, context, links, startTime, kind, depth) {
|
|
1173
|
-
this.name = name;
|
|
1174
|
-
this.parent = parent;
|
|
1175
|
-
this.context = context;
|
|
1176
|
-
this.startTime = startTime;
|
|
1177
|
-
this.kind = kind;
|
|
1178
|
-
this.depth = depth;
|
|
1179
|
-
this._tag = "Span";
|
|
1180
|
-
this.sampled = true;
|
|
1181
|
-
this.attributes = /* @__PURE__ */ new Map();
|
|
1182
|
-
this.links = [];
|
|
1183
|
-
this.traceId = parent._tag === "Some" ? parent.value.traceId : randomHexString(32);
|
|
1184
|
-
this.spanId = randomHexString(16);
|
|
1185
|
-
this.links = Array.from(links);
|
|
1186
|
-
this.status = { _tag: "Started", startTime };
|
|
1187
|
-
}
|
|
1188
|
-
end(endTime, exit) {
|
|
1189
|
-
if (this.status._tag === "Ended")
|
|
1190
|
-
return;
|
|
1191
|
-
const startTime = this.status.startTime;
|
|
1192
|
-
const durationNs = endTime - startTime;
|
|
1193
|
-
const durationMs = Number(durationNs) / 1e6;
|
|
1194
|
-
const indent = " ".repeat(this.depth);
|
|
1195
|
-
const attrs = Object.fromEntries(this.attributes);
|
|
1196
|
-
const status = Exit.isSuccess(exit) ? "ok" : "error";
|
|
1197
|
-
console.log(
|
|
1198
|
-
`${indent}[trace] ${this.name} ${durationMs.toFixed(2)}ms (${status})`,
|
|
1199
|
-
Object.keys(attrs).length > 0 ? attrs : ""
|
|
1200
|
-
);
|
|
1201
|
-
this.status = { _tag: "Ended", startTime, endTime, exit };
|
|
1202
|
-
}
|
|
1203
|
-
attribute(key, value) {
|
|
1204
|
-
this.attributes.set(key, value);
|
|
1205
|
-
}
|
|
1206
|
-
event(_name, _startTime, _attributes) {
|
|
1207
|
-
}
|
|
1208
|
-
addLinks(links) {
|
|
1209
|
-
this.links.push(...links);
|
|
1210
|
-
}
|
|
1211
|
-
};
|
|
1212
|
-
function getDepth(parent) {
|
|
1213
|
-
if (parent._tag === "None")
|
|
1214
|
-
return 0;
|
|
1215
|
-
const p = parent.value;
|
|
1216
|
-
if (p._tag === "ExternalSpan")
|
|
1217
|
-
return 0;
|
|
1218
|
-
return 1 + getDepth(p.parent);
|
|
1219
|
-
}
|
|
1220
|
-
var consoleTracer = Tracer.make({
|
|
1221
|
-
span: (name, parent, context, links, startTime, kind) => new ConsoleSpan(
|
|
1222
|
-
name,
|
|
1223
|
-
parent,
|
|
1224
|
-
context,
|
|
1225
|
-
links,
|
|
1226
|
-
startTime,
|
|
1227
|
-
kind,
|
|
1228
|
-
getDepth(parent)
|
|
1229
|
-
),
|
|
1230
|
-
context: (f) => f()
|
|
1231
|
-
});
|
|
1232
|
-
var consoleTracerLayer = Layer.setTracer(
|
|
1233
|
-
consoleTracer
|
|
1234
|
-
);
|
|
1235
|
-
|
|
1236
1196
|
export { Agent, AgentFactory, AgentNetwork, AgentNetworkEvent, Channel, ChannelName, ConfiguredChannel, DepedencyLayer, Done, EventAggregator, EventAggregatorInstance, EventMetaSchema, ExposeAuthError, ExpressEndpoint, LayerName, NextEndpoint, Sink, Skill, consoleTracer, consoleTracerLayer, formatSSE, isHttpStreamSink, toSSEStream };
|
|
1237
1197
|
//# sourceMappingURL=out.js.map
|
|
1238
1198
|
//# sourceMappingURL=index.js.map
|