dsh-vscode-mode 0.30.0 → 0.31.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/README.md +54 -10
- package/lib/client.js +143 -52
- package/lib/client.js.map +1 -1
- package/lib/index.js +3549 -1465
- package/lib/index.js.map +1 -1
- package/package.json +1 -1
- package/skills/dsh-vscodemode-html-plan/LICENSE +202 -0
- package/skills/dsh-vscodemode-html-plan/SKILL.md +147 -0
- package/skills/dsh-vscodemode-html-plan/examples/scheduled-send.html +298 -0
- package/skills/dsh-vscodemode-html-plan/references/blocks.md +227 -0
- package/skills/dsh-vscodemode-html-plan/references/subagent-brief.md +17 -0
- package/skills/dsh-vscodemode-html-plan/references/upstream-SKILL.md +174 -0
- package/skills/dsh-vscodemode-html-plan/runtime/htmlplan.css +564 -0
- package/skills/dsh-vscodemode-html-plan/runtime/htmlplan.js +1299 -0
- package/skills/dsh-vscodemode-html-plan/runtime/pack.mjs +300 -0
- package/src/ai/llmTask.ts +178 -0
- package/src/ai/llmTypes.ts +6 -0
- package/src/ai/svnCommitMsg.ts +107 -0
- package/src/ai/svnTriage.ts +34 -85
- package/src/client/aiCommit.ts +57 -0
- package/src/client/htmlPlan/HtmlPlanPanel.ts +261 -0
- package/src/client/htmlPlan/planActions.ts +347 -0
- package/src/client/htmlPlan/planPage.ts +333 -0
- package/src/client/htmlPreview.ts +218 -0
- package/src/client/index.ts +8 -0
- package/src/client/officialSidebar.ts +11 -3
- package/src/client/planState.ts +8 -2
- package/src/client/sidebar/menuItems.ts +1 -0
- package/src/client/sidebar/panels/SvnPanel.ts +17 -0
- package/src/client/sidebar/types.ts +2 -0
- package/src/client/styles/editor.css +77 -0
- package/src/client/svnActions.ts +7 -0
- package/src/client/svnCommit.ts +109 -0
- package/src/client/svnNotice.ts +1 -1
- package/src/client/svnStatus.ts +5 -2
- package/src/client/ui/EditorView.ts +85 -7
- package/src/client/ui/McpSettings.ts +59 -0
- package/src/client/ui/SvnAiPlanDialog.ts +24 -4
- package/src/client/ui/SvnCommitDialog.ts +458 -0
- package/src/client/ui/commandCatalog.ts +11 -0
- package/src/client/ui/svnCommitModel.ts +204 -0
- package/src/fileOpenSettings.ts +113 -5
- package/src/htmlPlanCommand.ts +176 -0
- package/src/index.ts +4 -0
- package/src/shared/keybindings.ts +11 -0
- package/src/shared/rpc.ts +13 -4
- package/src/shared/svn.ts +191 -10
- package/src/shared/svnActions.ts +16 -0
- package/src/svn.ts +205 -47
- package/src/svnCommit.ts +348 -0
- package/src/svnCore.ts +254 -0
- package/src/svnToolJson.ts +73 -0
- package/src/svnTools.ts +566 -0
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-vscode-mode client — HTML 计划页的官方动作桥(状态读取、审批、退出、回填)。
|
|
3
|
+
*
|
|
4
|
+
* 所有官方客户端服务都是可选探测:缺失或版本不符时返回可读错误,绝不抛、绝不伪造批准。
|
|
5
|
+
* 审批仍走官方 ask_user_question 通道(remote.userQuestions.answer);退出计划模式走
|
|
6
|
+
* 官方命令(remote.commands.execute('/plan off')),与 composer 计划徽标同源。
|
|
7
|
+
* @author ddj 2026年11月21号
|
|
8
|
+
*/
|
|
9
|
+
import { createAddToConversation } from '../addToConversation.js'
|
|
10
|
+
import { findPendingReview, reviewAnswerOf, reviewLabelsOf, type PendingReview } from './planPage.js'
|
|
11
|
+
|
|
12
|
+
/** 客户端上下文的最小结构面。 */
|
|
13
|
+
export interface PlanActionCtx {
|
|
14
|
+
get: (name: string) => unknown
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** 动作结果(失败带可展示原因)。 */
|
|
18
|
+
export interface PlanActionResult {
|
|
19
|
+
ok: boolean
|
|
20
|
+
error?: string
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** 已安装的动作宿主(index.ts 装配一次;未安装时所有动作返回未就绪)。 */
|
|
24
|
+
let host: {
|
|
25
|
+
ctx: PlanActionCtx
|
|
26
|
+
appendText: (sessionId: string | undefined, text: string) => Promise<string>
|
|
27
|
+
sendTask: (sessionId: string | undefined, text: string) => Promise<PlanActionResult>
|
|
28
|
+
} | null = null
|
|
29
|
+
/** 取异常的展示文案。 */
|
|
30
|
+
function messageOf(error: unknown): string {
|
|
31
|
+
if (error instanceof Error) return error.message
|
|
32
|
+
return String(error ?? '未知错误')
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** 失败结果。 */
|
|
36
|
+
function fail(error: string): PlanActionResult {
|
|
37
|
+
return { ok: false, error }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** 成功结果。 */
|
|
41
|
+
function ok(): PlanActionResult {
|
|
42
|
+
return { ok: true }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** 结构化探测可选服务(缺失/抛错一律 undefined)。 */
|
|
46
|
+
function serviceOf(ctx: PlanActionCtx, name: string): unknown {
|
|
47
|
+
try {
|
|
48
|
+
return ctx.get(name)
|
|
49
|
+
} catch {
|
|
50
|
+
return undefined
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** 取会话绑定(sessions.binding);服务缺失返回 undefined。 */
|
|
55
|
+
function bindingOf(ctx: PlanActionCtx, sessionId: string): unknown {
|
|
56
|
+
const sessions = serviceOf(ctx, 'sessions') as { binding?: (id: string) => unknown } | undefined
|
|
57
|
+
if (!sessions || typeof sessions.binding !== 'function') return undefined
|
|
58
|
+
try {
|
|
59
|
+
return sessions.binding(sessionId)
|
|
60
|
+
} catch {
|
|
61
|
+
return undefined
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** 取会话投影 face(缺失/抛错返回 undefined)。 */
|
|
66
|
+
function faceOf(ctx: PlanActionCtx, sessionId: string, key: string): unknown {
|
|
67
|
+
const binding = bindingOf(ctx, sessionId) as { session?: { projections?: { faceOf?: (name: string) => unknown } } } | undefined
|
|
68
|
+
const faceOfFn = binding?.session?.projections?.faceOf
|
|
69
|
+
if (typeof faceOfFn !== 'function') return undefined
|
|
70
|
+
try {
|
|
71
|
+
return faceOfFn.call(binding!.session!.projections, key)
|
|
72
|
+
} catch {
|
|
73
|
+
return undefined
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** 投影 face 快照(无 face 返回 undefined)。 */
|
|
78
|
+
function faceSnapshot(face: unknown): unknown {
|
|
79
|
+
const getSnapshot = (face as { getSnapshot?: () => unknown } | undefined)?.getSnapshot
|
|
80
|
+
return typeof getSnapshot === 'function' ? getSnapshot.call(face) : undefined
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** 读 plan 投影快照({ active, pending } 或 undefined)。 */
|
|
84
|
+
export function readPlanProjection(sessionId: string): unknown {
|
|
85
|
+
if (!host) return undefined
|
|
86
|
+
return faceSnapshot(faceOf(host.ctx, sessionId, 'plan'))
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** 在线待审批的会话卡片(PendingQuestion 实例:带 answer 与 intent)。 */
|
|
90
|
+
interface LiveReview {
|
|
91
|
+
pending: { answer: (answer: unknown) => Promise<unknown> }
|
|
92
|
+
labels: { questionId: string; approveLabel: string; keepLabel: string | null }
|
|
93
|
+
callId: string
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* 从 uiSession 的 Session 状态里取当前 plan-review 卡片。
|
|
98
|
+
* 计划审批是阻塞请求,不进 userQuestions 投影;只有 pendingInteraction 带着 intent 与实时候选。
|
|
99
|
+
* @author ddj 2026年11月21号
|
|
100
|
+
* @param sessionId 会话 id
|
|
101
|
+
* @returns 在线卡片与解析出的标签,或 null
|
|
102
|
+
*/
|
|
103
|
+
function liveReviewOf(sessionId: string): LiveReview | null {
|
|
104
|
+
if (!host) return null
|
|
105
|
+
const ui = serviceOf(host.ctx, 'uiSession') as { sessionStatus?: { getSnapshot?: () => unknown } } | undefined
|
|
106
|
+
const snapshot = ui?.sessionStatus?.getSnapshot?.()
|
|
107
|
+
const get = (snapshot as { get?: (id: string) => unknown } | undefined)?.get
|
|
108
|
+
if (typeof get !== 'function') return null
|
|
109
|
+
let status: unknown
|
|
110
|
+
try {
|
|
111
|
+
status = get.call(snapshot, sessionId)
|
|
112
|
+
} catch {
|
|
113
|
+
return null
|
|
114
|
+
}
|
|
115
|
+
const pending = status !== null && typeof status === 'object'
|
|
116
|
+
? (status as { pendingInteraction?: unknown }).pendingInteraction
|
|
117
|
+
: undefined
|
|
118
|
+
if (pending === null || typeof pending !== 'object') return null
|
|
119
|
+
const row = pending as { kind?: unknown; sessionId?: unknown; callId?: unknown; questions?: unknown; answer?: unknown }
|
|
120
|
+
if (row.kind !== 'plan-review' || typeof row.answer !== 'function') return null
|
|
121
|
+
if (row.sessionId !== undefined && String(row.sessionId) !== sessionId) return null
|
|
122
|
+
const labels = reviewLabelsOf(Array.isArray(row.questions) ? row.questions[0] : undefined)
|
|
123
|
+
if (!labels) return null
|
|
124
|
+
const callId = typeof row.callId === 'string' ? row.callId : ''
|
|
125
|
+
return { pending: pending as LiveReview['pending'], labels, callId }
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** 读当前待审批的 plan-review(先看在线卡片,再退回投影)。 */
|
|
129
|
+
export function readPendingReview(sessionId: string): PendingReview | null {
|
|
130
|
+
if (!host) return null
|
|
131
|
+
const live = liveReviewOf(sessionId)
|
|
132
|
+
if (live) {
|
|
133
|
+
return { callId: live.callId, questionId: live.labels.questionId, approveLabel: live.labels.approveLabel, keepLabel: live.labels.keepLabel }
|
|
134
|
+
}
|
|
135
|
+
return findPendingReview(faceSnapshot(faceOf(host.ctx, sessionId, 'userQuestions')))
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** 订阅 plan 与 userQuestions 投影变化;返回合并的退订器。 */
|
|
139
|
+
export function subscribePlanFaces(sessionId: string, listener: () => void): () => void {
|
|
140
|
+
if (!host) return () => {}
|
|
141
|
+
const offs: Array<() => void> = []
|
|
142
|
+
for (const key of ['plan', 'userQuestions']) {
|
|
143
|
+
const face = faceOf(host.ctx, sessionId, key) as { subscribe?: (fn: () => void) => unknown } | undefined
|
|
144
|
+
if (!face || typeof face.subscribe !== 'function') continue
|
|
145
|
+
try {
|
|
146
|
+
const off = face.subscribe(() => listener())
|
|
147
|
+
if (typeof off === 'function') offs.push(off as () => void)
|
|
148
|
+
} catch {
|
|
149
|
+
/* 订阅失败不影响其他 face */
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return () => {
|
|
153
|
+
for (const off of offs) {
|
|
154
|
+
try {
|
|
155
|
+
off()
|
|
156
|
+
} catch {
|
|
157
|
+
/* 退订异常忽略 */
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** 官方 Typert 结果解包:{ ok, value } / { ok:false, error } / 裸值。 */
|
|
164
|
+
function unwrap(result: unknown): PlanActionResult {
|
|
165
|
+
if (result === null || typeof result !== 'object') return result === false ? fail('官方服务拒绝了本次操作') : ok()
|
|
166
|
+
const row = result as { ok?: unknown; error?: unknown }
|
|
167
|
+
if (row.ok === false) {
|
|
168
|
+
const detail = row.error as { message?: unknown } | string | undefined
|
|
169
|
+
const message = typeof detail === 'string' ? detail : (typeof detail?.message === 'string' ? detail.message : '官方服务拒绝了本次操作')
|
|
170
|
+
return fail(message)
|
|
171
|
+
}
|
|
172
|
+
return ok()
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* 安装动作宿主(index.ts 装配一次)。
|
|
177
|
+
* @author ddj 2026年11月21号
|
|
178
|
+
* @param ctx 客户端上下文
|
|
179
|
+
* @returns 幂等卸载器
|
|
180
|
+
*/
|
|
181
|
+
/** 安装选项(appendText / sendTask 可注入,便于单测;生产缺省用会话输入门面)。 */
|
|
182
|
+
export interface HtmlPlanActionOptions {
|
|
183
|
+
appendText?: (sessionId: string | undefined, text: string) => Promise<string>
|
|
184
|
+
sendTask?: (sessionId: string | undefined, text: string) => Promise<unknown>
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* 把会话投递结果归一成动作结果:只有 accepted/submitted 视为成功。
|
|
189
|
+
* @author ddj 2026年11月21号
|
|
190
|
+
* @param raw TaskSendResult 或布尔裸值
|
|
191
|
+
* @returns 动作结果
|
|
192
|
+
*/
|
|
193
|
+
function sendResultOf(raw: unknown): PlanActionResult {
|
|
194
|
+
if (raw === true) return ok()
|
|
195
|
+
if (raw === false) return fail('会话拒绝接收答复')
|
|
196
|
+
if (raw === null || typeof raw !== 'object') return fail('发送结果未知')
|
|
197
|
+
const row = raw as { status?: unknown; message?: unknown; ok?: unknown }
|
|
198
|
+
if (row.ok === true) return ok()
|
|
199
|
+
if (row.ok === false) return fail(typeof row.message === 'string' ? row.message : '发送失败')
|
|
200
|
+
const status = typeof row.status === 'string' ? row.status : ''
|
|
201
|
+
if (status === 'accepted' || status === 'submitted') return ok()
|
|
202
|
+
const detail = typeof row.message === 'string' && row.message ? ':' + row.message : ''
|
|
203
|
+
if (status === 'rejected') return fail('会话拒绝接收答复' + detail)
|
|
204
|
+
if (status === 'unavailable') return fail('对话输入不可用' + detail)
|
|
205
|
+
if (status === 'cancelled') return fail('发送已取消' + detail)
|
|
206
|
+
return fail('发送结果未知' + detail)
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export function installHtmlPlanActions(ctx: PlanActionCtx, options: HtmlPlanActionOptions = {}): () => void {
|
|
210
|
+
const injectedAppend = options.appendText
|
|
211
|
+
const injectedSend = options.sendTask
|
|
212
|
+
const addToConversation = injectedAppend && injectedSend ? null : createAddToConversation(ctx as never)
|
|
213
|
+
const sendInjected = injectedSend
|
|
214
|
+
const conversations = addToConversation
|
|
215
|
+
host = {
|
|
216
|
+
ctx,
|
|
217
|
+
appendText: injectedAppend ?? ((sessionId, text) => conversations!.appendText(sessionId, text)),
|
|
218
|
+
sendTask: typeof sendInjected === 'function'
|
|
219
|
+
? async (sessionId, text) => sendResultOf(await sendInjected(sessionId, text))
|
|
220
|
+
: async (sessionId, text) => {
|
|
221
|
+
const send = conversations?.sendTaskResult
|
|
222
|
+
if (typeof send !== 'function') return fail('当前 DSH 不支持直接发送,请用「填入对话输入框」')
|
|
223
|
+
return sendResultOf(await send.call(conversations, sessionId, text))
|
|
224
|
+
},
|
|
225
|
+
}
|
|
226
|
+
return () => {
|
|
227
|
+
host = null
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* 回答官方 plan-review:批准 = 选批准标签;不批准 = 选继续规划标签(若官方提供)。
|
|
233
|
+
* @author ddj 2026年11月21号
|
|
234
|
+
* @param sessionId 会话 id
|
|
235
|
+
* @param approve 是否批准
|
|
236
|
+
* @returns 动作结果
|
|
237
|
+
*/
|
|
238
|
+
export async function answerPlanReview(sessionId: string, approve: boolean): Promise<PlanActionResult> {
|
|
239
|
+
if (!host) return fail('计划动作桥未就绪,请在对话审批卡片中操作')
|
|
240
|
+
if (!sessionId) return fail('当前没有活动会话')
|
|
241
|
+
const live = liveReviewOf(sessionId)
|
|
242
|
+
if (live) {
|
|
243
|
+
const label = approve ? live.labels.approveLabel : live.labels.keepLabel
|
|
244
|
+
if (!label) return fail(approve ? '该审批没有可用的批准选项' : '官方审批没有「继续规划」选项,请在对话中要求修改')
|
|
245
|
+
try {
|
|
246
|
+
await live.pending.answer({ answers: [{ id: live.labels.questionId, selected: [label] }] })
|
|
247
|
+
return ok()
|
|
248
|
+
} catch (error) {
|
|
249
|
+
return fail('提交审批失败:' + messageOf(error))
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
const review = readPendingReview(sessionId)
|
|
253
|
+
if (!review) return fail('没有待审批的计划(可能已在别处审批)')
|
|
254
|
+
const answer = reviewAnswerOf(review, approve)
|
|
255
|
+
if (!answer) return fail(approve ? '该审批没有可用的批准选项' : '官方审批没有「继续规划」选项,请在对话中要求修改')
|
|
256
|
+
const questions = serviceOf(host.ctx, 'remote.userQuestions') as { answer?: (id: string, callId: string, value: unknown) => Promise<unknown> } | undefined
|
|
257
|
+
if (!questions || typeof questions.answer !== 'function') {
|
|
258
|
+
return fail('当前 DSH 不支持在插件内作答,请在对话审批卡片中操作')
|
|
259
|
+
}
|
|
260
|
+
try {
|
|
261
|
+
return unwrap(await questions.answer(sessionId, review.callId, answer))
|
|
262
|
+
} catch (error) {
|
|
263
|
+
return fail('提交审批失败:' + messageOf(error))
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* 退出计划模式(等价 composer 计划徽标的 /plan off)。
|
|
269
|
+
* @author ddj 2026年11月21号
|
|
270
|
+
* @param sessionId 会话 id
|
|
271
|
+
* @returns 动作结果
|
|
272
|
+
*/
|
|
273
|
+
export async function exitPlanMode(sessionId: string): Promise<PlanActionResult> {
|
|
274
|
+
if (!host) return fail('计划动作桥未就绪,请点 composer 计划徽标退出')
|
|
275
|
+
if (!sessionId) return fail('当前没有活动会话')
|
|
276
|
+
const commands = serviceOf(host.ctx, 'remote.commands') as { execute?: (id: string, line: string, attachments: unknown[]) => Promise<unknown> } | undefined
|
|
277
|
+
if (!commands || typeof commands.execute !== 'function') {
|
|
278
|
+
return fail('当前 DSH 不支持在插件内执行命令,请点 composer 计划徽标退出')
|
|
279
|
+
}
|
|
280
|
+
try {
|
|
281
|
+
return unwrap(await commands.execute(sessionId, '/plan off', []))
|
|
282
|
+
} catch (error) {
|
|
283
|
+
return fail('退出计划模式失败:' + messageOf(error))
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* 发送计划页答复给 agent:有待审批时先提交「继续规划」,再发送反馈正文。
|
|
289
|
+
* @author ddj 2026年11月21号
|
|
290
|
+
* @param sessionId 会话 id
|
|
291
|
+
* @param text 答复 markdown
|
|
292
|
+
* @returns 动作结果(失败带原因,答复文本不丢)
|
|
293
|
+
*/
|
|
294
|
+
export async function submitPlanResponse(sessionId: string, text: string): Promise<PlanActionResult> {
|
|
295
|
+
if (!host) return fail('计划动作桥未就绪')
|
|
296
|
+
if (!sessionId) return fail('当前没有活动会话')
|
|
297
|
+
const body = String(text ?? '').trim()
|
|
298
|
+
if (!body) return fail('答复为空:先在页面里作答再点「回复」')
|
|
299
|
+
// 有待审批卡片:先结算为「同意执行」,再把答复作为用户消息送出;失败则不发,避免半截状态。
|
|
300
|
+
if (readPendingReview(sessionId)) {
|
|
301
|
+
const approved = await answerPlanReview(sessionId, true)
|
|
302
|
+
if (!approved.ok) return fail('先同意执行失败,答复未发送:' + String(approved.error ?? ''))
|
|
303
|
+
}
|
|
304
|
+
try {
|
|
305
|
+
return await host.sendTask(sessionId, body)
|
|
306
|
+
} catch (error) {
|
|
307
|
+
return fail('发送失败:' + messageOf(error))
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* 打开官方审批卡片(兜底入口;能力缺失返回 false)。
|
|
313
|
+
* @author ddj 2026年11月21号
|
|
314
|
+
* @param sessionId 会话 id
|
|
315
|
+
* @returns 是否已请求官方卡片
|
|
316
|
+
*/
|
|
317
|
+
export function revealPlanReview(sessionId: string): boolean {
|
|
318
|
+
if (!host) return false
|
|
319
|
+
const review = readPendingReview(sessionId)
|
|
320
|
+
if (!review) return false
|
|
321
|
+
const panels = serviceOf(host.ctx, 'userQuestionPanels') as { reveal?: (id: string, callId: string) => unknown } | undefined
|
|
322
|
+
if (!panels || typeof panels.reveal !== 'function') return false
|
|
323
|
+
try {
|
|
324
|
+
return panels.reveal(sessionId, review.callId) !== false
|
|
325
|
+
} catch {
|
|
326
|
+
return false
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* 把页面答复填入对话输入框(复用既有引用/纯文本注入通道,不自动发送)。
|
|
332
|
+
* @author ddj 2026年11月21号
|
|
333
|
+
* @param sessionId 会话 id
|
|
334
|
+
* @param text 答复 markdown(纯文本写入)
|
|
335
|
+
* @returns 动作结果
|
|
336
|
+
*/
|
|
337
|
+
export async function fillComposer(sessionId: string, text: string): Promise<PlanActionResult> {
|
|
338
|
+
if (!host) return fail('计划动作桥未就绪')
|
|
339
|
+
if (!sessionId) return fail('当前没有活动会话')
|
|
340
|
+
if (!String(text ?? '').trim()) return fail('没有可填入的答复:先在页面里点 Copy response')
|
|
341
|
+
try {
|
|
342
|
+
const outcome = await host.appendText(sessionId, String(text))
|
|
343
|
+
return outcome === 'ok' || outcome === 'busy' ? ok() : fail('写入输入框失败(' + outcome + ')')
|
|
344
|
+
} catch (error) {
|
|
345
|
+
return fail('写入输入框失败:' + messageOf(error))
|
|
346
|
+
}
|
|
347
|
+
}
|
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-vscode-mode client — HTML 计划页的纯逻辑(标记解析、路径归位、打包检测、状态推导、审批答案)。
|
|
3
|
+
*
|
|
4
|
+
* 页面格式来自上游 html-plan(MIT);本模块不碰 DOM/React,便于 node 单测。
|
|
5
|
+
* 约定:exit_plan_mode 的 plan 正文里写一行「HTML 计划页:<path>」,
|
|
6
|
+
* 插件据此把计划页签切成 HTML 模式;缺行或文件不可读时回落 Markdown 计划预览。
|
|
7
|
+
* @author ddj 2026年11月21号
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** plan 投影的最小结构面。 */
|
|
11
|
+
export interface PlanProjectionLike {
|
|
12
|
+
active?: unknown
|
|
13
|
+
pending?: unknown
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** 计划页签的状态模式。 */
|
|
17
|
+
export type PlanUiMode = 'unknown' | 'off' | 'planning' | 'reviewing' | 'approved' | 'revising' | 'exited'
|
|
18
|
+
|
|
19
|
+
/** 计划页签状态(徽标文案 + 原始布尔)。 */
|
|
20
|
+
export interface PlanUiState {
|
|
21
|
+
mode: PlanUiMode
|
|
22
|
+
label: string
|
|
23
|
+
active: boolean
|
|
24
|
+
pending: boolean
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** 待审批的 plan-review 问题(从 userQuestions 投影快照解析)。 */
|
|
28
|
+
export interface PendingReview {
|
|
29
|
+
callId: string
|
|
30
|
+
questionId: string
|
|
31
|
+
approveLabel: string
|
|
32
|
+
keepLabel: string | null
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** 插件桥接脚本标记(重复注入的幂等判据)。 */
|
|
36
|
+
const BRIDGE_MARK = 'data-edrv-htmlplan-bridge'
|
|
37
|
+
|
|
38
|
+
/** 计划页标记行:HTML 计划页:<path>(中英文冒号都接受)。 */
|
|
39
|
+
const PAGE_LINE = /^[ \t]*HTML[ \t]*计划页[ \t]*[::][ \t]*(.+?)[ \t]*$/im
|
|
40
|
+
|
|
41
|
+
/** 相对未打包页面的运行时引用(存在即说明还没 pack)。 */
|
|
42
|
+
const RUNTIME_REF = /(?:href|src)\s*=\s*["']([^"']*htmlplan\.(?:css|js))(?:[?#][^"']*)?["']/gi
|
|
43
|
+
|
|
44
|
+
/** 桥接脚本:拦截 iframe 内剪贴板写入并转发给父级;失败不影响页面。 */
|
|
45
|
+
const BRIDGE_SCRIPT = [
|
|
46
|
+
'<script ' + BRIDGE_MARK + '>',
|
|
47
|
+
'(() => {',
|
|
48
|
+
' try {',
|
|
49
|
+
' const send = (text) => {',
|
|
50
|
+
' try { parent.postMessage({ source: "edrv-htmlplan", type: "copy", text: String(text ?? "") }, "*") } catch (error) { /* 父级不可达 */ }',
|
|
51
|
+
' }',
|
|
52
|
+
' const clipboard = navigator.clipboard',
|
|
53
|
+
' const original = clipboard && clipboard.writeText',
|
|
54
|
+
' if (clipboard && typeof original === "function") {',
|
|
55
|
+
' clipboard.writeText = function writeText(text) { send(text); return original.apply(this, arguments) }',
|
|
56
|
+
' }',
|
|
57
|
+
' const exec = document.execCommand && document.execCommand.bind(document)',
|
|
58
|
+
' if (exec) {',
|
|
59
|
+
' document.execCommand = function execCommand(command) {',
|
|
60
|
+
' try { if (String(command).toLowerCase() === "copy" && window.getSelection) send(window.getSelection().toString()) } catch (error) { /* 忽略 */ }',
|
|
61
|
+
' return exec.apply(document, arguments)',
|
|
62
|
+
' }',
|
|
63
|
+
' }',
|
|
64
|
+
' document.addEventListener("click", (event) => {',
|
|
65
|
+
' const node = event.target && event.target.nodeType === 3 ? event.target.parentElement : event.target',
|
|
66
|
+
' const button = node && node.closest ? node.closest("button") : null',
|
|
67
|
+
' const label = button ? String(button.textContent || "").trim() : ""',
|
|
68
|
+
' if (!/copy response|复制答复/i.test(label)) return',
|
|
69
|
+
' const texts = Array.from(document.querySelectorAll("pre, textarea, [data-htmlplan-response]")).map((el) => String(el.value ?? el.textContent ?? ""))',
|
|
70
|
+
' texts.sort((left, right) => right.length - left.length)',
|
|
71
|
+
' if (texts[0]) send(texts[0])',
|
|
72
|
+
' }, true)',
|
|
73
|
+
' let reviewPending = false',
|
|
74
|
+
' const hookSheet = () => {',
|
|
75
|
+
' const sheets = document.querySelectorAll(".nw-sheet")',
|
|
76
|
+
' for (const sheet of sheets) {',
|
|
77
|
+
' if (sheet.dataset.edrvReply) continue',
|
|
78
|
+
' const title = sheet.querySelector("header h3")',
|
|
79
|
+
' if (!title || !/your response|你的回复/i.test(title.textContent || "")) continue',
|
|
80
|
+
' const pre = sheet.querySelector("pre")',
|
|
81
|
+
' const footer = sheet.querySelector("footer")',
|
|
82
|
+
' if (!pre || !footer) continue',
|
|
83
|
+
' sheet.dataset.edrvReply = "1"',
|
|
84
|
+
' const reply = document.createElement("button")',
|
|
85
|
+
' reply.type = "button"',
|
|
86
|
+
' reply.className = "nw-btn primary"',
|
|
87
|
+
' reply.textContent = reviewPending ? "回复并执行" : "回复"',
|
|
88
|
+
' reply.title = "直接把这份答复发送给 DSH agent"',
|
|
89
|
+
' reply.addEventListener("click", (event) => {',
|
|
90
|
+
' event.preventDefault()',
|
|
91
|
+
' reply.disabled = true',
|
|
92
|
+
' reply.textContent = "发送中…"',
|
|
93
|
+
' try { parent.postMessage({ source: "edrv-htmlplan", type: "respond", text: String(pre.textContent || "") }, "*") } catch (error) { /* 父级不可达 */ }',
|
|
94
|
+
' }, true)',
|
|
95
|
+
' const copy = Array.from(footer.querySelectorAll("button")).find((btn) => /copy response|复制答复/i.test(btn.textContent || ""))',
|
|
96
|
+
' footer.insertBefore(reply, copy || footer.firstChild)',
|
|
97
|
+
' const hint = sheet.querySelector("p.hint")',
|
|
98
|
+
' if (hint) hint.textContent = reviewPending ? "点「回复并执行」= 同意计划并把答复发给 agent;要修改请先在页签点「继续规划」。" : "点「回复」直接发送给 agent;也可以复制后手动粘贴。"',
|
|
99
|
+
' }',
|
|
100
|
+
' }',
|
|
101
|
+
' try { new MutationObserver(() => { try { hookSheet() } catch (error) { /* 忽略 */ } }).observe(document.body || document.documentElement, { childList: true, subtree: true }) } catch (error) { /* 无 MutationObserver */ }',
|
|
102
|
+
' try { hookSheet() } catch (error) { /* 忽略 */ }',
|
|
103
|
+
' window.addEventListener("message", (event) => {',
|
|
104
|
+
' const data = event.data',
|
|
105
|
+
' if (!data || data.source !== "edrv-plan-host") return',
|
|
106
|
+
' if (data.type === "state") {',
|
|
107
|
+
' reviewPending = data.reviewPending === true',
|
|
108
|
+
' for (const sheet of document.querySelectorAll(".nw-sheet")) {',
|
|
109
|
+
' const reply = Array.from(sheet.querySelectorAll("footer button")).find((btn) => /回复|发送中|已发送|发送失败/.test(btn.textContent || ""))',
|
|
110
|
+
' if (reply && !reply.disabled) reply.textContent = reviewPending ? "回复并执行" : "回复"',
|
|
111
|
+
' const hint = sheet.querySelector("p.hint")',
|
|
112
|
+
' if (hint && !/发送失败/.test(hint.textContent || "")) hint.textContent = reviewPending ? "点「回复并执行」= 同意计划并把答复发给 agent;要修改请先在页签点「继续规划」。" : "点「回复」直接发送给 agent;也可以复制后手动粘贴。"',
|
|
113
|
+
' }',
|
|
114
|
+
' return',
|
|
115
|
+
' }',
|
|
116
|
+
' if (data.type !== "respond-result") return',
|
|
117
|
+
' for (const sheet of document.querySelectorAll(".nw-sheet")) {',
|
|
118
|
+
' const reply = Array.from(sheet.querySelectorAll("footer button")).find((btn) => /回复|发送中|已发送|发送失败/.test(btn.textContent || ""))',
|
|
119
|
+
' if (!reply) continue',
|
|
120
|
+
' if (data.ok === true) {',
|
|
121
|
+
' reply.textContent = "已发送"',
|
|
122
|
+
' setTimeout(() => { try { sheet.querySelector("header button")?.click() } catch (error) { /* 忽略 */ } }, 500)',
|
|
123
|
+
' } else {',
|
|
124
|
+
' reply.disabled = false',
|
|
125
|
+
' reply.textContent = "回复"',
|
|
126
|
+
' const hint = sheet.querySelector("p.hint")',
|
|
127
|
+
' if (hint) hint.textContent = "发送失败:" + String(data.message || "未知原因") + "。可复制后手动粘贴。"',
|
|
128
|
+
' }',
|
|
129
|
+
' }',
|
|
130
|
+
' })',
|
|
131
|
+
' try { parent.postMessage({ source: "edrv-htmlplan", type: "ready" }, "*") } catch (error) { /* 忽略 */ }',
|
|
132
|
+
' } catch (error) { /* 桥接失败不影响页面 */ }',
|
|
133
|
+
'})();',
|
|
134
|
+
'</script>',
|
|
135
|
+
].join('\n')
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* 从计划正文里取 HTML 计划页路径(去掉包裹的反引号/引号)。
|
|
139
|
+
* @author ddj 2026年11月21号
|
|
140
|
+
* @param markdown 计划正文
|
|
141
|
+
* @returns 相对或绝对路径,未声明时 null
|
|
142
|
+
*/
|
|
143
|
+
export function planPagePathOf(markdown: unknown): string | null {
|
|
144
|
+
const text = typeof markdown === 'string' ? markdown : ''
|
|
145
|
+
const match = PAGE_LINE.exec(text)
|
|
146
|
+
if (!match) return null
|
|
147
|
+
const raw = match[1].trim().replace(/^["'\`]+|["'\`]+$/g, '').trim()
|
|
148
|
+
return raw ? raw.replace(/\\/g, '/') : null
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* 把计划页路径归位到工作区绝对路径(相对路径按 cwd 拼接)。
|
|
153
|
+
* @author ddj 2026年11月21号
|
|
154
|
+
* @param cwd 工作区目录
|
|
155
|
+
* @param pagePath 计划声明里的路径
|
|
156
|
+
* @returns 归一化路径(/ 分隔);空输入返回空串
|
|
157
|
+
*/
|
|
158
|
+
export function resolvePlanPagePath(cwd: unknown, pagePath: string): string {
|
|
159
|
+
const raw = String(pagePath ?? '').trim()
|
|
160
|
+
if (!raw) return ''
|
|
161
|
+
const normalized = raw.replace(/\\/g, '/')
|
|
162
|
+
if (/^(?:[a-zA-Z]:\/|\/)/.test(normalized)) return normalized
|
|
163
|
+
const base = String(cwd ?? '').replace(/\\/g, '/').replace(/\/+$/, '')
|
|
164
|
+
return base ? base + '/' + normalized.replace(/^\.\//, '') : normalized
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* 检测仍未内联的运行时引用(未执行 pack 的页面)。
|
|
169
|
+
* @author ddj 2026年11月21号
|
|
170
|
+
* @param html 页面文本
|
|
171
|
+
* @returns 引用串(去重,最多 8 条)
|
|
172
|
+
*/
|
|
173
|
+
export function unresolvedRuntimeRefs(html: unknown): string[] {
|
|
174
|
+
const text = typeof html === 'string' ? html : ''
|
|
175
|
+
const seen = new Set<string>()
|
|
176
|
+
for (const match of text.matchAll(RUNTIME_REF)) {
|
|
177
|
+
const ref = String(match[1] ?? '').trim()
|
|
178
|
+
if (ref) seen.add(ref)
|
|
179
|
+
if (seen.size >= 8) break
|
|
180
|
+
}
|
|
181
|
+
return [...seen]
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* 组装 iframe 的 srcdoc:注入剪贴板桥接脚本(已注入则原样返回)。
|
|
186
|
+
* @author ddj 2026年11月21号
|
|
187
|
+
* @param html 打包后的页面文本
|
|
188
|
+
* @returns 可安全交给 sandbox iframe 的 srcdoc
|
|
189
|
+
*/
|
|
190
|
+
export function buildPlanSrcDoc(html: unknown): string {
|
|
191
|
+
const text = typeof html === 'string' ? html : ''
|
|
192
|
+
if (!text.trim()) return ''
|
|
193
|
+
if (text.includes(BRIDGE_MARK)) return text
|
|
194
|
+
if (/<\/body\s*>/i.test(text)) return text.replace(/<\/body\s*>/i, BRIDGE_SCRIPT + '\n</body>')
|
|
195
|
+
return text + '\n' + BRIDGE_SCRIPT
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* 由 plan 投影 + 待审批状态 + 本地动作结果推导页签状态文案。
|
|
200
|
+
* @author ddj 2026年11月21号
|
|
201
|
+
* @param projection plan 投影快照(未知形状按不可用处理)
|
|
202
|
+
* @param reviewPending 是否存在 pending 的 plan-review 问题
|
|
203
|
+
* @param acted 本地刚完成的动作(approved / revising)
|
|
204
|
+
* @returns 页签状态
|
|
205
|
+
*/
|
|
206
|
+
export function derivePlanUiState(
|
|
207
|
+
projection: unknown,
|
|
208
|
+
reviewPending: boolean,
|
|
209
|
+
acted?: 'approved' | 'revising' | 'exited' | null,
|
|
210
|
+
): PlanUiState {
|
|
211
|
+
if (projection === null || typeof projection !== 'object') {
|
|
212
|
+
return { mode: 'unknown', label: '计划状态不可用', active: false, pending: false }
|
|
213
|
+
}
|
|
214
|
+
const row = projection as PlanProjectionLike
|
|
215
|
+
const active = row.active === true
|
|
216
|
+
const pending = row.pending === true
|
|
217
|
+
if (pending) return { mode: 'planning', label: '计划状态切换中', active, pending }
|
|
218
|
+
if (acted === 'approved') {
|
|
219
|
+
return { mode: 'approved', label: active ? '已批准 · 等待退出计划模式' : '已批准 · 已退出计划模式', active, pending }
|
|
220
|
+
}
|
|
221
|
+
if (acted === 'exited' && !active) return { mode: 'exited', label: '已退出计划模式', active, pending }
|
|
222
|
+
if (acted === 'revising' && active) return { mode: 'revising', label: '继续规划中', active, pending }
|
|
223
|
+
if (active && reviewPending) return { mode: 'reviewing', label: '待审批', active, pending }
|
|
224
|
+
if (active) return { mode: 'planning', label: '计划中', active, pending }
|
|
225
|
+
return { mode: 'off', label: '计划模式未开启', active, pending }
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** 选项标签归一:字符串或 { label } 数组。 */
|
|
229
|
+
function labelsOf(options: unknown): string[] {
|
|
230
|
+
if (!Array.isArray(options)) return []
|
|
231
|
+
const out: string[] = []
|
|
232
|
+
for (const option of options) {
|
|
233
|
+
if (typeof option === 'string') out.push(option.trim())
|
|
234
|
+
else if (option !== null && typeof option === 'object') {
|
|
235
|
+
const label = (option as { label?: unknown }).label
|
|
236
|
+
if (typeof label === 'string' && label.trim()) out.push(label.trim())
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return out
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** 按正则挑标签(大小写不敏感)。 */
|
|
243
|
+
function pickLabel(labels: string[], pattern: RegExp): string | null {
|
|
244
|
+
for (const label of labels) if (pattern.test(label)) return label
|
|
245
|
+
return null
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* 从一个 question 里提取 plan-review 的标签(批准 / 继续规划)。
|
|
250
|
+
* 官方内存请求带 intent 对象(approve 才是准);投影会丢 intent,只剩选项标签。
|
|
251
|
+
* @author ddj 2026年11月21号
|
|
252
|
+
* @param raw question 形状
|
|
253
|
+
* @returns 标签集合;不是 plan-review 或没有批准标签时 null
|
|
254
|
+
*/
|
|
255
|
+
export function reviewLabelsOf(raw: unknown): { questionId: string; approveLabel: string; keepLabel: string | null } | null {
|
|
256
|
+
if (raw === null || typeof raw !== 'object') return null
|
|
257
|
+
const question = raw as Record<string, unknown>
|
|
258
|
+
const intent = question.intent !== null && typeof question.intent === 'object'
|
|
259
|
+
? question.intent as Record<string, unknown>
|
|
260
|
+
: undefined
|
|
261
|
+
const labels = labelsOf(question.options)
|
|
262
|
+
const fromIntent = typeof intent?.approve === 'string' && intent.approve.trim() ? intent.approve.trim() : null
|
|
263
|
+
const questionId = typeof question.id === 'string' && question.id ? question.id : ''
|
|
264
|
+
const declaredReview = intent?.kind === 'plan-review' || questionId === 'plan-review'
|
|
265
|
+
const approveLabel = fromIntent ?? pickLabel(labels, /^(?:approve|approve plan|同意执行|同意)$/i) ?? pickLabel(labels, /approve|同意/i)
|
|
266
|
+
if (approveLabel === null) return null
|
|
267
|
+
if (!declaredReview && !labels.includes(approveLabel)) return null
|
|
268
|
+
const keepLabel = pickLabel(labels.filter(label => label !== approveLabel), /keep planning|继续规划|要求修改/i)
|
|
269
|
+
return { questionId: questionId || 'plan-review', approveLabel, keepLabel }
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** 从一行待审批记录里取 questions 数组。 */
|
|
273
|
+
function questionsOf(row: Record<string, unknown>): unknown[] {
|
|
274
|
+
const pending = row.pending !== null && typeof row.pending === 'object' ? row.pending as Record<string, unknown> : undefined
|
|
275
|
+
const request = row.request !== null && typeof row.request === 'object' ? row.request as Record<string, unknown> : undefined
|
|
276
|
+
const candidates = [pending?.questions, row.questions, request?.questions]
|
|
277
|
+
for (const value of candidates) if (Array.isArray(value)) return value
|
|
278
|
+
return []
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** 取该记录的调用 id(官方问答按 callId 结算)。 */
|
|
282
|
+
function callIdOf(row: Record<string, unknown>): string | null {
|
|
283
|
+
const pending = row.pending !== null && typeof row.pending === 'object' ? row.pending as Record<string, unknown> : undefined
|
|
284
|
+
const key = row.key !== null && typeof row.key === 'object' ? row.key as Record<string, unknown> : undefined
|
|
285
|
+
const wait = pending?.wait !== null && typeof pending?.wait === 'object' ? pending.wait as Record<string, unknown> : undefined
|
|
286
|
+
const values = [row.callId, pending?.callId, wait?.callId, (row.wait as Record<string, unknown> | undefined)?.callId, key?.callId, row.id]
|
|
287
|
+
for (const value of values) if (typeof value === 'string' && value) return value
|
|
288
|
+
return null
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* 在 userQuestions 投影快照里找待审批的 plan-review 问题。
|
|
293
|
+
* 形状容错:数组或 { items } 包裹;questions 可能在 row/questions/pending 三层。
|
|
294
|
+
* @author ddj 2026年11月21号
|
|
295
|
+
* @param snapshot 投影快照
|
|
296
|
+
* @returns 待审批描述,或 null
|
|
297
|
+
*/
|
|
298
|
+
export function findPendingReview(snapshot: unknown): PendingReview | null {
|
|
299
|
+
const rows: unknown[] = Array.isArray(snapshot)
|
|
300
|
+
? snapshot
|
|
301
|
+
: (snapshot !== null && typeof snapshot === 'object' && Array.isArray((snapshot as { items?: unknown }).items)
|
|
302
|
+
? (snapshot as { items: unknown[] }).items
|
|
303
|
+
: [])
|
|
304
|
+
for (const entry of rows) {
|
|
305
|
+
if (entry === null || typeof entry !== 'object') continue
|
|
306
|
+
const row = entry as Record<string, unknown>
|
|
307
|
+
const callId = callIdOf(row)
|
|
308
|
+
if (!callId) continue
|
|
309
|
+
for (const raw of questionsOf(row)) {
|
|
310
|
+
const labels = reviewLabelsOf(raw)
|
|
311
|
+
if (!labels) continue
|
|
312
|
+
return { callId, questionId: labels.questionId, approveLabel: labels.approveLabel, keepLabel: labels.keepLabel }
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
return null
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* 构造官方 ask_user_question 答案(批准 = 选批准标签;否则选继续规划标签)。
|
|
320
|
+
* @author ddj 2026年11月21号
|
|
321
|
+
* @param review 待审批描述
|
|
322
|
+
* @param approve 是否批准
|
|
323
|
+
* @returns 答案批次;不批准且没有问题级备选时 null
|
|
324
|
+
*/
|
|
325
|
+
export function reviewAnswerOf(
|
|
326
|
+
review: PendingReview | null,
|
|
327
|
+
approve: boolean,
|
|
328
|
+
): { answers: Array<{ id: string; selected: string[]; custom: string }> } | null {
|
|
329
|
+
if (!review) return null
|
|
330
|
+
const label = approve ? review.approveLabel : review.keepLabel
|
|
331
|
+
if (!label) return null
|
|
332
|
+
return { answers: [{ id: review.questionId, selected: [label], custom: '' }] }
|
|
333
|
+
}
|