@xiaohhhh1/canvas-agent 0.4.54 → 0.4.55
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/dist/workflow/manager.d.ts +12 -1
- package/dist/workflow/manager.js +62 -36
- package/package.json +1 -1
|
@@ -270,7 +270,6 @@ export declare class WorkflowManager {
|
|
|
270
270
|
private pumpScriptQueue;
|
|
271
271
|
private finishDownloadDirectorySelection;
|
|
272
272
|
private runScript;
|
|
273
|
-
private ensureCreativeCandidateSelections;
|
|
274
273
|
private runCandidateChunk;
|
|
275
274
|
/** 一个 chunk 使用独立线程;解析与中心持久化失败不会影响并行 lane。 */
|
|
276
275
|
private runScriptChunk;
|
|
@@ -296,5 +295,17 @@ export declare function terminalScriptChunkError(results: Array<{
|
|
|
296
295
|
}>): string;
|
|
297
296
|
export declare function scriptChunkPrompt(id: string, task: ScriptTask, ordinals: number[]): string;
|
|
298
297
|
export declare function creativeCandidatePrompt(id: string, task: ScriptTask, ordinals: number[]): string;
|
|
298
|
+
/**
|
|
299
|
+
* 已选创意永远优先进入脚本扩写;只有当前没有可写脚本时才生成下一小波候选。
|
|
300
|
+
* 每次最多占用受控 worker 数量的子批,避免把整批候选提前塞满 worker 队列。
|
|
301
|
+
*/
|
|
302
|
+
export declare function nextScriptPipelineWave(task: Pick<ScriptTask, "requested_count" | "selected_candidates">, receivedOrdinals: number[], durationSeconds: 10 | 20 | 30, scriptChunkSize: number, candidateChunkSize?: number, concurrency?: number): {
|
|
303
|
+
stage: "script";
|
|
304
|
+
chunks: number[][];
|
|
305
|
+
} | {
|
|
306
|
+
stage: "candidate";
|
|
307
|
+
chunks: number[][];
|
|
308
|
+
};
|
|
309
|
+
export declare function immediateScriptOrdinals(task: Pick<ScriptTask, "selected_candidates">, receivedOrdinals: number[], candidateOrdinals: number[]): number[];
|
|
299
310
|
export declare function missingOrdinals(total: number, received: number[]): number[];
|
|
300
311
|
export {};
|
package/dist/workflow/manager.js
CHANGED
|
@@ -244,7 +244,6 @@ export class WorkflowManager {
|
|
|
244
244
|
if (Date.parse(task.expires_at) <= Date.now())
|
|
245
245
|
throw new ExpiredCapabilityError("脚本交接已过期,请在网页重新点击交给本机 Codex");
|
|
246
246
|
const workspace = ensureSiteWorkspace(this.config);
|
|
247
|
-
task = await this.ensureCreativeCandidateSelections(id, task, workspace.workspacePath);
|
|
248
247
|
const durationSeconds = Number(task.duration_seconds || 10);
|
|
249
248
|
const chunkSizes = flowCScriptChunkSizes(durationSeconds);
|
|
250
249
|
// Fail locally before starting any worker if a future schema edit
|
|
@@ -253,17 +252,55 @@ export class WorkflowManager {
|
|
|
253
252
|
flowCScriptOutputSchema(durationSeconds, chunkSize);
|
|
254
253
|
record.activeChunks = 0;
|
|
255
254
|
let chunkSizeIndex = 0;
|
|
255
|
+
let candidateChunkSize = 2;
|
|
256
256
|
while (record.receivedOrdinals.length < task.requested_count) {
|
|
257
|
+
task = await this.scriptTask(id);
|
|
258
|
+
const selectedBefore = selectedCandidateOrdinals(task);
|
|
259
|
+
const wave = nextScriptPipelineWave(task, record.receivedOrdinals, durationSeconds, chunkSizes[chunkSizeIndex], candidateChunkSize);
|
|
260
|
+
if (wave.stage === "candidate") {
|
|
261
|
+
const receivedBefore = record.receivedOrdinals.length;
|
|
262
|
+
record.message = `本机 Codex 正在选择下一小批创意(${selectedBefore.length}/${task.requested_count});选好后立即写对应脚本`;
|
|
263
|
+
record.activeChunks = 0;
|
|
264
|
+
record.updatedAt = now();
|
|
265
|
+
this.save();
|
|
266
|
+
const pipelineResults = await Promise.all(wave.chunks.map(async (ordinals) => {
|
|
267
|
+
const candidateResult = await this.runCandidateChunk(id, task, ordinals, workspace.workspacePath);
|
|
268
|
+
if (candidateResult.error)
|
|
269
|
+
return [candidateResult];
|
|
270
|
+
const selectedTask = await this.scriptTask(id);
|
|
271
|
+
const scriptOrdinals = immediateScriptOrdinals(selectedTask, this.scriptRecord(id).receivedOrdinals, ordinals);
|
|
272
|
+
if (!scriptOrdinals.length)
|
|
273
|
+
return [candidateResult];
|
|
274
|
+
this.scriptRecord(id).attempts += 1;
|
|
275
|
+
const scriptResult = await this.runScriptChunk(id, selectedTask, scriptOrdinals, workspace.workspacePath);
|
|
276
|
+
return [candidateResult, scriptResult];
|
|
277
|
+
}));
|
|
278
|
+
const results = pipelineResults.flat();
|
|
279
|
+
task = await this.scriptTask(id);
|
|
280
|
+
const terminalError = terminalScriptChunkError(results);
|
|
281
|
+
if (terminalError)
|
|
282
|
+
throw new Error(`创意或脚本结构化契约被 Codex 拒绝,已停止自动重试(${terminalError})`);
|
|
283
|
+
if (record.receivedOrdinals.length > receivedBefore)
|
|
284
|
+
chunkSizeIndex = 0;
|
|
285
|
+
if (selectedCandidateOrdinals(task).length > selectedBefore.length) {
|
|
286
|
+
candidateChunkSize = 2;
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
if (candidateChunkSize > 1) {
|
|
290
|
+
candidateChunkSize = 1;
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
const lastError = results.map((result) => result.error).filter(Boolean).at(-1) || "候选生成或中心选题未返回结果";
|
|
294
|
+
throw new Error(`创意候选阶段已隔离到单条仍失败(${lastError}),请点击重试`);
|
|
295
|
+
}
|
|
257
296
|
const before = record.receivedOrdinals.length;
|
|
258
|
-
const missing = missingOrdinals(task.requested_count, record.receivedOrdinals);
|
|
259
297
|
const chunkSize = chunkSizes[chunkSizeIndex];
|
|
260
|
-
const chunks = flowCScriptChunks(durationSeconds, missing, chunkSize);
|
|
261
298
|
record.chunkSize = chunkSize;
|
|
262
|
-
record.attempts += chunks.length;
|
|
263
|
-
record.message =
|
|
299
|
+
record.attempts += wave.chunks.length;
|
|
300
|
+
record.message = `创意已选 ${selectedBefore.length}/${task.requested_count};正在立即写 ${wave.chunks.length} 个对应脚本子批(已回传 ${before}/${task.requested_count})`;
|
|
264
301
|
record.updatedAt = now();
|
|
265
302
|
this.save();
|
|
266
|
-
const results = await Promise.all(chunks.map((ordinals) => this.runScriptChunk(id, task, ordinals, workspace.workspacePath)));
|
|
303
|
+
const results = await Promise.all(wave.chunks.map((ordinals) => this.runScriptChunk(id, task, ordinals, workspace.workspacePath)));
|
|
267
304
|
task = await this.scriptTask(id);
|
|
268
305
|
const terminalError = terminalScriptChunkError(results);
|
|
269
306
|
if (terminalError) {
|
|
@@ -304,36 +341,6 @@ export class WorkflowManager {
|
|
|
304
341
|
this.runningScripts.delete(id);
|
|
305
342
|
}
|
|
306
343
|
}
|
|
307
|
-
async ensureCreativeCandidateSelections(id, initialTask, cwd) {
|
|
308
|
-
let task = initialTask;
|
|
309
|
-
let chunkSize = 2;
|
|
310
|
-
while (selectedCandidateOrdinals(task).length < task.requested_count) {
|
|
311
|
-
const before = selectedCandidateOrdinals(task).length;
|
|
312
|
-
const missing = missingOrdinals(task.requested_count, selectedCandidateOrdinals(task));
|
|
313
|
-
const chunks = chunkNumbers(missing, chunkSize);
|
|
314
|
-
const record = this.scriptRecord(id);
|
|
315
|
-
record.message = `本机 Codex 正在生成可审核创意候选,中心将自动选题(${before}/${task.requested_count})`;
|
|
316
|
-
record.activeChunks = 0;
|
|
317
|
-
record.updatedAt = now();
|
|
318
|
-
this.save();
|
|
319
|
-
const results = await Promise.all(chunks.map((ordinals) => this.runCandidateChunk(id, task, ordinals, cwd)));
|
|
320
|
-
task = await this.scriptTask(id);
|
|
321
|
-
const terminalError = terminalScriptChunkError(results);
|
|
322
|
-
if (terminalError)
|
|
323
|
-
throw new Error(`创意候选结构化契约被 Codex 拒绝,已停止自动重试(${terminalError})`);
|
|
324
|
-
if (selectedCandidateOrdinals(task).length > before) {
|
|
325
|
-
chunkSize = 2;
|
|
326
|
-
continue;
|
|
327
|
-
}
|
|
328
|
-
if (chunkSize > 1) {
|
|
329
|
-
chunkSize = 1;
|
|
330
|
-
continue;
|
|
331
|
-
}
|
|
332
|
-
const lastError = results.map((result) => result.error).filter(Boolean).at(-1) || "候选生成或中心选题未返回结果";
|
|
333
|
-
throw new Error(`创意候选阶段已隔离到单条仍失败(${lastError}),请点击重试`);
|
|
334
|
-
}
|
|
335
|
-
return task;
|
|
336
|
-
}
|
|
337
344
|
async runCandidateChunk(id, task, ordinals, cwd) {
|
|
338
345
|
const result = await runCodexWorkflowTurn(creativeCandidatePrompt(id, task, ordinals), this.emit, {
|
|
339
346
|
cwd,
|
|
@@ -676,6 +683,25 @@ function chunkNumbers(values, size) {
|
|
|
676
683
|
chunks.push(values.slice(index, index + size));
|
|
677
684
|
return chunks;
|
|
678
685
|
}
|
|
686
|
+
/**
|
|
687
|
+
* 已选创意永远优先进入脚本扩写;只有当前没有可写脚本时才生成下一小波候选。
|
|
688
|
+
* 每次最多占用受控 worker 数量的子批,避免把整批候选提前塞满 worker 队列。
|
|
689
|
+
*/
|
|
690
|
+
export function nextScriptPipelineWave(task, receivedOrdinals, durationSeconds, scriptChunkSize, candidateChunkSize = 2, concurrency = FLOW_C_CODEX_WORKER_CONCURRENCY) {
|
|
691
|
+
const selected = selectedCandidateOrdinals(task);
|
|
692
|
+
const selectedSet = new Set(selected);
|
|
693
|
+
const scriptReady = missingOrdinals(task.requested_count, receivedOrdinals).filter((ordinal) => selectedSet.has(ordinal));
|
|
694
|
+
if (scriptReady.length) {
|
|
695
|
+
return { stage: "script", chunks: flowCScriptChunks(durationSeconds, scriptReady, scriptChunkSize).slice(0, concurrency) };
|
|
696
|
+
}
|
|
697
|
+
const candidateMissing = missingOrdinals(task.requested_count, selected);
|
|
698
|
+
return { stage: "candidate", chunks: chunkNumbers(candidateMissing, candidateChunkSize).slice(0, concurrency) };
|
|
699
|
+
}
|
|
700
|
+
export function immediateScriptOrdinals(task, receivedOrdinals, candidateOrdinals) {
|
|
701
|
+
const selected = new Set(selectedCandidateOrdinals(task));
|
|
702
|
+
const received = new Set(receivedOrdinals);
|
|
703
|
+
return candidateOrdinals.filter((ordinal) => selected.has(ordinal) && !received.has(ordinal));
|
|
704
|
+
}
|
|
679
705
|
function compactJson(value, limit) {
|
|
680
706
|
return JSON.stringify(value).slice(0, limit);
|
|
681
707
|
}
|