fix(xiaoyi): preserve paid original when adopting another image
This commit is contained in:
parent
2176a3589f
commit
3d8fc1c7e4
@ -119,7 +119,7 @@ export async function openProductionOperations(host) {
|
||||
reviews:[...reviewsInFlight.values()],candidateReviews:Object.fromEntries(candidates),pictureCells,copyCells,
|
||||
total:slots.length,rendered:filled.size,copy:copyCells.filter(row=>row.state==='verified').length,
|
||||
copyPending:copyCells.filter(row=>row.state!=='verified').length,
|
||||
unresolved:Object.values(journal.items).filter(item=>item.pendingOperation).length});
|
||||
unresolved:Object.values(journal.items).filter(item=>item.pendingOperation||item.pendingAdoption).length});
|
||||
};
|
||||
const notice=heading=>progress(heading).catch(()=>{});
|
||||
const readImages=(slotId,args={})=>canvas.detailImages(matches(slotId).id,args.candidateId,{
|
||||
@ -127,37 +127,40 @@ export async function openProductionOperations(host) {
|
||||
});
|
||||
async function operate(slot,intent,values,signal) {
|
||||
const local=operationHost();
|
||||
const prior=journal.items[slot.id];
|
||||
if(intent==='adopt_candidate'&&prior?.pendingAdoption){
|
||||
return {phase:'unknown',...prior.pendingAdoption};
|
||||
const prior=journal.items[slot.id], adopting=intent==='adopt_candidate';
|
||||
const pendingKey=adopting?'pendingAdoption':'pendingOperation';
|
||||
const original=prior?.[pendingKey];
|
||||
if(original){
|
||||
const candidateId=original.candidateId??prior.candidateId;
|
||||
return {phase:'unknown',...original,candidateId,pendingOriginal:candidateId??original.operationId};
|
||||
}
|
||||
if(prior?.pendingOperation&&
|
||||
(intent==='generate_candidate'||prior.pendingOperation.intent==='adopt_candidate')){
|
||||
return {phase:'unknown',...prior.pendingOperation,candidateId:prior.candidateId,pendingOriginal:prior.candidateId??prior.pendingOperation.operationId};
|
||||
}
|
||||
if(intent==='generate_candidate'&&(prior?.heldOriginalId||prior?.state==='unknown')){
|
||||
if(!adopting&&(prior?.heldOriginalId||prior?.state==='unknown')){
|
||||
const candidateId=prior.heldOriginalId??prior.candidateId;
|
||||
return {phase:'unknown',candidateId,pendingOriginal:candidateId};
|
||||
}
|
||||
const args={version:(await current()).version,intent,outputId,slotId:slot.id,productVariantId:variantId,...values};
|
||||
let prepared;
|
||||
const archive=async operation=>{
|
||||
prepared={...operation,intent};
|
||||
await save(slot,{pendingOperation:prepared,state:'dispatching'});
|
||||
prepared={...operation,intent,...(adopting?{candidateId:values.candidateId}:null)};
|
||||
await save(slot,{[pendingKey]:prepared,...(!adopting?{state:'dispatching'}:null)});
|
||||
};
|
||||
try {
|
||||
const {operation}=await executeWorkflow(local,args,signal,{detailBatch:true,keepFailedCandidate:true,timeoutMs:Infinity,
|
||||
onPrepared:archive,onSubmitted:receipt=>receipt.candidateId
|
||||
? save(slot,{candidateId:receipt.candidateId}):undefined});
|
||||
onPrepared:archive,onSubmitted:async receipt=>{
|
||||
if(receipt.candidateId){
|
||||
prepared={...prepared,candidateId:receipt.candidateId};
|
||||
await save(slot,{[pendingKey]:prepared});
|
||||
}
|
||||
}});
|
||||
const state=operation.phase==='complete'?(intent==='generate_candidate'?'candidate':'applied'):operation.phase;
|
||||
if(state==='applied')filled.add(slot.id);
|
||||
await save(slot,{state,candidateId:operation.candidateId??prior?.candidateId,
|
||||
pendingOperation:operation.phase==='unknown'||state==='applied'&&operation.persistence==='session'?prepared:null,
|
||||
[pendingKey]:operation.phase==='unknown'||state==='applied'&&operation.persistence==='session'?prepared:null,
|
||||
...(intent==='generate_candidate'?{source:values.useProductReference?'product':'generate',prompt:values.instruction}:null)});
|
||||
return {...operation,...(operation.phase==='unknown'?{pendingOriginal:operation.candidateId??operation.operationId}:null)};
|
||||
}catch(error){
|
||||
if(prepared)await save(slot,{state:local.nativeUnresolved?'unknown':'failed',reason:error.message,
|
||||
pendingOperation:local.nativeUnresolved?prepared:null});
|
||||
[pendingKey]:local.nativeUnresolved?prepared:null});
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@ -171,12 +174,13 @@ export async function openProductionOperations(host) {
|
||||
await notice('候选图片回执');
|
||||
return operation;
|
||||
};
|
||||
const reconcile=async(slot,signal)=>{
|
||||
const reconcile=async(slot,signal,adopting=false)=>{
|
||||
const item=journal.items[slot.id];
|
||||
const heldId=item?.heldOriginalId??(item?.state==='unknown'?item.candidateId:undefined);
|
||||
const original=item?.pendingOperation??item?.pendingAdoption;
|
||||
const pendingKey=adopting?'pendingAdoption':'pendingOperation';
|
||||
const original=item?.[pendingKey];
|
||||
signal?.throwIfAborted();
|
||||
if(!original&&heldId){
|
||||
if(!adopting&&!original&&heldId){
|
||||
const receipt=await canvas.detailRecover(slot.id,{projectId:item.heldOriginalProjectId,candidateId:heldId});
|
||||
const phase=receipt?.id===heldId&&['complete','failed'].includes(receipt.status)?receipt.status:'unknown';
|
||||
if(phase!=='unknown')await save(slot,{state:phase==='complete'?'candidate':'failed',candidateId:heldId,
|
||||
@ -184,9 +188,9 @@ export async function openProductionOperations(host) {
|
||||
return {slotId:slot.id,candidateId:heldId,phase};
|
||||
}
|
||||
if(!original)return {slotId:slot.id,phase:'none'};
|
||||
const adoption=(original.intent==='adopt_candidate'||item?.pendingAdoption===original)?{outputId,productVariantId:variantId,slotId:slot.id}:undefined;
|
||||
const adoption=adopting?{outputId,productVariantId:variantId,slotId:slot.id}:undefined;
|
||||
const operation=await reconcileWorkflowReceipt(canvas,{...original,adoption});
|
||||
const candidateId=operation.candidateId??item.candidateId;
|
||||
const candidateId=operation.candidateId??original.candidateId??item.candidateId;
|
||||
const receipt=!adoption&&candidateId&&operation.phase==='unknown'
|
||||
? await canvas.detailRecover(slot.id,{candidateId}) : null;
|
||||
const phase=receipt?.id===candidateId&&['complete','failed'].includes(receipt.status)?receipt.status:operation.phase;
|
||||
@ -194,8 +198,8 @@ export async function openProductionOperations(host) {
|
||||
const applied=phase==='complete'&&adoption;
|
||||
if(applied)filled.add(slot.id);
|
||||
await save(slot,{state:applied?'applied':phase==='complete'?'candidate':phase,candidateId,
|
||||
...(applied&&operation.persistence==='session'?null:{pendingOperation:null,pendingAdoption:null}),
|
||||
heldOriginalId:null,heldOriginalProjectId:null});
|
||||
...(applied&&operation.persistence==='session'?null:{[pendingKey]:null}),
|
||||
...(!adopting?{heldOriginalId:null,heldOriginalProjectId:null}:null)});
|
||||
}
|
||||
return {slotId:slot.id,phase,candidateId,operationId:original.operationId};
|
||||
};
|
||||
@ -322,7 +326,12 @@ export async function openProductionOperations(host) {
|
||||
await notice('画布回填回执');
|
||||
return result;
|
||||
},
|
||||
async reconcile_image_slots({slotIds},signal){return Promise.all(selections(slotIds).map(slot=>reconcile(slot,signal)));},
|
||||
async reconcile_image_slots({slotIds},signal){
|
||||
return Promise.all(selections(slotIds).flatMap(slot=>[
|
||||
reconcile(slot,signal),
|
||||
...(journal.items[slot.id]?.pendingAdoption?[reconcile(slot,signal,true)]:[]),
|
||||
]));
|
||||
},
|
||||
async read_output_screen({page}){return imageResult(await canvas.detailRender({page}));},
|
||||
async record_screen_check({page,revision,issues}){
|
||||
if((await canvas.read()).revision!==revision)throw Error('本屏已变化,请读取当前画面');
|
||||
|
||||
@ -40,11 +40,11 @@ test('Pi chooses each image operation and keeps an unresolved paid request on it
|
||||
},
|
||||
workflowExecute:async input=>{
|
||||
submissions.push(input);
|
||||
return {phase:'submitted',operationId:input.operationId,candidateId:'existing-candidate'};
|
||||
return {phase:'submitted',operationId:input.operationId,candidateId:input.candidateId??'existing-candidate'};
|
||||
},
|
||||
workflowPoll:async operationId=>{
|
||||
const input=submissions.find(row=>row.operationId===operationId);
|
||||
return {operationId,candidateId:'existing-candidate',phase:input.intent==='generate_candidate'?'unknown':'complete'};
|
||||
return {operationId,candidateId:input.candidateId??'existing-candidate',phase:input.intent==='generate_candidate'?'unknown':'complete'};
|
||||
},
|
||||
workflowReconcile:async operation=>({...operation,phase:'unknown',candidateId:'existing-candidate'}),
|
||||
};
|
||||
@ -73,14 +73,19 @@ test('Pi chooses each image operation and keeps an unresolved paid request on it
|
||||
assert.equal((await actions.reconcile_image_slots({slotIds:['lamp']}))[0].phase,'unknown');
|
||||
assert.equal(lookups,1);
|
||||
assert.equal(submissions.length,1);
|
||||
assert.equal((await actions.adopt_production_image({slotId:'lamp',candidateId:'older-candidate'})).phase,'complete');
|
||||
assert.equal((await actions.generate_slot_image({slotId:'lamp',source:'generate',prompt:'动作已调整'})).phase,'unknown');
|
||||
assert.equal(submissions.length,2);
|
||||
recovered=true;
|
||||
assert.equal((await actions.reconcile_image_slots({slotIds:['lamp']}))[0].phase,'complete');
|
||||
const [original]=await actions.reconcile_image_slots({slotIds:['lamp']});
|
||||
assert.equal(original.phase,'complete');
|
||||
assert.equal(original.candidateId,'existing-candidate');
|
||||
const [checked]=await actions.check_image_slots({candidates:[{slotId:'lamp',candidateId:'existing-candidate'}]});
|
||||
assert.equal(checked.verdict,'pass');
|
||||
assert.equal(modelCalls,1);
|
||||
assert.equal(submissions.length,1,'verifying must not adopt or generate');
|
||||
assert.equal(submissions.length,2,'verifying must not adopt or generate');
|
||||
assert.equal((await actions.adopt_production_image({slotId:'lamp',candidateId:'existing-candidate',fit:'contain'})).phase,'complete');
|
||||
assert.deepEqual(submissions.map(row=>row.intent),['generate_candidate','adopt_candidate']);
|
||||
assert.deepEqual(submissions.map(row=>row.intent),['generate_candidate','adopt_candidate','adopt_candidate']);
|
||||
const result=await actions.finish_production({revision});
|
||||
assert.equal(result.production.canvasRevision,revision);
|
||||
assert.equal(result.production.complete,true);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user