@user-mrz/dsh-system-prompt-studio 0.2.0 → 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 +104 -1
- package/README.md +103 -25
- package/cordis.patch.yml +6 -0
- package/lib/client.js +172 -13
- package/lib/index.js +496 -14
- package/package.json +2 -2
package/lib/index.js
CHANGED
|
@@ -67,6 +67,8 @@ const KNOWN_ORDERS = {
|
|
|
67
67
|
'tool:grep': 1500,
|
|
68
68
|
'tool:jobs': 1600,
|
|
69
69
|
'tool:pty': 1700,
|
|
70
|
+
// 0.2.2-rc.2 新增:dsh-terminal-panel 注册的终端面板段,紧跟在 tool:pty 之后。
|
|
71
|
+
'tool:pty-panel': 1710,
|
|
70
72
|
'tool:web_search': 2000,
|
|
71
73
|
'tool:web_fetch': 2100,
|
|
72
74
|
'tool:lsp': 2200,
|
|
@@ -77,6 +79,9 @@ const KNOWN_ORDERS = {
|
|
|
77
79
|
'tool:ralph': 2700,
|
|
78
80
|
'tool:subagent': 2800,
|
|
79
81
|
'tool:report': 2900,
|
|
82
|
+
// 0.2.2-rc.2 新增:dsh-cua 注册的段名是 `computer-use:dsh-cua`,不是 `tool:` 前缀,
|
|
83
|
+
// 所以第一方前缀判定认不出它;顺序常量仍按官方的 TOOL_COMPUTER_USE = 3000。
|
|
84
|
+
'computer-use:dsh-cua': 3000,
|
|
80
85
|
'mcp-resource-servers': 3100,
|
|
81
86
|
'tools:sdk': 5000,
|
|
82
87
|
'ui:deliverable-file-references': 9000,
|
|
@@ -99,6 +104,12 @@ const DEFAULTS = {
|
|
|
99
104
|
maxObservedScopes: 64,
|
|
100
105
|
// 全局默认值套用到哪些会话:roots = 只套用到顶层会话;all = 含子代理。
|
|
101
106
|
globalDefaultScope: 'roots',
|
|
107
|
+
// 工作区级覆盖套用到哪些会话:all(默认)= 含子代理;roots = 只套用到顶层会话。
|
|
108
|
+
//
|
|
109
|
+
// 与 globalDefaultScope 默认值不同是有意的:全局默认是「所有对话的人格」,
|
|
110
|
+
// 漏进子代理会污染工具型子任务的提示词;而工作区级是「这个项目的规矩」,
|
|
111
|
+
// 子代理正是替这个项目干活的那批,不给它们就等于让实际执行者不守规矩。
|
|
112
|
+
workspaceScope: 'all',
|
|
102
113
|
// fork 继承:子会话没有自身覆盖时,沿 parentSession 链向上找。
|
|
103
114
|
// inheritOnFork=false 可退回「各会话独立」的旧行为。
|
|
104
115
|
inheritOnFork: true,
|
|
@@ -131,6 +142,7 @@ function resolveConfig(raw) {
|
|
|
131
142
|
observeAssemblies: cfg.observeAssemblies !== false,
|
|
132
143
|
maxObservedScopes: number(cfg.maxObservedScopes, DEFAULTS.maxObservedScopes, 1, 4096),
|
|
133
144
|
globalDefaultScope: cfg.globalDefaultScope === 'all' ? 'all' : 'roots',
|
|
145
|
+
workspaceScope: cfg.workspaceScope === 'roots' ? 'roots' : 'all',
|
|
134
146
|
inheritOnFork: cfg.inheritOnFork !== false,
|
|
135
147
|
maxInheritDepth: number(cfg.maxInheritDepth, DEFAULTS.maxInheritDepth, 0, MAX_INHERIT_DEPTH),
|
|
136
148
|
visible: cfg.visible !== false,
|
|
@@ -162,7 +174,7 @@ function emptyEntry(mode) {
|
|
|
162
174
|
}
|
|
163
175
|
|
|
164
176
|
function emptyState() {
|
|
165
|
-
return { version: 1, global: emptyEntry(), sessions: {} }
|
|
177
|
+
return { version: 1, global: emptyEntry(), sessions: {}, workspaces: {} }
|
|
166
178
|
}
|
|
167
179
|
|
|
168
180
|
/**
|
|
@@ -181,9 +193,51 @@ function highestWriteSeq(state) {
|
|
|
181
193
|
}
|
|
182
194
|
consider(state.global)
|
|
183
195
|
for (const entry of Object.values(state.sessions ?? {})) consider(entry)
|
|
196
|
+
for (const entry of Object.values(state.workspaces ?? {})) consider(entry)
|
|
184
197
|
return max
|
|
185
198
|
}
|
|
186
199
|
|
|
200
|
+
/**
|
|
201
|
+
* 工作区键:把目录路径洗成一个稳定的、可比较的字符串。
|
|
202
|
+
*
|
|
203
|
+
* 为什么用路径而不是 workspaceRegistry 的 WorkspaceId:id 是注册表内部标识,
|
|
204
|
+
* 同一个目录被删除后重新注册会拿到新 id,而用户的「这个项目的规矩」不该因此
|
|
205
|
+
* 失效。路径是用户自己能理解、能核对的那个东西。
|
|
206
|
+
*
|
|
207
|
+
* 归一化规则(只做「同一个目录的不同写法」之间必须一致的变换):
|
|
208
|
+
* - 统一用 `/` 分隔,去掉末尾分隔符(`C:\a\b\` 与 `C:/a/b` 必须相等);
|
|
209
|
+
* - Windows 上折叠大小写(NTFS 默认不区分),其余平台保留原样;
|
|
210
|
+
* - 折叠 `.` 段与重复分隔符。不做 `..` 解析:那需要文件系统语义,插件不碰。
|
|
211
|
+
*
|
|
212
|
+
* 空输入返回空串——空串表示「这个会话没有可用目录」,绝不能当成一个合法的
|
|
213
|
+
* 工作区键,否则所有无目录会话会共享同一条覆盖。
|
|
214
|
+
*/
|
|
215
|
+
function workspaceKeyOf(raw) {
|
|
216
|
+
if (typeof raw !== 'string') return ''
|
|
217
|
+
let text = raw.trim()
|
|
218
|
+
if (text === '') return ''
|
|
219
|
+
text = text.replace(/\\/gu, '/')
|
|
220
|
+
// 折叠重复分隔符与 `.` 段(保留开头的 `//` UNC 前缀)。
|
|
221
|
+
const unc = text.startsWith('//')
|
|
222
|
+
if (unc) text = text.slice(2)
|
|
223
|
+
const segments = []
|
|
224
|
+
for (const segment of text.split('/')) {
|
|
225
|
+
if (segment === '' || segment === '.') continue
|
|
226
|
+
segments.push(segment)
|
|
227
|
+
}
|
|
228
|
+
text = segments.join('/')
|
|
229
|
+
if (unc) text = `//${text}`
|
|
230
|
+
// Windows 盘符统一大写,其余折叠大小写。
|
|
231
|
+
if (process.platform === 'win32') {
|
|
232
|
+
text = text.toLowerCase()
|
|
233
|
+
text = text.replace(/^([a-z]):/u, (_match, drive) => `${drive.toUpperCase()}:`)
|
|
234
|
+
}
|
|
235
|
+
if (text === '') return ''
|
|
236
|
+
// 只有盘符没有后续段(`C:`)不是可用目录。
|
|
237
|
+
if (/^[A-Za-z]:$/u.test(text)) return ''
|
|
238
|
+
return text
|
|
239
|
+
}
|
|
240
|
+
|
|
187
241
|
function defaultStateFile() {
|
|
188
242
|
const home = process.env.DSH_HOME && process.env.DSH_HOME.trim() !== '' ? process.env.DSH_HOME : join(homedir(), '.dsh')
|
|
189
243
|
return join(home, 'dsh-system-prompt-studio', 'state.json')
|
|
@@ -249,6 +303,16 @@ function loadState(file) {
|
|
|
249
303
|
state.sessions[id] = normalizeEntry(entry)
|
|
250
304
|
}
|
|
251
305
|
}
|
|
306
|
+
// 工作区条目:键是归一化后的目录路径。老状态文件没有这一段,缺席即空表。
|
|
307
|
+
if (parsed.workspaces !== null && typeof parsed.workspaces === 'object') {
|
|
308
|
+
for (const [rawKey, entry] of Object.entries(parsed.workspaces)) {
|
|
309
|
+
// 老文件里可能存着未归一化的键(手工编辑、或换过归一化规则),
|
|
310
|
+
// 这里重新洗一遍,避免同一个目录出现两条互不可见的覆盖。
|
|
311
|
+
const key = workspaceKeyOf(rawKey)
|
|
312
|
+
if (key === '') continue
|
|
313
|
+
state.workspaces[key] = normalizeEntry(entry)
|
|
314
|
+
}
|
|
315
|
+
}
|
|
252
316
|
}
|
|
253
317
|
health.loaded = true
|
|
254
318
|
return { state, health }
|
|
@@ -367,7 +431,10 @@ function buildSignature(effective) {
|
|
|
367
431
|
if (effective === null || effective === undefined) return 'none|'
|
|
368
432
|
if (effective.kind === 'none') return `none|${effective.reason ?? ''}`
|
|
369
433
|
const origin = effective.inheritedFrom === undefined ? '' : `|from:${effective.inheritedFrom}`
|
|
370
|
-
|
|
434
|
+
// 工作区来源也要进签名:换目录(`workingDirectory.set`)后段必须重装到新键上,
|
|
435
|
+
// 否则用户会看到「工作区规则还生效着,但面板说这个目录没有规则」。
|
|
436
|
+
const workspace = typeof effective.workspaceKey === 'string' && effective.workspaceKey !== '' ? `|ws:${effective.workspaceKey}` : ''
|
|
437
|
+
return `${effective.kind}|${effective.target}|${effective.order}${origin}${workspace}`
|
|
371
438
|
}
|
|
372
439
|
|
|
373
440
|
/** 逐块取出一条消息的纯文本;非文本块跳过。 */
|
|
@@ -560,13 +627,27 @@ export const apply = mountOnce(name, function apply(ctx, rawConfig) {
|
|
|
560
627
|
}
|
|
561
628
|
}
|
|
562
629
|
|
|
563
|
-
// ──
|
|
564
|
-
|
|
630
|
+
// ── 观察 + 对齐:只记录、只重装,绝不改写本次装配 ─────────────────────
|
|
631
|
+
//
|
|
632
|
+
// 这个监听器**总是**注册,而不是只在 observeAssemblies 时注册:里面的
|
|
633
|
+
// {@link reconcile} 负责发现「会话工作目录变了」,而那个变化没有事件可听。
|
|
634
|
+
// 关掉段清单观察不该连带让工作区规则在换目录后失灵。
|
|
635
|
+
//
|
|
636
|
+
// 两件事都发生在 waterfall 里、装配已经取完 sections 之后,所以本次装配
|
|
637
|
+
// 完全不受影响;重装只对下一次请求生效。
|
|
638
|
+
{
|
|
565
639
|
const off = ctx.on('system-prompt/assemble', (assembly, context, next) => {
|
|
640
|
+
if (config.observeAssemblies) {
|
|
641
|
+
try {
|
|
642
|
+
observeAssembly(ctx, assembly, context)
|
|
643
|
+
} catch {
|
|
644
|
+
/* 观察失败绝不能影响装配 */
|
|
645
|
+
}
|
|
646
|
+
}
|
|
566
647
|
try {
|
|
567
|
-
|
|
648
|
+
reconcile()
|
|
568
649
|
} catch {
|
|
569
|
-
/*
|
|
650
|
+
/* 对齐失败也不能影响装配 */
|
|
570
651
|
}
|
|
571
652
|
return next()
|
|
572
653
|
})
|
|
@@ -669,20 +750,217 @@ export const apply = mountOnce(name, function apply(ctx, rawConfig) {
|
|
|
669
750
|
}
|
|
670
751
|
}
|
|
671
752
|
|
|
753
|
+
/**
|
|
754
|
+
* 一个会话所属的工作区(`workspaceRegistry` 里那条),没有就 undefined。
|
|
755
|
+
*
|
|
756
|
+
* 这是「工作区」的唯一权威:DSH 把工作区做成一等概念,由注册表按规范化的
|
|
757
|
+
* 项目目录维护 session → workspace 的归属,侧边栏看到的就是它。本插件按
|
|
758
|
+
* 用户看得见的那个东西来分组,而不是另造一套。
|
|
759
|
+
*/
|
|
760
|
+
function workspaceOf(sessionId) {
|
|
761
|
+
const registry = service('workspaceRegistry')
|
|
762
|
+
if (registry === undefined || typeof registry.list !== 'function' || sessionId === '') return undefined
|
|
763
|
+
try {
|
|
764
|
+
for (const workspace of registry.list()) {
|
|
765
|
+
const ids = workspace?.sessionIds
|
|
766
|
+
if (!Array.isArray(ids)) continue
|
|
767
|
+
if (ids.includes(sessionId)) return workspace
|
|
768
|
+
}
|
|
769
|
+
} catch {
|
|
770
|
+
return undefined
|
|
771
|
+
}
|
|
772
|
+
return undefined
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
/**
|
|
776
|
+
* 一次会话 → 工作区**原始路径**的索引,按需建立、单次 pass 内复用。
|
|
777
|
+
*
|
|
778
|
+
* 为什么需要缓存:`reconcile()` 在每次装配时对每个活 agent 重算签名,而签名
|
|
779
|
+
* 里含工作区键;如果每次都 `registry.list()` 再逐条 `sessionIds.includes`,
|
|
780
|
+
* 复杂度是 O(agent × workspace × session)。工作区一多就变成每次模型请求都在
|
|
781
|
+
* 做平方级的数组扫描。索引建一次是 O(workspace × session),之后每个 agent
|
|
782
|
+
* 是 O(1)。
|
|
783
|
+
*
|
|
784
|
+
* 存**原始路径**而不是归一化键:显示时要用原路径(归一化会把 Windows 盘符和
|
|
785
|
+
* 目录名小写化,用户核对时看着别扭),匹配时再洗一遍。
|
|
786
|
+
*
|
|
787
|
+
* 失效方式很简单:不跨 pass 复用。工作区归属会变(新建、删除、归档会话),
|
|
788
|
+
* 而注册表没有变更事件可订阅,所以只在单次 pass 内缓存,下一次重算。
|
|
789
|
+
*/
|
|
790
|
+
function workspaceIndex() {
|
|
791
|
+
const registry = service('workspaceRegistry')
|
|
792
|
+
const index = new Map()
|
|
793
|
+
if (registry === undefined || typeof registry.list !== 'function') return index
|
|
794
|
+
let list = []
|
|
795
|
+
try {
|
|
796
|
+
list = registry.list()
|
|
797
|
+
} catch {
|
|
798
|
+
return index
|
|
799
|
+
}
|
|
800
|
+
for (const workspace of list) {
|
|
801
|
+
const path = typeof workspace?.path === 'string' ? workspace.path : ''
|
|
802
|
+
const ids = workspace?.sessionIds
|
|
803
|
+
if (path === '' || !Array.isArray(ids)) continue
|
|
804
|
+
for (const id of ids) {
|
|
805
|
+
if (typeof id === 'string' && id !== '' && !index.has(id)) index.set(id, path)
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
return index
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
/**
|
|
812
|
+
* 一个会话的工作区,返回 `{ key, path }`;拿不到就返回 null。
|
|
813
|
+
* `key` 用于匹配(归一化),`path` 用于显示(原样)。
|
|
814
|
+
*
|
|
815
|
+
* `owner` 可以是 agent、Session,**也可以只是一个 sessionId 字符串**。后者
|
|
816
|
+
* 是冷会话的唯一可用形态:冷会话既没有活 agent,也不在内存 store 里,
|
|
817
|
+
* 所以拿不到 Session 对象——但注册表是持久化的,光凭 id 就能查到归属。
|
|
818
|
+
* 早期版本要求必须有 Session 对象,结果是冷会话的工作区档整个不可用。
|
|
819
|
+
*
|
|
820
|
+
* 顺序刻意是这样,每一档都对应一个真实的失败模式:
|
|
821
|
+
*
|
|
822
|
+
* 1. **注册表里的工作区目录**。这是用户看得见的那个「工作区」,也是唯一
|
|
823
|
+
* 稳定的归属——会话在注册表里的归属不会因为它中途 `cd` 而改变。
|
|
824
|
+
* 这一档只依赖 sessionId,所以冷会话也走得通。
|
|
825
|
+
*
|
|
826
|
+
* 2. **`workingDirectory.get()`,但只在它不等于 `defaultDirectory` 时采用**。
|
|
827
|
+
* 这一条是踩出来的:`get()` 的实现是 `stateOf(session) ?? defaultDirectory`,
|
|
828
|
+
* 也就是「没换过目录」的会话拿到的是**部署默认目录**,不是它自己的项目。
|
|
829
|
+
* 直接采用会让所有会话共用同一个键——用户在这个项目里设的规则会落到
|
|
830
|
+
* 默认目录名下,然后莫名其妙地对别处生效。只有当 get() 给出别的东西时,
|
|
831
|
+
* 才说明确实提交过一次目录变更。需要 Session 对象,冷会话跳过。
|
|
832
|
+
*
|
|
833
|
+
* 3. **`session.header.cwd`**,会话创建时记下的原始项目目录。
|
|
834
|
+
*
|
|
835
|
+
* 4. 最后才退回 `get()` 的兜底值(此时 header 也没有 cwd,它至少是个真目录)。
|
|
836
|
+
*
|
|
837
|
+
* @param owner agent / Session / sessionId 字符串
|
|
838
|
+
* @param index 可选的预建索引(`reconcile` 的 pass 内传入,避免重复扫描)
|
|
839
|
+
*/
|
|
840
|
+
function workspaceRefFor(owner, index) {
|
|
841
|
+
const asId = typeof owner === 'string' ? owner.trim() : ''
|
|
842
|
+
if (asId === '' && (owner === null || typeof owner !== 'object')) return null
|
|
843
|
+
// agent 有 `.session`;冷会话传进来的就是 Session 本身;也可能只是个 id。
|
|
844
|
+
const session =
|
|
845
|
+
asId !== ''
|
|
846
|
+
? undefined
|
|
847
|
+
: owner.session !== null && typeof owner.session === 'object'
|
|
848
|
+
? owner.session
|
|
849
|
+
: owner
|
|
850
|
+
const sessionId =
|
|
851
|
+
asId !== ''
|
|
852
|
+
? asId
|
|
853
|
+
: typeof session?.id === 'string'
|
|
854
|
+
? session.id
|
|
855
|
+
: typeof owner?.id === 'string'
|
|
856
|
+
? owner.id
|
|
857
|
+
: ''
|
|
858
|
+
|
|
859
|
+
/** 把一个原始路径包装成 `{ key, path }`;洗不出键就返回 null。 */
|
|
860
|
+
const ref = (raw) => {
|
|
861
|
+
const key = workspaceKeyOf(raw)
|
|
862
|
+
if (key === '') return null
|
|
863
|
+
// 显示用原路径;归一化只影响匹配,不该让用户看到一个被小写化的路径。
|
|
864
|
+
return { key, path: typeof raw === 'string' ? raw.trim() : key }
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
// ── 1. 注册表归属:只依赖 sessionId,冷会话同样成立 ──────────────────
|
|
868
|
+
const fromIndex = index !== undefined && sessionId !== '' ? index.get(sessionId) : undefined
|
|
869
|
+
if (fromIndex !== undefined && fromIndex !== null) {
|
|
870
|
+
const indexed = ref(fromIndex)
|
|
871
|
+
if (indexed !== null) return indexed
|
|
872
|
+
} else if (index === undefined) {
|
|
873
|
+
const workspace = workspaceOf(sessionId)
|
|
874
|
+
if (workspace !== undefined) {
|
|
875
|
+
const owned = ref(workspace.path)
|
|
876
|
+
if (owned !== null) return owned
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
// 后面几档都需要 Session 对象;冷会话到此为止。
|
|
881
|
+
if (session === undefined) return null
|
|
882
|
+
|
|
883
|
+
// ── 2. 当前目录,但排除「没换过目录时的部署默认值」 ─────────────────
|
|
884
|
+
const directory = service('workingDirectory')
|
|
885
|
+
const readDirectory = () => {
|
|
886
|
+
if (directory === undefined || typeof directory.get !== 'function') return ''
|
|
887
|
+
try {
|
|
888
|
+
const value = directory.get(session)
|
|
889
|
+
return typeof value === 'string' ? value : ''
|
|
890
|
+
} catch {
|
|
891
|
+
return ''
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
const current = readDirectory()
|
|
896
|
+
const changed = ref(current)
|
|
897
|
+
if (changed !== null) {
|
|
898
|
+
const fallback = workspaceKeyOf(directory?.defaultDirectory)
|
|
899
|
+
if (fallback === '' || changed.key !== fallback) return changed
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
// ── 3. 会话创建时记下的原始项目目录 ─────────────────────────────────
|
|
903
|
+
try {
|
|
904
|
+
const original = ref(session.header?.cwd)
|
|
905
|
+
if (original !== null) return original
|
|
906
|
+
} catch {
|
|
907
|
+
/* header 读不到就继续往下 */
|
|
908
|
+
}
|
|
909
|
+
return changed
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
/** 只要键的简写;内部判定都用这个。 */
|
|
913
|
+
function workspaceKeyFor(owner, index) {
|
|
914
|
+
const found = workspaceRefFor(owner, index)
|
|
915
|
+
return found === null ? '' : found.key
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
/**
|
|
919
|
+
* 工作区条目:命中归一化键的那一条;没有就 undefined。
|
|
920
|
+
*
|
|
921
|
+
* 先精确命中,再按「同一目录不同写法」兜底——状态文件里可能留着未归一化的
|
|
922
|
+
* 老键(手工编辑过、或归一化规则改过),只做精确匹配会让那份规则静默失效。
|
|
923
|
+
*/
|
|
924
|
+
function workspaceEntryFor(key) {
|
|
925
|
+
if (key === '') return undefined
|
|
926
|
+
const direct = runtime.state.workspaces[key]
|
|
927
|
+
if (direct !== undefined) return direct
|
|
928
|
+
for (const [storedKey, entry] of Object.entries(runtime.state.workspaces)) {
|
|
929
|
+
if (workspaceKeyOf(storedKey) === key) return entry
|
|
930
|
+
}
|
|
931
|
+
return undefined
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
/** 该 agent 的工作区条目是否允许套用(受 workspaceScope 限制)。 */
|
|
935
|
+
function workspaceApplies(agent) {
|
|
936
|
+
if (config.workspaceScope === 'roots' && !isRootAgent(agent)) return false
|
|
937
|
+
return true
|
|
938
|
+
}
|
|
939
|
+
|
|
672
940
|
/**
|
|
673
941
|
* 判一个 agent 当前该不该有覆盖、以及是什么形态。
|
|
674
942
|
*
|
|
675
|
-
*
|
|
943
|
+
* 优先级(从具体到宽泛,谁更靠近「这一次对话」谁赢):
|
|
676
944
|
* 1. 自身会话条目
|
|
677
945
|
* 2. 沿 parentSession 链继承(最近的有覆盖的祖先)
|
|
678
|
-
* 3.
|
|
946
|
+
* 3. 工作区条目(该对话当前目录的那条)
|
|
947
|
+
* 4. 全局默认(按 globalDefaultScope 决定是否碰子代理)
|
|
948
|
+
*
|
|
949
|
+
* 为什么工作区排在 fork 继承之后:fork 出来的对话带着上一段工作的上下文,
|
|
950
|
+
* 用户为它单独调过规则就是最具体的意图;而工作区规则是「这个目录的默认」,
|
|
951
|
+
* 比「这一次对话的调整」更宽泛。
|
|
952
|
+
*
|
|
953
|
+
* 为什么工作区排在全局之前:全局是「所有对话」,工作区是「这个项目」。
|
|
954
|
+
* 项目规则显然更具体,反过来的话工作区就永远不可能生效——那等于没这个功能。
|
|
679
955
|
*/
|
|
680
|
-
function effectiveFor(agent) {
|
|
956
|
+
function effectiveFor(agent, index) {
|
|
681
957
|
const id = typeof agent?.id === 'string' ? agent.id : ''
|
|
682
958
|
const own = id !== '' ? runtime.state.sessions[id] : undefined
|
|
683
959
|
if (own !== undefined && own.enabled) return overrideFromEntry(own, 'session')
|
|
684
960
|
const inherited = findInheritedOverride(agent)
|
|
685
961
|
if (inherited !== null) return inherited
|
|
962
|
+
const workspace = workspaceOverrideFor(agent, index)
|
|
963
|
+
if (workspace !== null) return workspace
|
|
686
964
|
if (runtime.state.global.enabled) {
|
|
687
965
|
if (config.globalDefaultScope === 'roots' && !isRootAgent(agent)) {
|
|
688
966
|
return { kind: 'none', source: 'global', reason: 'subagent' }
|
|
@@ -692,21 +970,78 @@ export const apply = mountOnce(name, function apply(ctx, rawConfig) {
|
|
|
692
970
|
return { kind: 'none' }
|
|
693
971
|
}
|
|
694
972
|
|
|
973
|
+
/**
|
|
974
|
+
* 工作区层的判定:命中且启用且形态有效才返回覆盖,否则 null。
|
|
975
|
+
*
|
|
976
|
+
* 返回 null 而不是 `{kind:'none'}` 是有意的:这一层「没话说」时必须让调用方
|
|
977
|
+
* 继续往全局走,而不是在这里截断优先级链。
|
|
978
|
+
*/
|
|
979
|
+
function workspaceOverrideFor(agent, index) {
|
|
980
|
+
if (!workspaceApplies(agent)) return null
|
|
981
|
+
const key = workspaceKeyFor(agent, index)
|
|
982
|
+
if (key === '') return null
|
|
983
|
+
const entry = workspaceEntryFor(key)
|
|
984
|
+
if (entry === undefined || !entry.enabled) return null
|
|
985
|
+
const override = overrideFromEntry(entry, 'workspace')
|
|
986
|
+
if (override.kind === 'none') return null
|
|
987
|
+
return { ...override, workspaceKey: key }
|
|
988
|
+
}
|
|
989
|
+
|
|
695
990
|
/**
|
|
696
991
|
* 冷会话(没有活 agent)时的退化判定。
|
|
697
992
|
*
|
|
698
|
-
* 冷会话读不到 header
|
|
993
|
+
* 冷会话读不到 header,因此找不到父链——只能看自身条目、工作区与全局默认。
|
|
699
994
|
* 这是本方案的已知边界:冷会话的继承要等它被恢复(有了 agent)之后才成立。
|
|
995
|
+
*
|
|
996
|
+
* 工作区这一层在冷会话下仍然可用:`sessions` 服务能拿到 Session 对象,
|
|
997
|
+
* `workingDirectory.get(session)` 对冷会话同样有效。所以「给某个目录设规则,
|
|
998
|
+
* 之后才打开的对话」这件事不需要会话活着。
|
|
700
999
|
*/
|
|
701
1000
|
function effectiveForId(sessionId) {
|
|
702
1001
|
const agent = resolveAgentById(sessionId)
|
|
703
1002
|
if (agent !== undefined) return effectiveFor(agent)
|
|
704
1003
|
const own = runtime.state.sessions[sessionId]
|
|
705
1004
|
if (own !== undefined && own.enabled) return overrideFromEntry(own, 'session')
|
|
1005
|
+
const coldWorkspace = workspaceOverrideForSession(sessionId)
|
|
1006
|
+
if (coldWorkspace !== null) return coldWorkspace
|
|
706
1007
|
if (runtime.state.global.enabled) return overrideFromEntry(runtime.state.global, 'global')
|
|
707
1008
|
return { kind: 'none' }
|
|
708
1009
|
}
|
|
709
1010
|
|
|
1011
|
+
/**
|
|
1012
|
+
* 冷会话的工作区判定。
|
|
1013
|
+
*
|
|
1014
|
+
* 关键点:**不要求 Session 对象**。注册表是持久化的,光凭 sessionId 就能查到
|
|
1015
|
+
* 归属;早期版本这里先要 `sessions.get(id)`,而冷会话不在内存 store 里,
|
|
1016
|
+
* 于是「工作区」这一档对冷会话整个不可用——面板显示没有工作区,用户也就
|
|
1017
|
+
* 没法给一个还没恢复的对话设项目规则。
|
|
1018
|
+
*
|
|
1019
|
+
* 子代理判定在冷会话下读不到 header.parentSession(没有 Session 对象),
|
|
1020
|
+
* 所以 `workspaceScope: roots` 时只能保守地按「不是子代理」处理——宁可多套用
|
|
1021
|
+
* 一次,也不要让用户明明设了规则却完全不生效。
|
|
1022
|
+
*/
|
|
1023
|
+
function workspaceOverrideForSession(sessionId) {
|
|
1024
|
+
if (config.workspaceScope === 'roots') {
|
|
1025
|
+
const session = coldSessionOf(service('sessions'), sessionId)
|
|
1026
|
+
if (session !== undefined) {
|
|
1027
|
+
let parent = ''
|
|
1028
|
+
try {
|
|
1029
|
+
parent = typeof session.header?.parentSession === 'string' ? session.header.parentSession : ''
|
|
1030
|
+
} catch {
|
|
1031
|
+
parent = ''
|
|
1032
|
+
}
|
|
1033
|
+
if (parent !== '') return null
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
const key = workspaceKeyFor(resolveAgentById(sessionId) ?? coldSessionOf(service('sessions'), sessionId) ?? sessionId)
|
|
1037
|
+
if (key === '') return null
|
|
1038
|
+
const entry = workspaceEntryFor(key)
|
|
1039
|
+
if (entry === undefined || !entry.enabled) return null
|
|
1040
|
+
const override = overrideFromEntry(entry, 'workspace')
|
|
1041
|
+
if (override.kind === 'none') return null
|
|
1042
|
+
return { ...override, workspaceKey: key }
|
|
1043
|
+
}
|
|
1044
|
+
|
|
710
1045
|
function signatureOf(effective) {
|
|
711
1046
|
return buildSignature(effective)
|
|
712
1047
|
}
|
|
@@ -762,7 +1097,7 @@ export const apply = mountOnce(name, function apply(ctx, rawConfig) {
|
|
|
762
1097
|
* 签名,只在形态变化时重装注册。文本变化不必重装——段的 text 是函数,
|
|
763
1098
|
* 每次装配都会重新求值。
|
|
764
1099
|
*/
|
|
765
|
-
function install(agent) {
|
|
1100
|
+
function install(agent, index) {
|
|
766
1101
|
if (agent === null || typeof agent !== 'object') return
|
|
767
1102
|
const sessionId = typeof agent.id === 'string' ? agent.id : ''
|
|
768
1103
|
if (sessionId === '') return
|
|
@@ -774,7 +1109,7 @@ export const apply = mountOnce(name, function apply(ctx, rawConfig) {
|
|
|
774
1109
|
entry.effective = { kind: 'none', reason: 'disabled' }
|
|
775
1110
|
return
|
|
776
1111
|
}
|
|
777
|
-
const effective = effectiveFor(agent)
|
|
1112
|
+
const effective = effectiveFor(agent, index)
|
|
778
1113
|
const signature = signatureOf(effective)
|
|
779
1114
|
if (entry.signature === signature) return
|
|
780
1115
|
disposeSections(entry)
|
|
@@ -904,6 +1239,59 @@ export const apply = mountOnce(name, function apply(ctx, rawConfig) {
|
|
|
904
1239
|
}
|
|
905
1240
|
}
|
|
906
1241
|
|
|
1242
|
+
/**
|
|
1243
|
+
* 按当前状态重新对齐所有活会话的注册。
|
|
1244
|
+
*
|
|
1245
|
+
* 为什么需要它:装段只在 `agent/created` 和「状态被写入」时发生,但有一类
|
|
1246
|
+
* 变化既没有事件、也不经过本插件的写入路径——**会话的工作目录变了**
|
|
1247
|
+
* (`workingDirectory.set`,例如用户在会话里切到另一个项目)。那时工作区层
|
|
1248
|
+
* 该命中另一条规则,可签名只在下次 install 时才会重算,段于是停在旧目录的
|
|
1249
|
+
* 规则上,且没有任何地方能解释这件事。
|
|
1250
|
+
*
|
|
1251
|
+
* 对齐点选在每次装配之后:那是模型请求真正发生、规则真正被读取的时刻,
|
|
1252
|
+
* 也是用户唯一会察觉不对的时刻。重装只改注册形态、不重算文本,代价可以忽略。
|
|
1253
|
+
*/
|
|
1254
|
+
function reconcile() {
|
|
1255
|
+
if (!config.enabled) return
|
|
1256
|
+
const registry = agents()
|
|
1257
|
+
if (registry === undefined) return
|
|
1258
|
+
let list = []
|
|
1259
|
+
try {
|
|
1260
|
+
list = registry.list()
|
|
1261
|
+
} catch {
|
|
1262
|
+
return
|
|
1263
|
+
}
|
|
1264
|
+
// 索引按 pass 建一次:每个 agent 的签名都要工作区键,逐 agent 扫注册表会
|
|
1265
|
+
// 变成 O(agent × workspace × session) 的平方级开销。
|
|
1266
|
+
const index = workspaceIndex()
|
|
1267
|
+
for (const agent of list) {
|
|
1268
|
+
const id = typeof agent?.id === 'string' ? agent.id : ''
|
|
1269
|
+
if (id === '') continue
|
|
1270
|
+
const entry = runtime.installed.get(id)
|
|
1271
|
+
if (entry === undefined) {
|
|
1272
|
+
// 新出现的 agent(例如观察先于 agent/created 到达):补装。
|
|
1273
|
+
try {
|
|
1274
|
+
install(agent, index)
|
|
1275
|
+
} catch {
|
|
1276
|
+
/* 对齐失败不能影响装配 */
|
|
1277
|
+
}
|
|
1278
|
+
continue
|
|
1279
|
+
}
|
|
1280
|
+
let expected = ''
|
|
1281
|
+
try {
|
|
1282
|
+
expected = signatureOf(effectiveFor(agent, index))
|
|
1283
|
+
} catch {
|
|
1284
|
+
continue
|
|
1285
|
+
}
|
|
1286
|
+
if (entry.signature === expected) continue
|
|
1287
|
+
try {
|
|
1288
|
+
install(agent, index)
|
|
1289
|
+
} catch {
|
|
1290
|
+
/* 同上 */
|
|
1291
|
+
}
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
|
|
907
1295
|
// ── 状态快照(GET 与写入后都返回同一形状) ───────────────────────────
|
|
908
1296
|
/** 最近一次请求真正发出去的系统提示词:取活会话里最后一个 system 消息。 */
|
|
909
1297
|
function currentSystemPrompt(sessions, sessionId) {
|
|
@@ -933,6 +1321,17 @@ export const apply = mountOnce(name, function apply(ctx, rawConfig) {
|
|
|
933
1321
|
return { text: null, note: '该对话还没有发出过带系统提示词的请求。' }
|
|
934
1322
|
}
|
|
935
1323
|
|
|
1324
|
+
/** 冷会话的 Session 对象(活会话没有 agent 时用);拿不到就 undefined。 */
|
|
1325
|
+
function coldSessionOf(store, sessionId) {
|
|
1326
|
+
if (store === undefined || typeof store.get !== 'function' || sessionId === '') return undefined
|
|
1327
|
+
try {
|
|
1328
|
+
const session = store.get(sessionId)
|
|
1329
|
+
return session === null || session === undefined ? undefined : session
|
|
1330
|
+
} catch {
|
|
1331
|
+
return undefined
|
|
1332
|
+
}
|
|
1333
|
+
}
|
|
1334
|
+
|
|
936
1335
|
function snapshot(sessionId) {
|
|
937
1336
|
const registry = agents()
|
|
938
1337
|
const sessions = service('sessions')
|
|
@@ -942,6 +1341,19 @@ export const apply = mountOnce(name, function apply(ctx, rawConfig) {
|
|
|
942
1341
|
const record = sessionId !== '' ? runtime.installed.get(sessionId) : undefined
|
|
943
1342
|
const watch = sessionId !== '' ? runtime.observed.get(sessionId) : undefined
|
|
944
1343
|
const current = sessionId !== '' ? currentSystemPrompt(sessions, sessionId) : { text: null, note: '未指定会话。' }
|
|
1344
|
+
/**
|
|
1345
|
+
* 该会话所属的工作区与它那条覆盖。
|
|
1346
|
+
*
|
|
1347
|
+
* 面板要同时显示两件事:这条覆盖的正文(用户能编辑),以及它属于哪个工作区
|
|
1348
|
+
* (用户能核对)。只给正文不给路径,用户没法判断自己是不是改到了另一个项目。
|
|
1349
|
+
*
|
|
1350
|
+
* `key` 用于匹配(归一化),`path` 用于显示(原样)。两者分开是因为归一化会
|
|
1351
|
+
* 把 Windows 盘符和目录名小写化——匹配需要它,显示不需要。
|
|
1352
|
+
*/
|
|
1353
|
+
const workspaceRef =
|
|
1354
|
+
sessionId !== '' ? workspaceRefFor(resolveAgentById(sessionId) ?? coldSessionOf(sessions, sessionId) ?? sessionId) : null
|
|
1355
|
+
const workspaceKey = workspaceRef === null ? '' : workspaceRef.key
|
|
1356
|
+
const workspaceEntry = workspaceKey !== '' ? workspaceEntryFor(workspaceKey) : undefined
|
|
945
1357
|
let live = false
|
|
946
1358
|
try {
|
|
947
1359
|
live = sessionId !== '' && typeof registry?.get === 'function' ? registry.get(sessionId) !== undefined : false
|
|
@@ -958,11 +1370,27 @@ export const apply = mountOnce(name, function apply(ctx, rawConfig) {
|
|
|
958
1370
|
sessions: sessions !== undefined,
|
|
959
1371
|
observeAssemblies: config.observeAssemblies,
|
|
960
1372
|
allowComplete: config.allowComplete,
|
|
1373
|
+
workspace: config.workspaceScope,
|
|
961
1374
|
},
|
|
962
1375
|
sessionId,
|
|
963
1376
|
live,
|
|
964
1377
|
global: { ...runtime.state.global },
|
|
965
1378
|
session: entry !== undefined ? { ...entry } : null,
|
|
1379
|
+
/**
|
|
1380
|
+
* 工作区层:`key` 是归一化路径(用于匹配与核对),`path` 是原始路径
|
|
1381
|
+
* (面板显示用),`entry` 是这条覆盖。
|
|
1382
|
+
*
|
|
1383
|
+
* `key` 为空表示这个会话没有可用工作区(例如注册表里没有归属、且读不到
|
|
1384
|
+
* 目录),此时面板必须说明「工作区级不可用」而不是显示一个空路径让用户
|
|
1385
|
+
* 以为能存。
|
|
1386
|
+
*/
|
|
1387
|
+
workspace: {
|
|
1388
|
+
key: workspaceKey,
|
|
1389
|
+
path: workspaceRef === null ? '' : workspaceRef.path,
|
|
1390
|
+
entry: workspaceEntry !== undefined ? { ...workspaceEntry } : null,
|
|
1391
|
+
},
|
|
1392
|
+
/** 已配置的工作区数量,面板用来提示「还有 N 个项目有自己的规则」。 */
|
|
1393
|
+
workspaceCount: Object.keys(runtime.state.workspaces).length,
|
|
966
1394
|
effective: {
|
|
967
1395
|
kind: effective.kind,
|
|
968
1396
|
target: effective.target ?? '',
|
|
@@ -972,6 +1400,8 @@ export const apply = mountOnce(name, function apply(ctx, rawConfig) {
|
|
|
972
1400
|
// 继承信息:面板据此显示「继承自 <父会话>」并提供「在此固定」。
|
|
973
1401
|
inheritedFrom: effective.inheritedFrom ?? '',
|
|
974
1402
|
inheritedDepth: typeof effective.inheritedDepth === 'number' ? effective.inheritedDepth : 0,
|
|
1403
|
+
// 工作区信息:面板据此显示「来自 <目录> 的项目规则」。
|
|
1404
|
+
workspaceKey: effective.workspaceKey ?? '',
|
|
975
1405
|
},
|
|
976
1406
|
/** 该会话自身的父会话 id(没有则为空串);面板用来解释继承关系。 */
|
|
977
1407
|
parentSession: parentIdOf(resolveAgentById(sessionId)),
|
|
@@ -1065,14 +1495,49 @@ export const apply = mountOnce(name, function apply(ctx, rawConfig) {
|
|
|
1065
1495
|
async function handlePost(payload) {
|
|
1066
1496
|
const body = payload !== null && typeof payload === 'object' ? payload : {}
|
|
1067
1497
|
const action = typeof body.action === 'string' ? body.action : ''
|
|
1068
|
-
|
|
1498
|
+
/**
|
|
1499
|
+
* 作用域三档:session(本对话)| workspace(本目录)| global(全局默认)。
|
|
1500
|
+
*
|
|
1501
|
+
* 未知值一律回落到 session 而不是静默当成 global:写错作用域却成功保存,
|
|
1502
|
+
* 用户会以为改的是这一个对话,实际改的是所有对话——这是本插件最危险的一种
|
|
1503
|
+
* 静默失败。回落 session 至少还会因为缺 sessionId 被下面那条检查拦住。
|
|
1504
|
+
*/
|
|
1505
|
+
const scope = body.scope === 'global' ? 'global' : body.scope === 'workspace' ? 'workspace' : 'session'
|
|
1069
1506
|
const sessionId = typeof body.sessionId === 'string' ? body.sessionId.trim() : ''
|
|
1507
|
+
/**
|
|
1508
|
+
* 工作区键的来源。
|
|
1509
|
+
*
|
|
1510
|
+
* 面板不直接传路径,而是传 sessionId,由 host 自己算出这个会话当前的目录。
|
|
1511
|
+
* 原因:路径是安全边界——如果接受客户端传任意路径,一个被 XSS 的面板就能
|
|
1512
|
+
* 给任意目录写入规则(含用户从没打开过的目录)。回环围栏挡得住远程,挡不住
|
|
1513
|
+
* 同源脚本。所以只允许「给这个会话所在目录」写规则。
|
|
1514
|
+
*
|
|
1515
|
+
* `workspaceKey` 字段仍然接受,但只在没有 sessionId 时用作显式指定(供脚本
|
|
1516
|
+
* 与测试使用),并且要过同一套归一化。
|
|
1517
|
+
*/
|
|
1518
|
+
let workspaceKey = ''
|
|
1519
|
+
if (scope === 'workspace') {
|
|
1520
|
+
if (sessionId !== '') {
|
|
1521
|
+
// 传 agent / Session 而不是裸 id:注册表归属那一档光凭 id 就够,但
|
|
1522
|
+
// 「没进注册表的会话」只能靠 Session 的 header.cwd,裸 id 拿不到它。
|
|
1523
|
+
const found = workspaceRefFor(
|
|
1524
|
+
resolveAgentById(sessionId) ?? coldSessionOf(service('sessions'), sessionId) ?? sessionId,
|
|
1525
|
+
)
|
|
1526
|
+
if (found !== null) workspaceKey = found.key
|
|
1527
|
+
}
|
|
1528
|
+
if (workspaceKey === '' && typeof body.workspaceKey === 'string') {
|
|
1529
|
+
workspaceKey = workspaceKeyOf(body.workspaceKey)
|
|
1530
|
+
}
|
|
1531
|
+
}
|
|
1070
1532
|
|
|
1071
1533
|
if (action === 'resetAll') {
|
|
1072
1534
|
if (body.confirm !== true) {
|
|
1073
1535
|
return { status: 400, body: { ok: false, error: 'CONFIRM_REQUIRED', message: '清空全部会话覆盖需要 confirm: true。' } }
|
|
1074
1536
|
}
|
|
1075
1537
|
runtime.state.sessions = {}
|
|
1538
|
+
// 工作区规则是「项目规矩」,不是「这一次对话的临时调整」。resetAll 的
|
|
1539
|
+
// 语义是清掉逐对话的临时状态,把工作区一起清掉会毁掉用户攒下来的项目
|
|
1540
|
+
// 配置——那不可恢复,且用户点这个按钮时并不预期它发生。
|
|
1076
1541
|
const persisted = await applyState()
|
|
1077
1542
|
return { status: 200, body: { ...snapshot(''), persisted } }
|
|
1078
1543
|
}
|
|
@@ -1083,9 +1548,20 @@ export const apply = mountOnce(name, function apply(ctx, rawConfig) {
|
|
|
1083
1548
|
if (scope === 'session' && sessionId === '') {
|
|
1084
1549
|
return { status: 400, body: { ok: false, error: 'MISSING_SESSION', message: '作用域是 session 时必须给 sessionId。' } }
|
|
1085
1550
|
}
|
|
1551
|
+
if (scope === 'workspace' && workspaceKey === '') {
|
|
1552
|
+
return {
|
|
1553
|
+
status: 400,
|
|
1554
|
+
body: {
|
|
1555
|
+
ok: false,
|
|
1556
|
+
error: 'MISSING_WORKSPACE',
|
|
1557
|
+
message: '这个对话没有可用的工作区,无法按工作区保存。请给 sessionId(由其工作区归属决定),或显式给 workspaceKey。',
|
|
1558
|
+
},
|
|
1559
|
+
}
|
|
1560
|
+
}
|
|
1086
1561
|
|
|
1087
1562
|
if (action === 'clear') {
|
|
1088
1563
|
if (scope === 'global') runtime.state.global = { ...emptyEntry(config.defaultMode), ...stampWrite() }
|
|
1564
|
+
else if (scope === 'workspace') delete runtime.state.workspaces[workspaceKey]
|
|
1089
1565
|
else delete runtime.state.sessions[sessionId]
|
|
1090
1566
|
const persisted = await applyState()
|
|
1091
1567
|
return { status: 200, body: { ...snapshot(sessionId), persisted } }
|
|
@@ -1124,7 +1600,11 @@ export const apply = mountOnce(name, function apply(ctx, rawConfig) {
|
|
|
1124
1600
|
|
|
1125
1601
|
const patch = body.patch !== null && typeof body.patch === 'object' ? body.patch : {}
|
|
1126
1602
|
const current =
|
|
1127
|
-
scope === 'global'
|
|
1603
|
+
scope === 'global'
|
|
1604
|
+
? runtime.state.global
|
|
1605
|
+
: scope === 'workspace'
|
|
1606
|
+
? workspaceEntryFor(workspaceKey) ?? emptyEntry(config.defaultMode)
|
|
1607
|
+
: runtime.state.sessions[sessionId] ?? emptyEntry(config.defaultMode)
|
|
1128
1608
|
// 逐字段合并:只写用户真正提交的键,避免面板把没传的键清掉。
|
|
1129
1609
|
const next = {
|
|
1130
1610
|
enabled: typeof patch.enabled === 'boolean' ? patch.enabled : current.enabled,
|
|
@@ -1143,6 +1623,7 @@ export const apply = mountOnce(name, function apply(ctx, rawConfig) {
|
|
|
1143
1623
|
}
|
|
1144
1624
|
|
|
1145
1625
|
if (scope === 'global') runtime.state.global = next
|
|
1626
|
+
else if (scope === 'workspace') runtime.state.workspaces[workspaceKey] = next
|
|
1146
1627
|
else runtime.state.sessions[sessionId] = next
|
|
1147
1628
|
const persisted = await applyState()
|
|
1148
1629
|
return { status: 200, body: { ...snapshot(sessionId), persisted } }
|
|
@@ -1240,6 +1721,7 @@ export const __internals = {
|
|
|
1240
1721
|
emptyEntry,
|
|
1241
1722
|
textOfMessage,
|
|
1242
1723
|
deriveOrders,
|
|
1724
|
+
workspaceKeyOf,
|
|
1243
1725
|
isLoopbackRequest,
|
|
1244
1726
|
isIPv4Loopback,
|
|
1245
1727
|
isLoopbackHostname,
|