@tulcn/lxz 0.0.0-stage → 0.3.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/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 +9 -0
- package/dist/auth/index.js +12 -0
- package/dist/auth/inspection.d.ts +14 -0
- package/dist/auth/inspection.js +20 -0
- package/dist/auth/modules.d.ts +12 -0
- package/dist/auth/modules.js +25 -0
- package/dist/auth/protocol.d.ts +17 -0
- package/dist/auth/protocol.js +1 -0
- package/dist/auth/provider.d.ts +36 -0
- package/dist/auth/provider.js +166 -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 +71 -0
- package/dist/auth/store.d.ts +25 -0
- package/dist/auth/store.js +170 -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 +5 -0
- package/dist/cli/deployment.js +23 -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,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);
|
|
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;
|
package/dist/cli/init.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { CliError, failure } from './errors.js';
|
|
2
|
+
import { errorSchema } from './schemas.js';
|
|
3
|
+
import { approvalNext, deviceLoginInBackground, pendingProperties, waitParameter, waitsInBackground } from './device-login.js';
|
|
4
|
+
const stageSchema = { type: 'object', required: ['status'], properties: { status: { enum: ['ready', 'failed', 'blocked', 'pending'] }, account: { type: 'string' }, target: { type: 'string' }, reason: { type: 'string' }, error: errorSchema } };
|
|
5
|
+
export const initializationSchema = {
|
|
6
|
+
type: 'object', required: ['identity', 'service', 'skill', 'ready'],
|
|
7
|
+
properties: {
|
|
8
|
+
identity: { ...stageSchema, properties: { ...stageSchema.properties, ...pendingProperties } }, service: stageSchema, ready: { type: 'boolean' },
|
|
9
|
+
next: { type: 'string', description: 'What to do next while authorization is pending' },
|
|
10
|
+
skill: { ...stageSchema, properties: {
|
|
11
|
+
...stageSchema.properties, directory: { type: 'string' }, version: { type: 'string' }, action: { enum: ['installed', 'updated', 'unchanged'] }, incompatible: { type: 'boolean' },
|
|
12
|
+
changes: { type: 'array', items: { type: 'object', properties: { file: { type: 'string' }, change: { enum: ['add', 'update', 'remove'] } }, required: ['file', 'change'] } },
|
|
13
|
+
installed: { type: 'array', items: { type: 'object', properties: { directory: { type: 'string' }, version: { type: 'string' }, action: { enum: ['installed', 'updated', 'unchanged'] } }, required: ['directory', 'version', 'action'] } },
|
|
14
|
+
} },
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
export function initCommand(identity, skill, clientId, example) {
|
|
18
|
+
return {
|
|
19
|
+
description: 'Authorize and install the Agent Skill for first use', effects: 'local', timeoutMs: 660000, interactiveTimeout: 'total',
|
|
20
|
+
parameters: {
|
|
21
|
+
'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 },
|
|
22
|
+
wait: waitParameter,
|
|
23
|
+
browser: { type: 'boolean', description: 'Open the authorization page in the default browser; use --no-browser to open the URL on another device', default: true },
|
|
24
|
+
'skills-dir': { type: 'string', description: 'Skills root; overrides LXZ_SKILLS_DIR. Default: registered roots, or ~/.agents/skills and ~/.linkwise/skills (Linkwise)' },
|
|
25
|
+
'skill-conflict': { type: 'string', description: 'Local Skill changes: error keeps them; replace overwrites managed files', choices: ['error', 'replace'], default: 'error' },
|
|
26
|
+
},
|
|
27
|
+
result: initializationSchema, examples: [example],
|
|
28
|
+
async run(input, context) {
|
|
29
|
+
// A stored session, or a failure that the stages below report, keeps the original flow.
|
|
30
|
+
if (waitsInBackground(input, context) && !await identity().status(context.signal).then(Boolean, () => true)) {
|
|
31
|
+
return deviceLoginInBackground(input, context, link => ({
|
|
32
|
+
identity: { status: 'pending', ...link, reason: 'Waiting in the background for the user to approve in a browser; the Skill is installed after approval' },
|
|
33
|
+
service: { status: 'blocked', target: clientId, reason: 'identity' }, skill: { status: 'pending' }, ready: false,
|
|
34
|
+
next: approvalNext(link, example.replace(/ init$/, '') + ' status'),
|
|
35
|
+
}));
|
|
36
|
+
}
|
|
37
|
+
const state = { identity: { status: 'pending' }, service: { status: 'blocked', target: clientId, reason: 'identity' }, skill: { status: 'pending' }, ready: false };
|
|
38
|
+
let failed = false;
|
|
39
|
+
try {
|
|
40
|
+
const auth = identity();
|
|
41
|
+
const current = await auth.status(context.signal) ?? await auth.login({ ...context, allowDeviceLogin: Boolean(input['device-login']), browser: input.browser !== false });
|
|
42
|
+
state.identity = { status: 'ready', account: current.account };
|
|
43
|
+
try {
|
|
44
|
+
await auth.credential(context.signal);
|
|
45
|
+
state.service = { status: 'ready', target: clientId };
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
context.signal.throwIfAborted();
|
|
49
|
+
const detail = failure(error);
|
|
50
|
+
if (detail.code === 'AUTH_REQUIRED' || detail.code === 'AUTH_EXPIRED')
|
|
51
|
+
state.identity = { status: 'failed', error: detail };
|
|
52
|
+
state.service = { status: 'failed', target: clientId, error: detail };
|
|
53
|
+
failed = true;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
context.signal.throwIfAborted();
|
|
58
|
+
state.identity = { status: 'failed', error: failure(error) };
|
|
59
|
+
failed = true;
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
const directory = typeof input['skills-dir'] === 'string' ? input['skills-dir'] : undefined;
|
|
63
|
+
context.signal.throwIfAborted();
|
|
64
|
+
state.skill = { status: 'ready', ...Object(await skill.install(directory, input['skill-conflict'], context.signal)) };
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
context.signal.throwIfAborted();
|
|
68
|
+
state.skill = { status: 'failed', error: failure(error) };
|
|
69
|
+
failed = true;
|
|
70
|
+
}
|
|
71
|
+
state.ready = !failed;
|
|
72
|
+
if (failed)
|
|
73
|
+
throw new CliError('INIT_INCOMPLETE', 'Initialization is incomplete; completed stages were kept.', 'Resolve each failed stage and run init again.', false, state);
|
|
74
|
+
return state;
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { CliError } from './errors.js';
|
|
2
|
+
export function registerCommandModules(modules) {
|
|
3
|
+
const commands = new Map();
|
|
4
|
+
const identities = new Set();
|
|
5
|
+
for (const module of modules) {
|
|
6
|
+
if ('enabled' in module || typeof module.commands === 'function')
|
|
7
|
+
throw new CliError('INCOMPATIBLE', `Module ${module.id} uses legacy registration. Remove enabled and omit disabled modules from the registration array. Pass a command record as commands; replace commands: () => createCommands() with commands: createCommands().`, 'Update this CLI module registration and rebuild.');
|
|
8
|
+
if (identities.has(module.id))
|
|
9
|
+
throw new CliError('CONFIGURATION', 'Duplicate feature module registration.', 'Ask the CLI maintainer to check module IDs.');
|
|
10
|
+
identities.add(module.id);
|
|
11
|
+
for (const [name, command] of Object.entries(module.commands)) {
|
|
12
|
+
if (commands.has(name))
|
|
13
|
+
throw new CliError('CONFIGURATION', `Command ${name} is registered more than once.`, 'Ask the CLI maintainer to rename commands or disable duplicate modules.');
|
|
14
|
+
commands.set(name, { ...command, module: module.id });
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return Object.fromEntries(commands);
|
|
18
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// Console decoders such as PowerShell on Chinese Windows read stdout with the local code page; ASCII JSON parses the same everywhere.
|
|
2
|
+
export function jsonText(value, space) {
|
|
3
|
+
return JSON.stringify(value, null, space).replace(/[^\x00-\x7e]/g, character => '\\u' + character.charCodeAt(0).toString(16).padStart(4, '0'));
|
|
4
|
+
}
|
|
5
|
+
function cell(value) {
|
|
6
|
+
return typeof value === 'string' ? JSON.stringify(value).slice(1, -1) : value === undefined ? '' : String(value);
|
|
7
|
+
}
|
|
8
|
+
function scalar(value) {
|
|
9
|
+
return value === null || typeof value !== 'object';
|
|
10
|
+
}
|
|
11
|
+
function rows(value) {
|
|
12
|
+
if (scalar(value))
|
|
13
|
+
return [cell(value)];
|
|
14
|
+
if (Array.isArray(value)) {
|
|
15
|
+
if (!value.length)
|
|
16
|
+
return ['[]'];
|
|
17
|
+
if (value.every(scalar))
|
|
18
|
+
return value.map(cell);
|
|
19
|
+
if (value.every(item => item !== null && !Array.isArray(item) && typeof item === 'object' && Object.values(item).every(scalar))) {
|
|
20
|
+
const columns = [...new Set(value.flatMap(item => Object.keys(item).filter(key => item[key] !== undefined)))];
|
|
21
|
+
if (columns.length)
|
|
22
|
+
return [columns.map(key => cell(key)).join('\t'), ...value.map(item => columns.map(key => Object.hasOwn(item, key) ? cell(item[key]) : '').join('\t'))];
|
|
23
|
+
}
|
|
24
|
+
return value.flatMap((item, index) => [`[${index}]:`, ...rows(item).map(line => ' ' + line)]);
|
|
25
|
+
}
|
|
26
|
+
const entries = Object.entries(value).filter(([, item]) => item !== undefined);
|
|
27
|
+
if (!entries.length)
|
|
28
|
+
return ['{}'];
|
|
29
|
+
return entries.flatMap(([key, item]) => scalar(item)
|
|
30
|
+
? [`${cell(key)}\t${cell(item)}`]
|
|
31
|
+
: [`${cell(key)}:`, ...rows(item).map(line => ' ' + line)]);
|
|
32
|
+
}
|
|
33
|
+
export function rowText(value) {
|
|
34
|
+
return typeof value === 'string' ? value : rows(value).join('\n');
|
|
35
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Readable, Writable } from 'node:stream';
|
|
2
|
+
interface PromptOptions {
|
|
3
|
+
message: string;
|
|
4
|
+
input?: Readable;
|
|
5
|
+
output?: Writable;
|
|
6
|
+
signal?: AbortSignal;
|
|
7
|
+
}
|
|
8
|
+
interface Choice<T> {
|
|
9
|
+
value: T;
|
|
10
|
+
label: string;
|
|
11
|
+
hint?: string;
|
|
12
|
+
}
|
|
13
|
+
export declare const prompts: {
|
|
14
|
+
isCancel: (value: unknown) => boolean;
|
|
15
|
+
text(options: PromptOptions & {
|
|
16
|
+
initialValue?: string;
|
|
17
|
+
validate?: (value: string | undefined) => string | undefined;
|
|
18
|
+
}): Promise<string | symbol>;
|
|
19
|
+
select<T>(options: PromptOptions & {
|
|
20
|
+
options: Choice<T>[];
|
|
21
|
+
initialValue?: T;
|
|
22
|
+
}): Promise<T | symbol>;
|
|
23
|
+
multiselect<T>(options: PromptOptions & {
|
|
24
|
+
options: Choice<T>[];
|
|
25
|
+
initialValues?: T[];
|
|
26
|
+
required?: boolean;
|
|
27
|
+
}): Promise<T[] | symbol>;
|
|
28
|
+
confirm(options: PromptOptions & {
|
|
29
|
+
initialValue?: boolean;
|
|
30
|
+
}): Promise<boolean | symbol>;
|
|
31
|
+
};
|
|
32
|
+
export declare function answer<T>(value: T | symbol): T;
|
|
33
|
+
export declare function ask(message: string, initialValue?: string, signal?: AbortSignal, validate?: (value: string) => string | undefined): Promise<string>;
|
|
34
|
+
export {};
|