import{r as e,t}from"./event-stream-BzGVPQTQ.js";import{t as n}from"./headers-5EYI0_pl.js";import{i as r,n as i,r as a,t as o}from"./github-copilot-headers-CMb2BbzT.js";import{r as s}from"./transform-messages-k0lyAmgl.js";import{n as c}from"./openai-Bf1npfRy.js";import{t as l}from"./openai-prompt-cache-CErE62Yt.js";import{n as u,r as d,t as f}from"./openai-responses-shared-BLKU_5Qn.js";var p=new Set([`openai`,`openai-codex`,`opencode`]);function m(e){return e||(typeof process<`u`&&{}.PI_CACHE_RETENTION===`long`?`long`:`short`)}function h(e){return{supportsDeveloperRole:e.compat?.supportsDeveloperRole??!0,sendSessionIdHeader:e.compat?.sendSessionIdHeader??!0,supportsLongCacheRetention:e.compat?.supportsLongCacheRetention??!0}}function g(e,t){return t===`long`&&e.supportsLongCacheRetention?`24h`:void 0}function _(e){if(e instanceof Error){let t=e.status,n=typeof t==`number`?t:void 0;return n===void 0?e.message:`OpenAI API error (${n}): ${e.message}`}try{return JSON.stringify(e)}catch{return String(e)}}var v=(e,r,i)=>{let a=new t;return(async()=>{let t={role:`assistant`,content:[],api:e.api,provider:e.provider,model:e.id,usage:{input:0,output:0,cacheRead:0,cacheWrite:0,totalTokens:0,cost:{input:0,output:0,cacheRead:0,cacheWrite:0,total:0}},stopReason:`stop`,timestamp:Date.now()};try{let o=i?.apiKey;if(!o)throw Error(`No API key for provider: ${e.provider}`);let s=m(i?.cacheRetention)===`none`?void 0:i?.sessionId,c=b(e,r,o,i?.headers,s),l=x(e,r,i),u=await i?.onPayload?.(l,e);u!==void 0&&(l=u);let f={...i?.signal?{signal:i.signal}:{},...i?.timeoutMs===void 0?{}:{timeout:i.timeoutMs},maxRetries:i?.maxRetries??0},{data:p,response:h}=await c.responses.create(l,f).withResponse();if(await i?.onResponse?.({status:h.status,headers:n(h.headers)},e),a.push({type:`start`,partial:t}),await d(p,t,a,e,{serviceTier:i?.serviceTier,applyServiceTierPricing:(t,n)=>C(t,n,e)}),i?.signal?.aborted)throw Error(`Request was aborted`);if(t.stopReason===`aborted`||t.stopReason===`error`)throw Error(`An unknown error occurred`);a.push({type:`done`,reason:t.stopReason,message:t}),a.end()}catch(e){for(let e of t.content)delete e.index,delete e.partialJson;t.stopReason=i?.signal?.aborted?`aborted`:`error`,t.errorMessage=_(e),a.push({type:`error`,reason:t.stopReason,error:t}),a.end()}})(),a},y=(t,n,r)=>{let i=r?.apiKey;if(!i)throw Error(`No API key for provider: ${t.provider}`);let a=s(t,r,i),o=r?.reasoning?e(t,r.reasoning):void 0,c=o===`off`?void 0:o;return v(t,n,{...a,reasoningEffort:c})};function b(e,t,n,s,l){let u=h(e),d={...e.headers};if(e.provider===`github-copilot`){let e=i(t.messages),n=o({messages:t.messages,hasImages:e});Object.assign(d,n)}l&&(u.sendSessionIdHeader&&(d.session_id=l),d[`x-client-request-id`]=l),s&&Object.assign(d,s);let f=e.provider===`cloudflare-ai-gateway`?{...d,Authorization:d.Authorization??null,"cf-aig-authorization":`Bearer ${n}`}:d;return new c({apiKey:n,baseURL:a(e.provider)?r(e):e.baseUrl,dangerouslyAllowBrowser:!0,defaultHeaders:f})}function x(e,t,n){let r=f(e,t,p),i=m(n?.cacheRetention),a=h(e),o={model:e.id,input:r,stream:!0,prompt_cache_key:i===`none`?void 0:l(n?.sessionId),prompt_cache_retention:g(a,i),store:!1};return n?.maxTokens&&(o.max_output_tokens=n?.maxTokens),n?.temperature!==void 0&&(o.temperature=n?.temperature),n?.serviceTier!==void 0&&(o.service_tier=n.serviceTier),t.tools&&t.tools.length>0&&(o.tools=u(t.tools)),e.reasoning&&(n?.reasoningEffort||n?.reasoningSummary?(o.reasoning={effort:n?.reasoningEffort?e.thinkingLevelMap?.[n.reasoningEffort]??n.reasoningEffort:`medium`,summary:n?.reasoningSummary||`auto`},o.include=[`reasoning.encrypted_content`]):e.provider!==`github-copilot`&&e.thinkingLevelMap?.off!==null&&(o.reasoning={effort:e.thinkingLevelMap?.off??`none`})),o}function S(e,t){switch(t){case`flex`:return .5;case`priority`:return e.id===`gpt-5.5`?2.5:2;default:return 1}}function C(e,t,n){let r=S(n,t);r!==1&&(e.cost.input*=r,e.cost.output*=r,e.cost.cacheRead*=r,e.cost.cacheWrite*=r,e.cost.total=e.cost.input+e.cost.output+e.cost.cacheRead+e.cost.cacheWrite)}export{v as streamOpenAIResponses,y as streamSimpleOpenAIResponses};
//# sourceMappingURL=openai-responses-8DF_peEQ.js.map