@7h3/protocol 0.1.2 → 0.5.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/.dockerignore +19 -0
- package/.github/dependabot.yml +32 -0
- package/.github/workflows/ci.yml +31 -0
- package/.github/workflows/publish.yml +59 -0
- package/.github/workflows/scorecard.yml +37 -0
- package/7h3.example.yaml +125 -0
- package/CHANGELOG.md +152 -0
- package/CONTRIBUTING.md +82 -0
- package/Dockerfile +73 -0
- package/GOVERNANCE.md +62 -0
- package/README.md +1317 -6
- package/SECURITY.md +70 -0
- package/bench-results/replay-cache-full-1777891033256.json +10 -0
- package/bench-results/replay-cache-full-1777896317488.json +10 -0
- package/bench-results/replay-cache-full-1777900993184.json +10 -0
- package/bench-results/replay-cache-full-1777901019285.json +10 -0
- package/bench-results/replay-cache-quick-1777870170126.json +10 -0
- package/bench-results/signature-profiles-quick-1775875160079.json +85 -0
- package/bench-results/signature-profiles-quick-1775983539716.json +85 -0
- package/bench-results/signature-profiles-quick-1776237913190.json +85 -0
- package/bench-results/wire-codecs-full-1777891019803.json +93 -0
- package/bench-results/wire-codecs-full-1777896260964.json +93 -0
- package/bench-results/wire-codecs-full-1777901004247.json +93 -0
- package/bench-results/wire-codecs-quick-1775972879056.json +93 -0
- package/bench-results/wire-codecs-quick-1775983541111.json +93 -0
- package/bench-results/wire-codecs-quick-1776237914299.json +93 -0
- package/bench-results/wire-codecs-quick-1777841285236.json +93 -0
- package/bench-results/wire-codecs-quick-1777841321772.json +93 -0
- package/bench-results/wire-codecs-quick-1777841330408.json +93 -0
- package/bench-results/wire-codecs-quick-1777852886082.json +93 -0
- package/bench-results/wire-codecs-quick-1777852988773.json +93 -0
- package/bench-results/wire-codecs-quick-1777870188095.json +93 -0
- package/bench-results/wire-codecs-quick-1777870263918.json +93 -0
- package/bench-results/wire-codecs-quick-1777870455034.json +93 -0
- package/bench-results/wire-codecs-quick-1778816163081.json +93 -0
- package/bench-results/wire-codecs-quick-1778843936130.json +93 -0
- package/bin/7h3.ts +406 -0
- package/conformance/7h3_v0_1.json +77 -0
- package/conformance/7h3_v0_1_binary.json +20 -0
- package/conformance/aip_v0_1_binary.json +20 -0
- package/docker-compose.yaml +77 -0
- package/docs/ADOPTION_PLAN.md +120 -0
- package/docs/AGENTS.md +77 -0
- package/docs/AIP_RFC_v0.1.md +97 -0
- package/docs/AI_DECISION_CARD.md +122 -0
- package/docs/AI_RUNTIME_POLICY.json +126 -0
- package/docs/AI_RUNTIME_POLICY.yaml +110 -0
- package/docs/BACKPRESSURE_TUNING.md +65 -0
- package/docs/BENCHMARK_CLAIM_MATRIX.md +42 -0
- package/docs/BENCHMARK_REPORT_TEMPLATE.md +169 -0
- package/docs/BINARY_CODEC_BENCH.md +23 -0
- package/docs/CLEAN_CLONE_RUNBOOK.md +36 -0
- package/docs/CLOCK_SKEW_POLICY.md +30 -0
- package/docs/DISTRIBUTED_REPLAY.md +142 -0
- package/docs/FUZZ_CAMPAIGN.md +121 -0
- package/docs/GATEWAY.md +195 -0
- package/docs/KEY_MANAGEMENT_POLICY.md +53 -0
- package/docs/KEY_REVOCATION.md +69 -0
- package/docs/MCP_WRAPPER.md +159 -0
- package/docs/MIGRATION_GUIDE.md +40 -0
- package/docs/OPERATORS.md +184 -0
- package/docs/PERF_REGRESSION_POLICY.md +34 -0
- package/docs/PROJECT_EXAMINATION_2026-05-31.md +219 -0
- package/docs/RELEASE_BENCHMARK_REPORT_2026-05-15.md +135 -0
- package/docs/RELEASE_GATE.md +25 -0
- package/docs/RELEASE_NOTES_v0.1.0.md +54 -0
- package/docs/SECURITY_REVIEW_2026-06-05.md +165 -0
- package/docs/TELEMETRY.md +41 -0
- package/docs/THREAT_MODEL.md +89 -0
- package/docs/VERSIONING_POLICY.md +30 -0
- package/docs/assets/banner-github.png +0 -0
- package/docs/assets/banner.png +0 -0
- package/docs/assets/banner.svg +123 -0
- package/eslint.config.js +15 -0
- package/fuzz/ts/harness-decode.ts +136 -0
- package/fuzz/ts/harness-verify.ts +121 -0
- package/fuzz/ts/run.ts +35 -0
- package/mcp-server/README.md +38 -0
- package/mcp-server/package-lock.json +1187 -0
- package/mcp-server/package.json +35 -0
- package/mcp-server/src/index.ts +236 -0
- package/mcp-server/tsconfig.json +14 -0
- package/package.json +121 -13
- package/scripts/aip-framework-quickstart.ts +110 -0
- package/scripts/aip-mcp-gateway.ts +38 -0
- package/scripts/aip-mcp-wrap-demo.ts +72 -0
- package/scripts/aip-quickstart.ts +60 -0
- package/scripts/bench-diff.ts +118 -0
- package/scripts/bench-protocol-e2e.ts +937 -0
- package/scripts/bench-protocol-openloop.ts +1397 -0
- package/scripts/bench-replay-cache.ts +76 -0
- package/scripts/bench-signature-profiles.ts +180 -0
- package/scripts/bench-wire-codecs.ts +161 -0
- package/scripts/build-binary-conformance.ts +36 -0
- package/scripts/build-release-dashboard.ts +175 -0
- package/scripts/canary-rollout.ts +38 -0
- package/scripts/mcpGatewayCli.test.ts +116 -0
- package/scripts/prepare-aip-package.ts +88 -0
- package/scripts/regen-conformance-sigs.ts +18 -0
- package/scripts/release-gate.ts +19 -0
- package/scripts/validate-runtime-policy.ts +18 -0
- package/sdk/browser/index.test.ts +162 -0
- package/sdk/browser/index.ts +257 -0
- package/sdk/browser/package.json +13 -0
- package/sdk/go/cbor.go +551 -0
- package/sdk/go/cbor_test.go +232 -0
- package/sdk/go/encryption.go +280 -0
- package/sdk/go/encryption_test.go +318 -0
- package/sdk/go/go.mod +7 -0
- package/sdk/go/go.sum +4 -0
- package/sdk/go/http.go +135 -0
- package/sdk/go/protocol.go +324 -0
- package/sdk/go/protocol_test.go +334 -0
- package/sdk/go/replay.go +121 -0
- package/sdk/go/replay_test.go +149 -0
- package/sdk/go/webhook.go +136 -0
- package/sdk/pq/package-lock.json +1358 -0
- package/sdk/pq/package.json +42 -0
- package/sdk/pq/src/index.test.ts +143 -0
- package/sdk/pq/src/index.ts +166 -0
- package/sdk/pq/tsconfig.json +14 -0
- package/sdk/pq/vitest.config.ts +7 -0
- package/sdk/python/README.md +18 -0
- package/sdk/python/protocol_7h3/__init__.py +46 -0
- package/sdk/python/protocol_7h3/encryption.py +252 -0
- package/sdk/python/protocol_7h3/http.py +212 -0
- package/sdk/python/protocol_7h3/keys.py +149 -0
- package/sdk/python/protocol_7h3/pq.py +244 -0
- package/sdk/python/protocol_7h3/protocol.py +525 -0
- package/sdk/python/protocol_7h3/queue.py +118 -0
- package/sdk/python/protocol_7h3/replay.py +98 -0
- package/sdk/python/protocol_7h3/webhook.py +116 -0
- package/sdk/python/pyproject.toml +40 -0
- package/sdk/python/tests/test_conformance.py +110 -0
- package/sdk/python/tests/test_encryption.py +206 -0
- package/sdk/python/tests/test_http.py +305 -0
- package/sdk/python/tests/test_keys.py +417 -0
- package/sdk/python/tests/test_queue.py +120 -0
- package/sdk/python/tests/test_webhook.py +345 -0
- package/sdk/rust/Cargo.lock +371 -0
- package/sdk/rust/Cargo.toml +25 -0
- package/sdk/rust/README.md +31 -0
- package/sdk/rust/fuzz/Cargo.toml +29 -0
- package/sdk/rust/fuzz/fuzz_targets/fuzz_canonicalize.rs +46 -0
- package/sdk/rust/fuzz/fuzz_targets/fuzz_decode.rs +11 -0
- package/sdk/rust/src/bin/aip_mcp_gateway.rs +59 -0
- package/sdk/rust/src/http.rs +145 -0
- package/sdk/rust/src/keys.rs +161 -0
- package/sdk/rust/src/lib.rs +688 -0
- package/sdk/rust/src/queue.rs +79 -0
- package/sdk/rust/src/webhook.rs +86 -0
- package/sdk/rust/tests/conformance.rs +148 -0
- package/sdk/rust/tests/gateway.rs +130 -0
- package/sdk/rust/tests/http_webhook_queue.rs +201 -0
- package/sdk/rust/tests/keys.rs +189 -0
- package/sdk/threshold/index.d.ts +68 -0
- package/sdk/threshold/index.d.ts.map +1 -0
- package/sdk/threshold/index.js +254 -0
- package/sdk/threshold/package-lock.json +1361 -0
- package/sdk/threshold/package.json +39 -0
- package/sdk/threshold/src/index.d.ts +68 -0
- package/sdk/threshold/src/index.d.ts.map +1 -0
- package/sdk/threshold/src/index.js +254 -0
- package/sdk/threshold/src/index.test.ts +238 -0
- package/sdk/threshold/src/index.ts +355 -0
- package/sdk/threshold/tsconfig.json +19 -0
- package/sdk/threshold/vitest.config.ts +12 -0
- package/src/agentAdapter.test.ts +48 -0
- package/src/agentAdapter.ts +56 -0
- package/src/auditLog.test.ts +145 -0
- package/src/auditLog.ts +147 -0
- package/src/capability.test.ts +504 -0
- package/src/capability.ts +380 -0
- package/src/cborCodec.test.ts +263 -0
- package/src/cborCodec.ts +339 -0
- package/src/conformance.test.ts +136 -0
- package/src/conformanceVectors.ts +99 -0
- package/src/encryption.test.ts +206 -0
- package/src/encryption.ts +245 -0
- package/src/envelopeCbor.ts +140 -0
- package/src/frameworkAdapters.test.ts +290 -0
- package/src/frameworkAdapters.ts +261 -0
- package/src/gateway.test.ts +343 -0
- package/src/gateway.ts +246 -0
- package/src/grpcBinding.test.ts +211 -0
- package/src/grpcBinding.ts +103 -0
- package/src/httpBinding.test.ts +376 -0
- package/src/httpBinding.ts +189 -0
- package/src/index.ts +39 -0
- package/src/keyInfra.test.ts +278 -0
- package/src/keyInfra.ts +228 -0
- package/src/keyRegistry.ts +59 -0
- package/src/keyRotation.test.ts +78 -0
- package/src/keyRotation.ts +72 -0
- package/src/mcpGateway.test.ts +129 -0
- package/src/mcpGateway.ts +250 -0
- package/src/mcpTransports.test.ts +92 -0
- package/src/mcpTransports.ts +169 -0
- package/src/mcpWrapper.test.ts +179 -0
- package/src/mcpWrapper.ts +206 -0
- package/src/otel.ts +136 -0
- package/src/policyEnforcer.test.ts +99 -0
- package/src/policyEnforcer.ts +169 -0
- package/src/policyTelemetryFeedback.test.ts +25 -0
- package/src/policyTelemetryFeedback.ts +38 -0
- package/src/protocol.bench.ts +37 -0
- package/{protocol.d.ts → src/protocol.d.ts} +2 -1
- package/src/protocol.d.ts.map +1 -0
- package/src/protocol.js +294 -0
- package/src/protocol.test.ts +155 -0
- package/src/protocol.ts +414 -0
- package/src/protocolAgent.test.ts +105 -0
- package/src/protocolAgent.ts +169 -0
- package/src/protocolBinary.test.ts +165 -0
- package/src/protocolBinary.ts +312 -0
- package/src/protocolCapabilities.ts +70 -0
- package/src/protocolFuzz.advanced.test.ts +235 -0
- package/src/protocolFuzz.test.ts +111 -0
- package/src/protocolNegative.test.ts +97 -0
- package/src/protocolReplay.test.ts +71 -0
- package/src/protocolReplay.ts +194 -0
- package/src/protocolTransport.test.ts +556 -0
- package/src/protocolTransport.ts +483 -0
- package/src/queueBinding.test.ts +130 -0
- package/src/queueBinding.ts +102 -0
- package/src/rateLimiter.test.ts +96 -0
- package/src/rateLimiter.ts +46 -0
- package/src/redisClient.ts +140 -0
- package/src/redisIntegration.test.ts +134 -0
- package/src/replayStores.test.ts +273 -0
- package/src/replayStores.ts +215 -0
- package/src/revocation.test.ts +98 -0
- package/src/revocation.ts +0 -0
- package/src/routePolicy.test.ts +87 -0
- package/src/routePolicy.ts +72 -0
- package/src/runtimePolicy.test.ts +49 -0
- package/src/runtimePolicy.ts +81 -0
- package/src/runtimePolicyManager.test.ts +29 -0
- package/src/runtimePolicyManager.ts +50 -0
- package/src/runtimePolicyPresets.ts +43 -0
- package/src/signedResponse.test.ts +111 -0
- package/src/signedResponse.ts +83 -0
- package/src/stream.test.ts +254 -0
- package/src/stream.ts +417 -0
- package/src/telemetry.test.ts +251 -0
- package/src/telemetry.ts +299 -0
- package/src/webhookBinding.test.ts +144 -0
- package/src/webhookBinding.ts +115 -0
- package/src/wsBinding.test.ts +221 -0
- package/src/wsBinding.ts +200 -0
- package/tsconfig.json +15 -0
- package/tsconfig.lib.json +23 -0
- package/vite.lib.config.ts +16 -0
- package/vitest.config.ts +11 -0
- package/agentAdapter.d.ts +0 -26
- package/conformanceVectors.d.ts +0 -20
- package/frameworkAdapters.d.ts +0 -72
- package/index.d.ts +0 -20
- package/index.js +0 -1702
- package/keyRotation.d.ts +0 -20
- package/mcpGateway.d.ts +0 -37
- package/mcpTransports.d.ts +0 -62
- package/mcpWrapper.d.ts +0 -83
- package/policyEnforcer.d.ts +0 -50
- package/policyTelemetryFeedback.d.ts +0 -11
- package/protocolAgent.d.ts +0 -58
- package/protocolBinary.d.ts +0 -8
- package/protocolCapabilities.d.ts +0 -24
- package/protocolReplay.d.ts +0 -35
- package/protocolTransport.d.ts +0 -73
- package/redisClient.d.ts +0 -49
- package/replayStores.d.ts +0 -32
- package/revocation.d.ts +0 -71
- package/runtimePolicy.d.ts +0 -24
- package/runtimePolicyManager.d.ts +0 -15
- package/runtimePolicyPresets.d.ts +0 -11
package/index.js
DELETED
|
@@ -1,1702 +0,0 @@
|
|
|
1
|
-
const S = new TextEncoder(), ue = 256, I = /* @__PURE__ */ new Map(), J = 256, M = /* @__PURE__ */ new Map(), k = /* @__PURE__ */ new Map();
|
|
2
|
-
function j() {
|
|
3
|
-
return globalThis.Buffer ?? null;
|
|
4
|
-
}
|
|
5
|
-
function he(t) {
|
|
6
|
-
const e = [
|
|
7
|
-
`"messageId":${JSON.stringify(t.messageId)}`,
|
|
8
|
-
`"nonce":${JSON.stringify(t.nonce)}`
|
|
9
|
-
];
|
|
10
|
-
return t.recipient !== void 0 && e.push(`"recipient":${JSON.stringify(t.recipient)}`), e.push(`"sender":${JSON.stringify(t.sender)}`), e.push(`"timestampMs":${t.timestampMs}`), e.push(`"ttlMs":${t.ttlMs}`), e.push(`"version":${JSON.stringify(t.version)}`), `{${e.join(",")}}`;
|
|
11
|
-
}
|
|
12
|
-
function fe(t) {
|
|
13
|
-
const e = [];
|
|
14
|
-
return t.capability !== void 0 && e.push(`"capability":${JSON.stringify(t.capability)}`), e.push(`"content":${JSON.stringify(t.content)}`), t.correlationId !== void 0 && e.push(`"correlationId":${JSON.stringify(t.correlationId)}`), e.push(`"intent":${JSON.stringify(t.intent)}`), `{${e.join(",")}}`;
|
|
15
|
-
}
|
|
16
|
-
function H(t) {
|
|
17
|
-
const e = `hs256:${t}`, r = I.get(e);
|
|
18
|
-
if (r) return r;
|
|
19
|
-
I.size >= ue && I.clear();
|
|
20
|
-
const s = b().importKey(
|
|
21
|
-
"raw",
|
|
22
|
-
S.encode(t),
|
|
23
|
-
{ name: "HMAC", hash: "SHA-256" },
|
|
24
|
-
!1,
|
|
25
|
-
["sign", "verify"]
|
|
26
|
-
).catch((i) => {
|
|
27
|
-
throw I.delete(e), i;
|
|
28
|
-
});
|
|
29
|
-
return I.set(e, s), s;
|
|
30
|
-
}
|
|
31
|
-
function ye(t) {
|
|
32
|
-
const e = `ed25519:pkcs8:${t}`, r = M.get(e);
|
|
33
|
-
if (r) return r;
|
|
34
|
-
M.size >= J && M.clear();
|
|
35
|
-
const s = b().importKey("pkcs8", V(N(t)), { name: "Ed25519" }, !1, ["sign"]).catch((i) => {
|
|
36
|
-
throw M.delete(e), i;
|
|
37
|
-
});
|
|
38
|
-
return M.set(e, s), s;
|
|
39
|
-
}
|
|
40
|
-
function pe(t) {
|
|
41
|
-
const e = `ed25519:spki:${t}`, r = k.get(e);
|
|
42
|
-
if (r) return r;
|
|
43
|
-
k.size >= J && k.clear();
|
|
44
|
-
const s = b().importKey("spki", V(N(t)), { name: "Ed25519" }, !1, ["verify"]).catch((i) => {
|
|
45
|
-
throw k.delete(e), i;
|
|
46
|
-
});
|
|
47
|
-
return k.set(e, s), s;
|
|
48
|
-
}
|
|
49
|
-
function C(t) {
|
|
50
|
-
const e = j();
|
|
51
|
-
return (e ? e.from(t).toString("base64") : btoa(String.fromCharCode(...t))).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/g, "");
|
|
52
|
-
}
|
|
53
|
-
function N(t) {
|
|
54
|
-
const e = t.replace(/-/g, "+").replace(/_/g, "/").padEnd(Math.ceil(t.length / 4) * 4, "="), r = j();
|
|
55
|
-
if (r)
|
|
56
|
-
return new Uint8Array(r.from(e, "base64"));
|
|
57
|
-
const n = atob(e), s = new Uint8Array(n.length);
|
|
58
|
-
for (let i = 0; i < n.length; i += 1)
|
|
59
|
-
s[i] = n.charCodeAt(i);
|
|
60
|
-
return s;
|
|
61
|
-
}
|
|
62
|
-
function V(t) {
|
|
63
|
-
return t.buffer.slice(t.byteOffset, t.byteOffset + t.byteLength);
|
|
64
|
-
}
|
|
65
|
-
function A(t) {
|
|
66
|
-
return `{"body":${fe(t.body)},"header":${he(t.header)}}`;
|
|
67
|
-
}
|
|
68
|
-
function b() {
|
|
69
|
-
if (typeof crypto > "u" || !crypto.subtle)
|
|
70
|
-
throw new Error("Web Crypto API is not available in this runtime");
|
|
71
|
-
return crypto.subtle;
|
|
72
|
-
}
|
|
73
|
-
async function W(t, e) {
|
|
74
|
-
const r = b(), n = await H(e), s = await r.sign("HMAC", n, S.encode(t));
|
|
75
|
-
return C(new Uint8Array(s));
|
|
76
|
-
}
|
|
77
|
-
async function G(t, e, r) {
|
|
78
|
-
const n = b(), s = await H(r), i = N(e);
|
|
79
|
-
return n.verify("HMAC", s, i.buffer, S.encode(t));
|
|
80
|
-
}
|
|
81
|
-
async function Y(t, e) {
|
|
82
|
-
const r = b(), n = await ye(e), s = await r.sign("Ed25519", n, S.encode(t));
|
|
83
|
-
return C(new Uint8Array(s));
|
|
84
|
-
}
|
|
85
|
-
async function X(t, e, r) {
|
|
86
|
-
const n = b(), s = await pe(r), i = N(e);
|
|
87
|
-
return n.verify("Ed25519", s, i.buffer, S.encode(t));
|
|
88
|
-
}
|
|
89
|
-
async function ct(t, e) {
|
|
90
|
-
return W(t, e);
|
|
91
|
-
}
|
|
92
|
-
async function me(t, e, r) {
|
|
93
|
-
return G(t, e, r);
|
|
94
|
-
}
|
|
95
|
-
async function dt() {
|
|
96
|
-
const t = b(), e = await t.generateKey({ name: "Ed25519" }, !0, ["sign", "verify"]), r = await t.exportKey("pkcs8", e.privateKey), n = await t.exportKey("spki", e.publicKey);
|
|
97
|
-
return {
|
|
98
|
-
privateKey: C(new Uint8Array(r)),
|
|
99
|
-
publicKey: C(new Uint8Array(n))
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
async function lt(t, e) {
|
|
103
|
-
return Y(t, e);
|
|
104
|
-
}
|
|
105
|
-
async function ge(t, e, r) {
|
|
106
|
-
return X(t, e, r);
|
|
107
|
-
}
|
|
108
|
-
async function Q(t, e, r = "local-dev-key") {
|
|
109
|
-
const n = A(t), s = await W(n, e);
|
|
110
|
-
return {
|
|
111
|
-
...t,
|
|
112
|
-
signature: {
|
|
113
|
-
alg: "HS256",
|
|
114
|
-
keyId: r,
|
|
115
|
-
value: s
|
|
116
|
-
}
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
async function we(t, e) {
|
|
120
|
-
if (!t.signature || t.signature.alg !== "HS256") return !1;
|
|
121
|
-
const r = {
|
|
122
|
-
header: t.header,
|
|
123
|
-
body: t.body
|
|
124
|
-
}, n = A(r);
|
|
125
|
-
return G(n, t.signature.value, e);
|
|
126
|
-
}
|
|
127
|
-
async function ut(t, e, r = "local-ed25519-key") {
|
|
128
|
-
const n = A(t), s = await Y(n, e);
|
|
129
|
-
return {
|
|
130
|
-
...t,
|
|
131
|
-
signature: {
|
|
132
|
-
alg: "ED25519",
|
|
133
|
-
keyId: r,
|
|
134
|
-
value: s
|
|
135
|
-
}
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
async function be(t, e) {
|
|
139
|
-
if (!t.signature || t.signature.alg !== "ED25519") return !1;
|
|
140
|
-
const r = {
|
|
141
|
-
header: t.header,
|
|
142
|
-
body: t.body
|
|
143
|
-
}, n = A(r);
|
|
144
|
-
return X(n, t.signature.value, e);
|
|
145
|
-
}
|
|
146
|
-
async function ht(t, e) {
|
|
147
|
-
return !t.signature || t.signature.alg !== e.alg ? !1 : e.alg === "HS256" ? we(t, e.secret) : be(t, e.publicKey);
|
|
148
|
-
}
|
|
149
|
-
async function ve(t, e, r) {
|
|
150
|
-
return !e || e.alg !== r.alg ? !1 : r.alg === "HS256" ? me(t, e.value, r.secret) : ge(t, e.value, r.publicKey);
|
|
151
|
-
}
|
|
152
|
-
function Ie(t, e = Date.now()) {
|
|
153
|
-
const r = [], n = t.header ?? {}, s = t.body ?? {}, i = typeof n.version == "string" ? n.version : "", a = typeof n.messageId == "string" ? n.messageId : "", o = typeof n.sender == "string" ? n.sender : "", l = typeof n.timestampMs == "number" ? n.timestampMs : 0, c = typeof n.ttlMs == "number" ? n.ttlMs : 0, h = typeof s.content == "string" ? s.content : "";
|
|
154
|
-
return i !== "aip/0.1" && r.push({ level: "error", message: `Unsupported protocol version '${i}'` }), a.trim() || r.push({ level: "error", message: "Missing messageId" }), o.trim() || r.push({ level: "error", message: "Missing sender identity" }), c <= 0 && r.push({ level: "error", message: "ttlMs must be greater than zero" }), l + c < e && r.push({ level: "error", message: "Message TTL expired" }), h.trim() || r.push({ level: "warning", message: "Empty content payload" }), r;
|
|
155
|
-
}
|
|
156
|
-
function P(t) {
|
|
157
|
-
const e = t.nowMs ?? Date.now();
|
|
158
|
-
return {
|
|
159
|
-
header: {
|
|
160
|
-
version: "aip/0.1",
|
|
161
|
-
messageId: t.messageId ?? `msg-${e}-${Math.random().toString(36).slice(2, 10)}`,
|
|
162
|
-
timestampMs: e,
|
|
163
|
-
ttlMs: t.ttlMs ?? 6e4,
|
|
164
|
-
sender: t.sender,
|
|
165
|
-
recipient: t.recipient,
|
|
166
|
-
nonce: t.nonce ?? Math.random().toString(36).slice(2, 12)
|
|
167
|
-
},
|
|
168
|
-
body: {
|
|
169
|
-
intent: t.intent,
|
|
170
|
-
content: t.content,
|
|
171
|
-
capability: t.capability,
|
|
172
|
-
correlationId: t.correlationId
|
|
173
|
-
}
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
|
-
const Z = [65, 73, 80, 66], D = Uint8Array.from(Z), q = 1, Me = 1024 * 1024, ee = 1, te = 2, re = 4, ne = 8, ke = new TextEncoder(), Se = new TextDecoder(), Ae = /* @__PURE__ */ new Set(["PING", "PONG", "CAPS", "TASK", "RESULT", "ERROR"]);
|
|
177
|
-
function Ee(t) {
|
|
178
|
-
return ke.encode(t);
|
|
179
|
-
}
|
|
180
|
-
function xe(t) {
|
|
181
|
-
return Se.decode(t);
|
|
182
|
-
}
|
|
183
|
-
function Re(t) {
|
|
184
|
-
return t === "ED25519" ? 2 : 1;
|
|
185
|
-
}
|
|
186
|
-
function Ce(t) {
|
|
187
|
-
return t === 1 ? "HS256" : t === 2 ? "ED25519" : null;
|
|
188
|
-
}
|
|
189
|
-
function g(t) {
|
|
190
|
-
return { bytes: Ee(t) };
|
|
191
|
-
}
|
|
192
|
-
function w(t) {
|
|
193
|
-
return t ? 4 + t.bytes.byteLength : 0;
|
|
194
|
-
}
|
|
195
|
-
class Ne {
|
|
196
|
-
bytes;
|
|
197
|
-
view;
|
|
198
|
-
offset = 0;
|
|
199
|
-
constructor(e) {
|
|
200
|
-
this.bytes = new Uint8Array(e), this.view = new DataView(this.bytes.buffer, this.bytes.byteOffset, this.bytes.byteLength);
|
|
201
|
-
}
|
|
202
|
-
writeU8(e) {
|
|
203
|
-
this.bytes[this.offset] = e & 255, this.offset += 1;
|
|
204
|
-
}
|
|
205
|
-
writeU32(e) {
|
|
206
|
-
this.view.setUint32(this.offset, e, !1), this.offset += 4;
|
|
207
|
-
}
|
|
208
|
-
writeU64(e) {
|
|
209
|
-
this.view.setBigUint64(this.offset, BigInt(e), !1), this.offset += 8;
|
|
210
|
-
}
|
|
211
|
-
writeBytes(e) {
|
|
212
|
-
this.bytes.set(e, this.offset), this.offset += e.byteLength;
|
|
213
|
-
}
|
|
214
|
-
writeString(e) {
|
|
215
|
-
this.writeU32(e.bytes.byteLength), this.writeBytes(e.bytes);
|
|
216
|
-
}
|
|
217
|
-
finish() {
|
|
218
|
-
return this.bytes;
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
class Pe {
|
|
222
|
-
bytes;
|
|
223
|
-
offset = 0;
|
|
224
|
-
constructor(e) {
|
|
225
|
-
this.bytes = e;
|
|
226
|
-
}
|
|
227
|
-
remaining() {
|
|
228
|
-
return this.bytes.byteLength - this.offset;
|
|
229
|
-
}
|
|
230
|
-
readU8() {
|
|
231
|
-
if (this.remaining() < 1) throw new Error("Unexpected end of binary envelope");
|
|
232
|
-
const e = this.bytes[this.offset];
|
|
233
|
-
return this.offset += 1, e ?? 0;
|
|
234
|
-
}
|
|
235
|
-
readU32() {
|
|
236
|
-
if (this.remaining() < 4) throw new Error("Unexpected end of binary envelope");
|
|
237
|
-
const e = new DataView(this.bytes.buffer, this.bytes.byteOffset + this.offset, 4).getUint32(0, !1);
|
|
238
|
-
return this.offset += 4, e;
|
|
239
|
-
}
|
|
240
|
-
readU64() {
|
|
241
|
-
if (this.remaining() < 8) throw new Error("Unexpected end of binary envelope");
|
|
242
|
-
const e = new DataView(this.bytes.buffer, this.bytes.byteOffset + this.offset, 8).getBigUint64(0, !1);
|
|
243
|
-
return this.offset += 8, Number(e);
|
|
244
|
-
}
|
|
245
|
-
readBytes(e) {
|
|
246
|
-
if (e < 0 || this.remaining() < e) throw new Error("Unexpected end of binary envelope");
|
|
247
|
-
const r = this.bytes.slice(this.offset, this.offset + e);
|
|
248
|
-
return this.offset += e, r;
|
|
249
|
-
}
|
|
250
|
-
readString() {
|
|
251
|
-
const e = this.readU32(), r = this.readBytes(e);
|
|
252
|
-
return xe(r);
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
function Te(t) {
|
|
256
|
-
const e = (t.header.recipient ? ee : 0) | (t.body.capability ? te : 0) | (t.body.correlationId ? re : 0) | (t.signature ? ne : 0), r = g(t.header.version), n = g(t.header.messageId), s = g(t.header.sender), i = g(t.header.nonce), a = g(t.body.intent), o = g(t.body.content), l = t.header.recipient ? g(t.header.recipient) : null, c = t.body.capability ? g(t.body.capability) : null, h = t.body.correlationId ? g(t.body.correlationId) : null, f = t.signature ? g(t.signature.keyId) : null, y = t.signature ? g(t.signature.value) : null, u = D.byteLength + 1 + 1 + 8 + 4 + w(r) + w(n) + w(s) + w(i) + w(a) + w(o) + w(l) + w(c) + w(h) + (t.signature ? 1 : 0) + w(f) + w(y), d = new Ne(u);
|
|
257
|
-
return d.writeBytes(D), d.writeU8(q), d.writeU8(e), d.writeU64(t.header.timestampMs), d.writeU32(t.header.ttlMs), d.writeString(r), d.writeString(n), d.writeString(s), d.writeString(i), d.writeString(a), d.writeString(o), l && d.writeString(l), c && d.writeString(c), h && d.writeString(h), t.signature && f && y && (d.writeU8(Re(t.signature.alg)), d.writeString(f), d.writeString(y)), d.finish();
|
|
258
|
-
}
|
|
259
|
-
function _e(t, e = {}) {
|
|
260
|
-
try {
|
|
261
|
-
const r = e.maxFrameBytes ?? Me;
|
|
262
|
-
if (t.byteLength > r)
|
|
263
|
-
return {
|
|
264
|
-
ok: !1,
|
|
265
|
-
diagnostics: [{ level: "error", message: `Binary envelope exceeds maximum frame size '${r}'` }],
|
|
266
|
-
envelope: null
|
|
267
|
-
};
|
|
268
|
-
const n = new Pe(t);
|
|
269
|
-
if ([n.readU8(), n.readU8(), n.readU8(), n.readU8()].some((h, f) => h !== Z[f]))
|
|
270
|
-
return {
|
|
271
|
-
ok: !1,
|
|
272
|
-
diagnostics: [{ level: "error", message: "Invalid binary envelope magic" }],
|
|
273
|
-
envelope: null
|
|
274
|
-
};
|
|
275
|
-
const i = n.readU8();
|
|
276
|
-
if (i !== q)
|
|
277
|
-
return {
|
|
278
|
-
ok: !1,
|
|
279
|
-
diagnostics: [{ level: "error", message: `Unsupported binary envelope version '${i}'` }],
|
|
280
|
-
envelope: null
|
|
281
|
-
};
|
|
282
|
-
const a = n.readU8(), o = n.readU64(), l = n.readU32(), c = {
|
|
283
|
-
header: {
|
|
284
|
-
version: n.readString(),
|
|
285
|
-
messageId: n.readString(),
|
|
286
|
-
timestampMs: o,
|
|
287
|
-
ttlMs: l,
|
|
288
|
-
sender: n.readString(),
|
|
289
|
-
nonce: n.readString()
|
|
290
|
-
},
|
|
291
|
-
body: {
|
|
292
|
-
intent: n.readString(),
|
|
293
|
-
content: n.readString()
|
|
294
|
-
}
|
|
295
|
-
};
|
|
296
|
-
if (c.header.version !== "aip/0.1")
|
|
297
|
-
return {
|
|
298
|
-
ok: !1,
|
|
299
|
-
diagnostics: [{ level: "error", message: `Unsupported protocol version '${c.header.version}'` }],
|
|
300
|
-
envelope: null
|
|
301
|
-
};
|
|
302
|
-
if (!Ae.has(c.body.intent))
|
|
303
|
-
return {
|
|
304
|
-
ok: !1,
|
|
305
|
-
diagnostics: [{ level: "error", message: `Unsupported intent '${c.body.intent}'` }],
|
|
306
|
-
envelope: null
|
|
307
|
-
};
|
|
308
|
-
if (a & ee && (c.header.recipient = n.readString()), a & te && (c.body.capability = n.readString()), a & re && (c.body.correlationId = n.readString()), a & ne) {
|
|
309
|
-
const h = n.readU8(), f = Ce(h);
|
|
310
|
-
if (!f)
|
|
311
|
-
return {
|
|
312
|
-
ok: !1,
|
|
313
|
-
diagnostics: [{ level: "error", message: `Unsupported binary signature algorithm '${h}'` }],
|
|
314
|
-
envelope: null
|
|
315
|
-
};
|
|
316
|
-
c.signature = {
|
|
317
|
-
alg: f,
|
|
318
|
-
keyId: n.readString(),
|
|
319
|
-
value: n.readString()
|
|
320
|
-
};
|
|
321
|
-
}
|
|
322
|
-
return n.remaining() !== 0 ? {
|
|
323
|
-
ok: !1,
|
|
324
|
-
diagnostics: [{ level: "error", message: "Trailing bytes found in binary envelope" }],
|
|
325
|
-
envelope: null
|
|
326
|
-
} : {
|
|
327
|
-
ok: !0,
|
|
328
|
-
diagnostics: [],
|
|
329
|
-
envelope: c
|
|
330
|
-
};
|
|
331
|
-
} catch (r) {
|
|
332
|
-
return {
|
|
333
|
-
ok: !1,
|
|
334
|
-
diagnostics: [{ level: "error", message: r instanceof Error ? r.message : "Invalid binary envelope" }],
|
|
335
|
-
envelope: null
|
|
336
|
-
};
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
class $e {
|
|
340
|
-
items = [];
|
|
341
|
-
peek() {
|
|
342
|
-
return this.items[0];
|
|
343
|
-
}
|
|
344
|
-
push(e) {
|
|
345
|
-
this.items.push(e), this.bubbleUp(this.items.length - 1);
|
|
346
|
-
}
|
|
347
|
-
pop() {
|
|
348
|
-
if (this.items.length === 0) return;
|
|
349
|
-
const e = this.items[0], r = this.items.pop();
|
|
350
|
-
return this.items.length > 0 && r && (this.items[0] = r, this.bubbleDown(0)), e;
|
|
351
|
-
}
|
|
352
|
-
bubbleUp(e) {
|
|
353
|
-
let r = e;
|
|
354
|
-
for (; r > 0; ) {
|
|
355
|
-
const n = Math.floor((r - 1) / 2), s = this.items[n], i = this.items[r];
|
|
356
|
-
if (!s || !i || s.expiresAt <= i.expiresAt) break;
|
|
357
|
-
this.items[n] = i, this.items[r] = s, r = n;
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
bubbleDown(e) {
|
|
361
|
-
let r = e;
|
|
362
|
-
for (; ; ) {
|
|
363
|
-
const n = r * 2 + 1, s = r * 2 + 2;
|
|
364
|
-
let i = r;
|
|
365
|
-
const a = this.items[r], o = this.items[n], l = this.items[s];
|
|
366
|
-
o && a && o.expiresAt < a.expiresAt && (i = n);
|
|
367
|
-
const c = this.items[i];
|
|
368
|
-
if (l && c && l.expiresAt < c.expiresAt && (i = s), i === r) break;
|
|
369
|
-
const h = this.items[r], f = this.items[i];
|
|
370
|
-
if (!h || !f) break;
|
|
371
|
-
this.items[r] = f, this.items[i] = h, r = i;
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
class T {
|
|
376
|
-
entries = /* @__PURE__ */ new Map();
|
|
377
|
-
maxEntries;
|
|
378
|
-
expiries = new $e();
|
|
379
|
-
constructor(e = 1e4) {
|
|
380
|
-
this.maxEntries = e;
|
|
381
|
-
}
|
|
382
|
-
makeKey(e) {
|
|
383
|
-
return `${e.header.sender}|${e.header.messageId}|${e.header.nonce}`;
|
|
384
|
-
}
|
|
385
|
-
prune(e = Date.now()) {
|
|
386
|
-
for (; ; ) {
|
|
387
|
-
const r = this.expiries.peek();
|
|
388
|
-
if (!r || r.expiresAt > e) break;
|
|
389
|
-
this.expiries.pop();
|
|
390
|
-
const n = this.entries.get(r.key);
|
|
391
|
-
n !== void 0 && n === r.expiresAt && n <= e && this.entries.delete(r.key);
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
evictOne() {
|
|
395
|
-
for (; this.entries.size >= this.maxEntries; ) {
|
|
396
|
-
const e = this.expiries.pop();
|
|
397
|
-
if (!e) break;
|
|
398
|
-
const r = this.entries.get(e.key);
|
|
399
|
-
if (r !== void 0 && r === e.expiresAt) {
|
|
400
|
-
this.entries.delete(e.key);
|
|
401
|
-
return;
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
if (this.entries.size >= this.maxEntries) {
|
|
405
|
-
const e = this.entries.keys().next().value;
|
|
406
|
-
e && this.entries.delete(e);
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
consume(e, r = Date.now()) {
|
|
410
|
-
this.prune(r);
|
|
411
|
-
const n = this.makeKey(e), s = this.entries.get(n);
|
|
412
|
-
if (s && s > r)
|
|
413
|
-
return { ok: !1, reason: "Replay detected for sender/messageId/nonce" };
|
|
414
|
-
this.entries.size >= this.maxEntries && this.evictOne();
|
|
415
|
-
const i = e.header.timestampMs + e.header.ttlMs;
|
|
416
|
-
return this.entries.set(n, i), this.expiries.push({ key: n, expiresAt: i }), { ok: !0 };
|
|
417
|
-
}
|
|
418
|
-
consumeMany(e, r = Date.now()) {
|
|
419
|
-
return e.map((n) => this.consume(n, r));
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
class ft {
|
|
423
|
-
store;
|
|
424
|
-
constructor(e) {
|
|
425
|
-
this.store = e;
|
|
426
|
-
}
|
|
427
|
-
makeKey(e) {
|
|
428
|
-
return `${e.header.sender}|${e.header.messageId}|${e.header.nonce}`;
|
|
429
|
-
}
|
|
430
|
-
async consume(e, r = Date.now()) {
|
|
431
|
-
const n = this.makeKey(e), s = e.header.timestampMs + e.header.ttlMs;
|
|
432
|
-
return await this.store.reserve(n, s, r) ? { ok: !0 } : { ok: !1, reason: "Replay detected for sender/messageId/nonce" };
|
|
433
|
-
}
|
|
434
|
-
async consumeMany(e, r = Date.now()) {
|
|
435
|
-
if (this.store.reserveMany) {
|
|
436
|
-
const n = e.map((i) => ({
|
|
437
|
-
key: this.makeKey(i),
|
|
438
|
-
expiresAtMs: i.header.timestampMs + i.header.ttlMs
|
|
439
|
-
}));
|
|
440
|
-
return (await this.store.reserveMany(n, r)).map(
|
|
441
|
-
(i) => i ? { ok: !0 } : { ok: !1, reason: "Replay detected for sender/messageId/nonce" }
|
|
442
|
-
);
|
|
443
|
-
}
|
|
444
|
-
return Promise.all(e.map((n) => this.consume(n, r)));
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
class Oe {
|
|
448
|
-
entries = /* @__PURE__ */ new Map();
|
|
449
|
-
maxEntries;
|
|
450
|
-
constructor(e = 512) {
|
|
451
|
-
this.maxEntries = e;
|
|
452
|
-
}
|
|
453
|
-
get(e, r = Date.now()) {
|
|
454
|
-
const n = this.entries.get(e);
|
|
455
|
-
if (n) {
|
|
456
|
-
if (n.expiresAtMs <= r) {
|
|
457
|
-
this.entries.delete(e);
|
|
458
|
-
return;
|
|
459
|
-
}
|
|
460
|
-
return n.material;
|
|
461
|
-
}
|
|
462
|
-
}
|
|
463
|
-
set(e, r, n) {
|
|
464
|
-
if (this.entries.size >= this.maxEntries) {
|
|
465
|
-
const s = this.entries.keys().next().value;
|
|
466
|
-
s && this.entries.delete(s);
|
|
467
|
-
}
|
|
468
|
-
this.entries.set(e, { expiresAtMs: n, material: r });
|
|
469
|
-
}
|
|
470
|
-
delete(e) {
|
|
471
|
-
this.entries.delete(e);
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
function Fe(t, e) {
|
|
475
|
-
return `${e}|${t.alg}|${t.keyId}`;
|
|
476
|
-
}
|
|
477
|
-
function ie(t) {
|
|
478
|
-
if (!t || typeof t != "object") return !1;
|
|
479
|
-
const e = t;
|
|
480
|
-
return !!(e.header && e.body);
|
|
481
|
-
}
|
|
482
|
-
function se(t) {
|
|
483
|
-
if (!t || typeof t != "object") return !1;
|
|
484
|
-
const e = t;
|
|
485
|
-
return e.v === "aip/0.1" && typeof e.mid == "string" && typeof e.i == "string";
|
|
486
|
-
}
|
|
487
|
-
function ae(t) {
|
|
488
|
-
return {
|
|
489
|
-
header: {
|
|
490
|
-
version: t.v,
|
|
491
|
-
messageId: t.mid,
|
|
492
|
-
timestampMs: t.ts,
|
|
493
|
-
ttlMs: t.ttl,
|
|
494
|
-
sender: t.s,
|
|
495
|
-
recipient: t.r,
|
|
496
|
-
nonce: t.n
|
|
497
|
-
},
|
|
498
|
-
body: {
|
|
499
|
-
intent: t.i,
|
|
500
|
-
content: t.c,
|
|
501
|
-
capability: t.cap,
|
|
502
|
-
correlationId: t.cid
|
|
503
|
-
},
|
|
504
|
-
signature: t.sig ? {
|
|
505
|
-
alg: t.sig.a ?? "HS256",
|
|
506
|
-
keyId: t.sig.k,
|
|
507
|
-
value: t.sig.v
|
|
508
|
-
} : void 0
|
|
509
|
-
};
|
|
510
|
-
}
|
|
511
|
-
function oe(t) {
|
|
512
|
-
return {
|
|
513
|
-
v: t.header.version,
|
|
514
|
-
mid: t.header.messageId,
|
|
515
|
-
ts: t.header.timestampMs,
|
|
516
|
-
ttl: t.header.ttlMs,
|
|
517
|
-
s: t.header.sender,
|
|
518
|
-
r: t.header.recipient,
|
|
519
|
-
n: t.header.nonce,
|
|
520
|
-
i: t.body.intent,
|
|
521
|
-
c: t.body.content,
|
|
522
|
-
cap: t.body.capability,
|
|
523
|
-
cid: t.body.correlationId,
|
|
524
|
-
sig: t.signature ? {
|
|
525
|
-
a: t.signature.alg,
|
|
526
|
-
k: t.signature.keyId,
|
|
527
|
-
v: t.signature.value
|
|
528
|
-
} : void 0
|
|
529
|
-
};
|
|
530
|
-
}
|
|
531
|
-
function Ke(t) {
|
|
532
|
-
if (t instanceof Uint8Array)
|
|
533
|
-
return _e(t);
|
|
534
|
-
try {
|
|
535
|
-
const e = JSON.parse(t);
|
|
536
|
-
return se(e) ? { ok: !0, diagnostics: [], envelope: ae(e) } : ie(e) ? { ok: !0, diagnostics: [], envelope: e } : {
|
|
537
|
-
ok: !1,
|
|
538
|
-
diagnostics: [{ level: "error", message: "Envelope JSON shape is not recognized" }],
|
|
539
|
-
envelope: null
|
|
540
|
-
};
|
|
541
|
-
} catch {
|
|
542
|
-
return {
|
|
543
|
-
ok: !1,
|
|
544
|
-
diagnostics: [{ level: "error", message: "Invalid JSON envelope" }],
|
|
545
|
-
envelope: null
|
|
546
|
-
};
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
function U(t, e = "json") {
|
|
550
|
-
return e === "binary" ? Te(t) : JSON.stringify(e === "compact" ? oe(t) : t);
|
|
551
|
-
}
|
|
552
|
-
function yt(t, e = "compact") {
|
|
553
|
-
return JSON.stringify(e === "compact" ? t.map(oe) : t);
|
|
554
|
-
}
|
|
555
|
-
function Ue(t) {
|
|
556
|
-
try {
|
|
557
|
-
const e = JSON.parse(t);
|
|
558
|
-
return Array.isArray(e) ? e.map((r) => se(r) ? { ok: !0, diagnostics: [], envelope: ae(r) } : ie(r) ? { ok: !0, diagnostics: [], envelope: r } : {
|
|
559
|
-
ok: !1,
|
|
560
|
-
diagnostics: [{ level: "error", message: "Batch item has unrecognized envelope shape" }],
|
|
561
|
-
envelope: null
|
|
562
|
-
}) : [
|
|
563
|
-
{
|
|
564
|
-
ok: !1,
|
|
565
|
-
diagnostics: [{ level: "error", message: "Batch payload must be a JSON array" }],
|
|
566
|
-
envelope: null
|
|
567
|
-
}
|
|
568
|
-
];
|
|
569
|
-
} catch {
|
|
570
|
-
return [
|
|
571
|
-
{
|
|
572
|
-
ok: !1,
|
|
573
|
-
diagnostics: [{ level: "error", message: "Invalid JSON batch payload" }],
|
|
574
|
-
envelope: null
|
|
575
|
-
}
|
|
576
|
-
];
|
|
577
|
-
}
|
|
578
|
-
}
|
|
579
|
-
async function E(t, e = {}) {
|
|
580
|
-
const r = e.nowMs ?? Date.now(), n = e.requireSignature ?? !0, s = e.maxClockSkewMs ?? 3e4;
|
|
581
|
-
async function i(u) {
|
|
582
|
-
e.telemetry && await e.telemetry({
|
|
583
|
-
nowMs: r,
|
|
584
|
-
...u
|
|
585
|
-
});
|
|
586
|
-
}
|
|
587
|
-
const a = typeof t == "string" || t instanceof Uint8Array ? Ke(t) : { ok: !0, diagnostics: [], envelope: t };
|
|
588
|
-
if (!a.ok || !a.envelope) return a;
|
|
589
|
-
if (await i({
|
|
590
|
-
phase: "decoded",
|
|
591
|
-
sender: a.envelope.header.sender,
|
|
592
|
-
messageId: a.envelope.header.messageId
|
|
593
|
-
}), a.envelope.header.timestampMs > r + s) {
|
|
594
|
-
const u = "Message timestamp exceeds allowed clock skew", d = [{ level: "error", message: u }];
|
|
595
|
-
return await i({
|
|
596
|
-
phase: "rejected_clock_skew",
|
|
597
|
-
sender: a.envelope.header.sender,
|
|
598
|
-
messageId: a.envelope.header.messageId,
|
|
599
|
-
reason: u
|
|
600
|
-
}), { ok: !1, diagnostics: d, envelope: a.envelope };
|
|
601
|
-
}
|
|
602
|
-
const o = [...Ie(a.envelope, r)];
|
|
603
|
-
if (o.some((u) => u.level === "error"))
|
|
604
|
-
return await i({
|
|
605
|
-
phase: "rejected_validation",
|
|
606
|
-
sender: a.envelope.header.sender,
|
|
607
|
-
messageId: a.envelope.header.messageId,
|
|
608
|
-
reason: o.find((u) => u.level === "error")?.message
|
|
609
|
-
}), { ok: !1, diagnostics: o, envelope: a.envelope };
|
|
610
|
-
if (!a.envelope.signature)
|
|
611
|
-
return n ? (o.push({ level: "error", message: "Missing signature" }), await i({
|
|
612
|
-
phase: "rejected_missing_signature",
|
|
613
|
-
sender: a.envelope.header.sender,
|
|
614
|
-
messageId: a.envelope.header.messageId,
|
|
615
|
-
reason: "Missing signature"
|
|
616
|
-
}), { ok: !1, diagnostics: o, envelope: a.envelope }) : (await i({
|
|
617
|
-
phase: "accepted",
|
|
618
|
-
sender: a.envelope.header.sender,
|
|
619
|
-
messageId: a.envelope.header.messageId
|
|
620
|
-
}), { ok: !0, diagnostics: o, envelope: a.envelope });
|
|
621
|
-
let l;
|
|
622
|
-
const c = a.envelope.signature, h = Fe(c, a.envelope.header.sender);
|
|
623
|
-
if (e.verificationMaterialCache && (l = e.verificationMaterialCache.get(h, r)), e.signatureResolver && (l = l ?? await e.signatureResolver(c, a.envelope.header.sender)), !l && c.alg === "HS256" && e.secretResolver) {
|
|
624
|
-
const u = await e.secretResolver(c.keyId, a.envelope.header.sender);
|
|
625
|
-
u && (l = { alg: "HS256", secret: u });
|
|
626
|
-
}
|
|
627
|
-
if (l && e.verificationMaterialCache) {
|
|
628
|
-
const u = Math.max(r + 1, a.envelope.header.timestampMs + a.envelope.header.ttlMs);
|
|
629
|
-
e.verificationMaterialCache.set(h, l, u);
|
|
630
|
-
}
|
|
631
|
-
if (!l)
|
|
632
|
-
return o.push({ level: "error", message: "No signature verification material found for keyId/sender" }), await i({
|
|
633
|
-
phase: "rejected_missing_material",
|
|
634
|
-
sender: a.envelope.header.sender,
|
|
635
|
-
messageId: a.envelope.header.messageId,
|
|
636
|
-
reason: "No signature verification material found for keyId/sender"
|
|
637
|
-
}), { ok: !1, diagnostics: o, envelope: a.envelope };
|
|
638
|
-
const f = A({ header: a.envelope.header, body: a.envelope.body });
|
|
639
|
-
if (!await ve(f, a.envelope.signature, l))
|
|
640
|
-
return o.push({ level: "error", message: "Signature verification failed" }), await i({
|
|
641
|
-
phase: "rejected_bad_signature",
|
|
642
|
-
sender: a.envelope.header.sender,
|
|
643
|
-
messageId: a.envelope.header.messageId,
|
|
644
|
-
reason: "Signature verification failed"
|
|
645
|
-
}), { ok: !1, diagnostics: o, envelope: a.envelope };
|
|
646
|
-
if (e.replayCache) {
|
|
647
|
-
const u = await e.replayCache.consume(a.envelope, r);
|
|
648
|
-
if (!u.ok)
|
|
649
|
-
return o.push({ level: "error", message: u.reason ?? "Replay detected" }), await i({
|
|
650
|
-
phase: "rejected_replay",
|
|
651
|
-
sender: a.envelope.header.sender,
|
|
652
|
-
messageId: a.envelope.header.messageId,
|
|
653
|
-
reason: u.reason
|
|
654
|
-
}), { ok: !1, diagnostics: o, envelope: a.envelope };
|
|
655
|
-
}
|
|
656
|
-
return await i({
|
|
657
|
-
phase: "accepted",
|
|
658
|
-
sender: a.envelope.header.sender,
|
|
659
|
-
messageId: a.envelope.header.messageId
|
|
660
|
-
}), { ok: !0, diagnostics: o, envelope: a.envelope };
|
|
661
|
-
}
|
|
662
|
-
async function De(t, e = {}) {
|
|
663
|
-
const r = Math.max(1, Math.floor(e.batchConcurrency ?? Number.POSITIVE_INFINITY)), n = async (s) => {
|
|
664
|
-
const i = new Array(s.length);
|
|
665
|
-
let a = 0;
|
|
666
|
-
const o = Number.isFinite(r) ? Math.min(r, s.length) : s.length;
|
|
667
|
-
return await Promise.all(
|
|
668
|
-
Array.from({ length: o }, async () => {
|
|
669
|
-
for (; a < s.length; ) {
|
|
670
|
-
const l = a;
|
|
671
|
-
a += 1;
|
|
672
|
-
const c = s[l];
|
|
673
|
-
c && typeof c == "object" && "ok" in c && "diagnostics" in c && "envelope" in c ? i[l] = c : i[l] = await E(c, e);
|
|
674
|
-
}
|
|
675
|
-
})
|
|
676
|
-
), await e.telemetry?.({
|
|
677
|
-
phase: "batch_summary",
|
|
678
|
-
nowMs: e.nowMs ?? Date.now(),
|
|
679
|
-
reason: `items=${s.length};accepted=${i.filter((l) => l?.ok).length}`
|
|
680
|
-
}), i;
|
|
681
|
-
};
|
|
682
|
-
if (typeof t == "string") {
|
|
683
|
-
const s = Ue(t);
|
|
684
|
-
return n(s.map((i) => i.ok && i.envelope ? i.envelope : i));
|
|
685
|
-
}
|
|
686
|
-
return n(t);
|
|
687
|
-
}
|
|
688
|
-
class pt {
|
|
689
|
-
options;
|
|
690
|
-
constructor(e = {}) {
|
|
691
|
-
this.options = {
|
|
692
|
-
...e,
|
|
693
|
-
replayCache: e.replayCache ?? new T(),
|
|
694
|
-
verificationMaterialCache: e.verificationMaterialCache ?? new Oe()
|
|
695
|
-
};
|
|
696
|
-
}
|
|
697
|
-
receive(e, r = Date.now()) {
|
|
698
|
-
return E(e, { ...this.options, nowMs: r });
|
|
699
|
-
}
|
|
700
|
-
receiveBatch(e, r = Date.now()) {
|
|
701
|
-
return De(e, { ...this.options, nowMs: r });
|
|
702
|
-
}
|
|
703
|
-
}
|
|
704
|
-
async function mt(t) {
|
|
705
|
-
const e = P(t);
|
|
706
|
-
return Q(e, t.secret, t.keyId);
|
|
707
|
-
}
|
|
708
|
-
const Be = ["binary", "compact", "json"], Le = ["fast", "receipt"];
|
|
709
|
-
function ze(t) {
|
|
710
|
-
return {
|
|
711
|
-
agent: t.agent,
|
|
712
|
-
capabilities: t.capabilities ?? [],
|
|
713
|
-
wireFormats: t.wireFormats ?? ["compact", "json"],
|
|
714
|
-
batchMax: t.batchMax ?? 1,
|
|
715
|
-
ackModes: t.ackModes ?? ["receipt"]
|
|
716
|
-
};
|
|
717
|
-
}
|
|
718
|
-
function Je(t) {
|
|
719
|
-
return JSON.stringify(t);
|
|
720
|
-
}
|
|
721
|
-
function gt(t) {
|
|
722
|
-
try {
|
|
723
|
-
const e = JSON.parse(t);
|
|
724
|
-
return typeof e.agent != "string" || !Array.isArray(e.capabilities) || !Array.isArray(e.wireFormats) || !Array.isArray(e.ackModes) || typeof e.batchMax != "number" || e.batchMax < 1 ? null : {
|
|
725
|
-
agent: e.agent,
|
|
726
|
-
capabilities: e.capabilities.filter((r) => typeof r == "string"),
|
|
727
|
-
wireFormats: e.wireFormats.filter((r) => r === "json" || r === "compact" || r === "binary"),
|
|
728
|
-
batchMax: Math.floor(e.batchMax),
|
|
729
|
-
ackModes: e.ackModes.filter((r) => r === "fast" || r === "receipt")
|
|
730
|
-
};
|
|
731
|
-
} catch {
|
|
732
|
-
return null;
|
|
733
|
-
}
|
|
734
|
-
}
|
|
735
|
-
function wt(t, e) {
|
|
736
|
-
const r = Be.find((s) => t.wireFormats.includes(s) && e.wireFormats.includes(s)) ?? "compact", n = Le.find((s) => t.ackModes.includes(s) && e.ackModes.includes(s)) ?? "receipt";
|
|
737
|
-
return {
|
|
738
|
-
wireFormat: r,
|
|
739
|
-
batchMax: Math.max(1, Math.min(t.batchMax, e.batchMax)),
|
|
740
|
-
ackMode: n
|
|
741
|
-
};
|
|
742
|
-
}
|
|
743
|
-
class bt {
|
|
744
|
-
records;
|
|
745
|
-
constructor(e = []) {
|
|
746
|
-
this.records = [...e];
|
|
747
|
-
}
|
|
748
|
-
add(e) {
|
|
749
|
-
this.records.push(e);
|
|
750
|
-
}
|
|
751
|
-
revoke(e, r, n = Date.now()) {
|
|
752
|
-
const s = this.records.find((i) => i.sender === e && i.keyId === r);
|
|
753
|
-
return s ? (s.status = "revoked", s.notAfterMs = Math.min(s.notAfterMs, n), !0) : !1;
|
|
754
|
-
}
|
|
755
|
-
selectSigningKey(e, r, n = Date.now()) {
|
|
756
|
-
return this.records.filter(
|
|
757
|
-
(i) => i.sender === e && i.alg === r && i.status === "active" && i.notBeforeMs <= n && n < i.notAfterMs
|
|
758
|
-
).sort((i, a) => a.notBeforeMs - i.notBeforeMs)[0] ?? null;
|
|
759
|
-
}
|
|
760
|
-
resolveVerificationMaterial(e, r, n = Date.now()) {
|
|
761
|
-
return this.records.filter(
|
|
762
|
-
(i) => i.sender === r && i.keyId === e.keyId && i.alg === e.alg && i.status !== "revoked" && i.notBeforeMs <= n && n < i.notAfterMs
|
|
763
|
-
).sort((i, a) => a.notBeforeMs - i.notBeforeMs)[0]?.material;
|
|
764
|
-
}
|
|
765
|
-
}
|
|
766
|
-
function vt(t, e = () => Date.now()) {
|
|
767
|
-
return async (r, n) => t.resolveVerificationMaterial(r, n, e());
|
|
768
|
-
}
|
|
769
|
-
class je {
|
|
770
|
-
agentId;
|
|
771
|
-
outboundSecret;
|
|
772
|
-
keyId;
|
|
773
|
-
capabilities;
|
|
774
|
-
supportedWireFormats;
|
|
775
|
-
maxBatchSize;
|
|
776
|
-
ackModes;
|
|
777
|
-
requireSignature;
|
|
778
|
-
replayCache;
|
|
779
|
-
sharedSecrets;
|
|
780
|
-
resolveInboundSecret;
|
|
781
|
-
onTask;
|
|
782
|
-
constructor(e) {
|
|
783
|
-
this.agentId = e.agentId, this.outboundSecret = e.outboundSecret, this.keyId = e.keyId ?? `${e.agentId}-k1`, this.capabilities = e.capabilities ?? [], this.supportedWireFormats = e.supportedWireFormats ?? ["compact", "json"], this.maxBatchSize = e.maxBatchSize ?? 1, this.ackModes = e.ackModes ?? ["receipt"], this.requireSignature = e.requireSignature ?? !0, this.replayCache = e.replayCache ?? new T(), this.sharedSecrets = e.sharedSecrets ?? {}, this.resolveInboundSecret = e.resolveInboundSecret, this.onTask = e.onTask;
|
|
784
|
-
}
|
|
785
|
-
async inboundSecretResolver(e, r) {
|
|
786
|
-
return this.resolveInboundSecret ? this.resolveInboundSecret(e, r) : this.sharedSecrets[r];
|
|
787
|
-
}
|
|
788
|
-
async createSignedIntent(e) {
|
|
789
|
-
const r = P({
|
|
790
|
-
sender: this.agentId,
|
|
791
|
-
recipient: e.recipient,
|
|
792
|
-
intent: e.intent,
|
|
793
|
-
content: e.content,
|
|
794
|
-
capability: e.capability,
|
|
795
|
-
correlationId: e.correlationId,
|
|
796
|
-
ttlMs: e.ttlMs,
|
|
797
|
-
messageId: e.messageId,
|
|
798
|
-
nonce: e.nonce,
|
|
799
|
-
nowMs: e.nowMs
|
|
800
|
-
});
|
|
801
|
-
return Q(r, this.outboundSecret, this.keyId);
|
|
802
|
-
}
|
|
803
|
-
async receiveAndRespond(e, r = Date.now()) {
|
|
804
|
-
const n = await E(e, {
|
|
805
|
-
nowMs: r,
|
|
806
|
-
requireSignature: this.requireSignature,
|
|
807
|
-
replayCache: this.replayCache,
|
|
808
|
-
secretResolver: (a, o) => this.inboundSecretResolver(a, o)
|
|
809
|
-
});
|
|
810
|
-
if (!n.ok || !n.envelope)
|
|
811
|
-
return {
|
|
812
|
-
ok: !1,
|
|
813
|
-
diagnostics: n.diagnostics,
|
|
814
|
-
received: n.envelope,
|
|
815
|
-
response: null
|
|
816
|
-
};
|
|
817
|
-
const s = n.envelope, i = await this.buildAutoResponse(s, r);
|
|
818
|
-
return {
|
|
819
|
-
ok: !0,
|
|
820
|
-
diagnostics: n.diagnostics,
|
|
821
|
-
received: s,
|
|
822
|
-
response: i
|
|
823
|
-
};
|
|
824
|
-
}
|
|
825
|
-
async buildAutoResponse(e, r) {
|
|
826
|
-
if (e.body.intent === "PING")
|
|
827
|
-
return this.createSignedIntent({
|
|
828
|
-
recipient: e.header.sender,
|
|
829
|
-
intent: "PONG",
|
|
830
|
-
content: "pong",
|
|
831
|
-
correlationId: e.header.messageId,
|
|
832
|
-
nowMs: r
|
|
833
|
-
});
|
|
834
|
-
if (e.body.intent === "CAPS")
|
|
835
|
-
return this.createSignedIntent({
|
|
836
|
-
recipient: e.header.sender,
|
|
837
|
-
intent: "RESULT",
|
|
838
|
-
content: Je(
|
|
839
|
-
ze({
|
|
840
|
-
agent: this.agentId,
|
|
841
|
-
capabilities: this.capabilities,
|
|
842
|
-
wireFormats: this.supportedWireFormats,
|
|
843
|
-
batchMax: this.maxBatchSize,
|
|
844
|
-
ackModes: this.ackModes
|
|
845
|
-
})
|
|
846
|
-
),
|
|
847
|
-
capability: "caps",
|
|
848
|
-
correlationId: e.header.messageId,
|
|
849
|
-
nowMs: r
|
|
850
|
-
});
|
|
851
|
-
if (e.body.intent === "TASK") {
|
|
852
|
-
const n = this.onTask ? await this.onTask(e) : { intent: "RESULT", content: "ACK", capability: e.body.capability };
|
|
853
|
-
return this.createSignedIntent({
|
|
854
|
-
recipient: e.header.sender,
|
|
855
|
-
intent: n.intent ?? "RESULT",
|
|
856
|
-
content: n.content,
|
|
857
|
-
capability: n.capability,
|
|
858
|
-
correlationId: e.header.messageId,
|
|
859
|
-
nowMs: r
|
|
860
|
-
});
|
|
861
|
-
}
|
|
862
|
-
return null;
|
|
863
|
-
}
|
|
864
|
-
}
|
|
865
|
-
class He {
|
|
866
|
-
session;
|
|
867
|
-
wireFormat;
|
|
868
|
-
constructor(e) {
|
|
869
|
-
this.session = new je(e), this.wireFormat = e.wireFormat ?? "compact";
|
|
870
|
-
}
|
|
871
|
-
toRaw(e) {
|
|
872
|
-
return U(e, this.wireFormat);
|
|
873
|
-
}
|
|
874
|
-
async createRawIntent(e) {
|
|
875
|
-
const r = await this.session.createSignedIntent(e);
|
|
876
|
-
return this.toRaw(r);
|
|
877
|
-
}
|
|
878
|
-
async receiveRaw(e, r = Date.now()) {
|
|
879
|
-
return this.session.receiveAndRespond(e, r);
|
|
880
|
-
}
|
|
881
|
-
async handleRaw(e, r, n = Date.now()) {
|
|
882
|
-
const s = await this.session.receiveAndRespond(e, n);
|
|
883
|
-
return s.response && await r(this.toRaw(s.response), s.response), s;
|
|
884
|
-
}
|
|
885
|
-
}
|
|
886
|
-
function B(t) {
|
|
887
|
-
return new He(t);
|
|
888
|
-
}
|
|
889
|
-
async function It(t, e, r = {}) {
|
|
890
|
-
return t.createRawIntent({
|
|
891
|
-
recipient: r.recipient,
|
|
892
|
-
intent: "TASK",
|
|
893
|
-
content: e.content,
|
|
894
|
-
capability: r.capability,
|
|
895
|
-
correlationId: r.correlationId
|
|
896
|
-
});
|
|
897
|
-
}
|
|
898
|
-
function Mt(t) {
|
|
899
|
-
return {
|
|
900
|
-
content: t.body.content,
|
|
901
|
-
name: t.body.intent,
|
|
902
|
-
additionalKwargs: {
|
|
903
|
-
capability: t.body.capability,
|
|
904
|
-
correlationId: t.body.correlationId,
|
|
905
|
-
sender: t.header.sender,
|
|
906
|
-
recipient: t.header.recipient,
|
|
907
|
-
messageId: t.header.messageId,
|
|
908
|
-
timestampMs: t.header.timestampMs
|
|
909
|
-
}
|
|
910
|
-
};
|
|
911
|
-
}
|
|
912
|
-
async function kt(t, e, r = {}) {
|
|
913
|
-
return t.createRawIntent({
|
|
914
|
-
recipient: r.recipient,
|
|
915
|
-
intent: "TASK",
|
|
916
|
-
content: e.content,
|
|
917
|
-
capability: r.capability,
|
|
918
|
-
correlationId: r.correlationId
|
|
919
|
-
});
|
|
920
|
-
}
|
|
921
|
-
function St(t) {
|
|
922
|
-
return {
|
|
923
|
-
role: t.body.intent === "TASK" ? "user" : "assistant",
|
|
924
|
-
content: t.body.content,
|
|
925
|
-
additionalKwargs: {
|
|
926
|
-
intent: t.body.intent,
|
|
927
|
-
capability: t.body.capability,
|
|
928
|
-
correlationId: t.body.correlationId,
|
|
929
|
-
sender: t.header.sender,
|
|
930
|
-
recipient: t.header.recipient
|
|
931
|
-
}
|
|
932
|
-
};
|
|
933
|
-
}
|
|
934
|
-
function L(t) {
|
|
935
|
-
return `mcp.${t}`;
|
|
936
|
-
}
|
|
937
|
-
async function Ve(t, e, r = {}) {
|
|
938
|
-
async function n(a) {
|
|
939
|
-
r.onPolicyEvent && await r.onPolicyEvent(a);
|
|
940
|
-
}
|
|
941
|
-
if (r.allowedMethods && !r.allowedMethods.includes(e.method)) {
|
|
942
|
-
const a = r.methodToCapability ? r.methodToCapability(e.method) : L(e.method), o = {
|
|
943
|
-
intent: "TASK",
|
|
944
|
-
recipient: r.recipient,
|
|
945
|
-
capability: a,
|
|
946
|
-
correlationId: String(e.id),
|
|
947
|
-
createdAtMs: Date.now()
|
|
948
|
-
};
|
|
949
|
-
throw await n({
|
|
950
|
-
stage: "allowlist",
|
|
951
|
-
decision: "deny",
|
|
952
|
-
method: e.method,
|
|
953
|
-
context: o,
|
|
954
|
-
reason: "method_not_allowed"
|
|
955
|
-
}), new Error(`JSON-RPC method '${e.method}' is not allowed`);
|
|
956
|
-
}
|
|
957
|
-
const s = r.methodToCapability ? r.methodToCapability(e.method) : L(e.method), i = {
|
|
958
|
-
intent: "TASK",
|
|
959
|
-
recipient: r.recipient,
|
|
960
|
-
capability: s,
|
|
961
|
-
correlationId: String(e.id),
|
|
962
|
-
createdAtMs: Date.now()
|
|
963
|
-
};
|
|
964
|
-
if (await n({
|
|
965
|
-
stage: "allowlist",
|
|
966
|
-
decision: "allow",
|
|
967
|
-
method: e.method,
|
|
968
|
-
context: i
|
|
969
|
-
}), r.authorizeMethod) {
|
|
970
|
-
if (!await r.authorizeMethod(e, i))
|
|
971
|
-
throw await n({
|
|
972
|
-
stage: "authorize",
|
|
973
|
-
decision: "deny",
|
|
974
|
-
method: e.method,
|
|
975
|
-
context: i,
|
|
976
|
-
reason: "not_authorized"
|
|
977
|
-
}), new Error(`JSON-RPC method '${e.method}' is not authorized`);
|
|
978
|
-
await n({
|
|
979
|
-
stage: "authorize",
|
|
980
|
-
decision: "allow",
|
|
981
|
-
method: e.method,
|
|
982
|
-
context: i
|
|
983
|
-
});
|
|
984
|
-
}
|
|
985
|
-
if (r.rateLimiter) {
|
|
986
|
-
const a = r.rateLimitKey ? r.rateLimitKey(e, i) : e.method;
|
|
987
|
-
if (!await r.rateLimiter.consume(a, e, i))
|
|
988
|
-
throw await n({
|
|
989
|
-
stage: "rateLimit",
|
|
990
|
-
decision: "deny",
|
|
991
|
-
method: e.method,
|
|
992
|
-
context: i,
|
|
993
|
-
reason: "rate_limited"
|
|
994
|
-
}), new Error(`JSON-RPC method '${e.method}' is rate-limited`);
|
|
995
|
-
await n({
|
|
996
|
-
stage: "rateLimit",
|
|
997
|
-
decision: "allow",
|
|
998
|
-
method: e.method,
|
|
999
|
-
context: i
|
|
1000
|
-
});
|
|
1001
|
-
}
|
|
1002
|
-
return t.createRawIntent({
|
|
1003
|
-
recipient: r.recipient,
|
|
1004
|
-
intent: "TASK",
|
|
1005
|
-
capability: s,
|
|
1006
|
-
correlationId: String(e.id),
|
|
1007
|
-
content: JSON.stringify({ method: e.method, params: e.params ?? null })
|
|
1008
|
-
});
|
|
1009
|
-
}
|
|
1010
|
-
function We(t, e, r = {}) {
|
|
1011
|
-
if (t.body.intent === "ERROR")
|
|
1012
|
-
return {
|
|
1013
|
-
jsonrpc: "2.0",
|
|
1014
|
-
id: e,
|
|
1015
|
-
error: {
|
|
1016
|
-
code: r.defaultErrorCode ?? -32e3,
|
|
1017
|
-
message: t.body.content,
|
|
1018
|
-
data: {
|
|
1019
|
-
capability: t.body.capability,
|
|
1020
|
-
correlationId: t.body.correlationId
|
|
1021
|
-
}
|
|
1022
|
-
}
|
|
1023
|
-
};
|
|
1024
|
-
let n = t.body.content;
|
|
1025
|
-
try {
|
|
1026
|
-
n = JSON.parse(t.body.content);
|
|
1027
|
-
} catch {
|
|
1028
|
-
n = t.body.content;
|
|
1029
|
-
}
|
|
1030
|
-
return {
|
|
1031
|
-
jsonrpc: "2.0",
|
|
1032
|
-
id: e,
|
|
1033
|
-
result: n
|
|
1034
|
-
};
|
|
1035
|
-
}
|
|
1036
|
-
function Ge(t) {
|
|
1037
|
-
return typeof t == "object" && t !== null && !Array.isArray(t);
|
|
1038
|
-
}
|
|
1039
|
-
function $(t, e) {
|
|
1040
|
-
if (typeof t != "string" || t.trim().length === 0)
|
|
1041
|
-
throw new Error(`Invalid runtime policy: '${e}' must be a non-empty string`);
|
|
1042
|
-
return t;
|
|
1043
|
-
}
|
|
1044
|
-
function Ye(t) {
|
|
1045
|
-
if (!Ge(t))
|
|
1046
|
-
throw new Error("Invalid runtime policy: root must be an object");
|
|
1047
|
-
const e = $(t.version, "version"), r = $(t.name, "name"), n = $(t.status, "status");
|
|
1048
|
-
return {
|
|
1049
|
-
...t,
|
|
1050
|
-
version: e,
|
|
1051
|
-
name: r,
|
|
1052
|
-
status: n
|
|
1053
|
-
};
|
|
1054
|
-
}
|
|
1055
|
-
function Xe(t) {
|
|
1056
|
-
let e;
|
|
1057
|
-
try {
|
|
1058
|
-
e = JSON.parse(t);
|
|
1059
|
-
} catch (r) {
|
|
1060
|
-
const n = r instanceof Error ? r.message : String(r);
|
|
1061
|
-
throw new Error(`Invalid runtime policy JSON: ${n}`);
|
|
1062
|
-
}
|
|
1063
|
-
return Ye(e);
|
|
1064
|
-
}
|
|
1065
|
-
async function Qe(t) {
|
|
1066
|
-
if (typeof fetch == "function") {
|
|
1067
|
-
const e = await fetch(t);
|
|
1068
|
-
if (!e.ok)
|
|
1069
|
-
throw new Error(`Failed to fetch runtime policy at '${t}': ${e.status} ${e.statusText}`);
|
|
1070
|
-
return e.text();
|
|
1071
|
-
}
|
|
1072
|
-
throw new Error(
|
|
1073
|
-
"No default runtime file reader available in this environment. Provide 'readTextFile' in loadRuntimePolicy(options)."
|
|
1074
|
-
);
|
|
1075
|
-
}
|
|
1076
|
-
async function Ze(t = {}) {
|
|
1077
|
-
const e = t.path ?? "AI_RUNTIME_POLICY.json", n = await (t.readTextFile ?? Qe)(e);
|
|
1078
|
-
return Xe(n);
|
|
1079
|
-
}
|
|
1080
|
-
function O(t, e) {
|
|
1081
|
-
if (!Array.isArray(t) || t.length !== 2)
|
|
1082
|
-
throw new Error(`Invalid policy tuning range '${e}': expected [min,max]`);
|
|
1083
|
-
const r = Number(t[0]), n = Number(t[1]);
|
|
1084
|
-
if (!Number.isFinite(r) || !Number.isFinite(n) || r <= 0 || n < r)
|
|
1085
|
-
throw new Error(`Invalid policy tuning range '${e}': expected positive ascending numbers`);
|
|
1086
|
-
return [Math.floor(r), Math.floor(n)];
|
|
1087
|
-
}
|
|
1088
|
-
function qe(t) {
|
|
1089
|
-
return t === "http" || t === "ws" || t === "http-binary" || t === "http-batch" || t === "http-binary-batch" || t === "ws-batch" || t === "ws-binary" || t === "ws-binary-batch";
|
|
1090
|
-
}
|
|
1091
|
-
function et(t) {
|
|
1092
|
-
const e = Number((t.slo_defaults ?? {}).drop_pct_max ?? 0.1), r = Number((t.slo_defaults ?? {}).p99_ms_max ?? 20), n = Number((t.tuning ?? {}).max_retry_attempts ?? 3), s = Array.isArray((t.tuning ?? {}).retriable_status_codes) ? (t.tuning ?? {}).retriable_status_codes.map((d) => Number(d)).filter((d) => Number.isFinite(d)) : [503], i = t.tuning ?? {}, a = i.batch_size, o = i.inflight_cap, l = i.retry_backoff_ms, c = (d) => ({
|
|
1093
|
-
batchSizeRange: O(a?.[`${d}_traffic`], `batch_size.${d}_traffic`),
|
|
1094
|
-
inflightCapRange: O(o?.[`${d}_traffic`], `inflight_cap.${d}_traffic`),
|
|
1095
|
-
retryBackoffMsRange: O(l?.[`${d}_traffic`], `retry_backoff_ms.${d}_traffic`)
|
|
1096
|
-
});
|
|
1097
|
-
function h(d) {
|
|
1098
|
-
return d.concurrency >= 100 ? "http-binary-batch" : d.latencySensitive ? "ws-batch" : d.compatibilityFirst ? "http" : "ws-batch";
|
|
1099
|
-
}
|
|
1100
|
-
function f(d) {
|
|
1101
|
-
const p = c(d.trafficClass), m = [];
|
|
1102
|
-
return typeof d.dropPct == "number" && d.dropPct > e && (m.push("reduce inflight cap"), m.push("reduce batch size"), m.push("increase retry backoff"), m.push("switch to binary-batch mode")), typeof d.p99Ms == "number" && d.p99Ms > r && (m.push("reduce batch size"), m.push("reduce inflight cap"), m.push("re-run adaptive benchmark")), {
|
|
1103
|
-
...p,
|
|
1104
|
-
actions: m
|
|
1105
|
-
};
|
|
1106
|
-
}
|
|
1107
|
-
function y(d) {
|
|
1108
|
-
if (d.attempt >= n) return !1;
|
|
1109
|
-
const p = d.error instanceof Error ? d.error.message : String(d.error), m = /http status\s+(\d+)/i.exec(p), v = m ? Number(m[1]) : NaN;
|
|
1110
|
-
return Number.isFinite(v) && s.includes(v);
|
|
1111
|
-
}
|
|
1112
|
-
function u(d) {
|
|
1113
|
-
const p = [];
|
|
1114
|
-
if (d.signatureVerification || p.push("signatureVerification=false"), d.canonicalization || p.push("canonicalization=false"), d.replayDefense || p.push("replayDefense=false"), d.ttlClockSkewEnforcement || p.push("ttlClockSkewEnforcement=false"), p.length > 0)
|
|
1115
|
-
throw new Error(`Invariant violation: ${p.join(", ")}`);
|
|
1116
|
-
}
|
|
1117
|
-
return {
|
|
1118
|
-
selectMode: h,
|
|
1119
|
-
tune: f,
|
|
1120
|
-
shouldRetry: y,
|
|
1121
|
-
assertInvariant: u,
|
|
1122
|
-
metadata: {
|
|
1123
|
-
policyName: t.name,
|
|
1124
|
-
policyVersion: t.version,
|
|
1125
|
-
policyStatus: t.status,
|
|
1126
|
-
enforcedSlo: {
|
|
1127
|
-
dropPctMax: e,
|
|
1128
|
-
p99MsMax: r
|
|
1129
|
-
}
|
|
1130
|
-
}
|
|
1131
|
-
};
|
|
1132
|
-
}
|
|
1133
|
-
async function F(t = {}) {
|
|
1134
|
-
const e = await Ze(t), r = et(e);
|
|
1135
|
-
return { policy: e, enforcer: r };
|
|
1136
|
-
}
|
|
1137
|
-
function At(t) {
|
|
1138
|
-
return qe(t);
|
|
1139
|
-
}
|
|
1140
|
-
const tt = ["tools/call", "resources/read", "prompts/get"];
|
|
1141
|
-
function K(t) {
|
|
1142
|
-
return JSON.stringify(t);
|
|
1143
|
-
}
|
|
1144
|
-
function x(t, e, r) {
|
|
1145
|
-
return K({
|
|
1146
|
-
jsonrpc: "2.0",
|
|
1147
|
-
id: t,
|
|
1148
|
-
error: { code: e, message: r }
|
|
1149
|
-
});
|
|
1150
|
-
}
|
|
1151
|
-
function rt(t) {
|
|
1152
|
-
const e = JSON.parse(t);
|
|
1153
|
-
return e.jsonrpc !== "2.0" || typeof e.method != "string" || e.method.length === 0 || typeof e.id != "string" && typeof e.id != "number" ? null : {
|
|
1154
|
-
jsonrpc: "2.0",
|
|
1155
|
-
id: e.id,
|
|
1156
|
-
method: e.method,
|
|
1157
|
-
params: e.params
|
|
1158
|
-
};
|
|
1159
|
-
}
|
|
1160
|
-
function nt(t) {
|
|
1161
|
-
return t.includes("not allowed") ? -32601 : t.includes("not authorized") ? -32001 : t.includes("rate-limited") ? -32002 : -32603;
|
|
1162
|
-
}
|
|
1163
|
-
function z(t) {
|
|
1164
|
-
const e = t.gatewayAgentId ?? "agent.gateway", r = t.workerAgentId ?? "agent.worker", n = t.allowedMethods ?? tt, s = t.workerCapabilityPrefix ?? "mcp.", i = t.gatewayWireFormat ?? "compact", a = t.workerWireFormat ?? "compact";
|
|
1165
|
-
async function o(h) {
|
|
1166
|
-
t.onAuditEvent && await t.onAuditEvent({
|
|
1167
|
-
...h,
|
|
1168
|
-
timestampMs: Date.now()
|
|
1169
|
-
});
|
|
1170
|
-
}
|
|
1171
|
-
const l = B({
|
|
1172
|
-
agentId: e,
|
|
1173
|
-
outboundSecret: t.sharedSecret,
|
|
1174
|
-
sharedSecrets: { [r]: t.sharedSecret },
|
|
1175
|
-
wireFormat: i
|
|
1176
|
-
}), c = B({
|
|
1177
|
-
agentId: r,
|
|
1178
|
-
outboundSecret: t.sharedSecret,
|
|
1179
|
-
sharedSecrets: { [e]: t.sharedSecret },
|
|
1180
|
-
wireFormat: a,
|
|
1181
|
-
onTask: async (h) => ({
|
|
1182
|
-
intent: "RESULT",
|
|
1183
|
-
content: K({
|
|
1184
|
-
ok: !0,
|
|
1185
|
-
capability: h.body.capability,
|
|
1186
|
-
content: h.body.content
|
|
1187
|
-
}),
|
|
1188
|
-
capability: h.body.capability?.startsWith(s) ? h.body.capability : `${s}${h.body.capability ?? "unknown"}`
|
|
1189
|
-
})
|
|
1190
|
-
});
|
|
1191
|
-
return {
|
|
1192
|
-
async handleLine(h) {
|
|
1193
|
-
const f = h.trim();
|
|
1194
|
-
if (!f) return null;
|
|
1195
|
-
let y = null;
|
|
1196
|
-
try {
|
|
1197
|
-
const u = rt(f);
|
|
1198
|
-
if (!u)
|
|
1199
|
-
return await o({
|
|
1200
|
-
phase: "request_error",
|
|
1201
|
-
code: -32600,
|
|
1202
|
-
message: "Invalid Request"
|
|
1203
|
-
}), x(null, -32600, "Invalid Request");
|
|
1204
|
-
y = u.id, await o({
|
|
1205
|
-
phase: "request_received",
|
|
1206
|
-
id: u.id,
|
|
1207
|
-
method: u.method
|
|
1208
|
-
});
|
|
1209
|
-
const d = {
|
|
1210
|
-
recipient: r,
|
|
1211
|
-
allowedMethods: n,
|
|
1212
|
-
methodToCapability: t.methodToCapability,
|
|
1213
|
-
authorizeMethod: t.authorizeMethod,
|
|
1214
|
-
rateLimitKey: t.rateLimitKey,
|
|
1215
|
-
rateLimiter: t.rateLimiter,
|
|
1216
|
-
onPolicyEvent: async (_) => {
|
|
1217
|
-
await o({
|
|
1218
|
-
phase: "policy",
|
|
1219
|
-
id: u.id,
|
|
1220
|
-
method: u.method,
|
|
1221
|
-
policy: _
|
|
1222
|
-
});
|
|
1223
|
-
}
|
|
1224
|
-
}, p = await Ve(l, u, d);
|
|
1225
|
-
let m = null;
|
|
1226
|
-
if (await c.handleRaw(p, async (_) => {
|
|
1227
|
-
m = _;
|
|
1228
|
-
}), !m)
|
|
1229
|
-
return await o({
|
|
1230
|
-
phase: "verification_failed",
|
|
1231
|
-
id: u.id,
|
|
1232
|
-
method: u.method,
|
|
1233
|
-
code: -32603,
|
|
1234
|
-
message: "AIP worker did not produce a response"
|
|
1235
|
-
}), x(y, -32603, "AIP worker did not produce a response");
|
|
1236
|
-
const v = await l.receiveRaw(m);
|
|
1237
|
-
return !v.ok || !v.received ? (await o({
|
|
1238
|
-
phase: "verification_failed",
|
|
1239
|
-
id: u.id,
|
|
1240
|
-
method: u.method,
|
|
1241
|
-
code: -32603,
|
|
1242
|
-
message: "AIP verification failed"
|
|
1243
|
-
}), x(y, -32603, "AIP verification failed")) : (await o({
|
|
1244
|
-
phase: "request_success",
|
|
1245
|
-
id: u.id,
|
|
1246
|
-
method: u.method
|
|
1247
|
-
}), K(We(v.received, u.id)));
|
|
1248
|
-
} catch (u) {
|
|
1249
|
-
const d = u instanceof Error ? u.message : "Internal error", p = nt(d);
|
|
1250
|
-
return await o({
|
|
1251
|
-
phase: "request_error",
|
|
1252
|
-
id: y,
|
|
1253
|
-
code: p,
|
|
1254
|
-
message: d
|
|
1255
|
-
}), x(y, p, d);
|
|
1256
|
-
}
|
|
1257
|
-
}
|
|
1258
|
-
};
|
|
1259
|
-
}
|
|
1260
|
-
async function Et(t) {
|
|
1261
|
-
if (!t.runtimePolicy?.enabled)
|
|
1262
|
-
return z(t);
|
|
1263
|
-
const { enforcer: e } = await F(t.runtimePolicy.options);
|
|
1264
|
-
e.assertInvariant({
|
|
1265
|
-
signatureVerification: !0,
|
|
1266
|
-
canonicalization: !0,
|
|
1267
|
-
replayDefense: !0,
|
|
1268
|
-
ttlClockSkewEnforcement: !0
|
|
1269
|
-
});
|
|
1270
|
-
const r = e.selectMode({
|
|
1271
|
-
concurrency: t.runtimePolicy.concurrencyHint ?? 1,
|
|
1272
|
-
latencySensitive: t.runtimePolicy.latencySensitive,
|
|
1273
|
-
compatibilityFirst: t.runtimePolicy.compatibilityFirst
|
|
1274
|
-
}), n = r.includes("binary") ? "binary" : "compact";
|
|
1275
|
-
return z({
|
|
1276
|
-
...t,
|
|
1277
|
-
gatewayWireFormat: n,
|
|
1278
|
-
workerWireFormat: n,
|
|
1279
|
-
gatewayAgentId: t.gatewayAgentId,
|
|
1280
|
-
workerAgentId: t.workerAgentId,
|
|
1281
|
-
onAuditEvent: async (s) => {
|
|
1282
|
-
t.onAuditEvent && await t.onAuditEvent({
|
|
1283
|
-
...s,
|
|
1284
|
-
message: s.message ?? `policy-mode=${r};wire-format=${n}`
|
|
1285
|
-
});
|
|
1286
|
-
}
|
|
1287
|
-
});
|
|
1288
|
-
}
|
|
1289
|
-
class xt {
|
|
1290
|
-
snapshot = null;
|
|
1291
|
-
options;
|
|
1292
|
-
constructor(e = {}) {
|
|
1293
|
-
this.options = e;
|
|
1294
|
-
}
|
|
1295
|
-
async loadInitial() {
|
|
1296
|
-
const e = await F(this.options);
|
|
1297
|
-
return this.snapshot = {
|
|
1298
|
-
...e,
|
|
1299
|
-
loadedAtMs: Date.now()
|
|
1300
|
-
}, this.snapshot;
|
|
1301
|
-
}
|
|
1302
|
-
current() {
|
|
1303
|
-
if (!this.snapshot)
|
|
1304
|
-
throw new Error("Runtime policy not loaded. Call loadInitial() first.");
|
|
1305
|
-
return this.snapshot;
|
|
1306
|
-
}
|
|
1307
|
-
async refresh() {
|
|
1308
|
-
const e = this.snapshot;
|
|
1309
|
-
try {
|
|
1310
|
-
const r = await F(this.options);
|
|
1311
|
-
return this.snapshot = {
|
|
1312
|
-
...r,
|
|
1313
|
-
loadedAtMs: Date.now()
|
|
1314
|
-
}, this.snapshot;
|
|
1315
|
-
} catch (r) {
|
|
1316
|
-
if (e)
|
|
1317
|
-
return e;
|
|
1318
|
-
throw r;
|
|
1319
|
-
}
|
|
1320
|
-
}
|
|
1321
|
-
}
|
|
1322
|
-
const it = {
|
|
1323
|
-
dev: {
|
|
1324
|
-
environment: "dev",
|
|
1325
|
-
mode: "ws-batch",
|
|
1326
|
-
poolSize: 1,
|
|
1327
|
-
batchSize: 8,
|
|
1328
|
-
inflightCap: 64,
|
|
1329
|
-
retryMaxAttempts: 2
|
|
1330
|
-
},
|
|
1331
|
-
staging: {
|
|
1332
|
-
environment: "staging",
|
|
1333
|
-
mode: "http-binary-batch",
|
|
1334
|
-
poolSize: 2,
|
|
1335
|
-
batchSize: 32,
|
|
1336
|
-
inflightCap: 256,
|
|
1337
|
-
retryMaxAttempts: 3
|
|
1338
|
-
},
|
|
1339
|
-
prod: {
|
|
1340
|
-
environment: "prod",
|
|
1341
|
-
mode: "http-binary-batch",
|
|
1342
|
-
poolSize: 4,
|
|
1343
|
-
batchSize: 64,
|
|
1344
|
-
inflightCap: 1024,
|
|
1345
|
-
retryMaxAttempts: 3
|
|
1346
|
-
}
|
|
1347
|
-
};
|
|
1348
|
-
function Rt(t) {
|
|
1349
|
-
return it[t];
|
|
1350
|
-
}
|
|
1351
|
-
function Ct(t) {
|
|
1352
|
-
const e = [];
|
|
1353
|
-
let r = "normal";
|
|
1354
|
-
return t.dropPct > 0.1 && (e.push("reduce inflight cap"), e.push("reduce batch size"), e.push("increase retry backoff"), t.mode === "http" && t.concurrency >= 100 && e.push("switch to http-binary-batch"), r = t.dropPct > 2 ? "critical" : "warn"), t.p99Ms > 20 && (e.push("reduce batch size"), e.push("run adaptive benchmark and re-baseline"), r === "normal" && (r = "warn"), t.p99Ms > 100 && (r = "critical")), {
|
|
1355
|
-
severity: r,
|
|
1356
|
-
actions: e
|
|
1357
|
-
};
|
|
1358
|
-
}
|
|
1359
|
-
class st {
|
|
1360
|
-
entries = /* @__PURE__ */ new Map();
|
|
1361
|
-
now;
|
|
1362
|
-
constructor(e = {}) {
|
|
1363
|
-
this.now = e.now ?? (() => Date.now());
|
|
1364
|
-
}
|
|
1365
|
-
live(e) {
|
|
1366
|
-
const r = this.entries.get(e);
|
|
1367
|
-
if (r) {
|
|
1368
|
-
if (r.expiresAt !== void 0 && r.expiresAt <= this.now()) {
|
|
1369
|
-
this.entries.delete(e);
|
|
1370
|
-
return;
|
|
1371
|
-
}
|
|
1372
|
-
return r;
|
|
1373
|
-
}
|
|
1374
|
-
}
|
|
1375
|
-
async set(e, r, n = {}) {
|
|
1376
|
-
if (n.nx && this.live(e)) return null;
|
|
1377
|
-
const s = n.pxMs !== void 0 ? this.now() + n.pxMs : void 0;
|
|
1378
|
-
return this.entries.set(e, { value: r, expiresAt: s }), "OK";
|
|
1379
|
-
}
|
|
1380
|
-
async get(e) {
|
|
1381
|
-
return this.live(e)?.value ?? null;
|
|
1382
|
-
}
|
|
1383
|
-
async del(e) {
|
|
1384
|
-
const r = this.live(e) !== void 0;
|
|
1385
|
-
return this.entries.delete(e), r ? 1 : 0;
|
|
1386
|
-
}
|
|
1387
|
-
pipeline() {
|
|
1388
|
-
const e = [], r = {
|
|
1389
|
-
set: (n, s, i) => (e.push({ key: n, value: s, options: i }), r),
|
|
1390
|
-
exec: async () => {
|
|
1391
|
-
const n = [];
|
|
1392
|
-
for (const s of e)
|
|
1393
|
-
n.push(await this.set(s.key, s.value, s.options));
|
|
1394
|
-
return n;
|
|
1395
|
-
}
|
|
1396
|
-
};
|
|
1397
|
-
return r;
|
|
1398
|
-
}
|
|
1399
|
-
}
|
|
1400
|
-
function at(t, e = {}) {
|
|
1401
|
-
const r = e.keyPrefix ?? "aip:replay:", n = e.errorBehavior ?? "fallback", s = e.onDegraded, i = e.fallback ?? (n === "fallback" ? (
|
|
1402
|
-
// The local fallback runs over a non-throwing in-memory client, so it
|
|
1403
|
-
// needs no fallback of its own — `reject` terminates the chain.
|
|
1404
|
-
at(new st(), { keyPrefix: r, errorBehavior: "reject" })
|
|
1405
|
-
) : void 0);
|
|
1406
|
-
function a(o, l, c, h) {
|
|
1407
|
-
return s?.(o, { key: l, behavior: n }), n === "allow" ? !0 : n === "reject" ? !1 : i ? i.reserve(l, c, h) : !1;
|
|
1408
|
-
}
|
|
1409
|
-
return {
|
|
1410
|
-
async reserve(o, l, c) {
|
|
1411
|
-
const h = Math.max(1, l - c);
|
|
1412
|
-
try {
|
|
1413
|
-
return await t.set(`${r}${o}`, "1", { nx: !0, pxMs: h }) === "OK";
|
|
1414
|
-
} catch (f) {
|
|
1415
|
-
return a(f, o, l, c);
|
|
1416
|
-
}
|
|
1417
|
-
},
|
|
1418
|
-
async reserveMany(o, l) {
|
|
1419
|
-
if (o.length === 0) return [];
|
|
1420
|
-
if (t.pipeline)
|
|
1421
|
-
try {
|
|
1422
|
-
const c = t.pipeline();
|
|
1423
|
-
for (const { key: f, expiresAtMs: y } of o)
|
|
1424
|
-
c.set(`${r}${f}`, "1", { nx: !0, pxMs: Math.max(1, y - l) });
|
|
1425
|
-
return (await c.exec()).map((f) => f === "OK");
|
|
1426
|
-
} catch (c) {
|
|
1427
|
-
return Promise.all(o.map((h) => a(c, h.key, h.expiresAtMs, l)));
|
|
1428
|
-
}
|
|
1429
|
-
return Promise.all(o.map((c) => this.reserve(c.key, c.expiresAtMs, l)));
|
|
1430
|
-
}
|
|
1431
|
-
};
|
|
1432
|
-
}
|
|
1433
|
-
class Nt {
|
|
1434
|
-
/** keyId map value: revoked-until timestamp, or `Infinity` for permanent. */
|
|
1435
|
-
revoked = /* @__PURE__ */ new Map();
|
|
1436
|
-
now;
|
|
1437
|
-
constructor(e = {}) {
|
|
1438
|
-
this.now = e.now ?? (() => Date.now());
|
|
1439
|
-
}
|
|
1440
|
-
makeKey(e, r) {
|
|
1441
|
-
return `${e}\0${r}`;
|
|
1442
|
-
}
|
|
1443
|
-
isRevoked(e, r, n = this.now()) {
|
|
1444
|
-
const s = this.revoked.get(this.makeKey(e, r));
|
|
1445
|
-
return s === void 0 ? !1 : s <= n ? (this.revoked.delete(this.makeKey(e, r)), !1) : !0;
|
|
1446
|
-
}
|
|
1447
|
-
revoke(e, r, n = {}) {
|
|
1448
|
-
this.revoked.set(this.makeKey(e, r), n.untilMs ?? Number.POSITIVE_INFINITY);
|
|
1449
|
-
}
|
|
1450
|
-
}
|
|
1451
|
-
function Pt(t, e = {}) {
|
|
1452
|
-
const r = e.keyPrefix ?? "aip:revoked:", n = e.errorBehavior ?? "reject", s = e.cacheTtlMs ?? 5e3, i = e.onDegraded, a = e.now ?? (() => Date.now()), o = /* @__PURE__ */ new Map();
|
|
1453
|
-
function l(c, h) {
|
|
1454
|
-
return `${r}${c}\0${h}`;
|
|
1455
|
-
}
|
|
1456
|
-
return {
|
|
1457
|
-
async isRevoked(c, h, f = a()) {
|
|
1458
|
-
const y = l(c, h), u = o.get(y);
|
|
1459
|
-
if (u && u.freshUntil > f) return u.revoked;
|
|
1460
|
-
try {
|
|
1461
|
-
if (!t.get) throw new Error("RedisLikeClient.get is required for revocation reads");
|
|
1462
|
-
const p = await t.get(y) !== null;
|
|
1463
|
-
return o.set(y, { revoked: p, freshUntil: f + s }), p;
|
|
1464
|
-
} catch (d) {
|
|
1465
|
-
return i?.(d, { sender: c, keyId: h }), u ? u.revoked : n === "reject";
|
|
1466
|
-
}
|
|
1467
|
-
},
|
|
1468
|
-
async revoke(c, h, f = {}) {
|
|
1469
|
-
const y = l(c, h), u = f.untilMs !== void 0 ? Math.max(1, f.untilMs - a()) : void 0;
|
|
1470
|
-
await t.set(y, "1", u !== void 0 ? { pxMs: u } : void 0), o.set(y, { revoked: !0, freshUntil: a() + s });
|
|
1471
|
-
}
|
|
1472
|
-
};
|
|
1473
|
-
}
|
|
1474
|
-
function Tt(t, e, r = {}) {
|
|
1475
|
-
const n = r.nowMsProvider ?? (() => Date.now());
|
|
1476
|
-
return async (s, i) => {
|
|
1477
|
-
if (!await e.isRevoked(i, s.keyId, n()))
|
|
1478
|
-
return t(s, i);
|
|
1479
|
-
};
|
|
1480
|
-
}
|
|
1481
|
-
function R(t, e, r) {
|
|
1482
|
-
return { jsonrpc: "2.0", id: t, error: { code: e, message: r } };
|
|
1483
|
-
}
|
|
1484
|
-
function ce(t, e) {
|
|
1485
|
-
return { requireSignature: !0, ...t.receive, replayCache: e };
|
|
1486
|
-
}
|
|
1487
|
-
function _t(t, e) {
|
|
1488
|
-
const r = e.wireFormat ?? "compact", n = e.ttlMs ?? 6e4, s = e.receive?.replayCache ?? new T(), i = ce(e, s);
|
|
1489
|
-
async function a(l) {
|
|
1490
|
-
return U(await e.sign(l), r);
|
|
1491
|
-
}
|
|
1492
|
-
function o(l, c, h, f) {
|
|
1493
|
-
return a(
|
|
1494
|
-
P({ sender: e.selfAgentId, recipient: l, intent: c, content: JSON.stringify(h), correlationId: f, ttlMs: n })
|
|
1495
|
-
);
|
|
1496
|
-
}
|
|
1497
|
-
return async (l) => {
|
|
1498
|
-
const c = await E(l, i), h = c.envelope?.header.sender, f = c.envelope?.header.messageId;
|
|
1499
|
-
if (!c.ok || !c.envelope) {
|
|
1500
|
-
const d = c.diagnostics.find((p) => p.level === "error")?.message ?? "AIP verification failed";
|
|
1501
|
-
return o(h, "ERROR", R(null, -32600, d), f);
|
|
1502
|
-
}
|
|
1503
|
-
if (c.envelope.header.recipient !== e.selfAgentId)
|
|
1504
|
-
return o(h, "ERROR", R(null, -32600, "Recipient mismatch: envelope not addressed to this agent"), f);
|
|
1505
|
-
let y;
|
|
1506
|
-
try {
|
|
1507
|
-
y = JSON.parse(c.envelope.body.content);
|
|
1508
|
-
} catch {
|
|
1509
|
-
return o(h, "ERROR", R(null, -32700, "Parse error: envelope body is not JSON-RPC"), f);
|
|
1510
|
-
}
|
|
1511
|
-
let u;
|
|
1512
|
-
try {
|
|
1513
|
-
u = await t(y);
|
|
1514
|
-
} catch (d) {
|
|
1515
|
-
u = R(y.id ?? null, -32603, d instanceof Error ? d.message : "Internal error");
|
|
1516
|
-
}
|
|
1517
|
-
return o(c.envelope.header.sender, "RESULT", u, f);
|
|
1518
|
-
};
|
|
1519
|
-
}
|
|
1520
|
-
function ot(t) {
|
|
1521
|
-
const e = t.wireFormat ?? "compact", r = t.ttlMs ?? 6e4, n = t.receive?.replayCache ?? new T(), s = ce(t, n);
|
|
1522
|
-
return {
|
|
1523
|
-
async encodeRequest(i) {
|
|
1524
|
-
const a = P({
|
|
1525
|
-
sender: t.selfAgentId,
|
|
1526
|
-
recipient: t.peerAgentId,
|
|
1527
|
-
intent: "TASK",
|
|
1528
|
-
content: JSON.stringify(i),
|
|
1529
|
-
ttlMs: r
|
|
1530
|
-
});
|
|
1531
|
-
return { raw: U(await t.sign(a), e), messageId: a.header.messageId };
|
|
1532
|
-
},
|
|
1533
|
-
async decodeResponse(i, a = {}) {
|
|
1534
|
-
const o = await E(i, s);
|
|
1535
|
-
if (!o.ok || !o.envelope) {
|
|
1536
|
-
const l = o.diagnostics.find((c) => c.level === "error")?.message ?? "unknown";
|
|
1537
|
-
throw new Error(`AIP response verification failed: ${l}`);
|
|
1538
|
-
}
|
|
1539
|
-
if (!t.peerAgentId)
|
|
1540
|
-
throw new Error("createMcpClientCodec requires peerAgentId to bind the response sender");
|
|
1541
|
-
if (o.envelope.header.sender !== t.peerAgentId)
|
|
1542
|
-
throw new Error(`AIP response sender '${o.envelope.header.sender}' is not the expected peer '${t.peerAgentId}'`);
|
|
1543
|
-
if (a.expectCorrelationId !== void 0 && o.envelope.body.correlationId !== a.expectCorrelationId)
|
|
1544
|
-
throw new Error(
|
|
1545
|
-
`AIP response correlationId '${o.envelope.body.correlationId ?? ""}' does not match request '${a.expectCorrelationId}'`
|
|
1546
|
-
);
|
|
1547
|
-
return JSON.parse(o.envelope.body.content);
|
|
1548
|
-
}
|
|
1549
|
-
};
|
|
1550
|
-
}
|
|
1551
|
-
function $t(t, e) {
|
|
1552
|
-
const r = ot(e);
|
|
1553
|
-
return async (n) => {
|
|
1554
|
-
const { raw: s, messageId: i } = await r.encodeRequest(n);
|
|
1555
|
-
return r.decodeResponse(await t(s), { expectCorrelationId: i });
|
|
1556
|
-
};
|
|
1557
|
-
}
|
|
1558
|
-
const de = {};
|
|
1559
|
-
function le(t) {
|
|
1560
|
-
if (typeof t != "string")
|
|
1561
|
-
throw new Error("stdio adapter supports json/compact (string) wire formats; use the HTTP adapter for binary");
|
|
1562
|
-
return t;
|
|
1563
|
-
}
|
|
1564
|
-
function Ot(t, e = {}) {
|
|
1565
|
-
const r = e.input ?? process.stdin, n = e.output ?? process.stdout, s = de.createInterface({ input: r, crlfDelay: Number.POSITIVE_INFINITY });
|
|
1566
|
-
let i = Promise.resolve();
|
|
1567
|
-
const a = (o) => {
|
|
1568
|
-
const l = o.trim();
|
|
1569
|
-
l && (i = i.then(async () => {
|
|
1570
|
-
try {
|
|
1571
|
-
const c = await t(l);
|
|
1572
|
-
n.write(`${le(c)}
|
|
1573
|
-
`);
|
|
1574
|
-
} catch (c) {
|
|
1575
|
-
e.onError?.(c);
|
|
1576
|
-
}
|
|
1577
|
-
}));
|
|
1578
|
-
};
|
|
1579
|
-
return s.on("line", a), {
|
|
1580
|
-
close() {
|
|
1581
|
-
s.off("line", a), s.close();
|
|
1582
|
-
}
|
|
1583
|
-
};
|
|
1584
|
-
}
|
|
1585
|
-
function Ft(t) {
|
|
1586
|
-
const e = de.createInterface({ input: t.input, crlfDelay: Number.POSITIVE_INFINITY }), r = [], n = (s) => {
|
|
1587
|
-
const i = s.trim();
|
|
1588
|
-
if (!i) return;
|
|
1589
|
-
const a = r.shift();
|
|
1590
|
-
a && a(i);
|
|
1591
|
-
};
|
|
1592
|
-
return e.on("line", n), {
|
|
1593
|
-
send(s) {
|
|
1594
|
-
const i = le(s);
|
|
1595
|
-
return new Promise((a) => {
|
|
1596
|
-
r.push(a), t.output.write(`${i}
|
|
1597
|
-
`);
|
|
1598
|
-
});
|
|
1599
|
-
},
|
|
1600
|
-
close() {
|
|
1601
|
-
e.off("line", n), e.close();
|
|
1602
|
-
}
|
|
1603
|
-
};
|
|
1604
|
-
}
|
|
1605
|
-
function Kt(t, e = {}) {
|
|
1606
|
-
return (r, n) => {
|
|
1607
|
-
const s = [];
|
|
1608
|
-
r.on("data", (i) => s.push(i)), r.on("error", () => {
|
|
1609
|
-
n.writeHead(400), n.end();
|
|
1610
|
-
}), r.on("end", () => {
|
|
1611
|
-
(async () => {
|
|
1612
|
-
try {
|
|
1613
|
-
const i = Buffer.concat(s), a = e.binary ? new Uint8Array(i) : i.toString("utf8"), o = await t(a);
|
|
1614
|
-
typeof o == "string" ? (n.writeHead(200, { "content-type": "application/json" }), n.end(o)) : (n.writeHead(200, { "content-type": "application/octet-stream" }), n.end(Buffer.from(o)));
|
|
1615
|
-
} catch (i) {
|
|
1616
|
-
n.writeHead(500, { "content-type": "application/json" }), n.end(JSON.stringify({ jsonrpc: "2.0", id: null, error: { code: -32603, message: i instanceof Error ? i.message : "internal error" } }));
|
|
1617
|
-
}
|
|
1618
|
-
})();
|
|
1619
|
-
});
|
|
1620
|
-
};
|
|
1621
|
-
}
|
|
1622
|
-
function Ut(t) {
|
|
1623
|
-
const e = t.fetchImpl ?? fetch, r = t.binary ? "application/octet-stream" : "application/json";
|
|
1624
|
-
return {
|
|
1625
|
-
async send(n) {
|
|
1626
|
-
const s = await e(t.url, {
|
|
1627
|
-
method: "POST",
|
|
1628
|
-
headers: { "content-type": r, ...t.headers },
|
|
1629
|
-
body: typeof n == "string" ? n : Buffer.from(n)
|
|
1630
|
-
});
|
|
1631
|
-
return t.binary ? new Uint8Array(await s.arrayBuffer()) : s.text();
|
|
1632
|
-
}
|
|
1633
|
-
};
|
|
1634
|
-
}
|
|
1635
|
-
export {
|
|
1636
|
-
je as AgentSession,
|
|
1637
|
-
He as AipAgentAdapter,
|
|
1638
|
-
Me as DEFAULT_MAX_BINARY_ENVELOPE_BYTES,
|
|
1639
|
-
ft as DistributedReplayCache,
|
|
1640
|
-
st as InMemoryRedisLikeClient,
|
|
1641
|
-
T as InMemoryReplayCache,
|
|
1642
|
-
Nt as InMemoryRevocationStore,
|
|
1643
|
-
Oe as InMemoryVerificationMaterialCache,
|
|
1644
|
-
bt as RollingKeyring,
|
|
1645
|
-
xt as RuntimePolicyManager,
|
|
1646
|
-
pt as SessionTransport,
|
|
1647
|
-
F as bootstrapRuntimePolicyEnforcer,
|
|
1648
|
-
A as canonicalizeEnvelope,
|
|
1649
|
-
B as createAipAgentAdapter,
|
|
1650
|
-
ze as createAipCapabilities,
|
|
1651
|
-
z as createAipMcpGatewayRuntime,
|
|
1652
|
-
Et as createAipMcpGatewayRuntimeWithPolicy,
|
|
1653
|
-
P as createEnvelope,
|
|
1654
|
-
Ut as createHttpMcpClient,
|
|
1655
|
-
Kt as createHttpMcpHandler,
|
|
1656
|
-
vt as createKeyringSignatureResolver,
|
|
1657
|
-
ot as createMcpClientCodec,
|
|
1658
|
-
et as createPolicyEnforcer,
|
|
1659
|
-
Ve as createRawTaskFromJsonRpc,
|
|
1660
|
-
It as createRawTaskFromLangChain,
|
|
1661
|
-
kt as createRawTaskFromLlamaIndex,
|
|
1662
|
-
at as createRedisReplayStore,
|
|
1663
|
-
Pt as createRedisRevocationStore,
|
|
1664
|
-
mt as createSignedMessage,
|
|
1665
|
-
Ft as createStdioMcpClient,
|
|
1666
|
-
Ke as decodeEnvelope,
|
|
1667
|
-
Ue as decodeEnvelopeBatch,
|
|
1668
|
-
_e as decodeEnvelopeBinary,
|
|
1669
|
-
Je as encodeAipCapabilities,
|
|
1670
|
-
U as encodeEnvelope,
|
|
1671
|
-
yt as encodeEnvelopeBatch,
|
|
1672
|
-
Te as encodeEnvelopeBinary,
|
|
1673
|
-
dt as generateEd25519KeypairBase64Url,
|
|
1674
|
-
Rt as getRuntimeTuningPreset,
|
|
1675
|
-
At as isRuntimeMode,
|
|
1676
|
-
Ze as loadRuntimePolicy,
|
|
1677
|
-
wt as negotiateAipCapabilities,
|
|
1678
|
-
gt as parseAipCapabilities,
|
|
1679
|
-
Xe as parseRuntimePolicyJson,
|
|
1680
|
-
E as receiveEnvelope,
|
|
1681
|
-
De as receiveEnvelopeBatch,
|
|
1682
|
-
Ct as recommendPolicyAdjustments,
|
|
1683
|
-
Ot as serveMcpOverStdio,
|
|
1684
|
-
lt as signCanonicalPayloadEd25519,
|
|
1685
|
-
ct as signCanonicalPayloadHmac,
|
|
1686
|
-
ut as signEnvelopeEd25519,
|
|
1687
|
-
Q as signEnvelopeHmac,
|
|
1688
|
-
We as toJsonRpcResponse,
|
|
1689
|
-
Mt as toLangChainMessage,
|
|
1690
|
-
St as toLlamaIndexMessage,
|
|
1691
|
-
Ie as validateEnvelope,
|
|
1692
|
-
Ye as validateRuntimePolicy,
|
|
1693
|
-
ge as verifyCanonicalPayloadEd25519,
|
|
1694
|
-
me as verifyCanonicalPayloadHmac,
|
|
1695
|
-
ve as verifyCanonicalPayloadSignature,
|
|
1696
|
-
be as verifyEnvelopeEd25519,
|
|
1697
|
-
we as verifyEnvelopeHmac,
|
|
1698
|
-
ht as verifyEnvelopeSignature,
|
|
1699
|
-
Tt as withRevocationCheck,
|
|
1700
|
-
$t as wrapMcpClient,
|
|
1701
|
-
_t as wrapMcpServer
|
|
1702
|
-
};
|