@cloud411716/fancy-webnovel 4.30.19 → 4.30.21
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
|
@@ -10,6 +10,7 @@ import fs from 'node:fs';
|
|
|
10
10
|
import path from 'node:path';
|
|
11
11
|
|
|
12
12
|
const TEAM_FILE = 'team.json';
|
|
13
|
+
const TEAM_CALL_TIMEOUT_MS = 20 * 60 * 1000;
|
|
13
14
|
const DEFAULT_TEAM = Object.freeze({
|
|
14
15
|
version: 1,
|
|
15
16
|
enabled: false,
|
|
@@ -122,8 +123,12 @@ function service(ctx, name) {
|
|
|
122
123
|
if (!ctx) return undefined;
|
|
123
124
|
if (typeof ctx.get === 'function') {
|
|
124
125
|
try {
|
|
125
|
-
const
|
|
126
|
-
if (
|
|
126
|
+
const active = ctx.get(name);
|
|
127
|
+
if (active !== undefined) return active;
|
|
128
|
+
} catch { /* optional service */ }
|
|
129
|
+
try {
|
|
130
|
+
const any = ctx.get(name, false);
|
|
131
|
+
if (any !== undefined) return any;
|
|
127
132
|
} catch { /* optional service */ }
|
|
128
133
|
}
|
|
129
134
|
try {
|
|
@@ -146,6 +151,11 @@ function writePromptFile(file, text) {
|
|
|
146
151
|
fs.writeFileSync(file, String(text || '').trim() + '\n', 'utf8');
|
|
147
152
|
}
|
|
148
153
|
|
|
154
|
+
function writeJsonFile(file, value) {
|
|
155
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
156
|
+
fs.writeFileSync(file, JSON.stringify(value, null, 2) + '\n', 'utf8');
|
|
157
|
+
}
|
|
158
|
+
|
|
149
159
|
const TEAM_RUBRIC = `
|
|
150
160
|
# Team 评审 rubric
|
|
151
161
|
|
|
@@ -166,14 +176,7 @@ PASS 仅当没有 blocker/major。minor 可以记录但不阻塞。
|
|
|
166
176
|
{ "verdict": "PASS" | "CHANGES", "issues": [{ "severity": "blocker|major|minor", "why": "...", "fix": "..." }] }
|
|
167
177
|
`;
|
|
168
178
|
|
|
169
|
-
|
|
170
|
-
const {
|
|
171
|
-
promptAFile, promptBFile, fileA, fileB, finalFile, rubricFile,
|
|
172
|
-
providerA, modelA, providerB, modelB, providerC, modelC,
|
|
173
|
-
maxRounds, maxCalls, reviewMode,
|
|
174
|
-
} = args
|
|
175
|
-
|
|
176
|
-
const reviewSchema = {
|
|
179
|
+
const REVIEW_SCHEMA = {
|
|
177
180
|
type: 'object',
|
|
178
181
|
properties: {
|
|
179
182
|
verdict: { type: 'string', enum: ['PASS', 'CHANGES'] },
|
|
@@ -193,9 +196,9 @@ const reviewSchema = {
|
|
|
193
196
|
},
|
|
194
197
|
required: ['verdict', 'issues'],
|
|
195
198
|
additionalProperties: false,
|
|
196
|
-
}
|
|
199
|
+
};
|
|
197
200
|
|
|
198
|
-
const
|
|
201
|
+
const RESPONSE_SCHEMA = {
|
|
199
202
|
type: 'object',
|
|
200
203
|
properties: {
|
|
201
204
|
decision: { type: 'string', enum: ['ACCEPT', 'REJECT'] },
|
|
@@ -203,9 +206,9 @@ const responseSchema = {
|
|
|
203
206
|
},
|
|
204
207
|
required: ['decision', 'reason'],
|
|
205
208
|
additionalProperties: false,
|
|
206
|
-
}
|
|
209
|
+
};
|
|
207
210
|
|
|
208
|
-
const
|
|
211
|
+
const VERDICT_SCHEMA = {
|
|
209
212
|
type: 'object',
|
|
210
213
|
properties: {
|
|
211
214
|
verdict: { type: 'string', enum: ['PASS', 'MAINTAIN'] },
|
|
@@ -213,9 +216,9 @@ const verdictSchema = {
|
|
|
213
216
|
},
|
|
214
217
|
required: ['verdict', 'reason'],
|
|
215
218
|
additionalProperties: false,
|
|
216
|
-
}
|
|
219
|
+
};
|
|
217
220
|
|
|
218
|
-
const
|
|
221
|
+
const REVISE_SCHEMA = {
|
|
219
222
|
type: 'object',
|
|
220
223
|
properties: {
|
|
221
224
|
changed: { type: 'boolean' },
|
|
@@ -223,9 +226,9 @@ const reviseSchema = {
|
|
|
223
226
|
},
|
|
224
227
|
required: ['changed', 'summary'],
|
|
225
228
|
additionalProperties: false,
|
|
226
|
-
}
|
|
229
|
+
};
|
|
227
230
|
|
|
228
|
-
const
|
|
231
|
+
const JUDGE_SCHEMA = {
|
|
229
232
|
type: 'object',
|
|
230
233
|
properties: {
|
|
231
234
|
decision: { type: 'string', enum: ['PICK_A', 'PICK_B', 'MERGE', 'REJECT'] },
|
|
@@ -233,151 +236,96 @@ const judgeSchema = {
|
|
|
233
236
|
},
|
|
234
237
|
required: ['decision', 'rationale'],
|
|
235
238
|
additionalProperties: false,
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
function textFromBlocks(blocks) {
|
|
242
|
+
return (Array.isArray(blocks) ? blocks : [])
|
|
243
|
+
.filter((block) => block && block.type === 'text')
|
|
244
|
+
.map((block) => String(block.text || ''))
|
|
245
|
+
.join('');
|
|
236
246
|
}
|
|
237
247
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
248
|
+
async function callSubagent({ subagents, parent, prompt, route, label, schema, signal }) {
|
|
249
|
+
let activation;
|
|
250
|
+
try {
|
|
251
|
+
activation = await subagents.startActivation({
|
|
252
|
+
provider: 'spawn',
|
|
253
|
+
label,
|
|
254
|
+
prompt: [{ type: 'text', text: prompt }],
|
|
255
|
+
parent,
|
|
256
|
+
signal,
|
|
257
|
+
delivery: 'caller',
|
|
258
|
+
agentOptions: { provider: route.provider, model: route.model },
|
|
259
|
+
...(schema ? { outputSchema: schema } : {}),
|
|
260
|
+
});
|
|
261
|
+
} catch (err) {
|
|
262
|
+
return { ok: false, error: err && err.message ? err.message : String(err) };
|
|
263
|
+
}
|
|
264
|
+
let timer = null;
|
|
265
|
+
try {
|
|
266
|
+
const result = await Promise.race([
|
|
267
|
+
activation.result,
|
|
268
|
+
new Promise((_, reject) => {
|
|
269
|
+
timer = setTimeout(() => reject(new Error(`子 agent ${label} 超时`)), TEAM_CALL_TIMEOUT_MS);
|
|
270
|
+
if (timer && typeof timer.unref === 'function') timer.unref();
|
|
271
|
+
}),
|
|
272
|
+
]);
|
|
273
|
+
if (!result || result.stopReason !== 'completed') {
|
|
274
|
+
return { ok: false, error: (result && (result.diagnostic || result.stopReason)) || '子 agent 未完成' };
|
|
275
|
+
}
|
|
276
|
+
if (schema) {
|
|
277
|
+
if (result.structured === undefined) return { ok: false, error: '子 agent 没有返回结构化结果' };
|
|
278
|
+
return { ok: true, value: result.structured };
|
|
279
|
+
}
|
|
280
|
+
return { ok: true, value: textFromBlocks(result.output) };
|
|
281
|
+
} catch (err) {
|
|
282
|
+
return { ok: false, error: err && err.message ? err.message : String(err) };
|
|
283
|
+
} finally {
|
|
284
|
+
if (timer) clearTimeout(timer);
|
|
285
|
+
try { await activation.dispose(); } catch { /* best effort */ }
|
|
286
|
+
}
|
|
244
287
|
}
|
|
245
288
|
|
|
246
289
|
const genPrompt = (promptFile, ownFile, otherFile, role) =>
|
|
247
290
|
'你是生成者 ' + role + '。先读取并严格执行 ' + promptFile + '。' +
|
|
248
291
|
'只写 ' + ownFile + ',不要修改 ' + otherFile + '。' +
|
|
249
|
-
'不要输出解释;完成后只回复:已写入 ' + ownFile
|
|
292
|
+
'不要输出解释;完成后只回复:已写入 ' + ownFile;
|
|
250
293
|
|
|
251
|
-
const reviewPrompt = (targetFile, role, round) =>
|
|
294
|
+
const reviewPrompt = (targetFile, role, round, rubricFile) =>
|
|
252
295
|
'你是审查者 ' + role + '。先读取 ' + rubricFile + ' 和 ' + targetFile + '。' +
|
|
253
296
|
'不要修改 ' + targetFile + '。这是第 ' + round + ' 轮审查。' +
|
|
254
297
|
'逐条检查,PASS 仅当没有 blocker/major。必须只返回 JSON:' +
|
|
255
|
-
'{"verdict":"PASS|CHANGES","issues":[{"severity":"blocker|major|minor","why":"...","fix":"..."}]}'
|
|
298
|
+
'{"verdict":"PASS|CHANGES","issues":[{"severity":"blocker|major|minor","why":"...","fix":"..."}]}';
|
|
256
299
|
|
|
257
300
|
const responsePrompt = (ownFile, review, role) =>
|
|
258
301
|
'你是作者 ' + role + '。读取 ' + ownFile + ' 和以下审查意见:\n' + JSON.stringify(review) +
|
|
259
302
|
'\n如果接受,返回 {"decision":"ACCEPT","reason":"..."};如果认为审查有误,返回 {"decision":"REJECT","reason":"..."}。' +
|
|
260
|
-
'不要修改文件,只返回 JSON。'
|
|
303
|
+
'不要修改文件,只返回 JSON。';
|
|
261
304
|
|
|
262
305
|
const revisePrompt = (ownFile, review, role) =>
|
|
263
306
|
'你是作者 ' + role + '。读取 ' + ownFile + ',按以下审查意见修改。' +
|
|
264
307
|
'保留已经通过的内容,只写 JSONL 到 ' + ownFile + '。\n' + JSON.stringify(review) +
|
|
265
|
-
'\n完成后只返回 {"changed":true|false,"summary":"..."}。'
|
|
308
|
+
'\n完成后只返回 {"changed":true|false,"summary":"..."}。';
|
|
266
309
|
|
|
267
310
|
const rebuttalPrompt = (targetFile, review, response, role) =>
|
|
268
311
|
'你是审查者 ' + role + '。作者对你的审查提出反驳:\n' +
|
|
269
312
|
JSON.stringify({ review, response }) +
|
|
270
313
|
'\n读取 ' + targetFile + ' 当前版本。如果反驳成立,返回 {"verdict":"PASS","reason":"..."};' +
|
|
271
|
-
'否则返回 {"verdict":"MAINTAIN","reason":"..."}。只返回 JSON。'
|
|
314
|
+
'否则返回 {"verdict":"MAINTAIN","reason":"..."}。只返回 JSON。';
|
|
272
315
|
|
|
273
|
-
const judgePrompt = (round, aPassed, bPassed, history) =>
|
|
316
|
+
const judgePrompt = (round, aPassed, bPassed, history, fileA, fileB, finalFile, rubricFile) =>
|
|
274
317
|
'你是主编。读取 ' + rubricFile + '、' + fileA + '、' + fileB + '。' +
|
|
275
318
|
'第 ' + round + ' 轮结束:A=' + (aPassed ? 'PASS' : 'OPEN') + ',B=' + (bPassed ? 'PASS' : 'OPEN') + '。' +
|
|
276
319
|
'交叉审查记录:\n' + JSON.stringify(history).slice(0, 16000) + '\n' +
|
|
277
320
|
'必须选择 PICK_A / PICK_B / MERGE / REJECT,不能和稀泥。' +
|
|
278
321
|
'如果选择 PICK_A、PICK_B 或 MERGE,把最终 JSONL 写到 ' + finalFile + '。' +
|
|
279
|
-
'返回 {"decision":"...","rationale":"..."}。'
|
|
280
|
-
|
|
281
|
-
phase('generate')
|
|
282
|
-
const generated = await parallel([
|
|
283
|
-
() => call(genPrompt(promptAFile, fileA, fileB, 'A'), {
|
|
284
|
-
provider: providerA, model: modelA, label: 'gen-a',
|
|
285
|
-
}),
|
|
286
|
-
() => call(genPrompt(promptBFile, fileB, fileA, 'B'), {
|
|
287
|
-
provider: providerB, model: modelB, label: 'gen-b',
|
|
288
|
-
}),
|
|
289
|
-
])
|
|
290
|
-
if (generated[0] === null || generated[1] === null) {
|
|
291
|
-
throw new Error('generator A or B failed')
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
let aPassed = false
|
|
295
|
-
let bPassed = false
|
|
296
|
-
let round = 0
|
|
297
|
-
const history = []
|
|
298
|
-
|
|
299
|
-
while (round < maxRounds && !(aPassed && bPassed) && calls < totalCap - 4) {
|
|
300
|
-
round += 1
|
|
301
|
-
phase('review-' + round)
|
|
302
|
-
log('cross-review round ' + round)
|
|
303
|
-
let reviewB
|
|
304
|
-
let reviewA
|
|
305
|
-
if (reviewMode === 'oneway') {
|
|
306
|
-
reviewB = await call(reviewPrompt(fileB, 'A', round), {
|
|
307
|
-
provider: providerA, model: modelA, schema: reviewSchema, label: 'review-b-' + round,
|
|
308
|
-
})
|
|
309
|
-
reviewA = { verdict: 'PASS', issues: [] }
|
|
310
|
-
} else {
|
|
311
|
-
[reviewB, reviewA] = await parallel([
|
|
312
|
-
() => call(reviewPrompt(fileB, 'A', round), {
|
|
313
|
-
provider: providerA, model: modelA, schema: reviewSchema, label: 'review-b-' + round,
|
|
314
|
-
}),
|
|
315
|
-
() => call(reviewPrompt(fileA, 'B', round), {
|
|
316
|
-
provider: providerB, model: modelB, schema: reviewSchema, label: 'review-a-' + round,
|
|
317
|
-
}),
|
|
318
|
-
])
|
|
319
|
-
}
|
|
320
|
-
history.push({ round, reviewB, reviewA })
|
|
321
|
-
|
|
322
|
-
if (!reviewB) {
|
|
323
|
-
bPassed = false
|
|
324
|
-
} else if (reviewB.verdict === 'PASS') {
|
|
325
|
-
bPassed = true
|
|
326
|
-
} else {
|
|
327
|
-
const response = await call(responsePrompt(fileB, reviewB, 'B'), {
|
|
328
|
-
provider: providerB, model: modelB, schema: responseSchema, label: 'response-b-' + round,
|
|
329
|
-
})
|
|
330
|
-
history.push({ round, responseB: response })
|
|
331
|
-
if (response && response.decision === 'ACCEPT') {
|
|
332
|
-
const revised = await call(revisePrompt(fileB, reviewB, 'B'), {
|
|
333
|
-
provider: providerB, model: modelB, schema: reviseSchema, label: 'revise-b-' + round,
|
|
334
|
-
})
|
|
335
|
-
history.push({ round, revisedB: revised })
|
|
336
|
-
bPassed = false
|
|
337
|
-
} else if (response) {
|
|
338
|
-
const decision = await call(rebuttalPrompt(fileB, reviewB, response, 'A'), {
|
|
339
|
-
provider: providerA, model: modelA, schema: verdictSchema, label: 'rebuttal-b-' + round,
|
|
340
|
-
})
|
|
341
|
-
history.push({ round, decisionB: decision })
|
|
342
|
-
bPassed = Boolean(decision && decision.verdict === 'PASS')
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
if (!reviewA) {
|
|
347
|
-
aPassed = false
|
|
348
|
-
} else if (reviewA.verdict === 'PASS') {
|
|
349
|
-
aPassed = true
|
|
350
|
-
} else {
|
|
351
|
-
const response = await call(responsePrompt(fileA, reviewA, 'A'), {
|
|
352
|
-
provider: providerB, model: modelB, schema: responseSchema, label: 'response-a-' + round,
|
|
353
|
-
})
|
|
354
|
-
history.push({ round, responseA: response })
|
|
355
|
-
if (response && response.decision === 'ACCEPT') {
|
|
356
|
-
const revised = await call(revisePrompt(fileA, reviewA, 'A'), {
|
|
357
|
-
provider: providerA, model: modelA, schema: reviseSchema, label: 'revise-a-' + round,
|
|
358
|
-
})
|
|
359
|
-
history.push({ round, revisedA: revised })
|
|
360
|
-
aPassed = false
|
|
361
|
-
} else if (response) {
|
|
362
|
-
const decision = await call(rebuttalPrompt(fileA, reviewA, response, 'B'), {
|
|
363
|
-
provider: providerB, model: modelB, schema: verdictSchema, label: 'rebuttal-a-' + round,
|
|
364
|
-
})
|
|
365
|
-
history.push({ round, decisionA: decision })
|
|
366
|
-
aPassed = Boolean(decision && decision.verdict === 'PASS')
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
log('round ' + round + ': A=' + (aPassed ? 'PASS' : 'OPEN') + ', B=' + (bPassed ? 'PASS' : 'OPEN'))
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
phase('judge')
|
|
374
|
-
const judge = await call(judgePrompt(round, aPassed, bPassed, history), {
|
|
375
|
-
provider: providerC, model: modelC, schema: judgeSchema, label: 'judge',
|
|
376
|
-
})
|
|
377
|
-
|
|
378
|
-
return { round, aPassed, bPassed, calls, judge, fileA, fileB, finalFile }
|
|
379
|
-
`;
|
|
322
|
+
'返回 {"decision":"...","rationale":"..."}。';
|
|
380
323
|
|
|
324
|
+
/**
|
|
325
|
+
* Run one batch through two generators, bounded cross-review, and a judge.
|
|
326
|
+
* Uses the host subagent service directly so the plugin does not depend on a
|
|
327
|
+
* preset-scoped workflow engine.
|
|
328
|
+
*/
|
|
381
329
|
export async function runTeamBatch({
|
|
382
330
|
ctx,
|
|
383
331
|
parent,
|
|
@@ -387,12 +335,13 @@ export async function runTeamBatch({
|
|
|
387
335
|
prompt,
|
|
388
336
|
manualBlock = '',
|
|
389
337
|
team,
|
|
338
|
+
signal,
|
|
390
339
|
batchId = '',
|
|
391
340
|
batchNo = 1,
|
|
392
341
|
}) {
|
|
393
|
-
const
|
|
394
|
-
if (!
|
|
395
|
-
return { ok: false, error: 'team 模式需要
|
|
342
|
+
const subagents = service(ctx, 'subagents');
|
|
343
|
+
if (!subagents || typeof subagents.startActivation !== 'function') {
|
|
344
|
+
return { ok: false, error: 'team 模式需要 subagents 服务,但当前 profile 没有加载 subagent。' };
|
|
396
345
|
}
|
|
397
346
|
const problem = teamConfigProblem(team);
|
|
398
347
|
if (problem) return { ok: false, error: problem };
|
|
@@ -410,61 +359,177 @@ export async function runTeamBatch({
|
|
|
410
359
|
writePromptFile(promptBFile, appendTeamRole(retargetPrompt(prompt, file, bFile), 'B', bFile, aFile));
|
|
411
360
|
writePromptFile(rubricFile, `${manualBlock || ''}\n\n${TEAM_RUBRIC}`);
|
|
412
361
|
|
|
413
|
-
let
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
modelB: team.generatorB.model,
|
|
432
|
-
providerC: team.judge.provider,
|
|
433
|
-
modelC: team.judge.model,
|
|
434
|
-
maxRounds: team.maxReviewRounds,
|
|
435
|
-
maxCalls: team.maxTotalCalls,
|
|
436
|
-
reviewMode: team.reviewMode,
|
|
437
|
-
batchId,
|
|
438
|
-
batchNo,
|
|
439
|
-
},
|
|
440
|
-
parent,
|
|
441
|
-
});
|
|
442
|
-
} catch (err) {
|
|
443
|
-
return { ok: false, error: '无法启动 team workflow:' + (err && err.message ? err.message : String(err)) };
|
|
362
|
+
let calls = 0;
|
|
363
|
+
const maxCalls = Math.max(6, Number(team.maxTotalCalls) || 12);
|
|
364
|
+
const call = async (promptText, route, label, schema) => {
|
|
365
|
+
if (calls >= maxCalls) return { ok: false, error: 'team workflow reached maxTotalCalls' };
|
|
366
|
+
calls += 1;
|
|
367
|
+
return callSubagent({ subagents, parent, prompt: promptText, route, label, schema });
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
const [genA, genB] = await Promise.all([
|
|
371
|
+
call(genPrompt(promptAFile, aFile, bFile, 'A'), team.generatorA, 'gen-a'),
|
|
372
|
+
call(genPrompt(promptBFile, bFile, aFile, 'B'), team.generatorB, 'gen-b'),
|
|
373
|
+
]);
|
|
374
|
+
if (!genA.ok) return { ok: false, error: '生成者 A 失败:' + genA.error };
|
|
375
|
+
if (!genB.ok) return { ok: false, error: '生成者 B 失败:' + genB.error };
|
|
376
|
+
for (const [label, candidate] of [['A', aFile], ['B', bFile]]) {
|
|
377
|
+
if (!fs.existsSync(candidate) || fs.statSync(candidate).size === 0) {
|
|
378
|
+
return { ok: false, error: `生成者 ${label} 没有写出文件:${candidate}` };
|
|
379
|
+
}
|
|
444
380
|
}
|
|
445
381
|
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
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;
|
|
385
|
+
let aPassed = false;
|
|
386
|
+
let bPassed = false;
|
|
387
|
+
let round = 0;
|
|
388
|
+
let stopReason = '';
|
|
389
|
+
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
|
+
|
|
403
|
+
// Keep one call in reserve for the judge. A full review-response-revision
|
|
404
|
+
// round costs up to six calls, so the budget check happens before each stage.
|
|
405
|
+
const canSpend = (n) => calls + n <= maxCalls - 1;
|
|
406
|
+
|
|
407
|
+
const saveReview = () => writeJsonFile(reviewFile, { round, aPassed, bPassed, calls, stopReason, history });
|
|
408
|
+
|
|
409
|
+
const settleSide = async (side, review, ownFile, authorRoute, reviewerRoute) => {
|
|
410
|
+
if (review.verdict === 'PASS') return { passed: true };
|
|
411
|
+
if (!canSpend(2)) {
|
|
412
|
+
stopReason = '预算不足,保留未解决意见交裁判';
|
|
413
|
+
return { passed: false };
|
|
414
|
+
}
|
|
415
|
+
const tag = side.toLowerCase();
|
|
416
|
+
const entry = history[history.length - 1];
|
|
417
|
+
const response = await call(
|
|
418
|
+
responsePrompt(ownFile, review, side),
|
|
419
|
+
authorRoute,
|
|
420
|
+
`response-${tag}-${round}`,
|
|
421
|
+
RESPONSE_SCHEMA,
|
|
422
|
+
);
|
|
423
|
+
if (!response.ok) return { error: `作者 ${side} 回应审查失败:${response.error}` };
|
|
424
|
+
entry[`response${side}`] = response.value;
|
|
425
|
+
saveReview();
|
|
426
|
+
if (response.value.decision === 'ACCEPT') {
|
|
427
|
+
const revised = await call(
|
|
428
|
+
revisePrompt(ownFile, review, side),
|
|
429
|
+
authorRoute,
|
|
430
|
+
`revise-${tag}-${round}`,
|
|
431
|
+
REVISE_SCHEMA,
|
|
432
|
+
);
|
|
433
|
+
if (!revised.ok) return { error: `作者 ${side} 修改失败:${revised.error}` };
|
|
434
|
+
entry[`revised${side}`] = revised.value;
|
|
435
|
+
saveReview();
|
|
436
|
+
return { passed: false };
|
|
437
|
+
}
|
|
438
|
+
const decision = await call(
|
|
439
|
+
rebuttalPrompt(ownFile, review, response.value, side === 'A' ? 'B' : 'A'),
|
|
440
|
+
reviewerRoute,
|
|
441
|
+
`rebuttal-${tag}-${round}`,
|
|
442
|
+
VERDICT_SCHEMA,
|
|
443
|
+
);
|
|
444
|
+
if (!decision.ok) return { error: `作者 ${side} 反驳裁决失败:${decision.error}` };
|
|
445
|
+
entry[`decision${side}`] = decision.value;
|
|
446
|
+
saveReview();
|
|
447
|
+
return { passed: decision.value.verdict === 'PASS' };
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
while (round < team.maxReviewRounds && !(aPassed && bPassed)) {
|
|
451
|
+
const cross = team.reviewMode !== 'oneway';
|
|
452
|
+
if (!canSpend(cross ? 2 : 1)) {
|
|
453
|
+
stopReason = '预算不足,提前交裁判';
|
|
454
|
+
break;
|
|
455
|
+
}
|
|
456
|
+
round += 1;
|
|
457
|
+
history.push({ round });
|
|
458
|
+
const [reviewB, reviewA] = await Promise.all([
|
|
459
|
+
bPassed
|
|
460
|
+
? Promise.resolve({ ok: true, value: { verdict: 'PASS', issues: [] } })
|
|
461
|
+
: call(reviewPrompt(bFile, 'A', round, rubricFile), team.generatorA, `review-b-${round}`, REVIEW_SCHEMA),
|
|
462
|
+
!cross || aPassed
|
|
463
|
+
? Promise.resolve({ ok: true, value: { verdict: 'PASS', issues: [] } })
|
|
464
|
+
: call(reviewPrompt(aFile, 'B', round, rubricFile), team.generatorB, `review-a-${round}`, REVIEW_SCHEMA),
|
|
465
|
+
]);
|
|
466
|
+
if (!reviewB.ok || !reviewA.ok) {
|
|
467
|
+
return { ok: false, error: '交叉审查失败:' + ((reviewB.error || reviewA.error) || '未知错误') };
|
|
468
|
+
}
|
|
469
|
+
history[history.length - 1].reviewB = reviewB.value;
|
|
470
|
+
history[history.length - 1].reviewA = reviewA.value;
|
|
471
|
+
saveReview();
|
|
472
|
+
|
|
473
|
+
if (!bPassed) {
|
|
474
|
+
const settled = await settleSide('B', reviewB.value, bFile, team.generatorB, team.generatorA);
|
|
475
|
+
if (settled.error) return { ok: false, error: settled.error };
|
|
476
|
+
bPassed = settled.passed;
|
|
477
|
+
}
|
|
478
|
+
if (!aPassed) {
|
|
479
|
+
const settled = await settleSide('A', reviewA.value, aFile, team.generatorA, team.generatorB);
|
|
480
|
+
if (settled.error) return { ok: false, error: settled.error };
|
|
481
|
+
aPassed = settled.passed;
|
|
482
|
+
}
|
|
483
|
+
saveReview();
|
|
451
484
|
}
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
485
|
+
|
|
486
|
+
saveReview();
|
|
487
|
+
meta.round = round;
|
|
488
|
+
meta.aPassed = aPassed;
|
|
489
|
+
meta.bPassed = bPassed;
|
|
490
|
+
meta.calls = calls;
|
|
491
|
+
meta.stopReason = stopReason;
|
|
492
|
+
writeJsonFile(metaFile, meta);
|
|
493
|
+
|
|
494
|
+
if (!team.judgeOnMaxRounds && !(aPassed && bPassed)) {
|
|
495
|
+
const error = `交叉审查达到 ${round} 轮仍未全部通过,且 judgeOnMaxRounds=false。`;
|
|
496
|
+
meta.error = error;
|
|
497
|
+
writeJsonFile(metaFile, meta);
|
|
498
|
+
return { ok: false, error };
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
const judge = await call(
|
|
502
|
+
judgePrompt(round, aPassed, bPassed, history, aFile, bFile, finalFile, rubricFile),
|
|
503
|
+
team.judge,
|
|
504
|
+
'judge',
|
|
505
|
+
JUDGE_SCHEMA,
|
|
506
|
+
);
|
|
507
|
+
if (!judge.ok) {
|
|
508
|
+
meta.error = '裁判失败:' + judge.error;
|
|
509
|
+
writeJsonFile(metaFile, meta);
|
|
510
|
+
return { ok: false, error: meta.error };
|
|
457
511
|
}
|
|
512
|
+
meta.judge = judge.value;
|
|
513
|
+
meta.decision = judge.value && judge.value.decision;
|
|
514
|
+
meta.calls = calls;
|
|
515
|
+
meta.finishedAt = new Date().toISOString();
|
|
458
516
|
|
|
459
517
|
if (fs.existsSync(finalFile)) {
|
|
460
518
|
fs.copyFileSync(finalFile, file);
|
|
461
|
-
|
|
519
|
+
meta.finalFile = finalFile;
|
|
520
|
+
writeJsonFile(metaFile, meta);
|
|
521
|
+
return { ok: true, result: { round, aPassed, bPassed, calls, judge: judge.value }, finalFile };
|
|
462
522
|
}
|
|
463
|
-
const decision =
|
|
523
|
+
const decision = judge.value && judge.value.decision;
|
|
464
524
|
const fallback = decision === 'PICK_A' ? aFile : decision === 'PICK_B' ? bFile : '';
|
|
465
525
|
if (fallback && fs.existsSync(fallback)) {
|
|
466
526
|
fs.copyFileSync(fallback, file);
|
|
467
|
-
|
|
527
|
+
meta.finalFile = fallback;
|
|
528
|
+
meta.fallback = true;
|
|
529
|
+
writeJsonFile(metaFile, meta);
|
|
530
|
+
return { ok: true, result: { round, aPassed, bPassed, calls, judge: judge.value }, finalFile: fallback, fallback: true };
|
|
468
531
|
}
|
|
469
|
-
|
|
532
|
+
meta.error = '裁判没有写出 final.jsonl(decision=' + (decision || 'none') + ')';
|
|
533
|
+
writeJsonFile(metaFile, meta);
|
|
534
|
+
return { ok: false, error: 'team workflow 完成,但' + meta.error + '。' };
|
|
470
535
|
}
|