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