- 规划提示词携带最近 6 轮对话记录:解决用户说「把刚才这个删掉/重新剪」时 模型看不到上下文、反复返回「未收到具体要求」空方案的问题 - 明确规则:有对话记录时不得以未收到要求为由返回空方案;指代必须结合记录理解 - 素材全部未分析时返回空方案并在摘要提示先执行「AI 分析」, 禁止凭空假设内容或把整段素材放上时间轴 - 扩充新建意图识别:重新/重做/重剪/全部删掉/清空时间轴 均触发先清空再重建 - 模型不存在(404 not found)等常见错误附带下载/检查提示 - 新增 2 个测试:对话历史注入、未分析素材拦截、模型不存在提示
74 lines
6.8 KiB
JavaScript
74 lines
6.8 KiB
JavaScript
import {test} from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import http from 'node:http';
|
|
import {complete,listModels,parseJson,plannerPrompt} from '../desktop/core/providers.mjs';
|
|
import {getStyle} from '../desktop/core/styles.mjs';
|
|
test('适配器协议合约(本机测试服务器,不代表云端联调)',async()=>{
|
|
let body,url,headers;const server=http.createServer(async(req,res)=>{let data='';for await(const part of req)data+=part;body=data?JSON.parse(data):null;url=req.url;headers=req.headers;res.setHeader('Content-Type','application/json');if(req.url==='/api/chat')res.end(JSON.stringify({message:{content:'{"ok":true}'},eval_count:4}));else if(req.url==='/messages')res.end(JSON.stringify({content:[{type:'text',text:'{"ok":true}'}]}));else if(req.url.includes('generateContent'))res.end(JSON.stringify({candidates:[{content:{parts:[{text:'{"ok":true}'}]}}]}));else if(req.url==='/models')res.end(JSON.stringify({data:[{id:'test-only'}]}));else res.end(JSON.stringify({choices:[{message:{content:'{"ok":true}'}}]}));});
|
|
await new Promise(r=>server.listen(0,'127.0.0.1',r));const baseUrl=`http://127.0.0.1:${server.address().port}`;
|
|
try{for(const protocol of ['ollama','openai','anthropic','gemini']){const r=await complete({protocol,baseUrl,model:'test-only',key:'contract-secret'},'json');assert.equal(r.data.ok,true);if(protocol==='anthropic'){assert.equal(headers['x-api-key'],'contract-secret');assert.equal(body.max_tokens,8192);}if(protocol==='gemini'){assert.ok(url.endsWith(':generateContent'));assert.equal(headers['x-goog-api-key'],'contract-secret');}if(protocol==='ollama')assert.equal(body.stream,false);}assert.equal((await listModels({protocol:'openai',baseUrl}))[0].id,'test-only');}
|
|
finally{server.closeAllConnections();server.close();}
|
|
await assert.rejects(complete({protocol:'openai',baseUrl:'https://example.com',model:'x',allowText:false},'private'),/未授权/);
|
|
assert.deepEqual(parseJson('```json\n{"ok":true}\n```'),{ok:true});
|
|
});
|
|
test('JSON 容错解析:思考标签、前置文字、嵌套花括号与字符串中的花括号',()=>{
|
|
assert.deepEqual(parseJson('<think>思考过程</think>{"ok":true}'),{ok:true});
|
|
assert.deepEqual(parseJson('好的,以下是结果:\n```json\n{"a":{"b":[1,{"c":"带}和{括号"}]}}\n```\n希望有帮助'),{a:{b:[1,{c:'带}和{括号'}]}});
|
|
assert.deepEqual(parseJson('{"results":[{"id":"x","text":"}]"}]}'),{results:[{id:'x',text:'}]'}]});
|
|
assert.throws(()=>parseJson('完全没有 JSON 内容'),/JSON/);
|
|
});
|
|
test('JSON 解析失败自动重试:第二次不再强制 format 并追加严格提示',async()=>{
|
|
const calls=[];
|
|
const server=http.createServer(async(req,res)=>{let data='';for await(const part of req)data+=part;const body=JSON.parse(data);calls.push(body);res.setHeader('Content-Type','application/json');
|
|
if(body.format==='json')res.end(JSON.stringify({message:{content:'这是一段没有 JSON 的回答'},eval_count:3}));
|
|
else res.end(JSON.stringify({message:{content:'{"ok":true}'},eval_count:4}));});
|
|
await new Promise(r=>server.listen(0,'127.0.0.1',r));const baseUrl=`http://127.0.0.1:${server.address().port}`;
|
|
try{
|
|
const r=await complete({protocol:'ollama',baseUrl,model:'test-only'},'请输出 JSON');
|
|
assert.equal(r.data.ok,true);
|
|
assert.equal(calls.length,2);
|
|
assert.equal(calls[1].format,undefined);
|
|
assert.ok(calls[1].messages[0].content.includes('只输出一个 JSON 对象'));
|
|
}finally{server.closeAllConnections();server.close();}
|
|
});
|
|
test('规划提示词:按评分筛选片段、丢弃占位描述、区分新建/修改模式、控制数据体量',()=>{
|
|
const mk=(id,score)=>({id,name:id,type:'video',duration:120,tags:['海边'],analysis:{segments:[
|
|
{start:0,end:5,description:'海滩日落,人物走过画面,光线柔和,构图开阔',score,reason:'构图好'},
|
|
{start:10,end:15,description:'尚未进行语义或动作识别',score:null},
|
|
{start:20,end:25,description:'城市夜景,车流灯光',score:score-10,reason:'节奏感强'},
|
|
]}});
|
|
const p={settings:{width:1920,height:1080,fps:30},media:[mk('a',90),mk('b',40)],clips:[{id:'c1',mediaId:'a',track:'video',start:0,in:0,out:5,speed:1,volume:1,motion:[{time:0,zoom:1,x:.5,y:.5}],text:'旧字幕'}]};
|
|
const fresh=plannerPrompt(p,'制作一个旅行短片',{fresh:true,target:45,style:getStyle('cinematic'),budget:9000});
|
|
assert.ok(fresh.includes('海滩日落'));
|
|
assert.ok(fresh.includes('城市夜景'));
|
|
assert.ok(!fresh.includes('尚未进行语义或动作识别'));
|
|
assert.ok(!fresh.includes('motion'));
|
|
assert.ok(fresh.includes('先 remove 现有全部片段'));
|
|
assert.ok(fresh.includes('45'));
|
|
assert.ok(fresh.includes('score'));
|
|
assert.ok(fresh.length<12000);
|
|
const modify=plannerPrompt(p,'把第一个镜头缩短一点',{fresh:false,budget:9000});
|
|
assert.ok(modify.includes('时间轴需要保留'));
|
|
assert.ok(modify.includes('c1'));
|
|
// 大数据量下仍受预算约束
|
|
const big={settings:p.settings,media:Array.from({length:40},(_,i)=>({id:'m'+i,name:'素材'+i,type:'video',duration:300,tags:['t'],analysis:{segments:Array.from({length:10},(_,j)=>({start:j*30,end:j*30+28,description:'第'+j+'段:这是很长的一段语义描述内容用来填充上下文窗口测试预算裁剪是否生效',score:99-j}))}})),clips:Array.from({length:60},(_,i)=>({id:'c'+i,mediaId:'m0',track:'video',start:i*5,in:0,out:5,speed:1,volume:1}))};
|
|
const small=plannerPrompt(big,'重新剪一遍',{fresh:true,budget:3800});
|
|
assert.ok(small.length<6500);
|
|
// 多轮对话历史:指代(这个/那个/现在)的上下文必须发给模型
|
|
const withHist=plannerPrompt(p,'把刚才说的那个删掉',{history:[{role:'user',text:'把片头字幕删掉'},{role:'assistant',text:'已删除片头字幕'}],budget:9000});
|
|
assert.ok(withHist.includes('把片头字幕删掉'));
|
|
assert.ok(withHist.includes('对话记录'));
|
|
assert.ok(withHist.includes('不得以「未收到具体要求」为由返回空方案'));
|
|
// 素材未分析时明确拦截:不允许凭空假设或整段取用
|
|
const raw={settings:p.settings,media:[{id:'u',name:'未分析素材',type:'video',duration:600,tags:[]}],clips:[]};
|
|
const unanalyzed=plannerPrompt(raw,'剪一个鲁迪和希露菲在一起的片段',{budget:9000});
|
|
assert.ok(unanalyzed.includes('素材均未经过 AI 分析'));
|
|
assert.ok(unanalyzed.includes('不要直接把整段素材放上时间轴'));
|
|
});
|
|
test('模型不存在等常见错误附带可操作提示',async()=>{
|
|
const server=http.createServer((req,res)=>{res.statusCode=404;res.setHeader('Content-Type','application/json');res.end(JSON.stringify({error:"model 'gemma3:4b' not found"}));});
|
|
await new Promise(r=>server.listen(0,'127.0.0.1',r));const baseUrl=`http://127.0.0.1:${server.address().port}`;
|
|
try{await assert.rejects(complete({protocol:'ollama',baseUrl,model:'gemma3:4b'},'帮我剪'),/未安装/);}
|
|
finally{server.closeAllConnections();server.close();}
|
|
});
|