@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.
Files changed (95) hide show
  1. package/README.md +63 -2
  2. package/dist/application.d.ts +2 -0
  3. package/dist/application.js +17 -0
  4. package/dist/auth/client.d.ts +5 -0
  5. package/dist/auth/client.js +14 -0
  6. package/dist/auth/gateway.d.ts +1 -0
  7. package/dist/auth/gateway.js +10 -0
  8. package/dist/auth/host.d.ts +2 -0
  9. package/dist/auth/host.js +16 -0
  10. package/dist/auth/identity.d.ts +45 -0
  11. package/dist/auth/identity.js +106 -0
  12. package/dist/auth/index.d.ts +10 -0
  13. package/dist/auth/index.js +13 -0
  14. package/dist/auth/inspection.d.ts +14 -0
  15. package/dist/auth/inspection.js +22 -0
  16. package/dist/auth/modules.d.ts +12 -0
  17. package/dist/auth/modules.js +25 -0
  18. package/dist/auth/platform.d.ts +26 -0
  19. package/dist/auth/platform.js +102 -0
  20. package/dist/auth/protocol.d.ts +17 -0
  21. package/dist/auth/protocol.js +1 -0
  22. package/dist/auth/provider.d.ts +40 -0
  23. package/dist/auth/provider.js +174 -0
  24. package/dist/auth/response.d.ts +2 -0
  25. package/dist/auth/response.js +17 -0
  26. package/dist/auth/service.d.ts +5 -0
  27. package/dist/auth/service.js +72 -0
  28. package/dist/auth/store.d.ts +26 -0
  29. package/dist/auth/store.js +171 -0
  30. package/dist/cli/browser.d.ts +1 -0
  31. package/dist/cli/browser.js +11 -0
  32. package/dist/cli/contracts.d.ts +85 -0
  33. package/dist/cli/contracts.js +51 -0
  34. package/dist/cli/deployment.d.ts +13 -0
  35. package/dist/cli/deployment.js +35 -0
  36. package/dist/cli/device-login.d.ts +29 -0
  37. package/dist/cli/device-login.js +87 -0
  38. package/dist/cli/environment.d.ts +9 -0
  39. package/dist/cli/environment.js +19 -0
  40. package/dist/cli/errors.d.ts +30 -0
  41. package/dist/cli/errors.js +22 -0
  42. package/dist/cli/files.d.ts +6 -0
  43. package/dist/cli/files.js +71 -0
  44. package/dist/cli/http.d.ts +6 -0
  45. package/dist/cli/http.js +38 -0
  46. package/dist/cli/identity-commands.d.ts +3 -0
  47. package/dist/cli/identity-commands.js +56 -0
  48. package/dist/cli/index.d.ts +13 -0
  49. package/dist/cli/index.js +13 -0
  50. package/dist/cli/init.d.ts +205 -0
  51. package/dist/cli/init.js +77 -0
  52. package/dist/cli/modules.d.ts +6 -0
  53. package/dist/cli/modules.js +18 -0
  54. package/dist/cli/output.d.ts +2 -0
  55. package/dist/cli/output.js +35 -0
  56. package/dist/cli/prompts.d.ts +34 -0
  57. package/dist/cli/prompts.js +58 -0
  58. package/dist/cli/runtime.d.ts +2 -0
  59. package/dist/cli/runtime.js +137 -0
  60. package/dist/cli/schemas.d.ts +187 -0
  61. package/dist/cli/schemas.js +46 -0
  62. package/dist/doctor.d.ts +2 -0
  63. package/dist/doctor.js +72 -0
  64. package/dist/internal/client.d.ts +6 -0
  65. package/dist/internal/client.js +51 -0
  66. package/dist/internal/host.d.ts +1 -0
  67. package/dist/internal/host.js +50 -0
  68. package/dist/internal/protocol.d.ts +27 -0
  69. package/dist/internal/protocol.js +1 -0
  70. package/dist/main.d.ts +2 -0
  71. package/dist/main.js +12 -0
  72. package/dist/skills/agents.d.ts +7 -0
  73. package/dist/skills/agents.js +11 -0
  74. package/dist/skills/artifacts.d.ts +9 -0
  75. package/dist/skills/artifacts.js +12 -0
  76. package/dist/skills/bundle.d.ts +22 -0
  77. package/dist/skills/bundle.js +77 -0
  78. package/dist/skills/client.d.ts +5 -0
  79. package/dist/skills/client.js +9 -0
  80. package/dist/skills/commands.d.ts +5 -0
  81. package/dist/skills/commands.js +20 -0
  82. package/dist/skills/delivery.d.ts +60 -0
  83. package/dist/skills/delivery.js +89 -0
  84. package/dist/skills/index.d.ts +7 -0
  85. package/dist/skills/index.js +7 -0
  86. package/dist/skills/installer.d.ts +23 -0
  87. package/dist/skills/installer.js +130 -0
  88. package/dist/skills/paths.d.ts +2 -0
  89. package/dist/skills/paths.js +28 -0
  90. package/package.json +36 -3
  91. package/scripts/install.mjs +17 -0
  92. package/skills/SKILL.md +38 -0
  93. package/skills/bundle.json +7 -0
  94. package/skills/commands.md +440 -0
  95. package/skills/features.md +12 -0
@@ -0,0 +1,130 @@
1
+ import { mkdir, readFile, writeFile, lstat, cp, rename, rm } from 'node:fs/promises';
2
+ import { createHash, randomUUID } from 'node:crypto';
3
+ import { resolve, join, dirname } from 'node:path';
4
+ import { CliError } from '../cli/index.js';
5
+ import { validateBundle, isManifest, manifestFile } from './bundle.js';
6
+ import { exists, managedFile } from './paths.js';
7
+ // Packages moved from @lianxz/<name> to @tulcn/<name>: a Skill installed by the old package belongs to the new one.
8
+ export const legacyPackageName = (name) => name.startsWith('@tulcn/') ? '@lianxz/' + name.slice('@tulcn/'.length) : undefined;
9
+ const hash = (text) => createHash('sha256').update(text).digest('hex');
10
+ export class SkillInstaller {
11
+ bundle;
12
+ constructor(bundle) {
13
+ this.bundle = bundle;
14
+ validateBundle(bundle);
15
+ }
16
+ async install(directory, conflict = 'error', signal = new AbortController().signal) {
17
+ try {
18
+ return await this.installFiles(directory, conflict, signal);
19
+ }
20
+ catch (error) {
21
+ signal.throwIfAborted();
22
+ if (error instanceof CliError)
23
+ throw error;
24
+ throw new CliError('IO_ERROR', 'Skill installation failed.', 'Check directory permissions and free disk space, then retry.');
25
+ }
26
+ }
27
+ async installFiles(directory, conflict, signal) {
28
+ signal.throwIfAborted();
29
+ const root = resolve(directory);
30
+ const target = join(root, this.bundle.name);
31
+ await mkdir(root, { recursive: true });
32
+ if ((await lstat(root)).isSymbolicLink())
33
+ throw new CliError('CONFLICT', 'The Skills root directory must not be a symbolic link.', 'Provide a real directory.');
34
+ const lock = join(root, `.${this.bundle.name}.install-lock`);
35
+ try {
36
+ await mkdir(lock);
37
+ }
38
+ catch (error) {
39
+ if (error.code === 'EEXIST')
40
+ throw new CliError('CONFLICT', 'This Skill is being installed or a previous installation was interrupted.', 'Retry later; remove this Skill .install-lock directory only after confirming no installer is running.', true);
41
+ throw new CliError('IO_ERROR', 'The Skills directory is not writable.', 'Check permissions for --skills-dir.');
42
+ }
43
+ const stage = join(root, `.${this.bundle.name}.${randomUUID()}.stage`);
44
+ const backup = join(root, `.${this.bundle.name}.${randomUUID()}.backup`);
45
+ let moved = false;
46
+ let committed = false;
47
+ try {
48
+ const stat = await exists(target);
49
+ if (stat && (!stat.isDirectory() || stat.isSymbolicLink()))
50
+ throw new CliError('CONFLICT', 'The Skill target is not a regular directory.', 'Choose another Skills directory.');
51
+ let previous;
52
+ if (stat) {
53
+ const manifestStat = await exists(join(target, manifestFile));
54
+ if (manifestStat && (!manifestStat.isFile() || manifestStat.isSymbolicLink()))
55
+ throw new CliError('CONFLICT', 'The Skill manifest is not a regular file.', 'Keep the conflicting directory and inspect its manifest.');
56
+ try {
57
+ previous = JSON.parse(await readFile(join(target, manifestFile), 'utf8'));
58
+ }
59
+ catch { /* Unmanaged directories cannot be overwritten. */ }
60
+ if (!isManifest(previous) || ![this.bundle.packageName, legacyPackageName(this.bundle.packageName)].includes(previous.packageName) || previous.name !== this.bundle.name) {
61
+ throw new CliError('CONFLICT', 'The directory belongs to another Skill or is unmanaged.', 'Keep the directory and choose another --skills-dir.');
62
+ }
63
+ }
64
+ const hashes = Object.fromEntries(Object.entries(this.bundle.files).map(([name, value]) => [name, hash(value)]));
65
+ const changes = [];
66
+ let modified = false;
67
+ for (const name of new Set([...Object.keys(previous?.hashes ?? {}), ...Object.keys(hashes)])) {
68
+ const file = join(target, name);
69
+ const fileStat = await managedFile(target, name);
70
+ let current;
71
+ if (fileStat) {
72
+ current = hash(await readFile(file, 'utf8'));
73
+ }
74
+ if (previous && current !== previous.hashes[name])
75
+ modified = true;
76
+ if (current !== hashes[name])
77
+ changes.push({ file: name, change: !hashes[name] ? 'remove' : !current ? 'add' : 'update' });
78
+ }
79
+ const incompatible = previous && (previous.cliVersion !== this.bundle.cliVersion || previous.contractVersion !== this.bundle.contractVersion);
80
+ if (modified && conflict !== 'replace')
81
+ throw new CliError('CONFLICT', 'The Skill has local changes or conflicting new files.', 'Review and back up local changes; retry with --skill-conflict replace to overwrite managed files.', false, { installedVersion: previous?.version, desiredVersion: this.bundle.version, incompatible, changes });
82
+ if (!changes.length && previous?.version === this.bundle.version && !incompatible)
83
+ return { directory: target, version: this.bundle.version, action: 'unchanged', changes: [] };
84
+ signal.throwIfAborted();
85
+ if (stat)
86
+ await cp(target, stage, { recursive: true, dereference: false });
87
+ else
88
+ await mkdir(stage);
89
+ for (const { file, change } of changes) {
90
+ signal.throwIfAborted();
91
+ if (change === 'remove')
92
+ await rm(join(stage, file));
93
+ else {
94
+ await mkdir(dirname(join(stage, file)), { recursive: true });
95
+ await writeFile(join(stage, file), this.bundle.files[file], { flag: 'w', signal });
96
+ }
97
+ }
98
+ const manifest = { format: 1, name: this.bundle.name, packageName: this.bundle.packageName, version: this.bundle.version, cliVersion: this.bundle.cliVersion, contractVersion: this.bundle.contractVersion, hashes };
99
+ await writeFile(join(stage, manifestFile), JSON.stringify(manifest, null, 2) + '\n');
100
+ signal.throwIfAborted();
101
+ if (stat) {
102
+ await rename(target, backup);
103
+ moved = true;
104
+ }
105
+ try {
106
+ await rename(stage, target);
107
+ committed = true;
108
+ }
109
+ catch (error) {
110
+ if (moved) {
111
+ try {
112
+ await rename(backup, target);
113
+ moved = false;
114
+ }
115
+ catch {
116
+ throw new CliError('IO_ERROR', 'Skill installation failed; old content remains in the backup directory.', `Check and restore ${backup} before retrying.`);
117
+ }
118
+ }
119
+ throw error;
120
+ }
121
+ return { directory: target, version: this.bundle.version, action: previous ? 'updated' : 'installed', incompatible: Boolean(incompatible), changes };
122
+ }
123
+ finally {
124
+ await rm(stage, { recursive: true, force: true });
125
+ if (moved && committed)
126
+ await rm(backup, { recursive: true, force: true });
127
+ await rm(lock, { recursive: true, force: true });
128
+ }
129
+ }
130
+ }
@@ -0,0 +1,2 @@
1
+ export declare function exists(path: string): Promise<import("fs").Stats | null>;
2
+ export declare function managedFile(root: string, relative: string): Promise<import("fs").Stats | null>;
@@ -0,0 +1,28 @@
1
+ import { lstat } from 'node:fs/promises';
2
+ import { join } from 'node:path';
3
+ import { CliError } from '../cli/index.js';
4
+ export async function exists(path) {
5
+ try {
6
+ return await lstat(path);
7
+ }
8
+ catch (error) {
9
+ if (error.code === 'ENOENT')
10
+ return null;
11
+ throw error;
12
+ }
13
+ }
14
+ export async function managedFile(root, relative) {
15
+ const parts = relative.split('/');
16
+ let current = root;
17
+ for (const [index, part] of parts.entries()) {
18
+ current = join(current, part);
19
+ const stat = await exists(current);
20
+ if (!stat)
21
+ return null;
22
+ if (stat.isSymbolicLink() || (index === parts.length - 1 ? !stat.isFile() : !stat.isDirectory()))
23
+ throw new CliError('CONFLICT', 'A managed Skill path is a link or has a file/directory conflict.', 'Keep a backup and move the conflicting content before retrying.', false, { file: relative });
24
+ if (index === parts.length - 1)
25
+ return stat;
26
+ }
27
+ return null;
28
+ }
package/package.json CHANGED
@@ -1,6 +1,39 @@
1
1
  {
2
2
  "name": "@tulcn/lxz",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.3.1",
4
+ "type": "module",
5
+ "description": "联小助 CLI 主程序与命令、授权、Skill 模块",
6
+ "license": "UNLICENSED",
7
+ "bin": {
8
+ "lxz": "dist/main.js"
9
+ },
10
+ "files": [
11
+ "dist",
12
+ "skills",
13
+ "scripts"
14
+ ],
15
+ "engines": {
16
+ "node": ">=22.12.0"
17
+ },
18
+ "dependencies": {
19
+ "citty": "^0.2.2",
20
+ "@clack/core": "^1.5.1"
21
+ },
22
+ "exports": {
23
+ "./cli": {
24
+ "types": "./dist/cli/index.d.ts",
25
+ "import": "./dist/cli/index.js"
26
+ },
27
+ "./auth": {
28
+ "types": "./dist/auth/index.d.ts",
29
+ "import": "./dist/auth/index.js"
30
+ },
31
+ "./skills": {
32
+ "types": "./dist/skills/index.d.ts",
33
+ "import": "./dist/skills/index.js"
34
+ }
35
+ },
36
+ "scripts": {
37
+ "postinstall": "node scripts/install.mjs"
38
+ }
6
39
  }
@@ -0,0 +1,17 @@
1
+ import { spawnSync } from 'node:child_process';
2
+ import { readFileSync } from 'node:fs';
3
+ import { fileURLToPath } from 'node:url';
4
+
5
+ if (process.env.LXZ_SKIP_SKILLS !== '1') {
6
+ const root = new URL('../', import.meta.url);
7
+ const pkg = JSON.parse(readFileSync(new URL('package.json', root), 'utf8'));
8
+ const [name, entry] = Object.entries(pkg.bin)[0];
9
+ const result = spawnSync(process.execPath, [fileURLToPath(new URL(entry, root)), 'install', 'skill', '--non-interactive'], {
10
+ stdio: 'inherit', windowsHide: true, timeout: 120000,
11
+ });
12
+ if (result.error || result.status !== 0) {
13
+ console.error(`${name} Agent Skill installation is incomplete.`);
14
+ console.error(`Resolve the error above, then run ${name} install skill --skills-dir <writable-directory>. Set LXZ_SKIP_SKILLS=1 to skip the package install hook.`);
15
+ process.exitCode = result.status || 1;
16
+ }
17
+ }
@@ -0,0 +1,38 @@
1
+ ---
2
+ name: lxz
3
+ description: 管理指定 CLI 的登录授权并检查运行环境
4
+ ---
5
+
6
+ # lxz
7
+
8
+ 此说明对应 @tulcn/lxz@0.3.1。优先使用已安装且版本匹配的 lxz;未安装时,可用 `pnpm dlx @tulcn/lxz@<版本>`(`<版本>` 取上面的版本号)作为调用前缀。以下命令适用于 pwsh 和 bash,选择 dlx 时替换调用前缀。
9
+
10
+ 功能定位可按需读取同目录 [features.md](features.md);它由当前命令注册自动生成,再沿子命令帮助查询参数。
11
+
12
+ ## 按任务查帮助
13
+
14
+ 先核对版本,再从 `lxz -h` 查找所需功能,沿分组逐层查询。例如授权任务先看 `lxz auth -h`,再看选定子命令的 `-h`,读取其参数、选项与示例。帮助不发起登录、不执行操作;不要凭记忆补写参数。
15
+
16
+ 需要结构化的输入和结果约束时,在选定命令或分组后加 `--schema --json`,只获取该范围的契约;根入口 `lxz --schema --json` 返回全量,仅在需要总览时使用。完整命令参考 [commands.md](commands.md),按需定位相关命令。本说明只保留流程和判断规则,子功能细节以当前帮助为准。
17
+
18
+ Agent 使用 `--non-interactive` 读取默认行数据,失败时读取 stderr。行数据使用 UTF-8,业务数据保留原文;需要程序解析时加 `--json`,先解析再读取字段,JSON 回执中的非 ASCII 字符以 `\uXXXX` 转义,解析后即为原文。
19
+
20
+ ## 和用户沟通
21
+
22
+ 用户关心业务结果,不关心命令怎么运行。回复只用业务说法,不提预览开关(如 dry-run)、命令名、参数、字段名、JSON、退出码或执行步骤;例如说“已经检查过,可以提交”,而不是“预览通过,返回 ok”。只有出错且需要用户处理时,才用业务说法说明原因和用户要做的事。授权链接和用户码例外,要原样写给用户。
23
+
24
+ ## 授权与诊断
25
+
26
+ 先确定用户要使用的业务 CLI,以及管理员分配的 CLI 标识。各 CLI 分别授权,其他 CLI 的登录不授予当前 CLI 权限;业务任务运行对应的独立 CLI,lxz 办理共用授权与诊断。
27
+
28
+ 默认连接正式授权中心;切换测试环境前确认目标地址、该环境的 CLI 登记和人员使用权,授权与令牌不跨环境复用。未知配置联系维护者,不猜测权限。状态查询和普通诊断不应自行触发登录。
29
+
30
+ 仅在用户要求登录时发起授权,先查登录帮助。`lxz auth login --client-id <CLI 标识> --device-login --non-interactive --json` 几秒内返回 `status: pending`,批准在后台等待:把 `authorizationUrl` 和 `userCode` 原样写给用户。链接写成单独一行的 Markdown 链接,例如 `[打开授权页面](authorizationUrl)`,不要写成裸 URL,链接后面也不要紧跟文字或标点(聊天窗口会把它们当成链接的一部分,点开就打不开);用户码另起一行原样写出。`browserOpened` 为 true 时请用户切到已打开的浏览器核对用户码,为 false 时请用户在系统浏览器打开链接(联小助里请右键选择用系统浏览器打开)。然后在同一轮、不等用户回话,运行结果 `next` 中带同一 `--client-id` 的 `auth status --wait-seconds 170` 命令,批准后立即返回并继续用户原来的任务。仍未批准时按 `next` 询问用户;链接有效期间不重复登录,拒绝或到期后停止,不自动重复登录。只有能实时看到输出的终端才用 `--wait` 前台等待;需要在另一设备确认时加 `--no-browser`。
31
+
32
+ 将后端失败原因与恢复提示如实告知用户。不得展示设备码、读取或复制令牌缓存,不在日志、结果或业务文档中记录访问令牌和刷新令牌。
33
+
34
+ 退出前说明本次授权撤销和本机清理的影响,并取得用户同意。撤销失败时说明服务端授权可能仍有效、需要时联系管理员;不自动重试或重新登录。人员身份与 CLI 标识不混用,缺失字段不推断,本机状态不等于实时权限核验。
35
+
36
+ ## 同步说明
37
+
38
+ 首次接入、升级或回滚后,按 `lxz install skill -h` 安装所用版本的说明,再核对版本。postinstall 可自动尝试,但不能依赖它执行;目录选择和冲突处理参数查当前帮助,保留用户修改,不擅自覆盖。
@@ -0,0 +1,7 @@
1
+ {
2
+ "name": "lxz",
3
+ "packageName": "@tulcn/lxz",
4
+ "version": "0.3.1",
5
+ "cliVersion": "0.3.1",
6
+ "contractVersion": "1.0.0"
7
+ }