@cloud411716/fancy-webnovel 4.30.21 → 4.30.23

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloud411716/fancy-webnovel",
3
- "version": "4.30.21",
3
+ "version": "4.30.23",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "exports": {
@@ -1831,6 +1831,8 @@ export async function runGenerationWithPlan(ctx, invocation, session, taskList,
1831
1831
  manualBlock,
1832
1832
  team,
1833
1833
  signal: invocation.signal,
1834
+ validate: (candidate) => validateBatch(candidate, item, groupId),
1835
+ batchLabel: item.kind + (item.subkind ? '_' + item.subkind : ''),
1834
1836
  batchId,
1835
1837
  batchNo,
1836
1838
  });
@@ -2059,6 +2061,8 @@ function teamText(root, config) {
2059
2061
  '',
2060
2062
  `配置文件:${teamConfigPath(root)}`,
2061
2063
  '',
2064
+ '注意:A、B、裁判必须使用不同模型,否则双生成和交叉审查没有意义。',
2065
+ '',
2062
2066
  '设置示例:',
2063
2067
  ' /fancy-material team set a opencode-go glm-5.3',
2064
2068
  ' /fancy-material team set b opencode-go kimi-k3',
@@ -2134,6 +2138,12 @@ async function handleTeamCommand(ctx, session, tokens) {
2134
2138
  ].filter(Boolean).join('\n'));
2135
2139
  }
2136
2140
  const field = key === 'a' ? 'generatorA' : key === 'b' ? 'generatorB' : 'judge';
2141
+ const clash = ['generatorA', 'generatorB', 'judge']
2142
+ .filter((name) => name !== field)
2143
+ .find((name) => config[name] && config[name].provider === provider && config[name].model === model);
2144
+ if (clash) {
2145
+ return notify(session, 'error', `❌ ${provider}/${model} 已被 ${clash} 使用。A、B、裁判必须使用不同模型。`);
2146
+ }
2137
2147
  config[field] = { provider, model };
2138
2148
  writeTeamConfig(root, config);
2139
2149
  return notify(session, 'success', `✅ ${field} 已设为 ${provider}/${model}。`);
@@ -84,6 +84,21 @@ export function teamConfigProblem(config) {
84
84
  if (!config.generatorB) missing.push('generatorB');
85
85
  if (!config.judge) missing.push('judge');
86
86
  if (missing.length) return 'team 模式缺少模型:' + missing.join('、') + '。用 /fancy-material team set 设置。';
87
+ const seen = new Map();
88
+ const duplicates = [];
89
+ for (const [label, route] of [
90
+ ['generatorA', config.generatorA],
91
+ ['generatorB', config.generatorB],
92
+ ['judge', config.judge],
93
+ ]) {
94
+ const key = `${route.provider}/${route.model}`;
95
+ if (seen.has(key)) duplicates.push(`${seen.get(key)} 与 ${label} 都是 ${key}`);
96
+ else seen.set(key, label);
97
+ }
98
+ if (duplicates.length) {
99
+ return 'team 模式的三个角色必须使用不同模型:' + duplicates.join(';')
100
+ + '。同模型会让双生成和交叉审查失去意义。';
101
+ }
87
102
  return '';
88
103
  }
89
104
 
@@ -156,6 +171,27 @@ function writeJsonFile(file, value) {
156
171
  fs.writeFileSync(file, JSON.stringify(value, null, 2) + '\n', 'utf8');
157
172
  }
158
173
 
174
+ /** Preserve the previous attempt's A/B/review artifacts before a retry overwrites them. */
175
+ function archivePreviousTeamArtifacts(runDir) {
176
+ const metaFile = path.join(runDir, 'team-meta.json');
177
+ let meta;
178
+ try { meta = JSON.parse(fs.readFileSync(metaFile, 'utf8')); } catch { return; }
179
+ const label = String(meta.batchLabel || meta.batchNo || 'batch').replace(/[^\w\u4e00-\u9fa5.-]+/g, '-');
180
+ const stamp = String(meta.startedAt || Date.now()).replace(/[:.]/g, '-');
181
+ const dir = path.join(runDir, 'team', `${label}-${stamp}`);
182
+ try {
183
+ fs.mkdirSync(dir, { recursive: true });
184
+ for (const name of [
185
+ 'team-prompt-a.txt', 'team-prompt-b.txt', 'team-rubric.md',
186
+ 'team-a.jsonl', 'team-b.jsonl', 'team-final.jsonl',
187
+ 'team-meta.json', 'team-review.json',
188
+ ]) {
189
+ const source = path.join(runDir, name);
190
+ if (fs.existsSync(source)) fs.copyFileSync(source, path.join(dir, name));
191
+ }
192
+ } catch { /* best-effort diagnostics */ }
193
+ }
194
+
159
195
  const TEAM_RUBRIC = `
160
196
  # Team 评审 rubric
161
197
 
@@ -279,7 +315,11 @@ async function callSubagent({ subagents, parent, prompt, route, label, schema, s
279
315
  }
280
316
  return { ok: true, value: textFromBlocks(result.output) };
281
317
  } catch (err) {
282
- return { ok: false, error: err && err.message ? err.message : String(err) };
318
+ return {
319
+ ok: false,
320
+ error: err && err.message ? err.message : String(err),
321
+ stack: err && err.stack ? String(err.stack) : '',
322
+ };
283
323
  } finally {
284
324
  if (timer) clearTimeout(timer);
285
325
  try { await activation.dispose(); } catch { /* best effort */ }
@@ -321,6 +361,13 @@ const judgePrompt = (round, aPassed, bPassed, history, fileA, fileB, finalFile,
321
361
  '如果选择 PICK_A、PICK_B 或 MERGE,把最终 JSONL 写到 ' + finalFile + '。' +
322
362
  '返回 {"decision":"...","rationale":"..."}。';
323
363
 
364
+ const repairPrompt = (candidateFile, detail, finalFile) =>
365
+ '你是主编。当前最终文件没有通过插件校验。\n读取 ' + candidateFile + '。\n' +
366
+ '校验失败原因:\n' + String(detail || '').slice(0, 12000) + '\n' +
367
+ '只修复被点名的条目;保持其余条目、条数、group_id、subkind、world_id、genres 和 source 不变。' +
368
+ '把修复后的完整 JSONL 写到 ' + finalFile + '。' +
369
+ '返回 {"decision":"MERGE","rationale":"..."}。';
370
+
324
371
  /**
325
372
  * Run one batch through two generators, bounded cross-review, and a judge.
326
373
  * Uses the host subagent service directly so the plugin does not depend on a
@@ -336,6 +383,8 @@ export async function runTeamBatch({
336
383
  manualBlock = '',
337
384
  team,
338
385
  signal,
386
+ validate,
387
+ batchLabel = 'batch',
339
388
  batchId = '',
340
389
  batchNo = 1,
341
390
  }) {
@@ -345,6 +394,7 @@ export async function runTeamBatch({
345
394
  }
346
395
  const problem = teamConfigProblem(team);
347
396
  if (problem) return { ok: false, error: problem };
397
+ const parentSignal = signal || new AbortController().signal;
348
398
 
349
399
  const aFile = path.join(runDir, 'team-a.jsonl');
350
400
  const bFile = path.join(runDir, 'team-b.jsonl');
@@ -352,6 +402,7 @@ export async function runTeamBatch({
352
402
  const promptAFile = path.join(runDir, 'team-prompt-a.txt');
353
403
  const promptBFile = path.join(runDir, 'team-prompt-b.txt');
354
404
  const rubricFile = path.join(runDir, 'team-rubric.md');
405
+ archivePreviousTeamArtifacts(runDir);
355
406
  for (const stale of [aFile, bFile, finalFile]) {
356
407
  try { fs.unlinkSync(stale); } catch { /* first run */ }
357
408
  }
@@ -359,50 +410,63 @@ export async function runTeamBatch({
359
410
  writePromptFile(promptBFile, appendTeamRole(retargetPrompt(prompt, file, bFile), 'B', bFile, aFile));
360
411
  writePromptFile(rubricFile, `${manualBlock || ''}\n\n${TEAM_RUBRIC}`);
361
412
 
413
+ const metaFile = path.join(runDir, 'team-meta.json');
414
+ const reviewFile = path.join(runDir, 'team-review.json');
415
+ const meta = {
416
+ batchId,
417
+ batchLabel,
418
+ batchNo,
419
+ startedAt: new Date().toISOString(),
420
+ generatorA: team.generatorA,
421
+ generatorB: team.generatorB,
422
+ judge: team.judge,
423
+ reviewMode: team.reviewMode,
424
+ maxReviewRounds: team.maxReviewRounds,
425
+ maxTotalCalls: Math.max(6, Number(team.maxTotalCalls) || 12),
426
+ phase: 'generate',
427
+ };
428
+ writeJsonFile(metaFile, meta);
429
+
362
430
  let calls = 0;
363
431
  const maxCalls = Math.max(6, Number(team.maxTotalCalls) || 12);
364
432
  const call = async (promptText, route, label, schema) => {
365
433
  if (calls >= maxCalls) return { ok: false, error: 'team workflow reached maxTotalCalls' };
366
434
  calls += 1;
367
- return callSubagent({ subagents, parent, prompt: promptText, route, label, schema });
435
+ return callSubagent({ subagents, parent, prompt: promptText, route, label, schema, signal: parentSignal });
368
436
  };
369
437
 
370
438
  const [genA, genB] = await Promise.all([
371
439
  call(genPrompt(promptAFile, aFile, bFile, 'A'), team.generatorA, 'gen-a'),
372
440
  call(genPrompt(promptBFile, bFile, aFile, 'B'), team.generatorB, 'gen-b'),
373
441
  ]);
374
- if (!genA.ok) return { ok: false, error: '生成者 A 失败:' + genA.error };
375
- if (!genB.ok) return { ok: false, error: '生成者 B 失败:' + genB.error };
442
+ meta.calls = calls;
443
+ if (!genA.ok || !genB.ok) {
444
+ meta.error = !genA.ok ? '生成者 A 失败:' + genA.error : '生成者 B 失败:' + genB.error;
445
+ meta.genA = genA;
446
+ meta.genB = genB;
447
+ writeJsonFile(metaFile, meta);
448
+ return { ok: false, error: meta.error + (genA.stack ? '\n' + genA.stack : '') + (genB.stack ? '\n' + genB.stack : '') };
449
+ }
376
450
  for (const [label, candidate] of [['A', aFile], ['B', bFile]]) {
377
451
  if (!fs.existsSync(candidate) || fs.statSync(candidate).size === 0) {
378
- return { ok: false, error: `生成者 ${label} 没有写出文件:${candidate}` };
452
+ meta.error = `生成者 ${label} 没有写出文件:${candidate}`;
453
+ writeJsonFile(metaFile, meta);
454
+ return { ok: false, error: meta.error };
379
455
  }
380
456
  }
381
457
 
382
- const metaFile = path.join(runDir, 'team-meta.json');
383
- const reviewFile = path.join(runDir, 'team-review.json');
384
- const parentSignal = signal || new AbortController().signal;
458
+ meta.phase = 'review';
459
+ writeJsonFile(metaFile, meta);
385
460
  let aPassed = false;
386
461
  let bPassed = false;
387
462
  let round = 0;
388
463
  let stopReason = '';
389
464
  const history = [];
390
- const meta = {
391
- batchId,
392
- batchNo,
393
- startedAt: new Date().toISOString(),
394
- generatorA: team.generatorA,
395
- generatorB: team.generatorB,
396
- judge: team.judge,
397
- reviewMode: team.reviewMode,
398
- maxReviewRounds: team.maxReviewRounds,
399
- maxTotalCalls: maxCalls,
400
- };
401
- writeJsonFile(metaFile, meta);
402
465
 
403
466
  // Keep one call in reserve for the judge. A full review-response-revision
404
467
  // round costs up to six calls, so the budget check happens before each stage.
405
- const canSpend = (n) => calls + n <= maxCalls - 1;
468
+ const reserve = typeof validate === 'function' ? 2 : 1;
469
+ const canSpend = (n) => calls + n <= maxCalls - reserve;
406
470
 
407
471
  const saveReview = () => writeJsonFile(reviewFile, { round, aPassed, bPassed, calls, stopReason, history });
408
472
 
@@ -498,6 +562,8 @@ export async function runTeamBatch({
498
562
  return { ok: false, error };
499
563
  }
500
564
 
565
+ meta.phase = 'judge';
566
+ writeJsonFile(metaFile, meta);
501
567
  const judge = await call(
502
568
  judgePrompt(round, aPassed, bPassed, history, aFile, bFile, finalFile, rubricFile),
503
569
  team.judge,
@@ -514,22 +580,69 @@ export async function runTeamBatch({
514
580
  meta.calls = calls;
515
581
  meta.finishedAt = new Date().toISOString();
516
582
 
517
- if (fs.existsSync(finalFile)) {
518
- fs.copyFileSync(finalFile, file);
519
- meta.finalFile = finalFile;
520
- writeJsonFile(metaFile, meta);
521
- return { ok: true, result: { round, aPassed, bPassed, calls, judge: judge.value }, finalFile };
583
+ let candidate = fs.existsSync(finalFile) ? finalFile : '';
584
+ let fallback = false;
585
+ if (!candidate) {
586
+ const decision = judge.value && judge.value.decision;
587
+ const pick = decision === 'PICK_A' ? aFile : decision === 'PICK_B' ? bFile : '';
588
+ if (pick && fs.existsSync(pick)) {
589
+ candidate = pick;
590
+ fallback = true;
591
+ }
522
592
  }
523
- const decision = judge.value && judge.value.decision;
524
- const fallback = decision === 'PICK_A' ? aFile : decision === 'PICK_B' ? bFile : '';
525
- if (fallback && fs.existsSync(fallback)) {
526
- fs.copyFileSync(fallback, file);
527
- meta.finalFile = fallback;
528
- meta.fallback = true;
593
+ if (!candidate) {
594
+ meta.error = '裁判没有写出 final.jsonl(decision=' + ((judge.value && judge.value.decision) || 'none') + ')';
529
595
  writeJsonFile(metaFile, meta);
530
- return { ok: true, result: { round, aPassed, bPassed, calls, judge: judge.value }, finalFile: fallback, fallback: true };
596
+ return { ok: false, error: 'team workflow 完成,但' + meta.error + '。' };
531
597
  }
532
- meta.error = '裁判没有写出 final.jsonl(decision=' + (decision || 'none') + ')';
598
+
599
+ if (typeof validate === 'function') {
600
+ let checked = await validate(candidate);
601
+ if (!checked.ok) {
602
+ meta.validationError = checked.detail;
603
+ writeJsonFile(metaFile, meta);
604
+ if (calls >= maxCalls) {
605
+ meta.error = '最终文件未通过校验,且没有剩余调用可修复:' + checked.detail;
606
+ writeJsonFile(metaFile, meta);
607
+ return { ok: false, error: meta.error };
608
+ }
609
+ const repaired = await call(
610
+ repairPrompt(candidate, checked.detail, finalFile),
611
+ team.judge,
612
+ 'repair',
613
+ JUDGE_SCHEMA,
614
+ );
615
+ if (!repaired.ok) {
616
+ meta.error = '裁判修复失败:' + repaired.error;
617
+ writeJsonFile(metaFile, meta);
618
+ return { ok: false, error: meta.error };
619
+ }
620
+ meta.repair = repaired.value;
621
+ if (!fs.existsSync(finalFile)) {
622
+ meta.error = '裁判修复后没有写出 final.jsonl';
623
+ writeJsonFile(metaFile, meta);
624
+ return { ok: false, error: meta.error };
625
+ }
626
+ checked = await validate(finalFile);
627
+ if (!checked.ok) {
628
+ meta.error = '裁判修复后仍未通过校验:' + checked.detail;
629
+ writeJsonFile(metaFile, meta);
630
+ return { ok: false, error: meta.error };
631
+ }
632
+ candidate = finalFile;
633
+ fallback = false;
634
+ }
635
+ }
636
+
637
+ fs.copyFileSync(candidate, file);
638
+ meta.finalFile = candidate;
639
+ meta.fallback = fallback;
640
+ meta.validated = typeof validate === 'function';
533
641
  writeJsonFile(metaFile, meta);
534
- return { ok: false, error: 'team workflow 完成,但' + meta.error + '。' };
642
+ return {
643
+ ok: true,
644
+ result: { round, aPassed, bPassed, calls, judge: judge.value },
645
+ finalFile: candidate,
646
+ fallback,
647
+ };
535
648
  }
@@ -25,7 +25,7 @@ export function helpText() {
25
25
  /fancy-material continue 沿用上次的世界母体、facet、风格和范围,继续追加素材
26
26
  /fancy-material team show 查看双生成 + 交叉审查 + 裁判的配置
27
27
  /fancy-material team models [provider] 查看当前可用模型(默认 opencode-go)
28
- /fancy-material team set a|b|judge <provider> <model> 设置生成者/裁判模型
28
+ /fancy-material team set a|b|judge <provider> <model> 设置生成者/裁判模型(三者必须不同)
29
29
  /fancy-material team set rounds <1-10> 设置最多交叉审查轮次(默认 5)
30
30
  /fancy-material team set max-calls <6-40> 设置整个 workflow 最多 agent 调用
31
31
  /fancy-material team on|off 开/关 Team 模式