@cloud411716/fancy-webnovel 4.27.2 → 4.28.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/douyin-plugin.md +9 -1
- package/docs/material-library/README.md +3 -1
- package/docs/material-library/prompt.md +7 -4
- package/docs/material-library/schema.surql +40 -1
- package/lib/material-db.js +9 -2
- package/package.json +1 -1
- package/plugins/fancy-bootstrap/index.js +1 -1
- package/plugins/fancy-db/index.js +1 -1
- package/plugins/fancy-douyin/index.js +83 -39
- package/plugins/fancy-douyin/state.js +24 -1
- package/plugins/fancy-douyin/style-distill.js +197 -0
- package/plugins/fancy-douyin/templates.js +8 -1
- package/plugins/fancy-material/builtin-styles.js +448 -0
- package/plugins/fancy-material/index.js +92 -8
- package/plugins/fancy-material/styles.js +441 -0
- package/plugins/fancy-material/templates.js +12 -0
package/docs/douyin-plugin.md
CHANGED
|
@@ -184,7 +184,15 @@ id / kind / subkind / name / tags / genres / note / source
|
|
|
184
184
|
|
|
185
185
|
## 任务状态与恢复
|
|
186
186
|
|
|
187
|
-
素材库 schema v12 新增 `douyin_account` 账号表;schema v11 新增多层素材 facet,并保留 `douyin_job` 和 `douyin_meta` 任务表。账号、任务状态与素材在同一个数据库里,备份/迁移整个素材库时会一起带走。
|
|
187
|
+
素材库 schema v13 新增 `douyin_style` 账号风格表;schema v12 新增 `douyin_account` 账号表;schema v11 新增多层素材 facet,并保留 `douyin_job` 和 `douyin_meta` 任务表。账号、任务状态与素材在同一个数据库里,备份/迁移整个素材库时会一起带走。
|
|
188
|
+
|
|
189
|
+
### 账号风格(douyin_style)
|
|
190
|
+
|
|
191
|
+
`douyin_style` 一个账号可以有多条记录(`style_key` 唯一),保存从作品提炼出的风格 DNA、命名规则、词汇域、跨 kind 示例、反例和题材兼容度。
|
|
192
|
+
|
|
193
|
+
- 每次采集结束、作品全部入库后,fancy-douyin 会按账号读取已有风格,用新作品做增量 patch,只写 `douyin_style`,不删除素材。
|
|
194
|
+
- `/fancy-douyin list` 会显示账号的 `style_key`、风格名、DNA 和示例,用户可直接把 key 传给 `/fancy-material --style <key>`。
|
|
195
|
+
- 内置风格在 `plugins/fancy-material/builtin-styles.js`;fancy-material 会把内置风格和数据库风格按 `style_key` 合并,数据库版本优先。
|
|
188
196
|
|
|
189
197
|
`/fancy-douyin add`、`list`、`remove` 现在直接读写 `douyin_account`。旧 `accounts.json` 只在首次运行时迁移一次,之后不再作为运行时状态源。
|
|
190
198
|
|
|
@@ -355,7 +355,9 @@ node plugins/fancy-material/scripts/query.js \
|
|
|
355
355
|
| `material_kind_name_unique` | `kind, name` | 防止同名同类型重复 |
|
|
356
356
|
| `material_name_search` | `name` | 中文 ngram 全文检索 |
|
|
357
357
|
|
|
358
|
-
素材库 schema
|
|
358
|
+
素材库 schema v13 还包含 `douyin_style` 账号风格表、`douyin_account` 账号表和 `douyin_job` 任务表;账号、作品采集/分析/导入状态和素材在同一个数据库里,完整备份/迁移素材库时会一起带走。`douyin_meta` 保存一次性迁移标记。
|
|
359
|
+
|
|
360
|
+
`douyin_style` 一个账号可以有多条风格记录(`style_key` 唯一),保存风格 DNA、命名规则、词汇域、跨 kind 示例、反例和题材兼容度。内置风格在 `plugins/fancy-material/builtin-styles.js`,运行 `/fancy-material` 时会和数据库风格合并;同 `style_key` 以数据库版本优先。`/fancy-douyin list` 会显示账号可用的 style_key、风格名和示例。
|
|
359
361
|
|
|
360
362
|
中文全文检索:
|
|
361
363
|
|
|
@@ -18,6 +18,8 @@
|
|
|
18
18
|
|
|
19
19
|
若指定了题材风格,在不违背以上原则的前提下遵循对应题材的专属创意规则与命名体例。当前风格规则:{{STYLE_RULES}}
|
|
20
20
|
|
|
21
|
+
{{STYLE_BLOCK}}
|
|
22
|
+
|
|
21
23
|
---
|
|
22
24
|
|
|
23
25
|
## 二、原子素材基础模型
|
|
@@ -77,10 +79,11 @@
|
|
|
77
79
|
|
|
78
80
|
### 4.1 总规则:体例后缀必须匹配题材文化体系
|
|
79
81
|
|
|
80
|
-
1.
|
|
81
|
-
2.
|
|
82
|
-
3.
|
|
83
|
-
4.
|
|
82
|
+
1. **作者风格优先**:如果「风格锁定」中选中了账号风格,先满足该风格的长度、句式、修辞与禁用项;题材体例只在风格没有覆盖时兜底。
|
|
83
|
+
2. **主题材决定命名体系**:以第一个题材为核心命名风格基准,其他题材融入核心逻辑,不乱用词缀。
|
|
84
|
+
3. **混合题材适配逻辑**:如「都市+西方哲学+高武」,主风格用都市高武的凝练感,核心意象用西方哲学/宗教元素,禁用中式“诀/式/法”后缀。
|
|
85
|
+
4. **禁止跨体系串用**:西方奇幻禁用中式武侠后缀,古代题材禁用科幻术语,现代题材禁用古奥生僻词。
|
|
86
|
+
5. **都市类二重身份**:地名、势力名、能力名优先写内层隐秘身份;表层日常伪装名写进 `note`。两层身份必须有逻辑关联,不能各写各的。
|
|
84
87
|
|
|
85
88
|
### 4.2 分题材命名体例对照表
|
|
86
89
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
-- 网文创作素材库 · 原子素材 schema
|
|
1
|
+
-- 网文创作素材库 · 原子素材 schema v13(SurrealDB 2.3+)
|
|
2
2
|
-- 用法:
|
|
3
3
|
-- surreal sql --endpoint http://127.0.0.1:8000 --user root --pass ... \
|
|
4
4
|
-- --namespace fancy --database material_library --multi < schema.surql
|
|
@@ -139,3 +139,42 @@ DEFINE FIELD OVERWRITE updated_at ON douyin_meta TYPE datetime VALUE time::now()
|
|
|
139
139
|
DEFINE INDEX IF NOT EXISTS douyin_meta_key_unique ON douyin_meta FIELDS meta_key UNIQUE;
|
|
140
140
|
|
|
141
141
|
ALTER TABLE douyin_meta PERMISSIONS FOR select, create, update, delete FULL;
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
-- ---------------------------------------------------------------------------
|
|
145
|
+
-- 抖音账号风格(v13)
|
|
146
|
+
--
|
|
147
|
+
-- 一个账号允许多条风格记录(style_key 唯一);内置风格在代码里,
|
|
148
|
+
-- 这里只存从作品提炼/增量 patch 的动态风格。
|
|
149
|
+
-- ---------------------------------------------------------------------------
|
|
150
|
+
DEFINE TABLE IF NOT EXISTS douyin_style SCHEMAFULL;
|
|
151
|
+
|
|
152
|
+
DEFINE FIELD OVERWRITE style_key ON douyin_style TYPE string;
|
|
153
|
+
DEFINE FIELD OVERWRITE account_url ON douyin_style TYPE string;
|
|
154
|
+
DEFINE FIELD OVERWRITE account_name ON douyin_style TYPE option<string>;
|
|
155
|
+
DEFINE FIELD OVERWRITE style_name ON douyin_style TYPE string;
|
|
156
|
+
DEFINE FIELD OVERWRITE style_type ON douyin_style TYPE string DEFAULT 'naming'
|
|
157
|
+
ASSERT $value IN ['naming', 'structure', 'teaching', 'visual'];
|
|
158
|
+
DEFINE FIELD OVERWRITE genre_tags ON douyin_style TYPE array<string> DEFAULT [];
|
|
159
|
+
DEFINE FIELD OVERWRITE dna ON douyin_style TYPE string DEFAULT '';
|
|
160
|
+
DEFINE FIELD OVERWRITE naming_rules ON douyin_style TYPE object DEFAULT {};
|
|
161
|
+
DEFINE FIELD OVERWRITE lexicon ON douyin_style TYPE object DEFAULT {};
|
|
162
|
+
DEFINE FIELD OVERWRITE cross_kind_examples ON douyin_style TYPE object DEFAULT {};
|
|
163
|
+
DEFINE FIELD OVERWRITE anti_examples ON douyin_style TYPE array<string> DEFAULT [];
|
|
164
|
+
DEFINE FIELD OVERWRITE compatibility ON douyin_style TYPE object DEFAULT {};
|
|
165
|
+
DEFINE FIELD OVERWRITE confidence ON douyin_style TYPE number DEFAULT 0;
|
|
166
|
+
DEFINE FIELD OVERWRITE source_works ON douyin_style TYPE int DEFAULT 0;
|
|
167
|
+
DEFINE FIELD OVERWRITE source_materials ON douyin_style TYPE int DEFAULT 0;
|
|
168
|
+
DEFINE FIELD OVERWRITE version ON douyin_style TYPE int DEFAULT 1;
|
|
169
|
+
DEFINE FIELD OVERWRITE status ON douyin_style TYPE string DEFAULT 'active'
|
|
170
|
+
ASSERT $value IN ['active', 'draft', 'failed'];
|
|
171
|
+
DEFINE FIELD OVERWRITE profile_json ON douyin_style TYPE object DEFAULT {};
|
|
172
|
+
DEFINE FIELD OVERWRITE created_at ON douyin_style TYPE datetime DEFAULT time::now();
|
|
173
|
+
DEFINE FIELD OVERWRITE updated_at ON douyin_style TYPE datetime VALUE time::now();
|
|
174
|
+
|
|
175
|
+
DEFINE INDEX IF NOT EXISTS douyin_style_key_unique ON douyin_style FIELDS style_key UNIQUE;
|
|
176
|
+
DEFINE INDEX IF NOT EXISTS douyin_style_account ON douyin_style FIELDS account_url;
|
|
177
|
+
DEFINE INDEX IF NOT EXISTS douyin_style_genres ON douyin_style FIELDS genre_tags;
|
|
178
|
+
DEFINE INDEX IF NOT EXISTS douyin_style_updated_at ON douyin_style FIELDS updated_at;
|
|
179
|
+
|
|
180
|
+
ALTER TABLE douyin_style PERMISSIONS FOR select, create, update, delete FULL;
|
package/lib/material-db.js
CHANGED
|
@@ -17,7 +17,7 @@ import { readSecrets, readWorkspace } from './workspace.js';
|
|
|
17
17
|
import { rpcUrl, startServer } from './surreal-server.js';
|
|
18
18
|
|
|
19
19
|
export const MATERIAL_DATABASE = 'material_library';
|
|
20
|
-
export const MATERIAL_SCHEMA_VERSION =
|
|
20
|
+
export const MATERIAL_SCHEMA_VERSION = 13;
|
|
21
21
|
|
|
22
22
|
const SCHEMA_FILE = fileURLToPath(new URL('../docs/material-library/schema.surql', import.meta.url));
|
|
23
23
|
|
|
@@ -61,7 +61,7 @@ export async function connectMaterial(root) {
|
|
|
61
61
|
/**
|
|
62
62
|
* Ensure the material database and its schema exist. Idempotent.
|
|
63
63
|
*
|
|
64
|
-
* Schema v12 adds the douyin_account table; v11 adds the multi-layer material facets (`subject`, `tropes`, `narrative`, `traits`, `roles`, `relations`, `moods`, `settings`) and keeps the v10 douyin task tables / v9 subkind / v8 note assertion / v7 group_id / 58-kind catalog. Before applying the
|
|
64
|
+
* Schema v13 adds the douyin_style table; v12 adds the douyin_account table; v11 adds the multi-layer material facets (`subject`, `tropes`, `narrative`, `traits`, `roles`, `relations`, `moods`, `settings`) and keeps the v10 douyin task tables / v9 subkind / v8 note assertion / v7 group_id / 58-kind catalog. Before applying the
|
|
65
65
|
* new ASSERT, rows belonging to retired kinds are removed; otherwise SurrealDB
|
|
66
66
|
* refuses to overwrite the field because existing records no longer conform.
|
|
67
67
|
*
|
|
@@ -116,6 +116,13 @@ export async function ensureMaterialDatabase(root) {
|
|
|
116
116
|
if (!accountTable.fields || !accountTable.fields.account_url || !accountTable.fields.added_at) {
|
|
117
117
|
throw new Error('素材库 schema 初始化后仍未找到 douyin_account 账号字段。');
|
|
118
118
|
}
|
|
119
|
+
const styleInfo = await db.query('INFO FOR TABLE douyin_style;');
|
|
120
|
+
const styleTable = Array.isArray(styleInfo) && styleInfo[0] ? styleInfo[0] : {};
|
|
121
|
+
if (!styleTable.fields || !styleTable.fields.style_key || !styleTable.fields.account_url
|
|
122
|
+
|| !styleTable.fields.style_name || !styleTable.fields.style_type
|
|
123
|
+
|| !styleTable.fields.profile_json || !styleTable.fields.naming_rules) {
|
|
124
|
+
throw new Error('素材库 schema 初始化后仍未找到 douyin_style 风格字段。');
|
|
125
|
+
}
|
|
119
126
|
return {
|
|
120
127
|
ok: true,
|
|
121
128
|
namespace,
|
package/package.json
CHANGED
|
@@ -304,7 +304,7 @@ export async function apply(ctx) {
|
|
|
304
304
|
formatWorkspace(result.workspace),
|
|
305
305
|
'',
|
|
306
306
|
'数据库:schema ' + result.schema_version + ' · validation ' + result.validation_version,
|
|
307
|
-
'素材库:' + (result.material?.database || 'material_library') + '(schema ' + (result.material?.schema_version ||
|
|
307
|
+
'素材库:' + (result.material?.database || 'material_library') + '(schema ' + (result.material?.schema_version || 13) + ')',
|
|
308
308
|
'服务:' + (result.server.running ? '运行中' : '未运行'),
|
|
309
309
|
...(result.started && result.started.endpoint_changed
|
|
310
310
|
? ['⚠️ 原端口被另一个 SurrealDB 占用,已改用:' + result.started.endpoint]
|
|
@@ -151,7 +151,7 @@ export async function apply(ctx) {
|
|
|
151
151
|
'✅ 工作区数据库初始化完成',
|
|
152
152
|
'服务:' + (result.server.running ? '运行中' : '未运行'),
|
|
153
153
|
'schema:' + result.schema_version + ' validation:' + result.validation_version,
|
|
154
|
-
'素材库:' + (result.material?.database || 'material_library') + '(schema ' + (result.material?.schema_version ||
|
|
154
|
+
'素材库:' + (result.material?.database || 'material_library') + '(schema ' + (result.material?.schema_version || 13) + ')',
|
|
155
155
|
'项目库:' + result.projects.length + ' 个' + (failed.length ? ',失败 ' + failed.length + ' 个' : ''),
|
|
156
156
|
...failed.map((f) => '❌ ' + f.project.name + ':' + f.error),
|
|
157
157
|
].join('\n'));
|
|
@@ -19,6 +19,8 @@ import { readJsonFile, resolveWorkspace } from '#workspace';
|
|
|
19
19
|
import { connectMaterial, ensureMaterialDatabase } from '#material-db';
|
|
20
20
|
import { MATERIAL_FACET_FIELDS } from '../fancy-material/facets.js';
|
|
21
21
|
import { deployMaterialTools } from '../fancy-material/tool-deploy.js';
|
|
22
|
+
import { loadStyleRegistry, stylesForAccount, upsertStyleProfiles } from '../fancy-material/styles.js';
|
|
23
|
+
import { loadStyleSamplesByAccount, parseStyleDistillResult, styleDistillPrompt, styleResultPath, removeStyleResult } from './style-distill.js';
|
|
22
24
|
import { makeWaitForTurn } from '../fancy-story-builder/writing-driver.js';
|
|
23
25
|
import { deployDouyinTools } from './deploy.js';
|
|
24
26
|
import {
|
|
@@ -316,13 +318,6 @@ function retryPrompt(target, detail) {
|
|
|
316
318
|
].join('\n');
|
|
317
319
|
}
|
|
318
320
|
|
|
319
|
-
export function expectedItemCount(title) {
|
|
320
|
-
const match = String(title || '').match(/(\d+)\s*\+?\s*(?:个|种|条)/);
|
|
321
|
-
if (!match) return 0;
|
|
322
|
-
const count = Number(match[1]);
|
|
323
|
-
return Number.isFinite(count) && count >= 5 ? count : 0;
|
|
324
|
-
}
|
|
325
|
-
|
|
326
321
|
const CJK_RE = /[\u3400-\u9fff\uf900-\ufaff]/;
|
|
327
322
|
|
|
328
323
|
/**
|
|
@@ -346,28 +341,6 @@ export function kindLanguageProblem(file) {
|
|
|
346
341
|
+ '不要写 color_word、person_appearance 这类英文内部标识。请改写后重新写入同一个文件。';
|
|
347
342
|
}
|
|
348
343
|
|
|
349
|
-
/**
|
|
350
|
-
* 清单类作品(标题里写“100个/100种”)必须按条目数量逐条提取,
|
|
351
|
-
* 并额外产出 10 条同 kind + 同 subkind 的扩写素材。
|
|
352
|
-
* 只有格式校验不够:模型可能只提炼了 20 条创作句,却完全漏掉原清单。
|
|
353
|
-
*/
|
|
354
|
-
export function coverageProblem(file, title) {
|
|
355
|
-
const expected = expectedItemCount(title);
|
|
356
|
-
if (!expected) return '';
|
|
357
|
-
const required = expected + 10;
|
|
358
|
-
let records;
|
|
359
|
-
try {
|
|
360
|
-
records = readJsonl(file);
|
|
361
|
-
} catch {
|
|
362
|
-
return '';
|
|
363
|
-
}
|
|
364
|
-
if (records.length >= required) return '';
|
|
365
|
-
return '作品标题写着 ' + expected + ' 条清单,加上同 kind + 同 subkind 的 10 条扩写素材,'
|
|
366
|
-
+ '至少应有 ' + required + ' 条,但当前只有 ' + records.length
|
|
367
|
-
+ ' 条。清单类内容必须逐条输出:name 保留原文条目,不要只提炼一部分或改写成意象句;'
|
|
368
|
-
+ '补全原始条目和 10 条扩写素材后,重新写入同一个文件。';
|
|
369
|
-
}
|
|
370
|
-
|
|
371
344
|
function parseFirstJsonObject(text) {
|
|
372
345
|
let cleaned = String(text || '').replace(/^\uFEFF/, '').trim();
|
|
373
346
|
cleaned = cleaned
|
|
@@ -522,7 +495,7 @@ async function detectCapabilities(ctx, invocation, session, taskList, root, runD
|
|
|
522
495
|
return { ok: false, error: 'LLM 没有返回可解析的能力自检结果' };
|
|
523
496
|
}
|
|
524
497
|
|
|
525
|
-
async function finalizeMaterialFile({ root, target,
|
|
498
|
+
async function finalizeMaterialFile({ root, target, materialTools }) {
|
|
526
499
|
const skip = readSkipMarker(target);
|
|
527
500
|
if (skip) {
|
|
528
501
|
return {
|
|
@@ -542,8 +515,6 @@ async function finalizeMaterialFile({ root, target, title, materialTools }) {
|
|
|
542
515
|
if (!checked.ok) return { ok: false, detail: checked.detail };
|
|
543
516
|
const kindProblem = kindLanguageProblem(target);
|
|
544
517
|
if (kindProblem) return { ok: false, detail: kindProblem };
|
|
545
|
-
const coverage = coverageProblem(target, title);
|
|
546
|
-
if (coverage) return { ok: false, detail: coverage };
|
|
547
518
|
try {
|
|
548
519
|
const result = await importOtherMaterials(root, target);
|
|
549
520
|
return { ok: true, ...result };
|
|
@@ -553,7 +524,7 @@ async function finalizeMaterialFile({ root, target, title, materialTools }) {
|
|
|
553
524
|
}
|
|
554
525
|
|
|
555
526
|
async function analyzeMaterials(ctx, invocation, session, {
|
|
556
|
-
root, prompt, target,
|
|
527
|
+
root, prompt, target, materialTools, taskList,
|
|
557
528
|
}) {
|
|
558
529
|
const waitForTurn = makeWaitForTurn(invocation, {
|
|
559
530
|
timeoutMs: ANALYSIS_TIMEOUT_MS,
|
|
@@ -594,7 +565,7 @@ async function analyzeMaterials(ctx, invocation, session, {
|
|
|
594
565
|
continue;
|
|
595
566
|
}
|
|
596
567
|
|
|
597
|
-
const finalized = await finalizeMaterialFile({ root, target,
|
|
568
|
+
const finalized = await finalizeMaterialFile({ root, target, materialTools });
|
|
598
569
|
if (finalized.ok) return finalized;
|
|
599
570
|
if (finalized.error) return finalized;
|
|
600
571
|
detail = finalized.detail;
|
|
@@ -618,7 +589,6 @@ async function analyzeNote(ctx, invocation, session, {
|
|
|
618
589
|
root,
|
|
619
590
|
prompt,
|
|
620
591
|
target,
|
|
621
|
-
title: note.title,
|
|
622
592
|
materialTools,
|
|
623
593
|
taskList,
|
|
624
594
|
});
|
|
@@ -689,7 +659,6 @@ async function analyzeVideo(ctx, invocation, session, {
|
|
|
689
659
|
root,
|
|
690
660
|
prompt,
|
|
691
661
|
target,
|
|
692
|
-
title: video.title,
|
|
693
662
|
materialTools,
|
|
694
663
|
taskList,
|
|
695
664
|
});
|
|
@@ -710,7 +679,6 @@ async function analyzeArticle(ctx, invocation, session, {
|
|
|
710
679
|
root,
|
|
711
680
|
prompt,
|
|
712
681
|
target: target,
|
|
713
|
-
title: article.title,
|
|
714
682
|
materialTools,
|
|
715
683
|
taskList,
|
|
716
684
|
});
|
|
@@ -919,6 +887,58 @@ function materialFileReady(dir) {
|
|
|
919
887
|
}
|
|
920
888
|
}
|
|
921
889
|
|
|
890
|
+
/**
|
|
891
|
+
* 账号作品全部入库后,按账号提炼/增量 patch 风格。
|
|
892
|
+
* 失败只记日志,不影响采集主流程;只写 douyin_style,不删除任何素材。
|
|
893
|
+
*/
|
|
894
|
+
async function distillStylesForRun({
|
|
895
|
+
root, db, accounts, styleAccounts, runDir, invocation, taskList, signal,
|
|
896
|
+
}) {
|
|
897
|
+
if (!styleAccounts.size) return [];
|
|
898
|
+
const samplesByAccount = await loadStyleSamplesByAccount(db);
|
|
899
|
+
const registry = await loadStyleRegistry(root, { db });
|
|
900
|
+
const results = [];
|
|
901
|
+
for (const account of accounts) {
|
|
902
|
+
if (signal && signal.aborted) break;
|
|
903
|
+
if (!styleAccounts.has(account.url)) continue;
|
|
904
|
+
const samples = samplesByAccount.get(account.url);
|
|
905
|
+
if (!samples || !samples.sourceMaterials) continue;
|
|
906
|
+
const existing = stylesForAccount(registry.styles, account);
|
|
907
|
+
const resultPath = styleResultPath(runDir, account);
|
|
908
|
+
fs.mkdirSync(path.dirname(resultPath), { recursive: true });
|
|
909
|
+
removeStyleResult(resultPath);
|
|
910
|
+
try {
|
|
911
|
+
taskList.progressAt(5, `提炼账号风格(${account.name || account.url})`);
|
|
912
|
+
const prompt = styleDistillPrompt({ account, samples, existing, resultPath });
|
|
913
|
+
const sent = await llmFollowup(invocation, prompt, { appendTempRule: false });
|
|
914
|
+
if (!sent || sent.ok !== true) {
|
|
915
|
+
results.push({ account: account.url, ok: false, error: (sent && sent.error) || '提示词投递失败' });
|
|
916
|
+
continue;
|
|
917
|
+
}
|
|
918
|
+
taskList.watchTurns(invocation);
|
|
919
|
+
const wait = makeWaitForTurn(invocation, {
|
|
920
|
+
timeoutMs: 5 * 60 * 1000,
|
|
921
|
+
idleGraceMs: 60 * 1000,
|
|
922
|
+
signal,
|
|
923
|
+
});
|
|
924
|
+
const waited = await wait([resultPath]);
|
|
925
|
+
if (!waited || !waited.ok) {
|
|
926
|
+
results.push({ account: account.url, ok: false, error: (waited && (waited.reason || waited.code)) || '等待风格提炼结果失败' });
|
|
927
|
+
continue;
|
|
928
|
+
}
|
|
929
|
+
const profiles = parseStyleDistillResult(fs.readFileSync(resultPath, 'utf8'));
|
|
930
|
+
const saved = await upsertStyleProfiles(db, profiles, { account });
|
|
931
|
+
results.push({ account: account.url, ok: true, styles: saved.map((style) => style.style_key) });
|
|
932
|
+
} catch (err) {
|
|
933
|
+
results.push({ account: account.url, ok: false, error: err && err.message ? err.message : String(err) });
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
for (const result of results.filter((item) => !item.ok)) {
|
|
937
|
+
console.error(`[style-distill] ${result.account}: ${result.error}`);
|
|
938
|
+
}
|
|
939
|
+
return results;
|
|
940
|
+
}
|
|
941
|
+
|
|
922
942
|
async function runCrawl(ctx, invocation, session, taskList) {
|
|
923
943
|
const root = workspaceOf(session);
|
|
924
944
|
const paths = ensureDouyinDir(root);
|
|
@@ -1050,6 +1070,8 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
1050
1070
|
|
|
1051
1071
|
queue = useSubagents ? createJobQueue() : null;
|
|
1052
1072
|
const queueStats = { queued: 0, finished: 0 };
|
|
1073
|
+
const styleAccounts = new Map();
|
|
1074
|
+
let styleResults = [];
|
|
1053
1075
|
let scannedAccounts = 0;
|
|
1054
1076
|
let foundNotes = 0;
|
|
1055
1077
|
let foundVideos = 0;
|
|
@@ -1090,7 +1112,6 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
1090
1112
|
analyzed = await finalizeMaterialFile({
|
|
1091
1113
|
root,
|
|
1092
1114
|
target: job.target,
|
|
1093
|
-
title: job.title,
|
|
1094
1115
|
materialTools,
|
|
1095
1116
|
});
|
|
1096
1117
|
}
|
|
@@ -1129,6 +1150,7 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
1129
1150
|
} else {
|
|
1130
1151
|
inserted += analyzed.inserted || 0;
|
|
1131
1152
|
skippedMaterials += analyzed.skipped || 0;
|
|
1153
|
+
if (job.accountUrl) styleAccounts.set(job.accountUrl, (styleAccounts.get(job.accountUrl) || 0) + Math.max(1, analyzed.inserted || 0));
|
|
1132
1154
|
if (job.jobKey) {
|
|
1133
1155
|
await updateJob(jobStore.db, job.jobKey, {
|
|
1134
1156
|
analysis_status: 'analyzed',
|
|
@@ -1726,6 +1748,16 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
1726
1748
|
}
|
|
1727
1749
|
}
|
|
1728
1750
|
|
|
1751
|
+
if (!aborted && styleAccounts.size && process.env.FANCY_DOUYIN_STYLE_DISTILL !== '0') {
|
|
1752
|
+
try {
|
|
1753
|
+
styleResults = await distillStylesForRun({
|
|
1754
|
+
root, db: jobStore.db, accounts, styleAccounts, runDir, invocation, taskList, signal: jobSignal,
|
|
1755
|
+
});
|
|
1756
|
+
} catch (err) {
|
|
1757
|
+
console.error('[style-distill] 提炼账号风格失败:' + (err && err.message ? err.message : String(err)));
|
|
1758
|
+
}
|
|
1759
|
+
}
|
|
1760
|
+
|
|
1729
1761
|
taskList.advance(5);
|
|
1730
1762
|
return notify(session, (aborted || failures.length) ? 'error' : 'success', summaryText({
|
|
1731
1763
|
accounts: accounts.length,
|
|
@@ -1748,6 +1780,7 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
1748
1780
|
mode: browser.mode,
|
|
1749
1781
|
analysisMode: useSubagents ? 'subagent' : 'single',
|
|
1750
1782
|
subagentWorkers,
|
|
1783
|
+
stylesUpdated: styleResults.filter((item) => item.ok).flatMap((item) => item.styles || []),
|
|
1751
1784
|
}));
|
|
1752
1785
|
} finally {
|
|
1753
1786
|
if (queue) {
|
|
@@ -1793,7 +1826,18 @@ async function runList(session) {
|
|
|
1793
1826
|
const accounts = await listAccounts(store.db);
|
|
1794
1827
|
await migrateCrawledJobs(store.db, root, accounts);
|
|
1795
1828
|
counts = await countJobsByAccount(store.db);
|
|
1796
|
-
|
|
1829
|
+
let stylesByAccount = new Map();
|
|
1830
|
+
try {
|
|
1831
|
+
const registry = await loadStyleRegistry(root, { db: store.db });
|
|
1832
|
+
for (const account of accounts) {
|
|
1833
|
+
const styles = stylesForAccount(registry.styles, account);
|
|
1834
|
+
if (styles.length) {
|
|
1835
|
+
stylesByAccount.set(account.url, styles);
|
|
1836
|
+
if (account.name) stylesByAccount.set(account.name, styles);
|
|
1837
|
+
}
|
|
1838
|
+
}
|
|
1839
|
+
} catch { /* 风格表未初始化时仍显示账号列表 */ }
|
|
1840
|
+
return notify(session, 'success', accountListText(accounts, counts, stylesByAccount));
|
|
1797
1841
|
} finally {
|
|
1798
1842
|
await store.close();
|
|
1799
1843
|
}
|
|
@@ -196,7 +196,7 @@ export function readCrawled(root) {
|
|
|
196
196
|
return value && typeof value === 'object' && !Array.isArray(value) ? value : {};
|
|
197
197
|
}
|
|
198
198
|
|
|
199
|
-
export function accountListText(accountsOrRoot, countsOverride = null) {
|
|
199
|
+
export function accountListText(accountsOrRoot, countsOverride = null, stylesByAccount = new Map()) {
|
|
200
200
|
const accounts = Array.isArray(accountsOrRoot) ? accountsOrRoot : readAccounts(accountsOrRoot);
|
|
201
201
|
if (!accounts.length) {
|
|
202
202
|
return [
|
|
@@ -223,6 +223,29 @@ export function accountListText(accountsOrRoot, countsOverride = null) {
|
|
|
223
223
|
lines.push(`${index + 1}. ${item.url}(未获取账号名/抖音号)`);
|
|
224
224
|
}
|
|
225
225
|
lines.push(` 已采集 ${count} 篇`);
|
|
226
|
+
const styles = stylesByAccount.get(item.url) || stylesByAccount.get(item.name) || [];
|
|
227
|
+
if (!styles.length) {
|
|
228
|
+
lines.push(' 风格:未提炼(运行一次作品分析后自动生成)');
|
|
229
|
+
} else {
|
|
230
|
+
lines.push(' 风格:');
|
|
231
|
+
for (const style of styles.slice(0, 8)) {
|
|
232
|
+
const genres = Array.isArray(style.genre_tags) && style.genre_tags.length
|
|
233
|
+
? '|' + style.genre_tags.join('、')
|
|
234
|
+
: '';
|
|
235
|
+
lines.push(` - ${style.style_key}(${style.style_name})${genres}`);
|
|
236
|
+
if (style.dna) lines.push(' ' + style.dna);
|
|
237
|
+
const examples = [];
|
|
238
|
+
for (const values of Object.values(style.cross_kind_examples || {})) {
|
|
239
|
+
if (!Array.isArray(values)) continue;
|
|
240
|
+
for (const value of values) {
|
|
241
|
+
if (value && !examples.includes(value)) examples.push(value);
|
|
242
|
+
if (examples.length >= 3) break;
|
|
243
|
+
}
|
|
244
|
+
if (examples.length >= 3) break;
|
|
245
|
+
}
|
|
246
|
+
if (examples.length) lines.push(' 示例:' + examples.join('、'));
|
|
247
|
+
}
|
|
248
|
+
}
|
|
226
249
|
});
|
|
227
250
|
return lines.join('\n');
|
|
228
251
|
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 从账号已入库作品中提炼/增量 patch 风格。
|
|
3
|
+
*
|
|
4
|
+
* 这里只负责:
|
|
5
|
+
* 1. 从 douyin_job + material 采样;
|
|
6
|
+
* 2. 生成结构化风格提炼提示词;
|
|
7
|
+
* 3. 解析 LLM 返回的 styles[];
|
|
8
|
+
* 4. 交给 fancy-material/styles.js 的 upsertStyleProfiles 增量写入。
|
|
9
|
+
*
|
|
10
|
+
* 不删除任何素材;只写 douyin_style。
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import fs from 'node:fs';
|
|
14
|
+
import path from 'node:path';
|
|
15
|
+
|
|
16
|
+
function unique(values) {
|
|
17
|
+
return [...new Set(values.filter(Boolean))];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function stripJsonFence(text) {
|
|
21
|
+
return String(text || '')
|
|
22
|
+
.replace(/^\uFEFF/, '')
|
|
23
|
+
.trim()
|
|
24
|
+
.replace(/^```(?:json)?\s*/i, '')
|
|
25
|
+
.replace(/\s*```$/, '')
|
|
26
|
+
.trim();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function parseStyleDistillResult(text) {
|
|
30
|
+
const src = stripJsonFence(text);
|
|
31
|
+
let data;
|
|
32
|
+
try {
|
|
33
|
+
data = JSON.parse(src);
|
|
34
|
+
} catch {
|
|
35
|
+
const start = src.indexOf('{');
|
|
36
|
+
const end = src.lastIndexOf('}');
|
|
37
|
+
if (start < 0 || end <= start) throw new Error('风格提炼结果不是 JSON 对象');
|
|
38
|
+
data = JSON.parse(src.slice(start, end + 1));
|
|
39
|
+
}
|
|
40
|
+
const styles = Array.isArray(data) ? data : data.styles;
|
|
41
|
+
if (!Array.isArray(styles) || !styles.length) throw new Error('风格提炼结果缺少 styles 数组');
|
|
42
|
+
return styles;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function styleDistillPrompt({ account, samples, existing = [], resultPath }) {
|
|
46
|
+
const kinds = samples && samples.kinds ? samples.kinds : {};
|
|
47
|
+
const titles = (samples && samples.titles) || [];
|
|
48
|
+
const lines = [
|
|
49
|
+
'你是抖音账号风格提炼器。你只分析账号已入库的作品标题和素材名,提炼可跨 kind 使用的风格,不写新素材。',
|
|
50
|
+
'',
|
|
51
|
+
'## 账号',
|
|
52
|
+
`- 名称:${account.name || '未命名'}`,
|
|
53
|
+
`- 地址:${account.url || ''}`,
|
|
54
|
+
`- 已导入作品:${samples?.sourceWorks ?? 0} 篇`,
|
|
55
|
+
`- 已入库素材:${samples?.sourceMaterials ?? 0} 条`,
|
|
56
|
+
'',
|
|
57
|
+
'## 已有风格(用于增量 patch;没有则为空)',
|
|
58
|
+
'```json',
|
|
59
|
+
JSON.stringify(existing, null, 2),
|
|
60
|
+
'```',
|
|
61
|
+
'',
|
|
62
|
+
'## 作品标题样本',
|
|
63
|
+
...titles.slice(0, 30).map((title) => '- ' + title),
|
|
64
|
+
'',
|
|
65
|
+
'## 素材名样本(按 kind 分组)',
|
|
66
|
+
];
|
|
67
|
+
for (const [kind, names] of Object.entries(kinds)) {
|
|
68
|
+
lines.push(`### ${kind}`);
|
|
69
|
+
lines.push((names || []).slice(0, 30).join('、') || '(无)');
|
|
70
|
+
lines.push('');
|
|
71
|
+
}
|
|
72
|
+
lines.push(
|
|
73
|
+
'## 提炼目标',
|
|
74
|
+
'提炼 1–3 个**跨 kind 的风格核心**,不是给单个 kind 分类。每个风格要能同时解释:地名、势力、功法/能力、物品、怪物、等级、规则等不同 kind 的写法。',
|
|
75
|
+
'如果账号明显是教学/视觉资产库而不是小说命名风格,style_type 写 teaching 或 visual,并在 dna 里说明。',
|
|
76
|
+
'',
|
|
77
|
+
'## 输出格式(只输出 JSON,不要 Markdown 解释)',
|
|
78
|
+
'```json',
|
|
79
|
+
JSON.stringify({
|
|
80
|
+
styles: [{
|
|
81
|
+
style_key: 'stable_ascii_slug',
|
|
82
|
+
style_name: '中文风格名',
|
|
83
|
+
style_type: 'naming',
|
|
84
|
+
genre_tags: ['仙侠', '古风'],
|
|
85
|
+
dna: '一句话风格 DNA',
|
|
86
|
+
naming_rules: {
|
|
87
|
+
length: [2, 4],
|
|
88
|
+
syntax: ['意象+意象', '动词+宾语'],
|
|
89
|
+
suffixes: [],
|
|
90
|
+
forbidden: ['秘境', '令', '诀'],
|
|
91
|
+
tone: '冷、克制',
|
|
92
|
+
},
|
|
93
|
+
lexicon: { preferred: ['灯', '骨'], forbidden: ['系统'] },
|
|
94
|
+
cross_kind_examples: {
|
|
95
|
+
place_name: ['无灯巷'],
|
|
96
|
+
faction_name: ['无面祠'],
|
|
97
|
+
skill_name: ['问骨'],
|
|
98
|
+
item_name: ['无字灯'],
|
|
99
|
+
monster_name: ['背灯人'],
|
|
100
|
+
rank_name: ['点灯'],
|
|
101
|
+
world_rule: ['灯灭之前,不能回头。'],
|
|
102
|
+
},
|
|
103
|
+
anti_examples: ['无灯巷秘境'],
|
|
104
|
+
compatibility: { 仙侠: 'native', 都市: 'adaptable' },
|
|
105
|
+
confidence: 0.8,
|
|
106
|
+
source_works: samples?.sourceWorks ?? 0,
|
|
107
|
+
source_materials: samples?.sourceMaterials ?? 0,
|
|
108
|
+
}],
|
|
109
|
+
}, null, 2),
|
|
110
|
+
'```',
|
|
111
|
+
'',
|
|
112
|
+
'规则:',
|
|
113
|
+
'- 每个 style_key 用稳定的小写 ASCII;已有风格沿用原 style_key,只补新增例子/词汇/规则。',
|
|
114
|
+
'- 不要照抄作品标题;示例要短,能体现命名语法。',
|
|
115
|
+
'- 风格必须跨 kind 成立;不要只写“这个账号会写地名”。',
|
|
116
|
+
'- 没有把握的风格不要编造;宁少勿多。',
|
|
117
|
+
'',
|
|
118
|
+
`把结果写入:\`${resultPath}\``,
|
|
119
|
+
);
|
|
120
|
+
return lines.join('\n');
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* 一次读取 jobs + material,按账号聚合采样。
|
|
125
|
+
* 只读,不修改素材。
|
|
126
|
+
*/
|
|
127
|
+
export async function loadStyleSamplesByAccount(db, { maxNamesPerKind = 30, maxTitles = 30 } = {}) {
|
|
128
|
+
const jobResult = await db.query(
|
|
129
|
+
"SELECT account_url, work_id, title FROM douyin_job WHERE import_status = 'imported';",
|
|
130
|
+
);
|
|
131
|
+
const jobRows = Array.isArray(jobResult) && Array.isArray(jobResult[0]) ? jobResult[0] : [];
|
|
132
|
+
const materialResult = await db.query(
|
|
133
|
+
"SELECT id, kind, subkind, name FROM material WHERE source = 'other';",
|
|
134
|
+
);
|
|
135
|
+
const materialRows = Array.isArray(materialResult) && Array.isArray(materialResult[0]) ? materialResult[0] : [];
|
|
136
|
+
return groupStyleSamples(jobRows, materialRows, { maxNamesPerKind, maxTitles });
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function groupStyleSamples(jobRows, materialRows, { maxNamesPerKind = 30, maxTitles = 30 } = {}) {
|
|
140
|
+
const jobByWork = new Map();
|
|
141
|
+
for (const job of Array.isArray(jobRows) ? jobRows : []) {
|
|
142
|
+
if (!job || !job.account_url || !job.work_id) continue;
|
|
143
|
+
jobByWork.set(String(job.work_id), {
|
|
144
|
+
account_url: String(job.account_url),
|
|
145
|
+
title: String(job.title || '').trim(),
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
const out = new Map();
|
|
149
|
+
const ensure = (url) => {
|
|
150
|
+
if (!out.has(url)) {
|
|
151
|
+
out.set(url, { titles: [], kinds: {}, sourceWorks: 0, sourceMaterials: 0, _works: new Set() });
|
|
152
|
+
}
|
|
153
|
+
return out.get(url);
|
|
154
|
+
};
|
|
155
|
+
for (const job of jobByWork.values()) {
|
|
156
|
+
const bucket = ensure(job.account_url);
|
|
157
|
+
if (job.title && !bucket.titles.includes(job.title) && bucket.titles.length < maxTitles) {
|
|
158
|
+
bucket.titles.push(job.title);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
for (const row of Array.isArray(materialRows) ? materialRows : []) {
|
|
162
|
+
const m = /^material:dy_(\d+)_/.exec(String(row && row.id));
|
|
163
|
+
if (!m) continue;
|
|
164
|
+
const job = jobByWork.get(m[1]);
|
|
165
|
+
if (!job) continue;
|
|
166
|
+
const bucket = ensure(job.account_url);
|
|
167
|
+
const kind = String(row.kind || '未分类').trim();
|
|
168
|
+
if (!bucket.kinds[kind]) bucket.kinds[kind] = [];
|
|
169
|
+
if (bucket.kinds[kind].length < maxNamesPerKind && row.name) {
|
|
170
|
+
bucket.kinds[kind].push(String(row.name));
|
|
171
|
+
}
|
|
172
|
+
bucket.sourceMaterials += 1;
|
|
173
|
+
bucket._works.add(m[1]);
|
|
174
|
+
}
|
|
175
|
+
for (const bucket of out.values()) {
|
|
176
|
+
bucket.sourceWorks = bucket._works.size;
|
|
177
|
+
delete bucket._works;
|
|
178
|
+
}
|
|
179
|
+
return out;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export async function persistStyleProfiles(db, profiles, { account = null } = {}) {
|
|
183
|
+
const { upsertStyleProfiles } = await import('../fancy-material/styles.js');
|
|
184
|
+
return upsertStyleProfiles(db, profiles, { account });
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export function styleResultPath(runDir, account) {
|
|
188
|
+
const slug = String(account?.url || account?.name || 'account')
|
|
189
|
+
.replace(/^https?:\/\//, '')
|
|
190
|
+
.replace(/[^A-Za-z0-9_-]/g, '_')
|
|
191
|
+
.slice(0, 80);
|
|
192
|
+
return path.join(runDir, 'styles', slug + '.json');
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export function removeStyleResult(file) {
|
|
196
|
+
try { fs.rmSync(file, { force: true }); } catch { /* ignore */ }
|
|
197
|
+
}
|