const v="/aiui/",u=`${v}api/ollama`;async function*w(o,t,a){const i=o.filter(e=>e.role!=="system").map(e=>({role:e.role,content:typeof e.content=="string"?e.content:e.content.map(d=>d.text??"").join("")})),r=o.find(e=>e.role==="system"),m=r?typeof r.content=="string"?r.content:r.content.map(e=>e.text??"").join(""):void 0;m&&i.unshift({role:"system",content:m});const y={model:t.model||"qwen2.5-coder:3b",messages:i,stream:!0};t.temperature!==void 0&&(y.temperature=t.temperature);let s;try{s=await fetch(`${u}/api/chat`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(y)})}catch{yield{type:"error",error:"Cannot connect to Ollama. Is it running on this node?"};return}if(!s.ok){const e=await s.text().catch(()=>"Could not read error body");yield{type:"error",error:`Ollama error ${s.status}: ${e}`};return}const l=s.body?.getReader();if(!l){yield{type:"error",error:"No response body"};return}const h=new TextDecoder;let c="";try{for(;;){const{done:e,value:d}=await l.read();if(e)break;c+=h.decode(d,{stream:!0});const f=c.split(`
`);c=f.pop()??"";for(const g of f)if(g.trim())try{const n=JSON.parse(g);if(n.message?.content&&(yield{type:"text",text:n.message.content}),n.done){yield{type:"done",usage:n.eval_count?{promptTokens:n.prompt_eval_count??0,completionTokens:n.eval_count??0}:void 0};return}}catch{}}}finally{l.cancel().catch(()=>{})}yield{type:"done"}}const x={id:"ollama",name:"Local AI (Ollama)",version:"1.0.0",type:"ai-provider",description:"Local AI model via Ollama — fully private, no data leaves your node",supportsStreaming:!0,supportsVision:!1,supportsTools:!1,async init(o){},async destroy(){},async isAvailable(){try{return(await fetch(`${u}/api/tags`,{signal:AbortSignal.timeout(3e3)})).ok}catch{return!1}},chat(o,t){return w(o,t)},async models(){try{const o=await fetch(`${u}/api/tags`,{signal:AbortSignal.timeout(5e3)});if(!o.ok)return p();const t=await o.json();return t.models?.length?t.models.map(a=>({id:a.name,name:b(a.name),provider:"ollama",supportsVision:!1,supportsTools:!1,contextWindow:8192})):p()}catch{return p()}}};function p(){return[{id:"qwen2.5-coder:3b",name:"Qwen 2.5 Coder 3B",provider:"ollama",supportsVision:!1,supportsTools:!1,contextWindow:8192}]}function b(o){const t=o.split(":"),a=t[0].replace(/[.-]/g," ").replace(/\b\w/g,r=>r.toUpperCase()),i=t[1]?` (${t[1]})`:"";return`${a}${i}`}export{x as ollamaProvider};
