dsh-chief 1.2.1 → 1.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 +47 -16
- package/README.en.md +5 -3
- package/README.md +5 -3
- package/api-routes.js +1274 -0
- package/archive-cabinets.js +766 -0
- package/archive-core.js +761 -0
- package/archive-dossiers.js +1185 -0
- package/archive-precipitated.js +428 -0
- package/archives.js +63 -2991
- package/board-actions.js +1879 -0
- package/board-schema.js +580 -0
- package/board-store.js +392 -0
- package/chief-approvals.js +841 -0
- package/chief-archives.js +905 -0
- package/chief-commands.js +2513 -0
- package/chief-compaction.js +591 -0
- package/chief-executor.js +196 -22
- package/chief-models.js +578 -0
- package/chief-network.js +720 -0
- package/chief-review.js +1064 -0
- package/chief-state.js +1777 -0
- package/chief-view.js +553 -0
- package/chief.js +94 -8541
- package/client.js +11834 -11638
- package/index.js +154 -6363
- package/model-tools.js +755 -0
- package/package.json +28 -1
- package/runtime-hooks.js +629 -0
- package/subagent-runtime.js +1399 -0
package/chief-models.js
ADDED
|
@@ -0,0 +1,578 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* chief-models.js
|
|
3
|
+
*
|
|
4
|
+
* Model catalog discovery, route cooldown tracking, and ctx.llm invocation for DSH Chief.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
MODEL_TIMEOUT_MS,
|
|
9
|
+
CATALOG_TTL_MS,
|
|
10
|
+
SUMMARY_MAX_TOKENS,
|
|
11
|
+
SUMMARY_RETRY_MAX_TOKENS,
|
|
12
|
+
clamp,
|
|
13
|
+
resolveWorkspaceConfig,
|
|
14
|
+
loadChiefState,
|
|
15
|
+
mutateChiefState,
|
|
16
|
+
} from './chief-state.js'
|
|
17
|
+
|
|
18
|
+
let dshLlmModulePromise = null
|
|
19
|
+
|
|
20
|
+
export async function loadDshLlm() {
|
|
21
|
+
if (!dshLlmModulePromise) {
|
|
22
|
+
dshLlmModulePromise = import('@deepseek-ai/dsh-llm').catch(() => null)
|
|
23
|
+
}
|
|
24
|
+
return dshLlmModulePromise
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
let modelCatalogCache = { at: 0, items: [] }
|
|
28
|
+
|
|
29
|
+
export function invalidateChiefModelCatalog() {
|
|
30
|
+
modelCatalogCache = { at: 0, items: [] }
|
|
31
|
+
unusableRouteUntil.clear()
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function splitModelRef(ref) {
|
|
35
|
+
const clean = String(ref || '').trim()
|
|
36
|
+
const idx = clean.indexOf('/')
|
|
37
|
+
if (idx <= 0 || idx >= clean.length - 1) return null
|
|
38
|
+
return { provider: clean.slice(0, idx), model: clean.slice(idx + 1), ref: clean }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function stripVendorPrefixFromModelId(modelId) {
|
|
42
|
+
const raw = String(modelId || '').trim()
|
|
43
|
+
if (!raw.includes('/')) return raw
|
|
44
|
+
const parts = raw.split('/').filter(Boolean)
|
|
45
|
+
return parts.length > 1 ? parts.slice(1).join('/') : raw
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function shortProviderTag(providerId, providerName) {
|
|
49
|
+
const pid = String(providerId || '').toLowerCase()
|
|
50
|
+
const pname = String(providerName || '').trim()
|
|
51
|
+
if (pid === 'commandcode' || /command\s*code/i.test(pname)) return 'Command Code'
|
|
52
|
+
if (pid.includes('account') || /account/i.test(pname)) return pname || 'DeepSeek Account'
|
|
53
|
+
if (pid === 'our-free-model') return 'Free Model'
|
|
54
|
+
return pname || providerId || ''
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function formatChiefModelLabel(ref, items = [], providers = [], compact = false) {
|
|
58
|
+
const parsed = splitModelRef(ref)
|
|
59
|
+
if (!parsed) return compact ? '自动跟随' : ''
|
|
60
|
+
const hit = Array.isArray(items) ? items.find(it => it && it.ref === parsed.ref) : null
|
|
61
|
+
const provObj = Array.isArray(providers) ? providers.find(p => p && p.id === parsed.provider) : null
|
|
62
|
+
const provLabel = shortProviderTag(parsed.provider, (hit && hit.providerName) || (provObj && (provObj.name || provObj.label)) || parsed.provider)
|
|
63
|
+
const rawModelName = (hit && hit.name) ? String(hit.name).trim() : stripVendorPrefixFromModelId(parsed.model)
|
|
64
|
+
|
|
65
|
+
if (compact) {
|
|
66
|
+
// Keep channel crystal-clear so Command Code DS is never confused with official DeepSeek API
|
|
67
|
+
const shortProv = parsed.provider === 'commandcode' ? 'CC' : provLabel
|
|
68
|
+
const alreadyTagged = /\(CC\)/i.test(rawModelName) && shortProv === 'CC'
|
|
69
|
+
const base = alreadyTagged ? rawModelName : (shortProv ? `${shortProv} · ${rawModelName}` : rawModelName)
|
|
70
|
+
return clamp(base, 24)
|
|
71
|
+
}
|
|
72
|
+
const alreadyHasProv = provLabel && rawModelName.toLowerCase().includes(provLabel.toLowerCase())
|
|
73
|
+
return alreadyHasProv || !provLabel ? rawModelName : `${provLabel} · ${rawModelName}`
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function detectUnkeyedProviders(runtime, llm) {
|
|
77
|
+
const missing = new Set()
|
|
78
|
+
if (!runtime || !llm || typeof llm.listConfigurableProviders !== 'function') return missing
|
|
79
|
+
try {
|
|
80
|
+
const settings = runtime.get('settings')
|
|
81
|
+
const credentials = runtime.get('credentials')
|
|
82
|
+
if (!settings || !credentials || typeof settings.describe !== 'function') return missing
|
|
83
|
+
const sections = new Map((settings.describe({ redactSecrets: true }) || []).map(s => [s.ns, s]))
|
|
84
|
+
for (const entry of (llm.listConfigurableProviders() || [])) {
|
|
85
|
+
if (!entry || !entry.provider || !Array.isArray(entry.settingsPath)) continue
|
|
86
|
+
const sec = sections.get(entry.settingsNs)
|
|
87
|
+
const profile = entry.settingsPath.reduce((v, k) => v?.[k], sec?.value)
|
|
88
|
+
const keyEnv = typeof profile?.apiKeyEnv === 'string' && profile.apiKeyEnv ? profile.apiKeyEnv : ''
|
|
89
|
+
if (keyEnv && process.env[keyEnv] === '') {
|
|
90
|
+
// Explicit empty env hint; async describe handled below when needed
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
} catch {}
|
|
94
|
+
return missing
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function detectMissingProviderKeysAsync(runtime, llm, providerIds) {
|
|
98
|
+
const missing = detectUnkeyedProviders(runtime, llm)
|
|
99
|
+
if (!runtime || !llm || typeof llm.listConfigurableProviders !== 'function') return missing
|
|
100
|
+
try {
|
|
101
|
+
const settings = runtime.get('settings')
|
|
102
|
+
const credentials = runtime.get('credentials')
|
|
103
|
+
if (!settings || !credentials || typeof settings.describe !== 'function' || typeof credentials.describe !== 'function') {
|
|
104
|
+
return missing
|
|
105
|
+
}
|
|
106
|
+
const sections = new Map((settings.describe({ redactSecrets: true }) || []).map(s => [s.ns, s]))
|
|
107
|
+
const keyRefs = new Map()
|
|
108
|
+
for (const entry of (llm.listConfigurableProviders() || [])) {
|
|
109
|
+
if (!entry || !entry.provider || !Array.isArray(entry.settingsPath)) continue
|
|
110
|
+
const sec = sections.get(entry.settingsNs)
|
|
111
|
+
const profile = entry.settingsPath.reduce((v, k) => v?.[k], sec?.value)
|
|
112
|
+
if (typeof profile?.apiKeyEnv === 'string' && profile.apiKeyEnv) {
|
|
113
|
+
keyRefs.set(entry.provider, profile.apiKeyEnv)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
await Promise.all(
|
|
117
|
+
providerIds
|
|
118
|
+
.filter(id => keyRefs.has(id))
|
|
119
|
+
.map(async (id) => {
|
|
120
|
+
const info = await Promise.resolve(credentials.describe(keyRefs.get(id))).catch(() => undefined)
|
|
121
|
+
if (info && info.configured === false) missing.add(id)
|
|
122
|
+
}),
|
|
123
|
+
)
|
|
124
|
+
} catch {}
|
|
125
|
+
return missing
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function extractAgentModelRef(agent) {
|
|
129
|
+
if (!agent) return ''
|
|
130
|
+
try {
|
|
131
|
+
const sess = agent.session
|
|
132
|
+
const header = sess && typeof sess.requestHeader === 'function'
|
|
133
|
+
? sess.requestHeader()
|
|
134
|
+
: (sess && sess.header ? sess.header : null)
|
|
135
|
+
const candidates = [
|
|
136
|
+
header && header.config,
|
|
137
|
+
header,
|
|
138
|
+
sess && sess.config,
|
|
139
|
+
sess && sess.modelSelection,
|
|
140
|
+
agent.modelSelection,
|
|
141
|
+
agent.config,
|
|
142
|
+
]
|
|
143
|
+
for (const cfg of candidates) {
|
|
144
|
+
if (cfg && typeof cfg.provider === 'string' && cfg.provider && typeof cfg.model === 'string' && cfg.model) {
|
|
145
|
+
if (!cfg.provider.startsWith('local-agent')) {
|
|
146
|
+
return `${cfg.provider}/${cfg.model}`
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
} catch {}
|
|
151
|
+
return ''
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function sessionModelRef(deps, sessionId) {
|
|
155
|
+
if (!deps || !deps.runtime) return ''
|
|
156
|
+
try {
|
|
157
|
+
const agents = deps.runtime.get('agents')
|
|
158
|
+
if (!agents || typeof agents.get !== 'function') return ''
|
|
159
|
+
if (sessionId) {
|
|
160
|
+
const direct = extractAgentModelRef(agents.get(sessionId))
|
|
161
|
+
if (direct) return direct
|
|
162
|
+
const rootId = deps.resolveRootSessionId ? deps.resolveRootSessionId(sessionId) : ''
|
|
163
|
+
if (rootId && rootId !== sessionId) {
|
|
164
|
+
const rootRef = extractAgentModelRef(agents.get(rootId))
|
|
165
|
+
if (rootRef) return rootRef
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
} catch {}
|
|
169
|
+
return ''
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function profileDefaultModelRef(runtime) {
|
|
173
|
+
if (!runtime) return ''
|
|
174
|
+
try {
|
|
175
|
+
const sel = runtime.get('agentDefaultModel')?.currentSelection?.()
|
|
176
|
+
if (sel && sel.provider && sel.model && !String(sel.provider).startsWith('local-agent')) {
|
|
177
|
+
return `${sel.provider}/${sel.model}`
|
|
178
|
+
}
|
|
179
|
+
} catch {}
|
|
180
|
+
return ''
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export async function listChiefModels(deps, fresh = false, sessionId = '') {
|
|
184
|
+
const llm = deps.getLlm ? deps.getLlm() : null
|
|
185
|
+
const runtime = deps.runtime
|
|
186
|
+
const state = loadChiefState()
|
|
187
|
+
const rootSid = (deps && deps.resolveRootSessionId ? deps.resolveRootSessionId(sessionId) : '') || String(sessionId || '').trim()
|
|
188
|
+
const effCfg = resolveWorkspaceConfig(state, rootSid)
|
|
189
|
+
let selectedRef = effCfg.modelRef || ''
|
|
190
|
+
|
|
191
|
+
const leadRef = sessionModelRef(deps, sessionId)
|
|
192
|
+
const profileDefaultRef = profileDefaultModelRef(runtime)
|
|
193
|
+
const followRef = leadRef || profileDefaultRef || ''
|
|
194
|
+
|
|
195
|
+
if (!llm || typeof llm.listProviders !== 'function' || typeof llm.listModels !== 'function') {
|
|
196
|
+
const resolvedRef = selectedRef || followRef || ''
|
|
197
|
+
return {
|
|
198
|
+
available: false,
|
|
199
|
+
selectedRef,
|
|
200
|
+
followRef,
|
|
201
|
+
followLabel: formatChiefModelLabel(followRef, [], [], false),
|
|
202
|
+
defaultRef: followRef,
|
|
203
|
+
resolvedRef,
|
|
204
|
+
resolvedLabel: formatChiefModelLabel(resolvedRef, [], [], true),
|
|
205
|
+
models: [],
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const rawProviders = (llm.listProviders() || []).filter(p => p && p.id && !String(p.id).startsWith('local-agent'))
|
|
210
|
+
const registeredProviderIds = new Set(rawProviders.map(p => String(p.id)))
|
|
211
|
+
|
|
212
|
+
// Auto-heal stale selectedRef if its provider was uninstalled/removed (e.g. legacy `opendesign/...`)
|
|
213
|
+
if (selectedRef) {
|
|
214
|
+
const parsedSel = splitModelRef(selectedRef)
|
|
215
|
+
if (!parsedSel || (registeredProviderIds.size > 0 && !registeredProviderIds.has(parsedSel.provider))) {
|
|
216
|
+
selectedRef = ''
|
|
217
|
+
mutateChiefState((s) => {
|
|
218
|
+
s.config.modelRef = ''
|
|
219
|
+
})
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
let items = []
|
|
224
|
+
if (!fresh && modelCatalogCache.items.length > 0 && Date.now() - modelCatalogCache.at < CATALOG_TTL_MS) {
|
|
225
|
+
items = modelCatalogCache.items.slice()
|
|
226
|
+
} else {
|
|
227
|
+
try {
|
|
228
|
+
const missingKeys = await detectMissingProviderKeysAsync(runtime, llm, [...registeredProviderIds])
|
|
229
|
+
for (const provider of rawProviders) {
|
|
230
|
+
const pid = String(provider.id)
|
|
231
|
+
const pname = String(provider.name || provider.label || pid)
|
|
232
|
+
const needsKey = missingKeys.has(pid)
|
|
233
|
+
let models = []
|
|
234
|
+
try {
|
|
235
|
+
models = (await llm.listModels(pid)) || []
|
|
236
|
+
} catch {
|
|
237
|
+
continue
|
|
238
|
+
}
|
|
239
|
+
for (const m of models) {
|
|
240
|
+
if (!m || !m.id) continue
|
|
241
|
+
items.push({
|
|
242
|
+
ref: `${pid}/${m.id}`,
|
|
243
|
+
provider: pid,
|
|
244
|
+
providerName: pname,
|
|
245
|
+
id: String(m.id),
|
|
246
|
+
name: String(m.name || m.label || m.id),
|
|
247
|
+
...(needsKey ? { needsKey: true } : {}),
|
|
248
|
+
})
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
} catch {}
|
|
252
|
+
modelCatalogCache = { at: Date.now(), items: items.slice() }
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// Ensure the active main-conversation model (`followRef`) & `profileDefaultRef` appear in the catalog
|
|
256
|
+
// even when a dynamic provider (e.g. Command Code) only returned a subset in `listModels()`!
|
|
257
|
+
const ensureListed = async (refToEnsure, markCurrent = false) => {
|
|
258
|
+
const parsed = splitModelRef(refToEnsure)
|
|
259
|
+
if (!parsed || !registeredProviderIds.has(parsed.provider)) return
|
|
260
|
+
const existing = items.find(it => it.ref === parsed.ref)
|
|
261
|
+
if (existing) {
|
|
262
|
+
if (markCurrent) existing.isCurrentLead = true
|
|
263
|
+
return
|
|
264
|
+
}
|
|
265
|
+
const provObj = rawProviders.find(p => String(p.id) === parsed.provider)
|
|
266
|
+
const pname = String((provObj && (provObj.name || provObj.label)) || parsed.provider)
|
|
267
|
+
let displayName = stripVendorPrefixFromModelId(parsed.model)
|
|
268
|
+
try {
|
|
269
|
+
if (typeof llm.resolveModelInfo === 'function') {
|
|
270
|
+
const info = await llm.resolveModelInfo(parsed.provider, parsed.model)
|
|
271
|
+
if (info && typeof info.name === 'string' && info.name.trim()) {
|
|
272
|
+
displayName = info.name.trim()
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
} catch {}
|
|
276
|
+
const entry = {
|
|
277
|
+
ref: parsed.ref,
|
|
278
|
+
provider: parsed.provider,
|
|
279
|
+
providerName: pname,
|
|
280
|
+
id: parsed.model,
|
|
281
|
+
name: displayName,
|
|
282
|
+
...(markCurrent ? { isCurrentLead: true } : {}),
|
|
283
|
+
}
|
|
284
|
+
// Insert at the top of its provider group so it is immediately visible without scrolling
|
|
285
|
+
const firstProvIdx = items.findIndex(it => it.provider === parsed.provider)
|
|
286
|
+
if (firstProvIdx >= 0) items.splice(firstProvIdx, 0, entry)
|
|
287
|
+
else items.unshift(entry)
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
await ensureListed(followRef, true)
|
|
291
|
+
if (profileDefaultRef && profileDefaultRef !== followRef) {
|
|
292
|
+
await ensureListed(profileDefaultRef, false)
|
|
293
|
+
}
|
|
294
|
+
if (selectedRef && selectedRef !== followRef && selectedRef !== profileDefaultRef) {
|
|
295
|
+
await ensureListed(selectedRef, false)
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const picked = pickRouteFromItems(items, selectedRef, followRef, deps, sessionId, registeredProviderIds)
|
|
299
|
+
const resolvedRef = (picked && picked.ref) || selectedRef || followRef || ''
|
|
300
|
+
return {
|
|
301
|
+
available: items.length > 0 || Boolean(resolvedRef),
|
|
302
|
+
selectedRef,
|
|
303
|
+
followRef,
|
|
304
|
+
followLabel: formatChiefModelLabel(followRef, items, rawProviders, false),
|
|
305
|
+
defaultRef: followRef,
|
|
306
|
+
resolvedRef,
|
|
307
|
+
resolvedLabel: formatChiefModelLabel(resolvedRef, items, rawProviders, true),
|
|
308
|
+
models: items,
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
const unusableRouteUntil = new Map()
|
|
313
|
+
|
|
314
|
+
function isRouteCoolingDown(ref, provider) {
|
|
315
|
+
const now = Date.now()
|
|
316
|
+
if (ref && (unusableRouteUntil.get(ref) || 0) > now) return true
|
|
317
|
+
if (provider && (unusableRouteUntil.get('prov:' + provider) || 0) > now) return true
|
|
318
|
+
return false
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function markRouteFailure(route, err) {
|
|
322
|
+
if (!route) return
|
|
323
|
+
const msg = String(err && err.message ? err.message : err || '')
|
|
324
|
+
const code = String(err && (err.code || err.failure?.code) ? (err.code || err.failure?.code) : '')
|
|
325
|
+
// Missing API key / auth error / hosted loop engine cannot serve auxiliary completions
|
|
326
|
+
if (/api[_ -]?key|unauthorized|authentication|401|403|not configured|missing key|no key|credentials/i.test(msg) || /AUTH|KEY/i.test(code)) {
|
|
327
|
+
unusableRouteUntil.set('prov:' + route.provider, Date.now() + 90_000)
|
|
328
|
+
} else if (/HOSTED_ENGINE_ROUTE|not a model endpoint|no adapter/i.test(msg) || code === 'HOSTED_ENGINE_ROUTE') {
|
|
329
|
+
unusableRouteUntil.set(route.ref, Date.now() + 300_000)
|
|
330
|
+
} else {
|
|
331
|
+
unusableRouteUntil.set(route.ref, Date.now() + 20_000)
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function buildRouteQueue(items, selectedRef, followRef, deps, sessionId, registeredProviderIds = null) {
|
|
336
|
+
const seen = new Set()
|
|
337
|
+
const out = []
|
|
338
|
+
const isProviderRegistered = (prov) => {
|
|
339
|
+
if (!prov || String(prov).startsWith('local-agent')) return false
|
|
340
|
+
if (registeredProviderIds && registeredProviderIds.size > 0) return registeredProviderIds.has(prov)
|
|
341
|
+
return true
|
|
342
|
+
}
|
|
343
|
+
const pushCand = (cand, allowUnlisted = false) => {
|
|
344
|
+
const parsed = splitModelRef(cand)
|
|
345
|
+
if (!parsed || !isProviderRegistered(parsed.provider)) return
|
|
346
|
+
if (seen.has(parsed.ref)) return
|
|
347
|
+
if (!allowUnlisted && items.length > 0 && !items.some(it => it.ref === parsed.ref && !it.needsKey)) return
|
|
348
|
+
seen.add(parsed.ref)
|
|
349
|
+
out.push(parsed)
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
const liveLeadRef = sessionModelRef(deps, sessionId)
|
|
353
|
+
const primaryFollow = liveLeadRef || followRef || profileDefaultModelRef(deps && deps.runtime)
|
|
354
|
+
|
|
355
|
+
if (selectedRef) {
|
|
356
|
+
// Explicit user-pinned Chief model: try it first, then same-provider models, then main session model
|
|
357
|
+
pushCand(selectedRef, true)
|
|
358
|
+
const selParsed = splitModelRef(selectedRef)
|
|
359
|
+
if (selParsed) {
|
|
360
|
+
for (const it of items) {
|
|
361
|
+
if (it && it.provider === selParsed.provider && !it.needsKey) pushCand(it.ref, false)
|
|
362
|
+
if (out.length >= 3) break
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
if (primaryFollow) pushCand(primaryFollow, true)
|
|
366
|
+
} else if (primaryFollow) {
|
|
367
|
+
// Auto-follow main conversation mode:
|
|
368
|
+
// MUST use the exact provider/model of the main conversation (e.g. `commandcode/deepseek/deepseek-v4.1-flash`)
|
|
369
|
+
// and NEVER silently jump across to a paid official API provider (like `deepseek/...`)!
|
|
370
|
+
pushCand(primaryFollow, true)
|
|
371
|
+
if (followRef && followRef !== primaryFollow) pushCand(followRef, true)
|
|
372
|
+
const followParsed = splitModelRef(primaryFollow)
|
|
373
|
+
if (followParsed) {
|
|
374
|
+
for (const it of items) {
|
|
375
|
+
if (it && it.provider === followParsed.provider && !it.needsKey) pushCand(it.ref, false)
|
|
376
|
+
if (out.length >= 4) break
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
} else {
|
|
380
|
+
// Only when neither a pinned model nor any main/default model exists at all: pick keyed catalog models
|
|
381
|
+
const seenProviders = new Set()
|
|
382
|
+
for (const it of items) {
|
|
383
|
+
if (!it || !it.ref || it.needsKey || seenProviders.has(it.provider)) continue
|
|
384
|
+
seenProviders.add(it.provider)
|
|
385
|
+
pushCand(it.ref, false)
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// Prefer routes not currently cooling down, keeping cooled ones only as last-resort tail
|
|
390
|
+
const healthy = out.filter(r => !isRouteCoolingDown(r.ref, r.provider))
|
|
391
|
+
const cooled = out.filter(r => isRouteCoolingDown(r.ref, r.provider))
|
|
392
|
+
return [...healthy, ...cooled]
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
function pickRouteFromItems(items, selectedRef, followRef, deps, sessionId, registeredProviderIds = null) {
|
|
396
|
+
const queue = buildRouteQueue(items, selectedRef, followRef, deps, sessionId, registeredProviderIds)
|
|
397
|
+
return queue[0] || null
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
async function callOneRoute(llm, route, system, userText, maxTokens = SUMMARY_MAX_TOKENS, sessionId = '', imageBlocks = []) {
|
|
401
|
+
let effort
|
|
402
|
+
let modelDefaultMaxTokens
|
|
403
|
+
try {
|
|
404
|
+
if (typeof llm.resolveModelInfo === 'function') {
|
|
405
|
+
const info = await llm.resolveModelInfo(route.provider, route.model)
|
|
406
|
+
if (typeof info?.defaultMaxTokens === 'number' && info.defaultMaxTokens > 0) {
|
|
407
|
+
modelDefaultMaxTokens = info.defaultMaxTokens
|
|
408
|
+
}
|
|
409
|
+
const efforts = info?.reasoning?.efforts
|
|
410
|
+
if (Array.isArray(efforts) && efforts.length > 0) {
|
|
411
|
+
effort = efforts.some(e => e.id === 'off') ? 'off' : efforts[0]?.id
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
} catch {}
|
|
415
|
+
|
|
416
|
+
const targetBudget = Math.max(Number(maxTokens) || 0, SUMMARY_MAX_TOKENS)
|
|
417
|
+
const firstBudget = modelDefaultMaxTokens ? Math.min(modelDefaultMaxTokens, targetBudget) : targetBudget
|
|
418
|
+
const retryBudget = firstBudget < targetBudget ? targetBudget : SUMMARY_RETRY_MAX_TOKENS
|
|
419
|
+
const budgets = retryBudget > firstBudget ? [firstBudget, retryBudget] : [firstBudget]
|
|
420
|
+
const safeImages = Array.isArray(imageBlocks) ? imageBlocks : []
|
|
421
|
+
|
|
422
|
+
const runOnce = async (budget) => {
|
|
423
|
+
const userContent = [...safeImages, { type: 'text', text: userText || '请查看附图并回答。' }]
|
|
424
|
+
const request = {
|
|
425
|
+
provider: route.provider,
|
|
426
|
+
model: route.model,
|
|
427
|
+
...(sessionId ? { sessionId } : {}),
|
|
428
|
+
...(effort !== undefined ? { reasoningEffort: effort } : {}),
|
|
429
|
+
system,
|
|
430
|
+
messages: [{ role: 'user', content: userContent }],
|
|
431
|
+
maxTokens: budget,
|
|
432
|
+
signal: AbortSignal.timeout(MODEL_TIMEOUT_MS),
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
const dshLlm = await loadDshLlm()
|
|
436
|
+
if (dshLlm && typeof dshLlm.BlockAssembler === 'function') {
|
|
437
|
+
const assembler = new dshLlm.BlockAssembler()
|
|
438
|
+
for await (const chunk of llm.stream(request)) {
|
|
439
|
+
assembler.push(chunk)
|
|
440
|
+
}
|
|
441
|
+
const finish = assembler.finish
|
|
442
|
+
if (finish && (finish.kind === 'error' || finish.kind === 'aborted')) {
|
|
443
|
+
const err = new Error(finish.failure?.message || finish.failure?.code || finish.kind || 'LLM request error')
|
|
444
|
+
err.failure = finish.failure
|
|
445
|
+
throw err
|
|
446
|
+
}
|
|
447
|
+
const text = assembler
|
|
448
|
+
.blocks()
|
|
449
|
+
.filter(b => b && b.type === 'text' && typeof b.text === 'string')
|
|
450
|
+
.map(b => b.text)
|
|
451
|
+
.join('\n')
|
|
452
|
+
.trim()
|
|
453
|
+
return { text, finishKind: finish?.kind || '', modelRef: route.ref }
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
// Pure standalone fallback stream reader (for unit tests / lightweight hosts)
|
|
457
|
+
const parts = []
|
|
458
|
+
let finishKind = ''
|
|
459
|
+
for await (const chunk of llm.stream(request)) {
|
|
460
|
+
if (!chunk || typeof chunk !== 'object') continue
|
|
461
|
+
if (chunk.type === 'text-delta' || chunk.type === 'text_delta') {
|
|
462
|
+
if (typeof chunk.text === 'string') parts.push(chunk.text)
|
|
463
|
+
else if (typeof chunk.delta === 'string') parts.push(chunk.delta)
|
|
464
|
+
} else if (chunk.type === 'text' && typeof chunk.text === 'string') parts.push(chunk.text)
|
|
465
|
+
else if (typeof chunk.content === 'string') parts.push(chunk.content)
|
|
466
|
+
else if (chunk.type === 'finish') {
|
|
467
|
+
finishKind = chunk.reason?.kind || chunk.finishReason || ''
|
|
468
|
+
if (finishKind === 'error') {
|
|
469
|
+
const err = new Error(chunk.reason?.failure?.message || 'LLM stream error')
|
|
470
|
+
err.failure = chunk.reason?.failure
|
|
471
|
+
throw err
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
return { text: parts.join('').trim(), finishKind, modelRef: route.ref }
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
let lastRes = { text: '', finishKind: '', modelRef: route.ref }
|
|
479
|
+
for (let i = 0; i < budgets.length; i += 1) {
|
|
480
|
+
const res = await runOnce(budgets[i])
|
|
481
|
+
lastRes = res
|
|
482
|
+
if (res.text && res.finishKind !== 'max-tokens') {
|
|
483
|
+
return { text: res.text, modelRef: res.modelRef }
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
return { text: lastRes.text, modelRef: lastRes.modelRef }
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
async function filterVisionRoutes(llm, queue) {
|
|
490
|
+
const visionRoutes = []
|
|
491
|
+
const unknownRoutes = []
|
|
492
|
+
for (const route of queue) {
|
|
493
|
+
let info = null
|
|
494
|
+
try {
|
|
495
|
+
if (typeof llm.resolveModelInfo === 'function') {
|
|
496
|
+
info = await Promise.resolve(llm.resolveModelInfo(route.provider, route.model))
|
|
497
|
+
}
|
|
498
|
+
} catch {}
|
|
499
|
+
const mods = Array.isArray(info?.inputModalities)
|
|
500
|
+
? info.inputModalities
|
|
501
|
+
: (Array.isArray(route.inputModalities) ? route.inputModalities : null)
|
|
502
|
+
if (mods) {
|
|
503
|
+
if (mods.includes('image')) visionRoutes.push(route)
|
|
504
|
+
} else {
|
|
505
|
+
unknownRoutes.push(route)
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
return [...visionRoutes, ...unknownRoutes]
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
export async function callChiefLlm(deps, {
|
|
512
|
+
sessionId = '',
|
|
513
|
+
system,
|
|
514
|
+
prompt,
|
|
515
|
+
maxTokens = SUMMARY_MAX_TOKENS,
|
|
516
|
+
imageBlocks = [],
|
|
517
|
+
allowTextFallbackOnVisionMiss = false,
|
|
518
|
+
}) {
|
|
519
|
+
const safeImages = Array.isArray(imageBlocks) ? imageBlocks : []
|
|
520
|
+
if (deps && typeof deps.callLlmOverride === 'function') {
|
|
521
|
+
return deps.callLlmOverride({ sessionId, system, prompt, maxTokens, imageBlocks: safeImages, allowTextFallbackOnVisionMiss })
|
|
522
|
+
}
|
|
523
|
+
const llm = deps.getLlm ? deps.getLlm() : null
|
|
524
|
+
if (!llm || typeof llm.stream !== 'function') {
|
|
525
|
+
return { ok: false, error: '当前环境未挂载 LLM 服务' }
|
|
526
|
+
}
|
|
527
|
+
const catalog = await listChiefModels(deps, false, sessionId)
|
|
528
|
+
const regSet = typeof llm.listProviders === 'function'
|
|
529
|
+
? new Set((llm.listProviders() || []).filter(p => p && p.id && !String(p.id).startsWith('local-agent')).map(p => String(p.id)))
|
|
530
|
+
: null
|
|
531
|
+
const baseQueue = buildRouteQueue(catalog.models, catalog.selectedRef, catalog.followRef || catalog.defaultRef, deps, sessionId, regSet)
|
|
532
|
+
if (!baseQueue.length) {
|
|
533
|
+
return { ok: false, error: '未找到可用的参谋长模型,请在 DSH 设置中配置模型供应商' }
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
let queue = baseQueue
|
|
537
|
+
let sendImages = safeImages
|
|
538
|
+
let degradedToTextOnly = false
|
|
539
|
+
if (safeImages.length > 0) {
|
|
540
|
+
const visionQueue = await filterVisionRoutes(llm, baseQueue)
|
|
541
|
+
if (visionQueue.length > 0) {
|
|
542
|
+
queue = visionQueue
|
|
543
|
+
} else if (allowTextFallbackOnVisionMiss) {
|
|
544
|
+
queue = baseQueue
|
|
545
|
+
sendImages = []
|
|
546
|
+
degradedToTextOnly = true
|
|
547
|
+
} else {
|
|
548
|
+
return {
|
|
549
|
+
ok: false,
|
|
550
|
+
error: '当前参谋长及备选模型均不支持图片输入(仅纯文本),请在右上角切换支持视觉的多模态模型(如 Gemini / Claude / GPT-4o / Qwen-VL 等)后再试。',
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
let lastError = '模型返回内容为空'
|
|
556
|
+
for (const route of queue.slice(0, 4)) {
|
|
557
|
+
try {
|
|
558
|
+
const res = await callOneRoute(llm, route, system, prompt, maxTokens, sessionId, sendImages)
|
|
559
|
+
if (res.text) return { ok: true, text: res.text, modelRef: res.modelRef, degradedToTextOnly }
|
|
560
|
+
lastError = `${route.ref} 返回内容为空`
|
|
561
|
+
markRouteFailure(route, new Error(lastError))
|
|
562
|
+
} catch (err) {
|
|
563
|
+
lastError = String(err && err.message ? err.message : err)
|
|
564
|
+
if (sendImages.length > 0 && allowTextFallbackOnVisionMiss) {
|
|
565
|
+
try {
|
|
566
|
+
const textRes = await callOneRoute(llm, route, system, prompt, maxTokens, sessionId, [])
|
|
567
|
+
if (textRes.text) return { ok: true, text: textRes.text, modelRef: textRes.modelRef, degradedToTextOnly: true }
|
|
568
|
+
} catch {}
|
|
569
|
+
}
|
|
570
|
+
markRouteFailure(route, err)
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
return { ok: false, error: lastError, degradedToTextOnly }
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/* -------------------------------------------------------------------------- */
|
|
577
|
+
/* Peer Discovery & Cross-Conversation Delivery ("递话 / 插话") */
|
|
578
|
+
/* -------------------------------------------------------------------------- */
|