dxkj 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +75 -75
- package/bin/dxk.js +11 -11
- package/fixtures/packages/hello-skill/SKILL.md +12 -12
- package/fixtures/packages/hello-skill/tool.js +5 -5
- package/fixtures/sample-registry.json +44 -44
- package/package.json +45 -45
- package/src/cli.js +320 -321
- package/src/lib/config.js +93 -93
- package/src/lib/format.js +49 -49
- package/src/lib/installer.js +69 -68
- package/src/lib/registry.js +93 -92
- package/src/lib/targets.js +50 -50
- package/src/lib/validator.js +84 -84
- package/src/lib/version.js +8 -0
- package/src/opencode.js +71 -71
package/src/lib/config.js
CHANGED
|
@@ -1,93 +1,93 @@
|
|
|
1
|
-
import fs from 'node:fs';
|
|
2
|
-
import os from 'node:os';
|
|
3
|
-
import path from 'node:path';
|
|
4
|
-
|
|
5
|
-
/** 默认服务地址:dxkj 工具站在服务器上的子域名 */
|
|
6
|
-
export const DEFAULT_SERVER_URL = 'https://tools.dxkj.online';
|
|
7
|
-
|
|
8
|
-
/** 目录 API 路径(由 NavLink 的 ai-catalog 插件提供) */
|
|
9
|
-
export const REGISTRY_PATH = '/api/plugins/ai-catalog/api/registry';
|
|
10
|
-
|
|
11
|
-
export const CONFIG_DIR = path.join(os.homedir(), '.dxkj');
|
|
12
|
-
export const CONFIG_PATH = path.join(CONFIG_DIR, 'config.json');
|
|
13
|
-
|
|
14
|
-
export function defaultConfig() {
|
|
15
|
-
return {
|
|
16
|
-
serverUrl: DEFAULT_SERVER_URL,
|
|
17
|
-
target: 'generic',
|
|
18
|
-
editor: 'opencode',
|
|
19
|
-
targets: {
|
|
20
|
-
aiEditor: {
|
|
21
|
-
opencode: {
|
|
22
|
-
base: path.join(os.homedir(), '.config', 'opencode'),
|
|
23
|
-
map: { skill: 'skills', agent: 'agent', prompt: 'command', plugin: 'plugin', tool: 'tool' }
|
|
24
|
-
},
|
|
25
|
-
claude: {
|
|
26
|
-
base: path.join(os.homedir(), '.claude'),
|
|
27
|
-
map: { skill: 'skills', agent: 'agents', prompt: 'commands', plugin: 'plugins', tool: 'tools' }
|
|
28
|
-
}
|
|
29
|
-
},
|
|
30
|
-
navlink: { pluginsDir: '' },
|
|
31
|
-
generic: { dir: '' }
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function isObject(v) {
|
|
37
|
-
return v && typeof v === 'object' && !Array.isArray(v);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export function deepMerge(base, over) {
|
|
41
|
-
if (!isObject(over)) return base;
|
|
42
|
-
const out = Array.isArray(base) ? [...base] : { ...base };
|
|
43
|
-
for (const [k, v] of Object.entries(over)) {
|
|
44
|
-
out[k] = isObject(v) && isObject(out[k]) ? deepMerge(out[k], v) : v;
|
|
45
|
-
}
|
|
46
|
-
return out;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export function readConfigFile() {
|
|
50
|
-
try {
|
|
51
|
-
return JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf-8').replace(/^\uFEFF/, ''));
|
|
52
|
-
} catch {
|
|
53
|
-
return {};
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export function loadConfig() {
|
|
58
|
-
return deepMerge(defaultConfig(), readConfigFile());
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export function saveConfig(cfg) {
|
|
62
|
-
fs.mkdirSync(CONFIG_DIR, { recursive: true });
|
|
63
|
-
fs.writeFileSync(CONFIG_PATH, JSON.stringify(cfg, null, 4) + '\n', 'utf-8');
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export function getByPath(obj, dotted) {
|
|
67
|
-
return dotted.split('.').reduce((acc, k) => (acc == null ? acc : acc[k]), obj);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/** 把 "a.b.c" 写成嵌套对象 */
|
|
71
|
-
export function setByPath(obj, dotted, value) {
|
|
72
|
-
const keys = dotted.split('.');
|
|
73
|
-
let cur = obj;
|
|
74
|
-
for (let i = 0; i < keys.length - 1; i++) {
|
|
75
|
-
if (!isObject(cur[keys[i]])) cur[keys[i]] = {};
|
|
76
|
-
cur = cur[keys[i]];
|
|
77
|
-
}
|
|
78
|
-
cur[keys[keys.length - 1]] = value;
|
|
79
|
-
return obj;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/** serverUrl 解析优先级:--server > DXKJ_SERVER_URL > 配置文件 > 默认 */
|
|
83
|
-
export function resolveServerUrl(opts = {}, cfg = loadConfig()) {
|
|
84
|
-
const raw = opts.server || process.env.DXKJ_SERVER_URL || cfg.serverUrl || DEFAULT_SERVER_URL;
|
|
85
|
-
return String(raw).replace(/\/+$/, '');
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
export function expandHome(p) {
|
|
89
|
-
if (!p) return p;
|
|
90
|
-
if (p === '~') return os.homedir();
|
|
91
|
-
if (p.startsWith('~/') || p.startsWith('~\\')) return path.join(os.homedir(), p.slice(2));
|
|
92
|
-
return p;
|
|
93
|
-
}
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
|
|
5
|
+
/** 默认服务地址:dxkj 工具站在服务器上的子域名 */
|
|
6
|
+
export const DEFAULT_SERVER_URL = 'https://tools.dxkj.online';
|
|
7
|
+
|
|
8
|
+
/** 目录 API 路径(由 NavLink 的 ai-catalog 插件提供) */
|
|
9
|
+
export const REGISTRY_PATH = '/api/plugins/ai-catalog/api/registry';
|
|
10
|
+
|
|
11
|
+
export const CONFIG_DIR = path.join(os.homedir(), '.dxkj');
|
|
12
|
+
export const CONFIG_PATH = path.join(CONFIG_DIR, 'config.json');
|
|
13
|
+
|
|
14
|
+
export function defaultConfig() {
|
|
15
|
+
return {
|
|
16
|
+
serverUrl: DEFAULT_SERVER_URL,
|
|
17
|
+
target: 'generic',
|
|
18
|
+
editor: 'opencode',
|
|
19
|
+
targets: {
|
|
20
|
+
aiEditor: {
|
|
21
|
+
opencode: {
|
|
22
|
+
base: path.join(os.homedir(), '.config', 'opencode'),
|
|
23
|
+
map: { skill: 'skills', agent: 'agent', prompt: 'command', plugin: 'plugin', tool: 'tool' }
|
|
24
|
+
},
|
|
25
|
+
claude: {
|
|
26
|
+
base: path.join(os.homedir(), '.claude'),
|
|
27
|
+
map: { skill: 'skills', agent: 'agents', prompt: 'commands', plugin: 'plugins', tool: 'tools' }
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
navlink: { pluginsDir: '' },
|
|
31
|
+
generic: { dir: '' }
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function isObject(v) {
|
|
37
|
+
return v && typeof v === 'object' && !Array.isArray(v);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function deepMerge(base, over) {
|
|
41
|
+
if (!isObject(over)) return base;
|
|
42
|
+
const out = Array.isArray(base) ? [...base] : { ...base };
|
|
43
|
+
for (const [k, v] of Object.entries(over)) {
|
|
44
|
+
out[k] = isObject(v) && isObject(out[k]) ? deepMerge(out[k], v) : v;
|
|
45
|
+
}
|
|
46
|
+
return out;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function readConfigFile() {
|
|
50
|
+
try {
|
|
51
|
+
return JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf-8').replace(/^\uFEFF/, ''));
|
|
52
|
+
} catch {
|
|
53
|
+
return {};
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function loadConfig() {
|
|
58
|
+
return deepMerge(defaultConfig(), readConfigFile());
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function saveConfig(cfg) {
|
|
62
|
+
fs.mkdirSync(CONFIG_DIR, { recursive: true });
|
|
63
|
+
fs.writeFileSync(CONFIG_PATH, JSON.stringify(cfg, null, 4) + '\n', 'utf-8');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function getByPath(obj, dotted) {
|
|
67
|
+
return dotted.split('.').reduce((acc, k) => (acc == null ? acc : acc[k]), obj);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** 把 "a.b.c" 写成嵌套对象 */
|
|
71
|
+
export function setByPath(obj, dotted, value) {
|
|
72
|
+
const keys = dotted.split('.');
|
|
73
|
+
let cur = obj;
|
|
74
|
+
for (let i = 0; i < keys.length - 1; i++) {
|
|
75
|
+
if (!isObject(cur[keys[i]])) cur[keys[i]] = {};
|
|
76
|
+
cur = cur[keys[i]];
|
|
77
|
+
}
|
|
78
|
+
cur[keys[keys.length - 1]] = value;
|
|
79
|
+
return obj;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** serverUrl 解析优先级:--server > DXKJ_SERVER_URL > 配置文件 > 默认 */
|
|
83
|
+
export function resolveServerUrl(opts = {}, cfg = loadConfig()) {
|
|
84
|
+
const raw = opts.server || process.env.DXKJ_SERVER_URL || cfg.serverUrl || DEFAULT_SERVER_URL;
|
|
85
|
+
return String(raw).replace(/\/+$/, '');
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function expandHome(p) {
|
|
89
|
+
if (!p) return p;
|
|
90
|
+
if (p === '~') return os.homedir();
|
|
91
|
+
if (p.startsWith('~/') || p.startsWith('~\\')) return path.join(os.homedir(), p.slice(2));
|
|
92
|
+
return p;
|
|
93
|
+
}
|
package/src/lib/format.js
CHANGED
|
@@ -1,49 +1,49 @@
|
|
|
1
|
-
export function truncate(str, len) {
|
|
2
|
-
const s = String(str ?? '');
|
|
3
|
-
return s.length > len ? s.slice(0, Math.max(0, len - 1)) + '…' : s;
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
export function renderList(items, { showIntro = false } = {}) {
|
|
7
|
-
if (!items.length) return '(空)';
|
|
8
|
-
const rows = items.map((it) => ({
|
|
9
|
-
no: String(it.number).padStart(4, ' '),
|
|
10
|
-
kind: truncate(it.kind, 7).padEnd(7, ' '),
|
|
11
|
-
id: it.id,
|
|
12
|
-
status: it.status,
|
|
13
|
-
desc: truncate(it.description, showIntro ? 60 : 40)
|
|
14
|
-
}));
|
|
15
|
-
const idw = Math.min(52, Math.max(4, ...rows.map((r) => r.id.length)));
|
|
16
|
-
return rows
|
|
17
|
-
.map((r) => `${r.no} ${r.kind} ${r.id.padEnd(idw, ' ')} ${r.desc}`)
|
|
18
|
-
.join('\n');
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function renderDetail(it, serverUrl) {
|
|
22
|
-
const lines = [];
|
|
23
|
-
lines.push(`#${it.number} ${it.name} [${it.kind}]`);
|
|
24
|
-
lines.push(` id ${it.id}`);
|
|
25
|
-
lines.push(` 分类 ${it.category}`);
|
|
26
|
-
lines.push(` 状态 ${it.status}`);
|
|
27
|
-
if (it.version) lines.push(` 版本 ${it.version}`);
|
|
28
|
-
if (it.author) lines.push(` 作者 ${it.author}`);
|
|
29
|
-
if (it.tags.length) lines.push(` 标签 ${it.tags.join(', ')}`);
|
|
30
|
-
lines.push(` 简介 ${it.description}`);
|
|
31
|
-
if (it.intro) {
|
|
32
|
-
lines.push(' ———— 简介区 ————');
|
|
33
|
-
for (const l of String(it.intro).split(/\r?\n/)) lines.push(` ${l}`);
|
|
34
|
-
}
|
|
35
|
-
if (it.images.length) {
|
|
36
|
-
lines.push(' ———— 图片展示区 ————');
|
|
37
|
-
for (const img of it.images) lines.push(` ${img}`);
|
|
38
|
-
}
|
|
39
|
-
lines.push(` 安装 dxkj install ${it.number}`);
|
|
40
|
-
return lines.join('\n');
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function pluck(value, keys) {
|
|
44
|
-
for (const k of keys) {
|
|
45
|
-
if (value == null) return value;
|
|
46
|
-
value = value[k];
|
|
47
|
-
}
|
|
48
|
-
return value;
|
|
49
|
-
}
|
|
1
|
+
export function truncate(str, len) {
|
|
2
|
+
const s = String(str ?? '');
|
|
3
|
+
return s.length > len ? s.slice(0, Math.max(0, len - 1)) + '…' : s;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export function renderList(items, { showIntro = false } = {}) {
|
|
7
|
+
if (!items.length) return '(空)';
|
|
8
|
+
const rows = items.map((it) => ({
|
|
9
|
+
no: String(it.number).padStart(4, ' '),
|
|
10
|
+
kind: truncate(it.kind, 7).padEnd(7, ' '),
|
|
11
|
+
id: it.id,
|
|
12
|
+
status: it.status,
|
|
13
|
+
desc: truncate(it.description, showIntro ? 60 : 40)
|
|
14
|
+
}));
|
|
15
|
+
const idw = Math.min(52, Math.max(4, ...rows.map((r) => r.id.length)));
|
|
16
|
+
return rows
|
|
17
|
+
.map((r) => `${r.no} ${r.kind} ${r.id.padEnd(idw, ' ')} ${r.desc}`)
|
|
18
|
+
.join('\n');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function renderDetail(it, serverUrl) {
|
|
22
|
+
const lines = [];
|
|
23
|
+
lines.push(`#${it.number} ${it.name} [${it.kind}]`);
|
|
24
|
+
lines.push(` id ${it.id}`);
|
|
25
|
+
lines.push(` 分类 ${it.category}`);
|
|
26
|
+
lines.push(` 状态 ${it.status}`);
|
|
27
|
+
if (it.version) lines.push(` 版本 ${it.version}`);
|
|
28
|
+
if (it.author) lines.push(` 作者 ${it.author}`);
|
|
29
|
+
if (it.tags.length) lines.push(` 标签 ${it.tags.join(', ')}`);
|
|
30
|
+
lines.push(` 简介 ${it.description}`);
|
|
31
|
+
if (it.intro) {
|
|
32
|
+
lines.push(' ———— 简介区 ————');
|
|
33
|
+
for (const l of String(it.intro).split(/\r?\n/)) lines.push(` ${l}`);
|
|
34
|
+
}
|
|
35
|
+
if (it.images.length) {
|
|
36
|
+
lines.push(' ———— 图片展示区 ————');
|
|
37
|
+
for (const img of it.images) lines.push(` ${img}`);
|
|
38
|
+
}
|
|
39
|
+
lines.push(` 安装 dxkj install ${it.number}`);
|
|
40
|
+
return lines.join('\n');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function pluck(value, keys) {
|
|
44
|
+
for (const k of keys) {
|
|
45
|
+
if (value == null) return value;
|
|
46
|
+
value = value[k];
|
|
47
|
+
}
|
|
48
|
+
return value;
|
|
49
|
+
}
|
package/src/lib/installer.js
CHANGED
|
@@ -1,68 +1,69 @@
|
|
|
1
|
-
import fs from 'node:fs';
|
|
2
|
-
import os from 'node:os';
|
|
3
|
-
import path from 'node:path';
|
|
4
|
-
import { pipeline } from 'node:stream/promises';
|
|
5
|
-
import AdmZip from 'adm-zip';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
fs.
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { pipeline } from 'node:stream/promises';
|
|
5
|
+
import AdmZip from 'adm-zip';
|
|
6
|
+
import { USER_AGENT } from './version.js';
|
|
7
|
+
|
|
8
|
+
export const MANIFEST = '.dxkj.json';
|
|
9
|
+
|
|
10
|
+
export async function downloadToTemp(url) {
|
|
11
|
+
const tmp = path.join(os.tmpdir(), `dxkj-${Date.now()}-${Math.random().toString(36).slice(2)}`);
|
|
12
|
+
fs.mkdirSync(tmp, { recursive: true });
|
|
13
|
+
const target = path.join(tmp, 'package.zip');
|
|
14
|
+
|
|
15
|
+
if (/^https?:\/\//i.test(url)) {
|
|
16
|
+
const res = await fetch(url, { headers: { 'User-Agent': USER_AGENT } });
|
|
17
|
+
if (!res.ok) throw new Error(`下载失败 HTTP ${res.status}:${url}`);
|
|
18
|
+
await pipeline(res.body, fs.createWriteStream(target));
|
|
19
|
+
} else {
|
|
20
|
+
const local = path.resolve(process.cwd(), url);
|
|
21
|
+
if (!fs.existsSync(local)) throw new Error(`安装包文件不存在:${local}`);
|
|
22
|
+
fs.copyFileSync(local, target);
|
|
23
|
+
}
|
|
24
|
+
return target;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** 解压 zip 到 destDir(含 Zip Slip 防护) */
|
|
28
|
+
export function extractZip(zipPath, destDir) {
|
|
29
|
+
const zip = new AdmZip(zipPath);
|
|
30
|
+
const resolvedDest = path.resolve(destDir);
|
|
31
|
+
fs.mkdirSync(resolvedDest, { recursive: true });
|
|
32
|
+
|
|
33
|
+
for (const entry of zip.getEntries()) {
|
|
34
|
+
const targetPath = path.resolve(resolvedDest, entry.entryName);
|
|
35
|
+
if (targetPath !== resolvedDest && !targetPath.startsWith(resolvedDest + path.sep)) {
|
|
36
|
+
throw new Error(`压缩包存在非法路径(Zip Slip):${entry.entryName}`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
zip.extractAllTo(resolvedDest, true);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function writeManifest(destDir, meta) {
|
|
43
|
+
fs.writeFileSync(path.join(destDir, MANIFEST), JSON.stringify(meta, null, 4) + '\n', 'utf-8');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function readManifest(destDir) {
|
|
47
|
+
try {
|
|
48
|
+
return JSON.parse(fs.readFileSync(path.join(destDir, MANIFEST), 'utf-8'));
|
|
49
|
+
} catch {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function removeDirSafe(destDir) {
|
|
55
|
+
const resolved = path.resolve(destDir);
|
|
56
|
+
const parsed = path.parse(resolved);
|
|
57
|
+
if (resolved === parsed.root || resolved === os.homedir()) {
|
|
58
|
+
throw new Error(`拒绝删除高危目录:${resolved}`);
|
|
59
|
+
}
|
|
60
|
+
fs.rmSync(resolved, { recursive: true, force: true });
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function cleanup(tempFile) {
|
|
64
|
+
try {
|
|
65
|
+
fs.rmSync(path.dirname(tempFile), { recursive: true, force: true });
|
|
66
|
+
} catch {
|
|
67
|
+
/* ignore */
|
|
68
|
+
}
|
|
69
|
+
}
|
package/src/lib/registry.js
CHANGED
|
@@ -1,92 +1,93 @@
|
|
|
1
|
-
import fs from 'node:fs';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
import { loadConfig, resolveServerUrl, REGISTRY_PATH } from './config.js';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
*
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
if (
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
if (opts.
|
|
81
|
-
if (opts.
|
|
82
|
-
if (opts.
|
|
83
|
-
return
|
|
84
|
-
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
return items.find((it) => it.
|
|
92
|
-
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { loadConfig, resolveServerUrl, REGISTRY_PATH } from './config.js';
|
|
4
|
+
import { USER_AGENT } from './version.js';
|
|
5
|
+
|
|
6
|
+
/** 去除 Windows 写文件常见的 UTF-8 BOM,避免 JSON.parse 失败 */
|
|
7
|
+
export function stripBom(text) {
|
|
8
|
+
return String(text).replace(/^\uFEFF/, '');
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 解析数据源。
|
|
13
|
+
* 优先级:--source > DXKJ_REGISTRY > <serverUrl>/api/plugins/ai-catalog/api/registry
|
|
14
|
+
*/
|
|
15
|
+
export function resolveSource(opts = {}, cfg = loadConfig()) {
|
|
16
|
+
if (opts.source) return opts.source;
|
|
17
|
+
if (process.env.DXKJ_REGISTRY) return process.env.DXKJ_REGISTRY;
|
|
18
|
+
return resolveServerUrl(opts, cfg) + REGISTRY_PATH;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function normalizeItem(raw, index) {
|
|
22
|
+
const install = raw.install || {};
|
|
23
|
+
return {
|
|
24
|
+
number: raw.number ?? raw.no ?? index + 1,
|
|
25
|
+
id: raw.id || '',
|
|
26
|
+
kind: raw.kind || raw.type || 'unknown',
|
|
27
|
+
name: raw.name || '',
|
|
28
|
+
category: raw.category || '',
|
|
29
|
+
description: raw.description || '',
|
|
30
|
+
intro: raw.intro || '',
|
|
31
|
+
version: raw.version || '',
|
|
32
|
+
status: raw.status || 'active',
|
|
33
|
+
author: raw.author || '',
|
|
34
|
+
tags: Array.isArray(raw.tags) ? raw.tags : [],
|
|
35
|
+
images: Array.isArray(raw.images) ? raw.images : [],
|
|
36
|
+
install: {
|
|
37
|
+
format: install.format || raw.install_format || 'zip',
|
|
38
|
+
url: install.url || raw.downloadUrl || raw.package_url || '',
|
|
39
|
+
entry: install.entry || raw.entry || ''
|
|
40
|
+
},
|
|
41
|
+
raw
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** 从 URL 或本地文件读取注册表 JSON */
|
|
46
|
+
export async function loadRegistry(opts = {}, cfg = loadConfig()) {
|
|
47
|
+
const source = resolveSource(opts, cfg);
|
|
48
|
+
let json;
|
|
49
|
+
|
|
50
|
+
if (/^https?:\/\//i.test(source)) {
|
|
51
|
+
const controller = new AbortController();
|
|
52
|
+
const timer = setTimeout(() => controller.abort(), 15000);
|
|
53
|
+
try {
|
|
54
|
+
const res = await fetch(source, {
|
|
55
|
+
signal: controller.signal,
|
|
56
|
+
headers: { 'User-Agent': USER_AGENT, Accept: 'application/json' }
|
|
57
|
+
});
|
|
58
|
+
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
59
|
+
json = await res.json();
|
|
60
|
+
} catch (err) {
|
|
61
|
+
throw new Error(
|
|
62
|
+
`无法读取目录数据源 ${source}\n ${err.message}\n 提示:站点尚未集成时,可用 --source ./fixtures/sample-registry.json 本地验证`
|
|
63
|
+
);
|
|
64
|
+
} finally {
|
|
65
|
+
clearTimeout(timer);
|
|
66
|
+
}
|
|
67
|
+
} else {
|
|
68
|
+
const file = path.resolve(process.cwd(), source);
|
|
69
|
+
if (!fs.existsSync(file)) throw new Error(`数据源文件不存在:${file}`);
|
|
70
|
+
json = JSON.parse(stripBom(fs.readFileSync(file, 'utf-8')));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const list = Array.isArray(json) ? json : json.items || json.plugins || [];
|
|
74
|
+
const items = list.map(normalizeItem);
|
|
75
|
+
return { source, items, schemaVersion: json.schemaVersion || '1.0', raw: json };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function filterItems(items, opts = {}) {
|
|
79
|
+
return items.filter((it) => {
|
|
80
|
+
if (opts.kind && it.kind !== opts.kind) return false;
|
|
81
|
+
if (opts.category && !it.category.toLowerCase().includes(String(opts.category).toLowerCase())) return false;
|
|
82
|
+
if (opts.status && it.status !== opts.status) return false;
|
|
83
|
+
if (opts.tag && !it.tags.map((t) => String(t).toLowerCase()).includes(String(opts.tag).toLowerCase())) return false;
|
|
84
|
+
return true;
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** 按编号或 id 查找 */
|
|
89
|
+
export function findByRef(items, ref) {
|
|
90
|
+
const r = String(ref).trim();
|
|
91
|
+
if (/^\d+$/.test(r)) return items.find((it) => String(it.number) === r) || null;
|
|
92
|
+
return items.find((it) => it.id === r || it.name === r) || null;
|
|
93
|
+
}
|