@cloud411716/fancy-webnovel 4.27.3 → 4.29.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 +53 -53
- package/docs/material-library/prompt.md +12 -6
- package/docs/material-library/schema.surql +40 -1
- package/docs/workspace-mcp.md +4 -3
- 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 +80 -1
- 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 +741 -190
- package/plugins/fancy-material/inventory.js +10 -8
- package/plugins/fancy-material/prompts/world-mother.md +81 -0
- package/plugins/fancy-material/styles.js +441 -0
- package/plugins/fancy-material/templates.js +56 -22
- package/plugins/fancy-material/world.js +602 -0
- package/plugins/fancy-topic/index.js +39 -18
- package/plugins/fancy-topic/prompts/02-setup-prompt.md +44 -25
- package/plugins/fancy-topic/scripts/save-minimal-topic.js +1 -1
- package/plugins/fancy-topic/templates.js +33 -19
|
@@ -36,8 +36,10 @@ export async function buildExistingIndex(root, genres, stamp) {
|
|
|
36
36
|
const records = Array.isArray(rows) && Array.isArray(rows[0]) ? rows[0] : [];
|
|
37
37
|
const selected = new Set((genres || []).filter(Boolean));
|
|
38
38
|
const filtered = selected.size
|
|
39
|
-
? records.filter((record) =>
|
|
40
|
-
.
|
|
39
|
+
? records.filter((record) => {
|
|
40
|
+
const values = new Set(Array.isArray(record.genres) ? record.genres.map(String) : []);
|
|
41
|
+
return [...selected].every((genre) => values.has(genre));
|
|
42
|
+
})
|
|
41
43
|
: records;
|
|
42
44
|
const file = path.join(
|
|
43
45
|
workspacePaths(root).tmp,
|
|
@@ -63,8 +65,8 @@ export async function buildExistingIndex(root, genres, stamp) {
|
|
|
63
65
|
/**
|
|
64
66
|
* Count existing materials per kind for the selected genres.
|
|
65
67
|
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
+
* Facet semantics are intersection: a material must contain **all** selected
|
|
69
|
+
* genres. CONTAINSALL counts each matching material once.
|
|
68
70
|
*
|
|
69
71
|
* @param {string} root workspace directory
|
|
70
72
|
* @param {string[]} genres selected genres; empty means all
|
|
@@ -74,11 +76,11 @@ export async function countByKind(root, genres) {
|
|
|
74
76
|
const { db } = await connectMaterial(root);
|
|
75
77
|
try {
|
|
76
78
|
const list = (genres || []).filter(Boolean);
|
|
77
|
-
// count() over a
|
|
78
|
-
//
|
|
79
|
+
// count() over a CONTAINSALL predicate counts every matching record.
|
|
80
|
+
// Subquery keeps the shape identical to the no-filter branch.
|
|
79
81
|
const rows = list.length
|
|
80
82
|
? await db.query(
|
|
81
|
-
'SELECT kind, count() AS n FROM (SELECT id, kind FROM material WHERE genres
|
|
83
|
+
'SELECT kind, count() AS n FROM (SELECT id, kind FROM material WHERE genres CONTAINSALL $genres) GROUP BY kind;',
|
|
82
84
|
{ genres: list },
|
|
83
85
|
)
|
|
84
86
|
: await db.query('SELECT kind, count() AS n FROM material GROUP BY kind;');
|
|
@@ -103,7 +105,7 @@ export async function countKind(root, kind, genres) {
|
|
|
103
105
|
const list = (genres || []).filter(Boolean);
|
|
104
106
|
const rows = list.length
|
|
105
107
|
? await db.query(
|
|
106
|
-
'SELECT count() AS n FROM (SELECT VALUE id FROM material WHERE kind = $kind AND genres
|
|
108
|
+
'SELECT count() AS n FROM (SELECT VALUE id FROM material WHERE kind = $kind AND genres CONTAINSALL $genres) GROUP ALL;',
|
|
107
109
|
{ kind, genres: list },
|
|
108
110
|
)
|
|
109
111
|
: await db.query(
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# 世界母体候选生成
|
|
2
|
+
|
|
3
|
+
你是一位网文世界的总设定师。你现在的任务不是写素材,而是先造一个能长出素材的世界母体。
|
|
4
|
+
|
|
5
|
+
工作区:`{{WORKSPACE}}`
|
|
6
|
+
|
|
7
|
+
## 一、本次选择
|
|
8
|
+
|
|
9
|
+
{{FACET_BLOCK}}
|
|
10
|
+
|
|
11
|
+
## 二、选题建议
|
|
12
|
+
|
|
13
|
+
{{SUGGESTION_BLOCK}}
|
|
14
|
+
|
|
15
|
+
## 三、风格
|
|
16
|
+
|
|
17
|
+
{{STYLE_BLOCK}}
|
|
18
|
+
|
|
19
|
+
## 四、生成 3 个世界母体候选
|
|
20
|
+
|
|
21
|
+
必须生成**恰好 3 个**候选,三个候选的差异要落在世界运转方式上,而不是只换名字:
|
|
22
|
+
|
|
23
|
+
1. `suggestion_anchor`:优先兑现选题建议的方向和定位。
|
|
24
|
+
2. `facet_driven`:优先兑现用户选择的 facet 交集。
|
|
25
|
+
3. `wildcard`:仍然满足全部 facet,但换一种更反常识的世界运转方式。
|
|
26
|
+
|
|
27
|
+
每个候选都必须同时满足:用户选中的全部题材、场景、情绪、情节、身份、关系。
|
|
28
|
+
**同一层多选是交集,不是并集。** 例如同时选 都市 + 异能 + 脑洞,世界必须同时是都市、异能、脑洞,不能只取其中一个。
|
|
29
|
+
|
|
30
|
+
### 命名质量红线
|
|
31
|
+
|
|
32
|
+
- 世界母体不是现实公司的换皮。不要用“夜班、值班、保安、物业、工单、培训手册”直接当世界观核心。
|
|
33
|
+
- 不要生成“试岗、替班、带班、值守”这种现实职级名。
|
|
34
|
+
- 不要生成“包月令、静音条”这种“日常词 + 后缀”的字面功能名。
|
|
35
|
+
- `positioning` 是读者承诺,不是词表;先把它翻译成世界公理、代价、禁忌,再让名字从公理里长出来。
|
|
36
|
+
- 名字要有画面、有第二层含义,换一个世界就不成立。
|
|
37
|
+
|
|
38
|
+
## 五、输出格式
|
|
39
|
+
|
|
40
|
+
只输出 JSON,不要 Markdown 解释。把 JSON 写入绝对路径:
|
|
41
|
+
|
|
42
|
+
`{{RESULT_PATH}}`
|
|
43
|
+
|
|
44
|
+
格式:
|
|
45
|
+
|
|
46
|
+
```json
|
|
47
|
+
{
|
|
48
|
+
"candidates": [
|
|
49
|
+
{
|
|
50
|
+
"candidate_type": "suggestion_anchor",
|
|
51
|
+
"world_id": "stable_ascii_slug",
|
|
52
|
+
"title": "世界母体名",
|
|
53
|
+
"logline": "一句话世界",
|
|
54
|
+
"core_conflict": "这个世界最核心、无法调和的矛盾",
|
|
55
|
+
"reader_promise": "读者会得到的核心体验",
|
|
56
|
+
"power_source": "力量/异常从哪来",
|
|
57
|
+
"cost": "使用力量或维持秩序必须付出的代价",
|
|
58
|
+
"world_rules": ["世界公理 1", "世界公理 2", "世界公理 3"],
|
|
59
|
+
"institutions": ["机构/场所 1", "机构/场所 2"],
|
|
60
|
+
"taboos": ["禁忌 1", "禁忌 2"],
|
|
61
|
+
"visual_motifs": ["视觉母题 1", "视觉母题 2"],
|
|
62
|
+
"naming_logic": "这个世界的人如何命名等级、技能、组织、物品",
|
|
63
|
+
"core_genres": ["必须包含全部选中题材"],
|
|
64
|
+
"supported_kinds": ["rank_name", "skill_name", "faction_name", "item_name", "world_rule"],
|
|
65
|
+
"suggestion_fit": "它如何兑现 direction_title 和 positioning",
|
|
66
|
+
"difference": "与另外两个候选的核心差异"
|
|
67
|
+
}
|
|
68
|
+
]
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
要求:
|
|
73
|
+
|
|
74
|
+
- `core_genres` 必须包含用户选中的全部核心题材,不能漏。
|
|
75
|
+
- `world_rules` 至少 3 条,必须能推出素材,不是空泛口号。
|
|
76
|
+
- `cost` 必须具体到日常动作或关系后果。
|
|
77
|
+
- `naming_logic` 要说明“等级/技能/组织/物品”各自的命名语法,不要只写“有古风感”。
|
|
78
|
+
- 三个候选的 `core_conflict`、`power_source`、`cost` 至少有两项明显不同。
|
|
79
|
+
- 写完后只回复:已写入 <目标文件路径>。
|
|
80
|
+
|
|
81
|
+
{{PREVIOUS_BLOCK}}
|
|
@@ -0,0 +1,441 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 风格注册表:内置风格 + 数据库 douyin_style。
|
|
3
|
+
*
|
|
4
|
+
* 风格是跨 kind 的不变量;kind 只决定"写什么信息点"。
|
|
5
|
+
* 这里不处理具体生成,只负责加载、合并、按题材选择、渲染成提示词。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { RecordId } from 'surrealdb';
|
|
9
|
+
import { connectMaterial } from '#material-db';
|
|
10
|
+
import { BUILTIN_STYLES, SELECTABLE_STYLE_TYPES } from './builtin-styles.js';
|
|
11
|
+
|
|
12
|
+
export const STYLE_TABLE = 'douyin_style';
|
|
13
|
+
|
|
14
|
+
function asArray(value) {
|
|
15
|
+
return Array.isArray(value) ? value : [];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function asObject(value) {
|
|
19
|
+
return value && typeof value === 'object' && !Array.isArray(value) ? value : {};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function unique(values) {
|
|
23
|
+
return [...new Set(values.filter(Boolean))];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function mergeObject(base, patch) {
|
|
27
|
+
const out = { ...asObject(base) };
|
|
28
|
+
for (const [key, value] of Object.entries(asObject(patch))) {
|
|
29
|
+
if (Array.isArray(value)) {
|
|
30
|
+
out[key] = unique([...asArray(out[key]), ...value]);
|
|
31
|
+
} else if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
32
|
+
out[key] = mergeObject(out[key], value);
|
|
33
|
+
} else if (value !== undefined && value !== null && value !== '') {
|
|
34
|
+
out[key] = value;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function normalizeStyle(raw, origin = 'database') {
|
|
41
|
+
const style = raw && typeof raw === 'object' ? raw : {};
|
|
42
|
+
const profile = asObject(style.profile_json);
|
|
43
|
+
const merged = { ...profile, ...style };
|
|
44
|
+
return {
|
|
45
|
+
style_key: String(merged.style_key || '').trim(),
|
|
46
|
+
style_name: String(merged.style_name || merged.name || merged.style_key || '').trim(),
|
|
47
|
+
style_type: ['naming', 'structure', 'teaching', 'visual'].includes(String(merged.style_type || '').trim())
|
|
48
|
+
? String(merged.style_type).trim()
|
|
49
|
+
: 'naming',
|
|
50
|
+
account_names: unique([
|
|
51
|
+
...asArray(merged.account_names),
|
|
52
|
+
merged.account_name,
|
|
53
|
+
].map((v) => String(v || '').trim()).filter(Boolean)),
|
|
54
|
+
account_urls: unique([
|
|
55
|
+
...asArray(merged.account_urls),
|
|
56
|
+
merged.account_url,
|
|
57
|
+
].map((v) => String(v || '').trim()).filter(Boolean)),
|
|
58
|
+
genre_tags: unique(asArray(merged.genre_tags).map((v) => String(v || '').trim())),
|
|
59
|
+
dna: String(merged.dna || '').trim(),
|
|
60
|
+
naming_rules: asObject(merged.naming_rules),
|
|
61
|
+
lexicon: asObject(merged.lexicon),
|
|
62
|
+
cross_kind_examples: asObject(merged.cross_kind_examples),
|
|
63
|
+
anti_examples: unique(asArray(merged.anti_examples).map((v) => String(v || '').trim())),
|
|
64
|
+
compatibility: asObject(merged.compatibility),
|
|
65
|
+
confidence: Math.max(0, Math.min(1, Number(merged.confidence) || 0)),
|
|
66
|
+
source_works: Number(merged.source_works) || 0,
|
|
67
|
+
source_materials: Number(merged.source_materials) || 0,
|
|
68
|
+
version: Number(merged.version) || 1,
|
|
69
|
+
status: String(merged.status || 'active'),
|
|
70
|
+
origin: style.origin || origin,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function mergeStyle(base, patch) {
|
|
75
|
+
const a = normalizeStyle(base, base.origin || 'builtin');
|
|
76
|
+
const b = normalizeStyle(patch, patch.origin || 'database');
|
|
77
|
+
return {
|
|
78
|
+
...a,
|
|
79
|
+
...b,
|
|
80
|
+
style_key: b.style_key || a.style_key,
|
|
81
|
+
style_name: b.style_name || a.style_name,
|
|
82
|
+
style_type: b.style_type || a.style_type,
|
|
83
|
+
account_names: unique([...a.account_names, ...b.account_names]),
|
|
84
|
+
account_urls: unique([...a.account_urls, ...b.account_urls]),
|
|
85
|
+
genre_tags: unique([...a.genre_tags, ...b.genre_tags]),
|
|
86
|
+
dna: b.dna || a.dna,
|
|
87
|
+
naming_rules: mergeObject(a.naming_rules, b.naming_rules),
|
|
88
|
+
lexicon: mergeObject(a.lexicon, b.lexicon),
|
|
89
|
+
cross_kind_examples: mergeObject(a.cross_kind_examples, b.cross_kind_examples),
|
|
90
|
+
anti_examples: unique([...a.anti_examples, ...b.anti_examples]),
|
|
91
|
+
compatibility: { ...a.compatibility, ...b.compatibility },
|
|
92
|
+
confidence: Math.max(a.confidence, b.confidence),
|
|
93
|
+
source_works: Math.max(a.source_works, b.source_works),
|
|
94
|
+
source_materials: Math.max(a.source_materials, b.source_materials),
|
|
95
|
+
version: Math.max(a.version, b.version),
|
|
96
|
+
origin: b.origin === 'database' ? 'merged' : a.origin,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function databaseStyles(db) {
|
|
101
|
+
if (!db) return [];
|
|
102
|
+
try {
|
|
103
|
+
const result = await db.query(
|
|
104
|
+
"SELECT style_key, account_url, account_name, style_name, style_type, genre_tags, dna, naming_rules, lexicon, cross_kind_examples, anti_examples, compatibility, confidence, source_works, source_materials, version, status, profile_json FROM douyin_style WHERE status = 'active';",
|
|
105
|
+
);
|
|
106
|
+
const rows = Array.isArray(result) && Array.isArray(result[0]) ? result[0] : [];
|
|
107
|
+
return rows.map((row) => normalizeStyle({ ...row, origin: 'database' }, 'database'))
|
|
108
|
+
.filter((style) => style.style_key);
|
|
109
|
+
} catch {
|
|
110
|
+
// 旧库还没执行 schema v13 时不应阻断生成。
|
|
111
|
+
return [];
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function styleIdentity(style) {
|
|
116
|
+
return String(style.style_name || '').trim() + '\u0000' + String(style.dna || '').trim();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** 同账号组可能产生多个 style_key(带账号前缀),按名称+DNA 去重。 */
|
|
120
|
+
function dedupeStyles(styles) {
|
|
121
|
+
const byIdentity = new Map();
|
|
122
|
+
for (const style of styles) {
|
|
123
|
+
const key = styleIdentity(style);
|
|
124
|
+
if (!key || key === '\u0000') {
|
|
125
|
+
byIdentity.set(style.style_key, style);
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
const existing = byIdentity.get(key);
|
|
129
|
+
if (!existing) {
|
|
130
|
+
byIdentity.set(key, style);
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
const merged = mergeStyle(existing, style);
|
|
134
|
+
merged.style_key = existing.style_key;
|
|
135
|
+
merged.origin = 'merged';
|
|
136
|
+
byIdentity.set(key, merged);
|
|
137
|
+
}
|
|
138
|
+
return [...byIdentity.values()];
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* 加载内置 + 数据库风格,同 style_key 合并;数据库字段优先。
|
|
143
|
+
*
|
|
144
|
+
* @param {string} root 工作区
|
|
145
|
+
* @param {{db?: object}} [options] 传入已有连接可避免重复连接
|
|
146
|
+
* @returns {Promise<{styles: object[], builtin: number, database: number}>}
|
|
147
|
+
*/
|
|
148
|
+
export async function loadStyleRegistry(root, { db = null } = {}) {
|
|
149
|
+
const byKey = new Map();
|
|
150
|
+
for (const style of BUILTIN_STYLES) {
|
|
151
|
+
const normalized = normalizeStyle(style, 'builtin');
|
|
152
|
+
if (normalized.style_key) byKey.set(normalized.style_key, normalized);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
let ownedDb = null;
|
|
156
|
+
let useDb = db;
|
|
157
|
+
if (!useDb) {
|
|
158
|
+
try {
|
|
159
|
+
const connected = await connectMaterial(root);
|
|
160
|
+
ownedDb = connected.db;
|
|
161
|
+
useDb = connected.db;
|
|
162
|
+
} catch {
|
|
163
|
+
useDb = null;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
let rows = [];
|
|
167
|
+
try {
|
|
168
|
+
rows = await databaseStyles(useDb);
|
|
169
|
+
} finally {
|
|
170
|
+
if (ownedDb) {
|
|
171
|
+
try { await ownedDb.close(); } catch { /* ignore */ }
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
for (const row of rows) {
|
|
176
|
+
if (!row.style_key) continue;
|
|
177
|
+
const existing = byKey.get(row.style_key);
|
|
178
|
+
byKey.set(row.style_key, existing ? mergeStyle(existing, row) : row);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return {
|
|
182
|
+
styles: dedupeStyles([...byKey.values()]),
|
|
183
|
+
builtin: BUILTIN_STYLES.length,
|
|
184
|
+
database: rows.length,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function compatibilityLevel(style, genres) {
|
|
189
|
+
let score = 0;
|
|
190
|
+
for (const raw of genres) {
|
|
191
|
+
const genre = String(raw || '').trim();
|
|
192
|
+
if (!genre) continue;
|
|
193
|
+
for (const [key, level] of Object.entries(style.compatibility || {})) {
|
|
194
|
+
if (!key) continue;
|
|
195
|
+
if (genre.includes(key) || key.includes(genre)) {
|
|
196
|
+
if (level === 'native') score += 6;
|
|
197
|
+
else if (level === 'adaptable') score += 3;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
for (const tag of style.genre_tags || []) {
|
|
201
|
+
if (tag && (genre.includes(tag) || tag.includes(genre))) score += 4;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return score;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function parseExplicit(explicit) {
|
|
208
|
+
if (Array.isArray(explicit)) return explicit.map((v) => String(v || '').trim()).filter(Boolean);
|
|
209
|
+
return String(explicit || '')
|
|
210
|
+
.split(/[,,、]/)
|
|
211
|
+
.map((v) => v.trim())
|
|
212
|
+
.filter(Boolean);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* 选择本次风格。
|
|
217
|
+
*
|
|
218
|
+
* explicit:
|
|
219
|
+
* '' / 'auto' -> 自动按题材选 2-3 个
|
|
220
|
+
* 'none' -> 不使用账号风格
|
|
221
|
+
* 'suci,...' -> 手动指定 style_key
|
|
222
|
+
*/
|
|
223
|
+
export function selectStyles(styles, {
|
|
224
|
+
genres = [],
|
|
225
|
+
explicit = '',
|
|
226
|
+
limit = 3,
|
|
227
|
+
} = {}) {
|
|
228
|
+
const keys = parseExplicit(explicit);
|
|
229
|
+
const lower = keys.map((key) => key.toLowerCase());
|
|
230
|
+
if (lower.includes('none')) return [];
|
|
231
|
+
const all = Array.isArray(styles) ? styles : [];
|
|
232
|
+
const byKey = new Map(all.map((style) => [style.style_key.toLowerCase(), style]));
|
|
233
|
+
|
|
234
|
+
if (keys.length && !lower.includes('auto')) {
|
|
235
|
+
const selected = [];
|
|
236
|
+
const unknown = [];
|
|
237
|
+
for (const key of keys) {
|
|
238
|
+
const style = byKey.get(key.toLowerCase());
|
|
239
|
+
if (style) selected.push(style);
|
|
240
|
+
else unknown.push(key);
|
|
241
|
+
}
|
|
242
|
+
if (unknown.length) {
|
|
243
|
+
const available = all.map((style) => style.style_key).filter(Boolean).join('、');
|
|
244
|
+
throw new Error('未找到风格:' + unknown.join('、') + '\n可用风格:' + (available || '(暂无)'));
|
|
245
|
+
}
|
|
246
|
+
return selected.slice(0, Math.max(1, limit));
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const ranked = all
|
|
250
|
+
.filter((style) => SELECTABLE_STYLE_TYPES.has(style.style_type))
|
|
251
|
+
.map((style) => ({ style, score: compatibilityLevel(style, genres) }))
|
|
252
|
+
.filter((item) => item.score > 0)
|
|
253
|
+
.sort((a, b) => (b.score - a.score) || (b.style.confidence - a.style.confidence));
|
|
254
|
+
return ranked.slice(0, Math.max(1, limit)).map((item) => item.style);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export function stylesForAccount(styles, account) {
|
|
258
|
+
const name = String(account && (account.name || account.account_name) || '').trim();
|
|
259
|
+
const url = String(account && (account.url || account.account_url) || '').trim();
|
|
260
|
+
return (Array.isArray(styles) ? styles : []).filter((style) => {
|
|
261
|
+
if (url && (style.account_urls || []).includes(url)) return true;
|
|
262
|
+
if (name && (style.account_names || []).includes(name)) return true;
|
|
263
|
+
return false;
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export function styleExamples(style, limit = 3) {
|
|
268
|
+
const out = [];
|
|
269
|
+
for (const values of Object.values(style.cross_kind_examples || {})) {
|
|
270
|
+
for (const value of asArray(values)) {
|
|
271
|
+
if (value && !out.includes(value)) out.push(value);
|
|
272
|
+
if (out.length >= limit) return out;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return out;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
export function styleIndexLines(styles, { limit = 20 } = {}) {
|
|
279
|
+
return (Array.isArray(styles) ? styles : [])
|
|
280
|
+
.filter((style) => SELECTABLE_STYLE_TYPES.has(style.style_type))
|
|
281
|
+
.slice(0, limit)
|
|
282
|
+
.map((style) => `- \`${style.style_key}\`(${style.style_name})|${(style.genre_tags || []).join('、') || '通用'}|${style.dna}`);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* 生成注入 prompt 的风格块。
|
|
287
|
+
* 只展开本次选中的风格;其余只给索引,避免上下文爆炸。
|
|
288
|
+
*/
|
|
289
|
+
export function stylePromptBlock(styles, selected, { genres = [] } = {}) {
|
|
290
|
+
const all = Array.isArray(styles) ? styles : [];
|
|
291
|
+
const picked = Array.isArray(selected) ? selected : [];
|
|
292
|
+
const lines = [
|
|
293
|
+
'## 风格锁定(最高优先级)',
|
|
294
|
+
'',
|
|
295
|
+
'风格是跨 kind 的不变量:kind 决定“写什么信息点”,风格决定“用什么方式写”。',
|
|
296
|
+
'如果下面的风格规则与题材命名体例冲突,以风格规则为准;题材只负责换词汇和禁用项。',
|
|
297
|
+
'',
|
|
298
|
+
'### 可用风格索引',
|
|
299
|
+
...styleIndexLines(all),
|
|
300
|
+
'',
|
|
301
|
+
];
|
|
302
|
+
if (!picked.length) {
|
|
303
|
+
lines.push('本次未选择账号风格,按通用规则生成。');
|
|
304
|
+
return lines.join('\n');
|
|
305
|
+
}
|
|
306
|
+
lines.push('### 本次选中的风格');
|
|
307
|
+
for (const style of picked) {
|
|
308
|
+
lines.push('');
|
|
309
|
+
lines.push(`#### \`${style.style_key}\`(${style.style_name})`);
|
|
310
|
+
lines.push('- 风格类型:' + style.style_type);
|
|
311
|
+
lines.push('- 适用题材:' + (style.genre_tags || []).join('、'));
|
|
312
|
+
lines.push('- DNA:' + style.dna);
|
|
313
|
+
const rules = style.naming_rules || {};
|
|
314
|
+
if (rules.length) lines.push('- 长度:' + (Array.isArray(rules.length) ? rules.length.join('–') + ' 字' : rules.length));
|
|
315
|
+
if (rules.syntax) lines.push('- 句式:' + asArray(rules.syntax).join(';'));
|
|
316
|
+
if (rules.suffixes) lines.push('- 可用后缀:' + (asArray(rules.suffixes).join('、') || '无固定后缀'));
|
|
317
|
+
if (rules.forbidden) lines.push('- 禁用:' + asArray(rules.forbidden).join('、'));
|
|
318
|
+
if (rules.tone) lines.push('- 语气:' + rules.tone);
|
|
319
|
+
const preferred = asArray(style.lexicon && style.lexicon.preferred);
|
|
320
|
+
const forbidden = asArray(style.lexicon && style.lexicon.forbidden);
|
|
321
|
+
if (preferred.length) lines.push('- 偏好意象:' + preferred.join('、'));
|
|
322
|
+
if (forbidden.length) lines.push('- 禁用意象:' + forbidden.join('、'));
|
|
323
|
+
lines.push('- 跨 kind 正例:');
|
|
324
|
+
for (const [kind, values] of Object.entries(style.cross_kind_examples || {})) {
|
|
325
|
+
const list = asArray(values).slice(0, 6);
|
|
326
|
+
if (list.length) lines.push(` - \`${kind}\`:${list.join('、')}`);
|
|
327
|
+
}
|
|
328
|
+
if (style.anti_examples && style.anti_examples.length) {
|
|
329
|
+
lines.push('- 反例(出现即废弃):' + style.anti_examples.slice(0, 12).join('、'));
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
lines.push('');
|
|
333
|
+
lines.push('本批题材:' + (genres.join('、') || '通用'));
|
|
334
|
+
lines.push('把选中风格的“核心语法”投射到本次题材和本批 kind 上;不要照抄正例,也不要退回通用仙气/古风形容词。');
|
|
335
|
+
return lines.join('\n');
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
export function styleSummaryText(styles) {
|
|
339
|
+
return (Array.isArray(styles) ? styles : [])
|
|
340
|
+
.map((style) => `${style.style_key}(${style.style_name})`)
|
|
341
|
+
.join('、');
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
export function styleBatchSummary(styles) {
|
|
345
|
+
return (Array.isArray(styles) ? styles : []).map((style) => {
|
|
346
|
+
const rules = style.naming_rules || {};
|
|
347
|
+
const bits = [
|
|
348
|
+
`- ${style.style_key}(${style.style_name}):${String(style.dna || '').replace(/[。;;]+$/u, '')}`,
|
|
349
|
+
];
|
|
350
|
+
if (rules.length) bits.push(`长度 ${Array.isArray(rules.length) ? rules.length.join('–') + ' 字' : rules.length}`);
|
|
351
|
+
if (rules.forbidden && rules.forbidden.length) bits.push('禁用 ' + rules.forbidden.join('、'));
|
|
352
|
+
return bits.join(';');
|
|
353
|
+
}).join('\n');
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* 把风格 patch 合并进数据库(同 style_key 做增量合并,不覆盖旧例子)。
|
|
358
|
+
* 只增/改 douyin_style,不碰 material、douyin_account、douyin_job。
|
|
359
|
+
*/
|
|
360
|
+
function accountKeyPrefix(account) {
|
|
361
|
+
const raw = String((account && (account.url || account.name)) || 'account')
|
|
362
|
+
.split('/')
|
|
363
|
+
.filter(Boolean)
|
|
364
|
+
.pop() || 'account';
|
|
365
|
+
return raw.replace(/[^A-Za-z0-9]/g, '').toLowerCase().slice(0, 24) || 'account';
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
export async function upsertStyleProfiles(db, profiles, { account = null } = {}) {
|
|
369
|
+
if (!db || !Array.isArray(profiles)) return [];
|
|
370
|
+
const saved = [];
|
|
371
|
+
for (const raw of profiles) {
|
|
372
|
+
if (!raw || !raw.style_key) continue;
|
|
373
|
+
let key = String(raw.style_key).trim();
|
|
374
|
+
if (!/^[a-z][a-z0-9_-]*$/i.test(key)) continue;
|
|
375
|
+
let existing = null;
|
|
376
|
+
// 内置风格已经有归属账号;别的账号复用同一个 key 时自动加账号前缀。
|
|
377
|
+
const builtin = BUILTIN_STYLES.find((style) => style.style_key === key);
|
|
378
|
+
if (builtin && account && account.name && !(builtin.account_names || []).includes(account.name)) {
|
|
379
|
+
key = accountKeyPrefix(account) + '_' + key;
|
|
380
|
+
}
|
|
381
|
+
try {
|
|
382
|
+
const result = await db.query('SELECT * FROM douyin_style WHERE style_key = $key LIMIT 1;', { key });
|
|
383
|
+
existing = Array.isArray(result) && Array.isArray(result[0]) ? result[0][0] : null;
|
|
384
|
+
} catch { /* table may not exist yet */ }
|
|
385
|
+
// 同 key 已被另一个账号占用时,加上账号前缀,避免跨账号覆盖。
|
|
386
|
+
if (existing && account && existing.account_url && account.url && existing.account_url !== account.url) {
|
|
387
|
+
key = accountKeyPrefix(account) + '_' + key;
|
|
388
|
+
try {
|
|
389
|
+
const result = await db.query('SELECT * FROM douyin_style WHERE style_key = $key LIMIT 1;', { key });
|
|
390
|
+
existing = Array.isArray(result) && Array.isArray(result[0]) ? result[0][0] : null;
|
|
391
|
+
} catch { existing = null; }
|
|
392
|
+
}
|
|
393
|
+
const base = existing ? normalizeStyle({ ...existing, origin: 'database' }) : null;
|
|
394
|
+
const patch = normalizeStyle({
|
|
395
|
+
...raw,
|
|
396
|
+
style_key: key,
|
|
397
|
+
account_url: raw.account_url || (account && account.url) || '',
|
|
398
|
+
account_name: raw.account_name || (account && account.name) || '',
|
|
399
|
+
origin: 'database',
|
|
400
|
+
}, 'database');
|
|
401
|
+
const merged = base ? mergeStyle(base, patch) : patch;
|
|
402
|
+
merged.style_key = key;
|
|
403
|
+
merged.version = Math.max(1, Number(base?.version || 0) + 1);
|
|
404
|
+
merged.source_works = Math.max(Number(base?.source_works || 0), Number(patch.source_works || 0));
|
|
405
|
+
merged.source_materials = Math.max(Number(base?.source_materials || 0), Number(patch.source_materials || 0));
|
|
406
|
+
merged.status = 'active';
|
|
407
|
+
const content = {
|
|
408
|
+
style_key: merged.style_key,
|
|
409
|
+
account_url: (account && account.url) || merged.account_urls[0] || '',
|
|
410
|
+
account_name: (account && account.name) || merged.account_names[0] || '',
|
|
411
|
+
style_name: merged.style_name,
|
|
412
|
+
style_type: merged.style_type,
|
|
413
|
+
genre_tags: merged.genre_tags,
|
|
414
|
+
dna: merged.dna,
|
|
415
|
+
naming_rules: merged.naming_rules,
|
|
416
|
+
lexicon: merged.lexicon,
|
|
417
|
+
cross_kind_examples: merged.cross_kind_examples,
|
|
418
|
+
anti_examples: merged.anti_examples,
|
|
419
|
+
compatibility: merged.compatibility,
|
|
420
|
+
confidence: merged.confidence,
|
|
421
|
+
source_works: merged.source_works,
|
|
422
|
+
source_materials: merged.source_materials,
|
|
423
|
+
version: merged.version,
|
|
424
|
+
status: merged.status,
|
|
425
|
+
profile_json: {
|
|
426
|
+
account_names: merged.account_names,
|
|
427
|
+
account_urls: merged.account_urls,
|
|
428
|
+
naming_rules: merged.naming_rules,
|
|
429
|
+
lexicon: merged.lexicon,
|
|
430
|
+
cross_kind_examples: merged.cross_kind_examples,
|
|
431
|
+
anti_examples: merged.anti_examples,
|
|
432
|
+
compatibility: merged.compatibility,
|
|
433
|
+
},
|
|
434
|
+
};
|
|
435
|
+
const record = db.upsert(new RecordId(STYLE_TABLE, key));
|
|
436
|
+
if (existing) await record.merge(content);
|
|
437
|
+
else await record.content(content);
|
|
438
|
+
saved.push(merged);
|
|
439
|
+
}
|
|
440
|
+
return saved;
|
|
441
|
+
}
|