@agentvault/agentvault 0.9.6 → 0.9.8

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.
@@ -0,0 +1,4 @@
1
+ // Re-export transport utilities from shared @agentvault/crypto package.
2
+ // Plugin code continues importing from ./crypto-helpers.js — no import changes needed.
3
+ export { hexToBytes, bytesToHex, base64ToBytes, bytesToBase64, encryptedMessageToTransport, transportToEncryptedMessage, } from "@agentvault/crypto";
4
+ //# sourceMappingURL=crypto-helpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crypto-helpers.js","sourceRoot":"","sources":["../src/crypto-helpers.ts"],"names":[],"mappings":"AAAA,wEAAwE;AACxE,uFAAuF;AACvF,OAAO,EACL,UAAU,EACV,UAAU,EACV,aAAa,EACb,aAAa,EACb,2BAA2B,EAC3B,2BAA2B,GAE5B,MAAM,oBAAoB,CAAC"}
package/dist/index.js CHANGED
@@ -6,7 +6,7 @@ import { writeFile as writeFile2, mkdir as mkdir2 } from "node:fs/promises";
6
6
  import { join as join2 } from "node:path";
7
7
  import { readFile as readFile2 } from "node:fs/promises";
8
8
 
9
- // ../../../../../node_modules/libsodium-sumo/dist/modules-sumo-esm/libsodium-sumo.mjs
9
+ // ../../node_modules/libsodium-sumo/dist/modules-sumo-esm/libsodium-sumo.mjs
10
10
  var __filename;
11
11
  var __dirname;
12
12
  try {
@@ -73,9 +73,9 @@ if (typeof Module.getRandomValue === "undefined") {
73
73
  Module.getRandomValue = randomValuesStandard;
74
74
  } catch (e) {
75
75
  try {
76
- crypto = null;
76
+ crypto2 = null;
77
77
  randomValueNodeJS = function() {
78
- var buf = crypto["randomBytes"](4);
78
+ var buf = crypto2["randomBytes"](4);
79
79
  return (buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3]) >>> 0;
80
80
  };
81
81
  randomValueNodeJS();
@@ -88,7 +88,7 @@ if (typeof Module.getRandomValue === "undefined") {
88
88
  var window_;
89
89
  var crypto_;
90
90
  var randomValuesStandard;
91
- var crypto;
91
+ var crypto2;
92
92
  var randomValueNodeJS;
93
93
  var _Module = Module;
94
94
  Module.ready = new Promise(function(resolve2, reject) {
@@ -214,7 +214,7 @@ Module.ready = new Promise(function(resolve2, reject) {
214
214
  }, Module: function(binary) {
215
215
  }, Instance: function(module2, info) {
216
216
  this.exports = // EMSCRIPTEN_START_ASM
217
- function instantiate(ea2) {
217
+ (function instantiate(ea2) {
218
218
  var a2;
219
219
  var b2 = new Uint8Array(123);
220
220
  for (var c2 = 25; c2 >= 0; --c2) {
@@ -40009,7 +40009,7 @@ Module.ready = new Promise(function(resolve2, reject) {
40009
40009
  } } }), f: yi, g: ma2, h: Ba2, i: ma2, j: ka2, k: Le2, l: Je2, m: zg, n: yg, o: xg, p: wg, q: ka2, r: Ba2, s: ka2, t: ka2, u: Le2, v: ra2, w: vg, x: ug, y: tg, z: sg, A: Ba2, B: rg, C: qg, D: pg, E: ng, F: mg, G: lg, H: kg, I: jg, J: ka2, K: Ce2, L: Ba2, M: ma2, N: Ta2, O: ra2, P: ka2, Q: Oa2, R: Ba2, S: ma2, T: Ta2, U: ra2, V: ig, W: hg, X: gg, Y: fg, Z: ka2, _: Ma2, $: Ba2, aa: ma2, ba: Ta2, ca: ra2, da: ka2, ea: ka2, fa: Kg, ga: wd, ha: vd, ia: ra2, ja: ka2, ka: ka2, la: Cb, ma: ra2, na: Xa2, oa: eg, pa: qb, qa: dg, ra: cg, sa: ua2, ta: ka2, ua: Dc, va: ra2, wa: Va2, xa: ud, ya: Ya2, za: bg, Aa: ag, Ba: ka2, Ca: ka2, Da: Dc, Ea: ra2, Fa: ze2, Ga: ud, Ha: Ng, Ia: wd, Ja: vd, Ka: ka2, La: ka2, Ma: ka2, Na: ka2, Oa: Ma2, Pa: ka2, Qa: ma2, Ra: ma2, Sa: Ta2, Ta: dh2, Ua: ch, Va: bh, Wa: ah, Xa: Lb, Ya: Kb, Za: td, _a: sd, $a: rd, ab: $f, bb: _f, cb: Zf, db: qd, eb: Yf, fb: pd, gb: Xf, hb: Wf, ib: Uf, jb: Md, kb: tc, lb: nb, mb, nb: Lb, ob: Kb, pb: td, qb: sd, rb: ka2, sb: ka2, tb: ka2, ub: ka2, vb: Ma2, wb: ka2, xb: ma2, yb: ma2, zb: Ta2, Ab: Aa2, Bb: ka2, Cb: ma2, Db: ka2, Eb: ma2, Fb: Sa2, Gb: ka2, Hb: ma2, Ib: ka2, Jb: ma2, Kb: vb, Lb: ua2, Mb: ma2, Nb: ka2, Ob: ma2, Pb: ub, Qb: ua2, Rb: ma2, Sb: ka2, Tb: ma2, Ub: tb, Vb: ua2, Wb: ma2, Xb: ka2, Yb: ma2, Zb: ma2, _b: ua2, $b: ka2, ac: ma2, bc: ua2, cc: ka2, dc: Nd, ec: pe2, fc: od, gc: fh, hc: nd, ic: eh, jc: ra2, kc: ma2, lc: ua2, mc: ka2, nc: ma2, oc: ua2, pc: ka2, qc: ma2, rc: ma2, sc: pe2, tc: ra2, uc: od, vc: Tf, wc: Ja2, xc: xh, yc: nd, zc: Ia2, Ac: ua2, Bc: md, Cc: Lg, Dc: ka2, Ec: Ag, Fc: Ub, Gc: Sf, Hc: Ua2, Ic: Rf, Jc: ua2, Kc: Cb, Lc: Za2, Mc: ic, Nc: Ca2, Oc: md, Pc: ma2, Qc: ua2, Rc: Oa2, Sc: ka2, Tc: ld, Uc: Nd, Vc: ma2, Wc: ua2, Xc: Oa2, Yc: ka2, Zc: ld, _c: ra2, $c: Gh, ad: Fh, bd: Eh, cd: Dh, dd: ra2, ed: Ch, fd: ka2, gd: Ba2, hd: Bh, id: Cb, jd: ze2, kd: hi, ld: gi, md: fi, nd: ei, od: di, pd: ua2, qd: Ba2, rd: ci, sd: Dc, td: Gg, ud: nb, vd: Fg, wd: Eg, xd: ka2, yd: ka2, zd: ka2, Ad: ka2, Bd: Dg, Cd: Rc, Dd: ma2, Ed: ka2, Fd: kd, Gd: jd, Hd: Sg, Id: id, Jd: Rg, Kd: Qg, Ld: ra2, Md: kd, Nd: jd, Od: Ga2, Pd: id, Qd: Fa2, Rd: ma2, Sd: ka2, Td: Rc, Ud: ra2, Vd: Db, Wd: ma2, Xd: wa2, Yd: Ba2, Zd: wa2, _d: ma2, $d: yc, ae: wh, be: Vb, ce: wa2, de: xc, ee: wc, fe: vc, ge: me2, he: vh, ie: uh, je: Oa2, ke: th, le: Pf, me: Of, ne: Nf, oe: Mf, pe: Lf, qe: fb, re: ma2, se: wa2, te: Ba2, ue: wa2, ve: ma2, we: yc, xe: fe2, ye: Db, ze: wa2, Ae: xc, Be: wc, Ce: fb, De: ee2, Ee: Vb, Fe: de2, Ge: vc, He: uc, Ie: Kf, Je: hd, Ke: Jf, Le: Db, Me: fb, Ne: fb, Oe: ma2, Pe: wa2, Qe: Ba2, Re: wa2, Se: ma2, Te: yc, Ue: fe2, Ve: Db, We: wa2, Xe: xc, Ye: wc, Ze: fb, _e: ee2, $e: Vb, af: de2, bf: vc, cf: uc, df: Hf, ef: hd, ff: Gf, gf: Ff, hf: Ef, jf: sh, kf: Jg, lf: Ig, mf: Hg, nf: ka2, of: ka2, pf: Ea2, qf: wb, rf: ka2, sf: ka2, tf: ka2, uf: Ma2, vf: ka2, wf: ma2, xf: ma2, yf: Ta2, zf: Mg, Af: Lb, Bf: Kb, Cf: ra2, Df: rd, Ef: Df, Ff: qd, Gf: pd, Hf: Lb, If: Kb, Jf: ka2, Kf: Ma2, Lf: ka2, Mf: ma2, Nf: ma2, Of: Ta2, Pf: ra2, Qf: ra2, Rf: Mh, Sf: Lh, Tf: Kh, Uf: Cf, Vf: Bf, Wf: Jh, Xf: Ih, Yf: Ma2, Zf: ka2, _f: Hh, $f: Ba2, ag: Db, bg: fb, cg: Vb, dg: Oa2, eg: ma2, fg: Tg, gg: gd, hg: Je2, ig: Oa2, jg: ma2, kg: gd, lg: Cb, mg: ua2, ng: ka2, og: ka2, pg: ua2, qg: Wd, rg: lh, sg: kh, tg: jh, ug: fd, vg: ed, wg: dd, xg: cd, yg: ih, zg: ic, Ag: hh, Bg: gh, Cg: Cb, Dg: ua2, Eg: ka2, Fg: ka2, Gg: ua2, Hg: Wd, Ig: nh, Jg: mh, Kg: Vd, Lg: ic, Mg: Ud, Ng: Td, Og: Yd, Pg: Xd, Qg: ph, Rg: oh, Sg: cd, Tg: ed, Ug: dd, Vg: fd, Wg: ka2, Xg: Oa2, Yg: wa2, Zg: ka2, _g: Ce2, $g: wa2, ah: zf, bh: yf, ch: xf, dh: wf, eh: vf, fh: uf, gh: ra2, hh: ra2, ih: ka2, jh: Ma2, kh: wa2, lh: qh, mh: bd, nh: ad, oh: ra2, ph: ka2, qh: Oa2, rh: wa2, sh: tf, th: sf, uh: rf, vh: ra2, wh: bd, xh: qf, yh: ad, zh: ka2, Ah: Ma2, Bh: wa2, Ch: ra2, Dh: ma2, Eh: ka2, Fh: ua2, Gh: db, Hh: kb, Ih: xd, Jh: oi, Kh: ni, Lh: Ae2, Mh: mi, Nh: za2, Oh: li, Ph: ka2, Qh: ki, Rh: pf, Sh: Ah, Th: zh, Uh: yh, Vh: Bc, Wh: Ac, Xh: ti, Yh: ji, Zh: ii, _h: Cg, $h: Bg, ai: fb, bi: Ba2, ci: tc, di: nb, ei: rh, fi: $c, gi: of, hi: nf, ii: mf, ji: _c, ki: lf, li: Yc, mi: kf, ni: ka2, oi: ka2, pi: ka2, qi: ka2, ri: Ma2, si: ma2, ti: Ta2, ui: jf, vi: hf, wi: Md, xi: bi, yi: ai, zi: $h, Ai: _h, Bi: Zh, Ci: ye2, Di: xe2, Ei: we2, Fi: ve2, Gi: ue2, Hi: te2, Ii: se2, Ji: re2, Ki: ka2, Li: ua2, Mi: ka2, Ni: ua2, Oi: ka2, Pi: Yh, Qi: Xh, Ri: Wh, Si: Vh, Ti: Uh, Ui: Th, Vi: Sh, Wi: Rh, Xi: Qh, Yi: Ph, Zi: Oh, _i: re2, $i: Nh, aj: ka2, bj: ua2, cj: ua2, dj: ka2, ej: gf, fj: ma2, gj: wa2, hj: Ba2, ij: wa2, jj: ka2, kj: $g, lj: _g, mj: Zg, nj: wa2, oj: Ed, pj: wa2, qj: Yg, rj: Ed, sj: me2, tj: uc, uj: ff, vj: ef, wj: df, xj: cf, yj: Xg, zj: Wg, Aj: Vg, Bj: Ug, Cj: ka2, Dj: ka2, Ej: Pg, Fj: Og, Gj: ka2, Hj: ka2, Ij: $c, Jj: bf, Kj: _c, Lj: Yc, Mj: ka2, Nj: Ma2, Oj: ma2, Pj: Ta2, Qj: ma2, Rj: ma2, Sj: af, Tj: $e2, Uj: _e2, Vj: ka2, Wj: Oa2, Xj: wa2, Yj: ra2, Zj: Ze2, _j: Ye2, $j: ka2, ak: Oa2, bk: wa2, ck: ra2, dk: ka2, ek: Ma2, fk: wa2, gk: Xe2, hk: We2, ik: Ve2, jk: ra2, kk: Da2, lk: ta2 };
40010
40010
  }
40011
40011
  return da2(ea2);
40012
- }(info);
40012
+ })(info);
40013
40013
  }, instantiate: function(binary, info) {
40014
40014
  return { then: function(ok) {
40015
40015
  var module2 = new WebAssembly2.Module(binary);
@@ -40283,7 +40283,7 @@ Module.ready = new Promise(function(resolve2, reject) {
40283
40283
  try {
40284
40284
  var window_ = "object" === typeof window ? window : self;
40285
40285
  var crypto_ = typeof window_.crypto !== "undefined" ? window_.crypto : window_.msCrypto;
40286
- crypto_ = crypto_ === void 0 ? crypto : crypto_;
40286
+ crypto_ = crypto_ === void 0 ? crypto2 : crypto_;
40287
40287
  var randomValuesStandard = function() {
40288
40288
  var buf = new Uint32Array(1);
40289
40289
  crypto_.getRandomValues(buf);
@@ -40293,9 +40293,9 @@ Module.ready = new Promise(function(resolve2, reject) {
40293
40293
  Module3.getRandomValue = randomValuesStandard;
40294
40294
  } catch (e) {
40295
40295
  try {
40296
- var crypto = null;
40296
+ var crypto2 = null;
40297
40297
  var randomValueNodeJS = function() {
40298
- var buf = crypto["randomBytes"](4);
40298
+ var buf = crypto2["randomBytes"](4);
40299
40299
  return (buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3]) >>> 0;
40300
40300
  };
40301
40301
  randomValueNodeJS();
@@ -41343,7 +41343,7 @@ Module.ready = new Promise(function(resolve2, reject) {
41343
41343
  try {
41344
41344
  var window_ = "object" === typeof window ? window : self;
41345
41345
  var crypto_ = typeof window_.crypto !== "undefined" ? window_.crypto : window_.msCrypto;
41346
- crypto_ = crypto_ === void 0 ? crypto : crypto_;
41346
+ crypto_ = crypto_ === void 0 ? crypto2 : crypto_;
41347
41347
  var randomValuesStandard = function() {
41348
41348
  var buf = new Uint32Array(1);
41349
41349
  crypto_.getRandomValues(buf);
@@ -41353,9 +41353,9 @@ Module.ready = new Promise(function(resolve2, reject) {
41353
41353
  Module2.getRandomValue = randomValuesStandard;
41354
41354
  } catch (e) {
41355
41355
  try {
41356
- var crypto = null;
41356
+ var crypto2 = null;
41357
41357
  var randomValueNodeJS = function() {
41358
- var buf = crypto["randomBytes"](4);
41358
+ var buf = crypto2["randomBytes"](4);
41359
41359
  return (buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3]) >>> 0;
41360
41360
  };
41361
41361
  randomValueNodeJS();
@@ -42011,7 +42011,7 @@ Module.ready = new Promise(function(resolve2, reject) {
42011
42011
  });
42012
42012
  var libsodium_sumo_default = Module;
42013
42013
 
42014
- // ../../../../../node_modules/libsodium-wrappers-sumo/dist/modules-sumo-esm/libsodium-wrappers.mjs
42014
+ // ../../node_modules/libsodium-wrappers-sumo/dist/modules-sumo-esm/libsodium-wrappers.mjs
42015
42015
  var a;
42016
42016
  var r = {};
42017
42017
  var t = libsodium_sumo_default.ready.then(function() {
@@ -44611,7 +44611,7 @@ var libsodium_wrappers_default = r;
44611
44611
  // src/channel.ts
44612
44612
  import WebSocket from "ws";
44613
44613
 
44614
- // ../../../../../packages/crypto/dist/keys.js
44614
+ // ../crypto/dist/keys.js
44615
44615
  async function generateIdentityKeypair() {
44616
44616
  await libsodium_wrappers_default.ready;
44617
44617
  const kp = libsodium_wrappers_default.crypto_sign_keypair();
@@ -44638,7 +44638,7 @@ function createProofOfPossession(privateKey, publicKey) {
44638
44638
  return libsodium_wrappers_default.crypto_sign_detached(publicKey, privateKey);
44639
44639
  }
44640
44640
 
44641
- // ../../../../../packages/crypto/dist/x3dh.js
44641
+ // ../crypto/dist/x3dh.js
44642
44642
  function ed25519PrivateToX25519(edPrivate) {
44643
44643
  return libsodium_wrappers_default.crypto_sign_ed25519_sk_to_curve25519(edPrivate);
44644
44644
  }
@@ -44660,7 +44660,7 @@ function performX3DH(params) {
44660
44660
  return libsodium_wrappers_default.crypto_generichash(32, combined);
44661
44661
  }
44662
44662
 
44663
- // ../../../../../packages/crypto/dist/ratchet.js
44663
+ // ../crypto/dist/ratchet.js
44664
44664
  var MAX_SKIP = 100;
44665
44665
  var CHAIN_KEY_SEED = new Uint8Array([1]);
44666
44666
  var MSG_KEY_SEED = new Uint8Array([2]);
@@ -44766,6 +44766,22 @@ var DoubleRatchet = class _DoubleRatchet {
44766
44766
  const currentDhHex = this.state.dhReceivingPublicKey ? libsodium_wrappers_default.to_hex(this.state.dhReceivingPublicKey) : null;
44767
44767
  if (currentDhHex === null && this.state.receivingChain) {
44768
44768
  this.state.dhReceivingPublicKey = message.header.dhPublicKey;
44769
+ } else if (currentDhHex === null && !this.state.receivingChain) {
44770
+ if (message.header.messageNumber === 0) {
44771
+ try {
44772
+ const { messageKey: testKey, nextChainKey: nextChainKey2 } = kdfChainKey(this.state.rootKey);
44773
+ const ad2 = serializeHeader(message.header);
44774
+ const ptBytes = libsodium_wrappers_default.crypto_aead_xchacha20poly1305_ietf_decrypt(null, message.ciphertext, ad2, message.nonce, testKey);
44775
+ this.state.dhReceivingPublicKey = message.header.dhPublicKey;
44776
+ this.state.receivingChain = {
44777
+ chainKey: nextChainKey2,
44778
+ messageNumber: 1
44779
+ };
44780
+ return libsodium_wrappers_default.to_string(ptBytes);
44781
+ } catch {
44782
+ }
44783
+ }
44784
+ this.dhRatchetReceive(message.header.dhPublicKey);
44769
44785
  } else if (headerDhHex !== currentDhHex) {
44770
44786
  if (this.state.receivingChain && this.state.dhReceivingPublicKey) {
44771
44787
  this.skipMessages(this.state.receivingChain, message.header.previousChainLength, this.state.dhReceivingPublicKey);
@@ -44921,7 +44937,7 @@ var DoubleRatchet = class _DoubleRatchet {
44921
44937
  }
44922
44938
  };
44923
44939
 
44924
- // ../../../../../packages/crypto/dist/file-crypto.js
44940
+ // ../crypto/dist/file-crypto.js
44925
44941
  function encryptFile(plainData) {
44926
44942
  const fileKey = libsodium_wrappers_default.randombytes_buf(libsodium_wrappers_default.crypto_aead_xchacha20poly1305_ietf_KEYBYTES);
44927
44943
  const fileNonce = libsodium_wrappers_default.randombytes_buf(libsodium_wrappers_default.crypto_aead_xchacha20poly1305_ietf_NPUBBYTES);
@@ -44954,7 +44970,7 @@ function computeFileDigest(data) {
44954
44970
  return libsodium_wrappers_default.to_hex(digestBytes);
44955
44971
  }
44956
44972
 
44957
- // ../../../../../packages/crypto/dist/scan-engine.js
44973
+ // ../crypto/dist/scan-engine.js
44958
44974
  var BUILTIN_PATTERNS = {
44959
44975
  pii_ssn: [/\b\d{3}-\d{2}-\d{4}\b/g, /\b\d{9}\b/g],
44960
44976
  pii_credit_card: [
@@ -45106,7 +45122,7 @@ var ScanEngine = class {
45106
45122
  }
45107
45123
  };
45108
45124
 
45109
- // ../../../../../packages/crypto/dist/transport.js
45125
+ // ../crypto/dist/transport.js
45110
45126
  function hexToBytes(hex) {
45111
45127
  return libsodium_wrappers_default.from_hex(hex);
45112
45128
  }
@@ -45149,6 +45165,208 @@ function transportToEncryptedMessage(transport) {
45149
45165
  };
45150
45166
  }
45151
45167
 
45168
+ // ../crypto/dist/telemetry.js
45169
+ function randomHex(byteCount) {
45170
+ const bytes = new Uint8Array(byteCount);
45171
+ crypto.getRandomValues(bytes);
45172
+ let hex = "";
45173
+ for (let i2 = 0; i2 < bytes.length; i2++) {
45174
+ hex += bytes[i2].toString(16).padStart(2, "0");
45175
+ }
45176
+ return hex;
45177
+ }
45178
+ function generateTraceId() {
45179
+ return randomHex(16);
45180
+ }
45181
+ function generateSpanId() {
45182
+ return randomHex(8);
45183
+ }
45184
+ function buildLlmSpan(opts) {
45185
+ const now = Date.now();
45186
+ const attributes = {
45187
+ "ai.agent.llm.model": opts.model,
45188
+ "ai.agent.llm.latency_ms": opts.latencyMs,
45189
+ "ai.agent.llm.tokens_input": opts.tokensInput,
45190
+ "ai.agent.llm.tokens_output": opts.tokensOutput
45191
+ };
45192
+ if (opts.provider !== void 0) {
45193
+ attributes["ai.agent.llm.provider"] = opts.provider;
45194
+ }
45195
+ const isError = opts.status === "error";
45196
+ const status = isError ? { code: 2, ...opts.statusMessage ? { message: opts.statusMessage } : {} } : { code: 0 };
45197
+ return {
45198
+ traceId: opts.traceId ?? generateTraceId(),
45199
+ spanId: opts.spanId ?? generateSpanId(),
45200
+ parentSpanId: opts.parentSpanId,
45201
+ name: "llm.inference",
45202
+ kind: "internal",
45203
+ startTime: now - opts.latencyMs,
45204
+ endTime: now,
45205
+ attributes,
45206
+ status
45207
+ };
45208
+ }
45209
+ function buildToolSpan(opts) {
45210
+ const now = Date.now();
45211
+ const attributes = {
45212
+ "ai.agent.tool.name": opts.toolName,
45213
+ "ai.agent.tool.latency_ms": opts.latencyMs,
45214
+ "ai.agent.tool.success": opts.success
45215
+ };
45216
+ const status = opts.success ? { code: 0 } : { code: 2, ...opts.errorMessage ? { message: opts.errorMessage } : {} };
45217
+ return {
45218
+ traceId: opts.traceId ?? generateTraceId(),
45219
+ spanId: opts.spanId ?? generateSpanId(),
45220
+ parentSpanId: opts.parentSpanId,
45221
+ name: "tool.execute",
45222
+ kind: "internal",
45223
+ startTime: now - opts.latencyMs,
45224
+ endTime: now,
45225
+ attributes,
45226
+ status
45227
+ };
45228
+ }
45229
+ function buildErrorSpan(opts) {
45230
+ const now = Date.now();
45231
+ return {
45232
+ traceId: opts.traceId ?? generateTraceId(),
45233
+ spanId: opts.spanId ?? generateSpanId(),
45234
+ parentSpanId: opts.parentSpanId,
45235
+ name: "error",
45236
+ kind: opts.spanKind ?? "internal",
45237
+ startTime: now,
45238
+ endTime: now,
45239
+ attributes: {
45240
+ "ai.agent.error.type": opts.errorType,
45241
+ "ai.agent.error.message": opts.errorMessage
45242
+ },
45243
+ status: { code: 2, message: opts.errorMessage }
45244
+ };
45245
+ }
45246
+
45247
+ // ../crypto/dist/telemetry-reporter.js
45248
+ function toOtlpAttributes(attrs) {
45249
+ return Object.entries(attrs).map(([key, val]) => {
45250
+ if (typeof val === "string") {
45251
+ return { key, value: { stringValue: val } };
45252
+ }
45253
+ if (typeof val === "boolean") {
45254
+ return { key, value: { boolValue: val } };
45255
+ }
45256
+ if (Number.isInteger(val)) {
45257
+ return { key, value: { intValue: val } };
45258
+ }
45259
+ return { key, value: { doubleValue: val } };
45260
+ });
45261
+ }
45262
+ function spanToOtlp(span) {
45263
+ const otlp = {
45264
+ traceId: span.traceId,
45265
+ spanId: span.spanId,
45266
+ name: span.name,
45267
+ kind: span.kind,
45268
+ startTimeUnixNano: String(span.startTime * 1e6),
45269
+ endTimeUnixNano: String(span.endTime * 1e6),
45270
+ attributes: toOtlpAttributes(span.attributes)
45271
+ };
45272
+ if (span.parentSpanId !== void 0) {
45273
+ otlp.parentSpanId = span.parentSpanId;
45274
+ }
45275
+ if (span.status) {
45276
+ otlp.status = span.status;
45277
+ }
45278
+ if (span.events && span.events.length > 0) {
45279
+ otlp.events = span.events;
45280
+ }
45281
+ return otlp;
45282
+ }
45283
+ var TelemetryReporter = class {
45284
+ _apiBase;
45285
+ _hubId;
45286
+ _authHeader;
45287
+ _fetch;
45288
+ _buffer = [];
45289
+ _timer = null;
45290
+ constructor(config) {
45291
+ this._apiBase = config.apiBase.replace(/\/+$/, "");
45292
+ this._hubId = config.hubId;
45293
+ this._authHeader = config.authHeader;
45294
+ this._fetch = config.fetchImpl ?? globalThis.fetch;
45295
+ }
45296
+ /** Number of spans waiting to be flushed. */
45297
+ get pendingCount() {
45298
+ return this._buffer.length;
45299
+ }
45300
+ // -- Report methods ---------------------------------------------------------
45301
+ /** Record an LLM inference call. */
45302
+ reportLlmCall(opts) {
45303
+ this._buffer.push(buildLlmSpan(opts));
45304
+ }
45305
+ /** Record a tool/function invocation. */
45306
+ reportToolCall(opts) {
45307
+ this._buffer.push(buildToolSpan(opts));
45308
+ }
45309
+ /** Record an error event. */
45310
+ reportError(opts) {
45311
+ this._buffer.push(buildErrorSpan(opts));
45312
+ }
45313
+ /** Record an arbitrary pre-built span. */
45314
+ reportCustomSpan(span) {
45315
+ this._buffer.push(span);
45316
+ }
45317
+ // -- Flush ------------------------------------------------------------------
45318
+ /**
45319
+ * POST all buffered spans to the backend ingest endpoint.
45320
+ *
45321
+ * - On success (HTTP 2xx): clears the buffer.
45322
+ * - On failure: keeps spans in the buffer for retry.
45323
+ * - Never throws — telemetry is best-effort.
45324
+ */
45325
+ async flush() {
45326
+ if (this._buffer.length === 0) {
45327
+ return;
45328
+ }
45329
+ const spans = this._buffer;
45330
+ this._buffer = [];
45331
+ try {
45332
+ const response = await this._fetch(`${this._apiBase}/api/v1/telemetry/ingest`, {
45333
+ method: "POST",
45334
+ headers: {
45335
+ "Content-Type": "application/json",
45336
+ Authorization: this._authHeader
45337
+ },
45338
+ body: JSON.stringify({
45339
+ hub_id: this._hubId,
45340
+ spans: spans.map(spanToOtlp)
45341
+ })
45342
+ });
45343
+ if (!response.ok) {
45344
+ this._buffer = spans.concat(this._buffer);
45345
+ }
45346
+ } catch {
45347
+ this._buffer = spans.concat(this._buffer);
45348
+ }
45349
+ }
45350
+ // -- Auto-flush -------------------------------------------------------------
45351
+ /**
45352
+ * Start a periodic flush timer.
45353
+ * @param intervalMs Flush interval in milliseconds (default 30 000).
45354
+ */
45355
+ startAutoFlush(intervalMs = 3e4) {
45356
+ this.stopAutoFlush();
45357
+ this._timer = setInterval(() => {
45358
+ void this.flush();
45359
+ }, intervalMs);
45360
+ }
45361
+ /** Stop the periodic flush timer. Safe to call when not started. */
45362
+ stopAutoFlush() {
45363
+ if (this._timer !== null) {
45364
+ clearInterval(this._timer);
45365
+ this._timer = null;
45366
+ }
45367
+ }
45368
+ };
45369
+
45152
45370
  // src/state.ts
45153
45371
  import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
45154
45372
  import { join } from "node:path";
@@ -45296,6 +45514,7 @@ var SecureChannel = class _SecureChannel extends EventEmitter {
45296
45514
  _a2aPendingQueue = {};
45297
45515
  _scanEngine = null;
45298
45516
  _scanRuleSetVersion = 0;
45517
+ _telemetryReporter = null;
45299
45518
  /** Topic ID from the most recent inbound message — used as fallback for replies. */
45300
45519
  _lastIncomingTopicId;
45301
45520
  // Liveness detection: server sends app-level {"event":"ping"} every 30s.
@@ -45326,6 +45545,10 @@ var SecureChannel = class _SecureChannel extends EventEmitter {
45326
45545
  get sessionCount() {
45327
45546
  return this._sessions.size;
45328
45547
  }
45548
+ /** Returns the TelemetryReporter instance (available after WebSocket connect). */
45549
+ get telemetry() {
45550
+ return this._telemetryReporter;
45551
+ }
45329
45552
  async start() {
45330
45553
  this._stopped = false;
45331
45554
  await libsodium_wrappers_default.ready;
@@ -45870,6 +46093,11 @@ var SecureChannel = class _SecureChannel extends EventEmitter {
45870
46093
  clearTimeout(this._reconnectTimer);
45871
46094
  this._reconnectTimer = null;
45872
46095
  }
46096
+ if (this._telemetryReporter) {
46097
+ this._telemetryReporter.stopAutoFlush();
46098
+ await this._telemetryReporter.flush();
46099
+ this._telemetryReporter = null;
46100
+ }
45873
46101
  if (this._ws) {
45874
46102
  this._ws.removeAllListeners();
45875
46103
  this._ws.close();
@@ -46393,6 +46621,31 @@ var SecureChannel = class _SecureChannel extends EventEmitter {
46393
46621
  this._scanEngine = new ScanEngine();
46394
46622
  await this._fetchScanRules();
46395
46623
  }
46624
+ if (this._persisted && this._persisted.hubAddress && !this._persisted.hubId && this._persisted.deviceJwt) {
46625
+ try {
46626
+ const listRes = await fetch(
46627
+ `${this.config.apiUrl}/api/v1/hub/identities`,
46628
+ { headers: { Authorization: `Bearer ${this._persisted.deviceJwt}` } }
46629
+ );
46630
+ if (listRes.ok) {
46631
+ const identities = await listRes.json();
46632
+ const match = identities.find((h2) => h2.hub_address === this._persisted.hubAddress);
46633
+ if (match) {
46634
+ this._persisted.hubId = match.hub_id;
46635
+ this._persistState();
46636
+ }
46637
+ }
46638
+ } catch {
46639
+ }
46640
+ }
46641
+ if (!this._telemetryReporter && this._persisted?.deviceJwt && this._persisted?.hubId) {
46642
+ this._telemetryReporter = new TelemetryReporter({
46643
+ apiBase: this.config.apiUrl,
46644
+ hubId: this._persisted.hubId,
46645
+ authHeader: `Bearer ${this._persisted.deviceJwt}`
46646
+ });
46647
+ this._telemetryReporter.startAutoFlush(3e4);
46648
+ }
46396
46649
  this.emit("ready");
46397
46650
  } catch (openErr) {
46398
46651
  console.error("[SecureChannel] Error in WS open handler:", openErr);
@@ -46494,6 +46747,7 @@ var SecureChannel = class _SecureChannel extends EventEmitter {
46494
46747
  if (data.event === "hub_identity_assigned") {
46495
46748
  if (this._persisted) {
46496
46749
  this._persisted.hubAddress = data.data.hub_address;
46750
+ this._persisted.hubId = data.data.hub_id;
46497
46751
  this._persistState();
46498
46752
  }
46499
46753
  this.emit("hub_identity_assigned", data.data);
@@ -46501,6 +46755,7 @@ var SecureChannel = class _SecureChannel extends EventEmitter {
46501
46755
  if (data.event === "hub_identity_removed") {
46502
46756
  if (this._persisted) {
46503
46757
  delete this._persisted.hubAddress;
46758
+ delete this._persisted.hubId;
46504
46759
  this._persistState();
46505
46760
  }
46506
46761
  this.emit("hub_identity_removed", data.data);