// @bun
import{Hh as u,Ih as f,Jh as _}from"./chunk-b2mq44kn.js";import{Kh as z,Mh as C,Qh as U,Rh as I,Th as v,Wh as b,di as R,ei as T,hi as S,ji as h,li as k}from"./chunk-2g868m1z.js";import"./chunk-qze335a6.js";import{Iv as V}from"./chunk-0dcnzv90.js";import"./chunk-xyhgvt88.js";var m=class{constructor(j,G){this.modelId=j,this.config=G,this.specificationVersion="v3",this.maxImagesPerCall=1}get provider(){return this.config.provider}async doGenerate({prompt:j,n:G,size:P,aspectRatio:W,seed:N,providerOptions:K,headers:Y,abortSignal:X,files:Q,mask:q}){var F,Z,J,A,D;let $=[],B=(J=(Z=(F=this.config._internal)==null?void 0:F.currentDate)==null?void 0:Z.call(F))!=null?J:new Date;if(Q!=null&&Q.length>0){let{value:y,responseHeaders:c}=await T({url:this.getEditUrl(),headers:z(this.config.headers(),Y),formData:U({model:this.modelId,prompt:j,image:await Promise.all(Q.map((H)=>w(H))),mask:q!=null?await w(q):void 0,n:G,size:P,...(A=K.deepinfra)!=null?A:{}},{useArrayBrackets:!1}),failedResponseHandler:S({errorSchema:t,errorToMessage:(H)=>{var x,M;return(M=(x=H.error)==null?void 0:x.message)!=null?M:"Unknown error"}}),successfulResponseHandler:h(d),abortSignal:X,fetch:this.config.fetch});return{images:y.data.map((H)=>H.b64_json),warnings:$,response:{timestamp:B,modelId:this.modelId,headers:c}}}let E=P==null?void 0:P.split("x"),{value:L,responseHeaders:O}=await R({url:`${this.config.baseURL}/${this.modelId}`,headers:z(this.config.headers(),Y),body:{prompt:j,num_images:G,...W&&{aspect_ratio:W},...E&&{width:E[0],height:E[1]},...N!=null&&{seed:N},...(D=K.deepinfra)!=null?D:{}},failedResponseHandler:S({errorSchema:g,errorToMessage:(y)=>y.detail.error}),successfulResponseHandler:h(p),abortSignal:X,fetch:this.config.fetch});return{images:L.images.map((y)=>y.replace(/^data:image\/\w+;base64,/,"")),warnings:$,response:{timestamp:B,modelId:this.modelId,headers:O}}}getEditUrl(){return`${this.config.baseURL.replace("/inference","/openai")}/images/edits`}},g=V.object({detail:V.object({error:V.string()})}),p=V.object({images:V.array(V.string())}),t=V.object({error:V.object({message:V.string()}).optional()}),d=V.object({data:V.array(V.object({b64_json:V.string()}))});async function w(j){if(j.type==="url")return I(j.url);let G=j.data instanceof Uint8Array?j.data:C(j.data);return new Blob([G],{type:j.mediaType})}var l=class extends u{constructor(j,G){super(j,G)}fixUsageForGeminiModels(j){var G,P;if(!j||!((G=j.completion_tokens_details)==null?void 0:G.reasoning_tokens))return j;let W=(P=j.completion_tokens)!=null?P:0,N=j.completion_tokens_details.reasoning_tokens;if(N>W){let K=W+N;return{...j,completion_tokens:K,total_tokens:j.total_tokens!=null?j.total_tokens+N:void 0}}return j}async doGenerate(j){var G,P,W,N,K,Y,X;let Q=await super.doGenerate(j);if((G=Q.usage)==null?void 0:G.raw){let q=this.fixUsageForGeminiModels(Q.usage.raw);if(q!==Q.usage.raw){let F=(P=q.prompt_tokens)!=null?P:0,Z=(W=q.completion_tokens)!=null?W:0,J=(K=(N=q.prompt_tokens_details)==null?void 0:N.cached_tokens)!=null?K:0,A=(X=(Y=q.completion_tokens_details)==null?void 0:Y.reasoning_tokens)!=null?X:0;return{...Q,usage:{inputTokens:{total:F,noCache:F-J,cacheRead:J,cacheWrite:void 0},outputTokens:{total:Z,text:Z-A,reasoning:A},raw:q}}}}return Q}async doStream(j){let G=await super.doStream(j),P=G.stream,W=this.fixUsageForGeminiModels.bind(this),N=new ReadableStream({async start(K){var Y,X,Q,q,F,Z,J;let A=P.getReader();try{while(!0){let{done:D,value:$}=await A.read();if(D)break;if($.type==="finish"&&((Y=$.usage)==null?void 0:Y.raw)){let B=W($.usage.raw);if(B!==$.usage.raw){let E=(X=B.prompt_tokens)!=null?X:0,L=(Q=B.completion_tokens)!=null?Q:0,O=(F=(q=B.prompt_tokens_details)==null?void 0:q.cached_tokens)!=null?F:0,y=(J=(Z=B.completion_tokens_details)==null?void 0:Z.reasoning_tokens)!=null?J:0;K.enqueue({...$,usage:{inputTokens:{total:E,noCache:E-O,cacheRead:O,cacheWrite:void 0},outputTokens:{total:L,text:L-y,reasoning:y},raw:B}})}else K.enqueue($)}else K.enqueue($)}K.close()}catch(D){K.error(D)}}});return{...G,stream:N}}},n="2.0.41";function i(j={}){var G;let P=k((G=j.baseURL)!=null?G:"https://api.deepinfra.com/v1"),W=()=>v({Authorization:`Bearer ${b({apiKey:j.apiKey,environmentVariableName:"DEEPINFRA_API_KEY",description:"DeepInfra's API key"})}`,...j.headers},`ai-sdk/deepinfra/${n}`),N=(F)=>({provider:`deepinfra.${F}`,url:({path:Z})=>`${P}/openai${Z}`,headers:W,fetch:j.fetch}),K=(F)=>{return new l(F,N("chat"))},Y=(F)=>new f(F,N("completion")),X=(F)=>new _(F,N("embedding")),Q=(F)=>new m(F,{...N("image"),baseURL:P?`${P}/inference`:"https://api.deepinfra.com/v1/inference"}),q=(F)=>K(F);return q.specificationVersion="v3",q.completionModel=Y,q.chatModel=K,q.image=Q,q.imageModel=Q,q.languageModel=K,q.embeddingModel=X,q.textEmbeddingModel=X,q}var jj=i();export{jj as deepinfra,i as createDeepInfra,n as VERSION};
