dsh-skill-harbor 0.0.1 → 0.1.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/README.md +39 -7
- package/dsh.plugin.json +16 -16
- package/lib/client.js +7 -3
- package/lib/collect/frontmatter.js +169 -0
- package/lib/collect/observe.js +143 -0
- package/lib/collect/ranks.js +56 -0
- package/lib/collect/roots.js +92 -0
- package/lib/collect/shadow.js +68 -0
- package/lib/index.js +14 -8
- package/lib/provider/lifecycle.js +74 -0
- package/package.json +48 -47
- package/src/client.js +7 -3
- package/src/collect/frontmatter.js +169 -0
- package/src/collect/observe.js +143 -0
- package/src/collect/ranks.js +56 -0
- package/src/collect/roots.js +92 -0
- package/src/collect/shadow.js +68 -0
- package/src/index.js +14 -8
- package/src/provider/lifecycle.js +74 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* provider 注册与生命周期。
|
|
3
|
+
*
|
|
4
|
+
* 两条硬约束(checklist Must Pass):
|
|
5
|
+
* 1. `registerProvider` 全部包在 `ctx.effect` 内 —— fiber 销毁时自动注销
|
|
6
|
+
* 2. `mounted()` 守卫覆盖全部异步续体 —— 防止 fiber 已销毁后仍有异步回调注册
|
|
7
|
+
* (这正是 #24 的形态:`already registered in this scope`)
|
|
8
|
+
*
|
|
9
|
+
* Phase 0 实测(findings):`registerProvider` 全线返回 `() => void`,可直接当 disposer;
|
|
10
|
+
* `ctx.fiber.uid` 在 dispose 时置 `null`,故用 `uid !== null` 判存活。
|
|
11
|
+
*/
|
|
12
|
+
import { collectRoots } from '../collect/roots.js'
|
|
13
|
+
import { observeSkills } from '../collect/observe.js'
|
|
14
|
+
import { applyShadowing } from '../collect/shadow.js'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 判断当前 fiber 是否仍挂载。
|
|
18
|
+
* Phase 0 实测:uid 类型是 `number | null`,**不是 optional**,
|
|
19
|
+
* 所以判 `!== null`;用 `=== undefined` 会永远 false(假死守卫)。
|
|
20
|
+
*/
|
|
21
|
+
export function isMounted(ctx) {
|
|
22
|
+
const fiber = ctx?.fiber
|
|
23
|
+
if (fiber === undefined || fiber === null) return true // 无 fiber 概念的环境放行
|
|
24
|
+
return fiber.uid !== null && fiber.uid !== undefined
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 注册 skill provider。
|
|
29
|
+
* @param {object} ctx cordis context
|
|
30
|
+
* @param {object} config 插件配置(customSkillDirs / bundledSkillDir / dshHome / agentsHome)
|
|
31
|
+
* @param {object} [deps] 注入点,便于单测
|
|
32
|
+
*/
|
|
33
|
+
export function installProvider(ctx, config = {}, deps = {}) {
|
|
34
|
+
const { collect = observeSkills, rootsFactory = collectRoots } = deps
|
|
35
|
+
const logger = ctx?.logger ?? console
|
|
36
|
+
|
|
37
|
+
const buildProvider = () => ({
|
|
38
|
+
name: 'dsh-skill-harbor',
|
|
39
|
+
async list() {
|
|
40
|
+
// 异步续体守卫:await 之后 fiber 可能已销毁
|
|
41
|
+
if (!isMounted(ctx)) return []
|
|
42
|
+
try {
|
|
43
|
+
const roots = rootsFactory({
|
|
44
|
+
cwd: config.cwd ?? process.cwd(),
|
|
45
|
+
dshHome: config.dshHome,
|
|
46
|
+
agentsHome: config.agentsHome,
|
|
47
|
+
customSkillDirs: config.customSkillDirs ?? [],
|
|
48
|
+
bundledSkillDir: config.bundledSkillDir,
|
|
49
|
+
})
|
|
50
|
+
const registrySummaries = typeof ctx?.skills?.list === 'function' ? await ctx.skills.list() : []
|
|
51
|
+
if (!isMounted(ctx)) return []
|
|
52
|
+
const observations = collect({ roots, registrySummaries })
|
|
53
|
+
return applyShadowing(observations).filter(o => o.shadowedBy === undefined)
|
|
54
|
+
} catch (error) {
|
|
55
|
+
logger.warn?.('[dsh-skill-harbor] provider.list failed', error)
|
|
56
|
+
return []
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
// 全部注册包在 effect 内:fiber 销毁 → 自动 dispose
|
|
62
|
+
const dispose = ctx.effect(() => {
|
|
63
|
+
const providerDisposer = ctx.skills.registerProvider(buildProvider)
|
|
64
|
+
return () => {
|
|
65
|
+
try {
|
|
66
|
+
providerDisposer()
|
|
67
|
+
} catch (error) {
|
|
68
|
+
logger.warn?.('[dsh-skill-harbor] provider dispose failed', error)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}, 'dsh-skill-harbor.registerProvider')
|
|
72
|
+
|
|
73
|
+
return dispose
|
|
74
|
+
}
|
package/package.json
CHANGED
|
@@ -1,47 +1,48 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "dsh-skill-harbor",
|
|
3
|
-
"version": "0.0
|
|
4
|
-
"description": "DSH 技能港湾:技能发现、遮蔽判定、审计与市场管理插件(基础骨架)",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"license": "MIT",
|
|
7
|
-
"repository": {
|
|
8
|
-
"type": "git",
|
|
9
|
-
"url": "https://github.com/drscrewdriver/dsh-skill-harbor"
|
|
10
|
-
},
|
|
11
|
-
"publishConfig": {
|
|
12
|
-
"access": "public",
|
|
13
|
-
"registry": "https://registry.npmjs.org"
|
|
14
|
-
},
|
|
15
|
-
"main": "lib/index.js",
|
|
16
|
-
"exports": {
|
|
17
|
-
".": "./lib/index.js",
|
|
18
|
-
"./client": "./lib/client.js",
|
|
19
|
-
"./package.json": "./package.json"
|
|
20
|
-
},
|
|
21
|
-
"engines": {
|
|
22
|
-
"node": ">=20"
|
|
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
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "dsh-skill-harbor",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "DSH 技能港湾:技能发现、遮蔽判定、审计与市场管理插件(基础骨架)",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/drscrewdriver/dsh-skill-harbor"
|
|
10
|
+
},
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public",
|
|
13
|
+
"registry": "https://registry.npmjs.org"
|
|
14
|
+
},
|
|
15
|
+
"main": "lib/index.js",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": "./lib/index.js",
|
|
18
|
+
"./client": "./lib/client.js",
|
|
19
|
+
"./package.json": "./package.json"
|
|
20
|
+
},
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=20",
|
|
23
|
+
"dsh": ">=0.1.7-alpha.1 <0.3.0-0"
|
|
24
|
+
},
|
|
25
|
+
"dsh": {
|
|
26
|
+
"bundle": {
|
|
27
|
+
"patch": "./cordis.patch.yml"
|
|
28
|
+
},
|
|
29
|
+
"client": {
|
|
30
|
+
"inject": [],
|
|
31
|
+
"platform": "web"
|
|
32
|
+
},
|
|
33
|
+
"manifestVersion": 1
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"lib",
|
|
37
|
+
"src",
|
|
38
|
+
"cordis.patch.yml",
|
|
39
|
+
"dsh.plugin.json",
|
|
40
|
+
"README.md",
|
|
41
|
+
"LICENSE"
|
|
42
|
+
],
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "node scripts/build.mjs",
|
|
45
|
+
"prepublishOnly": "npm run build",
|
|
46
|
+
"test": "node --test \"tests/**/*.test.mjs\""
|
|
47
|
+
}
|
|
48
|
+
}
|
package/src/client.js
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* dsh-skill-harbor — client half
|
|
3
|
-
*
|
|
2
|
+
* dsh-skill-harbor — client half(Phase 1 只占位,面板在 Phase 2 落地)。
|
|
3
|
+
*
|
|
4
|
+
* 注册 id **必须**等于 npm 包名,否则 DSH 静默忽略整个客户端半。
|
|
4
5
|
*/
|
|
5
6
|
const id = 'dsh-skill-harbor'
|
|
6
7
|
|
|
7
8
|
function factory() {
|
|
8
|
-
return {
|
|
9
|
+
return {
|
|
10
|
+
id,
|
|
11
|
+
// Phase 2 会在这里注册 slots / 面板 / 设置卡片
|
|
12
|
+
}
|
|
9
13
|
}
|
|
10
14
|
|
|
11
15
|
if (typeof window !== 'undefined' && window.__ModuleLoader__?.load) {
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 零依赖 frontmatter 解析。
|
|
3
|
+
*
|
|
4
|
+
* 不引 js-yaml:技能 frontmatter 的实际形态极窄(一层标量 + 少量布尔 + 两种块标量),
|
|
5
|
+
* 而 yaml 全量解析器会带进 ~100KB 与大量边界语义(锚点/多文档/类型推断),
|
|
6
|
+
* 我们只需要「读到正确字符串」。
|
|
7
|
+
*
|
|
8
|
+
* 支持:
|
|
9
|
+
* - `key: value` 标量(去引号)
|
|
10
|
+
* - `key: |` 字面块 / `key: >` 折叠块(含缩进剥离)
|
|
11
|
+
* - `disable-model-invocation` / `user-invocable` 布尔:
|
|
12
|
+
* true/false、1/0、yes/no、on/off(大小写不敏感)
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** 六种布尔写法的唯一收敛点。 */
|
|
16
|
+
const BOOL_TRUE = new Set(['true', '1', 'yes', 'on'])
|
|
17
|
+
const BOOL_FALSE = new Set(['false', '0', 'no', 'off'])
|
|
18
|
+
|
|
19
|
+
/** 解析布尔;不是布尔写法返回 undefined(区别于 false)。 */
|
|
20
|
+
export function parseBool(raw) {
|
|
21
|
+
if (typeof raw !== 'string') return undefined
|
|
22
|
+
const v = raw.trim().toLowerCase()
|
|
23
|
+
if (BOOL_TRUE.has(v)) return true
|
|
24
|
+
if (BOOL_FALSE.has(v)) return false
|
|
25
|
+
return undefined
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** 去掉成对引号(单双),并还原常见转义。 */
|
|
29
|
+
function unquote(raw) {
|
|
30
|
+
const v = raw.trim()
|
|
31
|
+
if (v.length >= 2) {
|
|
32
|
+
const first = v[0]
|
|
33
|
+
const last = v[v.length - 1]
|
|
34
|
+
if ((first === '"' || first === "'") && first === last) {
|
|
35
|
+
const inner = v.slice(1, -1)
|
|
36
|
+
return first === '"' ? inner.replace(/\\"/g, '"').replace(/\\n/g, '\n') : inner.replace(/''/g, "'")
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return v
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 从 Markdown 文本中切出 frontmatter 原文与正文。
|
|
44
|
+
* 只认文件最开头的 `---`(允许前置 BOM),与宿主一致。
|
|
45
|
+
*/
|
|
46
|
+
export function splitFrontmatter(text) {
|
|
47
|
+
const src = text.charCodeAt(0) === 0xfeff ? text.slice(1) : text
|
|
48
|
+
if (!src.startsWith('---')) return { raw: null, body: src }
|
|
49
|
+
|
|
50
|
+
// 找结束分隔行:独占一行的 --- 或 ...
|
|
51
|
+
const lines = src.split(/\r?\n/)
|
|
52
|
+
let end = -1
|
|
53
|
+
for (let i = 1; i < lines.length; i += 1) {
|
|
54
|
+
const line = lines[i]
|
|
55
|
+
if (line === '---' || line === '...') {
|
|
56
|
+
end = i
|
|
57
|
+
break
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (end === -1) return { raw: null, body: src }
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
raw: lines.slice(1, end).join('\n'),
|
|
64
|
+
body: lines.slice(end + 1).join('\n'),
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* 解析 frontmatter 原文为对象。
|
|
70
|
+
* 值一律是 string 或 string[](块标量),布尔交给调用方用 parseBool 收敛,
|
|
71
|
+
* 避免「看起来是字符串的 false」被当成真值。
|
|
72
|
+
*/
|
|
73
|
+
export function parseFrontmatter(raw) {
|
|
74
|
+
const out = Object.create(null)
|
|
75
|
+
if (typeof raw !== 'string' || raw.trim() === '') return out
|
|
76
|
+
|
|
77
|
+
const lines = raw.split(/\r?\n/)
|
|
78
|
+
let i = 0
|
|
79
|
+
while (i < lines.length) {
|
|
80
|
+
const line = lines[i]
|
|
81
|
+
if (line.trim() === '' || line.trimStart().startsWith('#')) {
|
|
82
|
+
i += 1
|
|
83
|
+
continue
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const match = /^([A-Za-z0-9_.-]+):(.*)$/.exec(line)
|
|
87
|
+
if (match === null) {
|
|
88
|
+
i += 1
|
|
89
|
+
continue
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const key = match[1]
|
|
93
|
+
const rest = match[2]
|
|
94
|
+
|
|
95
|
+
// 块标量:`key: |` 或 `key: >`(可带修饰符)
|
|
96
|
+
const blockMark = /^\s*([|>])[+-]?\s*$/.exec(rest)
|
|
97
|
+
if (blockMark !== null) {
|
|
98
|
+
const style = blockMark[1]
|
|
99
|
+
const body = []
|
|
100
|
+
i += 1
|
|
101
|
+
while (i < lines.length) {
|
|
102
|
+
const next = lines[i]
|
|
103
|
+
if (next.trim() !== '' && !/^\s/.test(next)) break
|
|
104
|
+
body.push(next)
|
|
105
|
+
i += 1
|
|
106
|
+
}
|
|
107
|
+
// 去掉公共缩进
|
|
108
|
+
const indents = body.filter(l => l.trim() !== '').map(l => l.match(/^\s*/)[0].length)
|
|
109
|
+
const cut = indents.length > 0 ? Math.min(...indents) : 0
|
|
110
|
+
const dedented = body.map(l => l.slice(cut))
|
|
111
|
+
// 去掉尾部空行
|
|
112
|
+
while (dedented.length > 0 && dedented[dedented.length - 1].trim() === '') dedented.pop()
|
|
113
|
+
out[key] = style === '|' ? dedented.join('\n') : dedented.join(' ').replace(/\s+/g, ' ').trim()
|
|
114
|
+
continue
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// 普通标量
|
|
118
|
+
const value = rest.trim()
|
|
119
|
+
if (value !== '') {
|
|
120
|
+
out[key] = unquote(value)
|
|
121
|
+
i += 1
|
|
122
|
+
continue
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// 空值 → 可能是紧随其后的 `- item` 列表
|
|
126
|
+
const items = []
|
|
127
|
+
i += 1
|
|
128
|
+
while (i < lines.length) {
|
|
129
|
+
const next = lines[i]
|
|
130
|
+
const item = /^\s*-\s+(.*)$/.exec(next)
|
|
131
|
+
if (item === null) break
|
|
132
|
+
items.push(unquote(item[1]))
|
|
133
|
+
i += 1
|
|
134
|
+
}
|
|
135
|
+
out[key] = items.length > 0 ? items : ''
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return out
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** 一站式:文本 → { data, body }。 */
|
|
142
|
+
export function readSkillDocument(text) {
|
|
143
|
+
const { raw, body } = splitFrontmatter(text)
|
|
144
|
+
return { data: parseFrontmatter(raw), body, hasFrontmatter: raw !== null }
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* 归一化成 SkillObservation 需要的三字段。
|
|
149
|
+
* `whenToUse` 兼容 whenToUse / when_to_use 两种写法。
|
|
150
|
+
*/
|
|
151
|
+
export function extractSkillMeta(text) {
|
|
152
|
+
const { data, body } = readSkillDocument(text)
|
|
153
|
+
const name = typeof data.name === 'string' ? data.name : ''
|
|
154
|
+
const description = typeof data.description === 'string' ? data.description : ''
|
|
155
|
+
const whenToUseRaw = data.whenToUse ?? data.when_to_use
|
|
156
|
+
const whenToUse = typeof whenToUseRaw === 'string' && whenToUseRaw !== '' ? whenToUseRaw : undefined
|
|
157
|
+
|
|
158
|
+
const disableRaw = data['disable-model-invocation']
|
|
159
|
+
const userRaw = data['user-invocable']
|
|
160
|
+
|
|
161
|
+
return {
|
|
162
|
+
name,
|
|
163
|
+
description,
|
|
164
|
+
...whenToUse === undefined ? {} : { whenToUse },
|
|
165
|
+
disableModelInvocation: parseBool(disableRaw) ?? false,
|
|
166
|
+
userInvocable: parseBool(userRaw) ?? true,
|
|
167
|
+
body,
|
|
168
|
+
}
|
|
169
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SkillObservation 组装 —— 证据层的核心。
|
|
3
|
+
*
|
|
4
|
+
* 三态 fileState(findings A4,采自 @linxin666,点名 issue #1842):
|
|
5
|
+
* - `editable` 本面板扫盘解析到了 SKILL.md,携带写路由可信的 path
|
|
6
|
+
* - `provider-file` 官方 registry 报了真实指令文件(SkillSummary.path),但本面板扫描没解析到
|
|
7
|
+
* —— 文件存在,说「不存在」正是 #1842;但它不是写路由信任的路径,
|
|
8
|
+
* 因此**不得提供任何写控件**
|
|
9
|
+
* - `virtual` registry 压根没报指令文件,「无本地文件」才是准确说法
|
|
10
|
+
*/
|
|
11
|
+
import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs'
|
|
12
|
+
import { basename, join } from 'node:path'
|
|
13
|
+
|
|
14
|
+
import { extractSkillMeta } from './frontmatter.js'
|
|
15
|
+
import { filterExistingRoots } from './roots.js'
|
|
16
|
+
|
|
17
|
+
/** 三态常量,避免散落字符串。 */
|
|
18
|
+
export const FILE_STATE = Object.freeze({
|
|
19
|
+
EDITABLE: 'editable',
|
|
20
|
+
PROVIDER_FILE: 'provider-file',
|
|
21
|
+
VIRTUAL: 'virtual',
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 在单个根下枚举技能:两种形态
|
|
26
|
+
* - 目录包 `<root>/<name>/SKILL.md`
|
|
27
|
+
* - 平铺 `<root>/<name>.md`
|
|
28
|
+
* 符号链接**跳过**(不跟随,findings A5)。
|
|
29
|
+
*/
|
|
30
|
+
export function scanRoot(root) {
|
|
31
|
+
const found = []
|
|
32
|
+
let entries
|
|
33
|
+
try {
|
|
34
|
+
entries = readdirSync(root.path, { withFileTypes: true })
|
|
35
|
+
} catch {
|
|
36
|
+
return found
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
for (const entry of entries) {
|
|
40
|
+
if (entry.isSymbolicLink()) continue
|
|
41
|
+
|
|
42
|
+
if (entry.isDirectory()) {
|
|
43
|
+
const skillFile = join(root.path, entry.name, 'SKILL.md')
|
|
44
|
+
if (safeIsFile(skillFile)) {
|
|
45
|
+
found.push({ name: entry.name, path: skillFile, dir: join(root.path, entry.name) })
|
|
46
|
+
}
|
|
47
|
+
continue
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (entry.isFile() && entry.name.toLowerCase().endsWith('.md')) {
|
|
51
|
+
// 平铺形态;SKILL.md 本身不算平铺技能(它属于目录包)
|
|
52
|
+
const name = basename(entry.name, entry.name.slice(-3))
|
|
53
|
+
found.push({ name, path: join(root.path, entry.name), dir: null })
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return found
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function safeIsFile(path) {
|
|
60
|
+
try {
|
|
61
|
+
return statSync(path).isFile()
|
|
62
|
+
} catch {
|
|
63
|
+
return false
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** 渐进披露第一层的字符数:name + description + whenToUse,不含正文。 */
|
|
68
|
+
export function announcementChars(meta) {
|
|
69
|
+
const parts = [meta.name ?? '', meta.description ?? '', meta.whenToUse ?? '']
|
|
70
|
+
return parts.reduce((sum, part) => sum + [...part].length, 0)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* 扫描全部根,产出 SkillObservation[](尚未做遮蔽判定)。
|
|
75
|
+
* registrySummaries 是官方 `ctx.skills.list()` 的结果,用于补齐 provider-file / virtual。
|
|
76
|
+
*/
|
|
77
|
+
export function observeSkills({ roots, registrySummaries = [] }) {
|
|
78
|
+
const observations = []
|
|
79
|
+
const scanned = new Map() // name -> observation
|
|
80
|
+
|
|
81
|
+
for (const root of filterExistingRoots(roots)) {
|
|
82
|
+
for (const hit of scanRoot(root)) {
|
|
83
|
+
let text
|
|
84
|
+
try {
|
|
85
|
+
text = readFileSync(hit.path, 'utf8')
|
|
86
|
+
} catch {
|
|
87
|
+
continue
|
|
88
|
+
}
|
|
89
|
+
const meta = extractSkillMeta(text)
|
|
90
|
+
const name = meta.name !== '' ? meta.name : hit.name
|
|
91
|
+
|
|
92
|
+
const observation = {
|
|
93
|
+
name,
|
|
94
|
+
description: meta.description,
|
|
95
|
+
...meta.whenToUse === undefined ? {} : { whenToUse: meta.whenToUse },
|
|
96
|
+
source: root.source,
|
|
97
|
+
rank: root.rank,
|
|
98
|
+
path: hit.path,
|
|
99
|
+
fileState: FILE_STATE.EDITABLE,
|
|
100
|
+
modelInvocable: !meta.disableModelInvocation,
|
|
101
|
+
announcementChars: announcementChars(meta),
|
|
102
|
+
}
|
|
103
|
+
observations.push(observation)
|
|
104
|
+
if (!scanned.has(name)) scanned.set(name, observation)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// 官方 registry 报了、我们没扫到的:区分 provider-file / virtual
|
|
109
|
+
for (const summary of registrySummaries) {
|
|
110
|
+
if (scanned.has(summary.name)) continue
|
|
111
|
+
const hasPath = typeof summary.path === 'string' && summary.path !== ''
|
|
112
|
+
observations.push({
|
|
113
|
+
name: summary.name,
|
|
114
|
+
description: summary.description ?? '',
|
|
115
|
+
...summary.whenToUse === undefined ? {} : { whenToUse: summary.whenToUse },
|
|
116
|
+
source: typeof summary.source === 'string' ? summary.source : 'runtime',
|
|
117
|
+
rank: typeof summary.rank === 'number' ? summary.rank : Number.MAX_SAFE_INTEGER,
|
|
118
|
+
...hasPath ? { path: summary.path } : {},
|
|
119
|
+
fileState: hasPath ? FILE_STATE.PROVIDER_FILE : FILE_STATE.VIRTUAL,
|
|
120
|
+
modelInvocable: summary.invocation?.model === true,
|
|
121
|
+
announcementChars: announcementChars({
|
|
122
|
+
name: summary.name,
|
|
123
|
+
description: summary.description,
|
|
124
|
+
whenToUse: summary.whenToUse,
|
|
125
|
+
}),
|
|
126
|
+
})
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return observations
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* 该条目是否允许写操作。
|
|
134
|
+
* `provider-file` / `virtual` **一律不可写**(UI 层断言也依赖此谓词)。
|
|
135
|
+
*/
|
|
136
|
+
export function isWritable(observation) {
|
|
137
|
+
return observation.fileState === FILE_STATE.EDITABLE && typeof observation.path === 'string'
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** 路径是否存在(用于确认扫描结果未过期)。 */
|
|
141
|
+
export function stillExists(observation) {
|
|
142
|
+
return typeof observation.path === 'string' && existsSync(observation.path)
|
|
143
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 官方 skill source rank 表 —— **必须与宿主完全一致,不得自创第 8 个 source**。
|
|
3
|
+
*
|
|
4
|
+
* 取证(dsh 0.2.3-alpha.2):
|
|
5
|
+
* - `packages/skill/skill-filesystem/src/index.ts:36-40`
|
|
6
|
+
* PROJECT_DSH_RANK = 100 / PROJECT_AGENTS_RANK = 200 /
|
|
7
|
+
* CUSTOM_RANK = 300 / USER_DSH_RANK = 400 / USER_AGENTS_RANK = 500
|
|
8
|
+
* - `packages/skill/skill/src/index.ts:28,31`
|
|
9
|
+
* RUNTIME_RANK = 250 / BUNDLED_SKILL_RANK = 600
|
|
10
|
+
*
|
|
11
|
+
* 注意 `runtime` 刻意排在 250,夹在 project-agents 与 custom 之间,**不是最后**。
|
|
12
|
+
* 数字小者胜(用于遮蔽判定)。
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** 七个官方 source,顺序即 rank 升序。 */
|
|
16
|
+
export const SOURCES = [
|
|
17
|
+
'project-dsh',
|
|
18
|
+
'project-agents',
|
|
19
|
+
'runtime',
|
|
20
|
+
'custom',
|
|
21
|
+
'user-dsh',
|
|
22
|
+
'user-agents',
|
|
23
|
+
'bundled',
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
/** source → rank。集中一处,单测钉死。 */
|
|
27
|
+
export const SOURCE_RANK = Object.freeze({
|
|
28
|
+
'project-dsh': 100,
|
|
29
|
+
'project-agents': 200,
|
|
30
|
+
runtime: 250,
|
|
31
|
+
custom: 300,
|
|
32
|
+
'user-dsh': 400,
|
|
33
|
+
'user-agents': 500,
|
|
34
|
+
bundled: 600,
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 约定根的相对形态。项目根相关项相对 `<projectRoot>`,
|
|
39
|
+
* 用户级项相对 `<home>`;`custom` 由配置注入,无固定路径。
|
|
40
|
+
*/
|
|
41
|
+
export const ROOT_LAYOUT = Object.freeze({
|
|
42
|
+
'project-dsh': '.dsh/skills',
|
|
43
|
+
'project-agents': '.agents/skills',
|
|
44
|
+
'user-dsh': '.dsh/skills',
|
|
45
|
+
'user-agents': '.agents/skills',
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
/** 取 rank;未知 source 给一个极大值(永不胜出)。 */
|
|
49
|
+
export function rankOf(source) {
|
|
50
|
+
return SOURCE_RANK[source] ?? Number.MAX_SAFE_INTEGER
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** a 是否比 b 更强(rank 更小)。rank 相同则视为不更强。 */
|
|
54
|
+
export function outranks(a, b) {
|
|
55
|
+
return rankOf(a) < rankOf(b)
|
|
56
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 根目录枚举:把 6 个约定根 + custom 解析成具体的 root 列表。
|
|
3
|
+
*
|
|
4
|
+
* 与宿主 `packages/skill/skill-filesystem/src/index.ts:250-270` 的分派保持一致:
|
|
5
|
+
* 项目级 → `<projectRoot>/.dsh/skills` / `<projectRoot>/.agents/skills`
|
|
6
|
+
* custom → 配置项 `customSkillDirs`(原样)
|
|
7
|
+
* 用户级 → `<dshHome>/skills` / `<agentsHome>/skills`
|
|
8
|
+
* bundled → `DSH_BUNDLED_SKILL_DIR`(存在才加)
|
|
9
|
+
*
|
|
10
|
+
* 项目根 = 向上找最近的 `.git`(与宿主 `findProjectRoot()` 同义)。
|
|
11
|
+
*/
|
|
12
|
+
import { existsSync, statSync } from 'node:fs'
|
|
13
|
+
import { dirname, join, resolve } from 'node:path'
|
|
14
|
+
|
|
15
|
+
import { SOURCE_RANK } from './ranks.js'
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 向上找最近含 `.git` 的目录。找不到返回 null(不回落 cwd——
|
|
19
|
+
* 回落会把用户家目录当成项目根,造出假的项目级技能)。
|
|
20
|
+
*/
|
|
21
|
+
export function findProjectRoot(startDir) {
|
|
22
|
+
let current = resolve(startDir)
|
|
23
|
+
for (;;) {
|
|
24
|
+
const dotGit = join(current, '.git')
|
|
25
|
+
if (existsSync(dotGit)) return current
|
|
26
|
+
const parent = dirname(current)
|
|
27
|
+
if (parent === current) return null
|
|
28
|
+
current = parent
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** 只保留真实存在的目录。 */
|
|
33
|
+
function existingDir(path) {
|
|
34
|
+
try {
|
|
35
|
+
return statSync(path).isDirectory()
|
|
36
|
+
} catch {
|
|
37
|
+
return false
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 枚举全部根。
|
|
43
|
+
* @param {object} options
|
|
44
|
+
* @param {string} options.cwd 起始目录(向上找 .git)
|
|
45
|
+
* @param {string} options.dshHome `~/.dsh` 一类
|
|
46
|
+
* @param {string} options.agentsHome `~/.agents` 一类
|
|
47
|
+
* @param {string[]} [options.customSkillDirs]
|
|
48
|
+
* @param {string} [options.bundledSkillDir]
|
|
49
|
+
* @returns {Array<{path:string, source:string, rank:number}>} rank 升序
|
|
50
|
+
*/
|
|
51
|
+
export function collectRoots(options) {
|
|
52
|
+
const { cwd, dshHome, agentsHome, customSkillDirs = [], bundledSkillDir } = options
|
|
53
|
+
const roots = []
|
|
54
|
+
|
|
55
|
+
const projectRoot = findProjectRoot(cwd)
|
|
56
|
+
if (projectRoot !== null) {
|
|
57
|
+
roots.push({
|
|
58
|
+
path: join(projectRoot, '.dsh/skills'),
|
|
59
|
+
source: 'project-dsh',
|
|
60
|
+
rank: SOURCE_RANK['project-dsh'],
|
|
61
|
+
projectRoot,
|
|
62
|
+
})
|
|
63
|
+
roots.push({
|
|
64
|
+
path: join(projectRoot, '.agents/skills'),
|
|
65
|
+
source: 'project-agents',
|
|
66
|
+
rank: SOURCE_RANK['project-agents'],
|
|
67
|
+
projectRoot,
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
for (const dir of customSkillDirs) {
|
|
72
|
+
roots.push({ path: dir, source: 'custom', rank: SOURCE_RANK.custom })
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (dshHome) {
|
|
76
|
+
roots.push({ path: join(dshHome, 'skills'), source: 'user-dsh', rank: SOURCE_RANK['user-dsh'] })
|
|
77
|
+
}
|
|
78
|
+
if (agentsHome) {
|
|
79
|
+
roots.push({ path: join(agentsHome, 'skills'), source: 'user-agents', rank: SOURCE_RANK['user-agents'] })
|
|
80
|
+
}
|
|
81
|
+
if (bundledSkillDir && existingDir(bundledSkillDir)) {
|
|
82
|
+
roots.push({ path: bundledSkillDir, source: 'bundled', rank: SOURCE_RANK.bundled })
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// 恒按 rank 升序,下游不必再排
|
|
86
|
+
return roots.sort((a, b) => a.rank - b.rank)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** 只留磁盘上真实存在的根(扫描前过滤,减少无谓 IO)。 */
|
|
90
|
+
export function filterExistingRoots(roots) {
|
|
91
|
+
return roots.filter(root => existingDir(root.path))
|
|
92
|
+
}
|