dsh-skill-harbor 0.5.4 → 0.6.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 -21
- package/README.md +44 -44
- package/cordis.patch.yml +4 -4
- package/dsh.plugin.json +1 -1
- package/package.json +2 -1
- 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/locales.js +688 -688
- 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/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/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
|
+
}
|
package/src/collect/shadow.js
CHANGED
|
@@ -1,68 +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
|
-
}
|
|
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/src/collect/watcher.js
CHANGED
|
@@ -1,86 +1,86 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 技能目录 watcher —— 增量感知,替代「每次请求全量重扫」。
|
|
3
|
-
*
|
|
4
|
-
* 设计取舍:
|
|
5
|
-
* - 不做「精确到文件的 diff」:那需要维护一棵完整树,且 fs.watch 在 Windows 上
|
|
6
|
-
* 对深层目录的事件并不可靠。这里只做**代数(generation)递增**:
|
|
7
|
-
* 任何目录变动 → 代数 +1 → 快照缓存失效。
|
|
8
|
-
* - 事件做 debounce 合并:一次保存会触发多个事件(创建/重命名/写入),
|
|
9
|
-
* 不合并会连打多次重扫。
|
|
10
|
-
* - watcher 失败(目录不存在 / 权限)不致命:降级为「无 watcher」,
|
|
11
|
-
* 此时只靠 TTL 兜底,并把这个降级状态如实报告给调用方。
|
|
12
|
-
*/
|
|
13
|
-
import { watch } from 'node:fs'
|
|
14
|
-
import { existsSync } from 'node:fs'
|
|
15
|
-
|
|
16
|
-
export const DEBOUNCE_MS = 250
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* 观察一组根目录。
|
|
20
|
-
* @param {Array<{path:string, source:string}>} roots
|
|
21
|
-
* @param {() => void} onInvalidate 变动时调用(已 debounce)
|
|
22
|
-
* @param {{ debounceMs?: number }} [options]
|
|
23
|
-
* @returns {{ generation: () => number, watching: string[], degraded: Array<{path:string,reason:string}>, close: () => void }}
|
|
24
|
-
*/
|
|
25
|
-
export function watchRoots(roots, onInvalidate, options = {}) {
|
|
26
|
-
const debounceMs = options.debounceMs ?? DEBOUNCE_MS
|
|
27
|
-
|
|
28
|
-
let generation = 0
|
|
29
|
-
let timer = null
|
|
30
|
-
const watchers = []
|
|
31
|
-
const watching = []
|
|
32
|
-
const degraded = []
|
|
33
|
-
|
|
34
|
-
const bump = () => {
|
|
35
|
-
if (timer !== null) clearTimeout(timer)
|
|
36
|
-
timer = setTimeout(() => {
|
|
37
|
-
timer = null
|
|
38
|
-
generation += 1
|
|
39
|
-
try {
|
|
40
|
-
onInvalidate(generation)
|
|
41
|
-
} catch {
|
|
42
|
-
/* 回调失败不应影响 watcher 本身 */
|
|
43
|
-
}
|
|
44
|
-
}, debounceMs)
|
|
45
|
-
// 允许进程退出,不被 timer 挂住
|
|
46
|
-
if (typeof timer.unref === 'function') timer.unref()
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
for (const root of roots) {
|
|
50
|
-
if (!existsSync(root.path)) {
|
|
51
|
-
degraded.push({ path: root.path, reason: 'not-exists' })
|
|
52
|
-
continue
|
|
53
|
-
}
|
|
54
|
-
try {
|
|
55
|
-
const watcher = watch(root.path, { recursive: true, persistent: false }, bump)
|
|
56
|
-
watcher.on('error', error => {
|
|
57
|
-
degraded.push({ path: root.path, reason: String(error?.code ?? error?.message ?? error) })
|
|
58
|
-
})
|
|
59
|
-
watchers.push(watcher)
|
|
60
|
-
watching.push(root.path)
|
|
61
|
-
} catch (error) {
|
|
62
|
-
// recursive 在某些平台/网络盘不支持 → 记录降级,不抛
|
|
63
|
-
degraded.push({ path: root.path, reason: String(error?.code ?? error?.message ?? error) })
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return {
|
|
68
|
-
generation: () => generation,
|
|
69
|
-
watching,
|
|
70
|
-
degraded,
|
|
71
|
-
close() {
|
|
72
|
-
if (timer !== null) {
|
|
73
|
-
clearTimeout(timer)
|
|
74
|
-
timer = null
|
|
75
|
-
}
|
|
76
|
-
for (const watcher of watchers) {
|
|
77
|
-
try {
|
|
78
|
-
watcher.close()
|
|
79
|
-
} catch {
|
|
80
|
-
/* ignore */
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
watchers.length = 0
|
|
84
|
-
},
|
|
85
|
-
}
|
|
86
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* 技能目录 watcher —— 增量感知,替代「每次请求全量重扫」。
|
|
3
|
+
*
|
|
4
|
+
* 设计取舍:
|
|
5
|
+
* - 不做「精确到文件的 diff」:那需要维护一棵完整树,且 fs.watch 在 Windows 上
|
|
6
|
+
* 对深层目录的事件并不可靠。这里只做**代数(generation)递增**:
|
|
7
|
+
* 任何目录变动 → 代数 +1 → 快照缓存失效。
|
|
8
|
+
* - 事件做 debounce 合并:一次保存会触发多个事件(创建/重命名/写入),
|
|
9
|
+
* 不合并会连打多次重扫。
|
|
10
|
+
* - watcher 失败(目录不存在 / 权限)不致命:降级为「无 watcher」,
|
|
11
|
+
* 此时只靠 TTL 兜底,并把这个降级状态如实报告给调用方。
|
|
12
|
+
*/
|
|
13
|
+
import { watch } from 'node:fs'
|
|
14
|
+
import { existsSync } from 'node:fs'
|
|
15
|
+
|
|
16
|
+
export const DEBOUNCE_MS = 250
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 观察一组根目录。
|
|
20
|
+
* @param {Array<{path:string, source:string}>} roots
|
|
21
|
+
* @param {() => void} onInvalidate 变动时调用(已 debounce)
|
|
22
|
+
* @param {{ debounceMs?: number }} [options]
|
|
23
|
+
* @returns {{ generation: () => number, watching: string[], degraded: Array<{path:string,reason:string}>, close: () => void }}
|
|
24
|
+
*/
|
|
25
|
+
export function watchRoots(roots, onInvalidate, options = {}) {
|
|
26
|
+
const debounceMs = options.debounceMs ?? DEBOUNCE_MS
|
|
27
|
+
|
|
28
|
+
let generation = 0
|
|
29
|
+
let timer = null
|
|
30
|
+
const watchers = []
|
|
31
|
+
const watching = []
|
|
32
|
+
const degraded = []
|
|
33
|
+
|
|
34
|
+
const bump = () => {
|
|
35
|
+
if (timer !== null) clearTimeout(timer)
|
|
36
|
+
timer = setTimeout(() => {
|
|
37
|
+
timer = null
|
|
38
|
+
generation += 1
|
|
39
|
+
try {
|
|
40
|
+
onInvalidate(generation)
|
|
41
|
+
} catch {
|
|
42
|
+
/* 回调失败不应影响 watcher 本身 */
|
|
43
|
+
}
|
|
44
|
+
}, debounceMs)
|
|
45
|
+
// 允许进程退出,不被 timer 挂住
|
|
46
|
+
if (typeof timer.unref === 'function') timer.unref()
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
for (const root of roots) {
|
|
50
|
+
if (!existsSync(root.path)) {
|
|
51
|
+
degraded.push({ path: root.path, reason: 'not-exists' })
|
|
52
|
+
continue
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
const watcher = watch(root.path, { recursive: true, persistent: false }, bump)
|
|
56
|
+
watcher.on('error', error => {
|
|
57
|
+
degraded.push({ path: root.path, reason: String(error?.code ?? error?.message ?? error) })
|
|
58
|
+
})
|
|
59
|
+
watchers.push(watcher)
|
|
60
|
+
watching.push(root.path)
|
|
61
|
+
} catch (error) {
|
|
62
|
+
// recursive 在某些平台/网络盘不支持 → 记录降级,不抛
|
|
63
|
+
degraded.push({ path: root.path, reason: String(error?.code ?? error?.message ?? error) })
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
generation: () => generation,
|
|
69
|
+
watching,
|
|
70
|
+
degraded,
|
|
71
|
+
close() {
|
|
72
|
+
if (timer !== null) {
|
|
73
|
+
clearTimeout(timer)
|
|
74
|
+
timer = null
|
|
75
|
+
}
|
|
76
|
+
for (const watcher of watchers) {
|
|
77
|
+
try {
|
|
78
|
+
watcher.close()
|
|
79
|
+
} catch {
|
|
80
|
+
/* ignore */
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
watchers.length = 0
|
|
84
|
+
},
|
|
85
|
+
}
|
|
86
|
+
}
|