@cloud411716/fancy-webnovel 4.1.0 → 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.
@@ -52,10 +52,10 @@ plugins/fancy-material/
52
52
  | `id` | 是 | `<kind>_<ascii_slug>_v4`,全局唯一 |
53
53
  | `kind` | 是 | `source=builtin` 时必须是 54 个原子类型之一;`source=other` 时不受限制 |
54
54
  | `name` | 是 | 素材本体:名字、短语、一句话机制/代价/规则/钩子 |
55
- | `tags` | 是 | 3–8 个短标签 |
56
- | `genres` | 是 | 1–3 个适用题材;跨题材写 `["通用"]` |
55
+ | `tags` | 是 | 3–8 个短标签,可同时覆盖多个题材/标签 |
56
+ | `genres` | 是 | 1–5 个适用题材;一个素材可以同时适配多个题材,跨题材写 `["通用"]` |
57
57
  | `note` | 否 | 只有 `name` 不够清楚时补一句 |
58
- | `source` | 是 | `builtin` 或 `other`;生成素材固定 `builtin` |
58
+ | `source` | 是 | `builtin` 或 `other`;内置题材生成 `builtin`,`/fancy-material <题材> <素材类型>` 生成 `other` |
59
59
 
60
60
  `source` 用来区分内置素材和外部素材:`builtin` 的 `kind` 受 54 个类型限制;`other` 可以写任意自定义 `kind`。不再有 `payload`、`summary`、`description`、`core_hook`、`market_diff`、`search_text` 这些复合字段。
61
61
 
@@ -77,6 +77,27 @@ plugins/fancy-material/
77
77
  - 需要搭配时用 `tags` 表达,例如 `["剑","雷系","高速"]`;不要写从属关系。
78
78
  - 一条素材删掉后正文没有任何损失,就不收。
79
79
 
80
+ ### 2.3 标签与题材是数组
81
+
82
+ `tags` 和 `genres` 都是字符串数组,不是单值:
83
+
84
+ - `tags` 3–8 个,从题材、类型、功能、元素、情绪、场景、代价、关系、强度等维度打标签。
85
+ - `genres` 1–5 个,必须原样包含本次主题材名;一个素材同时适配多个题材时全部写进去。
86
+ - 例:都市高武相关素材可以写 `tags: ["都市","高武","都市高武","地标"]`、`genres: ["都市高武","都市","高武"]`。
87
+
88
+ ### 2.4 自定义题材 + 自定义素材类型
89
+
90
+ ```text
91
+ /fancy-material 都市高武 城市地标
92
+ /fancy-material 克苏鲁 神话生物
93
+ ```
94
+
95
+ 第一个参数是题材,第二个参数是素材类型;两者都可以随便写,不要求命中内置题材/54 个 kind。插件会把它们交给 LLM:
96
+
97
+ - 素材类型命中内置 kind 时直接使用该 kind;否则由 LLM 自拟一个安全的小写 snake_case kind。
98
+ - 生成结果的 `source` 固定为 `other`,不占用内置目录,也不会被 `init` 当作旧内置 kind 清理。
99
+ - 同一批只能使用一个 kind;插件在第一轮识别后记住它,后续批次沿用。
100
+
80
101
  ## 3. 54 个 kind
81
102
 
82
103
  ### 命名
@@ -65,7 +65,7 @@ function validateRecord(raw, line, seenIds) {
65
65
  ensureString(raw.kind, 'kind', line);
66
66
  ensureString(raw.name, 'name', line);
67
67
  ensureStringArray(raw.tags, 'tags', line, 3, 8);
68
- ensureStringArray(raw.genres, 'genres', line, 1, 3);
68
+ ensureStringArray(raw.genres, 'genres', line, 1, 5);
69
69
  if (raw.note !== undefined) {
70
70
  if (typeof raw.note !== 'string') throw new ImportError('note 必须是字符串', line);
71
71
  }
@@ -34,7 +34,7 @@
34
34
  "tags": ["地名", "海", "尽头", "仙侠", "不祥"],
35
35
  "genres": ["仙侠", "通用"],
36
36
  "note": "",
37
- "source": "builtin"
37
+ "source": "{{SOURCE_FIXED}}"
38
38
  }
39
39
  ```
40
40
 
@@ -43,12 +43,12 @@
43
43
  | 字段 | 必填 | 说明 |
44
44
  | --- | --- | --- |
45
45
  | `id` | 是 | `<kind>_<ascii_slug>_v4`,全局唯一 |
46
- | `kind` | 是 | 下面素材类型总表之一;builtin 素材必须在表内 |
46
+ | `kind` | 是 | {{KIND_RULE}} |
47
47
  | `name` | 是 | **素材本体**:名字、短语、一句话机制/代价/规则/钩子 |
48
48
  | `tags` | 是 | 3–8 个短标签,用于检索和组合 |
49
- | `genres` | 是 | 1–3 个适用题材;跨题材写 `["通用"]` |
49
+ | `genres` | 是 | 1–5 个适用题材;一个素材可以同时适配多个题材,跨题材写 `["通用"]` |
50
50
  | `note` | 否 | 只有 `name` 不够清楚时补一句,默认空字符串 |
51
- | `source` | 是 | `builtin` 或 `other`;本次生成的素材固定写 `builtin` |
51
+ | `source` | 是 | `builtin` 或 `other`;{{SOURCE_NOTE}} |
52
52
 
53
53
  不要新增 `summary`、`description`、`core_hook`、`market_diff`、`payload`、`sources` 等字段。
54
54
 
@@ -211,6 +211,7 @@ brainstorm → 给活人打印死亡证明的打印店
211
211
 
212
212
  - 3–8 个短标签,不要写句子。
213
213
  - 维度:题材、类型、功能、元素、情绪、场景、代价、关系、强度。
214
+ - 一个素材能适配多个题材/标签时,把相关的都写进去。例如「都市高武」相关素材,tags 可以同时有「都市」「高武」「都市高武」,不要只写一个主题材。
214
215
  - 例:
215
216
 
216
217
  ```text
@@ -224,8 +225,9 @@ brainstorm → 给活人打印死亡证明的打印店
224
225
 
225
226
  ### `genres`
226
227
 
227
- - 1–3 个,跨题材写 `["通用"]`。
228
+ - 1–5 个,跨题材写 `["通用"]`。
228
229
  - 必须原样包含本次主题材名。例如本次是「仙侠·修真文明」,可以写:
230
+ - 如果素材同时适配多个题材/子题材,把所有能适配的都写进去;例如「都市高武」可以写 `["都市高武", "都市", "高武"]`,不要只写一个。
229
231
 
230
232
  ```json
231
233
  ["仙侠·修真文明", "仙侠", "修真"]
@@ -241,6 +243,7 @@ brainstorm → 给活人打印死亡证明的打印店
241
243
  - `topic`:题材来自 `_novel_topic.json`。
242
244
  - `suggestion`:题材来自 `_novel_topic_suggestion.json`。
243
245
  - `builtin`:没有选题记录,使用内置全题材。
246
+ - `other`:用户指定题材 + 素材类型;source 固定写 `other`。
244
247
  - 本次题材列表:{{GENRES}}
245
248
 
246
249
  不同题材优先补充的 kind:
@@ -303,15 +306,15 @@ node '{{QUERY_SCRIPT}}' --workspace '{{WORKSPACE}}' --search '残影 高速' --l
303
306
  - 字符串中的换行写成 `\n`。
304
307
  - `id` 全局唯一,格式 `<kind>_<ascii_slug>_v4`。
305
308
  - 同批不得重复 `id`;不要复用已有 id。
306
- - `source` 固定写 `builtin`;`other` 只用于库外手工导入的自定义素材。
309
+ - `source` 固定写 `{{SOURCE_FIXED}}`;{{SOURCE_NOTE}}
307
310
 
308
311
  示例:
309
312
 
310
313
  ```json
311
- {"id":"given_name_yanqiu_v4","kind":"given_name","name":"砚秋","tags":["男","双字","书卷","秋意","克制"],"genres":["悬疑","都市"],"note":"","source":"builtin"}
312
- {"id":"color_word_rain_soaked_cinnabar_v4","kind":"color_word","name":"被雨泡过的朱砂","tags":["红","暖色","潮湿","旧"],"genres":["悬疑","通用"],"note":"","source":"builtin"}
313
- {"id":"ability_cost_memory_loss_v4","kind":"ability_cost","name":"每次使用后永久丢失一段与死者无关的等长记忆","tags":["记忆","永久","关系代价","调查"],"genres":["悬疑","都市异能"],"note":"","source":"builtin"}
314
- {"id":"brainstorm_death_certificate_printshop_v4","kind":"brainstorm","name":"给活人打印死亡证明的打印店","tags":["制度","死亡","悬疑","日常"],"genres":["悬疑"],"note":"","source":"builtin"}
314
+ {"id":"given_name_yanqiu_v4","kind":"given_name","name":"砚秋","tags":["男","双字","书卷","秋意","克制"],"genres":["悬疑","都市"],"note":"","source":"{{SOURCE_FIXED}}"}
315
+ {"id":"color_word_rain_soaked_cinnabar_v4","kind":"color_word","name":"被雨泡过的朱砂","tags":["红","暖色","潮湿","旧"],"genres":["悬疑","通用"],"note":"","source":"{{SOURCE_FIXED}}"}
316
+ {"id":"ability_cost_memory_loss_v4","kind":"ability_cost","name":"每次使用后永久丢失一段与死者无关的等长记忆","tags":["记忆","永久","关系代价","调查"],"genres":["悬疑","都市异能"],"note":"","source":"{{SOURCE_FIXED}}"}
317
+ {"id":"brainstorm_death_certificate_printshop_v4","kind":"brainstorm","name":"给活人打印死亡证明的打印店","tags":["制度","死亡","悬疑","日常"],"genres":["悬疑"],"note":"","source":"{{SOURCE_FIXED}}"}
315
318
  ```
316
319
 
317
320
  ---
@@ -351,10 +354,10 @@ node '{{QUERY_SCRIPT}}' --workspace '{{WORKSPACE}}' --search '残影 高速' --l
351
354
  3. 它有没有具体参照物、感官、情绪或处境?
352
355
  4. 它能不能脱离当前人物/武器/场景,单独用在别处?
353
356
  5. 它和已有素材是不是同义换皮?
354
- 6. `tags` 是否 3–8 个、能用于检索?
357
+ 6. `tags` 是否 3–8 个、能用于检索?`genres` 是否 1–5 个,并覆盖了这个素材能适配的多个题材?
355
358
  7. `genres` 是否包含本次主题材名?
356
359
  8. 是否只写了当前 kind,没有混入其它 kind?
357
- 9. `source` 是否固定写成了 `builtin`?
360
+ 9. `source` 是否固定写成了 `{{SOURCE_FIXED}}`?
358
361
  10. 是否没有新增字段、没有 Markdown 围栏、JSON 合法?
359
362
 
360
363
  ---
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloud411716/fancy-webnovel",
3
- "version": "4.1.0",
3
+ "version": "4.2.0",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "exports": {
@@ -47,7 +47,7 @@ id / kind / name / tags / genres / note / source
47
47
  - `source` 固定写 `other`。
48
48
  - `kind` 不受内置列表限制;如果内容能归入下面的内置 kind,优先使用内置 kind。
49
49
  - `tags` 3–8 个短标签。
50
- - `genres` 1–3 个题材;拿不准就写 `["通用"]`。
50
+ - `genres` 1–5 个题材;如果素材同时适配多个题材/标签,把它们都写进去;拿不准就写 `["通用"]`。
51
51
  - `note` 可写一句补充,没有就写空字符串。
52
52
  - `id` 用小写字母、数字、下划线,格式建议:`<kind>_dy_<作品ID>_<序号>`,全局唯一。
53
53
 
@@ -28,7 +28,7 @@ import { makeWaitForTurn } from '../fancy-story-builder/writing-driver.js';
28
28
  import { exportMaterials } from './exporter.js';
29
29
  import { allKnownGenres } from './genres.js';
30
30
  import { buildExistingIndex, countByKind, countKind } from './inventory.js';
31
- import { KIND_LABELS, MATERIAL_KINDS, focusBlock, renderKindTable, resolveFocus } from './kinds.js';
31
+ import { KIND_LABELS, MATERIAL_KINDS, customFocusBlock, focusBlock, renderKindTable, resolveFocus } from './kinds.js';
32
32
  import { nextBatchRequest, planMaterialQueue } from './queue.js';
33
33
  import { deployMaterialTools } from './tool-deploy.js';
34
34
  import {
@@ -113,13 +113,22 @@ function renderBasePrompt({
113
113
  validateScript,
114
114
  targetPerKind = DEFAULT_TARGET_PER_KIND,
115
115
  batchSize = DEFAULT_BATCH_SIZE,
116
+ focusBlockText = '',
117
+ custom = null,
116
118
  }) {
119
+ const sourceFixed = mode === 'other' ? 'other' : 'builtin';
120
+ const sourceNote = sourceFixed === 'other'
121
+ ? '本次是用户指定题材 + 素材类型的自定义素材,source 固定写 `other`;不要写成 `builtin`。'
122
+ : '`other` 只用于库外手工导入的自定义素材。';
123
+ const kindRule = sourceFixed === 'other'
124
+ ? '下面素材类型总表之一;本次是 `other` 自定义素材,kind 可以不在表内,但同一批所有记录必须使用同一个 kind'
125
+ : '下面素材类型总表之一;builtin 素材必须在表内';
117
126
  return renderPrompt(readPrompt(), {
118
127
  MATERIAL_MODE: mode,
119
128
  GENRES: (genres || []).join('、'),
120
129
  TOPIC_FILE: topicFile || '(无)',
121
130
  KIND_TABLE: renderKindTable(),
122
- FOCUS_BLOCK: focusBlock(focus),
131
+ FOCUS_BLOCK: focusBlockText || focusBlock(focus),
123
132
  TARGET_PER_KIND: String(targetPerKind),
124
133
  BATCH_SIZE: String(batchSize),
125
134
  AUDIENCE: '男频 / 女频 / 通用',
@@ -131,6 +140,9 @@ function renderBasePrompt({
131
140
  QUERY_SCRIPT: queryScript || QUERY_SCRIPT,
132
141
  VALIDATE_SCRIPT: validateScript || VALIDATE_SCRIPT,
133
142
  WORKSPACE: workspace || '(运行时提供)',
143
+ SOURCE_FIXED: sourceFixed,
144
+ SOURCE_NOTE: sourceNote,
145
+ KIND_RULE: kindRule,
134
146
  });
135
147
  }
136
148
 
@@ -230,7 +242,8 @@ function kindCoverageProblem(file, kind) {
230
242
  * (the database defaults it to builtin), but it must not mark entries as
231
243
  * `other` to bypass the builtin kind list.
232
244
  */
233
- function sourceCoverageProblem(file) {
245
+ function sourceCoverageProblem(file, expectedSource = 'builtin') {
246
+ const expected = expectedSource === 'other' ? 'other' : 'builtin';
234
247
  let text;
235
248
  try { text = fs.readFileSync(file, 'utf8'); } catch { return ''; }
236
249
  const lines = text.split(/\r?\n/);
@@ -240,14 +253,58 @@ function sourceCoverageProblem(file) {
240
253
  if (!line) continue;
241
254
  let record;
242
255
  try { record = JSON.parse(line); } catch { continue; }
243
- if (record.source !== undefined && record.source !== 'builtin') {
256
+ if (expected === 'other') {
257
+ // 自定义素材必须显式写 other;省略 source 会被数据库默认成 builtin,
258
+ // 正好落进内置目录的 kind 约束里,后面必然出问题。
259
+ if (record.source !== 'other') {
260
+ bad.push(`第 ${i + 1} 条「${record.name || record.id || '未命名'}」是 ${record.source || '未写 source'}`);
261
+ }
262
+ } else if (record.source !== undefined && record.source !== 'builtin') {
244
263
  bad.push(`第 ${i + 1} 条「${record.name || record.id || '未命名'}」是 ${record.source}`);
245
264
  }
246
265
  }
247
266
  if (!bad.length) return '';
248
- return '生成批次只能写 source=builtin,但文件里出现了其它 source:' + bad.slice(0, 8).join('、')
249
- + (bad.length > 8 ? `等 ${bad.length} 条` : '')
250
- + '。请把 source 改成 builtin,或省略 source(数据库默认 builtin)。';
267
+ return expected === 'other'
268
+ ? '自定义素材必须写 source=other,但以下记录不是:' + bad.slice(0, 8).join('、')
269
+ + (bad.length > 8 ? `等 ${bad.length} 条` : '')
270
+ + '。请把 source 改成 other 后重新写入。'
271
+ : '生成批次只能写 source=builtin,但文件里出现了其它 source:' + bad.slice(0, 8).join('、')
272
+ + (bad.length > 8 ? `等 ${bad.length} 条` : '')
273
+ + '。请把 source 改成 builtin,或省略 source(数据库默认 builtin)。';
274
+ }
275
+
276
+ /**
277
+ * 自定义素材第一轮没有预设 kind:由 LLM 自己选。
278
+ * 这里检查同一批是否只出现一个 kind,并把该 kind 返回给插件,
279
+ * 后续批次和计数都沿用它。
280
+ */
281
+ function detectSingleKind(file) {
282
+ let text;
283
+ try { text = fs.readFileSync(file, 'utf8'); } catch {
284
+ return { kind: '', problem: '目标文件读不出来' };
285
+ }
286
+ const kinds = [];
287
+ const seen = new Set();
288
+ const lines = text.split(/\r?\n/);
289
+ for (let i = 0; i < lines.length; i += 1) {
290
+ const line = lines[i].trim();
291
+ if (!line) continue;
292
+ let record;
293
+ try { record = JSON.parse(line); } catch { continue; }
294
+ const kind = String(record.kind || '').trim();
295
+ if (!kind) return { kind: '', problem: `第 ${i + 1} 行没有 kind` };
296
+ // kind 会进文件名、数据库和后续提示词;模型自拟的 kind 必须限制成
297
+ // 安全的小写 snake_case,防止 `/`、`..` 或空格污染路径。
298
+ if (!/^[a-z][a-z0-9_]{0,119}$/.test(kind)) {
299
+ return { kind: '', problem: `第 ${i + 1} 行的 kind 不合法(只允许小写字母/数字/下划线):${kind}` };
300
+ }
301
+ if (!seen.has(kind)) { seen.add(kind); kinds.push(kind); }
302
+ }
303
+ if (!kinds.length) return { kind: '', problem: '文件里没有素材记录' };
304
+ if (kinds.length > 1) {
305
+ return { kind: '', problem: '同一批自定义素材只能使用一个 kind,但出现了:' + kinds.join('、') };
306
+ }
307
+ return { kind: kinds[0], problem: '' };
251
308
  }
252
309
 
253
310
  function importEnv(root) {
@@ -388,34 +445,52 @@ async function runExport(ctx, invocation, session, taskList) {
388
445
  return notify(session, 'success', exportResultText(result));
389
446
  }
390
447
 
391
- async function runGenerate(ctx, invocation, session, focusRaw = '', taskList) {
448
+ async function runGenerate(ctx, invocation, session, focusRaw = '', taskList, opts = {}) {
392
449
  const root = workspaceOf(session);
450
+ // 两参数形式:/fancy-material <题材> <素材类型>。
451
+ // 题材和素材类型都允许用户随便写:题材直接进提示词,素材类型由 LLM
452
+ // 判断对应哪个 kind(不在 54 个内置 kind 里时自拟 kind)。
453
+ const custom = opts.custom || null;
454
+ const isCustom = Boolean(custom && custom.genre && custom.materialType);
455
+ if (isCustom) focusRaw = custom.materialType;
393
456
  const focus = resolveFocus(focusRaw);
394
- if (focusRaw && !focus.kinds.length) {
457
+ if (!isCustom && focusRaw && !focus.kinds.length) {
395
458
  return notify(session, 'error', '❌ 未识别素材类型:' + focusRaw
396
- + '\n可用示例:地名、人名、姓氏、门派、技能、超能力、武器、颜色、特效、脑洞、人物性格、关系、场景。');
459
+ + '\n可用示例:地名、人名、姓氏、门派、技能、超能力、武器、颜色、特效、脑洞、人物性格、关系、场景。'
460
+ + '\n如果素材类型是自定义的,请用两参数形式:/fancy-material <题材> <素材类型>');
397
461
  }
398
462
  // 素材题材只认用户选题链路:
399
463
  // _novel_topic.json > _novel_topic_suggestion.json > 内置全题材。
400
- // 不再从 topic_decision 里猜题材,避免素材和用户最终选题脱节。
401
- let plan = resolveMaterialTopic(root);
402
- if (!plan.mode) {
403
- const asked = await askUserQuestion(ctx, invocation, [{
404
- id: 'material-builtin-confirm',
405
- header: '没有选题记录',
406
- question: '当前没有执行过扫榜和 /fancy-topic,将使用内置全题材生成素材(男频 + 女频)。是否继续?',
407
- detail: '内置题材共 ' + BUILTIN_GENRES.length + ' 类;确认后所有 kind 都会按全题材生成。',
408
- options: [
409
- { label: '确认', description: '使用内置全题材生成素材' },
410
- { label: '取消', description: '不生成任何素材' },
411
- ],
412
- }]);
413
- if (!asked.ok) {
414
- return notify(session, 'error', asked.failure?.message || '❌ 提问失败');
464
+ // 自定义两参数形式直接用用户给的题材,不走选题链路,也不弹框。
465
+ let plan;
466
+ if (isCustom) {
467
+ plan = {
468
+ mode: 'other',
469
+ genres: [String(custom.genre).trim()],
470
+ topicFile: null,
471
+ customGenre: String(custom.genre).trim(),
472
+ customMaterialType: String(custom.materialType).trim(),
473
+ };
474
+ } else {
475
+ plan = resolveMaterialTopic(root);
476
+ if (!plan.mode) {
477
+ const asked = await askUserQuestion(ctx, invocation, [{
478
+ id: 'material-builtin-confirm',
479
+ header: '没有选题记录',
480
+ question: '当前没有执行过扫榜和 /fancy-topic,将使用内置全题材生成素材(男频 + 女频)。是否继续?',
481
+ detail: '内置题材共 ' + BUILTIN_GENRES.length + ' 类;确认后所有 kind 都会按全题材生成。',
482
+ options: [
483
+ { label: '确认', description: '使用内置全题材生成素材' },
484
+ { label: '取消', description: '不生成任何素材' },
485
+ ],
486
+ }]);
487
+ if (!asked.ok) {
488
+ return notify(session, 'error', asked.failure?.message || '❌ 提问失败');
489
+ }
490
+ const selected = asked.answer?.answers?.[0]?.selected?.[0];
491
+ if (selected !== '确认') return notify(session, 'success', '操作已取消。');
492
+ plan = { mode: 'builtin', genres: [...BUILTIN_GENRES], topicFile: null };
415
493
  }
416
- const selected = asked.answer?.answers?.[0]?.selected?.[0];
417
- if (selected !== '确认') return notify(session, 'success', '操作已取消。');
418
- plan = { mode: 'builtin', genres: [...BUILTIN_GENRES], topicFile: null };
419
494
  }
420
495
 
421
496
  const config = generationConfig();
@@ -427,7 +502,8 @@ async function runGenerate(ctx, invocation, session, focusRaw = '', taskList) {
427
502
  }
428
503
  const database = materialDatabaseName(readWorkspace(root));
429
504
  const counts = await countByKind(root, plan.genres);
430
- const kinds = [...new Set(focus.kinds.length ? focus.kinds : MATERIAL_KINDS)];
505
+ // 自定义素材第一轮 kind 由 LLM 决定,这里先用一个占位项跑一条队列。
506
+ const kinds = [...new Set(focus.kinds.length ? focus.kinds : (isCustom ? ['__custom__'] : MATERIAL_KINDS))];
431
507
  const planned = planMaterialQueue({ kinds, target: config.targetPerKind });
432
508
 
433
509
  const startedAt = Date.now();
@@ -438,6 +514,13 @@ async function runGenerate(ctx, invocation, session, focusRaw = '', taskList) {
438
514
  const progressFile = path.join(runDir, 'progress.json');
439
515
  const batchLog = path.join(runDir, 'batches.jsonl');
440
516
  const existing = await buildExistingIndex(root, plan.genres, stamp);
517
+ const focusBlockText = isCustom
518
+ ? customFocusBlock({
519
+ genre: plan.customGenre,
520
+ materialType: plan.customMaterialType,
521
+ kinds: focus.kinds,
522
+ })
523
+ : '';
441
524
  const base = renderBasePrompt({
442
525
  mode: plan.mode,
443
526
  genres: plan.genres,
@@ -451,11 +534,15 @@ async function runGenerate(ctx, invocation, session, focusRaw = '', taskList) {
451
534
  validateScript: tools.validateScript,
452
535
  targetPerKind: config.targetPerKind,
453
536
  batchSize: config.batchSize,
537
+ focusBlockText,
538
+ custom: isCustom ? { genre: plan.customGenre, materialType: plan.customMaterialType } : null,
454
539
  });
455
540
 
456
541
  const queue = planned.all.map((item) => ({
457
- kind: item.kind,
458
- label: KIND_LABELS[item.kind] || item.kind,
542
+ kind: item.kind === '__custom__' ? '' : item.kind,
543
+ label: isCustom
544
+ ? plan.customMaterialType
545
+ : (KIND_LABELS[item.kind] || item.kind),
459
546
  db_existing: Number(counts[item.kind]) || 0,
460
547
  generated: item.generated,
461
548
  target: item.target,
@@ -479,6 +566,11 @@ async function runGenerate(ctx, invocation, session, focusRaw = '', taskList) {
479
566
  batch_size: config.batchSize,
480
567
  genre_mode: plan.mode,
481
568
  genres: plan.genres,
569
+ source: plan.mode === 'other' ? 'other' : 'builtin',
570
+ ...(isCustom ? {
571
+ custom_genre: plan.customGenre,
572
+ custom_material_type: plan.customMaterialType,
573
+ } : {}),
482
574
  kinds: queue,
483
575
  };
484
576
  const saveProgress = () => {
@@ -536,7 +628,7 @@ async function runGenerate(ctx, invocation, session, focusRaw = '', taskList) {
536
628
  const batchNo = item.batches + 1;
537
629
  const file = path.join(
538
630
  runDir,
539
- String(index + 1).padStart(3, '0') + '_' + item.kind + '_' + String(batchNo).padStart(2, '0') + '.jsonl',
631
+ String(index + 1).padStart(3, '0') + '_' + (item.kind || 'custom') + '_' + String(batchNo).padStart(2, '0') + '.jsonl',
540
632
  );
541
633
  const includeBase = batchNo === 1 || batchNo % FULL_PROMPT_EVERY === 1;
542
634
  let attemptDetail = '';
@@ -574,6 +666,9 @@ async function runGenerate(ctx, invocation, session, focusRaw = '', taskList) {
574
666
  queryScript: tools.queryScript,
575
667
  existingIndex: existing.file,
576
668
  existingTotal: existing.count,
669
+ custom: isCustom ? { genre: plan.customGenre, materialType: plan.customMaterialType } : null,
670
+ customKindPending: isCustom && !item.kind,
671
+ sourceFixed: plan.mode === 'other' ? 'other' : 'builtin',
577
672
  });
578
673
  const sent = await llmFollowup(invocation, prompt, { appendTempRule: false });
579
674
  if (!sent || sent.ok !== true) {
@@ -591,8 +686,26 @@ async function runGenerate(ctx, invocation, session, focusRaw = '', taskList) {
591
686
  if (fs.existsSync(file)) {
592
687
  const checked = await checkFile(file);
593
688
  const genreProblem = checked.code === 0 ? genreCoverageProblem(file, plan.genres) : '';
594
- const kindProblem = checked.code === 0 ? kindCoverageProblem(file, item.kind) : '';
595
- const sourceProblem = checked.code === 0 ? sourceCoverageProblem(file) : '';
689
+ let kindProblem = '';
690
+ if (checked.code === 0) {
691
+ if (isCustom && !item.kind) {
692
+ const detected = detectSingleKind(file);
693
+ if (detected.problem) {
694
+ kindProblem = detected.problem;
695
+ } else {
696
+ item.kind = detected.kind;
697
+ // 首次知道 kind,先把“导入前的库内数量”记下来,
698
+ // 否则 beforeCount=0 会把同 kind 的历史素材也算成本次新增。
699
+ item.db_existing = await countKind(root, item.kind, plan.genres);
700
+ kindProblem = kindCoverageProblem(file, item.kind);
701
+ }
702
+ } else {
703
+ kindProblem = kindCoverageProblem(file, item.kind);
704
+ }
705
+ }
706
+ const sourceProblem = checked.code === 0
707
+ ? sourceCoverageProblem(file, plan.mode === 'other' ? 'other' : 'builtin')
708
+ : '';
596
709
  if (checked.code === 0 && !genreProblem && !kindProblem && !sourceProblem) {
597
710
  try {
598
711
  await importFile(root, file, { noPrefetch: true });
@@ -652,12 +765,24 @@ async function runGenerate(ctx, invocation, session, focusRaw = '', taskList) {
652
765
  attemptDetail = genreProblem;
653
766
  continue;
654
767
  }
768
+ if (isCustom && !item.kind) {
769
+ // 自定义素材第一轮:模型自己选 kind,插件记住它,
770
+ // 后续批次和计数都沿用同一个 kind。
771
+ const detected = detectSingleKind(file);
772
+ if (detected.problem) {
773
+ attemptDetail = detected.problem;
774
+ continue;
775
+ }
776
+ item.kind = detected.kind;
777
+ // 同上:第一次知道 kind 时先取库内基线。
778
+ item.db_existing = await countKind(root, item.kind, plan.genres);
779
+ }
655
780
  const kindProblem = kindCoverageProblem(file, item.kind);
656
781
  if (kindProblem) {
657
782
  attemptDetail = kindProblem;
658
783
  continue;
659
784
  }
660
- const sourceProblem = sourceCoverageProblem(file);
785
+ const sourceProblem = sourceCoverageProblem(file, plan.mode === 'other' ? 'other' : 'builtin');
661
786
  if (sourceProblem) {
662
787
  attemptDetail = sourceProblem;
663
788
  continue;
@@ -743,7 +868,7 @@ async function runGenerate(ctx, invocation, session, focusRaw = '', taskList) {
743
868
  report.imported_total = importedTotal;
744
869
  report.batches = batchCount;
745
870
  const notStarted = queue.filter((item) => item.status === 'pending' || item.status === 'running');
746
- report.not_started = notStarted.map((item) => item.kind);
871
+ report.not_started = notStarted.map((item) => item.kind || item.label || '自定义素材');
747
872
  report.status = aborted
748
873
  ? 'aborted'
749
874
  : ((incomplete.length || notStarted.length || fatal || stopReason) ? 'incomplete' : 'complete');
@@ -778,6 +903,10 @@ function materialTaskTitles(invocation) {
778
903
  if (command === 'export') return ['读取素材库', '导出 JSONL'];
779
904
  if (command === 'init') return ['检查素材库', '初始化 schema 并部署查询脚本'];
780
905
  if (command === 'remove') return ['确认删除范围', '删除素材库数据'];
906
+ const special = new Set(['help', '?', 'import', 'export', 'init', 'remove', 'prompt']);
907
+ if (!special.has(command) && tokens.length >= 2) {
908
+ return ['解析自定义题材与素材类型', '读取已有素材索引', '分批生成并导入素材', '汇总生成结果'];
909
+ }
781
910
  return ['解析素材类型与工作区', '读取题材与已有素材索引', '分批生成并导入素材', '汇总生成结果'];
782
911
  }
783
912
 
@@ -817,6 +946,7 @@ export async function apply(ctx) {
817
946
  }
818
947
  }
819
948
 
949
+ const SPECIAL = new Set(['import', 'export', 'init', 'remove', 'prompt']);
820
950
  try {
821
951
  if (!command) return await runGenerate(ctx, invocation, session, '', taskList);
822
952
  if (command === 'import') {
@@ -835,7 +965,19 @@ export async function apply(ctx) {
835
965
  if (tokens.length > 1) return notify(session, 'error', '用法:/fancy-material remove(不接受额外参数)');
836
966
  return await runRemove(ctx, invocation, session, taskList);
837
967
  }
838
- // 其它参数视为素材类型重点,例如 /fancy-material 地名
968
+ // 两参数形式:/fancy-material <题材> <素材类型>。
969
+ // 题材和素材类型都可以随便写;source 记 other,不占用内置目录。
970
+ if (!SPECIAL.has(command) && tokens.length >= 2) {
971
+ const genre = stripQuotes(tokens[0]);
972
+ const materialType = stripQuotes(tokens.slice(1).join(' '));
973
+ if (!genre || !materialType) {
974
+ return notify(session, 'error', '用法:/fancy-material <题材> <素材类型>(两个参数都不能为空)');
975
+ }
976
+ return await runGenerate(ctx, invocation, session, '', taskList, {
977
+ custom: { genre, materialType },
978
+ });
979
+ }
980
+ // 其它单参数视为素材类型重点,例如 /fancy-material 地名
839
981
  return await runGenerate(ctx, invocation, session, raw, taskList);
840
982
  } catch (err) {
841
983
  return notify(session, 'error', '❌ ' + (err && err.message ? err.message : String(err)));
@@ -311,3 +311,26 @@ export function focusBlock(focus) {
311
311
  const list = focus.kinds.map((kind) => `\`${kind}\`(${KIND_LABELS[kind] || kind})`).join('、');
312
312
  return `本批用户指定的重点是「${focus.query}」,对应 ${list}。插件会按顺序逐个 kind 排队,每个批次只写一个 kind;不要生成其它 kind。其他质量、题材、追加和查询规则继续适用。`;
313
313
  }
314
+
315
+ /**
316
+ * 自定义题材 + 自定义素材类型的本批规则。
317
+ *
318
+ * 与内置目录最大的区别:
319
+ * - source 必须是 other;
320
+ * - 素材类型可以不在 54 个 kind 里,模型可以自拟 kind;
321
+ * - 同一批只能用一个 kind,插件会在第一轮之后记住它。
322
+ */
323
+ export function customFocusBlock({ genre, materialType, kinds = [] }) {
324
+ const known = Array.isArray(kinds) && kinds.length
325
+ ? '用户素材类型匹配到内置 kind:' + kinds.map((kind) => `\`${kind}\`(${KIND_LABELS[kind] || kind})`).join('、') + '。'
326
+ : '用户素材类型不在内置表内;请自拟一个稳定的小写 snake_case kind(例如 `custom_xxx`)。';
327
+ return `本批是**用户自定义素材**:题材「${genre}」,素材类型「${materialType}」。
328
+
329
+ - ${known}
330
+ - 如果素材类型明显对应总表里的某个 kind,优先使用内置 kind;否则自拟 kind。
331
+ - 同一批文件里的所有记录必须使用**同一个** kind;下一批插件会沿用你这次确定的 kind。
332
+ - source 固定写 \`other\`,不要写 \`builtin\`。
333
+ - tags 和 genres 都是数组:先想清楚这个素材能适配哪些题材/标签,再全部写进去。例如「都市高武」相关素材可以写 tags: ["都市","高武","都市高武", ...],genres: ["都市高武","都市","高武"]。
334
+ - genres 必须原样包含用户题材「${genre}」;可以再附加其它适用题材(1–5 个)。
335
+ - 其他质量、查询、追加规则继续适用。`;
336
+ }
@@ -2,30 +2,55 @@
2
2
  * fancy-material command text for atomic material blocks.
3
3
  */
4
4
 
5
+ import { BUILTIN_GENRES } from '#material-topic';
6
+ import { KIND_GROUPS, KIND_LABELS, MATERIAL_KINDS } from './kinds.js';
7
+
8
+ function wrapList(items, perLine = 8) {
9
+ const lines = [];
10
+ for (let i = 0; i < items.length; i += perLine) {
11
+ lines.push(' ' + items.slice(i, i + perLine).join('、'));
12
+ }
13
+ return lines.join('\n');
14
+ }
15
+
5
16
  export function helpText() {
17
+ const genreBlock = wrapList(BUILTIN_GENRES, 8);
18
+ const kindBlock = KIND_GROUPS.map((group) => {
19
+ const items = group.kinds.map((kind) => `${kind}(${KIND_LABELS[kind] || kind})`);
20
+ return `- ${group.title}(${group.kinds.length}):` + items.join('、');
21
+ }).join('\n');
6
22
  return `🧩 素材库 用法
7
23
 
8
- /fancy-material 生成全部素材类型:54 个 kind,每个新增 50 条
9
- /fancy-material <素材类型> 只生成指定类型,例如:/fancy-material 地名
10
- /fancy-material import <文件> 导入 JSONL;导入前先校验格式
11
- /fancy-material init 创建/补齐素材库 schema、索引和查询/校验脚本
12
- /fancy-material remove 删除整个素材库(会二次确认)
13
- /fancy-material prompt 展示全类型提示词,不发送给 LLM
14
- /fancy-material export 把素材库导出为 <工作区>/素材库.md
15
- /fancy-material help 显示本帮助
24
+ /fancy-material 生成全部素材类型:${MATERIAL_KINDS.length} 个 kind,每个新增 50 条
25
+ /fancy-material <素材类型> 只生成指定类型,例如:/fancy-material 地名
26
+ /fancy-material <题材> <素材类型> 自定义题材+素材类型,source=other,例如:/fancy-material 都市高武 地名
27
+ /fancy-material import <文件> 导入 JSONL;导入前先校验格式
28
+ /fancy-material init 创建/补齐素材库 schema、索引和查询/校验脚本
29
+ /fancy-material remove 删除整个素材库(会二次确认)
30
+ /fancy-material prompt 展示全类型提示词,不发送给 LLM
31
+ /fancy-material export 把素材库导出为 <工作区>/素材库.md
32
+ /fancy-material help 显示本帮助
16
33
 
17
34
  说明:
18
35
  - 素材是原子积木,每条只有 id / kind / name / tags / genres / note / source。
36
+ - tags 和 genres 都是数组:tags 3–8 个,genres 1–5 个;一个素材可以同时适配多个题材/标签。
19
37
  - 每次运行:每个 kind 新增 50 条;每批最多 10 条,插件自动分批直到完成。
20
- - 未指定类型:54 个 kind 全部排队。
38
+ - 未指定类型:${MATERIAL_KINDS.length} 个 kind 全部排队。
21
39
  - 题材来源:优先 _novel_topic.json,其次 _novel_topic_suggestion.json;都没有时弹框确认后使用内置全题材。
22
- - 指定类型:只生成指定 kind,题材来源不变。
40
+ - 指定类型:只生成指定 kind,题材来源不变,source=builtin。
41
+ - 自定义题材+素材类型:题材和素材类型都可以随便写,插件把它们交给 LLM;生成结果 source=other,不占用内置 kind 目录。
23
42
  - 不设库内上限:运行两次就是每个 kind 累计 100 条;已有素材只用于去重。
24
43
  - 中断后重跑会开始新一轮 50 条配额,已经导入的素材不会撤销。
25
44
  - 只追加,不覆盖;同 kind 同 name 的素材会被跳过。
26
45
  - 全文索引:name(中文 ngram);tags / genres 精确过滤。
27
46
  - 查询脚本:<工作区>/.fancy-tools/fancy-material/query.js
28
- - 校验脚本:<工作区>/.fancy-tools/fancy-material/validate.js`;
47
+ - 校验脚本:<工作区>/.fancy-tools/fancy-material/validate.js
48
+
49
+ 内置题材(${BUILTIN_GENRES.length}):
50
+ ${genreBlock}
51
+
52
+ 素材类型(${MATERIAL_KINDS.length}):
53
+ ${kindBlock}`;
29
54
  }
30
55
 
31
56
  export const DEFAULT_TARGET_PER_KIND = 50;
@@ -46,14 +71,17 @@ export function promptDisplayText(prompt) {
46
71
  return `📚 fancy-material 提示词(未发送给 LLM)\n\n${String(prompt || '').trim()}`;
47
72
  }
48
73
 
49
- function compactRules(genres, queryScript, workspace) {
74
+ function compactRules(genres, queryScript, workspace, sourceFixed = 'builtin') {
50
75
  const lines = [
51
76
  '## 本批规则(与最初提示词一致)',
52
77
  '',
53
78
  '- 只写本批指定的一个 kind;不要把其它 kind 混进同一个文件。',
54
79
  '- 每条必须独立、具体、可复用;拒绝“火红、耀眼、神秘、强大”这类空泛词。',
55
80
  '- 命名类必须有具体锚点;拒绝“复原眼、章眼、旧仓单、神秘剑、黑色长刀”这类废名。',
56
- '- 字段只能是 id / kind / name / tags / genres / note / source;source 写 builtin;tags 3–8 个,genres 1–3 个。',
81
+ '- 字段只能是 id / kind / name / tags / genres / note / source;'
82
+ + 'source 写 ' + sourceFixed + ';tags 3–8 个,genres 1–5 个。',
83
+ '- tags 和 genres 都是数组:一个素材能适配多个题材/标签时,把它们都写进去;'
84
+ + '例如「都市高武」可以写 tags: ["都市","高武","都市高武", ...]。',
57
85
  ];
58
86
  if (Array.isArray(genres) && genres.length) {
59
87
  lines.push('- genres 必须原样包含本次题材之一:' + genres.join('、'));
@@ -90,6 +118,9 @@ export function generationPrompt({
90
118
  queryScript,
91
119
  existingIndex,
92
120
  existingTotal,
121
+ custom = null,
122
+ customKindPending = false,
123
+ sourceFixed = 'builtin',
93
124
  }) {
94
125
  const lines = [];
95
126
  if (includeBase && base) {
@@ -97,7 +128,7 @@ export function generationPrompt({
97
128
  } else {
98
129
  lines.push('## 继续生成(同一素材库)', '');
99
130
  lines.push('继续遵守最初提示词中的角色、写作原则、质量红线、命名标准、描写标准、查询与追加规则。不要重新解释规则,直接写本批素材。', '');
100
- lines.push(...compactRules(genres, queryScript, workspace), '');
131
+ lines.push(...compactRules(genres, queryScript, workspace, sourceFixed), '');
101
132
  }
102
133
 
103
134
  lines.push('## 本次运行', '');
@@ -110,9 +141,16 @@ export function generationPrompt({
110
141
  ? 'suggestion(_novel_topic_suggestion.json)'
111
142
  : mode === 'builtin'
112
143
  ? 'builtin(内置全题材)'
113
- : String(mode || '未指定');
144
+ : mode === 'other'
145
+ ? 'other(用户自定义题材 + 素材类型)'
146
+ : String(mode || '未指定');
114
147
  lines.push('- 题材模式:' + modeLabel);
115
148
  if (Array.isArray(genres) && genres.length) lines.push('- 本次题材:' + genres.join('、'));
149
+ if (custom) {
150
+ lines.push('- 用户素材类型:' + custom.materialType);
151
+ lines.push('- 素材来源:**other**(用户自定义素材,不是内置目录)');
152
+ lines.push('- 标签要求:一个素材可以适配多个题材/标签;tags 和 genres 都要写全,不要只写一个主题材。');
153
+ }
116
154
  if (focus && focus.query) {
117
155
  const kinds = (focus.kinds || []).map((item) => `\`${item}\``).join('、');
118
156
  lines.push('- 本次重点:' + focus.query + (kinds ? '(' + kinds + ')' : ''));
@@ -129,6 +167,13 @@ export function generationPrompt({
129
167
  lines.push('- 本批要写:' + requestCount + ' 条(只写 `' + kind + '`)');
130
168
  lines.push('- 写不满时可以少写;不要用重复、换皮、空泛命名凑数,插件会继续补下一批');
131
169
  lines.push('- 写完本批后立即结束本轮;插件会自动发下一批,不要在同一个文件里继续写其它 kind');
170
+ } else if (customKindPending) {
171
+ lines.push('- 本次目标:用户自定义素材 1 个 kind,本次配额 ' + targetPerKind + ' 条');
172
+ lines.push('- 本批 kind:**由你选择**。如果用户素材类型对应素材类型总表里的某个 kind,直接用该 kind;否则自拟一个稳定的小写 snake_case kind(例如 `custom_xxx`)。');
173
+ lines.push('- 同一批文件里的所有记录必须使用同一个 kind;下一批会沿用你这次确定的 kind。');
174
+ lines.push('- 本批要写:' + requestCount + ' 条');
175
+ lines.push('- 写不满时可以少写;不要用重复、换皮、空泛命名凑数,插件会继续补下一批');
176
+ lines.push('- 写完本批后立即结束本轮;插件会自动发下一批,不要在同一个文件里继续写其它 kind');
132
177
  } else {
133
178
  lines.push('- 本次目标:每个 kind ' + targetPerKind + ' 条');
134
179
  lines.push('- 每批最多:' + batchSize + ' 条');
@@ -139,14 +184,15 @@ export function generationPrompt({
139
184
 
140
185
  lines.push('');
141
186
  lines.push('写入要求:');
142
- lines.push('1. 只把 JSONL 写进目标文件,一行一个对象;字段只能是 id / kind / name / tags / genres / note / source,source 固定写 builtin。');
187
+ lines.push('1. 只把 JSONL 写进目标文件,一行一个对象;字段只能是 id / kind / name / tags / genres / note / source,source 固定写 ' + sourceFixed + '。');
188
+ lines.push(' tags 和 genres 都是数组:一个素材能适配多个题材/标签时,把它们都写进去(例如 都市高武 → 都市、高武、都市高武)。');
143
189
  lines.push('2. 不要在文件里写 Markdown 代码围栏、标题、解释、前后缀。');
144
190
  lines.push('3. 可以直接写 JSONL,也可以使用 Python/Node 生成器;生成器放 <工作区>/.tmp/,输出必须是目标 JSONL。');
145
191
  lines.push('4. 本批文件只放本批素材;数据库导入只追加,不会覆盖已有素材。');
146
192
  lines.push('5. 生成前先用查询脚本/索引检查候选名字;同 kind 同 name 已存在就换一个。');
147
193
  lines.push('6. 写完后运行 validate.js;失败就按错误修正,直到通过。');
148
194
  lines.push('7. 不要自己运行导入命令;插件会负责最终校验、按 kind+name 去重并追加导入。');
149
- if (kind) {
195
+ if (kind || customKindPending) {
150
196
  lines.push('8. 本批写完后(尽量写满 ' + requestCount + ' 条)通过校验,只回复:已写入 <目标文件路径>;然后结束本轮,等插件发下一批。');
151
197
  } else {
152
198
  lines.push('8. 写完后只回复:已写入 <目标文件路径>。');
@@ -210,7 +256,7 @@ export function generationRunText({
210
256
  if (incomplete.length) {
211
257
  lines.push('未完成:' + incomplete.length + ' 个 kind');
212
258
  for (const item of incomplete.slice(0, 20)) {
213
- lines.push(' - ' + item.kind + ':' + item.generated + '/' + targetPerKind
259
+ lines.push(' - ' + (item.label || item.kind || '自定义素材') + ':' + item.generated + '/' + targetPerKind
214
260
  + (item.reason ? '(' + item.reason + ')' : ''));
215
261
  }
216
262
  if (incomplete.length > 20) lines.push(' - 其余 ' + (incomplete.length - 20) + ' 个省略');
@@ -26,7 +26,7 @@ function usage() {
26
26
  - source 只能是 builtin / other
27
27
  - builtin 的 kind 必须在允许列表内,id 以 kind 开头
28
28
  - other 的 kind 不受限制
29
- - tags 3–8 个,genres 1–3 个
29
+ - tags 3–8 个,genres 1–5 个(都是数组)
30
30
  - 同文件内 kind + name 不重复
31
31
 
32
32
  退出码:0=通过,1=有错误,2=用法错误`;
@@ -53,8 +53,8 @@ function validate(raw, line) {
53
53
  if (typeof tag !== 'string' || !tag.trim()) errors.push('第 ' + line + ' 行:tags 元素必须是非空字符串');
54
54
  }
55
55
  }
56
- if (!Array.isArray(raw.genres) || raw.genres.length < 1 || raw.genres.length > 3) {
57
- errors.push('第 ' + line + ' 行:genres 需要 1–3 项');
56
+ if (!Array.isArray(raw.genres) || raw.genres.length < 1 || raw.genres.length > 5) {
57
+ errors.push('第 ' + line + ' 行:genres 需要 1–5 项');
58
58
  } else {
59
59
  for (const genre of raw.genres) {
60
60
  if (typeof genre !== 'string' || !genre.trim()) errors.push('第 ' + line + ' 行:genres 元素必须是非空字符串');