@tulcn/lxz 0.0.0-stage → 0.3.1
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/README.md +63 -2
- package/dist/application.d.ts +2 -0
- package/dist/application.js +17 -0
- package/dist/auth/client.d.ts +5 -0
- package/dist/auth/client.js +14 -0
- package/dist/auth/gateway.d.ts +1 -0
- package/dist/auth/gateway.js +10 -0
- package/dist/auth/host.d.ts +2 -0
- package/dist/auth/host.js +16 -0
- package/dist/auth/identity.d.ts +45 -0
- package/dist/auth/identity.js +106 -0
- package/dist/auth/index.d.ts +10 -0
- package/dist/auth/index.js +13 -0
- package/dist/auth/inspection.d.ts +14 -0
- package/dist/auth/inspection.js +22 -0
- package/dist/auth/modules.d.ts +12 -0
- package/dist/auth/modules.js +25 -0
- package/dist/auth/platform.d.ts +26 -0
- package/dist/auth/platform.js +102 -0
- package/dist/auth/protocol.d.ts +17 -0
- package/dist/auth/protocol.js +1 -0
- package/dist/auth/provider.d.ts +40 -0
- package/dist/auth/provider.js +174 -0
- package/dist/auth/response.d.ts +2 -0
- package/dist/auth/response.js +17 -0
- package/dist/auth/service.d.ts +5 -0
- package/dist/auth/service.js +72 -0
- package/dist/auth/store.d.ts +26 -0
- package/dist/auth/store.js +171 -0
- package/dist/cli/browser.d.ts +1 -0
- package/dist/cli/browser.js +11 -0
- package/dist/cli/contracts.d.ts +85 -0
- package/dist/cli/contracts.js +51 -0
- package/dist/cli/deployment.d.ts +13 -0
- package/dist/cli/deployment.js +35 -0
- package/dist/cli/device-login.d.ts +29 -0
- package/dist/cli/device-login.js +87 -0
- package/dist/cli/environment.d.ts +9 -0
- package/dist/cli/environment.js +19 -0
- package/dist/cli/errors.d.ts +30 -0
- package/dist/cli/errors.js +22 -0
- package/dist/cli/files.d.ts +6 -0
- package/dist/cli/files.js +71 -0
- package/dist/cli/http.d.ts +6 -0
- package/dist/cli/http.js +38 -0
- package/dist/cli/identity-commands.d.ts +3 -0
- package/dist/cli/identity-commands.js +56 -0
- package/dist/cli/index.d.ts +13 -0
- package/dist/cli/index.js +13 -0
- package/dist/cli/init.d.ts +205 -0
- package/dist/cli/init.js +77 -0
- package/dist/cli/modules.d.ts +6 -0
- package/dist/cli/modules.js +18 -0
- package/dist/cli/output.d.ts +2 -0
- package/dist/cli/output.js +35 -0
- package/dist/cli/prompts.d.ts +34 -0
- package/dist/cli/prompts.js +58 -0
- package/dist/cli/runtime.d.ts +2 -0
- package/dist/cli/runtime.js +137 -0
- package/dist/cli/schemas.d.ts +187 -0
- package/dist/cli/schemas.js +46 -0
- package/dist/doctor.d.ts +2 -0
- package/dist/doctor.js +72 -0
- package/dist/internal/client.d.ts +6 -0
- package/dist/internal/client.js +51 -0
- package/dist/internal/host.d.ts +1 -0
- package/dist/internal/host.js +50 -0
- package/dist/internal/protocol.d.ts +27 -0
- package/dist/internal/protocol.js +1 -0
- package/dist/main.d.ts +2 -0
- package/dist/main.js +12 -0
- package/dist/skills/agents.d.ts +7 -0
- package/dist/skills/agents.js +11 -0
- package/dist/skills/artifacts.d.ts +9 -0
- package/dist/skills/artifacts.js +12 -0
- package/dist/skills/bundle.d.ts +22 -0
- package/dist/skills/bundle.js +77 -0
- package/dist/skills/client.d.ts +5 -0
- package/dist/skills/client.js +9 -0
- package/dist/skills/commands.d.ts +5 -0
- package/dist/skills/commands.js +20 -0
- package/dist/skills/delivery.d.ts +60 -0
- package/dist/skills/delivery.js +89 -0
- package/dist/skills/index.d.ts +7 -0
- package/dist/skills/index.js +7 -0
- package/dist/skills/installer.d.ts +23 -0
- package/dist/skills/installer.js +130 -0
- package/dist/skills/paths.d.ts +2 -0
- package/dist/skills/paths.js +28 -0
- package/package.json +36 -3
- package/scripts/install.mjs +17 -0
- package/skills/SKILL.md +38 -0
- package/skills/bundle.json +7 -0
- package/skills/commands.md +440 -0
- package/skills/features.md +12 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
export const deploymentDefaults = {
|
|
3
|
+
LINKWISE_ISSUER: 'https://ai-cli.tul.cn/linkwise-api',
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Roots serving the same paths as an issuer origin, used while that origin cannot be reached.
|
|
7
|
+
* LINKWISE_FALLBACK_URL overrides (an empty value disables); a test build ships its own in linkwise.env.json.
|
|
8
|
+
*/
|
|
9
|
+
export const deploymentFallbacks = {
|
|
10
|
+
'https://ai-cli.tul.cn': 'https://linkwise.tul.cn/cli',
|
|
11
|
+
};
|
|
12
|
+
function packagedDeployment(packageRoot) {
|
|
13
|
+
let text;
|
|
14
|
+
try {
|
|
15
|
+
text = readFileSync(new URL('linkwise.env.json', packageRoot), 'utf8');
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
if (error.code === 'ENOENT')
|
|
19
|
+
return undefined;
|
|
20
|
+
throw error;
|
|
21
|
+
}
|
|
22
|
+
const { LINKWISE_ISSUER: issuer, LINKWISE_FALLBACK_URL: fallback } = JSON.parse(text);
|
|
23
|
+
if (typeof issuer !== 'string' || !issuer)
|
|
24
|
+
throw new Error('linkwise.env.json must set LINKWISE_ISSUER.');
|
|
25
|
+
return { LINKWISE_ISSUER: issuer, ...(typeof fallback === 'string' ? { LINKWISE_FALLBACK_URL: fallback } : {}) };
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Fixes the authorization server for this process and the lxz processes it starts: environment, then the file a test build ships with, then production.
|
|
29
|
+
* The packaged fallback address goes with the packaged issuer only.
|
|
30
|
+
*/
|
|
31
|
+
export function applyDeployment(packageRoot, env = process.env) {
|
|
32
|
+
if (env.LINKWISE_ISSUER !== undefined)
|
|
33
|
+
return;
|
|
34
|
+
Object.assign(env, packagedDeployment(packageRoot) ?? deploymentDefaults);
|
|
35
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Context, Parameter } from './contracts.js';
|
|
2
|
+
export interface AuthorizationLink {
|
|
3
|
+
authorizationUrl: string;
|
|
4
|
+
userCode: string;
|
|
5
|
+
browserOpened: boolean;
|
|
6
|
+
log: string;
|
|
7
|
+
}
|
|
8
|
+
export declare const authorizationPrompt: (url: string, code: string) => string;
|
|
9
|
+
export declare const statusWaitLimitSeconds = 170;
|
|
10
|
+
export declare const waitParameter: Parameter;
|
|
11
|
+
export declare const pendingProperties: {
|
|
12
|
+
authorizationUrl: {
|
|
13
|
+
type: string;
|
|
14
|
+
};
|
|
15
|
+
userCode: {
|
|
16
|
+
type: string;
|
|
17
|
+
};
|
|
18
|
+
browserOpened: {
|
|
19
|
+
type: string;
|
|
20
|
+
description: string;
|
|
21
|
+
};
|
|
22
|
+
log: {
|
|
23
|
+
type: string;
|
|
24
|
+
description: string;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
export declare function waitsInBackground(input: Record<string, string | boolean>, context: Context): boolean;
|
|
28
|
+
export declare function approvalNext(link: AuthorizationLink, status: string): string;
|
|
29
|
+
export declare function deviceLoginInBackground(input: Record<string, string | boolean>, context: Context, pending: (link: AuthorizationLink) => unknown, entry?: string): Promise<unknown>;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { closeSync, mkdtempSync, openSync, readFileSync } from 'node:fs';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
6
|
+
import { openBrowser } from './browser.js';
|
|
7
|
+
import { CliError } from './errors.js';
|
|
8
|
+
const urlLabel = 'Authorization URL: ', codeLabel = 'User code: ';
|
|
9
|
+
export const authorizationPrompt = (url, code) => `${urlLabel}${url}\n${codeLabel}${code}`;
|
|
10
|
+
const labelled = (text, label) => text.split(/\r?\n/).find(line => line.startsWith(label))?.slice(label.length).trim();
|
|
11
|
+
export const statusWaitLimitSeconds = 170;
|
|
12
|
+
const linkDeadlineMs = 30000;
|
|
13
|
+
// An approval that is already done, or an issuer that approves at once, still finishes in the foreground.
|
|
14
|
+
const graceMs = 3000;
|
|
15
|
+
export const waitParameter = { type: 'boolean', description: 'With --device-login in non-interactive mode, wait for approval in the foreground instead of returning the link at once', default: false };
|
|
16
|
+
export const pendingProperties = {
|
|
17
|
+
authorizationUrl: { type: 'string' }, userCode: { type: 'string' },
|
|
18
|
+
browserOpened: { type: 'boolean', description: 'This command opened the authorization page in the system browser' },
|
|
19
|
+
log: { type: 'string', description: 'Log of the background process that waits for approval' },
|
|
20
|
+
};
|
|
21
|
+
export function waitsInBackground(input, context) {
|
|
22
|
+
return Boolean(input['device-login'] && !context.interactive && !input.wait && context.argv);
|
|
23
|
+
}
|
|
24
|
+
export function approvalNext(link, status) {
|
|
25
|
+
return (link.browserOpened
|
|
26
|
+
? 'The authorization page is open in the system browser on this computer. Tell the user to switch to the browser, check that it shows userCode and approve; also give the link and userCode in your reply in case the page did not appear. '
|
|
27
|
+
: 'Give the link and userCode in your reply and ask the user to open the link in the system browser, check userCode and approve. ')
|
|
28
|
+
+ 'Write authorizationUrl unchanged as a Markdown link on a line of its own, for example [Open the authorization page](authorizationUrl) with the label in the user\'s language; never as a bare URL, and with no text or punctuation right after it, because chat clients treat following characters as part of the URL. Write userCode on a line of its own. '
|
|
29
|
+
+ 'In Linkwise a clicked link opens in the built-in browser, so tell the user to right-click it and choose to open it in the system browser. '
|
|
30
|
+
+ `Then, in the same turn and without waiting for the user to reply, run ${status} --wait-seconds ${statusWaitLimitSeconds} --json --non-interactive; it returns once the user approves, and you continue the user's original task. Do not run init or login again while this link is valid.`;
|
|
31
|
+
}
|
|
32
|
+
function finalReceipt(text) {
|
|
33
|
+
for (const line of text.split(/\r?\n/).reverse()) {
|
|
34
|
+
if (!line.startsWith('{'))
|
|
35
|
+
continue;
|
|
36
|
+
let receipt;
|
|
37
|
+
try {
|
|
38
|
+
receipt = JSON.parse(line);
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
if (receipt.ok)
|
|
44
|
+
return receipt.data;
|
|
45
|
+
const { code, message, next, retryable, details } = receipt.error;
|
|
46
|
+
throw new CliError(code, message, next, retryable, details);
|
|
47
|
+
}
|
|
48
|
+
throw new CliError('UNAVAILABLE', 'Background authorization stopped without a result.', 'Run the command again.');
|
|
49
|
+
}
|
|
50
|
+
// Agents that show output only after a command exits cannot show the link of a device login that waits.
|
|
51
|
+
// A detached copy of this command waits instead; this one opens the browser once and returns the link.
|
|
52
|
+
export async function deviceLoginInBackground(input, context, pending, entry = process.argv[1]) {
|
|
53
|
+
const log = join(mkdtempSync(join(tmpdir(), 'lxz-authorization-')), 'authorization.log');
|
|
54
|
+
const output = openSync(log, 'a');
|
|
55
|
+
let exited = false;
|
|
56
|
+
try {
|
|
57
|
+
const child = spawn(process.execPath, [...process.execArgv, entry, ...context.argv, '--wait', '--no-browser', '--non-interactive', '--json'], { detached: true, windowsHide: true, stdio: ['ignore', output, output] });
|
|
58
|
+
child.once('exit', () => { exited = true; });
|
|
59
|
+
child.once('error', () => { exited = true; });
|
|
60
|
+
child.unref();
|
|
61
|
+
}
|
|
62
|
+
finally {
|
|
63
|
+
closeSync(output);
|
|
64
|
+
}
|
|
65
|
+
const read = () => { try {
|
|
66
|
+
return readFileSync(log, 'utf8');
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
return '';
|
|
70
|
+
} };
|
|
71
|
+
let seen = 0, browser;
|
|
72
|
+
for (const start = Date.now(); Date.now() - start < linkDeadlineMs;) {
|
|
73
|
+
await delay(200, undefined, { signal: context.signal });
|
|
74
|
+
const text = read();
|
|
75
|
+
if (exited)
|
|
76
|
+
return finalReceipt(text);
|
|
77
|
+
const authorizationUrl = labelled(text, urlLabel), userCode = labelled(text, codeLabel);
|
|
78
|
+
if (!authorizationUrl || !userCode)
|
|
79
|
+
continue;
|
|
80
|
+
seen ||= Date.now();
|
|
81
|
+
browser ??= input.browser === false ? Promise.resolve(false) : openBrowser(authorizationUrl, context.signal);
|
|
82
|
+
if (Date.now() - seen < graceMs)
|
|
83
|
+
continue;
|
|
84
|
+
return pending({ authorizationUrl, userCode, browserOpened: await browser, log });
|
|
85
|
+
}
|
|
86
|
+
throw new CliError('UNAVAILABLE', 'Could not get an authorization link in time.', `Check the network and run the command again; the background log is ${log}.`, true);
|
|
87
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { CliError } from './errors.js';
|
|
2
|
+
export const runtimeRequirements = { node: '22.12.0', pnpm: '12.16.1' };
|
|
3
|
+
function supported(version, minimum) {
|
|
4
|
+
const parsed = /^v?(\d+)\.(\d+)\.(\d+)$/.exec(version);
|
|
5
|
+
if (!parsed)
|
|
6
|
+
return false;
|
|
7
|
+
const parts = parsed.slice(1).map(Number);
|
|
8
|
+
const required = minimum.split('.').map(Number);
|
|
9
|
+
for (let index = 0; index < required.length; index++) {
|
|
10
|
+
if (parts[index] !== required[index])
|
|
11
|
+
return parts[index] > required[index];
|
|
12
|
+
}
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
export function checkRuntimeVersions(node, pnpm) {
|
|
16
|
+
if (!supported(node, runtimeRequirements.node) || !supported(pnpm, runtimeRequirements.pnpm))
|
|
17
|
+
throw new CliError('CONFIGURATION', 'Unsupported Node.js or pnpm version.', `Install stable Node.js >=${runtimeRequirements.node} and pnpm >=${runtimeRequirements.pnpm}.`);
|
|
18
|
+
return { node, pnpm, supported: true };
|
|
19
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export declare const exitCodes: {
|
|
2
|
+
readonly INVALID_INPUT: 2;
|
|
3
|
+
readonly AUTH_REQUIRED: 3;
|
|
4
|
+
readonly AUTH_EXPIRED: 3;
|
|
5
|
+
readonly ACCESS_DENIED: 4;
|
|
6
|
+
readonly UNAVAILABLE: 5;
|
|
7
|
+
readonly CONFLICT: 6;
|
|
8
|
+
readonly IO_ERROR: 7;
|
|
9
|
+
readonly CANCELLED: 130;
|
|
10
|
+
readonly TIMEOUT: 124;
|
|
11
|
+
readonly CONFIGURATION: 2;
|
|
12
|
+
readonly INCOMPATIBLE: 6;
|
|
13
|
+
readonly INIT_INCOMPLETE: 8;
|
|
14
|
+
readonly INTERNAL: 1;
|
|
15
|
+
};
|
|
16
|
+
export type ErrorCode = keyof typeof exitCodes;
|
|
17
|
+
export declare class CliError extends Error {
|
|
18
|
+
code: ErrorCode;
|
|
19
|
+
next: string;
|
|
20
|
+
retryable: boolean;
|
|
21
|
+
details?: unknown | undefined;
|
|
22
|
+
constructor(code: ErrorCode, message: string, next: string, retryable?: boolean, details?: unknown | undefined, cause?: unknown);
|
|
23
|
+
}
|
|
24
|
+
export declare function failure(error: unknown): {
|
|
25
|
+
details?: {} | null | undefined;
|
|
26
|
+
code: "INVALID_INPUT" | "AUTH_REQUIRED" | "AUTH_EXPIRED" | "ACCESS_DENIED" | "UNAVAILABLE" | "CONFLICT" | "IO_ERROR" | "CANCELLED" | "TIMEOUT" | "CONFIGURATION" | "INCOMPATIBLE" | "INIT_INCOMPLETE" | "INTERNAL";
|
|
27
|
+
message: string;
|
|
28
|
+
retryable: boolean;
|
|
29
|
+
next: string;
|
|
30
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export const exitCodes = {
|
|
2
|
+
INVALID_INPUT: 2, AUTH_REQUIRED: 3, AUTH_EXPIRED: 3, ACCESS_DENIED: 4,
|
|
3
|
+
UNAVAILABLE: 5, CONFLICT: 6, IO_ERROR: 7, CANCELLED: 130, TIMEOUT: 124,
|
|
4
|
+
CONFIGURATION: 2, INCOMPATIBLE: 6, INIT_INCOMPLETE: 8, INTERNAL: 1,
|
|
5
|
+
};
|
|
6
|
+
export class CliError extends Error {
|
|
7
|
+
code;
|
|
8
|
+
next;
|
|
9
|
+
retryable;
|
|
10
|
+
details;
|
|
11
|
+
constructor(code, message, next, retryable = false, details, cause) {
|
|
12
|
+
super(message, cause === undefined ? undefined : { cause });
|
|
13
|
+
this.code = code;
|
|
14
|
+
this.next = next;
|
|
15
|
+
this.retryable = retryable;
|
|
16
|
+
this.details = details;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export function failure(error) {
|
|
20
|
+
const known = error instanceof CliError ? error : new CliError('INTERNAL', 'Operation failed.', 'Check configuration; contact the maintainer with the command name and version.');
|
|
21
|
+
return { code: known.code, message: known.message, retryable: known.retryable, next: known.next, ...(known.details === undefined ? {} : { details: known.details }) };
|
|
22
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare function readText(path: string, signal: AbortSignal): Promise<string>;
|
|
2
|
+
export declare function decodeText(data: Uint8Array): string;
|
|
3
|
+
export declare function writeOutput(path: string, content: string | Uint8Array, overwrite: boolean, signal: AbortSignal): Promise<{
|
|
4
|
+
file: string;
|
|
5
|
+
bytes: number;
|
|
6
|
+
}>;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { open, readFile, writeFile, rename, rm, lstat } from 'node:fs/promises';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { resolve } from 'node:path';
|
|
4
|
+
import { CliError } from './errors.js';
|
|
5
|
+
export async function readText(path, signal) {
|
|
6
|
+
let data;
|
|
7
|
+
try {
|
|
8
|
+
data = await readFile(path, { signal });
|
|
9
|
+
}
|
|
10
|
+
catch {
|
|
11
|
+
signal.throwIfAborted();
|
|
12
|
+
throw new CliError('IO_ERROR', 'Cannot read the input file.', 'Check the file path and read permissions.');
|
|
13
|
+
}
|
|
14
|
+
return decodeText(data);
|
|
15
|
+
}
|
|
16
|
+
export function decodeText(data) {
|
|
17
|
+
const encodings = data[0] === 0xef && data[1] === 0xbb && data[2] === 0xbf ? ['utf-8'] : ['utf-8', 'gbk'];
|
|
18
|
+
for (const encoding of encodings) {
|
|
19
|
+
try {
|
|
20
|
+
return new TextDecoder(encoding, { fatal: true }).decode(data);
|
|
21
|
+
}
|
|
22
|
+
catch { /* Try the next supported encoding without replacing invalid bytes. */ }
|
|
23
|
+
}
|
|
24
|
+
throw new CliError('INVALID_INPUT', 'The input file is not valid UTF-8 or GBK text.', 'Save the file as UTF-8 or GBK and retry.');
|
|
25
|
+
}
|
|
26
|
+
export async function writeOutput(path, content, overwrite, signal) {
|
|
27
|
+
const file = resolve(path);
|
|
28
|
+
const temporary = `${file}.${randomUUID()}.tmp`;
|
|
29
|
+
signal.throwIfAborted();
|
|
30
|
+
try {
|
|
31
|
+
if (!overwrite) {
|
|
32
|
+
const handle = await open(file, 'wx');
|
|
33
|
+
try {
|
|
34
|
+
await handle.writeFile(content, { signal });
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
await handle.close();
|
|
38
|
+
await rm(file, { force: true });
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
await handle.close();
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
let stat;
|
|
45
|
+
try {
|
|
46
|
+
stat = await lstat(file);
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
if (error.code !== 'ENOENT')
|
|
50
|
+
throw error;
|
|
51
|
+
}
|
|
52
|
+
if (stat && (!stat.isFile() || stat.isSymbolicLink()))
|
|
53
|
+
throw new CliError('CONFLICT', 'The output target is not a regular file.', 'Choose another --output path.');
|
|
54
|
+
await writeFile(temporary, content, { flag: 'wx', signal });
|
|
55
|
+
signal.throwIfAborted();
|
|
56
|
+
await rename(temporary, file);
|
|
57
|
+
}
|
|
58
|
+
return { file, bytes: typeof content === 'string' ? Buffer.byteLength(content) : content.byteLength };
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
signal.throwIfAborted();
|
|
62
|
+
if (error instanceof CliError)
|
|
63
|
+
throw error;
|
|
64
|
+
if (error.code === 'EEXIST')
|
|
65
|
+
throw new CliError('CONFLICT', 'The output file already exists.', 'Choose another --output path or pass --overwrite.');
|
|
66
|
+
throw new CliError('IO_ERROR', 'Cannot write the output file.', 'Check the output path and write permissions.');
|
|
67
|
+
}
|
|
68
|
+
finally {
|
|
69
|
+
await rm(temporary, { force: true });
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare function secureUrl(value: string, label?: string, options?: {
|
|
2
|
+
allowHttp?: boolean;
|
|
3
|
+
allowQuery?: boolean;
|
|
4
|
+
}): URL;
|
|
5
|
+
export declare function request(url: string | URL, init?: RequestInit): Promise<Response>;
|
|
6
|
+
export declare function readJson(response: Response): Promise<Record<string, unknown>>;
|
package/dist/cli/http.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { CliError } from './errors.js';
|
|
2
|
+
export function secureUrl(value, label = 'Service URL', options = {}) {
|
|
3
|
+
let url;
|
|
4
|
+
try {
|
|
5
|
+
url = new URL(value);
|
|
6
|
+
}
|
|
7
|
+
catch {
|
|
8
|
+
throw new CliError('CONFIGURATION', `${label} is not a valid URL.`, 'Configure a complete HTTPS URL.');
|
|
9
|
+
}
|
|
10
|
+
if ((url.protocol !== 'https:' && !(url.protocol === 'http:' && (options.allowHttp || ['localhost', '127.0.0.1', '[::1]'].includes(url.hostname)))) || url.username || url.password || url.hash || (!options.allowQuery && url.search)) {
|
|
11
|
+
throw new CliError('CONFIGURATION', `${label} is not an allowed service URL.`, 'Use the configured HTTP(S) URL without embedded credentials or unexpected parameters.');
|
|
12
|
+
}
|
|
13
|
+
return url;
|
|
14
|
+
}
|
|
15
|
+
export async function request(url, init = {}) {
|
|
16
|
+
const timeout = AbortSignal.timeout(60000);
|
|
17
|
+
try {
|
|
18
|
+
return await fetch(url, { ...init, redirect: 'error', signal: AbortSignal.any([...(init.signal ? [init.signal] : []), timeout]) });
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
if (init.signal?.aborted)
|
|
22
|
+
throw init.signal.reason;
|
|
23
|
+
if (timeout.aborted)
|
|
24
|
+
throw new CliError('TIMEOUT', 'The service did not respond within 60 seconds.', 'Check the network and service status, then retry.', true);
|
|
25
|
+
throw new CliError('UNAVAILABLE', 'Cannot reach the service.', 'Check the network and service URL, then retry.', true, undefined, error);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export async function readJson(response) {
|
|
29
|
+
try {
|
|
30
|
+
const data = await response.json();
|
|
31
|
+
if (data && typeof data === 'object' && !Array.isArray(data))
|
|
32
|
+
return data;
|
|
33
|
+
}
|
|
34
|
+
catch (cause) {
|
|
35
|
+
throw new CliError('UNAVAILABLE', 'Cannot read the service response.', 'Retry later; if it still fails, contact the service maintainer.', true, undefined, cause);
|
|
36
|
+
}
|
|
37
|
+
throw new CliError('UNAVAILABLE', 'Cannot read the service response.', 'Retry later; if it still fails, contact the service maintainer.', true);
|
|
38
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
2
|
+
import { errorSchema } from './schemas.js';
|
|
3
|
+
import { approvalNext, deviceLoginInBackground, pendingProperties, statusWaitLimitSeconds, waitParameter, waitsInBackground } from './device-login.js';
|
|
4
|
+
const pollMs = 2000;
|
|
5
|
+
export function identityCommands(identity, invoke) {
|
|
6
|
+
const status = {
|
|
7
|
+
description: 'Show this CLI authorization status and current account', effects: 'none', timeoutMs: (statusWaitLimitSeconds + 5) * 1000,
|
|
8
|
+
parameters: {
|
|
9
|
+
'wait-seconds': { type: 'string', default: '0', pattern: `^(?:0|[1-9][0-9]?|1[0-6][0-9]|${statusWaitLimitSeconds})$`, description: `Wait up to this many seconds (0-${statusWaitLimitSeconds}) for a pending browser approval; returns as soon as it is approved` },
|
|
10
|
+
},
|
|
11
|
+
result: { type: 'object', properties: { authorized: { type: 'boolean', description: 'Local session exists within its refresh lifetime; server status has not been checked' }, account: { type: 'string', description: 'Backend account ID; defaults to token sub, which may differ from the login name' }, loginName: { type: 'string', description: 'Backend login name from token name; omitted when unavailable' }, clientId: { type: 'string', description: 'CLI client ID, separate from account identity' }, displayName: { type: 'string', description: 'Backend display name; omitted when unavailable' }, accessExpiresAt: { type: 'number' }, refreshExpiresAt: { type: 'number' }, next: { type: 'string', description: 'What to do when the approval did not arrive in time' } }, required: ['authorized'] },
|
|
12
|
+
examples: [invoke + ' status --json --non-interactive', `${invoke} status --wait-seconds ${statusWaitLimitSeconds} --json --non-interactive`],
|
|
13
|
+
async run(input, context) {
|
|
14
|
+
const seconds = Number(input['wait-seconds'] ?? 0);
|
|
15
|
+
const deadline = Date.now() + seconds * 1000;
|
|
16
|
+
for (;;) {
|
|
17
|
+
const current = await identity(input).status(context.signal);
|
|
18
|
+
if (current)
|
|
19
|
+
return current;
|
|
20
|
+
if (Date.now() >= deadline)
|
|
21
|
+
return seconds ? {
|
|
22
|
+
authorized: false,
|
|
23
|
+
next: `Not approved within ${seconds} seconds. Ask the user whether they approved in the browser. If the link is still valid, run ${statusCommand(input)} --wait-seconds ${statusWaitLimitSeconds} --json --non-interactive again; if it expired or was refused, run the init or login command again with --device-login --non-interactive --json for a new link.`,
|
|
24
|
+
} : { authorized: false };
|
|
25
|
+
await delay(Math.min(pollMs, deadline - Date.now()), undefined, { signal: context.signal });
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
// Repeats selectors such as --client-id that a host adds to these commands.
|
|
30
|
+
const statusCommand = (input) => invoke + ' status' + Object.keys(status.parameters).filter(name => name !== 'wait-seconds' && typeof input[name] === 'string').map(name => ` --${name} ${input[name]}`).join('');
|
|
31
|
+
return {
|
|
32
|
+
login: {
|
|
33
|
+
description: 'Sign in using a browser and authorize this CLI', effects: 'local', timeoutMs: 660000, interactiveTimeout: 'total',
|
|
34
|
+
parameters: {
|
|
35
|
+
'device-login': { type: 'boolean', description: 'Allow device authorization in non-interactive mode; returns the link at once and waits for approval in the background', default: false },
|
|
36
|
+
wait: waitParameter,
|
|
37
|
+
browser: { type: 'boolean', description: 'Open the authorization page in the default browser; use --no-browser to open the URL on another device', default: true },
|
|
38
|
+
},
|
|
39
|
+
result: { type: 'object', properties: { account: { type: 'string' }, status: { const: 'pending' }, ...pendingProperties, next: { type: 'string', description: 'What to do next while authorization is pending' } } },
|
|
40
|
+
examples: [invoke + ' login', invoke + ' login --device-login --json --non-interactive'],
|
|
41
|
+
async run(input, context) {
|
|
42
|
+
if (waitsInBackground(input, context) && !await identity(input).status(context.signal).then(Boolean, () => true)) {
|
|
43
|
+
return deviceLoginInBackground(input, context, link => ({ status: 'pending', ...link, next: approvalNext(link, statusCommand(input)) }));
|
|
44
|
+
}
|
|
45
|
+
return identity(input).login({ ...context, allowDeviceLogin: Boolean(input['device-login']), browser: input.browser !== false });
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
status,
|
|
49
|
+
logout: {
|
|
50
|
+
description: 'Revoke this CLI grant and delete local tokens; other devices and CLIs are unaffected', effects: 'remote',
|
|
51
|
+
result: { type: 'object', properties: { loggedOut: { const: true }, clientId: { type: 'string' }, impact: { type: 'string' }, revocation: { enum: ['revoked', 'failed', 'not-needed'] }, warning: { type: 'string' }, revocationError: errorSchema }, required: ['loggedOut', 'clientId', 'impact'] },
|
|
52
|
+
examples: [invoke + ' logout --json --non-interactive'],
|
|
53
|
+
run: (input, context) => identity(input).logout(context.signal),
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export * from './contracts.js';
|
|
2
|
+
export * from './errors.js';
|
|
3
|
+
export * from './http.js';
|
|
4
|
+
export * from './runtime.js';
|
|
5
|
+
export * from './output.js';
|
|
6
|
+
export * from './init.js';
|
|
7
|
+
export * from './prompts.js';
|
|
8
|
+
export * from './files.js';
|
|
9
|
+
export * from './environment.js';
|
|
10
|
+
export * from './deployment.js';
|
|
11
|
+
export * from './identity-commands.js';
|
|
12
|
+
export * from './schemas.js';
|
|
13
|
+
export * from './modules.js';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export * from './contracts.js';
|
|
2
|
+
export * from './errors.js';
|
|
3
|
+
export * from './http.js';
|
|
4
|
+
export * from './runtime.js';
|
|
5
|
+
export * from './output.js';
|
|
6
|
+
export * from './init.js';
|
|
7
|
+
export * from './prompts.js';
|
|
8
|
+
export * from './files.js';
|
|
9
|
+
export * from './environment.js';
|
|
10
|
+
export * from './deployment.js';
|
|
11
|
+
export * from './identity-commands.js';
|
|
12
|
+
export * from './schemas.js';
|
|
13
|
+
export * from './modules.js';
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import type { Command, Context } from './contracts.js';
|
|
2
|
+
export declare const initializationSchema: {
|
|
3
|
+
type: string;
|
|
4
|
+
required: string[];
|
|
5
|
+
properties: {
|
|
6
|
+
identity: {
|
|
7
|
+
properties: {
|
|
8
|
+
authorizationUrl: {
|
|
9
|
+
type: string;
|
|
10
|
+
};
|
|
11
|
+
userCode: {
|
|
12
|
+
type: string;
|
|
13
|
+
};
|
|
14
|
+
browserOpened: {
|
|
15
|
+
type: string;
|
|
16
|
+
description: string;
|
|
17
|
+
};
|
|
18
|
+
log: {
|
|
19
|
+
type: string;
|
|
20
|
+
description: string;
|
|
21
|
+
};
|
|
22
|
+
status: {
|
|
23
|
+
enum: string[];
|
|
24
|
+
};
|
|
25
|
+
account: {
|
|
26
|
+
type: string;
|
|
27
|
+
};
|
|
28
|
+
target: {
|
|
29
|
+
type: string;
|
|
30
|
+
};
|
|
31
|
+
reason: {
|
|
32
|
+
type: string;
|
|
33
|
+
};
|
|
34
|
+
error: {
|
|
35
|
+
type: string;
|
|
36
|
+
required: string[];
|
|
37
|
+
properties: {
|
|
38
|
+
code: {
|
|
39
|
+
enum: string[];
|
|
40
|
+
};
|
|
41
|
+
message: {
|
|
42
|
+
type: string;
|
|
43
|
+
};
|
|
44
|
+
retryable: {
|
|
45
|
+
type: string;
|
|
46
|
+
};
|
|
47
|
+
next: {
|
|
48
|
+
type: string;
|
|
49
|
+
};
|
|
50
|
+
details: {};
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
type: string;
|
|
55
|
+
required: string[];
|
|
56
|
+
};
|
|
57
|
+
service: {
|
|
58
|
+
type: string;
|
|
59
|
+
required: string[];
|
|
60
|
+
properties: {
|
|
61
|
+
status: {
|
|
62
|
+
enum: string[];
|
|
63
|
+
};
|
|
64
|
+
account: {
|
|
65
|
+
type: string;
|
|
66
|
+
};
|
|
67
|
+
target: {
|
|
68
|
+
type: string;
|
|
69
|
+
};
|
|
70
|
+
reason: {
|
|
71
|
+
type: string;
|
|
72
|
+
};
|
|
73
|
+
error: {
|
|
74
|
+
type: string;
|
|
75
|
+
required: string[];
|
|
76
|
+
properties: {
|
|
77
|
+
code: {
|
|
78
|
+
enum: string[];
|
|
79
|
+
};
|
|
80
|
+
message: {
|
|
81
|
+
type: string;
|
|
82
|
+
};
|
|
83
|
+
retryable: {
|
|
84
|
+
type: string;
|
|
85
|
+
};
|
|
86
|
+
next: {
|
|
87
|
+
type: string;
|
|
88
|
+
};
|
|
89
|
+
details: {};
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
ready: {
|
|
95
|
+
type: string;
|
|
96
|
+
};
|
|
97
|
+
next: {
|
|
98
|
+
type: string;
|
|
99
|
+
description: string;
|
|
100
|
+
};
|
|
101
|
+
skill: {
|
|
102
|
+
properties: {
|
|
103
|
+
directory: {
|
|
104
|
+
type: string;
|
|
105
|
+
};
|
|
106
|
+
version: {
|
|
107
|
+
type: string;
|
|
108
|
+
};
|
|
109
|
+
action: {
|
|
110
|
+
enum: string[];
|
|
111
|
+
};
|
|
112
|
+
incompatible: {
|
|
113
|
+
type: string;
|
|
114
|
+
};
|
|
115
|
+
changes: {
|
|
116
|
+
type: string;
|
|
117
|
+
items: {
|
|
118
|
+
type: string;
|
|
119
|
+
properties: {
|
|
120
|
+
file: {
|
|
121
|
+
type: string;
|
|
122
|
+
};
|
|
123
|
+
change: {
|
|
124
|
+
enum: string[];
|
|
125
|
+
};
|
|
126
|
+
};
|
|
127
|
+
required: string[];
|
|
128
|
+
};
|
|
129
|
+
};
|
|
130
|
+
installed: {
|
|
131
|
+
type: string;
|
|
132
|
+
items: {
|
|
133
|
+
type: string;
|
|
134
|
+
properties: {
|
|
135
|
+
directory: {
|
|
136
|
+
type: string;
|
|
137
|
+
};
|
|
138
|
+
version: {
|
|
139
|
+
type: string;
|
|
140
|
+
};
|
|
141
|
+
action: {
|
|
142
|
+
enum: string[];
|
|
143
|
+
};
|
|
144
|
+
};
|
|
145
|
+
required: string[];
|
|
146
|
+
};
|
|
147
|
+
};
|
|
148
|
+
status: {
|
|
149
|
+
enum: string[];
|
|
150
|
+
};
|
|
151
|
+
account: {
|
|
152
|
+
type: string;
|
|
153
|
+
};
|
|
154
|
+
target: {
|
|
155
|
+
type: string;
|
|
156
|
+
};
|
|
157
|
+
reason: {
|
|
158
|
+
type: string;
|
|
159
|
+
};
|
|
160
|
+
error: {
|
|
161
|
+
type: string;
|
|
162
|
+
required: string[];
|
|
163
|
+
properties: {
|
|
164
|
+
code: {
|
|
165
|
+
enum: string[];
|
|
166
|
+
};
|
|
167
|
+
message: {
|
|
168
|
+
type: string;
|
|
169
|
+
};
|
|
170
|
+
retryable: {
|
|
171
|
+
type: string;
|
|
172
|
+
};
|
|
173
|
+
next: {
|
|
174
|
+
type: string;
|
|
175
|
+
};
|
|
176
|
+
details: {};
|
|
177
|
+
};
|
|
178
|
+
};
|
|
179
|
+
};
|
|
180
|
+
type: string;
|
|
181
|
+
required: string[];
|
|
182
|
+
};
|
|
183
|
+
};
|
|
184
|
+
};
|
|
185
|
+
export interface IdentityPort {
|
|
186
|
+
inspect?(signal: AbortSignal): Promise<unknown>;
|
|
187
|
+
status(signal: AbortSignal): Promise<{
|
|
188
|
+
account?: string;
|
|
189
|
+
} | null>;
|
|
190
|
+
login(context: Context): Promise<{
|
|
191
|
+
account?: string;
|
|
192
|
+
}>;
|
|
193
|
+
credential(signal: AbortSignal, options?: {
|
|
194
|
+
forceRefresh?: boolean;
|
|
195
|
+
rejectedToken?: string;
|
|
196
|
+
}): Promise<{
|
|
197
|
+
token: string;
|
|
198
|
+
}>;
|
|
199
|
+
invalidate?(signal: AbortSignal, rejectedToken: string): Promise<void>;
|
|
200
|
+
logout(signal: AbortSignal): Promise<unknown>;
|
|
201
|
+
}
|
|
202
|
+
export interface SkillPort {
|
|
203
|
+
install(directory: string | undefined, conflict: 'error' | 'replace', signal: AbortSignal): Promise<unknown>;
|
|
204
|
+
}
|
|
205
|
+
export declare function initCommand(identity: () => IdentityPort, skill: SkillPort, clientId: string, example: string): Command;
|