2g 0.3.1 → 0.4.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/2g.mjs CHANGED
@@ -2,44 +2,44 @@ import e from "node:path";
2
2
 
3
3
  import { performance as t } from "node:perf_hooks";
4
4
 
5
- import { E as i, h as s, p as n, m as r, j as o, c, I as l, k as h, D as a, l as u, o as f, q as d, S as g, b as p, s as m, g as y, t as w, L as k } from "./chunks/sessionSockets-chunk.mjs";
5
+ import { E as i, f as s, e as n, m as r, L as o, b as c, I as l, g as h, D as a, h as u, i as f, j as d, k as g, l as p, S as m, a as y, n as w, o as k, r as v, q as S } from "./chunks/sessionSockets-chunk.mjs";
6
6
 
7
- import v from "node:fs";
7
+ import b from "node:fs";
8
8
 
9
- import S from "node:net";
9
+ import L from "node:net";
10
10
 
11
- import { s as b, f as L, a as _ } from "./chunks/pretty-chunk.mjs";
11
+ import { s as _, f as P, a as E } from "./chunks/pretty-chunk.mjs";
12
12
 
13
- import { Buffer as P } from "node:buffer";
13
+ import { Buffer as I } from "node:buffer";
14
14
 
15
- import { EventEmitter as E } from "node:events";
15
+ import { EventEmitter as $ } from "node:events";
16
16
 
17
- import { isMainThread as I, threadId as $ } from "node:worker_threads";
17
+ import { isMainThread as O, threadId as D } from "node:worker_threads";
18
18
 
19
- const O = Symbol.for(`2g/state-${i}`);
19
+ const C = Symbol.for(`2g/state-${i}`);
20
20
 
21
- const D = function getEventLogState() {
21
+ const x = function getEventLogState() {
22
22
  const e = globalThis;
23
- return e[O] ??= {
23
+ return e[C] ??= {
24
24
  logPath: process.cwd(),
25
25
  eventLoggerInfo: null,
26
26
  debug: !1
27
27
  };
28
28
  }();
29
29
 
30
- const C = t.timeOrigin;
30
+ const N = t.timeOrigin;
31
31
 
32
- const x = Array.from({
32
+ const T = Array.from({
33
33
  length: 1e3
34
34
  }, (e, t) => `.${`${t}`.padStart(3, "0")}`);
35
35
 
36
- let N = -1;
36
+ let j = -1;
37
37
 
38
- let T = "";
38
+ let F = "";
39
39
 
40
- let j;
40
+ let M;
41
41
 
42
- let F = "";
42
+ let R = "";
43
43
 
44
44
  function writeEvent(e, t, i, s, n, r) {
45
45
  try {
@@ -80,21 +80,21 @@ function writeCompleteEvent(e, t, i, s, n, r, o, c) {
80
80
  }
81
81
 
82
82
  function stringifyTimestamp() {
83
- const e = C + t.now();
83
+ const e = N + t.now();
84
84
  const i = Math.floor(e);
85
- if (i !== N) {
86
- N = i;
87
- T = `${i}`;
85
+ if (i !== j) {
86
+ j = i;
87
+ F = `${i}`;
88
88
  }
89
- return T + x[1e3 * (e - i) | 0];
89
+ return F + T[1e3 * (e - i) | 0];
90
90
  }
91
91
 
92
92
  function stringifyWorkerId(e) {
93
- if (e !== j) {
94
- j = e;
95
- F = JSON.stringify(e);
93
+ if (e !== M) {
94
+ M = e;
95
+ R = JSON.stringify(e);
96
96
  }
97
- return F;
97
+ return R;
98
98
  }
99
99
 
100
100
  function appendPayload(e, t) {
@@ -134,27 +134,27 @@ function serializeError(e, t = 0) {
134
134
 
135
135
  function createEventLogger(i, s) {
136
136
  function log(e, t) {
137
- if (D.primarySink && (!s || D.debug)) {
138
- writeEvent(D.primarySink, i, e, t, D.eventMeta, s);
137
+ if (x.primarySink && (!s || x.debug)) {
138
+ writeEvent(x.primarySink, i, e, t, x.eventMeta, s);
139
139
  }
140
140
  }
141
141
  log.span = function span() {
142
- if (!D.primarySink || s && !D.debug) {
142
+ if (!x.primarySink || s && !x.debug) {
143
143
  return NOOP_DONE;
144
144
  }
145
145
  const e = t.now();
146
146
  return function done(n, r) {
147
- if (!D.primarySink) {
147
+ if (!x.primarySink) {
148
148
  return;
149
149
  }
150
- writeCompleteEvent(D.primarySink, i, n, r, void 0, t.now() - e, D.eventMeta, s);
150
+ writeCompleteEvent(x.primarySink, i, n, r, void 0, t.now() - e, x.eventMeta, s);
151
151
  };
152
152
  };
153
153
  log.path = function relativePath(t) {
154
154
  return {
155
155
  toJSON() {
156
156
  try {
157
- return null != t && e.isAbsolute(t) ? e.relative(D.logPath, t).replace(/\\/g, "/") || "." : t ?? null;
157
+ return null != t && e.isAbsolute(t) ? e.relative(x.logPath, t).replace(/\\/g, "/") || "." : t ?? null;
158
158
  } catch {
159
159
  return t || null;
160
160
  }
@@ -189,7 +189,7 @@ function createDebugSink(e = process.env[o]) {
189
189
  if (!t) {
190
190
  return null;
191
191
  }
192
- const i = b(process.stderr);
192
+ const i = _(process.stderr);
193
193
  return {
194
194
  get writable() {
195
195
  return !0;
@@ -211,7 +211,7 @@ function createDebugSink(e = process.env[o]) {
211
211
  }, t)) {
212
212
  continue;
213
213
  }
214
- process.stderr.write(`${L(l, {
214
+ process.stderr.write(`${P(l, {
215
215
  colorize: i,
216
216
  stream: process.stderr
217
217
  })}\n`);
@@ -226,7 +226,7 @@ function createDebugSink(e = process.env[o]) {
226
226
  };
227
227
  }
228
228
 
229
- class LogStream extends E {
229
+ class LogStream extends $ {
230
230
  #e=-1;
231
231
  #t=null;
232
232
  #i=null;
@@ -254,7 +254,7 @@ class LogStream extends E {
254
254
  if (null != e) {
255
255
  this.#L(e);
256
256
  } else {
257
- this.#S(null, P.byteLength(this.#u));
257
+ this.#S(null, I.byteLength(this.#u));
258
258
  }
259
259
  };
260
260
  #_=() => {
@@ -268,7 +268,7 @@ class LogStream extends E {
268
268
  this.#s = !1 !== t.closeFd;
269
269
  if ("number" == typeof e) {
270
270
  try {
271
- v.fsyncSync(e);
271
+ b.fsyncSync(e);
272
272
  } catch {}
273
273
  this.#e = e;
274
274
  process.nextTick(() => this.emit("ready"));
@@ -347,8 +347,8 @@ class LogStream extends E {
347
347
  this.#f -= this.#u.length;
348
348
  this.#u = "";
349
349
  } else {
350
- if (P.byteLength(this.#u) > t) {
351
- const e = P.from(this.#u).toString("utf8", t);
350
+ if (I.byteLength(this.#u) > t) {
351
+ const e = I.from(this.#u).toString("utf8", t);
352
352
  this.#f -= this.#u.length - e.length;
353
353
  this.#u = e;
354
354
  } else {
@@ -379,15 +379,15 @@ class LogStream extends E {
379
379
  }
380
380
  #E(t) {
381
381
  this.#I(i => {
382
- v.mkdir(e.dirname(t), {
382
+ b.mkdir(e.dirname(t), {
383
383
  recursive: !0
384
384
  }, () => {
385
- v.open(t, "a", 384, (e, t) => {
385
+ b.open(t, "a", 384, (e, t) => {
386
386
  if (e || null == t) {
387
387
  i(e);
388
388
  return;
389
389
  }
390
- v.fchmod(t, 384, () => {
390
+ b.fchmod(t, 384, () => {
391
391
  i(null, t);
392
392
  });
393
393
  });
@@ -411,7 +411,7 @@ class LogStream extends E {
411
411
  this.#n = !1;
412
412
  this.emit("ready");
413
413
  if (null != n && n !== this.#e && !isStdFd(n)) {
414
- fsFsync(n, () => v.close(n, () => {}));
414
+ fsFsync(n, () => b.close(n, () => {}));
415
415
  }
416
416
  if (this.#l) {
417
417
  return;
@@ -450,7 +450,7 @@ class LogStream extends E {
450
450
  }
451
451
  fsFsync(this.#e, e => {
452
452
  if (!e && this.#s && !isStdFd(this.#e)) {
453
- v.close(this.#e, onClose);
453
+ b.close(this.#e, onClose);
454
454
  } else {
455
455
  onClose();
456
456
  }
@@ -487,7 +487,7 @@ class LogStream extends E {
487
487
  if (null != this.#k && (this.#y || this.#e < 0)) {
488
488
  this.#k(this.#u, this.#b);
489
489
  } else {
490
- v.write(this.#e, this.#u, this.#v);
490
+ b.write(this.#e, this.#u, this.#v);
491
491
  }
492
492
  }
493
493
  _end() {
@@ -617,7 +617,7 @@ class LogStream extends E {
617
617
  write(e, t, i) {
618
618
  const s = i || t;
619
619
  const n = "string" == typeof t ? t : "utf8";
620
- const r = "string" == typeof e ? e : P.from(e).toString(n);
620
+ const r = "string" == typeof e ? e : I.from(e).toString(n);
621
621
  const o = "function" == typeof s ? s : void 0;
622
622
  try {
623
623
  return this._write(r);
@@ -645,7 +645,7 @@ const isStdFd = e => {
645
645
 
646
646
  const fsFsync = (e, t) => {
647
647
  try {
648
- v.fsync(e, t);
648
+ b.fsync(e, t);
649
649
  } catch (e) {
650
650
  t(e);
651
651
  }
@@ -809,7 +809,7 @@ function openIpc(e, t = {}) {
809
809
  return (t, n) => {
810
810
  const r = Date.now();
811
811
  const attempt = () => {
812
- const o = S.connect(e);
812
+ const o = L.connect(e);
813
813
  let c = !1;
814
814
  o.once("connect", () => {
815
815
  const e = o._handle?.fd;
@@ -847,25 +847,25 @@ function openIpc(e, t = {}) {
847
847
  };
848
848
  }
849
849
 
850
- const M = Symbol.for("2g/process-origin");
850
+ const U = Symbol.for("2g/process-origin");
851
851
 
852
852
  function setLocalProcessOrigin(e) {
853
853
  const t = globalThis;
854
854
  if (e) {
855
- t[M] = e;
855
+ t[U] = e;
856
856
  } else {
857
- delete t[M];
857
+ delete t[U];
858
858
  }
859
859
  }
860
860
 
861
861
  function getProcessOrigin() {
862
- if (!I) {
862
+ if (!O) {
863
863
  return {
864
864
  kind: "worker_thread",
865
- id: String($)
865
+ id: String(D)
866
866
  };
867
867
  }
868
- const e = globalThis[M];
868
+ const e = globalThis[U];
869
869
  const t = process.env[h];
870
870
  if (t && t !== e) {
871
871
  return {
@@ -882,11 +882,11 @@ function getProcessOrigin() {
882
882
  return null;
883
883
  }
884
884
 
885
- const R = Symbol.for("2g/process-cleanup");
885
+ const A = Symbol.for("2g/process-cleanup");
886
886
 
887
887
  function getProcessCleanupState() {
888
888
  const e = globalThis;
889
- return e[R] ??= {
889
+ return e[A] ??= {
890
890
  registered: !1,
891
891
  callbacks: new Set
892
892
  };
@@ -901,24 +901,24 @@ function runProcessCleanup() {
901
901
  }
902
902
 
903
903
  function createSession(t) {
904
- const i = y();
904
+ const i = k();
905
905
  const s = Date.now();
906
906
  const n = `${process.pid}-${s.toString(36)}-${Math.random().toString(36).slice(2, 8).padEnd(6, "0")}`;
907
907
  const r = e.join(i, n);
908
908
  try {
909
- v.mkdirSync(r, {
909
+ b.mkdirSync(r, {
910
910
  recursive: !0,
911
911
  mode: 448
912
912
  });
913
- v.chmodSync(i, 448);
913
+ b.chmodSync(i, 448);
914
914
  } catch {}
915
- const o = t.maxSegments ?? a;
916
- const c = t.maxSegmentSize ?? u;
917
- const k = f(r, n, "live");
918
- const b = f(r, n, "ipc");
919
- const L = new LogStream(e.join(r, "0.jsonl"));
920
- const _ = new BroadcastChannel(L);
921
- _.add(createDebugSink());
915
+ const o = t.maxSegments ?? (x.debug ? a : u);
916
+ const c = t.maxSegmentSize ?? (x.debug ? f : d);
917
+ const S = g(r, n, "live");
918
+ const _ = g(r, n, "ipc");
919
+ const P = new LogStream(e.join(r, "0.jsonl"));
920
+ const E = new BroadcastChannel(P);
921
+ E.add(createDebugSink());
922
922
  !function trackSegmentRotation(t, i, s, n) {
923
923
  let r = 0;
924
924
  t.on("write", o => {
@@ -928,33 +928,33 @@ function createSession(t) {
928
928
  }
929
929
  r = 0;
930
930
  !function rotateSegments(t, i) {
931
- v.rmSync(e.join(t, i - 1 + ".jsonl"), {
931
+ b.rmSync(e.join(t, i - 1 + ".jsonl"), {
932
932
  force: !0
933
933
  });
934
934
  for (let s = i - 1; s >= 1; s--) {
935
935
  const i = e.join(t, s - 1 + ".jsonl");
936
936
  const n = e.join(t, `${s}.jsonl`);
937
937
  try {
938
- v.renameSync(i, n);
938
+ b.renameSync(i, n);
939
939
  } catch {}
940
940
  }
941
941
  }(i, s);
942
942
  t.reopen();
943
943
  });
944
- }(L, r, o, c);
945
- const P = S.createServer(e => _.attach(e));
946
- const E = S.createServer(e => _.ingest(e));
947
- const I = d(P, k.path);
948
- const $ = d(E, b.path);
949
- const O = function publishIpcPath(e) {
944
+ }(P, r, o, c);
945
+ const I = L.createServer(e => E.attach(e));
946
+ const $ = L.createServer(e => E.ingest(e));
947
+ const O = p(I, S.path);
948
+ const D = p($, _.path);
949
+ const C = function publishIpcPath(e) {
950
950
  process.env[l] = e;
951
951
  return () => {
952
952
  if (process.env[l] === e) {
953
953
  delete process.env[l];
954
954
  }
955
955
  };
956
- }(b.path);
957
- const D = function publishProcessOrigin() {
956
+ }(_.path);
957
+ const N = function publishProcessOrigin() {
958
958
  const e = String(process.pid);
959
959
  process.env[h] = e;
960
960
  setLocalProcessOrigin(e);
@@ -965,40 +965,40 @@ function createSession(t) {
965
965
  setLocalProcessOrigin(void 0);
966
966
  };
967
967
  }();
968
- const C = {
968
+ const T = {
969
969
  pid: process.pid,
970
- formatVersion: m,
970
+ formatVersion: w,
971
971
  startedAt: s,
972
972
  command: t.command ?? process.argv.slice(1).join(" "),
973
973
  cwd: process.cwd(),
974
974
  maxSegments: o,
975
975
  version: t.version,
976
- socket: k.name,
977
- ipcSocket: b.name,
976
+ socket: S.name,
977
+ ipcSocket: _.name,
978
978
  origin: createSessionOrigin()
979
979
  };
980
980
  try {
981
981
  !function writeJsonAtomic(e, t) {
982
982
  const i = `${e}.${process.pid}.tmp`;
983
- v.writeFileSync(i, JSON.stringify(t, null, 2), {
983
+ b.writeFileSync(i, JSON.stringify(t, null, 2), {
984
984
  mode: 384
985
985
  });
986
- v.renameSync(i, e);
987
- }(e.join(r, g.meta), C);
986
+ b.renameSync(i, e);
987
+ }(e.join(r, m.meta), T);
988
988
  } catch {}
989
- let x = !1;
989
+ let j = !1;
990
990
  const destroy = () => {
991
- if (x) {
991
+ if (j) {
992
992
  return;
993
993
  }
994
- x = !0;
995
- _.destroy();
996
- I();
997
- $();
994
+ j = !0;
995
+ E.destroy();
998
996
  O();
999
997
  D();
1000
- w(k.path);
1001
- w(b.path);
998
+ C();
999
+ N();
1000
+ v(S.path);
1001
+ v(_.path);
1002
1002
  };
1003
1003
  !function registerProcessCleanup(e) {
1004
1004
  const t = getProcessCleanupState();
@@ -1020,13 +1020,13 @@ function createSession(t) {
1020
1020
  }(destroy);
1021
1021
  setImmediate(() => {
1022
1022
  try {
1023
- p();
1023
+ y();
1024
1024
  } catch {}
1025
1025
  });
1026
1026
  return {
1027
1027
  sessionDir: r,
1028
- meta: C,
1029
- sink: _,
1028
+ meta: T,
1029
+ sink: E,
1030
1030
  destroy
1031
1031
  };
1032
1032
  }
@@ -1046,10 +1046,10 @@ function createSessionOrigin() {
1046
1046
  };
1047
1047
  }
1048
1048
 
1049
- const U = events("root");
1049
+ const B = events("root");
1050
1050
 
1051
1051
  function installChildEventLogger(e) {
1052
- if (D.primarySink) {
1052
+ if (x.primarySink) {
1053
1053
  return !0;
1054
1054
  }
1055
1055
  const t = function getProcessWorkerId() {
@@ -1057,7 +1057,7 @@ function installChildEventLogger(e) {
1057
1057
  return e ? `${e.kind}:${e.id}` : void 0;
1058
1058
  }();
1059
1059
  if (t) {
1060
- D.eventMeta = {
1060
+ x.eventMeta = {
1061
1061
  _w: t
1062
1062
  };
1063
1063
  }
@@ -1065,16 +1065,16 @@ function installChildEventLogger(e) {
1065
1065
  return process.env[l];
1066
1066
  }();
1067
1067
  if (i) {
1068
- D.debug = e?.debug ?? !!process.env[o];
1068
+ x.debug = e?.debug ?? !!process.env[o];
1069
1069
  !function connectToParent(e) {
1070
- D.primarySink = createPrimarySink(openIpc(e), {
1070
+ x.primarySink = createPrimarySink(openIpc(e), {
1071
1071
  closeFd: !1
1072
1072
  });
1073
- D.eventLoggerInfo = {
1073
+ x.eventLoggerInfo = {
1074
1074
  destination: "ipc",
1075
1075
  isUserVisibleOutput: !1
1076
1076
  };
1077
- U("init", getInitMetadata());
1077
+ B("init", getInitMetadata());
1078
1078
  }(i);
1079
1079
  return !0;
1080
1080
  } else {
@@ -1087,13 +1087,13 @@ function installEventLogger(e) {
1087
1087
  if (installChildEventLogger(t)) {
1088
1088
  return;
1089
1089
  }
1090
- const i = parseLogTarget(process.env[k]) ?? parseLogTarget("string" == typeof e || "number" == typeof e ? e : void 0);
1090
+ const i = parseLogTarget(process.env[S]) ?? parseLogTarget("string" == typeof e || "number" == typeof e ? e : void 0);
1091
1091
  if (null != i) {
1092
1092
  if ("number" == typeof i) {
1093
- _(i);
1093
+ E(i);
1094
1094
  }
1095
- D.debug = t?.debug ?? !0;
1096
- D.eventLoggerInfo = function getExplicitTargetInfo(e) {
1095
+ x.debug = t?.debug ?? !0;
1096
+ x.eventLoggerInfo = function getExplicitTargetInfo(e) {
1097
1097
  if ("number" == typeof e) {
1098
1098
  return {
1099
1099
  destination: 1 === e ? "stdout" : 2 === e ? "stderr" : "fd",
@@ -1111,10 +1111,10 @@ function installEventLogger(e) {
1111
1111
  return;
1112
1112
  }
1113
1113
  if (t && !1 !== t.session) {
1114
- D.debug = t.debug ?? !!process.env[o];
1114
+ x.debug = t.debug ?? !!process.env[o];
1115
1115
  const e = createSession(t);
1116
- D.logPath = e.sessionDir;
1117
- D.eventLoggerInfo = {
1116
+ x.logPath = e.sessionDir;
1117
+ x.eventLoggerInfo = {
1118
1118
  destination: "session",
1119
1119
  isUserVisibleOutput: !1,
1120
1120
  sessionDir: e.sessionDir
@@ -1124,11 +1124,11 @@ function installEventLogger(e) {
1124
1124
  }
1125
1125
  }
1126
1126
 
1127
- const getEventLoggerInfo = () => D.primarySink?.writable ? D.eventLoggerInfo : null;
1127
+ const getEventLoggerInfo = () => x.primarySink?.writable ? x.eventLoggerInfo : null;
1128
1128
 
1129
1129
  function flushEventLogger() {
1130
1130
  return new Promise(e => {
1131
- const t = D.primarySink;
1131
+ const t = x.primarySink;
1132
1132
  if (t?.flush) {
1133
1133
  t.flush(() => e());
1134
1134
  } else {
@@ -1151,7 +1151,7 @@ function parseLogTarget(t) {
1151
1151
  try {
1152
1152
  const i = e.parse(t);
1153
1153
  const s = e.format(i);
1154
- D.logPath = i.dir || process.cwd();
1154
+ x.logPath = i.dir || process.cwd();
1155
1155
  return s;
1156
1156
  } catch {
1157
1157
  return;
@@ -1161,8 +1161,8 @@ function parseLogTarget(t) {
1161
1161
  function createPrimarySink(e, t) {
1162
1162
  const i = new LogStream(e, t);
1163
1163
  i.once("error", () => {
1164
- D.primarySink = void 0;
1165
- D.eventLoggerInfo = null;
1164
+ x.primarySink = void 0;
1165
+ x.eventLoggerInfo = null;
1166
1166
  });
1167
1167
  return i;
1168
1168
  }
@@ -1170,15 +1170,15 @@ function createPrimarySink(e, t) {
1170
1170
  function getInitMetadata(e) {
1171
1171
  return {
1172
1172
  format: "v0-jsonl",
1173
- formatVersion: m,
1173
+ formatVersion: w,
1174
1174
  version: e ?? "UNVERSIONED",
1175
1175
  processOrigin: getProcessOrigin() ?? void 0
1176
1176
  };
1177
1177
  }
1178
1178
 
1179
1179
  function activateSink(e, t) {
1180
- D.primarySink = e;
1181
- U("init", getInitMetadata(t));
1180
+ x.primarySink = e;
1181
+ B("init", getInitMetadata(t));
1182
1182
  }
1183
1183
 
1184
1184
  installChildEventLogger();