'use strict';var chunkQXMXSHYS_cjs=require('./chunk-QXMXSHYS.cjs'),chunkW6MVJKWN_cjs=require('./chunk-W6MVJKWN.cjs');var L={"gpt-4.1":{input:2,output:8},"gpt-4.1-mini":{input:.4,output:1.6},"gpt-4.1-nano":{input:.1,output:.4},"gpt-4o":{input:2.5,output:10},"gpt-4o-mini":{input:.15,output:.6},"gpt-4-turbo":{input:10,output:30},"o4-mini":{input:1.1,output:4.4},o3:{input:10,output:40},"o3-mini":{input:1.1,output:4.4}};function B(g){let{apiKey:s,model:h="gpt-4o",maxTokens:m,baseURL:r="https://api.openai.com/v1",fetch:b=globalThis.fetch,timeoutMs:i,hooks:c,temperature:n,topP:l,stop:a,responseFormat:p}=g;chunkW6MVJKWN_cjs.d(r),chunkQXMXSHYS_cjs.c(s,"createOpenAIRunner");let o=p==="json"?{type:"json_object"}:p??void 0;return chunkW6MVJKWN_cjs.e({fetch:b,hooks:c,buildRequest:(t,u,d)=>({url:`${r}/chat/completions`,init:{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${s}`},body:JSON.stringify({model:t.model??h,...m!=null?{max_tokens:m}:{},...n!=null?{temperature:n}:{},...l!=null?{top_p:l}:{},...a!=null?{stop:a}:{},...o!=null?{response_format:o}:{},messages:[...t.instructions?[{role:"system",content:t.instructions}]:[],...d.map(e=>({role:e.role,content:e.content}))]}),...i!=null?{signal:AbortSignal.timeout(i)}:{}}}),parseResponse:async t=>{let u=await t.json(),d=u.choices?.[0]?.message?.content??"",e=u.usage?.prompt_tokens??0,f=u.usage?.completion_tokens??0;return {text:d,totalTokens:e+f,inputTokens:e,outputTokens:f}}})}function M(g){let{apiKey:s,model:h="text-embedding-3-small",dimensions:m=1536,baseURL:r="https://api.openai.com/v1",fetch:b=globalThis.fetch,timeoutMs:i}=g;return chunkW6MVJKWN_cjs.d(r),chunkQXMXSHYS_cjs.c(s,"createOpenAIEmbedder"),async c=>{let n=await b(`${r}/embeddings`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${s}`},body:JSON.stringify({model:h,input:c,dimensions:m}),signal:AbortSignal.timeout(i??3e4)});if(!n.ok){let p=await n.text().catch(()=>"");throw new Error(`[Directive] OpenAI embedding failed: ${n.status}${p?` \u2013 ${p.slice(0,200)}`:""}`)}let a=(await n.json()).data[0];if(!a)throw new Error("[Directive] OpenAI embedding response contained no data entries");return a.embedding}}function v(g){let{apiKey:s,model:h="gpt-4o",maxTokens:m,baseURL:r="https://api.openai.com/v1",fetch:b=globalThis.fetch,hooks:i,temperature:c,topP:n,stop:l,responseFormat:a}=g;chunkW6MVJKWN_cjs.d(r),chunkQXMXSHYS_cjs.c(s,"createOpenAIStreamingRunner");let p=a==="json"?{type:"json_object"}:a??void 0;return async(o,t,u)=>{let d=chunkQXMXSHYS_cjs.e(i,o,t);try{let e=await b(`${r}/chat/completions`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${s}`},body:JSON.stringify({model:o.model??h,...m!=null?{max_tokens:m}:{},...c!=null?{temperature:c}:{},...n!=null?{top_p:n}:{},...l!=null?{stop:l}:{},...p!=null?{response_format:p}:{},messages:[...o.instructions?[{role:"system",content:o.instructions}]:[],{role:"user",content:t}],stream:!0,stream_options:{include_usage:!0}}),signal:u.signal});e.ok||await chunkQXMXSHYS_cjs.a(e,"OpenAI");let f=chunkQXMXSHYS_cjs.b(e),{fullText:T,inputTokens:O,outputTokens:S}=await chunkQXMXSHYS_cjs.d(f,u.onToken,k=>{let y={},w=k.choices?.[0]?.delta;return w?.content&&(y.text=w.content),k.usage&&(y.inputTokens=k.usage.prompt_tokens??0,y.outputTokens=k.usage.completion_tokens??0),y},"OpenAI"),_={inputTokens:O,outputTokens:S},j=O+S;return u.onMessage?.({role:"assistant",content:T}),chunkQXMXSHYS_cjs.f(i,o,t,T,j,_,d),chunkQXMXSHYS_cjs.h(t,T,j,_)}catch(e){throw chunkQXMXSHYS_cjs.g(i,o,t,e,d),e}}}
exports.OPENAI_PRICING=L;exports.createOpenAIEmbedder=M;exports.createOpenAIRunner=B;exports.createOpenAIStreamingRunner=v;//# sourceMappingURL=openai.cjs.map
//# sourceMappingURL=openai.cjs.map