dsh-skill-harbor 0.5.3 → 0.5.4
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 -21
- package/README.md +44 -44
- package/cordis.patch.yml +4 -4
- package/dsh.plugin.json +1 -1
- package/lib/client.js +1447 -23
- package/lib/index.js +34 -1
- package/package.json +7 -2
- package/src/audit/normalize.js +77 -77
- package/src/audit/rules.js +167 -167
- package/src/budget/announcement.js +53 -53
- package/src/client/api.js +79 -79
- package/src/client/labels.js +6 -6
- package/src/client/lang.js +20 -0
- package/src/client/locales.js +688 -688
- package/src/client/mount.js +31 -0
- package/src/client/panel.js +5 -1
- package/src/client/react.js +13 -0
- package/src/client.js +35 -11
- package/src/collect/frontmatter.js +169 -169
- package/src/collect/observe.js +143 -143
- package/src/collect/ranks.js +56 -56
- package/src/collect/roots.js +92 -92
- package/src/collect/shadow.js +68 -68
- package/src/collect/watcher.js +86 -86
- package/src/config.js +42 -42
- package/src/index.js +34 -1
- package/src/market/fetch.js +94 -94
- package/src/market/install.js +169 -169
- package/src/market/source.js +90 -90
- package/src/market/store.js +66 -66
- package/src/market/zip.js +201 -201
- package/src/provider/lifecycle.js +74 -74
- package/src/routes/guard.js +156 -156
- package/src/routes/safe-path.js +96 -96
- package/src/routes/store.js +72 -72
- package/src/settings.js +83 -83
- package/lib/client/api.js +0 -79
- package/lib/client/labels.js +0 -6
- package/lib/client/locales.js +0 -688
- package/lib/client/panel.js +0 -527
package/src/collect/observe.js
CHANGED
|
@@ -1,143 +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
|
-
}
|
|
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
|
+
}
|
package/src/collect/ranks.js
CHANGED
|
@@ -1,56 +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
|
-
}
|
|
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
|
+
}
|
package/src/collect/roots.js
CHANGED
|
@@ -1,92 +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
|
-
}
|
|
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
|
+
}
|