mioku 1.2.3 → 1.3.0
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/dist/cli/index.cjs +1 -1
- package/dist/cli/index.js +1 -1
- package/dist/{exec-BPj3FRdI.js → exec-B_5FzqAA.js} +2 -2
- package/dist/exec-B_5FzqAA.js.map +1 -0
- package/dist/{exec-CniJOLOF.cjs → exec-CQ79KQZ-.cjs} +2 -2
- package/dist/exec-CQ79KQZ-.cjs.map +1 -0
- package/dist/index.cjs +811 -29
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +252 -17
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +252 -17
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +838 -72
- package/dist/index.js.map +1 -1
- package/package.json +4 -3
- package/dist/exec-BPj3FRdI.js.map +0 -1
- package/dist/exec-CniJOLOF.cjs.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const require_exec = require('./exec-
|
|
1
|
+
const require_exec = require('./exec-CQ79KQZ-.cjs');
|
|
2
2
|
const node_fs = require_exec.__toESM(require("node:fs"));
|
|
3
3
|
const node_path = require_exec.__toESM(require("node:path"));
|
|
4
4
|
const node_url = require_exec.__toESM(require("node:url"));
|
|
@@ -23,6 +23,8 @@ const node_os = require_exec.__toESM(require("node:os"));
|
|
|
23
23
|
const node_child_process = require_exec.__toESM(require("node:child_process"));
|
|
24
24
|
const node_module = require_exec.__toESM(require("node:module"));
|
|
25
25
|
const jiti = require_exec.__toESM(require("jiti"));
|
|
26
|
+
const yaml = require_exec.__toESM(require("yaml"));
|
|
27
|
+
const node_fs_promises = require_exec.__toESM(require("node:fs/promises"));
|
|
26
28
|
|
|
27
29
|
//#region src/config.ts
|
|
28
30
|
/** 当前机器人根目录,包一层对象以便整体重定向 */
|
|
@@ -938,17 +940,17 @@ var AdapterContextImpl = class {
|
|
|
938
940
|
const senderBot = this.#bots.pick(senderId);
|
|
939
941
|
if (senderBot && senderBot.bot_id !== event.self_id) {
|
|
940
942
|
this.#logger.debug(`拦截来自其他已连接 bot 的消息: sender=${senderId} self=${event.self_id}`);
|
|
941
|
-
return;
|
|
943
|
+
return false;
|
|
942
944
|
}
|
|
943
945
|
}
|
|
944
946
|
}
|
|
945
947
|
if (event.kind === "message") {
|
|
946
948
|
const duplicate = this.#correlator?.observationOf(event)?.duplicate ?? false;
|
|
947
949
|
if (!duplicate) {
|
|
948
|
-
if (await this.#commands.dispatch(event)) return;
|
|
950
|
+
if (await this.#commands.dispatch(event)) return true;
|
|
949
951
|
}
|
|
950
952
|
}
|
|
951
|
-
|
|
953
|
+
return this.#bus.dispatch(event);
|
|
952
954
|
}
|
|
953
955
|
listen(route, handler) {
|
|
954
956
|
return this.#bus.register(route, handler, {
|
|
@@ -1119,10 +1121,16 @@ var EventBus = class {
|
|
|
1119
1121
|
}
|
|
1120
1122
|
return matched;
|
|
1121
1123
|
}
|
|
1122
|
-
/**
|
|
1124
|
+
/**
|
|
1125
|
+
* 按优先级顺序把事件交给监听器,返回是否有人认领。
|
|
1126
|
+
*
|
|
1127
|
+
* 每个优先级组内按注册顺序逐个执行。handler 返回 `true` 即结束派发;
|
|
1128
|
+
* 返回 `undefined` / `false` 则继续交给后续监听器,全部不认领时静默结束。
|
|
1129
|
+
* 单个监听器出错只记日志,不影响后续监听器。
|
|
1130
|
+
*/
|
|
1123
1131
|
async dispatch(event) {
|
|
1124
1132
|
const matched = this.#matching(event);
|
|
1125
|
-
if (matched.length === 0) return;
|
|
1133
|
+
if (matched.length === 0) return false;
|
|
1126
1134
|
this.#dispatched++;
|
|
1127
1135
|
const priorityGroups = new Map();
|
|
1128
1136
|
for (const reg of matched) {
|
|
@@ -1133,9 +1141,21 @@ var EventBus = class {
|
|
|
1133
1141
|
const sortedPriorities = Array.from(priorityGroups.entries()).sort(([a], [b]) => a - b);
|
|
1134
1142
|
for (const [, regs] of sortedPriorities) {
|
|
1135
1143
|
regs.sort((a, b) => a.order - b.order);
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1144
|
+
for (const reg of regs) {
|
|
1145
|
+
if (reg.disposed) continue;
|
|
1146
|
+
const claimed = await this.#invoke(reg, event);
|
|
1147
|
+
if (claimed) return true;
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
return false;
|
|
1151
|
+
}
|
|
1152
|
+
/** 执行单个监听器;返回它是否认领了这条事件,抛错按未认领处理 */
|
|
1153
|
+
async #invoke(reg, event) {
|
|
1154
|
+
try {
|
|
1155
|
+
return await reg.handler(event) === true;
|
|
1156
|
+
} catch (err) {
|
|
1157
|
+
this.#logger?.("error", `Event handler "${reg.source}" for route "${reg.route}" failed`, err);
|
|
1158
|
+
return false;
|
|
1139
1159
|
}
|
|
1140
1160
|
}
|
|
1141
1161
|
stats() {
|
|
@@ -2127,6 +2147,9 @@ var MiokuContext = class {
|
|
|
2127
2147
|
/**
|
|
2128
2148
|
* 注册事件监听。route 支持层级路由,如 `message`、`message.group`、
|
|
2129
2149
|
* `onebotv11:message`;返回取消监听函数。
|
|
2150
|
+
*
|
|
2151
|
+
* handler 返回 `true` 表示认领该事件,总线不再交给后续监听器;
|
|
2152
|
+
* 返回 `undefined` / `false` 则继续下传。
|
|
2130
2153
|
*/
|
|
2131
2154
|
handle(route, handler) {
|
|
2132
2155
|
const inputRoutes = Array.isArray(route) ? route : [route];
|
|
@@ -2136,10 +2159,10 @@ var MiokuContext = class {
|
|
|
2136
2159
|
const handledEvents = new WeakSet();
|
|
2137
2160
|
const correlator = bypassDedup ? void 0 : this.#options.correlator;
|
|
2138
2161
|
const wrappedHandler = async (event) => {
|
|
2139
|
-
if (handledEvents.has(event)) return;
|
|
2140
|
-
if (correlator?.isDuplicate(event)) return;
|
|
2162
|
+
if (handledEvents.has(event)) return false;
|
|
2163
|
+
if (correlator?.isDuplicate(event)) return false;
|
|
2141
2164
|
handledEvents.add(event);
|
|
2142
|
-
await handler(event);
|
|
2165
|
+
return await handler(event) === true;
|
|
2143
2166
|
};
|
|
2144
2167
|
const disposers = routes.map((r) => this.#options.bus.register(r, wrappedHandler, {
|
|
2145
2168
|
source,
|
|
@@ -2821,11 +2844,17 @@ var CommandManager = class {
|
|
|
2821
2844
|
async dispatch(event) {
|
|
2822
2845
|
const text$1 = String(event.message?.text?.() ?? "").trim();
|
|
2823
2846
|
if (!text$1) return false;
|
|
2824
|
-
const
|
|
2825
|
-
|
|
2826
|
-
|
|
2847
|
+
const matches = this.#findAll(text$1);
|
|
2848
|
+
for (const match$1 of matches) {
|
|
2849
|
+
if (!this.#canUse(match$1.command, event)) return true;
|
|
2850
|
+
if (await this.#run(match$1, event, text$1)) return true;
|
|
2851
|
+
}
|
|
2852
|
+
return false;
|
|
2853
|
+
}
|
|
2854
|
+
/** 执行单个命令,返回它是否认领这条消息 */
|
|
2855
|
+
async #run(match$1, event, text$1) {
|
|
2827
2856
|
try {
|
|
2828
|
-
await match$1.command.handler({
|
|
2857
|
+
const result = await match$1.command.handler({
|
|
2829
2858
|
ctx: match$1.command.context,
|
|
2830
2859
|
event,
|
|
2831
2860
|
command: match$1.command,
|
|
@@ -2834,10 +2863,11 @@ var CommandManager = class {
|
|
|
2834
2863
|
args: match$1.args,
|
|
2835
2864
|
match: match$1.match
|
|
2836
2865
|
});
|
|
2866
|
+
return result !== false;
|
|
2837
2867
|
} catch (error) {
|
|
2838
2868
|
this.#logger?.error(`Command "${match$1.command.plugin}:${match$1.command.id}" failed`, error);
|
|
2869
|
+
return true;
|
|
2839
2870
|
}
|
|
2840
|
-
return true;
|
|
2841
2871
|
}
|
|
2842
2872
|
shouldDispatch(source, event) {
|
|
2843
2873
|
if (!source.startsWith("plugin:")) return true;
|
|
@@ -2849,7 +2879,8 @@ var CommandManager = class {
|
|
|
2849
2879
|
const matches = (metadata.config.accessHooks ?? []).filter((hook) => event.kind === "message" && textHookMatches(text$1, hook.match) || routeMatches(event, hook.event));
|
|
2850
2880
|
return matches.every((hook) => this.#commandAllowed(plugin, hook.id, event));
|
|
2851
2881
|
}
|
|
2852
|
-
#
|
|
2882
|
+
#findAll(text$1) {
|
|
2883
|
+
const matches = [];
|
|
2853
2884
|
for (const command of this.#orderedCommands()) {
|
|
2854
2885
|
const prefixes = parsePrefixes(command.prefixes ?? this.#getDefaultPrefix(), this.#getDefaultPrefix());
|
|
2855
2886
|
const body = commandBody(text$1, prefixes);
|
|
@@ -2862,11 +2893,14 @@ var CommandManager = class {
|
|
|
2862
2893
|
];
|
|
2863
2894
|
for (const candidate of candidates) {
|
|
2864
2895
|
const result = stringMatch(body, candidate);
|
|
2865
|
-
if (result)
|
|
2866
|
-
|
|
2867
|
-
|
|
2868
|
-
|
|
2869
|
-
|
|
2896
|
+
if (result) {
|
|
2897
|
+
matches.push({
|
|
2898
|
+
command,
|
|
2899
|
+
body,
|
|
2900
|
+
args: result.args
|
|
2901
|
+
});
|
|
2902
|
+
break;
|
|
2903
|
+
}
|
|
2870
2904
|
}
|
|
2871
2905
|
continue;
|
|
2872
2906
|
}
|
|
@@ -2874,14 +2908,14 @@ var CommandManager = class {
|
|
|
2874
2908
|
const matched = body.match(command.matcher);
|
|
2875
2909
|
if (!matched) continue;
|
|
2876
2910
|
const args = matched.length > 1 ? matched.slice(1).filter((item) => item != null) : body.slice(matched[0].length).trim().split(/\s+/).filter(Boolean);
|
|
2877
|
-
|
|
2911
|
+
matches.push({
|
|
2878
2912
|
command,
|
|
2879
2913
|
body,
|
|
2880
2914
|
args,
|
|
2881
2915
|
match: matched
|
|
2882
|
-
};
|
|
2916
|
+
});
|
|
2883
2917
|
}
|
|
2884
|
-
return
|
|
2918
|
+
return matches;
|
|
2885
2919
|
}
|
|
2886
2920
|
#resolveRegistered(plugin, ref) {
|
|
2887
2921
|
const raw = String(ref ?? "").trim();
|
|
@@ -4968,7 +5002,7 @@ const core = definePlugin({
|
|
|
4968
5002
|
const collectBots = () => ctx.bots;
|
|
4969
5003
|
/** 已加载的适配器优先,再补上只在 bot 上出现过的适配器名 */
|
|
4970
5004
|
const collectAdapters = () => {
|
|
4971
|
-
const describe = (name, adapter) => ({
|
|
5005
|
+
const describe$1 = (name, adapter) => ({
|
|
4972
5006
|
name,
|
|
4973
5007
|
version: adapter?.version,
|
|
4974
5008
|
impl: adapter?.impl ? {
|
|
@@ -4976,13 +5010,13 @@ const core = definePlugin({
|
|
|
4976
5010
|
version: adapter.impl.version
|
|
4977
5011
|
} : void 0
|
|
4978
5012
|
});
|
|
4979
|
-
const list = ctx.adapters.map((adapter) => describe(adapter.name, adapter));
|
|
5013
|
+
const list = ctx.adapters.map((adapter) => describe$1(adapter.name, adapter));
|
|
4980
5014
|
const seen = new Set(list.map((adapter) => adapter.name));
|
|
4981
5015
|
for (const bot of ctx.bots) {
|
|
4982
5016
|
const name = String(bot.adapter);
|
|
4983
5017
|
if (seen.has(name)) continue;
|
|
4984
5018
|
seen.add(name);
|
|
4985
|
-
list.push(describe(name, ctx.getAdapter(name)));
|
|
5019
|
+
list.push(describe$1(name, ctx.getAdapter(name)));
|
|
4986
5020
|
}
|
|
4987
5021
|
return list;
|
|
4988
5022
|
};
|
|
@@ -6261,6 +6295,733 @@ const startRuntime = async (options = {}) => {
|
|
|
6261
6295
|
} };
|
|
6262
6296
|
};
|
|
6263
6297
|
|
|
6298
|
+
//#endregion
|
|
6299
|
+
//#region src/skills/types.ts
|
|
6300
|
+
const SKILL_FILE_NAME = "SKILL.md";
|
|
6301
|
+
/** 规范要求的技能名:小写字母数字,单连字符分隔 */
|
|
6302
|
+
const SKILL_NAME_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
6303
|
+
const SKILL_NAME_MAX = 64;
|
|
6304
|
+
const SKILL_DESCRIPTION_MAX = 1024;
|
|
6305
|
+
const SKILL_COMPATIBILITY_MAX = 500;
|
|
6306
|
+
/** 规范定义的六个字段加上两个调用控制字段 */
|
|
6307
|
+
const SKILL_KNOWN_KEYS = [
|
|
6308
|
+
"name",
|
|
6309
|
+
"description",
|
|
6310
|
+
"license",
|
|
6311
|
+
"compatibility",
|
|
6312
|
+
"metadata",
|
|
6313
|
+
"allowed-tools",
|
|
6314
|
+
"disable-model-invocation",
|
|
6315
|
+
"user-invocable"
|
|
6316
|
+
];
|
|
6317
|
+
|
|
6318
|
+
//#endregion
|
|
6319
|
+
//#region src/skills/frontmatter.ts
|
|
6320
|
+
const FRONTMATTER_FENCE = "---";
|
|
6321
|
+
function diagnostic(code, message, path$8, level = "warn") {
|
|
6322
|
+
return {
|
|
6323
|
+
level,
|
|
6324
|
+
code,
|
|
6325
|
+
message,
|
|
6326
|
+
path: path$8
|
|
6327
|
+
};
|
|
6328
|
+
}
|
|
6329
|
+
function normalizeText(raw) {
|
|
6330
|
+
return raw.replace(/^\uFEFF/, "").replace(/\r\n?/g, "\n");
|
|
6331
|
+
}
|
|
6332
|
+
/** 拆出 `---` 围栏之间的 YAML 与正文 */
|
|
6333
|
+
function splitFrontmatter(text$1) {
|
|
6334
|
+
const lines = text$1.split("\n");
|
|
6335
|
+
if ((lines[0] ?? "").trimEnd() !== FRONTMATTER_FENCE) return void 0;
|
|
6336
|
+
for (let i = 1; i < lines.length; i++) {
|
|
6337
|
+
if (lines[i].trimEnd() !== FRONTMATTER_FENCE) continue;
|
|
6338
|
+
return {
|
|
6339
|
+
yaml: lines.slice(1, i).join("\n"),
|
|
6340
|
+
body: lines.slice(i + 1).join("\n")
|
|
6341
|
+
};
|
|
6342
|
+
}
|
|
6343
|
+
return void 0;
|
|
6344
|
+
}
|
|
6345
|
+
function stringField(source, key) {
|
|
6346
|
+
const value = source[key];
|
|
6347
|
+
return typeof value === "string" ? value.trim() : "";
|
|
6348
|
+
}
|
|
6349
|
+
function booleanField(value) {
|
|
6350
|
+
if (typeof value === "boolean") return value;
|
|
6351
|
+
if (value === 1 || value === "1") return true;
|
|
6352
|
+
if (value === 0 || value === "0") return false;
|
|
6353
|
+
if (typeof value === "string") {
|
|
6354
|
+
const lowered = value.trim().toLowerCase();
|
|
6355
|
+
if (lowered === "true" || lowered === "yes" || lowered === "on") return true;
|
|
6356
|
+
if (lowered === "false" || lowered === "no" || lowered === "off") return false;
|
|
6357
|
+
}
|
|
6358
|
+
return void 0;
|
|
6359
|
+
}
|
|
6360
|
+
/** 规范允许空格分隔的字符串或 YAML 列表 */
|
|
6361
|
+
function readAllowedTools(value) {
|
|
6362
|
+
const parts = Array.isArray(value) ? value.map((item) => String(item ?? "")) : typeof value === "string" ? value.split(/[\s,]+/) : [];
|
|
6363
|
+
return parts.map((part) => part.trim()).filter(Boolean);
|
|
6364
|
+
}
|
|
6365
|
+
function readMetadata(value) {
|
|
6366
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return void 0;
|
|
6367
|
+
const result = {};
|
|
6368
|
+
for (const [key, item] of Object.entries(value)) {
|
|
6369
|
+
if (item === null || item === void 0) continue;
|
|
6370
|
+
result[key] = typeof item === "string" ? item : JSON.stringify(item) ?? String(item);
|
|
6371
|
+
}
|
|
6372
|
+
return Object.keys(result).length > 0 ? result : void 0;
|
|
6373
|
+
}
|
|
6374
|
+
function clamp$1(value, max, label, path$8, diagnostics) {
|
|
6375
|
+
if (value.length <= max) return value;
|
|
6376
|
+
diagnostics.push(diagnostic("UNKNOWN_KEY", `frontmatter ${label} 超过 ${max} 字符,已截断`, path$8));
|
|
6377
|
+
return value.slice(0, max);
|
|
6378
|
+
}
|
|
6379
|
+
/**
|
|
6380
|
+
* 解析并校验一个 `SKILL.md`。
|
|
6381
|
+
* 缺失 `skill` 表示该文件不可用:缺少 frontmatter、YAML 非法、name/description 缺失或 name 非法。
|
|
6382
|
+
*/
|
|
6383
|
+
function parseSkillFile(path$8, raw) {
|
|
6384
|
+
const diagnostics = [];
|
|
6385
|
+
const text$1 = normalizeText(raw);
|
|
6386
|
+
const split = splitFrontmatter(text$1);
|
|
6387
|
+
if (!split) {
|
|
6388
|
+
diagnostics.push(diagnostic("MISSING_FRONTMATTER", "缺少 YAML frontmatter,已忽略", path$8, "error"));
|
|
6389
|
+
return { diagnostics };
|
|
6390
|
+
}
|
|
6391
|
+
let data;
|
|
6392
|
+
try {
|
|
6393
|
+
const parsed = (0, yaml.parse)(split.yaml);
|
|
6394
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
6395
|
+
diagnostics.push(diagnostic("INVALID_YAML", "frontmatter 不是键值映射,已忽略", path$8, "error"));
|
|
6396
|
+
return { diagnostics };
|
|
6397
|
+
}
|
|
6398
|
+
data = parsed;
|
|
6399
|
+
} catch (err) {
|
|
6400
|
+
diagnostics.push(diagnostic("INVALID_YAML", `frontmatter YAML 解析失败:${err}`, path$8, "error"));
|
|
6401
|
+
return { diagnostics };
|
|
6402
|
+
}
|
|
6403
|
+
for (const key of Object.keys(data)) {
|
|
6404
|
+
if (SKILL_KNOWN_KEYS.includes(key)) continue;
|
|
6405
|
+
diagnostics.push(diagnostic("UNKNOWN_KEY", `未知 frontmatter 字段 "${key}",已忽略`, path$8));
|
|
6406
|
+
}
|
|
6407
|
+
const name = stringField(data, "name");
|
|
6408
|
+
if (!name) {
|
|
6409
|
+
diagnostics.push(diagnostic("MISSING_FIELD", "frontmatter 缺少 name,已忽略", path$8, "error"));
|
|
6410
|
+
return { diagnostics };
|
|
6411
|
+
}
|
|
6412
|
+
if (name.length > SKILL_NAME_MAX || !SKILL_NAME_PATTERN.test(name)) {
|
|
6413
|
+
diagnostics.push(diagnostic("INVALID_NAME", `技能名 "${name}" 不合法(仅小写字母、数字与单个连字符,最长 ${SKILL_NAME_MAX}),已忽略`, path$8, "error"));
|
|
6414
|
+
return { diagnostics };
|
|
6415
|
+
}
|
|
6416
|
+
const rawDescription = stringField(data, "description");
|
|
6417
|
+
if (!rawDescription) {
|
|
6418
|
+
diagnostics.push(diagnostic("MISSING_FIELD", "frontmatter 缺少 description,已忽略", path$8, "error"));
|
|
6419
|
+
return { diagnostics };
|
|
6420
|
+
}
|
|
6421
|
+
const disableModelInvocation = booleanField(data["disable-model-invocation"]) ?? false;
|
|
6422
|
+
const userInvocable = booleanField(data["user-invocable"]) ?? true;
|
|
6423
|
+
const invoke = {
|
|
6424
|
+
modelInvocable: !disableModelInvocation,
|
|
6425
|
+
userInvocable
|
|
6426
|
+
};
|
|
6427
|
+
if (!invoke.modelInvocable && !invoke.userInvocable) diagnostics.push(diagnostic("UNUSABLE_INVOCATION", "disable-model-invocation 与 user-invocable:false 同时设置,技能不可被任何方式调用", path$8));
|
|
6428
|
+
const license = stringField(data, "license");
|
|
6429
|
+
const compatibility = stringField(data, "compatibility");
|
|
6430
|
+
return {
|
|
6431
|
+
skill: {
|
|
6432
|
+
name,
|
|
6433
|
+
description: clamp$1(rawDescription, SKILL_DESCRIPTION_MAX, "description", path$8, diagnostics),
|
|
6434
|
+
...license ? { license } : {},
|
|
6435
|
+
...compatibility ? { compatibility: clamp$1(compatibility, SKILL_COMPATIBILITY_MAX, "compatibility", path$8, diagnostics) } : {},
|
|
6436
|
+
...(() => {
|
|
6437
|
+
const metadata = readMetadata(data.metadata);
|
|
6438
|
+
return metadata ? { metadata } : {};
|
|
6439
|
+
})(),
|
|
6440
|
+
allowedTools: readAllowedTools(data["allowed-tools"]),
|
|
6441
|
+
invoke,
|
|
6442
|
+
content: split.body.trim()
|
|
6443
|
+
},
|
|
6444
|
+
diagnostics
|
|
6445
|
+
};
|
|
6446
|
+
}
|
|
6447
|
+
|
|
6448
|
+
//#endregion
|
|
6449
|
+
//#region src/skills/roots.ts
|
|
6450
|
+
/** 五个标准技能根,按 rank 升序;与 Claude Code / Codex / `npx skills add` 的目录约定一致 */
|
|
6451
|
+
const DEFAULT_SKILL_ROOT_SPECS = [
|
|
6452
|
+
{
|
|
6453
|
+
id: "project-agents",
|
|
6454
|
+
label: ".agents/skills",
|
|
6455
|
+
kind: "project-agents",
|
|
6456
|
+
scope: "project",
|
|
6457
|
+
segments: [".agents", "skills"],
|
|
6458
|
+
rank: 100
|
|
6459
|
+
},
|
|
6460
|
+
{
|
|
6461
|
+
id: "project-claude",
|
|
6462
|
+
label: ".claude/skills",
|
|
6463
|
+
kind: "project-claude",
|
|
6464
|
+
scope: "project",
|
|
6465
|
+
segments: [".claude", "skills"],
|
|
6466
|
+
rank: 200
|
|
6467
|
+
},
|
|
6468
|
+
{
|
|
6469
|
+
id: "user-agents",
|
|
6470
|
+
label: "~/.agents/skills",
|
|
6471
|
+
kind: "user-agents",
|
|
6472
|
+
scope: "user",
|
|
6473
|
+
segments: [".agents", "skills"],
|
|
6474
|
+
rank: 400
|
|
6475
|
+
},
|
|
6476
|
+
{
|
|
6477
|
+
id: "user-claude",
|
|
6478
|
+
label: "~/.claude/skills",
|
|
6479
|
+
kind: "user-claude",
|
|
6480
|
+
scope: "user",
|
|
6481
|
+
segments: [".claude", "skills"],
|
|
6482
|
+
rank: 500
|
|
6483
|
+
},
|
|
6484
|
+
{
|
|
6485
|
+
id: "user-codex",
|
|
6486
|
+
label: "~/.codex/skills",
|
|
6487
|
+
kind: "user-codex",
|
|
6488
|
+
scope: "user",
|
|
6489
|
+
segments: [".codex", "skills"],
|
|
6490
|
+
rank: 600
|
|
6491
|
+
}
|
|
6492
|
+
];
|
|
6493
|
+
const CUSTOM_RANK = 300;
|
|
6494
|
+
/** 展开开头的 `~/` */
|
|
6495
|
+
function expandHomePath(input, home = (0, node_os.homedir)()) {
|
|
6496
|
+
const raw = String(input ?? "").trim();
|
|
6497
|
+
if (raw === "~") return home;
|
|
6498
|
+
if (raw.startsWith("~/") || raw.startsWith("~\\")) return node_path.join(home, raw.slice(2));
|
|
6499
|
+
return raw;
|
|
6500
|
+
}
|
|
6501
|
+
function displayPath(target, home) {
|
|
6502
|
+
const rel = node_path.relative(home, target);
|
|
6503
|
+
if (rel && !rel.startsWith("..") && !node_path.isAbsolute(rel)) return `~/${rel.split(node_path.sep).join("/")}`;
|
|
6504
|
+
return target;
|
|
6505
|
+
}
|
|
6506
|
+
/** 解析出当前机器上的技能根列表,自定义根排在项目根之后、用户根之前 */
|
|
6507
|
+
function resolveSkillRoots(options = {}) {
|
|
6508
|
+
const botRoot = node_path.resolve(options.botRoot ?? process.cwd());
|
|
6509
|
+
const home = node_path.resolve(options.home ?? (0, node_os.homedir)());
|
|
6510
|
+
const roots = [];
|
|
6511
|
+
for (const spec of DEFAULT_SKILL_ROOT_SPECS) {
|
|
6512
|
+
const base = spec.scope === "project" ? botRoot : home;
|
|
6513
|
+
roots.push({
|
|
6514
|
+
id: spec.id,
|
|
6515
|
+
label: spec.label,
|
|
6516
|
+
kind: spec.kind,
|
|
6517
|
+
path: node_path.join(base, ...spec.segments),
|
|
6518
|
+
rank: spec.rank,
|
|
6519
|
+
writable: true
|
|
6520
|
+
});
|
|
6521
|
+
}
|
|
6522
|
+
const extra = (options.extraRoots ?? []).map((item) => expandHomePath(item, home)).filter(Boolean).map((item) => node_path.resolve(item));
|
|
6523
|
+
extra.forEach((target, index) => {
|
|
6524
|
+
roots.push({
|
|
6525
|
+
id: `custom:${index + 1}`,
|
|
6526
|
+
label: displayPath(target, home),
|
|
6527
|
+
kind: "custom",
|
|
6528
|
+
path: target,
|
|
6529
|
+
rank: CUSTOM_RANK,
|
|
6530
|
+
writable: true
|
|
6531
|
+
});
|
|
6532
|
+
});
|
|
6533
|
+
return roots.sort((a, b) => a.rank - b.rank);
|
|
6534
|
+
}
|
|
6535
|
+
function findSkillRoot(roots, id) {
|
|
6536
|
+
return roots.find((root) => root.id === id);
|
|
6537
|
+
}
|
|
6538
|
+
/** 判断路径是否落在某个根内,返回该根 */
|
|
6539
|
+
function rootOfPath(roots, target) {
|
|
6540
|
+
const resolved = node_path.resolve(target);
|
|
6541
|
+
return roots.find((root) => {
|
|
6542
|
+
const rel = node_path.relative(root.path, resolved);
|
|
6543
|
+
return rel !== "" && !rel.startsWith("..") && !node_path.isAbsolute(rel);
|
|
6544
|
+
});
|
|
6545
|
+
}
|
|
6546
|
+
|
|
6547
|
+
//#endregion
|
|
6548
|
+
//#region src/skills/discover.ts
|
|
6549
|
+
const DEFAULT_MAX_RESOURCE_FILES = 200;
|
|
6550
|
+
const DEFAULT_MAX_RESOURCE_DEPTH = 3;
|
|
6551
|
+
const MAX_SKILL_FILE_BYTES = 256 * 1024;
|
|
6552
|
+
const SKIPPED_DIRS$1 = new Set([".git", "node_modules"]);
|
|
6553
|
+
function toPosix(value) {
|
|
6554
|
+
return value.split(node_path.sep).join("/");
|
|
6555
|
+
}
|
|
6556
|
+
function shouldSkipName(name) {
|
|
6557
|
+
return name.startsWith(".") || SKIPPED_DIRS$1.has(name);
|
|
6558
|
+
}
|
|
6559
|
+
async function safeRealpath(target) {
|
|
6560
|
+
try {
|
|
6561
|
+
return await node_fs_promises.realpath(target);
|
|
6562
|
+
} catch {
|
|
6563
|
+
return void 0;
|
|
6564
|
+
}
|
|
6565
|
+
}
|
|
6566
|
+
/** 软链接要跟随:`npx skills add` 与 `.claude/skills -> ../.agents/skills` 都是常见布局 */
|
|
6567
|
+
async function entryKind(full, entry) {
|
|
6568
|
+
if (entry.isDirectory()) return "dir";
|
|
6569
|
+
if (entry.isFile()) return "file";
|
|
6570
|
+
if (!entry.isSymbolicLink()) return "none";
|
|
6571
|
+
try {
|
|
6572
|
+
const stat = await node_fs_promises.stat(full);
|
|
6573
|
+
if (stat.isDirectory()) return "dir";
|
|
6574
|
+
if (stat.isFile()) return "file";
|
|
6575
|
+
} catch {}
|
|
6576
|
+
return "none";
|
|
6577
|
+
}
|
|
6578
|
+
/** 技能目录内的附加文件,深度受限、跳过隐藏与依赖目录 */
|
|
6579
|
+
async function listResources(dir, maxFiles, maxDepth) {
|
|
6580
|
+
const found = [];
|
|
6581
|
+
const queue = [{
|
|
6582
|
+
dir,
|
|
6583
|
+
depth: 0
|
|
6584
|
+
}];
|
|
6585
|
+
while (queue.length > 0 && found.length < maxFiles) {
|
|
6586
|
+
const current = queue.shift();
|
|
6587
|
+
let entries;
|
|
6588
|
+
try {
|
|
6589
|
+
entries = await node_fs_promises.readdir(current.dir, { withFileTypes: true });
|
|
6590
|
+
} catch {
|
|
6591
|
+
continue;
|
|
6592
|
+
}
|
|
6593
|
+
for (const entry of entries) {
|
|
6594
|
+
if (shouldSkipName(entry.name)) continue;
|
|
6595
|
+
const full = node_path.join(current.dir, entry.name);
|
|
6596
|
+
if (entry.isDirectory()) {
|
|
6597
|
+
if (current.depth + 1 <= maxDepth) queue.push({
|
|
6598
|
+
dir: full,
|
|
6599
|
+
depth: current.depth + 1
|
|
6600
|
+
});
|
|
6601
|
+
continue;
|
|
6602
|
+
}
|
|
6603
|
+
if (!entry.isFile()) continue;
|
|
6604
|
+
if (entry.name === SKILL_FILE_NAME) continue;
|
|
6605
|
+
found.push(toPosix(node_path.relative(dir, full)));
|
|
6606
|
+
if (found.length >= maxFiles) break;
|
|
6607
|
+
}
|
|
6608
|
+
}
|
|
6609
|
+
return found.sort();
|
|
6610
|
+
}
|
|
6611
|
+
/** 枚举一个根下的技能候选:`<name>/SKILL.md` 与 `<name>.md` 两种形态 */
|
|
6612
|
+
async function listSkillEntries(root) {
|
|
6613
|
+
let entries;
|
|
6614
|
+
try {
|
|
6615
|
+
entries = await node_fs_promises.readdir(root.path, { withFileTypes: true });
|
|
6616
|
+
} catch {
|
|
6617
|
+
return [];
|
|
6618
|
+
}
|
|
6619
|
+
const found = [];
|
|
6620
|
+
for (const entry of entries) {
|
|
6621
|
+
if (shouldSkipName(entry.name)) continue;
|
|
6622
|
+
const full = node_path.join(root.path, entry.name);
|
|
6623
|
+
const kind = await entryKind(full, entry);
|
|
6624
|
+
if (kind === "dir") {
|
|
6625
|
+
found.push({
|
|
6626
|
+
entryName: entry.name,
|
|
6627
|
+
skillDir: full,
|
|
6628
|
+
skillPath: node_path.join(full, SKILL_FILE_NAME)
|
|
6629
|
+
});
|
|
6630
|
+
continue;
|
|
6631
|
+
}
|
|
6632
|
+
if (kind === "file" && entry.name.toLowerCase().endsWith(".md")) found.push({
|
|
6633
|
+
entryName: entry.name,
|
|
6634
|
+
skillDir: root.path,
|
|
6635
|
+
skillPath: full
|
|
6636
|
+
});
|
|
6637
|
+
}
|
|
6638
|
+
return found;
|
|
6639
|
+
}
|
|
6640
|
+
/** 读盘并解析一个技能候选;缺文件、软链接断链、超大都返回空结果 */
|
|
6641
|
+
async function readSkillEntry(root, entry, options = {}) {
|
|
6642
|
+
const maxResourceFiles = options.maxResourceFiles ?? DEFAULT_MAX_RESOURCE_FILES;
|
|
6643
|
+
const maxResourceDepth = options.maxResourceDepth ?? DEFAULT_MAX_RESOURCE_DEPTH;
|
|
6644
|
+
const realDir = await safeRealpath(entry.skillDir);
|
|
6645
|
+
if (!realDir) return {
|
|
6646
|
+
skills: [],
|
|
6647
|
+
diagnostics: []
|
|
6648
|
+
};
|
|
6649
|
+
let raw;
|
|
6650
|
+
try {
|
|
6651
|
+
raw = await node_fs_promises.readFile(entry.skillPath, "utf-8");
|
|
6652
|
+
} catch {
|
|
6653
|
+
return {
|
|
6654
|
+
skills: [],
|
|
6655
|
+
diagnostics: []
|
|
6656
|
+
};
|
|
6657
|
+
}
|
|
6658
|
+
if (Buffer.byteLength(raw, "utf-8") > MAX_SKILL_FILE_BYTES) return {
|
|
6659
|
+
skills: [],
|
|
6660
|
+
diagnostics: [{
|
|
6661
|
+
level: "error",
|
|
6662
|
+
code: "READ_FAILED",
|
|
6663
|
+
message: `${SKILL_FILE_NAME} 超过 256KB,已忽略`,
|
|
6664
|
+
path: entry.skillPath
|
|
6665
|
+
}]
|
|
6666
|
+
};
|
|
6667
|
+
const parsed = parseSkillFile(entry.skillPath, raw);
|
|
6668
|
+
const diagnostics = [...parsed.diagnostics];
|
|
6669
|
+
if (!parsed.skill) return {
|
|
6670
|
+
skills: [],
|
|
6671
|
+
diagnostics
|
|
6672
|
+
};
|
|
6673
|
+
const expected = entry.entryName.replace(/\.md$/i, "");
|
|
6674
|
+
if (entry.skillDir !== root.path && parsed.skill.name !== expected) diagnostics.push({
|
|
6675
|
+
level: "warn",
|
|
6676
|
+
code: "NAME_MISMATCH",
|
|
6677
|
+
message: `frontmatter name "${parsed.skill.name}" 与目录名 "${expected}" 不一致`,
|
|
6678
|
+
path: entry.skillPath
|
|
6679
|
+
});
|
|
6680
|
+
const resources = entry.skillDir === root.path ? [] : await listResources(realDir, maxResourceFiles, maxResourceDepth);
|
|
6681
|
+
return {
|
|
6682
|
+
skills: [{
|
|
6683
|
+
name: parsed.skill.name,
|
|
6684
|
+
description: parsed.skill.description,
|
|
6685
|
+
invoke: parsed.skill.invoke,
|
|
6686
|
+
rootId: root.id,
|
|
6687
|
+
dir: realDir,
|
|
6688
|
+
path: entry.skillPath,
|
|
6689
|
+
resources
|
|
6690
|
+
}],
|
|
6691
|
+
diagnostics
|
|
6692
|
+
};
|
|
6693
|
+
}
|
|
6694
|
+
/**
|
|
6695
|
+
* 按 rank 合并各组结果;重名时先出现的(rank 小的)获胜,后来者记一条 warn。
|
|
6696
|
+
* `groups` 必须已按 rank 升序排列。
|
|
6697
|
+
*/
|
|
6698
|
+
function mergeSkillGroups(groups, diagnostics) {
|
|
6699
|
+
const winners = new Map();
|
|
6700
|
+
for (const group of groups) for (const summary of group) {
|
|
6701
|
+
const existing = winners.get(summary.name);
|
|
6702
|
+
if (existing) {
|
|
6703
|
+
diagnostics.push({
|
|
6704
|
+
level: "warn",
|
|
6705
|
+
code: "NAME_DUPLICATE",
|
|
6706
|
+
message: `技能名 "${summary.name}" 已被 ${existing.rootId} 占用,${summary.rootId} 的这份被忽略`,
|
|
6707
|
+
path: summary.path
|
|
6708
|
+
});
|
|
6709
|
+
continue;
|
|
6710
|
+
}
|
|
6711
|
+
winners.set(summary.name, summary);
|
|
6712
|
+
}
|
|
6713
|
+
return [...winners.values()].sort((a, b) => a.name.localeCompare(b.name));
|
|
6714
|
+
}
|
|
6715
|
+
/** 扫描一个技能根(无缓存) */
|
|
6716
|
+
async function discoverRoot(root, options = {}) {
|
|
6717
|
+
const skills = [];
|
|
6718
|
+
const diagnostics = [];
|
|
6719
|
+
for (const entry of await listSkillEntries(root)) {
|
|
6720
|
+
const result = await readSkillEntry(root, entry, options);
|
|
6721
|
+
skills.push(...result.skills);
|
|
6722
|
+
diagnostics.push(...result.diagnostics);
|
|
6723
|
+
}
|
|
6724
|
+
return {
|
|
6725
|
+
skills,
|
|
6726
|
+
diagnostics
|
|
6727
|
+
};
|
|
6728
|
+
}
|
|
6729
|
+
/** 扫描全部根(无缓存);技能重名时 rank 小的先赢 */
|
|
6730
|
+
async function discoverSkills(roots, options = {}) {
|
|
6731
|
+
const diagnostics = [];
|
|
6732
|
+
const groups = [];
|
|
6733
|
+
for (const root of [...roots].sort((a, b) => a.rank - b.rank)) {
|
|
6734
|
+
const result = await discoverRoot(root, options);
|
|
6735
|
+
diagnostics.push(...result.diagnostics);
|
|
6736
|
+
groups.push(result.skills);
|
|
6737
|
+
}
|
|
6738
|
+
return {
|
|
6739
|
+
skills: mergeSkillGroups(groups, diagnostics),
|
|
6740
|
+
diagnostics
|
|
6741
|
+
};
|
|
6742
|
+
}
|
|
6743
|
+
|
|
6744
|
+
//#endregion
|
|
6745
|
+
//#region src/skills/catalog.ts
|
|
6746
|
+
const DEFAULT_RESCAN_INTERVAL_MS = 2e3;
|
|
6747
|
+
async function statKey(target) {
|
|
6748
|
+
try {
|
|
6749
|
+
const stat = await node_fs_promises.stat(target);
|
|
6750
|
+
return `${stat.mtimeMs}:${stat.size}`;
|
|
6751
|
+
} catch {
|
|
6752
|
+
return "missing";
|
|
6753
|
+
}
|
|
6754
|
+
}
|
|
6755
|
+
/** 技能目录:扫描五个标准根,合并去重,按需读正文 */
|
|
6756
|
+
function createSkillCatalog(options = {}) {
|
|
6757
|
+
const roots = [...options.roots ?? resolveSkillRoots(options)].sort((a, b) => a.rank - b.rank);
|
|
6758
|
+
const ttl = options.rescanIntervalMs ?? DEFAULT_RESCAN_INTERVAL_MS;
|
|
6759
|
+
const discoverOptions = { maxResourceFiles: options.maxResourceFiles };
|
|
6760
|
+
const logger = options.logger;
|
|
6761
|
+
const entries = new Map();
|
|
6762
|
+
let state;
|
|
6763
|
+
async function scanRoot(root, found) {
|
|
6764
|
+
const group = [];
|
|
6765
|
+
for (const entry of found) {
|
|
6766
|
+
const key = await entryStamp(entry);
|
|
6767
|
+
const cached = entries.get(entry.skillPath);
|
|
6768
|
+
if (cached && cached.key === key) {
|
|
6769
|
+
group.push(...cached.skills);
|
|
6770
|
+
continue;
|
|
6771
|
+
}
|
|
6772
|
+
const result = await readSkillEntry(root, entry, discoverOptions);
|
|
6773
|
+
entries.set(entry.skillPath, {
|
|
6774
|
+
key,
|
|
6775
|
+
skills: result.skills,
|
|
6776
|
+
diagnostics: result.diagnostics
|
|
6777
|
+
});
|
|
6778
|
+
group.push(...result.skills);
|
|
6779
|
+
}
|
|
6780
|
+
return group;
|
|
6781
|
+
}
|
|
6782
|
+
async function entryStamp(entry) {
|
|
6783
|
+
const [file, dir] = await Promise.all([statKey(entry.skillPath), statKey(entry.skillDir)]);
|
|
6784
|
+
return `${file}|${dir}`;
|
|
6785
|
+
}
|
|
6786
|
+
function errorSignature(diagnostics) {
|
|
6787
|
+
return diagnostics.filter((item) => item.level === "error").map((item) => `${item.path}:${item.message}`).join("\n");
|
|
6788
|
+
}
|
|
6789
|
+
async function scan() {
|
|
6790
|
+
const diagnostics = [];
|
|
6791
|
+
const seen = new Set();
|
|
6792
|
+
const groups = [];
|
|
6793
|
+
for (const root of roots) {
|
|
6794
|
+
const found = await listSkillEntries(root);
|
|
6795
|
+
for (const entry of found) seen.add(entry.skillPath);
|
|
6796
|
+
groups.push(await scanRoot(root, found));
|
|
6797
|
+
}
|
|
6798
|
+
for (const path$8 of [...entries.keys()]) if (!seen.has(path$8)) entries.delete(path$8);
|
|
6799
|
+
for (const cached of entries.values()) diagnostics.push(...cached.diagnostics);
|
|
6800
|
+
return {
|
|
6801
|
+
at: Date.now(),
|
|
6802
|
+
skills: mergeSkillGroups(groups, diagnostics),
|
|
6803
|
+
diagnostics,
|
|
6804
|
+
errorSignature: errorSignature(diagnostics)
|
|
6805
|
+
};
|
|
6806
|
+
}
|
|
6807
|
+
async function snapshot(opts = {}) {
|
|
6808
|
+
if (!opts.force && state && Date.now() - state.at < ttl) return state.skills;
|
|
6809
|
+
const next = await scan();
|
|
6810
|
+
if (next.errorSignature && next.errorSignature !== state?.errorSignature) {
|
|
6811
|
+
const failed = next.diagnostics.filter((item) => item.level === "error");
|
|
6812
|
+
logger?.warn(`技能扫描有 ${failed.length} 个文件不可用:${failed[0]?.message ?? ""}`);
|
|
6813
|
+
}
|
|
6814
|
+
state = next;
|
|
6815
|
+
return state.skills;
|
|
6816
|
+
}
|
|
6817
|
+
return {
|
|
6818
|
+
roots: () => roots,
|
|
6819
|
+
snapshot,
|
|
6820
|
+
async load(name) {
|
|
6821
|
+
const skills = await snapshot();
|
|
6822
|
+
const summary = skills.find((item) => item.name === name);
|
|
6823
|
+
if (!summary) return void 0;
|
|
6824
|
+
let raw;
|
|
6825
|
+
try {
|
|
6826
|
+
raw = await node_fs_promises.readFile(summary.path, "utf-8");
|
|
6827
|
+
} catch {
|
|
6828
|
+
state = void 0;
|
|
6829
|
+
return void 0;
|
|
6830
|
+
}
|
|
6831
|
+
const parsed = parseSkillFile(summary.path, raw);
|
|
6832
|
+
if (!parsed.skill || parsed.skill.name !== name) {
|
|
6833
|
+
state = void 0;
|
|
6834
|
+
entries.delete(summary.path);
|
|
6835
|
+
return void 0;
|
|
6836
|
+
}
|
|
6837
|
+
return {
|
|
6838
|
+
...summary,
|
|
6839
|
+
name: parsed.skill.name,
|
|
6840
|
+
description: parsed.skill.description,
|
|
6841
|
+
invoke: parsed.skill.invoke,
|
|
6842
|
+
content: parsed.skill.content,
|
|
6843
|
+
allowedTools: parsed.skill.allowedTools,
|
|
6844
|
+
...parsed.skill.license ? { license: parsed.skill.license } : {},
|
|
6845
|
+
...parsed.skill.compatibility ? { compatibility: parsed.skill.compatibility } : {},
|
|
6846
|
+
...parsed.skill.metadata ? { metadata: parsed.skill.metadata } : {}
|
|
6847
|
+
};
|
|
6848
|
+
},
|
|
6849
|
+
diagnostics: () => state?.diagnostics ?? [],
|
|
6850
|
+
invalidate() {
|
|
6851
|
+
entries.clear();
|
|
6852
|
+
state = void 0;
|
|
6853
|
+
}
|
|
6854
|
+
};
|
|
6855
|
+
}
|
|
6856
|
+
|
|
6857
|
+
//#endregion
|
|
6858
|
+
//#region src/skills/render.ts
|
|
6859
|
+
const DEFAULT_DESCRIPTION_MAX = 500;
|
|
6860
|
+
function escapeText(value) {
|
|
6861
|
+
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
6862
|
+
}
|
|
6863
|
+
/** 目录行:`- \`name\`: description` */
|
|
6864
|
+
function renderSkillCatalog(skills, options = {}) {
|
|
6865
|
+
const max = options.maxDescriptionLength ?? DEFAULT_DESCRIPTION_MAX;
|
|
6866
|
+
return skills.map((skill) => {
|
|
6867
|
+
const description = skill.description.length > max ? `${skill.description.slice(0, max - 1)}…` : skill.description;
|
|
6868
|
+
return `- \`${skill.name}\`: ${escapeText(description)}`;
|
|
6869
|
+
}).join("\n");
|
|
6870
|
+
}
|
|
6871
|
+
/** 加载结果:伪 XML 包裹的资源清单与正文,模型语料里常见的形状 */
|
|
6872
|
+
function renderSkillContent(skill) {
|
|
6873
|
+
const lines = [`技能目录:${skill.dir}`];
|
|
6874
|
+
if (skill.resources.length > 0) {
|
|
6875
|
+
lines.push("附带文件(相对该目录,用 read 读取、用 bash 执行):");
|
|
6876
|
+
for (const resource of skill.resources) lines.push(`- ${resource}`);
|
|
6877
|
+
} else lines.push("该技能没有附带文件。");
|
|
6878
|
+
if (skill.compatibility) lines.push(`环境要求:${skill.compatibility}`);
|
|
6879
|
+
if (skill.allowedTools.length > 0) lines.push(`该技能声明预授权工具:${skill.allowedTools.join(" ")}`);
|
|
6880
|
+
return [
|
|
6881
|
+
`<skill_content name="${skill.name}">`,
|
|
6882
|
+
"<skill_resources>",
|
|
6883
|
+
...lines.map(escapeText),
|
|
6884
|
+
"</skill_resources>",
|
|
6885
|
+
"",
|
|
6886
|
+
"<skill_instructions>",
|
|
6887
|
+
skill.content,
|
|
6888
|
+
"</skill_instructions>",
|
|
6889
|
+
"</skill_content>"
|
|
6890
|
+
].join("\n");
|
|
6891
|
+
}
|
|
6892
|
+
|
|
6893
|
+
//#endregion
|
|
6894
|
+
//#region src/skills/locate.ts
|
|
6895
|
+
const DEFAULT_MAX_DEPTH = 3;
|
|
6896
|
+
const DEFAULT_MAX_CANDIDATES = 50;
|
|
6897
|
+
const FILE_COUNT_CAP = 500;
|
|
6898
|
+
const SKIPPED_DIRS = new Set([".git", "node_modules"]);
|
|
6899
|
+
function shouldSkip(name) {
|
|
6900
|
+
return name.startsWith(".") || SKIPPED_DIRS.has(name);
|
|
6901
|
+
}
|
|
6902
|
+
async function isFile(target) {
|
|
6903
|
+
try {
|
|
6904
|
+
return (await node_fs_promises.stat(target)).isFile();
|
|
6905
|
+
} catch {
|
|
6906
|
+
return false;
|
|
6907
|
+
}
|
|
6908
|
+
}
|
|
6909
|
+
async function countFiles(dir) {
|
|
6910
|
+
let total = 0;
|
|
6911
|
+
const queue = [{
|
|
6912
|
+
dir,
|
|
6913
|
+
depth: 0
|
|
6914
|
+
}];
|
|
6915
|
+
while (queue.length > 0 && total < FILE_COUNT_CAP) {
|
|
6916
|
+
const current = queue.shift();
|
|
6917
|
+
let entries;
|
|
6918
|
+
try {
|
|
6919
|
+
entries = await node_fs_promises.readdir(current.dir, { withFileTypes: true });
|
|
6920
|
+
} catch {
|
|
6921
|
+
continue;
|
|
6922
|
+
}
|
|
6923
|
+
for (const entry of entries) {
|
|
6924
|
+
if (shouldSkip(entry.name)) continue;
|
|
6925
|
+
if (entry.isDirectory()) {
|
|
6926
|
+
if (current.depth + 1 <= 3) queue.push({
|
|
6927
|
+
dir: node_path.join(current.dir, entry.name),
|
|
6928
|
+
depth: current.depth + 1
|
|
6929
|
+
});
|
|
6930
|
+
continue;
|
|
6931
|
+
}
|
|
6932
|
+
if (entry.isFile()) total += 1;
|
|
6933
|
+
}
|
|
6934
|
+
}
|
|
6935
|
+
return total;
|
|
6936
|
+
}
|
|
6937
|
+
async function describe(dir, skillPath) {
|
|
6938
|
+
const fallbackName = node_path.basename(dir);
|
|
6939
|
+
const files = await countFiles(dir);
|
|
6940
|
+
let raw;
|
|
6941
|
+
try {
|
|
6942
|
+
raw = await node_fs_promises.readFile(skillPath, "utf-8");
|
|
6943
|
+
} catch {
|
|
6944
|
+
return {
|
|
6945
|
+
name: fallbackName,
|
|
6946
|
+
description: "",
|
|
6947
|
+
dir,
|
|
6948
|
+
path: skillPath,
|
|
6949
|
+
files,
|
|
6950
|
+
valid: false,
|
|
6951
|
+
diagnostics: [{
|
|
6952
|
+
level: "error",
|
|
6953
|
+
code: "READ_FAILED",
|
|
6954
|
+
message: "SKILL.md 无法读取",
|
|
6955
|
+
path: skillPath
|
|
6956
|
+
}]
|
|
6957
|
+
};
|
|
6958
|
+
}
|
|
6959
|
+
const parsed = parseSkillFile(skillPath, raw);
|
|
6960
|
+
if (!parsed.skill) return {
|
|
6961
|
+
name: fallbackName,
|
|
6962
|
+
description: "",
|
|
6963
|
+
dir,
|
|
6964
|
+
path: skillPath,
|
|
6965
|
+
files,
|
|
6966
|
+
valid: false,
|
|
6967
|
+
diagnostics: parsed.diagnostics
|
|
6968
|
+
};
|
|
6969
|
+
return {
|
|
6970
|
+
name: parsed.skill.name,
|
|
6971
|
+
description: parsed.skill.description,
|
|
6972
|
+
dir,
|
|
6973
|
+
path: skillPath,
|
|
6974
|
+
files,
|
|
6975
|
+
valid: true,
|
|
6976
|
+
diagnostics: parsed.diagnostics
|
|
6977
|
+
};
|
|
6978
|
+
}
|
|
6979
|
+
/**
|
|
6980
|
+
* 在下载/解包出来的目录里定位技能:根目录有 SKILL.md 就用它,
|
|
6981
|
+
* 否则向下(深度受限)找所有含 SKILL.md 的目录,供安装时挑选。
|
|
6982
|
+
*/
|
|
6983
|
+
async function locateSkillRoots(root, options = {}) {
|
|
6984
|
+
const maxDepth = options.maxDepth ?? DEFAULT_MAX_DEPTH;
|
|
6985
|
+
const maxCandidates = options.maxCandidates ?? DEFAULT_MAX_CANDIDATES;
|
|
6986
|
+
const base = node_path.resolve(root);
|
|
6987
|
+
const rootSkill = node_path.join(base, SKILL_FILE_NAME);
|
|
6988
|
+
if (await isFile(rootSkill)) return [await describe(base, rootSkill)];
|
|
6989
|
+
const found = [];
|
|
6990
|
+
const queue = [{
|
|
6991
|
+
dir: base,
|
|
6992
|
+
depth: 0
|
|
6993
|
+
}];
|
|
6994
|
+
while (queue.length > 0 && found.length < maxCandidates) {
|
|
6995
|
+
const current = queue.shift();
|
|
6996
|
+
let entries;
|
|
6997
|
+
try {
|
|
6998
|
+
entries = await node_fs_promises.readdir(current.dir, { withFileTypes: true });
|
|
6999
|
+
} catch {
|
|
7000
|
+
continue;
|
|
7001
|
+
}
|
|
7002
|
+
for (const entry of entries) {
|
|
7003
|
+
if (!entry.isDirectory() || shouldSkip(entry.name)) continue;
|
|
7004
|
+
const child = node_path.join(current.dir, entry.name);
|
|
7005
|
+
const skillPath = node_path.join(child, SKILL_FILE_NAME);
|
|
7006
|
+
if (await isFile(skillPath)) {
|
|
7007
|
+
found.push({
|
|
7008
|
+
dir: child,
|
|
7009
|
+
path: skillPath
|
|
7010
|
+
});
|
|
7011
|
+
if (found.length >= maxCandidates) break;
|
|
7012
|
+
continue;
|
|
7013
|
+
}
|
|
7014
|
+
if (current.depth + 1 < maxDepth) queue.push({
|
|
7015
|
+
dir: child,
|
|
7016
|
+
depth: current.depth + 1
|
|
7017
|
+
});
|
|
7018
|
+
}
|
|
7019
|
+
}
|
|
7020
|
+
const locations = [];
|
|
7021
|
+
for (const item of found) locations.push(await describe(item.dir, item.path));
|
|
7022
|
+
return locations;
|
|
7023
|
+
}
|
|
7024
|
+
|
|
6264
7025
|
//#endregion
|
|
6265
7026
|
//#region src/types.ts
|
|
6266
7027
|
function normalizeSkillPermissionRole(role) {
|
|
@@ -6369,6 +7130,7 @@ exports.CORE_PLUGINS = CORE_PLUGINS;
|
|
|
6369
7130
|
exports.CapabilityRegistry = CapabilityRegistry;
|
|
6370
7131
|
exports.ChromeUA = ChromeUA;
|
|
6371
7132
|
exports.CommandManager = CommandManager;
|
|
7133
|
+
exports.DEFAULT_SKILL_ROOT_SPECS = DEFAULT_SKILL_ROOT_SPECS;
|
|
6372
7134
|
exports.DriverShutdownError = DriverShutdownError;
|
|
6373
7135
|
exports.EventBus = EventBus;
|
|
6374
7136
|
exports.EventCorrelator = EventCorrelator;
|
|
@@ -6376,6 +7138,12 @@ exports.HttpRequestError = HttpRequestError;
|
|
|
6376
7138
|
exports.MessageSegmentImpl = MessageSegmentImpl;
|
|
6377
7139
|
exports.MiokuContext = MiokuContext;
|
|
6378
7140
|
exports.MiokuRuntime = MiokuRuntime;
|
|
7141
|
+
exports.SKILL_COMPATIBILITY_MAX = SKILL_COMPATIBILITY_MAX;
|
|
7142
|
+
exports.SKILL_DESCRIPTION_MAX = SKILL_DESCRIPTION_MAX;
|
|
7143
|
+
exports.SKILL_FILE_NAME = SKILL_FILE_NAME;
|
|
7144
|
+
exports.SKILL_KNOWN_KEYS = SKILL_KNOWN_KEYS;
|
|
7145
|
+
exports.SKILL_NAME_MAX = SKILL_NAME_MAX;
|
|
7146
|
+
exports.SKILL_NAME_PATTERN = SKILL_NAME_PATTERN;
|
|
6379
7147
|
exports.ServiceManager = ServiceManager;
|
|
6380
7148
|
exports.Services = Services;
|
|
6381
7149
|
exports.SystemMap = SystemMap;
|
|
@@ -6435,6 +7203,7 @@ exports.createImportContext = createImportContext;
|
|
|
6435
7203
|
exports.createMessage = createMessage;
|
|
6436
7204
|
exports.createMiokuLogger = createMiokuLogger;
|
|
6437
7205
|
exports.createRuntime = createRuntime;
|
|
7206
|
+
exports.createSkillCatalog = createSkillCatalog;
|
|
6438
7207
|
exports.createStore = createStore;
|
|
6439
7208
|
Object.defineProperty(exports, 'dayjs', {
|
|
6440
7209
|
enumerable: true,
|
|
@@ -6456,8 +7225,11 @@ exports.defineState = defineState;
|
|
|
6456
7225
|
exports.deleteServiceConfig = deleteServiceConfig;
|
|
6457
7226
|
exports.discoverAdapterCandidates = discoverAdapterCandidates;
|
|
6458
7227
|
exports.discoverPluginCandidates = discoverPluginCandidates;
|
|
7228
|
+
exports.discoverRoot = discoverRoot;
|
|
7229
|
+
exports.discoverSkills = discoverSkills;
|
|
6459
7230
|
exports.displayAdapterId = displayAdapterId;
|
|
6460
7231
|
exports.ensureDataDir = ensureDataDir;
|
|
7232
|
+
exports.expandHomePath = expandHomePath;
|
|
6461
7233
|
Object.defineProperty(exports, 'filesize', {
|
|
6462
7234
|
enumerable: true,
|
|
6463
7235
|
get: function () {
|
|
@@ -6467,6 +7239,7 @@ Object.defineProperty(exports, 'filesize', {
|
|
|
6467
7239
|
exports.filter = filter;
|
|
6468
7240
|
exports.find = find;
|
|
6469
7241
|
exports.findLocalPlugins = findLocalPlugins;
|
|
7242
|
+
exports.findSkillRoot = findSkillRoot;
|
|
6470
7243
|
exports.formatAdapterReport = formatAdapterReport;
|
|
6471
7244
|
exports.formatDuration = formatDuration;
|
|
6472
7245
|
exports.formatMiokuStatus = formatMiokuStatus;
|
|
@@ -6534,12 +7307,14 @@ exports.isOwnerOrAdmin = isOwnerOrAdmin;
|
|
|
6534
7307
|
exports.isRequestEvent = isRequestEvent;
|
|
6535
7308
|
exports.isSegment = isSegment;
|
|
6536
7309
|
exports.isString = isString;
|
|
7310
|
+
exports.listSkillEntries = listSkillEntries;
|
|
6537
7311
|
exports.loadAdapterDefinition = loadAdapterDefinition;
|
|
6538
7312
|
exports.loadLocalPlugin = loadLocalPlugin;
|
|
6539
7313
|
exports.loadNpmPlugin = loadNpmPlugin;
|
|
6540
7314
|
exports.localNum = localNum;
|
|
6541
7315
|
exports.localeDate = localeDate;
|
|
6542
7316
|
exports.localeTime = localeTime;
|
|
7317
|
+
exports.locateSkillRoots = locateSkillRoots;
|
|
6543
7318
|
exports.logger = rootLogger;
|
|
6544
7319
|
exports.match = match;
|
|
6545
7320
|
exports.md5 = md5;
|
|
@@ -6550,6 +7325,7 @@ exports.memberPoke = memberPoke;
|
|
|
6550
7325
|
exports.memberSetAdmin = memberSetAdmin;
|
|
6551
7326
|
exports.memberSetCard = memberSetCard;
|
|
6552
7327
|
exports.memberSetTitle = memberSetTitle;
|
|
7328
|
+
exports.mergeSkillGroups = mergeSkillGroups;
|
|
6553
7329
|
exports.messageGet = messageGet;
|
|
6554
7330
|
exports.messageGetForward = messageGetForward;
|
|
6555
7331
|
exports.messageRecall = messageRecall;
|
|
@@ -6565,6 +7341,7 @@ exports.noticeAdmins = noticeAdmins;
|
|
|
6565
7341
|
exports.noticeFriends = noticeFriends;
|
|
6566
7342
|
exports.noticeGroups = noticeGroups;
|
|
6567
7343
|
exports.noticeOwners = noticeOwners;
|
|
7344
|
+
exports.parseSkillFile = parseSkillFile;
|
|
6568
7345
|
Object.defineProperty(exports, 'path', {
|
|
6569
7346
|
enumerable: true,
|
|
6570
7347
|
get: function () {
|
|
@@ -6586,11 +7363,14 @@ exports.randomItems = randomItems;
|
|
|
6586
7363
|
exports.readMiokuConfig = readMiokuConfig;
|
|
6587
7364
|
exports.readPackageJson = readPackageJson;
|
|
6588
7365
|
exports.readPackageJsonSafe = readPackageJsonSafe;
|
|
7366
|
+
exports.readSkillEntry = readSkillEntry;
|
|
6589
7367
|
exports.registerPluginArtifacts = registerPluginArtifacts;
|
|
6590
7368
|
exports.registerServiceConfig = registerServiceConfig;
|
|
6591
7369
|
exports.registerStatusProvider = registerStatusProvider;
|
|
6592
7370
|
exports.reloadMiokuConfig = reloadMiokuConfig;
|
|
6593
7371
|
exports.removePluginMetadata = removePluginMetadata;
|
|
7372
|
+
exports.renderSkillCatalog = renderSkillCatalog;
|
|
7373
|
+
exports.renderSkillContent = renderSkillContent;
|
|
6594
7374
|
exports.requireService = requireService;
|
|
6595
7375
|
exports.resetPluginMetadata = resetPluginMetadata;
|
|
6596
7376
|
exports.resetPluginRuntimeState = resetPluginRuntimeState;
|
|
@@ -6598,7 +7378,9 @@ exports.resolveCommand = require_exec.resolveCommand;
|
|
|
6598
7378
|
exports.resolveEntry = resolveEntry;
|
|
6599
7379
|
exports.resolveLocalPluginEntry = resolveLocalPluginEntry;
|
|
6600
7380
|
exports.resolveLocalPluginPath = resolveLocalPluginPath;
|
|
7381
|
+
exports.resolveSkillRoots = resolveSkillRoots;
|
|
6601
7382
|
exports.rootLogger = rootLogger;
|
|
7383
|
+
exports.rootOfPath = rootOfPath;
|
|
6602
7384
|
exports.runCommand = require_exec.runCommand;
|
|
6603
7385
|
exports.runCommandInherit = require_exec.runCommandInherit;
|
|
6604
7386
|
exports.segment = segment;
|