import{c as S}from"./index-3wjvth7D.js";import{s as v}from"./hash-Cxk8sEP2.js";import{a as x}from"./json-parse-DUjbyQwB.js";import{s as _}from"./sanitize-unicode-B62XMGgN.js";import{t as $}from"./transform-messages-B_np3yTy.js";function T(r,t){const l={v:1,id:r};return t&&(l.phase=t),JSON.stringify(l)}function C(r){if(r){if(r.startsWith("{"))try{const t=JSON.parse(r);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:r}}}function M(r,t,l,y){const f=[],i=s=>{const o=s.replace(/[^a-zA-Z0-9_-]/g,"_");return(o.length>64?o.slice(0,64):o).replace(/_+$/,"")},n=s=>{const o=`fc_${v(s)}`;return o.length>64?o.slice(0,64):o},k=(s,o,p)=>{if(!l.has(r.provider)||!s.includes("|"))return i(s);const[h,g]=s.split("|"),m=i(h);let c=p.provider!==r.provider||p.api!==r.api?n(g):i(g);return c.startsWith("fc_")||(c=i(`fc_${c}`)),`${m}|${c}`},d=$(t.messages,r,k);if((y?.includeSystemPrompt??!0)&&t.systemPrompt){const s=r.reasoning?"developer":"system";f.push({role:s,content:_(t.systemPrompt)})}let e=0;for(const s of d){if(s.role==="user")if(typeof s.content=="string")f.push({role:"user",content:[{type:"input_text",text:_(s.content)}]});else{const o=s.content.map(p=>p.type==="text"?{type:"input_text",text:_(p.text)}:{type:"input_image",detail:"auto",image_url:`data:${p.mimeType};base64,${p.data}`});if(o.length===0)continue;f.push({role:"user",content:o})}else if(s.role==="assistant"){const o=[],p=s,h=p.model!==r.id&&p.provider===r.provider&&p.api===r.api;for(const g of s.content)if(g.type==="thinking"){if(g.thinkingSignature){const m=JSON.parse(g.thinkingSignature);o.push(m)}}else if(g.type==="text"){const m=g,u=C(m.textSignature);let c=u?.id;c?c.length>64&&(c=`msg_${v(c)}`):c=`msg_${e}`,o.push({type:"message",role:"assistant",content:[{type:"output_text",text:_(m.text),annotations:[]}],status:"completed",id:c,phase:u?.phase})}else if(g.type==="toolCall"){const m=g,[u,c]=m.id.split("|");let I=c;h&&I?.startsWith("fc_")&&(I=void 0),o.push({type:"function_call",id:I,call_id:u,name:m.name,arguments:JSON.stringify(m.arguments)})}if(o.length===0)continue;f.push(...o)}else if(s.role==="toolResult"){const o=s.content.filter(u=>u.type==="text").map(u=>u.text).join(`
`),p=s.content.some(u=>u.type==="image"),h=o.length>0,[g]=s.toolCallId.split("|");let m;if(p&&r.input.includes("image")){const u=[];h&&u.push({type:"input_text",text:_(o)});for(const c of s.content)c.type==="image"&&u.push({type:"input_image",detail:"auto",image_url:`data:${c.mimeType};base64,${c.data}`});m=u}else m=_(h?o:"(see attached image)");f.push({type:"function_call_output",call_id:g,output:m})}e++}return f}function W(r,t){const l=t?.strict===void 0?!1:t.strict;return r.map(y=>({type:"function",name:y.name,description:y.description,parameters:y.parameters,strict:l}))}async function N(r,t,l,y,f){let i=null,n=null;const k=t.content,d=()=>k.length-1;for await(const a of r)if(a.type==="response.created")t.responseId=a.response.id;else if(a.type==="response.output_item.added"){const e=a.item;e.type==="reasoning"?(i=e,n={type:"thinking",thinking:""},t.content.push(n),l.push({type:"thinking_start",contentIndex:d(),partial:t})):e.type==="message"?(i=e,n={type:"text",text:""},t.content.push(n),l.push({type:"text_start",contentIndex:d(),partial:t})):e.type==="function_call"&&(i=e,n={type:"toolCall",id:`${e.call_id}|${e.id}`,name:e.name,arguments:{},partialJson:e.arguments||""},t.content.push(n),l.push({type:"toolcall_start",contentIndex:d(),partial:t}))}else if(a.type==="response.reasoning_summary_part.added")i&&i.type==="reasoning"&&(i.summary=i.summary||[],i.summary.push(a.part));else if(a.type==="response.reasoning_summary_text.delta"){if(i?.type==="reasoning"&&n?.type==="thinking"){i.summary=i.summary||[];const e=i.summary[i.summary.length-1];e&&(n.thinking+=a.delta,e.text+=a.delta,l.push({type:"thinking_delta",contentIndex:d(),delta:a.delta,partial:t}))}}else if(a.type==="response.reasoning_summary_part.done"){if(i?.type==="reasoning"&&n?.type==="thinking"){i.summary=i.summary||[];const e=i.summary[i.summary.length-1];e&&(n.thinking+=`

`,e.text+=`

`,l.push({type:"thinking_delta",contentIndex:d(),delta:`

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

`)||"",o=e.content?.map(p=>p.text).join(`

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