codexmate 0.0.27 → 0.0.28
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 +421 -421
- package/README.zh.md +354 -354
- package/cli/agents-files.js +224 -224
- package/cli/archive-helpers.js +446 -446
- package/cli/auth-profiles.js +375 -375
- package/cli/builtin-proxy.js +1620 -1299
- package/cli/claude-proxy.js +1022 -1022
- package/cli/config-bootstrap.js +384 -384
- package/cli/config-health.js +338 -338
- package/cli/doctor-core.js +903 -903
- package/cli/import-skills-url.js +356 -356
- package/cli/openai-bridge.js +1489 -1091
- package/cli/openclaw-config.js +629 -629
- package/cli/session-convert-args.js +65 -65
- package/cli/session-convert-io.js +82 -82
- package/cli/session-convert.js +43 -43
- package/cli/session-usage.concurrent.js +28 -28
- package/cli/session-usage.js +118 -118
- package/cli/session-usage.models.js +176 -176
- package/cli/skills.js +1141 -1141
- package/cli/zip-commands.js +510 -510
- package/cli.js +15264 -15251
- package/lib/automation.js +404 -404
- package/lib/cli-file-utils.js +151 -151
- package/lib/cli-models-utils.js +440 -379
- package/lib/cli-network-utils.js +190 -190
- package/lib/cli-path-utils.js +85 -85
- package/lib/cli-session-utils.js +121 -121
- package/lib/cli-sessions.js +417 -417
- package/lib/cli-utils.js +155 -155
- package/lib/download-artifacts.js +92 -92
- package/lib/mcp-stdio.js +453 -453
- package/lib/task-orchestrator.js +869 -869
- package/lib/text-diff.js +303 -303
- package/lib/workflow-engine.js +340 -340
- package/package.json +3 -1
- package/plugins/README.md +20 -20
- package/plugins/README.zh-CN.md +20 -20
- package/plugins/prompt-templates/comment-polish/index.mjs +25 -25
- package/plugins/prompt-templates/computed.mjs +253 -253
- package/plugins/prompt-templates/index.mjs +8 -8
- package/plugins/prompt-templates/manifest.mjs +15 -15
- package/plugins/prompt-templates/methods.mjs +553 -619
- package/plugins/prompt-templates/overview.mjs +91 -90
- package/plugins/prompt-templates/ownership.mjs +19 -19
- package/plugins/prompt-templates/rule-ack/index.mjs +21 -21
- package/plugins/prompt-templates/storage.mjs +64 -64
- package/plugins/registry.mjs +16 -16
- package/web-ui/app.js +625 -634
- package/web-ui/index.html +35 -35
- package/web-ui/logic.agents-diff.mjs +386 -386
- package/web-ui/logic.claude.mjs +168 -168
- package/web-ui/logic.codex.mjs +56 -0
- package/web-ui/logic.mjs +5 -5
- package/web-ui/logic.runtime.mjs +128 -128
- package/web-ui/logic.session-convert.mjs +70 -70
- package/web-ui/logic.sessions.mjs +765 -765
- package/web-ui/modules/api.mjs +90 -90
- package/web-ui/modules/app.computed.dashboard.mjs +225 -171
- package/web-ui/modules/app.computed.index.mjs +17 -17
- package/web-ui/modules/app.computed.main-tabs.mjs +205 -205
- package/web-ui/modules/app.computed.session.mjs +994 -994
- package/web-ui/modules/app.constants.mjs +15 -15
- package/web-ui/modules/app.methods.agents.mjs +632 -632
- package/web-ui/modules/app.methods.claude-config.mjs +190 -184
- package/web-ui/modules/app.methods.codex-config.mjs +892 -860
- package/web-ui/modules/app.methods.index.mjs +92 -92
- package/web-ui/modules/app.methods.install.mjs +205 -205
- package/web-ui/modules/app.methods.navigation.mjs +743 -743
- package/web-ui/modules/app.methods.openclaw-core.mjs +814 -814
- package/web-ui/modules/app.methods.openclaw-editing.mjs +372 -372
- package/web-ui/modules/app.methods.openclaw-persist.mjs +369 -369
- package/web-ui/modules/app.methods.providers.mjs +412 -404
- package/web-ui/modules/app.methods.runtime.mjs +345 -345
- package/web-ui/modules/app.methods.session-actions.mjs +593 -596
- package/web-ui/modules/app.methods.session-browser.mjs +984 -989
- package/web-ui/modules/app.methods.session-timeline.mjs +479 -479
- package/web-ui/modules/app.methods.session-trash.mjs +439 -439
- package/web-ui/modules/app.methods.startup-claude.mjs +533 -526
- package/web-ui/modules/app.methods.task-orchestration.mjs +556 -556
- package/web-ui/modules/config-mode.computed.mjs +124 -124
- package/web-ui/modules/config-template-confirm-pref.mjs +33 -33
- package/web-ui/modules/i18n.dict.mjs +2109 -2131
- package/web-ui/modules/i18n.mjs +56 -56
- package/web-ui/modules/plugins.computed.mjs +3 -3
- package/web-ui/modules/plugins.methods.mjs +3 -3
- package/web-ui/modules/plugins.storage.mjs +11 -11
- package/web-ui/modules/provider-url-display.mjs +17 -0
- package/web-ui/modules/sessions-filters-url.mjs +85 -85
- package/web-ui/modules/skills.computed.mjs +107 -107
- package/web-ui/modules/skills.methods.mjs +481 -481
- package/web-ui/partials/index/layout-footer.html +13 -13
- package/web-ui/partials/index/layout-header.html +475 -475
- package/web-ui/partials/index/modal-config-template-agents.html +174 -174
- package/web-ui/partials/index/modal-confirm-toast.html +32 -32
- package/web-ui/partials/index/modal-health-check.html +45 -45
- package/web-ui/partials/index/modal-openclaw-config.html +280 -280
- package/web-ui/partials/index/modal-skills.html +200 -200
- package/web-ui/partials/index/modals-basic.html +165 -165
- package/web-ui/partials/index/panel-config-claude.html +188 -187
- package/web-ui/partials/index/panel-config-codex.html +312 -283
- package/web-ui/partials/index/panel-config-openclaw.html +83 -83
- package/web-ui/partials/index/panel-dashboard.html +186 -186
- package/web-ui/partials/index/panel-docs.html +147 -147
- package/web-ui/partials/index/panel-market.html +177 -177
- package/web-ui/partials/index/panel-orchestration.html +391 -391
- package/web-ui/partials/index/panel-plugins.html +253 -279
- package/web-ui/partials/index/panel-sessions.html +316 -326
- package/web-ui/partials/index/panel-settings.html +253 -274
- package/web-ui/partials/index/panel-usage.html +371 -371
- package/web-ui/res/json5.min.js +1 -1
- package/web-ui/res/vue.global.prod.js +13 -13
- package/web-ui/session-helpers.mjs +576 -576
- package/web-ui/source-bundle.cjs +233 -233
- package/web-ui/styles/base-theme.css +281 -268
- package/web-ui/styles/controls-forms.css +422 -423
- package/web-ui/styles/dashboard.css +274 -274
- package/web-ui/styles/docs-panel.css +247 -247
- package/web-ui/styles/feedback.css +108 -108
- package/web-ui/styles/health-check-dialog.css +144 -144
- package/web-ui/styles/layout-shell.css +606 -603
- package/web-ui/styles/modals-core.css +466 -464
- package/web-ui/styles/navigation-panels.css +391 -390
- package/web-ui/styles/openclaw-structured.css +266 -266
- package/web-ui/styles/plugins-panel.css +523 -523
- package/web-ui/styles/responsive.css +454 -454
- package/web-ui/styles/sessions-list.css +419 -415
- package/web-ui/styles/sessions-preview.css +411 -411
- package/web-ui/styles/sessions-toolbar-trash.css +330 -330
- package/web-ui/styles/sessions-usage.css +1040 -1040
- package/web-ui/styles/settings-panel.css +185 -185
- package/web-ui/styles/skills-list.css +303 -303
- package/web-ui/styles/skills-market.css +406 -406
- package/web-ui/styles/task-orchestration.css +822 -822
- package/web-ui/styles/titles-cards.css +472 -408
- package/web-ui/styles.css +21 -21
- package/web-ui.html +17 -17
package/cli/skills.js
CHANGED
|
@@ -1,1141 +1,1141 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
const os = require('os');
|
|
4
|
-
const crypto = require('crypto');
|
|
5
|
-
const zipLib = require('zip-lib');
|
|
6
|
-
const yauzl = require('yauzl');
|
|
7
|
-
const { execSync } = require('child_process');
|
|
8
|
-
|
|
9
|
-
const {
|
|
10
|
-
resolveExistingDir,
|
|
11
|
-
stripUtf8Bom
|
|
12
|
-
} = require('../lib/cli-utils');
|
|
13
|
-
const { ensureDir } = require('../lib/cli-file-utils');
|
|
14
|
-
const {
|
|
15
|
-
isPathInside,
|
|
16
|
-
resolveCopyTargetRoot,
|
|
17
|
-
commandExists
|
|
18
|
-
} = require('../lib/cli-path-utils');
|
|
19
|
-
const { registerDownloadArtifact } = require('../lib/download-artifacts');
|
|
20
|
-
const { createArchiveHelperController } = require('./archive-helpers');
|
|
21
|
-
|
|
22
|
-
const CONFIG_DIR = path.join(os.homedir(), '.codex');
|
|
23
|
-
const CLAUDE_DIR = path.join(os.homedir(), '.claude');
|
|
24
|
-
const CODEX_SKILLS_DIR = path.join(CONFIG_DIR, 'skills');
|
|
25
|
-
const CLAUDE_SKILLS_DIR = path.join(CLAUDE_DIR, 'skills');
|
|
26
|
-
const AGENTS_SKILLS_DIR = path.join(os.homedir(), '.agents', 'skills');
|
|
27
|
-
|
|
28
|
-
const MAX_UPLOAD_SIZE = 200 * 1024 * 1024;
|
|
29
|
-
const MAX_SKILLS_ZIP_UPLOAD_SIZE = 20 * 1024 * 1024;
|
|
30
|
-
const MAX_SKILLS_ZIP_ENTRY_COUNT = 2000;
|
|
31
|
-
const MAX_SKILLS_ZIP_UNCOMPRESSED_BYTES = 512 * 1024 * 1024;
|
|
32
|
-
|
|
33
|
-
function getCodexSkillsDir() {
|
|
34
|
-
const joinPath = (basePath, ...segments) => {
|
|
35
|
-
const base = typeof basePath === 'string' ? basePath.trim() : '';
|
|
36
|
-
const pathApi = base.includes('/') && !base.includes('\\') && path.posix ? path.posix : path;
|
|
37
|
-
return pathApi.join(base, ...segments);
|
|
38
|
-
};
|
|
39
|
-
const envCodexHome = typeof process.env.CODEX_HOME === 'string' ? process.env.CODEX_HOME.trim() : '';
|
|
40
|
-
if (envCodexHome) {
|
|
41
|
-
const target = joinPath(envCodexHome, 'skills');
|
|
42
|
-
return resolveExistingDir([target], target);
|
|
43
|
-
}
|
|
44
|
-
const xdgConfig = typeof process.env.XDG_CONFIG_HOME === 'string' ? process.env.XDG_CONFIG_HOME.trim() : '';
|
|
45
|
-
if (xdgConfig) {
|
|
46
|
-
const target = joinPath(xdgConfig, 'codex', 'skills');
|
|
47
|
-
return resolveExistingDir([target], target);
|
|
48
|
-
}
|
|
49
|
-
const homeConfigDir = joinPath(os.homedir(), '.config', 'codex', 'skills');
|
|
50
|
-
return resolveExistingDir([homeConfigDir], CODEX_SKILLS_DIR);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function getClaudeSkillsDir() {
|
|
54
|
-
const joinPath = (basePath, ...segments) => {
|
|
55
|
-
const base = typeof basePath === 'string' ? basePath.trim() : '';
|
|
56
|
-
const pathApi = base.includes('/') && !base.includes('\\') && path.posix ? path.posix : path;
|
|
57
|
-
return pathApi.join(base, ...segments);
|
|
58
|
-
};
|
|
59
|
-
const envClaudeHome = typeof process.env.CLAUDE_HOME === 'string' && process.env.CLAUDE_HOME.trim()
|
|
60
|
-
? process.env.CLAUDE_HOME.trim()
|
|
61
|
-
: (typeof process.env.CLAUDE_CONFIG_DIR === 'string' ? process.env.CLAUDE_CONFIG_DIR.trim() : '');
|
|
62
|
-
if (envClaudeHome) {
|
|
63
|
-
const target = joinPath(envClaudeHome, 'skills');
|
|
64
|
-
return resolveExistingDir([target], target);
|
|
65
|
-
}
|
|
66
|
-
const xdgConfig = typeof process.env.XDG_CONFIG_HOME === 'string' ? process.env.XDG_CONFIG_HOME.trim() : '';
|
|
67
|
-
if (xdgConfig) {
|
|
68
|
-
const target = joinPath(xdgConfig, 'claude', 'skills');
|
|
69
|
-
return resolveExistingDir([target], target);
|
|
70
|
-
}
|
|
71
|
-
const homeConfigDir = joinPath(os.homedir(), '.config', 'claude', 'skills');
|
|
72
|
-
return resolveExistingDir([homeConfigDir], CLAUDE_SKILLS_DIR);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const SKILL_TARGETS = Object.freeze([
|
|
76
|
-
Object.freeze({ app: 'codex', label: 'Codex', dir: getCodexSkillsDir() }),
|
|
77
|
-
Object.freeze({ app: 'claude', label: 'Claude Code', dir: getClaudeSkillsDir() })
|
|
78
|
-
]);
|
|
79
|
-
|
|
80
|
-
const SKILL_IMPORT_SOURCES = Object.freeze([
|
|
81
|
-
...SKILL_TARGETS,
|
|
82
|
-
Object.freeze({ app: 'agents', label: 'Agents', dir: AGENTS_SKILLS_DIR })
|
|
83
|
-
]);
|
|
84
|
-
|
|
85
|
-
const {
|
|
86
|
-
copyDirRecursive,
|
|
87
|
-
inspectZipArchiveLimits,
|
|
88
|
-
writeUploadZip,
|
|
89
|
-
extractUploadZip
|
|
90
|
-
} = createArchiveHelperController({
|
|
91
|
-
fs,
|
|
92
|
-
path,
|
|
93
|
-
os,
|
|
94
|
-
execSync,
|
|
95
|
-
zipLib,
|
|
96
|
-
yauzl,
|
|
97
|
-
ensureDir,
|
|
98
|
-
isPathInside,
|
|
99
|
-
commandExists,
|
|
100
|
-
MAX_UPLOAD_SIZE,
|
|
101
|
-
MAX_SKILLS_ZIP_UPLOAD_SIZE,
|
|
102
|
-
MAX_SKILLS_ZIP_ENTRY_COUNT,
|
|
103
|
-
MAX_SKILLS_ZIP_UNCOMPRESSED_BYTES
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
function normalizeCodexSkillName(name) {
|
|
107
|
-
const value = typeof name === 'string' ? name.trim() : '';
|
|
108
|
-
if (!value) {
|
|
109
|
-
return { error: '技能名称不能为空' };
|
|
110
|
-
}
|
|
111
|
-
if (value.includes('\0')) {
|
|
112
|
-
return { error: '技能名称非法' };
|
|
113
|
-
}
|
|
114
|
-
if (value === '.' || value === '..') {
|
|
115
|
-
return { error: '技能名称非法' };
|
|
116
|
-
}
|
|
117
|
-
if (value.includes('/') || value.includes('\\')) {
|
|
118
|
-
return { error: '技能名称非法' };
|
|
119
|
-
}
|
|
120
|
-
if (path.basename(value) !== value) {
|
|
121
|
-
return { error: '技能名称非法' };
|
|
122
|
-
}
|
|
123
|
-
if (value.startsWith('.')) {
|
|
124
|
-
return { error: '系统技能不可删除' };
|
|
125
|
-
}
|
|
126
|
-
return { name: value };
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
function normalizeSkillTargetApp(app) {
|
|
130
|
-
const value = typeof app === 'string' ? app.trim().toLowerCase() : '';
|
|
131
|
-
return SKILL_TARGETS.some((item) => item.app === value) ? value : '';
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
function getSkillTargetByApp(app) {
|
|
135
|
-
const normalizedApp = normalizeSkillTargetApp(app);
|
|
136
|
-
if (!normalizedApp) return null;
|
|
137
|
-
return SKILL_TARGETS.find((item) => item.app === normalizedApp) || null;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
function resolveSkillTarget(params = {}, defaultApp = 'codex') {
|
|
141
|
-
const hasExplicitTargetApp = !!(params && typeof params === 'object'
|
|
142
|
-
&& Object.prototype.hasOwnProperty.call(params, 'targetApp'));
|
|
143
|
-
const hasExplicitTarget = !!(params && typeof params === 'object'
|
|
144
|
-
&& Object.prototype.hasOwnProperty.call(params, 'target'));
|
|
145
|
-
const hasAnyExplicitTarget = hasExplicitTargetApp || hasExplicitTarget;
|
|
146
|
-
const rawTargetApp = hasExplicitTargetApp ? params.targetApp : '';
|
|
147
|
-
const rawTarget = hasExplicitTarget ? params.target : '';
|
|
148
|
-
const raw = rawTargetApp || rawTarget || '';
|
|
149
|
-
if (hasAnyExplicitTarget && raw === '') {
|
|
150
|
-
return null;
|
|
151
|
-
}
|
|
152
|
-
if (hasAnyExplicitTarget && !getSkillTargetByApp(raw)) {
|
|
153
|
-
return null;
|
|
154
|
-
}
|
|
155
|
-
return getSkillTargetByApp(raw)
|
|
156
|
-
|| getSkillTargetByApp(defaultApp)
|
|
157
|
-
|| SKILL_TARGETS[0]
|
|
158
|
-
|| null;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
function isSkillDirectoryEntryAtRoot(rootDir, entryName) {
|
|
162
|
-
const targetPath = path.join(rootDir, entryName);
|
|
163
|
-
try {
|
|
164
|
-
const stat = fs.statSync(targetPath);
|
|
165
|
-
return stat.isDirectory();
|
|
166
|
-
} catch (e) {
|
|
167
|
-
return false;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
function normalizeSkillImportSourceApp(app) {
|
|
172
|
-
const value = typeof app === 'string' ? app.trim().toLowerCase() : '';
|
|
173
|
-
return SKILL_IMPORT_SOURCES.some((item) => item.app === value) ? value : '';
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
function getSkillImportSourceByApp(app) {
|
|
177
|
-
const normalizedApp = normalizeSkillImportSourceApp(app);
|
|
178
|
-
if (!normalizedApp) return null;
|
|
179
|
-
return SKILL_IMPORT_SOURCES.find((item) => item.app === normalizedApp) || null;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
function parseSimpleSkillFrontmatter(content = '') {
|
|
183
|
-
const normalized = String(content || '').replace(/\r\n/g, '\n');
|
|
184
|
-
if (!normalized.startsWith('---\n')) {
|
|
185
|
-
return {};
|
|
186
|
-
}
|
|
187
|
-
const endIndex = normalized.indexOf('\n---\n', 4);
|
|
188
|
-
if (endIndex <= 4) {
|
|
189
|
-
return {};
|
|
190
|
-
}
|
|
191
|
-
const frontmatterRaw = normalized.slice(4, endIndex);
|
|
192
|
-
const result = {};
|
|
193
|
-
const lines = frontmatterRaw.split('\n');
|
|
194
|
-
for (let lineIndex = 0; lineIndex < lines.length; lineIndex += 1) {
|
|
195
|
-
const line = lines[lineIndex];
|
|
196
|
-
const trimmed = line.trim();
|
|
197
|
-
if (!trimmed || trimmed.startsWith('#')) continue;
|
|
198
|
-
const matched = trimmed.match(/^([A-Za-z0-9_-]+)\s*:\s*(.*)$/);
|
|
199
|
-
if (!matched) continue;
|
|
200
|
-
const key = matched[1];
|
|
201
|
-
let value = matched[2] || '';
|
|
202
|
-
const indicator = value.trim();
|
|
203
|
-
if (/^[>|]/.test(indicator)) {
|
|
204
|
-
const blockLines = [];
|
|
205
|
-
let cursor = lineIndex + 1;
|
|
206
|
-
while (cursor < lines.length) {
|
|
207
|
-
const candidateLine = lines[cursor];
|
|
208
|
-
if (!candidateLine.trim()) {
|
|
209
|
-
blockLines.push('');
|
|
210
|
-
cursor += 1;
|
|
211
|
-
continue;
|
|
212
|
-
}
|
|
213
|
-
if (/^\s/.test(candidateLine)) {
|
|
214
|
-
blockLines.push(candidateLine);
|
|
215
|
-
cursor += 1;
|
|
216
|
-
continue;
|
|
217
|
-
}
|
|
218
|
-
break;
|
|
219
|
-
}
|
|
220
|
-
lineIndex = cursor - 1;
|
|
221
|
-
const indents = blockLines
|
|
222
|
-
.filter((item) => item.trim())
|
|
223
|
-
.map((item) => {
|
|
224
|
-
const indentMatch = item.match(/^[ \t]*/);
|
|
225
|
-
return indentMatch ? indentMatch[0].length : 0;
|
|
226
|
-
});
|
|
227
|
-
const commonIndent = indents.length ? Math.min(...indents) : 0;
|
|
228
|
-
const deindented = blockLines.map((item) => {
|
|
229
|
-
if (!item.trim()) return '';
|
|
230
|
-
return item.slice(commonIndent);
|
|
231
|
-
});
|
|
232
|
-
if (indicator.startsWith('>')) {
|
|
233
|
-
const paragraphs = [];
|
|
234
|
-
let paragraphLines = [];
|
|
235
|
-
for (const blockLine of deindented) {
|
|
236
|
-
const blockTrimmed = blockLine.trim();
|
|
237
|
-
if (!blockTrimmed) {
|
|
238
|
-
if (paragraphLines.length) {
|
|
239
|
-
paragraphs.push(paragraphLines.join(' '));
|
|
240
|
-
paragraphLines = [];
|
|
241
|
-
}
|
|
242
|
-
continue;
|
|
243
|
-
}
|
|
244
|
-
paragraphLines.push(blockTrimmed);
|
|
245
|
-
}
|
|
246
|
-
if (paragraphLines.length) {
|
|
247
|
-
paragraphs.push(paragraphLines.join(' '));
|
|
248
|
-
}
|
|
249
|
-
value = paragraphs.join('\n');
|
|
250
|
-
} else {
|
|
251
|
-
value = deindented.join('\n');
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith('\'') && value.endsWith('\''))) {
|
|
255
|
-
value = value.slice(1, -1);
|
|
256
|
-
}
|
|
257
|
-
result[key] = value.trim();
|
|
258
|
-
}
|
|
259
|
-
return result;
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
function stripMarkdownFrontmatter(content = '') {
|
|
263
|
-
const normalized = String(content || '').replace(/\r\n/g, '\n');
|
|
264
|
-
if (!normalized.startsWith('---\n')) {
|
|
265
|
-
return normalized;
|
|
266
|
-
}
|
|
267
|
-
const endIndex = normalized.indexOf('\n---\n', 4);
|
|
268
|
-
if (endIndex <= 4) {
|
|
269
|
-
return normalized;
|
|
270
|
-
}
|
|
271
|
-
return normalized.slice(endIndex + 5);
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
function extractSkillDescriptionFromMarkdown(content = '') {
|
|
275
|
-
const normalized = String(content || '').replace(/\r\n/g, '\n');
|
|
276
|
-
const lines = normalized.split('\n');
|
|
277
|
-
let inFence = false;
|
|
278
|
-
for (const line of lines) {
|
|
279
|
-
const trimmedStart = line.trimStart();
|
|
280
|
-
if (trimmedStart.startsWith('```')) {
|
|
281
|
-
inFence = !inFence;
|
|
282
|
-
continue;
|
|
283
|
-
}
|
|
284
|
-
if (inFence) continue;
|
|
285
|
-
if (/^( {4}|\t)/.test(line)) continue;
|
|
286
|
-
const trimmed = line.trim();
|
|
287
|
-
if (!trimmed) continue;
|
|
288
|
-
if (trimmed.startsWith('#')) continue;
|
|
289
|
-
if (trimmed.startsWith('---')) continue;
|
|
290
|
-
if (/^([A-Za-z0-9_-]+)\s*:\s*/.test(trimmed)) continue;
|
|
291
|
-
return trimmed.slice(0, 200);
|
|
292
|
-
}
|
|
293
|
-
return '';
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
function readCodexSkillMetadata(skillPath) {
|
|
297
|
-
const skillFile = path.join(skillPath, 'SKILL.md');
|
|
298
|
-
if (!fs.existsSync(skillFile)) {
|
|
299
|
-
return {
|
|
300
|
-
hasSkillFile: false,
|
|
301
|
-
displayName: '',
|
|
302
|
-
description: ''
|
|
303
|
-
};
|
|
304
|
-
}
|
|
305
|
-
try {
|
|
306
|
-
const raw = fs.readFileSync(skillFile, 'utf-8');
|
|
307
|
-
const content = stripUtf8Bom(raw);
|
|
308
|
-
const frontmatter = parseSimpleSkillFrontmatter(content);
|
|
309
|
-
const contentWithoutFrontmatter = stripMarkdownFrontmatter(content);
|
|
310
|
-
const heading = contentWithoutFrontmatter.match(/^\s*#\s+(.+)$/m);
|
|
311
|
-
const displayName = typeof frontmatter.name === 'string' && frontmatter.name.trim()
|
|
312
|
-
? frontmatter.name.trim()
|
|
313
|
-
: (heading && heading[1] ? heading[1].trim() : '');
|
|
314
|
-
const description = typeof frontmatter.description === 'string' && frontmatter.description.trim()
|
|
315
|
-
? frontmatter.description.trim().slice(0, 200)
|
|
316
|
-
: extractSkillDescriptionFromMarkdown(contentWithoutFrontmatter);
|
|
317
|
-
return {
|
|
318
|
-
hasSkillFile: true,
|
|
319
|
-
displayName,
|
|
320
|
-
description
|
|
321
|
-
};
|
|
322
|
-
} catch (e) {
|
|
323
|
-
return {
|
|
324
|
-
hasSkillFile: false,
|
|
325
|
-
displayName: '',
|
|
326
|
-
description: ''
|
|
327
|
-
};
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
function getSkillEntryInfoByName(rootDir, entryName) {
|
|
332
|
-
const targetPath = path.join(rootDir, entryName);
|
|
333
|
-
const normalized = normalizeCodexSkillName(entryName);
|
|
334
|
-
if (normalized.error) {
|
|
335
|
-
return null;
|
|
336
|
-
}
|
|
337
|
-
const relativePath = path.relative(rootDir, targetPath);
|
|
338
|
-
if (relativePath.startsWith('..') || path.isAbsolute(relativePath)) {
|
|
339
|
-
return null;
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
try {
|
|
343
|
-
const lstat = fs.lstatSync(targetPath);
|
|
344
|
-
const isSymbolicLink = lstat.isSymbolicLink();
|
|
345
|
-
if (!lstat.isDirectory() && !isSymbolicLink) {
|
|
346
|
-
return null;
|
|
347
|
-
}
|
|
348
|
-
if (isSymbolicLink && !isSkillDirectoryEntryAtRoot(rootDir, entryName)) {
|
|
349
|
-
return null;
|
|
350
|
-
}
|
|
351
|
-
const metadata = readCodexSkillMetadata(targetPath);
|
|
352
|
-
return {
|
|
353
|
-
name: entryName,
|
|
354
|
-
path: targetPath,
|
|
355
|
-
hasSkillFile: !!metadata.hasSkillFile,
|
|
356
|
-
displayName: metadata.displayName || entryName,
|
|
357
|
-
description: metadata.description || '',
|
|
358
|
-
sourceType: isSymbolicLink ? 'symlink' : 'directory',
|
|
359
|
-
updatedAt: Number.isFinite(lstat.mtimeMs) ? Math.floor(lstat.mtimeMs) : 0
|
|
360
|
-
};
|
|
361
|
-
} catch (e) {
|
|
362
|
-
return null;
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
function listSkills(params = {}) {
|
|
367
|
-
const target = resolveSkillTarget(params);
|
|
368
|
-
if (!target) {
|
|
369
|
-
return { error: '目标宿主不支持' };
|
|
370
|
-
}
|
|
371
|
-
if (!fs.existsSync(target.dir)) {
|
|
372
|
-
return {
|
|
373
|
-
targetApp: target.app,
|
|
374
|
-
targetLabel: target.label,
|
|
375
|
-
root: target.dir,
|
|
376
|
-
exists: false,
|
|
377
|
-
items: []
|
|
378
|
-
};
|
|
379
|
-
}
|
|
380
|
-
try {
|
|
381
|
-
const entries = fs.readdirSync(target.dir, { withFileTypes: true });
|
|
382
|
-
const items = entries
|
|
383
|
-
.map((entry) => {
|
|
384
|
-
const name = entry && entry.name ? entry.name : '';
|
|
385
|
-
if (!name || name.startsWith('.')) return null;
|
|
386
|
-
return getSkillEntryInfoByName(target.dir, name);
|
|
387
|
-
})
|
|
388
|
-
.filter(Boolean)
|
|
389
|
-
.sort((a, b) => a.displayName.localeCompare(b.displayName, 'zh-Hans-CN'));
|
|
390
|
-
return {
|
|
391
|
-
targetApp: target.app,
|
|
392
|
-
targetLabel: target.label,
|
|
393
|
-
root: target.dir,
|
|
394
|
-
exists: true,
|
|
395
|
-
items
|
|
396
|
-
};
|
|
397
|
-
} catch (e) {
|
|
398
|
-
return { error: `读取 skills 目录失败: ${e.message}` };
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
function listCodexSkills() {
|
|
403
|
-
return listSkills({ targetApp: 'codex' });
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
function listSkillEntriesByRoot(rootDir) {
|
|
407
|
-
if (!rootDir || !fs.existsSync(rootDir)) {
|
|
408
|
-
return [];
|
|
409
|
-
}
|
|
410
|
-
try {
|
|
411
|
-
const entries = fs.readdirSync(rootDir, { withFileTypes: true });
|
|
412
|
-
return entries
|
|
413
|
-
.map((entry) => {
|
|
414
|
-
const name = entry && entry.name ? entry.name : '';
|
|
415
|
-
if (!name || name.startsWith('.')) return null;
|
|
416
|
-
const normalized = normalizeCodexSkillName(name);
|
|
417
|
-
if (normalized.error) return null;
|
|
418
|
-
const skillPath = path.join(rootDir, name);
|
|
419
|
-
const relativePath = path.relative(rootDir, skillPath);
|
|
420
|
-
if (relativePath.startsWith('..') || path.isAbsolute(relativePath)) {
|
|
421
|
-
return null;
|
|
422
|
-
}
|
|
423
|
-
try {
|
|
424
|
-
const lstat = fs.lstatSync(skillPath);
|
|
425
|
-
const isSymbolicLink = lstat.isSymbolicLink();
|
|
426
|
-
if (!lstat.isDirectory() && !isSymbolicLink) {
|
|
427
|
-
return null;
|
|
428
|
-
}
|
|
429
|
-
if (isSymbolicLink) {
|
|
430
|
-
const realPath = fs.realpathSync(skillPath);
|
|
431
|
-
const realStat = fs.statSync(realPath);
|
|
432
|
-
if (!realStat.isDirectory()) {
|
|
433
|
-
return null;
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
return {
|
|
437
|
-
name,
|
|
438
|
-
path: skillPath,
|
|
439
|
-
sourceType: isSymbolicLink ? 'symlink' : 'directory'
|
|
440
|
-
};
|
|
441
|
-
} catch (e) {
|
|
442
|
-
return null;
|
|
443
|
-
}
|
|
444
|
-
})
|
|
445
|
-
.filter(Boolean);
|
|
446
|
-
} catch (e) {
|
|
447
|
-
return [];
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
function scanUnmanagedSkills(params = {}) {
|
|
452
|
-
const getPathApi = (basePath) => {
|
|
453
|
-
const base = typeof basePath === 'string' ? basePath.trim() : '';
|
|
454
|
-
return base.includes('/') && !base.includes('\\') && path.posix ? path.posix : path;
|
|
455
|
-
};
|
|
456
|
-
const target = resolveSkillTarget(params);
|
|
457
|
-
if (!target) {
|
|
458
|
-
return { error: '目标宿主不支持' };
|
|
459
|
-
}
|
|
460
|
-
const targetRoot = resolveCopyTargetRoot(target.dir);
|
|
461
|
-
const targetPathApi = getPathApi(targetRoot);
|
|
462
|
-
const existing = listSkills({ targetApp: target.app });
|
|
463
|
-
if (existing.error) {
|
|
464
|
-
return { error: existing.error };
|
|
465
|
-
}
|
|
466
|
-
const existingNames = new Set((Array.isArray(existing.items) ? existing.items : [])
|
|
467
|
-
.map((item) => (item && typeof item.name === 'string' ? item.name.trim() : ''))
|
|
468
|
-
.filter(Boolean));
|
|
469
|
-
|
|
470
|
-
const items = [];
|
|
471
|
-
const sources = SKILL_IMPORT_SOURCES.filter((source) => source.app !== target.app);
|
|
472
|
-
for (const source of sources) {
|
|
473
|
-
const sourceEntries = listSkillEntriesByRoot(source.dir);
|
|
474
|
-
for (const entry of sourceEntries) {
|
|
475
|
-
const targetCandidate = targetPathApi.join(targetRoot, entry.name);
|
|
476
|
-
if (fs.existsSync(targetCandidate)) {
|
|
477
|
-
continue;
|
|
478
|
-
}
|
|
479
|
-
if (existingNames.has(entry.name)) {
|
|
480
|
-
continue;
|
|
481
|
-
}
|
|
482
|
-
const metadata = readCodexSkillMetadata(entry.path);
|
|
483
|
-
items.push({
|
|
484
|
-
key: `${source.app}:${entry.name}`,
|
|
485
|
-
name: entry.name,
|
|
486
|
-
displayName: metadata.displayName || entry.name,
|
|
487
|
-
description: metadata.description || '',
|
|
488
|
-
sourceApp: source.app,
|
|
489
|
-
sourceLabel: source.label,
|
|
490
|
-
sourcePath: entry.path,
|
|
491
|
-
sourceType: entry.sourceType,
|
|
492
|
-
hasSkillFile: !!metadata.hasSkillFile
|
|
493
|
-
});
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
items.sort((a, b) => {
|
|
498
|
-
const nameCompare = a.displayName.localeCompare(b.displayName, 'zh-Hans-CN');
|
|
499
|
-
if (nameCompare !== 0) return nameCompare;
|
|
500
|
-
return a.sourceLabel.localeCompare(b.sourceLabel, 'zh-Hans-CN');
|
|
501
|
-
});
|
|
502
|
-
|
|
503
|
-
return {
|
|
504
|
-
targetApp: target.app,
|
|
505
|
-
targetLabel: target.label,
|
|
506
|
-
root: target.dir,
|
|
507
|
-
items,
|
|
508
|
-
sources: sources.map((source) => ({
|
|
509
|
-
app: source.app,
|
|
510
|
-
label: source.label,
|
|
511
|
-
path: source.dir,
|
|
512
|
-
exists: fs.existsSync(source.dir)
|
|
513
|
-
}))
|
|
514
|
-
};
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
function scanUnmanagedCodexSkills() {
|
|
518
|
-
return scanUnmanagedSkills({ targetApp: 'codex' });
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
function importSkills(params = {}) {
|
|
522
|
-
const getPathApi = (basePath) => {
|
|
523
|
-
const base = typeof basePath === 'string' ? basePath.trim() : '';
|
|
524
|
-
return base.includes('/') && !base.includes('\\') && path.posix ? path.posix : path;
|
|
525
|
-
};
|
|
526
|
-
const target = resolveSkillTarget(params);
|
|
527
|
-
if (!target) {
|
|
528
|
-
return { error: '目标宿主不支持' };
|
|
529
|
-
}
|
|
530
|
-
const targetRoot = resolveCopyTargetRoot(target.dir);
|
|
531
|
-
const targetPathApi = getPathApi(targetRoot);
|
|
532
|
-
const rawItems = Array.isArray(params.items) ? params.items : [];
|
|
533
|
-
if (!rawItems.length) {
|
|
534
|
-
return { error: '请先选择要导入的 skill' };
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
const imported = [];
|
|
538
|
-
const failed = [];
|
|
539
|
-
const dedup = new Set();
|
|
540
|
-
|
|
541
|
-
for (const rawItem of rawItems) {
|
|
542
|
-
const safeItem = rawItem && typeof rawItem === 'object' ? rawItem : {};
|
|
543
|
-
const normalizedName = normalizeCodexSkillName(safeItem.name);
|
|
544
|
-
if (normalizedName.error) {
|
|
545
|
-
failed.push({
|
|
546
|
-
name: safeItem && safeItem.name ? String(safeItem.name) : '',
|
|
547
|
-
sourceApp: safeItem && safeItem.sourceApp ? String(safeItem.sourceApp) : '',
|
|
548
|
-
error: normalizedName.error
|
|
549
|
-
});
|
|
550
|
-
continue;
|
|
551
|
-
}
|
|
552
|
-
const source = getSkillImportSourceByApp(safeItem.sourceApp);
|
|
553
|
-
if (!source) {
|
|
554
|
-
failed.push({
|
|
555
|
-
name: normalizedName.name,
|
|
556
|
-
sourceApp: safeItem && safeItem.sourceApp ? String(safeItem.sourceApp) : '',
|
|
557
|
-
error: '来源应用不支持'
|
|
558
|
-
});
|
|
559
|
-
continue;
|
|
560
|
-
}
|
|
561
|
-
if (source.app === target.app) {
|
|
562
|
-
failed.push({
|
|
563
|
-
name: normalizedName.name,
|
|
564
|
-
sourceApp: source.app,
|
|
565
|
-
error: '来源与目标相同,无需导入'
|
|
566
|
-
});
|
|
567
|
-
continue;
|
|
568
|
-
}
|
|
569
|
-
const dedupKey = `${source.app}:${normalizedName.name}`;
|
|
570
|
-
if (dedup.has(dedupKey)) {
|
|
571
|
-
continue;
|
|
572
|
-
}
|
|
573
|
-
dedup.add(dedupKey);
|
|
574
|
-
|
|
575
|
-
const sourcePathApi = getPathApi(source.dir);
|
|
576
|
-
const sourcePath = sourcePathApi.join(source.dir, normalizedName.name);
|
|
577
|
-
const sourceRelative = sourcePathApi.relative(source.dir, sourcePath);
|
|
578
|
-
if (sourceRelative.startsWith('..') || sourcePathApi.isAbsolute(sourceRelative)) {
|
|
579
|
-
failed.push({
|
|
580
|
-
name: normalizedName.name,
|
|
581
|
-
sourceApp: source.app,
|
|
582
|
-
error: '来源路径非法'
|
|
583
|
-
});
|
|
584
|
-
continue;
|
|
585
|
-
}
|
|
586
|
-
if (!fs.existsSync(sourcePath)) {
|
|
587
|
-
failed.push({
|
|
588
|
-
name: normalizedName.name,
|
|
589
|
-
sourceApp: source.app,
|
|
590
|
-
error: '来源 skill 不存在'
|
|
591
|
-
});
|
|
592
|
-
continue;
|
|
593
|
-
}
|
|
594
|
-
|
|
595
|
-
const targetPath = targetPathApi.join(targetRoot, normalizedName.name);
|
|
596
|
-
const targetRelative = targetPathApi.relative(targetRoot, targetPath);
|
|
597
|
-
if (targetRelative.startsWith('..') || targetPathApi.isAbsolute(targetRelative)) {
|
|
598
|
-
failed.push({
|
|
599
|
-
name: normalizedName.name,
|
|
600
|
-
sourceApp: source.app,
|
|
601
|
-
error: '目标路径非法'
|
|
602
|
-
});
|
|
603
|
-
continue;
|
|
604
|
-
}
|
|
605
|
-
if (fs.existsSync(targetPath)) {
|
|
606
|
-
failed.push({
|
|
607
|
-
name: normalizedName.name,
|
|
608
|
-
sourceApp: source.app,
|
|
609
|
-
error: `${target.label} 中已存在同名 skill`
|
|
610
|
-
});
|
|
611
|
-
continue;
|
|
612
|
-
}
|
|
613
|
-
|
|
614
|
-
let copiedToTarget = false;
|
|
615
|
-
try {
|
|
616
|
-
const lstat = fs.lstatSync(sourcePath);
|
|
617
|
-
if (!lstat.isDirectory() && !lstat.isSymbolicLink()) {
|
|
618
|
-
failed.push({
|
|
619
|
-
name: normalizedName.name,
|
|
620
|
-
sourceApp: source.app,
|
|
621
|
-
error: '来源不是技能目录'
|
|
622
|
-
});
|
|
623
|
-
continue;
|
|
624
|
-
}
|
|
625
|
-
const sourceDirForCopy = lstat.isSymbolicLink() ? fs.realpathSync(sourcePath) : sourcePath;
|
|
626
|
-
const sourceStat = fs.statSync(sourceDirForCopy);
|
|
627
|
-
if (!sourceStat.isDirectory()) {
|
|
628
|
-
failed.push({
|
|
629
|
-
name: normalizedName.name,
|
|
630
|
-
sourceApp: source.app,
|
|
631
|
-
error: '来源 skill 无法读取'
|
|
632
|
-
});
|
|
633
|
-
continue;
|
|
634
|
-
}
|
|
635
|
-
if (isPathInside(targetRoot, sourceDirForCopy)) {
|
|
636
|
-
failed.push({
|
|
637
|
-
name: normalizedName.name,
|
|
638
|
-
sourceApp: source.app,
|
|
639
|
-
error: '目标路径不能位于来源 skill 目录内'
|
|
640
|
-
});
|
|
641
|
-
continue;
|
|
642
|
-
}
|
|
643
|
-
ensureDir(targetRoot);
|
|
644
|
-
const visitedRealPaths = new Set([sourceDirForCopy]);
|
|
645
|
-
copyDirRecursive(sourceDirForCopy, targetPath, {
|
|
646
|
-
dereferenceSymlinks: true,
|
|
647
|
-
allowedRootRealPath: sourceDirForCopy,
|
|
648
|
-
visitedRealPaths
|
|
649
|
-
});
|
|
650
|
-
copiedToTarget = true;
|
|
651
|
-
imported.push({
|
|
652
|
-
name: normalizedName.name,
|
|
653
|
-
sourceApp: source.app,
|
|
654
|
-
sourceLabel: source.label,
|
|
655
|
-
targetApp: target.app,
|
|
656
|
-
targetLabel: target.label,
|
|
657
|
-
path: targetPath
|
|
658
|
-
});
|
|
659
|
-
} catch (e) {
|
|
660
|
-
if (!copiedToTarget && fs.existsSync(targetPath)) {
|
|
661
|
-
try {
|
|
662
|
-
removeDirectoryRecursive(targetPath);
|
|
663
|
-
} catch (_) {}
|
|
664
|
-
}
|
|
665
|
-
failed.push({
|
|
666
|
-
name: normalizedName.name,
|
|
667
|
-
sourceApp: source.app,
|
|
668
|
-
error: e && e.message ? e.message : '导入失败'
|
|
669
|
-
});
|
|
670
|
-
}
|
|
671
|
-
}
|
|
672
|
-
|
|
673
|
-
return {
|
|
674
|
-
success: failed.length === 0,
|
|
675
|
-
imported,
|
|
676
|
-
failed,
|
|
677
|
-
targetApp: target.app,
|
|
678
|
-
targetLabel: target.label,
|
|
679
|
-
root: targetRoot
|
|
680
|
-
};
|
|
681
|
-
}
|
|
682
|
-
|
|
683
|
-
function importCodexSkills(params = {}) {
|
|
684
|
-
return importSkills({ ...(params || {}), targetApp: 'codex' });
|
|
685
|
-
}
|
|
686
|
-
|
|
687
|
-
function collectSkillDirectoriesFromRoot(rootDir, limit = MAX_SKILLS_ZIP_ENTRY_COUNT) {
|
|
688
|
-
const results = [];
|
|
689
|
-
let truncated = false;
|
|
690
|
-
if (!rootDir || !fs.existsSync(rootDir)) {
|
|
691
|
-
return { results, truncated };
|
|
692
|
-
}
|
|
693
|
-
const normalizedLimit = Number.isFinite(limit) && limit > 0
|
|
694
|
-
? Math.floor(limit)
|
|
695
|
-
: MAX_SKILLS_ZIP_ENTRY_COUNT;
|
|
696
|
-
const stack = [rootDir];
|
|
697
|
-
while (stack.length > 0) {
|
|
698
|
-
if (results.length >= normalizedLimit) {
|
|
699
|
-
truncated = true;
|
|
700
|
-
break;
|
|
701
|
-
}
|
|
702
|
-
const currentDir = stack.pop();
|
|
703
|
-
let entries = [];
|
|
704
|
-
try {
|
|
705
|
-
entries = fs.readdirSync(currentDir, { withFileTypes: true });
|
|
706
|
-
} catch (e) {
|
|
707
|
-
continue;
|
|
708
|
-
}
|
|
709
|
-
|
|
710
|
-
const hasSkillFile = entries.some((entry) => entry && entry.isFile() && String(entry.name || '') === 'SKILL.md');
|
|
711
|
-
if (hasSkillFile) {
|
|
712
|
-
results.push(currentDir);
|
|
713
|
-
continue;
|
|
714
|
-
}
|
|
715
|
-
|
|
716
|
-
for (const entry of entries) {
|
|
717
|
-
if (!entry || !entry.isDirectory()) continue;
|
|
718
|
-
const entryName = typeof entry.name === 'string' ? entry.name.trim() : '';
|
|
719
|
-
if (!entryName || entryName.startsWith('.')) {
|
|
720
|
-
continue;
|
|
721
|
-
}
|
|
722
|
-
stack.push(path.join(currentDir, entryName));
|
|
723
|
-
}
|
|
724
|
-
}
|
|
725
|
-
return { results, truncated };
|
|
726
|
-
}
|
|
727
|
-
|
|
728
|
-
function resolveSkillNameFromImportedDirectory(skillDir, extractionRoot, fallbackName = '') {
|
|
729
|
-
const directoryBaseName = path.basename(skillDir || '');
|
|
730
|
-
const extractionBaseName = path.basename(extractionRoot || '');
|
|
731
|
-
let candidate = directoryBaseName;
|
|
732
|
-
if (!candidate || candidate === extractionBaseName || candidate.startsWith('.')) {
|
|
733
|
-
const fallback = typeof fallbackName === 'string' ? fallbackName.trim() : '';
|
|
734
|
-
const fallbackBase = fallback ? path.basename(fallback, path.extname(fallback)) : '';
|
|
735
|
-
candidate = fallbackBase || candidate;
|
|
736
|
-
}
|
|
737
|
-
return normalizeCodexSkillName(candidate);
|
|
738
|
-
}
|
|
739
|
-
|
|
740
|
-
async function importSkillsFromZipFile(zipPath, options = {}) {
|
|
741
|
-
const getPathApi = (basePath) => {
|
|
742
|
-
const base = typeof basePath === 'string' ? basePath.trim() : '';
|
|
743
|
-
return base.includes('/') && !base.includes('\\') && path.posix ? path.posix : path;
|
|
744
|
-
};
|
|
745
|
-
const fallbackName = typeof options.fallbackName === 'string' ? options.fallbackName : '';
|
|
746
|
-
const tempDir = typeof options.tempDir === 'string' ? options.tempDir : '';
|
|
747
|
-
const imported = [];
|
|
748
|
-
const failed = [];
|
|
749
|
-
const dedupNames = new Set();
|
|
750
|
-
const extractionPathApi = getPathApi(tempDir || zipPath);
|
|
751
|
-
const extractionBaseDir = tempDir || extractionPathApi.dirname(zipPath);
|
|
752
|
-
const extractionRoot = extractionPathApi.join(extractionBaseDir, 'extract');
|
|
753
|
-
let target = null;
|
|
754
|
-
let targetRoot = '';
|
|
755
|
-
|
|
756
|
-
try {
|
|
757
|
-
target = resolveSkillTarget(options, 'codex');
|
|
758
|
-
if (!target) {
|
|
759
|
-
return { error: '目标宿主不支持' };
|
|
760
|
-
}
|
|
761
|
-
targetRoot = resolveCopyTargetRoot(target.dir);
|
|
762
|
-
const targetPathApi = getPathApi(targetRoot);
|
|
763
|
-
await inspectZipArchiveLimits(zipPath, {
|
|
764
|
-
maxEntryCount: MAX_SKILLS_ZIP_ENTRY_COUNT,
|
|
765
|
-
maxUncompressedBytes: MAX_SKILLS_ZIP_UNCOMPRESSED_BYTES
|
|
766
|
-
});
|
|
767
|
-
|
|
768
|
-
await extractUploadZip(zipPath, extractionRoot);
|
|
769
|
-
const discovery = collectSkillDirectoriesFromRoot(extractionRoot, MAX_SKILLS_ZIP_ENTRY_COUNT);
|
|
770
|
-
const discoveredDirs = discovery.results;
|
|
771
|
-
if (discoveredDirs.length === 0) {
|
|
772
|
-
return { error: '压缩包中未发现包含 SKILL.md 的技能目录' };
|
|
773
|
-
}
|
|
774
|
-
if (discovery.truncated) {
|
|
775
|
-
return { error: '压缩包中的技能目录数量超出导入上限' };
|
|
776
|
-
}
|
|
777
|
-
|
|
778
|
-
for (const skillDir of discoveredDirs) {
|
|
779
|
-
const normalizedName = resolveSkillNameFromImportedDirectory(skillDir, extractionRoot, fallbackName);
|
|
780
|
-
if (normalizedName.error) {
|
|
781
|
-
failed.push({
|
|
782
|
-
name: path.basename(skillDir || ''),
|
|
783
|
-
error: normalizedName.error
|
|
784
|
-
});
|
|
785
|
-
continue;
|
|
786
|
-
}
|
|
787
|
-
const dedupKey = normalizedName.name.toLowerCase();
|
|
788
|
-
if (dedupNames.has(dedupKey)) {
|
|
789
|
-
continue;
|
|
790
|
-
}
|
|
791
|
-
dedupNames.add(dedupKey);
|
|
792
|
-
|
|
793
|
-
const targetPath = targetPathApi.join(targetRoot, normalizedName.name);
|
|
794
|
-
const targetRelative = targetPathApi.relative(targetRoot, targetPath);
|
|
795
|
-
if (targetRelative.startsWith('..') || targetPathApi.isAbsolute(targetRelative)) {
|
|
796
|
-
failed.push({
|
|
797
|
-
name: normalizedName.name,
|
|
798
|
-
error: '目标路径非法'
|
|
799
|
-
});
|
|
800
|
-
continue;
|
|
801
|
-
}
|
|
802
|
-
if (fs.existsSync(targetPath)) {
|
|
803
|
-
failed.push({
|
|
804
|
-
name: normalizedName.name,
|
|
805
|
-
error: `${target.label} 中已存在同名 skill`
|
|
806
|
-
});
|
|
807
|
-
continue;
|
|
808
|
-
}
|
|
809
|
-
|
|
810
|
-
let copiedToTarget = false;
|
|
811
|
-
try {
|
|
812
|
-
const sourceRealPath = fs.realpathSync(skillDir);
|
|
813
|
-
const sourceStat = fs.statSync(sourceRealPath);
|
|
814
|
-
if (!sourceStat.isDirectory()) {
|
|
815
|
-
failed.push({
|
|
816
|
-
name: normalizedName.name,
|
|
817
|
-
error: '来源 skill 无法读取'
|
|
818
|
-
});
|
|
819
|
-
continue;
|
|
820
|
-
}
|
|
821
|
-
if (isPathInside(targetRoot, sourceRealPath)) {
|
|
822
|
-
failed.push({
|
|
823
|
-
name: normalizedName.name,
|
|
824
|
-
error: '目标路径不能位于来源 skill 目录内'
|
|
825
|
-
});
|
|
826
|
-
continue;
|
|
827
|
-
}
|
|
828
|
-
ensureDir(targetRoot);
|
|
829
|
-
const visitedRealPaths = new Set([sourceRealPath]);
|
|
830
|
-
copyDirRecursive(sourceRealPath, targetPath, {
|
|
831
|
-
dereferenceSymlinks: true,
|
|
832
|
-
allowedRootRealPath: sourceRealPath,
|
|
833
|
-
visitedRealPaths
|
|
834
|
-
});
|
|
835
|
-
copiedToTarget = true;
|
|
836
|
-
imported.push({
|
|
837
|
-
name: normalizedName.name,
|
|
838
|
-
targetApp: target.app,
|
|
839
|
-
targetLabel: target.label,
|
|
840
|
-
path: targetPath
|
|
841
|
-
});
|
|
842
|
-
} catch (e) {
|
|
843
|
-
if (!copiedToTarget && fs.existsSync(targetPath)) {
|
|
844
|
-
try {
|
|
845
|
-
removeDirectoryRecursive(targetPath);
|
|
846
|
-
} catch (_) {}
|
|
847
|
-
}
|
|
848
|
-
failed.push({
|
|
849
|
-
name: normalizedName.name,
|
|
850
|
-
error: e && e.message ? e.message : '导入失败'
|
|
851
|
-
});
|
|
852
|
-
}
|
|
853
|
-
}
|
|
854
|
-
|
|
855
|
-
if (imported.length === 0 && failed.length > 0) {
|
|
856
|
-
return {
|
|
857
|
-
error: failed[0].error || '导入失败',
|
|
858
|
-
imported,
|
|
859
|
-
failed,
|
|
860
|
-
targetApp: target.app,
|
|
861
|
-
targetLabel: target.label,
|
|
862
|
-
root: targetRoot
|
|
863
|
-
};
|
|
864
|
-
}
|
|
865
|
-
|
|
866
|
-
return {
|
|
867
|
-
success: failed.length === 0,
|
|
868
|
-
imported,
|
|
869
|
-
failed,
|
|
870
|
-
targetApp: target.app,
|
|
871
|
-
targetLabel: target.label,
|
|
872
|
-
root: targetRoot
|
|
873
|
-
};
|
|
874
|
-
} catch (e) {
|
|
875
|
-
return {
|
|
876
|
-
error: `导入失败:${e && e.message ? e.message : '未知错误'}`
|
|
877
|
-
};
|
|
878
|
-
} finally {
|
|
879
|
-
if (tempDir) {
|
|
880
|
-
try {
|
|
881
|
-
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
882
|
-
} catch (_) {}
|
|
883
|
-
} else if (fs.existsSync(extractionRoot)) {
|
|
884
|
-
try {
|
|
885
|
-
fs.rmSync(extractionRoot, { recursive: true, force: true });
|
|
886
|
-
} catch (_) {}
|
|
887
|
-
}
|
|
888
|
-
}
|
|
889
|
-
}
|
|
890
|
-
|
|
891
|
-
async function importCodexSkillsFromZipFile(zipPath, options = {}) {
|
|
892
|
-
return importSkillsFromZipFile(zipPath, { ...(options || {}), targetApp: 'codex' });
|
|
893
|
-
}
|
|
894
|
-
|
|
895
|
-
async function importSkillsFromZip(payload = {}) {
|
|
896
|
-
if (!payload || typeof payload.fileBase64 !== 'string' || !payload.fileBase64.trim()) {
|
|
897
|
-
return { error: '缺少技能压缩包内容' };
|
|
898
|
-
}
|
|
899
|
-
const fallbackTarget = resolveSkillTarget(payload, 'codex');
|
|
900
|
-
const fallbackTargetApp = fallbackTarget ? fallbackTarget.app : 'codex';
|
|
901
|
-
const fallbackName = payload.fileName || `${fallbackTargetApp}-skills.zip`;
|
|
902
|
-
const upload = writeUploadZip(payload.fileBase64, 'codex-skills-import', fallbackName);
|
|
903
|
-
if (upload.error) {
|
|
904
|
-
return { error: upload.error };
|
|
905
|
-
}
|
|
906
|
-
const importOptions = { tempDir: upload.tempDir, fallbackName };
|
|
907
|
-
if (Object.prototype.hasOwnProperty.call(payload, 'targetApp')) {
|
|
908
|
-
importOptions.targetApp = payload.targetApp;
|
|
909
|
-
}
|
|
910
|
-
if (Object.prototype.hasOwnProperty.call(payload, 'target')) {
|
|
911
|
-
importOptions.target = payload.target;
|
|
912
|
-
}
|
|
913
|
-
return importSkillsFromZipFile(upload.zipPath, importOptions);
|
|
914
|
-
}
|
|
915
|
-
|
|
916
|
-
async function importCodexSkillsFromZip(payload = {}) {
|
|
917
|
-
return importSkillsFromZip({ ...(payload || {}), targetApp: 'codex' });
|
|
918
|
-
}
|
|
919
|
-
|
|
920
|
-
async function exportSkills(params = {}) {
|
|
921
|
-
const target = resolveSkillTarget(params);
|
|
922
|
-
if (!target) {
|
|
923
|
-
return { error: '目标宿主不支持' };
|
|
924
|
-
}
|
|
925
|
-
const rawNames = Array.isArray(params.names) ? params.names : [];
|
|
926
|
-
const uniqueNames = Array.from(new Set(rawNames
|
|
927
|
-
.map((item) => (typeof item === 'string' ? item.trim() : ''))
|
|
928
|
-
.filter(Boolean)));
|
|
929
|
-
if (uniqueNames.length === 0) {
|
|
930
|
-
return { error: '请先选择要导出的 skill' };
|
|
931
|
-
}
|
|
932
|
-
|
|
933
|
-
const exported = [];
|
|
934
|
-
const failed = [];
|
|
935
|
-
const stagingTempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codex-skills-export-'));
|
|
936
|
-
const stagingRoot = path.join(stagingTempDir, 'skills');
|
|
937
|
-
ensureDir(stagingRoot);
|
|
938
|
-
|
|
939
|
-
try {
|
|
940
|
-
for (const rawName of uniqueNames) {
|
|
941
|
-
const normalizedName = normalizeCodexSkillName(rawName);
|
|
942
|
-
if (normalizedName.error) {
|
|
943
|
-
failed.push({ name: rawName, error: normalizedName.error });
|
|
944
|
-
continue;
|
|
945
|
-
}
|
|
946
|
-
const sourcePath = path.join(target.dir, normalizedName.name);
|
|
947
|
-
const sourceRelative = path.relative(target.dir, sourcePath);
|
|
948
|
-
if (sourceRelative.startsWith('..') || path.isAbsolute(sourceRelative)) {
|
|
949
|
-
failed.push({ name: normalizedName.name, error: '来源路径非法' });
|
|
950
|
-
continue;
|
|
951
|
-
}
|
|
952
|
-
if (!fs.existsSync(sourcePath)) {
|
|
953
|
-
failed.push({ name: normalizedName.name, error: 'skill 不存在' });
|
|
954
|
-
continue;
|
|
955
|
-
}
|
|
956
|
-
|
|
957
|
-
try {
|
|
958
|
-
const lstat = fs.lstatSync(sourcePath);
|
|
959
|
-
if (!lstat.isDirectory() && !lstat.isSymbolicLink()) {
|
|
960
|
-
failed.push({ name: normalizedName.name, error: '来源不是技能目录' });
|
|
961
|
-
continue;
|
|
962
|
-
}
|
|
963
|
-
const sourceDirForCopy = lstat.isSymbolicLink() ? fs.realpathSync(sourcePath) : sourcePath;
|
|
964
|
-
const sourceStat = fs.statSync(sourceDirForCopy);
|
|
965
|
-
if (!sourceStat.isDirectory()) {
|
|
966
|
-
failed.push({ name: normalizedName.name, error: '来源 skill 无法读取' });
|
|
967
|
-
continue;
|
|
968
|
-
}
|
|
969
|
-
const targetPath = path.join(stagingRoot, normalizedName.name);
|
|
970
|
-
const visitedRealPaths = new Set([sourceDirForCopy]);
|
|
971
|
-
copyDirRecursive(sourceDirForCopy, targetPath, {
|
|
972
|
-
dereferenceSymlinks: true,
|
|
973
|
-
allowedRootRealPath: sourceDirForCopy,
|
|
974
|
-
visitedRealPaths
|
|
975
|
-
});
|
|
976
|
-
exported.push({
|
|
977
|
-
name: normalizedName.name,
|
|
978
|
-
path: sourcePath
|
|
979
|
-
});
|
|
980
|
-
} catch (e) {
|
|
981
|
-
failed.push({
|
|
982
|
-
name: normalizedName.name,
|
|
983
|
-
error: e && e.message ? e.message : '导出失败'
|
|
984
|
-
});
|
|
985
|
-
}
|
|
986
|
-
}
|
|
987
|
-
|
|
988
|
-
if (exported.length === 0) {
|
|
989
|
-
return {
|
|
990
|
-
error: failed[0] && failed[0].error ? failed[0].error : '无可导出的 skill',
|
|
991
|
-
exported,
|
|
992
|
-
failed,
|
|
993
|
-
targetApp: target.app,
|
|
994
|
-
targetLabel: target.label,
|
|
995
|
-
root: target.dir
|
|
996
|
-
};
|
|
997
|
-
}
|
|
998
|
-
|
|
999
|
-
const randomToken = crypto.randomBytes(12).toString('hex');
|
|
1000
|
-
const zipFileName = `${target.app}-skills-${randomToken}.zip`;
|
|
1001
|
-
const zipFilePath = path.join(os.tmpdir(), zipFileName);
|
|
1002
|
-
if (fs.existsSync(zipFilePath)) {
|
|
1003
|
-
try {
|
|
1004
|
-
fs.unlinkSync(zipFilePath);
|
|
1005
|
-
} catch (_) {}
|
|
1006
|
-
}
|
|
1007
|
-
await zipLib.archiveFolder(stagingRoot, zipFilePath);
|
|
1008
|
-
const artifact = registerDownloadArtifact(zipFilePath, {
|
|
1009
|
-
fileName: zipFileName,
|
|
1010
|
-
deleteAfterDownload: true
|
|
1011
|
-
});
|
|
1012
|
-
|
|
1013
|
-
return {
|
|
1014
|
-
success: failed.length === 0,
|
|
1015
|
-
fileName: zipFileName,
|
|
1016
|
-
downloadPath: artifact.downloadPath,
|
|
1017
|
-
exported,
|
|
1018
|
-
failed,
|
|
1019
|
-
targetApp: target.app,
|
|
1020
|
-
targetLabel: target.label,
|
|
1021
|
-
root: target.dir
|
|
1022
|
-
};
|
|
1023
|
-
} catch (e) {
|
|
1024
|
-
return {
|
|
1025
|
-
error: `导出失败:${e && e.message ? e.message : '未知错误'}`,
|
|
1026
|
-
exported,
|
|
1027
|
-
failed,
|
|
1028
|
-
targetApp: target.app,
|
|
1029
|
-
targetLabel: target.label,
|
|
1030
|
-
root: target.dir
|
|
1031
|
-
};
|
|
1032
|
-
} finally {
|
|
1033
|
-
try {
|
|
1034
|
-
fs.rmSync(stagingTempDir, { recursive: true, force: true });
|
|
1035
|
-
} catch (_) {}
|
|
1036
|
-
}
|
|
1037
|
-
}
|
|
1038
|
-
|
|
1039
|
-
async function exportCodexSkills(params = {}) {
|
|
1040
|
-
return exportSkills({ ...(params || {}), targetApp: 'codex' });
|
|
1041
|
-
}
|
|
1042
|
-
|
|
1043
|
-
function removeDirectoryRecursive(targetPath) {
|
|
1044
|
-
if (typeof fs.rmSync === 'function') {
|
|
1045
|
-
fs.rmSync(targetPath, { recursive: true, force: false });
|
|
1046
|
-
return;
|
|
1047
|
-
}
|
|
1048
|
-
fs.rmdirSync(targetPath, { recursive: true });
|
|
1049
|
-
}
|
|
1050
|
-
|
|
1051
|
-
function deleteSkills(params = {}) {
|
|
1052
|
-
const target = resolveSkillTarget(params);
|
|
1053
|
-
if (!target) {
|
|
1054
|
-
return { error: '目标宿主不支持' };
|
|
1055
|
-
}
|
|
1056
|
-
const rawList = Array.isArray(params.names) ? params.names : [];
|
|
1057
|
-
const uniqueNames = Array.from(new Set(rawList
|
|
1058
|
-
.map((item) => (typeof item === 'string' ? item.trim() : ''))
|
|
1059
|
-
.filter(Boolean)));
|
|
1060
|
-
if (!uniqueNames.length) {
|
|
1061
|
-
return { error: '请先选择要删除的 skill' };
|
|
1062
|
-
}
|
|
1063
|
-
|
|
1064
|
-
const deleted = [];
|
|
1065
|
-
const failed = [];
|
|
1066
|
-
for (const rawName of uniqueNames) {
|
|
1067
|
-
const normalized = normalizeCodexSkillName(rawName);
|
|
1068
|
-
if (normalized.error) {
|
|
1069
|
-
failed.push({ name: rawName, error: normalized.error });
|
|
1070
|
-
continue;
|
|
1071
|
-
}
|
|
1072
|
-
|
|
1073
|
-
const skillPath = path.join(target.dir, normalized.name);
|
|
1074
|
-
const relativePath = path.relative(target.dir, skillPath);
|
|
1075
|
-
if (relativePath.startsWith('..') || path.isAbsolute(relativePath)) {
|
|
1076
|
-
failed.push({ name: normalized.name, error: '技能路径非法' });
|
|
1077
|
-
continue;
|
|
1078
|
-
}
|
|
1079
|
-
if (!fs.existsSync(skillPath)) {
|
|
1080
|
-
failed.push({ name: normalized.name, error: 'skill 不存在' });
|
|
1081
|
-
continue;
|
|
1082
|
-
}
|
|
1083
|
-
|
|
1084
|
-
try {
|
|
1085
|
-
const stat = fs.lstatSync(skillPath);
|
|
1086
|
-
if (!stat.isDirectory() && !stat.isSymbolicLink()) {
|
|
1087
|
-
failed.push({ name: normalized.name, error: '仅支持删除技能目录' });
|
|
1088
|
-
continue;
|
|
1089
|
-
}
|
|
1090
|
-
removeDirectoryRecursive(skillPath);
|
|
1091
|
-
deleted.push(normalized.name);
|
|
1092
|
-
} catch (e) {
|
|
1093
|
-
failed.push({
|
|
1094
|
-
name: normalized.name,
|
|
1095
|
-
error: e && e.message ? e.message : '删除失败'
|
|
1096
|
-
});
|
|
1097
|
-
}
|
|
1098
|
-
}
|
|
1099
|
-
|
|
1100
|
-
return {
|
|
1101
|
-
success: failed.length === 0,
|
|
1102
|
-
deleted,
|
|
1103
|
-
failed,
|
|
1104
|
-
targetApp: target.app,
|
|
1105
|
-
targetLabel: target.label,
|
|
1106
|
-
root: target.dir
|
|
1107
|
-
};
|
|
1108
|
-
}
|
|
1109
|
-
|
|
1110
|
-
function deleteCodexSkills(params = {}) {
|
|
1111
|
-
return deleteSkills({ ...(params || {}), targetApp: 'codex' });
|
|
1112
|
-
}
|
|
1113
|
-
|
|
1114
|
-
module.exports = {
|
|
1115
|
-
MAX_SKILLS_ZIP_UPLOAD_SIZE,
|
|
1116
|
-
MAX_SKILLS_ZIP_ENTRY_COUNT,
|
|
1117
|
-
MAX_SKILLS_ZIP_UNCOMPRESSED_BYTES,
|
|
1118
|
-
SKILL_TARGETS,
|
|
1119
|
-
SKILL_IMPORT_SOURCES,
|
|
1120
|
-
getCodexSkillsDir,
|
|
1121
|
-
getClaudeSkillsDir,
|
|
1122
|
-
normalizeSkillTargetApp,
|
|
1123
|
-
getSkillTargetByApp,
|
|
1124
|
-
resolveSkillTarget,
|
|
1125
|
-
resolveCopyTargetRoot,
|
|
1126
|
-
listSkills,
|
|
1127
|
-
listCodexSkills,
|
|
1128
|
-
scanUnmanagedSkills,
|
|
1129
|
-
scanUnmanagedCodexSkills,
|
|
1130
|
-
importSkills,
|
|
1131
|
-
importCodexSkills,
|
|
1132
|
-
importSkillsFromZipFile,
|
|
1133
|
-
importCodexSkillsFromZipFile,
|
|
1134
|
-
importSkillsFromZip,
|
|
1135
|
-
importCodexSkillsFromZip,
|
|
1136
|
-
exportSkills,
|
|
1137
|
-
exportCodexSkills,
|
|
1138
|
-
deleteSkills,
|
|
1139
|
-
deleteCodexSkills
|
|
1140
|
-
};
|
|
1141
|
-
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const os = require('os');
|
|
4
|
+
const crypto = require('crypto');
|
|
5
|
+
const zipLib = require('zip-lib');
|
|
6
|
+
const yauzl = require('yauzl');
|
|
7
|
+
const { execSync } = require('child_process');
|
|
8
|
+
|
|
9
|
+
const {
|
|
10
|
+
resolveExistingDir,
|
|
11
|
+
stripUtf8Bom
|
|
12
|
+
} = require('../lib/cli-utils');
|
|
13
|
+
const { ensureDir } = require('../lib/cli-file-utils');
|
|
14
|
+
const {
|
|
15
|
+
isPathInside,
|
|
16
|
+
resolveCopyTargetRoot,
|
|
17
|
+
commandExists
|
|
18
|
+
} = require('../lib/cli-path-utils');
|
|
19
|
+
const { registerDownloadArtifact } = require('../lib/download-artifacts');
|
|
20
|
+
const { createArchiveHelperController } = require('./archive-helpers');
|
|
21
|
+
|
|
22
|
+
const CONFIG_DIR = path.join(os.homedir(), '.codex');
|
|
23
|
+
const CLAUDE_DIR = path.join(os.homedir(), '.claude');
|
|
24
|
+
const CODEX_SKILLS_DIR = path.join(CONFIG_DIR, 'skills');
|
|
25
|
+
const CLAUDE_SKILLS_DIR = path.join(CLAUDE_DIR, 'skills');
|
|
26
|
+
const AGENTS_SKILLS_DIR = path.join(os.homedir(), '.agents', 'skills');
|
|
27
|
+
|
|
28
|
+
const MAX_UPLOAD_SIZE = 200 * 1024 * 1024;
|
|
29
|
+
const MAX_SKILLS_ZIP_UPLOAD_SIZE = 20 * 1024 * 1024;
|
|
30
|
+
const MAX_SKILLS_ZIP_ENTRY_COUNT = 2000;
|
|
31
|
+
const MAX_SKILLS_ZIP_UNCOMPRESSED_BYTES = 512 * 1024 * 1024;
|
|
32
|
+
|
|
33
|
+
function getCodexSkillsDir() {
|
|
34
|
+
const joinPath = (basePath, ...segments) => {
|
|
35
|
+
const base = typeof basePath === 'string' ? basePath.trim() : '';
|
|
36
|
+
const pathApi = base.includes('/') && !base.includes('\\') && path.posix ? path.posix : path;
|
|
37
|
+
return pathApi.join(base, ...segments);
|
|
38
|
+
};
|
|
39
|
+
const envCodexHome = typeof process.env.CODEX_HOME === 'string' ? process.env.CODEX_HOME.trim() : '';
|
|
40
|
+
if (envCodexHome) {
|
|
41
|
+
const target = joinPath(envCodexHome, 'skills');
|
|
42
|
+
return resolveExistingDir([target], target);
|
|
43
|
+
}
|
|
44
|
+
const xdgConfig = typeof process.env.XDG_CONFIG_HOME === 'string' ? process.env.XDG_CONFIG_HOME.trim() : '';
|
|
45
|
+
if (xdgConfig) {
|
|
46
|
+
const target = joinPath(xdgConfig, 'codex', 'skills');
|
|
47
|
+
return resolveExistingDir([target], target);
|
|
48
|
+
}
|
|
49
|
+
const homeConfigDir = joinPath(os.homedir(), '.config', 'codex', 'skills');
|
|
50
|
+
return resolveExistingDir([homeConfigDir], CODEX_SKILLS_DIR);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function getClaudeSkillsDir() {
|
|
54
|
+
const joinPath = (basePath, ...segments) => {
|
|
55
|
+
const base = typeof basePath === 'string' ? basePath.trim() : '';
|
|
56
|
+
const pathApi = base.includes('/') && !base.includes('\\') && path.posix ? path.posix : path;
|
|
57
|
+
return pathApi.join(base, ...segments);
|
|
58
|
+
};
|
|
59
|
+
const envClaudeHome = typeof process.env.CLAUDE_HOME === 'string' && process.env.CLAUDE_HOME.trim()
|
|
60
|
+
? process.env.CLAUDE_HOME.trim()
|
|
61
|
+
: (typeof process.env.CLAUDE_CONFIG_DIR === 'string' ? process.env.CLAUDE_CONFIG_DIR.trim() : '');
|
|
62
|
+
if (envClaudeHome) {
|
|
63
|
+
const target = joinPath(envClaudeHome, 'skills');
|
|
64
|
+
return resolveExistingDir([target], target);
|
|
65
|
+
}
|
|
66
|
+
const xdgConfig = typeof process.env.XDG_CONFIG_HOME === 'string' ? process.env.XDG_CONFIG_HOME.trim() : '';
|
|
67
|
+
if (xdgConfig) {
|
|
68
|
+
const target = joinPath(xdgConfig, 'claude', 'skills');
|
|
69
|
+
return resolveExistingDir([target], target);
|
|
70
|
+
}
|
|
71
|
+
const homeConfigDir = joinPath(os.homedir(), '.config', 'claude', 'skills');
|
|
72
|
+
return resolveExistingDir([homeConfigDir], CLAUDE_SKILLS_DIR);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const SKILL_TARGETS = Object.freeze([
|
|
76
|
+
Object.freeze({ app: 'codex', label: 'Codex', dir: getCodexSkillsDir() }),
|
|
77
|
+
Object.freeze({ app: 'claude', label: 'Claude Code', dir: getClaudeSkillsDir() })
|
|
78
|
+
]);
|
|
79
|
+
|
|
80
|
+
const SKILL_IMPORT_SOURCES = Object.freeze([
|
|
81
|
+
...SKILL_TARGETS,
|
|
82
|
+
Object.freeze({ app: 'agents', label: 'Agents', dir: AGENTS_SKILLS_DIR })
|
|
83
|
+
]);
|
|
84
|
+
|
|
85
|
+
const {
|
|
86
|
+
copyDirRecursive,
|
|
87
|
+
inspectZipArchiveLimits,
|
|
88
|
+
writeUploadZip,
|
|
89
|
+
extractUploadZip
|
|
90
|
+
} = createArchiveHelperController({
|
|
91
|
+
fs,
|
|
92
|
+
path,
|
|
93
|
+
os,
|
|
94
|
+
execSync,
|
|
95
|
+
zipLib,
|
|
96
|
+
yauzl,
|
|
97
|
+
ensureDir,
|
|
98
|
+
isPathInside,
|
|
99
|
+
commandExists,
|
|
100
|
+
MAX_UPLOAD_SIZE,
|
|
101
|
+
MAX_SKILLS_ZIP_UPLOAD_SIZE,
|
|
102
|
+
MAX_SKILLS_ZIP_ENTRY_COUNT,
|
|
103
|
+
MAX_SKILLS_ZIP_UNCOMPRESSED_BYTES
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
function normalizeCodexSkillName(name) {
|
|
107
|
+
const value = typeof name === 'string' ? name.trim() : '';
|
|
108
|
+
if (!value) {
|
|
109
|
+
return { error: '技能名称不能为空' };
|
|
110
|
+
}
|
|
111
|
+
if (value.includes('\0')) {
|
|
112
|
+
return { error: '技能名称非法' };
|
|
113
|
+
}
|
|
114
|
+
if (value === '.' || value === '..') {
|
|
115
|
+
return { error: '技能名称非法' };
|
|
116
|
+
}
|
|
117
|
+
if (value.includes('/') || value.includes('\\')) {
|
|
118
|
+
return { error: '技能名称非法' };
|
|
119
|
+
}
|
|
120
|
+
if (path.basename(value) !== value) {
|
|
121
|
+
return { error: '技能名称非法' };
|
|
122
|
+
}
|
|
123
|
+
if (value.startsWith('.')) {
|
|
124
|
+
return { error: '系统技能不可删除' };
|
|
125
|
+
}
|
|
126
|
+
return { name: value };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function normalizeSkillTargetApp(app) {
|
|
130
|
+
const value = typeof app === 'string' ? app.trim().toLowerCase() : '';
|
|
131
|
+
return SKILL_TARGETS.some((item) => item.app === value) ? value : '';
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function getSkillTargetByApp(app) {
|
|
135
|
+
const normalizedApp = normalizeSkillTargetApp(app);
|
|
136
|
+
if (!normalizedApp) return null;
|
|
137
|
+
return SKILL_TARGETS.find((item) => item.app === normalizedApp) || null;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function resolveSkillTarget(params = {}, defaultApp = 'codex') {
|
|
141
|
+
const hasExplicitTargetApp = !!(params && typeof params === 'object'
|
|
142
|
+
&& Object.prototype.hasOwnProperty.call(params, 'targetApp'));
|
|
143
|
+
const hasExplicitTarget = !!(params && typeof params === 'object'
|
|
144
|
+
&& Object.prototype.hasOwnProperty.call(params, 'target'));
|
|
145
|
+
const hasAnyExplicitTarget = hasExplicitTargetApp || hasExplicitTarget;
|
|
146
|
+
const rawTargetApp = hasExplicitTargetApp ? params.targetApp : '';
|
|
147
|
+
const rawTarget = hasExplicitTarget ? params.target : '';
|
|
148
|
+
const raw = rawTargetApp || rawTarget || '';
|
|
149
|
+
if (hasAnyExplicitTarget && raw === '') {
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
if (hasAnyExplicitTarget && !getSkillTargetByApp(raw)) {
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
return getSkillTargetByApp(raw)
|
|
156
|
+
|| getSkillTargetByApp(defaultApp)
|
|
157
|
+
|| SKILL_TARGETS[0]
|
|
158
|
+
|| null;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function isSkillDirectoryEntryAtRoot(rootDir, entryName) {
|
|
162
|
+
const targetPath = path.join(rootDir, entryName);
|
|
163
|
+
try {
|
|
164
|
+
const stat = fs.statSync(targetPath);
|
|
165
|
+
return stat.isDirectory();
|
|
166
|
+
} catch (e) {
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function normalizeSkillImportSourceApp(app) {
|
|
172
|
+
const value = typeof app === 'string' ? app.trim().toLowerCase() : '';
|
|
173
|
+
return SKILL_IMPORT_SOURCES.some((item) => item.app === value) ? value : '';
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function getSkillImportSourceByApp(app) {
|
|
177
|
+
const normalizedApp = normalizeSkillImportSourceApp(app);
|
|
178
|
+
if (!normalizedApp) return null;
|
|
179
|
+
return SKILL_IMPORT_SOURCES.find((item) => item.app === normalizedApp) || null;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function parseSimpleSkillFrontmatter(content = '') {
|
|
183
|
+
const normalized = String(content || '').replace(/\r\n/g, '\n');
|
|
184
|
+
if (!normalized.startsWith('---\n')) {
|
|
185
|
+
return {};
|
|
186
|
+
}
|
|
187
|
+
const endIndex = normalized.indexOf('\n---\n', 4);
|
|
188
|
+
if (endIndex <= 4) {
|
|
189
|
+
return {};
|
|
190
|
+
}
|
|
191
|
+
const frontmatterRaw = normalized.slice(4, endIndex);
|
|
192
|
+
const result = {};
|
|
193
|
+
const lines = frontmatterRaw.split('\n');
|
|
194
|
+
for (let lineIndex = 0; lineIndex < lines.length; lineIndex += 1) {
|
|
195
|
+
const line = lines[lineIndex];
|
|
196
|
+
const trimmed = line.trim();
|
|
197
|
+
if (!trimmed || trimmed.startsWith('#')) continue;
|
|
198
|
+
const matched = trimmed.match(/^([A-Za-z0-9_-]+)\s*:\s*(.*)$/);
|
|
199
|
+
if (!matched) continue;
|
|
200
|
+
const key = matched[1];
|
|
201
|
+
let value = matched[2] || '';
|
|
202
|
+
const indicator = value.trim();
|
|
203
|
+
if (/^[>|]/.test(indicator)) {
|
|
204
|
+
const blockLines = [];
|
|
205
|
+
let cursor = lineIndex + 1;
|
|
206
|
+
while (cursor < lines.length) {
|
|
207
|
+
const candidateLine = lines[cursor];
|
|
208
|
+
if (!candidateLine.trim()) {
|
|
209
|
+
blockLines.push('');
|
|
210
|
+
cursor += 1;
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
if (/^\s/.test(candidateLine)) {
|
|
214
|
+
blockLines.push(candidateLine);
|
|
215
|
+
cursor += 1;
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
break;
|
|
219
|
+
}
|
|
220
|
+
lineIndex = cursor - 1;
|
|
221
|
+
const indents = blockLines
|
|
222
|
+
.filter((item) => item.trim())
|
|
223
|
+
.map((item) => {
|
|
224
|
+
const indentMatch = item.match(/^[ \t]*/);
|
|
225
|
+
return indentMatch ? indentMatch[0].length : 0;
|
|
226
|
+
});
|
|
227
|
+
const commonIndent = indents.length ? Math.min(...indents) : 0;
|
|
228
|
+
const deindented = blockLines.map((item) => {
|
|
229
|
+
if (!item.trim()) return '';
|
|
230
|
+
return item.slice(commonIndent);
|
|
231
|
+
});
|
|
232
|
+
if (indicator.startsWith('>')) {
|
|
233
|
+
const paragraphs = [];
|
|
234
|
+
let paragraphLines = [];
|
|
235
|
+
for (const blockLine of deindented) {
|
|
236
|
+
const blockTrimmed = blockLine.trim();
|
|
237
|
+
if (!blockTrimmed) {
|
|
238
|
+
if (paragraphLines.length) {
|
|
239
|
+
paragraphs.push(paragraphLines.join(' '));
|
|
240
|
+
paragraphLines = [];
|
|
241
|
+
}
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
paragraphLines.push(blockTrimmed);
|
|
245
|
+
}
|
|
246
|
+
if (paragraphLines.length) {
|
|
247
|
+
paragraphs.push(paragraphLines.join(' '));
|
|
248
|
+
}
|
|
249
|
+
value = paragraphs.join('\n');
|
|
250
|
+
} else {
|
|
251
|
+
value = deindented.join('\n');
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith('\'') && value.endsWith('\''))) {
|
|
255
|
+
value = value.slice(1, -1);
|
|
256
|
+
}
|
|
257
|
+
result[key] = value.trim();
|
|
258
|
+
}
|
|
259
|
+
return result;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function stripMarkdownFrontmatter(content = '') {
|
|
263
|
+
const normalized = String(content || '').replace(/\r\n/g, '\n');
|
|
264
|
+
if (!normalized.startsWith('---\n')) {
|
|
265
|
+
return normalized;
|
|
266
|
+
}
|
|
267
|
+
const endIndex = normalized.indexOf('\n---\n', 4);
|
|
268
|
+
if (endIndex <= 4) {
|
|
269
|
+
return normalized;
|
|
270
|
+
}
|
|
271
|
+
return normalized.slice(endIndex + 5);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function extractSkillDescriptionFromMarkdown(content = '') {
|
|
275
|
+
const normalized = String(content || '').replace(/\r\n/g, '\n');
|
|
276
|
+
const lines = normalized.split('\n');
|
|
277
|
+
let inFence = false;
|
|
278
|
+
for (const line of lines) {
|
|
279
|
+
const trimmedStart = line.trimStart();
|
|
280
|
+
if (trimmedStart.startsWith('```')) {
|
|
281
|
+
inFence = !inFence;
|
|
282
|
+
continue;
|
|
283
|
+
}
|
|
284
|
+
if (inFence) continue;
|
|
285
|
+
if (/^( {4}|\t)/.test(line)) continue;
|
|
286
|
+
const trimmed = line.trim();
|
|
287
|
+
if (!trimmed) continue;
|
|
288
|
+
if (trimmed.startsWith('#')) continue;
|
|
289
|
+
if (trimmed.startsWith('---')) continue;
|
|
290
|
+
if (/^([A-Za-z0-9_-]+)\s*:\s*/.test(trimmed)) continue;
|
|
291
|
+
return trimmed.slice(0, 200);
|
|
292
|
+
}
|
|
293
|
+
return '';
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function readCodexSkillMetadata(skillPath) {
|
|
297
|
+
const skillFile = path.join(skillPath, 'SKILL.md');
|
|
298
|
+
if (!fs.existsSync(skillFile)) {
|
|
299
|
+
return {
|
|
300
|
+
hasSkillFile: false,
|
|
301
|
+
displayName: '',
|
|
302
|
+
description: ''
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
try {
|
|
306
|
+
const raw = fs.readFileSync(skillFile, 'utf-8');
|
|
307
|
+
const content = stripUtf8Bom(raw);
|
|
308
|
+
const frontmatter = parseSimpleSkillFrontmatter(content);
|
|
309
|
+
const contentWithoutFrontmatter = stripMarkdownFrontmatter(content);
|
|
310
|
+
const heading = contentWithoutFrontmatter.match(/^\s*#\s+(.+)$/m);
|
|
311
|
+
const displayName = typeof frontmatter.name === 'string' && frontmatter.name.trim()
|
|
312
|
+
? frontmatter.name.trim()
|
|
313
|
+
: (heading && heading[1] ? heading[1].trim() : '');
|
|
314
|
+
const description = typeof frontmatter.description === 'string' && frontmatter.description.trim()
|
|
315
|
+
? frontmatter.description.trim().slice(0, 200)
|
|
316
|
+
: extractSkillDescriptionFromMarkdown(contentWithoutFrontmatter);
|
|
317
|
+
return {
|
|
318
|
+
hasSkillFile: true,
|
|
319
|
+
displayName,
|
|
320
|
+
description
|
|
321
|
+
};
|
|
322
|
+
} catch (e) {
|
|
323
|
+
return {
|
|
324
|
+
hasSkillFile: false,
|
|
325
|
+
displayName: '',
|
|
326
|
+
description: ''
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function getSkillEntryInfoByName(rootDir, entryName) {
|
|
332
|
+
const targetPath = path.join(rootDir, entryName);
|
|
333
|
+
const normalized = normalizeCodexSkillName(entryName);
|
|
334
|
+
if (normalized.error) {
|
|
335
|
+
return null;
|
|
336
|
+
}
|
|
337
|
+
const relativePath = path.relative(rootDir, targetPath);
|
|
338
|
+
if (relativePath.startsWith('..') || path.isAbsolute(relativePath)) {
|
|
339
|
+
return null;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
try {
|
|
343
|
+
const lstat = fs.lstatSync(targetPath);
|
|
344
|
+
const isSymbolicLink = lstat.isSymbolicLink();
|
|
345
|
+
if (!lstat.isDirectory() && !isSymbolicLink) {
|
|
346
|
+
return null;
|
|
347
|
+
}
|
|
348
|
+
if (isSymbolicLink && !isSkillDirectoryEntryAtRoot(rootDir, entryName)) {
|
|
349
|
+
return null;
|
|
350
|
+
}
|
|
351
|
+
const metadata = readCodexSkillMetadata(targetPath);
|
|
352
|
+
return {
|
|
353
|
+
name: entryName,
|
|
354
|
+
path: targetPath,
|
|
355
|
+
hasSkillFile: !!metadata.hasSkillFile,
|
|
356
|
+
displayName: metadata.displayName || entryName,
|
|
357
|
+
description: metadata.description || '',
|
|
358
|
+
sourceType: isSymbolicLink ? 'symlink' : 'directory',
|
|
359
|
+
updatedAt: Number.isFinite(lstat.mtimeMs) ? Math.floor(lstat.mtimeMs) : 0
|
|
360
|
+
};
|
|
361
|
+
} catch (e) {
|
|
362
|
+
return null;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function listSkills(params = {}) {
|
|
367
|
+
const target = resolveSkillTarget(params);
|
|
368
|
+
if (!target) {
|
|
369
|
+
return { error: '目标宿主不支持' };
|
|
370
|
+
}
|
|
371
|
+
if (!fs.existsSync(target.dir)) {
|
|
372
|
+
return {
|
|
373
|
+
targetApp: target.app,
|
|
374
|
+
targetLabel: target.label,
|
|
375
|
+
root: target.dir,
|
|
376
|
+
exists: false,
|
|
377
|
+
items: []
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
try {
|
|
381
|
+
const entries = fs.readdirSync(target.dir, { withFileTypes: true });
|
|
382
|
+
const items = entries
|
|
383
|
+
.map((entry) => {
|
|
384
|
+
const name = entry && entry.name ? entry.name : '';
|
|
385
|
+
if (!name || name.startsWith('.')) return null;
|
|
386
|
+
return getSkillEntryInfoByName(target.dir, name);
|
|
387
|
+
})
|
|
388
|
+
.filter(Boolean)
|
|
389
|
+
.sort((a, b) => a.displayName.localeCompare(b.displayName, 'zh-Hans-CN'));
|
|
390
|
+
return {
|
|
391
|
+
targetApp: target.app,
|
|
392
|
+
targetLabel: target.label,
|
|
393
|
+
root: target.dir,
|
|
394
|
+
exists: true,
|
|
395
|
+
items
|
|
396
|
+
};
|
|
397
|
+
} catch (e) {
|
|
398
|
+
return { error: `读取 skills 目录失败: ${e.message}` };
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
function listCodexSkills() {
|
|
403
|
+
return listSkills({ targetApp: 'codex' });
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
function listSkillEntriesByRoot(rootDir) {
|
|
407
|
+
if (!rootDir || !fs.existsSync(rootDir)) {
|
|
408
|
+
return [];
|
|
409
|
+
}
|
|
410
|
+
try {
|
|
411
|
+
const entries = fs.readdirSync(rootDir, { withFileTypes: true });
|
|
412
|
+
return entries
|
|
413
|
+
.map((entry) => {
|
|
414
|
+
const name = entry && entry.name ? entry.name : '';
|
|
415
|
+
if (!name || name.startsWith('.')) return null;
|
|
416
|
+
const normalized = normalizeCodexSkillName(name);
|
|
417
|
+
if (normalized.error) return null;
|
|
418
|
+
const skillPath = path.join(rootDir, name);
|
|
419
|
+
const relativePath = path.relative(rootDir, skillPath);
|
|
420
|
+
if (relativePath.startsWith('..') || path.isAbsolute(relativePath)) {
|
|
421
|
+
return null;
|
|
422
|
+
}
|
|
423
|
+
try {
|
|
424
|
+
const lstat = fs.lstatSync(skillPath);
|
|
425
|
+
const isSymbolicLink = lstat.isSymbolicLink();
|
|
426
|
+
if (!lstat.isDirectory() && !isSymbolicLink) {
|
|
427
|
+
return null;
|
|
428
|
+
}
|
|
429
|
+
if (isSymbolicLink) {
|
|
430
|
+
const realPath = fs.realpathSync(skillPath);
|
|
431
|
+
const realStat = fs.statSync(realPath);
|
|
432
|
+
if (!realStat.isDirectory()) {
|
|
433
|
+
return null;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
return {
|
|
437
|
+
name,
|
|
438
|
+
path: skillPath,
|
|
439
|
+
sourceType: isSymbolicLink ? 'symlink' : 'directory'
|
|
440
|
+
};
|
|
441
|
+
} catch (e) {
|
|
442
|
+
return null;
|
|
443
|
+
}
|
|
444
|
+
})
|
|
445
|
+
.filter(Boolean);
|
|
446
|
+
} catch (e) {
|
|
447
|
+
return [];
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
function scanUnmanagedSkills(params = {}) {
|
|
452
|
+
const getPathApi = (basePath) => {
|
|
453
|
+
const base = typeof basePath === 'string' ? basePath.trim() : '';
|
|
454
|
+
return base.includes('/') && !base.includes('\\') && path.posix ? path.posix : path;
|
|
455
|
+
};
|
|
456
|
+
const target = resolveSkillTarget(params);
|
|
457
|
+
if (!target) {
|
|
458
|
+
return { error: '目标宿主不支持' };
|
|
459
|
+
}
|
|
460
|
+
const targetRoot = resolveCopyTargetRoot(target.dir);
|
|
461
|
+
const targetPathApi = getPathApi(targetRoot);
|
|
462
|
+
const existing = listSkills({ targetApp: target.app });
|
|
463
|
+
if (existing.error) {
|
|
464
|
+
return { error: existing.error };
|
|
465
|
+
}
|
|
466
|
+
const existingNames = new Set((Array.isArray(existing.items) ? existing.items : [])
|
|
467
|
+
.map((item) => (item && typeof item.name === 'string' ? item.name.trim() : ''))
|
|
468
|
+
.filter(Boolean));
|
|
469
|
+
|
|
470
|
+
const items = [];
|
|
471
|
+
const sources = SKILL_IMPORT_SOURCES.filter((source) => source.app !== target.app);
|
|
472
|
+
for (const source of sources) {
|
|
473
|
+
const sourceEntries = listSkillEntriesByRoot(source.dir);
|
|
474
|
+
for (const entry of sourceEntries) {
|
|
475
|
+
const targetCandidate = targetPathApi.join(targetRoot, entry.name);
|
|
476
|
+
if (fs.existsSync(targetCandidate)) {
|
|
477
|
+
continue;
|
|
478
|
+
}
|
|
479
|
+
if (existingNames.has(entry.name)) {
|
|
480
|
+
continue;
|
|
481
|
+
}
|
|
482
|
+
const metadata = readCodexSkillMetadata(entry.path);
|
|
483
|
+
items.push({
|
|
484
|
+
key: `${source.app}:${entry.name}`,
|
|
485
|
+
name: entry.name,
|
|
486
|
+
displayName: metadata.displayName || entry.name,
|
|
487
|
+
description: metadata.description || '',
|
|
488
|
+
sourceApp: source.app,
|
|
489
|
+
sourceLabel: source.label,
|
|
490
|
+
sourcePath: entry.path,
|
|
491
|
+
sourceType: entry.sourceType,
|
|
492
|
+
hasSkillFile: !!metadata.hasSkillFile
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
items.sort((a, b) => {
|
|
498
|
+
const nameCompare = a.displayName.localeCompare(b.displayName, 'zh-Hans-CN');
|
|
499
|
+
if (nameCompare !== 0) return nameCompare;
|
|
500
|
+
return a.sourceLabel.localeCompare(b.sourceLabel, 'zh-Hans-CN');
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
return {
|
|
504
|
+
targetApp: target.app,
|
|
505
|
+
targetLabel: target.label,
|
|
506
|
+
root: target.dir,
|
|
507
|
+
items,
|
|
508
|
+
sources: sources.map((source) => ({
|
|
509
|
+
app: source.app,
|
|
510
|
+
label: source.label,
|
|
511
|
+
path: source.dir,
|
|
512
|
+
exists: fs.existsSync(source.dir)
|
|
513
|
+
}))
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
function scanUnmanagedCodexSkills() {
|
|
518
|
+
return scanUnmanagedSkills({ targetApp: 'codex' });
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
function importSkills(params = {}) {
|
|
522
|
+
const getPathApi = (basePath) => {
|
|
523
|
+
const base = typeof basePath === 'string' ? basePath.trim() : '';
|
|
524
|
+
return base.includes('/') && !base.includes('\\') && path.posix ? path.posix : path;
|
|
525
|
+
};
|
|
526
|
+
const target = resolveSkillTarget(params);
|
|
527
|
+
if (!target) {
|
|
528
|
+
return { error: '目标宿主不支持' };
|
|
529
|
+
}
|
|
530
|
+
const targetRoot = resolveCopyTargetRoot(target.dir);
|
|
531
|
+
const targetPathApi = getPathApi(targetRoot);
|
|
532
|
+
const rawItems = Array.isArray(params.items) ? params.items : [];
|
|
533
|
+
if (!rawItems.length) {
|
|
534
|
+
return { error: '请先选择要导入的 skill' };
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
const imported = [];
|
|
538
|
+
const failed = [];
|
|
539
|
+
const dedup = new Set();
|
|
540
|
+
|
|
541
|
+
for (const rawItem of rawItems) {
|
|
542
|
+
const safeItem = rawItem && typeof rawItem === 'object' ? rawItem : {};
|
|
543
|
+
const normalizedName = normalizeCodexSkillName(safeItem.name);
|
|
544
|
+
if (normalizedName.error) {
|
|
545
|
+
failed.push({
|
|
546
|
+
name: safeItem && safeItem.name ? String(safeItem.name) : '',
|
|
547
|
+
sourceApp: safeItem && safeItem.sourceApp ? String(safeItem.sourceApp) : '',
|
|
548
|
+
error: normalizedName.error
|
|
549
|
+
});
|
|
550
|
+
continue;
|
|
551
|
+
}
|
|
552
|
+
const source = getSkillImportSourceByApp(safeItem.sourceApp);
|
|
553
|
+
if (!source) {
|
|
554
|
+
failed.push({
|
|
555
|
+
name: normalizedName.name,
|
|
556
|
+
sourceApp: safeItem && safeItem.sourceApp ? String(safeItem.sourceApp) : '',
|
|
557
|
+
error: '来源应用不支持'
|
|
558
|
+
});
|
|
559
|
+
continue;
|
|
560
|
+
}
|
|
561
|
+
if (source.app === target.app) {
|
|
562
|
+
failed.push({
|
|
563
|
+
name: normalizedName.name,
|
|
564
|
+
sourceApp: source.app,
|
|
565
|
+
error: '来源与目标相同,无需导入'
|
|
566
|
+
});
|
|
567
|
+
continue;
|
|
568
|
+
}
|
|
569
|
+
const dedupKey = `${source.app}:${normalizedName.name}`;
|
|
570
|
+
if (dedup.has(dedupKey)) {
|
|
571
|
+
continue;
|
|
572
|
+
}
|
|
573
|
+
dedup.add(dedupKey);
|
|
574
|
+
|
|
575
|
+
const sourcePathApi = getPathApi(source.dir);
|
|
576
|
+
const sourcePath = sourcePathApi.join(source.dir, normalizedName.name);
|
|
577
|
+
const sourceRelative = sourcePathApi.relative(source.dir, sourcePath);
|
|
578
|
+
if (sourceRelative.startsWith('..') || sourcePathApi.isAbsolute(sourceRelative)) {
|
|
579
|
+
failed.push({
|
|
580
|
+
name: normalizedName.name,
|
|
581
|
+
sourceApp: source.app,
|
|
582
|
+
error: '来源路径非法'
|
|
583
|
+
});
|
|
584
|
+
continue;
|
|
585
|
+
}
|
|
586
|
+
if (!fs.existsSync(sourcePath)) {
|
|
587
|
+
failed.push({
|
|
588
|
+
name: normalizedName.name,
|
|
589
|
+
sourceApp: source.app,
|
|
590
|
+
error: '来源 skill 不存在'
|
|
591
|
+
});
|
|
592
|
+
continue;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
const targetPath = targetPathApi.join(targetRoot, normalizedName.name);
|
|
596
|
+
const targetRelative = targetPathApi.relative(targetRoot, targetPath);
|
|
597
|
+
if (targetRelative.startsWith('..') || targetPathApi.isAbsolute(targetRelative)) {
|
|
598
|
+
failed.push({
|
|
599
|
+
name: normalizedName.name,
|
|
600
|
+
sourceApp: source.app,
|
|
601
|
+
error: '目标路径非法'
|
|
602
|
+
});
|
|
603
|
+
continue;
|
|
604
|
+
}
|
|
605
|
+
if (fs.existsSync(targetPath)) {
|
|
606
|
+
failed.push({
|
|
607
|
+
name: normalizedName.name,
|
|
608
|
+
sourceApp: source.app,
|
|
609
|
+
error: `${target.label} 中已存在同名 skill`
|
|
610
|
+
});
|
|
611
|
+
continue;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
let copiedToTarget = false;
|
|
615
|
+
try {
|
|
616
|
+
const lstat = fs.lstatSync(sourcePath);
|
|
617
|
+
if (!lstat.isDirectory() && !lstat.isSymbolicLink()) {
|
|
618
|
+
failed.push({
|
|
619
|
+
name: normalizedName.name,
|
|
620
|
+
sourceApp: source.app,
|
|
621
|
+
error: '来源不是技能目录'
|
|
622
|
+
});
|
|
623
|
+
continue;
|
|
624
|
+
}
|
|
625
|
+
const sourceDirForCopy = lstat.isSymbolicLink() ? fs.realpathSync(sourcePath) : sourcePath;
|
|
626
|
+
const sourceStat = fs.statSync(sourceDirForCopy);
|
|
627
|
+
if (!sourceStat.isDirectory()) {
|
|
628
|
+
failed.push({
|
|
629
|
+
name: normalizedName.name,
|
|
630
|
+
sourceApp: source.app,
|
|
631
|
+
error: '来源 skill 无法读取'
|
|
632
|
+
});
|
|
633
|
+
continue;
|
|
634
|
+
}
|
|
635
|
+
if (isPathInside(targetRoot, sourceDirForCopy)) {
|
|
636
|
+
failed.push({
|
|
637
|
+
name: normalizedName.name,
|
|
638
|
+
sourceApp: source.app,
|
|
639
|
+
error: '目标路径不能位于来源 skill 目录内'
|
|
640
|
+
});
|
|
641
|
+
continue;
|
|
642
|
+
}
|
|
643
|
+
ensureDir(targetRoot);
|
|
644
|
+
const visitedRealPaths = new Set([sourceDirForCopy]);
|
|
645
|
+
copyDirRecursive(sourceDirForCopy, targetPath, {
|
|
646
|
+
dereferenceSymlinks: true,
|
|
647
|
+
allowedRootRealPath: sourceDirForCopy,
|
|
648
|
+
visitedRealPaths
|
|
649
|
+
});
|
|
650
|
+
copiedToTarget = true;
|
|
651
|
+
imported.push({
|
|
652
|
+
name: normalizedName.name,
|
|
653
|
+
sourceApp: source.app,
|
|
654
|
+
sourceLabel: source.label,
|
|
655
|
+
targetApp: target.app,
|
|
656
|
+
targetLabel: target.label,
|
|
657
|
+
path: targetPath
|
|
658
|
+
});
|
|
659
|
+
} catch (e) {
|
|
660
|
+
if (!copiedToTarget && fs.existsSync(targetPath)) {
|
|
661
|
+
try {
|
|
662
|
+
removeDirectoryRecursive(targetPath);
|
|
663
|
+
} catch (_) {}
|
|
664
|
+
}
|
|
665
|
+
failed.push({
|
|
666
|
+
name: normalizedName.name,
|
|
667
|
+
sourceApp: source.app,
|
|
668
|
+
error: e && e.message ? e.message : '导入失败'
|
|
669
|
+
});
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
return {
|
|
674
|
+
success: failed.length === 0,
|
|
675
|
+
imported,
|
|
676
|
+
failed,
|
|
677
|
+
targetApp: target.app,
|
|
678
|
+
targetLabel: target.label,
|
|
679
|
+
root: targetRoot
|
|
680
|
+
};
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
function importCodexSkills(params = {}) {
|
|
684
|
+
return importSkills({ ...(params || {}), targetApp: 'codex' });
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
function collectSkillDirectoriesFromRoot(rootDir, limit = MAX_SKILLS_ZIP_ENTRY_COUNT) {
|
|
688
|
+
const results = [];
|
|
689
|
+
let truncated = false;
|
|
690
|
+
if (!rootDir || !fs.existsSync(rootDir)) {
|
|
691
|
+
return { results, truncated };
|
|
692
|
+
}
|
|
693
|
+
const normalizedLimit = Number.isFinite(limit) && limit > 0
|
|
694
|
+
? Math.floor(limit)
|
|
695
|
+
: MAX_SKILLS_ZIP_ENTRY_COUNT;
|
|
696
|
+
const stack = [rootDir];
|
|
697
|
+
while (stack.length > 0) {
|
|
698
|
+
if (results.length >= normalizedLimit) {
|
|
699
|
+
truncated = true;
|
|
700
|
+
break;
|
|
701
|
+
}
|
|
702
|
+
const currentDir = stack.pop();
|
|
703
|
+
let entries = [];
|
|
704
|
+
try {
|
|
705
|
+
entries = fs.readdirSync(currentDir, { withFileTypes: true });
|
|
706
|
+
} catch (e) {
|
|
707
|
+
continue;
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
const hasSkillFile = entries.some((entry) => entry && entry.isFile() && String(entry.name || '') === 'SKILL.md');
|
|
711
|
+
if (hasSkillFile) {
|
|
712
|
+
results.push(currentDir);
|
|
713
|
+
continue;
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
for (const entry of entries) {
|
|
717
|
+
if (!entry || !entry.isDirectory()) continue;
|
|
718
|
+
const entryName = typeof entry.name === 'string' ? entry.name.trim() : '';
|
|
719
|
+
if (!entryName || entryName.startsWith('.')) {
|
|
720
|
+
continue;
|
|
721
|
+
}
|
|
722
|
+
stack.push(path.join(currentDir, entryName));
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
return { results, truncated };
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
function resolveSkillNameFromImportedDirectory(skillDir, extractionRoot, fallbackName = '') {
|
|
729
|
+
const directoryBaseName = path.basename(skillDir || '');
|
|
730
|
+
const extractionBaseName = path.basename(extractionRoot || '');
|
|
731
|
+
let candidate = directoryBaseName;
|
|
732
|
+
if (!candidate || candidate === extractionBaseName || candidate.startsWith('.')) {
|
|
733
|
+
const fallback = typeof fallbackName === 'string' ? fallbackName.trim() : '';
|
|
734
|
+
const fallbackBase = fallback ? path.basename(fallback, path.extname(fallback)) : '';
|
|
735
|
+
candidate = fallbackBase || candidate;
|
|
736
|
+
}
|
|
737
|
+
return normalizeCodexSkillName(candidate);
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
async function importSkillsFromZipFile(zipPath, options = {}) {
|
|
741
|
+
const getPathApi = (basePath) => {
|
|
742
|
+
const base = typeof basePath === 'string' ? basePath.trim() : '';
|
|
743
|
+
return base.includes('/') && !base.includes('\\') && path.posix ? path.posix : path;
|
|
744
|
+
};
|
|
745
|
+
const fallbackName = typeof options.fallbackName === 'string' ? options.fallbackName : '';
|
|
746
|
+
const tempDir = typeof options.tempDir === 'string' ? options.tempDir : '';
|
|
747
|
+
const imported = [];
|
|
748
|
+
const failed = [];
|
|
749
|
+
const dedupNames = new Set();
|
|
750
|
+
const extractionPathApi = getPathApi(tempDir || zipPath);
|
|
751
|
+
const extractionBaseDir = tempDir || extractionPathApi.dirname(zipPath);
|
|
752
|
+
const extractionRoot = extractionPathApi.join(extractionBaseDir, 'extract');
|
|
753
|
+
let target = null;
|
|
754
|
+
let targetRoot = '';
|
|
755
|
+
|
|
756
|
+
try {
|
|
757
|
+
target = resolveSkillTarget(options, 'codex');
|
|
758
|
+
if (!target) {
|
|
759
|
+
return { error: '目标宿主不支持' };
|
|
760
|
+
}
|
|
761
|
+
targetRoot = resolveCopyTargetRoot(target.dir);
|
|
762
|
+
const targetPathApi = getPathApi(targetRoot);
|
|
763
|
+
await inspectZipArchiveLimits(zipPath, {
|
|
764
|
+
maxEntryCount: MAX_SKILLS_ZIP_ENTRY_COUNT,
|
|
765
|
+
maxUncompressedBytes: MAX_SKILLS_ZIP_UNCOMPRESSED_BYTES
|
|
766
|
+
});
|
|
767
|
+
|
|
768
|
+
await extractUploadZip(zipPath, extractionRoot);
|
|
769
|
+
const discovery = collectSkillDirectoriesFromRoot(extractionRoot, MAX_SKILLS_ZIP_ENTRY_COUNT);
|
|
770
|
+
const discoveredDirs = discovery.results;
|
|
771
|
+
if (discoveredDirs.length === 0) {
|
|
772
|
+
return { error: '压缩包中未发现包含 SKILL.md 的技能目录' };
|
|
773
|
+
}
|
|
774
|
+
if (discovery.truncated) {
|
|
775
|
+
return { error: '压缩包中的技能目录数量超出导入上限' };
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
for (const skillDir of discoveredDirs) {
|
|
779
|
+
const normalizedName = resolveSkillNameFromImportedDirectory(skillDir, extractionRoot, fallbackName);
|
|
780
|
+
if (normalizedName.error) {
|
|
781
|
+
failed.push({
|
|
782
|
+
name: path.basename(skillDir || ''),
|
|
783
|
+
error: normalizedName.error
|
|
784
|
+
});
|
|
785
|
+
continue;
|
|
786
|
+
}
|
|
787
|
+
const dedupKey = normalizedName.name.toLowerCase();
|
|
788
|
+
if (dedupNames.has(dedupKey)) {
|
|
789
|
+
continue;
|
|
790
|
+
}
|
|
791
|
+
dedupNames.add(dedupKey);
|
|
792
|
+
|
|
793
|
+
const targetPath = targetPathApi.join(targetRoot, normalizedName.name);
|
|
794
|
+
const targetRelative = targetPathApi.relative(targetRoot, targetPath);
|
|
795
|
+
if (targetRelative.startsWith('..') || targetPathApi.isAbsolute(targetRelative)) {
|
|
796
|
+
failed.push({
|
|
797
|
+
name: normalizedName.name,
|
|
798
|
+
error: '目标路径非法'
|
|
799
|
+
});
|
|
800
|
+
continue;
|
|
801
|
+
}
|
|
802
|
+
if (fs.existsSync(targetPath)) {
|
|
803
|
+
failed.push({
|
|
804
|
+
name: normalizedName.name,
|
|
805
|
+
error: `${target.label} 中已存在同名 skill`
|
|
806
|
+
});
|
|
807
|
+
continue;
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
let copiedToTarget = false;
|
|
811
|
+
try {
|
|
812
|
+
const sourceRealPath = fs.realpathSync(skillDir);
|
|
813
|
+
const sourceStat = fs.statSync(sourceRealPath);
|
|
814
|
+
if (!sourceStat.isDirectory()) {
|
|
815
|
+
failed.push({
|
|
816
|
+
name: normalizedName.name,
|
|
817
|
+
error: '来源 skill 无法读取'
|
|
818
|
+
});
|
|
819
|
+
continue;
|
|
820
|
+
}
|
|
821
|
+
if (isPathInside(targetRoot, sourceRealPath)) {
|
|
822
|
+
failed.push({
|
|
823
|
+
name: normalizedName.name,
|
|
824
|
+
error: '目标路径不能位于来源 skill 目录内'
|
|
825
|
+
});
|
|
826
|
+
continue;
|
|
827
|
+
}
|
|
828
|
+
ensureDir(targetRoot);
|
|
829
|
+
const visitedRealPaths = new Set([sourceRealPath]);
|
|
830
|
+
copyDirRecursive(sourceRealPath, targetPath, {
|
|
831
|
+
dereferenceSymlinks: true,
|
|
832
|
+
allowedRootRealPath: sourceRealPath,
|
|
833
|
+
visitedRealPaths
|
|
834
|
+
});
|
|
835
|
+
copiedToTarget = true;
|
|
836
|
+
imported.push({
|
|
837
|
+
name: normalizedName.name,
|
|
838
|
+
targetApp: target.app,
|
|
839
|
+
targetLabel: target.label,
|
|
840
|
+
path: targetPath
|
|
841
|
+
});
|
|
842
|
+
} catch (e) {
|
|
843
|
+
if (!copiedToTarget && fs.existsSync(targetPath)) {
|
|
844
|
+
try {
|
|
845
|
+
removeDirectoryRecursive(targetPath);
|
|
846
|
+
} catch (_) {}
|
|
847
|
+
}
|
|
848
|
+
failed.push({
|
|
849
|
+
name: normalizedName.name,
|
|
850
|
+
error: e && e.message ? e.message : '导入失败'
|
|
851
|
+
});
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
if (imported.length === 0 && failed.length > 0) {
|
|
856
|
+
return {
|
|
857
|
+
error: failed[0].error || '导入失败',
|
|
858
|
+
imported,
|
|
859
|
+
failed,
|
|
860
|
+
targetApp: target.app,
|
|
861
|
+
targetLabel: target.label,
|
|
862
|
+
root: targetRoot
|
|
863
|
+
};
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
return {
|
|
867
|
+
success: failed.length === 0,
|
|
868
|
+
imported,
|
|
869
|
+
failed,
|
|
870
|
+
targetApp: target.app,
|
|
871
|
+
targetLabel: target.label,
|
|
872
|
+
root: targetRoot
|
|
873
|
+
};
|
|
874
|
+
} catch (e) {
|
|
875
|
+
return {
|
|
876
|
+
error: `导入失败:${e && e.message ? e.message : '未知错误'}`
|
|
877
|
+
};
|
|
878
|
+
} finally {
|
|
879
|
+
if (tempDir) {
|
|
880
|
+
try {
|
|
881
|
+
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
882
|
+
} catch (_) {}
|
|
883
|
+
} else if (fs.existsSync(extractionRoot)) {
|
|
884
|
+
try {
|
|
885
|
+
fs.rmSync(extractionRoot, { recursive: true, force: true });
|
|
886
|
+
} catch (_) {}
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
async function importCodexSkillsFromZipFile(zipPath, options = {}) {
|
|
892
|
+
return importSkillsFromZipFile(zipPath, { ...(options || {}), targetApp: 'codex' });
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
async function importSkillsFromZip(payload = {}) {
|
|
896
|
+
if (!payload || typeof payload.fileBase64 !== 'string' || !payload.fileBase64.trim()) {
|
|
897
|
+
return { error: '缺少技能压缩包内容' };
|
|
898
|
+
}
|
|
899
|
+
const fallbackTarget = resolveSkillTarget(payload, 'codex');
|
|
900
|
+
const fallbackTargetApp = fallbackTarget ? fallbackTarget.app : 'codex';
|
|
901
|
+
const fallbackName = payload.fileName || `${fallbackTargetApp}-skills.zip`;
|
|
902
|
+
const upload = writeUploadZip(payload.fileBase64, 'codex-skills-import', fallbackName);
|
|
903
|
+
if (upload.error) {
|
|
904
|
+
return { error: upload.error };
|
|
905
|
+
}
|
|
906
|
+
const importOptions = { tempDir: upload.tempDir, fallbackName };
|
|
907
|
+
if (Object.prototype.hasOwnProperty.call(payload, 'targetApp')) {
|
|
908
|
+
importOptions.targetApp = payload.targetApp;
|
|
909
|
+
}
|
|
910
|
+
if (Object.prototype.hasOwnProperty.call(payload, 'target')) {
|
|
911
|
+
importOptions.target = payload.target;
|
|
912
|
+
}
|
|
913
|
+
return importSkillsFromZipFile(upload.zipPath, importOptions);
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
async function importCodexSkillsFromZip(payload = {}) {
|
|
917
|
+
return importSkillsFromZip({ ...(payload || {}), targetApp: 'codex' });
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
async function exportSkills(params = {}) {
|
|
921
|
+
const target = resolveSkillTarget(params);
|
|
922
|
+
if (!target) {
|
|
923
|
+
return { error: '目标宿主不支持' };
|
|
924
|
+
}
|
|
925
|
+
const rawNames = Array.isArray(params.names) ? params.names : [];
|
|
926
|
+
const uniqueNames = Array.from(new Set(rawNames
|
|
927
|
+
.map((item) => (typeof item === 'string' ? item.trim() : ''))
|
|
928
|
+
.filter(Boolean)));
|
|
929
|
+
if (uniqueNames.length === 0) {
|
|
930
|
+
return { error: '请先选择要导出的 skill' };
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
const exported = [];
|
|
934
|
+
const failed = [];
|
|
935
|
+
const stagingTempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codex-skills-export-'));
|
|
936
|
+
const stagingRoot = path.join(stagingTempDir, 'skills');
|
|
937
|
+
ensureDir(stagingRoot);
|
|
938
|
+
|
|
939
|
+
try {
|
|
940
|
+
for (const rawName of uniqueNames) {
|
|
941
|
+
const normalizedName = normalizeCodexSkillName(rawName);
|
|
942
|
+
if (normalizedName.error) {
|
|
943
|
+
failed.push({ name: rawName, error: normalizedName.error });
|
|
944
|
+
continue;
|
|
945
|
+
}
|
|
946
|
+
const sourcePath = path.join(target.dir, normalizedName.name);
|
|
947
|
+
const sourceRelative = path.relative(target.dir, sourcePath);
|
|
948
|
+
if (sourceRelative.startsWith('..') || path.isAbsolute(sourceRelative)) {
|
|
949
|
+
failed.push({ name: normalizedName.name, error: '来源路径非法' });
|
|
950
|
+
continue;
|
|
951
|
+
}
|
|
952
|
+
if (!fs.existsSync(sourcePath)) {
|
|
953
|
+
failed.push({ name: normalizedName.name, error: 'skill 不存在' });
|
|
954
|
+
continue;
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
try {
|
|
958
|
+
const lstat = fs.lstatSync(sourcePath);
|
|
959
|
+
if (!lstat.isDirectory() && !lstat.isSymbolicLink()) {
|
|
960
|
+
failed.push({ name: normalizedName.name, error: '来源不是技能目录' });
|
|
961
|
+
continue;
|
|
962
|
+
}
|
|
963
|
+
const sourceDirForCopy = lstat.isSymbolicLink() ? fs.realpathSync(sourcePath) : sourcePath;
|
|
964
|
+
const sourceStat = fs.statSync(sourceDirForCopy);
|
|
965
|
+
if (!sourceStat.isDirectory()) {
|
|
966
|
+
failed.push({ name: normalizedName.name, error: '来源 skill 无法读取' });
|
|
967
|
+
continue;
|
|
968
|
+
}
|
|
969
|
+
const targetPath = path.join(stagingRoot, normalizedName.name);
|
|
970
|
+
const visitedRealPaths = new Set([sourceDirForCopy]);
|
|
971
|
+
copyDirRecursive(sourceDirForCopy, targetPath, {
|
|
972
|
+
dereferenceSymlinks: true,
|
|
973
|
+
allowedRootRealPath: sourceDirForCopy,
|
|
974
|
+
visitedRealPaths
|
|
975
|
+
});
|
|
976
|
+
exported.push({
|
|
977
|
+
name: normalizedName.name,
|
|
978
|
+
path: sourcePath
|
|
979
|
+
});
|
|
980
|
+
} catch (e) {
|
|
981
|
+
failed.push({
|
|
982
|
+
name: normalizedName.name,
|
|
983
|
+
error: e && e.message ? e.message : '导出失败'
|
|
984
|
+
});
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
if (exported.length === 0) {
|
|
989
|
+
return {
|
|
990
|
+
error: failed[0] && failed[0].error ? failed[0].error : '无可导出的 skill',
|
|
991
|
+
exported,
|
|
992
|
+
failed,
|
|
993
|
+
targetApp: target.app,
|
|
994
|
+
targetLabel: target.label,
|
|
995
|
+
root: target.dir
|
|
996
|
+
};
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
const randomToken = crypto.randomBytes(12).toString('hex');
|
|
1000
|
+
const zipFileName = `${target.app}-skills-${randomToken}.zip`;
|
|
1001
|
+
const zipFilePath = path.join(os.tmpdir(), zipFileName);
|
|
1002
|
+
if (fs.existsSync(zipFilePath)) {
|
|
1003
|
+
try {
|
|
1004
|
+
fs.unlinkSync(zipFilePath);
|
|
1005
|
+
} catch (_) {}
|
|
1006
|
+
}
|
|
1007
|
+
await zipLib.archiveFolder(stagingRoot, zipFilePath);
|
|
1008
|
+
const artifact = registerDownloadArtifact(zipFilePath, {
|
|
1009
|
+
fileName: zipFileName,
|
|
1010
|
+
deleteAfterDownload: true
|
|
1011
|
+
});
|
|
1012
|
+
|
|
1013
|
+
return {
|
|
1014
|
+
success: failed.length === 0,
|
|
1015
|
+
fileName: zipFileName,
|
|
1016
|
+
downloadPath: artifact.downloadPath,
|
|
1017
|
+
exported,
|
|
1018
|
+
failed,
|
|
1019
|
+
targetApp: target.app,
|
|
1020
|
+
targetLabel: target.label,
|
|
1021
|
+
root: target.dir
|
|
1022
|
+
};
|
|
1023
|
+
} catch (e) {
|
|
1024
|
+
return {
|
|
1025
|
+
error: `导出失败:${e && e.message ? e.message : '未知错误'}`,
|
|
1026
|
+
exported,
|
|
1027
|
+
failed,
|
|
1028
|
+
targetApp: target.app,
|
|
1029
|
+
targetLabel: target.label,
|
|
1030
|
+
root: target.dir
|
|
1031
|
+
};
|
|
1032
|
+
} finally {
|
|
1033
|
+
try {
|
|
1034
|
+
fs.rmSync(stagingTempDir, { recursive: true, force: true });
|
|
1035
|
+
} catch (_) {}
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
async function exportCodexSkills(params = {}) {
|
|
1040
|
+
return exportSkills({ ...(params || {}), targetApp: 'codex' });
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
function removeDirectoryRecursive(targetPath) {
|
|
1044
|
+
if (typeof fs.rmSync === 'function') {
|
|
1045
|
+
fs.rmSync(targetPath, { recursive: true, force: false });
|
|
1046
|
+
return;
|
|
1047
|
+
}
|
|
1048
|
+
fs.rmdirSync(targetPath, { recursive: true });
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
function deleteSkills(params = {}) {
|
|
1052
|
+
const target = resolveSkillTarget(params);
|
|
1053
|
+
if (!target) {
|
|
1054
|
+
return { error: '目标宿主不支持' };
|
|
1055
|
+
}
|
|
1056
|
+
const rawList = Array.isArray(params.names) ? params.names : [];
|
|
1057
|
+
const uniqueNames = Array.from(new Set(rawList
|
|
1058
|
+
.map((item) => (typeof item === 'string' ? item.trim() : ''))
|
|
1059
|
+
.filter(Boolean)));
|
|
1060
|
+
if (!uniqueNames.length) {
|
|
1061
|
+
return { error: '请先选择要删除的 skill' };
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
const deleted = [];
|
|
1065
|
+
const failed = [];
|
|
1066
|
+
for (const rawName of uniqueNames) {
|
|
1067
|
+
const normalized = normalizeCodexSkillName(rawName);
|
|
1068
|
+
if (normalized.error) {
|
|
1069
|
+
failed.push({ name: rawName, error: normalized.error });
|
|
1070
|
+
continue;
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
const skillPath = path.join(target.dir, normalized.name);
|
|
1074
|
+
const relativePath = path.relative(target.dir, skillPath);
|
|
1075
|
+
if (relativePath.startsWith('..') || path.isAbsolute(relativePath)) {
|
|
1076
|
+
failed.push({ name: normalized.name, error: '技能路径非法' });
|
|
1077
|
+
continue;
|
|
1078
|
+
}
|
|
1079
|
+
if (!fs.existsSync(skillPath)) {
|
|
1080
|
+
failed.push({ name: normalized.name, error: 'skill 不存在' });
|
|
1081
|
+
continue;
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
try {
|
|
1085
|
+
const stat = fs.lstatSync(skillPath);
|
|
1086
|
+
if (!stat.isDirectory() && !stat.isSymbolicLink()) {
|
|
1087
|
+
failed.push({ name: normalized.name, error: '仅支持删除技能目录' });
|
|
1088
|
+
continue;
|
|
1089
|
+
}
|
|
1090
|
+
removeDirectoryRecursive(skillPath);
|
|
1091
|
+
deleted.push(normalized.name);
|
|
1092
|
+
} catch (e) {
|
|
1093
|
+
failed.push({
|
|
1094
|
+
name: normalized.name,
|
|
1095
|
+
error: e && e.message ? e.message : '删除失败'
|
|
1096
|
+
});
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
|
|
1100
|
+
return {
|
|
1101
|
+
success: failed.length === 0,
|
|
1102
|
+
deleted,
|
|
1103
|
+
failed,
|
|
1104
|
+
targetApp: target.app,
|
|
1105
|
+
targetLabel: target.label,
|
|
1106
|
+
root: target.dir
|
|
1107
|
+
};
|
|
1108
|
+
}
|
|
1109
|
+
|
|
1110
|
+
function deleteCodexSkills(params = {}) {
|
|
1111
|
+
return deleteSkills({ ...(params || {}), targetApp: 'codex' });
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
module.exports = {
|
|
1115
|
+
MAX_SKILLS_ZIP_UPLOAD_SIZE,
|
|
1116
|
+
MAX_SKILLS_ZIP_ENTRY_COUNT,
|
|
1117
|
+
MAX_SKILLS_ZIP_UNCOMPRESSED_BYTES,
|
|
1118
|
+
SKILL_TARGETS,
|
|
1119
|
+
SKILL_IMPORT_SOURCES,
|
|
1120
|
+
getCodexSkillsDir,
|
|
1121
|
+
getClaudeSkillsDir,
|
|
1122
|
+
normalizeSkillTargetApp,
|
|
1123
|
+
getSkillTargetByApp,
|
|
1124
|
+
resolveSkillTarget,
|
|
1125
|
+
resolveCopyTargetRoot,
|
|
1126
|
+
listSkills,
|
|
1127
|
+
listCodexSkills,
|
|
1128
|
+
scanUnmanagedSkills,
|
|
1129
|
+
scanUnmanagedCodexSkills,
|
|
1130
|
+
importSkills,
|
|
1131
|
+
importCodexSkills,
|
|
1132
|
+
importSkillsFromZipFile,
|
|
1133
|
+
importCodexSkillsFromZipFile,
|
|
1134
|
+
importSkillsFromZip,
|
|
1135
|
+
importCodexSkillsFromZip,
|
|
1136
|
+
exportSkills,
|
|
1137
|
+
exportCodexSkills,
|
|
1138
|
+
deleteSkills,
|
|
1139
|
+
deleteCodexSkills
|
|
1140
|
+
};
|
|
1141
|
+
|