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