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