@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.
@@ -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))
@@ -21,56 +21,114 @@ var __privateSet = (obj, member, value, setter) => {
21
21
  setter ? setter.call(obj, value) : member.set(obj, value);
22
22
  return value;
23
23
  };
24
- var KEBAB_CASE_REGEX = /^[a-z0-9]+(-[a-z0-9]+)*$/;
25
- var ChannelName = effect.Brand.refined(
26
- (s) => typeof s === "string" && KEBAB_CASE_REGEX.test(s),
27
- (s) => effect.Brand.error(`Expected kebab-case (e.g. my-channel-name), got: ${s}`)
28
- );
29
-
30
- // src/matrix/agent-network/channel.ts
31
- var Sink = {
32
- kafka(config) {
33
- return { _tag: "SinkDef", type: "kafka", config };
34
- },
35
- httpStream() {
36
- return { _tag: "SinkDef", type: "http-stream", config: {} };
24
+ var _params, _logic, _id, _listensTo;
25
+ var Agent = class {
26
+ constructor(logic, params, listensTo) {
27
+ __privateAdd(this, _params, void 0);
28
+ __privateAdd(this, _logic, void 0);
29
+ __privateAdd(this, _id, void 0);
30
+ __privateAdd(this, _listensTo, void 0);
31
+ __privateSet(this, _logic, logic);
32
+ __privateSet(this, _params, params);
33
+ __privateSet(this, _id, `agent-${crypto$1.randomUUID()}`);
34
+ __privateSet(this, _listensTo, listensTo ?? []);
35
+ }
36
+ getListensTo() {
37
+ return __privateGet(this, _listensTo);
38
+ }
39
+ async invoke(options) {
40
+ const { triggerEvent, emit, runEvents, contextEvents } = options ?? {};
41
+ const emitFn = emit ?? ((_event) => {
42
+ });
43
+ await __privateGet(this, _logic).call(this, {
44
+ params: __privateGet(this, _params),
45
+ triggerEvent: triggerEvent ?? void 0,
46
+ emit: emitFn,
47
+ runEvents: runEvents ?? [],
48
+ contextEvents: contextEvents ?? {
49
+ all: [],
50
+ byRun: () => [],
51
+ map: /* @__PURE__ */ new Map()
52
+ }
53
+ });
54
+ }
55
+ getId() {
56
+ return __privateGet(this, _id);
37
57
  }
38
58
  };
39
- function isHttpStreamSink(sink) {
40
- return sink.type === "http-stream";
41
- }
42
- var ConfiguredChannel = class {
43
- constructor(name) {
44
- this._tag = "ConfiguredChannel";
45
- this._events = [];
46
- this._sinks = [];
47
- this.name = name;
59
+ _params = new WeakMap();
60
+ _logic = new WeakMap();
61
+ _id = new WeakMap();
62
+ _listensTo = new WeakMap();
63
+
64
+ // src/matrix/agent-factory.ts
65
+ var AgentFactory = class _AgentFactory {
66
+ constructor({
67
+ logic,
68
+ paramsSchema,
69
+ listensTo = [],
70
+ emits = []
71
+ }) {
72
+ this._logic = logic;
73
+ this._paramsSchema = paramsSchema;
74
+ this._listensTo = listensTo;
75
+ this._emits = emits;
48
76
  }
49
- events(events) {
50
- this._events = [...events];
51
- return this;
77
+ getConstructorState() {
78
+ return {
79
+ logic: this._logic,
80
+ paramsSchema: this._paramsSchema,
81
+ listensTo: this._listensTo,
82
+ emits: this._emits
83
+ };
52
84
  }
53
- sink(sink) {
54
- this._sinks = [...this._sinks, sink];
55
- return this;
85
+ /** Union of all event definitions this agent listens to */
86
+ getListensTo() {
87
+ return this._listensTo;
56
88
  }
57
- sinks(sinks) {
58
- this._sinks = [...sinks];
59
- return this;
89
+ /** Union of all event definitions this agent can emit */
90
+ getEmits() {
91
+ return this._emits;
60
92
  }
61
- getEvents() {
62
- return this._events;
93
+ getLogic() {
94
+ return this._logic;
63
95
  }
64
- getSinks() {
65
- return this._sinks;
96
+ static run() {
97
+ return new _AgentFactory({});
66
98
  }
67
- };
68
- var Channel = {
69
- of(name) {
70
- return {
71
- _tag: "ChannelDef",
72
- name
73
- };
99
+ params(params) {
100
+ const { logic, ...rest } = this.getConstructorState();
101
+ return new _AgentFactory({
102
+ ...rest,
103
+ logic,
104
+ paramsSchema: params
105
+ });
106
+ }
107
+ listensTo(events) {
108
+ return new _AgentFactory({
109
+ ...this.getConstructorState(),
110
+ listensTo: [...this._listensTo, ...events]
111
+ });
112
+ }
113
+ emits(events) {
114
+ return new _AgentFactory({
115
+ ...this.getConstructorState(),
116
+ emits: [...this._emits, ...events]
117
+ });
118
+ }
119
+ logic(fn) {
120
+ return new _AgentFactory({
121
+ ...this.getConstructorState(),
122
+ logic: fn
123
+ });
124
+ }
125
+ produce(params) {
126
+ const listensTo = this._listensTo.map((e) => e.name);
127
+ return new Agent(
128
+ this._logic,
129
+ params,
130
+ listensTo
131
+ );
74
132
  }
75
133
  };
76
134
 
@@ -292,6 +350,60 @@ var run = (network, plane, options) => effect.Effect.gen(function* () {
292
350
  }
293
351
  yield* effect.Effect.never;
294
352
  });
353
+ var KEBAB_CASE_REGEX = /^[a-z0-9]+(-[a-z0-9]+)*$/;
354
+ var ChannelName = effect.Brand.refined(
355
+ (s) => typeof s === "string" && KEBAB_CASE_REGEX.test(s),
356
+ (s) => effect.Brand.error(`Expected kebab-case (e.g. my-channel-name), got: ${s}`)
357
+ );
358
+
359
+ // src/matrix/agent-network/channel.ts
360
+ var Sink = {
361
+ kafka(config) {
362
+ return { _tag: "SinkDef", type: "kafka", config };
363
+ },
364
+ httpStream() {
365
+ return { _tag: "SinkDef", type: "http-stream", config: {} };
366
+ }
367
+ };
368
+ function isHttpStreamSink(sink) {
369
+ return sink.type === "http-stream";
370
+ }
371
+ var ConfiguredChannel = class {
372
+ constructor(name) {
373
+ this._tag = "ConfiguredChannel";
374
+ this._events = [];
375
+ this._sinks = [];
376
+ this.name = name;
377
+ }
378
+ events(events) {
379
+ this._events = [...events];
380
+ return this;
381
+ }
382
+ sink(sink) {
383
+ this._sinks = [...this._sinks, sink];
384
+ return this;
385
+ }
386
+ sinks(sinks) {
387
+ this._sinks = [...sinks];
388
+ return this;
389
+ }
390
+ getEvents() {
391
+ return this._events;
392
+ }
393
+ getSinks() {
394
+ return this._sinks;
395
+ }
396
+ };
397
+ var Channel = {
398
+ of(name) {
399
+ return {
400
+ _tag: "ChannelDef",
401
+ name
402
+ };
403
+ }
404
+ };
405
+
406
+ // src/matrix/io/expose.ts
295
407
  async function extractPayload(req) {
296
408
  const webRequest = req.request;
297
409
  if (webRequest?.method === "POST") {
@@ -674,27 +786,27 @@ var EventAggregator = class _EventAggregator {
674
786
  });
675
787
  }
676
788
  };
677
- var _id, _listensTo, _emitWhen, _mapToEmit;
789
+ var _id2, _listensTo2, _emitWhen, _mapToEmit;
678
790
  var EventAggregatorInstance = class {
679
791
  constructor({
680
792
  listensTo,
681
793
  emitWhen,
682
794
  mapToEmit
683
795
  }) {
684
- __privateAdd(this, _id, void 0);
685
- __privateAdd(this, _listensTo, void 0);
796
+ __privateAdd(this, _id2, void 0);
797
+ __privateAdd(this, _listensTo2, void 0);
686
798
  __privateAdd(this, _emitWhen, void 0);
687
799
  __privateAdd(this, _mapToEmit, void 0);
688
- __privateSet(this, _id, `event-aggregator-${crypto$1.randomUUID()}`);
689
- __privateSet(this, _listensTo, listensTo);
800
+ __privateSet(this, _id2, `event-aggregator-${crypto$1.randomUUID()}`);
801
+ __privateSet(this, _listensTo2, listensTo);
690
802
  __privateSet(this, _emitWhen, emitWhen);
691
803
  __privateSet(this, _mapToEmit, mapToEmit);
692
804
  }
693
805
  getId() {
694
- return __privateGet(this, _id);
806
+ return __privateGet(this, _id2);
695
807
  }
696
808
  getListensTo() {
697
- return __privateGet(this, _listensTo);
809
+ return __privateGet(this, _listensTo2);
698
810
  }
699
811
  async invoke(options) {
700
812
  const { triggerEvent, emit, runEvents, contextEvents } = options ?? {};
@@ -725,118 +837,205 @@ var EventAggregatorInstance = class {
725
837
  });
726
838
  }
727
839
  };
728
- _id = new WeakMap();
729
- _listensTo = new WeakMap();
840
+ _id2 = new WeakMap();
841
+ _listensTo2 = new WeakMap();
730
842
  _emitWhen = new WeakMap();
731
843
  _mapToEmit = new WeakMap();
732
- var _params, _logic, _id2, _listensTo2;
733
- var Agent = class {
734
- constructor(logic, params, listensTo) {
735
- __privateAdd(this, _params, void 0);
736
- __privateAdd(this, _logic, void 0);
737
- __privateAdd(this, _id2, void 0);
738
- __privateAdd(this, _listensTo2, void 0);
739
- __privateSet(this, _logic, logic);
740
- __privateSet(this, _params, params);
741
- __privateSet(this, _id2, `agent-${crypto$1.randomUUID()}`);
742
- __privateSet(this, _listensTo2, listensTo ?? []);
743
- }
744
- getListensTo() {
745
- return __privateGet(this, _listensTo2);
746
- }
747
- async invoke(options) {
748
- const { triggerEvent, emit, runEvents, contextEvents } = options ?? {};
749
- const emitFn = emit ?? ((_event) => {
750
- });
751
- await __privateGet(this, _logic).call(this, {
752
- params: __privateGet(this, _params),
753
- triggerEvent: triggerEvent ?? void 0,
754
- emit: emitFn,
755
- runEvents: runEvents ?? [],
756
- contextEvents: contextEvents ?? {
757
- all: [],
758
- byRun: () => [],
759
- map: /* @__PURE__ */ new Map()
760
- }
761
- });
762
- }
763
- getId() {
764
- return __privateGet(this, _id2);
844
+ var randomHexString = (length) => {
845
+ const chars = "abcdef0123456789";
846
+ let result = "";
847
+ for (let i = 0; i < length; i++) {
848
+ result += chars.charAt(Math.floor(Math.random() * chars.length));
765
849
  }
850
+ return result;
766
851
  };
767
- _params = new WeakMap();
768
- _logic = new WeakMap();
769
- _id2 = new WeakMap();
770
- _listensTo2 = new WeakMap();
771
-
772
- // src/matrix/agent-factory.ts
773
- var AgentFactory = class _AgentFactory {
774
- constructor({
775
- logic,
776
- paramsSchema,
777
- listensTo = [],
778
- emits = []
779
- }) {
780
- this._logic = logic;
781
- this._paramsSchema = paramsSchema;
782
- this._listensTo = listensTo;
783
- this._emits = emits;
784
- }
785
- getConstructorState() {
786
- return {
787
- logic: this._logic,
788
- paramsSchema: this._paramsSchema,
789
- listensTo: this._listensTo,
790
- emits: this._emits
791
- };
792
- }
793
- /** Union of all event definitions this agent listens to */
794
- getListensTo() {
795
- return this._listensTo;
796
- }
797
- /** Union of all event definitions this agent can emit */
798
- getEmits() {
799
- return this._emits;
800
- }
801
- getLogic() {
802
- return this._logic;
852
+ var ConsoleSpan = class {
853
+ constructor(name, parent, context, links, startTime, kind, depth) {
854
+ this.name = name;
855
+ this.parent = parent;
856
+ this.context = context;
857
+ this.startTime = startTime;
858
+ this.kind = kind;
859
+ this.depth = depth;
860
+ this._tag = "Span";
861
+ this.sampled = true;
862
+ this.attributes = /* @__PURE__ */ new Map();
863
+ this.links = [];
864
+ this.traceId = parent._tag === "Some" ? parent.value.traceId : randomHexString(32);
865
+ this.spanId = randomHexString(16);
866
+ this.links = Array.from(links);
867
+ this.status = { _tag: "Started", startTime };
803
868
  }
804
- static run() {
805
- return new _AgentFactory({});
869
+ end(endTime, exit) {
870
+ if (this.status._tag === "Ended")
871
+ return;
872
+ const startTime = this.status.startTime;
873
+ const durationNs = endTime - startTime;
874
+ const durationMs = Number(durationNs) / 1e6;
875
+ const indent = " ".repeat(this.depth);
876
+ const attrs = Object.fromEntries(this.attributes);
877
+ const status = effect.Exit.isSuccess(exit) ? "ok" : "error";
878
+ console.log(
879
+ `${indent}[trace] ${this.name} ${durationMs.toFixed(2)}ms (${status})`,
880
+ Object.keys(attrs).length > 0 ? attrs : ""
881
+ );
882
+ this.status = { _tag: "Ended", startTime, endTime, exit };
806
883
  }
807
- params(params) {
808
- const { logic, ...rest } = this.getConstructorState();
809
- return new _AgentFactory({
810
- ...rest,
811
- logic,
812
- paramsSchema: params
813
- });
884
+ attribute(key, value) {
885
+ this.attributes.set(key, value);
814
886
  }
815
- listensTo(events) {
816
- return new _AgentFactory({
817
- ...this.getConstructorState(),
818
- listensTo: [...this._listensTo, ...events]
819
- });
887
+ event(_name, _startTime, _attributes) {
820
888
  }
821
- emits(events) {
822
- return new _AgentFactory({
823
- ...this.getConstructorState(),
824
- emits: [...this._emits, ...events]
825
- });
889
+ addLinks(links) {
890
+ this.links.push(...links);
826
891
  }
827
- logic(fn) {
828
- return new _AgentFactory({
829
- ...this.getConstructorState(),
830
- logic: fn
831
- });
892
+ };
893
+ function getDepth(parent) {
894
+ if (parent._tag === "None")
895
+ return 0;
896
+ const p = parent.value;
897
+ if (p._tag === "ExternalSpan")
898
+ return 0;
899
+ return 1 + getDepth(p.parent);
900
+ }
901
+ var consoleTracer = effect.Tracer.make({
902
+ span: (name, parent, context, links, startTime, kind) => new ConsoleSpan(name, parent, context, links, startTime, kind, getDepth(parent)),
903
+ context: (f) => f()
904
+ });
905
+ var consoleTracerLayer = effect.Layer.setTracer(consoleTracer);
906
+
907
+ // src/matrix/io/protocols/sse.ts
908
+ function formatSSE(envelope) {
909
+ const data = JSON.stringify(envelope);
910
+ return `event: ${envelope.name}
911
+ data: ${data}
912
+
913
+ `;
914
+ }
915
+ function toSSEStream(source, signal) {
916
+ const encoder = new TextEncoder();
917
+ return new ReadableStream({
918
+ async start(controller) {
919
+ const onAbort = () => controller.close();
920
+ signal?.addEventListener("abort", onAbort, { once: true });
921
+ try {
922
+ for await (const envelope of source) {
923
+ if (signal?.aborted)
924
+ break;
925
+ controller.enqueue(encoder.encode(formatSSE(envelope)));
926
+ }
927
+ } finally {
928
+ signal?.removeEventListener("abort", onAbort);
929
+ controller.close();
930
+ }
931
+ }
932
+ });
933
+ }
934
+
935
+ // src/matrix/io/adapters/next-endpoint.ts
936
+ var NextEndpoint = {
937
+ from(api, options) {
938
+ if (api.protocol !== "sse") {
939
+ throw new Error(`NextEndpoint: unsupported protocol "${api.protocol}"`);
940
+ }
941
+ const { requestToContextId, requestToRunId } = options;
942
+ return {
943
+ handler() {
944
+ return async (request) => {
945
+ const req = {
946
+ request,
947
+ contextId: requestToContextId(request),
948
+ runId: requestToRunId(request)
949
+ };
950
+ try {
951
+ const encoder = new TextEncoder();
952
+ const { readable, writable } = new TransformStream();
953
+ let consumerStarted;
954
+ const started = new Promise((resolve) => {
955
+ consumerStarted = resolve;
956
+ });
957
+ const streamDone = api.createStream(req, async (stream) => {
958
+ consumerStarted();
959
+ const writer = writable.getWriter();
960
+ try {
961
+ for await (const envelope of stream) {
962
+ if (request.signal?.aborted)
963
+ break;
964
+ await writer.write(encoder.encode(formatSSE(envelope)));
965
+ }
966
+ } finally {
967
+ await writer.close();
968
+ }
969
+ });
970
+ await Promise.race([started, streamDone]);
971
+ streamDone.catch(() => {
972
+ });
973
+ return new Response(readable, {
974
+ headers: {
975
+ "Content-Type": "text/event-stream",
976
+ "Cache-Control": "no-cache",
977
+ Connection: "keep-alive"
978
+ }
979
+ });
980
+ } catch (e) {
981
+ if (e instanceof ExposeAuthError) {
982
+ return new Response(e.message, { status: e.status });
983
+ }
984
+ throw e;
985
+ }
986
+ };
987
+ }
988
+ };
832
989
  }
833
- produce(params) {
834
- const listensTo = this._listensTo.map((e) => e.name);
835
- return new Agent(
836
- this._logic,
837
- params,
838
- listensTo
839
- );
990
+ };
991
+
992
+ // src/matrix/io/adapters/express-endpoint.ts
993
+ var ExpressEndpoint = {
994
+ from(api, options) {
995
+ if (api.protocol !== "sse") {
996
+ throw new Error(`ExpressEndpoint: unsupported protocol "${api.protocol}"`);
997
+ }
998
+ const { requestToContextId, requestToRunId } = options;
999
+ return {
1000
+ handler() {
1001
+ return async (req, res) => {
1002
+ const controller = new AbortController();
1003
+ req.on("close", () => controller.abort());
1004
+ const exposeReq = {
1005
+ request: { signal: controller.signal },
1006
+ req,
1007
+ res,
1008
+ contextId: requestToContextId(req),
1009
+ runId: requestToRunId(req)
1010
+ };
1011
+ try {
1012
+ const encoder = new TextEncoder();
1013
+ await api.createStream(exposeReq, async (stream) => {
1014
+ res.setHeader("Content-Type", "text/event-stream");
1015
+ res.setHeader("Cache-Control", "no-cache");
1016
+ res.setHeader("Connection", "keep-alive");
1017
+ res.flushHeaders?.();
1018
+ try {
1019
+ for await (const envelope of stream) {
1020
+ if (controller.signal.aborted)
1021
+ break;
1022
+ res.write(encoder.encode(formatSSE(envelope)));
1023
+ res.flush?.();
1024
+ }
1025
+ } finally {
1026
+ res.end();
1027
+ }
1028
+ });
1029
+ } catch (e) {
1030
+ if (e instanceof ExposeAuthError) {
1031
+ res.status(e.status).send(e.message);
1032
+ return;
1033
+ }
1034
+ throw e;
1035
+ }
1036
+ };
1037
+ }
1038
+ };
840
1039
  }
841
1040
  };
842
1041
  var CAMEL_CASE_REGEX = /^[a-z][a-zA-Z0-9]*$/;
@@ -995,203 +1194,6 @@ var Skill = class _Skill {
995
1194
  }
996
1195
  };
997
1196
 
998
- // src/matrix/io/protocols/sse.ts
999
- function formatSSE(envelope) {
1000
- const data = JSON.stringify(envelope);
1001
- return `event: ${envelope.name}
1002
- data: ${data}
1003
-
1004
- `;
1005
- }
1006
- function toSSEStream(source, signal) {
1007
- const encoder = new TextEncoder();
1008
- return new ReadableStream({
1009
- async start(controller) {
1010
- const onAbort = () => controller.close();
1011
- signal?.addEventListener("abort", onAbort, { once: true });
1012
- try {
1013
- for await (const envelope of source) {
1014
- if (signal?.aborted)
1015
- break;
1016
- controller.enqueue(encoder.encode(formatSSE(envelope)));
1017
- }
1018
- } finally {
1019
- signal?.removeEventListener("abort", onAbort);
1020
- controller.close();
1021
- }
1022
- }
1023
- });
1024
- }
1025
-
1026
- // src/matrix/io/adapters/next-endpoint.ts
1027
- var NextEndpoint = {
1028
- from(api, options) {
1029
- if (api.protocol !== "sse") {
1030
- throw new Error(`NextEndpoint: unsupported protocol "${api.protocol}"`);
1031
- }
1032
- const { requestToContextId, requestToRunId } = options;
1033
- return {
1034
- handler() {
1035
- return async (request) => {
1036
- const req = {
1037
- request,
1038
- contextId: requestToContextId(request),
1039
- runId: requestToRunId(request)
1040
- };
1041
- try {
1042
- const encoder = new TextEncoder();
1043
- const { readable, writable } = new TransformStream();
1044
- let consumerStarted;
1045
- const started = new Promise((resolve) => {
1046
- consumerStarted = resolve;
1047
- });
1048
- const streamDone = api.createStream(req, async (stream) => {
1049
- consumerStarted();
1050
- const writer = writable.getWriter();
1051
- try {
1052
- for await (const envelope of stream) {
1053
- if (request.signal?.aborted)
1054
- break;
1055
- await writer.write(encoder.encode(formatSSE(envelope)));
1056
- }
1057
- } finally {
1058
- await writer.close();
1059
- }
1060
- });
1061
- await Promise.race([started, streamDone]);
1062
- streamDone.catch(() => {
1063
- });
1064
- return new Response(readable, {
1065
- headers: {
1066
- "Content-Type": "text/event-stream",
1067
- "Cache-Control": "no-cache",
1068
- Connection: "keep-alive"
1069
- }
1070
- });
1071
- } catch (e) {
1072
- if (e instanceof ExposeAuthError) {
1073
- return new Response(e.message, { status: e.status });
1074
- }
1075
- throw e;
1076
- }
1077
- };
1078
- }
1079
- };
1080
- }
1081
- };
1082
-
1083
- // src/matrix/io/adapters/express-endpoint.ts
1084
- var ExpressEndpoint = {
1085
- from(api, options) {
1086
- if (api.protocol !== "sse") {
1087
- throw new Error(`ExpressEndpoint: unsupported protocol "${api.protocol}"`);
1088
- }
1089
- const { requestToContextId, requestToRunId } = options;
1090
- return {
1091
- handler() {
1092
- return async (req, res) => {
1093
- const controller = new AbortController();
1094
- req.on("close", () => controller.abort());
1095
- const exposeReq = {
1096
- request: { signal: controller.signal },
1097
- req,
1098
- res,
1099
- contextId: requestToContextId(req),
1100
- runId: requestToRunId(req)
1101
- };
1102
- try {
1103
- const encoder = new TextEncoder();
1104
- await api.createStream(exposeReq, async (stream) => {
1105
- res.setHeader("Content-Type", "text/event-stream");
1106
- res.setHeader("Cache-Control", "no-cache");
1107
- res.setHeader("Connection", "keep-alive");
1108
- res.flushHeaders?.();
1109
- try {
1110
- for await (const envelope of stream) {
1111
- if (controller.signal.aborted)
1112
- break;
1113
- res.write(encoder.encode(formatSSE(envelope)));
1114
- res.flush?.();
1115
- }
1116
- } finally {
1117
- res.end();
1118
- }
1119
- });
1120
- } catch (e) {
1121
- if (e instanceof ExposeAuthError) {
1122
- res.status(e.status).send(e.message);
1123
- return;
1124
- }
1125
- throw e;
1126
- }
1127
- };
1128
- }
1129
- };
1130
- }
1131
- };
1132
- var randomHexString = (length) => {
1133
- const chars = "abcdef0123456789";
1134
- let result = "";
1135
- for (let i = 0; i < length; i++) {
1136
- result += chars.charAt(Math.floor(Math.random() * chars.length));
1137
- }
1138
- return result;
1139
- };
1140
- var ConsoleSpan = class {
1141
- constructor(name, parent, context, links, startTime, kind, depth) {
1142
- this.name = name;
1143
- this.parent = parent;
1144
- this.context = context;
1145
- this.startTime = startTime;
1146
- this.kind = kind;
1147
- this.depth = depth;
1148
- this._tag = "Span";
1149
- this.sampled = true;
1150
- this.attributes = /* @__PURE__ */ new Map();
1151
- this.links = [];
1152
- this.traceId = parent._tag === "Some" ? parent.value.traceId : randomHexString(32);
1153
- this.spanId = randomHexString(16);
1154
- this.links = Array.from(links);
1155
- this.status = { _tag: "Started", startTime };
1156
- }
1157
- end(endTime, exit) {
1158
- if (this.status._tag === "Ended")
1159
- return;
1160
- const startTime = this.status.startTime;
1161
- const durationNs = endTime - startTime;
1162
- const durationMs = Number(durationNs) / 1e6;
1163
- const indent = " ".repeat(this.depth);
1164
- const attrs = Object.fromEntries(this.attributes);
1165
- const status = effect.Exit.isSuccess(exit) ? "ok" : "error";
1166
- console.log(
1167
- `${indent}[trace] ${this.name} ${durationMs.toFixed(2)}ms (${status})`,
1168
- Object.keys(attrs).length > 0 ? attrs : ""
1169
- );
1170
- this.status = { _tag: "Ended", startTime, endTime, exit };
1171
- }
1172
- attribute(key, value) {
1173
- this.attributes.set(key, value);
1174
- }
1175
- event(_name, _startTime, _attributes) {
1176
- }
1177
- addLinks(links) {
1178
- this.links.push(...links);
1179
- }
1180
- };
1181
- function getDepth(parent) {
1182
- if (parent._tag === "None")
1183
- return 0;
1184
- const p = parent.value;
1185
- if (p._tag === "ExternalSpan")
1186
- return 0;
1187
- return 1 + getDepth(p.parent);
1188
- }
1189
- var consoleTracer = effect.Tracer.make({
1190
- span: (name, parent, context, links, startTime, kind) => new ConsoleSpan(name, parent, context, links, startTime, kind, getDepth(parent)),
1191
- context: (f) => f()
1192
- });
1193
- var consoleTracerLayer = effect.Layer.setTracer(consoleTracer);
1194
-
1195
1197
  Object.defineProperty(exports, 'S', {
1196
1198
  enumerable: true,
1197
1199
  get: function () { return effect.Schema; }