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,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/index.js
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* dsh-skill-harbor — host half
|
|
3
|
-
*
|
|
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 下界依据)
|
|
4
11
|
*/
|
|
12
|
+
import { installProvider } from './provider/lifecycle.js'
|
|
13
|
+
|
|
5
14
|
export const name = 'dsh-skill-harbor'
|
|
6
15
|
|
|
16
|
+
export const inject = ['skills']
|
|
17
|
+
|
|
7
18
|
export function apply(ctx, config = {}) {
|
|
8
|
-
|
|
9
|
-
return {
|
|
10
|
-
name,
|
|
11
|
-
config,
|
|
12
|
-
skills: [],
|
|
13
|
-
}
|
|
19
|
+
installProvider(ctx, config)
|
|
14
20
|
}
|
|
@@ -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
|
+
}
|