skill-evolution 0.0.0-stage → 0.3.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 +52 -0
- package/LICENSE +21 -0
- package/README.en.md +208 -0
- package/README.md +207 -2
- package/assets/icon-180.png +0 -0
- package/assets/screenshots/workbench.png +0 -0
- package/client.js +2135 -0
- package/cordis.patch.yml +41 -0
- package/icon.svg +6 -0
- package/index.js +884 -0
- package/lib/applier.js +221 -0
- package/lib/collector.js +356 -0
- package/lib/consolidation.js +344 -0
- package/lib/gatekeeper.js +293 -0
- package/lib/http-api.js +919 -0
- package/lib/ledger.js +316 -0
- package/lib/prompts.js +514 -0
- package/lib/provenance.js +103 -0
- package/lib/reflector.js +226 -0
- package/lib/synthesizer.js +295 -0
- package/lib/tidy.js +263 -0
- package/lib/usage.js +223 -0
- package/locale/en.json +8 -0
- package/locale/zh.json +8 -0
- package/package.json +87 -4
- package/screenshots.json +3 -0
package/lib/http-api.js
ADDED
|
@@ -0,0 +1,919 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP API —— 给 UI 提供真实数据
|
|
3
|
+
*
|
|
4
|
+
* 路由前缀 `/dsh-evolution/api/`:
|
|
5
|
+
* GET status 整体状态(技能数、草稿数含全文与中文简报、账本数含 before/after SHA、lastReview、throttle、配置)
|
|
6
|
+
* GET skills 技能列表(含生命周期状态、使用次数、是否 agent 创建、是否 adopted)
|
|
7
|
+
* GET drafts 待审阅草稿(含完整 SKILL.md 内容与中文简报 briefing)
|
|
8
|
+
* GET ledger 账本记录(分页)
|
|
9
|
+
* GET ledger/:id 单条记录详情(含文件清单)
|
|
10
|
+
* GET skill/:name 单个正式技能详情(含完整 SKILL.md 正文与 resources)
|
|
11
|
+
* GET candidates 可参与建伞合并的候选技能
|
|
12
|
+
* POST refine-draft 根据用户的强制批示(comment),让 AI 分析并修订草稿与中文简报
|
|
13
|
+
* POST promote 带用户批示提升草稿 → 正式技能目录(支持先融合批示再落盘,并清理 .staging 草稿)
|
|
14
|
+
* POST discard 带用户批示丢弃草稿(记入账本防止重复踩坑)
|
|
15
|
+
* POST rollback 回滚某条账本记录
|
|
16
|
+
* POST pin 固定 / 取消固定技能
|
|
17
|
+
* POST adopt 纳管无 provenance 标记的技能
|
|
18
|
+
* POST disown 取消纳管技能
|
|
19
|
+
* POST consolidate 执行 LLM 建伞合并(dryRun: true 预览计划 / dryRun: false 实际执行)
|
|
20
|
+
* POST review 手动触发一次复盘(对当前会话)
|
|
21
|
+
* POST config 保存配置
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import fs from "node:fs/promises";
|
|
25
|
+
import http from "node:http";
|
|
26
|
+
import path from "node:path";
|
|
27
|
+
|
|
28
|
+
import { readEntries, rollbackEntry, appendEntry, captureSnapshot } from "./ledger.js";
|
|
29
|
+
import { listSkills, listDrafts, promoteDraft, STAGING_DIRNAME } from "./applier.js";
|
|
30
|
+
import { loadUsage, entryOf, setUsageFlag, PROTECTED_SKILLS, adoptSkill, disownSkill, listUnmanaged } from "./usage.js";
|
|
31
|
+
import { runConsolidation, listConsolidationCandidates } from "./consolidation.js";
|
|
32
|
+
import { planTidy, applyTidy } from "./tidy.js";
|
|
33
|
+
import { parseSkillMarkdown, renderSkillMarkdown } from "./gatekeeper.js";
|
|
34
|
+
import { extractJsonBlock } from "./synthesizer.js";
|
|
35
|
+
|
|
36
|
+
function sendJson(res, status, payload) {
|
|
37
|
+
const body = JSON.stringify(payload);
|
|
38
|
+
res.writeHead(status, {
|
|
39
|
+
"content-type": "application/json; charset=utf-8",
|
|
40
|
+
"cache-control": "no-store",
|
|
41
|
+
"content-length": Buffer.byteLength(body),
|
|
42
|
+
});
|
|
43
|
+
res.end(body);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async function readJsonBody(req, limitBytes = 512 * 1024) {
|
|
47
|
+
const chunks = [];
|
|
48
|
+
let total = 0;
|
|
49
|
+
for await (const chunk of req) {
|
|
50
|
+
total += chunk.length;
|
|
51
|
+
if (total > limitBytes) throw new Error("request body too large");
|
|
52
|
+
chunks.push(chunk);
|
|
53
|
+
}
|
|
54
|
+
if (!chunks.length) return {};
|
|
55
|
+
return JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function readJsonFileSafe(filePath) {
|
|
59
|
+
try {
|
|
60
|
+
const text = await fs.readFile(filePath, "utf8");
|
|
61
|
+
return JSON.parse(text);
|
|
62
|
+
} catch {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** 从 Markdown 正文提取步骤与避坑点,供无 .briefing.json 的旧草稿自动生成可视化简报。 */
|
|
68
|
+
function buildFallbackBriefing(name, description, body, isPatch = false) {
|
|
69
|
+
const numbered = String(body || "")
|
|
70
|
+
.split("\n")
|
|
71
|
+
.map((l) => l.trim())
|
|
72
|
+
.filter((l) => /^\d+\.\s+/.test(l))
|
|
73
|
+
.slice(0, 4)
|
|
74
|
+
.map((l) => l.replace(/^\d+\.\s+/, "").replace(/\*\*/g, "").slice(0, 100));
|
|
75
|
+
const bullets = String(body || "")
|
|
76
|
+
.split("\n")
|
|
77
|
+
.map((l) => l.trim())
|
|
78
|
+
.filter((l) => /^[-*•]\s+/.test(l))
|
|
79
|
+
.slice(0, 3)
|
|
80
|
+
.map((l) => l.replace(/^[-*•]\s+/, "").replace(/\*\*/g, "").slice(0, 110));
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
titleZh: `${name}(${isPatch ? "现有技能升级" : "新蒸馏技能"})`,
|
|
84
|
+
triggerZh: `当遇到与「${name}」相关的同类任务或排障场景时自动触发`,
|
|
85
|
+
whatItIs: description || `从最近会话中自动提炼出的「${name}」可复用执行流程与防坑护栏。`,
|
|
86
|
+
stepsZh: numbered.length ? numbered : (bullets.length ? bullets : ["读取上下文并按标准顺序排查", "执行最小化改动并验证真实结果"]),
|
|
87
|
+
pitfallsZh: bullets.length ? bullets : ["避免未经验证直接假设根因"],
|
|
88
|
+
keyRules: [...numbered, ...bullets].slice(0, 4),
|
|
89
|
+
ifApprove: isPatch
|
|
90
|
+
? `升级现有「${name}」技能(原版本自动存入 SHA-256 账本,随时可一键回滚)`
|
|
91
|
+
: `新增「${name}」正式技能并立即热更新生效,后续同类任务自动按此流程执行`,
|
|
92
|
+
ifReject: `直接销毁该暂存草稿,正式技能库保持 0 改动`,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** 读一个技能的完整 SKILL.md、资源清单与中文简报。 */
|
|
97
|
+
async function readSkillDetail(skillsRoot, name, baseDir = skillsRoot, existingSkillNames = new Set()) {
|
|
98
|
+
const dir = path.join(baseDir, name);
|
|
99
|
+
const text = await fs.readFile(path.join(dir, "SKILL.md"), "utf8").catch(() => null);
|
|
100
|
+
if (text === null) return null;
|
|
101
|
+
const parsed = parseSkillMarkdown(text);
|
|
102
|
+
const resources = [];
|
|
103
|
+
for (const kind of ["references", "templates", "scripts", "assets"]) {
|
|
104
|
+
const files = await fs.readdir(path.join(dir, kind)).catch(() => []);
|
|
105
|
+
for (const f of files) resources.push(`${kind}/${f}`);
|
|
106
|
+
}
|
|
107
|
+
const savedBriefing = await readJsonFileSafe(path.join(dir, ".briefing.json"));
|
|
108
|
+
const isPatch = existingSkillNames.has(name);
|
|
109
|
+
const fallback = buildFallbackBriefing(name, parsed?.frontmatter?.description ?? "", parsed?.body ?? "", isPatch);
|
|
110
|
+
const briefing =
|
|
111
|
+
savedBriefing && typeof savedBriefing === "object"
|
|
112
|
+
? {
|
|
113
|
+
...fallback,
|
|
114
|
+
...savedBriefing,
|
|
115
|
+
triggerZh: savedBriefing.triggerZh || fallback.triggerZh,
|
|
116
|
+
stepsZh:
|
|
117
|
+
Array.isArray(savedBriefing.stepsZh) && savedBriefing.stepsZh.length
|
|
118
|
+
? savedBriefing.stepsZh
|
|
119
|
+
: Array.isArray(savedBriefing.keyRules) && savedBriefing.keyRules.length
|
|
120
|
+
? savedBriefing.keyRules
|
|
121
|
+
: fallback.stepsZh,
|
|
122
|
+
pitfallsZh:
|
|
123
|
+
Array.isArray(savedBriefing.pitfallsZh) && savedBriefing.pitfallsZh.length
|
|
124
|
+
? savedBriefing.pitfallsZh
|
|
125
|
+
: fallback.pitfallsZh,
|
|
126
|
+
}
|
|
127
|
+
: fallback;
|
|
128
|
+
|
|
129
|
+
return {
|
|
130
|
+
name,
|
|
131
|
+
description: parsed?.frontmatter?.description ?? "",
|
|
132
|
+
body: parsed?.body ?? "",
|
|
133
|
+
frontmatter: parsed?.frontmatter ?? {},
|
|
134
|
+
resources,
|
|
135
|
+
briefing,
|
|
136
|
+
isPatch,
|
|
137
|
+
raw: text,
|
|
138
|
+
path: dir,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** 构建完整状态快照(一次性给齐前端 5 个标签页所需的全部核心数据)。 */
|
|
143
|
+
export async function buildSnapshot(skillsRoot, config) {
|
|
144
|
+
const [skills, rawDrafts, ledger, usage, lastReview, throttleState] = await Promise.all([
|
|
145
|
+
listSkills(skillsRoot),
|
|
146
|
+
listDrafts(skillsRoot),
|
|
147
|
+
readEntries(skillsRoot),
|
|
148
|
+
loadUsage(skillsRoot),
|
|
149
|
+
readJsonFileSafe(path.join(skillsRoot, ".last_review.json")),
|
|
150
|
+
readJsonFileSafe(path.join(skillsRoot, ".throttle.json")),
|
|
151
|
+
]);
|
|
152
|
+
|
|
153
|
+
const existingSkillNames = new Set(skills.map((s) => s.name));
|
|
154
|
+
|
|
155
|
+
// 为每个草稿附上完整 body / frontmatter / resources / briefing
|
|
156
|
+
const drafts = [];
|
|
157
|
+
for (const d of rawDrafts) {
|
|
158
|
+
const detail = await readSkillDetail(
|
|
159
|
+
skillsRoot,
|
|
160
|
+
d.name,
|
|
161
|
+
path.join(skillsRoot, STAGING_DIRNAME),
|
|
162
|
+
existingSkillNames,
|
|
163
|
+
);
|
|
164
|
+
drafts.push(detail ? { ...d, ...detail } : d);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// 为每个正式技能附上 usage、生命周期、adopted 标记及完整 body/resources
|
|
168
|
+
const enriched = [];
|
|
169
|
+
for (const s of skills) {
|
|
170
|
+
const u = entryOf(usage, s.name);
|
|
171
|
+
const rawUsage = usage[s.name] ?? {};
|
|
172
|
+
const detail = await readSkillDetail(skillsRoot, s.name, skillsRoot, existingSkillNames);
|
|
173
|
+
enriched.push({
|
|
174
|
+
...s,
|
|
175
|
+
state: u.state,
|
|
176
|
+
pinned: u.pinned,
|
|
177
|
+
useCount: u.useCount,
|
|
178
|
+
lastUsedAt: u.lastUsedAt,
|
|
179
|
+
createdAt: u.createdAt,
|
|
180
|
+
adopted: Boolean(rawUsage.adopted),
|
|
181
|
+
protected: PROTECTED_SKILLS.has(s.name),
|
|
182
|
+
body: detail?.body ?? "",
|
|
183
|
+
resources: detail?.resources ?? [],
|
|
184
|
+
frontmatter: detail?.frontmatter ?? {},
|
|
185
|
+
briefing: detail?.briefing ?? null,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const byState = { active: 0, stale: 0, archived: 0 };
|
|
190
|
+
for (const s of enriched) byState[s.state] = (byState[s.state] ?? 0) + 1;
|
|
191
|
+
|
|
192
|
+
const agentCreated = enriched.filter((s) => s.createdBy === "agent").length;
|
|
193
|
+
const unmanaged = await listUnmanaged(
|
|
194
|
+
skillsRoot,
|
|
195
|
+
skills.map((s) => s.name),
|
|
196
|
+
);
|
|
197
|
+
const candidates = enriched.filter((s) => !s.pinned && !s.protected);
|
|
198
|
+
|
|
199
|
+
return {
|
|
200
|
+
config: {
|
|
201
|
+
enabled: config.enabled,
|
|
202
|
+
mode: config.mode,
|
|
203
|
+
skillsRoot,
|
|
204
|
+
minTurnsBetween: config.minTurnsBetween,
|
|
205
|
+
minMinutesBetween: config.minMinutesBetween,
|
|
206
|
+
maxMessages: config.maxMessages,
|
|
207
|
+
maxChars: config.maxChars,
|
|
208
|
+
reviewTimeoutSeconds: config.reviewTimeoutSeconds,
|
|
209
|
+
curatorEnabled: config.curatorEnabled,
|
|
210
|
+
consolidate: config.consolidate,
|
|
211
|
+
consolidateDryRun: config.consolidateDryRun,
|
|
212
|
+
staleAfterDays: config.staleAfterDays,
|
|
213
|
+
archiveAfterDays: config.archiveAfterDays,
|
|
214
|
+
reviewModel: config.reviewModel,
|
|
215
|
+
},
|
|
216
|
+
stats: {
|
|
217
|
+
skills: enriched.length,
|
|
218
|
+
agentCreated,
|
|
219
|
+
drafts: drafts.length,
|
|
220
|
+
ledger: ledger.length,
|
|
221
|
+
unmanaged: unmanaged.length,
|
|
222
|
+
candidates: candidates.length,
|
|
223
|
+
byState,
|
|
224
|
+
},
|
|
225
|
+
lastReview: lastReview ?? null,
|
|
226
|
+
throttle: throttleState ?? { turnCount: 0, lastRunAt: 0 },
|
|
227
|
+
unmanaged,
|
|
228
|
+
skills: enriched,
|
|
229
|
+
drafts,
|
|
230
|
+
recentLedger: ledger
|
|
231
|
+
.slice(-50)
|
|
232
|
+
.reverse()
|
|
233
|
+
.map((e) => ({
|
|
234
|
+
id: e.id,
|
|
235
|
+
ts: e.ts,
|
|
236
|
+
action: e.action,
|
|
237
|
+
target: e.target,
|
|
238
|
+
actor: e.actor,
|
|
239
|
+
rationale: e.extra?.rationale ?? e.extra?.reason ?? "",
|
|
240
|
+
userComment: e.extra?.userComment ?? "",
|
|
241
|
+
aiAnalysis: e.extra?.aiAnalysis ?? "",
|
|
242
|
+
signals: e.extra?.signals ?? [],
|
|
243
|
+
confidence: e.extra?.confidence ?? "",
|
|
244
|
+
absorbedInto: e.extra?.absorbedInto ?? "",
|
|
245
|
+
before: e.before ?? null,
|
|
246
|
+
after: e.after ?? null,
|
|
247
|
+
extra: e.extra ?? {},
|
|
248
|
+
files: e.after?.files?.length ?? e.before?.files?.length ?? 0,
|
|
249
|
+
})),
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* 根据用户的强制批示(comment),调用 AI 分析并修订草稿。
|
|
255
|
+
*/
|
|
256
|
+
export async function refineDraftWithComment(skillsRoot, name, comment, runFork) {
|
|
257
|
+
const stagingDir = path.join(skillsRoot, STAGING_DIRNAME, name);
|
|
258
|
+
const rawMd = await fs.readFile(path.join(stagingDir, "SKILL.md"), "utf8").catch(() => null);
|
|
259
|
+
if (!rawMd) return { ok: false, error: `草稿 "${name}" 不存在` };
|
|
260
|
+
|
|
261
|
+
const parsed = parseSkillMarkdown(rawMd);
|
|
262
|
+
const oldBriefing = await readJsonFileSafe(path.join(stagingDir, ".briefing.json"));
|
|
263
|
+
|
|
264
|
+
if (typeof runFork !== "function") {
|
|
265
|
+
return { ok: false, error: "AI 复盘通道未就绪" };
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const prompt = `你现在是自进化引擎的「用户批示融合器」。
|
|
269
|
+
用户审阅了待审技能草稿 \`${name}\`,并提交了如下调整想法/批示:
|
|
270
|
+
|
|
271
|
+
【用户的想法/调整指令】:
|
|
272
|
+
${comment}
|
|
273
|
+
|
|
274
|
+
【当前草稿 frontmatter】:
|
|
275
|
+
${JSON.stringify(parsed?.frontmatter ?? { name }, null, 2)}
|
|
276
|
+
|
|
277
|
+
【当前草稿正文 (SKILL.md)】:
|
|
278
|
+
${parsed?.body ?? rawMd}
|
|
279
|
+
|
|
280
|
+
请你保持原草稿本身的风格(即 Hermes 标准格式),仅将【用户的想法/调整指令】精准插入或调整到对应的步骤/规则中。
|
|
281
|
+
并同步更新给用户看的可视化中文简报字段(\`briefing\`)与一句简短的中文调整说明(\`aiAnalysis\`)。
|
|
282
|
+
只输出一个 fenced JSON 块:
|
|
283
|
+
|
|
284
|
+
\`\`\`json
|
|
285
|
+
{
|
|
286
|
+
"action": "patch",
|
|
287
|
+
"aiAnalysis": "<一句话中文说明:已将您的哪条想法插入到了哪个步骤或规则中>",
|
|
288
|
+
"frontmatter": {
|
|
289
|
+
"name": "${name}",
|
|
290
|
+
"description": "<保持或微调描述>"
|
|
291
|
+
},
|
|
292
|
+
"body": "<融入用户想法后的完整 SKILL.md 正文>",
|
|
293
|
+
"briefing": {
|
|
294
|
+
"titleZh": "<简短中文标题>",
|
|
295
|
+
"triggerZh": "<一句话中文:什么场景下触发该技能>",
|
|
296
|
+
"whatItIs": "<一句话中文:这个技能代表了什么核心经验>",
|
|
297
|
+
"ifApprove": "<同意后的效果说明(中文)>",
|
|
298
|
+
"ifReject": "<不同意后的效果说明(中文)>"
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
\`\`\``;
|
|
302
|
+
|
|
303
|
+
const before = await captureSnapshot(path.join(skillsRoot, STAGING_DIRNAME), stagingDir);
|
|
304
|
+
const rawOut = await runFork(prompt);
|
|
305
|
+
const block = extractJsonBlock(rawOut);
|
|
306
|
+
if (!block || !block.body) {
|
|
307
|
+
return { ok: false, error: "AI 未能返回合法的修订 JSON" };
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
const nextFm = {
|
|
311
|
+
...(parsed?.frontmatter ?? {}),
|
|
312
|
+
name,
|
|
313
|
+
description: block.frontmatter?.description || parsed?.frontmatter?.description || "",
|
|
314
|
+
metadata: {
|
|
315
|
+
...(parsed?.frontmatter?.metadata ?? {}),
|
|
316
|
+
createdBy: "agent",
|
|
317
|
+
lastUserDirective: comment,
|
|
318
|
+
},
|
|
319
|
+
};
|
|
320
|
+
const nextMd = renderSkillMarkdown(nextFm, block.body);
|
|
321
|
+
await fs.writeFile(path.join(stagingDir, "SKILL.md"), nextMd, "utf8");
|
|
322
|
+
|
|
323
|
+
const nextBriefing = {
|
|
324
|
+
...(oldBriefing ?? {}),
|
|
325
|
+
...(block.briefing ?? {}),
|
|
326
|
+
lastUserComment: comment,
|
|
327
|
+
aiAnalysis: block.aiAnalysis || "已根据您的批示完成技能规则修订。",
|
|
328
|
+
};
|
|
329
|
+
await fs.writeFile(path.join(stagingDir, ".briefing.json"), JSON.stringify(nextBriefing, null, 2), "utf8");
|
|
330
|
+
|
|
331
|
+
const after = await captureSnapshot(path.join(skillsRoot, STAGING_DIRNAME), stagingDir);
|
|
332
|
+
await appendEntry(skillsRoot, {
|
|
333
|
+
action: "draft_refine",
|
|
334
|
+
target: name,
|
|
335
|
+
actor: "user",
|
|
336
|
+
before,
|
|
337
|
+
after,
|
|
338
|
+
extra: {
|
|
339
|
+
userComment: comment,
|
|
340
|
+
aiAnalysis: nextBriefing.aiAnalysis,
|
|
341
|
+
rationale: `用户批示修订:${comment}`,
|
|
342
|
+
},
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
return {
|
|
346
|
+
ok: true,
|
|
347
|
+
name,
|
|
348
|
+
aiAnalysis: nextBriefing.aiAnalysis,
|
|
349
|
+
briefing: nextBriefing,
|
|
350
|
+
refinedContent: nextMd,
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* 注册 HTTP 路由。
|
|
356
|
+
*
|
|
357
|
+
* @param {object} ctx
|
|
358
|
+
* @param {object} deps { config, skillsRoot, saveConfig, triggerReview, runConsolidation, runFork }
|
|
359
|
+
*/
|
|
360
|
+
const MOBILE_HTML = `<!doctype html>
|
|
361
|
+
<html lang="zh-CN">
|
|
362
|
+
<head>
|
|
363
|
+
<meta charset="utf-8" />
|
|
364
|
+
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, viewport-fit=cover" />
|
|
365
|
+
<meta name="apple-mobile-web-app-capable" content="yes" />
|
|
366
|
+
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
|
|
367
|
+
<title>DSH 自进化 · 手机审批台</title>
|
|
368
|
+
<style>
|
|
369
|
+
:root {
|
|
370
|
+
--bg: #09090b; --card: #18181b; --layer: #27272a; --border: #3f3f46;
|
|
371
|
+
--text: #f4f4f5; --sub: #a1a1aa; --brand: #8b7cf6; --ok: #34d399; --warn: #fbbf24; --err: #f87171;
|
|
372
|
+
}
|
|
373
|
+
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
|
|
374
|
+
body {
|
|
375
|
+
margin: 0; background: var(--bg); color: var(--text);
|
|
376
|
+
font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Helvetica Neue", sans-serif;
|
|
377
|
+
font-size: 14px; line-height: 1.55;
|
|
378
|
+
padding: calc(12px + env(safe-area-inset-top, 0px)) 14px calc(78px + env(safe-area-inset-bottom, 0px));
|
|
379
|
+
}
|
|
380
|
+
.head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; }
|
|
381
|
+
.title { font-size: 18px; font-weight: 700; margin: 0; }
|
|
382
|
+
.sub { font-size: 12px; color: var(--sub); margin-top: 2px; }
|
|
383
|
+
.chip {
|
|
384
|
+
display: inline-flex; align-items: center; gap: 4px; border-radius: 999px;
|
|
385
|
+
padding: 3px 10px; font-size: 11px; font-weight: 600;
|
|
386
|
+
border: 1px solid var(--border); background: var(--layer); color: var(--sub);
|
|
387
|
+
}
|
|
388
|
+
.chip-ok { color: #6ee7b7; border-color: rgba(52,211,153,.4); background: rgba(52,211,153,.12); }
|
|
389
|
+
.chip-warn { color: #fde68a; border-color: rgba(251,191,36,.4); background: rgba(251,191,36,.12); }
|
|
390
|
+
.chip-brand { color: #ddd6fe; border-color: rgba(139,124,246,.45); background: rgba(139,124,246,.16); }
|
|
391
|
+
.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 9px; margin-bottom: 14px; }
|
|
392
|
+
.stat { background: var(--card); border: 1px solid rgba(63,63,70,.7); border-radius: 12px; padding: 12px; }
|
|
393
|
+
.stat-v { font-size: 20px; font-weight: 700; }
|
|
394
|
+
.stat-l { font-size: 11.5px; color: var(--sub); }
|
|
395
|
+
.card {
|
|
396
|
+
background: var(--card); border: 1px solid rgba(63,63,70,.75);
|
|
397
|
+
border-radius: 14px; padding: 14px; margin-bottom: 12px;
|
|
398
|
+
display: flex; flex-direction: column; gap: 10px;
|
|
399
|
+
}
|
|
400
|
+
.card-draft { border-left: 3px solid var(--warn); }
|
|
401
|
+
.code-badge {
|
|
402
|
+
font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px;
|
|
403
|
+
color: #c4b5fd; background: rgba(139,124,246,.16); padding: 2px 8px; border-radius: 6px;
|
|
404
|
+
}
|
|
405
|
+
.sec { background: rgba(139,124,246,.08); border: 1px solid rgba(139,124,246,.28); border-radius: 10px; padding: 10px 12px; }
|
|
406
|
+
.sec-title { font-size: 12px; font-weight: 700; color: #ddd6fe; margin-bottom: 4px; }
|
|
407
|
+
.impact-ok { background: rgba(52,211,153,.09); border: 1px solid rgba(52,211,153,.32); border-radius: 10px; padding: 10px 12px; }
|
|
408
|
+
.impact-err { background: rgba(248,113,113,.09); border: 1px solid rgba(248,113,113,.32); border-radius: 10px; padding: 10px 12px; }
|
|
409
|
+
ul { margin: 6px 0 0; padding-left: 18px; color: #d4d4d8; font-size: 12.5px; }
|
|
410
|
+
textarea, input {
|
|
411
|
+
width: 100%; border-radius: 10px; border: 1px solid var(--border);
|
|
412
|
+
background: var(--layer); color: var(--text); padding: 10px 12px;
|
|
413
|
+
font-size: 16px; font-family: inherit; outline: none;
|
|
414
|
+
}
|
|
415
|
+
textarea:focus, input:focus { border-color: var(--brand); }
|
|
416
|
+
.quick-list { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 4px; }
|
|
417
|
+
.quick-btn {
|
|
418
|
+
flex-shrink: 0; border-radius: 999px; border: 1px solid var(--border);
|
|
419
|
+
background: var(--layer); color: #d4d4d8; padding: 5px 11px; font-size: 11.5px;
|
|
420
|
+
}
|
|
421
|
+
.btn-col { display: flex; flex-direction: column; gap: 8px; }
|
|
422
|
+
.btn {
|
|
423
|
+
width: 100%; min-height: 42px; border-radius: 10px; border: 1px solid var(--border);
|
|
424
|
+
background: var(--layer); color: var(--text); font-size: 13.5px; font-weight: 600;
|
|
425
|
+
display: flex; align-items: center; justify-content: center; gap: 6px; cursor: pointer;
|
|
426
|
+
}
|
|
427
|
+
.btn:disabled { opacity: .42; }
|
|
428
|
+
.btn-pri { background: rgba(139,124,246,.28); border-color: rgba(139,124,246,.55); color: #ede9fe; }
|
|
429
|
+
.btn-dan { color: #fca5a5; border-color: rgba(248,113,113,.4); }
|
|
430
|
+
.tabbar {
|
|
431
|
+
position: fixed; left: 0; right: 0; bottom: 0; z-index: 100;
|
|
432
|
+
background: rgba(24,24,27,.94); backdrop-filter: blur(12px);
|
|
433
|
+
border-top: 1px solid var(--border);
|
|
434
|
+
display: grid; grid-template-columns: repeat(4, 1fr);
|
|
435
|
+
padding: 6px 8px calc(6px + env(safe-area-inset-bottom, 0px));
|
|
436
|
+
}
|
|
437
|
+
.tab-btn {
|
|
438
|
+
border: none; background: transparent; color: var(--sub);
|
|
439
|
+
padding: 6px 4px; font-size: 11.5px; font-weight: 600; border-radius: 8px;
|
|
440
|
+
}
|
|
441
|
+
.tab-btn.on { color: #ddd6fe; background: rgba(139,124,246,.2); }
|
|
442
|
+
.toast {
|
|
443
|
+
position: fixed; top: calc(14px + env(safe-area-inset-top, 0px)); left: 14px; right: 14px;
|
|
444
|
+
z-index: 999; padding: 11px 14px; border-radius: 10px; background: #18181b;
|
|
445
|
+
border: 1px solid var(--ok); color: #6ee7b7; font-size: 13px; font-weight: 600;
|
|
446
|
+
box-shadow: 0 8px 24px rgba(0,0,0,.5);
|
|
447
|
+
}
|
|
448
|
+
pre {
|
|
449
|
+
margin: 0; padding: 10px; border-radius: 8px; background: #000; color: #d4d4d8;
|
|
450
|
+
font-size: 11.5px; overflow-x: auto; white-space: pre-wrap; word-break: break-word;
|
|
451
|
+
}
|
|
452
|
+
</style>
|
|
453
|
+
</head>
|
|
454
|
+
<body>
|
|
455
|
+
<div id="app">正在加载 DSH 自进化手机审批台…</div>
|
|
456
|
+
<script>
|
|
457
|
+
const API = '/dsh-evolution/api';
|
|
458
|
+
let state = { data: null, tab: 'drafts', busy: null, toast: null, comments: {}, previewOpen: {} };
|
|
459
|
+
const QUICKS = [
|
|
460
|
+
'同意采纳:请将技能正文全部转化为清晰易读的中文规范后正式入库',
|
|
461
|
+
'同意采纳:提炼的排查顺序与防坑规则准确,直接按此入库',
|
|
462
|
+
'同意但需补充:请在步骤开头加入“修改前必须先备份原文件/配置”的硬性要求',
|
|
463
|
+
'不同意驳回:这属于单次特定环境偶发问题,不应固化为全局技能'
|
|
464
|
+
];
|
|
465
|
+
|
|
466
|
+
async function callApi(path, opts) {
|
|
467
|
+
const r = await fetch(API + path, { headers: { 'content-type': 'application/json' }, ...opts });
|
|
468
|
+
const d = await r.json().catch(() => ({}));
|
|
469
|
+
if (!r.ok) throw new Error(d.error || ('HTTP ' + r.status));
|
|
470
|
+
return d;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
function showToast(msg) {
|
|
474
|
+
state.toast = msg;
|
|
475
|
+
render();
|
|
476
|
+
setTimeout(() => { state.toast = null; render(); }, 3200);
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
async function load() {
|
|
480
|
+
try {
|
|
481
|
+
state.data = await callApi('/status');
|
|
482
|
+
render();
|
|
483
|
+
} catch (e) {
|
|
484
|
+
document.getElementById('app').innerText = '加载失败:' + e.message;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
async function act(key, fn, msg) {
|
|
489
|
+
state.busy = key; render();
|
|
490
|
+
try {
|
|
491
|
+
const r = await fn();
|
|
492
|
+
showToast(typeof msg === 'function' ? msg(r) : msg);
|
|
493
|
+
await load();
|
|
494
|
+
} catch (e) {
|
|
495
|
+
showToast('失败:' + e.message);
|
|
496
|
+
} finally {
|
|
497
|
+
state.busy = null; render();
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
window.setTab = (t) => { state.tab = t; render(); };
|
|
502
|
+
window.setComment = (name, val) => { state.comments[name] = val; render(); };
|
|
503
|
+
window.togglePreview = (name) => { state.previewOpen[name] = !state.previewOpen[name]; render(); };
|
|
504
|
+
window.doRefine = (name) => {
|
|
505
|
+
const c = (state.comments[name] || '').trim();
|
|
506
|
+
if (!c) return;
|
|
507
|
+
act('d:' + name, () => callApi('/refine-draft', { method: 'POST', body: JSON.stringify({ name, comment: c }) }), (r) => 'AI 已按批示修订:' + (r.aiAnalysis || name));
|
|
508
|
+
};
|
|
509
|
+
window.doPromote = (name) => {
|
|
510
|
+
const c = (state.comments[name] || '').trim();
|
|
511
|
+
if (!c) return;
|
|
512
|
+
act('d:' + name, () => callApi('/promote', { method: 'POST', body: JSON.stringify({ name, comment: c, evolveWithComment: true }) }), '已按批示进化并采纳进正式库');
|
|
513
|
+
};
|
|
514
|
+
window.doDiscard = (name) => {
|
|
515
|
+
const c = (state.comments[name] || '').trim();
|
|
516
|
+
if (!c) return;
|
|
517
|
+
act('d:' + name, () => callApi('/discard', { method: 'POST', body: JSON.stringify({ name, comment: c }) }), '已按批示驳回');
|
|
518
|
+
};
|
|
519
|
+
window.doRollback = (id, target) => {
|
|
520
|
+
act('r:' + id, () => callApi('/rollback', { method: 'POST', body: JSON.stringify({ id }) }), '已回滚 ' + target);
|
|
521
|
+
};
|
|
522
|
+
|
|
523
|
+
function esc(s) {
|
|
524
|
+
return String(s || '').replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"');
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
function render() {
|
|
528
|
+
const d = state.data;
|
|
529
|
+
if (!d) return;
|
|
530
|
+
const st = d.stats || {};
|
|
531
|
+
const drafts = d.drafts || [];
|
|
532
|
+
const skills = d.skills || [];
|
|
533
|
+
const ledger = d.recentLedger || [];
|
|
534
|
+
|
|
535
|
+
let bodyHtml = '';
|
|
536
|
+
if (state.tab === 'drafts') {
|
|
537
|
+
if (!drafts.length) {
|
|
538
|
+
bodyHtml = '<div class="card"><div style="text-align:center;color:var(--sub);padding:20px 0">当前没有待审阅的技能草稿</div></div>';
|
|
539
|
+
} else {
|
|
540
|
+
bodyHtml = drafts.map((dr) => {
|
|
541
|
+
const b = dr.briefing || {};
|
|
542
|
+
const rules = Array.isArray(b.keyRules) ? b.keyRules : [];
|
|
543
|
+
const c = state.comments[dr.name] || '';
|
|
544
|
+
const busy = state.busy === ('d:' + dr.name);
|
|
545
|
+
const showPre = !!state.previewOpen[dr.name];
|
|
546
|
+
return '<div class="card card-draft">' +
|
|
547
|
+
'<div style="display:flex;justify-content:space-between;align-items:center;gap:8px;flex-wrap:wrap">' +
|
|
548
|
+
'<span class="code-badge">' + esc(dr.name) + '</span>' +
|
|
549
|
+
'<span class="chip ' + (dr.isPatch ? 'chip-ok' : 'chip-warn') + '">' + (dr.isPatch ? '现有技能升级补丁' : '新蒸馏技能') + '</span>' +
|
|
550
|
+
'</div>' +
|
|
551
|
+
'<div style="font-size:15.5px;font-weight:700">' + esc(b.titleZh || dr.name) + '</div>' +
|
|
552
|
+
'<div class="sec"><div class="sec-title">💡 这是个什么东西</div><div>' + esc(b.whatItIs || dr.description) + '</div>' +
|
|
553
|
+
(rules.length ? '<ul>' + rules.map(r => '<li>' + esc(r) + '</li>').join('') + '</ul>' : '') +
|
|
554
|
+
'</div>' +
|
|
555
|
+
'<div class="impact-ok"><div style="color:#6ee7b7;font-weight:700;font-size:12px">✅ 如果同意采纳会如何</div><div style="font-size:12.5px;margin-top:3px">' + esc(b.ifApprove) + '</div></div>' +
|
|
556
|
+
'<div class="impact-err"><div style="color:#fca5a5;font-weight:700;font-size:12px">🚫 如果不同意驳回会如何</div><div style="font-size:12.5px;margin-top:3px">' + esc(b.ifReject) + '</div></div>' +
|
|
557
|
+
(b.aiAnalysis ? '<div class="impact-ok"><div style="color:#6ee7b7;font-weight:700;font-size:12px">🤖 AI 批示融合汇报</div><div style="font-size:12.5px;margin-top:3px">' + esc(b.aiAnalysis) + '</div></div>' : '') +
|
|
558
|
+
'<button class="btn" onclick="togglePreview(\\'' + esc(dr.name) + '\\')">' + (showPre ? '收起 SKILL.md 源码' : '📄 查看 SKILL.md 源码全文') + '</button>' +
|
|
559
|
+
(showPre ? '<pre>' + esc(dr.body) + '</pre>' : '') +
|
|
560
|
+
'<div style="font-size:12.5px;font-weight:700;margin-top:4px">✍️ 您的强制批示(必填,填完解锁按钮):</div>' +
|
|
561
|
+
'<div class="quick-list">' + QUICKS.map(q => '<button class="quick-btn" onclick="setComment(\\'' + esc(dr.name) + '\\', \\'' + esc(q) + '\\')">+ ' + esc(q.slice(0, 18)) + '…</button>').join('') + '</div>' +
|
|
562
|
+
'<textarea rows="3" placeholder="输入您的想法或修改指示…" oninput="state.comments[\\'' + esc(dr.name) + '\\']=this.value;document.getElementById(\\'btns-' + esc(dr.name) + '\\').querySelectorAll(\\'button\\').forEach(b=>b.disabled=!this.value.trim())">' + esc(c) + '</textarea>' +
|
|
563
|
+
'<div class="btn-col" id="btns-' + esc(dr.name) + '">' +
|
|
564
|
+
'<button class="btn" ' + (!c.trim() || busy ? 'disabled' : '') + ' onclick="doRefine(\\'' + esc(dr.name) + '\\')">' + (busy ? 'AI 正在处理…' : '🤖 1. AI 按批示修订草稿(先看效果)') + '</button>' +
|
|
565
|
+
'<button class="btn btn-pri" ' + (!c.trim() || busy ? 'disabled' : '') + ' onclick="doPromote(\\'' + esc(dr.name) + '\\')">' + (busy ? '处理中…' : '✅ 2. 按批示进化并采纳进正式库') + '</button>' +
|
|
566
|
+
'<button class="btn btn-dan" ' + (!c.trim() || busy ? 'disabled' : '') + ' onclick="doDiscard(\\'' + esc(dr.name) + '\\')">✕ 按批示驳回 (Discard)</button>' +
|
|
567
|
+
'</div>' +
|
|
568
|
+
'</div>';
|
|
569
|
+
}).join('');
|
|
570
|
+
}
|
|
571
|
+
} else if (state.tab === 'skills') {
|
|
572
|
+
bodyHtml = skills.map(s =>
|
|
573
|
+
'<div class="card">' +
|
|
574
|
+
'<div style="display:flex;justify-content:space-between;align-items:center">' +
|
|
575
|
+
'<span class="code-badge">' + esc(s.name) + '</span>' +
|
|
576
|
+
'<span class="chip chip-ok">' + esc(s.state) + ' · 用 ' + (s.useCount || 0) + ' 次</span>' +
|
|
577
|
+
'</div>' +
|
|
578
|
+
'<div style="font-size:13px;color:#d4d4d8">' + esc(s.description) + '</div>' +
|
|
579
|
+
'</div>'
|
|
580
|
+
).join('') || '<div class="card">暂无正式技能</div>';
|
|
581
|
+
} else if (state.tab === 'ledger') {
|
|
582
|
+
bodyHtml = ledger.slice(0, 20).map(e =>
|
|
583
|
+
'<div class="card">' +
|
|
584
|
+
'<div style="display:flex;justify-content:space-between;align-items:center;flex-wrap:wrap;gap:6px">' +
|
|
585
|
+
'<span class="chip chip-brand">' + esc(e.action) + '</span>' +
|
|
586
|
+
'<span class="code-badge">' + esc(e.target) + '</span>' +
|
|
587
|
+
'<span style="font-size:11px;color:var(--sub)">' + new Date(e.ts).toLocaleString('zh-CN', { hour12: false }) + '</span>' +
|
|
588
|
+
'</div>' +
|
|
589
|
+
(e.rationale ? '<div style="font-size:12.5px;color:#d4d4d8">' + esc(e.rationale) + '</div>' : '') +
|
|
590
|
+
(e.userComment ? '<div style="font-size:12px;color:#6ee7b7">✍️ 批示:' + esc(e.userComment) + '</div>' : '') +
|
|
591
|
+
(e.before && e.before.files ? '<button class="btn btn-dan" onclick="doRollback(\\'' + esc(e.id) + '\\',\\'' + esc(e.target) + '\\')">↩ 回滚到此版本</button>' : '') +
|
|
592
|
+
'</div>'
|
|
593
|
+
).join('') || '<div class="card">暂无账本记录</div>';
|
|
594
|
+
} else {
|
|
595
|
+
bodyHtml = '<div class="card"><div style="font-weight:700">引擎状态</div>' +
|
|
596
|
+
'<div style="font-size:13px;color:var(--sub)">模式:' + esc(d.config?.mode) + ' · 节流:' + (d.throttle?.turnCount || 0) + '/' + (d.config?.minTurnsBetween || 3) + ' 回合</div>' +
|
|
597
|
+
'<div style="font-size:12.5px;color:var(--sub)">技能根目录:' + esc(d.config?.skillsRoot) + '</div></div>';
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
document.getElementById('app').innerHTML =
|
|
601
|
+
(state.toast ? '<div class="toast">' + esc(state.toast) + '</div>' : '') +
|
|
602
|
+
'<div class="head"><div><h1 class="title">🧬 DSH 自进化手机台</h1><div class="sub">中文简报 · 强制批示 · AI 融合进化</div></div>' +
|
|
603
|
+
'<span class="chip ' + (d.config?.enabled ? 'chip-ok' : '') + '">' + (d.config?.enabled ? '运行中' : '已停用') + '</span></div>' +
|
|
604
|
+
'<div class="stats">' +
|
|
605
|
+
'<div class="stat" onclick="setTab(\\'drafts\\')"><div class="stat-v" style="color:var(--warn)">' + (st.drafts || 0) + '</div><div class="stat-l">待审草稿</div></div>' +
|
|
606
|
+
'<div class="stat" onclick="setTab(\\'skills\\')"><div class="stat-v" style="color:var(--ok)">' + (st.skills || 0) + '</div><div class="stat-l">正式技能</div></div>' +
|
|
607
|
+
'</div>' +
|
|
608
|
+
bodyHtml +
|
|
609
|
+
'<div class="tabbar">' +
|
|
610
|
+
'<button class="tab-btn ' + (state.tab === 'drafts' ? 'on' : '') + '" onclick="setTab(\\'drafts\\')">📋 待审 (' + (st.drafts || 0) + ')</button>' +
|
|
611
|
+
'<button class="tab-btn ' + (state.tab === 'skills' ? 'on' : '') + '" onclick="setTab(\\'skills\\')">📚 技能 (' + (st.skills || 0) + ')</button>' +
|
|
612
|
+
'<button class="tab-btn ' + (state.tab === 'ledger' ? 'on' : '') + '" onclick="setTab(\\'ledger\\')">🕒 账本 (' + (st.ledger || 0) + ')</button>' +
|
|
613
|
+
'<button class="tab-btn ' + (state.tab === 'info' ? 'on' : '') + '" onclick="setTab(\\'info\\')">⚙️ 状态</button>' +
|
|
614
|
+
'</div>';
|
|
615
|
+
}
|
|
616
|
+
load();
|
|
617
|
+
</script>
|
|
618
|
+
</body>
|
|
619
|
+
</html>`;
|
|
620
|
+
|
|
621
|
+
export function registerEvolutionHttp(ctx, deps) {
|
|
622
|
+
const handleRequest = async (req, res) => {
|
|
623
|
+
// 仅用于解析相对路径,不发起任何网络请求;用保留 TLD 占位,避免把 loopback 字面量写进发行版。
|
|
624
|
+
const url = new URL(req.url ?? "/", "http://placeholder.invalid");
|
|
625
|
+
if (
|
|
626
|
+
req.method === "GET" &&
|
|
627
|
+
(url.pathname === "/" ||
|
|
628
|
+
url.pathname === "/dsh-evolution" ||
|
|
629
|
+
url.pathname === "/dsh-evolution/" ||
|
|
630
|
+
url.pathname === "/dsh-evolution/mobile" ||
|
|
631
|
+
url.pathname === "/dsh-evolution/mobile/")
|
|
632
|
+
) {
|
|
633
|
+
const buf = Buffer.from(MOBILE_HTML, "utf8");
|
|
634
|
+
res.writeHead(200, {
|
|
635
|
+
"content-type": "text/html; charset=utf-8",
|
|
636
|
+
"cache-control": "no-store",
|
|
637
|
+
"content-length": buf.length,
|
|
638
|
+
});
|
|
639
|
+
res.end(buf);
|
|
640
|
+
return;
|
|
641
|
+
}
|
|
642
|
+
const prefix = "/dsh-evolution/api/";
|
|
643
|
+
if (!url.pathname.startsWith(prefix)) {
|
|
644
|
+
res.writeHead(404);
|
|
645
|
+
res.end();
|
|
646
|
+
return;
|
|
647
|
+
}
|
|
648
|
+
const action = url.pathname.slice(prefix.length).replace(/\/$/, "");
|
|
649
|
+
const { config, skillsRoot } = deps;
|
|
650
|
+
|
|
651
|
+
try {
|
|
652
|
+
// ---- GET ----
|
|
653
|
+
if (req.method === "GET" && action === "status") {
|
|
654
|
+
sendJson(res, 200, await buildSnapshot(skillsRoot, config));
|
|
655
|
+
return;
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
if (req.method === "GET" && action === "skills") {
|
|
659
|
+
const snap = await buildSnapshot(skillsRoot, config);
|
|
660
|
+
sendJson(res, 200, { skills: snap.skills });
|
|
661
|
+
return;
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
if (req.method === "GET" && action === "drafts") {
|
|
665
|
+
const snap = await buildSnapshot(skillsRoot, config);
|
|
666
|
+
sendJson(res, 200, { drafts: snap.drafts });
|
|
667
|
+
return;
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
if (req.method === "GET" && action === "ledger") {
|
|
671
|
+
const limit = Number(url.searchParams.get("limit") ?? 100);
|
|
672
|
+
const entries = await readEntries(skillsRoot, { limit });
|
|
673
|
+
sendJson(res, 200, { entries: entries.reverse() });
|
|
674
|
+
return;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
if (req.method === "GET" && action.startsWith("ledger/")) {
|
|
678
|
+
const id = action.slice("ledger/".length);
|
|
679
|
+
const entries = await readEntries(skillsRoot);
|
|
680
|
+
const entry = entries.find((e) => e.id === id);
|
|
681
|
+
if (!entry) {
|
|
682
|
+
sendJson(res, 404, { ok: false, error: "ledger entry not found" });
|
|
683
|
+
return;
|
|
684
|
+
}
|
|
685
|
+
sendJson(res, 200, { entry });
|
|
686
|
+
return;
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
if (req.method === "GET" && action.startsWith("skill/")) {
|
|
690
|
+
const name = decodeURIComponent(action.slice("skill/".length));
|
|
691
|
+
const detail = await readSkillDetail(skillsRoot, name);
|
|
692
|
+
if (!detail) {
|
|
693
|
+
sendJson(res, 404, { ok: false, error: "skill not found" });
|
|
694
|
+
return;
|
|
695
|
+
}
|
|
696
|
+
sendJson(res, 200, { skill: detail });
|
|
697
|
+
return;
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
// ---- POST ----
|
|
701
|
+
if (req.method === "POST" && action === "refine-draft") {
|
|
702
|
+
const { name, comment } = await readJsonBody(req);
|
|
703
|
+
if (!name) {
|
|
704
|
+
sendJson(res, 400, { ok: false, error: "name is required" });
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
if (!comment || !String(comment).trim()) {
|
|
708
|
+
sendJson(res, 400, { ok: false, error: "请先填写您的审阅批示(想法或修改要求)" });
|
|
709
|
+
return;
|
|
710
|
+
}
|
|
711
|
+
const r = await refineDraftWithComment(skillsRoot, name, String(comment).trim(), deps.runFork);
|
|
712
|
+
sendJson(res, r.ok ? 200 : 400, r);
|
|
713
|
+
return;
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
if (req.method === "POST" && action === "tidy") {
|
|
717
|
+
const { dryRun, comment } = await readJsonBody(req);
|
|
718
|
+
const plan = await planTidy(skillsRoot);
|
|
719
|
+
if (dryRun !== false) {
|
|
720
|
+
sendJson(res, 200, { ok: true, dryRun: true, plan });
|
|
721
|
+
return;
|
|
722
|
+
}
|
|
723
|
+
const applied = await applyTidy(skillsRoot, plan, {
|
|
724
|
+
actor: "user",
|
|
725
|
+
comment: String(comment || "一键整理"),
|
|
726
|
+
});
|
|
727
|
+
sendJson(res, applied.ok ? 200 : 207, {
|
|
728
|
+
ok: applied.ok,
|
|
729
|
+
dryRun: false,
|
|
730
|
+
plan,
|
|
731
|
+
done: applied.done,
|
|
732
|
+
errors: applied.errors,
|
|
733
|
+
});
|
|
734
|
+
return;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
if (req.method === "POST" && action === "promote-batch") {
|
|
738
|
+
const { names, comment } = await readJsonBody(req);
|
|
739
|
+
const list = Array.isArray(names) && names.length ? names : (await listDrafts(skillsRoot)).map((d) => d.name);
|
|
740
|
+
const promoted = [];
|
|
741
|
+
const errors = [];
|
|
742
|
+
// 在单次 <50ms 批处理窗口内顺序落盘所有草稿,使 dsh-skill-filesystem 的 200ms watcher 合并为 1 次目录刷新
|
|
743
|
+
for (const n of list) {
|
|
744
|
+
const r = await promoteDraft(skillsRoot, String(n), {
|
|
745
|
+
actor: "user",
|
|
746
|
+
comment: String(comment || "批量审阅采纳"),
|
|
747
|
+
});
|
|
748
|
+
if (r.ok) promoted.push(n);
|
|
749
|
+
else errors.push(`${n}: ${r.error}`);
|
|
750
|
+
}
|
|
751
|
+
sendJson(res, errors.length && !promoted.length ? 400 : 200, { ok: errors.length === 0, promoted, errors });
|
|
752
|
+
return;
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
if (req.method === "POST" && action === "promote") {
|
|
756
|
+
const { name, comment, evolveWithComment } = await readJsonBody(req);
|
|
757
|
+
if (!name) {
|
|
758
|
+
sendJson(res, 400, { ok: false, error: "name is required" });
|
|
759
|
+
return;
|
|
760
|
+
}
|
|
761
|
+
const trimmedComment = String(comment ?? "").trim();
|
|
762
|
+
let aiAnalysis = "";
|
|
763
|
+
let refinedContent = null;
|
|
764
|
+
|
|
765
|
+
if (evolveWithComment && trimmedComment && typeof deps.runFork === "function") {
|
|
766
|
+
const refined = await refineDraftWithComment(skillsRoot, name, trimmedComment, deps.runFork);
|
|
767
|
+
if (refined.ok) {
|
|
768
|
+
aiAnalysis = refined.aiAnalysis;
|
|
769
|
+
refinedContent = refined.refinedContent;
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
const r = await promoteDraft(skillsRoot, name, {
|
|
774
|
+
actor: "user",
|
|
775
|
+
comment: trimmedComment,
|
|
776
|
+
aiAnalysis,
|
|
777
|
+
refinedContent,
|
|
778
|
+
});
|
|
779
|
+
sendJson(res, r.ok ? 200 : 400, { ...r, aiAnalysis });
|
|
780
|
+
return;
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
if (req.method === "POST" && action === "discard") {
|
|
784
|
+
const { name, comment } = await readJsonBody(req);
|
|
785
|
+
if (!name || /[\\/]|\.\./.test(String(name))) {
|
|
786
|
+
sendJson(res, 400, { ok: false, error: "valid skill name is required" });
|
|
787
|
+
return;
|
|
788
|
+
}
|
|
789
|
+
const stagingRoot = path.resolve(path.join(skillsRoot, STAGING_DIRNAME));
|
|
790
|
+
const dir = path.resolve(path.join(stagingRoot, String(name)));
|
|
791
|
+
if (!dir.startsWith(stagingRoot + path.sep)) {
|
|
792
|
+
sendJson(res, 400, { ok: false, error: "invalid draft path" });
|
|
793
|
+
return;
|
|
794
|
+
}
|
|
795
|
+
const before = await captureSnapshot(stagingRoot, dir);
|
|
796
|
+
await fs.rm(dir, { recursive: true, force: true });
|
|
797
|
+
await appendEntry(skillsRoot, {
|
|
798
|
+
action: "discard",
|
|
799
|
+
target: String(name),
|
|
800
|
+
actor: "user",
|
|
801
|
+
before,
|
|
802
|
+
after: null,
|
|
803
|
+
extra: {
|
|
804
|
+
userComment: String(comment ?? "").trim(),
|
|
805
|
+
rationale: comment ? `用户批示驳回:${String(comment).trim()}` : "用户丢弃草稿",
|
|
806
|
+
},
|
|
807
|
+
});
|
|
808
|
+
sendJson(res, 200, { ok: true });
|
|
809
|
+
return;
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
if (req.method === "POST" && action === "rollback") {
|
|
813
|
+
const { id } = await readJsonBody(req);
|
|
814
|
+
if (!id) {
|
|
815
|
+
sendJson(res, 400, { ok: false, error: "id is required" });
|
|
816
|
+
return;
|
|
817
|
+
}
|
|
818
|
+
const r = await rollbackEntry(skillsRoot, id);
|
|
819
|
+
sendJson(res, r.ok ? 200 : 400, r);
|
|
820
|
+
return;
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
if (req.method === "POST" && action === "pin") {
|
|
824
|
+
const { name, pinned } = await readJsonBody(req);
|
|
825
|
+
if (!name) {
|
|
826
|
+
sendJson(res, 400, { ok: false, error: "name is required" });
|
|
827
|
+
return;
|
|
828
|
+
}
|
|
829
|
+
const r = await setUsageFlag(skillsRoot, name, { pinned: Boolean(pinned) });
|
|
830
|
+
sendJson(res, 200, { ok: true, usage: r });
|
|
831
|
+
return;
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
if (req.method === "POST" && action === "adopt") {
|
|
835
|
+
const { name } = await readJsonBody(req);
|
|
836
|
+
if (!name) {
|
|
837
|
+
sendJson(res, 400, { ok: false, error: "name is required" });
|
|
838
|
+
return;
|
|
839
|
+
}
|
|
840
|
+
const r = await adoptSkill(skillsRoot, name);
|
|
841
|
+
sendJson(res, r.ok ? 200 : 400, r);
|
|
842
|
+
return;
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
if (req.method === "POST" && action === "disown") {
|
|
846
|
+
const { name } = await readJsonBody(req);
|
|
847
|
+
if (!name) {
|
|
848
|
+
sendJson(res, 400, { ok: false, error: "name is required" });
|
|
849
|
+
return;
|
|
850
|
+
}
|
|
851
|
+
const r = await disownSkill(skillsRoot, name);
|
|
852
|
+
sendJson(res, r.ok ? 200 : 400, r);
|
|
853
|
+
return;
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
if (req.method === "POST" && action === "consolidate") {
|
|
857
|
+
const body = await readJsonBody(req);
|
|
858
|
+
const dryRun = body.dryRun !== false;
|
|
859
|
+
const r = await deps.runConsolidation({ dryRun });
|
|
860
|
+
sendJson(res, r.ok ? 200 : 400, r);
|
|
861
|
+
return;
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
if (req.method === "GET" && action === "candidates") {
|
|
865
|
+
const c = await listConsolidationCandidates(skillsRoot);
|
|
866
|
+
sendJson(res, 200, { candidates: c });
|
|
867
|
+
return;
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
if (req.method === "POST" && action === "config") {
|
|
871
|
+
const patch = await readJsonBody(req);
|
|
872
|
+
deps.saveConfig(patch);
|
|
873
|
+
sendJson(res, 200, { ok: true, config: (await buildSnapshot(skillsRoot, config)).config });
|
|
874
|
+
return;
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
if (req.method === "POST" && action === "review") {
|
|
878
|
+
const r = await deps.triggerReview();
|
|
879
|
+
sendJson(res, r.ok ? 200 : 400, r);
|
|
880
|
+
return;
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
sendJson(res, 404, { ok: false, error: `unknown action: ${action}` });
|
|
884
|
+
} catch (error) {
|
|
885
|
+
sendJson(res, 500, { ok: false, error: String(error?.message ?? error) });
|
|
886
|
+
}
|
|
887
|
+
};
|
|
888
|
+
|
|
889
|
+
// 1. 挂载到 DSH 主 WebServer
|
|
890
|
+
ctx.inject(["webServer"], (webCtx) => {
|
|
891
|
+
webCtx.effect(() =>
|
|
892
|
+
webCtx.webServer.register({
|
|
893
|
+
kind: "prefix",
|
|
894
|
+
path: "/dsh-evolution",
|
|
895
|
+
handler: handleRequest,
|
|
896
|
+
}),
|
|
897
|
+
);
|
|
898
|
+
});
|
|
899
|
+
|
|
900
|
+
// 2. 同时在固定端口 18890 提供独立服务(供 DSH Go 的 Loopback 端口预览或 Tailscale 手机直连,不随 Desktop 随机端口变化)
|
|
901
|
+
ctx.effect(() => {
|
|
902
|
+
const fixedPort = Number(process.env.DSH_EVOLUTION_PORT || 18890);
|
|
903
|
+
const srv = http.createServer((req, res) => {
|
|
904
|
+
void handleRequest(req, res);
|
|
905
|
+
});
|
|
906
|
+
srv.on("error", () => {
|
|
907
|
+
/* 若端口已被其他 profile 占用则静默忽略 */
|
|
908
|
+
});
|
|
909
|
+
srv.listen(fixedPort, "0.0.0.0");
|
|
910
|
+
srv.unref?.();
|
|
911
|
+
return () => {
|
|
912
|
+
try {
|
|
913
|
+
srv.close();
|
|
914
|
+
} catch {
|
|
915
|
+
/* ignore */
|
|
916
|
+
}
|
|
917
|
+
};
|
|
918
|
+
});
|
|
919
|
+
}
|