@m4trix/core 0.15.1 → 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.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 KEBAB_CASE_REGEX = /^[a-z0-9]+(-[a-z0-9]+)*$/;
96
- var ChannelName = Brand.refined(
97
- (s) => typeof s === "string" && KEBAB_CASE_REGEX.test(s),
98
- (s) => Brand.error(`Expected kebab-case (e.g. my-channel-name), got: ${s}`)
99
- );
100
-
101
- // src/matrix/agent-network/channel.ts
102
- var Sink = {
103
- kafka(config) {
104
- return { _tag: "SinkDef", type: "kafka", config };
105
- },
106
- httpStream() {
107
- return { _tag: "SinkDef", type: "http-stream", config: {} };
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
- function isHttpStreamSink(sink) {
111
- return sink.type === "http-stream";
112
- }
113
- var ConfiguredChannel = class {
114
- constructor(name) {
115
- this._tag = "ConfiguredChannel";
116
- this._events = [];
117
- this._sinks = [];
118
- this.name = name;
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
- events(events) {
121
- this._events = [...events];
122
- return this;
148
+ getConstructorState() {
149
+ return {
150
+ logic: this._logic,
151
+ paramsSchema: this._paramsSchema,
152
+ listensTo: this._listensTo,
153
+ emits: this._emits
154
+ };
123
155
  }
124
- sink(sink) {
125
- this._sinks = [...this._sinks, sink];
126
- return this;
156
+ /** Union of all event definitions this agent listens to */
157
+ getListensTo() {
158
+ return this._listensTo;
127
159
  }
128
- sinks(sinks) {
129
- this._sinks = [...sinks];
130
- return this;
160
+ /** Union of all event definitions this agent can emit */
161
+ getEmits() {
162
+ return this._emits;
131
163
  }
132
- getEvents() {
133
- return this._events;
164
+ getLogic() {
165
+ return this._logic;
134
166
  }
135
- getSinks() {
136
- return this._sinks;
167
+ static run() {
168
+ return new _AgentFactory({});
137
169
  }
138
- };
139
- var Channel = {
140
- of(name) {
141
- return {
142
- _tag: "ChannelDef",
143
- name
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") {
@@ -745,27 +857,27 @@ var EventAggregator = class _EventAggregator {
745
857
  });
746
858
  }
747
859
  };
748
- var _id, _listensTo, _emitWhen, _mapToEmit;
860
+ var _id2, _listensTo2, _emitWhen, _mapToEmit;
749
861
  var EventAggregatorInstance = class {
750
862
  constructor({
751
863
  listensTo,
752
864
  emitWhen,
753
865
  mapToEmit
754
866
  }) {
755
- __privateAdd(this, _id, void 0);
756
- __privateAdd(this, _listensTo, void 0);
867
+ __privateAdd(this, _id2, void 0);
868
+ __privateAdd(this, _listensTo2, void 0);
757
869
  __privateAdd(this, _emitWhen, void 0);
758
870
  __privateAdd(this, _mapToEmit, void 0);
759
- __privateSet(this, _id, `event-aggregator-${randomUUID()}`);
760
- __privateSet(this, _listensTo, listensTo);
871
+ __privateSet(this, _id2, `event-aggregator-${randomUUID()}`);
872
+ __privateSet(this, _listensTo2, listensTo);
761
873
  __privateSet(this, _emitWhen, emitWhen);
762
874
  __privateSet(this, _mapToEmit, mapToEmit);
763
875
  }
764
876
  getId() {
765
- return __privateGet(this, _id);
877
+ return __privateGet(this, _id2);
766
878
  }
767
879
  getListensTo() {
768
- return __privateGet(this, _listensTo);
880
+ return __privateGet(this, _listensTo2);
769
881
  }
770
882
  async invoke(options) {
771
883
  const { triggerEvent, emit, runEvents, contextEvents } = options ?? {};
@@ -796,118 +908,205 @@ var EventAggregatorInstance = class {
796
908
  });
797
909
  }
798
910
  };
799
- _id = new WeakMap();
800
- _listensTo = new WeakMap();
911
+ _id2 = new WeakMap();
912
+ _listensTo2 = new WeakMap();
801
913
  _emitWhen = new WeakMap();
802
914
  _mapToEmit = new WeakMap();
803
- var _params, _logic, _id2, _listensTo2;
804
- var Agent = class {
805
- constructor(logic, params, listensTo) {
806
- __privateAdd(this, _params, void 0);
807
- __privateAdd(this, _logic, void 0);
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);
915
+ var randomHexString = (length) => {
916
+ const chars = "abcdef0123456789";
917
+ let result = "";
918
+ for (let i = 0; i < length; i++) {
919
+ result += chars.charAt(Math.floor(Math.random() * chars.length));
836
920
  }
921
+ return result;
837
922
  };
838
- _params = new WeakMap();
839
- _logic = new WeakMap();
840
- _id2 = new WeakMap();
841
- _listensTo2 = new WeakMap();
842
-
843
- // src/matrix/agent-factory.ts
844
- var AgentFactory = class _AgentFactory {
845
- constructor({
846
- logic,
847
- paramsSchema,
848
- listensTo = [],
849
- emits = []
850
- }) {
851
- this._logic = logic;
852
- this._paramsSchema = paramsSchema;
853
- this._listensTo = listensTo;
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;
923
+ var ConsoleSpan = class {
924
+ constructor(name, parent, context, links, startTime, kind, depth) {
925
+ this.name = name;
926
+ this.parent = parent;
927
+ this.context = context;
928
+ this.startTime = startTime;
929
+ this.kind = kind;
930
+ this.depth = depth;
931
+ this._tag = "Span";
932
+ this.sampled = true;
933
+ this.attributes = /* @__PURE__ */ new Map();
934
+ this.links = [];
935
+ this.traceId = parent._tag === "Some" ? parent.value.traceId : randomHexString(32);
936
+ this.spanId = randomHexString(16);
937
+ this.links = Array.from(links);
938
+ this.status = { _tag: "Started", startTime };
874
939
  }
875
- static run() {
876
- return new _AgentFactory({});
940
+ end(endTime, exit) {
941
+ if (this.status._tag === "Ended")
942
+ return;
943
+ const startTime = this.status.startTime;
944
+ const durationNs = endTime - startTime;
945
+ const durationMs = Number(durationNs) / 1e6;
946
+ const indent = " ".repeat(this.depth);
947
+ const attrs = Object.fromEntries(this.attributes);
948
+ const status = Exit.isSuccess(exit) ? "ok" : "error";
949
+ console.log(
950
+ `${indent}[trace] ${this.name} ${durationMs.toFixed(2)}ms (${status})`,
951
+ Object.keys(attrs).length > 0 ? attrs : ""
952
+ );
953
+ this.status = { _tag: "Ended", startTime, endTime, exit };
877
954
  }
878
- params(params) {
879
- const { logic, ...rest } = this.getConstructorState();
880
- return new _AgentFactory({
881
- ...rest,
882
- logic,
883
- paramsSchema: params
884
- });
955
+ attribute(key, value) {
956
+ this.attributes.set(key, value);
885
957
  }
886
- listensTo(events) {
887
- return new _AgentFactory({
888
- ...this.getConstructorState(),
889
- listensTo: [...this._listensTo, ...events]
890
- });
958
+ event(_name, _startTime, _attributes) {
891
959
  }
892
- emits(events) {
893
- return new _AgentFactory({
894
- ...this.getConstructorState(),
895
- emits: [...this._emits, ...events]
896
- });
960
+ addLinks(links) {
961
+ this.links.push(...links);
897
962
  }
898
- logic(fn) {
899
- return new _AgentFactory({
900
- ...this.getConstructorState(),
901
- logic: fn
902
- });
963
+ };
964
+ function getDepth(parent) {
965
+ if (parent._tag === "None")
966
+ return 0;
967
+ const p = parent.value;
968
+ if (p._tag === "ExternalSpan")
969
+ return 0;
970
+ return 1 + getDepth(p.parent);
971
+ }
972
+ var consoleTracer = Tracer.make({
973
+ span: (name, parent, context, links, startTime, kind) => new ConsoleSpan(name, parent, context, links, startTime, kind, getDepth(parent)),
974
+ context: (f) => f()
975
+ });
976
+ var consoleTracerLayer = Layer.setTracer(consoleTracer);
977
+
978
+ // src/matrix/io/protocols/sse.ts
979
+ function formatSSE(envelope) {
980
+ const data = JSON.stringify(envelope);
981
+ return `event: ${envelope.name}
982
+ data: ${data}
983
+
984
+ `;
985
+ }
986
+ function toSSEStream(source, signal) {
987
+ const encoder = new TextEncoder();
988
+ return new ReadableStream({
989
+ async start(controller) {
990
+ const onAbort = () => controller.close();
991
+ signal?.addEventListener("abort", onAbort, { once: true });
992
+ try {
993
+ for await (const envelope of source) {
994
+ if (signal?.aborted)
995
+ break;
996
+ controller.enqueue(encoder.encode(formatSSE(envelope)));
997
+ }
998
+ } finally {
999
+ signal?.removeEventListener("abort", onAbort);
1000
+ controller.close();
1001
+ }
1002
+ }
1003
+ });
1004
+ }
1005
+
1006
+ // src/matrix/io/adapters/next-endpoint.ts
1007
+ var NextEndpoint = {
1008
+ from(api, options) {
1009
+ if (api.protocol !== "sse") {
1010
+ throw new Error(`NextEndpoint: unsupported protocol "${api.protocol}"`);
1011
+ }
1012
+ const { requestToContextId, requestToRunId } = options;
1013
+ return {
1014
+ handler() {
1015
+ return async (request) => {
1016
+ const req = {
1017
+ request,
1018
+ contextId: requestToContextId(request),
1019
+ runId: requestToRunId(request)
1020
+ };
1021
+ try {
1022
+ const encoder = new TextEncoder();
1023
+ const { readable, writable } = new TransformStream();
1024
+ let consumerStarted;
1025
+ const started = new Promise((resolve) => {
1026
+ consumerStarted = resolve;
1027
+ });
1028
+ const streamDone = api.createStream(req, async (stream) => {
1029
+ consumerStarted();
1030
+ const writer = writable.getWriter();
1031
+ try {
1032
+ for await (const envelope of stream) {
1033
+ if (request.signal?.aborted)
1034
+ break;
1035
+ await writer.write(encoder.encode(formatSSE(envelope)));
1036
+ }
1037
+ } finally {
1038
+ await writer.close();
1039
+ }
1040
+ });
1041
+ await Promise.race([started, streamDone]);
1042
+ streamDone.catch(() => {
1043
+ });
1044
+ return new Response(readable, {
1045
+ headers: {
1046
+ "Content-Type": "text/event-stream",
1047
+ "Cache-Control": "no-cache",
1048
+ Connection: "keep-alive"
1049
+ }
1050
+ });
1051
+ } catch (e) {
1052
+ if (e instanceof ExposeAuthError) {
1053
+ return new Response(e.message, { status: e.status });
1054
+ }
1055
+ throw e;
1056
+ }
1057
+ };
1058
+ }
1059
+ };
903
1060
  }
904
- produce(params) {
905
- const listensTo = this._listensTo.map((e) => e.name);
906
- return new Agent(
907
- this._logic,
908
- params,
909
- listensTo
910
- );
1061
+ };
1062
+
1063
+ // src/matrix/io/adapters/express-endpoint.ts
1064
+ var ExpressEndpoint = {
1065
+ from(api, options) {
1066
+ if (api.protocol !== "sse") {
1067
+ throw new Error(`ExpressEndpoint: unsupported protocol "${api.protocol}"`);
1068
+ }
1069
+ const { requestToContextId, requestToRunId } = options;
1070
+ return {
1071
+ handler() {
1072
+ return async (req, res) => {
1073
+ const controller = new AbortController();
1074
+ req.on("close", () => controller.abort());
1075
+ const exposeReq = {
1076
+ request: { signal: controller.signal },
1077
+ req,
1078
+ res,
1079
+ contextId: requestToContextId(req),
1080
+ runId: requestToRunId(req)
1081
+ };
1082
+ try {
1083
+ const encoder = new TextEncoder();
1084
+ await api.createStream(exposeReq, async (stream) => {
1085
+ res.setHeader("Content-Type", "text/event-stream");
1086
+ res.setHeader("Cache-Control", "no-cache");
1087
+ res.setHeader("Connection", "keep-alive");
1088
+ res.flushHeaders?.();
1089
+ try {
1090
+ for await (const envelope of stream) {
1091
+ if (controller.signal.aborted)
1092
+ break;
1093
+ res.write(encoder.encode(formatSSE(envelope)));
1094
+ res.flush?.();
1095
+ }
1096
+ } finally {
1097
+ res.end();
1098
+ }
1099
+ });
1100
+ } catch (e) {
1101
+ if (e instanceof ExposeAuthError) {
1102
+ res.status(e.status).send(e.message);
1103
+ return;
1104
+ }
1105
+ throw e;
1106
+ }
1107
+ };
1108
+ }
1109
+ };
911
1110
  }
912
1111
  };
913
1112
  var CAMEL_CASE_REGEX = /^[a-z][a-zA-Z0-9]*$/;
@@ -1066,203 +1265,6 @@ var Skill = class _Skill {
1066
1265
  }
1067
1266
  };
1068
1267
 
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
1268
  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
1269
  //# sourceMappingURL=out.js.map
1268
1270
  //# sourceMappingURL=index.js.map