fix(client): report generated candidates from native receipts

This commit is contained in:
WuYongJie 2026-11-13 10:30:46 +08:00
parent fd1710b6ee
commit 2df29d15f5
4 changed files with 71 additions and 5 deletions

View File

@ -544,6 +544,14 @@ export class DetailPiAgent {
finally { this.session.setAutoCompactionEnabled(false); }
});
}
generatedCandidateReply() {
if (!this.generatedCandidates.size) return null;
const adopted = [...this.generatedCandidates.values()].filter(Boolean).length;
let reply = `已生成 ${this.generatedCandidates.size} 张候选图,可以在画布对应图位或对话中的「生成图片」查看。`;
if (adopted) reply += `其中 ${adopted} 张已采纳到画布。`;
if (this.workflowStopReason) reply += `\n\n后续操作已停止。${this.workflowStopReason}`;
return reply;
}
async checkLocalBudgetRecovery(signal) {
const recovery = this.localBudgetRecovery;
if (signal?.aborted || recovery?.signal?.aborted || this.disposed) throw Error('预算压缩已停止;原工具结果与历史已保留');
@ -623,6 +631,7 @@ export class DetailPiAgent {
this.rounds = 0; this.text = ''; this.presentation = new StreamPresentation(); this.lastDelta = 0; this.recallCounts.clear(); this.recallRepeats = 0;
this.turnEpoch = (this.turnEpoch || 0) + 1;
this.canvasWriteRequested = false; this.canvasWritesStopped = false; this.workflowStopReason = null;
this.generatedCandidates = new Map();
this.canvasNotificationsDetached=false;this.canvasEndAttempted=false;
this.activityScope = activityScope(this.store.identity);
this.localBudgetRecovery = null;
@ -676,7 +685,8 @@ export class DetailPiAgent {
this.localBudgetRecovery.phase = 'completed';
await this.store.appendEvent({type: 'local_budget_recovery_completed', planId: this.localBudgetRecovery.planId});
}
const textResult = this.previewCancelled&&!this.previewReceipt?'已取消本次预览,没有应用这项修改。':presentText(this.session.getLastAssistantText() || '');
const textResult = this.generatedCandidateReply() ?? (this.previewCancelled&&!this.previewReceipt
? '已取消本次预览,没有应用这项修改。' : presentText(this.session.getLastAssistantText() || ''));
if (!textResult.trim()) throw Error('模型没有返回正文,请更换模型后再试;本轮工具结果已保留');
if (this.expectCopyPreview && !this.previewReceipt && !this.previewCancelled) throw Error('本轮尚未生成可采纳的修改预览;文案候选和工具结果已保留');
if(this.expectCopyFields&&!this.copyFieldsReceipt)throw Error('本轮尚未生成与文字格对应的候选文案;原文和画布未改变');
@ -688,7 +698,9 @@ export class DetailPiAgent {
return { text: textResult, rounds: this.rounds, messages: this.session.messages.length, sessionFile: this.manager.getSessionFile() };
} catch (error) {
await this.nativeUI?.stop().catch(() => {});
if (this.canvas) await this.notifyCanvas('end',error.message).catch(() => {});
const generated = this.generatedCandidateReply();
if (this.canvas) await this.notifyCanvas('end', generated && !this.workflowStopReason
? `${generated}\n\n${error.message}` : generated ?? error.message).catch(() => {});
throw error;
} finally {
if (this.heartbeat) clearInterval(this.heartbeat);

View File

@ -97,8 +97,15 @@ export function workflowHistory(context, { version } = {}) {
return { ...context, messages: context.messages.map((message, index) => {
const value = parsed.get(index);
if (!value) return message;
const current = turnStart >= 0 && index > turnStart && message.toolName === 'workflow_read' && latest.get(`${value.section || 'overview'}:${value.outputId || ''}`) === index && (!version || value.version === version);
const sameTurn = turnStart >= 0 && index > turnStart;
const current = sameTurn && message.toolName === 'workflow_read' && latest.get(`${value.section || 'overview'}:${value.outputId || ''}`) === index && (!version || value.version === version);
if (current) return message;
if (sameTurn && message.toolName === 'workflow_execute' && value.operation?.intent === 'generate_candidate'
&& value.operation.phase === 'complete' && value.operation.candidateId) {
const next = { ...message, content: [{ type: 'text', text: JSON.stringify({ operation: workflowResult(value.operation) }) }] };
delete next.details;
return next;
}
const proposal = value.proposal && typeof value.proposal === 'object' && !Array.isArray(value.proposal) ? value.proposal : value;
const receipt = { archivedWorkflowResult: message.toolCallId, tool: message.toolName, version: value.version, section: value.section,
stage: value.stage, outputId: proposal.outputId || value.outputId, slotId: proposal.slotId, variant: proposal.variant,

View File

@ -82,11 +82,19 @@ export async function executeWorkflow(host, args, signal, { timeoutMs, pause = d
}
terminal = result.phase === 'complete' || result.phase === 'failed';
if (result.phase !== 'complete') throw Error(workflowResult(result).message || (result.phase === 'failed' ? '原工作流操作失败,请核查结果;不会重发' : '原工作流操作结果未知,请核查原请求;不会重发'));
await host.store.appendEvent({ type:'workflow_operation_completed', run:host.canvas.run, operationId, intent:args.intent, slotId:args.slotId, productVariantId:args.productVariantId });
const operation = workflowResult({
...result, intent: args.intent, outputId: args.outputId, slotId: args.slotId, productVariantId: args.productVariantId,
});
const candidateId = typeof result.candidateId === 'string' && result.candidateId.trim() ? result.candidateId : null;
const generated = args.intent === 'generate_candidate' && candidateId !== null;
if (generated) host.generatedCandidates?.set(candidateId, false);
if (args.intent === 'adopt_candidate' && host.generatedCandidates?.has(candidateId)) host.generatedCandidates.set(candidateId, true);
await host.store.appendEvent({ type:'workflow_operation_completed', run, operationId, intent:args.intent, slotId:args.slotId,
productVariantId:args.productVariantId, ...(generated ? { candidateId:result.candidateId } : {}) });
let current;
try { current = await host.readWorkflow({ section:'slots', outputId:args.outputId }); }
catch { throw Error('原工作流操作已完成,但刷新状态失败;请核查,禁止重发'); }
return { operation: workflowResult(result), workflow: current };
return { operation, workflow: current };
} catch (error) {
if (dispatched && !terminal) host.nativeUnresolved = { kind:'workflow', operationId, run, documentId, identity, epoch };
if (host.turnEpoch === epoch && (dispatched || signal?.aborted || /接手|画布已修改|未获许可|项目已切换|结果未知|尚未确认|不会重发|连接中断|已取消/.test(error?.message || ''))) {

View File

@ -289,6 +289,45 @@ test('recovery can restore workflow_read but never replays navigation or review
}
});
test('a generated candidate keeps its receipt through projection and drives the final canvas reply', async () => {
let originalOperation;
const x = await fixture((context, round) => {
if (round === 1) return tool('workflow_execute', {
version: 1, intent: 'generate_candidate', outputId: 'detail', slotId: 'hero', productVariantId: 'walnut',
}, 'generate-hero');
if (round === 3) return answer('这是另一轮对话。');
return answer('没有新候选,原回执已经归档。');
}, { execute: true });
const originalExecute = x.canvas.workflowExecute;
x.canvas.workflowExecute = async args => {
x.native.generated++;
originalOperation = args.operationId;
return originalExecute(args);
};
x.canvas.workflowPoll = async operationId => ({
operationId, phase: 'complete', candidateId: 'new-hero', message: '图片已生成',
});
const replies = [];
x.canvas.end = async text => replies.push(text);
try {
const result = await x.host.prompt('生成一张首屏候选图。');
const projected = x.calls[1].context.messages.find(m => m.toolCallId === 'generate-hero');
const receipt = JSON.parse(projected.content[0].text);
assert.equal(receipt.operation.phase, 'complete');
assert.equal(receipt.operation.operationId, originalOperation);
assert.equal(receipt.operation.candidateId, 'new-hero');
assert.equal(receipt.operation.slotId, 'hero');
assert.equal(receipt.archivedWorkflowResult, undefined);
assert.match(result.text, /已生成.*候选/);
assert.doesNotMatch(result.text, /没有新候选|归档/);
assert.deepEqual(replies, [result.text]);
assert.equal(x.native.generated, 1);
const next = await x.host.prompt('只聊一下当前资料。');
assert.equal(next.text, '这是另一轮对话。');
assert.equal(x.native.generated, 1);
} finally { await x.close(); }
});
test('SDK stops at a known local rejection without another model step or a reconciliation loop',async()=>{
const x=await fixture(()=>tool('workflow_execute',{version:1,intent:'generate_candidate',outputId:'detail',slotId:'hero',productVariantId:'walnut'}),{execute:true});
x.canvas.documentId=7;