import{c as e,d as t,u as n}from"./index-D0CVLdX_.js";import{t as r}from"./headers-5EYI0_pl.js";import{r as i}from"./transform-messages-CmnxG9RB.js";import{i as a,n as o,r as s,t as c}from"./github-copilot-headers-CMb2BbzT.js";import{n as l}from"./openai-Cn7eGqwa.js";import{n as u,r as d,t as f}from"./openai-responses-shared-DF3ZGaUx.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{sendSessionIdHeader:e.compat?.sendSessionIdHeader??!0,supportsLongCacheRetention:e.compat?.supportsLongCacheRetention??!0}}function g(e,t){return t===`long`&&e.supportsLongCacheRetention?`24h`:void 0}var _=(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=m(a?.cacheRetention)===`none`?void 0:a?.sessionId,l=y(n,i,s,a?.headers,c),u=b(n,i,a),f=await a?.onPayload?.(u,n);f!==void 0&&(u=f);let p={...a?.signal?{signal:a.signal}:{},...a?.timeoutMs===void 0?{}:{timeout:a.timeoutMs},...a?.maxRetries===void 0?{}:{maxRetries:a.maxRetries}},{data:h,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 d(h,e,o,n,{serviceTier:a?.serviceTier,applyServiceTierPricing:(e,t)=>S(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=t instanceof Error?t.message:JSON.stringify(t),o.push({type:`error`,reason:e.stopReason,error:e}),o.end()}})(),o},v=(e,r,a)=>{let o=a?.apiKey||t(e.provider);if(!o)throw Error(`No API key for provider: ${e.provider}`);let s=i(e,a,o),c=a?.reasoning?n(e,a.reasoning):void 0,l=c===`off`?void 0:c;return _(e,r,{...s,reasoningEffort:l})};function y(e,t,n,r,i){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=h(e),d={...e.headers};if(e.provider===`github-copilot`){let e=o(t.messages),n=c({messages:t.messages,hasImages:e});Object.assign(d,n)}i&&(u.sendSessionIdHeader&&(d.session_id=i),d[`x-client-request-id`]=i),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:s(e.provider)?a(e):e.baseUrl,dangerouslyAllowBrowser:!0,defaultHeaders:f})}function b(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: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 x(e,t){switch(t){case`flex`:return .5;case`priority`:return e.id===`gpt-5.5`?2.5:2;default:return 1}}function S(e,t,n){let r=x(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{_ as streamOpenAIResponses,v as streamSimpleOpenAIResponses};