fito-tui 0.9.3 → 0.11.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/agents/fito-docs.md +124 -0
- package/extensions/fito-fetch-tools.js +113 -7
- package/extensions/fito-fetch-tools.ts +133 -7
- package/extensions/fito-startup.js +40 -10
- package/extensions/fito-startup.ts +42 -10
- package/package.json +1 -1
- package/prompts/fito-docs.md +70 -0
- package/prompts/fito-login-by-device.md +24 -0
- package/prompts/fito-tutor.md +5 -3
- package/shared/fito-api/README.md +35 -6
- package/shared/fito-api/client.js +31 -4
- package/shared/fito-api/client.ts +48 -4
- package/shared/fito-api/guide.js +50 -0
- package/shared/fito-api/guide.ts +63 -0
- package/shared/fito-guide/fito-guide.js +84 -0
- package/shared/fito-guide/fito-guide.ts +143 -0
- package/shared/fito-session/credentials.js +28 -0
- package/shared/fito-session/credentials.ts +33 -0
- package/shared/fito-url/fito-url.js +21 -0
- package/shared/fito-url/fito-url.ts +23 -0
- package/shared/tui-package/tui-package.js +104 -0
- package/shared/tui-package/tui-package.ts +143 -0
- package/skills/fito-auth/SKILL.md +24 -8
- package/skills/fito-auth/login-by-device.js +98 -0
- package/skills/fito-auth/login-by-device.ts +119 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/fito-login-by-device` CLI entrypoint (Task 17.32): a login that needs no `localhost`, for a
|
|
3
|
+
* browser that cannot call the CLI on this computer (a blocked browser, SSH, WSL, a VM). It starts
|
|
4
|
+
* a device login (RFC 8628), prints `<FITO_URL>/device` and the code, tries to open the direct link,
|
|
5
|
+
* and polls until the user approves the code on the website. Then it writes this Fito's
|
|
6
|
+
* `~/.fito/credentials-<host>`, as `/fito-login` does. No token passes through the browser.
|
|
7
|
+
* Run from this skill's directory: `node login-by-device.js`.
|
|
8
|
+
*/
|
|
9
|
+
import * as os from "node:os";
|
|
10
|
+
import * as path from "node:path";
|
|
11
|
+
import { fileURLToPath } from "node:url";
|
|
12
|
+
import { openBrowser } from "./browser.js";
|
|
13
|
+
import { loginSuccessMessage } from "./login.js";
|
|
14
|
+
import { FitoApiError, createFitoApiClient, } from "../../shared/fito-api/client.js";
|
|
15
|
+
import { credentialsFileName, writeCredentials } from "../../shared/fito-session/credentials.js";
|
|
16
|
+
import { fitoUrl } from "../../shared/fito-url/fito-url.js";
|
|
17
|
+
/** How much longer each later poll waits after a `429` (RFC 8628 §3.5's `slow_down`; PLAN decision D3). */
|
|
18
|
+
const SLOW_DOWN_MS = 5000;
|
|
19
|
+
const EXPIRED_LINE = "✗ The code expired. Run /fito-login-by-device again.";
|
|
20
|
+
/** `600` as "10 minutes"; a lifetime that is not whole minutes, in seconds. */
|
|
21
|
+
function describeLifetime(seconds) {
|
|
22
|
+
if (seconds % 60 !== 0)
|
|
23
|
+
return `${seconds} seconds`;
|
|
24
|
+
const minutes = seconds / 60;
|
|
25
|
+
return minutes === 1 ? "1 minute" : `${minutes} minutes`;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Runs one device login and returns its exit code: `0` once the credentials are saved, `1` when the
|
|
29
|
+
* code expired. Any other failure throws, for the caller to report.
|
|
30
|
+
*
|
|
31
|
+
* The code has expired when this script's own clock passes its lifetime, or when the poll answers
|
|
32
|
+
* `400`. The backend refuses a code that is unknown, expired or used, and for the new code this
|
|
33
|
+
* script polls, and stops polling once it is used, only "expired" happens: the backend's clock can
|
|
34
|
+
* reach the end a little before this one.
|
|
35
|
+
*/
|
|
36
|
+
export async function runDeviceLogin(options) {
|
|
37
|
+
const started = await options.client.startDeviceLogin();
|
|
38
|
+
const deadline = options.now() + started.expiresIn * 1000;
|
|
39
|
+
const approvalUrl = `${options.webOrigin}/device`;
|
|
40
|
+
const directLink = `${approvalUrl}?code=${encodeURIComponent(started.userCode)}`;
|
|
41
|
+
options.print(`Open ${approvalUrl} and confirm the code ${started.userCode}.`);
|
|
42
|
+
options.print(`Or open this link: ${directLink}`);
|
|
43
|
+
options.print(`The code expires in ${describeLifetime(started.expiresIn)}. Waiting for your approval…`);
|
|
44
|
+
try {
|
|
45
|
+
options.openBrowser(directLink);
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
// Best effort: over SSH or with no browser nothing opens, and the printed address is enough.
|
|
49
|
+
}
|
|
50
|
+
let waitMs = started.interval * 1000;
|
|
51
|
+
for (;;) {
|
|
52
|
+
await options.sleep(waitMs);
|
|
53
|
+
if (options.now() >= deadline)
|
|
54
|
+
break;
|
|
55
|
+
let answer;
|
|
56
|
+
try {
|
|
57
|
+
answer = await options.client.pollDeviceLogin(started.deviceCode);
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
if (error instanceof FitoApiError && error.status === 429) {
|
|
61
|
+
waitMs += SLOW_DOWN_MS;
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
if (error instanceof FitoApiError && error.status === 400)
|
|
65
|
+
break;
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
if (answer.status === "approved") {
|
|
69
|
+
const { status: _status, ...tokens } = answer;
|
|
70
|
+
writeCredentials({ ...tokens, savedAt: new Date(options.now()).toISOString() }, options.home, options.webOrigin);
|
|
71
|
+
options.print(loginSuccessMessage(credentialsFileName(options.webOrigin)));
|
|
72
|
+
return 0;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
options.printError(EXPIRED_LINE);
|
|
76
|
+
return 1;
|
|
77
|
+
}
|
|
78
|
+
async function main() {
|
|
79
|
+
process.exitCode = await runDeviceLogin({
|
|
80
|
+
client: createFitoApiClient(),
|
|
81
|
+
webOrigin: fitoUrl(),
|
|
82
|
+
home: os.homedir(),
|
|
83
|
+
sleep: (ms) => new Promise((resolve) => setTimeout(resolve, ms)),
|
|
84
|
+
now: Date.now,
|
|
85
|
+
openBrowser: (url) => openBrowser(url),
|
|
86
|
+
print: (line) => console.log(line),
|
|
87
|
+
printError: (line) => console.error(line),
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
/** Only auto-invoke the CLI when run directly (`node login-by-device.js`), not when imported. */
|
|
91
|
+
const entry = process.argv[1];
|
|
92
|
+
const isMainModule = !!entry && path.resolve(entry) === fileURLToPath(import.meta.url);
|
|
93
|
+
if (isMainModule) {
|
|
94
|
+
main().catch((error) => {
|
|
95
|
+
console.error(`✗ Login failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
96
|
+
process.exitCode = 1;
|
|
97
|
+
});
|
|
98
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/fito-login-by-device` CLI entrypoint (Task 17.32): a login that needs no `localhost`, for a
|
|
3
|
+
* browser that cannot call the CLI on this computer (a blocked browser, SSH, WSL, a VM). It starts
|
|
4
|
+
* a device login (RFC 8628), prints `<FITO_URL>/device` and the code, tries to open the direct link,
|
|
5
|
+
* and polls until the user approves the code on the website. Then it writes this Fito's
|
|
6
|
+
* `~/.fito/credentials-<host>`, as `/fito-login` does. No token passes through the browser.
|
|
7
|
+
* Run from this skill's directory: `node login-by-device.js`.
|
|
8
|
+
*/
|
|
9
|
+
import * as os from "node:os";
|
|
10
|
+
import * as path from "node:path";
|
|
11
|
+
import { fileURLToPath } from "node:url";
|
|
12
|
+
|
|
13
|
+
import { openBrowser } from "./browser.ts";
|
|
14
|
+
import { loginSuccessMessage } from "./login.ts";
|
|
15
|
+
import {
|
|
16
|
+
type DeviceLoginPoll,
|
|
17
|
+
type FitoApiClient,
|
|
18
|
+
FitoApiError,
|
|
19
|
+
createFitoApiClient,
|
|
20
|
+
} from "../../shared/fito-api/client.ts";
|
|
21
|
+
import { credentialsFileName, writeCredentials } from "../../shared/fito-session/credentials.ts";
|
|
22
|
+
import { fitoUrl } from "../../shared/fito-url/fito-url.ts";
|
|
23
|
+
|
|
24
|
+
/** How much longer each later poll waits after a `429` (RFC 8628 §3.5's `slow_down`; PLAN decision D3). */
|
|
25
|
+
const SLOW_DOWN_MS = 5000;
|
|
26
|
+
|
|
27
|
+
const EXPIRED_LINE = "✗ The code expired. Run /fito-login-by-device again.";
|
|
28
|
+
|
|
29
|
+
export interface DeviceLoginOptions {
|
|
30
|
+
client: Pick<FitoApiClient, "startDeviceLogin" | "pollDeviceLogin">;
|
|
31
|
+
/** The address of the Fito the user approves on, which also names the credentials file. */
|
|
32
|
+
webOrigin: string;
|
|
33
|
+
home: string;
|
|
34
|
+
sleep: (ms: number) => Promise<void>;
|
|
35
|
+
now: () => number;
|
|
36
|
+
openBrowser: (url: string) => void;
|
|
37
|
+
print: (line: string) => void;
|
|
38
|
+
printError: (line: string) => void;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** `600` as "10 minutes"; a lifetime that is not whole minutes, in seconds. */
|
|
42
|
+
function describeLifetime(seconds: number): string {
|
|
43
|
+
if (seconds % 60 !== 0) return `${seconds} seconds`;
|
|
44
|
+
const minutes = seconds / 60;
|
|
45
|
+
return minutes === 1 ? "1 minute" : `${minutes} minutes`;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Runs one device login and returns its exit code: `0` once the credentials are saved, `1` when the
|
|
50
|
+
* code expired. Any other failure throws, for the caller to report.
|
|
51
|
+
*
|
|
52
|
+
* The code has expired when this script's own clock passes its lifetime, or when the poll answers
|
|
53
|
+
* `400`. The backend refuses a code that is unknown, expired or used, and for the new code this
|
|
54
|
+
* script polls, and stops polling once it is used, only "expired" happens: the backend's clock can
|
|
55
|
+
* reach the end a little before this one.
|
|
56
|
+
*/
|
|
57
|
+
export async function runDeviceLogin(options: DeviceLoginOptions): Promise<number> {
|
|
58
|
+
const started = await options.client.startDeviceLogin();
|
|
59
|
+
const deadline = options.now() + started.expiresIn * 1000;
|
|
60
|
+
const approvalUrl = `${options.webOrigin}/device`;
|
|
61
|
+
const directLink = `${approvalUrl}?code=${encodeURIComponent(started.userCode)}`;
|
|
62
|
+
|
|
63
|
+
options.print(`Open ${approvalUrl} and confirm the code ${started.userCode}.`);
|
|
64
|
+
options.print(`Or open this link: ${directLink}`);
|
|
65
|
+
options.print(`The code expires in ${describeLifetime(started.expiresIn)}. Waiting for your approval…`);
|
|
66
|
+
try {
|
|
67
|
+
options.openBrowser(directLink);
|
|
68
|
+
} catch {
|
|
69
|
+
// Best effort: over SSH or with no browser nothing opens, and the printed address is enough.
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
let waitMs = started.interval * 1000;
|
|
73
|
+
for (;;) {
|
|
74
|
+
await options.sleep(waitMs);
|
|
75
|
+
if (options.now() >= deadline) break;
|
|
76
|
+
let answer: DeviceLoginPoll;
|
|
77
|
+
try {
|
|
78
|
+
answer = await options.client.pollDeviceLogin(started.deviceCode);
|
|
79
|
+
} catch (error) {
|
|
80
|
+
if (error instanceof FitoApiError && error.status === 429) {
|
|
81
|
+
waitMs += SLOW_DOWN_MS;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (error instanceof FitoApiError && error.status === 400) break;
|
|
85
|
+
throw error;
|
|
86
|
+
}
|
|
87
|
+
if (answer.status === "approved") {
|
|
88
|
+
const { status: _status, ...tokens } = answer;
|
|
89
|
+
writeCredentials({ ...tokens, savedAt: new Date(options.now()).toISOString() }, options.home, options.webOrigin);
|
|
90
|
+
options.print(loginSuccessMessage(credentialsFileName(options.webOrigin)));
|
|
91
|
+
return 0;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
options.printError(EXPIRED_LINE);
|
|
95
|
+
return 1;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function main(): Promise<void> {
|
|
99
|
+
process.exitCode = await runDeviceLogin({
|
|
100
|
+
client: createFitoApiClient(),
|
|
101
|
+
webOrigin: fitoUrl(),
|
|
102
|
+
home: os.homedir(),
|
|
103
|
+
sleep: (ms) => new Promise((resolve) => setTimeout(resolve, ms)),
|
|
104
|
+
now: Date.now,
|
|
105
|
+
openBrowser: (url) => openBrowser(url),
|
|
106
|
+
print: (line) => console.log(line),
|
|
107
|
+
printError: (line) => console.error(line),
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Only auto-invoke the CLI when run directly (`node login-by-device.js`), not when imported. */
|
|
112
|
+
const entry = process.argv[1];
|
|
113
|
+
const isMainModule = !!entry && path.resolve(entry) === fileURLToPath(import.meta.url);
|
|
114
|
+
if (isMainModule) {
|
|
115
|
+
main().catch((error: unknown) => {
|
|
116
|
+
console.error(`✗ Login failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
117
|
+
process.exitCode = 1;
|
|
118
|
+
});
|
|
119
|
+
}
|