dsh-project-flow 0.9.3 → 0.10.2
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 +225 -12
- package/README.md +25 -0
- package/checks/assertion.mjs +104 -0
- package/checks/command.mjs +416 -0
- package/checks/requirement.mjs +91 -0
- package/commands/slash.mjs +754 -0
- package/commands/steer.mjs +43 -0
- package/config/settings.mjs +217 -0
- package/docs/FEATURES.md +9 -6
- package/docs/anchors/file-whitelist.md +1 -1
- package/http/routes.mjs +252 -0
- package/index.js +173 -8612
- package/issues/issue.mjs +322 -0
- package/package.json +16 -1
- package/persistence/lock.mjs +67 -0
- package/persistence/log.mjs +43 -0
- package/persistence/recovery.mjs +118 -0
- package/persistence/state.mjs +202 -0
- package/pricing/estimate.mjs +72 -0
- package/prompts/rules.mjs +192 -0
- package/rollback/ledger.mjs +237 -0
- package/runner/pipeline.mjs +1190 -0
- package/runner/stage.mjs +370 -0
- package/runner/tools/context.mjs +64 -0
- package/runner/tools/decorate-cost.mjs +49 -0
- package/runner/tools/doctor.mjs +59 -0
- package/runner/tools/estimate.mjs +63 -0
- package/runner/tools/execute-run.mjs +255 -0
- package/runner/tools/models.mjs +55 -0
- package/runner/tools/resume.mjs +142 -0
- package/runner/tools/rollback.mjs +291 -0
- package/runner/tools/run.mjs +271 -0
- package/runner/tools/set.mjs +65 -0
- package/runner/tools/snapshots.mjs +83 -0
- package/runner/tools/status.mjs +70 -0
- package/runner/tools.mjs +94 -0
- package/shared/constants.mjs +273 -0
- package/shared/tool.mjs +32 -0
- package/shared/util.mjs +231 -0
- package/snapshot/repo.mjs +452 -0
- package/state/registry.mjs +40 -0
- package/tests/aesthetic.test.mjs +20 -12
- package/tests/apply-wiring.test.mjs +172 -0
- package/tests/assertions-e2e.mjs +2 -1
- package/tests/atomic-write.test.mjs +129 -0
- package/tests/client-bundle.test.mjs +3 -3
- package/tests/command-whitelist.test.mjs +135 -0
- package/tests/contract.test.mjs +2 -2
- package/tests/desktop.test.mjs +2 -2
- package/tests/domains.test.mjs +10 -7
- package/tests/e2e.mjs +2 -1
- package/tests/extra-phase.test.mjs +2 -2
- package/tests/feedback.test.mjs +6 -1
- package/tests/flow-gate.test.mjs +16 -56
- package/tests/issues-e2e.mjs +2 -1
- package/tests/panel-independence.test.mjs +2 -2
- package/tests/panel-report.test.mjs +2 -2
- package/tests/paths.mjs +70 -1
- package/tests/preview-path.test.mjs +2 -1
- package/tests/qa-assertions.test.mjs +10 -3
- package/tests/run-all.mjs +5 -0
- package/tests/snapshot-core.test.mjs +234 -0
- package/tests/stage-model.test.mjs +2 -2
- package/tests/stop-resume.test.mjs +10 -2
- package/tests/watch-root.test.mjs +33 -2
- package/ui/prompt.mjs +101 -0
- package/workspace/models.mjs +111 -0
- package/workspace/panel.mjs +370 -0
- package/workspace/paths.mjs +393 -0
- package/workspace/resume.mjs +107 -0
package/ui/prompt.mjs
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 注入给模型的那段系统提示:四阶是什么、门控怎么走、按钮是什么。
|
|
3
|
+
*/
|
|
4
|
+
import { COPYRIGHT, FLOW_DIR_NAME, INTENT_CONFIDENCE_FLOOR } from '../shared/constants.mjs'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 返回卸载函数,交给调用方挂到 ctx.effect 上。
|
|
8
|
+
* 整段就是一条 ctx.inject:宿主有那个服务时才跑,服务晚到也能补上。
|
|
9
|
+
*/
|
|
10
|
+
export function createPrompt(ctx) {
|
|
11
|
+
return ctx.systemPrompt.section({
|
|
12
|
+
name: 'project-flow:policy',
|
|
13
|
+
// A renamed SECTION_ORDERS key would make this NaN and throw inside
|
|
14
|
+
// section(), taking the whole plugin down; fall back to the known value.
|
|
15
|
+
order: (ctx.systemPrompt.getSectionOrder('TOOL_SUBAGENT') ?? 2800) + 1,
|
|
16
|
+
text: () => `## 工作模式(项目流程)
|
|
17
|
+
|
|
18
|
+
**用户的命令永远排在第一位。** 这套流程只约束「怎么把活干完」,不约束「干什么」。
|
|
19
|
+
用户说停就停、说改就改、说换模型就换模型,不需要先走完手里的阶段,也不需要用流程
|
|
20
|
+
本身去论证该不该听。流程和用户指令冲突时,永远按用户的。唯一例外是用户让你做的事
|
|
21
|
+
会直接弄坏他自己的东西、而你还没说清后果——那就先说清,然后照做。
|
|
22
|
+
|
|
23
|
+
**用户说「继续」,就是在给你下一个命令,不是在征求你的意见。** 这时先看
|
|
24
|
+
\`.project-flow/\` 里有没有停住的项目(\`state.json\` 的 \`status\` 是 \`stopped\` 或
|
|
25
|
+
带 \`interruptedAt\`、且 \`stage\` 没到 4)。有就直接调 \`project_resume\`,项目名从
|
|
26
|
+
目录名取;有多个就挑 \`interruptedAt\` 最新的那个。不要重新 \`project_run\`,
|
|
27
|
+
不要重问需求,不要把已经跑过的段重做一遍。
|
|
28
|
+
|
|
29
|
+
**工作模式只在「${FLOW_DIR_NAME}」目录里开。** 这是给用户的一道分界线,不是权限墙:
|
|
30
|
+
重要的、值得留档、要复盘的任务,把工作目录切到那里再开工;不重要的直接做,不用走这套。
|
|
31
|
+
不在那个目录里时 \`project_run\` 会直接拒绝并告诉用户该去哪,你不要绕过、不要换个目录重试、
|
|
32
|
+
也不要自己手动模拟四阶流程——照直说这一句就行:「这个放普通任务里做就行,要留档的话挪到
|
|
33
|
+
${FLOW_DIR_NAME} 目录」。
|
|
34
|
+
|
|
35
|
+
遇到需要产出或改动的任务,先问用户一句「这次开工作模式吗」。开了就按四阶走,没开就正常做。
|
|
36
|
+
|
|
37
|
+
**四阶隔离,靠文件串联,上下文不进内存。** 每一阶开一个独立子会话,跑完即销毁,
|
|
38
|
+
它只拿到上一阶落盘的文件。所以文件写得好不好,直接决定下一阶跑不跑偏。
|
|
39
|
+
|
|
40
|
+
| 阶 | 谁在跑 | 读 | 写 |
|
|
41
|
+
|---|---|---|---|
|
|
42
|
+
| 1 需求 | 主对话(子会话问不了用户) | — | \`Requirement.md\` |
|
|
43
|
+
| 2 规划 | 子会话 A | \`Requirement.md\` | \`Plan.md\` |
|
|
44
|
+
| 3 实施 | 子会话 B | \`Plan.md\`(返工时加 \`QA_Report.md\`) | \`Execution_Log.md\` + 代码 |
|
|
45
|
+
| 4 质检 | 子会话 C | \`Requirement.md\` \`Plan.md\` \`Execution_Log.md\`,外加一份从快照仓库算出来的 \`git diff\` | \`QA_Report.md\` |
|
|
46
|
+
|
|
47
|
+
**第 4 阶只输出 PASS 或 FAIL,不写建议、不写「基本通过」。** 判 FAIL 时 \`reasons\` 就是下一轮的修改清单,
|
|
48
|
+
带着 \`QA_Report.md\` 回第 3 阶重跑,最多返工 \`maxRework\` 次,超了停机交给你。
|
|
49
|
+
|
|
50
|
+
**第 1 阶不是走过场,它是硬门槛。** \`project_run\` 要求你交 \`intentConfidence\`(0-100)和 \`questionsAsked\`,低于 ${INTENT_CONFIDENCE_FLOOR} 分或一个问题都没问过会直接报错、不执行。所以:
|
|
51
|
+
- 用 \`ask_user_question\` **一次只问一个**,把真问题问出来,别一次甩一串。
|
|
52
|
+
- 每答一次就重新评估把握。到了 ${INTENT_CONFIDENCE_FLOOR} 分就停手开工,别凑问题、别为了流程而问。
|
|
53
|
+
- 用户说「就按这个做、别再问了」→ 传 \`override: true\` 并在 \`overrideReason\` 里写他的原话。
|
|
54
|
+
- 你没问但按默认定了的地方,全写进 \`assumptions\`,质检时要逐条核。
|
|
55
|
+
|
|
56
|
+
**推进前必须交《需求完备度自查表》,这不是走形式,是门控。** 输出一段 JSON:
|
|
57
|
+
|
|
58
|
+
\`\`\`json
|
|
59
|
+
{
|
|
60
|
+
"score": 96,
|
|
61
|
+
"blindspots": {
|
|
62
|
+
"permissions": "谁来做、要不要授权、凭证归谁——写出实际结论",
|
|
63
|
+
"concurrency": "同时几个人用、会不会撞车、量级多大——写出实际结论",
|
|
64
|
+
"edge_cases": "出错怎么办、空数据、超时、重试、能不能回滚——写出实际结论",
|
|
65
|
+
"stack": "用什么语言框架、跑在哪、能不能加依赖——写出实际结论",
|
|
66
|
+
"data_flow": "数据从哪来到哪去、存哪儿、谁能读——写出实际结论"
|
|
67
|
+
},
|
|
68
|
+
"assumptions": ["没问、按默认值定了的地方,写清验收时怎么核"]
|
|
69
|
+
}
|
|
70
|
+
\`\`\`
|
|
71
|
+
|
|
72
|
+
然后调 \`project_run\`,把 \`intentConfidence\` 填成上面的 score,\`blindspots\` 和 \`assumptions\` 原样传上去。
|
|
73
|
+
|
|
74
|
+
判定规则(代码层执行,不是建议):
|
|
75
|
+
- **两个条件同时满足才放行**:score 达到 ${INTENT_CONFIDENCE_FLOOR} 且五个盲区全部有结论。
|
|
76
|
+
- 分数够但漏一个盲区 → 拦。**分数是自评,改个数字零成本;清单要写具体结论,编不出来。两样都要。**
|
|
77
|
+
- 盲区全填但分数不够 → 也拦。
|
|
78
|
+
- 某个维度确实问不出来,就把它写进 \`assumptions\` 并说清验收时怎么核——但别把盲区留空当作「默认没问题」。
|
|
79
|
+
- 写「已确认」三个字骗不过检验:第 4 阶质检会拿这些结论逐条核。
|
|
80
|
+
|
|
81
|
+
**开工前先调 \`project_context\`。** 这个工作流目录上次确认过的五格结论和假设都在那儿。
|
|
82
|
+
能沿用的照原样交上去,只问这次确实变了的那一格——门控要的是「五格都有具体结论」,
|
|
83
|
+
不是「每一格都要重新问一遍」。同一件事问用户两遍,比问不清楚更让人烦。
|
|
84
|
+
|
|
85
|
+
已开启时:
|
|
86
|
+
|
|
87
|
+
1. 第 1 阶在主对话完成:一次只问一个问题,问出真实意图,并和用户确认一份逐条可核对的「要求清单」。不确认完不进入第 2 段。
|
|
88
|
+
2. 第 2 至 4 段调用 \`project_run\` 一次完成:规划、实施、质检都在独立子会话里跑,彼此只传任务书,不传过程。你不参与其中,也不要把中间产物搬进对话。这三段各自用哪个模型由插件配置决定;用户问起时用 \`project_models\` 查,不要凭印象回答。
|
|
89
|
+
3. \`project_run\` 返回 \`passed\` 就结束了,没有第 5 段。给用户的汇报你自己写:说清哪几条做到了、哪几条没做到、返工几次、残留什么风险。不要只说好话,也别把 \`QA_Report.md\` 的 PASS 当成「用户一定满意」。
|
|
90
|
+
4. \`project_run\` 返回 \`stopped\` 或 \`blocked\` 时应停手把情况交给用户,不要自己加循环重跑。查进度用 \`project_status\`。
|
|
91
|
+
5. 第 1 阶和用户对话时,如果用户指定的意图模型不是当前主对话模型,提醒他先在界面顶部切换——这一段插件切不了。
|
|
92
|
+
6. 插件自带自愈:单段异常会自动重试并带超时;进程崩了、插件重载、窗口被杀之后,用 \`project_resume\` 按磁盘产物从中断那段接着跑,不要从头重来。怀疑工作区有问题(遗留锁、state.json 损坏、悬空阶段)时先跑 \`project_doctor\`,默认只报告,\`fix: true\` 才动手。
|
|
93
|
+
7. 每次实施前后会自动打快照。用户说「改废了」「回滚」「退回刚才的样子」时,先用 \`project_snapshots\` 列出目标,再用 \`project_rollback\` 干跑给 diff,用户确认后才 \`dryRun: false\` 真回滚。\`mode: "full"\` 整棵恢复、\`mode: "precise"\` 只还原那一轮动过的文件;说不清就用 full。
|
|
94
|
+
|
|
95
|
+
**输入框上方那条状态栏**显示阶段灯、各阶模型和停止/继续两个按钮。它和你是两条独立的路:用户点停止,这一轮立刻停在原地,返回 \`outcome: "stopped"\` 而不是报错;用户点继续或说「继续」,按上面说的调 \`project_resume\`。用户点的按钮不需要你复述或解释,也不要劝他改成别的方式。
|
|
96
|
+
|
|
97
|
+
子 agent 问不了用户,所以只有你能问;把答案压成要求清单再交给流程。
|
|
98
|
+
(${COPYRIGHT} · dsh-project-flow · PolyForm Small Business 1.0.0,小公司和个人可直接商用)`,
|
|
99
|
+
})
|
|
100
|
+
}
|
|
101
|
+
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 本机模型目录:装了哪些 provider、每个下面有哪些模型。只有 LLM 注册表能回答。
|
|
3
|
+
*/
|
|
4
|
+
import { resolvePriceTable } from '../watchdog.mjs'
|
|
5
|
+
import { messageOf } from '../shared/util.mjs'
|
|
6
|
+
import { pfState } from '../state/registry.mjs'
|
|
7
|
+
|
|
8
|
+
export function createModels(ctx) {
|
|
9
|
+
|
|
10
|
+
let clientModules
|
|
11
|
+
ctx.inject(['clientModules'], modulesCtx => {
|
|
12
|
+
pfState.clientModules = modulesCtx.get('clientModules')
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
// 浏览器那半边到底跑到哪一步了,从这边看不见。所以让它反过来打这个宿主路由:
|
|
16
|
+
// apply 跑起来打一次(stage=apply),第一次渲染打一次(stage=render)。
|
|
17
|
+
// 宿主记一笔,/flow-diag 读出来——不用开控制台就能分辨
|
|
18
|
+
// 「bundle 没物化」和「物化了但没渲染」这两件完全不同的事。
|
|
19
|
+
const clientHits = []
|
|
20
|
+
// LLM 注册表:本机装了哪些 provider、每个 provider 下有哪些模型,只有它能回答。
|
|
21
|
+
// 之前客户端把三个模型名写死在代码里——别人的 dsh 里可能一个都没有。
|
|
22
|
+
let llm
|
|
23
|
+
ctx.inject(['llm'], llmCtx => {
|
|
24
|
+
pfState.llm = llmCtx.get('llm')
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 活着的会话表:用它把一个 sessionId 反查成「这个会话在哪个目录」。
|
|
29
|
+
*
|
|
30
|
+
* 必须走 ctx.inject 拿到,不能直接 ctx.get('agents')——Cordis 对没在 inject
|
|
31
|
+
* 里声明的服务是「访问即抛」,外面再包 try/catch 也只会得到 undefined。
|
|
32
|
+
* 之前就是栽在这儿:状态栏只带着 sid 来问目录,宿主这边 quietly 拿不到 agents,
|
|
33
|
+
* 于是每次都回 empty,状态栏整块空着。
|
|
34
|
+
*/
|
|
35
|
+
let agents
|
|
36
|
+
ctx.inject(['agents'], agentsCtx => {
|
|
37
|
+
pfState.agents = agentsCtx.get('agents')
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
const listModelCatalog = async () => {
|
|
41
|
+
if (pfState.llm === undefined) return { providers: [], error: 'llm 服务取不到' }
|
|
42
|
+
let providers = []
|
|
43
|
+
try {
|
|
44
|
+
providers = pfState.llm.listProviders() ?? []
|
|
45
|
+
} catch (error) {
|
|
46
|
+
return { providers: [], error: `listProviders 失败:${messageOf(error)}` }
|
|
47
|
+
}
|
|
48
|
+
const groups = []
|
|
49
|
+
for (const provider of providers) {
|
|
50
|
+
try {
|
|
51
|
+
const models = await pfState.llm.listModels(provider.id)
|
|
52
|
+
groups.push({
|
|
53
|
+
id: provider.id,
|
|
54
|
+
name: typeof provider.name === 'string' ? provider.name : provider.id,
|
|
55
|
+
models: (models ?? []).map(model => ({
|
|
56
|
+
id: model.id,
|
|
57
|
+
name: typeof model.name === 'string' && model.name !== '' ? model.name : model.id,
|
|
58
|
+
})),
|
|
59
|
+
})
|
|
60
|
+
} catch {
|
|
61
|
+
// 单个 provider 挂了不该连累整张表,但要让调用方知道它存在过
|
|
62
|
+
groups.push({ id: provider.id, name: provider.id, models: [], failed: true })
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return { providers: groups.filter(group => group.models.length > 0 || group.failed === true) }
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const unknownModelMessage = async model => {
|
|
69
|
+
if (typeof model !== 'string' || model.trim() === '') return undefined
|
|
70
|
+
const wanted = model.trim()
|
|
71
|
+
const catalog = await listModelCatalog()
|
|
72
|
+
const known = []
|
|
73
|
+
for (const group of catalog.providers ?? []) {
|
|
74
|
+
for (const item of group.models ?? []) {
|
|
75
|
+
if (typeof item.id !== 'string' || item.id === '') continue
|
|
76
|
+
known.push(item.id, `${group.id}/${item.id}`, `${group.id} ${item.id}`)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (known.length === 0) return undefined
|
|
80
|
+
const lower = wanted.toLowerCase()
|
|
81
|
+
if (known.some(id => id.toLowerCase() === lower)) return undefined
|
|
82
|
+
const unique = [...new Set(known)]
|
|
83
|
+
return `模型「${wanted}」不在本机可用列表里,没有写入。可用的有:\n${unique.map(id => ` ${id}`).join('\n')}`
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const refreshDetectedModels = async () => {
|
|
87
|
+
try {
|
|
88
|
+
const catalog = await listModelCatalog()
|
|
89
|
+
const ids = []
|
|
90
|
+
const seen = new Set()
|
|
91
|
+
for (const group of catalog.providers ?? []) {
|
|
92
|
+
for (const model of group.models ?? []) {
|
|
93
|
+
if (typeof model?.id !== 'string' || model.id === '' || seen.has(model.id)) continue
|
|
94
|
+
seen.add(model.id)
|
|
95
|
+
ids.push(model.id)
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
// 一个都没读到时别把上一次的好数据冲掉
|
|
99
|
+
if (ids.length > 0) pfState.detectedModelIds = ids
|
|
100
|
+
return { ids, error: catalog.error }
|
|
101
|
+
} catch (error) {
|
|
102
|
+
return { ids: pfState.detectedModelIds, error: messageOf(error) }
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const priceTableNow = settings => resolvePriceTable(settings.priceTable, pfState.detectedModelIds)
|
|
107
|
+
|
|
108
|
+
return { listModelCatalog, unknownModelMessage, refreshDetectedModels, priceTableNow }
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
|
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 状态栏数据:一行摘要、整张表、面板缓存、预览图。
|
|
3
|
+
*/
|
|
4
|
+
import { readdir, stat, readFile, mkdir } from 'node:fs/promises'
|
|
5
|
+
import { join } from 'node:path'
|
|
6
|
+
import { PREVIEW_DIR } from '../aesthetic.mjs'
|
|
7
|
+
import { parseAssertionBlock, parseIssueBlock } from '../issues/issue.mjs'
|
|
8
|
+
import { readArtifact, readState } from '../persistence/state.mjs'
|
|
9
|
+
import { ARTIFACT, LIST_MAX_DEPTH, LIST_MAX_ENTRIES, LIST_SKIP_DIRS, MEDIA_TYPES, STAGE_LABEL } from '../shared/constants.mjs'
|
|
10
|
+
import { PKG_VERSION, dirExists, nowIso, stateDir, writeFileAtomic } from '../shared/util.mjs'
|
|
11
|
+
import { flowModeOf, flowRootConfigured, flowRootOf, flowRootTargetOf, focusOf, insideFlow, isScratchPath, watchRootOf } from './paths.mjs'
|
|
12
|
+
import { pfState } from '../state/registry.mjs'
|
|
13
|
+
|
|
14
|
+
export function statusLine(state) {
|
|
15
|
+
if (state === undefined) return '没有这个项目'
|
|
16
|
+
const stage = state.stage === undefined ? '?' : state.stage
|
|
17
|
+
if (stage === 0) return `${state.project}:还没开跑 —— ${state.status ?? '未开始'}`
|
|
18
|
+
const label = STAGE_LABEL[stage] ?? '?'
|
|
19
|
+
const rework = state.rework === undefined || state.rework === 0 ? '' : `,已返工 ${state.rework} 次`
|
|
20
|
+
const line = `${state.project}:第 ${stage} 段「${label}」 · ${state.status}${rework}`
|
|
21
|
+
const tail = []
|
|
22
|
+
if (typeof state.interruptedAt === 'string' && state.interruptedAt !== '') {
|
|
23
|
+
tail.push(`上次中断于 ${state.interruptedAt},跑 project_resume 续上`)
|
|
24
|
+
}
|
|
25
|
+
if (typeof state.lastError === 'string' && state.lastError !== '') {
|
|
26
|
+
tail.push(`最后错误:${state.lastError.slice(0, 200)}`)
|
|
27
|
+
}
|
|
28
|
+
return tail.length === 0 ? line : `${line}\n └ ${tail.join(';')}`
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export async function listProjectLines(cwd) {
|
|
32
|
+
let names = []
|
|
33
|
+
try {
|
|
34
|
+
const entries = await readdir(join(cwd, '.project-flow'), { withFileTypes: true })
|
|
35
|
+
names = entries.filter(entry => entry.isDirectory()).map(entry => entry.name)
|
|
36
|
+
} catch {
|
|
37
|
+
return []
|
|
38
|
+
}
|
|
39
|
+
const lines = []
|
|
40
|
+
for (const project of names.sort()) {
|
|
41
|
+
const state = await readState(cwd, project)
|
|
42
|
+
lines.push(state === undefined ? `${project}:state.json 缺失,可用 project_doctor 修` : statusLine(state))
|
|
43
|
+
}
|
|
44
|
+
return lines
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export async function listFiles(dir, limit = 60) {
|
|
48
|
+
const found = []
|
|
49
|
+
let entries = 0
|
|
50
|
+
let queue = [{ path: '', depth: 0 }]
|
|
51
|
+
try {
|
|
52
|
+
while (queue.length > 0 && found.length < limit * 6 && entries < LIST_MAX_ENTRIES) {
|
|
53
|
+
const next = []
|
|
54
|
+
for (const current of queue) {
|
|
55
|
+
if (entries >= LIST_MAX_ENTRIES) break
|
|
56
|
+
let dirents
|
|
57
|
+
try {
|
|
58
|
+
dirents = await readdir(current.path === '' ? dir : join(dir, current.path), { withFileTypes: true })
|
|
59
|
+
} catch {
|
|
60
|
+
continue
|
|
61
|
+
}
|
|
62
|
+
for (const dirent of dirents) {
|
|
63
|
+
entries += 1
|
|
64
|
+
if (entries > LIST_MAX_ENTRIES) break
|
|
65
|
+
const relative = current.path === '' ? dirent.name : `${current.path}/${dirent.name}`
|
|
66
|
+
if (dirent.isDirectory()) {
|
|
67
|
+
if (LIST_SKIP_DIRS.has(dirent.name)) continue
|
|
68
|
+
if (current.depth + 1 > LIST_MAX_DEPTH) continue
|
|
69
|
+
next.push({ path: relative, depth: current.depth + 1 })
|
|
70
|
+
continue
|
|
71
|
+
}
|
|
72
|
+
if (dirent.isFile()) found.push(relative)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
queue = next
|
|
76
|
+
}
|
|
77
|
+
} catch {
|
|
78
|
+
return found.slice(0, limit)
|
|
79
|
+
}
|
|
80
|
+
return found.slice(0, limit)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export async function previewImageParts(cwd, project, limit = 8, budgetBytes = 6 * 1024 * 1024) {
|
|
84
|
+
// 提示词让子会话导出到「项目目录下的 preview/」,也就是 cwd/preview。
|
|
85
|
+
// 之前这里读的是 stateDir(cwd, project) —— 那是 .project-flow/<项目>/,
|
|
86
|
+
// 里面只有 snap.git 和几个 md,一张图都没有。于是 40 张预览图躺在
|
|
87
|
+
// cwd/preview 里没人看,质检每次都判「没有预览图」然后 FAIL、返工,
|
|
88
|
+
// 白烧几轮。读的地方必须和写的地方是同一个。
|
|
89
|
+
const primary = join(cwd, PREVIEW_DIR)
|
|
90
|
+
const first = await collectPreviewParts(primary, limit, budgetBytes)
|
|
91
|
+
if (first.parts.length > 0) return first
|
|
92
|
+
// 有任务会把图放进状态目录,两种都认,先认约定的那个
|
|
93
|
+
const fallback = await collectPreviewParts(join(stateDir(cwd, project), PREVIEW_DIR), limit, budgetBytes)
|
|
94
|
+
return fallback.parts.length > 0 ? fallback : first
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export async function collectPreviewParts(dir, limit = 8, budgetBytes = 6 * 1024 * 1024) {
|
|
98
|
+
let names = []
|
|
99
|
+
try {
|
|
100
|
+
names = (await readdir(dir)).filter(name => MEDIA_TYPES[name.slice(name.lastIndexOf('.')).toLowerCase()] !== undefined)
|
|
101
|
+
} catch {
|
|
102
|
+
return { parts: [], note: '没有预览图' }
|
|
103
|
+
}
|
|
104
|
+
names.sort()
|
|
105
|
+
const parts = []
|
|
106
|
+
let bytes = 0
|
|
107
|
+
let skipped = 0
|
|
108
|
+
for (const name of names) {
|
|
109
|
+
if (parts.length >= limit) {
|
|
110
|
+
skipped += 1
|
|
111
|
+
continue
|
|
112
|
+
}
|
|
113
|
+
const path = join(dir, name)
|
|
114
|
+
try {
|
|
115
|
+
const info = await stat(path)
|
|
116
|
+
if (info.size > budgetBytes - bytes) {
|
|
117
|
+
skipped += 1
|
|
118
|
+
continue
|
|
119
|
+
}
|
|
120
|
+
const data = (await readFile(path)).toString('base64')
|
|
121
|
+
bytes += info.size
|
|
122
|
+
parts.push({
|
|
123
|
+
type: 'image',
|
|
124
|
+
mediaType: MEDIA_TYPES[name.slice(name.lastIndexOf('.')).toLowerCase()],
|
|
125
|
+
data,
|
|
126
|
+
name,
|
|
127
|
+
})
|
|
128
|
+
} catch {
|
|
129
|
+
skipped += 1
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (parts.length === 0) return { parts: [], note: `preview/ 里有 ${names.length} 个文件但一个都读不出来` }
|
|
133
|
+
const size = Math.round(bytes / 1024)
|
|
134
|
+
return {
|
|
135
|
+
parts,
|
|
136
|
+
note: `喂了 ${parts.length} 张预览图(共 ${size}KB)${skipped === 0 ? '' : `,另有 ${skipped} 张因限额跳过`}`,
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export async function writePanelFile(cwd, settings) {
|
|
141
|
+
// 只在工作流目录里落这份:否则在普通目录改一下模型,状态栏就会在那边点亮,
|
|
142
|
+
// 而那个目录其实不接受工作模式。
|
|
143
|
+
if (!insideFlow(cwd, settings)) return undefined
|
|
144
|
+
// 写到「状态栏正在看的那个目录」,不是会话当前目录。
|
|
145
|
+
//
|
|
146
|
+
// 以前是 join(cwd, '.project-flow', 'panel.json'),于是同一个工作流会长出两份
|
|
147
|
+
// 面板数据:在外层壳目录开一次工落在壳里,项目却在它下面的「工作流」子目录里。
|
|
148
|
+
// 状态栏读一份、项目在另一份,界面上就是「项目在跑但状态栏没动」。
|
|
149
|
+
//
|
|
150
|
+
// 这里必须用 watchRootOf 而不是 flowRootTargetOf:读和写要用同一个解析函数,
|
|
151
|
+
// 否则「读哪儿」和「写哪儿」各算一套,错位比原来更难查。两者在配了绝对
|
|
152
|
+
// flowRoot 时结果相同,在没配时 flowRootTargetOf 会算出 cwd/工作流 这个
|
|
153
|
+
// 根本不存在的路径,而 watchRootOf 会正确回到 cwd 或找到祖先里那层「工作流」。
|
|
154
|
+
const root = await watchRootOf(cwd, settings)
|
|
155
|
+
const route = (llmProvider, model) => (model === '' ? '继承主对话' : `${llmProvider}/${model}`)
|
|
156
|
+
const payload = {
|
|
157
|
+
routes: {
|
|
158
|
+
intent: settings.intentModel === '' ? '主对话当前模型' : settings.intentModel,
|
|
159
|
+
plan: route(settings.planProvider === '' ? '默认路由' : settings.planProvider, settings.planModel),
|
|
160
|
+
build: route(settings.buildProvider === '' ? '默认路由' : settings.buildProvider, settings.buildModel),
|
|
161
|
+
check: route(settings.checkProvider === '' ? '默认路由' : settings.checkProvider, settings.checkModel),
|
|
162
|
+
},
|
|
163
|
+
maxRework: settings.maxRework,
|
|
164
|
+
concurrency: settings.maxConcurrentProjects,
|
|
165
|
+
updatedAt: nowIso(),
|
|
166
|
+
}
|
|
167
|
+
try {
|
|
168
|
+
await mkdir(join(root, '.project-flow'), { recursive: true })
|
|
169
|
+
await writeFileAtomic(join(root, '.project-flow', 'panel.json'), `${JSON.stringify(payload, null, 2)}\n`)
|
|
170
|
+
} catch {
|
|
171
|
+
// 写不进去只影响状态栏显示,不该拖累正在跑的东西
|
|
172
|
+
}
|
|
173
|
+
return payload
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export async function panelReportOf(watch, name) {
|
|
177
|
+
let text
|
|
178
|
+
try {
|
|
179
|
+
text = await readArtifact(watch, name, ARTIFACT.qa)
|
|
180
|
+
} catch {
|
|
181
|
+
text = undefined
|
|
182
|
+
}
|
|
183
|
+
const empty = { issues: [], assertions: { total: 0, pass: 0, fail: 0, unverified: 0 }, failed: [] }
|
|
184
|
+
if (typeof text !== 'string' || text.trim() === '') return empty
|
|
185
|
+
|
|
186
|
+
const all = parseIssueBlock(text)
|
|
187
|
+
const assertions = parseAssertionBlock(text)
|
|
188
|
+
const count = status => assertions.filter(item => item.status === status).length
|
|
189
|
+
return {
|
|
190
|
+
issues: all
|
|
191
|
+
.filter(item => item.status === 'open' || item.status === 'reopened')
|
|
192
|
+
.map(item => ({
|
|
193
|
+
id: typeof item.id === 'string' ? item.id : '',
|
|
194
|
+
severity: typeof item.severity === 'string' ? item.severity : '',
|
|
195
|
+
location: typeof item.location === 'string' ? item.location.slice(0, 120) : '',
|
|
196
|
+
claim: typeof item.claim === 'string' ? item.claim.slice(0, 160) : '',
|
|
197
|
+
detect: typeof item.detect === 'string' ? item.detect.slice(0, 160) : '',
|
|
198
|
+
status: item.status,
|
|
199
|
+
})),
|
|
200
|
+
assertions: {
|
|
201
|
+
total: assertions.length,
|
|
202
|
+
pass: count('pass'),
|
|
203
|
+
fail: count('fail'),
|
|
204
|
+
unverified: count('unverified'),
|
|
205
|
+
},
|
|
206
|
+
// 挂掉的命令单独拎出来。面板上要显示「哪一条、退出码多少」——
|
|
207
|
+
// 只说「失败 1 条」等于让人再去翻文件。
|
|
208
|
+
failed: assertions
|
|
209
|
+
.filter(item => item.status === 'fail' || item.status === 'timeout' || item.status === 'denied')
|
|
210
|
+
.map(item => ({
|
|
211
|
+
req: item.req,
|
|
212
|
+
cmd: typeof item.cmd === 'string' ? item.cmd.slice(0, 160) : '',
|
|
213
|
+
exit: Number.isInteger(item.exit) ? item.exit : null,
|
|
214
|
+
status: item.status,
|
|
215
|
+
})),
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export async function panelSnapshot(cwd, settings, onlyProject, sessionId) {
|
|
220
|
+
const watch = await watchRootOf(cwd, settings)
|
|
221
|
+
const inside = insideFlow(cwd, settings)
|
|
222
|
+
// cwd 落在工作流根下面的哪个子目录——那就是「当前这个任务」。
|
|
223
|
+
// 人在 …\工作流\csv2md 里开会话,想看的是 csv2md,不是整张表。
|
|
224
|
+
const focus = focusOf(cwd, watch)
|
|
225
|
+
const route = (llmProvider, model) => (model === '' ? '继承主对话' : `${llmProvider}/${model}`)
|
|
226
|
+
let names = []
|
|
227
|
+
if (onlyProject !== undefined) {
|
|
228
|
+
names = [onlyProject]
|
|
229
|
+
} else {
|
|
230
|
+
try {
|
|
231
|
+
const entries = await readdir(join(watch, '.project-flow'), { withFileTypes: true })
|
|
232
|
+
names = entries.filter(entry => entry.isDirectory()).map(entry => entry.name).sort()
|
|
233
|
+
} catch {
|
|
234
|
+
names = []
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
const projects = []
|
|
238
|
+
for (const name of names) {
|
|
239
|
+
const state = await readState(watch, name)
|
|
240
|
+
if (state === undefined) continue
|
|
241
|
+
const stage = typeof state.stage === 'number' ? state.stage : 0
|
|
242
|
+
const runningEntry = pfState.panelStops.get(name)
|
|
243
|
+
projects.push({
|
|
244
|
+
project: name,
|
|
245
|
+
stage,
|
|
246
|
+
stageLabel: STAGE_LABEL[stage] ?? '?',
|
|
247
|
+
status: typeof state.status === 'string' ? state.status : '',
|
|
248
|
+
rework: typeof state.rework === 'number' ? state.rework : 0,
|
|
249
|
+
maxRework: typeof state.maxRework === 'number' ? state.maxRework : settings.maxRework,
|
|
250
|
+
// 未闭环问题数(P0+P1 的 open+reopened 数):阶段灯旁那个 open: N。
|
|
251
|
+
// 它和 rework 是两件事——一个是「还剩几件事」,一个是「花了几轮」。
|
|
252
|
+
open: typeof state.openCritical === 'number' ? state.openCritical : (typeof state.openIssues === 'number' ? state.openIssues : 0),
|
|
253
|
+
running: runningEntry !== undefined,
|
|
254
|
+
/**
|
|
255
|
+
* 这个项目是不是「你这个会话」开的。
|
|
256
|
+
*
|
|
257
|
+
* 两个项目同时跑的时候,running 会有两条,客户端以前取第一个,
|
|
258
|
+
* 于是两个会话的状态栏显示同一个项目——看起来就像状态栏不独立。
|
|
259
|
+
* 有了 mine,每个会话先认自己开的那个。
|
|
260
|
+
*/
|
|
261
|
+
mine: runningEntry !== undefined
|
|
262
|
+
&& sessionId !== undefined
|
|
263
|
+
&& runningEntry.sessionId === sessionId,
|
|
264
|
+
/**
|
|
265
|
+
* 这个项目是不是「你这个对话」碰过的——跑完之后也算。
|
|
266
|
+
*
|
|
267
|
+
* mine 只覆盖「正在跑」,跑完就假了;状态栏靠这一条才能一个对话一个,
|
|
268
|
+
* 而不是所有对话都退回显示同一个项目。
|
|
269
|
+
*
|
|
270
|
+
* 三种值,缺一不可:
|
|
271
|
+
* true 归我
|
|
272
|
+
* false 不归我(这个项目有归属记录,但里面没有我这个会话)
|
|
273
|
+
* null 未知归属——升级之前跑的老项目没有 ownerSessions,
|
|
274
|
+
* 不能judge成「不是我的」,否则升级当天所有老项目一起消失
|
|
275
|
+
*/
|
|
276
|
+
owned: sessionId !== undefined && Array.isArray(state.ownerSessions)
|
|
277
|
+
? state.ownerSessions.includes(sessionId)
|
|
278
|
+
: null,
|
|
279
|
+
// 已花掉的钱:每阶结束都会落盘,状态栏直接读,不用再算
|
|
280
|
+
cost: state.cost === undefined ? undefined : {
|
|
281
|
+
cny: state.cost.spentCny,
|
|
282
|
+
usd: state.cost.spentUsd,
|
|
283
|
+
},
|
|
284
|
+
interruptedAt: state.interruptedAt ?? null,
|
|
285
|
+
updatedAt: state.updatedAt ?? null,
|
|
286
|
+
// 开工时间。面板要显示「这一轮跑了多久」——只有 updatedAt 算不出时长,
|
|
287
|
+
// 而 state 里本来就有这一项,不用另存。
|
|
288
|
+
startedAt: state.startedAt ?? null,
|
|
289
|
+
verdict: typeof state.verdict === 'string' ? state.verdict : '',
|
|
290
|
+
// 面板要能看到「还差哪几条」「哪条命令没过」。光有一个 open 数字
|
|
291
|
+
// 说不出事,用户还得去翻 QA_Report.md——那正是这个面板想省掉的一步。
|
|
292
|
+
//
|
|
293
|
+
// 读不到就返回空数组,不报错:没跑过质检的项目本来就没有这份报告。
|
|
294
|
+
report: await panelReportOf(watch, name),
|
|
295
|
+
})
|
|
296
|
+
}
|
|
297
|
+
const snapshot = {
|
|
298
|
+
// 面板顶栏那一行要显示插件版本。宿主自己读 package.json,不靠客户端猜。
|
|
299
|
+
version: PKG_VERSION,
|
|
300
|
+
// 算出来的时间。客户端靠它判断这份是不是自己刚触发的那一次——
|
|
301
|
+
// 缓存是全进程共用的,不校验时间就会读到别的栏(工作流/默认工作区)的数据。
|
|
302
|
+
at: nowIso(),
|
|
303
|
+
// root 是「状态栏正在看的那个目录」,不一定等于当前工作目录:
|
|
304
|
+
// 人在别处开会话时,看的仍然是他那个工作流。
|
|
305
|
+
root: watch,
|
|
306
|
+
// 只有「当前目录不能开工、而且看的确实是另一个目录」才算在看别处。
|
|
307
|
+
// 在 …\工作流\csv2md 里开会话时 root 虽然是上一层的根,但那还是自己家,
|
|
308
|
+
// 不能提示「当前目录不在工作流里」;反过来,没线索时看的就是当前目录,
|
|
309
|
+
// 也不算「看别处」。
|
|
310
|
+
watching: inside !== true && watch !== cwd,
|
|
311
|
+
// 当前会话落在工作流里的哪个项目目录下;空表示没对着具体项目
|
|
312
|
+
focus,
|
|
313
|
+
// inside 只回答「能不能在当前目录开工」,和上面那个是两件事。
|
|
314
|
+
inside,
|
|
315
|
+
// 当前会话自己的目录。用户看到状态栏写「在看 C:\…」时要能分辨
|
|
316
|
+
// 那是「我现在在哪儿」,不是「我的工作流在哪」。
|
|
317
|
+
cwd,
|
|
318
|
+
/**
|
|
319
|
+
* 你的工作流到底在哪个目录。
|
|
320
|
+
*
|
|
321
|
+
* 配置里写死的 flowRoot 优先,并且会往下找一层真的装着 .project-flow 的
|
|
322
|
+
* 「工作流」子目录。状态栏拿它来告诉用户「该切到哪儿」,
|
|
323
|
+
* 不再把会话所在的 C 盘目录当成工作流位置——那正是用户报的那个疑问:
|
|
324
|
+
* 「为什么工作流的目录是在 C 盘」。
|
|
325
|
+
*/
|
|
326
|
+
wherePath: flowRootTargetOf(cwd, settings),
|
|
327
|
+
flowRootConfigured: flowRootConfigured(settings),
|
|
328
|
+
gate: flowModeOf(settings),
|
|
329
|
+
// 不在工作流目录、但会自动建的时候,把该建的路径告诉界面。
|
|
330
|
+
// 三个条件缺一不可:不在工作流里、确实在看当前目录(正在看别处时
|
|
331
|
+
// 不该劝人在当前位置再建一个)、而且那个目录真的还不存在——
|
|
332
|
+
// 它已经在那儿了还写「会自动建」就是在骗人。
|
|
333
|
+
willCreate: inside !== true
|
|
334
|
+
&& watch === cwd
|
|
335
|
+
&& settings.autoCreateFlowDir === true
|
|
336
|
+
&& flowModeOf(settings) === 'strict'
|
|
337
|
+
&& !await dirExists(flowRootOf(cwd, settings))
|
|
338
|
+
? flowRootOf(cwd, settings)
|
|
339
|
+
: undefined,
|
|
340
|
+
domain: settings.domain ?? 'auto',
|
|
341
|
+
routes: {
|
|
342
|
+
intent: settings.intentModel === '' ? '主对话当前模型' : settings.intentModel,
|
|
343
|
+
plan: route(settings.planProvider === '' ? '默认路由' : settings.planProvider, settings.planModel),
|
|
344
|
+
build: route(settings.buildProvider === '' ? '默认路由' : settings.buildProvider, settings.buildModel),
|
|
345
|
+
check: route(settings.checkProvider === '' ? '默认路由' : settings.checkProvider, settings.checkModel),
|
|
346
|
+
},
|
|
347
|
+
concurrency: { running: pfState.panelStops.size, limit: settings.maxConcurrentProjects },
|
|
348
|
+
projects,
|
|
349
|
+
}
|
|
350
|
+
pfState.lastPanelReport = {
|
|
351
|
+
at: nowIso(),
|
|
352
|
+
cwd,
|
|
353
|
+
root: watch,
|
|
354
|
+
focus,
|
|
355
|
+
inside,
|
|
356
|
+
watching: watch !== cwd,
|
|
357
|
+
gate: flowModeOf(settings),
|
|
358
|
+
willCreate: snapshot.willCreate ?? null,
|
|
359
|
+
projects: projects.map(item => `${item.project}:第${item.stage}阶 ${item.status}`),
|
|
360
|
+
}
|
|
361
|
+
// 临时目录里算出来的不算数:测试、e2e 全在那儿跑。
|
|
362
|
+
// 让它覆盖缓存的话,真实会话会读到一份「没有项目的就绪」——
|
|
363
|
+
// 这个坑刚踩过:跑完测试,每个会话的状态栏都变成「就绪」。
|
|
364
|
+
if (!isScratchPath(cwd)) {
|
|
365
|
+
pfState.lastPanelByRoot.set(watch, snapshot)
|
|
366
|
+
pfState.lastPanelFull = snapshot
|
|
367
|
+
}
|
|
368
|
+
return snapshot
|
|
369
|
+
}
|
|
370
|
+
|