import{c as e,d as t,u as n}from"./index-8Q1Ovled.js";import{t as r}from"./headers-5EYI0_pl.js";import{i,n as a,r as o,t as s}from"./github-copilot-headers-CMb2BbzT.js";import{r as c}from"./transform-messages-Dhj_4OTw.js";import{n as l}from"./openai-Bf1npfRy.js";import{t as u}from"./openai-prompt-cache-CErE62Yt.js";import{n as d,r as f,t as p}from"./openai-responses-shared-G6WDDqJ8.js";var m=new Set([`openai`,`openai-codex`,`opencode`]);function h(e){return e||(typeof process<`u`&&{}.PI_CACHE_RETENTION===`long`?`long`:`short`)}function g(e){return{sendSessionIdHeader:e.compat?.sendSessionIdHeader??!0,supportsLongCacheRetention:e.compat?.supportsLongCacheRetention??!0}}function _(e,t){return t===`long`&&e.supportsLongCacheRetention?`24h`:void 0}function v(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 y=(n,i,a)=>{let o=new e;return(async()=>{let e={role:`assistant`,content:[],api:n.api,provider:n.provider,model:n.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 s=a?.apiKey||t(n.provider)||``,c=h(a?.cacheRetention)===`none`?void 0:a?.sessionId,l=x(n,i,s,a?.headers,c),u=S(n,i,a),d=await a?.onPayload?.(u,n);d!==void 0&&(u=d);let p={...a?.signal?{signal:a.signal}:{},...a?.timeoutMs===void 0?{}:{timeout:a.timeoutMs},...a?.maxRetries===void 0?{}:{maxRetries:a.maxRetries}},{data:m,response:g}=await l.responses.create(u,p).withResponse();if(await a?.onResponse?.({status:g.status,headers:r(g.headers)},n),o.push({type:`start`,partial:e}),await f(m,e,o,n,{serviceTier:a?.serviceTier,applyServiceTierPricing:(e,t)=>w(e,t,n)}),a?.signal?.aborted)throw Error(`Request was aborted`);if(e.stopReason===`aborted`||e.stopReason===`error`)throw Error(`An unknown error occurred`);o.push({type:`done`,reason:e.stopReason,message:e}),o.end()}catch(t){for(let t of e.content)delete t.index,delete t.partialJson;e.stopReason=a?.signal?.aborted?`aborted`:`error`,e.errorMessage=v(t),o.push({type:`error`,reason:e.stopReason,error:e}),o.end()}})(),o},b=(e,r,i)=>{let a=i?.apiKey||t(e.provider);if(!a)throw Error(`No API key for provider: ${e.provider}`);let o=c(e,i,a),s=i?.reasoning?n(e,i.reasoning):void 0,l=s===`off`?void 0:s;return y(e,r,{...o,reasoningEffort:l})};function x(e,t,n,r,c){if(!n){if(!{}.OPENAI_API_KEY)throw Error(`OpenAI API key is required. Set OPENAI_API_KEY environment variable or pass it as an argument.`);n={}.OPENAI_API_KEY}let u=g(e),d={...e.headers};if(e.provider===`github-copilot`){let e=a(t.messages),n=s({messages:t.messages,hasImages:e});Object.assign(d,n)}c&&(u.sendSessionIdHeader&&(d.session_id=c),d[`x-client-request-id`]=c),r&&Object.assign(d,r);let f=e.provider===`cloudflare-ai-gateway`?{...d,Authorization:d.Authorization??null,"cf-aig-authorization":`Bearer ${n}`}:d;return new l({apiKey:n,baseURL:o(e.provider)?i(e):e.baseUrl,dangerouslyAllowBrowser:!0,defaultHeaders:f})}function S(e,t,n){let r=p(e,t,m),i=h(n?.cacheRetention),a=g(e),o={model:e.id,input:r,stream:!0,prompt_cache_key:i===`none`?void 0:u(n?.sessionId),prompt_cache_retention:_(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=d(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 C(e,t){switch(t){case`flex`:return .5;case`priority`:return e.id===`gpt-5.5`?2.5:2;default:return 1}}function w(e,t,n){let r=C(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{y as streamOpenAIResponses,b as streamSimpleOpenAIResponses};