import{t as e}from"./env-api-keys-Cuz5irWW.mjs";import{o as t,t as n}from"./event-stream-B7Ei41Bu.mjs";import{t 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,r as c,t as l}from"./openai-responses-shared-DqW1JSux.mjs";const u=new Set([`openai`,`openai-codex`,`opencode`,`azure-openai-responses`]);function d(e){let t=new Map;if(!e)return t;for(let n of e.split(`,`)){let e=n.trim();if(!e)continue;let[r,i]=e.split(`=`,2);!r||!i||t.set(r.trim(),i.trim())}return t}function f(e,t){return t?.azureDeploymentName?t.azureDeploymentName:d(process.env.AZURE_OPENAI_DEPLOYMENT_NAME_MAP).get(e.id)||e.id}const p=(t,r,a)=>{let o=new n;return(async()=>{let n=f(t,a),s={role:`assistant`,content:[],api:`azure-openai-responses`,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 l=v(t,a?.apiKey||e(t.provider)||``,a),u=y(t,r,a,n),d=await a?.onPayload?.(u,t);d!==void 0&&(u=d);let{data:f,response:p}=await l.responses.create(u,a?.signal?{signal:a.signal}:void 0).withResponse();if(await a?.onResponse?.({status:p.status,headers:i(p.headers)},t),o.push({type:`start`,partial:s}),await c(f,s,o,t),a?.signal?.aborted)throw Error(`Request was aborted`);if(s.stopReason===`aborted`||s.stopReason===`error`)throw Error(`An unknown error occurred`);o.push({type:`done`,reason:s.stopReason,message:s}),o.end()}catch(e){for(let e of s.content)delete e.index,delete e.partialJson;s.stopReason=a?.signal?.aborted?`aborted`:`error`,s.errorMessage=e instanceof Error?e.message:JSON.stringify(e),o.push({type:`error`,reason:s.stopReason,error:s}),o.end()}})(),o},m=(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 p(n,r,{...c,reasoningEffort:l})};function h(e){return e.replace(/\/+$/,``)}function g(e){return`https://${e}.openai.azure.com/openai/v1`}function _(e,t){let n=t?.azureApiVersion||process.env.AZURE_OPENAI_API_VERSION||`v1`,r=t?.azureBaseUrl?.trim()||process.env.AZURE_OPENAI_BASE_URL?.trim()||void 0,i=t?.azureResourceName||process.env.AZURE_OPENAI_RESOURCE_NAME,a=r;if(!a&&i&&(a=g(i)),!a&&e.baseUrl&&(a=e.baseUrl),!a)throw Error(`Azure OpenAI base URL is required. Set AZURE_OPENAI_BASE_URL or AZURE_OPENAI_RESOURCE_NAME, or pass azureBaseUrl, azureResourceName, or model.baseUrl.`);return{baseUrl:h(a),apiVersion:n}}function v(e,t,n){if(!t){if(!process.env.AZURE_OPENAI_API_KEY)throw Error(`Azure OpenAI API key is required. Set AZURE_OPENAI_API_KEY environment variable or pass it as an argument.`);t=process.env.AZURE_OPENAI_API_KEY}let i={...e.headers};n?.headers&&Object.assign(i,n.headers);let{baseUrl:a,apiVersion:o}=_(e,n);return new r({apiKey:t,apiVersion:o,dangerouslyAllowBrowser:!0,defaultHeaders:i,baseURL:a})}function y(e,t,n,r){let i={model:r,input:l(e,t,u),stream:!0,prompt_cache_key:n?.sessionId};return n?.maxTokens&&(i.max_output_tokens=n?.maxTokens),n?.temperature!==void 0&&(i.temperature=n?.temperature),t.tools&&(i.tools=s(t.tools)),e.reasoning&&(n?.reasoningEffort||n?.reasoningSummary?(i.reasoning={effort:n?.reasoningEffort||`medium`,summary:n?.reasoningSummary||`auto`},i.include=[`reasoning.encrypted_content`]):i.reasoning={effort:`none`}),i}export{p as streamAzureOpenAIResponses,m as streamSimpleAzureOpenAIResponses};