dsh-skill-harbor 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +43 -2
- package/cordis.patch.yml +4 -0
- package/dsh.plugin.json +16 -0
- package/lib/client.js +19 -0
- 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 +20 -0
- package/lib/provider/lifecycle.js +74 -0
- package/package.json +48 -6
- package/src/client.js +19 -0
- 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 +20 -0
- package/src/provider/lifecycle.js +74 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 drscrewdriver
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,44 @@
|
|
|
1
|
-
#
|
|
1
|
+
# dsh-skill-harbor(技能港湾)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/dsh-skill-harbor)
|
|
4
|
+
|
|
5
|
+
DSH 技能管理面板插件:**技能发现 / 遮蔽判定 / 审计 / 市场管理**。
|
|
6
|
+
|
|
7
|
+
> 当前版本 `0.0.1` = **基础骨架**(宿主半 + 客户端半 + 清单 + 构建脚本),业务逻辑按计划分阶段落地。
|
|
8
|
+
|
|
9
|
+
## 安装
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
dsh plugin --profile <name> add dsh-skill-harbor@0.0.1
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## 版本线
|
|
16
|
+
|
|
17
|
+
| 版本 | 内容 | 状态 |
|
|
18
|
+
|---|---|---|
|
|
19
|
+
| `0.0.1` | 基础骨架 | ✅ 已发布 |
|
|
20
|
+
| `0.1.0` | Phase 1 证据层(根枚举 / frontmatter / observe / shadow / provider) | 规划中 |
|
|
21
|
+
| `0.2.0` | Phase 2 本地管理(路由闸 / 列表 / 写 / 导入) | 规划中 |
|
|
22
|
+
| `0.3.0` | Phase 3 预算与审计 = **MVP** | 规划中 |
|
|
23
|
+
| `0.4.0` | Phase 4 市场(不进 MVP) | 规划中 |
|
|
24
|
+
|
|
25
|
+
发布粒度:每 Phase 一个中版本,每 task 一次 rc。完整 26 个版本节点的拆解见 `docs/plans/dsh-skill-harbor/tasks.md`。
|
|
26
|
+
|
|
27
|
+
## 结构
|
|
28
|
+
|
|
29
|
+
```
|
|
30
|
+
src/index.js 宿主半(apply(ctx, config))
|
|
31
|
+
src/client.js 客户端半(__ModuleLoader__ 注册,id = 包名)
|
|
32
|
+
scripts/build.mjs 零依赖构建 → lib/
|
|
33
|
+
cordis.patch.yml bundle patch(dsh plugin add 自动挂载)
|
|
34
|
+
dsh.plugin.json DSH 插件清单
|
|
35
|
+
docs/plans/ 规划文档(spec / tasks / checklist / findings)
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## 背景
|
|
39
|
+
|
|
40
|
+
DSH 官方没有技能管理界面,且 `packages/bundle/web-app/cordis.patch.yml` 把 `skill-filesystem` / `tool-skill` 在 host 层设为 `disabled: true`,技能发现被下放到 preset(agent scope)层——host 面的 `ctx.skills` 读不到项目/用户技能。本插件要回答:**这个技能到底在哪、谁在遮蔽谁、模型收没收到。**
|
|
41
|
+
|
|
42
|
+
## License
|
|
43
|
+
|
|
44
|
+
MIT
|
package/cordis.patch.yml
ADDED
package/dsh.plugin.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "dsh-external/dsh-skill-harbor",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"main": "./lib/index.js",
|
|
5
|
+
"description": "技能港湾:技能发现、遮蔽判定、审计与市场管理(基础骨架)",
|
|
6
|
+
"engines": {
|
|
7
|
+
"dsh": ">=0.1.7-alpha.1 <0.3.0-0"
|
|
8
|
+
},
|
|
9
|
+
"contributes": {
|
|
10
|
+
"tools": [],
|
|
11
|
+
"skills": []
|
|
12
|
+
},
|
|
13
|
+
"client": {
|
|
14
|
+
"main": "./lib/client.js"
|
|
15
|
+
}
|
|
16
|
+
}
|
package/lib/client.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-skill-harbor — client half(Phase 1 只占位,面板在 Phase 2 落地)。
|
|
3
|
+
*
|
|
4
|
+
* 注册 id **必须**等于 npm 包名,否则 DSH 静默忽略整个客户端半。
|
|
5
|
+
*/
|
|
6
|
+
const id = 'dsh-skill-harbor'
|
|
7
|
+
|
|
8
|
+
function factory() {
|
|
9
|
+
return {
|
|
10
|
+
id,
|
|
11
|
+
// Phase 2 会在这里注册 slots / 面板 / 设置卡片
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
if (typeof window !== 'undefined' && window.__ModuleLoader__?.load) {
|
|
16
|
+
window.__ModuleLoader__.load({ id, factory })
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export { id, factory }
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 同名遮蔽判定。
|
|
3
|
+
*
|
|
4
|
+
* 规则:同名技能在多个 source 出现时,rank 最小(数字最小)者为胜者,
|
|
5
|
+
* 败者携带 `shadowedBy: { source, rank }` 指向胜者。rank 相同视为并列,
|
|
6
|
+
* **都不标记遮蔽**(谁也没赢,报遮蔽会误导用户)。
|
|
7
|
+
*
|
|
8
|
+
* Windows 路径大小写不敏感 —— 但遮蔽判定按 **技能名** 而非路径,
|
|
9
|
+
* 因此这里只对名字做大小写不敏感比较(Windows 上 `Foo` 与 `foo` 是同一个技能)。
|
|
10
|
+
*/
|
|
11
|
+
import { rankOf } from './ranks.js'
|
|
12
|
+
|
|
13
|
+
/** 名字归一化:Windows 语义下大小写不敏感。 */
|
|
14
|
+
function normalizeName(name) {
|
|
15
|
+
return String(name).trim().toLowerCase()
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @param {Array<object>} observations
|
|
20
|
+
* @param {{ caseInsensitive?: boolean }} [options]
|
|
21
|
+
* @returns {Array<object>} 新数组,胜者 shadowedBy 为 undefined
|
|
22
|
+
*/
|
|
23
|
+
export function applyShadowing(observations, options = {}) {
|
|
24
|
+
const { caseInsensitive = true } = options
|
|
25
|
+
const keyOf = caseInsensitive ? normalizeName : name => String(name).trim()
|
|
26
|
+
|
|
27
|
+
// name-key -> 当前胜者
|
|
28
|
+
const winners = new Map()
|
|
29
|
+
|
|
30
|
+
for (const observation of observations) {
|
|
31
|
+
const key = keyOf(observation.name)
|
|
32
|
+
const current = winners.get(key)
|
|
33
|
+
if (current === undefined) {
|
|
34
|
+
winners.set(key, observation)
|
|
35
|
+
continue
|
|
36
|
+
}
|
|
37
|
+
// rank 更小者胜;相等则保持先到者,且不互相遮蔽
|
|
38
|
+
if (rankOf(observation.source) < rankOf(current.source)) {
|
|
39
|
+
winners.set(key, observation)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return observations.map(observation => {
|
|
44
|
+
const key = keyOf(observation.name)
|
|
45
|
+
const winner = winners.get(key)
|
|
46
|
+
if (winner === undefined) {
|
|
47
|
+
return { ...observation, shadowedBy: undefined }
|
|
48
|
+
}
|
|
49
|
+
// 只有胜者 rank **严格更小**才算遮蔽;并列不遮蔽(谁也没赢)
|
|
50
|
+
if (rankOf(winner.source) < rankOf(observation.source)) {
|
|
51
|
+
return {
|
|
52
|
+
...observation,
|
|
53
|
+
shadowedBy: { source: winner.source, rank: rankOf(winner.source) },
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return { ...observation, shadowedBy: undefined }
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** 只保留胜者(未被遮蔽的)。 */
|
|
61
|
+
export function visibleSkills(observations) {
|
|
62
|
+
return observations.filter(observation => observation.shadowedBy === undefined)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** 是否真能被模型看见:胜者 且 未被 disable-model-invocation 关掉。 */
|
|
66
|
+
export function modelVisibleSkills(observations) {
|
|
67
|
+
return visibleSkills(observations).filter(observation => observation.modelInvocable === true)
|
|
68
|
+
}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-skill-harbor — host half.
|
|
3
|
+
*
|
|
4
|
+
* Phase 1(证据层):扫描 6 个约定根、解析 frontmatter、三态判定、遮蔽判定,
|
|
5
|
+
* 通过 provider 把结果交给官方 skill registry。
|
|
6
|
+
*
|
|
7
|
+
* Phase 0 实测结论(findings.md):
|
|
8
|
+
* - `ctx.skills.registerProvider` 全线返回 `() => void`,可直接当 disposer
|
|
9
|
+
* - `ctx.fiber.uid` 为 `number | null`,dispose 后置 `null`
|
|
10
|
+
* - `SkillSummary.path` 自 0.1.7-alpha.1 起存在(engines 下界依据)
|
|
11
|
+
*/
|
|
12
|
+
import { installProvider } from './provider/lifecycle.js'
|
|
13
|
+
|
|
14
|
+
export const name = 'dsh-skill-harbor'
|
|
15
|
+
|
|
16
|
+
export const inject = ['skills']
|
|
17
|
+
|
|
18
|
+
export function apply(ctx, config = {}) {
|
|
19
|
+
installProvider(ctx, config)
|
|
20
|
+
}
|