import{t as e}from"./env-api-keys-Cuz5irWW.mjs";import{o as t,t as n}from"./event-stream-B7Ei41Bu.mjs";import{n as r}from"./openai-C-rgGb7X.mjs";import{t as i}from"./headers-Bpsp70B9.mjs";import{i as a,r as o}from"./transform-messages-BTA3xbAI.mjs";import{n as s,t as c}from"./github-copilot-headers-BfhryVuU.mjs";import{n as l,r as u,t as d}from"./openai-responses-shared-DqW1JSux.mjs";const f=new Set([`openai`,`openai-codex`,`opencode`]);function p(e){return e||(typeof process<`u`&&process.env.PI_CACHE_RETENTION===`long`?`long`:`short`)}function m(e,t){if(t===`long`&&e.includes(`api.openai.com`))return`24h`}const h=(t,r,a)=>{let o=new n;return(async()=>{let n={role:`assistant`,content:[],api:t.api,provider:t.provider,model:t.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||e(t.provider)||``,c=p(a?.cacheRetention)===`none`?void 0:a?.sessionId,l=_(t,r,s,a?.headers,c),d=v(t,r,a),f=await a?.onPayload?.(d,t);f!==void 0&&(d=f);let{data:m,response:h}=await l.responses.create(d,a?.signal?{signal:a.signal}:void 0).withResponse();if(await a?.onResponse?.({status:h.status,headers:i(h.headers)},t),o.push({type:`start`,partial:n}),await u(m,n,o,t,{serviceTier:a?.serviceTier,applyServiceTierPricing:b}),a?.signal?.aborted)throw Error(`Request was aborted`);if(n.stopReason===`aborted`||n.stopReason===`error`)throw Error(`An unknown error occurred`);o.push({type:`done`,reason:n.stopReason,message:n}),o.end()}catch(e){for(let e of n.content)delete e.index,delete e.partialJson;n.stopReason=a?.signal?.aborted?`aborted`:`error`,n.errorMessage=e instanceof Error?e.message:JSON.stringify(e),o.push({type:`error`,reason:n.stopReason,error:n}),o.end()}})(),o},g=(n,r,i)=>{let s=i?.apiKey||e(n.provider);if(!s)throw Error(`No API key for provider: ${n.provider}`);let c=o(n,i,s),l=t(n)?i?.reasoning:a(i?.reasoning);return h(n,r,{...c,reasoningEffort:l})};function _(e,t,n,i,a){if(!n){if(!process.env.OPENAI_API_KEY)throw Error(`OpenAI API key is required. Set OPENAI_API_KEY environment variable or pass it as an argument.`);n=process.env.OPENAI_API_KEY}let o={...e.headers};if(e.provider===`github-copilot`){let e=s(t.messages),n=c({messages:t.messages,hasImages:e});Object.assign(o,n)}return a&&(o.session_id=a,o[`x-client-request-id`]=a),i&&Object.assign(o,i),new r({apiKey:n,baseURL:e.baseUrl,dangerouslyAllowBrowser:!0,defaultHeaders:o})}function v(e,t,n){let r=d(e,t,f),i=p(n?.cacheRetention),a={model:e.id,input:r,stream:!0,prompt_cache_key:i===`none`?void 0:n?.sessionId,prompt_cache_retention:m(e.baseUrl,i),store:!1};return n?.maxTokens&&(a.max_output_tokens=n?.maxTokens),n?.temperature!==void 0&&(a.temperature=n?.temperature),n?.serviceTier!==void 0&&(a.service_tier=n.serviceTier),t.tools&&(a.tools=l(t.tools)),e.reasoning&&(n?.reasoningEffort||n?.reasoningSummary?(a.reasoning={effort:n?.reasoningEffort||`medium`,summary:n?.reasoningSummary||`auto`},a.include=[`reasoning.encrypted_content`]):e.provider!==`github-copilot`&&(a.reasoning={effort:`none`})),a}function y(e){switch(e){case`flex`:return .5;case`priority`:return 2;default:return 1}}function b(e,t){let n=y(t);n!==1&&(e.cost.input*=n,e.cost.output*=n,e.cost.cacheRead*=n,e.cost.cacheWrite*=n,e.cost.total=e.cost.input+e.cost.output+e.cost.cacheRead+e.cost.cacheWrite)}export{h as streamOpenAIResponses,g as streamSimpleOpenAIResponses};