gongxiangp2/apps/detail-agent-pi/src/image-wait-summary.mjs

89 lines
5.1 KiB
JavaScript

const MAX_RECEIPT_CHARS = 10_000;
const excerpt = (value, limit = 120) =>
typeof value === 'string' ? value.slice(0, limit).toWellFormed() : undefined;
function finding({slotId,nativeLayerId,field,location,issue,suggestion}) {
return {slotId,nativeLayerId,field,location:excerpt(location,36),
issue:excerpt(issue,64),suggestion:excerpt(suggestion,64)};
}
function screenSummary({page,revision,screenFingerprint,state,verdict,summary,error,reviewRound,issueHistory,imageIssues,overlayFixes,copyFixes}) {
const issueCount=(imageIssues?.length??0)+(overlayFixes?.length??0)+(copyFixes?.length??0);
return {page,revision,screenFingerprint,state,verdict,reviewRound,issueHistory,summary:excerpt(summary,96),error:excerpt(error),
...(issueCount ? {issueCount} : null),
...(imageIssues ? {imageIssues:imageIssues.slice(0,1).map(finding)} : null),
...(overlayFixes ? {overlayFixes:overlayFixes.slice(0,1).map(finding)} : null),
...(copyFixes ? {copyFixes:copyFixes.slice(0,1).map(finding)} : null)};
}
export function summarizeImageWait({outputId,variantId,active,checking,timedOut,waitedMs,budget,progressSummary,generation,checks,screenChecks,pendingWork}) {
const result={outputId,variantId,active,checking,timedOut,waitedMs,...(budget?{budget}:null)};
if(progressSummary){
const {revision,passedScreens,unreviewedScreens,screensReadyForReview,screensWithOpenIssues,unfilledImageSlots}=progressSummary;
result.progressSummary={revision,
passedScreens:Array.isArray(passedScreens)?passedScreens.length:passedScreens,
unreviewedScreens:Array.isArray(unreviewedScreens)?unreviewedScreens.length:unreviewedScreens,
screensReadyForReview:Array.isArray(screensReadyForReview)?screensReadyForReview.length:screensReadyForReview,
screensWithOpenIssues:Array.isArray(screensWithOpenIssues)?screensWithOpenIssues.length:screensWithOpenIssues,
unfilledImageSlots:Array.isArray(unfilledImageSlots)?unfilledImageSlots.length:unfilledImageSlots};
}
const batches = [
['pendingWork',pendingWork,({slotId,candidateId,requestId,page,stage,elapsedMs})=>({slotId,candidateId,requestId,page,stage:excerpt(stage,40),elapsedMs})],
['screenChecks',screenChecks,screenSummary],
['generation',generation,({slotId,candidateId,operationId,requestId,sourceRequestId,phase,reason,error,resolutionSufficient,reworkOf,requestedSource,sourceNote,pendingOriginal,generationEvidence,target,candidateCount,candidatesRemaining,maxCandidatesPerSlot})=>{
const {actualPixels,actualRatio,promptHashMatches}=generationEvidence??{};
return {
slotId,candidateId,operationId,requestId,sourceRequestId,phase,target,actualPixels,actualRatio,reason:excerpt(reason??error,80),
resolutionSufficient,reworkOf,requestedSource,candidateCount,candidatesRemaining,maxCandidatesPerSlot,sourceNote:excerpt(sourceNote,60),pendingOriginal,
...(actualPixels||typeof promptHashMatches==='boolean'?{generationEvidence:{actualPixels:actualPixels??undefined,promptHashMatches}}:null)};
}],
['checks',checks,({slotId,candidateId,phase,verdict,recommendedAction,reason,error,drawLimitReached})=>({
slotId,candidateId,phase,verdict,recommendedAction,reason:excerpt(reason,80),error:excerpt(error),drawLimitReached})],
];
const projected=batches.filter(([,rows])=>rows).map(([key,rows,project])=>({
key, total:rows.length, rows:rows.map(project),
}));
const full = Object.fromEntries(projected.map(({key,rows})=>[key,rows]));
if(JSON.stringify({...result,...full}).length<=MAX_RECEIPT_CHARS)return {...result,...full};
for(const batch of projected){
result[batch.key]=[];
if(batch.key==='screenChecks'){
batch.rows=batch.rows.map(({revision,screenFingerprint,imageIssues,overlayFixes,copyFixes,...row})=>{
const groups={imageIssues,overlayFixes,copyFixes};
const first=Object.keys(groups).find(key=>groups[key]?.length);
return {...row,...Object.fromEntries(Object.entries(groups).map(([key,issues])=>[key,key===first?issues:[]]))};
});
}
if(batch.key==='generation'||batch.key==='checks'){
const bySlot=new Map();
const ordered=batch.key==='checks'?batch.rows.toReversed():batch.rows;
for(const row of ordered){
const id=row.slotId??row.candidateId;
if(!bySlot.has(id))bySlot.set(id,{
...row,reason:excerpt(row.reason,48),
...(row.operationId===row.candidateId?{operationId:undefined}:null),
});
}
batch.rows=[...bySlot.values()];
}
}
// Reserve room for omitted counts, so even a long queue stays within the tool budget.
let used=JSON.stringify(result).length+256;
const append=(key,item)=>{
const size=JSON.stringify(item).length+1;
if(used+size>MAX_RECEIPT_CHARS)return;
result[key].push(item);
used+=size;
};
for(const {key,rows} of projected)if(rows.length)append(key,rows[0]);
for(const {key,rows} of projected){
for(const item of rows.slice(1))append(key,item);
}
const omitted={};
for(const {key,total} of projected){
if(total>result[key].length)omitted[key]=total-result[key].length;
}
if(Object.keys(omitted).length)result.omitted=omitted;
return result;
}