@cloud411716/fancy-webnovel 4.0.3 → 4.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/material-library/README.md +24 -3
- package/docs/material-library/import.js +1 -1
- package/docs/material-library/prompt.md +15 -12
- package/lib/scraper-utils.js +11 -7
- package/lib/task-list.js +359 -0
- package/package.json +3 -2
- package/plugins/fancy-analyze/index.js +31 -3
- package/plugins/fancy-bootstrap/index.js +16 -3
- package/plugins/fancy-db/index.js +30 -3
- package/plugins/fancy-douyin/index.js +27 -8
- package/plugins/fancy-douyin/prompts/01-analysis-prompt.md +1 -1
- package/plugins/fancy-export/index.js +14 -3
- package/plugins/fancy-import/index.js +22 -3
- package/plugins/fancy-material/index.js +216 -50
- package/plugins/fancy-material/kinds.js +23 -0
- package/plugins/fancy-material/templates.js +64 -18
- package/plugins/fancy-material/validate.js +3 -3
- package/plugins/fancy-project/index.js +19 -3
- package/plugins/fancy-scan/index.js +301 -6
- package/plugins/fancy-scan/scripts/run-scan.js +14 -0
- package/plugins/fancy-scan/scripts/scrapers/fanqie-rank-scraper.js +163 -15
- package/plugins/fancy-scan/templates/platforms.js +20 -6
- package/plugins/fancy-scan/templates.js +10 -2
- package/plugins/fancy-scan/vision.js +388 -0
- package/plugins/fancy-story-builder/index.js +13 -3
- package/plugins/fancy-topic/index.js +37 -7
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* vision.js — 番茄「LLM 视觉模型扫描」的提示词、结果解析与 Markdown 渲染
|
|
3
|
+
*
|
|
4
|
+
* 这条链路和 API / OCR 的根本区别:
|
|
5
|
+
*
|
|
6
|
+
* api —— 番茄明文接口,scraper 直接拿到书名/作者/简介;
|
|
7
|
+
* ocr —— scraper 截图后在本机跑 tesseract;
|
|
8
|
+
* vision —— scraper **只截图**,把截图绝对路径写进 manifest,
|
|
9
|
+
* 由当前 LLM 用视觉能力识别,再由插件把识别结果写回标准 md。
|
|
10
|
+
*
|
|
11
|
+
* 本文件只放纯函数,方便单测;真正的 LLM 派活与等待在 fancy-scan/index.js。
|
|
12
|
+
*
|
|
13
|
+
* 两条硬约束(用户明确要求):
|
|
14
|
+
* 1) 视觉提示词必须让模型**先判断自己是否支持视觉识别**,
|
|
15
|
+
* 不支持就停止并告知用户;只能用视觉,禁止用 OCR 替代。
|
|
16
|
+
* 2) LLM 返回的书名/作者/简介必须走与晋江/番茄相同的 Markdown 特殊字符
|
|
17
|
+
* 清洗(sanitizeBookFields / renderIntroLines),不能直接拼进文件。
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import fs from 'fs';
|
|
21
|
+
import path from 'path';
|
|
22
|
+
|
|
23
|
+
import {
|
|
24
|
+
renderIntroLines,
|
|
25
|
+
sanitizeBookFields,
|
|
26
|
+
sanitizeMarkdownInline,
|
|
27
|
+
} from '#scraper-utils';
|
|
28
|
+
|
|
29
|
+
export const VISION_MANIFEST_VERSION = 1;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 从一段可能带 Markdown 代码块/前后说明的文字里取出第一个完整 JSON 对象。
|
|
33
|
+
*
|
|
34
|
+
* 模型写文件时经常不严格:```json 包裹、前面加一句“结果如下:”、
|
|
35
|
+
* 后面再补一段解释。直接 JSON.parse 会失败,但内容其实是好的。
|
|
36
|
+
*/
|
|
37
|
+
export function extractJsonObject(text) {
|
|
38
|
+
const src = String(text == null ? '' : text)
|
|
39
|
+
.replace(/^\uFEFF/, '')
|
|
40
|
+
.trim();
|
|
41
|
+
if (!src) throw new Error('结果文件为空');
|
|
42
|
+
|
|
43
|
+
// 优先去掉整段代码块围栏
|
|
44
|
+
const fenced = src.match(/```(?:json)?\s*([\s\S]*?)```/i);
|
|
45
|
+
const candidate = fenced ? fenced[1].trim() : src;
|
|
46
|
+
|
|
47
|
+
const start = candidate.indexOf('{');
|
|
48
|
+
if (start < 0) throw new Error('结果里没有 JSON 对象');
|
|
49
|
+
|
|
50
|
+
let depth = 0;
|
|
51
|
+
let inString = false;
|
|
52
|
+
let escaped = false;
|
|
53
|
+
for (let i = start; i < candidate.length; i += 1) {
|
|
54
|
+
const ch = candidate[i];
|
|
55
|
+
if (inString) {
|
|
56
|
+
if (escaped) escaped = false;
|
|
57
|
+
else if (ch === '\\') escaped = true;
|
|
58
|
+
else if (ch === '"') inString = false;
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (ch === '"') { inString = true; continue; }
|
|
62
|
+
if (ch === '{') depth += 1;
|
|
63
|
+
else if (ch === '}') {
|
|
64
|
+
depth -= 1;
|
|
65
|
+
if (depth === 0) {
|
|
66
|
+
const json = candidate.slice(start, i + 1);
|
|
67
|
+
try {
|
|
68
|
+
return JSON.parse(json);
|
|
69
|
+
} catch (e) {
|
|
70
|
+
throw new Error('JSON 解析失败:' + (e && e.message ? e.message : String(e)));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
throw new Error('JSON 对象没有闭合');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function asString(value) {
|
|
79
|
+
return String(value == null ? '' : value).trim();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 解析视觉识别结果。
|
|
84
|
+
*
|
|
85
|
+
* 支持两种形态:
|
|
86
|
+
* { "vision_supported": false, "reason": "..." }
|
|
87
|
+
* { "vision_supported": true, "books": [ ... ] }
|
|
88
|
+
*
|
|
89
|
+
* `vision_supported` 缺失但 books 是数组时按“支持”处理 —— 有些模型会
|
|
90
|
+
* 省略这个字段,但结果本身是完整的,没必要因此重跑一整轮。
|
|
91
|
+
*/
|
|
92
|
+
export function parseVisionResult(text) {
|
|
93
|
+
const data = typeof text === 'string' ? extractJsonObject(text) : text;
|
|
94
|
+
if (!data || typeof data !== 'object' || Array.isArray(data)) {
|
|
95
|
+
throw new Error('视觉结果必须是 JSON 对象');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const supportedRaw = data.vision_supported;
|
|
99
|
+
const supported = supportedRaw === false || String(supportedRaw).toLowerCase() === 'false'
|
|
100
|
+
? false
|
|
101
|
+
: true;
|
|
102
|
+
|
|
103
|
+
if (!supported) {
|
|
104
|
+
return {
|
|
105
|
+
visionSupported: false,
|
|
106
|
+
reason: asString(data.reason) || '当前模型不支持视觉识别',
|
|
107
|
+
books: [],
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (!Array.isArray(data.books)) {
|
|
112
|
+
throw new Error('视觉结果缺少 books 数组');
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const books = data.books.map((raw, index) => {
|
|
116
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) {
|
|
117
|
+
throw new Error(`books[${index}] 不是对象`);
|
|
118
|
+
}
|
|
119
|
+
return {
|
|
120
|
+
index,
|
|
121
|
+
bookId: asString(raw.bookId ?? raw.book_id ?? raw.id),
|
|
122
|
+
rank: asString(raw.rank ?? raw.index),
|
|
123
|
+
title: asString(raw.title ?? raw.bookName ?? raw.name),
|
|
124
|
+
author: asString(raw.author),
|
|
125
|
+
status: asString(raw.status),
|
|
126
|
+
desc: asString(raw.desc ?? raw.description ?? raw.abstract),
|
|
127
|
+
};
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
return { visionSupported: true, reason: '', books };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* 把 LLM 识别结果按 bookId(优先)或 rank 合并回 manifest 里的书。
|
|
135
|
+
*
|
|
136
|
+
* 识别失败/缺字段时的兜底必须与 OCR 路径一致:
|
|
137
|
+
* 书名 → `(bookId: <id>)`
|
|
138
|
+
* 作者 → `未知`
|
|
139
|
+
* 状态 → DOM 明文(番茄的状态本来就是明文,不需要识别)→ `未知`
|
|
140
|
+
* 简介 → 空字符串
|
|
141
|
+
*
|
|
142
|
+
* @returns {{books: object[], missing: string[]}}
|
|
143
|
+
*/
|
|
144
|
+
export function mergeVisionCategory(category, parsed) {
|
|
145
|
+
const books = Array.isArray(category && category.books) ? category.books : [];
|
|
146
|
+
const recognized = Array.isArray(parsed && parsed.books) ? parsed.books : [];
|
|
147
|
+
|
|
148
|
+
// 排名可能是 "01" 也可能是 "1"(模型经常按示例写成 1);
|
|
149
|
+
// 同时按原值和去前导零的值建索引,bookId 精度丢失时还能靠排名对上。
|
|
150
|
+
const normRank = (value) => {
|
|
151
|
+
const s = asString(value);
|
|
152
|
+
return /^\d+$/.test(s) ? String(Number(s)) : s;
|
|
153
|
+
};
|
|
154
|
+
const byId = new Map();
|
|
155
|
+
const byRank = new Map();
|
|
156
|
+
for (const item of recognized) {
|
|
157
|
+
if (item.bookId && !byId.has(item.bookId)) byId.set(item.bookId, item);
|
|
158
|
+
if (item.rank && !byRank.has(item.rank)) byRank.set(item.rank, item);
|
|
159
|
+
const normalized = normRank(item.rank);
|
|
160
|
+
if (normalized && !byRank.has(normalized)) byRank.set(normalized, item);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const used = new Set();
|
|
164
|
+
const merged = [];
|
|
165
|
+
const missing = [];
|
|
166
|
+
|
|
167
|
+
for (const raw of books) {
|
|
168
|
+
const bookId = asString(raw.bookId);
|
|
169
|
+
const rank = asString(raw.rank);
|
|
170
|
+
let hit = null;
|
|
171
|
+
if (bookId && byId.has(bookId)) hit = byId.get(bookId);
|
|
172
|
+
else if (rank && byRank.has(rank)) hit = byRank.get(rank);
|
|
173
|
+
else if (rank && byRank.has(normRank(rank))) hit = byRank.get(normRank(rank));
|
|
174
|
+
|
|
175
|
+
if (hit) used.add(hit);
|
|
176
|
+
else missing.push(bookId || rank || '未知条目');
|
|
177
|
+
|
|
178
|
+
merged.push({
|
|
179
|
+
rank,
|
|
180
|
+
bookId,
|
|
181
|
+
url: raw.url || '',
|
|
182
|
+
readerCount: raw.readerCount || raw.read_count || '',
|
|
183
|
+
rankChange: raw.rankChange || null,
|
|
184
|
+
title: (hit && hit.title) || `(bookId: ${bookId})`,
|
|
185
|
+
author: (hit && hit.author) || '未知',
|
|
186
|
+
// 番茄的状态在 DOM 里本来就是明文(不需要识别),DOM 有值就以 DOM 为准:
|
|
187
|
+
// 视觉模型偶尔会把“连载中”看成“已完结”,没必要让它覆盖确定的文本。
|
|
188
|
+
status: (raw.status && raw.status !== '未知' ? raw.status : '') || (hit && hit.status) || '未知',
|
|
189
|
+
desc: (hit && hit.desc) || '',
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return { books: merged, missing };
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* 视觉识别提示词。
|
|
198
|
+
*
|
|
199
|
+
* 用户要求的两句话必须逐字体现:
|
|
200
|
+
* - 先判断自己是否有视觉识别能力;
|
|
201
|
+
* - 没有就停止、禁止 OCR 替代、告知用户当前模型不支持视觉识别。
|
|
202
|
+
*
|
|
203
|
+
* 结果文件路径用绝对路径,且要求模型先覆盖写状态再写结果;
|
|
204
|
+
* 插件只认这个文件,不去猜模型在对话里说了什么。
|
|
205
|
+
*/
|
|
206
|
+
export function buildVisionPrompt({ manifest, category, categoryIndex, categoryCount, resultPath }) {
|
|
207
|
+
const books = Array.isArray(category && category.books) ? category.books : [];
|
|
208
|
+
// 截图目录是题材根目录(<截图根>/<题材>/),不是某本书的排名子目录。
|
|
209
|
+
// 用户示例里给的也是题材/书目目录,这里统一给题材根目录 + 每本书的绝对路径,
|
|
210
|
+
// 模型既知道整体范围,也不会认错某张图属于哪本书。
|
|
211
|
+
const firstScreen = books.find((b) => b && b.screen);
|
|
212
|
+
const screenshotDir = firstScreen
|
|
213
|
+
? path.dirname(path.dirname(firstScreen.screen))
|
|
214
|
+
: (manifest && manifest.screenshots_root) || '';
|
|
215
|
+
|
|
216
|
+
const lines = [];
|
|
217
|
+
lines.push('请执行番茄榜单截图的 LLM 视觉识别。');
|
|
218
|
+
lines.push('');
|
|
219
|
+
lines.push('## 第一步:能力自检(必须最先做)');
|
|
220
|
+
lines.push('');
|
|
221
|
+
lines.push('请先判断你当前使用的模型是否具备视觉识别能力:你是否能直接查看图片内容(用 read 工具读取图片后能真正看到画面)。');
|
|
222
|
+
lines.push('');
|
|
223
|
+
lines.push('- 如果**不具备**视觉识别能力:立即停止,不要读取截图,不要尝试 OCR、tesseract、像素识别或任何文字识别替代方案。');
|
|
224
|
+
lines.push(' 请把下面的状态 JSON 写入结果文件,然后结束本轮:');
|
|
225
|
+
lines.push(' `' + resultPath + '`');
|
|
226
|
+
lines.push(' ```json');
|
|
227
|
+
lines.push(' {"vision_supported": false, "reason": "当前模型不支持视觉识别"}');
|
|
228
|
+
lines.push(' ```');
|
|
229
|
+
lines.push(' 并在最终回复里明确告诉用户:**当前模型不支持视觉识别,无法完成截图识别**。');
|
|
230
|
+
lines.push('');
|
|
231
|
+
lines.push('- 如果具备视觉识别能力:继续第二步。');
|
|
232
|
+
lines.push('');
|
|
233
|
+
lines.push('## 第二步:识别截图');
|
|
234
|
+
lines.push('');
|
|
235
|
+
lines.push(`截图目录(绝对路径):\`${screenshotDir}\``);
|
|
236
|
+
lines.push('');
|
|
237
|
+
lines.push(`本批是第 ${categoryIndex + 1}/${categoryCount} 个题材:**${sanitizeMarkdownInline(category.name)}**,共 ${books.length} 本书。`);
|
|
238
|
+
lines.push('只识别本轮列出的书;不要合并、重复或参考之前批次的截图与结果。');
|
|
239
|
+
lines.push('请用 read 工具逐一读取以下截图(绝对路径),识别每本书的书名、作者、状态、简介:');
|
|
240
|
+
lines.push('');
|
|
241
|
+
books.forEach((book, i) => {
|
|
242
|
+
const meta = [];
|
|
243
|
+
if (book.rank) meta.push(`排名 ${book.rank}`);
|
|
244
|
+
if (book.bookId) meta.push(`bookId ${book.bookId}`);
|
|
245
|
+
if (book.readerCount) meta.push(`在读 ${book.readerCount}`);
|
|
246
|
+
if (book.rankChange) meta.push(`排名变动 ${book.rankChange}`);
|
|
247
|
+
const suffix = meta.length ? `(${meta.join(',')})` : '';
|
|
248
|
+
if (book.screen) {
|
|
249
|
+
lines.push(`${i + 1}. \`${book.screen}\`${suffix}`);
|
|
250
|
+
} else {
|
|
251
|
+
lines.push(`${i + 1}. (截图缺失)${suffix}`);
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
lines.push('');
|
|
255
|
+
lines.push('识别要求:');
|
|
256
|
+
lines.push('- 书名、作者、状态、简介按截图中显示的中文原文抄录;不要翻译、不要改写、不要编造。');
|
|
257
|
+
lines.push('- 这是抄录任务,不是校对任务:截图已经是 2x 高清,直接用 read 看原图即可。**不要用 bash/Python/PIL 裁剪、放大、拼接或逐行处理截图**,也不要反复放大核对;看不清的字按最接近的字写,实在看不清才留空。');
|
|
258
|
+
lines.push('- 看不清的字段填空字符串 `""`,不要猜。');
|
|
259
|
+
lines.push('- 禁止使用任何 OCR 方案(tesseract / pytesseract / 文字识别接口都不行),只能用你的视觉能力读图。');
|
|
260
|
+
lines.push('- 不要调用 `todo_write` 修改任务列表(本命令的进度由插件维护)。');
|
|
261
|
+
lines.push('- 读完截图后直接写结果文件,不要在对话里输出长篇推理或逐字校验过程。');
|
|
262
|
+
lines.push('');
|
|
263
|
+
lines.push('## 第三步:写结果文件');
|
|
264
|
+
lines.push('');
|
|
265
|
+
lines.push('把识别结果写入(覆盖)下面的绝对路径:');
|
|
266
|
+
lines.push('`' + resultPath + '`');
|
|
267
|
+
lines.push('');
|
|
268
|
+
lines.push('严格写成一个 JSON 对象,不要 Markdown 代码块、不要前后解释:');
|
|
269
|
+
lines.push('```json');
|
|
270
|
+
lines.push('{');
|
|
271
|
+
lines.push(' "vision_supported": true,');
|
|
272
|
+
lines.push(' "books": [');
|
|
273
|
+
lines.push(' {"bookId": "123", "rank": "1", "title": "书名", "author": "作者", "status": "连载中", "desc": "简介"}');
|
|
274
|
+
lines.push(' ]');
|
|
275
|
+
lines.push('}');
|
|
276
|
+
lines.push('```');
|
|
277
|
+
lines.push('');
|
|
278
|
+
lines.push(`books 必须覆盖上面列出的全部 ${books.length} 本书,顺序一致,bookId 使用上面给出的值。`);
|
|
279
|
+
lines.push('书名/作者/简介里的 Markdown 特殊字符不用处理,原样写进 JSON 即可,插件会统一清洗。');
|
|
280
|
+
lines.push('写完结果文件后结束本轮,不要对榜单做市场分析。');
|
|
281
|
+
return lines.join('\n');
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function renderBookLines(raw) {
|
|
285
|
+
const b = sanitizeBookFields(raw);
|
|
286
|
+
const rank = sanitizeMarkdownInline(b.rank);
|
|
287
|
+
const lines = [];
|
|
288
|
+
lines.push(`### #${rank} ${b.title}`);
|
|
289
|
+
lines.push(`- **作者:** ${b.author}`);
|
|
290
|
+
if (b.status) lines.push(`- **状态:** ${b.status}`);
|
|
291
|
+
const readers = b.readCount || b.readerCount;
|
|
292
|
+
if (readers) lines.push(`- **在读:** ${readers}`);
|
|
293
|
+
if (b.wordCount) lines.push(`- **字数:** ${b.wordCount}`);
|
|
294
|
+
if (b.tags && b.tags.length) lines.push(`- **标签:** ${b.tags.map(sanitizeMarkdownInline).join('、')}`);
|
|
295
|
+
if (b.rankChange) lines.push(`- **排名变动:** ${b.rankChange}`);
|
|
296
|
+
lines.push(...renderIntroLines(b.desc || ''));
|
|
297
|
+
if (b.url) {
|
|
298
|
+
const url = /^https?:\/\//i.test(b.url) ? b.url : `https://fanqienovel.com${b.url}`;
|
|
299
|
+
lines.push(`- [作品页](${url})`);
|
|
300
|
+
}
|
|
301
|
+
lines.push('');
|
|
302
|
+
return lines;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* 把 manifest 的题材元数据 + 每题材的识别结果渲染成标准扫榜 Markdown。
|
|
307
|
+
*
|
|
308
|
+
* 格式与 fanqie-rank-scraper.js 的 buildMarkdown 保持一致:
|
|
309
|
+
* 下游的 llmAnalysisPrompt / report-validate 都按这个结构解析。
|
|
310
|
+
*
|
|
311
|
+
* @param {object} manifest
|
|
312
|
+
* @param {Array<{name:string, books?:object[], error?:string}>} categoryResults
|
|
313
|
+
* @param {string} [date] YYYYMMDD
|
|
314
|
+
*/
|
|
315
|
+
export function renderVisionMarkdown(manifest, categoryResults, date) {
|
|
316
|
+
const chLabel = manifest.channel_label || (String(manifest.channel) === '1' ? '男频' : '女频');
|
|
317
|
+
const tyLabel = manifest.type_label || (String(manifest.type) === '2' ? '阅读榜' : '新书榜');
|
|
318
|
+
const categories = Array.isArray(categoryResults) ? categoryResults : [];
|
|
319
|
+
|
|
320
|
+
const lines = [];
|
|
321
|
+
lines.push(`# 番茄 · ${chLabel}${tyLabel} · 全 ${categories.length} 题材`);
|
|
322
|
+
lines.push('');
|
|
323
|
+
lines.push(`- 频道参数:channel=${manifest.channel},type=${manifest.type}`);
|
|
324
|
+
lines.push(`- 抓取时间:${manifest.created_at || new Date().toISOString()}`);
|
|
325
|
+
if (date) lines.push(`- 日期:${date}`);
|
|
326
|
+
lines.push(`- 每题材前${manifest.top || 10}本`);
|
|
327
|
+
lines.push('- 识别方式:LLM 视觉模型(截图识别;未使用 OCR)');
|
|
328
|
+
lines.push('');
|
|
329
|
+
lines.push('---');
|
|
330
|
+
lines.push('');
|
|
331
|
+
|
|
332
|
+
for (const cat of categories) {
|
|
333
|
+
const name = sanitizeMarkdownInline(cat && cat.name ? cat.name : '未分类');
|
|
334
|
+
if (cat && cat.error) {
|
|
335
|
+
lines.push(`## ${name} — 识别失败`, '');
|
|
336
|
+
lines.push(`- 失败原因:${sanitizeMarkdownInline(cat.error)}`);
|
|
337
|
+
lines.push('', '---', '');
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
const books = Array.isArray(cat && cat.books) ? cat.books : [];
|
|
341
|
+
lines.push(`## ${name} — ${books.length} 本`, '');
|
|
342
|
+
for (const book of books) lines.push(...renderBookLines(book));
|
|
343
|
+
lines.push('---', '');
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
return lines.join('\n');
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/** 标准数据文件名:与 API/OCR 路径完全一致,后续分析提示词不用区分来源。 */
|
|
350
|
+
export function visionMarkdownFileName(manifest, date) {
|
|
351
|
+
const chLabel = manifest.channel_label || (String(manifest.channel) === '1' ? '男频' : '女频');
|
|
352
|
+
const tyLabel = manifest.type_label || (String(manifest.type) === '2' ? '阅读榜' : '新书榜');
|
|
353
|
+
return `番茄${chLabel}${tyLabel}_${date}.md`;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* 每题材的识别结果文件。放在 扫榜结果/vision/results/ 下:
|
|
358
|
+
* - 与截图目录分开,模型不会把它误当成截图;
|
|
359
|
+
* - 留在项目里方便排查“模型到底识别成了什么”。
|
|
360
|
+
*/
|
|
361
|
+
export function visionResultPath(manifestPath, categoryIndex) {
|
|
362
|
+
const dir = path.join(path.dirname(manifestPath), 'results');
|
|
363
|
+
const base = path.basename(manifestPath).replace(/\.json$/i, '');
|
|
364
|
+
return path.join(dir, `${base}_cat${String(categoryIndex + 1).padStart(2, '0')}.json`);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** 读并做最小结构校验。manifest 是 scraper 自己写的,但仍按不可信输入处理。 */
|
|
368
|
+
export function readVisionManifest(manifestPath) {
|
|
369
|
+
let raw;
|
|
370
|
+
try {
|
|
371
|
+
raw = fs.readFileSync(manifestPath, 'utf-8');
|
|
372
|
+
} catch (e) {
|
|
373
|
+
throw new Error(`视觉清单读不出来:${e.message}`);
|
|
374
|
+
}
|
|
375
|
+
let data;
|
|
376
|
+
try {
|
|
377
|
+
data = JSON.parse(raw);
|
|
378
|
+
} catch (e) {
|
|
379
|
+
throw new Error(`视觉清单不是合法 JSON:${e.message}`);
|
|
380
|
+
}
|
|
381
|
+
if (!data || typeof data !== 'object' || Array.isArray(data)) {
|
|
382
|
+
throw new Error('视觉清单必须是 JSON 对象');
|
|
383
|
+
}
|
|
384
|
+
if (!Array.isArray(data.categories) || !data.categories.length) {
|
|
385
|
+
throw new Error('视觉清单里没有 categories');
|
|
386
|
+
}
|
|
387
|
+
return data;
|
|
388
|
+
}
|
|
@@ -25,6 +25,7 @@ import { execFile } from 'child_process';
|
|
|
25
25
|
import { join, dirname } from 'path';
|
|
26
26
|
import { fileURLToPath } from 'url';
|
|
27
27
|
import { askUserQuestion, llmFollowup, notify, spawnScript } from '#infra';
|
|
28
|
+
import { registerCommandWithTasks } from '#task-list';
|
|
28
29
|
import { runWithStoryDb, isStoryDbProject } from './db/stage-sync.js';
|
|
29
30
|
import { handleDbQuery, handleAnalyze as handleDbAnalyze } from './db/commands.js';
|
|
30
31
|
import { openStoryDb } from './db/client.js';
|
|
@@ -908,15 +909,21 @@ export function parseStoryBuilderCommand(input) {
|
|
|
908
909
|
return { ok: false, group: null, action, actionRaw: '', args: [], reason: '未知阶段:' + action };
|
|
909
910
|
}
|
|
910
911
|
|
|
912
|
+
function buildTaskTitles(invocation) {
|
|
913
|
+
const raw = String(invocation.rawInput || '').trim().toLowerCase();
|
|
914
|
+
if (raw.split(/\s+/)[0] === 'help') return ['显示故事构建帮助'];
|
|
915
|
+
return ['解析构建阶段', '预检上游依赖', '执行阶段任务', '汇总阶段结果'];
|
|
916
|
+
}
|
|
917
|
+
|
|
911
918
|
export async function apply(ctx) {
|
|
912
919
|
await registerNativeTools(ctx);
|
|
913
|
-
ctx
|
|
920
|
+
registerCommandWithTasks(ctx, {
|
|
914
921
|
name: 'fancy-build',
|
|
915
922
|
description: '📖 故事构建 ( usage: /fancy-build scale | macro | cast | rhythm | outline | write | deslop | help )',
|
|
916
923
|
input: {
|
|
917
924
|
hint: '.',
|
|
918
925
|
},
|
|
919
|
-
handler: async (invocation) => {
|
|
926
|
+
handler: async (invocation, taskList) => {
|
|
920
927
|
const session = invocation.agent.session;
|
|
921
928
|
const { signal } = invocation;
|
|
922
929
|
|
|
@@ -1011,6 +1018,9 @@ export async function apply(ctx) {
|
|
|
1011
1018
|
if (!preflight.ok) {
|
|
1012
1019
|
return notify(session, preflight.kind || 'error', storyBuilder.notify(preflight.notify));
|
|
1013
1020
|
}
|
|
1021
|
+
taskList.advance(2);
|
|
1022
|
+
// 阶段执行里会有多轮 LLM 派活;让任务列表跨 turn 保持可见。
|
|
1023
|
+
taskList.watchTurns(invocation);
|
|
1014
1024
|
|
|
1015
1025
|
// 2.0 各阶段都要求 LLM 把参考决策写进台账;先确保文件存在,
|
|
1016
1026
|
// 避免模型把“新建台账”当成额外任务或直接跳过。
|
|
@@ -1252,7 +1262,7 @@ export async function apply(ctx) {
|
|
|
1252
1262
|
return notify(session, 'error', storyBuilder.notify({ type: 'no_subcommand' }));
|
|
1253
1263
|
}
|
|
1254
1264
|
},
|
|
1255
|
-
});
|
|
1265
|
+
}, buildTaskTitles);
|
|
1256
1266
|
}
|
|
1257
1267
|
|
|
1258
1268
|
|
|
@@ -11,6 +11,7 @@ import { existsSync, lstatSync, readdirSync, readFileSync, realpathSync, mkdirSy
|
|
|
11
11
|
import { join, resolve } from 'path';
|
|
12
12
|
import { fileURLToPath } from 'url';
|
|
13
13
|
import { askUserQuestion, llmFollowup, notify, spawnScript } from '#infra';
|
|
14
|
+
import { registerCommandWithTasks } from '#task-list';
|
|
14
15
|
import { readDeployedState, deployedStateProblemText, DEPLOYED_STATUS } from '#deployed-state';
|
|
15
16
|
import { repairTopicDecision, selectNonRedundantBlocks, inspectBlockBodies } from '#topic-decision';
|
|
16
17
|
import { isSensitiveProjectPath, isValidProjectPath } from '#path-guard';
|
|
@@ -304,7 +305,7 @@ function platformEqual(a, b) {
|
|
|
304
305
|
// 路径 A:探索模式
|
|
305
306
|
// ---------------------------------------------------------------------------
|
|
306
307
|
|
|
307
|
-
async function pathExplore(ctx, session, invocation, signal, projectRoot) {
|
|
308
|
+
async function pathExplore(ctx, session, invocation, signal, projectRoot, tasks) {
|
|
308
309
|
// 路径 A 不再调用 LLM:
|
|
309
310
|
// 选 topic_decision 文件 → 选该文件里已有的平台 → 选候选方向 → 写 suggestion。
|
|
310
311
|
const scanDir = join(projectRoot, '扫榜结果');
|
|
@@ -376,6 +377,7 @@ async function pathExplore(ctx, session, invocation, signal, projectRoot) {
|
|
|
376
377
|
}));
|
|
377
378
|
}
|
|
378
379
|
|
|
380
|
+
tasks.advance(1);
|
|
379
381
|
const { platforms } = parseDecisionCandidates(content);
|
|
380
382
|
if (!platforms.length) {
|
|
381
383
|
return notify(session, 'error', topic.notify({
|
|
@@ -400,6 +402,7 @@ async function pathExplore(ctx, session, invocation, signal, projectRoot) {
|
|
|
400
402
|
return notify(session, 'error', topic.notify({ type: 'cancelled' }));
|
|
401
403
|
}
|
|
402
404
|
|
|
405
|
+
tasks.advance(2);
|
|
403
406
|
if (signal?.aborted) {
|
|
404
407
|
return notify(session, 'error', topic.notify({ type: 'cancelled' }));
|
|
405
408
|
}
|
|
@@ -433,6 +436,7 @@ async function pathExplore(ctx, session, invocation, signal, projectRoot) {
|
|
|
433
436
|
}));
|
|
434
437
|
}
|
|
435
438
|
|
|
439
|
+
tasks.advance(3);
|
|
436
440
|
const outPath = join(projectRoot, NOVEL_SUGGESTION_FILE);
|
|
437
441
|
try {
|
|
438
442
|
atomicWriteJSON(outPath, validated.normalized);
|
|
@@ -457,7 +461,7 @@ async function pathExplore(ctx, session, invocation, signal, projectRoot) {
|
|
|
457
461
|
// 路径 B:敲定模式
|
|
458
462
|
// ---------------------------------------------------------------------------
|
|
459
463
|
|
|
460
|
-
async function pathSetup(ctx, session, invocation, signal, projectRoot) {
|
|
464
|
+
async function pathSetup(ctx, session, invocation, signal, projectRoot, tasks) {
|
|
461
465
|
const suggestion = readSuggestion(projectRoot);
|
|
462
466
|
if (!suggestion) {
|
|
463
467
|
return notify(session, 'error', topic.notify({ type: 'no_suggestion' }));
|
|
@@ -483,6 +487,7 @@ async function pathSetup(ctx, session, invocation, signal, projectRoot) {
|
|
|
483
487
|
}));
|
|
484
488
|
}
|
|
485
489
|
|
|
490
|
+
tasks.advance(1);
|
|
486
491
|
// 没有能力/剧情素材时,不让 LLM 凭空编 5 个方案。
|
|
487
492
|
let counts = {};
|
|
488
493
|
try {
|
|
@@ -499,6 +504,7 @@ async function pathSetup(ctx, session, invocation, signal, projectRoot) {
|
|
|
499
504
|
}));
|
|
500
505
|
}
|
|
501
506
|
|
|
507
|
+
tasks.advance(2);
|
|
502
508
|
const prompt = topic.setupPrompt({
|
|
503
509
|
suggestion,
|
|
504
510
|
queryScript: tools.queryScript,
|
|
@@ -519,6 +525,11 @@ async function pathSetup(ctx, session, invocation, signal, projectRoot) {
|
|
|
519
525
|
err: (follow && follow.error) || '提示词投递失败',
|
|
520
526
|
}));
|
|
521
527
|
}
|
|
528
|
+
// 这是 fire-and-forget:命令立即返回,LLM 在下一个 turn 生成 5 个方案。
|
|
529
|
+
// 任务列表要跨过这个 turn,等方案生成完再标成“等待用户确认”。
|
|
530
|
+
tasks.detachUntilTurnEnd(invocation, (t) => {
|
|
531
|
+
t.advance(3);
|
|
532
|
+
});
|
|
522
533
|
return notify(session, 'success', topic.notify({
|
|
523
534
|
type: 'setup_started',
|
|
524
535
|
genre: suggestion.genre,
|
|
@@ -642,15 +653,34 @@ async function registerFinalizeTool(ctx) {
|
|
|
642
653
|
}));
|
|
643
654
|
}
|
|
644
655
|
|
|
656
|
+
function topicTaskTitles(invocation) {
|
|
657
|
+
const raw = String(invocation.rawInput || '').trim().toLowerCase();
|
|
658
|
+
if (raw === 'help') return ['显示选题帮助'];
|
|
659
|
+
if (raw === 'setup') {
|
|
660
|
+
return [
|
|
661
|
+
'检查工作区与 phase',
|
|
662
|
+
'读取题材建议与素材',
|
|
663
|
+
'生成 5 个选题方案',
|
|
664
|
+
'等待用户确认方案',
|
|
665
|
+
];
|
|
666
|
+
}
|
|
667
|
+
return [
|
|
668
|
+
'检查工作区与 phase',
|
|
669
|
+
'读取扫榜结果与候选方向',
|
|
670
|
+
'选择文件、平台与方向',
|
|
671
|
+
'写入选题建议',
|
|
672
|
+
];
|
|
673
|
+
}
|
|
674
|
+
|
|
645
675
|
export async function apply(ctx) {
|
|
646
676
|
await registerFinalizeTool(ctx);
|
|
647
|
-
ctx
|
|
677
|
+
registerCommandWithTasks(ctx, {
|
|
648
678
|
name: 'fancy-topic',
|
|
649
679
|
description: '📝 选题与创意约束 ( usage: /fancy-topic [setup] | help )',
|
|
650
680
|
input: {
|
|
651
681
|
hint: '.',
|
|
652
682
|
},
|
|
653
|
-
handler: async (invocation) => {
|
|
683
|
+
handler: async (invocation, tasks) => {
|
|
654
684
|
const session = invocation.agent.session;
|
|
655
685
|
const { signal } = invocation;
|
|
656
686
|
|
|
@@ -735,10 +765,10 @@ export async function apply(ctx) {
|
|
|
735
765
|
|
|
736
766
|
// 4. 路径分发
|
|
737
767
|
if (isSetupPath) {
|
|
738
|
-
return pathSetup(ctx, session, invocation, signal, projectRootAbs);
|
|
768
|
+
return pathSetup(ctx, session, invocation, signal, projectRootAbs, tasks);
|
|
739
769
|
} else {
|
|
740
|
-
return pathExplore(ctx, session, invocation, signal, projectRootAbs);
|
|
770
|
+
return pathExplore(ctx, session, invocation, signal, projectRootAbs, tasks);
|
|
741
771
|
}
|
|
742
772
|
},
|
|
743
|
-
});
|
|
773
|
+
}, topicTaskTitles);
|
|
744
774
|
}
|