import{r as e,t}from"./event-stream-BzGVPQTQ.js";import{t as n}from"./headers-5EYI0_pl.js";import{r}from"./transform-messages-k0lyAmgl.js";import{t as i}from"./openai-Bf1npfRy.js";import{t as a}from"./openai-prompt-cache-CErE62Yt.js";import{n as o,r as s,t as c}from"./openai-responses-shared-BLKU_5Qn.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}function p(e){if(e instanceof Error){let t=e.status,n=typeof t==`number`?t:void 0;return n===void 0?e.message:`Azure OpenAI API error (${n}): ${e.message}`}try{return JSON.stringify(e)}catch{return String(e)}}var m=(e,r,i)=>{let a=new t;return(async()=>{let t=f(e,i),o={role:`assistant`,content:[],api:`azure-openai-responses`,provider:e.provider,model:e.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 c=i?.apiKey;if(!c)throw Error(`No API key for provider: ${e.provider}`);let l=y(e,c,i),u=b(e,r,i,t),d=await i?.onPayload?.(u,e);d!==void 0&&(u=d);let f={...i?.signal?{signal:i.signal}:{},...i?.timeoutMs===void 0?{}:{timeout:i.timeoutMs},maxRetries:i?.maxRetries??0},{data:p,response:m}=await l.responses.create(u,f).withResponse();if(await i?.onResponse?.({status:m.status,headers:n(m.headers)},e),a.push({type:`start`,partial:o}),await s(p,o,a,e),i?.signal?.aborted)throw Error(`Request was aborted`);if(o.stopReason===`aborted`||o.stopReason===`error`)throw Error(`An unknown error occurred`);a.push({type:`done`,reason:o.stopReason,message:o}),a.end()}catch(e){for(let e of o.content)delete e.index,delete e.partialJson;o.stopReason=i?.signal?.aborted?`aborted`:`error`,o.errorMessage=p(e),a.push({type:`error`,reason:o.stopReason,error:o}),a.end()}})(),a},h=(t,n,i)=>{let a=i?.apiKey;if(!a)throw Error(`No API key for provider: ${t.provider}`);let o=r(t,i,a),s=i?.reasoning?e(t,i.reasoning):void 0,c=s===`off`?void 0:s;return m(t,n,{...o,reasoningEffort:c})};function g(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 _(e){return`https://${e}.openai.azure.com/openai/v1`}function v(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=_(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:g(a),apiVersion:n}}function y(e,t,n){let r={...e.headers};n?.headers&&Object.assign(r,n.headers);let{baseUrl:a,apiVersion:o}=v(e,n);return new i({apiKey:t,apiVersion:o,dangerouslyAllowBrowser:!0,defaultHeaders:r,baseURL:a})}function b(e,t,n,r){let i={model:r,input:c(e,t,u),stream:!0,prompt_cache_key:a(n?.sessionId),store:!1};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{m as streamAzureOpenAIResponses,h as streamSimpleAzureOpenAIResponses};
//# sourceMappingURL=azure-openai-responses-DHO2j-X_.js.map