@frequencytalk/agent 0.2.3 → 0.2.4

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/cli.js CHANGED
@@ -4655,6 +4655,9 @@ var BlockedWordsSchema = z14.object({ words: z14.array(BlockedWordSchema).max(2e
4655
4655
 
4656
4656
  // ../shared/dist/nativeTransport.js
4657
4657
  var NATIVE_TRANSPORT_VERSION = 1;
4658
+ var NATIVE_TRANSPORT_WIRE_V2 = 2;
4659
+ var FLAG_CONTROL = 16;
4660
+ var FLAG_CONTENT_KEY = 32;
4658
4661
  var NODE_SEQUENCE_BASE = 1n << 63n;
4659
4662
  var HEADER = {
4660
4663
  /** version (low nibble) + flags (high nibble). */
@@ -4713,6 +4716,19 @@ var PAYLOAD = {
4713
4716
  OPUS_OFFSET: 9
4714
4717
  };
4715
4718
  var PAYLOAD_TYPE = { audio: 0, keepalive: 1, contentKey: 2, videoShard: 3, shareAudio: 4 };
4719
+ var CONTENT_KEY_PAYLOAD = {
4720
+ TYPE_OFFSET: 0,
4721
+ /** uint32. The stream whose audio `K_s` seals. */
4722
+ STREAM_ID_OFFSET: 1,
4723
+ /** uint32, 48 kHz. The talkspurt's timestamp when this key packet was sent; informational. */
4724
+ TIMESTAMP_OFFSET: 5,
4725
+ /** uint32. Monotonic per stream; its LOW BYTE is the generation an audio header carries. */
4726
+ CONTENT_KEY_ID_OFFSET: 9,
4727
+ /** 32 bytes: `K_s`, an AES-256-GCM key. */
4728
+ KEY_OFFSET: 13,
4729
+ LENGTH: 45
4730
+ };
4731
+ var SHARE_CHANNEL_ID = 4294967295;
4716
4732
  var VIDEO_SHARD = {
4717
4733
  TYPE_OFFSET: 0,
4718
4734
  STREAM_ID_OFFSET: 1,
@@ -4751,8 +4767,13 @@ var PROJECTS_BY_PLATFORM = {
4751
4767
  // than a rider on `broadcast` (a Windows lane) or `tuner_apple`, for the reason `broadcast` got
4752
4768
  // one: the board enforces one in-flight task per PROJECT, so a shared lane makes one app's claim
4753
4769
  // answer 409 AGENT_BUSY while the other holds the slot.
4770
+ // `switchboard_apple` is the macOS Switchboard (owner, 2026-11-27: "build switchboard for mac"):
4771
+ // a native build of the same product the Windows `switchboard` lane builds, developed here on the
4772
+ // Mac like `tuner_apple` and `broadcast_apple`. Its own lane rather than a rider on `switchboard`
4773
+ // (a Windows lane) or `switchboard_steamos` (the SteamOS group), for the one-in-flight reason
4774
+ // `broadcast_apple` records above. A Mac lane only: not in the Windows set, not in the SteamOS group.
4754
4775
  // The SteamOS group (`STEAMOS_PROJECTS`, above) is on both platforms since 2026-11-03.
4755
- mac: ["frequency", "tuner_apple", "site", "platform", "broadcast_apple", ...STEAMOS_PROJECTS],
4776
+ mac: ["frequency", "tuner_apple", "site", "platform", "broadcast_apple", "switchboard_apple", ...STEAMOS_PROJECTS],
4756
4777
  // `broadcast` is its own lane rather than a rider on `windows` (2026-08-31): the board
4757
4778
  // enforces one in-flight task per PROJECT, and `windows` already carries the Jacked
4758
4779
  // audio-driver work, so sharing it meant a Broadcast claim answered 409 AGENT_BUSY for
@@ -5131,6 +5152,8 @@ var NativeSession = class {
5131
5152
  producing = null;
5132
5153
  timestamp = 0;
5133
5154
  owners = /* @__PURE__ */ new Map();
5155
+ /** `${connectionId}:${streamId}` -> that stream's content keys (wire v2). */
5156
+ contentKeys = /* @__PURE__ */ new Map();
5134
5157
  pending = [];
5135
5158
  /** The last 500 signalling events, oldest first. A probe reads them; `waitFor` searches them. */
5136
5159
  events = [];
@@ -5142,7 +5165,7 @@ var NativeSession = class {
5142
5165
  /** The stale-key watch: consecutive packets that would not open since the last one that did, and when a refresh was last asked for. */
5143
5166
  staleRun = 0;
5144
5167
  lastStaleRefresh = 0;
5145
- receiveStats = { frames: 0, unsupportedVersion: 0, unknownChannel: 0, undecryptable: 0, notAudio: 0, misattributed: 0 };
5168
+ receiveStats = { frames: 0, unsupportedVersion: 0, contentKeys: 0, awaitingContentKey: 0, unknownChannel: 0, undecryptable: 0, notAudio: 0, misattributed: 0 };
5146
5169
  /** The node-issued datapath identity, 0 until `open()` has authenticated. */
5147
5170
  get connectionId() {
5148
5171
  return this.connection;
@@ -5244,7 +5267,7 @@ var NativeSession = class {
5244
5267
  this.owners.set(streamId, connectionId);
5245
5268
  this.options.onStream?.({ streamId, connectionId, producerUserId: event.producerUserId, producerCallsign: event.producerCallsign, channelId: event.channelId });
5246
5269
  }
5247
- if (event.type === "nativeStreamClosed") this.owners.delete(Number(event.streamId));
5270
+ if (event.type === "nativeStreamClosed") this.forgetStream(Number(event.streamId));
5248
5271
  if (event.type === "nativeKeysRotated") this.onKeysRotated(String(event.keysetId));
5249
5272
  for (const p of [...this.pending]) {
5250
5273
  if (p.type === event.type) {
@@ -5395,12 +5418,24 @@ var NativeSession = class {
5395
5418
  }
5396
5419
  onPacket(packet) {
5397
5420
  if (!this.native || packet.length < HEADER.LENGTH + AEAD.TAG_BYTES) return;
5398
- if ((packet[HEADER.VERSION_FLAGS_OFFSET] & 15) !== NATIVE_TRANSPORT_VERSION) {
5421
+ const byte0 = packet[HEADER.VERSION_FLAGS_OFFSET];
5422
+ const version = byte0 & 15;
5423
+ const flags = byte0 & 240;
5424
+ const connectionId = packet.readUInt32BE(HEADER.CONNECTION_ID_OFFSET);
5425
+ const channelNativeId = packet.readUInt32BE(HEADER.CHANNEL_ID_OFFSET);
5426
+ if (version === NATIVE_TRANSPORT_VERSION && flags === FLAG_CONTROL) {
5427
+ this.receiveStats.notAudio += 1;
5428
+ return;
5429
+ }
5430
+ if (version === NATIVE_TRANSPORT_WIRE_V2 && flags === 0 && channelNativeId !== SHARE_CHANNEL_ID) {
5431
+ this.onV2Audio(packet, connectionId, channelNativeId);
5432
+ return;
5433
+ }
5434
+ const keyPacket = version === NATIVE_TRANSPORT_WIRE_V2 && flags === FLAG_CONTENT_KEY;
5435
+ if (!keyPacket && !(version === NATIVE_TRANSPORT_VERSION && flags === 0)) {
5399
5436
  this.receiveStats.unsupportedVersion += 1;
5400
5437
  return;
5401
5438
  }
5402
- const connectionId = packet.readUInt32BE(HEADER.CONNECTION_ID_OFFSET);
5403
- const channelNativeId = packet.readUInt32BE(HEADER.CHANNEL_ID_OFFSET);
5404
5439
  const entry = Object.values(this.native.channelKeys.keys).find((k) => k.id === channelNativeId);
5405
5440
  if (!entry) {
5406
5441
  this.receiveStats.unknownChannel += 1;
@@ -5415,10 +5450,89 @@ var NativeSession = class {
5415
5450
  return;
5416
5451
  }
5417
5452
  this.staleRun = 0;
5453
+ if (keyPacket) {
5454
+ this.onContentKey(connectionId, body);
5455
+ return;
5456
+ }
5418
5457
  if (body.type !== PAYLOAD_TYPE.audio) {
5419
5458
  this.receiveStats.notAudio += 1;
5420
5459
  return;
5421
5460
  }
5461
+ this.deliver(connectionId, body);
5462
+ }
5463
+ /** A v2 key packet that opened under a channel key: attribute it, then file `K_s` under the stream's home. */
5464
+ onContentKey(connectionId, body) {
5465
+ const rest = body.opus;
5466
+ const restLength = CONTENT_KEY_PAYLOAD.LENGTH - CONTENT_KEY_PAYLOAD.CONTENT_KEY_ID_OFFSET;
5467
+ if (body.type !== PAYLOAD_TYPE.contentKey || rest.length !== restLength) {
5468
+ this.receiveStats.notAudio += 1;
5469
+ return;
5470
+ }
5471
+ const owner = this.owners.get(body.streamId);
5472
+ if (owner === void 0 || owner !== connectionId) {
5473
+ this.receiveStats.misattributed += 1;
5474
+ return;
5475
+ }
5476
+ const contentKeyId = rest.readUInt32BE(0);
5477
+ const key = Buffer.from(rest.subarray(CONTENT_KEY_PAYLOAD.KEY_OFFSET - CONTENT_KEY_PAYLOAD.CONTENT_KEY_ID_OFFSET));
5478
+ const slot = `${connectionId}:${body.streamId}`;
5479
+ let stream = this.contentKeys.get(slot);
5480
+ if (!stream) {
5481
+ stream = { connectionId, streamId: body.streamId, generations: /* @__PURE__ */ new Map() };
5482
+ this.contentKeys.set(slot, stream);
5483
+ }
5484
+ const generation = contentKeyId & 255;
5485
+ const held = stream.generations.get(generation);
5486
+ if (held && held.contentKeyId === contentKeyId) return;
5487
+ stream.generations.delete(generation);
5488
+ stream.generations.set(generation, { contentKeyId, key });
5489
+ while (stream.generations.size > 2) stream.generations.delete(stream.generations.keys().next().value);
5490
+ this.receiveStats.contentKeys += 1;
5491
+ }
5492
+ /**
5493
+ * v2 audio: sealed under `K_s`, found by (connection, home wire id, generation). The stream bound
5494
+ * to that home is tried first; a connection's streams whose home is not yet learned are tried
5495
+ * after (one connection rarely carries more than one), and the first that opens with its own
5496
+ * streamId inside binds the home.
5497
+ */
5498
+ onV2Audio(packet, connectionId, homeWireId) {
5499
+ const generation = packet[HEADER.KEY_EPOCH_OFFSET];
5500
+ const candidates = [];
5501
+ for (const entry of this.contentKeys.values()) {
5502
+ if (entry.connectionId !== connectionId || !entry.generations.has(generation)) continue;
5503
+ if (entry.homeWireId === homeWireId) candidates.unshift(entry);
5504
+ else if (entry.homeWireId === void 0) candidates.push(entry);
5505
+ }
5506
+ if (candidates.length === 0) {
5507
+ this.receiveStats.awaitingContentKey += 1;
5508
+ return;
5509
+ }
5510
+ for (const stream of candidates) {
5511
+ let body;
5512
+ try {
5513
+ body = open(packet, stream.generations.get(generation).key);
5514
+ } catch {
5515
+ continue;
5516
+ }
5517
+ if (body.type !== PAYLOAD_TYPE.audio) {
5518
+ this.receiveStats.notAudio += 1;
5519
+ return;
5520
+ }
5521
+ if (body.streamId !== stream.streamId) {
5522
+ this.receiveStats.misattributed += 1;
5523
+ return;
5524
+ }
5525
+ if (stream.homeWireId === void 0) {
5526
+ for (const [slot, other] of this.contentKeys) if (other !== stream && other.connectionId === connectionId && other.homeWireId === homeWireId) this.contentKeys.delete(slot);
5527
+ stream.homeWireId = homeWireId;
5528
+ }
5529
+ this.deliver(connectionId, body);
5530
+ return;
5531
+ }
5532
+ this.receiveStats.undecryptable += 1;
5533
+ }
5534
+ /** The owner check and hand-off every opened audio frame goes through, whichever wire version carried it. */
5535
+ deliver(connectionId, body) {
5422
5536
  const owner = this.owners.get(body.streamId);
5423
5537
  if (owner === void 0 || owner !== connectionId) {
5424
5538
  this.receiveStats.misattributed += 1;
@@ -5429,6 +5543,10 @@ var NativeSession = class {
5429
5543
  this.options.onAudio?.(frame);
5430
5544
  for (const listener of this.audioListeners) listener(frame);
5431
5545
  }
5546
+ forgetStream(streamId) {
5547
+ this.owners.delete(streamId);
5548
+ for (const [slot, entry] of this.contentKeys) if (entry.streamId === streamId) this.contentKeys.delete(slot);
5549
+ }
5432
5550
  /**
5433
5551
  * The backstop for a missed `nativeKeysRotated` (the node announces a rotation only once it
5434
5552
  * has a baseline to compare against, so a rotation inside its first poll window is never
@@ -5495,7 +5613,7 @@ async function createOpus() {
5495
5613
  return {
5496
5614
  encode: (pcm) => enc.encode(Buffer.from(pcm.buffer, pcm.byteOffset, pcm.byteLength)),
5497
5615
  decode: (packet) => toInt16(enc.decode(packet)),
5498
- conceal: () => new Int16Array(FRAME_SAMPLES),
5616
+ conceal: (samples = FRAME_SAMPLES) => new Int16Array(samples),
5499
5617
  close: () => enc.delete?.()
5500
5618
  };
5501
5619
  } catch {
@@ -5508,7 +5626,7 @@ async function createOpus() {
5508
5626
  return {
5509
5627
  encode: (pcm) => codec.encode(Buffer.from(pcm.buffer, pcm.byteOffset, pcm.byteLength), FRAME_SAMPLES),
5510
5628
  decode: (packet) => toInt16(codec.decode(packet)),
5511
- conceal: () => new Int16Array(FRAME_SAMPLES),
5629
+ conceal: (samples = FRAME_SAMPLES) => new Int16Array(samples),
5512
5630
  close: () => codec.delete()
5513
5631
  };
5514
5632
  }
@@ -5657,15 +5775,17 @@ var Listener = class {
5657
5775
  if (!this.options.includeOwn && frame.connectionId === this.session.connectionId) return;
5658
5776
  let s = this.streams.get(frame.streamId);
5659
5777
  if (!s) {
5660
- s = { connectionId: frame.connectionId, chunks: [], lastTimestamp: null, first: frame.timestamp };
5778
+ s = { connectionId: frame.connectionId, chunks: [], lastTimestamp: null, lastSamples: 0, first: frame.timestamp };
5661
5779
  this.streams.set(frame.streamId, s);
5662
5780
  }
5663
5781
  if (s.lastTimestamp !== null) {
5664
- const missing = Math.min(50, Math.round((frame.timestamp - s.lastTimestamp) / FRAME_SAMPLES) - 1);
5665
- for (let i = 0; i < missing; i++) s.chunks.push(codec.conceal());
5782
+ const gap = (frame.timestamp - s.lastTimestamp >>> 0) - s.lastSamples;
5783
+ if (gap > 0 && gap <= 48e3) s.chunks.push(codec.conceal(gap));
5666
5784
  }
5667
- s.chunks.push(codec.decode(frame.opus));
5785
+ const pcm = codec.decode(frame.opus);
5786
+ s.chunks.push(pcm);
5668
5787
  s.lastTimestamp = frame.timestamp;
5788
+ s.lastSamples = pcm.length;
5669
5789
  };
5670
5790
  this.detach = this.session.attachAudio(handler);
5671
5791
  }