import {e as e$1,f,h,g}from'./chunk-265ZKXYE.js';import {d,e}from'./chunk-W6WZBQER.js';var J={llama3:{input:0,output:0},"llama3.1":{input:0,output:0},"llama3.2":{input:0,output:0},"llama3.3":{input:0,output:0},mistral:{input:0,output:0},mixtral:{input:0,output:0},codellama:{input:0,output:0},gemma2:{input:0,output:0},phi3:{input:0,output:0},qwen2:{input:0,output:0},deepseek:{input:0,output:0},"deepseek-coder":{input:0,output:0},"command-r":{input:0,output:0}};function B(b={}){let{model:k="llama3",baseURL:a="http://localhost:11434",fetch:O=globalThis.fetch,timeoutMs:i,hooks:f,temperature:p,topP:c,stop:m,numPredict:n}=b;d(a);let r={};p!=null&&(r.temperature=p),c!=null&&(r.top_p=c),m!=null&&(r.stop=m),n!=null&&(r.num_predict=n);let s=Object.keys(r).length>0;return e({fetch:O,hooks:f,buildRequest:(e,o,u)=>({url:`${a}/api/chat`,init:{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({model:e.model??k,messages:[...e.instructions?[{role:"system",content:e.instructions}]:[],...u.map(t=>({role:t.role,content:t.content}))],stream:false,...s?{options:r}:{}}),...i!=null?{signal:AbortSignal.timeout(i)}:{}}}),parseResponse:async e=>{let o;try{o=await e.json();}catch{throw new Error(`[Directive] Ollama returned non-JSON response. Is Ollama running at ${a}? Start it with: ollama serve`)}let u=o.message?.content??"",t=o.prompt_eval_count??0,l=o.eval_count??0;return {text:u,totalTokens:t+l,inputTokens:t,outputTokens:l}}})}function I(b={}){let{model:k="llama3",baseURL:a="http://localhost:11434",fetch:O=globalThis.fetch,hooks:i,temperature:f$1,topP:p,stop:c,numPredict:m}=b;d(a);let n={};f$1!=null&&(n.temperature=f$1),p!=null&&(n.top_p=p),c!=null&&(n.stop=c),m!=null&&(n.num_predict=m);let r=Object.keys(n).length>0;return async(s,e,o)=>{let u=e$1(i,s,e);try{let t=await O(`${a}/api/chat`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({model:s.model??k,messages:[...s.instructions?[{role:"system",content:s.instructions}]:[],{role:"user",content:e}],stream:!0,...r?{options:n}:{}}),signal:o.signal});if(!t.ok){let g=await t.text().catch(()=>"");throw new Error(`[Directive] Ollama streaming error ${t.status}${g?` \u2013 ${g.slice(0,200)}`:""}`)}let l=t.body?.getReader();if(!l)throw new Error("[Directive] No response body from Ollama");let D=new TextDecoder,R="",h$1="",y=0,w=0;try{for(;;){let{done:g,value:E}=await l.read();if(g)break;R+=D.decode(E,{stream:!0});let x=R.split(`
`);R=x.pop()??"";for(let H of x){let T=H.trim();if(!T)continue;let d;try{d=JSON.parse(T);}catch{typeof process<"u"&&process.env?.NODE_ENV==="development"&&console.warn("[Directive] Malformed streaming chunk from Ollama:",T);continue}let S=d.message?.content??"";S&&(h$1+=S,o.onToken?.(S)),d.done&&(y=d.prompt_eval_count??0,w=d.eval_count??0);}}}finally{l.cancel().catch(()=>{});}let _={inputTokens:y,outputTokens:w},P=y+w;return o.onMessage?.({role:"assistant",content:h$1}),f(i,s,e,h$1,P,_,u),h(e,h$1,P,_)}catch(t){throw g(i,s,e,t,u),t}}}export{J as OLLAMA_PRICING,B as createOllamaRunner,I as createOllamaStreamingRunner};//# sourceMappingURL=ollama.js.map
//# sourceMappingURL=ollama.js.map