@kpeace/dsh-prompt-notes 0.0.0-stage → 0.3.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/CHANGELOG.md +23 -0
- package/LICENSE +21 -0
- package/README.md +82 -2
- package/README.zh.md +83 -0
- package/client.js +1023 -0
- package/cordis.patch.yml +16 -0
- package/docs/DETAIL.md +1 -0
- package/docs/DETAIL.zh.md +1 -0
- package/docs/preview.svg +47 -0
- package/icon.svg +8 -0
- package/index.js +397 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +79 -4
- package/src/mutations.js +81 -0
- package/src/notes.js +399 -0
- package/src/routes.js +208 -0
- package/src/share.js +231 -0
- package/src/store.js +214 -0
- package/src/system.js +123 -0
- package/src/tools.js +244 -0
package/cordis.patch.yml
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# dsh-prompt-notes:把「提示词便签」接进 profile 的层栈。
|
|
2
|
+
#
|
|
3
|
+
# host 半体:把已启用的便签注入 system prompt(一个 interpolate:false 的段)+ /dsh-prompt-notes/ 路由
|
|
4
|
+
# client 半体:在右侧栏注册「便签」页签(依赖 dsh-better-sidebar 提供的 registerTab)
|
|
5
|
+
#
|
|
6
|
+
# 默认没有便签 → 注入段为空 → 装上不改变任何请求。可选配置:
|
|
7
|
+
# maxChars: 8000 # 每个会话里提示词便签的字数预算;0 = 不限
|
|
8
|
+
# order: 100 # system prompt 段位置(0=persona,500=plan policy)
|
|
9
|
+
# file: ~ # 数据文件,默认 $DSH_HOME/prompt-notes/notes.json
|
|
10
|
+
# mcp: true # false = 不登记任何 MCP 便签(MCP 会启动本地进程)
|
|
11
|
+
# pollMs: 2000 # 探测数据文件被外部改动的间隔(毫秒)
|
|
12
|
+
# enabled: true # false = 整个插件休眠
|
|
13
|
+
- insert:
|
|
14
|
+
- id: prompt-notes
|
|
15
|
+
name: "@kpeace/dsh-prompt-notes"
|
|
16
|
+
config: {}
|
package/docs/DETAIL.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+

|
|
@@ -0,0 +1 @@
|
|
|
1
|
+

|
package/docs/preview.svg
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="640" height="300" viewBox="0 0 640 300">
|
|
2
|
+
<defs>
|
|
3
|
+
<linearGradient id="pad" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="#fffdf0"/><stop offset="1" stop-color="#fbf3d4"/></linearGradient>
|
|
4
|
+
<linearGradient id="y" x1="0" y1="0" x2="1" y2="1"><stop offset="0" stop-color="#fff6b0"/><stop offset="1" stop-color="#ffe985"/></linearGradient>
|
|
5
|
+
<linearGradient id="b" x1="0" y1="0" x2="1" y2="1"><stop offset="0" stop-color="#d7ecff"/><stop offset="1" stop-color="#b6dbff"/></linearGradient>
|
|
6
|
+
<linearGradient id="g" x1="0" y1="0" x2="1" y2="1"><stop offset="0" stop-color="#dcf5d0"/><stop offset="1" stop-color="#bfe8ae"/></linearGradient>
|
|
7
|
+
<radialGradient id="pin" cx=".34" cy=".28" r=".8"><stop offset="0" stop-color="#ffc0b8"/><stop offset=".14" stop-color="#ffc0b8"/><stop offset=".4" stop-color="#e8493d"/><stop offset="1" stop-color="#8e1e16"/></radialGradient>
|
|
8
|
+
<filter id="sh" x="-5%" y="-5%" width="110%" height="120%"><feDropShadow dx="0" dy="2" stdDeviation="1.6" flood-color="#000" flood-opacity=".28"/></filter>
|
|
9
|
+
</defs>
|
|
10
|
+
<rect width="640" height="300" fill="#1f2430"/>
|
|
11
|
+
<g transform="translate(150 14)">
|
|
12
|
+
<!-- 索引标签:平色,当前页略高 -->
|
|
13
|
+
<rect x="10" y="2" width="50" height="22" rx="6" fill="#fdf8e2" stroke="#c9b977"/>
|
|
14
|
+
<rect x="64" y="5" width="44" height="19" rx="6" fill="#e6dcb4" stroke="#c9b977"/>
|
|
15
|
+
<rect x="112" y="5" width="52" height="19" rx="6" fill="#e6dcb4" stroke="#c9b977"/>
|
|
16
|
+
<rect x="168" y="5" width="44" height="19" rx="6" fill="#e6dcb4" stroke="#c9b977"/>
|
|
17
|
+
<rect x="216" y="5" width="40" height="19" rx="6" fill="#e0dfd6" stroke="#c9b977"/>
|
|
18
|
+
<rect x="0" y="24" width="340" height="262" rx="5" fill="url(#pad)" stroke="#c9b977"/>
|
|
19
|
+
<!-- 标题、总开关、字数量尺 -->
|
|
20
|
+
<rect x="14" y="36" width="64" height="16" fill="#ffcf5a" opacity=".55"/>
|
|
21
|
+
<rect x="270" y="34" width="52" height="20" rx="5" fill="#e4dfcb" stroke="#6d6753"/>
|
|
22
|
+
<rect x="300" y="36" width="20" height="16" rx="3" fill="#fafaf5"/>
|
|
23
|
+
<rect x="14" y="62" width="312" height="9" rx="2" fill="#fff9d6" stroke="#8d7c3c"/>
|
|
24
|
+
<rect x="14" y="62" width="138" height="9" rx="2" fill="#ffc400" opacity=".55"/>
|
|
25
|
+
<!-- 分组条:素色左标 -->
|
|
26
|
+
<rect x="12" y="82" width="52" height="18" rx="0" fill="#3c2d0a" opacity=".07"/><rect x="12" y="82" width="3" height="18" fill="#3f8a4f"/>
|
|
27
|
+
<rect x="22" y="88" width="30" height="6" rx="3" fill="#2f2410" opacity=".7"/>
|
|
28
|
+
<!-- 提示词(黄) -->
|
|
29
|
+
<g filter="url(#sh)"><rect x="14" y="106" width="312" height="54" fill="url(#y)"/></g>
|
|
30
|
+
<rect x="22" y="114" width="28" height="13" rx="2" fill="#2d2f33"/><rect x="56" y="116" width="92" height="9" rx="4" fill="#2f2410"/>
|
|
31
|
+
<rect x="22" y="134" width="214" height="6" rx="3" fill="#2f2410" opacity=".55"/><rect x="22" y="145" width="140" height="6" rx="3" fill="#2f2410" opacity=".55"/>
|
|
32
|
+
<rect x="262" y="140" width="46" height="13" rx="3" fill="none" stroke="#9a2820" stroke-width="1.4"/>
|
|
33
|
+
<circle cx="302" cy="106" r="8" fill="url(#pin)"/>
|
|
34
|
+
<!-- 技能(蓝) -->
|
|
35
|
+
<g filter="url(#sh)"><rect x="14" y="170" width="312" height="54" fill="url(#b)"/></g>
|
|
36
|
+
<rect x="22" y="178" width="28" height="13" rx="2" fill="#1f4f86"/><rect x="56" y="180" width="78" height="9" rx="4" fill="#2f2410"/>
|
|
37
|
+
<rect x="22" y="198" width="230" height="6" rx="3" fill="#2f4a66" opacity=".6"/><rect x="22" y="209" width="120" height="6" rx="3" fill="#2f4a66" opacity=".6"/>
|
|
38
|
+
<rect x="262" y="204" width="46" height="13" rx="3" fill="none" stroke="#14487f" stroke-width="1.4"/>
|
|
39
|
+
<circle cx="302" cy="170" r="8" fill="url(#pin)"/>
|
|
40
|
+
<!-- MCP(绿)+ 连接指示灯 -->
|
|
41
|
+
<g filter="url(#sh)"><rect x="14" y="234" width="312" height="40" fill="url(#g)"/></g>
|
|
42
|
+
<rect x="22" y="242" width="28" height="13" rx="2" fill="#1f6b3a"/><rect x="56" y="244" width="40" height="9" rx="4" fill="#2f2410"/>
|
|
43
|
+
<rect x="22" y="260" width="130" height="6" rx="3" fill="#2f4a2a" opacity=".6"/>
|
|
44
|
+
<circle cx="270" cy="263" r="4" fill="#2fbf57"/>
|
|
45
|
+
<circle cx="302" cy="234" r="8" fill="url(#pin)"/>
|
|
46
|
+
</g>
|
|
47
|
+
</svg>
|
package/icon.svg
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="36" height="36" viewBox="0 0 36 36">
|
|
2
|
+
<rect x="6" y="5" width="24" height="26" rx="4" fill="#f5c542"/>
|
|
3
|
+
<path d="M22 31l8-8v4a4 4 0 0 1-4 4z" fill="#d9a521"/>
|
|
4
|
+
<rect x="11" y="12" width="14" height="2.4" rx="1.2" fill="#6b4e00"/>
|
|
5
|
+
<rect x="11" y="17.5" width="14" height="2.4" rx="1.2" fill="#6b4e00"/>
|
|
6
|
+
<rect x="11" y="23" width="8" height="2.4" rx="1.2" fill="#6b4e00"/>
|
|
7
|
+
<circle cx="26" cy="9" r="3" fill="#3fb27f"/>
|
|
8
|
+
</svg>
|
package/index.js
ADDED
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-prompt-notes —— host 半体。
|
|
3
|
+
*
|
|
4
|
+
* 把用户的「便签」按作用域送进系统的三个入口:
|
|
5
|
+
* prompt → system prompt 的一个段(按当前会话/工作目录过滤)
|
|
6
|
+
* skill → skill 注册表的一个提供方(全局/工作区走全局提供方,会话级走每个 agent 自己的提供方)
|
|
7
|
+
* mcp → mcpManager.register 登记服务器;工具调用按作用域经 tools.guard 拦截
|
|
8
|
+
* 另外开一组 HTTP 路由给右侧栏的便签面板读写,并读出系统自带的注入内容供面板展示。
|
|
9
|
+
*
|
|
10
|
+
* 默认没有便签 → 三条通道都是空的 → 装上这个插件不会改变任何一次请求。
|
|
11
|
+
*
|
|
12
|
+
* 配置(cordis.patch.yml 的 config,均可省略):
|
|
13
|
+
* enabled boolean false 时整个插件休眠。默认 true
|
|
14
|
+
* maxChars number 每个会话里 prompt 便签合计的字符预算;0 = 不限。默认 8000
|
|
15
|
+
* order number system prompt 段位置(0=persona,500=plan policy)。默认 100
|
|
16
|
+
* file string 数据文件路径。默认 $DSH_HOME/prompt-notes/notes.json
|
|
17
|
+
* mcp boolean false 时不登记 MCP 便签(MCP 会启动本地进程,想彻底关掉就设 false)。默认 true
|
|
18
|
+
* pollMs number 探测数据文件被外部改动的间隔(毫秒,≥10)。默认 2000
|
|
19
|
+
*/
|
|
20
|
+
import {
|
|
21
|
+
DEFAULT_MAX_CHARS, appliesTo, effectiveNotes, mcpEntry, pathWithin, planInjection, renderSection,
|
|
22
|
+
skillCandidate, skillDefinition,
|
|
23
|
+
} from './src/notes.js'
|
|
24
|
+
import { NoteStore, resolveDataFile } from './src/store.js'
|
|
25
|
+
import { ROUTE_PREFIX, createHandler } from './src/routes.js'
|
|
26
|
+
import { collectSystem, liveSessionIds, resolveViewer } from './src/system.js'
|
|
27
|
+
import { registerNoteTools } from './src/tools.js'
|
|
28
|
+
|
|
29
|
+
export const name = 'prompt-notes'
|
|
30
|
+
export const inject = ['systemPrompt']
|
|
31
|
+
|
|
32
|
+
const SECTION_NAME = 'prompt-notes:injected'
|
|
33
|
+
const PROVIDER = 'prompt-notes'
|
|
34
|
+
|
|
35
|
+
function warn(...args) {
|
|
36
|
+
try { console.warn('[prompt-notes]', ...args) } catch { /* ignore */ }
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 从「服务持有者」取 cwd。
|
|
41
|
+
*
|
|
42
|
+
* 为什么不直接 ctx.workingDirectory:Cordis 要求访问 ctx.<服务> 时当前 fiber 链上声明过该依赖,
|
|
43
|
+
* 否则抛 "cannot get property … without inject"。可选依赖的惯用法是嵌套的 ctx.inject([...], cb),
|
|
44
|
+
* 在回调里拿到服务并存进 svc(见 apply 里的 want)。曾经直接在外层 ctx 上取、再把异常吞掉,
|
|
45
|
+
* 结果在真实系统里 cwd 永远是 undefined,工作区作用域悄悄失效——所以这里不吞异常,只在服务缺席时返回空。
|
|
46
|
+
*/
|
|
47
|
+
function viewerOfAgent(svc, agent) {
|
|
48
|
+
if (!agent) return {}
|
|
49
|
+
const wd = svc.workingDirectory
|
|
50
|
+
return { sessionId: agent.id, cwd: wd?.get ? wd.get(agent.session) : undefined }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function apply(ctx, config = {}) {
|
|
54
|
+
const cfg = config && typeof config === 'object' ? config : {}
|
|
55
|
+
if (cfg.enabled === false) return
|
|
56
|
+
|
|
57
|
+
const maxChars = Number.isFinite(cfg.maxChars) && cfg.maxChars >= 0 ? cfg.maxChars : DEFAULT_MAX_CHARS
|
|
58
|
+
const order = Number.isFinite(cfg.order) ? cfg.order : 100
|
|
59
|
+
const allowMcp = cfg.mcp !== false
|
|
60
|
+
const pollMs = Number.isFinite(cfg.pollMs) && cfg.pollMs >= 10 ? cfg.pollMs : 2000
|
|
61
|
+
const file = resolveDataFile(cfg.file)
|
|
62
|
+
const store = new NoteStore(file, warn)
|
|
63
|
+
|
|
64
|
+
// 可选依赖的持有者:服务就绪时填入,卸载时清空。systemPrompt 是必需依赖,直接可用。
|
|
65
|
+
const svc = { systemPrompt: ctx.systemPrompt }
|
|
66
|
+
const want = (name) => {
|
|
67
|
+
try {
|
|
68
|
+
ctx.inject([name], (c) => {
|
|
69
|
+
svc[name] = c[name]
|
|
70
|
+
c.effect(() => () => { if (svc[name] === c[name]) delete svc[name] })
|
|
71
|
+
})
|
|
72
|
+
} catch (error) {
|
|
73
|
+
warn(`${name} unavailable:`, String(error))
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
want('agents')
|
|
77
|
+
want('workingDirectory')
|
|
78
|
+
|
|
79
|
+
// ───────── prompt ─────────
|
|
80
|
+
// interpolate:false —— 便签是用户写的任意文本,里面出现 `{{` 不能让整次模型调用因
|
|
81
|
+
// 「unknown prompt variable」而失败,也不该把 `{{cwd}}` 之类悄悄替换掉用户写的字面量。
|
|
82
|
+
ctx.systemPrompt.section({
|
|
83
|
+
name: SECTION_NAME,
|
|
84
|
+
order,
|
|
85
|
+
interpolate: false,
|
|
86
|
+
text: (context) => {
|
|
87
|
+
try {
|
|
88
|
+
const doc = store.snapshot()
|
|
89
|
+
const viewer = viewerOfAgent(svc, context?.agent)
|
|
90
|
+
return renderSection(planInjection(doc.notes, maxChars, { paused: doc.paused, viewer }).injected)
|
|
91
|
+
} catch (error) {
|
|
92
|
+
// 组装 system prompt 绝不能因为便签文件出问题而失败。
|
|
93
|
+
warn('failed to render notes section:', String(error))
|
|
94
|
+
return ''
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
// ───────── skill ─────────
|
|
100
|
+
const invalidators = new Set()
|
|
101
|
+
const invalidateSkills = () => { for (const fn of invalidators) { try { fn() } catch { /* 已卸载的提供方 */ } } }
|
|
102
|
+
/**
|
|
103
|
+
* @param viewerOf 由提供方收到的查询选项算出观察者。提供方这一层只拿得到 cwd,拿不到会话 id,
|
|
104
|
+
* 所以会话级便签必须由「挂在该 agent.ctx 上」的提供方负责——它在创建时就知道自己是哪个会话。
|
|
105
|
+
*/
|
|
106
|
+
const skillProvider = (match, viewerOf) => (control) => {
|
|
107
|
+
invalidators.add(control.invalidate)
|
|
108
|
+
control.signal.addEventListener('abort', () => invalidators.delete(control.invalidate), { once: true })
|
|
109
|
+
const pick = (options) => {
|
|
110
|
+
const doc = store.snapshot()
|
|
111
|
+
return effectiveNotes(doc.notes, 'skill', viewerOf(options), doc.paused).filter(match)
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
name: PROVIDER,
|
|
115
|
+
list: async (options) => ({ candidates: pick(options).map(skillCandidate), complete: true }),
|
|
116
|
+
get: async (candidate, options) => {
|
|
117
|
+
const note = pick(options).find((n) => n.id === candidate.locator)
|
|
118
|
+
return note ? skillDefinition(note) : undefined
|
|
119
|
+
},
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
try {
|
|
124
|
+
ctx.inject(['skills'], (c) => {
|
|
125
|
+
const skills = c.skills
|
|
126
|
+
svc.skills = skills
|
|
127
|
+
c.effect(() => () => { delete svc.skills })
|
|
128
|
+
if (!skills?.registerProvider) return
|
|
129
|
+
// 全局提供方:global 与 workspace 便签(workspace 靠调用方 cwd 过滤)
|
|
130
|
+
const dispose = skills.registerProvider(skillProvider((n) => n.scope !== 'session', (o) => ({ cwd: o.cwd })))
|
|
131
|
+
c.effect(() => dispose)
|
|
132
|
+
|
|
133
|
+
// 每个会话一个提供方:只放 session 级便签。注册在 agent.ctx 上,随该 agent 一起消亡。
|
|
134
|
+
// TODO(prompt-notes): 提供方随 agent.ctx 销毁时会 abort 并从 invalidators 摘掉;
|
|
135
|
+
// 若发现长跑进程里仍有累积,可在 agent 销毁事件上主动回收。
|
|
136
|
+
const attach = (agent) => {
|
|
137
|
+
// agent.ctx 是另一棵树,没声明依赖就取不到 skills("cannot get property skills without inject")。
|
|
138
|
+
// 所以在 agent.ctx 上再嵌一层 inject:回调里的 actx 既带 skills,又带该 agent 的作用域,
|
|
139
|
+
// 注册进去的提供方只对这个会话可见,并随 agent 一起消亡。
|
|
140
|
+
try {
|
|
141
|
+
agent.ctx.inject(['skills'], (actx) => {
|
|
142
|
+
const undo = actx.skills.registerProvider(
|
|
143
|
+
skillProvider((n) => n.scope === 'session' && n.target === agent.id, () => ({ sessionId: agent.id })))
|
|
144
|
+
actx.effect(() => undo)
|
|
145
|
+
})
|
|
146
|
+
} catch (error) {
|
|
147
|
+
warn(`could not attach session skill provider for ${agent.id}:`, String(error))
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
c.on('agent/created', ({ agent }) => { attach(agent) })
|
|
151
|
+
for (const a of svc.agents?.list?.() ?? []) attach(a)
|
|
152
|
+
})
|
|
153
|
+
} catch (error) {
|
|
154
|
+
warn('skills service unavailable, skill notes are inactive:', String(error))
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// ───────── mcp ─────────
|
|
158
|
+
/** MCP 登记失败的原因,按名字索引,给面板显示。 */
|
|
159
|
+
const mcpFailures = new Map()
|
|
160
|
+
const registered = new Map() // name → { sig, dispose }
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* 这张 MCP 便签现在该不该真的起一个进程。
|
|
164
|
+
*
|
|
165
|
+
* MCP 进程是全局的(连上之后工具会挂到所有会话上),但「全局」不等于「无条件」:
|
|
166
|
+
* 一张钉在某个会话 / 某个目录上的便签,在没有适用的活动会话时启动一个子进程是纯浪费——
|
|
167
|
+
* 用户看不到它(工具只对适用的会话有意义,调用还会被下面的 guard 拒掉),却要为它付内存与启动时间。
|
|
168
|
+
*
|
|
169
|
+
* 名字与便签是 1:1(路由层的 conflict 保证全库 MCP 名唯一),所以这里不需要引用计数:
|
|
170
|
+
* 「该不该活」完全由那一张便签的 scope 决定。
|
|
171
|
+
*
|
|
172
|
+
* 服务缺席时(只可能出现在单测的假 ctx 里)返回 true:保留 v0.2 的「总是登记」,
|
|
173
|
+
* 宁可多起一个进程,也不要让用户在能力齐全的环境里莫名其妙看不到自己的 MCP。
|
|
174
|
+
*/
|
|
175
|
+
const mcpWanted = (note) => {
|
|
176
|
+
if (note.scope === 'global') return true
|
|
177
|
+
const listed = svc.agents?.list?.()
|
|
178
|
+
if (!Array.isArray(listed)) return true
|
|
179
|
+
if (note.scope === 'session') return listed.some((a) => a.id === note.target)
|
|
180
|
+
const wd = svc.workingDirectory
|
|
181
|
+
if (typeof wd?.get !== 'function') return true
|
|
182
|
+
return listed.some((a) => {
|
|
183
|
+
try { return pathWithin(wd.get(a.session), note.target) } catch { return false }
|
|
184
|
+
})
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const reconcileMcp = (manager) => {
|
|
188
|
+
const doc = store.snapshot()
|
|
189
|
+
// MCP 进程是全局的(工具挂到所有会话上),作用域靠 tools.guard 在调用时拦截;
|
|
190
|
+
// 「什么时候该起进程」则由 mcpWanted 决定:没有适用的活动会话就不登记。
|
|
191
|
+
const wanted = new Map()
|
|
192
|
+
if (allowMcp && !doc.paused) {
|
|
193
|
+
for (const n of doc.notes) if (n.kind === 'mcp' && n.enabled && mcpWanted(n)) wanted.set(n.mcp.name, n)
|
|
194
|
+
}
|
|
195
|
+
for (const [name, rec] of registered) {
|
|
196
|
+
const note = wanted.get(name)
|
|
197
|
+
if (!note || JSON.stringify(mcpEntry(note)) !== rec.sig) {
|
|
198
|
+
try { rec.dispose() } catch (error) { warn(`failed to unregister mcp "${name}":`, String(error)) }
|
|
199
|
+
registered.delete(name)
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
for (const [name, note] of wanted) {
|
|
203
|
+
if (registered.has(name)) continue
|
|
204
|
+
const entry = mcpEntry(note)
|
|
205
|
+
try {
|
|
206
|
+
const dispose = manager.register(name, entry)
|
|
207
|
+
registered.set(name, { sig: JSON.stringify(entry), dispose })
|
|
208
|
+
mcpFailures.delete(name)
|
|
209
|
+
} catch (error) {
|
|
210
|
+
mcpFailures.set(name, String(error?.message ?? error))
|
|
211
|
+
warn(`could not register mcp "${name}":`, mcpFailures.get(name))
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
for (const name of [...mcpFailures.keys()]) if (!wanted.has(name)) mcpFailures.delete(name)
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
let mcpManager
|
|
218
|
+
try {
|
|
219
|
+
ctx.inject(['mcpManager'], (c) => {
|
|
220
|
+
mcpManager = c.mcpManager
|
|
221
|
+
svc.mcpManager = mcpManager
|
|
222
|
+
if (!mcpManager?.register) { mcpManager = undefined; return }
|
|
223
|
+
reconcileMcp(mcpManager)
|
|
224
|
+
c.effect(() => () => {
|
|
225
|
+
for (const rec of registered.values()) { try { rec.dispose() } catch { /* 随插件卸载 */ } }
|
|
226
|
+
registered.clear()
|
|
227
|
+
mcpManager = undefined
|
|
228
|
+
delete svc.mcpManager
|
|
229
|
+
})
|
|
230
|
+
})
|
|
231
|
+
} catch (error) {
|
|
232
|
+
warn('mcpManager unavailable, mcp notes are inactive:', String(error))
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** 这个工具名属于哪一张 MCP 便签(最长前缀优先:wsonly_x 不会被 wsonly 抢走)。 */
|
|
236
|
+
const mcpOwnerOf = (notes, toolName) => {
|
|
237
|
+
let owner
|
|
238
|
+
for (const n of notes) {
|
|
239
|
+
if (n.kind !== 'mcp' || !n.mcp?.name) continue
|
|
240
|
+
if (!toolName.startsWith(`mcp__${n.mcp.name}__`)) continue
|
|
241
|
+
if (!owner || n.mcp.name.length > owner.mcp.name.length) owner = n
|
|
242
|
+
}
|
|
243
|
+
return owner
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
try {
|
|
247
|
+
ctx.inject(['tools'], (c) => {
|
|
248
|
+
svc.tools = c.tools
|
|
249
|
+
c.effect(() => () => { delete svc.tools })
|
|
250
|
+
// 助手工具:让模型也能读写便签(MCP 会被强制未钉住,见 src/tools.js 顶部的边界说明)
|
|
251
|
+
try {
|
|
252
|
+
const disposeNoteTools = registerNoteTools(c.tools, { store, viewerOf: (agent) => viewerOfAgent(svc, agent) })
|
|
253
|
+
c.effect(() => disposeNoteTools)
|
|
254
|
+
} catch (error) {
|
|
255
|
+
warn('note tools failed to register:', String(error))
|
|
256
|
+
}
|
|
257
|
+
if (!c.tools?.guard) return
|
|
258
|
+
// 兜底拦截:万一工具还在列表里(restrict 没跟上),调用这一刻也要按作用域放行/拒绝。
|
|
259
|
+
const dispose = c.tools.guard((exec) => {
|
|
260
|
+
if (typeof exec?.name !== 'string' || !exec.name.startsWith('mcp__')) return undefined
|
|
261
|
+
const owner = mcpOwnerOf(store.snapshot().notes, exec.name)
|
|
262
|
+
if (!owner || owner.scope === 'global') return undefined
|
|
263
|
+
if (appliesTo(owner, viewerOfAgent(svc, exec.agent))) return undefined
|
|
264
|
+
const where = owner.scope === 'session' ? 'another session' : `the workspace ${owner.target}`
|
|
265
|
+
return `The MCP server "${owner.mcp.name}" is limited to ${where} by the user's prompt notes; it is not available here.`
|
|
266
|
+
})
|
|
267
|
+
c.effect(() => dispose)
|
|
268
|
+
})
|
|
269
|
+
} catch (error) {
|
|
270
|
+
warn('tools service unavailable, mcp scope guard is inactive:', String(error))
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// ───────── 把不适用的 MCP 工具从会话里藏起来 ─────────
|
|
274
|
+
/**
|
|
275
|
+
* 为什么要等「连接成功」之后才能做:`tools.restrict({deny})` 只接受**此刻已经存在的全局工具名**,
|
|
276
|
+
* 而 MCP 的工具名要等服务器连上、把 schema 注册进来才知道。提前调只会抛
|
|
277
|
+
* "names unknown global tool"。
|
|
278
|
+
* 为什么只能在 `agent.ctx` 上做:restrict 的语义是「这一个 agent 继承到的全局工具」,
|
|
279
|
+
* 在根 ctx 上调会被直接拒绝(那会把所有 agent 的工具一起遮掉)。
|
|
280
|
+
*
|
|
281
|
+
* 于是每个 tick 对齐一次:读出每个活动 agent 现在看得到的 `mcp__*`,
|
|
282
|
+
* 按便签作用域算出「不该给它看的」,用 restrict 藏起来;只在 deny 集合变化时动注册表。
|
|
283
|
+
*/
|
|
284
|
+
const restricted = new Map() // agentId → { sig, lift }
|
|
285
|
+
const restrictFailures = new Set()
|
|
286
|
+
const applyMcpRestrictions = () => {
|
|
287
|
+
const tools = svc.tools
|
|
288
|
+
if (!tools?.definitions || !tools?.restrict) return
|
|
289
|
+
const listed = svc.agents?.list?.()
|
|
290
|
+
if (!Array.isArray(listed)) return
|
|
291
|
+
const notes = store.snapshot().notes
|
|
292
|
+
const liveIds = new Set()
|
|
293
|
+
for (const agent of listed) {
|
|
294
|
+
liveIds.add(agent.id)
|
|
295
|
+
let names
|
|
296
|
+
try { names = tools.definitions(agent).map((d) => d.name) } catch { continue }
|
|
297
|
+
const viewer = viewerOfAgent(svc, agent)
|
|
298
|
+
const deny = names.filter((name) => {
|
|
299
|
+
if (!name.startsWith('mcp__')) return false
|
|
300
|
+
const owner = mcpOwnerOf(notes, name)
|
|
301
|
+
return Boolean(owner) && owner.scope !== 'global' && !appliesTo(owner, viewer)
|
|
302
|
+
})
|
|
303
|
+
const sig = deny.slice().sort().join(',')
|
|
304
|
+
const rec = restricted.get(agent.id)
|
|
305
|
+
if (rec && rec.sig === sig) continue
|
|
306
|
+
if (rec) { try { rec.lift() } catch { /* agent 已经消失 */ } restricted.delete(agent.id) }
|
|
307
|
+
if (!deny.length) continue
|
|
308
|
+
try {
|
|
309
|
+
agent.ctx.inject(['tools'], (t) => {
|
|
310
|
+
try {
|
|
311
|
+
const lift = t.tools.restrict({ deny })
|
|
312
|
+
t.effect(() => () => { try { lift() } catch { /* 随 agent 卸载 */ } })
|
|
313
|
+
restricted.set(agent.id, { sig, lift })
|
|
314
|
+
} catch (error) {
|
|
315
|
+
// 失败不记账(下一 tick 还会重试),但同一种失败只 warn 一次,别把日志刷满
|
|
316
|
+
const key = `${agent.id}|${sig}`
|
|
317
|
+
if (!restrictFailures.has(key)) { restrictFailures.add(key); warn(`could not hide mcp tools from ${agent.id}:`, String(error)) }
|
|
318
|
+
}
|
|
319
|
+
})
|
|
320
|
+
} catch (error) {
|
|
321
|
+
warn(`could not attach mcp restrictions for ${agent.id}:`, String(error))
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
for (const [id, rec] of restricted) if (!liveIds.has(id)) { try { rec.lift() } catch { /* 已随 agent 卸载 */ } restricted.delete(id) }
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// ───────── 变更联动 ─────────
|
|
328
|
+
/**
|
|
329
|
+
* 便签没变、但「活着的会话」变了:一张钉在会话 A 上的 MCP,A 一关就该停掉子进程;
|
|
330
|
+
* A 再打开(新建或恢复会话)又该起回来。list() 很便宜,用「id:cwd 拼起来的签名」判断
|
|
331
|
+
* 有没有变化,变了才重算,免得每 2 秒都把注册表搅一遍(重试失败的注册还会刷日志)。
|
|
332
|
+
*/
|
|
333
|
+
let liveSig = null
|
|
334
|
+
const reconcileIfLiveChanged = () => {
|
|
335
|
+
if (!mcpManager) return
|
|
336
|
+
const listed = svc.agents?.list?.()
|
|
337
|
+
if (!Array.isArray(listed)) return
|
|
338
|
+
const wd = svc.workingDirectory
|
|
339
|
+
const sig = listed.map((a) => `${a.id}:${wd?.get?.(a.session) ?? ''}`).join('|')
|
|
340
|
+
if (sig === liveSig) return
|
|
341
|
+
liveSig = sig
|
|
342
|
+
reconcileMcp(mcpManager)
|
|
343
|
+
}
|
|
344
|
+
try {
|
|
345
|
+
ctx.on('agent/created', () => reconcileIfLiveChanged())
|
|
346
|
+
} catch (error) {
|
|
347
|
+
warn('agent/created listener unavailable:', String(error))
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// 外部手改数据文件也要生效:定时探测一次(只是一次 stat),变化时重登记 MCP、刷新 skill 目录。
|
|
351
|
+
// 同一个 tick 里顺带看一眼活着的会话(关掉会话 → 停掉它的 MCP 子进程):不新增定时器。
|
|
352
|
+
const unsubscribe = store.subscribe(() => {
|
|
353
|
+
invalidateSkills()
|
|
354
|
+
if (mcpManager) reconcileMcp(mcpManager)
|
|
355
|
+
try { applyMcpRestrictions() } catch (error) { warn('mcp restrict failed:', String(error)) }
|
|
356
|
+
})
|
|
357
|
+
const timer = setInterval(() => {
|
|
358
|
+
try { store.poll() } catch (error) { warn('poll failed:', String(error)) }
|
|
359
|
+
try { reconcileIfLiveChanged() } catch (error) { warn('mcp reconcile failed:', String(error)) }
|
|
360
|
+
try { applyMcpRestrictions() } catch (error) { warn('mcp restrict failed:', String(error)) }
|
|
361
|
+
}, pollMs)
|
|
362
|
+
timer.unref?.()
|
|
363
|
+
ctx.effect(() => () => { clearInterval(timer); unsubscribe() })
|
|
364
|
+
|
|
365
|
+
// ───────── 面板路由 ─────────
|
|
366
|
+
const hostApi = {
|
|
367
|
+
resolveViewer: (query) => resolveViewer(svc, query).viewer,
|
|
368
|
+
liveSessionIds: () => liveSessionIds(svc),
|
|
369
|
+
system: (query) => collectSystem(svc, query, { selfSection: SECTION_NAME }),
|
|
370
|
+
mcpRuntime: () => {
|
|
371
|
+
const out = {}
|
|
372
|
+
try {
|
|
373
|
+
for (const v of mcpManager?.list?.() ?? []) out[v.name] = { state: v.status?.state ?? 'unknown', error: v.status?.lastError }
|
|
374
|
+
} catch { /* 状态读不到就不显示 */ }
|
|
375
|
+
for (const [n, message] of mcpFailures) out[n] = { state: 'failed', error: message }
|
|
376
|
+
return out
|
|
377
|
+
},
|
|
378
|
+
mcpAllowed: allowMcp,
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
try {
|
|
382
|
+
ctx.inject(['webServer'], (c) => {
|
|
383
|
+
const webServer = c.webServer
|
|
384
|
+
if (!webServer || typeof webServer.register !== 'function') return
|
|
385
|
+
const dispose = webServer.register({
|
|
386
|
+
kind: 'prefix',
|
|
387
|
+
// 不带尾斜杠:webServer 的前缀按路径段匹配(spellbook / cove-task 都是 '/dsh-xxx'),
|
|
388
|
+
// 注册成 '/dsh-prompt-notes/' 会让 '/dsh-prompt-notes/state' 匹配不上而 404。
|
|
389
|
+
path: ROUTE_PREFIX.replace(/\/$/, ''),
|
|
390
|
+
handler: createHandler({ store, maxChars, order, file, host: hostApi }),
|
|
391
|
+
})
|
|
392
|
+
c.effect(() => dispose)
|
|
393
|
+
})
|
|
394
|
+
} catch (error) {
|
|
395
|
+
warn('webServer unavailable, panel routes not registered:', String(error))
|
|
396
|
+
}
|
|
397
|
+
}
|
package/locale/en.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "Prompt notes",
|
|
4
|
+
"description": "A right-sidebar \"Notes\" tab: a pad of paper notes for standing prompts, skills and MCP servers. A push pin turns each one on; choose its scope — every session, one workspace, or a single session. The System tab shows what DSH already injects. Notes can be imported and exported, and the assistant can manage them through tools. Needs dsh-better-sidebar. Prompt notes add tokens to every request; a pinned MCP note runs a local command, so pin only what you trust. Restart DSH after updating.",
|
|
5
|
+
"detail": "docs/DETAIL.md"
|
|
6
|
+
}
|
|
7
|
+
}
|
package/locale/zh.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "便签",
|
|
4
|
+
"description": "右侧栏「便签」页签:一本便签簿,管理常驻提示词、技能和 MCP。图钉决定是否生效,每张可选作用域——所有会话、某个工作区或单个会话。「系统」页展示 DSH 已有的注入内容。支持导入导出,助手也能通过工具管理便签。依赖 dsh-better-sidebar。提示词便签会增加每次请求的 token;钉住的 MCP 便签会在本机运行命令,只钉住你信任的。更新后需重启 DSH。",
|
|
5
|
+
"detail": "docs/DETAIL.zh.md"
|
|
6
|
+
}
|
|
7
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,81 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kpeace/dsh-prompt-notes",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "DSH 插件:便签 —— 右侧栏一本横向拟物便签簿,管理常驻提示词、技能与 MCP,按全局 / 工作区 / 单个会话控制注入范围,并读出系统自带的注入内容。",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "kp-z",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"deepseek",
|
|
10
|
+
"dsh",
|
|
11
|
+
"dsh-better-sidebar",
|
|
12
|
+
"dsh-plugin",
|
|
13
|
+
"harness",
|
|
14
|
+
"mcp",
|
|
15
|
+
"prompt",
|
|
16
|
+
"scope",
|
|
17
|
+
"sidebar",
|
|
18
|
+
"skill",
|
|
19
|
+
"sticky-notes",
|
|
20
|
+
"workspace"
|
|
21
|
+
],
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://github.com/kp-z/dsh-plugins.git",
|
|
25
|
+
"directory": "packages/dsh-prompt-notes"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://github.com/kp-z/dsh-plugins/tree/main/packages/dsh-prompt-notes#readme",
|
|
28
|
+
"main": "./index.js",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": "./index.js",
|
|
31
|
+
"./client": {
|
|
32
|
+
"default": "./client.js"
|
|
33
|
+
},
|
|
34
|
+
"./cordis.patch.yml": "./cordis.patch.yml",
|
|
35
|
+
"./package.json": "./package.json",
|
|
36
|
+
"./locale/*.json": "./locale/*.json",
|
|
37
|
+
"./icon": "./icon.svg"
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"index.js",
|
|
41
|
+
"client.js",
|
|
42
|
+
"src",
|
|
43
|
+
"cordis.patch.yml",
|
|
44
|
+
"locale/*.json",
|
|
45
|
+
"docs",
|
|
46
|
+
"icon.svg",
|
|
47
|
+
"README.md",
|
|
48
|
+
"README.zh.md",
|
|
49
|
+
"CHANGELOG.md",
|
|
50
|
+
"LICENSE"
|
|
51
|
+
],
|
|
52
|
+
"scripts": {
|
|
53
|
+
"test": "node --test test/",
|
|
54
|
+
"check": "node --check index.js && node --check client.js && for f in src/*.js test/e2e/*.mjs test/fixtures/*.mjs demo/*.js demo/*.mjs; do node --check \"$f\" || exit 1; done",
|
|
55
|
+
"e2e": "node test/e2e/smoke.mjs",
|
|
56
|
+
"demo": "node demo/build.mjs",
|
|
57
|
+
"e2e:scope": "node test/e2e/scope.mjs",
|
|
58
|
+
"e2e:share": "node test/e2e/share.mjs",
|
|
59
|
+
"e2e:robust": "node test/e2e/robust.mjs"
|
|
60
|
+
},
|
|
61
|
+
"dsh": {
|
|
62
|
+
"bundle": {
|
|
63
|
+
"patch": "./cordis.patch.yml"
|
|
64
|
+
},
|
|
65
|
+
"client": {
|
|
66
|
+
"platform": "web",
|
|
67
|
+
"inject": [
|
|
68
|
+
"slots",
|
|
69
|
+
"betterSidebar"
|
|
70
|
+
]
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
"peerDependencies": {
|
|
74
|
+
"dsh-better-sidebar": ">=0.12.0"
|
|
75
|
+
},
|
|
76
|
+
"peerDependenciesMeta": {
|
|
77
|
+
"dsh-better-sidebar": {
|
|
78
|
+
"optional": true
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|