dsh-project-flow 0.0.0-stage → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +189 -0
- package/LICENSE +128 -0
- package/README.md +422 -2
- package/aesthetic-library.mjs +288 -0
- package/aesthetic-seeds.mjs +373 -0
- package/aesthetic.mjs +273 -0
- package/assets/icon-180.png +0 -0
- package/assets/icon-256.png +0 -0
- package/assets/icon-512.png +0 -0
- package/client.js +1079 -0
- package/cordis.patch.yml +48 -0
- package/desktop.mjs +100 -0
- package/docs/COMMERCIAL.md +79 -0
- package/docs/DETAIL.md +279 -0
- package/docs/FEATURES.md +345 -0
- package/docs/LAUNCH.md +148 -0
- package/docs/MOMENTS.md +118 -0
- package/docs/RELEASE-0.3.0.md +52 -0
- package/docs/USAGE.md +232 -0
- package/domains.mjs +306 -0
- package/evidence.mjs +504 -0
- package/gate.mjs +389 -0
- package/icon.svg +12 -0
- package/index.js +5863 -0
- package/locale/en.json +24 -0
- package/locale/zh.json +24 -0
- package/package.json +86 -4
- package/prices.mjs +215 -0
- package/watchdog.mjs +583 -0
package/client.js
ADDED
|
@@ -0,0 +1,1079 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* project-flow 的状态栏(Client 半边)。
|
|
3
|
+
*
|
|
4
|
+
* 挂在 `conversation.session.header.actions`:会话标题栏里那组 chip
|
|
5
|
+
* (「标准模式」「N 个子智能体」就在那儿),和它们并排。
|
|
6
|
+
*
|
|
7
|
+
* 默认是紧凑一格:小进度条 + `2/4` + 状态。点它展开一个浮层,看全貌、
|
|
8
|
+
* 改三阶模型、停当前这一阶、或者从断点继续。收起状态不占地方。
|
|
9
|
+
*
|
|
10
|
+
* 那个槽位的契约写明「overflow is clipped and faded before the utilities」,
|
|
11
|
+
* 所以浮层必须 portal 到 document.body——留在原地会被裁掉。
|
|
12
|
+
*
|
|
13
|
+
* 两条硬约束:
|
|
14
|
+
*
|
|
15
|
+
* 1. **不轮询命令。** 每次命令执行都会往会话日志写一条 `command/run`,
|
|
16
|
+
* 一秒一次等于刷聊天记录。读状态走 `remote.workspaceFiles`,
|
|
17
|
+
* 宿主自己的只读文件服务,带 `changes()` 推送。只有用户点按钮才走命令。
|
|
18
|
+
* 2. **依赖压到最少。** `inject` 只写 `slots` 和 `remote`;多写一个名字写错,
|
|
19
|
+
* 整个插件不激活,连渲染都没有、错误还看不见。
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
window.__ModuleLoader__.load({
|
|
23
|
+
id: 'dsh-project-flow',
|
|
24
|
+
factory(require) {
|
|
25
|
+
const React = require('react')
|
|
26
|
+
const h = React.createElement
|
|
27
|
+
const { useCallback, useEffect, useRef, useState } = React
|
|
28
|
+
let createPortal
|
|
29
|
+
try {
|
|
30
|
+
createPortal = require('react-dom').createPortal
|
|
31
|
+
} catch {
|
|
32
|
+
createPortal = undefined
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const STAGES = [
|
|
36
|
+
{ no: 1, label: '需求', key: 'intent' },
|
|
37
|
+
{ no: 2, label: '规划', key: 'plan' },
|
|
38
|
+
{ no: 3, label: '实施', key: 'build' },
|
|
39
|
+
{ no: 4, label: '质检', key: 'check' },
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
/** 能改模型的只有 2/3/4 阶;第 1 阶跑在主对话,插件改不了。 */
|
|
43
|
+
const SWITCHABLE = [2, 3, 4]
|
|
44
|
+
|
|
45
|
+
/** 内置名字只为下拉好点;不在表里的照样能用 /flow-set 换。 */
|
|
46
|
+
/**
|
|
47
|
+
* 本机可用模型,从宿主现读,不写死。
|
|
48
|
+
*
|
|
49
|
+
* 别人的 dsh 里可能只有 deepseek 一家,也可能装了别的 provider。写死三个名字的
|
|
50
|
+
* 结果就是那些人的下拉里全是选不了的选项。取不到就返回空表,界面上如实说「没检测到」。
|
|
51
|
+
*/
|
|
52
|
+
async function readModels() {
|
|
53
|
+
try {
|
|
54
|
+
if (typeof fetch !== 'function') return []
|
|
55
|
+
const response = await fetch('plugins/dsh-project-flow/models', { cache: 'no-store' })
|
|
56
|
+
if (response.ok !== true) return []
|
|
57
|
+
const body = await response.json()
|
|
58
|
+
const groups = Array.isArray(body?.providers) ? body.providers : []
|
|
59
|
+
return groups
|
|
60
|
+
.filter(group => Array.isArray(group?.models) && group.models.length > 0)
|
|
61
|
+
.map(group => ({
|
|
62
|
+
id: typeof group.id === 'string' ? group.id : '',
|
|
63
|
+
name: typeof group.name === 'string' && group.name !== '' ? group.name : group.id,
|
|
64
|
+
models: group.models
|
|
65
|
+
.filter(model => typeof model?.id === 'string' && model.id !== '')
|
|
66
|
+
.map(model => ({
|
|
67
|
+
id: model.id,
|
|
68
|
+
name: typeof model.name === 'string' && model.name !== '' ? model.name : model.id,
|
|
69
|
+
})),
|
|
70
|
+
}))
|
|
71
|
+
} catch {
|
|
72
|
+
return []
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const INHERIT = '__inherit__'
|
|
77
|
+
const UNKNOWN = '__unknown__'
|
|
78
|
+
|
|
79
|
+
/** 和宿主那边的 FLOW_DIR_NAME 必须是同一个名字,有测试盯着。 */
|
|
80
|
+
const FLOW_DIR_NAME = '工作流'
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 能一键跑的命令,都摆在浮层里,省得回去打斜杠。
|
|
84
|
+
*
|
|
85
|
+
* 只放**不需要额外参数**的。
|
|
86
|
+
* `/flow` 要一段任务描述、`/flow-set` 要两个参数、`/flow-continue` 和
|
|
87
|
+
* `/flow-stop` 已经有按钮了——这些不重复列。
|
|
88
|
+
*/
|
|
89
|
+
const COMMANDS = [
|
|
90
|
+
{ label: '状态', line: '/flow-status', title: '看当前项目跑到第几段、返工几次' },
|
|
91
|
+
{ label: '模型', line: '/flow-models', title: '看四阶各用什么模型' },
|
|
92
|
+
{ label: '花费', line: '/flow-cost', title: '看这个项目花了多少:分模型统计的 token 与人民币' },
|
|
93
|
+
{ label: '费率', line: '/flow-prices', title: '本机每个可用模型的费率,以及哪个是估算' },
|
|
94
|
+
{ label: '体检', line: '/flow-doctor', title: '体检 .project-flow:遗留锁、损坏的 state.json、没收尾的阶段' },
|
|
95
|
+
{ label: '快照', line: '/flow-snapshots', title: '列出项目的全部快照,回滚时挑目标' },
|
|
96
|
+
{ label: '接续', line: '/flow-resume', title: '接续被中断的项目:留空先列出所有项目' },
|
|
97
|
+
{ label: '自检', line: '/flow-diag', title: '状态栏自检:宿主有没有把客户端入口放进启动图里' },
|
|
98
|
+
]
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* 只挂这一个:会话标题栏里那组 chip(`标准模式`、`N 个子智能体` 所在)。
|
|
102
|
+
*
|
|
103
|
+
* 之前为了排查,把输入框上面和下面那两个 dock 也一起挂了,结果同样的东西
|
|
104
|
+
* 出现三份。功能完全一样,留一个就够。
|
|
105
|
+
*/
|
|
106
|
+
const SLOT_CANDIDATES = [
|
|
107
|
+
'conversation.session.header.actions',
|
|
108
|
+
]
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* 回打宿主一次,告诉它我跑到哪一步了。
|
|
112
|
+
*
|
|
113
|
+
* 浏览器这半边出问题在宿主那边完全看不见——控制台里有一行、但没人会去翻。
|
|
114
|
+
* 所以 apply 跑起来打一次、第一次渲染打一次,宿主记下来,
|
|
115
|
+
* `/flow-diag` 里就能读出「没物化」还是「物化了没渲染」。
|
|
116
|
+
* 打不通就算了,绝对不影响面板本身。
|
|
117
|
+
*/
|
|
118
|
+
let beaconsSent = false
|
|
119
|
+
/**
|
|
120
|
+
* 从宿主返回里把「命令真正的返回值」剥出来。
|
|
121
|
+
*
|
|
122
|
+
* 这层包装在不同 DSH 版本上不一样,实测见过的形状:
|
|
123
|
+
* { kind, text } ← 直接给
|
|
124
|
+
* { commandId, result: { kind, text } } ← 命令服务的票据 + 结果
|
|
125
|
+
* { ok, value: { commandId, result } } ← 远程层再套一层
|
|
126
|
+
*
|
|
127
|
+
* 以前写死了某一种,换一层就全错——状态栏所有按钮点了都报
|
|
128
|
+
* 「命令没送到宿主」,可宿主那边明明算成功了。所以别赌层数:
|
|
129
|
+
* 一层层往下剥,剥到出现 kind 为止。
|
|
130
|
+
*/
|
|
131
|
+
function unwrapCommandValue(result) {
|
|
132
|
+
let cursor = result
|
|
133
|
+
for (let depth = 0; depth < 5; depth += 1) {
|
|
134
|
+
if (cursor === null || cursor === undefined || typeof cursor !== 'object') return undefined
|
|
135
|
+
if (typeof cursor.kind === 'string') return cursor
|
|
136
|
+
if (cursor.value !== undefined) {
|
|
137
|
+
cursor = cursor.value
|
|
138
|
+
continue
|
|
139
|
+
}
|
|
140
|
+
if (cursor.result !== undefined) {
|
|
141
|
+
cursor = cursor.result
|
|
142
|
+
continue
|
|
143
|
+
}
|
|
144
|
+
return undefined
|
|
145
|
+
}
|
|
146
|
+
return undefined
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function beacon(stage) {
|
|
150
|
+
try {
|
|
151
|
+
if (typeof fetch !== 'function') return
|
|
152
|
+
const target = `plugins/dsh-project-flow/ping?stage=${encodeURIComponent(stage)}`
|
|
153
|
+
fetch(target, { cache: 'no-store', keepalive: true }).catch(() => {})
|
|
154
|
+
} catch {
|
|
155
|
+
// 静默:探针不该成为新的故障点
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* 从一个不声明依赖的 ctx 上取服务。
|
|
161
|
+
*
|
|
162
|
+
* `ctx.get(name)` 是 Cordis 的查询入口;老版本或沙箱化的上下文上可能只有
|
|
163
|
+
* 属性访问,所以两条路都试。取不到返回 undefined,由调用方决定说什么。
|
|
164
|
+
*/
|
|
165
|
+
function pick(ctx, name) {
|
|
166
|
+
try {
|
|
167
|
+
const viaGet = typeof ctx?.get === 'function' ? ctx.get(name) : undefined
|
|
168
|
+
if (viaGet !== undefined) return viaGet
|
|
169
|
+
} catch {
|
|
170
|
+
// ctx.get 在某些代理上下文上会抛,退到属性访问
|
|
171
|
+
}
|
|
172
|
+
try {
|
|
173
|
+
return ctx?.[name]
|
|
174
|
+
} catch {
|
|
175
|
+
return undefined
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const STATUS_TEXT = {
|
|
180
|
+
planning: '规划中',
|
|
181
|
+
building: '实施中',
|
|
182
|
+
checking: '质检中',
|
|
183
|
+
passed: '已通过',
|
|
184
|
+
stopped: '已停止',
|
|
185
|
+
'watchdog-tripped': '熔断停了',
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** 一整栏的状态,几个判词:已通过 / 未通过 / 出错了 / 已停止 / 进行中。 */
|
|
189
|
+
function verdictOf(projects) {
|
|
190
|
+
if (projects.length === 0) return ''
|
|
191
|
+
if (projects.some(project => project.running === true)) return '进行中'
|
|
192
|
+
const statusOf = project => String(project.status ?? '')
|
|
193
|
+
// 「出错了」和「未通过」是两件事,不能混:
|
|
194
|
+
// 出错了 = 这一阶根本没跑完(子会话报错、超时、模型起不来)
|
|
195
|
+
// 未通过 = 跑完了,但质检判了 FAIL
|
|
196
|
+
// 之前两个都显示「未通过」,看着像流程坏了,其实很多时候只是质检意见没过。
|
|
197
|
+
if (projects.some(project => /error|出错|没跑完|失败:/iu.test(statusOf(project)))) return '出错了'
|
|
198
|
+
if (projects.some(project => /失败|failed|blocked/iu.test(statusOf(project)))) return '未通过'
|
|
199
|
+
// 停下来的不算「进行中」:它不动了,一直显示在跑会让人以为还在烧钱。
|
|
200
|
+
if (projects.some(project => /stopped|中断|停止/iu.test(statusOf(project)))) return '已停止'
|
|
201
|
+
const done = project => project.stage >= 4 && /passed/iu.test(statusOf(project))
|
|
202
|
+
if (projects.every(done)) return '已通过'
|
|
203
|
+
return '进行中'
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** chip 上只放得下两个字,用短名;浮层里再展开说。 */
|
|
207
|
+
function shortStatus(project) {
|
|
208
|
+
if (project === undefined) return ''
|
|
209
|
+
if (project.running === true) return '在跑'
|
|
210
|
+
const known = STATUS_TEXT[project.status]
|
|
211
|
+
if (known !== undefined) return known
|
|
212
|
+
const raw = typeof project.status === 'string' ? project.status : ''
|
|
213
|
+
if (raw.startsWith('规划失败')) return '规划失败'
|
|
214
|
+
if (raw.startsWith('实施失败')) return '实施失败'
|
|
215
|
+
if (raw.startsWith('质检失败')) return '质检失败'
|
|
216
|
+
if (raw.startsWith('checking')) return '质检中'
|
|
217
|
+
if (raw.startsWith('rework')) return '返工中'
|
|
218
|
+
return raw === '' ? '待跑' : raw
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** 停住了还能接着跑:阶段没走完,而且不在跑。 */
|
|
222
|
+
function resumable(project) {
|
|
223
|
+
return project !== undefined
|
|
224
|
+
&& project.running !== true
|
|
225
|
+
&& !(project.stage >= 4 && project.status === 'passed')
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function entryName(entry) {
|
|
229
|
+
if (typeof entry === 'string') return entry
|
|
230
|
+
if (entry !== null && typeof entry === 'object') {
|
|
231
|
+
const name = entry.name ?? entry.path ?? entry.fileName
|
|
232
|
+
if (typeof name === 'string') return name.replace(/\/+$/u, '')
|
|
233
|
+
}
|
|
234
|
+
return ''
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
async function readJson(files, sessionId, path) {
|
|
238
|
+
try {
|
|
239
|
+
const page = await files.read(sessionId, path, { offset: 0, limit: 600 }, undefined)
|
|
240
|
+
const text = page?.text ?? ''
|
|
241
|
+
return text === '' ? undefined : JSON.parse(text)
|
|
242
|
+
} catch {
|
|
243
|
+
return undefined
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* 一阶在长条里占什么状态。
|
|
249
|
+
* 只说自己确知的:跑完满格、没到空着、当前在动、停住静止。
|
|
250
|
+
* 阶段内部的步数拿不到,不编百分比。
|
|
251
|
+
*/
|
|
252
|
+
function segStateOf(entry, stage, project) {
|
|
253
|
+
if (project === undefined) return 'idle'
|
|
254
|
+
if (stage > entry.no) return 'done'
|
|
255
|
+
if (stage === 4 && project.status === 'passed') return 'done'
|
|
256
|
+
if (stage === entry.no) {
|
|
257
|
+
if (project.status === 'passed') return 'done'
|
|
258
|
+
if (project.running === true || /planning|building|checking|rework/u.test(project.status)) return 'active'
|
|
259
|
+
return 'halted'
|
|
260
|
+
}
|
|
261
|
+
return 'idle'
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const SEG_LABEL = { done: '已过', active: '进行中', halted: '停在这一阶', idle: '还没到' }
|
|
265
|
+
|
|
266
|
+
async function readPanel(files, sessionId) {
|
|
267
|
+
const dir = await files.list(sessionId, '.project-flow', undefined).catch(() => undefined)
|
|
268
|
+
const entries = Array.isArray(dir?.entries) ? dir.entries.map(entryName) : []
|
|
269
|
+
const names = entries.filter(name => name !== '' && !name.endsWith('.json') && !name.endsWith('.lock'))
|
|
270
|
+
|
|
271
|
+
const projects = []
|
|
272
|
+
for (const name of names.slice(0, 40)) {
|
|
273
|
+
const state = await readJson(files, sessionId, `.project-flow/${name}/state.json`)
|
|
274
|
+
if (state === undefined) continue
|
|
275
|
+
projects.push({
|
|
276
|
+
project: name,
|
|
277
|
+
stage: typeof state.stage === 'number' ? state.stage : 0,
|
|
278
|
+
status: typeof state.status === 'string' ? state.status : '',
|
|
279
|
+
rework: typeof state.rework === 'number' ? state.rework : 0,
|
|
280
|
+
maxRework: typeof state.maxRework === 'number' ? state.maxRework : 2,
|
|
281
|
+
routes: state.routes ?? undefined,
|
|
282
|
+
updatedAt: typeof state.updatedAt === 'string' ? state.updatedAt : null,
|
|
283
|
+
running: false,
|
|
284
|
+
})
|
|
285
|
+
}
|
|
286
|
+
// 锁文件是「在跑」最及时的信号:state.json 的 status 要等阶段切换才写
|
|
287
|
+
for (const project of projects) {
|
|
288
|
+
if (entries.includes(`${project.project}/run.lock`)) project.running = true
|
|
289
|
+
}
|
|
290
|
+
projects.sort((a, b) => String(b.updatedAt ?? '').localeCompare(String(a.updatedAt ?? '')))
|
|
291
|
+
const panelFile = await readJson(files, sessionId, '.project-flow/panel.json')
|
|
292
|
+
|
|
293
|
+
// 工作区到底在哪。
|
|
294
|
+
//
|
|
295
|
+
// 之前判断「是不是工作流目录」只看 `.project-flow` 存不存在——那等于要求
|
|
296
|
+
// 这里**已经跑过一次**项目,才承认它是工作流目录。新建的、或者还没跑过项目的
|
|
297
|
+
// 工作流目录永远是「未启用」,而且不告诉人该去哪儿。
|
|
298
|
+
//
|
|
299
|
+
// stat('.') 会带 absolutePath,于是这里能按和宿主同一套规则判断:
|
|
300
|
+
// 路径里有没有「工作流」这一段。
|
|
301
|
+
let rootPath
|
|
302
|
+
try {
|
|
303
|
+
const info = await files.stat(sessionId, '.', undefined)
|
|
304
|
+
rootPath = typeof info?.absolutePath === 'string' ? info.absolutePath : undefined
|
|
305
|
+
} catch {
|
|
306
|
+
rootPath = undefined
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const segments = typeof rootPath === 'string' ? rootPath.split(/[\\/]+/u).filter(Boolean) : []
|
|
310
|
+
const nameMatch = segments.includes(FLOW_DIR_NAME)
|
|
311
|
+
const separator = typeof rootPath === 'string' && rootPath.includes('\\') ? '\\' : '/'
|
|
312
|
+
const suggestPath = typeof rootPath !== 'string'
|
|
313
|
+
? undefined
|
|
314
|
+
: nameMatch
|
|
315
|
+
? rootPath
|
|
316
|
+
: `${rootPath}${rootPath.endsWith(separator) ? '' : separator}${FLOW_DIR_NAME}`
|
|
317
|
+
|
|
318
|
+
return {
|
|
319
|
+
projects,
|
|
320
|
+
routes: panelFile?.routes,
|
|
321
|
+
// 文件服务这条路读不到宿主的权威判断,只能按路径说个大概:
|
|
322
|
+
// 有「工作流」这一段才算在里面。准确口径走 /flow-panel。
|
|
323
|
+
inside: nameMatch === true,
|
|
324
|
+
watching: false,
|
|
325
|
+
ranBefore: dir !== undefined,
|
|
326
|
+
rootPath,
|
|
327
|
+
nameMatch,
|
|
328
|
+
suggestPath,
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
function FlowChip({ sessionId, files, runCommand }) {
|
|
333
|
+
if (beaconsSent !== true) {
|
|
334
|
+
beaconsSent = true
|
|
335
|
+
beacon('render')
|
|
336
|
+
}
|
|
337
|
+
const [panel, setPanel] = useState({ projects: [], routes: undefined, inside: undefined })
|
|
338
|
+
const [open, setOpen] = useState(false)
|
|
339
|
+
const [busy, setBusy] = useState(false)
|
|
340
|
+
const [notice, setNotice] = useState('')
|
|
341
|
+
const [catalog, setCatalog] = useState([])
|
|
342
|
+
|
|
343
|
+
const anchorRef = useRef(null)
|
|
344
|
+
const popRef = useRef(null)
|
|
345
|
+
const alive = useRef(true)
|
|
346
|
+
useEffect(() => {
|
|
347
|
+
alive.current = true
|
|
348
|
+
return () => { alive.current = false }
|
|
349
|
+
}, [])
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* 拿不到工作区文件服务时的退路:走一条命令。
|
|
353
|
+
*
|
|
354
|
+
* `remote.workspaceFiles` 在这个构建里取不到(`remote.commands` 却能取到),
|
|
355
|
+
* 而 `/flow-panel` 本来就返回同构的 JSON。走命令的代价是每次调用会在会话里
|
|
356
|
+
* 留一条 command/run——所以只在**没有文件服务**时用,而且只在挂载和展开时各调一次,
|
|
357
|
+
* 不做轮询。
|
|
358
|
+
*/
|
|
359
|
+
/** 把宿主给的那份数据落成面板状态。命令和 HTTP 两条来源共用。 */
|
|
360
|
+
const commitPanel = data => {
|
|
361
|
+
const rootPath = typeof data?.root === 'string' ? data.root : undefined
|
|
362
|
+
const segments = rootPath === undefined ? [] : rootPath.split(/[\\/]+/u).filter(Boolean)
|
|
363
|
+
const nameMatch = segments.includes(FLOW_DIR_NAME)
|
|
364
|
+
const separator = rootPath !== undefined && rootPath.includes('\\') ? '\\' : '/'
|
|
365
|
+
const suggestPath = rootPath === undefined
|
|
366
|
+
? undefined
|
|
367
|
+
: nameMatch
|
|
368
|
+
? rootPath
|
|
369
|
+
: `${rootPath}${rootPath.endsWith(separator) ? '' : separator}${FLOW_DIR_NAME}`
|
|
370
|
+
const projects = Array.isArray(data?.projects) ? data.projects : []
|
|
371
|
+
if (alive.current !== true) {
|
|
372
|
+
beacon('rc-dead')
|
|
373
|
+
return
|
|
374
|
+
}
|
|
375
|
+
setPanel({
|
|
376
|
+
projects,
|
|
377
|
+
routes: data?.routes,
|
|
378
|
+
inside: data?.inside !== false,
|
|
379
|
+
watching: data?.watching === true,
|
|
380
|
+
// 当前会话对着哪个项目:宿主按 cwd 算出来的,别在这儿再猜一遍
|
|
381
|
+
focus: typeof data?.focus === 'string' && data.focus !== '' ? data.focus : undefined,
|
|
382
|
+
gate: typeof data?.gate === 'string' ? data.gate : 'strict',
|
|
383
|
+
willCreate: typeof data?.willCreate === 'string' ? data.willCreate : undefined,
|
|
384
|
+
ranBefore: projects.length > 0,
|
|
385
|
+
rootPath,
|
|
386
|
+
nameMatch,
|
|
387
|
+
suggestPath,
|
|
388
|
+
})
|
|
389
|
+
beacon(`rc-set-${projects.length}`)
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* 拿状态栏数据——**只走 HTTP,绝不调命令**。
|
|
394
|
+
*
|
|
395
|
+
* 这是用户反复报的那个「点一下,对话框里就多一段看不懂的字」的真正原因:
|
|
396
|
+
* 命令服务每一次执行都会往会话里追加两条生命周期事件
|
|
397
|
+
* (`command/run` 和带 text 的 `command/done`),界面把它们当消息渲染出来。
|
|
398
|
+
* 而这里以前是「每次打开面板就调一次 /flow-panel 触发计算,再回头用 HTTP 取数据」,
|
|
399
|
+
* 于是每点一次状态栏,对话框里就多一条 `/flow-panel` 的结果——那是一条 JSON。
|
|
400
|
+
*
|
|
401
|
+
* 所以:取数据这件事和命令彻底分开。命令只留给用户主动点的动作,
|
|
402
|
+
* 那些结果本来就该出现在对话里;自动刷新不许在对话里留任何痕迹。
|
|
403
|
+
*/
|
|
404
|
+
const refreshPanel = useCallback(async () => {
|
|
405
|
+
if (sessionId === undefined) {
|
|
406
|
+
beacon('rc-no-session')
|
|
407
|
+
return false
|
|
408
|
+
}
|
|
409
|
+
// 关键是把 sid 带上:宿主拿它就能问出这个会话的工作目录,
|
|
410
|
+
// 不需要浏览器先知道路径(workspaceFiles 在有些 profile 里根本不存在)。
|
|
411
|
+
try {
|
|
412
|
+
const params = new URLSearchParams()
|
|
413
|
+
params.set('sid', String(sessionId))
|
|
414
|
+
// 顺手也带上 cwd:能给就给,宿主优先用它,省一次会话查找。
|
|
415
|
+
if (files !== undefined && files !== null) {
|
|
416
|
+
try {
|
|
417
|
+
const info = await files.stat(sessionId, '.', undefined)
|
|
418
|
+
if (typeof info?.absolutePath === 'string' && info.absolutePath !== '') {
|
|
419
|
+
params.set('cwd', info.absolutePath)
|
|
420
|
+
}
|
|
421
|
+
} catch {
|
|
422
|
+
// 取不到就靠 sid,宿主自己会问
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
const query = params.toString() === '' ? '' : `?${params.toString()}`
|
|
426
|
+
const res = await fetch(`plugins/dsh-project-flow/panel${query}`, { cache: 'no-store' })
|
|
427
|
+
beacon(`rc-http-${res.status}`)
|
|
428
|
+
if (res.ok !== true) return false
|
|
429
|
+
const data = await res.json()
|
|
430
|
+
if (data === null || typeof data !== 'object' || data.empty === true) {
|
|
431
|
+
beacon('rc-http-empty')
|
|
432
|
+
return false
|
|
433
|
+
}
|
|
434
|
+
beacon('rc-src-http')
|
|
435
|
+
commitPanel(data)
|
|
436
|
+
return true
|
|
437
|
+
} catch {
|
|
438
|
+
beacon('rc-http-threw')
|
|
439
|
+
return false
|
|
440
|
+
}
|
|
441
|
+
}, [sessionId, files])
|
|
442
|
+
|
|
443
|
+
const refresh = useCallback(async () => {
|
|
444
|
+
if (sessionId === undefined) return
|
|
445
|
+
// 先走 HTTP:宿主按 sid 认会话、自己算这一份,带得出 mine 和 cost。
|
|
446
|
+
// 这条路不产生任何会话事件,所以打开面板不会在对话里留下东西。
|
|
447
|
+
const committed = await refreshPanel()
|
|
448
|
+
if (committed === true) return
|
|
449
|
+
if (files !== undefined && files !== null) {
|
|
450
|
+
try {
|
|
451
|
+
const next = await readPanel(files, sessionId)
|
|
452
|
+
if (alive.current) setPanel(next)
|
|
453
|
+
} catch {
|
|
454
|
+
// 读不到就当没有项目,chip 不消失
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
}, [files, sessionId, refreshPanel])
|
|
458
|
+
|
|
459
|
+
useEffect(() => { void refresh() }, [refresh])
|
|
460
|
+
|
|
461
|
+
// 模型清单:挂载时读一次,展开浮层时再读一次(装新模型不用重启页面)。
|
|
462
|
+
// 这是本机注册表的一次查询,不写会话日志。
|
|
463
|
+
useEffect(() => {
|
|
464
|
+
let cancelled = false
|
|
465
|
+
void (async () => {
|
|
466
|
+
const groups = await readModels()
|
|
467
|
+
if (cancelled !== true) setCatalog(groups)
|
|
468
|
+
})()
|
|
469
|
+
return () => { cancelled = true }
|
|
470
|
+
}, [open])
|
|
471
|
+
|
|
472
|
+
// 跟着文件变化刷,不轮询。chip 收起时也要跟——不然 `2/4` 会停在旧值
|
|
473
|
+
useEffect(() => {
|
|
474
|
+
if (files === undefined || sessionId === undefined) return undefined
|
|
475
|
+
const controller = typeof AbortController === 'function' ? new AbortController() : undefined
|
|
476
|
+
let cancelled = false
|
|
477
|
+
let timer
|
|
478
|
+
const start = async () => {
|
|
479
|
+
try {
|
|
480
|
+
const stream = await files.changes(sessionId, '.project-flow', controller?.signal)
|
|
481
|
+
if (cancelled) return
|
|
482
|
+
for await (const _frame of stream) {
|
|
483
|
+
clearTimeout(timer)
|
|
484
|
+
timer = setTimeout(() => { void refresh() }, 120)
|
|
485
|
+
}
|
|
486
|
+
} catch {
|
|
487
|
+
// 监听失败就只靠打开时刷一次
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
void start()
|
|
491
|
+
return () => {
|
|
492
|
+
cancelled = true
|
|
493
|
+
clearTimeout(timer)
|
|
494
|
+
controller?.abort()
|
|
495
|
+
}
|
|
496
|
+
}, [files, sessionId, refresh])
|
|
497
|
+
|
|
498
|
+
// 浮层打开时:刷一次,并且按 Esc / 点外面关掉
|
|
499
|
+
useEffect(() => {
|
|
500
|
+
if (open !== true) return undefined
|
|
501
|
+
void refresh()
|
|
502
|
+
const onKey = event => { if (event.key === 'Escape') setOpen(false) }
|
|
503
|
+
const onDown = event => {
|
|
504
|
+
const target = event.target
|
|
505
|
+
if (popRef.current?.contains?.(target) === true) return
|
|
506
|
+
if (anchorRef.current?.contains?.(target) === true) return
|
|
507
|
+
setOpen(false)
|
|
508
|
+
}
|
|
509
|
+
document.addEventListener('keydown', onKey)
|
|
510
|
+
document.addEventListener('pointerdown', onDown, true)
|
|
511
|
+
return () => {
|
|
512
|
+
document.removeEventListener('keydown', onKey)
|
|
513
|
+
document.removeEventListener('pointerdown', onDown, true)
|
|
514
|
+
}
|
|
515
|
+
}, [open, refresh])
|
|
516
|
+
|
|
517
|
+
const act = useCallback(async line => {
|
|
518
|
+
if (runCommand === undefined) {
|
|
519
|
+
setNotice('宿主那边没接上命令服务')
|
|
520
|
+
return
|
|
521
|
+
}
|
|
522
|
+
setBusy(true)
|
|
523
|
+
setNotice('')
|
|
524
|
+
try {
|
|
525
|
+
const result = await runCommand(line)
|
|
526
|
+
/**
|
|
527
|
+
* 成功时**不**往面板下方贴结果文字。
|
|
528
|
+
*
|
|
529
|
+
* 那些命令的完整输出已经进了会话日志(宿主每次执行都会落一条
|
|
530
|
+
* command/done,带 text),界面上再贴一遍既重复又难读——
|
|
531
|
+
* 一段 1200 字的多行中文塞进 11px 的小框里,用户的原话是「看不懂」。
|
|
532
|
+
*
|
|
533
|
+
* 只有失败才留一句话:不然点了没反应,用户分不清是没生效还是真没变化。
|
|
534
|
+
*/
|
|
535
|
+
if (result.ok !== true) {
|
|
536
|
+
const reason = result.error ?? '命令没执行成功'
|
|
537
|
+
// 失败原因可能很长(比如整段门控说明),只留第一行,详情去日志看。
|
|
538
|
+
setNotice(reason.split('\n')[0].slice(0, 160))
|
|
539
|
+
} else {
|
|
540
|
+
// 成功只给一句确认,不贴正文。
|
|
541
|
+
//
|
|
542
|
+
// 正文(状态表、花费表、体检清单)已经完整进了会话日志,
|
|
543
|
+
// 界面上再塞一遍又多又难读。留这一句是为了让人知道「点到了」——
|
|
544
|
+
// 完全没有反馈的按钮,用起来像坏的。
|
|
545
|
+
setNotice('已执行 · 结果见会话日志')
|
|
546
|
+
}
|
|
547
|
+
await refresh()
|
|
548
|
+
} catch (error) {
|
|
549
|
+
setNotice(`命令出错:${error?.message ?? error}`)
|
|
550
|
+
} finally {
|
|
551
|
+
if (alive.current) setBusy(false)
|
|
552
|
+
}
|
|
553
|
+
}, [runCommand, refresh])
|
|
554
|
+
|
|
555
|
+
const running = panel.projects.find(project => project.running === true)
|
|
556
|
+
// 优先级:我这个会话开的那个 → 任意在跑的 → 当前目录对着的 → 最近更新的。
|
|
557
|
+
//
|
|
558
|
+
// 第一档是修「两个项目同时跑,两条状态栏显示一样」:
|
|
559
|
+
// 以前只认 running,两个都在跑时 find 永远返回排在前面的那个,
|
|
560
|
+
// 于是两个会话看到的是同一个项目。mine 由宿主按「这一轮是哪个会话发起的」标记。
|
|
561
|
+
const mine = panel.projects.find(project => project.mine === true)
|
|
562
|
+
const focused = panel.focus === undefined
|
|
563
|
+
? undefined
|
|
564
|
+
: panel.projects.find(project => project.project === panel.focus)
|
|
565
|
+
const target = mine ?? running ?? focused ?? panel.projects[0]
|
|
566
|
+
// 「停止」按钮该停谁:我自己开的优先,其次才是任意在跑的那个。
|
|
567
|
+
const stoppable = mine ?? running
|
|
568
|
+
const routes = panel.routes ?? target?.routes ?? undefined
|
|
569
|
+
const stage = target === undefined ? 0 : target.stage
|
|
570
|
+
const doneCount = target === undefined
|
|
571
|
+
? 0
|
|
572
|
+
: (target.stage >= 4 && target.status === 'passed' ? 4 : Math.max(0, Math.min(target.stage - 1, 4)))
|
|
573
|
+
|
|
574
|
+
const openPop = () => { setOpen(!open) }
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
* 浮层一开一收都把提示清掉。
|
|
578
|
+
*
|
|
579
|
+
* 以前只在切换按钮那一条路上清,而且只在收起时清:
|
|
580
|
+
* - 点 ×、按 Esc、点外面这另外三条关闭路径都不清,旧提示会留到下次打开;
|
|
581
|
+
* - 打开时也不清,于是「什么都没点,它就自己冒一段字」。
|
|
582
|
+
* 现在 open 一变就清,两头都干净。
|
|
583
|
+
*
|
|
584
|
+
* 注意它不会误清 act() 刚写的那句:act 不改变 open,
|
|
585
|
+
* 所以这个 effect 不会因为点按键而重跑。
|
|
586
|
+
*/
|
|
587
|
+
useEffect(() => {
|
|
588
|
+
setNotice('')
|
|
589
|
+
}, [open])
|
|
590
|
+
|
|
591
|
+
// 当前值统一成 `provider/model`,和下拉选项同一个形状。
|
|
592
|
+
// 这样选一下就能同时定 provider 和 model,不会出现「换了个 provider 家的模型、
|
|
593
|
+
// 却还在用原来 provider」的错配。
|
|
594
|
+
const modelOf = key => {
|
|
595
|
+
const raw = typeof routes?.[key] === 'string' ? routes[key] : ''
|
|
596
|
+
if (raw === '') return INHERIT
|
|
597
|
+
if (raw === '继承') return INHERIT
|
|
598
|
+
return raw
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
const setRoute = (no, key, value) => {
|
|
602
|
+
const label = STAGES.find(entry => entry.no === no).label
|
|
603
|
+
if (value === INHERIT) {
|
|
604
|
+
void act(`/flow-set ${label} 清空`)
|
|
605
|
+
return
|
|
606
|
+
}
|
|
607
|
+
const cut = value.indexOf('/')
|
|
608
|
+
// 带 provider 的写法:/flow-set 实施 deepseek xxx
|
|
609
|
+
void act(cut === -1
|
|
610
|
+
? `/flow-set ${label} ${value}`
|
|
611
|
+
: `/flow-set ${label} ${value.slice(0, cut)} ${value.slice(cut + 1)}`)
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
// ── 收起态:一格 chip ──────────────────────────────
|
|
615
|
+
// 「接不上」只有在这两条路都断的时候才成立:
|
|
616
|
+
// 既没有工作区文件服务,也没有命令服务。只要有一条能走,就别说接不上。
|
|
617
|
+
const blind = (files === undefined || files === null) && runCommand === undefined
|
|
618
|
+
// 状态栏显示的是「我那个工作流里跑到哪了」,不是「当前目录能不能开工」。
|
|
619
|
+
// 所以在别处打开会话时,只要那边有项目,照样把进度显示出来——
|
|
620
|
+
// 之前先判 inside,于是人在别处永远只看到「未启用」,工作流里明明跑着活。
|
|
621
|
+
const chipLabel = blind
|
|
622
|
+
? '工作模式 · 未接上'
|
|
623
|
+
// 数据还没到:别先喊「未启用」再改成别的,那一闪看着像出错
|
|
624
|
+
: panel.inside === undefined
|
|
625
|
+
? '工作模式'
|
|
626
|
+
: (() => {
|
|
627
|
+
// 只报状态词:已通过 / 未通过 / 进行中。
|
|
628
|
+
// 之前报「4/4」和「3 个项目」,既不像状态,也容易被读成
|
|
629
|
+
// 「当前这个活干完了」——用户要的是判词,不是计数。
|
|
630
|
+
const verdict = target === undefined ? verdictOf(panel.projects) : verdictOf([target])
|
|
631
|
+
if (verdict === '') {
|
|
632
|
+
return panel.inside !== true
|
|
633
|
+
? (typeof panel.willCreate === 'string' ? '工作模式 · 自动建目录' : '工作模式 · 未启用')
|
|
634
|
+
: '工作模式 · 就绪'
|
|
635
|
+
}
|
|
636
|
+
return panel.focus !== undefined && target !== undefined
|
|
637
|
+
? `${target.project} · ${verdict}`
|
|
638
|
+
: `工作模式 · ${verdict}`
|
|
639
|
+
})()
|
|
640
|
+
|
|
641
|
+
// 那行字就是全部结论,也报一次——界面到底显示了什么,
|
|
642
|
+
// 从宿主那边就能读到,不用再让人复述或截图。
|
|
643
|
+
useEffect(() => {
|
|
644
|
+
beacon(`chip-${chipLabel}`)
|
|
645
|
+
}, [chipLabel])
|
|
646
|
+
|
|
647
|
+
const chip = h('button', {
|
|
648
|
+
ref: anchorRef,
|
|
649
|
+
type: 'button',
|
|
650
|
+
className: 'pfchip' + (open ? ' is-open' : '') + ((blind || (panel.inside === false && panel.projects.length === 0)) ? ' is-off' : ''),
|
|
651
|
+
'aria-expanded': open,
|
|
652
|
+
'aria-haspopup': 'dialog',
|
|
653
|
+
title: blind
|
|
654
|
+
? '接不上宿主的工作区文件服务,读不到项目状态。这跟目录对不对无关。'
|
|
655
|
+
: (() => {
|
|
656
|
+
// 把「工作流到底在哪个目录」写进悬停提示。
|
|
657
|
+
//
|
|
658
|
+
// 用 wherePath(宿主编的、配置里的真实位置),不是会话目录:
|
|
659
|
+
// 以前这里显示 C 盘的会话目录,用户会以为工作流被建到 C 盘去了。
|
|
660
|
+
const where = typeof panel.wherePath === 'string' && panel.wherePath !== ''
|
|
661
|
+
? ` (工作流在:${panel.wherePath})`
|
|
662
|
+
: ''
|
|
663
|
+
return panel.inside !== true && target === undefined
|
|
664
|
+
? `这个工作区不在工作流里,工作模式没启用。点开看该切到哪。${where}`
|
|
665
|
+
: `点开看四阶全貌、改模型、停当前这一阶${where}`
|
|
666
|
+
})(),
|
|
667
|
+
onClick: openPop,
|
|
668
|
+
},
|
|
669
|
+
h('span', { className: 'pfchip-mini' },
|
|
670
|
+
STAGES.map(entry => h('span', {
|
|
671
|
+
key: entry.no,
|
|
672
|
+
className: `pfchip-seg is-${segStateOf(entry, stage, target)}`,
|
|
673
|
+
})),
|
|
674
|
+
),
|
|
675
|
+
h('span', { className: 'pfchip-text' }, chipLabel),
|
|
676
|
+
)
|
|
677
|
+
|
|
678
|
+
// 收起时也要带上样式表。
|
|
679
|
+
// 之前只有展开的浮层里带 <style>,收起状态下整份 CSS 根本没加载,
|
|
680
|
+
// 于是 <button> 露出浏览器默认的灰底加边框——看着像我没改样式。
|
|
681
|
+
if (open !== true) return h(React.Fragment, null, h('style', null, CSS), chip)
|
|
682
|
+
|
|
683
|
+
// ── 展开态:浮层 ──────────────────────────────────
|
|
684
|
+
const rect = anchorRef.current?.getBoundingClientRect?.() ?? { left: 0, bottom: 0, right: 0 }
|
|
685
|
+
const width = 320
|
|
686
|
+
const left = Math.max(8, Math.min(rect.right - width, (typeof window !== 'undefined' ? window.innerWidth : width) - width - 8))
|
|
687
|
+
const top = (rect.bottom ?? 0) + 6
|
|
688
|
+
|
|
689
|
+
const panelBody = h('div', {
|
|
690
|
+
ref: popRef,
|
|
691
|
+
className: 'pfpop',
|
|
692
|
+
role: 'dialog',
|
|
693
|
+
'aria-label': '工作模式状态',
|
|
694
|
+
style: { position: 'fixed', top: `${top}px`, left: `${left}px`, width: `${width}px` },
|
|
695
|
+
},
|
|
696
|
+
h('style', null, CSS),
|
|
697
|
+
|
|
698
|
+
h('div', { className: 'pfpop-head' },
|
|
699
|
+
h('span', { className: 'pfpop-title' }, target === undefined ? '工作模式' : target.project),
|
|
700
|
+
h('button', {
|
|
701
|
+
type: 'button',
|
|
702
|
+
className: 'pfpop-x',
|
|
703
|
+
'aria-label': '关闭',
|
|
704
|
+
onClick: () => setOpen(false),
|
|
705
|
+
}, '×'),
|
|
706
|
+
),
|
|
707
|
+
|
|
708
|
+
blind
|
|
709
|
+
? h('div', { className: 'pfpop-body' },
|
|
710
|
+
h('p', { className: 'pfpop-p' }, '接不上宿主的工作区文件服务,读不到项目状态。'),
|
|
711
|
+
h('p', { className: 'pfpop-p pfpop-dim' },
|
|
712
|
+
'这跟「目录对不对」是两件事:目录可能完全正确,只是这一层连接没建立。'),
|
|
713
|
+
)
|
|
714
|
+
: panel.inside === false && panel.projects.length === 0
|
|
715
|
+
? h('div', { className: 'pfpop-body' },
|
|
716
|
+
h('p', { className: 'pfpop-p' },
|
|
717
|
+
typeof panel.willCreate === 'string'
|
|
718
|
+
? '工作模式在「工作流」目录里跑,这个目录还没有——第一次开工会自动建好。'
|
|
719
|
+
: '这个工作区不在工作流目录里,工作模式没启用。'),
|
|
720
|
+
/**
|
|
721
|
+
* 工作流到底在哪。
|
|
722
|
+
*
|
|
723
|
+
* 以前这里显示的是 `panel.rootPath`,也就是「当前会话目录」——
|
|
724
|
+
* 人在默认工作区(C 盘)打开时会写成「当前工作区:C:\…」,
|
|
725
|
+
* 看着就像工作流被建到 C 盘去了。用户当场就问「我的工作流不是在 D 盘吗」。
|
|
726
|
+
*
|
|
727
|
+
* 现在优先用宿主算出来的 wherePath(配置的 flowRoot 里真正装着
|
|
728
|
+
* .project-flow 的那一层),实在没有才退回会话目录。
|
|
729
|
+
*/
|
|
730
|
+
(() => {
|
|
731
|
+
const where = typeof panel.wherePath === 'string' && panel.wherePath !== ''
|
|
732
|
+
? panel.wherePath
|
|
733
|
+
: (typeof panel.rootPath === 'string' && panel.rootPath !== '' ? panel.rootPath : undefined)
|
|
734
|
+
if (where === undefined) return null
|
|
735
|
+
return h('p', { className: 'pfpop-p' }, `工作流在:${where}`)
|
|
736
|
+
})(),
|
|
737
|
+
typeof panel.willCreate === 'string'
|
|
738
|
+
? h('p', { className: 'pfpop-p' }, `会自动建:${panel.willCreate}`)
|
|
739
|
+
: (typeof panel.wherePath === 'string' && panel.wherePath !== ''
|
|
740
|
+
? h('p', { className: 'pfpop-p pfpop-dim' }, '把工作区切到上面那个目录再开,插件不会替你换地方。')
|
|
741
|
+
: null),
|
|
742
|
+
h('p', { className: 'pfpop-p pfpop-dim' },
|
|
743
|
+
'重要、值得留档、要复盘的任务放进去做;不重要的直接做,不用走这套。'),
|
|
744
|
+
)
|
|
745
|
+
: h('div', { className: 'pfpop-body' },
|
|
746
|
+
// 四段全貌
|
|
747
|
+
h('div', { className: 'pftrack' },
|
|
748
|
+
STAGES.map(entry => {
|
|
749
|
+
const state = segStateOf(entry, stage, target)
|
|
750
|
+
return h('span', {
|
|
751
|
+
key: entry.no,
|
|
752
|
+
className: `pfseg is-${state}`,
|
|
753
|
+
title: `第 ${entry.no} 阶:${entry.label} · ${SEG_LABEL[state]}`,
|
|
754
|
+
},
|
|
755
|
+
h('span', { className: 'pfseg-label' }, entry.label),
|
|
756
|
+
h('span', { className: 'pfseg-state' }, SEG_LABEL[state]),
|
|
757
|
+
)
|
|
758
|
+
}),
|
|
759
|
+
),
|
|
760
|
+
|
|
761
|
+
h('div', { className: 'pfpop-line' },
|
|
762
|
+
h('span', null, target === undefined ? '没有进行中的项目' : shortStatus(target)),
|
|
763
|
+
target !== undefined && target.rework > 0
|
|
764
|
+
? h('span', { className: 'pfpop-dim' }, `返工 ${target.rework}/${target.maxRework}`)
|
|
765
|
+
: null,
|
|
766
|
+
h('span', { className: 'pfpop-grow' }),
|
|
767
|
+
target !== undefined ? h('span', { className: 'pfpop-dim' }, `${doneCount}/4 阶`) : null,
|
|
768
|
+
),
|
|
769
|
+
|
|
770
|
+
// 三阶模型
|
|
771
|
+
h('div', { className: 'pfpop-models' },
|
|
772
|
+
SWITCHABLE.map(no => {
|
|
773
|
+
const key = STAGES.find(entry => entry.no === no).key
|
|
774
|
+
const value = modelOf(key)
|
|
775
|
+
// 选项全部来自本机注册表。当前值如果不在表里(比如配置里写了个
|
|
776
|
+
// 已经卸载的模型),照样留在列表最前面,免得下拉显示成空白。
|
|
777
|
+
const seen = new Set()
|
|
778
|
+
const options = []
|
|
779
|
+
for (const group of catalog) {
|
|
780
|
+
for (const model of group.models) {
|
|
781
|
+
const item = `${group.id}/${model.id}`
|
|
782
|
+
if (seen.has(item)) continue
|
|
783
|
+
seen.add(item)
|
|
784
|
+
options.push({
|
|
785
|
+
value: item,
|
|
786
|
+
label: model.name === model.id ? model.id : `${model.name}(${group.name})`,
|
|
787
|
+
})
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
if (value !== INHERIT && value !== UNKNOWN && seen.has(value) !== true) {
|
|
791
|
+
options.unshift({ value, label: `${value}(当前,未在可用列表里)` })
|
|
792
|
+
}
|
|
793
|
+
// 一个都没检测到时,直接把这件事写在下拉里。
|
|
794
|
+
// 空下拉会让人以为界面坏了,而不是「这台机器上确实没有别的模型」。
|
|
795
|
+
if (catalog.length === 0) {
|
|
796
|
+
options.push({ value: '', label: '没检测到可用模型', disabled: true })
|
|
797
|
+
}
|
|
798
|
+
return h('label', { key, className: 'pfpop-model' },
|
|
799
|
+
h('span', null, STAGES.find(entry => entry.no === no).label),
|
|
800
|
+
h('select', {
|
|
801
|
+
value,
|
|
802
|
+
disabled: busy,
|
|
803
|
+
onChange: event => { setRoute(no, key, event.target.value) },
|
|
804
|
+
},
|
|
805
|
+
value === UNKNOWN ? h('option', { value: UNKNOWN }, '未知') : null,
|
|
806
|
+
h('option', { value: INHERIT }, '继承'),
|
|
807
|
+
options.length === 0
|
|
808
|
+
? h('option', { value: '', disabled: true }, '没检测到可用模型')
|
|
809
|
+
: options.map(item => h('option', {
|
|
810
|
+
key: item.value,
|
|
811
|
+
value: item.value,
|
|
812
|
+
disabled: item.disabled === true,
|
|
813
|
+
}, item.label)),
|
|
814
|
+
),
|
|
815
|
+
)
|
|
816
|
+
}),
|
|
817
|
+
),
|
|
818
|
+
|
|
819
|
+
// 命令:不用回输入框打斜杠了
|
|
820
|
+
h('div', { className: 'pfpop-cmds' },
|
|
821
|
+
COMMANDS.map(item => h('button', {
|
|
822
|
+
key: item.line,
|
|
823
|
+
type: 'button',
|
|
824
|
+
className: 'pfcmd',
|
|
825
|
+
disabled: busy,
|
|
826
|
+
title: `${item.line} —— ${item.title}`,
|
|
827
|
+
onClick: () => { void act(item.line) },
|
|
828
|
+
}, item.label)),
|
|
829
|
+
),
|
|
830
|
+
|
|
831
|
+
// 动作
|
|
832
|
+
//
|
|
833
|
+
// 停哪个:优先停「我自己开的那个」。
|
|
834
|
+
// 以前一律用 running——两个项目同时跑时 running 取的是排在前面的那个,
|
|
835
|
+
// 于是点自己这条状态栏的停止,停掉的可能是别人的项目。
|
|
836
|
+
h('div', { className: 'pfpop-foot' },
|
|
837
|
+
stoppable !== undefined
|
|
838
|
+
? h('button', {
|
|
839
|
+
type: 'button',
|
|
840
|
+
className: 'pfbtn is-stop',
|
|
841
|
+
disabled: busy,
|
|
842
|
+
onClick: () => { void act(`/flow-stop ${stoppable.project}`) },
|
|
843
|
+
}, busy ? '停止中…' : `停止第 ${stoppable.stage} 阶`)
|
|
844
|
+
: resumable(target)
|
|
845
|
+
? h('button', {
|
|
846
|
+
type: 'button',
|
|
847
|
+
className: 'pfbtn',
|
|
848
|
+
disabled: busy,
|
|
849
|
+
onClick: () => { void act(`/flow-continue ${target.project}`) },
|
|
850
|
+
}, busy ? '正在接…' : `从第 ${target.stage} 阶继续`)
|
|
851
|
+
: h('button', { type: 'button', className: 'pfbtn', disabled: true },
|
|
852
|
+
target === undefined ? '还没有项目' : '四阶已经走完'),
|
|
853
|
+
h('span', { className: 'pfpop-grow' }),
|
|
854
|
+
h('span', { className: 'pfpop-dim' }, '停住后进度不丢,可接着跑'),
|
|
855
|
+
),
|
|
856
|
+
),
|
|
857
|
+
|
|
858
|
+
// 提示条只在「这里真能点东西」时才渲染。
|
|
859
|
+
//
|
|
860
|
+
// 工作模式没启用(不在工作流目录、也没有任何项目)时,浮层里只有
|
|
861
|
+
// 一段说明,一个能点的按钮都没有——这种状态下冒出一条提示,
|
|
862
|
+
// 用户既不知道它从哪来,也没法据此做任何事。干脆不渲染。
|
|
863
|
+
// 这一条是硬保证:不管别处留下什么字符串,这个状态都不会出现提示条。
|
|
864
|
+
(notice === '' || blind || (panel.inside === false && panel.projects.length === 0))
|
|
865
|
+
? null
|
|
866
|
+
: h('div', { className: 'pfpop-notice' }, notice),
|
|
867
|
+
)
|
|
868
|
+
|
|
869
|
+
return h(React.Fragment, null,
|
|
870
|
+
chip,
|
|
871
|
+
createPortal === undefined ? panelBody : createPortal(panelBody, document.body),
|
|
872
|
+
)
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
// 只用 currentColor 和系统色,不赌 --dsw-alias-* 的具体名字:
|
|
876
|
+
// 名字改了最多难看,不会崩。浮层盖在内容上,必须有实底。
|
|
877
|
+
const CSS = [
|
|
878
|
+
// chip
|
|
879
|
+
// 底纹一律透明:常驻的东西不该在标题栏里多出一块色块,
|
|
880
|
+
// 和旁边「标准模式」「N 个子智能体」站在一起要像同一排。
|
|
881
|
+
// 边框也留透明,只在鼠标移上去和展开时才现出来——需要时看得见,
|
|
882
|
+
// 不需要时不占视觉。
|
|
883
|
+
// 字体完全不自己定,整套继承下来:字族、字号、字重、行高、字距。
|
|
884
|
+
// 它和「标准模式」「N 个子智能体」站在同一排,看起来必须是同一行字,
|
|
885
|
+
// 而不是插进去的一个小控件。透明度也不压,否则比旁边的字淡一档。
|
|
886
|
+
'.pfchip{display:inline-flex;align-items:center;gap:5px;height:20px;padding:0 2px;font-family:inherit;font-size:inherit;font-weight:inherit;font-style:inherit;line-height:inherit;letter-spacing:inherit;color:inherit;background:transparent;background-image:none;border:1px solid transparent;border-radius:6px;cursor:pointer;opacity:1;white-space:nowrap;max-width:15em;overflow:hidden;appearance:none;-webkit-appearance:none;box-shadow:none;outline:none;vertical-align:middle}',
|
|
887
|
+
'.pfchip:hover{opacity:1;border-color:currentColor}',
|
|
888
|
+
'.pfchip.is-open{opacity:1;border-color:currentColor;background:transparent}',
|
|
889
|
+
'.pfchip.is-off{opacity:.7}',
|
|
890
|
+
'.pfchip-mini{display:inline-flex;gap:1.5px;flex:0 0 auto}',
|
|
891
|
+
'.pfchip-seg{display:inline-block;width:6px;height:8px;border-radius:1.5px;border:1px solid currentColor;opacity:.28}',
|
|
892
|
+
'.pfchip-seg.is-done{opacity:.85}',
|
|
893
|
+
'.pfchip-seg.is-active{opacity:1}',
|
|
894
|
+
'.pfchip-seg.is-halted{opacity:.6}',
|
|
895
|
+
'.pfchip-text{overflow:hidden;text-overflow:ellipsis}',
|
|
896
|
+
|
|
897
|
+
// 浮层
|
|
898
|
+
'.pfpop{z-index:9999;display:flex;flex-direction:column;gap:8px;padding:10px;font-size:12px;line-height:1.5;color:CanvasText;background:Canvas;border:1px solid rgba(128,128,128,.38);border-radius:10px;box-shadow:0 10px 30px rgba(0,0,0,.28)}',
|
|
899
|
+
'.pfpop-head{display:flex;align-items:center;gap:8px}',
|
|
900
|
+
'.pfpop-title{font-weight:600;flex:1 1 auto;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}',
|
|
901
|
+
'.pfpop-x{font:inherit;font-size:14px;line-height:1;color:inherit;background:transparent;border:0;cursor:pointer;opacity:.6;padding:0 2px}',
|
|
902
|
+
'.pfpop-x:hover{opacity:1}',
|
|
903
|
+
'.pfpop-body{display:flex;flex-direction:column;gap:8px}',
|
|
904
|
+
'.pfpop-p{margin:0}',
|
|
905
|
+
'.pfpop-dim{opacity:.6}',
|
|
906
|
+
'.pfpop-line{display:flex;align-items:center;gap:8px}',
|
|
907
|
+
'.pfpop-grow{flex:1 1 auto}',
|
|
908
|
+
|
|
909
|
+
// 四段全貌
|
|
910
|
+
'.pftrack{display:flex;gap:3px;width:100%}',
|
|
911
|
+
'.pfseg{position:relative;flex:1 1 0;min-width:0;display:flex;flex-direction:column;gap:1px;align-items:flex-start;padding:4px 6px;border:1px solid rgba(128,128,128,.38);border-radius:6px;font-size:10px;overflow:hidden}',
|
|
912
|
+
'.pfseg::before{content:"";position:absolute;inset:0;background-color:currentColor;opacity:0}',
|
|
913
|
+
'.pfseg.is-done::before{opacity:.16}',
|
|
914
|
+
'.pfseg.is-active{border-color:currentColor;opacity:1;font-weight:600}',
|
|
915
|
+
'.pfseg.is-active::before{opacity:.14;background-color:transparent;background-image:repeating-linear-gradient(115deg,currentColor 0 5px,transparent 5px 11px);background-size:22px 100%;animation:pfflow 1.1s linear infinite}',
|
|
916
|
+
'.pfseg.is-halted::before{opacity:.07}',
|
|
917
|
+
'.pfseg.is-idle{opacity:.45}',
|
|
918
|
+
'.pfseg-label{position:relative;z-index:1}',
|
|
919
|
+
'.pfseg-state{position:relative;z-index:1;opacity:.65;font-size:9px}',
|
|
920
|
+
'@keyframes pfflow{from{background-position:0 0}to{background-position:22px 0}}',
|
|
921
|
+
'@media (prefers-reduced-motion: reduce){.pfseg.is-active::before{animation:none}}',
|
|
922
|
+
|
|
923
|
+
// 模型与动作
|
|
924
|
+
'.pfpop-models{display:flex;flex-wrap:wrap;gap:6px}',
|
|
925
|
+
'.pfpop-model{display:inline-flex;align-items:center;gap:4px;font-size:11px;opacity:.85}',
|
|
926
|
+
'.pfpop-model select{font:inherit;font-size:11px;color:inherit;background:Canvas;border:1px solid rgba(128,128,128,.45);border-radius:5px;padding:1px 3px;max-width:10em}',
|
|
927
|
+
'.pfpop-foot{display:flex;align-items:center;gap:8px;padding-top:2px;border-top:1px solid rgba(128,128,128,.22)}',
|
|
928
|
+
'.pfbtn{font:inherit;font-size:11px;color:inherit;background:transparent;border:1px solid currentColor;border-radius:6px;padding:2px 10px;cursor:pointer;opacity:.9;white-space:nowrap}',
|
|
929
|
+
'.pfbtn:hover:not(:disabled){opacity:1}',
|
|
930
|
+
'.pfbtn:disabled{opacity:.35;cursor:default}',
|
|
931
|
+
'.pfbtn.is-stop{border-color:rgba(128,128,128,.7)}',
|
|
932
|
+
'.pfpop-cmds{display:flex;flex-wrap:wrap;gap:4px}',
|
|
933
|
+
'.pfcmd{font:inherit;font-size:11px;color:inherit;background:transparent;border:1px solid rgba(128,128,128,.38);border-radius:6px;padding:1px 8px;cursor:pointer;opacity:.9;white-space:nowrap}',
|
|
934
|
+
'.pfcmd:hover:not(:disabled){opacity:1;border-color:currentColor}',
|
|
935
|
+
'.pfcmd:disabled{opacity:.35;cursor:default}',
|
|
936
|
+
// 命令结果可能是多行,给一个能滚的框,不再切成一行省略号
|
|
937
|
+
'.pfpop-notice{font-size:11px;opacity:.8;white-space:pre-wrap;max-height:110px;overflow:auto;word-break:break-word}',
|
|
938
|
+
].join('\n')
|
|
939
|
+
|
|
940
|
+
return {
|
|
941
|
+
/*
|
|
942
|
+
* 一个服务都不声明。
|
|
943
|
+
*
|
|
944
|
+
* 原来写的是 `inject: ['slots', 'remote']`。这个写法的代价不对称:
|
|
945
|
+
* 名字只要有一个在这个构建里不存在,Cordis 就会一直等,`apply` 永远
|
|
946
|
+
* 不被调用——不抛错、不进控制台、界面上什么都没有。排查的时候
|
|
947
|
+
* 完全看不出是「等注入」还是「根本没加载」。
|
|
948
|
+
*
|
|
949
|
+
* 所以声明留空,服务在使用那一刻再去取(见下面的 pick)。取不到就
|
|
950
|
+
* 明确说一句,而不是静默停住。
|
|
951
|
+
*/
|
|
952
|
+
inject: [],
|
|
953
|
+
apply(ctx) {
|
|
954
|
+
// 第一件事就回连:apply 只要跑了,宿主立刻知道。
|
|
955
|
+
// 这一枪比什么都重要——它把「没加载」和「加载了但没渲染」彻底分开。
|
|
956
|
+
beacon('apply')
|
|
957
|
+
|
|
958
|
+
// 顺手报一下这个上下文里到底有哪些服务。名字写错是静默故障,
|
|
959
|
+
// 与其继续猜 'slots' / 'remote' 存不存在,不如让它自己说出来。
|
|
960
|
+
const candidates = ['slots', 'remote', 'commands', 'modules', 'sessions', 'locale', 'renderer', 'uiConversation', 'resources']
|
|
961
|
+
const have = candidates.filter(name => pick(ctx, name) !== undefined)
|
|
962
|
+
beacon(`have-${have.join('_') === '' ? 'none' : have.join('_')}`)
|
|
963
|
+
|
|
964
|
+
// 挂载。
|
|
965
|
+
//
|
|
966
|
+
// 探针一律打在动作**之前**。之前都打在之后,结果只要那一步把执行吞掉,
|
|
967
|
+
// 日志里就什么都看不见——「没走到」和「走到了但没回来」分不出来。
|
|
968
|
+
// 现在的规则:先报「我要做这一步」,再做。最后一笔是什么,就卡在哪一步。
|
|
969
|
+
const definitionOf = (name, index, files, commands) => ({
|
|
970
|
+
name,
|
|
971
|
+
id: `project-flow-bar-${index}`,
|
|
972
|
+
order: 31 + index,
|
|
973
|
+
inject: sessionId => ({
|
|
974
|
+
sessionId,
|
|
975
|
+
files,
|
|
976
|
+
runCommand: commands === undefined
|
|
977
|
+
? undefined
|
|
978
|
+
: async line => {
|
|
979
|
+
let result
|
|
980
|
+
try {
|
|
981
|
+
result = await commands.execute(sessionId, line, [])
|
|
982
|
+
} catch (error) {
|
|
983
|
+
// 真的调到宿主了,但那一趟抛了(handler 抛错、或被取消)。
|
|
984
|
+
return { ok: false, error: `命令执行失败:${error?.message ?? error}` }
|
|
985
|
+
}
|
|
986
|
+
if (result === undefined) {
|
|
987
|
+
// 语法没解析出来,或者这个会话里没有这条命令——这两种宿主都返回 undefined。
|
|
988
|
+
return { ok: false, error: `没有这个命令:${line}` }
|
|
989
|
+
}
|
|
990
|
+
const value = unwrapCommandValue(result)
|
|
991
|
+
if (value === undefined) {
|
|
992
|
+
// 认不出形状时把实际的键报出来,别再让人对着「未知原因」猜。
|
|
993
|
+
let shape = ''
|
|
994
|
+
try {
|
|
995
|
+
shape = Object.keys(result).join(',')
|
|
996
|
+
} catch {
|
|
997
|
+
shape = typeof result
|
|
998
|
+
}
|
|
999
|
+
return { ok: false, error: `命令返回的形状认不出来(${shape}):${line}` }
|
|
1000
|
+
}
|
|
1001
|
+
return {
|
|
1002
|
+
ok: value.kind === 'success',
|
|
1003
|
+
error: value.kind === 'success' ? undefined : value.text,
|
|
1004
|
+
text: value.text,
|
|
1005
|
+
}
|
|
1006
|
+
},
|
|
1007
|
+
}),
|
|
1008
|
+
})
|
|
1009
|
+
|
|
1010
|
+
const register = slots => {
|
|
1011
|
+
// 服务上的属性访问会抛。
|
|
1012
|
+
//
|
|
1013
|
+
// Cordis 对没声明的服务做的是「访问即抛」,不是返回 undefined。
|
|
1014
|
+
// 而 `remote.workspaceFiles` / `remote.commands` 是 remote 服务上的
|
|
1015
|
+
// 子命名空间,取它们本身就受同一套限制。之前我把这两句写成普通读取,
|
|
1016
|
+
// 于是每一次都死在这里:apply 跑了、have 有了,然后什么痕迹都没有。
|
|
1017
|
+
const safe = fn => {
|
|
1018
|
+
try {
|
|
1019
|
+
return fn()
|
|
1020
|
+
} catch {
|
|
1021
|
+
return undefined
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
const remote = safe(() => pick(ctx, 'remote'))
|
|
1025
|
+
// `remote.workspaceFiles` 是一个服务名,不是 `remote` 上的普通属性。
|
|
1026
|
+
// inject 为空的时候「访问属性」会被 Cordis 拒绝(访问即抛),
|
|
1027
|
+
// 但 `ctx.get('remote.workspaceFiles')` 能直接拿到它。
|
|
1028
|
+
// 之前只走属性那条路,于是 files 永远是 undefined,面板永远显示「未启用」——
|
|
1029
|
+
// 明明目录是对的、项目也在跑。
|
|
1030
|
+
const files = pick(ctx, 'remote.workspaceFiles') ?? safe(() => remote?.workspaceFiles)
|
|
1031
|
+
const commands = pick(ctx, 'remote.commands') ?? safe(() => remote?.commands)
|
|
1032
|
+
beacon(`svc-${files === undefined ? 'nofiles' : 'files'}-${commands === undefined ? 'nocmd' : 'cmd'}`)
|
|
1033
|
+
beacon(`register-is-${typeof slots?.register}`)
|
|
1034
|
+
let ok = 0
|
|
1035
|
+
|
|
1036
|
+
for (const [index, name] of SLOT_CANDIDATES.entries()) {
|
|
1037
|
+
// 先直接 register。官方插件都套一层 slots.inject(name, () => register(...)),
|
|
1038
|
+
// 但 inject 是「等这个槽位声明出来」的机制,槽位本来就在时它不是必需的,
|
|
1039
|
+
// 而它也是最可疑的一步(它之前的日志有、之后的没有)。
|
|
1040
|
+
beacon(`try-direct-${index}`)
|
|
1041
|
+
try {
|
|
1042
|
+
slots.register(definitionOf(name, index, files, commands), FlowChip)
|
|
1043
|
+
ok += 1
|
|
1044
|
+
beacon(`direct-ok-${index}`)
|
|
1045
|
+
continue
|
|
1046
|
+
} catch (error) {
|
|
1047
|
+
beacon(`direct-fail-${index}`)
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
beacon(`try-inject-${index}`)
|
|
1051
|
+
try {
|
|
1052
|
+
slots.inject(name, () => slots.register(definitionOf(name, index, files, commands), FlowChip))
|
|
1053
|
+
ok += 1
|
|
1054
|
+
beacon(`inject-ok-${index}`)
|
|
1055
|
+
} catch (error) {
|
|
1056
|
+
beacon(`inject-fail-${index}`)
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
beacon(`done-${ok}`)
|
|
1061
|
+
console.log(`[project-flow] 状态栏挂了 ${ok}/${SLOT_CANDIDATES.length} 个槽位`)
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
const slots = pick(ctx, 'slots')
|
|
1065
|
+
if (slots === undefined) {
|
|
1066
|
+
beacon('no-slots')
|
|
1067
|
+
return
|
|
1068
|
+
}
|
|
1069
|
+
beacon('have-slots-object')
|
|
1070
|
+
try {
|
|
1071
|
+
register(slots)
|
|
1072
|
+
} catch (error) {
|
|
1073
|
+
// 兜底:不管哪一步抛了,都要留下一句话,不能再一次无声中断
|
|
1074
|
+
beacon(`register-threw-${String(error?.message ?? error).slice(0, 60).replace(/[^\w\u4e00-\u9fa5]/gu, '_')}`)
|
|
1075
|
+
}
|
|
1076
|
+
},
|
|
1077
|
+
}
|
|
1078
|
+
},
|
|
1079
|
+
})
|