a2a-exposed 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +19 -2
- package/bin/a2a-exposed.mjs +21 -0
- package/lib/a2a.mjs +187 -0
- package/lib/commands.mjs +262 -0
- package/lib/config.mjs +97 -0
- package/lib/deploy.mjs +492 -0
- package/lib/main.mjs +226 -0
- package/lib/pair.mjs +349 -0
- package/lib/status.mjs +198 -0
- package/lib/tunnel.mjs +450 -0
- package/lib/update.mjs +71 -0
- package/lib/workersdev.mjs +97 -0
- package/package.json +46 -5
- package/worker/cloudflare.config.ts +75 -0
- package/worker/deploy.env.example +33 -0
- package/worker/migrations/0001_init.sql +74 -0
- package/worker/migrations/0002_wake_budget.sql +8 -0
- package/worker/migrations/0003_device_pairing.sql +39 -0
- package/worker/migrations/0004_pairing_replace.sql +6 -0
- package/worker/migrations/0005_facade_owners.sql +9 -0
- package/worker/package.json +20 -0
- package/worker/src/a2a.ts +216 -0
- package/worker/src/facade.ts +254 -0
- package/worker/src/index.ts +1445 -0
- package/worker/src/pairing.ts +313 -0
- package/worker/src/wake.ts +242 -0
- package/worker/test/a2a.test.ts +102 -0
- package/worker/test/d1.ts +38 -0
- package/worker/test/facade.test.ts +421 -0
- package/worker/test/migrations.test.ts +124 -0
- package/worker/test/pairing.test.ts +651 -0
- package/worker/test/wake.test.ts +141 -0
- package/worker/tsconfig.json +22 -0
- package/worker/vite.config.ts +6 -0
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// Run: npm test (node --test, Node >= 22.18 native TypeScript type stripping)
|
|
2
|
+
import { test } from "node:test";
|
|
3
|
+
import assert from "node:assert/strict";
|
|
4
|
+
import { createHmac } from "node:crypto";
|
|
5
|
+
import { renderWake, renderTemplate, wakeSummary, redact, addAccessHeaders, cloudflareErrorHint, PRESETS, type WakeEvent } from "../src/wake.ts";
|
|
6
|
+
|
|
7
|
+
const ev: WakeEvent = {
|
|
8
|
+
contextId: "ctx-1", taskId: "t-2", taskIds: ["t-1", "t-2"], from: "peer-a",
|
|
9
|
+
preview: 'Please "do" X\nnow', kind: "inbound", publicUrl: "https://agent.example.com",
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
test("unconfigured wake is a no-op", async () => {
|
|
13
|
+
assert.equal(await renderWake({ preset: "grok-bot" }, ev), null);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test("grok-bot: bearer + JSON payload", async () => {
|
|
17
|
+
const r = (await renderWake({ preset: "grok-bot", url: "https://hook.example/x", key: "k1" }, ev))!;
|
|
18
|
+
assert.equal(r.headers["authorization"], "Bearer k1");
|
|
19
|
+
const b = JSON.parse(r.body);
|
|
20
|
+
assert.deepEqual([b.contextId, b.taskId, b.from, b.kind], ["ctx-1", "t-2", "peer-a", "inbound"]);
|
|
21
|
+
assert.deepEqual(b.taskIds, ["t-1", "t-2"]);
|
|
22
|
+
assert.match(b.hint, /inbox --context ctx-1/);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test("claude-code: anthropic-version + text body", async () => {
|
|
26
|
+
const r = (await renderWake({ preset: "claude-code", url: "https://api.anthropic.com/v1/claude_code/routines/trig_x/fire", key: "rt" }, ev))!;
|
|
27
|
+
assert.equal(r.headers["anthropic-version"], "2023-06-01");
|
|
28
|
+
assert.equal(r.headers["authorization"], "Bearer rt");
|
|
29
|
+
const b = JSON.parse(r.body);
|
|
30
|
+
assert.deepEqual(Object.keys(b), ["text"]);
|
|
31
|
+
assert.match(b.text, /2 new messages from peer "peer-a"/);
|
|
32
|
+
assert.match(b.text, /untrusted/);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test("openclaw-wake: no peer text, mode now, agentId", async () => {
|
|
36
|
+
const r = (await renderWake({ preset: "openclaw-wake", url: "https://gw/hooks/wake", key: "h", agentId: "ops" }, ev))!;
|
|
37
|
+
const b = JSON.parse(r.body);
|
|
38
|
+
assert.equal(b.mode, "now"); assert.equal(b.agentId, "ops");
|
|
39
|
+
assert.ok(!b.text.includes("Please"), "preview must not be included");
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test("openclaw-agent: isolated, deliver false, idempotency key", async () => {
|
|
43
|
+
const r = (await renderWake({ preset: "openclaw-agent", url: "https://gw/hooks/agent", key: "h" }, ev, { requestId: "rid-1" }))!;
|
|
44
|
+
const b = JSON.parse(r.body);
|
|
45
|
+
assert.equal(b.sessionMode, "isolated"); assert.equal(b.deliver, false); assert.equal(b.agentId, "main");
|
|
46
|
+
assert.equal(r.headers["idempotency-key"], "rid-1");
|
|
47
|
+
assert.ok(b.message.includes("peer-a"));
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test("hermes: HMAC V2 over '<ts>.<body>' matches node:crypto", async () => {
|
|
51
|
+
const now = 1_760_000_000_000;
|
|
52
|
+
const r = (await renderWake({ preset: "hermes", url: "https://h:8644/webhooks/a2a", hmacSecret: "s3cret" }, ev, { nowMs: now, requestId: "r" }))!;
|
|
53
|
+
const ts = r.headers["X-Webhook-Timestamp"];
|
|
54
|
+
assert.equal(ts, String(now / 1000));
|
|
55
|
+
const expect = createHmac("sha256", "s3cret").update(`${ts}.${r.body}`).digest("hex");
|
|
56
|
+
assert.equal(r.headers["X-Webhook-Signature-V2"], expect);
|
|
57
|
+
assert.equal(r.headers["x-request-id"], "r");
|
|
58
|
+
assert.equal(JSON.parse(r.body).event_type, "a2a_wake");
|
|
59
|
+
assert.equal(r.headers["authorization"], undefined);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("generic: custom header/prefix + template with escaping", async () => {
|
|
63
|
+
const tpl = '{"text":"{{from}}: {{preview}}","ids":{{taskIdsJson}},"raw":{{payload}}}';
|
|
64
|
+
const r = (await renderWake({ preset: "generic", url: "https://n8n/webhook/x", key: "abc", keyHeader: "X-Api-Key", keyPrefix: "" , bodyTemplate: tpl }, ev))!;
|
|
65
|
+
assert.equal(r.headers["x-api-key"], "abc");
|
|
66
|
+
const b = JSON.parse(r.body);
|
|
67
|
+
assert.equal(b.text, 'peer-a: Please "do" X\nnow');
|
|
68
|
+
assert.deepEqual(b.ids, ["t-1", "t-2"]);
|
|
69
|
+
assert.equal(b.raw.contextId, "ctx-1");
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test("generic: invalid template throws", () => {
|
|
73
|
+
assert.throws(() => renderTemplate('{"a": {{from}} }', ev, "cli"));
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("every preset renders valid JSON", async () => {
|
|
77
|
+
for (const p of PRESETS) {
|
|
78
|
+
const r = (await renderWake({ preset: p, url: "https://x/y", key: "k", hmacSecret: "s" }, ev))!;
|
|
79
|
+
JSON.parse(r.body);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test("redact masks credentials", async () => {
|
|
84
|
+
const r = (await renderWake({ preset: "grok-bot", url: "https://hook/x", key: "supersecretkey" }, ev))!;
|
|
85
|
+
assert.ok(!JSON.stringify(redact(r)).includes("supersecretkey"));
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("summary for outbound updates points at history", () => {
|
|
89
|
+
assert.match(wakeSummary({ ...ev, kind: "outbound_update" }, "a2a"), /a2a history ctx-1/);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test("generic custom key header is rendered with prefix and redacted in previews", async () => {
|
|
93
|
+
const ev: WakeEvent = { contextId: "c1", taskId: "t1", taskIds: ["t1"], from: "bob", preview: "hi", kind: "inbound", publicUrl: "https://agent.example.com" };
|
|
94
|
+
const req = await renderWake({ preset: "generic", url: "https://hooks.example.com/abc", key: "supersecretvalue", keyHeader: "X-Api-Key", keyPrefix: "" }, ev);
|
|
95
|
+
assert.ok(req);
|
|
96
|
+
assert.equal(req.headers["x-api-key"], "supersecretvalue");
|
|
97
|
+
const red = redact(req);
|
|
98
|
+
assert.ok(!JSON.stringify(red).includes("supersecretvalue"));
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// ---------------------------------------------------------------- Cloudflare Access service token (tunnel wakes)
|
|
102
|
+
const ACCESS = { accessClientId: "abc123.access", accessClientSecret: "s3cr3t-value-0123456789" };
|
|
103
|
+
|
|
104
|
+
test("Access service-token headers are added for every preset, next to the preset's own auth", async () => {
|
|
105
|
+
for (const preset of PRESETS) {
|
|
106
|
+
const r = (await renderWake({ preset, url: "https://wake-x.example.com/hooks/wake", key: "k1", hmacSecret: "h1", ...ACCESS }, ev))!;
|
|
107
|
+
assert.equal(r.headers["CF-Access-Client-Id"], "abc123.access", preset);
|
|
108
|
+
assert.equal(r.headers["CF-Access-Client-Secret"], "s3cr3t-value-0123456789", preset);
|
|
109
|
+
if (preset === "hermes") assert.ok(r.headers["X-Webhook-Signature-V2"], "hermes keeps its HMAC");
|
|
110
|
+
else assert.ok(r.headers["authorization"], `${preset} keeps its bearer`);
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test("Access headers need both halves; none without them", async () => {
|
|
115
|
+
const none = (await renderWake({ preset: "generic", url: "https://h.example/x" }, ev))!;
|
|
116
|
+
assert.ok(!("CF-Access-Client-Id" in none.headers) && !("CF-Access-Client-Secret" in none.headers));
|
|
117
|
+
assert.deepEqual(addAccessHeaders({}, { accessClientId: "only-id" }), {});
|
|
118
|
+
assert.deepEqual(addAccessHeaders({}, { accessClientSecret: "only-secret" }), {});
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test("hermes HMAC is computed over the body only, unaffected by Access headers", async () => {
|
|
122
|
+
const a = (await renderWake({ preset: "hermes", url: "https://h.example/webhooks/a2a", hmacSecret: "h1" }, ev, { nowMs: 1e12, requestId: "r" }))!;
|
|
123
|
+
const b = (await renderWake({ preset: "hermes", url: "https://h.example/webhooks/a2a", hmacSecret: "h1", ...ACCESS }, ev, { nowMs: 1e12, requestId: "r" }))!;
|
|
124
|
+
assert.equal(a.headers["X-Webhook-Signature-V2"], b.headers["X-Webhook-Signature-V2"]);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
test("redact masks both Access headers", async () => {
|
|
128
|
+
const r = redact((await renderWake({ preset: "openclaw-wake", url: "https://wake-x.example.com/hooks/wake", key: "hooks-token", ...ACCESS }, ev))!);
|
|
129
|
+
assert.equal(r.headers["CF-Access-Client-Id"], "abc1…");
|
|
130
|
+
assert.equal(r.headers["CF-Access-Client-Secret"], "s3cr…");
|
|
131
|
+
assert.ok(!JSON.stringify(r).includes("s3cr3t-value"));
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test("cloudflareErrorHint explains tunnel / Access failures", () => {
|
|
135
|
+
assert.match(cloudflareErrorHint(530, "<html>... error code: 1033 ...</html>"), /1033.*no running connector/);
|
|
136
|
+
assert.match(cloudflareErrorHint(530, "<title>Error 1033</title>"), /1033/);
|
|
137
|
+
assert.match(cloudflareErrorHint(530, '<script>a={event:"feedback clicked",properties:{errorCode: 1033 }}</script>'), /1033/);
|
|
138
|
+
assert.match(cloudflareErrorHint(403, "Forbidden. You don't have access. cloudflareaccess.com"), /Cloudflare Access/);
|
|
139
|
+
assert.equal(cloudflareErrorHint(500, "boom"), "");
|
|
140
|
+
});
|
|
141
|
+
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es2024",
|
|
4
|
+
"lib": [
|
|
5
|
+
"es2024"
|
|
6
|
+
],
|
|
7
|
+
"module": "preserve",
|
|
8
|
+
"moduleResolution": "bundler",
|
|
9
|
+
"types": [],
|
|
10
|
+
"strict": true,
|
|
11
|
+
"noEmit": true,
|
|
12
|
+
"allowImportingTsExtensions": true,
|
|
13
|
+
"isolatedModules": true,
|
|
14
|
+
"verbatimModuleSyntax": true,
|
|
15
|
+
"skipLibCheck": true
|
|
16
|
+
},
|
|
17
|
+
"include": [
|
|
18
|
+
"src",
|
|
19
|
+
"cloudflare.config.ts",
|
|
20
|
+
".cloudflare/types"
|
|
21
|
+
]
|
|
22
|
+
}
|