zcode-pro 1.5.1 → 1.6.2
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.en.md +33 -21
- package/README.md +23 -13
- package/dist/inject.js +9803 -7820
- package/package.json +3 -2
- package/src/host/cdp.mjs +13 -3
- package/src/host/helper.mjs +1591 -495
- package/src/host/main.mjs +412 -26
- package/src/host/paths.mjs +17 -9
- package/src/host/pickFolder.mjs +11 -4
- package/src/host/settings.mjs +15 -2
- package/src/host/setup.mjs +57 -18
- package/src/host/skill-stats.mjs +163 -0
- package/src/host/taskIndex.mjs +198 -66
- package/src/host/taskOrder.mjs +128 -57
- package/src/host/version.mjs +15 -7
- package/src/host/wallpaper.mjs +328 -0
- package/src/host/webdav.mjs +66 -10
- package/src/inject/core.js +300 -105
- package/src/inject/features/alias-migration.js +8 -4
- package/src/inject/features/alias.js +2 -2
- package/src/inject/features/auto-rename.js +166 -0
- package/src/inject/features/composer-provider.js +82 -63
- package/src/inject/features/copy-path.js +1 -1
- package/src/inject/features/file-menu.js +9 -3
- package/src/inject/features/idle-reclaim.js +31 -8
- package/src/inject/features/image-menu.js +2 -2
- package/src/inject/features/newtab-panels.js +13 -3
- package/src/inject/features/pinned-collapse.js +11 -3
- package/src/inject/features/plugin-card-retry.js +18 -8
- package/src/inject/features/project-menu.js +1 -1
- package/src/inject/features/relocate-dialog.js +2 -1
- package/src/inject/features/sandbox-reclaim.js +17 -1
- package/src/inject/features/session-link.js +200 -0
- package/src/inject/features/session-switch.js +61 -27
- package/src/inject/features/settings-dialog.js +544 -158
- package/src/inject/features/status-bar.js +126 -24
- package/src/inject/features/table-copy.js +18 -2
- package/src/inject/features/task-order.js +48 -14
- package/src/inject/features/theme.js +644 -99
- package/src/inject/features/title-gen.js +5 -4
- package/src/inject/features/title-lock.js +13 -8
- package/src/inject/features/turn-usage.js +69 -29
- package/src/inject/features/ws-running.js +32 -11
- package/src/inject/index.js +18 -1
- package/src/inject/provider-fields.js +2 -2
- package/src/inject/ui.js +91 -17
package/src/host/paths.mjs
CHANGED
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
// 跨平台定位 ZCode 桌面版可执行文件。
|
|
2
2
|
// 优先级:--zcode-path 参数 > ZCODEPRO_ZCODE_PATH 环境变量(进程 > 用户注册表) >
|
|
3
3
|
// 平台默认候选路径 > 注册表 App Paths/卸载表(Windows) > PATH 上的 zcode。
|
|
4
|
-
import {
|
|
4
|
+
import { accessSync, constants as fsConstants, statSync } from 'node:fs';
|
|
5
5
|
import { join } from 'node:path';
|
|
6
|
-
import {
|
|
6
|
+
import { homedir, platform } from 'node:os';
|
|
7
7
|
import { execFileSync } from 'node:child_process';
|
|
8
8
|
|
|
9
|
-
// reg 输出按行取 REG_SZ 后的值,容忍引号与 ",0"
|
|
9
|
+
// reg 输出按行取 REG_SZ / REG_EXPAND_SZ 后的值,容忍引号与 ",0" 图标序号后缀;
|
|
10
|
+
// REG_EXPAND_SZ 的 %VAR% 按当前环境变量展开(cmd 入口的同款兜底也认这种形态)
|
|
10
11
|
const reg = (args) => {
|
|
11
12
|
try {
|
|
12
13
|
return execFileSync('reg', args, { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] });
|
|
13
14
|
} catch { return ''; }
|
|
14
15
|
};
|
|
15
|
-
const valueAfter = (out) => (out.split(/
|
|
16
|
+
const valueAfter = (out) => (out.split(/REG_(?:EXPAND_)?SZ\s+/)[1] || '').trim()
|
|
17
|
+
.replace(/%([^%]+)%/g, (m, name) => process.env[name] || m);
|
|
16
18
|
|
|
17
19
|
// 安装脚本 -ZCodePath 持久化的用户级环境变量(HKCU\Environment):写入后不会传播
|
|
18
20
|
// 到已运行的资源管理器,进程环境变量读不到时从这里兜底,免注销/重启(issue #1)。
|
|
@@ -36,6 +38,7 @@ function windowsRegistryCandidates() {
|
|
|
36
38
|
seen.add(p.toLowerCase());
|
|
37
39
|
winRegistryCandidates.push(p);
|
|
38
40
|
}
|
|
41
|
+
return p;
|
|
39
42
|
};
|
|
40
43
|
// App Paths:安装器注册的可执行文件全路径(ShellExecute 同款查找)
|
|
41
44
|
for (const root of ['HKLM', 'HKCU']) {
|
|
@@ -54,10 +57,12 @@ function windowsRegistryCandidates() {
|
|
|
54
57
|
]) {
|
|
55
58
|
const out = reg(['query', key, '/s', '/v', 'DisplayIcon']);
|
|
56
59
|
for (const line of out.split(/\r?\n/)) {
|
|
57
|
-
const seg = (line.split(/
|
|
60
|
+
const seg = (line.split(/REG_(?:EXPAND_)?SZ\s+/)[1] || '').trim();
|
|
58
61
|
if (seg && /zcode/i.test(seg)) {
|
|
59
|
-
|
|
60
|
-
|
|
62
|
+
// 目录推导要用去引号去 ",0" 的干净路径:带引号的 DisplayIcon
|
|
63
|
+
//(Inno 类安装器常见 "...\app.exe",0)不然推出的候选带多余引号
|
|
64
|
+
const clean = push(seg);
|
|
65
|
+
const dir = clean.replace(/[^\\\/]+$/, '');
|
|
61
66
|
if (dir) {
|
|
62
67
|
push(join(dir, 'ZCode.exe'));
|
|
63
68
|
push(join(dir, 'zcode.exe'));
|
|
@@ -97,7 +102,6 @@ function candidates() {
|
|
|
97
102
|
'/usr/local/ZCode/zcode',
|
|
98
103
|
join(home, '.local', 'share', 'zcode', 'zcode'),
|
|
99
104
|
];
|
|
100
|
-
if (arch() === 'arm64') list.unshift('/opt/ZCode/zcode'); // AUR aarch64 同路径
|
|
101
105
|
return list;
|
|
102
106
|
}
|
|
103
107
|
|
|
@@ -107,7 +111,11 @@ export function resolveZcodeExecutable(explicit) {
|
|
|
107
111
|
if (!p) return null;
|
|
108
112
|
tried.push(p);
|
|
109
113
|
try {
|
|
110
|
-
|
|
114
|
+
// 只认可执行的普通文件:名为 ZCode.exe 的目录会被 existsSync 误判成命中
|
|
115
|
+
const st = statSync(p);
|
|
116
|
+
if (!st.isFile()) return null;
|
|
117
|
+
if (platform() !== 'win32') accessSync(p, fsConstants.X_OK); // POSIX 上还要可执行
|
|
118
|
+
return p;
|
|
111
119
|
} catch { /* ignore */ }
|
|
112
120
|
return null;
|
|
113
121
|
};
|
package/src/host/pickFolder.mjs
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import { spawn, spawnSync } from 'node:child_process';
|
|
6
6
|
import { existsSync, statSync } from 'node:fs';
|
|
7
|
-
import { dirname } from 'node:path';
|
|
7
|
+
import { dirname, isAbsolute } from 'node:path';
|
|
8
8
|
|
|
9
9
|
// kdialog(KDE 原生):起始目录参数可靠生效。
|
|
10
10
|
// xdg-desktop-portal-kde 不读 current_folder 选项(二进制中无该字符串),
|
|
@@ -126,7 +126,9 @@ function portalPickFolder(startDir, title, timeoutMs) {
|
|
|
126
126
|
const call = spawnSync('gdbus', ['call', '--session', '--dest', 'org.freedesktop.portal.Desktop',
|
|
127
127
|
'--object-path', '/org/freedesktop/portal/desktop',
|
|
128
128
|
'--method', 'org.freedesktop.portal.FileChooser.OpenFile', '', title, opts], { encoding: 'utf8' });
|
|
129
|
-
|
|
129
|
+
// gdbus 的 GVariant 文本输出写作 objectpath(无空格),旧版按 "object path"
|
|
130
|
+
// 匹配永远判失败;两种写法都认
|
|
131
|
+
if (call.status !== 0 || !/object ?path/i.test((call.stdout || '') + (call.stderr || ''))) finish(undefined);
|
|
130
132
|
});
|
|
131
133
|
|
|
132
134
|
timer = setTimeout(() => finish(null), timeoutMs);
|
|
@@ -175,13 +177,18 @@ function powershellPickFolder(startDir, title, timeoutMs = 600000) {
|
|
|
175
177
|
|
|
176
178
|
export async function pickFolderSystem(startDir, title, timeoutMs = 180000) {
|
|
177
179
|
const dialogTitle = title || '选择文件夹';
|
|
180
|
+
// 起始目录只接受绝对路径:相对路径(尤其以 -- 开头的)会被 kdialog 当成命令行
|
|
181
|
+
// 选项;不合法就不作起始目录——Windows 忽略预选(对话框自行落到默认位置),
|
|
182
|
+
// Linux 退回默认目录
|
|
183
|
+
const rawStart = String(startDir || '');
|
|
184
|
+
const startArg = rawStart && isAbsolute(rawStart) ? rawStart : '';
|
|
178
185
|
if (process.platform === 'win32') {
|
|
179
|
-
const r = await powershellPickFolder(
|
|
186
|
+
const r = await powershellPickFolder(startArg, dialogTitle, timeoutMs);
|
|
180
187
|
if (r !== undefined) return { path: r };
|
|
181
188
|
return { unavailable: true };
|
|
182
189
|
}
|
|
183
190
|
if (process.platform !== 'linux') return { unavailable: true };
|
|
184
|
-
const start = firstExistingDir(
|
|
191
|
+
const start = firstExistingDir(startArg || '/');
|
|
185
192
|
if (kdialogAvailable()) {
|
|
186
193
|
const r = await kdialogPickFolder(start, dialogTitle);
|
|
187
194
|
if (r !== undefined) return { path: r };
|
package/src/host/settings.mjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// 读用于 helper 的 /projects 端点;写仅用于“切换文件夹”后的路径引用同步,
|
|
3
3
|
// 采用原子写并保留备份。
|
|
4
4
|
|
|
5
|
-
import { copyFileSync, existsSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
5
|
+
import { copyFileSync, existsSync, readFileSync, renameSync, statSync, writeFileSync } from 'node:fs';
|
|
6
6
|
import { join } from 'node:path';
|
|
7
7
|
|
|
8
8
|
export function readSettings(settingsFile) {
|
|
@@ -24,11 +24,24 @@ export function writeSettingsAtomic(settingsFile, next) {
|
|
|
24
24
|
if (existsSync(settingsFile)) {
|
|
25
25
|
try { copyFileSync(settingsFile, backup); } catch { /* 备份失败不阻塞 */ }
|
|
26
26
|
}
|
|
27
|
-
const tmp = settingsFile
|
|
27
|
+
const tmp = `${settingsFile}.${process.pid}.${Date.now().toString(36)}.tmp`; // 防多写者共用同一 .tmp 撕裂
|
|
28
28
|
writeFileSync(tmp, JSON.stringify(next, null, 2), 'utf8');
|
|
29
29
|
renameSync(tmp, settingsFile);
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
// 读改写 setting.json 的路径引用同步:应用本体也在写这个文件,写回前核对文件
|
|
33
|
+
// mtime,期间被写过就重读重算再写(最多重算一次),别用旧快照覆盖应用刚写的条目
|
|
34
|
+
export function remapSettingsPathsAtomic(settingsFile, oldPath, newPath) {
|
|
35
|
+
const mtimeOf = () => { try { return statSync(settingsFile).mtimeMs; } catch { return 0; } };
|
|
36
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
37
|
+
const stamp = mtimeOf();
|
|
38
|
+
const next = remapSettingsPaths(readSettings(settingsFile), oldPath, newPath);
|
|
39
|
+
if (attempt === 0 && mtimeOf() !== stamp) continue; // 期间被写过:重读重算再写
|
|
40
|
+
writeSettingsAtomic(settingsFile, next);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
32
45
|
// 将 setting.json 中所有引用旧路径的字段映射到新路径,返回新 settings 对象。
|
|
33
46
|
// 替换时保留原值的尾分隔符风格(ZCode 自身多写带 / 的路径),减少与宿主写入的差异。
|
|
34
47
|
export function remapSettingsPaths(settings, oldPath, newPath) {
|
package/src/host/setup.mjs
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
// 文件编码,避开代码页问题;
|
|
16
16
|
// - 仓库内 PowerShell 脚本(install.ps1 等)必须保存为带 BOM 的 UTF-8。
|
|
17
17
|
import { execFileSync } from 'node:child_process';
|
|
18
|
-
import { accessSync, chmodSync, constants as fsConstants, copyFileSync, existsSync, mkdirSync, readFileSync, realpathSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
|
|
18
|
+
import { accessSync, chmodSync, constants as fsConstants, copyFileSync, existsSync, lstatSync, mkdirSync, readFileSync, readlinkSync, realpathSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
|
|
19
19
|
import { basename, dirname, join, sep } from 'node:path';
|
|
20
20
|
import { homedir, platform } from 'node:os';
|
|
21
21
|
import { fileURLToPath } from 'node:url';
|
|
@@ -144,6 +144,13 @@ function lnkIsOurs(lnkPath) {
|
|
|
144
144
|
|
|
145
145
|
const psQuote = (s) => String(s).replace(/'/g, "''"); // PowerShell 单引号字面量转义
|
|
146
146
|
|
|
147
|
+
// 降级快捷方式的起始位置:命令所在目录;剥不出绝对路径(PATH 里的裸命令名)就
|
|
148
|
+
// 交给调用方走回退(USERPROFILE 等),别把文件名当目录
|
|
149
|
+
const cmdWorkDir = (cmd) => {
|
|
150
|
+
const d = String(cmd).replace(/[\\/][^\\/]*$/, '');
|
|
151
|
+
return /^[A-Za-z]:[\\/]/.test(d) || /^[\\/]{2}/.test(d) ? d : '';
|
|
152
|
+
};
|
|
153
|
+
|
|
147
154
|
// 快捷方式计划(导出仅供测试审查,不执行)。desktop 为真时同时创建桌面快捷方式,
|
|
148
155
|
// 桌面目录由 PowerShell 的 GetFolderPath 解析(正确跟随 OneDrive 重定向等)。
|
|
149
156
|
// wshOk 为假(WSH 异常的机器)时降级:.lnk 直接经 cmd 最小化启动,不再依赖
|
|
@@ -159,7 +166,9 @@ export function windowsShortcutPlan(cmd, desktop = false, wshOk = true, menuLnk
|
|
|
159
166
|
' Dim parts()',
|
|
160
167
|
' ReDim parts(WScript.Arguments.Count - 1)',
|
|
161
168
|
' For i = 0 To WScript.Arguments.Count - 1',
|
|
162
|
-
'
|
|
169
|
+
" ' strip double quotes from each arg (Windows filenames never contain them)",
|
|
170
|
+
" ' an embedded quote would close the quoting and let the rest run as a command",
|
|
171
|
+
' parts(i) = """" & Replace(WScript.Arguments(i), """", "") & """"',
|
|
163
172
|
' Next',
|
|
164
173
|
' cmd = cmd & " " & Join(parts, " ")',
|
|
165
174
|
'End If',
|
|
@@ -176,15 +185,19 @@ export function windowsShortcutPlan(cmd, desktop = false, wshOk = true, menuLnk
|
|
|
176
185
|
' $l.TargetPath = "$env:SystemRoot\\System32\\wscript.exe"',
|
|
177
186
|
` $l.Arguments = '${psQuote(`"${paths.vbsPath}"`)}'`,
|
|
178
187
|
` $l.WorkingDirectory = '${psQuote(paths.vbsDir)}'`,
|
|
179
|
-
" $l.Description = 'ZCode
|
|
188
|
+
" $l.Description = 'ZCode 桌面版增强启动器(壁纸引擎、皮肤主题等,不修改客户端文件)'",
|
|
180
189
|
iconLine,
|
|
181
190
|
'}',
|
|
182
191
|
] : [
|
|
183
192
|
'function setProps($l) {',
|
|
184
193
|
' $l.TargetPath = "$env:SystemRoot\\System32\\cmd.exe"',
|
|
194
|
+
// cmd.exe 会展开参数里的 %VAR%:cmd 路径本身含 % 的安装位置会被展开成别的
|
|
195
|
+
// 文件。命令行参数没有可靠转义方式,暂不处理(vbs 主路径不受影响)
|
|
185
196
|
` $l.Arguments = '/c start "ZCodePro-Setup" /min "${psQuote(cmd)}"'`,
|
|
186
|
-
|
|
187
|
-
|
|
197
|
+
// 降级分支不建 vbsDir(目录可能不存在,快捷方式会报“起始位置无效”):
|
|
198
|
+
// 起始位置改用命令所在目录
|
|
199
|
+
` $l.WorkingDirectory = '${psQuote(cmdWorkDir(cmd) || process.env.USERPROFILE || process.env.HOME || paths.vbsDir)}'`,
|
|
200
|
+
" $l.Description = 'ZCode 桌面版增强启动器(壁纸引擎、皮肤主题等,不修改客户端文件)'",
|
|
188
201
|
iconLine,
|
|
189
202
|
' $l.WindowStyle = 7',
|
|
190
203
|
'}',
|
|
@@ -215,7 +228,7 @@ export function windowsShortcutPlan(cmd, desktop = false, wshOk = true, menuLnk
|
|
|
215
228
|
// PowerShell 的 GetFolderPath,正确跟随 OneDrive 重定向)。OneDrive 桌面重定向时
|
|
216
229
|
// 桌面在 %OneDrive%\Desktop,未重定向时在用户主目录
|
|
217
230
|
export function desktopLnkPathApprox() {
|
|
218
|
-
for (const od of [process.env.OneDrive, process.env.OneDriveConsumer]) {
|
|
231
|
+
for (const od of [process.env.OneDrive, process.env.OneDriveConsumer, process.env.OneDriveCommercial]) {
|
|
219
232
|
if (od && existsSync(join(od, 'Desktop'))) return join(od, 'Desktop', 'ZCode Pro.lnk');
|
|
220
233
|
}
|
|
221
234
|
return join(homedir(), 'Desktop', 'ZCode Pro.lnk');
|
|
@@ -233,7 +246,7 @@ function desktopEntry(execCmd) {
|
|
|
233
246
|
'[Desktop Entry]',
|
|
234
247
|
'Type=Application',
|
|
235
248
|
'Name=ZCode Pro',
|
|
236
|
-
'Comment=ZCode
|
|
249
|
+
'Comment=ZCode 桌面版增强启动器(壁纸引擎、皮肤主题等,不修改客户端文件)',
|
|
237
250
|
`Exec=${exec}`,
|
|
238
251
|
'Icon=zcode',
|
|
239
252
|
'Terminal=false',
|
|
@@ -325,7 +338,10 @@ const shortcutItem = {
|
|
|
325
338
|
// 早期版本写过 0644 的条目:即使内容无需重写也补上执行位(原因见 writeDesktopEntry)
|
|
326
339
|
chmodSync(target, 0o755);
|
|
327
340
|
const exec = (cur.match(/^Exec=(.+)$/m) || [])[1] || '';
|
|
328
|
-
|
|
341
|
+
// 去掉引号后全等比较:子串判断会把指向旧命令(如 zcode-pro-old)的条目
|
|
342
|
+
// 误当成已指向当前命令而跳过更新
|
|
343
|
+
const execCmd = exec.trim().replace(/^"(.*)"$/, '$1');
|
|
344
|
+
if (execCmd === cmd) {
|
|
329
345
|
parts.push('app-menu entry already exists and points to the current command, nothing to do');
|
|
330
346
|
} else {
|
|
331
347
|
writeDesktopEntry(target, cmd);
|
|
@@ -394,6 +410,10 @@ const shortcutItem = {
|
|
|
394
410
|
if (existsSync(app) && !existsSync(marker)) {
|
|
395
411
|
return { skipped: `${app} exists and was not created by setup, not overwritten` };
|
|
396
412
|
}
|
|
413
|
+
// 归属标记先于内容写入:中途失败(复制图标、写启动器等)留下的残包仍带标记,
|
|
414
|
+
// 下次重装直接覆盖重建;反过来(标记最后写)会留下装不进也卸不掉的半成品
|
|
415
|
+
mkdirSync(contents, { recursive: true });
|
|
416
|
+
writeFileSync(marker, '');
|
|
397
417
|
let iconCopied = false;
|
|
398
418
|
for (const src of [join('/', 'Applications', 'ZCode.app', 'Contents', 'Resources', 'app.icns'),
|
|
399
419
|
join(homedir(), 'Applications', 'ZCode.app', 'Contents', 'Resources', 'app.icns')]) {
|
|
@@ -407,20 +427,28 @@ const shortcutItem = {
|
|
|
407
427
|
}
|
|
408
428
|
mkdirSync(join(contents, 'MacOS'), { recursive: true });
|
|
409
429
|
const launcher = join(contents, 'MacOS', 'zcode-pro');
|
|
410
|
-
|
|
430
|
+
// 路径里的 " ` $ \ 按 shell 双引号规则转义:不转义时路径含这些字符会破坏
|
|
431
|
+
// 生成的启动器甚至被当命令替换求值
|
|
432
|
+
writeFileSync(launcher, `#!/bin/sh\nexec "${String(cmd).replace(/([\\"`$])/g, '\\$1')}" "$@"\n`);
|
|
411
433
|
chmodSync(launcher, 0o755);
|
|
412
434
|
writeFileSync(join(contents, 'Info.plist'), plistContent(iconCopied));
|
|
413
|
-
writeFileSync(marker, '');
|
|
414
435
|
let detail = `created ${app} (launcher -> ${cmd}${iconCopied ? ', icon copied' : ''})`;
|
|
415
436
|
if (opts.desktop) {
|
|
416
437
|
const link = macDesktopLink();
|
|
417
|
-
|
|
438
|
+
// 断链符号链接 existsSync 也返回假,会被误判成不存在,symlinkSync 反而报
|
|
439
|
+
// EEXIST 被空 catch 吞掉、桌面入口静默建不出:存在性改用 lstat 判断
|
|
440
|
+
let linkExists = true;
|
|
441
|
+
try { lstatSync(link); } catch { linkExists = false; }
|
|
442
|
+
if (linkExists) {
|
|
418
443
|
detail += '; desktop entry with the same name exists, not overwritten';
|
|
419
444
|
} else {
|
|
420
445
|
try {
|
|
421
446
|
symlinkSync(app, link);
|
|
422
447
|
detail += `; desktop ${link}`;
|
|
423
|
-
} catch {
|
|
448
|
+
} catch (err) {
|
|
449
|
+
// EEXIST(判断与创建之间被抢先建出)按“已存在不覆盖”汇报;其余失败只省略桌面入口
|
|
450
|
+
if (err?.code === 'EEXIST') detail += '; desktop entry with the same name exists, not overwritten';
|
|
451
|
+
}
|
|
424
452
|
}
|
|
425
453
|
}
|
|
426
454
|
return { detail };
|
|
@@ -454,7 +482,10 @@ const shortcutItem = {
|
|
|
454
482
|
const paths = windowsPaths();
|
|
455
483
|
const hadVbs = existsSync(paths.vbsPath);
|
|
456
484
|
const hadLnk = existsSync(paths.lnkPath);
|
|
457
|
-
|
|
485
|
+
// 桌面 .lnk 可能是唯一残留(开始菜单与 vbs 已被清掉):提前返回前一并判断,
|
|
486
|
+
// 否则桌面快捷方式漏删
|
|
487
|
+
const hadDesk = existsSync(desktopLnkPathApprox());
|
|
488
|
+
if (!hadVbs && !hadLnk && !hadDesk) return { skipped: 'no setup-created shortcuts found' };
|
|
458
489
|
// .lnk 可能已被 install.ps1 等重建并指向它自己:只有 Arguments 带本功能归属
|
|
459
490
|
// 标记才删;校验失败也保留 .lnk(宁可漏删不误删)
|
|
460
491
|
let lnkRemoved = false;
|
|
@@ -473,7 +504,7 @@ const shortcutItem = {
|
|
|
473
504
|
'}',
|
|
474
505
|
].join('\r\n'));
|
|
475
506
|
verified = true;
|
|
476
|
-
lnkRemoved = !existsSync(paths.lnkPath);
|
|
507
|
+
lnkRemoved = !existsSync(paths.lnkPath) && !existsSync(desktopLnkPathApprox());
|
|
477
508
|
} catch { /* 校验失败则不动 .lnk */ }
|
|
478
509
|
let vbsCleaned = !hadVbs;
|
|
479
510
|
if (hadVbs) {
|
|
@@ -493,7 +524,7 @@ const shortcutItem = {
|
|
|
493
524
|
}
|
|
494
525
|
}
|
|
495
526
|
const parts = [];
|
|
496
|
-
if (hadLnk) {
|
|
527
|
+
if (hadLnk || hadDesk) {
|
|
497
528
|
parts.push(lnkRemoved ? 'start-menu/desktop shortcut' : verified ? 'shortcut not created by setup, kept' : 'shortcut ownership check failed, .lnk kept');
|
|
498
529
|
}
|
|
499
530
|
if (hadVbs) parts.push(vbsCleaned ? 'generated launcher script' : 'launcher script (delete failed)');
|
|
@@ -506,9 +537,17 @@ const shortcutItem = {
|
|
|
506
537
|
if (!existsSync(marker)) return { skipped: 'no setup-created app bundle found' };
|
|
507
538
|
// setup --desktop 建的桌面入口一并清理(只删指向本应用包的链接)
|
|
508
539
|
const link = macDesktopLink();
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
} catch { /*
|
|
540
|
+
// 断链符号链接 existsSync 返回假、realpath 抛错,都会漏删:存在性用 lstat 判断
|
|
541
|
+
let linkInfo = null;
|
|
542
|
+
try { linkInfo = lstatSync(link); } catch { /* 无桌面入口 */ }
|
|
543
|
+
if (linkInfo) {
|
|
544
|
+
// 目标比较先用链接记录的文本(本功能建链时写的就是应用包路径,断链也读得出),
|
|
545
|
+
// 再退回解析后的真实路径(目标经其他符号链接中转的情形)
|
|
546
|
+
let ours = false;
|
|
547
|
+
try { ours = linkInfo.isSymbolicLink() && readlinkSync(link) === app; } catch { /* 读不出目标 */ }
|
|
548
|
+
if (!ours) { try { ours = realpathSync(link) === realpathSync(app); } catch { /* 断链且目标文本不同 */ } }
|
|
549
|
+
if (ours) rmSync(link);
|
|
550
|
+
}
|
|
512
551
|
rmSync(app, { recursive: true, force: true });
|
|
513
552
|
return { detail: `removed ${app}` };
|
|
514
553
|
},
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
// 技能清单检查:扫描本地与插件技能,按 ZCode 注入清单的条目格式拼出完整清单,
|
|
2
|
+
// 统计字符数是否超出 skills.metadataBudget(超预算时 ZCode 会把清单降级成仅名称)。
|
|
3
|
+
// 条目格式、描述 250 字符截断、默认预算 20000 均于 2026-10-28 反查 ZCode 程序
|
|
4
|
+
// (resources/glm/zcode.cjs 的 buildSkillsContent/formatSkillLine)核对,升级后需复核。
|
|
5
|
+
// 估算只要够判断超没超预算即可,不要求与程序逐字符一致(frontmatter 续行的
|
|
6
|
+
// 折叠方式等按常规处理)。
|
|
7
|
+
import { readdirSync, readFileSync, existsSync } from 'node:fs';
|
|
8
|
+
import { join, dirname, basename } from 'node:path';
|
|
9
|
+
|
|
10
|
+
export const DEFAULT_METADATA_BUDGET = 20000;
|
|
11
|
+
const DESC_LIMIT = 250; // description - when_to_use 合并后截断到 250 字符
|
|
12
|
+
const DESC_LOAD_LIMIT = 1024; // description 超 1024 字符的技能加载失败(丢弃)
|
|
13
|
+
const HEADER = 'The following skills are available for use with the Skill tool:\n\n';
|
|
14
|
+
|
|
15
|
+
// SKILL.md frontmatter:只取顶层 key: value(缩进行忽略,>/| 折叠续行);
|
|
16
|
+
// 没有 frontmatter 返回 null
|
|
17
|
+
function parseFrontmatter(text) {
|
|
18
|
+
const m = /^---[ \t]*\r?\n([\s\S]*?)\r?\n---[ \t]*(\r?\n|$)/.exec(text);
|
|
19
|
+
if (!m) return null;
|
|
20
|
+
const out = {};
|
|
21
|
+
let cur = null;
|
|
22
|
+
for (const line of m[1].split(/\r?\n/)) {
|
|
23
|
+
const kv = /^([A-Za-z_][\w-]*):[ \t]*(.*)$/.exec(line);
|
|
24
|
+
if (kv) {
|
|
25
|
+
cur = kv[1];
|
|
26
|
+
out[cur] = /^[>|][+-]?$/.test(kv[2]) ? '' : kv[2].trim();
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
if (cur && /^\s+\S/.test(line)) out[cur] = (out[cur] ? out[cur] + ' ' : '') + line.trim();
|
|
30
|
+
}
|
|
31
|
+
return out;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// 一个技能目录 → 清单条目;按 ZCode 的两级失败模型处理:
|
|
35
|
+
// 无 frontmatter 用目录名(可加载但描述为空);有 frontmatter 而缺
|
|
36
|
+
// name/description 或 description 超长则加载失败(不进清单)
|
|
37
|
+
function loadSkill(dir) {
|
|
38
|
+
const path = join(dir, 'SKILL.md');
|
|
39
|
+
if (!existsSync(path)) return null;
|
|
40
|
+
let fm;
|
|
41
|
+
try {
|
|
42
|
+
fm = parseFrontmatter(readFileSync(path, 'utf8'));
|
|
43
|
+
} catch {
|
|
44
|
+
return null; // 断链、无权限等坏条目跳过,别砸掉整个统计
|
|
45
|
+
}
|
|
46
|
+
let name;
|
|
47
|
+
let description = '';
|
|
48
|
+
let whenToUse = '';
|
|
49
|
+
if (fm) {
|
|
50
|
+
name = (fm.name || '').trim();
|
|
51
|
+
description = (fm.description || '').trim();
|
|
52
|
+
whenToUse = (fm.when_to_use || '').trim();
|
|
53
|
+
if (!name || !description || description.length > DESC_LOAD_LIMIT) return null;
|
|
54
|
+
} else {
|
|
55
|
+
name = basename(dir);
|
|
56
|
+
}
|
|
57
|
+
return { name, description, whenToUse, path };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const listDirs = (dir) => {
|
|
61
|
+
try {
|
|
62
|
+
return readdirSync(dir, { withFileTypes: true })
|
|
63
|
+
.filter((d) => d.isDirectory() && (!d.name.startsWith('.') || d.name === '.system') && d.name !== 'node_modules')
|
|
64
|
+
.map((d) => join(dir, d.name));
|
|
65
|
+
} catch {
|
|
66
|
+
return [];
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
// 扫一个技能根目录:每个直接子目录里有 SKILL.md 即一个技能
|
|
71
|
+
function scanRoot(root, pluginId) {
|
|
72
|
+
const out = [];
|
|
73
|
+
for (const dir of listDirs(root)) {
|
|
74
|
+
const entry = loadSkill(dir);
|
|
75
|
+
if (entry) {
|
|
76
|
+
// 插件技能的限定名形如 插件名:技能名,与裸名不同还附“也可以用裸名加载”
|
|
77
|
+
if (pluginId) entry.qualifiedName = `${pluginId}:${entry.name}`;
|
|
78
|
+
out.push(entry);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return out;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// 插件缓存目录按 <市场>/<插件>/<版本>/skills 布局,同一插件只取最高版本
|
|
85
|
+
function pluginVersionDirs(cacheDir) {
|
|
86
|
+
const byPlugin = new Map();
|
|
87
|
+
for (const marketDir of listDirs(cacheDir)) {
|
|
88
|
+
const market = basename(marketDir);
|
|
89
|
+
for (const pluginDir of listDirs(marketDir)) {
|
|
90
|
+
const plugin = basename(pluginDir);
|
|
91
|
+
const key = `${market}/${plugin}`;
|
|
92
|
+
const versions = listDirs(pluginDir).map((v) => basename(v));
|
|
93
|
+
if (!versions.length) continue;
|
|
94
|
+
versions.sort((a, b) => {
|
|
95
|
+
const pa = a.split('.').map((n) => parseInt(n, 10) || 0);
|
|
96
|
+
const pb = b.split('.').map((n) => parseInt(n, 10) || 0);
|
|
97
|
+
for (let i = 0; i < Math.max(pa.length, pb.length); i++) {
|
|
98
|
+
if ((pa[i] || 0) !== (pb[i] || 0)) return (pa[i] || 0) - (pb[i] || 0);
|
|
99
|
+
}
|
|
100
|
+
return 0;
|
|
101
|
+
});
|
|
102
|
+
byPlugin.set(key, { market, plugin, versionDir: join(pluginDir, versions[versions.length - 1]) });
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return [...byPlugin.values()];
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// 发现顺序(与 ZCode 一致):显式配置的目录 → ~/.zcode/skills → ~/.agents/skills
|
|
109
|
+
// → 启用中的插件(最低优先级)。同一技能名出现在多个路径时清单里都算(条目身份
|
|
110
|
+
// 是文件路径),这里不做同名去重。项目内的 .zcode/.agents 技能随项目而变,不计入。
|
|
111
|
+
export function scanSkills({ dataRoot, roots = [], pluginsEnabled = true, enabledPlugins = {}, suppressedBuiltins = [] }) {
|
|
112
|
+
const userRoots = [
|
|
113
|
+
...roots,
|
|
114
|
+
join(dataRoot, 'skills'),
|
|
115
|
+
join(dirname(dataRoot), '.agents', 'skills'),
|
|
116
|
+
];
|
|
117
|
+
const entries = [];
|
|
118
|
+
for (const root of userRoots) entries.push(...scanRoot(root));
|
|
119
|
+
const pluginEntries = [];
|
|
120
|
+
if (pluginsEnabled) {
|
|
121
|
+
for (const { market, plugin, versionDir } of pluginVersionDirs(join(dataRoot, 'cli', 'plugins', 'cache'))) {
|
|
122
|
+
const id = `${plugin}@${market}`;
|
|
123
|
+
if (enabledPlugins[id] === false || suppressedBuiltins.includes(plugin) || suppressedBuiltins.includes(id)) continue;
|
|
124
|
+
pluginEntries.push(...scanRoot(join(versionDir, 'skills'), plugin));
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return { entries, pluginEntries };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// 清单条目行(ZCode formatSkillLine)
|
|
131
|
+
function formatLine(e) {
|
|
132
|
+
const display = e.qualifiedName ?? e.name;
|
|
133
|
+
const alias = e.qualifiedName && e.qualifiedName !== e.name ? ` (also loadable as ${e.name})` : '';
|
|
134
|
+
const desc = e.whenToUse ? `${e.description} - ${e.whenToUse}` : e.description;
|
|
135
|
+
const cut = desc.length > DESC_LIMIT ? `${desc.slice(0, DESC_LIMIT - 1)}...` : desc;
|
|
136
|
+
return `- ${display}: ${cut}${alias} (file: ${e.path})`;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// 完整清单字符数(含表头)——ZCode 拿这个长度和预算比,超了就降级成仅名称
|
|
140
|
+
function fullContentChars(entries) {
|
|
141
|
+
const lines = [...entries]
|
|
142
|
+
.sort((a, b) => (a.qualifiedName ?? a.name).localeCompare(b.qualifiedName ?? b.name))
|
|
143
|
+
.map(formatLine);
|
|
144
|
+
return (HEADER + lines.join('\n')).length;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function skillStats(opts) {
|
|
148
|
+
const { entries, pluginEntries } = scanSkills(opts);
|
|
149
|
+
const all = [...entries, ...pluginEntries];
|
|
150
|
+
const fullChars = fullContentChars(all);
|
|
151
|
+
const budget = Number.isFinite(opts.budget) && opts.budget >= 0 ? opts.budget : DEFAULT_METADATA_BUDGET;
|
|
152
|
+
return {
|
|
153
|
+
total: all.length,
|
|
154
|
+
userCount: entries.length,
|
|
155
|
+
pluginCount: pluginEntries.length,
|
|
156
|
+
fullChars,
|
|
157
|
+
budget,
|
|
158
|
+
defaultBudget: DEFAULT_METADATA_BUDGET,
|
|
159
|
+
degraded: fullChars > budget,
|
|
160
|
+
// 建议预算:完整清单加 20% 余量,向上取整到千
|
|
161
|
+
suggested: Math.ceil((fullChars * 1.2) / 1000) * 1000,
|
|
162
|
+
};
|
|
163
|
+
}
|