@cloud411716/fancy-webnovel 4.0.0 → 4.0.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/README.md +5 -0
- package/lib/scraper-utils.js +34 -1
- package/lib/workspace.js +28 -0
- package/package.json +1 -1
- package/plugins/fancy-scan/scripts/scrapers/fanqie-rank-scraper.js +12 -2
- package/plugins/fancy-topic/index.js +18 -1
- package/plugins/fancy-topic/prompts/02-setup-prompt.md +3 -2
- package/plugins/fancy-topic/templates.js +12 -1
package/README.md
CHANGED
|
@@ -37,6 +37,11 @@ allowBuilds:
|
|
|
37
37
|
|
|
38
38
|
然后重新执行安装。`tesseract.js` 的 postinstall 只是 OpenCollective 提示;不允许构建时 OCR 模式不可用,但插件其余功能不受影响。
|
|
39
39
|
|
|
40
|
+
番茄 OCR 模式会优先使用本地 `chi_sim.traineddata`,不会默认走 jsdelivr 下载:
|
|
41
|
+
依次查找 `FANCY_TESSDATA_DIR`、`$DSH_HOME`、`~/.dsh`、工作区 `.fancy/tessdata`、
|
|
42
|
+
工作区 `tessdata` 和系统 tessdata 目录。DSH 沙箱里外网请求经常是 `fetch failed`,
|
|
43
|
+
把 `chi_sim.traineddata` 放到上述任一目录即可离线 OCR。
|
|
44
|
+
|
|
40
45
|
## 依赖解析
|
|
41
46
|
|
|
42
47
|
依赖声明在 `dependencies` / `peerDependencies` 中。用 `dsh plugin add` 从 npm 安装时,
|
package/lib/scraper-utils.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* scraper-utils.js — 采集脚本公共工具
|
|
3
3
|
*
|
|
4
|
-
* 提供各平台 scraper
|
|
4
|
+
* 提供各平台 scraper 公用的纯函数;除 tesseract 本地语言包定位外不依赖外部服务。
|
|
5
5
|
*/
|
|
6
|
+
import fs from 'fs';
|
|
7
|
+
import os from 'os';
|
|
8
|
+
import path from 'path';
|
|
6
9
|
|
|
7
10
|
export function getArg(args, name) {
|
|
8
11
|
const i = args.indexOf(name);
|
|
@@ -13,6 +16,36 @@ export function getArg(args, name) {
|
|
|
13
16
|
return inline === undefined ? null : String(inline).slice(prefix.length);
|
|
14
17
|
}
|
|
15
18
|
|
|
19
|
+
/**
|
|
20
|
+
* 找本地 chi_sim.traineddata,避免 tesseract.js 默认从 jsdelivr 下载。
|
|
21
|
+
*
|
|
22
|
+
* DSH 的 bash 沙箱通常只放行工作区/本机文件,外网请求会直接 fetch failed。
|
|
23
|
+
* ~/.dsh 下已经有安装时带的 chi_sim.traineddata,直接把它作为 langPath +
|
|
24
|
+
* cachePath 即可:worker 先读 cache,读到了就不会再走网络。
|
|
25
|
+
*
|
|
26
|
+
* 支持 chi_sim.traineddata(未压缩)和 chi_sim.traineddata.gz(压缩)。
|
|
27
|
+
*
|
|
28
|
+
* @returns {{dir:string,gzip:boolean}|null}
|
|
29
|
+
*/
|
|
30
|
+
export function resolveTessdata() {
|
|
31
|
+
const home = process.env.HOME || os.homedir();
|
|
32
|
+
const dshHome = process.env.DSH_HOME || path.join(home, '.dsh');
|
|
33
|
+
const candidates = [
|
|
34
|
+
process.env.FANCY_TESSDATA_DIR,
|
|
35
|
+
dshHome,
|
|
36
|
+
path.join(process.cwd(), '.fancy', 'tessdata'),
|
|
37
|
+
path.join(process.cwd(), 'tessdata'),
|
|
38
|
+
'/usr/share/tesseract-ocr/5/tessdata',
|
|
39
|
+
'/usr/share/tesseract-ocr/4.00/tessdata',
|
|
40
|
+
'/usr/share/tessdata',
|
|
41
|
+
].filter(Boolean);
|
|
42
|
+
for (const dir of candidates) {
|
|
43
|
+
if (fs.existsSync(path.join(dir, 'chi_sim.traineddata'))) return { dir, gzip: false };
|
|
44
|
+
if (fs.existsSync(path.join(dir, 'chi_sim.traineddata.gz'))) return { dir, gzip: true };
|
|
45
|
+
}
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
|
|
16
49
|
// ---------------------------------------------------------------------------
|
|
17
50
|
// 扫描方式(取数路径)
|
|
18
51
|
// ---------------------------------------------------------------------------
|
package/lib/workspace.js
CHANGED
|
@@ -286,6 +286,34 @@ export function registerGlobalWorkspace(root, opts = {}) {
|
|
|
286
286
|
return entry;
|
|
287
287
|
}
|
|
288
288
|
|
|
289
|
+
/**
|
|
290
|
+
* Remove a workspace from the machine-level registry.
|
|
291
|
+
*
|
|
292
|
+
* Test helpers create temporary workspaces through `initWorkspace`; without
|
|
293
|
+
* unregistering, every run leaves a stale `/tmp/fancy-ws-test-*` entry behind.
|
|
294
|
+
* Also moves the legacy pointer if it still targets the removed workspace.
|
|
295
|
+
*
|
|
296
|
+
* @param {string} root workspace directory
|
|
297
|
+
* @returns {boolean} true when an entry was removed
|
|
298
|
+
*/
|
|
299
|
+
export function unregisterGlobalWorkspace(root) {
|
|
300
|
+
const workspacePath = path.resolve(root);
|
|
301
|
+
const registry = readGlobalRegistry();
|
|
302
|
+
const remaining = registry.workspaces.filter((entry) => entry.workspace !== workspacePath);
|
|
303
|
+
const removed = remaining.length !== registry.workspaces.length;
|
|
304
|
+
if (removed) writeGlobalRegistry({ ...registry, workspaces: remaining });
|
|
305
|
+
|
|
306
|
+
const pointer = readGlobalPointer();
|
|
307
|
+
if (pointer && pointer.workspace === workspacePath) {
|
|
308
|
+
if (remaining.length) {
|
|
309
|
+
writeGlobalPointer(remaining[0].workspace);
|
|
310
|
+
} else {
|
|
311
|
+
try { fs.unlinkSync(globalPointerPath()); } catch { /* already gone */ }
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return removed;
|
|
315
|
+
}
|
|
316
|
+
|
|
289
317
|
export function listGlobalWorkspaces() {
|
|
290
318
|
return readGlobalRegistry().workspaces
|
|
291
319
|
.map((entry) => ({ ...entry, exists: isWorkspace(entry.workspace) }))
|
package/package.json
CHANGED
|
@@ -26,7 +26,7 @@ import path from 'path';
|
|
|
26
26
|
import { pathToFileURL } from 'url';
|
|
27
27
|
import { chromium } from 'playwright-core';
|
|
28
28
|
import Tesseract from 'tesseract.js';
|
|
29
|
-
import { getArg, localDateStamp, runCli, normalizeScanMode, SCAN_MODE_API, renderIntroLines, sanitizeBookFields, sanitizeMarkdownInline } from '#scraper-utils';
|
|
29
|
+
import { getArg, localDateStamp, runCli, normalizeScanMode, SCAN_MODE_API, renderIntroLines, sanitizeBookFields, sanitizeMarkdownInline, resolveTessdata } from '#scraper-utils';
|
|
30
30
|
|
|
31
31
|
// 捕获未处理的 promise 错误——只写 stderr,让上层重试逻辑处理,不要直接退出
|
|
32
32
|
process.on('unhandledRejection', (reason) => {
|
|
@@ -181,7 +181,17 @@ async function resetOcrWorker() {
|
|
|
181
181
|
async function getOcrWorker() {
|
|
182
182
|
if (_ocrWorker) return _ocrWorker;
|
|
183
183
|
if (_ocrInitPromise) return _ocrInitPromise;
|
|
184
|
-
const
|
|
184
|
+
const tessdata = resolveTessdata();
|
|
185
|
+
if (!tessdata) {
|
|
186
|
+
process.stderr.write('[ocr] 未找到本地 chi_sim.traineddata,将尝试在线下载;如果网络被沙箱拦截,请把文件放到 ~/.dsh/ 或设置 FANCY_TESSDATA_DIR\n');
|
|
187
|
+
}
|
|
188
|
+
const init = Tesseract.createWorker('chi_sim', 1, tessdata
|
|
189
|
+
? {
|
|
190
|
+
langPath: tessdata.dir,
|
|
191
|
+
cachePath: tessdata.dir,
|
|
192
|
+
gzip: tessdata.gzip,
|
|
193
|
+
}
|
|
194
|
+
: {});
|
|
185
195
|
_ocrInitPromise = (async () => {
|
|
186
196
|
try {
|
|
187
197
|
const w = await withTimeout(init, OCR_INIT_TIMEOUT_MS, 'OCR worker 初始化');
|
|
@@ -15,11 +15,12 @@ import { readDeployedState, deployedStateProblemText, DEPLOYED_STATUS } from '#d
|
|
|
15
15
|
import { repairTopicDecision, selectNonRedundantBlocks, inspectBlockBodies } from '#topic-decision';
|
|
16
16
|
import { isSensitiveProjectPath, isValidProjectPath } from '#path-guard';
|
|
17
17
|
import { parseDecisionCandidates } from '#topic-candidates';
|
|
18
|
-
import { NOVEL_SUGGESTION_FILE, validateSuggestion, validateMinimalTopic, readSuggestion } from '#material-topic';
|
|
18
|
+
import { NOVEL_SUGGESTION_FILE, validateSuggestion, validateMinimalTopic, readSuggestion, ABILITY_KINDS, PLOT_KINDS } from '#material-topic';
|
|
19
19
|
import { atomicWriteJSON } from '#atomic-write';
|
|
20
20
|
import { resolveWorkspace } from '#workspace';
|
|
21
21
|
import { topic } from './templates.js';
|
|
22
22
|
import { deployMaterialTools } from '../fancy-material/tool-deploy.js';
|
|
23
|
+
import { countByKind } from '../fancy-material/inventory.js';
|
|
23
24
|
|
|
24
25
|
export const inject = ['commands', 'userQuestions', 'tools'];
|
|
25
26
|
|
|
@@ -482,6 +483,22 @@ async function pathSetup(ctx, session, invocation, signal, projectRoot) {
|
|
|
482
483
|
}));
|
|
483
484
|
}
|
|
484
485
|
|
|
486
|
+
// 没有能力/剧情素材时,不让 LLM 凭空编 5 个方案。
|
|
487
|
+
let counts = {};
|
|
488
|
+
try {
|
|
489
|
+
counts = await countByKind(projectRoot, [suggestion.genre]);
|
|
490
|
+
} catch { /* 查询失败时继续,让提示词里的查询给出具体错误 */ }
|
|
491
|
+
const abilityTotal = ABILITY_KINDS.reduce((sum, kind) => sum + (Number(counts[kind]) || 0), 0);
|
|
492
|
+
const plotTotal = PLOT_KINDS.reduce((sum, kind) => sum + (Number(counts[kind]) || 0), 0);
|
|
493
|
+
if (!abilityTotal || !plotTotal) {
|
|
494
|
+
return notify(session, 'error', topic.notify({
|
|
495
|
+
type: 'material_empty',
|
|
496
|
+
genre: suggestion.genre,
|
|
497
|
+
abilityTotal,
|
|
498
|
+
plotTotal,
|
|
499
|
+
}));
|
|
500
|
+
}
|
|
501
|
+
|
|
485
502
|
const prompt = topic.setupPrompt({
|
|
486
503
|
suggestion,
|
|
487
504
|
queryScript: tools.queryScript,
|
|
@@ -30,8 +30,9 @@ node '${QUERY_SCRIPT}' --workspace '${WORKSPACE}' --genre '${GENRE}' --kind <kin
|
|
|
30
30
|
1. 每个 kind 至少查询一次;用 `--json` 读取结果。
|
|
31
31
|
2. 只使用素材库里真实存在的素材;不要编造素材名或能力名。
|
|
32
32
|
3. 如果某个 kind 在该题材下没有结果,可以去掉 `--genre` 再查一次,优先选通用素材,并在方案里说明。
|
|
33
|
-
4.
|
|
34
|
-
5.
|
|
33
|
+
4. 如果某一组 kind 整体查不到任何素材,停止并告诉用户先运行 `/fancy-material 能力` 或 `/fancy-material 剧情` 补齐;不要编造素材。
|
|
34
|
+
5. 组合素材时保持因果自洽:能力机制、发动条件、代价、限制、反制、力量来源要能互相对上;剧情与脑洞要能落成具体冲突、反转或爽点。
|
|
35
|
+
6. 不要修改、删除 `.fancy-tools/fancy-material/` 下的脚本。
|
|
35
36
|
|
|
36
37
|
## 三、生成 5 个差异化方案
|
|
37
38
|
|
|
@@ -405,7 +405,7 @@ export const topic = {
|
|
|
405
405
|
/**
|
|
406
406
|
* 通知模板(左侧消息栏)
|
|
407
407
|
*/
|
|
408
|
-
notify({ type, platform, phase, decisionFile, blockCount, fieldCount, missing, err, projectRoot, extra, available, notes, file, reasons, arg, direction, genre, suggestionPath }) {
|
|
408
|
+
notify({ type, platform, phase, decisionFile, blockCount, fieldCount, missing, err, projectRoot, extra, available, notes, file, reasons, arg, direction, genre, suggestionPath, abilityTotal, plotTotal }) {
|
|
409
409
|
switch (type) {
|
|
410
410
|
case 'help':
|
|
411
411
|
return `📝 选题 用法
|
|
@@ -526,6 +526,17 @@ export const topic = {
|
|
|
526
526
|
case 'material_not_initialized':
|
|
527
527
|
return '⚠️ 当前工作区还没有初始化素材库。\n请先运行 /fancy-bootstrap 初始化工作区,或运行 /fancy-material init 补齐素材库 schema。';
|
|
528
528
|
|
|
529
|
+
case 'material_empty': {
|
|
530
|
+
const missing = [];
|
|
531
|
+
if (!abilityTotal) missing.push('能力类素材');
|
|
532
|
+
if (!plotTotal) missing.push('剧情与脑洞类素材');
|
|
533
|
+
return `⚠️ 素材库中「${genre || ''}」题材缺少${missing.join('和')}。\n` +
|
|
534
|
+
'请先补齐素材,再运行 /fancy-topic setup:\n' +
|
|
535
|
+
'- /fancy-material 能力\n' +
|
|
536
|
+
'- /fancy-material 剧情\n' +
|
|
537
|
+
'补齐后再回来生成 5 个候选方案。';
|
|
538
|
+
}
|
|
539
|
+
|
|
529
540
|
case 'setup_started':
|
|
530
541
|
return `✅ 已把素材驱动选题任务交给 LLM\n` +
|
|
531
542
|
`🏷️ 题材:${genre || ''}\n` +
|