@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
package/dist/doctor.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { defaultIdentity } from './auth/index.js';
|
|
3
|
+
import { CliError, failure, request, checkRuntimeVersions } from './cli/index.js';
|
|
4
|
+
async function pnpm(args, signal) {
|
|
5
|
+
return new Promise((resolve, reject) => {
|
|
6
|
+
const child = process.platform === 'win32'
|
|
7
|
+
? spawn('cmd.exe', ['/d', '/s', '/c', ['pnpm', ...args].join(' ')], { windowsHide: true, signal, timeout: 15000 })
|
|
8
|
+
: spawn('pnpm', args, { signal, timeout: 15000 });
|
|
9
|
+
let output = '';
|
|
10
|
+
child.stdout.on('data', (chunk) => { output += chunk; });
|
|
11
|
+
child.stderr.resume();
|
|
12
|
+
child.on('error', () => reject(new CliError('CONFIGURATION', 'pnpm is unavailable.', 'Install a supported pnpm version and add it to PATH.')));
|
|
13
|
+
child.on('close', (code) => code === 0 ? resolve(output.trim()) : reject(new CliError('CONFIGURATION', 'pnpm failed.', 'Repair the pnpm installation.')));
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
// The registry pnpm resolves @tulcn packages from: the scope setting when there is one, otherwise the default registry
|
|
17
|
+
async function packageRegistry(signal) {
|
|
18
|
+
const web = (value) => /^https?:\/\//i.test(value) ? value : '';
|
|
19
|
+
const value = web(await pnpm(['config', 'get', '@tulcn:registry'], signal)) || web(await pnpm(['config', 'get', 'registry'], signal));
|
|
20
|
+
if (!value)
|
|
21
|
+
throw new CliError('CONFIGURATION', 'pnpm has no package registry configured.', 'Run pnpm config get registry and repair the registry setting.');
|
|
22
|
+
return new URL(value.endsWith('/') ? value : value + '/');
|
|
23
|
+
}
|
|
24
|
+
export const doctor = {
|
|
25
|
+
description: 'Check runtime, package registry, authorization service and local session without logging in', effects: 'none',
|
|
26
|
+
parameters: { 'client-id': { type: 'string', description: 'Client ID to check; defaults to LXZ_AUTH_CLIENT_ID' } },
|
|
27
|
+
examples: ['pnpm dlx @tulcn/lxz doctor --json --non-interactive', 'pnpm dlx @tulcn/lxz doctor --client-id crm-demo1 --json --non-interactive'], result: { type: 'object', required: ['checks', 'healthy'] },
|
|
28
|
+
async run(input, context) {
|
|
29
|
+
const clientId = typeof input['client-id'] === 'string' ? input['client-id'] : undefined;
|
|
30
|
+
const checks = {};
|
|
31
|
+
let healthy = true;
|
|
32
|
+
const check = async (name, fn) => {
|
|
33
|
+
try {
|
|
34
|
+
checks[name] = { status: 'ready', data: await fn() };
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
healthy = false;
|
|
38
|
+
checks[name] = { status: 'failed', error: failure(error) };
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
await Promise.all([
|
|
42
|
+
check('runtime', async () => checkRuntimeVersions(process.versions.node, await pnpm(['--version'], context.signal))),
|
|
43
|
+
check('registry', async () => {
|
|
44
|
+
const registry = await packageRegistry(context.signal);
|
|
45
|
+
const response = await request(new URL('@tulcn%2flxz', registry), { headers: { accept: 'application/vnd.npm.install-v1+json' }, signal: context.signal });
|
|
46
|
+
await response.body?.cancel();
|
|
47
|
+
if (response.status === 401 || response.status === 403)
|
|
48
|
+
return { registry: registry.href, reachable: true, packageAccessVerified: false, next: `Configure credentials for ${registry.href}, or point @tulcn:registry at a registry that serves @tulcn packages.` };
|
|
49
|
+
if (response.status === 404)
|
|
50
|
+
throw new CliError('CONFIGURATION', `${registry.href} does not serve @tulcn/lxz.`, 'Point @tulcn:registry at a registry that serves @tulcn packages, or remove the setting to use the default registry.');
|
|
51
|
+
if (!response.ok)
|
|
52
|
+
throw new CliError('CONFIGURATION', 'Package registry check failed.', 'Check the network, the @tulcn:registry setting and registry credentials; packages are fetched before the CLI starts.');
|
|
53
|
+
return { registry: registry.href, reachable: true, packageAccessVerified: true };
|
|
54
|
+
}),
|
|
55
|
+
check('authorization', async () => {
|
|
56
|
+
const identity = defaultIdentity(clientId);
|
|
57
|
+
if (!identity.inspect)
|
|
58
|
+
throw new CliError('CONFIGURATION', 'The selected authorization module has no service diagnostics.', 'Ask the CLI maintainer to add diagnostics for the enabled authorization module.');
|
|
59
|
+
return identity.inspect(context.signal);
|
|
60
|
+
}),
|
|
61
|
+
check('identity', async () => {
|
|
62
|
+
const state = await defaultIdentity(clientId).status(context.signal);
|
|
63
|
+
if (!state)
|
|
64
|
+
throw new CliError('AUTH_REQUIRED', 'The selected CLI has no local authorization.', 'Run the CLI init command, or pnpm dlx @tulcn/lxz auth login --client-id <client-id>.');
|
|
65
|
+
return { ...state, validity: 'local-only', next: 'Run the business CLI init or a business command; the gateway checks the token and connects the business service.' };
|
|
66
|
+
}),
|
|
67
|
+
]);
|
|
68
|
+
if (!healthy)
|
|
69
|
+
throw new CliError('CONFIGURATION', 'Some environment checks failed.', 'Follow the recovery instructions for each failed check.', false, { healthy, checks });
|
|
70
|
+
return { healthy, checks };
|
|
71
|
+
},
|
|
72
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { fork } from 'node:child_process';
|
|
2
|
+
import { CliError, failure } from '../cli/errors.js';
|
|
3
|
+
export function callMain(request, { signal, env = process.env, log }) {
|
|
4
|
+
signal.throwIfAborted();
|
|
5
|
+
return new Promise((resolve, reject) => {
|
|
6
|
+
const child = fork(new URL('../main.js', import.meta.url), ['--lxz-ipc'], {
|
|
7
|
+
env, execArgv: [], windowsHide: true, stdio: ['ignore', 'ignore', 'ignore', 'ipc'],
|
|
8
|
+
});
|
|
9
|
+
let ready = false;
|
|
10
|
+
let result;
|
|
11
|
+
let killTimer;
|
|
12
|
+
const cancel = () => {
|
|
13
|
+
if (!ready) {
|
|
14
|
+
child.kill('SIGKILL');
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
child.send({ operation: 'cancel', error: failure(signal.reason) }, () => { });
|
|
18
|
+
killTimer = setTimeout(() => child.kill('SIGKILL'), 30000);
|
|
19
|
+
};
|
|
20
|
+
signal.addEventListener('abort', cancel, { once: true });
|
|
21
|
+
child.on('message', (message) => {
|
|
22
|
+
if (message.type === 'ready') {
|
|
23
|
+
ready = true;
|
|
24
|
+
if (signal.aborted)
|
|
25
|
+
child.kill('SIGKILL');
|
|
26
|
+
else
|
|
27
|
+
child.send(request, (error) => { if (error)
|
|
28
|
+
child.kill('SIGKILL'); });
|
|
29
|
+
}
|
|
30
|
+
else if (message.type === 'log')
|
|
31
|
+
log?.(message.message);
|
|
32
|
+
else
|
|
33
|
+
result = message;
|
|
34
|
+
});
|
|
35
|
+
child.once('error', () => { });
|
|
36
|
+
child.once('close', () => {
|
|
37
|
+
signal.removeEventListener('abort', cancel);
|
|
38
|
+
clearTimeout(killTimer);
|
|
39
|
+
if (signal.aborted && !(request.operation === 'logout' && result?.ok))
|
|
40
|
+
reject(signal.reason);
|
|
41
|
+
else if (result?.ok)
|
|
42
|
+
resolve(result.data);
|
|
43
|
+
else if (result) {
|
|
44
|
+
const { code, message, next, retryable, details } = result.error;
|
|
45
|
+
reject(new CliError(code, message, next, retryable, details));
|
|
46
|
+
}
|
|
47
|
+
else
|
|
48
|
+
reject(new CliError('UNAVAILABLE', 'The lxz host could not complete the operation.', 'Reinstall the CLI dependencies and retry; if it still fails, ask the maintainer to check the lxz installation.'));
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function runMainHost(): void;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { CliError, failure } from '../cli/errors.js';
|
|
2
|
+
import { executeAuthorization } from '../auth/host.js';
|
|
3
|
+
import { BundledSkill } from '../skills/delivery.js';
|
|
4
|
+
export function runMainHost() {
|
|
5
|
+
const controller = new AbortController();
|
|
6
|
+
const { signal } = controller;
|
|
7
|
+
const send = (message) => {
|
|
8
|
+
if (process.connected)
|
|
9
|
+
process.send(message, () => { });
|
|
10
|
+
};
|
|
11
|
+
const disconnected = () => controller.abort(new CliError('CANCELLED', 'The host call has ended.', 'Run the required command again.'));
|
|
12
|
+
const cancel = () => controller.abort(new CliError('CANCELLED', 'The host operation was cancelled.', 'Run the command again when needed.'));
|
|
13
|
+
process.once('disconnect', disconnected);
|
|
14
|
+
process.once('SIGINT', cancel);
|
|
15
|
+
process.once('SIGTERM', cancel);
|
|
16
|
+
let started = false;
|
|
17
|
+
process.on('message', async (request) => {
|
|
18
|
+
if (request.operation === 'cancel') {
|
|
19
|
+
const { code, message, next, retryable, details } = request.error;
|
|
20
|
+
controller.abort(new CliError(code, message, next, retryable, details));
|
|
21
|
+
}
|
|
22
|
+
else if (!started) {
|
|
23
|
+
started = true;
|
|
24
|
+
try {
|
|
25
|
+
const data = await execute(request);
|
|
26
|
+
send({ type: 'result', ok: true, data });
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
send({ type: 'result', ok: false, error: failure(error) });
|
|
30
|
+
}
|
|
31
|
+
finally {
|
|
32
|
+
process.removeListener('disconnect', disconnected);
|
|
33
|
+
process.removeListener('SIGINT', cancel);
|
|
34
|
+
process.removeListener('SIGTERM', cancel);
|
|
35
|
+
if (process.connected)
|
|
36
|
+
process.disconnect();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
send({ type: 'ready' });
|
|
41
|
+
async function execute(request) {
|
|
42
|
+
if ('root' in request) {
|
|
43
|
+
const skill = new BundledSkill(request.root, request.name);
|
|
44
|
+
return request.operation === 'skill.install'
|
|
45
|
+
? skill.install(request.directory, request.conflict, signal)
|
|
46
|
+
: skill.sync(request.directory, request.conflict, signal);
|
|
47
|
+
}
|
|
48
|
+
return executeAuthorization(request, signal, (message) => send({ type: 'log', message }));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { AuthorizationRequest } from '../auth/protocol.js';
|
|
2
|
+
import type { failure } from '../cli/errors.js';
|
|
3
|
+
export type MainRequest = AuthorizationRequest | {
|
|
4
|
+
operation: 'skill.install' | 'skill.sync';
|
|
5
|
+
root: string;
|
|
6
|
+
name: string;
|
|
7
|
+
directory?: string;
|
|
8
|
+
conflict: 'error' | 'replace';
|
|
9
|
+
};
|
|
10
|
+
export type MainMessage = {
|
|
11
|
+
type: 'ready';
|
|
12
|
+
} | {
|
|
13
|
+
type: 'log';
|
|
14
|
+
message: string;
|
|
15
|
+
} | {
|
|
16
|
+
type: 'result';
|
|
17
|
+
ok: true;
|
|
18
|
+
data: unknown;
|
|
19
|
+
} | {
|
|
20
|
+
type: 'result';
|
|
21
|
+
ok: false;
|
|
22
|
+
error: ReturnType<typeof failure>;
|
|
23
|
+
};
|
|
24
|
+
export type MainCall = MainRequest | {
|
|
25
|
+
operation: 'cancel';
|
|
26
|
+
error: ReturnType<typeof failure>;
|
|
27
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/main.d.ts
ADDED
package/dist/main.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { applyDeployment } from './cli/deployment.js';
|
|
3
|
+
applyDeployment(new URL('../', import.meta.url));
|
|
4
|
+
if (process.send && process.argv.length === 3 && process.argv[2] === '--lxz-ipc') {
|
|
5
|
+
const { runMainHost } = await import('./internal/host.js');
|
|
6
|
+
runMainHost();
|
|
7
|
+
}
|
|
8
|
+
else {
|
|
9
|
+
const { runApplication } = await import('./cli/index.js');
|
|
10
|
+
const { application } = await import('./application.js');
|
|
11
|
+
await runApplication(application);
|
|
12
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { homedir } from 'node:os';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
export const agentSkillDirectories = {
|
|
4
|
+
shared: '.agents/skills',
|
|
5
|
+
codex: '.codex/skills',
|
|
6
|
+
claude: '.claude/skills',
|
|
7
|
+
linkwise: '.linkwise/skills',
|
|
8
|
+
};
|
|
9
|
+
export function defaultSkillDirectories() {
|
|
10
|
+
return [agentSkillDirectories.shared, agentSkillDirectories.linkwise].map(path => join(homedir(), path));
|
|
11
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Application } from '../cli/index.js';
|
|
2
|
+
export declare function skillManifest(app: Pick<Application, 'name' | 'packageName' | 'version'>): {
|
|
3
|
+
name: string;
|
|
4
|
+
packageName: string;
|
|
5
|
+
version: string;
|
|
6
|
+
cliVersion: string;
|
|
7
|
+
contractVersion: string;
|
|
8
|
+
};
|
|
9
|
+
export declare function skillArtifacts(app: Application): Record<string, string>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { commandReference, contractVersion, describe } from '../cli/index.js';
|
|
2
|
+
export function skillManifest(app) {
|
|
3
|
+
return { name: app.name, packageName: app.packageName, version: app.version, cliVersion: app.version, contractVersion };
|
|
4
|
+
}
|
|
5
|
+
export function skillArtifacts(app) {
|
|
6
|
+
const features = Object.entries(describe(app).commands).map(([name, command]) => `| ${command.module ?? 'application'} | ${command.description.replaceAll('|', '\\|')} | \`${app.name} ${name} -h\` | ${command.effects} |`);
|
|
7
|
+
return {
|
|
8
|
+
'features.md': `# ${app.name} feature map\n\nGenerated from registered commands. Use this map to locate a task, then read that command help.\n\n| Module | Task | Help entry | Effects |\n| --- | --- | --- | --- |\n${features.join('\n')}\n`,
|
|
9
|
+
'commands.md': commandReference(app) + '\n',
|
|
10
|
+
'bundle.json': JSON.stringify(skillManifest(app), null, 2) + '\n',
|
|
11
|
+
};
|
|
12
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export interface SkillBundle {
|
|
2
|
+
name: string;
|
|
3
|
+
packageName: string;
|
|
4
|
+
version: string;
|
|
5
|
+
cliVersion: string;
|
|
6
|
+
contractVersion: string;
|
|
7
|
+
files: Record<string, string>;
|
|
8
|
+
}
|
|
9
|
+
export interface SkillIdentity {
|
|
10
|
+
name: string;
|
|
11
|
+
packageName: string;
|
|
12
|
+
cliVersion: string;
|
|
13
|
+
}
|
|
14
|
+
export interface Manifest extends Omit<SkillBundle, 'files'> {
|
|
15
|
+
format: 1;
|
|
16
|
+
hashes: Record<string, string>;
|
|
17
|
+
}
|
|
18
|
+
export declare const manifestFile = ".lianxz-skill.json";
|
|
19
|
+
export declare function validPaths(paths: string[]): boolean;
|
|
20
|
+
export declare function validateBundle(value: unknown, expected?: SkillIdentity): asserts value is SkillBundle;
|
|
21
|
+
export declare function isManifest(value: unknown): value is Manifest;
|
|
22
|
+
export declare function loadSkill(directory: string, expected?: SkillIdentity): Promise<SkillBundle>;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { lstat, readFile, readdir } from 'node:fs/promises';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { CliError, contractVersion } from '../cli/index.js';
|
|
4
|
+
export const manifestFile = '.lianxz-skill.json';
|
|
5
|
+
function validSegment(segment) {
|
|
6
|
+
return Boolean(segment) && segment !== '.' && segment !== '..'
|
|
7
|
+
&& !/[\\<>:"|?*\x00-\x1f]/.test(segment) && !/[. ]$/.test(segment)
|
|
8
|
+
&& !/^(?:con|prn|aux|nul|com[1-9]|lpt[1-9])(?:\.|$)/i.test(segment);
|
|
9
|
+
}
|
|
10
|
+
export function validPaths(paths) {
|
|
11
|
+
const known = new Set();
|
|
12
|
+
for (const path of paths) {
|
|
13
|
+
const segments = path.split('/');
|
|
14
|
+
if (!segments.every(validSegment) || segments[0]?.toLowerCase() === manifestFile || path.toLowerCase() === 'bundle.json')
|
|
15
|
+
return false;
|
|
16
|
+
const canonical = path.toLowerCase();
|
|
17
|
+
if (known.has(canonical))
|
|
18
|
+
return false;
|
|
19
|
+
known.add(canonical);
|
|
20
|
+
}
|
|
21
|
+
return paths.every((path) => path.split('/').slice(0, -1).every((_, index, parts) => !known.has(parts.slice(0, index + 1).join('/').toLowerCase())));
|
|
22
|
+
}
|
|
23
|
+
function record(value) {
|
|
24
|
+
return Boolean(value && typeof value === 'object' && !Array.isArray(value));
|
|
25
|
+
}
|
|
26
|
+
function metadata(value) {
|
|
27
|
+
return record(value) && ['name', 'packageName', 'version', 'cliVersion', 'contractVersion'].every((key) => typeof value[key] === 'string' && value[key].length > 0) && /^[a-z][a-z0-9-]*$/.test(value.name);
|
|
28
|
+
}
|
|
29
|
+
export function validateBundle(value, expected) {
|
|
30
|
+
if (!metadata(value) || !record(value.files))
|
|
31
|
+
throw new CliError('CONFIGURATION', 'The bundled Skill manifest or file set is invalid.', 'Reinstall this exact CLI version; if it still fails, ask the maintainer to check its bundled Skill.');
|
|
32
|
+
const files = value.files;
|
|
33
|
+
if (!files['SKILL.md'] || !validPaths(Object.keys(files)) || Object.values(files).some((content) => typeof content !== 'string'))
|
|
34
|
+
throw new CliError('CONFIGURATION', 'The bundled Skill is missing SKILL.md or contains invalid resource paths.', 'Reinstall this CLI; if it still fails, ask the maintainer to check its Skill files and directories.');
|
|
35
|
+
if (value.contractVersion !== contractVersion)
|
|
36
|
+
throw new CliError('INCOMPATIBLE', 'The Skill contract does not match this CLI.', 'Reinstall this exact CLI version; if it still fails, ask the maintainer to check bundled resources and shared component versions.');
|
|
37
|
+
if (expected && (value.name !== expected.name || value.packageName !== expected.packageName || value.cliVersion !== expected.cliVersion))
|
|
38
|
+
throw new CliError('INCOMPATIBLE', 'The bundled Skill does not match the running CLI.', 'Reinstall this exact CLI version; if it still fails, ask the maintainer to check the Skill name and version.', false, { expected, bundled: { name: value.name, packageName: value.packageName, cliVersion: value.cliVersion } });
|
|
39
|
+
}
|
|
40
|
+
export function isManifest(value) {
|
|
41
|
+
if (!metadata(value) || !record(value.hashes) || value.format !== 1)
|
|
42
|
+
return false;
|
|
43
|
+
const hashes = value.hashes;
|
|
44
|
+
return validPaths(Object.keys(hashes)) && Object.values(hashes).every((hash) => typeof hash === 'string' && /^[a-f0-9]{64}$/.test(hash));
|
|
45
|
+
}
|
|
46
|
+
export async function loadSkill(directory, expected) {
|
|
47
|
+
try {
|
|
48
|
+
const bundlePath = join(directory, 'bundle.json');
|
|
49
|
+
if (!(await lstat(bundlePath)).isFile())
|
|
50
|
+
throw new Error('invalid manifest');
|
|
51
|
+
const bundle = JSON.parse(await readFile(bundlePath, 'utf8'));
|
|
52
|
+
const files = {};
|
|
53
|
+
async function collect(relative = '') {
|
|
54
|
+
const entries = await readdir(join(directory, relative), { withFileTypes: true });
|
|
55
|
+
for (const entry of entries) {
|
|
56
|
+
const name = relative ? `${relative}/${entry.name}` : entry.name;
|
|
57
|
+
if (!relative && entry.name === 'bundle.json')
|
|
58
|
+
continue;
|
|
59
|
+
if (entry.isSymbolicLink() || (!entry.isFile() && !entry.isDirectory()))
|
|
60
|
+
throw new CliError('CONFIGURATION', 'Skill resources contain symbolic links or special files.', 'Store bundled resources as regular files and directories.');
|
|
61
|
+
if (entry.isDirectory())
|
|
62
|
+
await collect(name);
|
|
63
|
+
else
|
|
64
|
+
files[name] = await readFile(join(directory, name), 'utf8');
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
await collect();
|
|
68
|
+
const result = { ...(record(bundle) ? bundle : {}), files };
|
|
69
|
+
validateBundle(result, expected);
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
if (error instanceof CliError)
|
|
74
|
+
throw error;
|
|
75
|
+
throw new CliError('CONFIGURATION', 'Cannot read the bundled Skill.', 'Reinstall this exact CLI version; if it still fails, ask the maintainer to check the bundled Skill files.');
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { fileURLToPath } from 'node:url';
|
|
2
|
+
import { callMain } from '../internal/client.js';
|
|
3
|
+
export function createLxzSkill(root, name, { env = process.env } = {}) {
|
|
4
|
+
const source = { root: root instanceof URL ? fileURLToPath(root) : root, name };
|
|
5
|
+
return {
|
|
6
|
+
install: (directory, conflict, signal) => callMain({ ...source, operation: 'skill.install', directory, conflict }, { env, signal }),
|
|
7
|
+
sync: (directory, conflict, signal) => callMain({ ...source, operation: 'skill.sync', directory, conflict }, { env, signal }),
|
|
8
|
+
};
|
|
9
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Command } from '../cli/index.js';
|
|
2
|
+
export interface SkillSyncPort {
|
|
3
|
+
sync(directory: string | undefined, conflict: 'error' | 'replace', signal: AbortSignal): Promise<unknown>;
|
|
4
|
+
}
|
|
5
|
+
export declare function skillCommands(skill: SkillSyncPort, invoke: string): Record<string, Command>;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export function skillCommands(skill, invoke) {
|
|
2
|
+
return {
|
|
3
|
+
'install skill': {
|
|
4
|
+
description: 'Install the bundled Skill for this version; synchronize registered directories on upgrade or rollback',
|
|
5
|
+
effects: 'local',
|
|
6
|
+
parameters: {
|
|
7
|
+
'skills-dir': { type: 'string', description: 'Skills root; overrides LXZ_SKILLS_DIR. First install targets ~/.agents/skills and ~/.linkwise/skills (Linkwise)' },
|
|
8
|
+
'skill-conflict': { type: 'string', description: 'Local changes: error keeps them; replace overwrites files managed by this CLI', choices: ['error', 'replace'], default: 'error' },
|
|
9
|
+
},
|
|
10
|
+
result: { type: 'object', required: ['installed'], properties: { installed: { type: 'array', items: { type: 'object', required: ['directory', 'version', 'action'], properties: { directory: { type: 'string' }, version: { type: 'string' }, action: { enum: ['installed', 'updated', 'unchanged'] } } } } } },
|
|
11
|
+
examples: [invoke + ' install skill', invoke + ' install skill --skills-dir ./ceshi123/skills --json --non-interactive'],
|
|
12
|
+
format(result) {
|
|
13
|
+
return result.installed.map((item) => `Agent Skill: ${item.directory}\nVersion: ${item.version}; ${{ installed: 'installed', updated: 'updated', unchanged: 'already current' }[item.action]}`).join('\n\n');
|
|
14
|
+
},
|
|
15
|
+
run(input, context) {
|
|
16
|
+
return skill.sync(typeof input['skills-dir'] === 'string' ? input['skills-dir'] : undefined, input['skill-conflict'], context.signal);
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
export declare class BundledSkill {
|
|
2
|
+
readonly name: string;
|
|
3
|
+
readonly env: NodeJS.ProcessEnv;
|
|
4
|
+
readonly root: string;
|
|
5
|
+
constructor(root: string | URL, name: string, env?: NodeJS.ProcessEnv);
|
|
6
|
+
private source;
|
|
7
|
+
directory(input?: string): string;
|
|
8
|
+
private remember;
|
|
9
|
+
install(directory: string | undefined, conflict: 'error' | 'replace', signal: AbortSignal): Promise<{
|
|
10
|
+
directory: string;
|
|
11
|
+
version: string;
|
|
12
|
+
action: string;
|
|
13
|
+
changes: never[];
|
|
14
|
+
incompatible?: undefined;
|
|
15
|
+
} | {
|
|
16
|
+
directory: string;
|
|
17
|
+
version: string;
|
|
18
|
+
action: string;
|
|
19
|
+
incompatible: boolean;
|
|
20
|
+
changes: {
|
|
21
|
+
file: string;
|
|
22
|
+
change: string;
|
|
23
|
+
}[];
|
|
24
|
+
} | {
|
|
25
|
+
installed: ({
|
|
26
|
+
directory: string;
|
|
27
|
+
version: string;
|
|
28
|
+
action: string;
|
|
29
|
+
changes: never[];
|
|
30
|
+
incompatible?: undefined;
|
|
31
|
+
} | {
|
|
32
|
+
directory: string;
|
|
33
|
+
version: string;
|
|
34
|
+
action: string;
|
|
35
|
+
incompatible: boolean;
|
|
36
|
+
changes: {
|
|
37
|
+
file: string;
|
|
38
|
+
change: string;
|
|
39
|
+
}[];
|
|
40
|
+
})[];
|
|
41
|
+
}>;
|
|
42
|
+
sync(directory: string | undefined, conflict: 'error' | 'replace', signal: AbortSignal): Promise<{
|
|
43
|
+
installed: ({
|
|
44
|
+
directory: string;
|
|
45
|
+
version: string;
|
|
46
|
+
action: string;
|
|
47
|
+
changes: never[];
|
|
48
|
+
incompatible?: undefined;
|
|
49
|
+
} | {
|
|
50
|
+
directory: string;
|
|
51
|
+
version: string;
|
|
52
|
+
action: string;
|
|
53
|
+
incompatible: boolean;
|
|
54
|
+
changes: {
|
|
55
|
+
file: string;
|
|
56
|
+
change: string;
|
|
57
|
+
}[];
|
|
58
|
+
})[];
|
|
59
|
+
}>;
|
|
60
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
2
|
+
import { readFile, readdir, mkdir, writeFile, rename, rm } from 'node:fs/promises';
|
|
3
|
+
import { homedir } from 'node:os';
|
|
4
|
+
import { join, resolve } from 'node:path';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import { CliError, failure } from '../cli/index.js';
|
|
7
|
+
import { loadSkill } from './bundle.js';
|
|
8
|
+
import { SkillInstaller, legacyPackageName } from './installer.js';
|
|
9
|
+
import { defaultSkillDirectories } from './agents.js';
|
|
10
|
+
export class BundledSkill {
|
|
11
|
+
name;
|
|
12
|
+
env;
|
|
13
|
+
root;
|
|
14
|
+
constructor(root, name, env = process.env) {
|
|
15
|
+
this.name = name;
|
|
16
|
+
this.env = env;
|
|
17
|
+
this.root = root instanceof URL ? fileURLToPath(root) : root;
|
|
18
|
+
}
|
|
19
|
+
async source() {
|
|
20
|
+
const pkg = JSON.parse(await readFile(join(this.root, 'package.json'), 'utf8'));
|
|
21
|
+
const bundle = await loadSkill(join(this.root, 'skills'), { name: this.name, packageName: pkg.name, cliVersion: pkg.version });
|
|
22
|
+
const state = this.env.LXZ_SKILLS_STATE_DIR ?? join(homedir(), '.lianxz', 'skills');
|
|
23
|
+
const registryOf = (name) => join(state, createHash('sha256').update(name).digest('hex'));
|
|
24
|
+
const legacy = legacyPackageName(pkg.name);
|
|
25
|
+
return { installer: new SkillInstaller(bundle), registry: registryOf(pkg.name), legacyRegistry: legacy && registryOf(legacy) };
|
|
26
|
+
}
|
|
27
|
+
directory(input) {
|
|
28
|
+
const value = input ?? (this.env.LXZ_SKILLS_DIR || join(homedir(), '.agents', 'skills'));
|
|
29
|
+
if (!value.trim())
|
|
30
|
+
throw new CliError('INVALID_INPUT', 'The Skill installation directory must not be empty.', 'Pass --skills-dir <agent-skills-root>.');
|
|
31
|
+
return resolve(value.replace(/^~(?=$|[\\/])/, () => homedir()));
|
|
32
|
+
}
|
|
33
|
+
async remember(registry, directory) {
|
|
34
|
+
await mkdir(registry, { recursive: true, mode: 0o700 });
|
|
35
|
+
const key = process.platform === 'win32' ? directory.toLowerCase() : directory;
|
|
36
|
+
const file = join(registry, createHash('sha256').update(key).digest('hex') + '.json');
|
|
37
|
+
const temporary = `${file}.${randomUUID()}.tmp`;
|
|
38
|
+
try {
|
|
39
|
+
await writeFile(temporary, JSON.stringify({ directory }) + '\n', { mode: 0o600, flag: 'wx' });
|
|
40
|
+
await rename(temporary, file);
|
|
41
|
+
}
|
|
42
|
+
finally {
|
|
43
|
+
await rm(temporary, { force: true });
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
async install(directory, conflict, signal) {
|
|
47
|
+
const result = await this.sync(directory, conflict, signal);
|
|
48
|
+
return directory !== undefined || this.env.LXZ_SKILLS_DIR ? result.installed[0] : result;
|
|
49
|
+
}
|
|
50
|
+
async sync(directory, conflict, signal) {
|
|
51
|
+
const { installer, registry, legacyRegistry } = await this.source();
|
|
52
|
+
let targets = directory !== undefined || this.env.LXZ_SKILLS_DIR
|
|
53
|
+
? [this.directory(directory)]
|
|
54
|
+
: defaultSkillDirectories();
|
|
55
|
+
if (directory === undefined && !this.env.LXZ_SKILLS_DIR) {
|
|
56
|
+
try {
|
|
57
|
+
// Until the new package has installed anywhere, keep updating where the old @lianxz package installed
|
|
58
|
+
for (const source of [registry, legacyRegistry]) {
|
|
59
|
+
const files = source ? (await readdir(source).catch((error) => { if (error.code === 'ENOENT')
|
|
60
|
+
return []; throw error; })).filter((name) => name.endsWith('.json')) : [];
|
|
61
|
+
if (files.length) {
|
|
62
|
+
targets = await Promise.all(files.map(async (file) => JSON.parse(await readFile(join(source, file), 'utf8')).directory));
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
if (error.code !== 'ENOENT')
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const installed = [];
|
|
73
|
+
const failed = [];
|
|
74
|
+
for (const target of new Set(targets)) {
|
|
75
|
+
signal.throwIfAborted();
|
|
76
|
+
try {
|
|
77
|
+
await this.remember(registry, target);
|
|
78
|
+
installed.push(await installer.install(target, conflict, signal));
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
signal.throwIfAborted();
|
|
82
|
+
failed.push({ directory: target, error: failure(error) });
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (failed.length)
|
|
86
|
+
throw new CliError('IO_ERROR', 'Some Skill directories could not be updated; successful updates were kept.', 'Resolve each directory error and run install skill again; back up local changes before using --skill-conflict replace.', false, { installed, failed });
|
|
87
|
+
return { installed };
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { SkillBundle } from './bundle.js';
|
|
2
|
+
export declare const legacyPackageName: (name: string) => string | undefined;
|
|
3
|
+
export declare class SkillInstaller {
|
|
4
|
+
readonly bundle: SkillBundle;
|
|
5
|
+
constructor(bundle: SkillBundle);
|
|
6
|
+
install(directory: string, conflict?: 'error' | 'replace', signal?: AbortSignal): Promise<{
|
|
7
|
+
directory: string;
|
|
8
|
+
version: string;
|
|
9
|
+
action: string;
|
|
10
|
+
changes: never[];
|
|
11
|
+
incompatible?: undefined;
|
|
12
|
+
} | {
|
|
13
|
+
directory: string;
|
|
14
|
+
version: string;
|
|
15
|
+
action: string;
|
|
16
|
+
incompatible: boolean;
|
|
17
|
+
changes: {
|
|
18
|
+
file: string;
|
|
19
|
+
change: string;
|
|
20
|
+
}[];
|
|
21
|
+
}>;
|
|
22
|
+
private installFiles;
|
|
23
|
+
}
|