dsh-coding 0.0.1 → 0.1.1-rc1
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/README.md +65 -58
- package/cordis.patch.yml +231 -4
- package/docs/DETAIL.md +183 -0
- package/docs/DEVELOPMENT.md +189 -0
- package/icon.svg +4 -4
- package/lib/apply-patch.js +268 -0
- package/lib/client/capsule.js +120 -0
- package/lib/client/card.js +220 -0
- package/lib/client/command-row.js +76 -0
- package/lib/client/config-page.js +392 -0
- package/lib/client/definition.js +164 -0
- package/lib/client/index.js +129 -0
- package/lib/client/locales.js +156 -0
- package/lib/client/mode-picker.js +77 -0
- package/lib/client/styles.js +283 -0
- package/lib/client.js +1691 -0
- package/lib/coding-projection.js +349 -0
- package/lib/config.js +204 -0
- package/lib/contract.js +124 -0
- package/lib/editor.js +23 -0
- package/lib/failures.js +142 -0
- package/lib/handoff.js +312 -0
- package/lib/index.js +109 -7
- package/lib/language.js +136 -0
- package/lib/messages.js +43 -0
- package/lib/models.js +154 -0
- package/lib/patch.js +235 -0
- package/lib/preset.js +1488 -0
- package/lib/reasoning-levels.js +28 -0
- package/lib/reasoning-projection.js +178 -0
- package/lib/reasoning.js +585 -0
- package/lib/stages.js +306 -0
- package/lib/state.js +550 -0
- package/lib/system-language.js +56 -0
- package/lib/types/apply-patch.d.ts +13 -0
- package/lib/types/client/capsule.d.ts +9 -0
- package/lib/types/client/card.d.ts +72 -0
- package/lib/types/client/command-row.d.ts +42 -0
- package/lib/types/client/config-page.d.ts +136 -0
- package/lib/types/client/definition.d.ts +51 -0
- package/lib/types/client/index.d.ts +10 -0
- package/lib/types/client/locales.d.ts +18 -0
- package/lib/types/client/mode-picker.d.ts +25 -0
- package/lib/types/client/styles.d.ts +28 -0
- package/lib/types/coding-projection.d.ts +116 -0
- package/lib/types/config.d.ts +193 -0
- package/lib/types/contract.d.ts +33 -0
- package/lib/types/editor.d.ts +9 -0
- package/lib/types/failures.d.ts +62 -0
- package/lib/types/handoff.d.ts +112 -0
- package/lib/types/index.d.ts +144 -0
- package/lib/types/language.d.ts +56 -0
- package/lib/types/messages.d.ts +27 -0
- package/lib/types/models.d.ts +82 -0
- package/lib/types/patch.d.ts +37 -0
- package/lib/types/preset.d.ts +22 -0
- package/lib/types/reasoning-levels.d.ts +20 -0
- package/lib/types/reasoning-projection.d.ts +64 -0
- package/lib/types/reasoning.d.ts +141 -0
- package/lib/types/stages.d.ts +164 -0
- package/lib/types/state.d.ts +261 -0
- package/lib/types/system-language.d.ts +10 -0
- package/lib/types/verification-tool.d.ts +32 -0
- package/lib/types/verification.d.ts +77 -0
- package/lib/types/verifier.d.ts +9 -0
- package/lib/types/volatile.d.ts +17 -0
- package/lib/verification-tool.js +176 -0
- package/lib/verification.js +337 -0
- package/lib/verifier.js +25 -0
- package/lib/volatile.js +20 -0
- package/locale/en.json +3 -2
- package/locale/zh.json +3 -2
- package/package.json +83 -9
- package/lib/index.d.ts +0 -11
package/lib/reasoning.js
ADDED
|
@@ -0,0 +1,585 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Automatic, configured, and per-session reasoning-level policy.
|
|
3
|
+
*
|
|
4
|
+
* The level is chosen before every model call and written through the Agent's
|
|
5
|
+
* own model selection (`agent.model.select()`), so the Provider receives the
|
|
6
|
+
* effort on the next step. Three sources, in priority order:
|
|
7
|
+
*
|
|
8
|
+
* 1. the settings pin (`reasoning: low|high|max`) — the session is never
|
|
9
|
+
* classified and an in-session override is refused;
|
|
10
|
+
* 2. an explicit per-session override — the chat box pick folded from a
|
|
11
|
+
* `model/selection` event, or the `coding_reasoning` tool;
|
|
12
|
+
* 3. the deterministic classifier below, biased to Low and escalated by
|
|
13
|
+
* multi-file scope, module-level review/refactor, a long plan, and the rare
|
|
14
|
+
* project-level requests.
|
|
15
|
+
*
|
|
16
|
+
* The classifier spends no extra model request and exposes no transcript.
|
|
17
|
+
*
|
|
18
|
+
* @module dsh-coding/reasoning
|
|
19
|
+
*/
|
|
20
|
+
import { ReasoningEffortId } from '@deepseek-ai/dsh-llm';
|
|
21
|
+
import { higher, levelForEffort } from './reasoning-levels.js';
|
|
22
|
+
import { REASONING_PROJECTION_KEY } from './reasoning-projection.js';
|
|
23
|
+
// Re-exported so callers keep importing the level vocabulary from this module.
|
|
24
|
+
export { higher, levelForEffort, rank } from './reasoning-levels.js';
|
|
25
|
+
function textOf(content) {
|
|
26
|
+
if (!Array.isArray(content))
|
|
27
|
+
return '';
|
|
28
|
+
const parts = [];
|
|
29
|
+
for (const block of content) {
|
|
30
|
+
if (typeof block !== 'object' || block === null)
|
|
31
|
+
continue;
|
|
32
|
+
const item = block;
|
|
33
|
+
if (item.type === 'text' && typeof item.text === 'string')
|
|
34
|
+
parts.push(item.text);
|
|
35
|
+
}
|
|
36
|
+
return parts.join('\n');
|
|
37
|
+
}
|
|
38
|
+
function directUserText(message) {
|
|
39
|
+
if (message?.role !== 'user')
|
|
40
|
+
return '';
|
|
41
|
+
if (message.source?.kind !== 'user')
|
|
42
|
+
return '';
|
|
43
|
+
return textOf(message.content);
|
|
44
|
+
}
|
|
45
|
+
/** Latest direct user message in a message batch, excluding injected context. */
|
|
46
|
+
export function latestUserTextIn(messages) {
|
|
47
|
+
if (!Array.isArray(messages))
|
|
48
|
+
return '';
|
|
49
|
+
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
50
|
+
const text = directUserText(messages[index]);
|
|
51
|
+
if (text.trim() !== '')
|
|
52
|
+
return text;
|
|
53
|
+
}
|
|
54
|
+
return '';
|
|
55
|
+
}
|
|
56
|
+
/** Latest direct user message, excluding injected context and skill catalogs. */
|
|
57
|
+
export function latestUserText(session) {
|
|
58
|
+
let messages;
|
|
59
|
+
try {
|
|
60
|
+
messages = session?.deriveMessages?.();
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return '';
|
|
64
|
+
}
|
|
65
|
+
return latestUserTextIn(messages);
|
|
66
|
+
}
|
|
67
|
+
const QUESTION_PATTERN = /[??]\s*$|[吗呢]\s*$|\bdo\s+(?:you|we|i|they|it|this|that|these|those)\b|^(?:how|what|why|should|which|where|when|who|is|are|does|can|could|would|will)\b|^(?:怎么|如何|为什么|是否|哪|什么|要不要|该不该|能不能|可不可以)/i;
|
|
68
|
+
/** A question that starts with an action verb asks for work, not advice. */
|
|
69
|
+
const ACTION_REQUEST_PATTERN = /^(?:can you|could you|would you|will you|can we|could we|would we|please|help me|review|audit|refactor|rewrite|implement|fix|add|update|modify|create|build|make|remove|delete|rename|move|migrate|redesign|scaffold|bootstrap|initialize|init|set up|handle|resolve|investigate|debug|analyze|optimize|optimise|improve|verify|check|test|document|simplify|clean|帮我|请|能|能否|可以|能不能|可不可以|给我|替我|麻烦|我要|我想|需要|是否(?:可以|能|要|需要)?帮我|审查|评审|重构|实现|开发|修复|修改|更新|新增|添加|删除|移除|替换|重写|翻译|调整|创建|搭建|初始化|迁移|检查|调查|排查|定位|优化|改进|验证|测试|处理|解决|分析)/i;
|
|
70
|
+
const EXPLANATION_PATTERN = /\b(?:explain|describe|summari[sz]e|tell me|walk me through|want to know|wondering|curious|what is|what does|why is|why does|how does|how do|should i|should we|is it worth|does it make sense|what should|which (?:is|should|approach))\b|(?:解释|说明|总结|介绍一下|讲讲|说说|为什么|怎么回事|什么是|如何理解|怎么理解|该怎么|应该怎么|分析一下|有什么建议|想知道)/i;
|
|
71
|
+
const ADVICE_PATTERN = /\b(?:advice|opinion|recommend|recommendation|suggest|suggestion|best way|which approach|trade-?offs?|thoughts|worth|benefit|better|make sense|advisable)\b|(?:建议|意见|推荐|值得|看法|选型|怎么选|如何选择|该不该|要不要|你觉得|你认为)/i;
|
|
72
|
+
const PATH_PATTERN_GLOBAL = /(?:^|[\s`"'(])(?:[\w.@-]+[\\/])+[\w.@-]+\b|[A-Za-z]:\\[^\s]+/g;
|
|
73
|
+
/** Explicit single-file scope beats a project word elsewhere in the sentence. */
|
|
74
|
+
const SINGLE_FILE_PATTERN = /\b(?:single[- ]file|one file|this file)\b|(?:单文件|一个文件|这个文件)/i;
|
|
75
|
+
/** Max is deliberately narrow: project-scale or genuinely unfixable work only. */
|
|
76
|
+
const PROJECT_SETUP_PATTERNS = [
|
|
77
|
+
/\b(?:scaffold|greenfield)\b/i,
|
|
78
|
+
/\bbootstrap\b.{0,30}\b(?:project|app|application|service|system)\b/i,
|
|
79
|
+
/\b(?:create|start|build|make|set up|initialize|init|bootstrap|scaffold)\b.{0,30}\b(?:new )?project\b(?!\s+(?:file|setting|settings|config|configuration|field|option|property|section|entry|row)\b)/i,
|
|
80
|
+
/\b(?:new )?project\b.{0,30}\b(?:from scratch|setup|template)\b/i,
|
|
81
|
+
/\bfrom scratch\b.{0,40}\b(?:project|app|application|service|system|cli|website|backend|frontend)\b/i,
|
|
82
|
+
/\b(?:project|app|application|service|system|cli|website|backend|frontend)\b.{0,40}\bfrom scratch\b/i,
|
|
83
|
+
/(?:项目脚手架|项目模板|初始化项目|项目初始化|新建项目|项目搭建|搭建.{0,6}项目|起一个新项目|做一个新项目|搭一个项目|创建.{0,6}新?项目(?!配置|文件|设置|字段|选项|属性)|建.{0,4}新?项目(?!配置|文件|设置|字段|选项|属性)|(?:从零|从0|从 0).{0,20}(?:项目|应用|系统|服务|网站|后台|前端|后端|脚手架|框架|平台)|(?:项目|应用|系统).{0,6}(?:搭建|脚手架|初始化))/i,
|
|
84
|
+
];
|
|
85
|
+
const PROJECT_REFACTOR_PATTERNS = [
|
|
86
|
+
/\b(?:refactor|restructure|rewrite|migrate|redesign)\b.{0,60}\b(?:project|codebase|repository|repo|architecture)\b/i,
|
|
87
|
+
/\b(?:project|codebase|repository|repo|architecture)\b.{0,60}\b(?:refactor|restructure|rewrite|migrate|redesign)\b/i,
|
|
88
|
+
/(?:项目重构|重构整个|重构项目|整体重构|全项目重构|架构重构|代码库重构|重写整个|迁移整个|项目级重构|重构.{0,12}(?:项目|架构|代码库)|(?:项目|代码库).{0,8}(?:架构)?重构)/i,
|
|
89
|
+
];
|
|
90
|
+
const PROJECT_REVIEW_PATTERNS = [
|
|
91
|
+
/\b(?:review|audit)\b.{0,60}\b(?:project|codebase|repository|repo)\b(?!\s+(?:file|setting|settings|config|configuration|field|option|property|section|entry|row)\b)|\b(?:review|audit)\b.{0,60}\b(?:all files|every file)\b/i,
|
|
92
|
+
/\b(?:project|codebase|repository|repo)\b(?!\s+(?:file|setting|settings|config|configuration|field|option|property|section|entry|row)\b).{0,60}\b(?:review|audit)\b/i,
|
|
93
|
+
/(?:项目级审查|项目审查|全项目审查|整个项目审查|全局审查|代码库审查|审查整个|审查全部|全量审查|项目级代码审查|审查.{0,12}(?:项目|代码库|全部)|(?:项目|代码库)(?!配置|文件|设置|字段|选项|属性).{0,8}(?:代码)?审查)/i,
|
|
94
|
+
];
|
|
95
|
+
const UNFIXABLE_PATTERNS = [
|
|
96
|
+
/\b(?:unfixable|cannot (?:be )?fix(?:ed)?|can'?t fix|unable to fix|persistent bug|intermittent bug|heisenbug|root cause unknown|cannot reproduce|can'?t reproduce|unable to reproduce)\b/i,
|
|
97
|
+
/(?:无法修复|修不好|找不到原因|反复失败|反复出现|顽固|一次性无法|无法复现|始终无法|一直修不好|反复修复)/i,
|
|
98
|
+
];
|
|
99
|
+
/** High is for multi-file work and a feature-module review/refactor, not ordinary edits. */
|
|
100
|
+
const MULTI_FILE_PATTERNS = [
|
|
101
|
+
/\b(?:multi[- ]?file|multiple files|several files|across files|across (?:the )?(?:repo|codebase|project|app|application|modules?|packages?)|whole feature|module[- ]wide|codebase[- ]wide|end[- ]to[- ]end|e2e)\b/i,
|
|
102
|
+
/\b(?:[2-9]|\d{2,}|two|three|four|five|six|seven|eight|nine|ten)\s*(?:files|file changes|source files)\b/i,
|
|
103
|
+
/\b(?:review|audit|refactor)\b.{0,40}\b(?:feature|module)\b/i,
|
|
104
|
+
/\b(?:feature|module)\b.{0,40}\b(?:review|audit|refactor)\b/i,
|
|
105
|
+
/(?:多文件|多个文件|跨文件|跨多个文件|模块级|整个模块|多个模块|跨模块|跨包|端到端)/i,
|
|
106
|
+
/(?:审查|评审|重构).{0,8}(?:功能模块|模块)/i,
|
|
107
|
+
/(?:功能模块|模块).{0,8}(?:审查|评审|重构)/i,
|
|
108
|
+
/(?:[2-9]|\d{2,}|两|二|三|四|五|六|七|八|九|十)\s*个文件/,
|
|
109
|
+
];
|
|
110
|
+
function matches(patterns, text) {
|
|
111
|
+
return patterns.some((pattern) => pattern.test(text));
|
|
112
|
+
}
|
|
113
|
+
function pathCount(text) {
|
|
114
|
+
const seen = new Set();
|
|
115
|
+
for (const match of text.matchAll(PATH_PATTERN_GLOBAL)) {
|
|
116
|
+
seen.add(match[0].replace(/^[\s`"'(]+/, '').trim());
|
|
117
|
+
}
|
|
118
|
+
return seen.size;
|
|
119
|
+
}
|
|
120
|
+
function cleanUserText(text) {
|
|
121
|
+
if (typeof text !== 'string')
|
|
122
|
+
return '';
|
|
123
|
+
return text
|
|
124
|
+
.replace(/<system-reminder>[\s\S]*?<\/system-reminder>/gi, ' ')
|
|
125
|
+
.replace(/<skill_content[\s\S]*?<\/skill_content>/gi, ' ')
|
|
126
|
+
.replace(/\s+/g, ' ')
|
|
127
|
+
.trim();
|
|
128
|
+
}
|
|
129
|
+
function planItems(value) {
|
|
130
|
+
if (!Array.isArray(value))
|
|
131
|
+
return [];
|
|
132
|
+
return value.filter((item) => (typeof item === 'object'
|
|
133
|
+
&& item !== null
|
|
134
|
+
&& typeof item.status === 'string'));
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Classify one user turn with a deliberately low-biased policy.
|
|
138
|
+
*
|
|
139
|
+
* Low is the default. Max is reserved for four rare cases: creative project
|
|
140
|
+
* setup, project-wide refactor, project-level review, and a bug that cannot be
|
|
141
|
+
* fixed in one pass. High exists for multi-file work and a feature-module
|
|
142
|
+
* review/refactor. Questions, advice, simple fixes, and single-file reviews
|
|
143
|
+
* stay Low.
|
|
144
|
+
*/
|
|
145
|
+
export function classifyTask(rawText, todos = []) {
|
|
146
|
+
const text = cleanUserText(rawText);
|
|
147
|
+
if (text.length === 0) {
|
|
148
|
+
return { level: 'low', score: 1, reason: 'no direct user text; no complexity signal' };
|
|
149
|
+
}
|
|
150
|
+
const question = QUESTION_PATTERN.test(text);
|
|
151
|
+
const actionRequest = ACTION_REQUEST_PATTERN.test(text);
|
|
152
|
+
const guidance = EXPLANATION_PATTERN.test(text)
|
|
153
|
+
|| ADVICE_PATTERN.test(text)
|
|
154
|
+
|| (question && !actionRequest);
|
|
155
|
+
if (guidance) {
|
|
156
|
+
return { level: 'low', score: 1, reason: 'question, advice, or guidance request' };
|
|
157
|
+
}
|
|
158
|
+
const paths = pathCount(text);
|
|
159
|
+
if (SINGLE_FILE_PATTERN.test(text) && paths < 2 && !matches(MULTI_FILE_PATTERNS, text)) {
|
|
160
|
+
return { level: 'low', score: 1, reason: 'explicit single-file scope' };
|
|
161
|
+
}
|
|
162
|
+
if (matches(PROJECT_SETUP_PATTERNS, text)) {
|
|
163
|
+
return { level: 'max', score: 5, reason: 'creative project setup' };
|
|
164
|
+
}
|
|
165
|
+
if (matches(PROJECT_REFACTOR_PATTERNS, text)) {
|
|
166
|
+
return { level: 'max', score: 5, reason: 'project-wide refactor' };
|
|
167
|
+
}
|
|
168
|
+
if (matches(PROJECT_REVIEW_PATTERNS, text)) {
|
|
169
|
+
return { level: 'max', score: 5, reason: 'project-level review' };
|
|
170
|
+
}
|
|
171
|
+
if (matches(UNFIXABLE_PATTERNS, text)) {
|
|
172
|
+
return { level: 'max', score: 5, reason: 'one-shot unfixable bug' };
|
|
173
|
+
}
|
|
174
|
+
if (paths >= 2) {
|
|
175
|
+
return { level: 'high', score: 3, reason: `${paths} repository files referenced` };
|
|
176
|
+
}
|
|
177
|
+
if (matches(MULTI_FILE_PATTERNS, text)) {
|
|
178
|
+
return { level: 'high', score: 3, reason: 'multi-file or feature-module work' };
|
|
179
|
+
}
|
|
180
|
+
const plan = planItems(todos);
|
|
181
|
+
const open = plan.filter((item) => item.status !== 'completed').length;
|
|
182
|
+
if (plan.length >= 6 || open >= 5) {
|
|
183
|
+
return { level: 'high', score: 3, reason: `${plan.length}-step execution plan` };
|
|
184
|
+
}
|
|
185
|
+
return { level: 'low', score: 1, reason: 'default low; no multi-file or project-level signal' };
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Pick the closest supported provider effort. Providers differ in which effort
|
|
189
|
+
* ids they accept, and a rejected id aborts the turn before any I/O.
|
|
190
|
+
*/
|
|
191
|
+
export function mapEffort(desired, supported) {
|
|
192
|
+
if (!Array.isArray(supported) || supported.length === 0)
|
|
193
|
+
return undefined;
|
|
194
|
+
const available = new Set(supported.filter((id) => typeof id === 'string'));
|
|
195
|
+
const preferences = {
|
|
196
|
+
low: ['low', 'minimal', 'medium', 'high', 'max'],
|
|
197
|
+
high: ['high', 'medium', 'xhigh', 'max', 'low', 'minimal'],
|
|
198
|
+
max: ['max', 'xhigh', 'high', 'medium', 'low'],
|
|
199
|
+
};
|
|
200
|
+
for (const id of preferences[desired]) {
|
|
201
|
+
if (available.has(id))
|
|
202
|
+
return id;
|
|
203
|
+
}
|
|
204
|
+
return undefined;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Owns the per-session reasoning decision and applies it through the entry
|
|
208
|
+
* point's model-selection reference. Exported for focused tests.
|
|
209
|
+
*/
|
|
210
|
+
export class ReasoningController {
|
|
211
|
+
ctx;
|
|
212
|
+
options;
|
|
213
|
+
hooks;
|
|
214
|
+
states = new WeakMap();
|
|
215
|
+
overrides = new WeakMap();
|
|
216
|
+
userChoices = new WeakMap();
|
|
217
|
+
applied = new WeakMap();
|
|
218
|
+
support = new Map();
|
|
219
|
+
/**
|
|
220
|
+
* Resolved effort ids per route, readable synchronously. The inbox hook has
|
|
221
|
+
* to choose an effort before the Host snapshots the step's selection and
|
|
222
|
+
* cannot await `resolveModelInfo`, so the asynchronous lookup fills this map
|
|
223
|
+
* ahead of time and a cold entry simply defers to the pre-step path.
|
|
224
|
+
*/
|
|
225
|
+
resolved = new Map();
|
|
226
|
+
/** Latest direct user text per turn; a continuation step reuses it. */
|
|
227
|
+
latestTexts = new WeakMap();
|
|
228
|
+
constructor(ctx, options, hooks) {
|
|
229
|
+
this.ctx = ctx;
|
|
230
|
+
this.options = options;
|
|
231
|
+
this.hooks = hooks;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Resolve a Host or Agent service from the Agent scope first. Preset child
|
|
235
|
+
* plugins sit above the Agent scope, so Host-level services such as the
|
|
236
|
+
* session controller and LLM registry are not always visible from `ctx`.
|
|
237
|
+
*/
|
|
238
|
+
service(agent, name) {
|
|
239
|
+
try {
|
|
240
|
+
const scoped = agent?.ctx?.get?.(name);
|
|
241
|
+
if (scoped !== undefined)
|
|
242
|
+
return scoped;
|
|
243
|
+
}
|
|
244
|
+
catch {
|
|
245
|
+
// Fall through to the preset context.
|
|
246
|
+
}
|
|
247
|
+
try {
|
|
248
|
+
return this.ctx.get(name);
|
|
249
|
+
}
|
|
250
|
+
catch {
|
|
251
|
+
return undefined;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
/** Durable reasoning state, folded from the Session log by this preset. */
|
|
255
|
+
projectionState(agent) {
|
|
256
|
+
const session = agent?.session;
|
|
257
|
+
if (session === undefined)
|
|
258
|
+
return undefined;
|
|
259
|
+
try {
|
|
260
|
+
const projections = this.service(agent, 'sessionProjections');
|
|
261
|
+
const value = projections?.snapshot?.(session, [REASONING_PROJECTION_KEY])
|
|
262
|
+
?.values?.[REASONING_PROJECTION_KEY];
|
|
263
|
+
return typeof value === 'object' && value !== null
|
|
264
|
+
? value
|
|
265
|
+
: undefined;
|
|
266
|
+
}
|
|
267
|
+
catch {
|
|
268
|
+
return undefined;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
/** Explicit override and chat-box pick, projection-first. */
|
|
272
|
+
effective(agent) {
|
|
273
|
+
const projected = this.projectionState(agent);
|
|
274
|
+
const rawOverride = projected === undefined ? this.overrides.get(agent) : projected.override;
|
|
275
|
+
const override = rawOverride === 'low' || rawOverride === 'high' || rawOverride === 'max'
|
|
276
|
+
? rawOverride
|
|
277
|
+
: undefined;
|
|
278
|
+
const rawEffort = projected === undefined ? this.userChoices.get(agent) : projected.userEffort;
|
|
279
|
+
const userEffort = typeof rawEffort === 'string' && rawEffort.trim() !== ''
|
|
280
|
+
? rawEffort.trim()
|
|
281
|
+
: undefined;
|
|
282
|
+
return {
|
|
283
|
+
...override === undefined ? {} : { override },
|
|
284
|
+
...userEffort === undefined ? {} : { userEffort },
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Fold the projection back into this controller. A chat-box pick lands as a
|
|
289
|
+
* `model/selection` event, a `coding_reasoning` call as a tool event, and the
|
|
290
|
+
* projection is what survives a Host restart.
|
|
291
|
+
*/
|
|
292
|
+
syncProjection(agent) {
|
|
293
|
+
const session = agent?.session;
|
|
294
|
+
if (session === undefined)
|
|
295
|
+
return;
|
|
296
|
+
const state = this.projectionState(agent);
|
|
297
|
+
if (state === undefined)
|
|
298
|
+
return;
|
|
299
|
+
const override = state.override;
|
|
300
|
+
if (override === 'low' || override === 'high' || override === 'max') {
|
|
301
|
+
this.overrides.set(agent, override);
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
this.overrides.delete(agent);
|
|
305
|
+
}
|
|
306
|
+
const userEffort = typeof state.userEffort === 'string' && state.userEffort.trim() !== ''
|
|
307
|
+
? state.userEffort.trim()
|
|
308
|
+
: undefined;
|
|
309
|
+
if (userEffort === undefined)
|
|
310
|
+
this.userChoices.delete(agent);
|
|
311
|
+
else
|
|
312
|
+
this.userChoices.set(agent, userEffort);
|
|
313
|
+
// The route may have moved since the last lookup; warm the synchronous
|
|
314
|
+
// cache so the next inbox hook can choose an effort itself.
|
|
315
|
+
void this.prewarm(agent).catch(() => { });
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* The step's model selection. DSH 0.2.2 owns it on `agent.model`; every read
|
|
319
|
+
* and write goes through that service.
|
|
320
|
+
*/
|
|
321
|
+
selection(agent) {
|
|
322
|
+
const model = agent?.model;
|
|
323
|
+
if (typeof model?.current !== 'function')
|
|
324
|
+
return undefined;
|
|
325
|
+
try {
|
|
326
|
+
return model.current();
|
|
327
|
+
}
|
|
328
|
+
catch {
|
|
329
|
+
return undefined;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
/** Resolve a route's supported efforts in the background; failures stay unknown. */
|
|
333
|
+
async prewarm(agent) {
|
|
334
|
+
const current = this.selection(agent);
|
|
335
|
+
if (current === undefined)
|
|
336
|
+
return;
|
|
337
|
+
const key = `${current.provider}/${current.model}`;
|
|
338
|
+
if (this.support.has(key))
|
|
339
|
+
return;
|
|
340
|
+
await this.effortsFor(agent, current.provider, current.model, undefined);
|
|
341
|
+
}
|
|
342
|
+
async effortsFor(agent, provider, model, signal) {
|
|
343
|
+
const llm = this.service(agent, 'llm');
|
|
344
|
+
if (llm?.resolveModelInfo === undefined)
|
|
345
|
+
return null;
|
|
346
|
+
const key = `${provider}/${model}`;
|
|
347
|
+
const cached = this.support.get(key);
|
|
348
|
+
if (cached !== undefined)
|
|
349
|
+
return cached;
|
|
350
|
+
const pending = (async () => {
|
|
351
|
+
try {
|
|
352
|
+
const info = await llm.resolveModelInfo(provider, model, signal);
|
|
353
|
+
const efforts = Array.isArray(info?.reasoning?.efforts) ? info.reasoning.efforts : [];
|
|
354
|
+
const ids = efforts
|
|
355
|
+
.map((effort) => effort?.id)
|
|
356
|
+
.filter((id) => typeof id === 'string');
|
|
357
|
+
this.resolved.set(key, ids);
|
|
358
|
+
return ids;
|
|
359
|
+
}
|
|
360
|
+
catch {
|
|
361
|
+
// A transient capability lookup must not pin this route to "no
|
|
362
|
+
// reasoning" for the rest of the process. Retry after a short pause.
|
|
363
|
+
this.resolved.set(key, null);
|
|
364
|
+
if (signal?.aborted === true)
|
|
365
|
+
this.support.delete(key);
|
|
366
|
+
else
|
|
367
|
+
setTimeout(() => {
|
|
368
|
+
this.support.delete(key);
|
|
369
|
+
this.resolved.delete(key);
|
|
370
|
+
}, 30_000).unref?.();
|
|
371
|
+
return null;
|
|
372
|
+
}
|
|
373
|
+
})();
|
|
374
|
+
this.support.set(key, pending);
|
|
375
|
+
return pending;
|
|
376
|
+
}
|
|
377
|
+
/** Latest direct user message for one turn, derived at most once per turn. */
|
|
378
|
+
textFor(agent, turn) {
|
|
379
|
+
const cached = this.latestTexts.get(agent);
|
|
380
|
+
if (cached !== undefined && cached.turn === turn)
|
|
381
|
+
return cached.text;
|
|
382
|
+
const text = latestUserText(agent.session);
|
|
383
|
+
this.latestTexts.set(agent, { turn, text });
|
|
384
|
+
return text;
|
|
385
|
+
}
|
|
386
|
+
async supported(agent, signal) {
|
|
387
|
+
const current = this.selection(agent);
|
|
388
|
+
if (current === undefined)
|
|
389
|
+
return null;
|
|
390
|
+
return this.effortsFor(agent, current.provider, current.model, signal);
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* Prefer the effort the user picked in the chat box, but only while the route
|
|
394
|
+
* still accepts it; otherwise fall back to the level's closest effort.
|
|
395
|
+
*/
|
|
396
|
+
resolveEffort(choice, supported) {
|
|
397
|
+
const picked = choice.effort;
|
|
398
|
+
if (picked !== undefined && supported.includes(picked))
|
|
399
|
+
return picked;
|
|
400
|
+
return mapEffort(choice.level, supported);
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Record the effort as the session's model selection. The Host fixes a
|
|
404
|
+
* step's selection before prompt assembly and refuses an `agent/request`
|
|
405
|
+
* rewrite, so `agent.model.select()` is the only supported write; it applies
|
|
406
|
+
* from the next step, which is why the inbox hook runs it before `preStep`
|
|
407
|
+
* captures the selection for the incoming turn.
|
|
408
|
+
*/
|
|
409
|
+
selectEffort(agent, effort) {
|
|
410
|
+
const model = agent?.model;
|
|
411
|
+
if (typeof model?.select !== 'function')
|
|
412
|
+
return false;
|
|
413
|
+
const current = this.selection(agent);
|
|
414
|
+
if (current === undefined)
|
|
415
|
+
return false;
|
|
416
|
+
if (effort === undefined) {
|
|
417
|
+
this.applied.delete(agent);
|
|
418
|
+
return true;
|
|
419
|
+
}
|
|
420
|
+
if (String(current.reasoningEffort ?? '') === effort) {
|
|
421
|
+
this.applied.set(agent, effort);
|
|
422
|
+
return true;
|
|
423
|
+
}
|
|
424
|
+
try {
|
|
425
|
+
model.select({
|
|
426
|
+
provider: current.provider,
|
|
427
|
+
model: current.model,
|
|
428
|
+
reasoningEffort: ReasoningEffortId(effort),
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
catch {
|
|
432
|
+
return false;
|
|
433
|
+
}
|
|
434
|
+
this.applied.set(agent, effort);
|
|
435
|
+
return true;
|
|
436
|
+
}
|
|
437
|
+
/** Treat a malformed runtime value as the safe automatic mode. */
|
|
438
|
+
mode(value) {
|
|
439
|
+
return value === 'low' || value === 'high' || value === 'max' ? value : 'auto';
|
|
440
|
+
}
|
|
441
|
+
/** Classify one request; the caller decides when to write the selection. */
|
|
442
|
+
classify(agent, text, turn) {
|
|
443
|
+
const options = this.options();
|
|
444
|
+
const mode = this.mode(options.reasoning);
|
|
445
|
+
const plan = this.hooks.planOf(agent);
|
|
446
|
+
const classified = mode === 'auto'
|
|
447
|
+
? classifyTask(text, plan)
|
|
448
|
+
: {
|
|
449
|
+
level: mode,
|
|
450
|
+
score: 0,
|
|
451
|
+
reason: `fixed by Coding settings (${mode})`,
|
|
452
|
+
};
|
|
453
|
+
let level = classified.level;
|
|
454
|
+
let effort;
|
|
455
|
+
if (mode === 'auto') {
|
|
456
|
+
const previous = this.states.get(agent);
|
|
457
|
+
if (previous !== undefined && previous.turn === turn)
|
|
458
|
+
level = higher(previous.level, level);
|
|
459
|
+
// The durable projection is the authority: a chat-box pick may have
|
|
460
|
+
// landed since the last pre-step, and the inbox hook reads this before
|
|
461
|
+
// `syncProjection` runs for the incoming turn.
|
|
462
|
+
const { override, userEffort } = this.effective(agent);
|
|
463
|
+
if (userEffort !== undefined) {
|
|
464
|
+
// A pick in the chat box is the user's explicit decision for this
|
|
465
|
+
// session: skip the classifier instead of overwriting it each step.
|
|
466
|
+
level = levelForEffort(userEffort);
|
|
467
|
+
effort = userEffort;
|
|
468
|
+
classified.reason = `picked ${userEffort} in the chat box`;
|
|
469
|
+
}
|
|
470
|
+
else if (override !== undefined) {
|
|
471
|
+
level = override;
|
|
472
|
+
classified.reason = `session override (${override})`;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
const choice = {
|
|
476
|
+
...classified,
|
|
477
|
+
level,
|
|
478
|
+
...effort === undefined ? {} : { effort },
|
|
479
|
+
};
|
|
480
|
+
this.states.set(agent, { turn, level, choice });
|
|
481
|
+
return choice;
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* Choose for a message claimed by the inbox. The Host emits this hook before
|
|
485
|
+
* it snapshots the step's model selection, so a synchronous capability cache
|
|
486
|
+
* lets the automatic level reach the turn's first step instead of the next
|
|
487
|
+
* one. A cold cache writes nothing and leaves the pre-step path to catch up.
|
|
488
|
+
*/
|
|
489
|
+
applySync(agent, message, turn) {
|
|
490
|
+
if (agent?.session === undefined)
|
|
491
|
+
return undefined;
|
|
492
|
+
const text = directUserText(message);
|
|
493
|
+
if (text.trim() === '')
|
|
494
|
+
return undefined;
|
|
495
|
+
const choice = this.classify(agent, text, turn ?? this.hooks.turnOf(agent));
|
|
496
|
+
const current = this.selection(agent);
|
|
497
|
+
if (current !== undefined) {
|
|
498
|
+
const supported = this.resolved.get(`${current.provider}/${current.model}`);
|
|
499
|
+
if (supported !== undefined && supported !== null) {
|
|
500
|
+
this.selectEffort(agent, this.resolveEffort(choice, supported));
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
return choice;
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* Recompute the decision for the current step. When the caller supplies the
|
|
507
|
+
* admitted message batch, classify the request itself; otherwise fall back
|
|
508
|
+
* to the session's latest direct user message.
|
|
509
|
+
*/
|
|
510
|
+
async apply(agent, signal, messages, turn) {
|
|
511
|
+
if (agent?.session === undefined)
|
|
512
|
+
return undefined;
|
|
513
|
+
const admitted = messages === undefined ? '' : latestUserTextIn(messages);
|
|
514
|
+
const at = turn ?? this.hooks.turnOf(agent);
|
|
515
|
+
const text = admitted.trim() !== '' ? admitted : this.textFor(agent, at);
|
|
516
|
+
if (admitted.trim() !== '')
|
|
517
|
+
this.latestTexts.set(agent, { turn: at, text });
|
|
518
|
+
const choice = this.classify(agent, text, at);
|
|
519
|
+
const supported = await this.supported(agent, signal);
|
|
520
|
+
if (supported !== null)
|
|
521
|
+
this.selectEffort(agent, this.resolveEffort(choice, supported));
|
|
522
|
+
return choice;
|
|
523
|
+
}
|
|
524
|
+
/** Current selector value without resolving provider capabilities. */
|
|
525
|
+
currentEffort(agent) {
|
|
526
|
+
const value = this.selection(agent)?.reasoningEffort;
|
|
527
|
+
return value === undefined ? undefined : String(value);
|
|
528
|
+
}
|
|
529
|
+
/** Explicit in-session override; `auto` clears it. */
|
|
530
|
+
override(agent, value, turn) {
|
|
531
|
+
const requested = value === 'low' || value === 'high' || value === 'max' ? value : 'auto';
|
|
532
|
+
// An explicit tool decision replaces a chat-box pick.
|
|
533
|
+
this.userChoices.delete(agent);
|
|
534
|
+
const at = turn ?? this.hooks.turnOf(agent);
|
|
535
|
+
if (requested === 'auto') {
|
|
536
|
+
this.overrides.delete(agent);
|
|
537
|
+
const choice = classifyTask(this.textFor(agent, at), this.hooks.planOf(agent));
|
|
538
|
+
this.states.set(agent, { turn: at, level: choice.level, choice });
|
|
539
|
+
return choice;
|
|
540
|
+
}
|
|
541
|
+
this.overrides.set(agent, requested);
|
|
542
|
+
const choice = {
|
|
543
|
+
level: requested,
|
|
544
|
+
score: 0,
|
|
545
|
+
reason: `session override (${requested})`,
|
|
546
|
+
};
|
|
547
|
+
this.states.set(agent, { turn: at, level: requested, choice });
|
|
548
|
+
return choice;
|
|
549
|
+
}
|
|
550
|
+
describe(agent) {
|
|
551
|
+
return {
|
|
552
|
+
options: this.options(),
|
|
553
|
+
choice: this.states.get(agent)?.choice,
|
|
554
|
+
effort: this.applied.get(agent) ?? this.currentEffort(agent),
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
/**
|
|
558
|
+
* Where the level comes from, for `coding_reasoning`. The settings pin wins
|
|
559
|
+
* over an in-session override because it already suppresses the classifier.
|
|
560
|
+
* Without an agent the answer falls back to the settings alone.
|
|
561
|
+
*/
|
|
562
|
+
summary(agent) {
|
|
563
|
+
const mode = this.mode(this.options().reasoning);
|
|
564
|
+
if (mode !== 'auto') {
|
|
565
|
+
return { source: 'manual', level: mode, reason: 'pinned by Coding settings' };
|
|
566
|
+
}
|
|
567
|
+
if (agent === undefined)
|
|
568
|
+
return { source: 'auto' };
|
|
569
|
+
const { override, userEffort } = this.effective(agent);
|
|
570
|
+
if (override !== undefined) {
|
|
571
|
+
return { source: 'manual', level: override, reason: 'session override' };
|
|
572
|
+
}
|
|
573
|
+
if (userEffort !== undefined) {
|
|
574
|
+
return {
|
|
575
|
+
source: 'manual',
|
|
576
|
+
level: levelForEffort(userEffort),
|
|
577
|
+
reason: `picked ${userEffort} in the chat box`,
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
const choice = this.states.get(agent)?.choice;
|
|
581
|
+
if (choice === undefined)
|
|
582
|
+
return { source: 'auto' };
|
|
583
|
+
return { source: 'auto', level: choice.level, reason: choice.reason };
|
|
584
|
+
}
|
|
585
|
+
}
|