dsh-chief 1.4.0 → 1.6.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.
@@ -546,6 +546,7 @@ export async function recommendAndSetCompactThreshold(deps, { sessionId = '' } =
546
546
 
547
547
  const llmRes = await callChiefLlm(deps, {
548
548
  sessionId: rootId,
549
+ reasonKind: 'recommend_compact',
549
550
  system: COMPACT_ADVISOR_SYSTEM,
550
551
  prompt: advisorPrompt,
551
552
  maxTokens: 320,
package/chief-models.js CHANGED
@@ -10,9 +10,13 @@ import {
10
10
  SUMMARY_MAX_TOKENS,
11
11
  SUMMARY_RETRY_MAX_TOKENS,
12
12
  clamp,
13
+ normalizeThinkingEffort,
14
+ resolveAlignedModelEffort,
13
15
  resolveWorkspaceConfig,
14
16
  loadChiefState,
15
17
  mutateChiefState,
18
+ checkManagedBudgetBeforeCall,
19
+ recordChiefLlmUsage,
16
20
  } from './chief-state.js'
17
21
 
18
22
  let dshLlmModulePromise = null
@@ -180,6 +184,75 @@ function profileDefaultModelRef(runtime) {
180
184
  return ''
181
185
  }
182
186
 
187
+ const modelEffortCache = new Map()
188
+
189
+ export function decideAdaptiveChiefEffort({
190
+ reasonKind = '',
191
+ userText = '',
192
+ isBlankContext = false,
193
+ tactical = {},
194
+ } = {}) {
195
+ const rk = String(reasonKind || '').trim().toLowerCase()
196
+ const txt = String(userText || '').trim()
197
+
198
+ if (rk === 'chat_summary' || rk === 'dream_roam' || rk === 'quick_status' || rk === 'auto_summary') {
199
+ return 'off'
200
+ }
201
+ if (rk === 'hazard_diagnosis' || rk === 'multi_wall_collision') {
202
+ return 'xhigh'
203
+ }
204
+ if (
205
+ rk === 'blank_explore'
206
+ || rk === 'wall_blocked'
207
+ || rk === 'strong_intervention'
208
+ || rk === 'precipitate'
209
+ || rk === 'distill'
210
+ || rk === 'craft_wall'
211
+ || isBlankContext === true
212
+ ) {
213
+ if (tactical && tactical.intervention && /(?:架构|根因|死锁|并发|重构|安全审计)/.test(txt)) {
214
+ return 'xhigh'
215
+ }
216
+ return 'high'
217
+ }
218
+ if (txt && /(?:深度|全面|彻底|根因|重构|架构|死锁|并发|安全审计|复盘|仔细想|好好想|正则墙|物理墙)/.test(txt)) {
219
+ return 'high'
220
+ }
221
+ if (rk === 'ask_chat' && txt && txt.length <= 28 && /^(?:在吗|进度|到哪一步|怎么样|状态|你好|看看)/.test(txt)) {
222
+ return 'low'
223
+ }
224
+ return 'medium'
225
+ }
226
+
227
+ export async function resolveRouteCapabilities(llm, provider, model) {
228
+ const ref = provider && model ? `${provider}/${model}` : ''
229
+ if (ref && modelEffortCache.has(ref)) {
230
+ const cached = modelEffortCache.get(ref)
231
+ if (Date.now() - cached.at < CATALOG_TTL_MS * 6) return cached.data
232
+ }
233
+ const data = { defaultMaxTokens: 0, efforts: [], defaultEffort: '' }
234
+ try {
235
+ if (llm && typeof llm.resolveModelInfo === 'function' && provider && model) {
236
+ const info = await llm.resolveModelInfo(provider, model)
237
+ if (typeof info?.defaultMaxTokens === 'number' && info.defaultMaxTokens > 0) {
238
+ data.defaultMaxTokens = info.defaultMaxTokens
239
+ }
240
+ if (Array.isArray(info?.reasoning?.efforts)) {
241
+ data.efforts = info.reasoning.efforts.map(e => (
242
+ typeof e === 'string'
243
+ ? { id: e, name: e }
244
+ : { id: String(e?.id || ''), name: String(e?.name || e?.id || ''), description: String(e?.description || '') }
245
+ )).filter(e => e.id)
246
+ }
247
+ if (typeof info?.reasoning?.defaultEffort === 'string') {
248
+ data.defaultEffort = info.reasoning.defaultEffort
249
+ }
250
+ }
251
+ } catch {}
252
+ if (ref) modelEffortCache.set(ref, { at: Date.now(), data })
253
+ return data
254
+ }
255
+
183
256
  export async function listChiefModels(deps, fresh = false, sessionId = '') {
184
257
  const llm = deps.getLlm ? deps.getLlm() : null
185
258
  const runtime = deps.runtime
@@ -297,6 +370,16 @@ export async function listChiefModels(deps, fresh = false, sessionId = '') {
297
370
 
298
371
  const picked = pickRouteFromItems(items, selectedRef, followRef, deps, sessionId, registeredProviderIds)
299
372
  const resolvedRef = (picked && picked.ref) || selectedRef || followRef || ''
373
+ let supportedEfforts = []
374
+ let defaultEffort = ''
375
+ if (resolvedRef) {
376
+ const parsedResolved = splitModelRef(resolvedRef)
377
+ if (parsedResolved) {
378
+ const caps = await resolveRouteCapabilities(llm, parsedResolved.provider, parsedResolved.model)
379
+ supportedEfforts = caps.efforts || []
380
+ defaultEffort = caps.defaultEffort || ''
381
+ }
382
+ }
300
383
  return {
301
384
  available: items.length > 0 || Boolean(resolvedRef),
302
385
  selectedRef,
@@ -305,6 +388,8 @@ export async function listChiefModels(deps, fresh = false, sessionId = '') {
305
388
  defaultRef: followRef,
306
389
  resolvedRef,
307
390
  resolvedLabel: formatChiefModelLabel(resolvedRef, items, rawProviders, true),
391
+ supportedEfforts,
392
+ defaultEffort,
308
393
  models: items,
309
394
  }
310
395
  }
@@ -397,27 +482,48 @@ function pickRouteFromItems(items, selectedRef, followRef, deps, sessionId, regi
397
482
  return queue[0] || null
398
483
  }
399
484
 
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
- }
485
+ export function estimateTokensRough(text = '') {
486
+ const s = String(text || '')
487
+ if (!s) return 0
488
+ let cjk = 0
489
+ for (let i = 0; i < s.length; i += 1) {
490
+ const code = s.charCodeAt(i)
491
+ if (code >= 0x4E00 && code <= 0x9FFF) cjk += 1
492
+ }
493
+ const rest = s.length - cjk
494
+ return Math.max(1, Math.round(cjk * 0.72 + rest * 0.28))
495
+ }
496
+
497
+ function extractUsageMetrics(rawUsage, promptText = '', outputText = '') {
498
+ const u = rawUsage && typeof rawUsage === 'object' ? rawUsage : null
499
+ const inRaw = u ? Number(u.inputTokens ?? u.promptTokens ?? u.prompt_tokens ?? u.input_tokens ?? 0) : 0
500
+ const outRaw = u ? Number(u.outputTokens ?? u.completionTokens ?? u.completion_tokens ?? u.output_tokens ?? 0) : 0
501
+ if (Number.isFinite(inRaw) && Number.isFinite(outRaw) && (inRaw > 0 || outRaw > 0)) {
502
+ return {
503
+ inputTokens: Math.max(0, Math.round(inRaw)),
504
+ outputTokens: Math.max(0, Math.round(outRaw)),
505
+ estimated: false,
413
506
  }
414
- } catch {}
507
+ }
508
+ return {
509
+ inputTokens: estimateTokensRough(promptText),
510
+ outputTokens: estimateTokensRough(outputText),
511
+ estimated: true,
512
+ }
513
+ }
514
+
515
+ async function callOneRoute(llm, route, system, userText, maxTokens = SUMMARY_MAX_TOKENS, sessionId = '', imageBlocks = [], desiredEffort = 'medium') {
516
+ const caps = await resolveRouteCapabilities(llm, route.provider, route.model)
517
+ const modelDefaultMaxTokens = caps.defaultMaxTokens || 0
518
+ const efforts = Array.isArray(caps.efforts) ? caps.efforts : []
519
+ const effort = resolveAlignedModelEffort(desiredEffort, efforts)
415
520
 
416
521
  const targetBudget = Math.max(Number(maxTokens) || 0, SUMMARY_MAX_TOKENS)
417
522
  const firstBudget = modelDefaultMaxTokens ? Math.min(modelDefaultMaxTokens, targetBudget) : targetBudget
418
523
  const retryBudget = firstBudget < targetBudget ? targetBudget : SUMMARY_RETRY_MAX_TOKENS
419
524
  const budgets = retryBudget > firstBudget ? [firstBudget, retryBudget] : [firstBudget]
420
525
  const safeImages = Array.isArray(imageBlocks) ? imageBlocks : []
526
+ const promptBasis = `${system || ''}\n${userText || ''}`
421
527
 
422
528
  const runOnce = async (budget) => {
423
529
  const userContent = [...safeImages, { type: 'text', text: userText || '请查看附图并回答。' }]
@@ -435,8 +541,12 @@ async function callOneRoute(llm, route, system, userText, maxTokens = SUMMARY_MA
435
541
  const dshLlm = await loadDshLlm()
436
542
  if (dshLlm && typeof dshLlm.BlockAssembler === 'function') {
437
543
  const assembler = new dshLlm.BlockAssembler()
544
+ let streamUsage = null
438
545
  for await (const chunk of llm.stream(request)) {
439
546
  assembler.push(chunk)
547
+ if (chunk && typeof chunk === 'object' && (chunk.usage || chunk.type === 'usage')) {
548
+ streamUsage = chunk.usage || chunk
549
+ }
440
550
  }
441
551
  const finish = assembler.finish
442
552
  if (finish && (finish.kind === 'error' || finish.kind === 'aborted')) {
@@ -450,14 +560,19 @@ async function callOneRoute(llm, route, system, userText, maxTokens = SUMMARY_MA
450
560
  .map(b => b.text)
451
561
  .join('\n')
452
562
  .trim()
453
- return { text, finishKind: finish?.kind || '', modelRef: route.ref }
563
+ const usage = extractUsageMetrics(assembler.usage || finish?.usage || finish?.reason?.usage || streamUsage, promptBasis, text)
564
+ return { text, finishKind: finish?.kind || '', modelRef: route.ref, thinkingEffort: effort || 'none', usage }
454
565
  }
455
566
 
456
567
  // Pure standalone fallback stream reader (for unit tests / lightweight hosts)
457
568
  const parts = []
458
569
  let finishKind = ''
570
+ let streamUsage = null
459
571
  for await (const chunk of llm.stream(request)) {
460
572
  if (!chunk || typeof chunk !== 'object') continue
573
+ if (chunk.usage || chunk.type === 'usage') {
574
+ streamUsage = chunk.usage || chunk
575
+ }
461
576
  if (chunk.type === 'text-delta' || chunk.type === 'text_delta') {
462
577
  if (typeof chunk.text === 'string') parts.push(chunk.text)
463
578
  else if (typeof chunk.delta === 'string') parts.push(chunk.delta)
@@ -465,6 +580,7 @@ async function callOneRoute(llm, route, system, userText, maxTokens = SUMMARY_MA
465
580
  else if (typeof chunk.content === 'string') parts.push(chunk.content)
466
581
  else if (chunk.type === 'finish') {
467
582
  finishKind = chunk.reason?.kind || chunk.finishReason || ''
583
+ if (chunk.reason?.usage) streamUsage = chunk.reason.usage
468
584
  if (finishKind === 'error') {
469
585
  const err = new Error(chunk.reason?.failure?.message || 'LLM stream error')
470
586
  err.failure = chunk.reason?.failure
@@ -472,18 +588,26 @@ async function callOneRoute(llm, route, system, userText, maxTokens = SUMMARY_MA
472
588
  }
473
589
  }
474
590
  }
475
- return { text: parts.join('').trim(), finishKind, modelRef: route.ref }
591
+ const text = parts.join('').trim()
592
+ const usage = extractUsageMetrics(streamUsage, promptBasis, text)
593
+ return { text, finishKind, modelRef: route.ref, thinkingEffort: effort || 'none', usage }
476
594
  }
477
595
 
478
- let lastRes = { text: '', finishKind: '', modelRef: route.ref }
596
+ let lastRes = {
597
+ text: '',
598
+ finishKind: '',
599
+ modelRef: route.ref,
600
+ thinkingEffort: effort || 'none',
601
+ usage: extractUsageMetrics(null, promptBasis, ''),
602
+ }
479
603
  for (let i = 0; i < budgets.length; i += 1) {
480
604
  const res = await runOnce(budgets[i])
481
605
  lastRes = res
482
606
  if (res.text && res.finishKind !== 'max-tokens') {
483
- return { text: res.text, modelRef: res.modelRef }
607
+ return { text: res.text, modelRef: res.modelRef, thinkingEffort: res.thinkingEffort, usage: res.usage }
484
608
  }
485
609
  }
486
- return { text: lastRes.text, modelRef: lastRes.modelRef }
610
+ return { text: lastRes.text, modelRef: lastRes.modelRef, thinkingEffort: lastRes.thinkingEffort, usage: lastRes.usage }
487
611
  }
488
612
 
489
613
  async function filterVisionRoutes(llm, queue) {
@@ -515,14 +639,76 @@ export async function callChiefLlm(deps, {
515
639
  maxTokens = SUMMARY_MAX_TOKENS,
516
640
  imageBlocks = [],
517
641
  allowTextFallbackOnVisionMiss = false,
642
+ reasonKind = '',
643
+ userText = '',
644
+ isBlankContext = false,
645
+ tactical = null,
646
+ effortOverride = '',
518
647
  }) {
519
648
  const safeImages = Array.isArray(imageBlocks) ? imageBlocks : []
649
+ const rootSid = (deps && deps.resolveRootSessionId ? deps.resolveRootSessionId(sessionId) : '') || String(sessionId || '').trim()
650
+ const st = loadChiefState()
651
+ const effCfg = resolveWorkspaceConfig(st, rootSid)
652
+ const cfgEffort = normalizeThinkingEffort(effortOverride || effCfg.thinkingEffort || 'auto')
653
+ const thinkingEffortMode = cfgEffort === 'auto' ? 'auto' : 'manual'
654
+ const desiredEffort = cfgEffort === 'auto'
655
+ ? decideAdaptiveChiefEffort({
656
+ reasonKind,
657
+ userText: userText || prompt,
658
+ isBlankContext,
659
+ tactical: tactical || effCfg.tacticalModes || {},
660
+ })
661
+ : cfgEffort
662
+
663
+ const budgetCheck = checkManagedBudgetBeforeCall(rootSid, reasonKind)
664
+ if (budgetCheck && !budgetCheck.allowed) {
665
+ return {
666
+ ok: false,
667
+ budgetExceeded: true,
668
+ budgetTripped: true,
669
+ error: budgetCheck.reason || '已触发托管硬预算熔断',
670
+ thinkingEffort: desiredEffort,
671
+ thinkingEffortMode,
672
+ }
673
+ }
674
+
675
+ const promptBasis = `${system || ''}\n${prompt || ''}`
676
+
520
677
  if (deps && typeof deps.callLlmOverride === 'function') {
521
- return deps.callLlmOverride({ sessionId, system, prompt, maxTokens, imageBlocks: safeImages, allowTextFallbackOnVisionMiss })
678
+ const overrideRes = await deps.callLlmOverride({
679
+ sessionId,
680
+ system,
681
+ prompt,
682
+ maxTokens,
683
+ imageBlocks: safeImages,
684
+ allowTextFallbackOnVisionMiss,
685
+ reasonKind,
686
+ desiredEffort,
687
+ thinkingEffortMode,
688
+ })
689
+ if (overrideRes && typeof overrideRes === 'object') {
690
+ const usage = extractUsageMetrics(overrideRes.usage, promptBasis, overrideRes.text || '')
691
+ const ledgerRec = recordChiefLlmUsage(rootSid, {
692
+ reasonKind,
693
+ modelRef: overrideRes.modelRef || 'override',
694
+ inputTokens: usage.inputTokens,
695
+ outputTokens: usage.outputTokens,
696
+ estimated: usage.estimated,
697
+ ok: overrideRes.ok !== false,
698
+ })
699
+ return {
700
+ thinkingEffort: overrideRes.thinkingEffort || desiredEffort,
701
+ thinkingEffortMode: overrideRes.thinkingEffortMode || thinkingEffortMode,
702
+ usage,
703
+ ...(ledgerRec && ledgerRec.tripped ? { budgetTripped: true } : {}),
704
+ ...overrideRes,
705
+ }
706
+ }
707
+ return overrideRes
522
708
  }
523
709
  const llm = deps.getLlm ? deps.getLlm() : null
524
710
  if (!llm || typeof llm.stream !== 'function') {
525
- return { ok: false, error: '当前环境未挂载 LLM 服务' }
711
+ return { ok: false, error: '当前环境未挂载 LLM 服务', thinkingEffort: desiredEffort, thinkingEffortMode }
526
712
  }
527
713
  const catalog = await listChiefModels(deps, false, sessionId)
528
714
  const regSet = typeof llm.listProviders === 'function'
@@ -530,7 +716,7 @@ export async function callChiefLlm(deps, {
530
716
  : null
531
717
  const baseQueue = buildRouteQueue(catalog.models, catalog.selectedRef, catalog.followRef || catalog.defaultRef, deps, sessionId, regSet)
532
718
  if (!baseQueue.length) {
533
- return { ok: false, error: '未找到可用的参谋长模型,请在 DSH 设置中配置模型供应商' }
719
+ return { ok: false, error: '未找到可用的参谋长模型,请在 DSH 设置中配置模型供应商', thinkingEffort: desiredEffort, thinkingEffortMode }
534
720
  }
535
721
 
536
722
  let queue = baseQueue
@@ -548,6 +734,8 @@ export async function callChiefLlm(deps, {
548
734
  return {
549
735
  ok: false,
550
736
  error: '当前参谋长及备选模型均不支持图片输入(仅纯文本),请在右上角切换支持视觉的多模态模型(如 Gemini / Claude / GPT-4o / Qwen-VL 等)后再试。',
737
+ thinkingEffort: desiredEffort,
738
+ thinkingEffortMode,
551
739
  }
552
740
  }
553
741
  }
@@ -555,22 +743,71 @@ export async function callChiefLlm(deps, {
555
743
  let lastError = '模型返回内容为空'
556
744
  for (const route of queue.slice(0, 4)) {
557
745
  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 }
746
+ const res = await callOneRoute(llm, route, system, prompt, maxTokens, sessionId, sendImages, desiredEffort)
747
+ if (res.text) {
748
+ const usage = res.usage || extractUsageMetrics(null, promptBasis, res.text)
749
+ const ledgerRec = recordChiefLlmUsage(rootSid, {
750
+ reasonKind,
751
+ modelRef: res.modelRef,
752
+ inputTokens: usage.inputTokens,
753
+ outputTokens: usage.outputTokens,
754
+ estimated: usage.estimated,
755
+ ok: true,
756
+ })
757
+ return {
758
+ ok: true,
759
+ text: res.text,
760
+ modelRef: res.modelRef,
761
+ degradedToTextOnly,
762
+ thinkingEffort: res.thinkingEffort || desiredEffort,
763
+ thinkingEffortMode,
764
+ usage,
765
+ ...(ledgerRec && ledgerRec.tripped ? { budgetTripped: true } : {}),
766
+ }
767
+ }
560
768
  lastError = `${route.ref} 返回内容为空`
561
769
  markRouteFailure(route, new Error(lastError))
562
770
  } catch (err) {
563
771
  lastError = String(err && err.message ? err.message : err)
564
772
  if (sendImages.length > 0 && allowTextFallbackOnVisionMiss) {
565
773
  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 }
774
+ const textRes = await callOneRoute(llm, route, system, prompt, maxTokens, sessionId, [], desiredEffort)
775
+ if (textRes.text) {
776
+ const usage = textRes.usage || extractUsageMetrics(null, promptBasis, textRes.text)
777
+ const ledgerRec = recordChiefLlmUsage(rootSid, {
778
+ reasonKind,
779
+ modelRef: textRes.modelRef,
780
+ inputTokens: usage.inputTokens,
781
+ outputTokens: usage.outputTokens,
782
+ estimated: usage.estimated,
783
+ ok: true,
784
+ })
785
+ return {
786
+ ok: true,
787
+ text: textRes.text,
788
+ modelRef: textRes.modelRef,
789
+ degradedToTextOnly: true,
790
+ thinkingEffort: textRes.thinkingEffort || desiredEffort,
791
+ thinkingEffortMode,
792
+ usage,
793
+ ...(ledgerRec && ledgerRec.tripped ? { budgetTripped: true } : {}),
794
+ }
795
+ }
568
796
  } catch {}
569
797
  }
570
798
  markRouteFailure(route, err)
571
799
  }
572
800
  }
573
- return { ok: false, error: lastError, degradedToTextOnly }
801
+ const failUsage = extractUsageMetrics(null, promptBasis, '')
802
+ recordChiefLlmUsage(rootSid, {
803
+ reasonKind,
804
+ modelRef: queue[0] ? queue[0].ref : '',
805
+ inputTokens: failUsage.inputTokens,
806
+ outputTokens: 0,
807
+ estimated: true,
808
+ ok: false,
809
+ })
810
+ return { ok: false, error: lastError, degradedToTextOnly, thinkingEffort: desiredEffort, thinkingEffortMode }
574
811
  }
575
812
 
576
813
  /* -------------------------------------------------------------------------- */