@m4trix/core 0.9.0 → 0.11.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 +319 -1663
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -3
- package/dist/index.js +316 -1657
- package/dist/index.js.map +1 -1
- package/dist/matrix/index.cjs +319 -36
- package/dist/matrix/index.cjs.map +1 -1
- package/dist/matrix/index.d.ts +111 -2
- package/dist/matrix/index.js +316 -38
- package/dist/matrix/index.js.map +1 -1
- package/package.json +1 -1
package/dist/matrix/index.cjs
CHANGED
|
@@ -151,7 +151,16 @@ var createEventPlane = (options) => effect.Effect.gen(function* () {
|
|
|
151
151
|
};
|
|
152
152
|
const publishToPubSub = (channel, envelope) => effect.PubSub.publish(getPubsub(channel), envelope);
|
|
153
153
|
const publish = (channel, envelope) => effect.Effect.sync(() => recordEvent(envelope)).pipe(
|
|
154
|
-
effect.Effect.flatMap(() => publishToPubSub(channel, envelope))
|
|
154
|
+
effect.Effect.flatMap(() => publishToPubSub(channel, envelope)),
|
|
155
|
+
effect.Effect.withSpan("event.publish", {
|
|
156
|
+
attributes: {
|
|
157
|
+
"event.name": envelope.name,
|
|
158
|
+
"event.payload": payloadForSpan(envelope.payload),
|
|
159
|
+
channel,
|
|
160
|
+
runId: envelope.meta.runId,
|
|
161
|
+
contextId: envelope.meta.contextId
|
|
162
|
+
}
|
|
163
|
+
})
|
|
155
164
|
);
|
|
156
165
|
const publishToChannels = (targetChannels, envelope) => effect.Effect.sync(() => recordEvent(envelope)).pipe(
|
|
157
166
|
effect.Effect.flatMap(
|
|
@@ -160,7 +169,15 @@ var createEventPlane = (options) => effect.Effect.gen(function* () {
|
|
|
160
169
|
{ concurrency: "unbounded" }
|
|
161
170
|
)
|
|
162
171
|
),
|
|
163
|
-
effect.Effect.map((results) => results.every(Boolean))
|
|
172
|
+
effect.Effect.map((results) => results.every(Boolean)),
|
|
173
|
+
effect.Effect.withSpan("event.publish", {
|
|
174
|
+
attributes: {
|
|
175
|
+
"event.name": envelope.name,
|
|
176
|
+
"event.payload": payloadForSpan(envelope.payload),
|
|
177
|
+
runId: envelope.meta.runId,
|
|
178
|
+
contextId: envelope.meta.contextId
|
|
179
|
+
}
|
|
180
|
+
})
|
|
164
181
|
);
|
|
165
182
|
const subscribe = (channel) => effect.PubSub.subscribe(getPubsub(channel));
|
|
166
183
|
const getRunEvents = (runId, contextId) => {
|
|
@@ -193,8 +210,17 @@ var createEventPlane = (options) => effect.Effect.gen(function* () {
|
|
|
193
210
|
shutdown
|
|
194
211
|
};
|
|
195
212
|
});
|
|
196
|
-
|
|
213
|
+
function payloadForSpan(payload, maxLen = 500) {
|
|
214
|
+
try {
|
|
215
|
+
const s = JSON.stringify(payload);
|
|
216
|
+
return s.length > maxLen ? `${s.slice(0, maxLen)}...` : s;
|
|
217
|
+
} catch {
|
|
218
|
+
return String(payload);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
var runSubscriber = (agent, publishesTo, dequeue, plane, emitQueue, channelName) => effect.Effect.gen(function* () {
|
|
197
222
|
const listensTo = agent.getListensTo?.() ?? [];
|
|
223
|
+
const agentId = agent.getId();
|
|
198
224
|
const processOne = () => effect.Effect.gen(function* () {
|
|
199
225
|
const envelope = yield* effect.Queue.take(dequeue);
|
|
200
226
|
if (listensTo.length > 0 && !listensTo.includes(envelope.name)) {
|
|
@@ -205,34 +231,51 @@ var runSubscriber = (agent, publishesTo, dequeue, plane, emitQueue) => effect.Ef
|
|
|
205
231
|
envelope.meta.contextId
|
|
206
232
|
);
|
|
207
233
|
const contextEvents = plane.getContextEvents(envelope.meta.contextId);
|
|
208
|
-
yield* effect.Effect.
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
234
|
+
yield* effect.Effect.withSpan("agent.listen", {
|
|
235
|
+
attributes: {
|
|
236
|
+
agentId,
|
|
237
|
+
"event.name": envelope.name,
|
|
238
|
+
"event.payload": payloadForSpan(envelope.payload),
|
|
239
|
+
...channelName !== void 0 && { channel: channelName }
|
|
240
|
+
}
|
|
241
|
+
})(
|
|
242
|
+
effect.Effect.withSpan("agent.invoke", {
|
|
243
|
+
attributes: {
|
|
244
|
+
agentId,
|
|
245
|
+
"event.name": envelope.name,
|
|
246
|
+
"event.payload": payloadForSpan(envelope.payload)
|
|
247
|
+
}
|
|
248
|
+
})(
|
|
249
|
+
effect.Effect.tryPromise({
|
|
250
|
+
try: () => agent.invoke({
|
|
251
|
+
triggerEvent: envelope,
|
|
252
|
+
emit: (userEvent) => {
|
|
253
|
+
const fullEnvelope = {
|
|
254
|
+
name: userEvent.name,
|
|
255
|
+
meta: envelope.meta,
|
|
256
|
+
payload: userEvent.payload
|
|
257
|
+
};
|
|
258
|
+
if (emitQueue) {
|
|
259
|
+
effect.Effect.runPromise(
|
|
260
|
+
effect.Queue.offer(emitQueue, {
|
|
261
|
+
channels: publishesTo,
|
|
262
|
+
envelope: fullEnvelope
|
|
263
|
+
})
|
|
264
|
+
).catch(() => {
|
|
265
|
+
});
|
|
266
|
+
} else {
|
|
267
|
+
effect.Effect.runFork(
|
|
268
|
+
plane.publishToChannels(publishesTo, fullEnvelope)
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
},
|
|
272
|
+
runEvents,
|
|
273
|
+
contextEvents
|
|
274
|
+
}),
|
|
275
|
+
catch: (e) => e
|
|
276
|
+
})
|
|
277
|
+
)
|
|
278
|
+
);
|
|
236
279
|
}).pipe(
|
|
237
280
|
effect.Effect.catchAllCause(
|
|
238
281
|
(cause) => effect.Cause.isInterrupted(cause) ? effect.Effect.void : effect.Effect.sync(() => {
|
|
@@ -254,7 +297,8 @@ var run = (network, plane, options) => effect.Effect.gen(function* () {
|
|
|
254
297
|
reg.publishesTo,
|
|
255
298
|
dequeue,
|
|
256
299
|
plane,
|
|
257
|
-
emitQueue
|
|
300
|
+
emitQueue,
|
|
301
|
+
channel.name
|
|
258
302
|
);
|
|
259
303
|
}
|
|
260
304
|
}
|
|
@@ -328,7 +372,8 @@ function expose(network, options) {
|
|
|
328
372
|
select,
|
|
329
373
|
plane: providedPlane,
|
|
330
374
|
onRequest,
|
|
331
|
-
triggerEvents
|
|
375
|
+
triggerEvents,
|
|
376
|
+
tracingLayer
|
|
332
377
|
} = options;
|
|
333
378
|
const triggerEventDef = triggerEvents?.[0];
|
|
334
379
|
const triggerEventName = triggerEventDef?.name ?? "request";
|
|
@@ -342,7 +387,7 @@ function expose(network, options) {
|
|
|
342
387
|
const payload = await extractPayload(req);
|
|
343
388
|
const signal = req.request?.signal;
|
|
344
389
|
const program = effect.Effect.gen(function* () {
|
|
345
|
-
const plane = providedPlane ?? (yield* createEventPlane({ network }));
|
|
390
|
+
const plane = providedPlane ?? (yield* createEventPlane({ network, store: network.getStore() }));
|
|
346
391
|
if (!providedPlane) {
|
|
347
392
|
const emitQueue = yield* effect.Queue.unbounded();
|
|
348
393
|
yield* effect.Effect.fork(
|
|
@@ -410,7 +455,8 @@ function expose(network, options) {
|
|
|
410
455
|
}
|
|
411
456
|
return stream;
|
|
412
457
|
});
|
|
413
|
-
|
|
458
|
+
const runnable = tracingLayer ? program.pipe(effect.Effect.provide(tracingLayer), effect.Effect.scoped) : program.pipe(effect.Effect.scoped);
|
|
459
|
+
return effect.Effect.runPromise(runnable);
|
|
414
460
|
};
|
|
415
461
|
return {
|
|
416
462
|
protocol: "sse",
|
|
@@ -442,6 +488,7 @@ var AgentNetwork = class _AgentNetwork {
|
|
|
442
488
|
this.channels = /* @__PURE__ */ new Map();
|
|
443
489
|
this.agentRegistrations = /* @__PURE__ */ new Map();
|
|
444
490
|
this.spawnerRegistrations = [];
|
|
491
|
+
this._store = createInMemoryNetworkStore();
|
|
445
492
|
}
|
|
446
493
|
/* ─── Public Static Factory ─── */
|
|
447
494
|
static setup(callback) {
|
|
@@ -529,6 +576,10 @@ var AgentNetwork = class _AgentNetwork {
|
|
|
529
576
|
getSpawnerRegistrations() {
|
|
530
577
|
return this.spawnerRegistrations;
|
|
531
578
|
}
|
|
579
|
+
/** Store defined at network setup time. Shared across all event planes created for this network. */
|
|
580
|
+
getStore() {
|
|
581
|
+
return this._store;
|
|
582
|
+
}
|
|
532
583
|
/**
|
|
533
584
|
* Expose the network as a streamable API (e.g. SSE). Returns an ExposedAPI
|
|
534
585
|
* that adapters (NextEndpoint, ExpressEndpoint) consume to produce streamed
|
|
@@ -554,7 +605,11 @@ var AgentNetwork = class _AgentNetwork {
|
|
|
554
605
|
}
|
|
555
606
|
runScoped(network, capacity) {
|
|
556
607
|
return effect.Effect.gen(function* () {
|
|
557
|
-
const plane = yield* createEventPlane({
|
|
608
|
+
const plane = yield* createEventPlane({
|
|
609
|
+
network,
|
|
610
|
+
capacity,
|
|
611
|
+
store: network.getStore()
|
|
612
|
+
});
|
|
558
613
|
yield* effect.Effect.fork(run(network, plane));
|
|
559
614
|
return plane;
|
|
560
615
|
});
|
|
@@ -712,6 +767,157 @@ var AgentFactory = class _AgentFactory {
|
|
|
712
767
|
);
|
|
713
768
|
}
|
|
714
769
|
};
|
|
770
|
+
var CAMEL_CASE_REGEX = /^[a-z][a-zA-Z0-9]*$/;
|
|
771
|
+
var LayerName = effect.Brand.refined(
|
|
772
|
+
(s) => typeof s === "string" && CAMEL_CASE_REGEX.test(s),
|
|
773
|
+
(s) => effect.Brand.error(`Expected camelCase (e.g. myLayerFoo), got: ${s}`)
|
|
774
|
+
);
|
|
775
|
+
var SkillDependency = {
|
|
776
|
+
of(config) {
|
|
777
|
+
const name = LayerName(config.name);
|
|
778
|
+
const decode = effect.Schema.decodeUnknown(config.shape);
|
|
779
|
+
return {
|
|
780
|
+
_tag: "SkillDependencyDef",
|
|
781
|
+
name,
|
|
782
|
+
_name: config.name,
|
|
783
|
+
shape: config.shape,
|
|
784
|
+
decode
|
|
785
|
+
};
|
|
786
|
+
}
|
|
787
|
+
};
|
|
788
|
+
function toLayerArray(layers) {
|
|
789
|
+
if (layers.length === 1 && Array.isArray(layers[0])) {
|
|
790
|
+
return layers[0];
|
|
791
|
+
}
|
|
792
|
+
return [...layers];
|
|
793
|
+
}
|
|
794
|
+
function assertUniqueLayerNames(layers) {
|
|
795
|
+
const seen = /* @__PURE__ */ new Set();
|
|
796
|
+
for (const dep of layers) {
|
|
797
|
+
const key = dep.name;
|
|
798
|
+
if (seen.has(key)) {
|
|
799
|
+
throw new Error(`Duplicate layer name: ${key}`);
|
|
800
|
+
}
|
|
801
|
+
seen.add(key);
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
var Skill = class _Skill {
|
|
805
|
+
constructor(params) {
|
|
806
|
+
this._inputSchema = params.inputSchema;
|
|
807
|
+
this._chunkSchema = params.chunkSchema;
|
|
808
|
+
this._doneSchema = params.doneSchema;
|
|
809
|
+
this._layers = params.layers;
|
|
810
|
+
this._defineFn = params.defineFn;
|
|
811
|
+
}
|
|
812
|
+
getState() {
|
|
813
|
+
return {
|
|
814
|
+
inputSchema: this._inputSchema,
|
|
815
|
+
chunkSchema: this._chunkSchema,
|
|
816
|
+
doneSchema: this._doneSchema,
|
|
817
|
+
layers: this._layers,
|
|
818
|
+
defineFn: this._defineFn
|
|
819
|
+
};
|
|
820
|
+
}
|
|
821
|
+
static of(_options) {
|
|
822
|
+
return new _Skill({
|
|
823
|
+
layers: []
|
|
824
|
+
});
|
|
825
|
+
}
|
|
826
|
+
input(schema) {
|
|
827
|
+
return new _Skill({
|
|
828
|
+
...this.getState(),
|
|
829
|
+
inputSchema: schema
|
|
830
|
+
});
|
|
831
|
+
}
|
|
832
|
+
chunk(schema) {
|
|
833
|
+
return new _Skill({
|
|
834
|
+
...this.getState(),
|
|
835
|
+
chunkSchema: schema
|
|
836
|
+
});
|
|
837
|
+
}
|
|
838
|
+
done(schema) {
|
|
839
|
+
return new _Skill({
|
|
840
|
+
...this.getState(),
|
|
841
|
+
doneSchema: schema
|
|
842
|
+
});
|
|
843
|
+
}
|
|
844
|
+
use(...layers) {
|
|
845
|
+
const normalized = toLayerArray(layers);
|
|
846
|
+
const allLayers = [...this._layers, ...normalized];
|
|
847
|
+
assertUniqueLayerNames(allLayers);
|
|
848
|
+
return new _Skill({
|
|
849
|
+
...this.getState(),
|
|
850
|
+
layers: allLayers
|
|
851
|
+
});
|
|
852
|
+
}
|
|
853
|
+
define(fn) {
|
|
854
|
+
const state = this.getState();
|
|
855
|
+
const inputSchema = state.inputSchema;
|
|
856
|
+
const chunkSchema = state.chunkSchema;
|
|
857
|
+
const doneSchema = state.doneSchema;
|
|
858
|
+
const defineFn = fn;
|
|
859
|
+
if (!inputSchema || !chunkSchema || !doneSchema || !defineFn) {
|
|
860
|
+
throw new Error(
|
|
861
|
+
"Skill.define requires input(), chunk(), and done() to be called before define()"
|
|
862
|
+
);
|
|
863
|
+
}
|
|
864
|
+
const decodeInput = effect.Schema.decodeUnknown(inputSchema);
|
|
865
|
+
const decodeChunk = effect.Schema.decodeUnknown(chunkSchema);
|
|
866
|
+
const decodeDone = effect.Schema.decodeUnknown(doneSchema);
|
|
867
|
+
const runDefine = async (input, runtime) => {
|
|
868
|
+
const layersObj = runtime?.layers ?? {};
|
|
869
|
+
const chunks = [];
|
|
870
|
+
const emit = (chunk) => {
|
|
871
|
+
const decoded = effect.Effect.runSync(
|
|
872
|
+
decodeChunk(chunk)
|
|
873
|
+
);
|
|
874
|
+
chunks.push(decoded);
|
|
875
|
+
};
|
|
876
|
+
const done = await defineFn({
|
|
877
|
+
input,
|
|
878
|
+
emit,
|
|
879
|
+
layers: layersObj
|
|
880
|
+
});
|
|
881
|
+
const decodedDone = effect.Effect.runSync(
|
|
882
|
+
decodeDone(done)
|
|
883
|
+
);
|
|
884
|
+
return { chunks, done: decodedDone };
|
|
885
|
+
};
|
|
886
|
+
return {
|
|
887
|
+
invokeStream: async function* (input, runtime) {
|
|
888
|
+
const decodedInput = effect.Effect.runSync(
|
|
889
|
+
decodeInput(input)
|
|
890
|
+
);
|
|
891
|
+
const layersObj = runtime?.layers ?? {};
|
|
892
|
+
const chunks = [];
|
|
893
|
+
const emit = (chunk) => {
|
|
894
|
+
const decoded = effect.Effect.runSync(
|
|
895
|
+
decodeChunk(chunk)
|
|
896
|
+
);
|
|
897
|
+
chunks.push(decoded);
|
|
898
|
+
};
|
|
899
|
+
const done = await defineFn({
|
|
900
|
+
input: decodedInput,
|
|
901
|
+
emit,
|
|
902
|
+
layers: layersObj
|
|
903
|
+
});
|
|
904
|
+
const decodedDone = effect.Effect.runSync(
|
|
905
|
+
decodeDone(done)
|
|
906
|
+
);
|
|
907
|
+
for (const c of chunks) {
|
|
908
|
+
yield c;
|
|
909
|
+
}
|
|
910
|
+
yield { _tag: "Done", done: decodedDone };
|
|
911
|
+
},
|
|
912
|
+
invoke: async (input, runtime) => {
|
|
913
|
+
const decodedInput = effect.Effect.runSync(
|
|
914
|
+
decodeInput(input)
|
|
915
|
+
);
|
|
916
|
+
return runDefine(decodedInput, runtime);
|
|
917
|
+
}
|
|
918
|
+
};
|
|
919
|
+
}
|
|
920
|
+
};
|
|
715
921
|
|
|
716
922
|
// src/matrix/io/protocols/sse.ts
|
|
717
923
|
function formatSSE(envelope) {
|
|
@@ -849,6 +1055,78 @@ var ExpressEndpoint = {
|
|
|
849
1055
|
};
|
|
850
1056
|
}
|
|
851
1057
|
};
|
|
1058
|
+
var randomHexString = (length) => {
|
|
1059
|
+
const chars = "abcdef0123456789";
|
|
1060
|
+
let result = "";
|
|
1061
|
+
for (let i = 0; i < length; i++) {
|
|
1062
|
+
result += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
1063
|
+
}
|
|
1064
|
+
return result;
|
|
1065
|
+
};
|
|
1066
|
+
var ConsoleSpan = class {
|
|
1067
|
+
constructor(name, parent, context, links, startTime, kind, depth) {
|
|
1068
|
+
this.name = name;
|
|
1069
|
+
this.parent = parent;
|
|
1070
|
+
this.context = context;
|
|
1071
|
+
this.startTime = startTime;
|
|
1072
|
+
this.kind = kind;
|
|
1073
|
+
this.depth = depth;
|
|
1074
|
+
this._tag = "Span";
|
|
1075
|
+
this.sampled = true;
|
|
1076
|
+
this.attributes = /* @__PURE__ */ new Map();
|
|
1077
|
+
this.links = [];
|
|
1078
|
+
this.traceId = parent._tag === "Some" ? parent.value.traceId : randomHexString(32);
|
|
1079
|
+
this.spanId = randomHexString(16);
|
|
1080
|
+
this.links = Array.from(links);
|
|
1081
|
+
this.status = { _tag: "Started", startTime };
|
|
1082
|
+
}
|
|
1083
|
+
end(endTime, exit) {
|
|
1084
|
+
if (this.status._tag === "Ended")
|
|
1085
|
+
return;
|
|
1086
|
+
const startTime = this.status.startTime;
|
|
1087
|
+
const durationNs = endTime - startTime;
|
|
1088
|
+
const durationMs = Number(durationNs) / 1e6;
|
|
1089
|
+
const indent = " ".repeat(this.depth);
|
|
1090
|
+
const attrs = Object.fromEntries(this.attributes);
|
|
1091
|
+
const status = effect.Exit.isSuccess(exit) ? "ok" : "error";
|
|
1092
|
+
console.log(
|
|
1093
|
+
`${indent}[trace] ${this.name} ${durationMs.toFixed(2)}ms (${status})`,
|
|
1094
|
+
Object.keys(attrs).length > 0 ? attrs : ""
|
|
1095
|
+
);
|
|
1096
|
+
this.status = { _tag: "Ended", startTime, endTime, exit };
|
|
1097
|
+
}
|
|
1098
|
+
attribute(key, value) {
|
|
1099
|
+
this.attributes.set(key, value);
|
|
1100
|
+
}
|
|
1101
|
+
event(_name, _startTime, _attributes) {
|
|
1102
|
+
}
|
|
1103
|
+
addLinks(links) {
|
|
1104
|
+
this.links.push(...links);
|
|
1105
|
+
}
|
|
1106
|
+
};
|
|
1107
|
+
function getDepth(parent) {
|
|
1108
|
+
if (parent._tag === "None")
|
|
1109
|
+
return 0;
|
|
1110
|
+
const p = parent.value;
|
|
1111
|
+
if (p._tag === "ExternalSpan")
|
|
1112
|
+
return 0;
|
|
1113
|
+
return 1 + getDepth(p.parent);
|
|
1114
|
+
}
|
|
1115
|
+
var consoleTracer = effect.Tracer.make({
|
|
1116
|
+
span: (name, parent, context, links, startTime, kind) => new ConsoleSpan(
|
|
1117
|
+
name,
|
|
1118
|
+
parent,
|
|
1119
|
+
context,
|
|
1120
|
+
links,
|
|
1121
|
+
startTime,
|
|
1122
|
+
kind,
|
|
1123
|
+
getDepth(parent)
|
|
1124
|
+
),
|
|
1125
|
+
context: (f) => f()
|
|
1126
|
+
});
|
|
1127
|
+
var consoleTracerLayer = effect.Layer.setTracer(
|
|
1128
|
+
consoleTracer
|
|
1129
|
+
);
|
|
852
1130
|
|
|
853
1131
|
Object.defineProperty(exports, 'S', {
|
|
854
1132
|
enumerable: true,
|
|
@@ -864,8 +1142,13 @@ exports.ConfiguredChannel = ConfiguredChannel;
|
|
|
864
1142
|
exports.EventMetaSchema = EventMetaSchema;
|
|
865
1143
|
exports.ExposeAuthError = ExposeAuthError;
|
|
866
1144
|
exports.ExpressEndpoint = ExpressEndpoint;
|
|
1145
|
+
exports.LayerName = LayerName;
|
|
867
1146
|
exports.NextEndpoint = NextEndpoint;
|
|
868
1147
|
exports.Sink = Sink;
|
|
1148
|
+
exports.Skill = Skill;
|
|
1149
|
+
exports.SkillDependency = SkillDependency;
|
|
1150
|
+
exports.consoleTracer = consoleTracer;
|
|
1151
|
+
exports.consoleTracerLayer = consoleTracerLayer;
|
|
869
1152
|
exports.formatSSE = formatSSE;
|
|
870
1153
|
exports.isHttpStreamSink = isHttpStreamSink;
|
|
871
1154
|
exports.toSSEStream = toSSEStream;
|