aap-agent-client 3.0.0 → 3.1.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/index.js +21 -48
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @aap/client v3.
|
|
2
|
+
* @aap/client v3.1.0
|
|
3
3
|
*
|
|
4
4
|
* WebSocket client for Agent Attestation Protocol.
|
|
5
|
-
*
|
|
5
|
+
* Batch mode: receive all challenges, solve, submit.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import WebSocket from 'ws';
|
|
9
9
|
|
|
10
|
-
export const PROTOCOL_VERSION = '3.
|
|
10
|
+
export const PROTOCOL_VERSION = '3.1.0';
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* AAP WebSocket Client
|
|
@@ -21,7 +21,7 @@ export class AAPClient {
|
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
23
|
* Connect and verify
|
|
24
|
-
* @param {Function} [solver] - async (
|
|
24
|
+
* @param {Function} [solver] - async (challenges) => answers[]
|
|
25
25
|
* @returns {Promise<Object>} Verification result
|
|
26
26
|
*/
|
|
27
27
|
async verify(solver) {
|
|
@@ -31,9 +31,7 @@ export class AAPClient {
|
|
|
31
31
|
const ws = new WebSocket(this.serverUrl);
|
|
32
32
|
let result = null;
|
|
33
33
|
|
|
34
|
-
ws.on('open', () => {
|
|
35
|
-
// Wait for handshake
|
|
36
|
-
});
|
|
34
|
+
ws.on('open', () => {});
|
|
37
35
|
|
|
38
36
|
ws.on('message', async (data) => {
|
|
39
37
|
try {
|
|
@@ -41,36 +39,26 @@ export class AAPClient {
|
|
|
41
39
|
|
|
42
40
|
switch (msg.type) {
|
|
43
41
|
case 'handshake':
|
|
44
|
-
// Send ready
|
|
45
42
|
ws.send(JSON.stringify({
|
|
46
43
|
type: 'ready',
|
|
47
44
|
publicId: this.publicId
|
|
48
45
|
}));
|
|
49
46
|
break;
|
|
50
47
|
|
|
51
|
-
case '
|
|
48
|
+
case 'challenges':
|
|
52
49
|
if (!solve) {
|
|
53
|
-
|
|
54
|
-
ws.send(JSON.stringify({ type: 'answer', answer: {} }));
|
|
50
|
+
ws.send(JSON.stringify({ type: 'answers', answers: [] }));
|
|
55
51
|
break;
|
|
56
52
|
}
|
|
57
53
|
|
|
58
54
|
try {
|
|
59
|
-
const
|
|
60
|
-
ws.send(JSON.stringify({ type: '
|
|
55
|
+
const answers = await solve(msg.challenges);
|
|
56
|
+
ws.send(JSON.stringify({ type: 'answers', answers }));
|
|
61
57
|
} catch (e) {
|
|
62
|
-
ws.send(JSON.stringify({ type: '
|
|
58
|
+
ws.send(JSON.stringify({ type: 'answers', answers: [] }));
|
|
63
59
|
}
|
|
64
60
|
break;
|
|
65
61
|
|
|
66
|
-
case 'ack':
|
|
67
|
-
// Challenge acknowledged, waiting for next
|
|
68
|
-
break;
|
|
69
|
-
|
|
70
|
-
case 'timeout':
|
|
71
|
-
// Too slow
|
|
72
|
-
break;
|
|
73
|
-
|
|
74
62
|
case 'result':
|
|
75
63
|
result = msg;
|
|
76
64
|
break;
|
|
@@ -87,16 +75,11 @@ export class AAPClient {
|
|
|
87
75
|
});
|
|
88
76
|
|
|
89
77
|
ws.on('close', () => {
|
|
90
|
-
if (result)
|
|
91
|
-
|
|
92
|
-
} else {
|
|
93
|
-
reject(new Error('Connection closed without result'));
|
|
94
|
-
}
|
|
78
|
+
if (result) resolve(result);
|
|
79
|
+
else reject(new Error('Connection closed without result'));
|
|
95
80
|
});
|
|
96
81
|
|
|
97
|
-
ws.on('error',
|
|
98
|
-
reject(err);
|
|
99
|
-
});
|
|
82
|
+
ws.on('error', reject);
|
|
100
83
|
});
|
|
101
84
|
}
|
|
102
85
|
}
|
|
@@ -104,41 +87,31 @@ export class AAPClient {
|
|
|
104
87
|
/**
|
|
105
88
|
* Create a solver function from an LLM callback
|
|
106
89
|
* @param {Function} llm - async (prompt) => responseString
|
|
107
|
-
* @returns {Function} Solver function
|
|
90
|
+
* @returns {Function} Solver function
|
|
108
91
|
*/
|
|
109
92
|
export function createSolver(llm) {
|
|
110
|
-
return async (
|
|
111
|
-
const prompt = `Solve
|
|
93
|
+
return async (challenges) => {
|
|
94
|
+
const prompt = `Solve ALL these challenges. Return a JSON array of answers in order.
|
|
95
|
+
|
|
96
|
+
${challenges.map((c, i) => `[${i}] ${c.challenge}`).join('\n\n')}
|
|
112
97
|
|
|
113
|
-
|
|
98
|
+
Respond with ONLY a JSON array like: [{...}, {...}, ...]`;
|
|
114
99
|
|
|
115
100
|
const response = await llm(prompt);
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
const match = response.match(/\{[\s\S]*?\}/);
|
|
119
|
-
if (!match) {
|
|
120
|
-
throw new Error('No JSON found in response');
|
|
121
|
-
}
|
|
122
|
-
|
|
101
|
+
const match = response.match(/\[[\s\S]*\]/);
|
|
102
|
+
if (!match) throw new Error('No JSON array found');
|
|
123
103
|
return JSON.parse(match[0]);
|
|
124
104
|
};
|
|
125
105
|
}
|
|
126
106
|
|
|
127
107
|
/**
|
|
128
108
|
* Quick verify helper
|
|
129
|
-
* @param {string} serverUrl - WebSocket URL
|
|
130
|
-
* @param {Function} [solver] - Solver function
|
|
131
|
-
* @param {string} [publicId] - Public ID
|
|
132
|
-
* @returns {Promise<Object>} Verification result
|
|
133
109
|
*/
|
|
134
110
|
export async function verify(serverUrl, solver, publicId) {
|
|
135
111
|
const client = new AAPClient({ serverUrl, solver, publicId });
|
|
136
112
|
return client.verify();
|
|
137
113
|
}
|
|
138
114
|
|
|
139
|
-
/**
|
|
140
|
-
* Create client instance
|
|
141
|
-
*/
|
|
142
115
|
export function createClient(options) {
|
|
143
116
|
return new AAPClient(options);
|
|
144
117
|
}
|