nia-wizard 0.1.17 → 0.1.18
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/bin.js +1 -1
- package/dist/{chunk-XF23S5X2.js → chunk-KAGHMAER.js} +42 -26
- package/dist/chunk-KAGHMAER.js.map +1 -0
- package/dist/index.js +1 -1
- package/package.json +11 -10
- package/dist/chunk-XF23S5X2.js.map +0 -1
package/dist/bin.js
CHANGED
|
@@ -15,13 +15,15 @@ function debug(...args) {
|
|
|
15
15
|
var BACKEND_URL = process.env.NIA_BACKEND_URL || "https://apigcp.trynia.ai";
|
|
16
16
|
var APP_URL = process.env.NIA_APP_URL || "";
|
|
17
17
|
var SESSION_TTL_MS = 15 * 60 * 1e3;
|
|
18
|
+
var FETCH_TIMEOUT_MS = 1e4;
|
|
18
19
|
async function startDeviceSession() {
|
|
19
20
|
debug("Starting device session...");
|
|
20
21
|
const response = await fetch(`${BACKEND_URL}/public/mcp-device/start`, {
|
|
21
22
|
method: "POST",
|
|
22
23
|
headers: {
|
|
23
24
|
"Content-Type": "application/json"
|
|
24
|
-
}
|
|
25
|
+
},
|
|
26
|
+
signal: AbortSignal.timeout(FETCH_TIMEOUT_MS)
|
|
25
27
|
});
|
|
26
28
|
if (!response.ok) {
|
|
27
29
|
const errorText = await response.text().catch(() => "Unknown error");
|
|
@@ -62,7 +64,8 @@ async function exchangeForApiKey(session) {
|
|
|
62
64
|
body: JSON.stringify({
|
|
63
65
|
authorization_session_id: session.authorization_session_id,
|
|
64
66
|
user_code: session.user_code
|
|
65
|
-
})
|
|
67
|
+
}),
|
|
68
|
+
signal: AbortSignal.timeout(FETCH_TIMEOUT_MS)
|
|
66
69
|
});
|
|
67
70
|
if (!response.ok) {
|
|
68
71
|
const errorData = await response.json().catch(() => ({ detail: "Unknown error" }));
|
|
@@ -376,68 +379,81 @@ async function runDeviceFlow() {
|
|
|
376
379
|
console.log("");
|
|
377
380
|
clack_default.log.step(chalk.yellow("Complete these steps in your browser:"));
|
|
378
381
|
console.log(" 1. Sign in or create an account");
|
|
379
|
-
console.log(" 2.
|
|
382
|
+
console.log(" 2. Complete any setup steps shown");
|
|
383
|
+
console.log("");
|
|
384
|
+
clack_default.log.message(chalk.dim("The CLI will detect when you're done and continue automatically."));
|
|
380
385
|
console.log("");
|
|
381
386
|
return await waitForAuthorizationAndExchange(session);
|
|
382
387
|
}
|
|
388
|
+
function sleep(ms) {
|
|
389
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
390
|
+
}
|
|
383
391
|
async function waitForAuthorizationAndExchange(session) {
|
|
392
|
+
const spinner = clack_default.spinner();
|
|
393
|
+
spinner.start("Waiting for browser authorization...");
|
|
394
|
+
const POLL_INTERVAL_MS = 2e3;
|
|
395
|
+
const MAX_NETWORK_ERRORS = 5;
|
|
396
|
+
let consecutiveNetworkErrors = 0;
|
|
384
397
|
while (true) {
|
|
385
398
|
if (!isSessionValid(session)) {
|
|
399
|
+
spinner.stop("Session expired");
|
|
386
400
|
clack_default.log.error("Session has expired. Please start over.");
|
|
387
401
|
return abort("Session expired", 1);
|
|
388
402
|
}
|
|
389
|
-
const shouldContinue = await abortIfCancelled(
|
|
390
|
-
clack_default.confirm({
|
|
391
|
-
message: "Press Enter once you've signed in (or N to enter key manually)",
|
|
392
|
-
initialValue: true
|
|
393
|
-
})
|
|
394
|
-
);
|
|
395
|
-
if (!shouldContinue) {
|
|
396
|
-
return await promptForManualApiKey();
|
|
397
|
-
}
|
|
398
|
-
const spinner = clack_default.spinner();
|
|
399
|
-
spinner.start("Checking authorization...");
|
|
400
403
|
try {
|
|
401
404
|
const apiKey = await exchangeForApiKey(session);
|
|
402
405
|
spinner.stop(chalk.green("\u2713 Authorized!"));
|
|
403
406
|
clack_default.log.success("API key obtained successfully!");
|
|
404
407
|
return apiKey;
|
|
405
408
|
} catch (error) {
|
|
406
|
-
spinner.stop("Not ready yet");
|
|
407
409
|
if (isDeviceFlowError(error)) {
|
|
408
410
|
switch (error.type) {
|
|
409
411
|
case "not_ready":
|
|
410
|
-
|
|
411
|
-
console.log("");
|
|
412
|
-
clack_default.log.message(
|
|
413
|
-
chalk.dim("Make sure you have:\n") + " \u2022 Signed in to your Nia account\n \u2022 Completed any setup steps in the browser"
|
|
414
|
-
);
|
|
415
|
-
console.log("");
|
|
412
|
+
consecutiveNetworkErrors = 0;
|
|
416
413
|
break;
|
|
417
414
|
case "expired":
|
|
415
|
+
spinner.stop("Session expired");
|
|
418
416
|
clack_default.log.error("Session has expired.");
|
|
419
417
|
clack_default.log.info("Please run the wizard again to start a new session.");
|
|
420
418
|
return abort("Session expired", 1);
|
|
421
419
|
case "consumed":
|
|
420
|
+
spinner.stop("Session already used");
|
|
422
421
|
clack_default.log.error("This session was already used.");
|
|
423
422
|
clack_default.log.info("Please run the wizard again to start a new session.");
|
|
424
423
|
return abort("Session already used", 1);
|
|
425
424
|
case "invalid":
|
|
425
|
+
spinner.stop("Invalid session");
|
|
426
426
|
clack_default.log.error(error.message);
|
|
427
427
|
clack_default.log.info("Please run the wizard again to start a new session.");
|
|
428
428
|
return abort("Invalid session", 1);
|
|
429
|
+
case "network":
|
|
430
|
+
consecutiveNetworkErrors++;
|
|
431
|
+
if (consecutiveNetworkErrors >= MAX_NETWORK_ERRORS) {
|
|
432
|
+
spinner.stop("Connection failed");
|
|
433
|
+
clack_default.log.error(`Failed to reach Nia servers after ${MAX_NETWORK_ERRORS} attempts.`);
|
|
434
|
+
clack_default.log.info("Falling back to manual API key entry.");
|
|
435
|
+
return await promptForManualApiKey();
|
|
436
|
+
}
|
|
437
|
+
break;
|
|
429
438
|
default:
|
|
439
|
+
spinner.stop("Error");
|
|
430
440
|
clack_default.log.error(error.message);
|
|
431
441
|
clack_default.log.info("Falling back to manual API key entry.");
|
|
432
442
|
return await promptForManualApiKey();
|
|
433
443
|
}
|
|
434
444
|
} else {
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
445
|
+
consecutiveNetworkErrors++;
|
|
446
|
+
if (consecutiveNetworkErrors >= MAX_NETWORK_ERRORS) {
|
|
447
|
+
spinner.stop("Connection failed");
|
|
448
|
+
clack_default.log.error("Lost connection to Nia servers.");
|
|
449
|
+
debug(`Exchange error: ${error}`);
|
|
450
|
+
clack_default.log.info("Falling back to manual API key entry.");
|
|
451
|
+
return await promptForManualApiKey();
|
|
452
|
+
}
|
|
439
453
|
}
|
|
440
454
|
}
|
|
455
|
+
const backoff = consecutiveNetworkErrors > 0 ? POLL_INTERVAL_MS * Math.min(consecutiveNetworkErrors, 4) : POLL_INTERVAL_MS;
|
|
456
|
+
await sleep(backoff);
|
|
441
457
|
}
|
|
442
458
|
}
|
|
443
459
|
async function promptForManualApiKey() {
|
|
@@ -3889,4 +3905,4 @@ export {
|
|
|
3889
3905
|
runSkillAdd,
|
|
3890
3906
|
printAgentGuide
|
|
3891
3907
|
};
|
|
3892
|
-
//# sourceMappingURL=chunk-
|
|
3908
|
+
//# sourceMappingURL=chunk-KAGHMAER.js.map
|