@highway1/cli 0.1.19 → 0.1.20
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/dist/index.js +31 -84
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/commands/join.ts +32 -89
package/package.json
CHANGED
package/src/commands/join.ts
CHANGED
|
@@ -16,28 +16,19 @@ export function registerJoinCommand(program: Command): void {
|
|
|
16
16
|
.command('join')
|
|
17
17
|
.description('Join the Clawiverse network')
|
|
18
18
|
.option('--bootstrap <peers...>', 'Bootstrap peer addresses')
|
|
19
|
-
.option('--json', 'Output messages as JSON (for agent automation)')
|
|
20
19
|
.action(async (options) => {
|
|
21
20
|
try {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
if (!jsonMode) {
|
|
25
|
-
printHeader('Join Clawiverse Network');
|
|
26
|
-
}
|
|
21
|
+
printHeader('Join Clawiverse Network');
|
|
27
22
|
|
|
28
23
|
const identity = getIdentity();
|
|
29
24
|
const card = getAgentCard();
|
|
30
25
|
|
|
31
26
|
if (!identity || !card) {
|
|
32
|
-
|
|
33
|
-
console.log(JSON.stringify({ error: 'No identity found. Run "hw1 init" first.' }));
|
|
34
|
-
} else {
|
|
35
|
-
error('No identity found. Run "hw1 init" first.');
|
|
36
|
-
}
|
|
27
|
+
error('No identity found. Run "hw1 init" first.');
|
|
37
28
|
process.exit(1);
|
|
38
29
|
}
|
|
39
30
|
|
|
40
|
-
const spin =
|
|
31
|
+
const spin = spinner('Starting libp2p node...');
|
|
41
32
|
|
|
42
33
|
const keyPair = importKeyPair({
|
|
43
34
|
publicKey: identity.publicKey,
|
|
@@ -54,22 +45,14 @@ export function registerJoinCommand(program: Command): void {
|
|
|
54
45
|
|
|
55
46
|
await node.start();
|
|
56
47
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
} else {
|
|
63
|
-
console.log(JSON.stringify({
|
|
64
|
-
event: 'node_started',
|
|
65
|
-
peerId: node.getPeerId(),
|
|
66
|
-
did: identity.did,
|
|
67
|
-
multiaddrs: node.getMultiaddrs(),
|
|
68
|
-
}));
|
|
69
|
-
}
|
|
48
|
+
spin.succeed('Node started successfully!');
|
|
49
|
+
|
|
50
|
+
info(`Peer ID: ${node.getPeerId()}`);
|
|
51
|
+
info(`DID: ${identity.did}`);
|
|
52
|
+
info(`Listening on: ${node.getMultiaddrs().join(', ')}`);
|
|
70
53
|
|
|
71
54
|
// Wait for bootstrap peer connection before publishing to DHT
|
|
72
|
-
const connectSpin =
|
|
55
|
+
const connectSpin = spinner('Connecting to bootstrap peers...');
|
|
73
56
|
await new Promise<void>((resolve) => {
|
|
74
57
|
const timeout = setTimeout(resolve, 10000);
|
|
75
58
|
node.libp2p.addEventListener('peer:connect', () => {
|
|
@@ -77,12 +60,10 @@ export function registerJoinCommand(program: Command): void {
|
|
|
77
60
|
setTimeout(resolve, 500);
|
|
78
61
|
}, { once: true });
|
|
79
62
|
});
|
|
80
|
-
|
|
81
|
-
connectSpin.succeed('Connected to network!');
|
|
82
|
-
}
|
|
63
|
+
connectSpin.succeed('Connected to network!');
|
|
83
64
|
|
|
84
65
|
// Publish Agent Card with peerId and multiaddrs so others can dial us
|
|
85
|
-
const cardSpin =
|
|
66
|
+
const cardSpin = spinner('Publishing Agent Card to DHT...');
|
|
86
67
|
|
|
87
68
|
// Include circuit relay addresses so nodes behind NAT can be reached
|
|
88
69
|
const directAddrs = node.getMultiaddrs();
|
|
@@ -108,11 +89,7 @@ export function registerJoinCommand(program: Command): void {
|
|
|
108
89
|
const dht = createDHTOperations(node.libp2p);
|
|
109
90
|
await dht.publishAgentCard(signedCard);
|
|
110
91
|
|
|
111
|
-
|
|
112
|
-
cardSpin.succeed('Agent Card published!');
|
|
113
|
-
} else {
|
|
114
|
-
console.log(JSON.stringify({ event: 'agent_card_published' }));
|
|
115
|
-
}
|
|
92
|
+
cardSpin.succeed('Agent Card published!');
|
|
116
93
|
|
|
117
94
|
// Register message handlers for incoming messages
|
|
118
95
|
const router = createMessageRouter(
|
|
@@ -124,34 +101,17 @@ export function registerJoinCommand(program: Command): void {
|
|
|
124
101
|
// Generic message handler that accepts any protocol
|
|
125
102
|
const messageHandler = async (envelope: any) => {
|
|
126
103
|
const payload = envelope.payload as Record<string, unknown>;
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
to: envelope.to,
|
|
135
|
-
protocol: envelope.protocol,
|
|
136
|
-
type: envelope.type,
|
|
137
|
-
payload,
|
|
138
|
-
timestamp: Date.now(),
|
|
139
|
-
}));
|
|
140
|
-
} else {
|
|
141
|
-
console.log();
|
|
142
|
-
success(`>>> Received message from ${envelope.from}`);
|
|
143
|
-
info(` Message ID: ${envelope.id}`);
|
|
144
|
-
info(` Protocol: ${envelope.protocol}`);
|
|
145
|
-
info(` Type: ${envelope.type}`);
|
|
146
|
-
info(` Payload: ${JSON.stringify(payload, null, 2)}`);
|
|
147
|
-
console.log();
|
|
148
|
-
}
|
|
104
|
+
console.log();
|
|
105
|
+
success(`>>> Received message from ${envelope.from}`);
|
|
106
|
+
info(` Message ID: ${envelope.id}`);
|
|
107
|
+
info(` Protocol: ${envelope.protocol}`);
|
|
108
|
+
info(` Type: ${envelope.type}`);
|
|
109
|
+
info(` Payload: ${JSON.stringify(payload, null, 2)}`);
|
|
110
|
+
console.log();
|
|
149
111
|
|
|
150
112
|
// If this is a request, send back a simple acknowledgment response
|
|
151
113
|
if (envelope.type === 'request') {
|
|
152
|
-
|
|
153
|
-
info(' Sending acknowledgment response...');
|
|
154
|
-
}
|
|
114
|
+
info(' Sending acknowledgment response...');
|
|
155
115
|
|
|
156
116
|
const { createEnvelope, signEnvelope, sign } = await import('@highway1/core');
|
|
157
117
|
const identity = getIdentity();
|
|
@@ -193,40 +153,23 @@ export function registerJoinCommand(program: Command): void {
|
|
|
193
153
|
|
|
194
154
|
await router.start();
|
|
195
155
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
info('Press Ctrl+C to stop');
|
|
201
|
-
} else {
|
|
202
|
-
console.log(JSON.stringify({ event: 'ready', status: 'listening' }));
|
|
203
|
-
}
|
|
156
|
+
console.log();
|
|
157
|
+
success('Successfully joined the Clawiverse network!');
|
|
158
|
+
info('Listening for incoming messages...');
|
|
159
|
+
info('Press Ctrl+C to stop');
|
|
204
160
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
stopSpin.succeed('Node stopped');
|
|
212
|
-
} else {
|
|
213
|
-
await router.stop();
|
|
214
|
-
await node.stop();
|
|
215
|
-
console.log(JSON.stringify({ event: 'shutdown', status: 'stopped' }));
|
|
216
|
-
}
|
|
161
|
+
process.on('SIGINT', async () => {
|
|
162
|
+
console.log();
|
|
163
|
+
const stopSpin = spinner('Stopping node...');
|
|
164
|
+
await router.stop();
|
|
165
|
+
await node.stop();
|
|
166
|
+
stopSpin.succeed('Node stopped');
|
|
217
167
|
process.exit(0);
|
|
218
|
-
};
|
|
219
|
-
|
|
220
|
-
process.on('SIGINT', shutdown);
|
|
221
|
-
process.on('SIGTERM', shutdown);
|
|
168
|
+
});
|
|
222
169
|
|
|
223
170
|
await new Promise(() => {});
|
|
224
171
|
} catch (err) {
|
|
225
|
-
|
|
226
|
-
console.log(JSON.stringify({ error: (err as Error).message }));
|
|
227
|
-
} else {
|
|
228
|
-
error(`Failed to join network: ${(err as Error).message}`);
|
|
229
|
-
}
|
|
172
|
+
error(`Failed to join network: ${(err as Error).message}`);
|
|
230
173
|
process.exit(1);
|
|
231
174
|
}
|
|
232
175
|
});
|