feat(ai): 按参数组接只选已分析素材并轮转保多样性;规划流程识别新建/修改意图
按参数组接(assemble): - 存在已分析(有评分)素材时只使用这些素材,未分析素材不再乱入 - 按素材轮转取用镜头,避免单一素材堆砌,每素材数量有上限 - 摘要说明镜头来源数量与未使用素材,未分析时明确提示先做 AI 分析 规划流程(plan handler): - 识别新建意图(如「制作一个 45 秒旅行视频」):先 remove 现有全部片段再重建,避免旧片段与新材料混在一起 - 接入 sanitizeOperations 净化层与 3 次修正重试,方案摘要显示自动修正数量 - 失败时在聊天中显示原因(此前只弹 toast) - 本地模型提示词预算 3800 字符、云端 9000 字符;用户指令上限 3000 字符
This commit is contained in:
1 parent
de19ecc42c
commit
ffb57c9868
3 files changed
+81
-6
No files matched your search
+24
-1
@@ -69,7 +69,30 @@ async function init(){
|
||||
if(!analysis)analysis=await media.analyzeTechnical(m,cache,signal,(v,d)=>progress((i+v*.5)/selected.length,d),{acceleration:config.processingMode});
|
||||
if(vision&&m.type!=='audio'){if(!c.visionVerified)throw Error('请先在模型管理中通过视觉能力测试');if(!analysis.model||analysis.model!==c.model)analysis=await providers.visionAnalyze(c,m,analysis,signal,(v,d)=>progress((i+.5+v*.5)/selected.length,d));}
|
||||
signal.throwIfAborted();sameProject(pid);m.analysis=analysis;m.tags=[...new Set([...(m.tags||[]),...analysis.segments.flatMap(s=>s.tags||[])])];changed();progress((i+1)/selected.length,`已分析 ${i+1}/${selected.length}`);}return {count:selected.length};});});
|
||||
handle('plan',(instruction,options={})=>{const snapshot=structuredClone(project),c=forTask('text');if(!c.model)throw Error('请先选择规划模型');return task('AI 规划时间轴',async(signal,progress)=>{progress(.1,'读取项目和片段分析,调用规划模型');let r,lastError;const style=styleModule.getStyle(options.style);const prompt=providers.plannerPrompt(snapshot,String(instruction).slice(0,8000)+(snapshot.clips.length?'\n已有时间轴:仅修改用户明确提及的字段,不改变其余参数;除非用户要求改变风格,否则不要套用任何风格参数。':'\n参考风格参数:'+JSON.stringify(style))+(options.target?';新建作品目标时长 '+Number(options.target)+' 秒(修改已有时间轴时优先遵守本次用户要求)':''));for(let attempt=0;attempt<2;attempt++){try{r=await providers.complete(c,prompt+(lastError?'\n上一次方案被时间轴校验拒绝:'+lastError+'。请修正并返回有效命令。':''),[],signal);r.data.operations=styleModule.styleOperations(r.data.operations,style);core.applyOperations(snapshot,r.data.operations);lastError=null;break;}catch(e){signal.throwIfAborted();lastError=e.message;if(attempt===1)throw e;progress(.4,'方案未通过校验,请求模型修正');}}sameProject(snapshot.id);const id=randomUUID(),plan={id,summary:String(r.data.summary||'AI 剪辑方案'),operations:r.data.operations,base:JSON.stringify(snapshot.clips),projectId:snapshot.id,usage:r.usage};pendingPlans.set(id,plan);project.messages.push({id:randomUUID(),role:'user',text:instruction,time:new Date().toISOString()},{id:randomUUID(),role:'assistant',text:plan.summary,planId:id,operations:plan.operations,usage:r.usage,model:c.model,elapsed:r.elapsed,time:new Date().toISOString()});changed();emit('plan',plan);return {planId:id,usage:r.usage};});});
|
||||
handle('plan',(instruction,options={})=>{const snapshot=structuredClone(project),c=forTask('text');if(!c.model)throw Error('请先在模型管理中为剪辑规划选择模型');return task('AI 规划时间轴',async(signal,progress)=>{progress(.1,'读取项目和片段分析,调用规划模型');
|
||||
const hasVideo=snapshot.clips.some(x=>x.track==='video');
|
||||
const createIntent=/(制作|创建|新建|生成|剪一?个|做一?个|重新[剪做]|从头).{0,12}(视频|短片|vlog|混剪|宣传片|片子|作品|成片)/i.test(instruction)||/(^|[^a-z])(make|create|build)[^a-z].{0,15}video/i.test(instruction);
|
||||
const fresh=!hasVideo||createIntent;
|
||||
const style=styleModule.getStyle(options.style),target=Number(options.target);
|
||||
const budget=providers.isLocal(c.baseUrl)?3800:9000;
|
||||
let r,ops,lastError='';
|
||||
for(let attempt=0;attempt<3;attempt++){
|
||||
try{
|
||||
const prompt=providers.plannerPrompt(snapshot,String(instruction).slice(0,3000),{fresh,target,style,budget})+(lastError?'\n上一次方案被校验拒绝:'+lastError+'。请只修正被拒绝的部分,重新返回完整 JSON。':'');
|
||||
r=await providers.complete(c,prompt,[],signal);
|
||||
if(!Array.isArray(r.data.operations))throw Error('模型返回缺少 operations 数组');
|
||||
ops=r.data.operations.filter(op=>op&&typeof op==='object'&&['add','update','remove'].includes(op.type));
|
||||
if(fresh){const removed=new Set(snapshot.clips.map(x=>x.id));ops=ops.filter(op=>op.type!=='remove'||!removed.has(op.id));ops=[...snapshot.clips.map(x=>({type:'remove',id:x.id})),...ops];}
|
||||
const cleaned=core.sanitizeOperations(snapshot,ops);
|
||||
ops=styleModule.styleOperations(cleaned.operations,style);
|
||||
core.applyOperations(snapshot,ops);
|
||||
if(cleaned.notes.length)r.data.summary=String(r.data.summary||'AI 剪辑方案')+`(已自动修正 ${cleaned.notes.length} 处细节)`;
|
||||
lastError='';break;
|
||||
}catch(e){signal.throwIfAborted();lastError=String(e.message||e).slice(0,400);if(attempt===2)break;progress(.4,'方案未通过校验,请求模型修正(第 '+(attempt+1)+' 次)');}
|
||||
}
|
||||
if(!r){const text='AI 规划失败:'+(lastError||'未知错误');try{sameProject(snapshot.id);project.messages.push({id:randomUUID(),role:'assistant',error:true,text,time:new Date().toISOString()});changed();}catch{}throw Error(text);}
|
||||
sameProject(snapshot.id);const id=randomUUID(),plan={id,summary:String(r.data.summary||'AI 剪辑方案').slice(0,600),operations:ops,base:JSON.stringify(snapshot.clips),projectId:snapshot.id,usage:r.usage};pendingPlans.set(id,plan);
|
||||
project.messages.push({id:randomUUID(),role:'user',text:String(instruction).slice(0,3000),time:new Date().toISOString()},{id:randomUUID(),role:'assistant',text:plan.summary,planId:id,operations:plan.operations,usage:r.usage,model:c.model,elapsed:r.elapsed,time:new Date().toISOString()});changed();emit('plan',plan);return {planId:id,usage:r.usage};});});
|
||||
handle('assemble',options=>{const result=styleModule.assemble(project,options);core.applyOperations(project,result.operations);const id=randomUUID(),plan={id,...result,base:JSON.stringify(project.clips),projectId:project.id};pendingPlans.set(id,plan);emit('plan',plan);return plan;});
|
||||
handle('applyPlan',id=>{const plan=pendingPlans.get(id);if(!plan||plan.projectId!==project.id)throw Error('方案已失效,请重新规划');if(plan.base!==JSON.stringify(project.clips))throw Error('时间轴已修改,请重新生成方案');project=core.applyOperations(project,plan.operations,'AI:'+plan.summary.slice(0,60));project.messages.push({id:randomUUID(),role:'system',text:'已应用方案,可撤销。',time:new Date().toISOString()});pendingPlans.delete(id);changed();return view();});
|
||||
handle('preview',()=>{const snapshot=structuredClone(project);return task('渲染时间轴预览',async(signal,progress)=>{const f=path.join(cache,`preview-${randomUUID()}.mp4`);await media.exportVideo(snapshot,f,{preview:true,acceleration:config.processingMode},signal,progress);sameProject(snapshot.id);if(JSON.stringify(snapshot.clips)!==JSON.stringify(project.clips))throw Error('时间轴已变化,请重新生成预览');emit('preview',{url:urlFor(f),path:f});return {path:f};});});
|
||||
|
||||
Reference in new issue
Block a user