agent-comms 8.15.26 → 9.0.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.
Files changed (68) hide show
  1. package/.claude-plugin/marketplace.json +1 -1
  2. package/.claude-plugin/plugin.json +1 -1
  3. package/dist/bridges/user/web/actions.d.ts +0 -8
  4. package/dist/bridges/user/web/actions.d.ts.map +1 -1
  5. package/dist/bridges/user/web/actions.js +0 -35
  6. package/dist/bridges/user/web/actions.js.map +1 -1
  7. package/dist/bridges/user/web/contract.d.ts +0 -19
  8. package/dist/bridges/user/web/contract.d.ts.map +1 -1
  9. package/dist/bridges/user/web/contract.js +0 -16
  10. package/dist/bridges/user/web/contract.js.map +1 -1
  11. package/dist/bridges/user/web/dist/bundle.js +1 -1
  12. package/dist/bridges/user/web/dist/mesh-worker.js +4 -4
  13. package/dist/bridges/user/web/dist/sw.js +1 -1
  14. package/dist/bridges/user/web/frontend/dispatch-action.d.ts +1 -1
  15. package/dist/bridges/user/web/frontend/dispatch-action.d.ts.map +1 -1
  16. package/dist/bridges/user/web/frontend/dispatch-action.js +0 -26
  17. package/dist/bridges/user/web/frontend/dispatch-action.js.map +1 -1
  18. package/dist/bridges/user/web/frontend/mesh-worker.d.ts.map +1 -1
  19. package/dist/bridges/user/web/frontend/mesh-worker.js +0 -6
  20. package/dist/bridges/user/web/frontend/mesh-worker.js.map +1 -1
  21. package/dist/bridges/user/web/frontend/sw.d.ts +1 -4
  22. package/dist/bridges/user/web/frontend/sw.d.ts.map +1 -1
  23. package/dist/bridges/user/web/frontend/sw.js +1 -54
  24. package/dist/bridges/user/web/frontend/sw.js.map +1 -1
  25. package/dist/bridges/user/web/frontend/tab-contract.d.ts +0 -19
  26. package/dist/bridges/user/web/frontend/tab-contract.d.ts.map +1 -1
  27. package/dist/bridges/user/web/router.d.ts +0 -21
  28. package/dist/bridges/user/web/router.d.ts.map +1 -1
  29. package/dist/bridges/user/web/router.js +1 -3
  30. package/dist/bridges/user/web/router.js.map +1 -1
  31. package/dist/bridges/user/web/server.d.ts +0 -2
  32. package/dist/bridges/user/web/server.d.ts.map +1 -1
  33. package/dist/bridges/user/web/server.js +2 -36
  34. package/dist/bridges/user/web/server.js.map +1 -1
  35. package/dist/bridges/user/web/test/contract.unit.test.js +3 -3
  36. package/dist/bridges/user/web/test/contract.unit.test.js.map +1 -1
  37. package/dist/bridges/user/web/test/web-server-unbuilt-frontend.integration.test.js +2 -2
  38. package/dist/bridges/user/web/test/web-server-unbuilt-frontend.integration.test.js.map +1 -1
  39. package/dist/bridges/user/web/test/web-server.integration.test.js +10 -0
  40. package/dist/bridges/user/web/test/web-server.integration.test.js.map +1 -1
  41. package/dist/core/index.d.ts +0 -4
  42. package/dist/core/index.d.ts.map +1 -1
  43. package/dist/core/index.js +0 -2
  44. package/dist/core/index.js.map +1 -1
  45. package/package.json +1 -1
  46. package/src/bridges/user/web/actions.ts +0 -43
  47. package/src/bridges/user/web/contract.ts +0 -22
  48. package/src/bridges/user/web/dist/bundle.js +1 -1
  49. package/src/bridges/user/web/dist/mesh-worker.js +4 -4
  50. package/src/bridges/user/web/dist/sw.js +1 -1
  51. package/src/bridges/user/web/router.ts +0 -12
  52. package/src/bridges/user/web/server.ts +2 -41
  53. package/src/core/index.ts +0 -4
  54. package/dist/core/push-manager.d.ts +0 -40
  55. package/dist/core/push-manager.d.ts.map +0 -1
  56. package/dist/core/push-manager.js +0 -60
  57. package/dist/core/push-manager.js.map +0 -1
  58. package/dist/core/vapid.d.ts +0 -22
  59. package/dist/core/vapid.d.ts.map +0 -1
  60. package/dist/core/vapid.js +0 -54
  61. package/dist/core/vapid.js.map +0 -1
  62. package/dist/core/web-push.d.ts +0 -37
  63. package/dist/core/web-push.d.ts.map +0 -1
  64. package/dist/core/web-push.js +0 -216
  65. package/dist/core/web-push.js.map +0 -1
  66. package/src/core/push-manager.ts +0 -76
  67. package/src/core/vapid.ts +0 -81
  68. package/src/core/web-push.ts +0 -322
@@ -1,76 +0,0 @@
1
- /**
2
- * Push subscription manager — tracks browser push subscriptions and
3
- * dispatches Web Push notifications when agents are offline.
4
- *
5
- * The PushManager is a standalone module — MeshStore doesn't know about it.
6
- * The web server bridge checks whether the target agent's WebSocket is
7
- * disconnected and falls back to push when a subscription exists.
8
- */
9
-
10
- import type { VapidKeys } from "./vapid.js";
11
- import { generateVapidKeys } from "./vapid.js";
12
- import {
13
- sendWebPush,
14
- type PushSubscription,
15
- type PushPayload,
16
- } from "./web-push.js";
17
-
18
- // ---------------------------------------------------------------------------
19
- // PushManager
20
- // ---------------------------------------------------------------------------
21
-
22
- export class PushManager {
23
- private readonly vapidKeys: VapidKeys;
24
- private readonly subject: string;
25
- private readonly subscriptions = new Map<string, PushSubscription>();
26
-
27
- constructor(subject = "mailto:agent-comms@localhost") {
28
- this.vapidKeys = generateVapidKeys();
29
- this.subject = subject;
30
- }
31
-
32
- /**
33
- * Register a push subscription for a given agent ID.
34
- * Overwrites any existing subscription for the same agent.
35
- */
36
- addSubscription(agentId: string, subscription: PushSubscription): void {
37
- this.subscriptions.set(agentId, subscription);
38
- }
39
-
40
- /**
41
- * Remove a push subscription.
42
- */
43
- removeSubscription(agentId: string): void {
44
- this.subscriptions.delete(agentId);
45
- }
46
-
47
- /**
48
- * Check whether a push subscription exists for the given agent.
49
- */
50
- hasSubscription(agentId: string): boolean {
51
- return this.subscriptions.has(agentId);
52
- }
53
-
54
- /**
55
- * Send a push notification to a subscribed agent.
56
- *
57
- * No-ops silently if no subscription exists — callers should check
58
- * hasSubscription() first if they need to distinguish the two cases.
59
- */
60
- async sendPush(
61
- agentId: string,
62
- payload: Readonly<PushPayload>,
63
- ): Promise<void> {
64
- const subscription = this.subscriptions.get(agentId);
65
- if (!subscription) return;
66
-
67
- await sendWebPush(subscription, payload, this.vapidKeys, this.subject);
68
- }
69
-
70
- /**
71
- * Get the VAPID public key for the PWA to use in `pushManager.subscribe()`.
72
- */
73
- getPublicKey(): string {
74
- return this.vapidKeys.publicKey;
75
- }
76
- }
package/src/core/vapid.ts DELETED
@@ -1,81 +0,0 @@
1
- /**
2
- * VAPID key generation for Web Push (RFC 8292).
3
- *
4
- * Generates a P-256 EC key pair once per process lifetime and caches it
5
- * in memory. The public key is shared with the PWA client so it can
6
- * create push subscriptions; the private key signs push requests.
7
- */
8
-
9
- import * as crypto from "node:crypto";
10
-
11
- // ---------------------------------------------------------------------------
12
- // Types
13
- // ---------------------------------------------------------------------------
14
-
15
- export interface VapidKeys {
16
- /** Base64url-encoded uncompressed P-256 public key (65 bytes). */
17
- publicKey: string;
18
- /** Base64url-encoded P-256 private key (32 bytes). */
19
- privateKey: string;
20
- }
21
-
22
- // ---------------------------------------------------------------------------
23
- // Cached singleton
24
- // ---------------------------------------------------------------------------
25
-
26
- let cachedKeys: VapidKeys | undefined;
27
-
28
- /** Length in bytes of an uncompressed P-256 public key point (0x04 prefix + 32-byte X + 32-byte Y). */
29
- const P256_PUBLIC_KEY_BYTES = 65;
30
-
31
- /** Length in bytes of a P-256 private scalar. */
32
- const P256_PRIVATE_KEY_BYTES = 32;
33
-
34
- // ---------------------------------------------------------------------------
35
- // Implementation
36
- // ---------------------------------------------------------------------------
37
-
38
- /**
39
- * Generate or return cached VAPID keys.
40
- *
41
- * Uses Node.js `crypto.generateKeyPairSync` with P-256, extracts the
42
- * raw 65-byte uncompressed public point and the 32-byte private scalar,
43
- * and base64url-encodes both per RFC 8291.
44
- */
45
- export function generateVapidKeys(): VapidKeys {
46
- if (cachedKeys) return cachedKeys;
47
-
48
- const { publicKey, privateKey } = crypto.generateKeyPairSync("ec", {
49
- namedCurve: "P-256",
50
- });
51
-
52
- // Export public key as raw DER, extract the 65-byte uncompressed point.
53
- // SPKI DER for P-256 has a 26-byte header before the 65-byte point.
54
- const pubDer = publicKey.export({ type: "spki", format: "der" });
55
- const publicKeyBuffer = pubDer.subarray(
56
- pubDer.length - P256_PUBLIC_KEY_BYTES,
57
- );
58
-
59
- // Export private key as PKCS8 DER, extract the 32-byte private scalar.
60
- // PKCS8 DER for P-256 has a 39-byte header before the 32-byte scalar.
61
- // The scalar is the last 32 bytes (after the leading zero pad byte).
62
- const privDer = privateKey.export({ type: "pkcs8", format: "der" });
63
- const privateKeyBuffer = privDer.subarray(
64
- privDer.length - P256_PRIVATE_KEY_BYTES,
65
- );
66
-
67
- cachedKeys = {
68
- publicKey: base64url(publicKeyBuffer),
69
- privateKey: base64url(privateKeyBuffer),
70
- };
71
-
72
- return cachedKeys;
73
- }
74
-
75
- // ---------------------------------------------------------------------------
76
- // Helpers
77
- // ---------------------------------------------------------------------------
78
-
79
- function base64url(buffer: Buffer): string {
80
- return buffer.toString("base64url");
81
- }
@@ -1,322 +0,0 @@
1
- /**
2
- * Web Push encryption and VAPID JWT signing (RFC 8291 + RFC 8292).
3
- *
4
- * Implements the Web Push protocol using only Node.js built-in `crypto`
5
- * and `https` — no external dependencies.
6
- *
7
- * Encryption flow (RFC 8291):
8
- * 1. Derive a shared ECDH secret from the subscription's p256dh key
9
- * 2. HKDF-derive the encryption key and nonce from the shared secret + auth
10
- * 3. Encrypt the payload with AES-128-GCM
11
- * 4. Prepend the sender's public key (65 bytes) to the ciphertext
12
- *
13
- * VAPID signing (RFC 8292):
14
- * 1. Build a JWT with sub, exp, and aud claims
15
- * 2. Sign with ES256 (ECDSA + P-256 + SHA-256)
16
- * 3. Append the public key and token to the Crypto-Key header
17
- */
18
-
19
- import * as crypto from "node:crypto";
20
- import * as https from "node:https";
21
- import * as http from "node:http";
22
- import { URL } from "node:url";
23
-
24
- import type { VapidKeys } from "./vapid.js";
25
-
26
- // ---------------------------------------------------------------------------
27
- // Types
28
- // ---------------------------------------------------------------------------
29
-
30
- export interface PushSubscription {
31
- endpoint: string;
32
- keys: { p256dh: string; auth: string };
33
- }
34
-
35
- export interface PushPayload {
36
- title: string;
37
- body: string;
38
- }
39
-
40
- // ---------------------------------------------------------------------------
41
- // Constants
42
- // ---------------------------------------------------------------------------
43
- const CONTENT_ENCODING = "aes128gcm";
44
- const KEY_LENGTH = 16;
45
- const NONCE_LENGTH = 12;
46
- const SALT_LENGTH = 16;
47
- const MAX_PAYLOAD_SIZE = 4078;
48
- const EC_UNCOMPRESSED_POINT_LENGTH = 65;
49
- const RECORD_SIZE = 4096;
50
- const RECORD_SIZE_FIELD_LENGTH = 4;
51
- const VAPID_JWT_EXPIRY_HOURS = 12;
52
- const MINUTES_PER_HOUR = 60;
53
- const SECONDS_PER_MINUTE = 60;
54
- const MS_PER_SECOND = 1000;
55
- const HTTPS_DEFAULT_PORT = 443;
56
- const HTTP_DEFAULT_PORT = 80;
57
- const HTTP_STATUS_OK_MIN = 200;
58
- const HTTP_STATUS_OK_MAX = 299;
59
-
60
- // ---------------------------------------------------------------------------
61
- // Public API
62
- // ---------------------------------------------------------------------------
63
-
64
- /**
65
- * Send a Web Push notification.
66
- *
67
- * Encrypts the payload per RFC 8291, signs with VAPID per RFC 8292,
68
- * and POSTs to the push service endpoint.
69
- */
70
- export async function sendWebPush(
71
- subscription: PushSubscription,
72
- payload: Readonly<PushPayload>,
73
- vapidKeys: Readonly<VapidKeys>,
74
- subject: string,
75
- ): Promise<void> {
76
- const plaintext = JSON.stringify(payload);
77
- const plaintextBuf = Buffer.from(plaintext, "utf-8");
78
-
79
- if (plaintextBuf.length > MAX_PAYLOAD_SIZE) {
80
- throw new Error(
81
- `Payload too large: ${String(plaintextBuf.length)} bytes (max ${String(MAX_PAYLOAD_SIZE)})`,
82
- );
83
- }
84
-
85
- const encrypted = encrypt(plaintextBuf, subscription.keys);
86
- const headers = buildHeaders(
87
- encrypted,
88
- subscription.endpoint,
89
- vapidKeys,
90
- subject,
91
- );
92
-
93
- await post(subscription.endpoint, headers, encrypted);
94
- }
95
-
96
- // ---------------------------------------------------------------------------
97
- // Encryption (RFC 8291)
98
- // ---------------------------------------------------------------------------
99
-
100
- function encrypt(plaintext: Buffer, keys: PushSubscription["keys"]): Buffer {
101
- // Decode the subscription's p256dh and auth keys
102
- const recipientPubKey = base64urlDecode(keys.p256dh);
103
- const authKey = base64urlDecode(keys.auth);
104
-
105
- // Generate an ephemeral ECDH key pair for this message
106
- const ecdh = crypto.generateKeyPairSync("ec", { namedCurve: "P-256" });
107
- const senderPrivateKey = crypto.createPrivateKey({
108
- key: ecdh.privateKey.export({ type: "pkcs8", format: "der" }),
109
- format: "der",
110
- type: "pkcs8",
111
- });
112
- const senderPublicDer = ecdh.publicKey.export({
113
- type: "spki",
114
- format: "der",
115
- });
116
- const senderPublicKey = senderPublicDer.subarray(
117
- senderPublicDer.length - EC_UNCOMPRESSED_POINT_LENGTH,
118
- );
119
-
120
- // Derive the shared secret via ECDH
121
- const sharedSecret = crypto.diffieHellman({
122
- privateKey: senderPrivateKey,
123
- publicKey: crypto.createPublicKey({
124
- key: concat(
125
- Buffer.from(
126
- "3059301306072a8648ce3d020106082a8648ce3d030107034200",
127
- "hex",
128
- ),
129
- recipientPubKey,
130
- ),
131
- format: "der",
132
- type: "spki",
133
- }),
134
- });
135
-
136
- // Derive the pseudo-random key (PRK) using HKDF with auth
137
- const prk = hkdfExtract(authKey, sharedSecret);
138
-
139
- // Derive the encryption key and nonce
140
- const keyInfo = concat(
141
- Buffer.from("Content-Encoding: aes128gcm\0", "utf-8"),
142
- senderPublicKey,
143
- );
144
- const nonceInfo = Buffer.from("Content-Encoding: nonce\0", "utf-8");
145
-
146
- const encryptionKey = hkdfExpand(prk, keyInfo, KEY_LENGTH);
147
- const nonce = hkdfExpand(prk, nonceInfo, NONCE_LENGTH);
148
-
149
- // Generate a random salt
150
- const salt = crypto.randomBytes(SALT_LENGTH);
151
-
152
- // Pad the plaintext: plaintext + 2-byte padding delimiter (0x02 0x00 for no padding)
153
- // RFC 8291 §4: record is plaintext || 0x02 || padding zeros
154
- const padded = concat(plaintext, Buffer.from([0x02]));
155
-
156
- // Encrypt with AES-128-GCM
157
- const cipher = crypto.createCipheriv("aes-128-gcm", encryptionKey, nonce);
158
- const ciphertext = cipher.update(padded);
159
- const authTag = cipher.getAuthTag();
160
-
161
- // RFC 8291 §4: salt (16) || rs (4, big-endian, 4096) || sender pubkey (65) || ciphertext || tag
162
- const rs = Buffer.alloc(RECORD_SIZE_FIELD_LENGTH);
163
- rs.writeUInt32BE(RECORD_SIZE);
164
-
165
- return concat(salt, rs, senderPublicKey, ciphertext, authTag);
166
- }
167
-
168
- // ---------------------------------------------------------------------------
169
- // HKDF helpers
170
- // ---------------------------------------------------------------------------
171
-
172
- function hkdfExtract(salt: Buffer, ikm: Buffer): Buffer {
173
- const hmac = crypto.createHmac("sha256", salt);
174
- hmac.update(ikm);
175
- return hmac.digest();
176
- }
177
-
178
- function hkdfExpand(prk: Buffer, info: Buffer, length: number): Buffer {
179
- const hmac = crypto.createHmac("sha256", prk);
180
- hmac.update(concat(info, Buffer.from([0x01])));
181
- const t = hmac.digest();
182
- return t.subarray(0, length);
183
- }
184
-
185
- // ---------------------------------------------------------------------------
186
- // VAPID JWT signing (RFC 8292)
187
- // ---------------------------------------------------------------------------
188
-
189
- function buildVapidJwt(
190
- audience: string,
191
- vapidKeys: Readonly<VapidKeys>,
192
- subject: string,
193
- ): string {
194
- const header = base64url(Buffer.from('{"alg":"ES256","typ":"JWT"}', "utf-8"));
195
-
196
- const exp =
197
- Math.floor(Date.now() / MS_PER_SECOND) +
198
- VAPID_JWT_EXPIRY_HOURS * MINUTES_PER_HOUR * SECONDS_PER_MINUTE;
199
- const claims = JSON.stringify({ aud: audience, exp, sub: subject });
200
- const payload = base64url(Buffer.from(claims, "utf-8"));
201
-
202
- const signInput = `${header}.${payload}`;
203
-
204
- // Reconstruct the private key from the raw 32-byte scalar
205
- // P-256 PKCS8 DER wrapper: 39 bytes header + 32 bytes scalar (with 0x00 prefix)
206
- const privScalar = base64urlDecode(vapidKeys.privateKey);
207
- // Build the EC private key in SEC1 DER format
208
- const sec1Der = concat(
209
- Buffer.from("30770201010420", "hex"),
210
- privScalar,
211
- Buffer.from("a00a06082a8648ce3d030107a14403420004", "hex"),
212
- base64urlDecode(vapidKeys.publicKey),
213
- );
214
- const privateKey = crypto.createPrivateKey({
215
- key: sec1Der,
216
- format: "der",
217
- type: "sec1",
218
- });
219
-
220
- const signature = crypto.sign("sha256", Buffer.from(signInput), privateKey);
221
- // ECDSA signature is a DER-encoded SEQUENCE of two INTEGERs.
222
- // Web Push requires base64url encoding of the raw DER.
223
- const sigB64 = base64url(signature);
224
-
225
- return `${signInput}.${sigB64}`;
226
- }
227
-
228
- // ---------------------------------------------------------------------------
229
- // HTTP headers
230
- // ---------------------------------------------------------------------------
231
-
232
- function buildHeaders(
233
- encrypted: Buffer,
234
- endpoint: string,
235
- vapidKeys: Readonly<VapidKeys>,
236
- subject: string,
237
- ): Record<string, string> {
238
- const audience = new URL(endpoint).origin;
239
- const jwt = buildVapidJwt(audience, vapidKeys, subject);
240
-
241
- return {
242
- "Content-Type": "application/octet-stream",
243
- "Content-Encoding": CONTENT_ENCODING,
244
- "Content-Length": String(encrypted.length),
245
- Authorization: `vapid t=${jwt}, k=${vapidKeys.publicKey}`,
246
- TTL: "86400",
247
- Urgency: "high",
248
- };
249
- }
250
-
251
- // ---------------------------------------------------------------------------
252
- // HTTP POST
253
- // ---------------------------------------------------------------------------
254
-
255
- async function post(
256
- endpoint: string,
257
- headers: Readonly<Record<string, string>>,
258
- body: Buffer,
259
- ): Promise<void> {
260
- return new Promise((resolve, reject) => {
261
- const url = new URL(endpoint);
262
- const isHttps = url.protocol === "https:";
263
- const mod = isHttps ? https : http;
264
- const port =
265
- url.port !== ""
266
- ? url.port
267
- : String(isHttps ? HTTPS_DEFAULT_PORT : HTTP_DEFAULT_PORT);
268
-
269
- const req = mod.request(
270
- {
271
- hostname: url.hostname,
272
- port,
273
- path: url.pathname + url.search,
274
- method: "POST",
275
- headers,
276
- },
277
- (res) => {
278
- // Drain the response body
279
- const chunks: Buffer[] = [];
280
- res.on("data", (chunk: Buffer) => {
281
- chunks.push(chunk);
282
- });
283
- res.on("end", () => {
284
- if (
285
- res.statusCode !== undefined &&
286
- (res.statusCode < HTTP_STATUS_OK_MIN ||
287
- res.statusCode > HTTP_STATUS_OK_MAX)
288
- ) {
289
- const responseBody = Buffer.concat(chunks).toString("utf-8");
290
- reject(
291
- new Error(
292
- `Push service returned ${String(res.statusCode)}: ${responseBody}`,
293
- ),
294
- );
295
- } else {
296
- resolve();
297
- }
298
- });
299
- },
300
- );
301
-
302
- req.on("error", reject);
303
- req.write(body);
304
- req.end();
305
- });
306
- }
307
-
308
- // ---------------------------------------------------------------------------
309
- // Utilities
310
- // ---------------------------------------------------------------------------
311
-
312
- function base64urlDecode(input: string): Buffer {
313
- return Buffer.from(input, "base64url");
314
- }
315
-
316
- function base64url(input: Buffer): string {
317
- return input.toString("base64url");
318
- }
319
-
320
- function concat(...buffers: readonly Buffer[]): Buffer {
321
- return Buffer.concat(buffers);
322
- }