fix(xiaoyi): use native edit receipts in production

This commit is contained in:
WuYongJie 2026-11-26 02:46:08 +08:00
parent 68977c2f34
commit 970fae4c10
2 changed files with 31 additions and 1 deletions

View File

@ -85,6 +85,8 @@ function toolsFor(host) {
return format(name==='canvas_read'&&args.revision!==undefined?{...value,changedSinceRevision:value.revision!==args.revision}:value);
} });
const string = maxLength => Type.String({ maxLength });
const commands = nativeTools(host, make);
const nativeCall = commands.find(tool => tool.name === 'native_call');
const tools = [
...(host.productionGoal ? productionTools.map(([name, description, schema]) =>
make(name, description, schema, (args,signal)=>callProductionTool(host,name,args,signal),
@ -114,6 +116,11 @@ function toolsFor(host) {
}),
make('canvas_edit', '一次修改现有图层,共享本机撤销历史。revision须等于刚读取的版本。支持type.edit/layer.translate/layer.setProps,参数沿用原生画布。', Type.Object({ revision: Type.Integer({ minimum: 0 }), command: Type.Union([Type.Literal('type.edit'), Type.Literal('layer.translate'), Type.Literal('layer.setProps')]), params: Type.Object({ layer: Type.Integer({ minimum: 1 }), text: Type.Optional(string(3000)), size: Type.Optional(Type.Number({ minimum: 8, maximum: 240 })), color: Type.Optional(Type.String({ pattern: '^#[0-9a-fA-F]{6}$' })), name: Type.Optional(string(120)), dx: Type.Optional(Type.Integer({ minimum: -3000, maximum: 3000 })), dy: Type.Optional(Type.Integer({ minimum: -3000, maximum: 3000 })), opacity: Type.Optional(Type.Number({ minimum: 0, maximum: 1 })), visible: Type.Optional(Type.Boolean()) }, { additionalProperties: false }), label: Type.String({ minLength: 1, maxLength: 120 }) }), ({ revision, ...edit }, signal) => {
if (!host.canvas) throw Error('本轮没有连接画布');
if (host.productionGoal && nativeCall) {
return nativeCall.execute('canvas-edit', {
revision, method:'engine.execute', params:{command:edit.command,params:edit.params},
},signal).then(({details})=>({...details,applied:details.executed}));
}
if (host.nativeUnresolved || host.nativeToolActive) throw Error('原操作尚未确认,先核查原请求;可使用 native_reconcile 只读核查,不会重发');
const epoch = host.turnEpoch, run = host.canvas.run, documentId = host.canvas.documentId, owner = Symbol('canvas edit');
const identity = {scope:host.store.identity?.scope, project:host.store.identity?.project, accountId:host.canvas.identity?.accountId, origin:host.canvas.identity?.origin};
@ -190,7 +197,7 @@ function toolsFor(host) {
details:metadata};
},value=>value),namespace:PHOTOCRAFT_NAMESPACE,exposure:'deferred'});
}
active.push(...nativeTools(host, make), ...enterpriseTools(host, make));
active.push(...commands, ...enterpriseTools(host, make));
if (typeof host.canvas?.copyOptions === 'function') active.push({...make('copywriting_options', '把已生成的产品卖点逐条显示为可多选的候选。items中每条一个重点,只使用已确认资料;缺失依据放missing。展示不代表采纳或写入画布,等用户选择后再改写或预览。', Type.Object({items:Type.Array(Type.String({minLength:1,maxLength:160}),{minItems:1,maxItems:20}),missing:Type.Optional(Type.Array(Type.String({minLength:1,maxLength:160}),{maxItems:20}))},{additionalProperties:false}), async args => {
const value = await host.canvas.copyOptions(args);
if (!value?.optionsId || value.run !== host.canvas.run || value.documentId !== host.canvas.documentId) throw Error('卖点选项不属于当前会话');

View File

@ -515,6 +515,29 @@ test('production exposes direct native tools and Pi file tools',async()=>{
} finally {await x.close();}
});
test('production canvas edit returns the direct native command receipt',async()=>{
const executed=[];
const x=await setup((_,request)=>{
const name=request===1?'canvas_edit':'finish_production';
const args=request===1?{revision:1,command:'type.edit',params:{layer:2,text:'New'},label:'update copy'}:{revision:1};
return sse({tool_calls:[{index:0,id:'edit-'+request,type:'function',function:{name,arguments:JSON.stringify(args)}}]},'tool_calls');
},undefined,{
progress:()=>({state:{waitingOnOriginals:true}}),
canvas:{
nativeCatalog:async({id})=>({items:[{id,requiresRevisionLease:true}]}),
requestWrite:async revision=>({allowed:true,revision}),
edit:async()=>{assert.fail('production must use the original native command receipt');},
nativeCall:async args=>{executed.push(args);return {completed:true,executed:true,revision:2};},
},
});
try{
assert.equal((await x.host.prompt('完成制作')).production.complete,true);
assert.deepEqual(executed,[{revision:1,method:'engine.execute',params:{command:'type.edit',params:{layer:2,text:'New'}}}]);
const row=x.host.manager.getBranch().find(({message})=>message?.toolCallId==='edit-1')?.message;
assert.equal(JSON.parse(row.content[0].text).applied,true);
}finally{await x.close();}
});
test('a rejected native text edit can be corrected in the same production turn', async () => {
let edits = 0;
const x = await setup((_, request) => {