dsh-subagent-dispatch-control 0.0.0-stage → 1.0.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 -0
- package/README.md +183 -2
- package/client.js +9781 -0
- package/cordis.patch.yml +13 -0
- package/docs/BOOST-EVIDENCE.md +259 -0
- package/docs/CONFORMANCE.md +236 -0
- package/docs/FACTS-CONTRACT.md +368 -0
- package/docs/FLOW-COVERAGE.md +215 -0
- package/docs/PACKAGE-README.md +858 -0
- package/docs/RANGE-PLAN.md +357 -0
- package/docs/SECURITY-NOTES.md +110 -0
- package/docs/SPLIT-PLAN.md +604 -0
- package/docs/STAGE3-CONTRACT.md +287 -0
- package/docs/TIER-ROUTING.md +275 -0
- package/docs/badge/CONFORMANCE.md +246 -0
- package/docs/badge/DETAIL.md +319 -0
- package/docs/badge/PROJECTIONS.md +264 -0
- package/docs/badge/TEST-PLAN.md +252 -0
- package/docs/images/badge-menu.png +0 -0
- package/docs/images/dispatch.png +0 -0
- package/docs/images/header.png +0 -0
- package/docs/images/pair-header.png +0 -0
- package/docs/images/pair-panel.png +0 -0
- package/docs/images/panel.png +0 -0
- package/docs/wiki/Home.md +43 -0
- package/docs/wiki//344/275/277/347/224/250/346/214/207/345/215/227.md +97 -0
- package/docs/wiki//345/256/211/350/243/205/344/270/216/345/215/207/347/272/247.md +87 -0
- package/docs/wiki//346/225/205/351/232/234/346/216/222/346/237/245.md +94 -0
- package/icon.svg +12 -0
- package/lib/badge-policy.mjs +502 -0
- package/lib/badge-sync.mjs +385 -0
- package/lib/command.mjs +490 -0
- package/lib/completion.mjs +288 -0
- package/lib/concurrency.mjs +2555 -0
- package/lib/dispatch-command.mjs +181 -0
- package/lib/dispatch-gate.mjs +924 -0
- package/lib/facts-store.mjs +183 -0
- package/lib/gate-state.mjs +465 -0
- package/lib/gate.mjs +886 -0
- package/lib/index.js +309 -0
- package/lib/inject.mjs +195 -0
- package/lib/interrupt.mjs +376 -0
- package/lib/ledger.mjs +948 -0
- package/lib/lifecycle.mjs +274 -0
- package/lib/messages.mjs +532 -0
- package/lib/notice.mjs +769 -0
- package/lib/plan-gate.mjs +385 -0
- package/lib/projection.mjs +978 -0
- package/lib/report.mjs +290 -0
- package/lib/route.mjs +346 -0
- package/lib/run-registry.mjs +402 -0
- package/lib/services.mjs +293 -0
- package/lib/stuck.mjs +567 -0
- package/lib/tier-config.mjs +212 -0
- package/lib/tier-policy.mjs +167 -0
- package/lib/tier-state.mjs +137 -0
- package/lib/todo-gate.mjs +1672 -0
- package/lib/tools.mjs +2912 -0
- package/locale/en.json +9 -0
- package/locale/zh.json +9 -0
- package/package.json +83 -4
- package/vendor/node_modules/@deepseek-ai/cosmokit/LICENSE +21 -0
- package/vendor/node_modules/@deepseek-ai/cosmokit/README.md +30 -0
- package/vendor/node_modules/@deepseek-ai/cosmokit/lib/index.js +418 -0
- package/vendor/node_modules/@deepseek-ai/cosmokit/package.json +32 -0
- package/vendor/node_modules/@deepseek-ai/cosmokit/src/array.ts +42 -0
- package/vendor/node_modules/@deepseek-ai/cosmokit/src/index.ts +12 -0
- package/vendor/node_modules/@deepseek-ai/cosmokit/src/misc.ts +78 -0
- package/vendor/node_modules/@deepseek-ai/cosmokit/src/string.ts +113 -0
- package/vendor/node_modules/@deepseek-ai/cosmokit/src/time.ts +92 -0
- package/vendor/node_modules/@deepseek-ai/cosmokit/src/types.ts +168 -0
- package/vendor/node_modules/@deepseek-ai/cosmokit/src/volatile.ts +86 -0
- package/vendor/node_modules/@deepseek-ai/schemastery/LICENSE +21 -0
- package/vendor/node_modules/@deepseek-ai/schemastery/README.md +393 -0
- package/vendor/node_modules/@deepseek-ai/schemastery/lib/index.cjs +635 -0
- package/vendor/node_modules/@deepseek-ai/schemastery/lib/index.mjs +635 -0
- package/vendor/node_modules/@deepseek-ai/schemastery/package.json +39 -0
- package/vendor/node_modules/@deepseek-ai/schemastery/src/index.ts +962 -0
- package/vendor/node_modules/@standard-schema/spec/LICENSE +21 -0
- package/vendor/node_modules/@standard-schema/spec/README.md +198 -0
- package/vendor/node_modules/@standard-schema/spec/dist/index.cjs +18 -0
- package/vendor/node_modules/@standard-schema/spec/dist/index.js +0 -0
- package/vendor/node_modules/@standard-schema/spec/package.json +39 -0
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 徽标同步(【变更 2026-11-27 档位 UI 同步】新增模块)。
|
|
3
|
+
*
|
|
4
|
+
* 单一职责(W4-17 修正语义):把 /tier 命令写下的**档位绑定**(deep / standard / cheap
|
|
5
|
+
* → 一张具体路由)同步进 01-route-badge 的宿主 settings 会话表,让徽标 UI 的
|
|
6
|
+
* 「深度思考代理模型」等二级菜单立即显示绑定结果。第四波的「切换当前档位」语义已废弃:
|
|
7
|
+
* 本模块**不改变**会话当前使用的模型(当前选择 = 旧三键 provider/model/effort)。
|
|
8
|
+
*
|
|
9
|
+
* 【变更 2026-11-27 档位配置】/tier 改为「编辑各档位绑定的模型」后新增一组
|
|
10
|
+
* 档位绑定字段 API(patchBadgeEntry / syncTierBinding / clearTierBindingFields /
|
|
11
|
+
* readTierBindings):它们只增删改本会话条目上的 deepProvider/deepModel/deepEffort、
|
|
12
|
+
* standardProvider/…、cheapProvider/… 键,**绝不**写旧三键 provider/model/effort
|
|
13
|
+
* (那是 01 徽标的「当前选择」)——「编辑档位绑定」与「切换当前模型」在字段层面隔离。
|
|
14
|
+
*
|
|
15
|
+
* 数据通道(与 01 桥接写入 / 02 readUiSelection 同一张表,三方契约):
|
|
16
|
+
* - settings namespace `subagent-route-badge` 的 `sessions` 键存 JSON 会话表
|
|
17
|
+
* `{ [sessionId]: entry }`;本模块写的 entry 形如
|
|
18
|
+
* `{ provider, model, effort, tier, updatedBy: 'tier-command' }`;
|
|
19
|
+
* - 01 徽标 client(hostTierRouteOf)只认领 `updatedBy === 'tier-command'`
|
|
20
|
+
* 且 provider/model 非空的条目,经 form.subscribe(adopt) 在快照每次变更时
|
|
21
|
+
* 单向采纳到本地选择——这就是「徽标 UI 采纳机制」,本模块只负责把条目写对;
|
|
22
|
+
* - 02 自己的读取路径(services.readUiSelection)只取 provider/model/effort,
|
|
23
|
+
* 多出来的 tier / updatedBy 键对它透明;
|
|
24
|
+
* - W4-28 修正:本模块**只写空串哨兵**、绝不写非空旧三键 —— badgeOpsOf 每次
|
|
25
|
+
* 随写 provider='' / model='' / effort=''(与 01 buildOps 同款、同键序),
|
|
26
|
+
* 让旧版徽标回到「继承」;非空旧三键(= 01 的当前选择)永不触碰。
|
|
27
|
+
*
|
|
28
|
+
* 设计约束(六条硬约束同款纪律):
|
|
29
|
+
* 1. 可选服务一律 ctx.get?.() + try/catch 静默降级(第 4 条)——settings 缺失 /
|
|
30
|
+
* badge 命名空间不存在 / describe·mutate 抛错一律折成 { ok:false, reason },
|
|
31
|
+
* 绝不向命令通道冒泡,更不能让 /tier 因为徽标没装而失败;
|
|
32
|
+
* 2. 纯函数收口:parseSessions / mergeBadgeEntry 零依赖零 I/O(与 01 client 的
|
|
33
|
+
* mergeSessions 同一套语义:脏值重建、删旧再插、64 条上限淘汰最旧)。
|
|
34
|
+
* W4-17:触 ctx 的**不止**旧三 API —— 新四 API 里的 patchBadgeEntry /
|
|
35
|
+
* readTierBindings(及 syncTierBinding / clearTierBindingFields 的底层)同样读
|
|
36
|
+
* ctx.get('settings')。旧三 API(readBadgeSession / syncBadgeTier / clearBadgeTier)
|
|
37
|
+
* 已无生产调用,标注 @deprecated(见各函数)。
|
|
38
|
+
* 3. 会话表是**共享数据**:本模块只增删本会话条目,其它会话的条目原样保留;
|
|
39
|
+
* 重建(脏 JSON)只发生在整张表已不可解析时——那时旧表已不可信,按空表写
|
|
40
|
+
* 是唯一诚实的选项(与 01 侧同款)。
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
import { UI_SELECTION_NS } from './services.mjs'
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 徽标宿主 settings 命名空间(与 01-route-badge client.js 的 NS 逐字一致)。
|
|
47
|
+
*
|
|
48
|
+
* 单一来源纪律(test-contract §C2.4):02 的 lib/ 下该字面量只允许出现在
|
|
49
|
+
* services.mjs(UI_SELECTION_NS)一处 —— 这里引用它,而不是再写一份副本。
|
|
50
|
+
*/
|
|
51
|
+
export const BADGE_NS = UI_SELECTION_NS
|
|
52
|
+
|
|
53
|
+
/** 会话表上限:超出删最旧(插入序第一个 key 即最旧,与 01 侧 SESSIONS_CAP 同值)。 */
|
|
54
|
+
export const SESSIONS_CAP = 64
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 安全解析 sessions JSON 会话表。脏值一律 → {}(空表):
|
|
58
|
+
* 非字符串 / 空串 / 非法 JSON / 数组 / null / 原始值都按「表为空」处理,
|
|
59
|
+
* 绝不让一份坏数据把读写通道整个堵死(与 01 侧 mergeSessions 同款语义)。
|
|
60
|
+
*
|
|
61
|
+
* @param {unknown} raw descriptor.value.sessions(JSON 字符串或任何脏值)
|
|
62
|
+
* @returns {Record<string, object>} 解析出的对象表;不可解析 → {}
|
|
63
|
+
*/
|
|
64
|
+
export function parseSessions(raw) {
|
|
65
|
+
if (typeof raw !== 'string' || raw === '') return {}
|
|
66
|
+
try {
|
|
67
|
+
const parsed = JSON.parse(raw)
|
|
68
|
+
if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) return {}
|
|
69
|
+
return parsed
|
|
70
|
+
} catch {
|
|
71
|
+
return {}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 会话表读-改-写:删除本会话旧键后按需重插(重选移到表尾),超出上限删最旧。
|
|
77
|
+
*
|
|
78
|
+
* - entry 为 undefined → 只删本会话条目(继承语义),其它会话原样保留;
|
|
79
|
+
* - entry 为对象 → 先删后插:键移到表尾,让淘汰「第一个 key」删掉的恰好是
|
|
80
|
+
* **最久未更新**的会话(纯插入序会把刚重选的旧会话误淘汰,01 侧同款);
|
|
81
|
+
* - 超出 SESSIONS_CAP(64)条 → 从表头(最旧)开始删除;
|
|
82
|
+
* - table 非对象(脏值)→ 按空表重建,绝不抛错。
|
|
83
|
+
*
|
|
84
|
+
* JSON 对象保持插入序(ES2015+);条目键是 sessionId 字符串(session-xxx /
|
|
85
|
+
* uuid 形状,非纯数字),不触发整数键的数值排序例外。
|
|
86
|
+
*
|
|
87
|
+
* @param {unknown} table 当前会话表(parseSessions 的产物或脏值)
|
|
88
|
+
* @param {string} sessionKey 会话标识
|
|
89
|
+
* @param {object|undefined} [entry] 要写入的本会话条目;undefined = 只删
|
|
90
|
+
* @returns {Record<string, object>} 新表(输入不被原地修改)
|
|
91
|
+
*/
|
|
92
|
+
export function mergeBadgeEntry(table, sessionKey, entry) {
|
|
93
|
+
const source = table !== null && typeof table === 'object' && !Array.isArray(table) ? table : {}
|
|
94
|
+
const next = { ...source }
|
|
95
|
+
if (typeof sessionKey === 'string' && sessionKey !== '') {
|
|
96
|
+
delete next[sessionKey]
|
|
97
|
+
if (entry !== null && typeof entry === 'object') {
|
|
98
|
+
next[sessionKey] = entry
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
const keys = Object.keys(next)
|
|
102
|
+
const excess = keys.length - SESSIONS_CAP
|
|
103
|
+
for (let index = 0; index < excess; index += 1) delete next[keys[index]]
|
|
104
|
+
return next
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* 读宿主会话表里**本会话**的徽标条目(status 对照用)。
|
|
109
|
+
*
|
|
110
|
+
* 返回形状与 services.readUiSelection 同款约定:
|
|
111
|
+
* - effort 非空 → 带 reasoningEffort;空串 / 缺失 → 省略(= 模型默认);
|
|
112
|
+
* - tier 非空字符串 → 带 tier(/tier 写下的档位名),否则省略;
|
|
113
|
+
* - 服务缺失 / 无命名空间 / 脏表 / 条目残缺(provider 或 model 非非空字符串)/
|
|
114
|
+
* 会话键非法 / 任何异常 → undefined(「读不到」不冒充任何结论)。
|
|
115
|
+
*
|
|
116
|
+
* @param {object} ctx cordis context(只做可选读取)
|
|
117
|
+
* @param {string|undefined} sessionKey 会话标识(sessionKeyOf 取得)
|
|
118
|
+
* @returns {Promise<{provider: string, model: string, reasoningEffort?: string, tier?: string}|undefined>}
|
|
119
|
+
*/
|
|
120
|
+
/** @deprecated W4-05:生产 0 引用(仅 test-badge-sync 覆盖)。按 docs/TIER-ROUTING.md 的
|
|
121
|
+
* 绑定语义应读 readTierBindings;保留只为兼容旧调用方,勿在新代码使用。 */
|
|
122
|
+
export async function readBadgeSession(ctx, sessionKey) {
|
|
123
|
+
try {
|
|
124
|
+
if (typeof sessionKey !== 'string' || sessionKey === '') return undefined
|
|
125
|
+
const settings = ctx?.get?.('settings')
|
|
126
|
+
if (settings === undefined || settings === null || typeof settings.describe !== 'function') return undefined
|
|
127
|
+
const descriptors = await settings.describe()
|
|
128
|
+
if (!Array.isArray(descriptors)) return undefined
|
|
129
|
+
const found = descriptors.find((descriptor) => descriptor?.ns === BADGE_NS)
|
|
130
|
+
if (found === undefined) return undefined
|
|
131
|
+
const table = parseSessions(found.value?.sessions)
|
|
132
|
+
const entry = table[sessionKey]
|
|
133
|
+
if (entry === null || typeof entry !== 'object' || Array.isArray(entry)) return undefined
|
|
134
|
+
const provider = entry.provider
|
|
135
|
+
const model = entry.model
|
|
136
|
+
if (typeof provider !== 'string' || provider === '' || typeof model !== 'string' || model === '') {
|
|
137
|
+
return undefined
|
|
138
|
+
}
|
|
139
|
+
const effort = entry.effort
|
|
140
|
+
const tier = entry.tier
|
|
141
|
+
return {
|
|
142
|
+
provider,
|
|
143
|
+
model,
|
|
144
|
+
...(typeof effort === 'string' && effort !== '' ? { reasoningEffort: effort } : {}),
|
|
145
|
+
...(typeof tier === 'string' && tier !== '' ? { tier } : {}),
|
|
146
|
+
}
|
|
147
|
+
} catch {
|
|
148
|
+
return undefined
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* 构造与 01 桥接 buildOps 同款、同键序的写 ops:旧三键空串哨兵 + sessions 表。
|
|
154
|
+
* (单点抽取:三键顺序 provider → model → effort → sessions 逐字对齐 01 侧,
|
|
155
|
+
* 改名前先看 01 client.js 的 buildOps 与 test-contract.mjs §C5。)
|
|
156
|
+
*
|
|
157
|
+
* @param {Record<string, object>} nextTable mergeBadgeEntry 产物
|
|
158
|
+
* @returns {Array<{op: 'set', path: string[], value: string}>}
|
|
159
|
+
*/
|
|
160
|
+
const badgeOpsOf = (nextTable) => [
|
|
161
|
+
{ op: 'set', path: ['provider'], value: '' },
|
|
162
|
+
{ op: 'set', path: ['model'], value: '' },
|
|
163
|
+
{ op: 'set', path: ['effort'], value: '' },
|
|
164
|
+
{ op: 'set', path: ['sessions'], value: JSON.stringify(nextTable) },
|
|
165
|
+
]
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* 把 /tier 命令确认的档位路由写进宿主徽标会话表(深写入口)。
|
|
169
|
+
*
|
|
170
|
+
* 写入前经 describe() 取 descriptor.revision(乐观并发:徽标若在我们读表之后
|
|
171
|
+
* 写过,mutate 会因 revision 不匹配抛错 → 折成 { ok:false, reason },由命令
|
|
172
|
+
* 回执如实告知「界面可能仍显示旧模型」,绝不静默假装同步成功)。
|
|
173
|
+
*
|
|
174
|
+
* @param {object} ctx cordis context
|
|
175
|
+
* @param {string} sessionKey 会话标识
|
|
176
|
+
* @param {{provider: string, model: string, effort?: string}} route 确认档位解析出的路由
|
|
177
|
+
* @param {unknown} tier 档位 id('deep'|'standard'|'cheap')
|
|
178
|
+
* @returns {Promise<{ok: true}|{ok: false, reason: string}>}
|
|
179
|
+
*/
|
|
180
|
+
/** @deprecated W4-05:生产 0 引用。**危险**:它写旧三键 provider/model/effort
|
|
181
|
+
* (即 01 的「当前选择」),与 /tier「不改变当前模型」的不变量冲突;
|
|
182
|
+
* 新代码一律用 syncTierBinding(只写 tier* 绑定字段)。 */
|
|
183
|
+
export async function syncBadgeTier(ctx, sessionKey, route, tier) {
|
|
184
|
+
try {
|
|
185
|
+
const settings = ctx?.get?.('settings')
|
|
186
|
+
if (settings === undefined || settings === null
|
|
187
|
+
|| typeof settings.describe !== 'function' || typeof settings.mutate !== 'function') {
|
|
188
|
+
return { ok: false, reason: 'settings-unavailable' }
|
|
189
|
+
}
|
|
190
|
+
const descriptors = await settings.describe()
|
|
191
|
+
if (!Array.isArray(descriptors)) return { ok: false, reason: 'settings-unavailable' }
|
|
192
|
+
const descriptor = descriptors.find((item) => item?.ns === BADGE_NS)
|
|
193
|
+
if (descriptor === undefined) return { ok: false, reason: 'badge-namespace-missing' }
|
|
194
|
+
const table = parseSessions(descriptor.value?.sessions)
|
|
195
|
+
const entry = {
|
|
196
|
+
provider: route?.provider ?? '',
|
|
197
|
+
model: route?.model ?? '',
|
|
198
|
+
effort: route?.effort ?? '',
|
|
199
|
+
tier,
|
|
200
|
+
updatedBy: 'tier-command',
|
|
201
|
+
}
|
|
202
|
+
const ops = badgeOpsOf(mergeBadgeEntry(table, sessionKey, entry))
|
|
203
|
+
await settings.mutate(BADGE_NS, ops, descriptor.revision)
|
|
204
|
+
return { ok: true }
|
|
205
|
+
} catch (error) {
|
|
206
|
+
return { ok: false, reason: String(error?.message ?? error) }
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* 清除本会话的徽标条目(/tier inherit 恢复继承时调用;只删本会话,其它会话
|
|
212
|
+
* 的条目原样保留)。失败语义与 syncBadgeTier 一致:折成 { ok:false, reason }。
|
|
213
|
+
*
|
|
214
|
+
* @param {object} ctx cordis context
|
|
215
|
+
* @param {string} sessionKey 会话标识
|
|
216
|
+
* @returns {Promise<{ok: true}|{ok: false, reason: string}>}
|
|
217
|
+
*/
|
|
218
|
+
/** @deprecated W4-05:生产 0 引用(仅 test-badge-sync 覆盖);新代码用 clearTierBindingFields。 */
|
|
219
|
+
export async function clearBadgeTier(ctx, sessionKey) {
|
|
220
|
+
try {
|
|
221
|
+
const settings = ctx?.get?.('settings')
|
|
222
|
+
if (settings === undefined || settings === null
|
|
223
|
+
|| typeof settings.describe !== 'function' || typeof settings.mutate !== 'function') {
|
|
224
|
+
return { ok: false, reason: 'settings-unavailable' }
|
|
225
|
+
}
|
|
226
|
+
const descriptors = await settings.describe()
|
|
227
|
+
if (!Array.isArray(descriptors)) return { ok: false, reason: 'settings-unavailable' }
|
|
228
|
+
const descriptor = descriptors.find((item) => item?.ns === BADGE_NS)
|
|
229
|
+
if (descriptor === undefined) return { ok: false, reason: 'badge-namespace-missing' }
|
|
230
|
+
const table = parseSessions(descriptor.value?.sessions)
|
|
231
|
+
const ops = badgeOpsOf(mergeBadgeEntry(table, sessionKey, undefined))
|
|
232
|
+
await settings.mutate(BADGE_NS, ops, descriptor.revision)
|
|
233
|
+
return { ok: true }
|
|
234
|
+
} catch (error) {
|
|
235
|
+
return { ok: false, reason: String(error?.message ?? error) }
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// ═══ 【变更 2026-11-27 档位配置】/tier 编辑档位绑定字段(会话表条目内的 tier* 键)═══
|
|
240
|
+
//
|
|
241
|
+
// /tier 的语义改为「编辑各档位绑定的模型」后,徽标宿主表的本会话条目上新增一组
|
|
242
|
+
// 档位绑定字段:deepProvider / deepModel / deepEffort、standardProvider / …、
|
|
243
|
+
// cheapProvider / …。它们与旧三键 provider/model/effort(**当前界面选择**)活在
|
|
244
|
+
// 同一个条目对象里,但语义互斥:本组 API 只增删改 tier* 绑定键,**绝不**写
|
|
245
|
+
// provider/model/effort —— 那三个键是 01 徽标的「当前选择」,写它们就等于替用户
|
|
246
|
+
// 切换了当前模型(/tier 明令禁止的事)。旧三键空串哨兵随 mutate 原样发出(01 侧
|
|
247
|
+
// buildOps 同款键序不变),但值恒为空串哨兵本身,不带任何命令写入的路由。
|
|
248
|
+
|
|
249
|
+
/** 三档绑定字段的键前缀(tier id 原样作前缀:deep/standard/cheap)。 */
|
|
250
|
+
const TIER_FIELD_PREFIXES = ['deep', 'standard', 'cheap']
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* 【变更 2026-11-27 档位配置】读本会话条目上的档位绑定字段。
|
|
254
|
+
* 纯函数:脏字段(类型不对 / 空串 / 缺失)一律按「未绑定」跳过,绝不编造。
|
|
255
|
+
*
|
|
256
|
+
* @param {object|undefined} entry 本会话条目(parseSessions 产物的一格)
|
|
257
|
+
* @returns {{deep?: {provider, model, effort?}, standard?: {...}, cheap?: {...}}}
|
|
258
|
+
* 只含「provider+model 都非空字符串」的档位;一个都没有 → {}。
|
|
259
|
+
*/
|
|
260
|
+
function tierFieldsOf(entry) {
|
|
261
|
+
const out = {}
|
|
262
|
+
if (entry === null || typeof entry !== 'object' || Array.isArray(entry)) return out
|
|
263
|
+
for (const prefix of TIER_FIELD_PREFIXES) {
|
|
264
|
+
const provider = entry[prefix + 'Provider']
|
|
265
|
+
const model = entry[prefix + 'Model']
|
|
266
|
+
if (typeof provider !== 'string' || provider === '' || typeof model !== 'string' || model === '') continue
|
|
267
|
+
const effort = entry[prefix + 'Effort']
|
|
268
|
+
out[prefix] = { provider, model, ...(typeof effort === 'string' && effort !== '' ? { effort } : {}) }
|
|
269
|
+
}
|
|
270
|
+
return out
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* 【变更 2026-11-27 档位配置】对宿主徽标会话表里**本会话条目**做一次字段级合并写。
|
|
275
|
+
*
|
|
276
|
+
* 语义(纯读-改-写,绝不发明第二张表):
|
|
277
|
+
* ① describe() 取徽标 descriptor(revision 乐观并发,与 syncBadgeTier 同款);
|
|
278
|
+
* ② 取本会话现有 entry(没有 → {} 从零起,其它会话条目原样保留);
|
|
279
|
+
* ③ 把 patch 逐键合并进 entry:值为 null → 删除该字段;否则原样赋值——
|
|
280
|
+
* 既有字段(provider/model/effort/tier/updatedBy/…)不碰的**原样保留**;
|
|
281
|
+
* ④ mutate(旧三键哨兵 + sessions 表,键序与 01 桥接逐字一致)。
|
|
282
|
+
*
|
|
283
|
+
* 失败语义与 syncBadgeTier 完全一致:settings 缺失 / 无 ns / mutate 抛错(含
|
|
284
|
+
* revision 冲突)一律折成 { ok:false, reason },绝不向命令通道冒泡。
|
|
285
|
+
*
|
|
286
|
+
* @param {object} ctx cordis context
|
|
287
|
+
* @param {string} sessionKey 会话标识
|
|
288
|
+
* @param {Record<string, unknown>} patch 字段级补丁(null = 删除该字段)
|
|
289
|
+
* @returns {Promise<{ok: true}|{ok: false, reason: string}>}
|
|
290
|
+
*/
|
|
291
|
+
export async function patchBadgeEntry(ctx, sessionKey, patch) {
|
|
292
|
+
try {
|
|
293
|
+
if (patch === null || typeof patch !== 'object' || Array.isArray(patch)) {
|
|
294
|
+
return { ok: false, reason: 'bad-patch' }
|
|
295
|
+
}
|
|
296
|
+
const settings = ctx?.get?.('settings')
|
|
297
|
+
if (settings === undefined || settings === null
|
|
298
|
+
|| typeof settings.describe !== 'function' || typeof settings.mutate !== 'function') {
|
|
299
|
+
return { ok: false, reason: 'settings-unavailable' }
|
|
300
|
+
}
|
|
301
|
+
const descriptors = await settings.describe()
|
|
302
|
+
if (!Array.isArray(descriptors)) return { ok: false, reason: 'settings-unavailable' }
|
|
303
|
+
const descriptor = descriptors.find((item) => item?.ns === BADGE_NS)
|
|
304
|
+
if (descriptor === undefined) return { ok: false, reason: 'badge-namespace-missing' }
|
|
305
|
+
const table = parseSessions(descriptor.value?.sessions)
|
|
306
|
+
const existing = table[sessionKey]
|
|
307
|
+
const entry = existing !== null && typeof existing === 'object' && !Array.isArray(existing)
|
|
308
|
+
? { ...existing }
|
|
309
|
+
: {}
|
|
310
|
+
for (const [key, value] of Object.entries(patch)) {
|
|
311
|
+
if (value === null) delete entry[key]
|
|
312
|
+
else entry[key] = value
|
|
313
|
+
}
|
|
314
|
+
const ops = badgeOpsOf(mergeBadgeEntry(table, sessionKey, entry))
|
|
315
|
+
await settings.mutate(BADGE_NS, ops, descriptor.revision)
|
|
316
|
+
return { ok: true }
|
|
317
|
+
} catch (error) {
|
|
318
|
+
return { ok: false, reason: String(error?.message ?? error) }
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* 【变更 2026-11-27 档位配置】把一个档位的绑定路由写成本会话条目上的 tier* 字段。
|
|
324
|
+
* 只写 tier+'Provider/Model/Effort' 三个键(effort 缺省写空串哨兵 = 模型默认),
|
|
325
|
+
* 绝不触碰 provider/model/effort(当前选择)。通道是 patchBadgeEntry。
|
|
326
|
+
*
|
|
327
|
+
* @param {object} ctx cordis context
|
|
328
|
+
* @param {string} sessionKey 会话标识
|
|
329
|
+
* @param {unknown} tier 档位 id('deep'|'standard'|'cheap')
|
|
330
|
+
* @param {{provider: string, model: string, effort?: string}} route 绑定的路由
|
|
331
|
+
* @returns {Promise<{ok: true}|{ok: false, reason: string}>}
|
|
332
|
+
*/
|
|
333
|
+
export async function syncTierBinding(ctx, sessionKey, tier, route) {
|
|
334
|
+
return patchBadgeEntry(ctx, sessionKey, {
|
|
335
|
+
[String(tier) + 'Provider']: route?.provider ?? '',
|
|
336
|
+
[String(tier) + 'Model']: route?.model ?? '',
|
|
337
|
+
[String(tier) + 'Effort']: route?.effort ?? '',
|
|
338
|
+
})
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* 【变更 2026-11-27 档位配置】删除本会话条目上给定档位的 tier* 绑定字段。
|
|
343
|
+
* tiers 省略 = 清全部三档(/tier 继承);给定子集只清对应字段(/tier 深度 继承)。
|
|
344
|
+
*
|
|
345
|
+
* @param {object} ctx cordis context
|
|
346
|
+
* @param {string} sessionKey 会话标识
|
|
347
|
+
* @param {Array<'deep'|'standard'|'cheap'>} [tiers=[]] 要清的档位;空 = 全部三档
|
|
348
|
+
* @returns {Promise<{ok: true}|{ok: false, reason: string}>}
|
|
349
|
+
*/
|
|
350
|
+
export async function clearTierBindingFields(ctx, sessionKey, tiers = []) {
|
|
351
|
+
const list = Array.isArray(tiers) && tiers.length > 0 ? tiers : TIER_FIELD_PREFIXES
|
|
352
|
+
const patch = {}
|
|
353
|
+
for (const tier of list) {
|
|
354
|
+
patch[String(tier) + 'Provider'] = null
|
|
355
|
+
patch[String(tier) + 'Model'] = null
|
|
356
|
+
patch[String(tier) + 'Effort'] = null
|
|
357
|
+
}
|
|
358
|
+
return patchBadgeEntry(ctx, sessionKey, patch)
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* 【变更 2026-11-27 档位配置】读本会话条目上的全部档位绑定(查看用,只读不写)。
|
|
363
|
+
*
|
|
364
|
+
* @param {object} ctx cordis context
|
|
365
|
+
* @param {string} sessionKey 会话标识
|
|
366
|
+
* @returns {Promise<{deep?: {provider, model, effort?}, standard?: {...}, cheap?: {...}}|undefined>}
|
|
367
|
+
* settings 缺失 / 无 ns / 脏表 / 会话键非法 / 异常 → undefined(读不到不冒充结论);
|
|
368
|
+
* 条目存在但一个绑定都没有 → {}(空对象 = 「条目在、绑定未设置」,与 undefined 区分)。
|
|
369
|
+
*/
|
|
370
|
+
export async function readTierBindings(ctx, sessionKey) {
|
|
371
|
+
try {
|
|
372
|
+
if (typeof sessionKey !== 'string' || sessionKey === '') return undefined
|
|
373
|
+
const settings = ctx?.get?.('settings')
|
|
374
|
+
if (settings === undefined || settings === null || typeof settings.describe !== 'function') return undefined
|
|
375
|
+
const descriptors = await settings.describe()
|
|
376
|
+
if (!Array.isArray(descriptors)) return undefined
|
|
377
|
+
const found = descriptors.find((descriptor) => descriptor?.ns === BADGE_NS)
|
|
378
|
+
if (found === undefined) return undefined
|
|
379
|
+
const table = parseSessions(found.value?.sessions)
|
|
380
|
+
const entry = table[sessionKey]
|
|
381
|
+
return tierFieldsOf(entry)
|
|
382
|
+
} catch {
|
|
383
|
+
return undefined
|
|
384
|
+
}
|
|
385
|
+
}
|