import{n as e}from"./event-stream-BzGVPQTQ.js";import{n as t}from"./index-X6jQcISW.js";import{t as n}from"./sanitize-unicode-BhyPmlyt.js";import{t as r}from"./transform-messages-k0lyAmgl.js";import{t as i}from"./hash-kZ2KD_no.js";function a(e,t){let n={v:1,id:e};return t&&(n.phase=t),JSON.stringify(n)}function o(e){if(e){if(e.startsWith(`{`))try{let t=JSON.parse(e);if(t.v===1&&typeof t.id==`string`)return t.phase===`commentary`||t.phase===`final_answer`?{id:t.id,phase:t.phase}:{id:t.id}}catch{}return{id:e}}}function s(e,t,a,s){let c=[],l=e=>{let t=e.replace(/[^a-zA-Z0-9_-]/g,`_`);return(t.length>64?t.slice(0,64):t).replace(/_+$/,``)},u=e=>{let t=`fc_${i(e)}`;return t.length>64?t.slice(0,64):t},d=r(t.messages,e,(t,n,r)=>{if(!a.has(e.provider)||!t.includes(`|`))return l(t);let[i,o]=t.split(`|`),s=l(i),c=r.provider!==e.provider||r.api!==e.api?u(o):l(o);return c.startsWith(`fc_`)||(c=l(`fc_${c}`)),`${s}|${c}`});if((s?.includeSystemPrompt??!0)&&t.systemPrompt){let r=e.compat,i=e.reasoning&&r?.supportsDeveloperRole!==!1?`developer`:`system`;c.push({role:i,content:n(t.systemPrompt)})}let f=0;for(let t of d){if(t.role===`user`)if(typeof t.content==`string`)c.push({role:`user`,content:[{type:`input_text`,text:n(t.content)}]});else{let e=t.content.map(e=>e.type===`text`?{type:`input_text`,text:n(e.text)}:{type:`input_image`,detail:`auto`,image_url:`data:${e.mimeType};base64,${e.data}`});if(e.length===0)continue;c.push({role:`user`,content:e})}else if(t.role===`assistant`){let r=[],a=t,s=a.model!==e.id&&a.provider===e.provider&&a.api===e.api,l=0;for(let e of t.content)if(e.type===`thinking`){if(e.thinkingSignature){let t=JSON.parse(e.thinkingSignature);r.push(t)}}else if(e.type===`text`){let t=e,a=o(t.textSignature),s=l===0?`msg_pi_${f}`:`msg_pi_${f}_${l}`;l++;let c=a?.id;c?c.length>64&&(c=`msg_${i(c)}`):c=s,r.push({type:`message`,role:`assistant`,content:[{type:`output_text`,text:n(t.text),annotations:[]}],status:`completed`,id:c,phase:a?.phase})}else if(e.type===`toolCall`){let t=e,[n,i]=t.id.split(`|`),a=i;s&&a?.startsWith(`fc_`)&&(a=void 0),r.push({type:`function_call`,id:a,call_id:n,name:t.name,arguments:JSON.stringify(t.arguments)})}if(r.length===0)continue;c.push(...r)}else if(t.role===`toolResult`){let r=t.content.filter(e=>e.type===`text`).map(e=>e.text).join(`
`),i=t.content.some(e=>e.type===`image`),a=r.length>0,[o]=t.toolCallId.split(`|`),s;if(i&&e.input.includes(`image`)){let e=[];a&&e.push({type:`input_text`,text:n(r)});for(let n of t.content)n.type===`image`&&e.push({type:`input_image`,detail:`auto`,image_url:`data:${n.mimeType};base64,${n.data}`});s=e}else s=n(a?r:`(see attached image)`);c.push({type:`function_call_output`,call_id:o,output:s})}f++}return c}function c(e,t){let n=t?.strict===void 0?!1:t.strict;return e.map(e=>({type:`function`,name:e.name,description:e.description,parameters:e.parameters,strict:n}))}async function l(n,r,i,o,s){let c=null,l=null,d=r.content,f=()=>d.length-1;for await(let d of n)if(d.type===`response.created`)r.responseId=d.response.id;else if(d.type===`response.output_item.added`){let e=d.item;e.type===`reasoning`?(c=e,l={type:`thinking`,thinking:``},r.content.push(l),i.push({type:`thinking_start`,contentIndex:f(),partial:r})):e.type===`message`?(c=e,l={type:`text`,text:``},r.content.push(l),i.push({type:`text_start`,contentIndex:f(),partial:r})):e.type===`function_call`&&(c=e,l={type:`toolCall`,id:`${e.call_id}|${e.id}`,name:e.name,arguments:{},partialJson:e.arguments||``},r.content.push(l),i.push({type:`toolcall_start`,contentIndex:f(),partial:r}))}else if(d.type===`response.reasoning_summary_part.added`)c&&c.type===`reasoning`&&(c.summary=c.summary||[],c.summary.push(d.part));else if(d.type===`response.reasoning_summary_text.delta`){if(c?.type===`reasoning`&&l?.type===`thinking`){c.summary=c.summary||[];let e=c.summary[c.summary.length-1];e&&(l.thinking+=d.delta,e.text+=d.delta,i.push({type:`thinking_delta`,contentIndex:f(),delta:d.delta,partial:r}))}}else if(d.type===`response.reasoning_summary_part.done`){if(c?.type===`reasoning`&&l?.type===`thinking`){c.summary=c.summary||[];let e=c.summary[c.summary.length-1];e&&(l.thinking+=`

`,e.text+=`

`,i.push({type:`thinking_delta`,contentIndex:f(),delta:`

`,partial:r}))}}else if(d.type===`response.reasoning_text.delta`)c?.type===`reasoning`&&l?.type===`thinking`&&(l.thinking+=d.delta,i.push({type:`thinking_delta`,contentIndex:f(),delta:d.delta,partial:r}));else if(d.type===`response.content_part.added`)c?.type===`message`&&(c.content=c.content||[],(d.part.type===`output_text`||d.part.type===`refusal`)&&c.content.push(d.part));else if(d.type===`response.output_text.delta`){if(c?.type===`message`&&l?.type===`text`){if(!c.content||c.content.length===0)continue;let e=c.content[c.content.length-1];e?.type===`output_text`&&(l.text+=d.delta,e.text+=d.delta,i.push({type:`text_delta`,contentIndex:f(),delta:d.delta,partial:r}))}}else if(d.type===`response.refusal.delta`){if(c?.type===`message`&&l?.type===`text`){if(!c.content||c.content.length===0)continue;let e=c.content[c.content.length-1];e?.type===`refusal`&&(l.text+=d.delta,e.refusal+=d.delta,i.push({type:`text_delta`,contentIndex:f(),delta:d.delta,partial:r}))}}else if(d.type===`response.function_call_arguments.delta`)c?.type===`function_call`&&l?.type===`toolCall`&&(l.partialJson+=d.delta,l.arguments=t(l.partialJson),i.push({type:`toolcall_delta`,contentIndex:f(),delta:d.delta,partial:r}));else if(d.type===`response.function_call_arguments.done`){if(c?.type===`function_call`&&l?.type===`toolCall`){let e=l.partialJson;if(l.partialJson=d.arguments,l.arguments=t(l.partialJson),d.arguments.startsWith(e)){let t=d.arguments.slice(e.length);t.length>0&&i.push({type:`toolcall_delta`,contentIndex:f(),delta:t,partial:r})}}}else if(d.type===`response.output_item.done`){let e=d.item;if(e.type===`reasoning`&&l?.type===`thinking`){let t=e.summary?.map(e=>e.text).join(`

`)||``,n=e.content?.map(e=>e.text).join(`

`)||``;l.thinking=t||n||l.thinking,l.thinkingSignature=JSON.stringify(e),i.push({type:`thinking_end`,contentIndex:f(),content:l.thinking,partial:r}),l=null}else if(e.type===`message`&&l?.type===`text`)l.text=e.content.map(e=>e.type===`output_text`?e.text:e.refusal).join(``),l.textSignature=a(e.id,e.phase??void 0),i.push({type:`text_end`,contentIndex:f(),content:l.text,partial:r}),l=null;else if(e.type===`function_call`){let n=l?.type===`toolCall`&&l.partialJson?t(l.partialJson):t(e.arguments||`{}`),a;l?.type===`toolCall`?(l.arguments=n,delete l.partialJson,a=l):a={type:`toolCall`,id:`${e.call_id}|${e.id}`,name:e.name,arguments:n},l=null,i.push({type:`toolcall_end`,contentIndex:f(),toolCall:a,partial:r})}}else if(d.type===`response.completed`){let t=d.response;if(t?.id&&(r.responseId=t.id),t?.usage){let e=t.usage.input_tokens_details?.cached_tokens||0;r.usage={input:(t.usage.input_tokens||0)-e,output:t.usage.output_tokens||0,cacheRead:e,cacheWrite:0,totalTokens:t.usage.total_tokens||0,cost:{input:0,output:0,cacheRead:0,cacheWrite:0,total:0}}}if(e(o,r.usage),s?.applyServiceTierPricing){let e=s.resolveServiceTier?s.resolveServiceTier(t?.service_tier,s.serviceTier):t?.service_tier??s.serviceTier;s.applyServiceTierPricing(r.usage,e)}r.stopReason=u(t?.status),r.content.some(e=>e.type===`toolCall`)&&r.stopReason===`stop`&&(r.stopReason=`toolUse`)}else if(d.type===`error`)throw Error(`Error Code ${d.code}: ${d.message}`||`Unknown error`);else if(d.type===`response.failed`){let e=d.response?.error,t=d.response?.incomplete_details,n=e?`${e.code||`unknown`}: ${e.message||`no message`}`:t?.reason?`incomplete: ${t.reason}`:`Unknown error (no error details in response)`;throw Error(n)}}function u(e){if(!e)return`stop`;switch(e){case`completed`:return`stop`;case`incomplete`:return`length`;case`failed`:case`cancelled`:return`error`;case`in_progress`:case`queued`:return`stop`;default:throw Error(`Unhandled stop reason: ${e}`)}}export{c as n,l as r,s as t};
//# sourceMappingURL=openai-responses-shared-BLKU_5Qn.js.map