dsh-vscode-mode 0.38.0 → 0.40.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.
Files changed (66) hide show
  1. package/README.md +38 -33
  2. package/lib/client.js +87 -68
  3. package/lib/client.js.map +1 -1
  4. package/lib/index.js +11406 -4920
  5. package/lib/index.js.map +1 -1
  6. package/package.json +3 -2
  7. package/skills/dsh-vscodemode-html-plan/SKILL.md +44 -42
  8. package/skills/dsh-vscodemode-test-check/SKILL.md +82 -0
  9. package/src/archiveDb.ts +24 -0
  10. package/src/attachmentBytes.ts +84 -0
  11. package/src/client/htmlPlan/HtmlPlanModeChip.ts +181 -0
  12. package/src/client/htmlPlan/HtmlPlanPanel.ts +230 -61
  13. package/src/client/htmlPlan/planActions.ts +38 -8
  14. package/src/client/htmlPlan/planPage.ts +111 -9
  15. package/src/client/index.ts +9 -0
  16. package/src/client/openReceipt.ts +5 -2
  17. package/src/client/planBridge.ts +27 -2
  18. package/src/client/planState.ts +51 -4
  19. package/src/client/sidebar/panels/TestCheckPanel.ts +485 -0
  20. package/src/client/sidebar/panels/useTestCheckData.ts +414 -0
  21. package/src/client/sidebar/types.ts +2 -0
  22. package/src/client/state/diffSummary.ts +3 -0
  23. package/src/client/styles/editor.css +91 -0
  24. package/src/client/testCheck/TestCheckChip.ts +181 -0
  25. package/src/client/testCheck/TestCheckPanel.ts +339 -0
  26. package/src/client/testCheckPanelState.ts +173 -0
  27. package/src/client/ui/EditorView.ts +77 -7
  28. package/src/client/ui/McpSettings.ts +2 -2
  29. package/src/client/ui/OfficialReview.ts +6 -4
  30. package/src/htmlPlanAttachments.ts +4 -1
  31. package/src/htmlPlanCommand.ts +87 -235
  32. package/src/htmlPlanGuidance.ts +67 -0
  33. package/src/htmlPlanMode.ts +547 -0
  34. package/src/index.ts +28 -55
  35. package/src/officialCapture.ts +58 -9
  36. package/src/pending/externalCapture.ts +195 -401
  37. package/src/pending/kernel.ts +24 -1
  38. package/src/pending/persist.ts +109 -22
  39. package/src/pending/provenance.ts +5 -28
  40. package/src/pending/store.ts +109 -100
  41. package/src/pending/tracking.ts +81 -0
  42. package/src/pendingCompat.ts +26 -1
  43. package/src/pendingRpc.ts +8 -56
  44. package/src/planAutoPage.ts +306 -0
  45. package/src/planPagePack.ts +148 -0
  46. package/src/registry.ts +7 -2
  47. package/src/reviewDb.ts +1 -1
  48. package/src/reviewRpc.ts +14 -6
  49. package/src/rpc.ts +34 -0
  50. package/src/shared/htmlPlanMode.ts +26 -0
  51. package/src/shared/pendingReview.ts +15 -7
  52. package/src/shared/review.ts +5 -0
  53. package/src/shared/rpc.ts +53 -7
  54. package/src/shared/testCheck.ts +759 -0
  55. package/src/shared/testCheckHtml.ts +276 -0
  56. package/src/shared/testCheckPolicyDefault.ts +37 -0
  57. package/src/shared/testCheckSnippets.ts +197 -0
  58. package/src/store.ts +17 -1
  59. package/src/testCheckCommand.ts +287 -0
  60. package/src/testCheckGuidance.ts +79 -0
  61. package/src/testCheckMode.ts +654 -0
  62. package/src/testCheckPolicy.ts +141 -0
  63. package/src/testCheckRpc.ts +196 -0
  64. package/src/testCheckStore.ts +822 -0
  65. package/src/pending/externalAlign.ts +0 -297
  66. package/src/pending/vcs.ts +0 -584
@@ -0,0 +1,547 @@
1
+ /**
2
+ * dsh-vscode-mode host — HTML 计划模式(独立模式,结构对齐官方 `@deepseek-ai/dsh-plan-mode`)。
3
+ *
4
+ * 四件套:
5
+ * 1. `sessionProjections` 注册 `html-plan` 投影:折叠 `/html-plan` 命令事件,得到 `{ active, entered }`;
6
+ * `entered` 粘性,用来让规则段落「首次进入后常驻」(与官方 plan:policy 一致)。
7
+ * 2. 两条系统提示 section:`html-plan:policy`(常驻规则,首句限定只在活跃时适用)与
8
+ * `html-plan:status`(每个请求按 active 输出一句话)。
9
+ * 3. `/html-plan [off|任务]` 命令:只切换模式并把任务作为普通用户消息送进会话。
10
+ * 4. `exit_html_plan_mode` 工具:从首次进入起常驻准入(对齐官方渐进式披露),执行时再校验活跃;
11
+ * 审批走官方 `userQuestions` 的 `plan-review` intent,批准后执行 `/html-plan off` 收尾。
12
+ *
13
+ * 官方 `plan/mode` 状态、`/plan`、`exit_plan_mode` 一律不碰;本模式与官方计划模式互斥:
14
+ * 任意一次成功的 `/plan` 命令都会把本模式折回 inactive。
15
+ * @author ddj 2026年11月25号
16
+ */
17
+ import { z } from 'zod'
18
+ import { log } from './log.js'
19
+ import { loadHostModule } from './hostImport.js'
20
+ import { htmlPlanCommandDefinition, type AttachmentPrep, type HtmlPlanInvocation } from './htmlPlanCommand.js'
21
+ import { htmlPlanPolicyText, htmlPlanStatusText } from './htmlPlanGuidance.js'
22
+ import { HTML_PLAN_EXIT_TOOL, HTML_PLAN_MODE_KEY, HTML_PLAN_OFF_LINE } from './shared/htmlPlanMode.js'
23
+ import type { Ctx } from './store.js'
24
+
25
+ //region 结构面
26
+
27
+ /** 投影状态(只描述模式本身;官方计划状态仍由官方 plan 投影负责)。 */
28
+ export interface HtmlPlanModeState {
29
+ /** 模式当前是否活跃。 */
30
+ active: boolean
31
+ /** 是否进入过模式(粘性;规则段落据此常驻)。 */
32
+ entered: boolean
33
+ /** 尚未结算的命令事件(run 与 done 配对用)。 */
34
+ running: { commandId: string; off: boolean } | null
35
+ }
36
+
37
+ /** 投影初始状态。 */
38
+ export const HTML_PLAN_MODE_INITIAL: HtmlPlanModeState = { active: false, entered: false, running: null }
39
+
40
+ /** 投影定义里运行时的最小结构面(stateOf 一次折叠到当前游标)。 */
41
+ interface ProjectionHandleLike {
42
+ stateOf: (session: unknown) => unknown
43
+ }
44
+
45
+ /** 会话投影服务最小结构面。 */
46
+ interface ProjectionsLike {
47
+ register?: (definition: unknown) => Promise<ProjectionHandleLike>
48
+ }
49
+
50
+ /** 系统提示服务最小结构面。 */
51
+ interface SystemPromptLike {
52
+ section?: (spec: unknown) => unknown
53
+ getSectionOrder?: (name: string) => number
54
+ }
55
+
56
+ /** 工具服务最小结构面。 */
57
+ interface ToolsLike {
58
+ register?: (definition: unknown, options?: { when?: (context: { agent: { session: unknown } }) => boolean }) => unknown
59
+ }
60
+
61
+ /** 命令服务最小结构面。 */
62
+ interface CommandsLike {
63
+ register?: (definition: unknown) => unknown
64
+ execute?: (agent: unknown, line: string, attachments: readonly unknown[], signal?: AbortSignal) => Promise<{ result?: { kind?: string; text?: string } } | undefined>
65
+ }
66
+
67
+ /** 用户问答服务最小结构面。 */
68
+ interface UserQuestionsLike {
69
+ ask?: (request: unknown) => Promise<{ answers?: Array<{ id?: string; selected?: string[]; custom?: string }> }>
70
+ }
71
+
72
+ /** 退出工具的调用上下文最小结构面。 */
73
+ interface ExitToolExec {
74
+ agent?: { session?: unknown } | undefined
75
+ signal?: AbortSignal
76
+ callId?: unknown
77
+ concludeTurn?: () => void
78
+ }
79
+
80
+ /** 安装选项(测试可替换宿主模块加载与消息构造)。 */
81
+ export interface HtmlPlanModeOptions {
82
+ /** 把纯文本包成宿主用户消息;缺省从 `@deepseek-ai/dsh-llm` 取 createUserMessage。 */
83
+ buildUserMessage?: (text: string) => unknown
84
+ /** 附件准备(默认落盘到 plans/<slug>/assets/);测试可替换。 */
85
+ prepareAttachments?: (invocation: HtmlPlanInvocation, task: string) => Promise<AttachmentPrep>
86
+ /** 宿主模块加载(测试可注入假的)。 */
87
+ loadModule?: (specifier: string) => Promise<unknown>
88
+ }
89
+
90
+ //endregion
91
+
92
+ //region 投影折叠(纯)
93
+
94
+ /**
95
+ * 折叠一条会话事件到模式状态。
96
+ *
97
+ * 只认两件事:`command/run`(`html-plan` 与官方 `plan`)与配对的 `command/done`。
98
+ * 成功才提交:`off` → inactive;`html-plan` → active 且 entered 置真;官方 `plan` → 关闭本模式(互斥)。
99
+ * @author ddj 2026年11月25号
100
+ * @param state 上一状态
101
+ * @param event 会话事件(只读 type/data)
102
+ * @returns 新状态;不关心的事件返回原引用
103
+ */
104
+ export function foldHtmlPlanMode(state: HtmlPlanModeState, event: unknown): HtmlPlanModeState {
105
+ const row = event !== null && typeof event === 'object' ? event as { type?: unknown; data?: unknown } : {}
106
+ const type = typeof row.type === 'string' ? row.type : ''
107
+ const data = row.data !== null && typeof row.data === 'object' ? row.data as Record<string, unknown> : {}
108
+
109
+ if (type === 'command/run') {
110
+ const name = typeof data.name === 'string' ? data.name : ''
111
+ if (name !== 'html-plan' && name !== 'plan') return state
112
+ const args = typeof data.args === 'string' ? data.args : ''
113
+ const commandId = typeof data.commandId === 'string' ? data.commandId : ''
114
+ // 官方 /plan 的任意成功动作都关闭本模式;html-plan 只有 off 才关。
115
+ const off = name === 'plan' ? true : args.trim().toLowerCase() === 'off'
116
+ return { ...state, running: { commandId, off } }
117
+ }
118
+
119
+ if (type === 'command/done') {
120
+ const running = state.running
121
+ if (!running) return state
122
+ if (String(data.commandId ?? '') !== running.commandId) return state
123
+ if (data.kind !== 'success') return { ...state, running: null }
124
+ const active = !running.off
125
+ return { active, entered: state.entered || active, running: null }
126
+ }
127
+
128
+ return state
129
+ }
130
+
131
+ /** 投影定义(key/版本/校验/折叠/wire 视图)。 */
132
+ export const htmlPlanProjectionDefinition = {
133
+ key: HTML_PLAN_MODE_KEY,
134
+ stateVersion: 1,
135
+ stateSchema: z.object({
136
+ active: z.boolean(),
137
+ entered: z.boolean(),
138
+ running: z.object({ commandId: z.string(), off: z.boolean() }).strict().nullable(),
139
+ }).strict(),
140
+ init: (): HtmlPlanModeState => HTML_PLAN_MODE_INITIAL,
141
+ apply: (state: HtmlPlanModeState, event: unknown): HtmlPlanModeState => foldHtmlPlanMode(state, event),
142
+ wire: {
143
+ viewSchema: z.object({ active: z.boolean() }).strict(),
144
+ view: (state: HtmlPlanModeState) => ({ active: state.active }),
145
+ },
146
+ }
147
+
148
+ //endregion
149
+
150
+ //region 退出工具
151
+
152
+ /** 官方计划审批问题的 id(客户端按它识别 plan-review)。 */
153
+ const REVIEW_ID = 'plan-review'
154
+
155
+ /** 批准标签(与官方 plan-review 一致)。 */
156
+ const APPROVE_LABEL = 'Approve'
157
+
158
+ /** 继续规划标签(与官方 plan-review 一致)。 */
159
+ const KEEP_PLANNING_LABEL = 'Keep planning'
160
+
161
+ /**
162
+ * 取计划正文第一个标题,用作工具卡标题。
163
+ * @author ddj 2026年11月25号
164
+ * @param plan 计划 markdown
165
+ * @returns 标题文本;没有标题时 undefined
166
+ */
167
+ function firstHeading(plan: unknown): string | undefined {
168
+ for (const line of String(plan ?? '').split('\n')) {
169
+ const match = /^#{1,6}\s+(.+?)\s*$/.exec(line)
170
+ if (match) return match[1]
171
+ }
172
+ return undefined
173
+ }
174
+
175
+ /**
176
+ * 取异常的文案。
177
+ * @author ddj 2026年11月25号
178
+ * @param error 捕获值
179
+ * @returns 可读文本
180
+ */
181
+ function messageOf(error: unknown): string {
182
+ if (error instanceof Error) return error.message
183
+ return String(error ?? '未知错误')
184
+ }
185
+
186
+ /** 退出工具依赖。 */
187
+ export interface ExitHtmlPlanToolDeps {
188
+ /** 模式是否活跃。 */
189
+ activeOf: (session: unknown) => boolean
190
+ /** 取出用户问答服务(每次调用时探测)。 */
191
+ questions: () => UserQuestionsLike | undefined
192
+ /** 执行 `/html-plan off` 并返回成败。 */
193
+ exitMode: (agent: unknown, signal?: AbortSignal) => Promise<{ ok: boolean; error?: string }>
194
+ }
195
+
196
+ /**
197
+ * 构造退出工具定义(`defineTool` 的入参)。
198
+ * @author ddj 2026年11月25号
199
+ * @param deps 状态读取、问答通道与退出动作
200
+ * @returns 工具定义参数
201
+ */
202
+ export function exitHtmlPlanToolOptions(deps: ExitHtmlPlanToolDeps) {
203
+ /**
204
+ * 提交计划并等待审批;批准则退出模式。
205
+ * @author ddj 2026年11月25号
206
+ * @param args 工具参数
207
+ * @param exec 调用上下文
208
+ * @returns `{ approved }`
209
+ */
210
+ async function execute(args: { plan?: unknown }, exec: ExitToolExec): Promise<{ approved: boolean }> {
211
+ const agent = exec?.agent
212
+ const session = agent?.session
213
+ if (!agent || !session) throw new Error('exit_html_plan_mode 需要调用方 agent')
214
+ if (!deps.activeOf(session)) throw new Error('HTML 计划模式未激活:请先 /html-plan 进入模式')
215
+
216
+ const plan = String(args?.plan ?? '').trim()
217
+ if (!/^#\s+\S/.test(plan)) throw new Error('exit_html_plan_mode 需要以 # 标题开头的完整 markdown 计划')
218
+
219
+ const questions = deps.questions()
220
+ if (!questions || typeof questions.ask !== 'function') {
221
+ throw new Error('当前宿主没有用户问答通道;请用 /html-plan off 离开模式')
222
+ }
223
+
224
+ let answer: { answers?: Array<{ id?: string; selected?: string[]; custom?: string }> }
225
+ try {
226
+ answer = await questions.ask({
227
+ questions: [{
228
+ id: REVIEW_ID,
229
+ header: 'Plan review',
230
+ question: 'Approve this plan and leave HTML plan mode?',
231
+ detail: plan,
232
+ options: [
233
+ { label: APPROVE_LABEL, description: 'Leave HTML plan mode; the plan is carried out from the next step.' },
234
+ { label: KEEP_PLANNING_LABEL, description: 'Stay in HTML plan mode; feedback goes back to the model.' },
235
+ ],
236
+ intent: { kind: 'plan-review', approve: APPROVE_LABEL, callId: exec.callId },
237
+ }],
238
+ agent,
239
+ signal: exec.signal,
240
+ })
241
+ } catch (cause) {
242
+ // 用户关闭审批改为发言:结束本轮,不算失败(与官方一致)。
243
+ if ((cause as { code?: unknown } | null)?.code === 'ASK_CANCELLED') {
244
+ if (typeof exec.concludeTurn === 'function') exec.concludeTurn()
245
+ return { approved: false }
246
+ }
247
+ throw cause
248
+ }
249
+
250
+ const items = (answer?.answers ?? []).filter((item) => item?.id === REVIEW_ID)
251
+ const item = items.length === 1 ? items[0] : undefined
252
+ if (!item || item.selected?.length !== 1 || item.selected[0] !== APPROVE_LABEL || item.custom) {
253
+ const feedback = item?.custom ?? ''
254
+ throw new Error(feedback
255
+ ? '用户选择继续规划;反馈:' + feedback
256
+ : '用户选择继续规划;请修订计划后重新提交')
257
+ }
258
+
259
+ const exited = await deps.exitMode(agent, exec.signal)
260
+ if (!exited.ok) throw new Error('批准后无法退出 HTML 计划模式:' + String(exited.error ?? '未知原因') + ';请手动执行 /html-plan off')
261
+ return { approved: true }
262
+ }
263
+
264
+ return {
265
+ name: HTML_PLAN_EXIT_TOOL,
266
+ description: 'Use only in HTML plan mode. Present the complete plan for the user\'s review and leave the mode on approval.',
267
+ parameters: {
268
+ plan: { type: 'string', required: true, description: 'The complete plan, as markdown, starting with a # heading that names it.' },
269
+ },
270
+ output: {
271
+ schema: {
272
+ type: 'object',
273
+ additionalProperties: false,
274
+ properties: { approved: { type: 'boolean', required: true } },
275
+ },
276
+ /** @author ddj 2026年11月25号 @param _args 参数 @param result 执行结果 @returns 模型可见文本 */
277
+ render: (_args: unknown, result: { approved?: boolean }) => [{
278
+ type: 'text',
279
+ text: result?.approved === true
280
+ ? 'Plan approved — HTML plan mode exited; carry out the plan starting with your next step.'
281
+ : 'The user dismissed the plan review to reply in their own words; HTML plan mode remains active.',
282
+ }],
283
+ },
284
+ /** @author ddj 2026年11月25号 @param args 参数 @returns 工具调用卡片 */
285
+ presentCall: (args: { plan?: unknown }) => ({
286
+ card: 'generic',
287
+ title: firstHeading(args?.plan) ?? 'Plan',
288
+ kind: 'other',
289
+ content: [{ type: 'text', text: String(args?.plan ?? '') }],
290
+ }),
291
+ /** @author ddj 2026年11月25号 @param _args 参数 @param result 结果 @returns 结果卡片 */
292
+ presentResult: (_args: unknown, result: { content?: unknown }) => ({
293
+ card: 'generic',
294
+ title: 'Plan review',
295
+ content: result?.content ?? [],
296
+ }),
297
+ isConcurrencySafe: false,
298
+ execute,
299
+ }
300
+ }
301
+
302
+ //endregion
303
+
304
+ //region 装配
305
+
306
+ /**
307
+ * 装配 HTML 计划模式:用 `ctx.inject` 等齐四项宿主服务后再注册。
308
+ *
309
+ * 服务齐了才装配(缺任何一项都不做半截模式);旧宿主没有 `sessionProjections` 时
310
+ * 调用会一直等待、不报错——`/html-plan` 命令自然不可用,普通 `/plan` 不受影响。
311
+ * @author ddj 2026年11月25号
312
+ * @param ctx 宿主上下文
313
+ * @param options 安装选项
314
+ * @returns 是否已调度装配
315
+ */
316
+ export function installHtmlPlanMode(ctx: Ctx, options: HtmlPlanModeOptions = {}): boolean {
317
+ if (typeof ctx?.inject !== 'function') {
318
+ log.warn('/html-plan 未装配:DSH 未提供 ctx.inject')
319
+ return false
320
+ }
321
+ try {
322
+ ctx.inject(
323
+ ['sessionProjections', 'systemPrompt', 'tools', 'commands'],
324
+ (sctx: unknown) => {
325
+ void setupHtmlPlanMode(sctx, options).catch((error: unknown) => {
326
+ log.warn('/html-plan 装配失败:' + messageOf(error))
327
+ })
328
+ },
329
+ )
330
+ return true
331
+ } catch (error) {
332
+ log.warn('/html-plan 装配调度失败:' + messageOf(error))
333
+ return false
334
+ }
335
+ }
336
+
337
+ //region 服务读取
338
+
339
+ /**
340
+ * 从(注入作用域的)宿主上下文取服务:优先 `ctx.get`,退回同名属性。
341
+ * @author ddj 2026年11月25号
342
+ * @param ctx 宿主上下文
343
+ * @param name 服务名
344
+ * @returns 服务实例或 undefined
345
+ */
346
+ function serviceOf(ctx: unknown, name: string): unknown {
347
+ const scope = ctx as { get?: (key: string) => unknown } | undefined
348
+ if (scope && typeof scope.get === 'function') {
349
+ try {
350
+ const value = scope.get(name)
351
+ if (value !== undefined) return value
352
+ } catch {
353
+ /* 未注入的服务在旧宿主上可能抛错:退回属性读取 */
354
+ }
355
+ }
356
+ return (ctx as Record<string, unknown> | undefined)?.[name]
357
+ }
358
+
359
+ /**
360
+ * 在宿主生命期里执行一次注册:有 `effect` 就交给它托管卸载,没有就直接跑。
361
+ * @author ddj 2026年11月25号
362
+ * @param ctx 宿主上下文
363
+ * @param run 注册动作
364
+ * @param label 诊断标签
365
+ * @returns 无
366
+ */
367
+ function effectOf(ctx: unknown, run: () => unknown, label: string): void {
368
+ const scope = ctx as { effect?: (action: () => unknown, label?: string) => unknown } | undefined
369
+ if (scope && typeof scope.effect === 'function') {
370
+ scope.effect(run, label)
371
+ return
372
+ }
373
+ run()
374
+ }
375
+
376
+ //endregion
377
+
378
+ /**
379
+ * 真正把投影、两条 section、退出工具与命令挂到宿主上。
380
+ * @author ddj 2026年11月25号
381
+ * @param ctx 已注入服务的宿主上下文
382
+ * @param options 安装选项
383
+ * @returns 是否装配成功
384
+ */
385
+ export async function setupHtmlPlanMode(ctx: unknown, options: HtmlPlanModeOptions = {}): Promise<boolean> {
386
+ const loadModule = typeof options.loadModule === 'function' ? options.loadModule : loadHostModule
387
+ const projections = serviceOf(ctx, 'sessionProjections') as ProjectionsLike | undefined
388
+ if (!projections || typeof projections.register !== 'function') {
389
+ log.warn('/html-plan 未装配:宿主没有 sessionProjections 服务(HTML 计划模式不可用)')
390
+ return false
391
+ }
392
+
393
+ let handle: ProjectionHandleLike
394
+ try {
395
+ handle = await projections.register(htmlPlanProjectionDefinition as unknown)
396
+ } catch (error) {
397
+ log.warn('/html-plan 未装配:会话投影注册失败:' + messageOf(error))
398
+ return false
399
+ }
400
+
401
+ /** @author ddj 2026年11月25号 @param session 会话 @returns 投影状态(异常按未进入处理) */
402
+ function stateOf(session: unknown): HtmlPlanModeState {
403
+ if (!session) return HTML_PLAN_MODE_INITIAL
404
+ try {
405
+ const raw = handle.stateOf(session) as Partial<HtmlPlanModeState> | undefined
406
+ return { active: raw?.active === true, entered: raw?.entered === true, running: raw?.running ?? null }
407
+ } catch {
408
+ return HTML_PLAN_MODE_INITIAL
409
+ }
410
+ }
411
+ /** @author ddj 2026年11月25号 @param session 会话 @returns 是否活跃 */
412
+ function activeOf(session: unknown): boolean {
413
+ return stateOf(session).active === true
414
+ }
415
+ /** @author ddj 2026年11月25号 @param session 会话 @returns 是否进入过模式 */
416
+ function enteredOf(session: unknown): boolean {
417
+ return stateOf(session).entered === true
418
+ }
419
+
420
+ // 两条系统提示 section(对齐官方 plan:policy / plan:status)。
421
+ const systemPrompt = serviceOf(ctx, 'systemPrompt') as SystemPromptLike | undefined
422
+ if (systemPrompt && typeof systemPrompt.section === 'function') {
423
+ let baseOrder = 550
424
+ try {
425
+ if (typeof systemPrompt.getSectionOrder === 'function') {
426
+ const order = systemPrompt.getSectionOrder('PLAN_POLICY')
427
+ if (typeof order === 'number' && Number.isFinite(order)) baseOrder = order
428
+ }
429
+ } catch {
430
+ /* 旧宿主没有该常量表:用回退顺序 */
431
+ }
432
+ const policy = {
433
+ name: 'vscode-mode:html-plan-policy',
434
+ retirement: 'HTML plan mode is no longer available. Its instructions no longer apply.',
435
+ order: baseOrder + 0.5,
436
+ /** @author ddj 2026年11月25号 @param asm 装配上下文 @returns 规则文本或空串 */
437
+ text: (asm: { agent?: { session?: unknown } }) => enteredOf(asm?.agent?.session) ? htmlPlanPolicyText() : '',
438
+ }
439
+ const status = {
440
+ name: 'vscode-mode:html-plan-status',
441
+ replacement: '',
442
+ order: baseOrder + 1.5,
443
+ /** @author ddj 2026年11月25号 @param asm 装配上下文 @returns 一行状态或空串 */
444
+ text: (asm: { agent?: { session?: unknown } }) => {
445
+ const session = asm?.agent?.session
446
+ if (!session) return ''
447
+ const state = stateOf(session)
448
+ return htmlPlanStatusText(state.entered, state.active)
449
+ },
450
+ }
451
+ effectOf(ctx, () => systemPrompt.section!(policy), 'vscode-mode: html plan policy section')
452
+ effectOf(ctx, () => systemPrompt.section!(status), 'vscode-mode: html plan status section')
453
+ } else {
454
+ log.warn('/html-plan 规则未注入:宿主没有 systemPrompt.section 服务')
455
+ }
456
+
457
+ // 退出工具:从首次进入起常驻准入(对齐官方渐进式披露)。
458
+ try {
459
+ const toolsModule = await loadModule('@deepseek-ai/dsh-tools') as { defineTool?: (options: unknown) => unknown }
460
+ if (typeof toolsModule?.defineTool === 'function') {
461
+ const definition = toolsModule.defineTool(exitHtmlPlanToolOptions({
462
+ activeOf,
463
+ questions: () => serviceOf(ctx, 'userQuestions') as UserQuestionsLike | undefined,
464
+ exitMode: (agent, signal) => runExitLine(ctx, agent, signal),
465
+ }))
466
+ effectOf(ctx, () => {
467
+ const tools = serviceOf(ctx, 'tools') as ToolsLike | undefined
468
+ if (!tools || typeof tools.register !== 'function') {
469
+ log.warn('exit_html_plan_mode 未注册:宿主没有 tools 服务')
470
+ return undefined
471
+ }
472
+ return tools.register(definition, { when: (context) => enteredOf(context?.agent?.session) })
473
+ }, 'vscode-mode: html plan exit tool')
474
+ } else {
475
+ log.warn('exit_html_plan_mode 未注册:无法从宿主加载 defineTool')
476
+ }
477
+ } catch (error) {
478
+ log.warn('exit_html_plan_mode 未注册:' + messageOf(error))
479
+ }
480
+
481
+ // 命令:`/html-plan [off|任务]`(commands 已在注入列表里,不会缺席)。
482
+ const llm = await safeLoad(loadModule, '@deepseek-ai/dsh-llm') as { createUserMessage?: (input: unknown) => unknown } | undefined
483
+ const buildUserMessage = typeof options.buildUserMessage === 'function'
484
+ ? options.buildUserMessage
485
+ : (typeof llm?.createUserMessage === 'function'
486
+ ? (text: string) => llm!.createUserMessage!({ content: [{ type: 'text', text }], source: { kind: 'user' } })
487
+ : undefined)
488
+
489
+ const commands = serviceOf(ctx, 'commands') as CommandsLike | undefined
490
+ if (!commands || typeof commands.register !== 'function') {
491
+ log.warn('/html-plan 命令未注册:commands 服务不支持 register')
492
+ return false
493
+ }
494
+ try {
495
+ const definition = htmlPlanCommandDefinition(commands, {
496
+ buildUserMessage,
497
+ prepareAttachments: options.prepareAttachments,
498
+ hostCtx: ctx,
499
+ })
500
+ effectOf(ctx, () => commands.register!(definition), 'vscode-mode: html-plan command')
501
+ log.info('/html-plan 已注册:HTML 计划模式(投影 + 规则 + 退出工具)')
502
+ } catch (error) {
503
+ log.warn('/html-plan 注册失败:' + messageOf(error))
504
+ return false
505
+ }
506
+ return true
507
+ }
508
+
509
+ /**
510
+ * 执行 `/html-plan off` 并归一成败(退出工具收尾用)。
511
+ * @author ddj 2026年11月25号
512
+ * @param ctx 宿主上下文
513
+ * @param agent 调用方 agent
514
+ * @param signal 取消信号
515
+ * @returns ok + 失败原因
516
+ */
517
+ async function runExitLine(ctx: unknown, agent: unknown, signal?: AbortSignal): Promise<{ ok: boolean; error?: string }> {
518
+ const commands = serviceOf(ctx, 'commands') as CommandsLike | undefined
519
+ if (!commands || typeof commands.execute !== 'function') return { ok: false, error: '命令服务不可用' }
520
+ try {
521
+ const execution = await commands.execute(agent, HTML_PLAN_OFF_LINE, [], signal)
522
+ if (!execution) return { ok: false, error: '命令没有返回结果' }
523
+ const kind = execution.result?.kind
524
+ if (kind === 'success') return { ok: true }
525
+ return { ok: false, error: String(execution.result?.text ?? '未知原因') }
526
+ } catch (error) {
527
+ return { ok: false, error: messageOf(error) }
528
+ }
529
+ }
530
+
531
+ /**
532
+ * 加载可选宿主模块;失败返回 undefined(不阻断装配)。
533
+ * @author ddj 2026年11月25号
534
+ * @param load 加载函数
535
+ * @param specifier 模块名
536
+ * @returns 模块命名空间或 undefined
537
+ */
538
+ async function safeLoad(load: (specifier: string) => Promise<unknown>, specifier: string): Promise<unknown> {
539
+ try {
540
+ return await load(specifier)
541
+ } catch (error) {
542
+ log.warn('宿主模块加载失败(' + specifier + '):' + messageOf(error))
543
+ return undefined
544
+ }
545
+ }
546
+
547
+ //endregion