-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathsettings.ts
More file actions
342 lines (303 loc) · 10.4 KB
/
Copy pathsettings.ts
File metadata and controls
342 lines (303 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/env bun
/**
* Settings skill CLI
* Manages AIBTC configuration stored at ~/.aibtc/config.json
*
* Usage: bun run settings/settings.ts <subcommand> [options]
*/
import { Command } from "commander";
import { createRequire } from "module";
import {
getHiroApiKey,
setHiroApiKey,
clearHiroApiKey,
getStacksApiUrl,
setStacksApiUrl,
clearStacksApiUrl,
initializeStorage,
} from "../src/lib/utils/storage.js";
import { getApiBaseUrl, NETWORK } from "../src/lib/config/networks.js";
import { getSponsorRelayUrl } from "../src/lib/config/sponsor.js";
import { checkRelayHealth } from "../src/lib/services/relay-health.service.js";
import { printJson, handleError } from "../src/lib/utils/cli.js";
const require = createRequire(import.meta.url);
// ---------------------------------------------------------------------------
// Program
// ---------------------------------------------------------------------------
const program = new Command();
program
.name("settings")
.description("Manage AIBTC configuration stored at ~/.aibtc/config.json")
.version("0.1.0");
// ---------------------------------------------------------------------------
// set-hiro-api-key
// ---------------------------------------------------------------------------
program
.command("set-hiro-api-key")
.description(
"Save a Hiro API key to ~/.aibtc/config.json for authenticated Hiro API requests"
)
.requiredOption("--api-key <apiKey>", "Your Hiro API key (sensitive)")
.action(async (opts: { apiKey: string }) => {
try {
await initializeStorage();
await setHiroApiKey(opts.apiKey);
const masked =
opts.apiKey.length > 8
? `${opts.apiKey.slice(0, 4)}...${opts.apiKey.slice(-4)}`
: "****";
printJson({
success: true,
message:
"Hiro API key saved. All subsequent Hiro API requests will use this key.",
maskedKey: masked,
storedIn: "~/.aibtc/config.json",
});
} catch (error) {
handleError(error);
}
});
// ---------------------------------------------------------------------------
// get-hiro-api-key
// ---------------------------------------------------------------------------
program
.command("get-hiro-api-key")
.description(
"Check whether a Hiro API key is configured, and show its source and masked preview"
)
.action(async () => {
try {
await initializeStorage();
const storedKey = await getHiroApiKey();
const envKey = process.env.HIRO_API_KEY || "";
const activeKey = storedKey || envKey;
const source = storedKey
? "~/.aibtc/config.json"
: envKey
? "HIRO_API_KEY environment variable"
: "none";
const masked =
activeKey.length > 8
? `${activeKey.slice(0, 4)}...${activeKey.slice(-4)}`
: activeKey
? "****"
: "";
printJson({
configured: !!activeKey,
source,
maskedKey: masked || "(not set)",
hint: activeKey
? "API key is active. Hiro API requests use authenticated rate limits."
: "No API key configured. Using public rate limits. Get a key at https://platform.hiro.so/",
});
} catch (error) {
handleError(error);
}
});
// ---------------------------------------------------------------------------
// delete-hiro-api-key
// ---------------------------------------------------------------------------
program
.command("delete-hiro-api-key")
.description(
"Remove the stored Hiro API key from ~/.aibtc/config.json"
)
.action(async () => {
try {
await initializeStorage();
const hadKey = !!(await getHiroApiKey());
await clearHiroApiKey();
const envFallback = !!process.env.HIRO_API_KEY;
printJson({
success: true,
message: hadKey
? "Hiro API key removed from ~/.aibtc/config.json."
: "No stored Hiro API key to remove.",
envFallbackActive: envFallback,
hint: envFallback
? "HIRO_API_KEY environment variable is still set and will be used."
: "No API key configured. Requests will use public rate limits.",
});
} catch (error) {
handleError(error);
}
});
// ---------------------------------------------------------------------------
// set-stacks-api-url
// ---------------------------------------------------------------------------
program
.command("set-stacks-api-url")
.description(
"Point all Stacks API requests at a custom node instead of the default Hiro API"
)
.requiredOption(
"--url <url>",
"Base URL of your Stacks API node (e.g. http://localhost:3999)"
)
.action(async (opts: { url: string }) => {
try {
await initializeStorage();
const cleanUrl = opts.url.replace(/\/+$/, "");
await setStacksApiUrl(cleanUrl);
printJson({
success: true,
message:
"Custom Stacks API URL saved. All subsequent Stacks API requests will use this node.",
url: cleanUrl,
storedIn: "~/.aibtc/config.json",
tip: "Use get-stacks-api-url to verify, or delete-stacks-api-url to revert to the default Hiro API.",
});
} catch (error) {
handleError(error);
}
});
// ---------------------------------------------------------------------------
// get-stacks-api-url
// ---------------------------------------------------------------------------
program
.command("get-stacks-api-url")
.description(
"Show the current Stacks API URL being used for blockchain queries"
)
.action(async () => {
try {
await initializeStorage();
const customUrl = await getStacksApiUrl();
const defaultUrl = getApiBaseUrl(NETWORK);
printJson({
activeUrl: customUrl || defaultUrl,
isCustom: !!customUrl,
source: customUrl ? "~/.aibtc/config.json" : "default (Hiro API)",
defaultUrl,
network: NETWORK,
hint: customUrl
? "Using custom Stacks API node. Use delete-stacks-api-url to revert to the default Hiro API."
: "Using default Hiro API. Use set-stacks-api-url to point to your own node.",
});
} catch (error) {
handleError(error);
}
});
// ---------------------------------------------------------------------------
// delete-stacks-api-url
// ---------------------------------------------------------------------------
program
.command("delete-stacks-api-url")
.description(
"Remove the custom Stacks API URL and revert to the default Hiro API"
)
.action(async () => {
try {
await initializeStorage();
const hadUrl = !!(await getStacksApiUrl());
await clearStacksApiUrl();
const defaultUrl = getApiBaseUrl(NETWORK);
printJson({
success: true,
message: hadUrl
? `Custom Stacks API URL removed. Reverted to default: ${defaultUrl}`
: "No custom Stacks API URL was set.",
activeUrl: defaultUrl,
network: NETWORK,
});
} catch (error) {
handleError(error);
}
});
// ---------------------------------------------------------------------------
// get-server-version
// ---------------------------------------------------------------------------
program
.command("get-server-version")
.description(
"Check the currently installed package version and compare with the latest on npm"
)
.action(async () => {
try {
// Read current version from package.json
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const packageJson = require("../package.json");
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const currentVersion = packageJson.version as string;
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const packageName = (packageJson.name as string) || "@aibtc/skills";
let latestVersion = "unknown";
try {
const response = await fetch(
`https://registry.npmjs.org/${packageName}/latest`
);
if (response.ok) {
const data = (await response.json()) as { version: string };
latestVersion = data.version;
}
} catch {
// Network error or npm registry unavailable â non-fatal
}
const compare = (a: string, b: string): number => {
const pa = a.split(".").map(Number);
const pb = b.split(".").map(Number);
for (let i = 0; i < Math.max(pa.length, pb.length); i++) {
const na = pa[i] ?? 0;
const nb = pb[i] ?? 0;
if (na !== nb) return na - nb;
}
return 0;
};
const fetched = latestVersion !== "unknown";
const updateAvailable = fetched && compare(currentVersion, latestVersion) < 0;
const isLatest = fetched && compare(currentVersion, latestVersion) >= 0;
printJson({
currentVersion,
latestVersion,
isLatest,
updateAvailable,
package: packageName,
hint: updateAvailable
? "Update available! Install the latest version to get new features."
: fetched
? "Running the latest version."
: "Unable to verify latest version (network error).",
});
} catch (error) {
handleError(error);
}
});
// ---------------------------------------------------------------------------
// check-relay-health
// ---------------------------------------------------------------------------
program
.command("check-relay-health")
.description(
"Check x402 relay health, pool state, and sponsor nonce diagnostics"
)
.option(
"--relay-url <url>",
"Base URL of the sponsor relay",
getSponsorRelayUrl(NETWORK)
)
.option(
"--sponsor-address <address>",
"Optional STX address of a specific sponsor wallet to inspect"
)
.action(
async (opts: { relayUrl: string; sponsorAddress?: string }) => {
try {
const status = await checkRelayHealth(NETWORK, {
relayUrl: opts.relayUrl,
sponsorAddress: opts.sponsorAddress,
});
printJson({
...status,
hint: status.healthy
? "Relay pool and sponsor diagnostics look healthy."
: "Issues detected inspect pool, sponsor, and stuck transaction details.",
});
} catch (error) {
handleError(error);
}
}
);
// ---------------------------------------------------------------------------
// Parse
// ---------------------------------------------------------------------------
program.parse(process.argv);