@7h3/protocol 0.4.0 → 0.5.1
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/README.md +6 -323
- package/agentAdapter.d.ts +26 -0
- package/auditLog.d.ts +54 -0
- package/capability.d.ts +67 -0
- package/cborCodec.d.ts +34 -0
- package/conformanceVectors.d.ts +20 -0
- package/encryption.d.ts +85 -0
- package/envelopeCbor.d.ts +33 -0
- package/frameworkAdapters.d.ts +72 -0
- package/gateway.d.ts +53 -0
- package/grpcBinding.d.ts +29 -0
- package/httpBinding.d.ts +50 -0
- package/index.d.ts +39 -0
- package/index.js +3662 -0
- package/keyInfra.d.ts +64 -0
- package/keyRegistry.d.ts +9 -0
- package/keyRotation.d.ts +20 -0
- package/mcpGateway.d.ts +37 -0
- package/mcpTransports.d.ts +62 -0
- package/mcpWrapper.d.ts +83 -0
- package/otel.d.ts +65 -0
- package/package.json +13 -79
- package/policyEnforcer.d.ts +50 -0
- package/policyTelemetryFeedback.d.ts +11 -0
- package/protocol.d.ts +66 -0
- package/protocolAgent.d.ts +58 -0
- package/protocolBinary.d.ts +8 -0
- package/protocolCapabilities.d.ts +24 -0
- package/protocolReplay.d.ts +35 -0
- package/protocolTransport.d.ts +73 -0
- package/queueBinding.d.ts +43 -0
- package/rateLimiter.d.ts +18 -0
- package/{src/redisClient.ts → redisClient.d.ts} +25 -75
- package/replayStores.d.ts +98 -0
- package/revocation.d.ts +71 -0
- package/routePolicy.d.ts +30 -0
- package/runtimePolicy.d.ts +24 -0
- package/runtimePolicyManager.d.ts +15 -0
- package/runtimePolicyPresets.d.ts +11 -0
- package/signedResponse.d.ts +21 -0
- package/stream.d.ts +59 -0
- package/telemetry.d.ts +79 -0
- package/webhookBinding.d.ts +30 -0
- package/wsBinding.d.ts +52 -0
- package/.dockerignore +0 -19
- package/.github/dependabot.yml +0 -32
- package/.github/workflows/ci.yml +0 -31
- package/.github/workflows/publish.yml +0 -59
- package/.github/workflows/scorecard.yml +0 -37
- package/7h3.example.yaml +0 -125
- package/CHANGELOG.md +0 -92
- package/CONTRIBUTING.md +0 -82
- package/Dockerfile +0 -73
- package/GOVERNANCE.md +0 -62
- package/SECURITY.md +0 -70
- package/bench-results/replay-cache-full-1777891033256.json +0 -10
- package/bench-results/replay-cache-full-1777896317488.json +0 -10
- package/bench-results/replay-cache-full-1777900993184.json +0 -10
- package/bench-results/replay-cache-full-1777901019285.json +0 -10
- package/bench-results/replay-cache-quick-1777870170126.json +0 -10
- package/bench-results/signature-profiles-quick-1775875160079.json +0 -85
- package/bench-results/signature-profiles-quick-1775983539716.json +0 -85
- package/bench-results/signature-profiles-quick-1776237913190.json +0 -85
- package/bench-results/wire-codecs-full-1777891019803.json +0 -93
- package/bench-results/wire-codecs-full-1777896260964.json +0 -93
- package/bench-results/wire-codecs-full-1777901004247.json +0 -93
- package/bench-results/wire-codecs-quick-1775972879056.json +0 -93
- package/bench-results/wire-codecs-quick-1775983541111.json +0 -93
- package/bench-results/wire-codecs-quick-1776237914299.json +0 -93
- package/bench-results/wire-codecs-quick-1777841285236.json +0 -93
- package/bench-results/wire-codecs-quick-1777841321772.json +0 -93
- package/bench-results/wire-codecs-quick-1777841330408.json +0 -93
- package/bench-results/wire-codecs-quick-1777852886082.json +0 -93
- package/bench-results/wire-codecs-quick-1777852988773.json +0 -93
- package/bench-results/wire-codecs-quick-1777870188095.json +0 -93
- package/bench-results/wire-codecs-quick-1777870263918.json +0 -93
- package/bench-results/wire-codecs-quick-1777870455034.json +0 -93
- package/bench-results/wire-codecs-quick-1778816163081.json +0 -93
- package/bench-results/wire-codecs-quick-1778843936130.json +0 -93
- package/bin/7h3.ts +0 -385
- package/conformance/7h3_v0_1.json +0 -77
- package/conformance/7h3_v0_1_binary.json +0 -20
- package/conformance/aip_v0_1_binary.json +0 -20
- package/docker-compose.yaml +0 -77
- package/docs/ADOPTION_PLAN.md +0 -120
- package/docs/AGENTS.md +0 -77
- package/docs/AIP_RFC_v0.1.md +0 -97
- package/docs/AI_DECISION_CARD.md +0 -122
- package/docs/AI_RUNTIME_POLICY.json +0 -126
- package/docs/AI_RUNTIME_POLICY.yaml +0 -110
- package/docs/BACKPRESSURE_TUNING.md +0 -65
- package/docs/BENCHMARK_CLAIM_MATRIX.md +0 -42
- package/docs/BENCHMARK_REPORT_TEMPLATE.md +0 -169
- package/docs/BINARY_CODEC_BENCH.md +0 -23
- package/docs/CLEAN_CLONE_RUNBOOK.md +0 -36
- package/docs/CLOCK_SKEW_POLICY.md +0 -30
- package/docs/DISTRIBUTED_REPLAY.md +0 -142
- package/docs/FUZZ_CAMPAIGN.md +0 -121
- package/docs/GATEWAY.md +0 -195
- package/docs/KEY_MANAGEMENT_POLICY.md +0 -53
- package/docs/KEY_REVOCATION.md +0 -69
- package/docs/MCP_WRAPPER.md +0 -159
- package/docs/MIGRATION_GUIDE.md +0 -40
- package/docs/OPERATORS.md +0 -184
- package/docs/PERF_REGRESSION_POLICY.md +0 -34
- package/docs/PROJECT_EXAMINATION_2026-05-31.md +0 -219
- package/docs/RELEASE_BENCHMARK_REPORT_2026-05-15.md +0 -135
- package/docs/RELEASE_GATE.md +0 -25
- package/docs/RELEASE_NOTES_v0.1.0.md +0 -54
- package/docs/SECURITY_REVIEW_2026-06-05.md +0 -165
- package/docs/TELEMETRY.md +0 -41
- package/docs/THREAT_MODEL.md +0 -89
- package/docs/VERSIONING_POLICY.md +0 -30
- package/docs/assets/banner.png +0 -0
- package/eslint.config.js +0 -15
- package/fuzz/ts/harness-decode.ts +0 -136
- package/fuzz/ts/harness-verify.ts +0 -121
- package/fuzz/ts/run.ts +0 -35
- package/mcp-server/README.md +0 -38
- package/mcp-server/package-lock.json +0 -1187
- package/mcp-server/package.json +0 -35
- package/mcp-server/src/index.ts +0 -236
- package/mcp-server/tsconfig.json +0 -14
- package/scripts/aip-framework-quickstart.ts +0 -110
- package/scripts/aip-mcp-gateway.ts +0 -38
- package/scripts/aip-mcp-wrap-demo.ts +0 -72
- package/scripts/aip-quickstart.ts +0 -60
- package/scripts/bench-diff.ts +0 -118
- package/scripts/bench-protocol-e2e.ts +0 -937
- package/scripts/bench-protocol-openloop.ts +0 -1397
- package/scripts/bench-replay-cache.ts +0 -76
- package/scripts/bench-signature-profiles.ts +0 -180
- package/scripts/bench-wire-codecs.ts +0 -161
- package/scripts/build-binary-conformance.ts +0 -36
- package/scripts/build-release-dashboard.ts +0 -175
- package/scripts/canary-rollout.ts +0 -38
- package/scripts/mcpGatewayCli.test.ts +0 -116
- package/scripts/prepare-aip-package.ts +0 -88
- package/scripts/regen-conformance-sigs.ts +0 -18
- package/scripts/release-gate.ts +0 -19
- package/scripts/validate-runtime-policy.ts +0 -18
- package/sdk/browser/index.test.ts +0 -162
- package/sdk/browser/index.ts +0 -257
- package/sdk/browser/package.json +0 -13
- package/sdk/go/go.mod +0 -3
- package/sdk/go/http.go +0 -135
- package/sdk/go/protocol.go +0 -324
- package/sdk/go/protocol_test.go +0 -334
- package/sdk/go/webhook.go +0 -136
- package/sdk/python/README.md +0 -18
- package/sdk/python/protocol_7h3/__init__.py +0 -46
- package/sdk/python/protocol_7h3/http.py +0 -212
- package/sdk/python/protocol_7h3/keys.py +0 -149
- package/sdk/python/protocol_7h3/protocol.py +0 -525
- package/sdk/python/protocol_7h3/queue.py +0 -118
- package/sdk/python/protocol_7h3/webhook.py +0 -116
- package/sdk/python/pyproject.toml +0 -40
- package/sdk/python/tests/test_conformance.py +0 -110
- package/sdk/python/tests/test_http.py +0 -305
- package/sdk/python/tests/test_keys.py +0 -417
- package/sdk/python/tests/test_queue.py +0 -120
- package/sdk/python/tests/test_webhook.py +0 -345
- package/sdk/rust/Cargo.lock +0 -371
- package/sdk/rust/Cargo.toml +0 -25
- package/sdk/rust/README.md +0 -31
- package/sdk/rust/fuzz/Cargo.toml +0 -29
- package/sdk/rust/fuzz/fuzz_targets/fuzz_canonicalize.rs +0 -46
- package/sdk/rust/fuzz/fuzz_targets/fuzz_decode.rs +0 -11
- package/sdk/rust/src/bin/aip_mcp_gateway.rs +0 -59
- package/sdk/rust/src/http.rs +0 -145
- package/sdk/rust/src/keys.rs +0 -161
- package/sdk/rust/src/lib.rs +0 -688
- package/sdk/rust/src/queue.rs +0 -79
- package/sdk/rust/src/webhook.rs +0 -86
- package/sdk/rust/tests/conformance.rs +0 -148
- package/sdk/rust/tests/gateway.rs +0 -130
- package/sdk/rust/tests/http_webhook_queue.rs +0 -201
- package/sdk/rust/tests/keys.rs +0 -189
- package/src/agentAdapter.test.ts +0 -48
- package/src/agentAdapter.ts +0 -56
- package/src/auditLog.test.ts +0 -145
- package/src/auditLog.ts +0 -147
- package/src/conformance.test.ts +0 -136
- package/src/conformanceVectors.ts +0 -99
- package/src/frameworkAdapters.test.ts +0 -290
- package/src/frameworkAdapters.ts +0 -261
- package/src/gateway.test.ts +0 -343
- package/src/gateway.ts +0 -171
- package/src/grpcBinding.test.ts +0 -211
- package/src/grpcBinding.ts +0 -103
- package/src/httpBinding.test.ts +0 -376
- package/src/httpBinding.ts +0 -163
- package/src/index.ts +0 -32
- package/src/keyInfra.test.ts +0 -278
- package/src/keyInfra.ts +0 -228
- package/src/keyRegistry.ts +0 -59
- package/src/keyRotation.test.ts +0 -78
- package/src/keyRotation.ts +0 -72
- package/src/mcpGateway.test.ts +0 -129
- package/src/mcpGateway.ts +0 -250
- package/src/mcpTransports.test.ts +0 -92
- package/src/mcpTransports.ts +0 -169
- package/src/mcpWrapper.test.ts +0 -179
- package/src/mcpWrapper.ts +0 -206
- package/src/policyEnforcer.test.ts +0 -99
- package/src/policyEnforcer.ts +0 -169
- package/src/policyTelemetryFeedback.test.ts +0 -25
- package/src/policyTelemetryFeedback.ts +0 -38
- package/src/protocol.bench.ts +0 -37
- package/src/protocol.test.ts +0 -155
- package/src/protocol.ts +0 -413
- package/src/protocolAgent.test.ts +0 -105
- package/src/protocolAgent.ts +0 -169
- package/src/protocolBinary.test.ts +0 -165
- package/src/protocolBinary.ts +0 -312
- package/src/protocolCapabilities.ts +0 -70
- package/src/protocolFuzz.advanced.test.ts +0 -235
- package/src/protocolFuzz.test.ts +0 -111
- package/src/protocolNegative.test.ts +0 -97
- package/src/protocolReplay.test.ts +0 -71
- package/src/protocolReplay.ts +0 -194
- package/src/protocolTransport.test.ts +0 -556
- package/src/protocolTransport.ts +0 -483
- package/src/queueBinding.test.ts +0 -130
- package/src/queueBinding.ts +0 -102
- package/src/rateLimiter.test.ts +0 -96
- package/src/rateLimiter.ts +0 -46
- package/src/redisIntegration.test.ts +0 -134
- package/src/replayStores.test.ts +0 -141
- package/src/replayStores.ts +0 -82
- package/src/revocation.test.ts +0 -98
- package/src/revocation.ts +0 -0
- package/src/routePolicy.test.ts +0 -87
- package/src/routePolicy.ts +0 -72
- package/src/runtimePolicy.test.ts +0 -49
- package/src/runtimePolicy.ts +0 -81
- package/src/runtimePolicyManager.test.ts +0 -29
- package/src/runtimePolicyManager.ts +0 -50
- package/src/runtimePolicyPresets.ts +0 -43
- package/src/signedResponse.test.ts +0 -111
- package/src/signedResponse.ts +0 -83
- package/src/webhookBinding.test.ts +0 -144
- package/src/webhookBinding.ts +0 -115
- package/src/wsBinding.test.ts +0 -221
- package/src/wsBinding.ts +0 -100
- package/tsconfig.json +0 -15
- package/tsconfig.lib.json +0 -23
- package/vite.lib.config.ts +0 -16
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest'
|
|
2
|
-
import { createEnvelope, signEnvelopeEd25519, signEnvelopeHmac } from './protocol'
|
|
3
|
-
import { DEFAULT_MAX_BINARY_ENVELOPE_BYTES, decodeEnvelopeBinary, encodeEnvelopeBinary } from './protocolBinary'
|
|
4
|
-
|
|
5
|
-
function stringFieldOffset(raw: Uint8Array, fieldIndex: number): { lengthOffset: number; valueOffset: number; length: number } {
|
|
6
|
-
let offset = 4 + 1 + 1 + 8 + 4
|
|
7
|
-
const view = new DataView(raw.buffer, raw.byteOffset, raw.byteLength)
|
|
8
|
-
for (let index = 0; index <= fieldIndex; index += 1) {
|
|
9
|
-
const lengthOffset = offset
|
|
10
|
-
const length = view.getUint32(lengthOffset, false)
|
|
11
|
-
const valueOffset = lengthOffset + 4
|
|
12
|
-
if (index === fieldIndex) return { lengthOffset, valueOffset, length }
|
|
13
|
-
offset = valueOffset + length
|
|
14
|
-
}
|
|
15
|
-
throw new Error(`missing string field ${fieldIndex}`)
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function replaceAscii(raw: Uint8Array, fieldIndex: number, value: string): Uint8Array {
|
|
19
|
-
const copy = raw.slice()
|
|
20
|
-
const field = stringFieldOffset(copy, fieldIndex)
|
|
21
|
-
if (value.length !== field.length) throw new Error('replacement must preserve encoded length')
|
|
22
|
-
for (let index = 0; index < value.length; index += 1) {
|
|
23
|
-
copy[field.valueOffset + index] = value.charCodeAt(index)
|
|
24
|
-
}
|
|
25
|
-
return copy
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
describe('AIP binary wire codec', () => {
|
|
29
|
-
it('roundtrips HS256 signed envelope', async () => {
|
|
30
|
-
const signed = await signEnvelopeHmac(
|
|
31
|
-
createEnvelope({
|
|
32
|
-
sender: 'agent.alpha',
|
|
33
|
-
recipient: 'agent.beta',
|
|
34
|
-
intent: 'TASK',
|
|
35
|
-
content: 'route:binary',
|
|
36
|
-
capability: 'task.plan',
|
|
37
|
-
correlationId: 'corr-b1',
|
|
38
|
-
messageId: 'bin-1',
|
|
39
|
-
nonce: 'nb1',
|
|
40
|
-
nowMs: 1000,
|
|
41
|
-
ttlMs: 60_000,
|
|
42
|
-
}),
|
|
43
|
-
'shared-secret',
|
|
44
|
-
'k1',
|
|
45
|
-
)
|
|
46
|
-
|
|
47
|
-
const raw = encodeEnvelopeBinary(signed)
|
|
48
|
-
const decoded = decodeEnvelopeBinary(raw)
|
|
49
|
-
expect(decoded.ok).toBe(true)
|
|
50
|
-
expect(decoded.envelope).toEqual(signed)
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
it('roundtrips ED25519 signed envelope', async () => {
|
|
54
|
-
const privateKey = 'MC4CAQAwBQYDK2VwBCIEICheZbQGuDVb6hezIlcs0QnCHGxz6IhiLkC9M0qr8OOZ'
|
|
55
|
-
const signed = await signEnvelopeEd25519(
|
|
56
|
-
createEnvelope({
|
|
57
|
-
sender: 'agent.ed',
|
|
58
|
-
recipient: 'agent.verify',
|
|
59
|
-
intent: 'TASK',
|
|
60
|
-
content: 'route:ed25519-binary',
|
|
61
|
-
capability: 'task.sign',
|
|
62
|
-
correlationId: 'corr-ed-b1',
|
|
63
|
-
messageId: 'bin-ed-1',
|
|
64
|
-
nonce: 'n-ed-b1',
|
|
65
|
-
nowMs: 1000,
|
|
66
|
-
ttlMs: 45_000,
|
|
67
|
-
}),
|
|
68
|
-
privateKey,
|
|
69
|
-
'ed-k1',
|
|
70
|
-
)
|
|
71
|
-
|
|
72
|
-
const raw = encodeEnvelopeBinary(signed)
|
|
73
|
-
const decoded = decodeEnvelopeBinary(raw)
|
|
74
|
-
expect(decoded.ok).toBe(true)
|
|
75
|
-
expect(decoded.envelope).toEqual(signed)
|
|
76
|
-
})
|
|
77
|
-
|
|
78
|
-
it('rejects bad magic and trailing bytes', async () => {
|
|
79
|
-
const signed = await signEnvelopeHmac(
|
|
80
|
-
createEnvelope({
|
|
81
|
-
sender: 'agent.alpha',
|
|
82
|
-
intent: 'PING',
|
|
83
|
-
content: 'x',
|
|
84
|
-
messageId: 'bin-2',
|
|
85
|
-
nonce: 'nb2',
|
|
86
|
-
nowMs: 1000,
|
|
87
|
-
ttlMs: 60_000,
|
|
88
|
-
}),
|
|
89
|
-
'shared-secret',
|
|
90
|
-
'k1',
|
|
91
|
-
)
|
|
92
|
-
|
|
93
|
-
const raw = encodeEnvelopeBinary(signed)
|
|
94
|
-
const badMagic = raw.slice()
|
|
95
|
-
badMagic[0] = 0
|
|
96
|
-
const decodedMagic = decodeEnvelopeBinary(badMagic)
|
|
97
|
-
expect(decodedMagic.ok).toBe(false)
|
|
98
|
-
expect(decodedMagic.diagnostics.some((d) => d.message.includes('magic'))).toBe(true)
|
|
99
|
-
|
|
100
|
-
const trailing = new Uint8Array(raw.byteLength + 1)
|
|
101
|
-
trailing.set(raw)
|
|
102
|
-
trailing[raw.byteLength] = 1
|
|
103
|
-
const decodedTrailing = decodeEnvelopeBinary(trailing)
|
|
104
|
-
expect(decodedTrailing.ok).toBe(false)
|
|
105
|
-
expect(decodedTrailing.diagnostics.some((d) => d.message.includes('Trailing bytes'))).toBe(true)
|
|
106
|
-
})
|
|
107
|
-
|
|
108
|
-
it('rejects oversized binary frames before parsing', () => {
|
|
109
|
-
const oversized = new Uint8Array(DEFAULT_MAX_BINARY_ENVELOPE_BYTES + 1)
|
|
110
|
-
const decoded = decodeEnvelopeBinary(oversized)
|
|
111
|
-
|
|
112
|
-
expect(decoded.ok).toBe(false)
|
|
113
|
-
expect(decoded.diagnostics.some((d) => d.message.includes('maximum frame size'))).toBe(true)
|
|
114
|
-
})
|
|
115
|
-
|
|
116
|
-
it('rejects malformed string lengths', async () => {
|
|
117
|
-
const signed = await signEnvelopeHmac(
|
|
118
|
-
createEnvelope({
|
|
119
|
-
sender: 'agent.alpha',
|
|
120
|
-
intent: 'PING',
|
|
121
|
-
content: 'x',
|
|
122
|
-
messageId: 'bin-malformed-length',
|
|
123
|
-
nonce: 'nb-malformed-length',
|
|
124
|
-
nowMs: 1000,
|
|
125
|
-
ttlMs: 60_000,
|
|
126
|
-
}),
|
|
127
|
-
'shared-secret',
|
|
128
|
-
'k1',
|
|
129
|
-
)
|
|
130
|
-
|
|
131
|
-
const raw = encodeEnvelopeBinary(signed)
|
|
132
|
-
const malformed = raw.slice()
|
|
133
|
-
const version = stringFieldOffset(malformed, 0)
|
|
134
|
-
new DataView(malformed.buffer, malformed.byteOffset, malformed.byteLength).setUint32(version.lengthOffset, raw.byteLength, false)
|
|
135
|
-
|
|
136
|
-
const decoded = decodeEnvelopeBinary(malformed)
|
|
137
|
-
expect(decoded.ok).toBe(false)
|
|
138
|
-
expect(decoded.diagnostics.some((d) => d.message.includes('Unexpected end'))).toBe(true)
|
|
139
|
-
})
|
|
140
|
-
|
|
141
|
-
it('rejects unsupported decoded protocol fields', async () => {
|
|
142
|
-
const signed = await signEnvelopeHmac(
|
|
143
|
-
createEnvelope({
|
|
144
|
-
sender: 'agent.alpha',
|
|
145
|
-
intent: 'TASK',
|
|
146
|
-
content: 'x',
|
|
147
|
-
messageId: 'bin-invalid-fields',
|
|
148
|
-
nonce: 'nb-invalid-fields',
|
|
149
|
-
nowMs: 1000,
|
|
150
|
-
ttlMs: 60_000,
|
|
151
|
-
}),
|
|
152
|
-
'shared-secret',
|
|
153
|
-
'k1',
|
|
154
|
-
)
|
|
155
|
-
|
|
156
|
-
const raw = encodeEnvelopeBinary(signed)
|
|
157
|
-
const badVersion = decodeEnvelopeBinary(replaceAscii(raw, 0, 'aip/0.2'))
|
|
158
|
-
const badIntent = decodeEnvelopeBinary(replaceAscii(raw, 4, 'NOPE'))
|
|
159
|
-
|
|
160
|
-
expect(badVersion.ok).toBe(false)
|
|
161
|
-
expect(badVersion.diagnostics.some((d) => d.message.includes('Unsupported protocol version'))).toBe(true)
|
|
162
|
-
expect(badIntent.ok).toBe(false)
|
|
163
|
-
expect(badIntent.diagnostics.some((d) => d.message.includes('Unsupported intent'))).toBe(true)
|
|
164
|
-
})
|
|
165
|
-
})
|
package/src/protocolBinary.ts
DELETED
|
@@ -1,312 +0,0 @@
|
|
|
1
|
-
import type { ProtocolEnvelope, ProtocolSignature } from './protocol'
|
|
2
|
-
import type { ReceiveEnvelopeResult } from './protocolTransport'
|
|
3
|
-
|
|
4
|
-
const MAGIC = [0x41, 0x49, 0x50, 0x42] as const // AIPB
|
|
5
|
-
const MAGIC_BYTES = Uint8Array.from(MAGIC)
|
|
6
|
-
const FORMAT_VERSION = 1
|
|
7
|
-
export const DEFAULT_MAX_BINARY_ENVELOPE_BYTES = 1024 * 1024
|
|
8
|
-
|
|
9
|
-
const FLAG_RECIPIENT = 1 << 0
|
|
10
|
-
const FLAG_CAPABILITY = 1 << 1
|
|
11
|
-
const FLAG_CORRELATION = 1 << 2
|
|
12
|
-
const FLAG_SIGNATURE = 1 << 3
|
|
13
|
-
const textEncoder = new TextEncoder()
|
|
14
|
-
const textDecoder = new TextDecoder()
|
|
15
|
-
const INTENT_KINDS = new Set(['PING', 'PONG', 'CAPS', 'TASK', 'RESULT', 'ERROR'])
|
|
16
|
-
|
|
17
|
-
export interface BinaryDecodeOptions {
|
|
18
|
-
maxFrameBytes?: number
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function utf8Encode(value: string): Uint8Array {
|
|
22
|
-
return textEncoder.encode(value)
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function utf8Decode(value: Uint8Array): string {
|
|
26
|
-
return textDecoder.decode(value)
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function signatureAlgCode(alg: ProtocolSignature['alg']): number {
|
|
30
|
-
return alg === 'ED25519' ? 2 : 1
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function signatureAlgFromCode(code: number): ProtocolSignature['alg'] | null {
|
|
34
|
-
if (code === 1) return 'HS256'
|
|
35
|
-
if (code === 2) return 'ED25519'
|
|
36
|
-
return null
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
interface EncodedString {
|
|
40
|
-
bytes: Uint8Array
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function encodeString(value: string): EncodedString {
|
|
44
|
-
return { bytes: utf8Encode(value) }
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function encodedStringSize(encoded: EncodedString | null): number {
|
|
48
|
-
return encoded ? 4 + encoded.bytes.byteLength : 0
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
class BinaryWriter {
|
|
52
|
-
private readonly bytes: Uint8Array
|
|
53
|
-
|
|
54
|
-
private readonly view: DataView
|
|
55
|
-
|
|
56
|
-
private offset = 0
|
|
57
|
-
|
|
58
|
-
constructor(size: number) {
|
|
59
|
-
this.bytes = new Uint8Array(size)
|
|
60
|
-
this.view = new DataView(this.bytes.buffer, this.bytes.byteOffset, this.bytes.byteLength)
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
writeU8(value: number): void {
|
|
64
|
-
this.bytes[this.offset] = value & 0xff
|
|
65
|
-
this.offset += 1
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
writeU32(value: number): void {
|
|
69
|
-
this.view.setUint32(this.offset, value, false)
|
|
70
|
-
this.offset += 4
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
writeU64(value: number): void {
|
|
74
|
-
this.view.setBigUint64(this.offset, BigInt(value), false)
|
|
75
|
-
this.offset += 8
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
writeBytes(value: Uint8Array): void {
|
|
79
|
-
this.bytes.set(value, this.offset)
|
|
80
|
-
this.offset += value.byteLength
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
writeString(encoded: EncodedString): void {
|
|
84
|
-
this.writeU32(encoded.bytes.byteLength)
|
|
85
|
-
this.writeBytes(encoded.bytes)
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
finish(): Uint8Array {
|
|
89
|
-
return this.bytes
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
class BinaryReader {
|
|
94
|
-
private readonly bytes: Uint8Array
|
|
95
|
-
|
|
96
|
-
private offset = 0
|
|
97
|
-
|
|
98
|
-
constructor(bytes: Uint8Array) {
|
|
99
|
-
this.bytes = bytes
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
remaining(): number {
|
|
103
|
-
return this.bytes.byteLength - this.offset
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
readU8(): number {
|
|
107
|
-
if (this.remaining() < 1) throw new Error('Unexpected end of binary envelope')
|
|
108
|
-
const value = this.bytes[this.offset]
|
|
109
|
-
this.offset += 1
|
|
110
|
-
return value ?? 0
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
readU32(): number {
|
|
114
|
-
if (this.remaining() < 4) throw new Error('Unexpected end of binary envelope')
|
|
115
|
-
const value = new DataView(this.bytes.buffer, this.bytes.byteOffset + this.offset, 4).getUint32(0, false)
|
|
116
|
-
this.offset += 4
|
|
117
|
-
return value
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
readU64(): number {
|
|
121
|
-
if (this.remaining() < 8) throw new Error('Unexpected end of binary envelope')
|
|
122
|
-
const value = new DataView(this.bytes.buffer, this.bytes.byteOffset + this.offset, 8).getBigUint64(0, false)
|
|
123
|
-
this.offset += 8
|
|
124
|
-
return Number(value)
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
readBytes(length: number): Uint8Array {
|
|
128
|
-
if (length < 0 || this.remaining() < length) throw new Error('Unexpected end of binary envelope')
|
|
129
|
-
const value = this.bytes.slice(this.offset, this.offset + length)
|
|
130
|
-
this.offset += length
|
|
131
|
-
return value
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
readString(): string {
|
|
135
|
-
const length = this.readU32()
|
|
136
|
-
const value = this.readBytes(length)
|
|
137
|
-
return utf8Decode(value)
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
export function encodeEnvelopeBinary(envelope: ProtocolEnvelope): Uint8Array {
|
|
142
|
-
const flags =
|
|
143
|
-
(envelope.header.recipient ? FLAG_RECIPIENT : 0) |
|
|
144
|
-
(envelope.body.capability ? FLAG_CAPABILITY : 0) |
|
|
145
|
-
(envelope.body.correlationId ? FLAG_CORRELATION : 0) |
|
|
146
|
-
(envelope.signature ? FLAG_SIGNATURE : 0)
|
|
147
|
-
|
|
148
|
-
const version = encodeString(envelope.header.version)
|
|
149
|
-
const messageId = encodeString(envelope.header.messageId)
|
|
150
|
-
const sender = encodeString(envelope.header.sender)
|
|
151
|
-
const nonce = encodeString(envelope.header.nonce)
|
|
152
|
-
const intent = encodeString(envelope.body.intent)
|
|
153
|
-
const content = encodeString(envelope.body.content)
|
|
154
|
-
const recipient = envelope.header.recipient ? encodeString(envelope.header.recipient) : null
|
|
155
|
-
const capability = envelope.body.capability ? encodeString(envelope.body.capability) : null
|
|
156
|
-
const correlationId = envelope.body.correlationId ? encodeString(envelope.body.correlationId) : null
|
|
157
|
-
const signatureKeyId = envelope.signature ? encodeString(envelope.signature.keyId) : null
|
|
158
|
-
const signatureValue = envelope.signature ? encodeString(envelope.signature.value) : null
|
|
159
|
-
|
|
160
|
-
const size =
|
|
161
|
-
MAGIC_BYTES.byteLength +
|
|
162
|
-
1 +
|
|
163
|
-
1 +
|
|
164
|
-
8 +
|
|
165
|
-
4 +
|
|
166
|
-
encodedStringSize(version) +
|
|
167
|
-
encodedStringSize(messageId) +
|
|
168
|
-
encodedStringSize(sender) +
|
|
169
|
-
encodedStringSize(nonce) +
|
|
170
|
-
encodedStringSize(intent) +
|
|
171
|
-
encodedStringSize(content) +
|
|
172
|
-
encodedStringSize(recipient) +
|
|
173
|
-
encodedStringSize(capability) +
|
|
174
|
-
encodedStringSize(correlationId) +
|
|
175
|
-
(envelope.signature ? 1 : 0) +
|
|
176
|
-
encodedStringSize(signatureKeyId) +
|
|
177
|
-
encodedStringSize(signatureValue)
|
|
178
|
-
|
|
179
|
-
const writer = new BinaryWriter(size)
|
|
180
|
-
writer.writeBytes(MAGIC_BYTES)
|
|
181
|
-
writer.writeU8(FORMAT_VERSION)
|
|
182
|
-
writer.writeU8(flags)
|
|
183
|
-
writer.writeU64(envelope.header.timestampMs)
|
|
184
|
-
writer.writeU32(envelope.header.ttlMs)
|
|
185
|
-
|
|
186
|
-
writer.writeString(version)
|
|
187
|
-
writer.writeString(messageId)
|
|
188
|
-
writer.writeString(sender)
|
|
189
|
-
writer.writeString(nonce)
|
|
190
|
-
writer.writeString(intent)
|
|
191
|
-
writer.writeString(content)
|
|
192
|
-
|
|
193
|
-
if (recipient) writer.writeString(recipient)
|
|
194
|
-
if (capability) writer.writeString(capability)
|
|
195
|
-
if (correlationId) writer.writeString(correlationId)
|
|
196
|
-
|
|
197
|
-
if (envelope.signature && signatureKeyId && signatureValue) {
|
|
198
|
-
writer.writeU8(signatureAlgCode(envelope.signature.alg))
|
|
199
|
-
writer.writeString(signatureKeyId)
|
|
200
|
-
writer.writeString(signatureValue)
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
return writer.finish()
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
export function decodeEnvelopeBinary(payload: Uint8Array, options: BinaryDecodeOptions = {}): ReceiveEnvelopeResult {
|
|
207
|
-
try {
|
|
208
|
-
const maxFrameBytes = options.maxFrameBytes ?? DEFAULT_MAX_BINARY_ENVELOPE_BYTES
|
|
209
|
-
if (payload.byteLength > maxFrameBytes) {
|
|
210
|
-
return {
|
|
211
|
-
ok: false,
|
|
212
|
-
diagnostics: [{ level: 'error', message: `Binary envelope exceeds maximum frame size '${maxFrameBytes}'` }],
|
|
213
|
-
envelope: null,
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
const reader = new BinaryReader(payload)
|
|
218
|
-
const magic = [reader.readU8(), reader.readU8(), reader.readU8(), reader.readU8()]
|
|
219
|
-
if (magic.some((value, idx) => value !== MAGIC[idx])) {
|
|
220
|
-
return {
|
|
221
|
-
ok: false,
|
|
222
|
-
diagnostics: [{ level: 'error', message: 'Invalid binary envelope magic' }],
|
|
223
|
-
envelope: null,
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
const version = reader.readU8()
|
|
228
|
-
if (version !== FORMAT_VERSION) {
|
|
229
|
-
return {
|
|
230
|
-
ok: false,
|
|
231
|
-
diagnostics: [{ level: 'error', message: `Unsupported binary envelope version '${version}'` }],
|
|
232
|
-
envelope: null,
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
const flags = reader.readU8()
|
|
237
|
-
const timestampMs = reader.readU64()
|
|
238
|
-
const ttlMs = reader.readU32()
|
|
239
|
-
|
|
240
|
-
const envelope: ProtocolEnvelope = {
|
|
241
|
-
header: {
|
|
242
|
-
version: reader.readString() as '7h3/0.1',
|
|
243
|
-
messageId: reader.readString(),
|
|
244
|
-
timestampMs,
|
|
245
|
-
ttlMs,
|
|
246
|
-
sender: reader.readString(),
|
|
247
|
-
nonce: reader.readString(),
|
|
248
|
-
},
|
|
249
|
-
body: {
|
|
250
|
-
intent: reader.readString() as ProtocolEnvelope['body']['intent'],
|
|
251
|
-
content: reader.readString(),
|
|
252
|
-
},
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
if (envelope.header.version !== '7h3/0.1') {
|
|
256
|
-
return {
|
|
257
|
-
ok: false,
|
|
258
|
-
diagnostics: [{ level: 'error', message: `Unsupported protocol version '${envelope.header.version}'` }],
|
|
259
|
-
envelope: null,
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
if (!INTENT_KINDS.has(envelope.body.intent)) {
|
|
264
|
-
return {
|
|
265
|
-
ok: false,
|
|
266
|
-
diagnostics: [{ level: 'error', message: `Unsupported intent '${envelope.body.intent}'` }],
|
|
267
|
-
envelope: null,
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
if (flags & FLAG_RECIPIENT) envelope.header.recipient = reader.readString()
|
|
272
|
-
if (flags & FLAG_CAPABILITY) envelope.body.capability = reader.readString()
|
|
273
|
-
if (flags & FLAG_CORRELATION) envelope.body.correlationId = reader.readString()
|
|
274
|
-
|
|
275
|
-
if (flags & FLAG_SIGNATURE) {
|
|
276
|
-
const algCode = reader.readU8()
|
|
277
|
-
const alg = signatureAlgFromCode(algCode)
|
|
278
|
-
if (!alg) {
|
|
279
|
-
return {
|
|
280
|
-
ok: false,
|
|
281
|
-
diagnostics: [{ level: 'error', message: `Unsupported binary signature algorithm '${algCode}'` }],
|
|
282
|
-
envelope: null,
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
envelope.signature = {
|
|
286
|
-
alg,
|
|
287
|
-
keyId: reader.readString(),
|
|
288
|
-
value: reader.readString(),
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
if (reader.remaining() !== 0) {
|
|
293
|
-
return {
|
|
294
|
-
ok: false,
|
|
295
|
-
diagnostics: [{ level: 'error', message: 'Trailing bytes found in binary envelope' }],
|
|
296
|
-
envelope: null,
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
return {
|
|
301
|
-
ok: true,
|
|
302
|
-
diagnostics: [],
|
|
303
|
-
envelope,
|
|
304
|
-
}
|
|
305
|
-
} catch (error: unknown) {
|
|
306
|
-
return {
|
|
307
|
-
ok: false,
|
|
308
|
-
diagnostics: [{ level: 'error', message: error instanceof Error ? error.message : 'Invalid binary envelope' }],
|
|
309
|
-
envelope: null,
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
}
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import type { WireFormat } from './protocolTransport'
|
|
2
|
-
|
|
3
|
-
export type AckMode = 'fast' | 'receipt'
|
|
4
|
-
|
|
5
|
-
export interface AipCapabilities {
|
|
6
|
-
agent: string
|
|
7
|
-
capabilities: string[]
|
|
8
|
-
wireFormats: WireFormat[]
|
|
9
|
-
batchMax: number
|
|
10
|
-
ackModes: AckMode[]
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export interface NegotiatedAipCapabilities {
|
|
14
|
-
wireFormat: WireFormat
|
|
15
|
-
batchMax: number
|
|
16
|
-
ackMode: AckMode
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const WIRE_FORMAT_ORDER: WireFormat[] = ['binary', 'compact', 'json']
|
|
20
|
-
const ACK_MODE_ORDER: AckMode[] = ['fast', 'receipt']
|
|
21
|
-
|
|
22
|
-
export function createAipCapabilities(input: {
|
|
23
|
-
agent: string
|
|
24
|
-
capabilities?: string[]
|
|
25
|
-
wireFormats?: WireFormat[]
|
|
26
|
-
batchMax?: number
|
|
27
|
-
ackModes?: AckMode[]
|
|
28
|
-
}): AipCapabilities {
|
|
29
|
-
return {
|
|
30
|
-
agent: input.agent,
|
|
31
|
-
capabilities: input.capabilities ?? [],
|
|
32
|
-
wireFormats: input.wireFormats ?? ['compact', 'json'],
|
|
33
|
-
batchMax: input.batchMax ?? 1,
|
|
34
|
-
ackModes: input.ackModes ?? ['receipt'],
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export function encodeAipCapabilities(capabilities: AipCapabilities): string {
|
|
39
|
-
return JSON.stringify(capabilities)
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export function parseAipCapabilities(raw: string): AipCapabilities | null {
|
|
43
|
-
try {
|
|
44
|
-
const parsed = JSON.parse(raw) as Partial<AipCapabilities>
|
|
45
|
-
if (typeof parsed.agent !== 'string') return null
|
|
46
|
-
if (!Array.isArray(parsed.capabilities)) return null
|
|
47
|
-
if (!Array.isArray(parsed.wireFormats)) return null
|
|
48
|
-
if (!Array.isArray(parsed.ackModes)) return null
|
|
49
|
-
if (typeof parsed.batchMax !== 'number' || parsed.batchMax < 1) return null
|
|
50
|
-
return {
|
|
51
|
-
agent: parsed.agent,
|
|
52
|
-
capabilities: parsed.capabilities.filter((item): item is string => typeof item === 'string'),
|
|
53
|
-
wireFormats: parsed.wireFormats.filter((item): item is WireFormat => item === 'json' || item === 'compact' || item === 'binary'),
|
|
54
|
-
batchMax: Math.floor(parsed.batchMax),
|
|
55
|
-
ackModes: parsed.ackModes.filter((item): item is AckMode => item === 'fast' || item === 'receipt'),
|
|
56
|
-
}
|
|
57
|
-
} catch {
|
|
58
|
-
return null
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export function negotiateAipCapabilities(local: AipCapabilities, remote: AipCapabilities): NegotiatedAipCapabilities {
|
|
63
|
-
const wireFormat = WIRE_FORMAT_ORDER.find((format) => local.wireFormats.includes(format) && remote.wireFormats.includes(format)) ?? 'compact'
|
|
64
|
-
const ackMode = ACK_MODE_ORDER.find((mode) => local.ackModes.includes(mode) && remote.ackModes.includes(mode)) ?? 'receipt'
|
|
65
|
-
return {
|
|
66
|
-
wireFormat,
|
|
67
|
-
batchMax: Math.max(1, Math.min(local.batchMax, remote.batchMax)),
|
|
68
|
-
ackMode,
|
|
69
|
-
}
|
|
70
|
-
}
|