@m4trix/core 0.15.1 → 0.16.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.
- package/dist/index.cjs +358 -354
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +358 -354
- package/dist/index.js.map +1 -1
- package/dist/matrix/index.cjs +358 -354
- package/dist/matrix/index.cjs.map +1 -1
- package/dist/matrix/index.d.ts +104 -104
- package/dist/matrix/index.js +358 -354
- package/dist/matrix/index.js.map +1 -1
- package/package.json +1 -1
package/dist/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))
|
|
@@ -92,56 +92,114 @@ var SocketIoFactory = class _SocketIoFactory {
|
|
|
92
92
|
return this.prefix ? `${this.prefix}:${event}` : event;
|
|
93
93
|
}
|
|
94
94
|
};
|
|
95
|
-
var
|
|
96
|
-
var
|
|
97
|
-
(
|
|
98
|
-
|
|
99
|
-
);
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
95
|
+
var _params, _logic, _id, _listensTo;
|
|
96
|
+
var Agent = class {
|
|
97
|
+
constructor(logic, params, listensTo) {
|
|
98
|
+
__privateAdd(this, _params, void 0);
|
|
99
|
+
__privateAdd(this, _logic, void 0);
|
|
100
|
+
__privateAdd(this, _id, void 0);
|
|
101
|
+
__privateAdd(this, _listensTo, void 0);
|
|
102
|
+
__privateSet(this, _logic, logic);
|
|
103
|
+
__privateSet(this, _params, params);
|
|
104
|
+
__privateSet(this, _id, `agent-${randomUUID()}`);
|
|
105
|
+
__privateSet(this, _listensTo, listensTo ?? []);
|
|
106
|
+
}
|
|
107
|
+
getListensTo() {
|
|
108
|
+
return __privateGet(this, _listensTo);
|
|
109
|
+
}
|
|
110
|
+
async invoke(options) {
|
|
111
|
+
const { triggerEvent, emit, runEvents, contextEvents } = options ?? {};
|
|
112
|
+
const emitFn = emit ?? ((_event) => {
|
|
113
|
+
});
|
|
114
|
+
await __privateGet(this, _logic).call(this, {
|
|
115
|
+
params: __privateGet(this, _params),
|
|
116
|
+
triggerEvent: triggerEvent ?? void 0,
|
|
117
|
+
emit: emitFn,
|
|
118
|
+
runEvents: runEvents ?? [],
|
|
119
|
+
contextEvents: contextEvents ?? {
|
|
120
|
+
all: [],
|
|
121
|
+
byRun: () => [],
|
|
122
|
+
map: /* @__PURE__ */ new Map()
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
getId() {
|
|
127
|
+
return __privateGet(this, _id);
|
|
108
128
|
}
|
|
109
129
|
};
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
130
|
+
_params = new WeakMap();
|
|
131
|
+
_logic = new WeakMap();
|
|
132
|
+
_id = new WeakMap();
|
|
133
|
+
_listensTo = new WeakMap();
|
|
134
|
+
|
|
135
|
+
// src/matrix/agent-factory.ts
|
|
136
|
+
var AgentFactory = class _AgentFactory {
|
|
137
|
+
constructor({
|
|
138
|
+
logic,
|
|
139
|
+
paramsSchema,
|
|
140
|
+
listensTo = [],
|
|
141
|
+
emits = []
|
|
142
|
+
}) {
|
|
143
|
+
this._logic = logic;
|
|
144
|
+
this._paramsSchema = paramsSchema;
|
|
145
|
+
this._listensTo = listensTo;
|
|
146
|
+
this._emits = emits;
|
|
119
147
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
148
|
+
getConstructorState() {
|
|
149
|
+
return {
|
|
150
|
+
logic: this._logic,
|
|
151
|
+
paramsSchema: this._paramsSchema,
|
|
152
|
+
listensTo: this._listensTo,
|
|
153
|
+
emits: this._emits
|
|
154
|
+
};
|
|
123
155
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
return this;
|
|
156
|
+
/** Union of all event definitions this agent listens to */
|
|
157
|
+
getListensTo() {
|
|
158
|
+
return this._listensTo;
|
|
127
159
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
return this;
|
|
160
|
+
/** Union of all event definitions this agent can emit */
|
|
161
|
+
getEmits() {
|
|
162
|
+
return this._emits;
|
|
131
163
|
}
|
|
132
|
-
|
|
133
|
-
return this.
|
|
164
|
+
getLogic() {
|
|
165
|
+
return this._logic;
|
|
134
166
|
}
|
|
135
|
-
|
|
136
|
-
return
|
|
167
|
+
static run() {
|
|
168
|
+
return new _AgentFactory({});
|
|
137
169
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
};
|
|
170
|
+
params(params) {
|
|
171
|
+
const { logic, ...rest } = this.getConstructorState();
|
|
172
|
+
return new _AgentFactory({
|
|
173
|
+
...rest,
|
|
174
|
+
logic,
|
|
175
|
+
paramsSchema: params
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
listensTo(events) {
|
|
179
|
+
return new _AgentFactory({
|
|
180
|
+
...this.getConstructorState(),
|
|
181
|
+
listensTo: [...this._listensTo, ...events]
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
emits(events) {
|
|
185
|
+
return new _AgentFactory({
|
|
186
|
+
...this.getConstructorState(),
|
|
187
|
+
emits: [...this._emits, ...events]
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
logic(fn) {
|
|
191
|
+
return new _AgentFactory({
|
|
192
|
+
...this.getConstructorState(),
|
|
193
|
+
logic: fn
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
produce(params) {
|
|
197
|
+
const listensTo = this._listensTo.map((e) => e.name);
|
|
198
|
+
return new Agent(
|
|
199
|
+
this._logic,
|
|
200
|
+
params,
|
|
201
|
+
listensTo
|
|
202
|
+
);
|
|
145
203
|
}
|
|
146
204
|
};
|
|
147
205
|
|
|
@@ -363,6 +421,60 @@ var run = (network, plane, options) => Effect.gen(function* () {
|
|
|
363
421
|
}
|
|
364
422
|
yield* Effect.never;
|
|
365
423
|
});
|
|
424
|
+
var KEBAB_CASE_REGEX = /^[a-z0-9]+(-[a-z0-9]+)*$/;
|
|
425
|
+
var ChannelName = Brand.refined(
|
|
426
|
+
(s) => typeof s === "string" && KEBAB_CASE_REGEX.test(s),
|
|
427
|
+
(s) => Brand.error(`Expected kebab-case (e.g. my-channel-name), got: ${s}`)
|
|
428
|
+
);
|
|
429
|
+
|
|
430
|
+
// src/matrix/agent-network/channel.ts
|
|
431
|
+
var Sink = {
|
|
432
|
+
kafka(config) {
|
|
433
|
+
return { _tag: "SinkDef", type: "kafka", config };
|
|
434
|
+
},
|
|
435
|
+
httpStream() {
|
|
436
|
+
return { _tag: "SinkDef", type: "http-stream", config: {} };
|
|
437
|
+
}
|
|
438
|
+
};
|
|
439
|
+
function isHttpStreamSink(sink) {
|
|
440
|
+
return sink.type === "http-stream";
|
|
441
|
+
}
|
|
442
|
+
var ConfiguredChannel = class {
|
|
443
|
+
constructor(name) {
|
|
444
|
+
this._tag = "ConfiguredChannel";
|
|
445
|
+
this._events = [];
|
|
446
|
+
this._sinks = [];
|
|
447
|
+
this.name = name;
|
|
448
|
+
}
|
|
449
|
+
events(events) {
|
|
450
|
+
this._events = [...events];
|
|
451
|
+
return this;
|
|
452
|
+
}
|
|
453
|
+
sink(sink) {
|
|
454
|
+
this._sinks = [...this._sinks, sink];
|
|
455
|
+
return this;
|
|
456
|
+
}
|
|
457
|
+
sinks(sinks) {
|
|
458
|
+
this._sinks = [...sinks];
|
|
459
|
+
return this;
|
|
460
|
+
}
|
|
461
|
+
getEvents() {
|
|
462
|
+
return this._events;
|
|
463
|
+
}
|
|
464
|
+
getSinks() {
|
|
465
|
+
return this._sinks;
|
|
466
|
+
}
|
|
467
|
+
};
|
|
468
|
+
var Channel = {
|
|
469
|
+
of(name) {
|
|
470
|
+
return {
|
|
471
|
+
_tag: "ChannelDef",
|
|
472
|
+
name
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
};
|
|
476
|
+
|
|
477
|
+
// src/matrix/io/expose.ts
|
|
366
478
|
async function extractPayload(req) {
|
|
367
479
|
const webRequest = req.request;
|
|
368
480
|
if (webRequest?.method === "POST") {
|
|
@@ -540,12 +652,10 @@ var AgentNetwork = class _AgentNetwork {
|
|
|
540
652
|
/* ─── Public Static Factory ─── */
|
|
541
653
|
static setup(callback) {
|
|
542
654
|
const network = new _AgentNetwork();
|
|
655
|
+
const mainChannel = network.addChannel("main");
|
|
656
|
+
network.setMainChannel(mainChannel);
|
|
543
657
|
const ctx = {
|
|
544
|
-
mainChannel
|
|
545
|
-
const channel = network.addChannel(name);
|
|
546
|
-
network.setMainChannel(channel);
|
|
547
|
-
return channel;
|
|
548
|
-
},
|
|
658
|
+
mainChannel,
|
|
549
659
|
createChannel: (name) => network.addChannel(name),
|
|
550
660
|
sink: Sink,
|
|
551
661
|
registerAgent: (agent) => network.registerAgentInternal(agent),
|
|
@@ -558,6 +668,10 @@ var AgentNetwork = class _AgentNetwork {
|
|
|
558
668
|
/* ─── Internal Builders ─── */
|
|
559
669
|
addChannel(name) {
|
|
560
670
|
const channelName = ChannelName(name);
|
|
671
|
+
const existing = this.channels.get(channelName);
|
|
672
|
+
if (existing) {
|
|
673
|
+
return existing;
|
|
674
|
+
}
|
|
561
675
|
const channel = new ConfiguredChannel(channelName);
|
|
562
676
|
this.channels.set(channelName, channel);
|
|
563
677
|
return channel;
|
|
@@ -745,27 +859,27 @@ var EventAggregator = class _EventAggregator {
|
|
|
745
859
|
});
|
|
746
860
|
}
|
|
747
861
|
};
|
|
748
|
-
var
|
|
862
|
+
var _id2, _listensTo2, _emitWhen, _mapToEmit;
|
|
749
863
|
var EventAggregatorInstance = class {
|
|
750
864
|
constructor({
|
|
751
865
|
listensTo,
|
|
752
866
|
emitWhen,
|
|
753
867
|
mapToEmit
|
|
754
868
|
}) {
|
|
755
|
-
__privateAdd(this,
|
|
756
|
-
__privateAdd(this,
|
|
869
|
+
__privateAdd(this, _id2, void 0);
|
|
870
|
+
__privateAdd(this, _listensTo2, void 0);
|
|
757
871
|
__privateAdd(this, _emitWhen, void 0);
|
|
758
872
|
__privateAdd(this, _mapToEmit, void 0);
|
|
759
|
-
__privateSet(this,
|
|
760
|
-
__privateSet(this,
|
|
873
|
+
__privateSet(this, _id2, `event-aggregator-${randomUUID()}`);
|
|
874
|
+
__privateSet(this, _listensTo2, listensTo);
|
|
761
875
|
__privateSet(this, _emitWhen, emitWhen);
|
|
762
876
|
__privateSet(this, _mapToEmit, mapToEmit);
|
|
763
877
|
}
|
|
764
878
|
getId() {
|
|
765
|
-
return __privateGet(this,
|
|
879
|
+
return __privateGet(this, _id2);
|
|
766
880
|
}
|
|
767
881
|
getListensTo() {
|
|
768
|
-
return __privateGet(this,
|
|
882
|
+
return __privateGet(this, _listensTo2);
|
|
769
883
|
}
|
|
770
884
|
async invoke(options) {
|
|
771
885
|
const { triggerEvent, emit, runEvents, contextEvents } = options ?? {};
|
|
@@ -796,118 +910,205 @@ var EventAggregatorInstance = class {
|
|
|
796
910
|
});
|
|
797
911
|
}
|
|
798
912
|
};
|
|
799
|
-
|
|
800
|
-
|
|
913
|
+
_id2 = new WeakMap();
|
|
914
|
+
_listensTo2 = new WeakMap();
|
|
801
915
|
_emitWhen = new WeakMap();
|
|
802
916
|
_mapToEmit = new WeakMap();
|
|
803
|
-
var
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
__privateAdd(this, _id2, void 0);
|
|
809
|
-
__privateAdd(this, _listensTo2, void 0);
|
|
810
|
-
__privateSet(this, _logic, logic);
|
|
811
|
-
__privateSet(this, _params, params);
|
|
812
|
-
__privateSet(this, _id2, `agent-${randomUUID()}`);
|
|
813
|
-
__privateSet(this, _listensTo2, listensTo ?? []);
|
|
814
|
-
}
|
|
815
|
-
getListensTo() {
|
|
816
|
-
return __privateGet(this, _listensTo2);
|
|
817
|
-
}
|
|
818
|
-
async invoke(options) {
|
|
819
|
-
const { triggerEvent, emit, runEvents, contextEvents } = options ?? {};
|
|
820
|
-
const emitFn = emit ?? ((_event) => {
|
|
821
|
-
});
|
|
822
|
-
await __privateGet(this, _logic).call(this, {
|
|
823
|
-
params: __privateGet(this, _params),
|
|
824
|
-
triggerEvent: triggerEvent ?? void 0,
|
|
825
|
-
emit: emitFn,
|
|
826
|
-
runEvents: runEvents ?? [],
|
|
827
|
-
contextEvents: contextEvents ?? {
|
|
828
|
-
all: [],
|
|
829
|
-
byRun: () => [],
|
|
830
|
-
map: /* @__PURE__ */ new Map()
|
|
831
|
-
}
|
|
832
|
-
});
|
|
833
|
-
}
|
|
834
|
-
getId() {
|
|
835
|
-
return __privateGet(this, _id2);
|
|
917
|
+
var randomHexString = (length) => {
|
|
918
|
+
const chars = "abcdef0123456789";
|
|
919
|
+
let result = "";
|
|
920
|
+
for (let i = 0; i < length; i++) {
|
|
921
|
+
result += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
836
922
|
}
|
|
923
|
+
return result;
|
|
837
924
|
};
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
this.
|
|
852
|
-
this.
|
|
853
|
-
this.
|
|
854
|
-
this._emits = emits;
|
|
855
|
-
}
|
|
856
|
-
getConstructorState() {
|
|
857
|
-
return {
|
|
858
|
-
logic: this._logic,
|
|
859
|
-
paramsSchema: this._paramsSchema,
|
|
860
|
-
listensTo: this._listensTo,
|
|
861
|
-
emits: this._emits
|
|
862
|
-
};
|
|
863
|
-
}
|
|
864
|
-
/** Union of all event definitions this agent listens to */
|
|
865
|
-
getListensTo() {
|
|
866
|
-
return this._listensTo;
|
|
867
|
-
}
|
|
868
|
-
/** Union of all event definitions this agent can emit */
|
|
869
|
-
getEmits() {
|
|
870
|
-
return this._emits;
|
|
871
|
-
}
|
|
872
|
-
getLogic() {
|
|
873
|
-
return this._logic;
|
|
925
|
+
var ConsoleSpan = class {
|
|
926
|
+
constructor(name, parent, context, links, startTime, kind, depth) {
|
|
927
|
+
this.name = name;
|
|
928
|
+
this.parent = parent;
|
|
929
|
+
this.context = context;
|
|
930
|
+
this.startTime = startTime;
|
|
931
|
+
this.kind = kind;
|
|
932
|
+
this.depth = depth;
|
|
933
|
+
this._tag = "Span";
|
|
934
|
+
this.sampled = true;
|
|
935
|
+
this.attributes = /* @__PURE__ */ new Map();
|
|
936
|
+
this.links = [];
|
|
937
|
+
this.traceId = parent._tag === "Some" ? parent.value.traceId : randomHexString(32);
|
|
938
|
+
this.spanId = randomHexString(16);
|
|
939
|
+
this.links = Array.from(links);
|
|
940
|
+
this.status = { _tag: "Started", startTime };
|
|
874
941
|
}
|
|
875
|
-
|
|
876
|
-
|
|
942
|
+
end(endTime, exit) {
|
|
943
|
+
if (this.status._tag === "Ended")
|
|
944
|
+
return;
|
|
945
|
+
const startTime = this.status.startTime;
|
|
946
|
+
const durationNs = endTime - startTime;
|
|
947
|
+
const durationMs = Number(durationNs) / 1e6;
|
|
948
|
+
const indent = " ".repeat(this.depth);
|
|
949
|
+
const attrs = Object.fromEntries(this.attributes);
|
|
950
|
+
const status = Exit.isSuccess(exit) ? "ok" : "error";
|
|
951
|
+
console.log(
|
|
952
|
+
`${indent}[trace] ${this.name} ${durationMs.toFixed(2)}ms (${status})`,
|
|
953
|
+
Object.keys(attrs).length > 0 ? attrs : ""
|
|
954
|
+
);
|
|
955
|
+
this.status = { _tag: "Ended", startTime, endTime, exit };
|
|
877
956
|
}
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
return new _AgentFactory({
|
|
881
|
-
...rest,
|
|
882
|
-
logic,
|
|
883
|
-
paramsSchema: params
|
|
884
|
-
});
|
|
957
|
+
attribute(key, value) {
|
|
958
|
+
this.attributes.set(key, value);
|
|
885
959
|
}
|
|
886
|
-
|
|
887
|
-
return new _AgentFactory({
|
|
888
|
-
...this.getConstructorState(),
|
|
889
|
-
listensTo: [...this._listensTo, ...events]
|
|
890
|
-
});
|
|
960
|
+
event(_name, _startTime, _attributes) {
|
|
891
961
|
}
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
...this.getConstructorState(),
|
|
895
|
-
emits: [...this._emits, ...events]
|
|
896
|
-
});
|
|
962
|
+
addLinks(links) {
|
|
963
|
+
this.links.push(...links);
|
|
897
964
|
}
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
965
|
+
};
|
|
966
|
+
function getDepth(parent) {
|
|
967
|
+
if (parent._tag === "None")
|
|
968
|
+
return 0;
|
|
969
|
+
const p = parent.value;
|
|
970
|
+
if (p._tag === "ExternalSpan")
|
|
971
|
+
return 0;
|
|
972
|
+
return 1 + getDepth(p.parent);
|
|
973
|
+
}
|
|
974
|
+
var consoleTracer = Tracer.make({
|
|
975
|
+
span: (name, parent, context, links, startTime, kind) => new ConsoleSpan(name, parent, context, links, startTime, kind, getDepth(parent)),
|
|
976
|
+
context: (f) => f()
|
|
977
|
+
});
|
|
978
|
+
var consoleTracerLayer = Layer.setTracer(consoleTracer);
|
|
979
|
+
|
|
980
|
+
// src/matrix/io/protocols/sse.ts
|
|
981
|
+
function formatSSE(envelope) {
|
|
982
|
+
const data = JSON.stringify(envelope);
|
|
983
|
+
return `event: ${envelope.name}
|
|
984
|
+
data: ${data}
|
|
985
|
+
|
|
986
|
+
`;
|
|
987
|
+
}
|
|
988
|
+
function toSSEStream(source, signal) {
|
|
989
|
+
const encoder = new TextEncoder();
|
|
990
|
+
return new ReadableStream({
|
|
991
|
+
async start(controller) {
|
|
992
|
+
const onAbort = () => controller.close();
|
|
993
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
994
|
+
try {
|
|
995
|
+
for await (const envelope of source) {
|
|
996
|
+
if (signal?.aborted)
|
|
997
|
+
break;
|
|
998
|
+
controller.enqueue(encoder.encode(formatSSE(envelope)));
|
|
999
|
+
}
|
|
1000
|
+
} finally {
|
|
1001
|
+
signal?.removeEventListener("abort", onAbort);
|
|
1002
|
+
controller.close();
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
});
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
// src/matrix/io/adapters/next-endpoint.ts
|
|
1009
|
+
var NextEndpoint = {
|
|
1010
|
+
from(api, options) {
|
|
1011
|
+
if (api.protocol !== "sse") {
|
|
1012
|
+
throw new Error(`NextEndpoint: unsupported protocol "${api.protocol}"`);
|
|
1013
|
+
}
|
|
1014
|
+
const { requestToContextId, requestToRunId } = options;
|
|
1015
|
+
return {
|
|
1016
|
+
handler() {
|
|
1017
|
+
return async (request) => {
|
|
1018
|
+
const req = {
|
|
1019
|
+
request,
|
|
1020
|
+
contextId: requestToContextId(request),
|
|
1021
|
+
runId: requestToRunId(request)
|
|
1022
|
+
};
|
|
1023
|
+
try {
|
|
1024
|
+
const encoder = new TextEncoder();
|
|
1025
|
+
const { readable, writable } = new TransformStream();
|
|
1026
|
+
let consumerStarted;
|
|
1027
|
+
const started = new Promise((resolve) => {
|
|
1028
|
+
consumerStarted = resolve;
|
|
1029
|
+
});
|
|
1030
|
+
const streamDone = api.createStream(req, async (stream) => {
|
|
1031
|
+
consumerStarted();
|
|
1032
|
+
const writer = writable.getWriter();
|
|
1033
|
+
try {
|
|
1034
|
+
for await (const envelope of stream) {
|
|
1035
|
+
if (request.signal?.aborted)
|
|
1036
|
+
break;
|
|
1037
|
+
await writer.write(encoder.encode(formatSSE(envelope)));
|
|
1038
|
+
}
|
|
1039
|
+
} finally {
|
|
1040
|
+
await writer.close();
|
|
1041
|
+
}
|
|
1042
|
+
});
|
|
1043
|
+
await Promise.race([started, streamDone]);
|
|
1044
|
+
streamDone.catch(() => {
|
|
1045
|
+
});
|
|
1046
|
+
return new Response(readable, {
|
|
1047
|
+
headers: {
|
|
1048
|
+
"Content-Type": "text/event-stream",
|
|
1049
|
+
"Cache-Control": "no-cache",
|
|
1050
|
+
Connection: "keep-alive"
|
|
1051
|
+
}
|
|
1052
|
+
});
|
|
1053
|
+
} catch (e) {
|
|
1054
|
+
if (e instanceof ExposeAuthError) {
|
|
1055
|
+
return new Response(e.message, { status: e.status });
|
|
1056
|
+
}
|
|
1057
|
+
throw e;
|
|
1058
|
+
}
|
|
1059
|
+
};
|
|
1060
|
+
}
|
|
1061
|
+
};
|
|
903
1062
|
}
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
1063
|
+
};
|
|
1064
|
+
|
|
1065
|
+
// src/matrix/io/adapters/express-endpoint.ts
|
|
1066
|
+
var ExpressEndpoint = {
|
|
1067
|
+
from(api, options) {
|
|
1068
|
+
if (api.protocol !== "sse") {
|
|
1069
|
+
throw new Error(`ExpressEndpoint: unsupported protocol "${api.protocol}"`);
|
|
1070
|
+
}
|
|
1071
|
+
const { requestToContextId, requestToRunId } = options;
|
|
1072
|
+
return {
|
|
1073
|
+
handler() {
|
|
1074
|
+
return async (req, res) => {
|
|
1075
|
+
const controller = new AbortController();
|
|
1076
|
+
req.on("close", () => controller.abort());
|
|
1077
|
+
const exposeReq = {
|
|
1078
|
+
request: { signal: controller.signal },
|
|
1079
|
+
req,
|
|
1080
|
+
res,
|
|
1081
|
+
contextId: requestToContextId(req),
|
|
1082
|
+
runId: requestToRunId(req)
|
|
1083
|
+
};
|
|
1084
|
+
try {
|
|
1085
|
+
const encoder = new TextEncoder();
|
|
1086
|
+
await api.createStream(exposeReq, async (stream) => {
|
|
1087
|
+
res.setHeader("Content-Type", "text/event-stream");
|
|
1088
|
+
res.setHeader("Cache-Control", "no-cache");
|
|
1089
|
+
res.setHeader("Connection", "keep-alive");
|
|
1090
|
+
res.flushHeaders?.();
|
|
1091
|
+
try {
|
|
1092
|
+
for await (const envelope of stream) {
|
|
1093
|
+
if (controller.signal.aborted)
|
|
1094
|
+
break;
|
|
1095
|
+
res.write(encoder.encode(formatSSE(envelope)));
|
|
1096
|
+
res.flush?.();
|
|
1097
|
+
}
|
|
1098
|
+
} finally {
|
|
1099
|
+
res.end();
|
|
1100
|
+
}
|
|
1101
|
+
});
|
|
1102
|
+
} catch (e) {
|
|
1103
|
+
if (e instanceof ExposeAuthError) {
|
|
1104
|
+
res.status(e.status).send(e.message);
|
|
1105
|
+
return;
|
|
1106
|
+
}
|
|
1107
|
+
throw e;
|
|
1108
|
+
}
|
|
1109
|
+
};
|
|
1110
|
+
}
|
|
1111
|
+
};
|
|
911
1112
|
}
|
|
912
1113
|
};
|
|
913
1114
|
var CAMEL_CASE_REGEX = /^[a-z][a-zA-Z0-9]*$/;
|
|
@@ -1066,203 +1267,6 @@ var Skill = class _Skill {
|
|
|
1066
1267
|
}
|
|
1067
1268
|
};
|
|
1068
1269
|
|
|
1069
|
-
// src/matrix/io/protocols/sse.ts
|
|
1070
|
-
function formatSSE(envelope) {
|
|
1071
|
-
const data = JSON.stringify(envelope);
|
|
1072
|
-
return `event: ${envelope.name}
|
|
1073
|
-
data: ${data}
|
|
1074
|
-
|
|
1075
|
-
`;
|
|
1076
|
-
}
|
|
1077
|
-
function toSSEStream(source, signal) {
|
|
1078
|
-
const encoder = new TextEncoder();
|
|
1079
|
-
return new ReadableStream({
|
|
1080
|
-
async start(controller) {
|
|
1081
|
-
const onAbort = () => controller.close();
|
|
1082
|
-
signal?.addEventListener("abort", onAbort, { once: true });
|
|
1083
|
-
try {
|
|
1084
|
-
for await (const envelope of source) {
|
|
1085
|
-
if (signal?.aborted)
|
|
1086
|
-
break;
|
|
1087
|
-
controller.enqueue(encoder.encode(formatSSE(envelope)));
|
|
1088
|
-
}
|
|
1089
|
-
} finally {
|
|
1090
|
-
signal?.removeEventListener("abort", onAbort);
|
|
1091
|
-
controller.close();
|
|
1092
|
-
}
|
|
1093
|
-
}
|
|
1094
|
-
});
|
|
1095
|
-
}
|
|
1096
|
-
|
|
1097
|
-
// src/matrix/io/adapters/next-endpoint.ts
|
|
1098
|
-
var NextEndpoint = {
|
|
1099
|
-
from(api, options) {
|
|
1100
|
-
if (api.protocol !== "sse") {
|
|
1101
|
-
throw new Error(`NextEndpoint: unsupported protocol "${api.protocol}"`);
|
|
1102
|
-
}
|
|
1103
|
-
const { requestToContextId, requestToRunId } = options;
|
|
1104
|
-
return {
|
|
1105
|
-
handler() {
|
|
1106
|
-
return async (request) => {
|
|
1107
|
-
const req = {
|
|
1108
|
-
request,
|
|
1109
|
-
contextId: requestToContextId(request),
|
|
1110
|
-
runId: requestToRunId(request)
|
|
1111
|
-
};
|
|
1112
|
-
try {
|
|
1113
|
-
const encoder = new TextEncoder();
|
|
1114
|
-
const { readable, writable } = new TransformStream();
|
|
1115
|
-
let consumerStarted;
|
|
1116
|
-
const started = new Promise((resolve) => {
|
|
1117
|
-
consumerStarted = resolve;
|
|
1118
|
-
});
|
|
1119
|
-
const streamDone = api.createStream(req, async (stream) => {
|
|
1120
|
-
consumerStarted();
|
|
1121
|
-
const writer = writable.getWriter();
|
|
1122
|
-
try {
|
|
1123
|
-
for await (const envelope of stream) {
|
|
1124
|
-
if (request.signal?.aborted)
|
|
1125
|
-
break;
|
|
1126
|
-
await writer.write(encoder.encode(formatSSE(envelope)));
|
|
1127
|
-
}
|
|
1128
|
-
} finally {
|
|
1129
|
-
await writer.close();
|
|
1130
|
-
}
|
|
1131
|
-
});
|
|
1132
|
-
await Promise.race([started, streamDone]);
|
|
1133
|
-
streamDone.catch(() => {
|
|
1134
|
-
});
|
|
1135
|
-
return new Response(readable, {
|
|
1136
|
-
headers: {
|
|
1137
|
-
"Content-Type": "text/event-stream",
|
|
1138
|
-
"Cache-Control": "no-cache",
|
|
1139
|
-
Connection: "keep-alive"
|
|
1140
|
-
}
|
|
1141
|
-
});
|
|
1142
|
-
} catch (e) {
|
|
1143
|
-
if (e instanceof ExposeAuthError) {
|
|
1144
|
-
return new Response(e.message, { status: e.status });
|
|
1145
|
-
}
|
|
1146
|
-
throw e;
|
|
1147
|
-
}
|
|
1148
|
-
};
|
|
1149
|
-
}
|
|
1150
|
-
};
|
|
1151
|
-
}
|
|
1152
|
-
};
|
|
1153
|
-
|
|
1154
|
-
// src/matrix/io/adapters/express-endpoint.ts
|
|
1155
|
-
var ExpressEndpoint = {
|
|
1156
|
-
from(api, options) {
|
|
1157
|
-
if (api.protocol !== "sse") {
|
|
1158
|
-
throw new Error(`ExpressEndpoint: unsupported protocol "${api.protocol}"`);
|
|
1159
|
-
}
|
|
1160
|
-
const { requestToContextId, requestToRunId } = options;
|
|
1161
|
-
return {
|
|
1162
|
-
handler() {
|
|
1163
|
-
return async (req, res) => {
|
|
1164
|
-
const controller = new AbortController();
|
|
1165
|
-
req.on("close", () => controller.abort());
|
|
1166
|
-
const exposeReq = {
|
|
1167
|
-
request: { signal: controller.signal },
|
|
1168
|
-
req,
|
|
1169
|
-
res,
|
|
1170
|
-
contextId: requestToContextId(req),
|
|
1171
|
-
runId: requestToRunId(req)
|
|
1172
|
-
};
|
|
1173
|
-
try {
|
|
1174
|
-
const encoder = new TextEncoder();
|
|
1175
|
-
await api.createStream(exposeReq, async (stream) => {
|
|
1176
|
-
res.setHeader("Content-Type", "text/event-stream");
|
|
1177
|
-
res.setHeader("Cache-Control", "no-cache");
|
|
1178
|
-
res.setHeader("Connection", "keep-alive");
|
|
1179
|
-
res.flushHeaders?.();
|
|
1180
|
-
try {
|
|
1181
|
-
for await (const envelope of stream) {
|
|
1182
|
-
if (controller.signal.aborted)
|
|
1183
|
-
break;
|
|
1184
|
-
res.write(encoder.encode(formatSSE(envelope)));
|
|
1185
|
-
res.flush?.();
|
|
1186
|
-
}
|
|
1187
|
-
} finally {
|
|
1188
|
-
res.end();
|
|
1189
|
-
}
|
|
1190
|
-
});
|
|
1191
|
-
} catch (e) {
|
|
1192
|
-
if (e instanceof ExposeAuthError) {
|
|
1193
|
-
res.status(e.status).send(e.message);
|
|
1194
|
-
return;
|
|
1195
|
-
}
|
|
1196
|
-
throw e;
|
|
1197
|
-
}
|
|
1198
|
-
};
|
|
1199
|
-
}
|
|
1200
|
-
};
|
|
1201
|
-
}
|
|
1202
|
-
};
|
|
1203
|
-
var randomHexString = (length) => {
|
|
1204
|
-
const chars = "abcdef0123456789";
|
|
1205
|
-
let result = "";
|
|
1206
|
-
for (let i = 0; i < length; i++) {
|
|
1207
|
-
result += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
1208
|
-
}
|
|
1209
|
-
return result;
|
|
1210
|
-
};
|
|
1211
|
-
var ConsoleSpan = class {
|
|
1212
|
-
constructor(name, parent, context, links, startTime, kind, depth) {
|
|
1213
|
-
this.name = name;
|
|
1214
|
-
this.parent = parent;
|
|
1215
|
-
this.context = context;
|
|
1216
|
-
this.startTime = startTime;
|
|
1217
|
-
this.kind = kind;
|
|
1218
|
-
this.depth = depth;
|
|
1219
|
-
this._tag = "Span";
|
|
1220
|
-
this.sampled = true;
|
|
1221
|
-
this.attributes = /* @__PURE__ */ new Map();
|
|
1222
|
-
this.links = [];
|
|
1223
|
-
this.traceId = parent._tag === "Some" ? parent.value.traceId : randomHexString(32);
|
|
1224
|
-
this.spanId = randomHexString(16);
|
|
1225
|
-
this.links = Array.from(links);
|
|
1226
|
-
this.status = { _tag: "Started", startTime };
|
|
1227
|
-
}
|
|
1228
|
-
end(endTime, exit) {
|
|
1229
|
-
if (this.status._tag === "Ended")
|
|
1230
|
-
return;
|
|
1231
|
-
const startTime = this.status.startTime;
|
|
1232
|
-
const durationNs = endTime - startTime;
|
|
1233
|
-
const durationMs = Number(durationNs) / 1e6;
|
|
1234
|
-
const indent = " ".repeat(this.depth);
|
|
1235
|
-
const attrs = Object.fromEntries(this.attributes);
|
|
1236
|
-
const status = Exit.isSuccess(exit) ? "ok" : "error";
|
|
1237
|
-
console.log(
|
|
1238
|
-
`${indent}[trace] ${this.name} ${durationMs.toFixed(2)}ms (${status})`,
|
|
1239
|
-
Object.keys(attrs).length > 0 ? attrs : ""
|
|
1240
|
-
);
|
|
1241
|
-
this.status = { _tag: "Ended", startTime, endTime, exit };
|
|
1242
|
-
}
|
|
1243
|
-
attribute(key, value) {
|
|
1244
|
-
this.attributes.set(key, value);
|
|
1245
|
-
}
|
|
1246
|
-
event(_name, _startTime, _attributes) {
|
|
1247
|
-
}
|
|
1248
|
-
addLinks(links) {
|
|
1249
|
-
this.links.push(...links);
|
|
1250
|
-
}
|
|
1251
|
-
};
|
|
1252
|
-
function getDepth(parent) {
|
|
1253
|
-
if (parent._tag === "None")
|
|
1254
|
-
return 0;
|
|
1255
|
-
const p = parent.value;
|
|
1256
|
-
if (p._tag === "ExternalSpan")
|
|
1257
|
-
return 0;
|
|
1258
|
-
return 1 + getDepth(p.parent);
|
|
1259
|
-
}
|
|
1260
|
-
var consoleTracer = Tracer.make({
|
|
1261
|
-
span: (name, parent, context, links, startTime, kind) => new ConsoleSpan(name, parent, context, links, startTime, kind, getDepth(parent)),
|
|
1262
|
-
context: (f) => f()
|
|
1263
|
-
});
|
|
1264
|
-
var consoleTracerLayer = Layer.setTracer(consoleTracer);
|
|
1265
|
-
|
|
1266
1270
|
export { Agent, AgentFactory, AgentNetwork, AgentNetworkEvent, Channel, ChannelName, ConfiguredChannel, DepedencyLayer, Done, EventAggregator, EventAggregatorInstance, EventMetaSchema, ExposeAuthError, ExpressEndpoint, LayerName, NextEndpoint, Sink, Skill, SocketIoFactory, consoleTracer, consoleTracerLayer, formatSSE, isHttpStreamSink, toSSEStream };
|
|
1267
1271
|
//# sourceMappingURL=out.js.map
|
|
1268
1272
|
//# sourceMappingURL=index.js.map
|