Merge pull request 'fix(ai): 模型返回容错解析与失败自动重试,减少 AI 剪辑助手报错' (#2) from fix/ai-json-robustness into main

Reviewed-on: http://git.xhmcl.com/xhgit/AI-cut/pulls/2
This commit was merged in pull request #2.
This commit is contained in:
xhgit committed 2026-11-14 03:19:28 +00:00
commit 61cff864b9
2 files changed
+46 -5

No files matched your search

+26 -5
View File
@@ -20,14 +20,35 @@ export async function request(c,endpoint,body,signal){
}
if(!res.ok){let detail=(await res.text()).slice(0,1200);if(c.key)detail=detail.replaceAll(c.key,'[已隐藏]');throw Error(`模型服务 HTTP ${res.status}:${detail}`);}return res;
}
export function parseJson(text){const cleaned=text.replace(/<think>[\s\S]*?<\/think>/g,'').replace(/^\s*```(?:json)?\s*/,'').replace(/\s*```\s*$/,'').trim();try{return JSON.parse(cleaned);}catch{const start=cleaned.indexOf('{'),end=cleaned.lastIndexOf('}');if(start>=0&&end>start)return JSON.parse(cleaned.slice(start,end+1));throw Error('模型未返回有效 JSON,请重试或更换模型');}}
export async function complete(c,prompt,images=[],signal){const started=Date.now();try{const result=await completeRequest(c,prompt,images,signal);observer({time:new Date().toISOString(),model:c.model,protocol:c.protocol,accountId:c.id,images:images.length,status:'ok',elapsed:Date.now()-started,usage:result.usage});return result;}catch(e){let error=String(e.message);if(c.key)error=error.replaceAll(c.key,'[已隐藏]');observer({time:new Date().toISOString(),model:c.model,protocol:c.protocol,accountId:c.id,images:images.length,status:'error',elapsed:Date.now()-started,error});throw Error(error);}}
async function completeRequest(c,prompt,images=[],signal){
export function parseJson(text){
let t=String(text||'').replace(/^/,'').replace(/<think[\s\S]*?<\/think>/g,'').replace(/<\/?thinking>/g,'').trim();
const fence=/^```(?:json)?\s*([\s\S]*?)\s*```$/.exec(t);if(fence)t=fence[1].trim();
try{return JSON.parse(t);}catch{}
const start=t.indexOf('{');
if(start>=0){let depth=0,inStr=false,esc=false;
for(let i=start;i<t.length;i++){const ch=t[i];
if(inStr){if(esc)esc=false;else if(ch==='\\')esc=true;else if(ch==='"')inStr=false;continue;}
if(ch==='"')inStr=true;else if(ch==='{')depth++;else if(ch==='}'&&--depth===0){const cand=t.slice(start,i+1);try{return JSON.parse(cand);}catch{break;}}}
}
throw Error('模型未返回有效 JSON,请重试,或在模型管理中更换能力更强的模型');
}
const STRICT='\n请只输出一个 JSON 对象,不要输出解释、代码块标记或任何其他文本。';
function friendlify(msg){if(/context.{0,40}(length|大小|超出)|(length|大小).{0,20}context/i.test(msg))return msg+';项目数据过大,可先移除无关素材或精简时间轴后再试';return msg;}
export async function complete(c,prompt,images=[],signal){
const started=Date.now();
try{
let result,attempts=1;
try{result=await completeRequest(c,prompt,images,signal);}
catch(e){if(!/JSON/i.test(String(e&&e.message)))throw e;attempts=2;result=await completeRequest(c,prompt+STRICT,images,signal,{formatJson:false});}
observer({time:new Date().toISOString(),model:c.model,protocol:c.protocol,accountId:c.id,images:images.length,status:'ok',elapsed:Date.now()-started,usage:result.usage,attempts});return result;
}catch(e){let error=String(e.message);if(c.key)error=error.replaceAll(c.key,'[已隐藏]');error=friendlify(error);observer({time:new Date().toISOString(),model:c.model,protocol:c.protocol,accountId:c.id,images:images.length,status:'error',elapsed:Date.now()-started,error});throw Error(error);}
}
async function completeRequest(c,prompt,images=[],signal,{formatJson=true}={}){
if(images.length&&!isLocal(c.baseUrl)&&!c.allowFrames)throw Error('云端帧上传未授权,请在模型管理中明确允许');
if(!isLocal(c.baseUrl)&&!c.allowText)throw Error('云端文字与分析摘要传输未授权');
const encoded=await Promise.all(images.map(async i=>(await fs.readFile(i)).toString('base64')));let body,endpoint;
if(c.protocol==='ollama'){endpoint='api/chat';body={model:c.model,stream:false,format:'json',messages:[{role:'user',content:prompt,...(encoded.length?{images:encoded}:{})}],options:{temperature:0.2,num_ctx:16384}};}
else if(c.protocol==='anthropic'){endpoint='messages';body={model:c.model,max_tokens:8192,messages:[{role:'user',content:[...encoded.map(data=>({type:'image',source:{type:'base64',media_type:'image/jpeg',data}})),{type:'text',text:prompt}]}]};}
if(c.protocol==='ollama'){endpoint='api/chat';body={model:c.model,stream:false,...(formatJson?{format:'json'}:{}),messages:[{role:'user',content:prompt,...(encoded.length?{images:encoded}:{})}],options:{temperature:0.2,num_ctx:16384}};}
else if(c.protocol==='anthropic'){endpoint='messages';body={model:c.model,max_tokens:8192,temperature:0.2,messages:[{role:'user',content:[...encoded.map(data=>({type:'image',source:{type:'base64',media_type:'image/jpeg',data}})),{type:'text',text:prompt}]}]};}
else if(c.protocol==='gemini'){endpoint=`models/${encodeURIComponent(c.model)}:generateContent`;body={contents:[{parts:[{text:prompt},...encoded.map(data=>({inline_data:{mime_type:'image/jpeg',data}}))]}],generationConfig:{responseMimeType:'application/json',temperature:0.2}};}
else {endpoint='chat/completions';body={model:c.model,messages:[{role:'user',content:encoded.length?[{type:'text',text:prompt},...encoded.map(data=>({type:'image_url',image_url:{url:'data:image/jpeg;base64,'+data}}))]:prompt}],temperature:0.2};}
const started=Date.now(),j=await(await request(c,endpoint,body,signal)).json();const text=c.protocol==='ollama'?j.message?.content:c.protocol==='anthropic'?j.content?.filter(x=>x.type==='text').map(x=>x.text).join(''):c.protocol==='gemini'?j.candidates?.[0]?.content?.parts?.map(x=>x.text||'').join(''):j.choices?.[0]?.message?.content;
+20
View File
@@ -10,3 +10,23 @@ test('适配器协议合约(本机测试服务器,不代表云端联调)',
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();}
});