diff --git a/apps/detail-agent-pi/src/agent.mjs b/apps/detail-agent-pi/src/agent.mjs index de4aba6b..feb7e965 100644 --- a/apps/detail-agent-pi/src/agent.mjs +++ b/apps/detail-agent-pi/src/agent.mjs @@ -1,5 +1,6 @@ +import { createInitialSystemMessage, toToolDeclaration } from '@earendil-works/pi-ai'; import {setTimeout as delay} from 'node:timers/promises'; -import { runWholeDetailAgents } from './whole-detail.mjs'; +import { runWholeDetail } from './whole-detail.mjs'; import { enterpriseTools } from './enterprise-knowledge.mjs'; import { executeWorkflow } from './workflow-execute.mjs'; import { StreamPresentation, presentText } from './stream-presentation.mjs'; @@ -479,20 +480,24 @@ export class DetailPiAgent { resourceLoader: loader, customTools, tools: photoCraftToolAllowlist(customTools), settingsManager: SettingsManager.inMemory({ compaction: { enabled: false, keepRecentTokens: 2048, reserveTokens: 4096 }, retry: { enabled: false, maxRetries: 0 }, summarization: { enabled: false } }), sessionManager: host.manager, }); + let detailAgent; host.runDetailAgent = async (slotId, systemPrompt, prompt, tools) => { const model = runtime.getModel('jiansun-native-98k', info.model.id); const scoped = broker?.forDetailSlot?.(slotId); - if (!model || !scoped && !transport?.stream) throw Error('详情页子代理连接未就绪'); + if (!model || !scoped && !transport?.stream) throw Error('详情页模型连接未就绪'); const used = new Map(); - const agent = new Agent({ - initialState:{model,systemPrompt,tools,thinkingLevel:'off'}, - beforeToolCall:async ({toolCall})=>{ + detailAgent ??= new Agent({initialState:{model,thinkingLevel:'off'},toolExecution:'sequential',streamFn:apiImpl.streamSimple}); + const agent = detailAgent; + agent.reset(); + agent.state.tools=tools; + agent.state.messages=[createInitialSystemMessage(systemPrompt,tools.map(toToolDeclaration))]; + agent.beforeToolCall=async ({toolCall})=>{ const key=JSON.stringify([toolCall.name,toolCall.arguments]); const count=(used.get(key)??0)+1; used.set(key,count); if(count>2)return {block:true,terminate:true,reason:'同一操作已返回结果,请保留当前图位记录'}; - }, - streamFn:(selected,context,options={})=>{ + }; + agent.streamFunction=(selected,context,options={})=>{ if(transport?.stream) return transport.stream(selected,context,options); const output=createAssistantMessageEventStream(); (async()=>{ @@ -519,7 +524,7 @@ export class DetailPiAgent { const valid=finalEvent?.type==='done' && ['stop','toolUse'].includes(message.stopReason); const receipt=await round.finish(valid,message.stopReason); await store.appendEvent({type:'provider_finished',id:round.id,slotId,...receipt}); - if(!valid||receipt.phase!=='complete')throw Error('原子代理请求尚未核实,工具未执行'); + if(!valid||receipt.phase!=='complete')throw Error('原模型请求尚未核实,工具未执行'); const raw=await wire; if(!raw)throw Error('原始模型响应尚未读完,工具未执行'); const audit=auditResponse(raw,info.model.protocol); @@ -545,13 +550,12 @@ export class DetailPiAgent { } })(); return output; - }, - }); + }; try { await agent.prompt(prompt); if(agent.state.errorMessage)throw Error(agent.state.errorMessage); const result=agent.state.messages.filter(message=>message.role==='assistant').at(-1); - if(result?.stopReason==='error'||result?.stopReason==='aborted')throw Error(result.errorMessage||'子代理未完成'); + if(result?.stopReason==='error'||result?.stopReason==='aborted')throw Error(result.errorMessage||'当前制作步骤未完成'); } finally { if(scoped)await scoped.close(); } }; host.session = session; @@ -849,7 +853,7 @@ export class DetailPiAgent { heartbeat.unref(); this.wholeDetailActive=true; try { - const result=await runWholeDetailAgents(this,this.runDetailAgent,async plan=>{ + const result=await runWholeDetail(this,this.runDetailAgent,async plan=>{ if(!plan.template.enterprise)return null; this.enterpriseContext??=new EnterpriseContext(this.store); const brief=plan.productEvidence?.brief; diff --git a/apps/detail-agent-pi/src/whole-detail.mjs b/apps/detail-agent-pi/src/whole-detail.mjs index b1c304d2..8aace772 100644 --- a/apps/detail-agent-pi/src/whole-detail.mjs +++ b/apps/detail-agent-pi/src/whole-detail.mjs @@ -56,7 +56,7 @@ async function loadSlots(canvas) { return {plan,slots}; } -export async function runWholeDetailAgents(host, runAgent, loadKnowledge, {pause=delay}={}) { +export async function runWholeDetail(host, runAgent, loadKnowledge, {pause=delay}={}) { const {canvas,store}=host; const {plan,slots}=await loadSlots(canvas); if(!slots.length)return {text:'当前详情页没有图片图位。'}; @@ -69,10 +69,11 @@ export async function runWholeDetailAgents(host, runAgent, loadKnowledge, {pause } journal.creationRevision=plan.revisions?.creation; } - const writeJournal=ordered(), writeCanvas=ordered(), announce=ordered(); + const writeJournal=ordered(), writeCanvas=ordered(); const written=new Set(slots.filter(slot=>slot.selectedCandidateId).map(slot=>slot.id)); - const submitted=new Set(); - let active=0, width=4, successes=0, cooldown=0; + let phase="正在读取模板与商品资料"; + const progress=()=>canvas.delta(phase+" · 已回填 "+written.size+"/"+slots.length); + await progress(); const current=async()=>{ const state=await canvas.read(); if(state?.conversationActive!==true||state.writeBlocked||host.canvasWritesStopped){ @@ -90,9 +91,6 @@ export async function runWholeDetailAgents(host, runAgent, loadKnowledge, {pause journal.items[slot.id]={...journal.items[slot.id],label:slot.label,...fields}; await atomic(path.join(store.dir,'whole-detail.json'),journal); }); - const progress=()=>announce(()=>canvas.delta( - '小壹正在制作详情页 · '+active+' 个图位处理中 · 已回填 '+written.size+'/'+slots.length, - )); const waitUntil=async(deadline,signal)=>{ const duration=Math.max(0,deadline-Date.now()); for(let remaining=duration;remaining>0;remaining-=1000){ @@ -108,7 +106,7 @@ export async function runWholeDetailAgents(host, runAgent, loadKnowledge, {pause }; async function runSlot(slot,goal) { const start=journal.items[slot.id]; - if(written.has(slot.id)||['missing','rejected'].includes(start?.state))return; + if(written.has(slot.id)||start?.state==='missing'||start?.state==='rejected'&&start.candidateId)return; const position=slots.indexOf(slot), offset=Math.floor(position/12)*12; const refresh=async()=>{ const page=await canvas.workflowRead({section:'slots',outputId,offset}); @@ -132,7 +130,7 @@ export async function runWholeDetailAgents(host, runAgent, loadKnowledge, {pause if(original.status==='failed')return; } const needsOriginal=requiresProductOriginal(slot); - const worker=Object.assign(Object.create(host),{nativeToolActive:false,nativeToolOwner:null,nativeUnresolved:null, + const operationHost=Object.assign(Object.create(host),{nativeToolActive:false,nativeToolOwner:null,nativeUnresolved:null, canvasWritesStopped:false,canvasNotificationsDetached:false,generatedCandidates:new Map(), readWorkflow:args=>canvas.workflowRead(args)}); let inspected; @@ -141,6 +139,7 @@ export async function runWholeDetailAgents(host, runAgent, loadKnowledge, {pause await current(); const item=journal.items[slot.id]; if(candidateId && item?.candidateId!==candidateId)throw Error('请读取当前图位候选'); + if(candidateId)await canvas.delta(phase+' · 核对候选'); const input=await canvas.detailImages(slot.id,candidateId); const expected=input.referenceCount+(input.layoutCount??0)+(candidateId?1:0); if(!input.referenceCount||input.candidateId!==(candidateId??null)||input.images?.length!==expected){ @@ -170,17 +169,21 @@ export async function runWholeDetailAgents(host, runAgent, loadKnowledge, {pause throw Error('此图位有已确认失败的原候选,请按保存的原要求重试'); } while(true){ - await waitUntil(Math.max(cooldown,journal.items[slot.id]?.retryAt??0),signal); + if (journal.items[slot.id]?.retryAt>Date.now()) { + await canvas.delta(phase+" · 生图服务暂时繁忙,等待后继续"); + await waitUntil(journal.items[slot.id].retryAt,signal); + } const latest=await refresh(); if(latest.selectedCandidateId){written.add(slot.id);return report({status:'applied'},true);} const baseline=latest.candidates.map(candidate=>candidate.id); await save(slot,{state:'dispatching',attempts,baseline,source,prompt,candidateId:null,review:null,retryOf,retryAt:null}); + await canvas.delta(phase+(source==='product'?' · 读取商品原图':' · 正在生图')); let operation; try { - ({operation}=await invoke(worker,slot,'generate_candidate',source==='product'?{useProductReference:true}: + ({operation}=await invoke(operationHost,slot,'generate_candidate',source==='product'?{useProductReference:true}: {instruction:prompt,...(retryOf?{retryOf}:{})},signal)); } catch(error){ - await save(slot,{state:worker.nativeUnresolved?'unknown':'rejected',reason:error.message}); + await save(slot,{state:operationHost.nativeUnresolved?'unknown':'rejected',reason:error.message}); throw error; } if(operation.phase==='unknown'){ @@ -188,7 +191,6 @@ export async function runWholeDetailAgents(host, runAgent, loadKnowledge, {pause return report({status:'unknown',candidateId:operation.candidateId},true); } if(operation.phase==='complete'){ - if(source==='generate'&&++successes>=4){width=Math.min(8,width+1);successes=0;} await save(slot,{state:'candidate',candidateId:operation.candidateId,transportRetries:retries,reason:null}); return report({candidateId:operation.candidateId,status:'complete'}); } @@ -198,9 +200,7 @@ export async function runWholeDetailAgents(host, runAgent, loadKnowledge, {pause } const waitMs=Math.max(transportDelays[retries++],Number.isFinite(operation.retryAfterSeconds)?operation.retryAfterSeconds*1000:0); retryOf=operation.candidateId; - cooldown=Math.max(cooldown,Date.now()+waitMs); - width=Math.max(1,Math.floor(width/2));successes=0; - await save(slot,{state:'waiting_retry',candidateId:retryOf,transportRetries:retries,retryAt:cooldown,reason:operation.message}); + await save(slot,{state:'waiting_retry',candidateId:retryOf,transportRetries:retries,retryAt:Date.now()+waitMs,reason:operation.message}); } }), tool('review_candidate','记录实际看过的候选核对。通过须确认结构、颜色、构图和商品依据。',params({ @@ -223,7 +223,8 @@ export async function runWholeDetailAgents(host, runAgent, loadKnowledge, {pause if(item?.review?.candidateId!==candidateId||item.review.decision!=='pass')throw Error('先验图并确认通过'); const latest=await refresh(); if(latest.selectedCandidateId){written.add(slot.id);return report({status:'applied'},true);} - const {operation}=await invoke(worker,slot,'adopt_candidate',{candidateId,fit},signal); + await canvas.delta(phase+' · 正在回填'); + const {operation}=await invoke(operationHost,slot,'adopt_candidate',{candidateId,fit},signal); if(operation?.phase!=='complete')throw Error('采纳结果尚未确认'); const after=await refresh(); if(after.selectedCandidateId!==candidateId)throw Error('当前画布未读到本候选的回填记录'); @@ -237,7 +238,7 @@ export async function runWholeDetailAgents(host, runAgent, loadKnowledge, {pause }), ]; const instructions=[ - '你是小壹分派的详情页图位子代理。用工具独立完成指定图位。先核对当前证据,再决定使用实物原图、创作候选或记录缺项。', + '你是小壹。用工具完成当前图位,确认质量后立即回填。先核对当前证据,再决定使用实物原图、创作候选或记录缺项。', '候选图只能是一张完整视觉画面。排版文字、乱码、边框、外部白边、拼版、商品结构或花色不符,均不可采纳。文字由画布独立文字层承担。', '对承重、认证、尺寸和质保等缺证据的内容注明待补。模板参考可指导构图和光线,商品事实只来自同款资料。保留手改和待核查记录。', ].join('\n'); @@ -253,39 +254,9 @@ export async function runWholeDetailAgents(host, runAgent, loadKnowledge, {pause } } - async function delegate(requested,goal) { - const waiting=requested.map(id=>slots.find(slot=>slot.id===id)).filter(slot=>slot&&!submitted.has(slot.id)&&!written.has(slot.id)); - for(const slot of waiting)submitted.add(slot.id); - const running=new Set(); - while(waiting.length||running.size){ - while(waiting.length&&running.size{ - active++; - try{await progress();await runSlot(slot,goal);} - catch(error){await save(slot,{reason:error.message});} - finally{active--;} - }); - running.add(task); - task.then(()=>running.delete(task),()=>running.delete(task)); - } - if(running.size)await Promise.race(running); - } - } - - const supervisor=[ - tool('delegate_image_slots','分派图位给小壹的 Pi 子代理。工具会并行读图、生图、验图与回填,返回实际状态。',params({ - slotIds:Type.Array(short,{minItems:1,maxItems:300}),goal:short, - }),async({slotIds,goal})=>{ - if(slotIds.some(id=>!slots.some(slot=>slot.id===id)))throw Error('图位不属于本详情页'); - const distinct=[...new Set(slotIds)]; - await delegate(distinct,goal); - return report(distinct.map(id=>({id,status:written.has(id)?'applied':journal.items[id]?.state??'pending',reason:journal.items[id]?.reason}))); - }), - ]; let textWritten=0, textPending=0; if(typeof canvas.detailCopyFields==='function'&&typeof canvas.detailApplyCopy==='function'){ - supervisor.push( + const copyTools=[ tool('read_copy_fields','查看固定模板的分屏、待填写文字格和容量。',params({}),async()=>report(await canvas.detailCopyFields())), tool('write_copy_fields','把同款商品事实写入指定文字格。缺证据可写待补。',params({items:Type.Array(params({layer:Type.Integer({minimum:1}),text:short}),{minItems:1,maxItems:32})}), ({items},signal)=>writeCanvas(async()=>{ @@ -299,22 +270,28 @@ export async function runWholeDetailAgents(host, runAgent, loadKnowledge, {pause textPending=result.pending; return report(result); })), - ); + ]; textPending=(await canvas.detailCopyFields()).length; - } - const list=slots.filter(slot=>!written.has(slot.id)).map(({id,label})=>({id,label,state:journal.items[id]?.state})); - let stopped; - if(list.length||textPending){ - await runAgent('supervisor','你是小壹详情页制作负责人。根据当前资料和固定模板,调用 delegate_image_slots 分派所有待处理图位,给出整套视觉目标和事实要求。若有文案工具,读取待填文字格并按商品事实填写。保留已写文字、品牌版式与已采纳结果。认证、质保、承重、未经证实材质或选配须填待补。等实际回执后报告完成和待补。', - JSON.stringify({product:plan.productEvidence?.brief,facts:plan.productEvidence?.facts,slots:list,textPending}),supervisor).catch(async error=>{ - stopped=error.message; - await store.appendEvent({type:'whole_detail_supervisor_stopped',reason:stopped}); + if(textPending){ + phase='正在填写模板文案'; + await progress(); + await runAgent('supervisor','你是小壹。读取待填文案,按商品事实填写。标题、卖点和配套短标签须写入对应文字格。认证、质保、承重等缺乏依据的字段写待补,不沿用模板样品信息。单次写入最多 32 格,分批完成后结束。', + JSON.stringify({product:plan.productEvidence?.brief,facts:plan.productEvidence?.facts,textPending}),copyTools).catch(async error=>{ + await store.appendEvent({type:'whole_detail_copy_stopped',reason:error.message}); }); + } + } + for(const [index,slot] of slots.entries()){ + if(written.has(slot.id))continue; + await current(); + phase='正在处理 '+(index+1)+'/'+slots.length+' · '+slot.label; + await progress(); + await runSlot(slot,'保持模板视觉风格与同款商品的实际结构和花色'); + await progress(); } const pending=slots.filter(slot=>!written.has(slot.id)); const summary='整套详情页共 '+slots.length+' 个图位,已回填 '+written.size+' 个' +(textWritten||textPending?',已填写 '+textWritten+' 个文字格,'+textPending+' 个待填':''); - if(stopped&&!written.size&&!textWritten)return {text:summary+'。本次制作暂停:'+stopped}; return {text:pending.length?summary+','+pending.length+' 个待处理。\n'+pending.map(slot=> '· '+slot.label+':'+(journal.items[slot.id]?.reason||'尚未验收')).join('\n'):summary+'。可在画布预览成品。'}; } diff --git a/apps/detail-agent-pi/test/whole-detail.test.mjs b/apps/detail-agent-pi/test/whole-detail.test.mjs index d527f500..b820b63d 100644 --- a/apps/detail-agent-pi/test/whole-detail.test.mjs +++ b/apps/detail-agent-pi/test/whole-detail.test.mjs @@ -3,7 +3,7 @@ import assert from 'node:assert/strict'; import fs from 'node:fs/promises'; import os from 'node:os'; import path from 'node:path'; -import {runWholeDetailAgents} from '../src/whole-detail.mjs'; +import {runWholeDetail} from '../src/whole-detail.mjs'; const checks={structure:true,color:true,composition:true,evidence:true}; function invoke(tools,name,args={}){ @@ -51,12 +51,6 @@ async function fixture(t,{total=9,failures=[],unknownSlot=''}={}){ const waits=[]; const stub=(custom)=>async (slotId,_system,prompt,toolset)=>{ tools.push({slotId,tools:toolset.map(t=>t.name)}); - if(slotId==='supervisor'){ - const sheet=JSON.parse(prompt); - const after=result(await invoke(toolset,'delegate_image_slots',{slotIds:sheet.slots.map(s=>s.id),goal:'构图贴合真实商品'})); - assert.equal(after.length,sheet.slots.length); - return; - } await invoke(toolset,'view_slot'); const item=result(await invoke(toolset,'make_candidate',{source:'generate',prompt:'沿用真实结构'})); if(item.status!=='complete')return; @@ -66,18 +60,17 @@ async function fixture(t,{total=9,failures=[],unknownSlot=''}={}){ await invoke(toolset,'place_candidate',{candidateId:item.candidateId,fit:'cover'}); }; return {tools,calls,slots,canvas,waits,get peak(){return peak;},get overlapAdoptions(){return overlapAdoptions;}, - run:custom=>runWholeDetailAgents({canvas,store,turnEpoch:1,readWorkflow:()=>canvas.workflowRead()},stub(custom),async()=>null,{pause:async ms=>{waits.push(ms);}})}; + run:custom=>runWholeDetail({canvas,store,turnEpoch:1,readWorkflow:()=>canvas.workflowRead()},stub(custom),async()=>null,{pause:async ms=>{waits.push(ms);}})}; } -test('supervisor delegates distinct slots with concurrent image work and serialized canvas application',async t=>{ +test('one-click continues through every slot with one image request at a time',async t=>{ const f=await fixture(t); const summary=await f.run(); assert.match(summary.text,/已回填 9 个/); - assert.ok(f.peak>=4 && f.peak<=8); + assert.equal(f.peak,1); assert.equal(f.overlapAdoptions,1); assert.equal(f.calls.length,18); - assert.equal(f.tools[0].slotId,'supervisor'); - assert.deepEqual(f.tools[0].tools,['delegate_image_slots']); + assert.equal(f.tools[0].slotId,'slot-0'); assert.equal(f.tools.filter(worker=>worker.tools.includes('review_candidate')).length,9); }); @@ -111,7 +104,7 @@ test('review rejects uninspected images and protects a manually adopted result', assert.equal(f.calls.some(op=>op.intent==='adopt_candidate'),false); }); -test('the real Pi agent loop dispatches both supervisor and scoped child tools',async t=>{ +test('the real Pi agent loop reviews and places the candidate',async t=>{ const {createAssistantMessageEventStream,getCurrentTools}=await import('@earendil-works/pi-ai/compat'); const {DetailPiAgent}=await import('../src/agent.mjs'); const {ProjectStore}=await import('../src/store.mjs'); @@ -131,9 +124,8 @@ test('the real Pi agent loop dispatches both supervisor and scoped child tools', const what=previous?.toolName; const candidate='slot-0-candidate-1'; let request; - if(options.includes('delegate_image_slots')){ - if(!what)request=['delegate_image_slots',{slotIds:['slot-0'],goal:'核对同款'}]; - }else if(!what)request=['view_slot',{}]; + assert.ok(!options.includes('delegate_image_slots')); + if(!what)request=['view_slot',{}]; else if(what==='view_slot'&&previous.content.filter(part=>part.type==='image').length===1) request=['make_candidate',{source:'generate',prompt:'按原图创作'}]; else if(what==='make_candidate')request=['view_slot',{candidateId:candidate}]; @@ -156,7 +148,7 @@ test('the real Pi agent loop dispatches both supervisor and scoped child tools', try{ const response=await client.wholeDetail('一键制作整套详情页'); assert.match(response.text,/已回填 1 个/); - assert.deepEqual(toolCalls,['delegate_image_slots','view_slot','make_candidate','view_slot','review_candidate','place_candidate']); + assert.deepEqual(toolCalls,['view_slot','make_candidate','view_slot','review_candidate','place_candidate']); assert.equal(f.calls.find(op=>op.intent==='adopt_candidate').fit,'contain'); }finally{await client.dispose();} }); diff --git a/apps/detail-photocraft/crates/ui-egui/src/jiansun/canvas_workspace.rs b/apps/detail-photocraft/crates/ui-egui/src/jiansun/canvas_workspace.rs index 33a9bb74..de2c4216 100644 --- a/apps/detail-photocraft/crates/ui-egui/src/jiansun/canvas_workspace.rs +++ b/apps/detail-photocraft/crates/ui-egui/src/jiansun/canvas_workspace.rs @@ -3136,6 +3136,12 @@ impl Workspace { let slots: HashMap<_, _> = rows.iter().filter_map(|row| row.slot["id"].as_str().map(|id| (id, row))).collect(); let mut original=0;let mut processed=0;let mut changed=0; for field in fields { + if mapping["omitSampleCopy"] == true && field["kind"] == "text" + && field["nativeLayerId"].as_u64().and_then(|id| doc.layer(photocraft_doc::LayerId(id))) + .is_some_and(|layer| matches!(&layer.content, LayerContent::Text(text) if text.text.trim().is_empty())) + { + continue; + } let status=processing_status(doc,field,variant,&self.messages); match status {"小壹已处理"=>processed+=1,"已修改"=>changed+=1,_=>original+=1}; let frame_value=&field["frame"]; diff --git a/apps/detail-photocraft/crates/ui-egui/src/jiansun/workflow_bridge.rs b/apps/detail-photocraft/crates/ui-egui/src/jiansun/workflow_bridge.rs index dceec3eb..a5144a95 100644 --- a/apps/detail-photocraft/crates/ui-egui/src/jiansun/workflow_bridge.rs +++ b/apps/detail-photocraft/crates/ui-egui/src/jiansun/workflow_bridge.rs @@ -776,7 +776,8 @@ fn template_input(w: &Workspace, intent: &str, proposal: &Value) -> Result