@cloud411716/fancy-webnovel 4.1.0 → 4.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/material-library/README.md +34 -11
- package/docs/material-library/import.js +13 -4
- package/docs/material-library/prompt.md +249 -198
- package/docs/material-library/sample.jsonl +2 -2
- package/docs/material-library/schema.surql +5 -2
- package/lib/material-db.js +4 -4
- 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/prompts/01-analysis-prompt.md +1 -1
- package/plugins/fancy-material/exporter.js +3 -1
- package/plugins/fancy-material/index.js +179 -37
- package/plugins/fancy-material/inventory.js +3 -2
- package/plugins/fancy-material/kinds.js +111 -5
- package/plugins/fancy-material/scripts/query.js +4 -2
- package/plugins/fancy-material/templates.js +65 -19
- package/plugins/fancy-material/validate.js +12 -5
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 = 7;
|
|
21
21
|
|
|
22
22
|
const SCHEMA_FILE = fileURLToPath(new URL('../docs/material-library/schema.surql', import.meta.url));
|
|
23
23
|
|
|
@@ -57,7 +57,7 @@ export async function connectMaterial(root) {
|
|
|
57
57
|
/**
|
|
58
58
|
* Ensure the material database and its schema exist. Idempotent.
|
|
59
59
|
*
|
|
60
|
-
* Schema
|
|
60
|
+
* Schema v7 adds `group_id` and the 58-kind catalog. It keeps the v6 `source` field. Before applying the
|
|
61
61
|
* new ASSERT, rows belonging to retired kinds are removed; otherwise SurrealDB
|
|
62
62
|
* refuses to overwrite the field because existing records no longer conform.
|
|
63
63
|
*
|
|
@@ -97,8 +97,8 @@ export async function ensureMaterialDatabase(root) {
|
|
|
97
97
|
await db.query(materialSchemaText());
|
|
98
98
|
const tableInfo = await db.query('INFO FOR TABLE material;');
|
|
99
99
|
const table = Array.isArray(tableInfo) && tableInfo[0] ? tableInfo[0] : {};
|
|
100
|
-
if (!table.fields || !table.fields.source || !table.fields.kind || !table.fields.name || !table.fields.tags) {
|
|
101
|
-
throw new Error('素材库 schema 初始化后仍未找到 material.kind/name/tags 字段。');
|
|
100
|
+
if (!table.fields || !table.fields.source || !table.fields.kind || !table.fields.name || !table.fields.tags || !table.fields.group_id) {
|
|
101
|
+
throw new Error('素材库 schema 初始化后仍未找到 material.kind/name/tags/group_id 字段。');
|
|
102
102
|
}
|
|
103
103
|
return {
|
|
104
104
|
ok: true,
|
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 || 7) + ')',
|
|
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 || 7) + ')',
|
|
155
155
|
'项目库:' + result.projects.length + ' 个' + (failed.length ? ',失败 ' + failed.length + ' 个' : ''),
|
|
156
156
|
...failed.map((f) => '❌ ' + f.project.name + ':' + f.error),
|
|
157
157
|
].join('\n'));
|
|
@@ -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–
|
|
50
|
+
- `genres` 1–5 个题材;如果素材同时适配多个题材/标签,把它们都写进去;拿不准就写 `["通用"]`。
|
|
51
51
|
- `note` 可写一句补充,没有就写空字符串。
|
|
52
52
|
- `id` 用小写字母、数字、下划线,格式建议:`<kind>_dy_<作品ID>_<序号>`,全局唯一。
|
|
53
53
|
|
|
@@ -97,6 +97,7 @@ export function renderMaterialMarkdown(records, meta = {}) {
|
|
|
97
97
|
lines.push('');
|
|
98
98
|
lines.push('- **tags**:' + valueText(record.tags));
|
|
99
99
|
lines.push('- **genres**:' + valueText(record.genres));
|
|
100
|
+
if (record.group_id) lines.push('- **group_id**:' + text(record.group_id));
|
|
100
101
|
if (record.source) lines.push('- **source**:' + text(record.source));
|
|
101
102
|
if (record.note) lines.push('- **note**:' + text(record.note));
|
|
102
103
|
lines.push('');
|
|
@@ -117,6 +118,7 @@ export function renderMaterialMarkdown(records, meta = {}) {
|
|
|
117
118
|
lines.push('');
|
|
118
119
|
lines.push('- **tags**:' + valueText(record.tags));
|
|
119
120
|
lines.push('- **genres**:' + valueText(record.genres));
|
|
121
|
+
if (record.group_id) lines.push('- **group_id**:' + text(record.group_id));
|
|
120
122
|
if (record.source) lines.push('- **source**:' + text(record.source));
|
|
121
123
|
if (record.note) lines.push('- **note**:' + text(record.note));
|
|
122
124
|
lines.push('');
|
|
@@ -136,7 +138,7 @@ export function renderMaterialMarkdown(records, meta = {}) {
|
|
|
136
138
|
export async function exportMaterials(root) {
|
|
137
139
|
const { db, database } = await connectMaterial(root);
|
|
138
140
|
try {
|
|
139
|
-
const rows = await db.query('SELECT kind, name, tags, genres, note, source FROM material ORDER BY kind, name;');
|
|
141
|
+
const rows = await db.query('SELECT kind, name, tags, genres, group_id, note, source FROM material ORDER BY kind, name;');
|
|
140
142
|
const records = Array.isArray(rows) && Array.isArray(rows[0]) ? rows[0] : [];
|
|
141
143
|
const file = path.join(path.resolve(root), '素材库.md');
|
|
142
144
|
const markdown = renderMaterialMarkdown(records, {
|
|
@@ -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 (
|
|
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
|
|
249
|
-
+
|
|
250
|
-
|
|
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(不在 58 个内置 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
|
-
//
|
|
401
|
-
let plan
|
|
402
|
-
if (
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
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
|
-
|
|
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:
|
|
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
|
-
|
|
595
|
-
|
|
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
|
-
//
|
|
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)));
|
|
@@ -20,7 +20,7 @@ function clean(value) {
|
|
|
20
20
|
/**
|
|
21
21
|
* Write an existing-material index to <workspace>/.tmp.
|
|
22
22
|
*
|
|
23
|
-
* One JSON object per line: { kind, name, tags, genres }.
|
|
23
|
+
* One JSON object per line: { kind, name, tags, genres, group_id?, source }.
|
|
24
24
|
* It is intentionally a file, not prompt text: the model can grep/search it
|
|
25
25
|
* without loading a six-figure line list into the context window.
|
|
26
26
|
*
|
|
@@ -32,7 +32,7 @@ function clean(value) {
|
|
|
32
32
|
export async function buildExistingIndex(root, genres, stamp) {
|
|
33
33
|
const { db } = await connectMaterial(root);
|
|
34
34
|
try {
|
|
35
|
-
const rows = await db.query('SELECT kind, name, tags, genres, source FROM material ORDER BY kind, name;');
|
|
35
|
+
const rows = await db.query('SELECT kind, name, tags, genres, group_id, source FROM material ORDER BY kind, name;');
|
|
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
|
|
@@ -49,6 +49,7 @@ export async function buildExistingIndex(root, genres, stamp) {
|
|
|
49
49
|
name: clean(record.name),
|
|
50
50
|
tags: Array.isArray(record.tags) ? record.tags.map(clean) : [],
|
|
51
51
|
genres: Array.isArray(record.genres) ? record.genres.map(clean) : [],
|
|
52
|
+
...(record.group_id ? { group_id: clean(record.group_id) } : {}),
|
|
52
53
|
source: clean(record.source) || 'builtin',
|
|
53
54
|
}));
|
|
54
55
|
fs.writeFileSync(file, lines.length ? lines.join('\n') + '\n' : '', 'utf8');
|