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{t as a}from"./openai-Cn7eGqwa.js";import{n as o,r as s,t as c}from"./openai-responses-shared-DF3ZGaUx.js";var l=`v1`,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({}.AZURE_OPENAI_DEPLOYMENT_NAME_MAP).get(e.id)||e.id}var p=(n,i,a)=>{let o=new e;return(async()=>{let e=f(n,a),c={role:`assistant`,content:[],api:`azure-openai-responses`,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 l=v(n,a?.apiKey||t(n.provider)||``,a),u=y(n,i,a,e),d=await a?.onPayload?.(u,n);d!==void 0&&(u=d);let f={...a?.signal?{signal:a.signal}:{},...a?.timeoutMs===void 0?{}:{timeout:a.timeoutMs},...a?.maxRetries===void 0?{}:{maxRetries:a.maxRetries}},{data:p,response:m}=await l.responses.create(u,f).withResponse();if(await a?.onResponse?.({status:m.status,headers:r(m.headers)},n),o.push({type:`start`,partial:c}),await s(p,c,o,n),a?.signal?.aborted)throw Error(`Request was aborted`);if(c.stopReason===`aborted`||c.stopReason===`error`)throw Error(`An unknown error occurred`);o.push({type:`done`,reason:c.stopReason,message:c}),o.end()}catch(e){for(let e of c.content)delete e.index,delete e.partialJson;c.stopReason=a?.signal?.aborted?`aborted`:`error`,c.errorMessage=e instanceof Error?e.message:JSON.stringify(e),o.push({type:`error`,reason:c.stopReason,error:c}),o.end()}})(),o},m=(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 p(e,r,{...s,reasoningEffort:l})};function h(e){let t=e.trim().replace(/\/+$/,``),n;try{n=new URL(t)}catch{throw Error(`Invalid Azure OpenAI base URL: ${e}`)}let r=n.hostname.endsWith(`.openai.azure.com`)||n.hostname.endsWith(`.cognitiveservices.azure.com`),i=n.pathname.replace(/\/+$/,``);return r&&(i===``||i===`/`||i===`/openai`)&&(n.pathname=`/openai/v1`,n.search=``),n.toString().replace(/\/+$/,``)}function g(e){return`https://${e}.openai.azure.com/openai/v1`}function _(e,t){let n=t?.azureApiVersion||{}.AZURE_OPENAI_API_VERSION||l,r=t?.azureBaseUrl?.trim()||{}.AZURE_OPENAI_BASE_URL?.trim()||void 0,i=t?.azureResourceName||{}.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(!{}.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={}.AZURE_OPENAI_API_KEY}let r={...e.headers};n?.headers&&Object.assign(r,n.headers);let{baseUrl:i,apiVersion:o}=_(e,n);return new a({apiKey:t,apiVersion:o,dangerouslyAllowBrowser:!0,defaultHeaders:r,baseURL:i})}function y(e,t,n,r){let i={model:r,input:c(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&&t.tools.length>0&&(i.tools=o(t.tools)),e.reasoning&&(n?.reasoningEffort||n?.reasoningSummary?(i.reasoning={effort:n?.reasoningEffort?e.thinkingLevelMap?.[n.reasoningEffort]??n.reasoningEffort:`medium`,summary:n?.reasoningSummary||`auto`},i.include=[`reasoning.encrypted_content`]):e.thinkingLevelMap?.off!==null&&(i.reasoning={effort:e.thinkingLevelMap?.off??`none`})),i}export{p as streamAzureOpenAIResponses,m as streamSimpleAzureOpenAIResponses};