async function e(e,t){const s=e.body.getReader(),a=new TextDecoder("utf-8");let o="";for(;;){const{done:e,value:r}=await s.read();if(e)break;o+=a.decode(r,{stream:!0});const i=o.split("\n");o=i.pop();for(const s of i)if(s.startsWith("data:")){const e=s.substring(5).trim();if(""===e)continue;try{const s=JSON.parse(e);if(s.output&&s.output.choices&&s.output.choices[0].message){const e=s.output.choices[0].message.content;let a="";if("string"==typeof e)a=e;else if(Array.isArray(e))for(const t of e)t.text&&(a+=t.text);a&&t.postMessage({type:"delta",content:a})}}catch(n){}}}t.postMessage({type:"end"})}chrome.runtime.onConnect.addListener(t=>{"qwen-stream"===t.name&&t.onMessage.addListener(s=>{"start_stream"===s.action&&async function(t,s){const{apiKey:a,systemPrompt:o,userContent:n,model:r}=t,i="https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation";try{const t=await fetch(i,{method:"POST",headers:{Authorization:`Bearer ${a}`,"Content-Type":"application/json","X-DashScope-SSE":"enable"},body:JSON.stringify({model:"qwen3-max",input:{messages:[{role:"system",content:o},{role:"user",content:n}]},parameters:{result_format:"message",response_format:{type:"json_object"},incremental_output:!0}})});if(!t.ok){const e=await t.text();return void s.postMessage({type:"error",content:`Text API Error ${t.status}: ${e}`})}await e(t,s)}catch(c){s.postMessage({type:"error",content:c.message})}}(s,t)}),"qwen-vl-stream"===t.name&&t.onMessage.addListener(s=>{"start_vl_stream"===s.action&&async function(t,s){const{apiKey:a,messages:o,model:n}=t,r="https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation";try{const t=await fetch(r,{method:"POST",headers:{Authorization:`Bearer ${a}`,"Content-Type":"application/json","X-DashScope-SSE":"enable"},body:JSON.stringify({model:"qwen3.5-plus",input:{messages:o},parameters:{result_format:"message",response_format:{type:"json_object"},incremental_output:!0}})});if(!t.ok){const e=await t.text();return void s.postMessage({type:"error",content:`VL API Error ${t.status}: ${e}`})}await e(t,s)}catch(i){s.postMessage({type:"error",content:i.message})}}(s,t)})}),chrome.runtime.onMessage.addListener((e,t,s)=>{if("translate_image"===e.action)return async function(e,t){const{apiKey:s,imageUrl:a,sourceLang:o,targetLang:n}=e;try{const e=await fetch("https://dashscope.aliyuncs.com/api/v1/services/aigc/image2image/image-synthesis",{method:"POST",headers:{"X-DashScope-Async":"enable",Authorization:`Bearer ${s}`,"Content-Type":"application/json"},body:JSON.stringify({model:"qwen-mt-image",input:{image_url:a,source_lang:o,target_lang:n}})}),r=await e.json();if(!r.output||!r.output.task_id)throw new Error(r.message||"任务创建失败");const i=await async function(e,t){const s=45;let a=0;return new Promise((o,n)=>{const r=setInterval(async()=>{var i,c;a++;try{const u=await fetch(`https://dashscope.aliyuncs.com/api/v1/tasks/${e}`,{method:"GET",headers:{Authorization:`Bearer ${t}`}}),p=await u.json(),m=p.output.task_status;"SUCCEEDED"===m?(clearInterval(r),o(p.output.image_url||(null==(c=null==(i=p.output.results)?void 0:i[0])?void 0:c.url))):"FAILED"===m||"CANCELED"===m?(clearInterval(r),n(new Error(p.output.message||"Task failed"))):a>=s&&(clearInterval(r),n(new Error("Task timed out")))}catch(u){clearInterval(r),n(u)}},2e3)})}(r.output.task_id,s);t({success:!0,originalUrl:a,resultUrl:i})}catch(r){t({success:!1,originalUrl:a,error:r.message})}}(e,s),!0});
