import{a4 as $}from"./edit-B9XsMHfU.js";import{s as S}from"./hash-DMDecQcg.js";import{a as k}from"./json-parse-c_QnhFH-.js";import{s as x}from"./sanitize-unicode-cmozpu7l.js";import{t as T}from"./transform-messages-DSNYjDRn.js";function C(r,t){const l={v:1,id:r};return t&&(l.phase=t),JSON.stringify(l)}function J(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 W(r,t,l,h){const u=[],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_${S(s)}`;return o.length>64?o.slice(0,64):o},I=(s,o,c)=>{if(!l.has(r.provider)||!s.includes("|"))return i(s);const[_,y]=s.split("|"),f=i(_);let m=c.provider!==r.provider||c.api!==r.api?n(y):i(y);return m.startsWith("fc_")||(m=i(`fc_${m}`)),`${f}|${m}`},d=T(t.messages,r,I);if((h?.includeSystemPrompt??!0)&&t.systemPrompt){const s=r.reasoning?"developer":"system";u.push({role:s,content:x(t.systemPrompt)})}let e=0;for(const s of d){if(s.role==="user")if(typeof s.content=="string")u.push({role:"user",content:[{type:"input_text",text:x(s.content)}]});else{const o=s.content.map(c=>c.type==="text"?{type:"input_text",text:x(c.text)}:{type:"input_image",detail:"auto",image_url:`data:${c.mimeType};base64,${c.data}`});if(o.length===0)continue;u.push({role:"user",content:o})}else if(s.role==="assistant"){const o=[],c=s,_=c.model!==r.id&&c.provider===r.provider&&c.api===r.api;let y=0;for(const f of s.content)if(f.type==="thinking"){if(f.thinkingSignature){const p=JSON.parse(f.thinkingSignature);o.push(p)}}else if(f.type==="text"){const p=f,m=J(p.textSignature),v=y===0?`msg_pi_${e}`:`msg_pi_${e}_${y}`;y++;let g=m?.id;g?g.length>64&&(g=`msg_${S(g)}`):g=v,o.push({type:"message",role:"assistant",content:[{type:"output_text",text:x(p.text),annotations:[]}],status:"completed",id:g,phase:m?.phase})}else if(f.type==="toolCall"){const p=f,[m,v]=p.id.split("|");let g=v;_&&g?.startsWith("fc_")&&(g=void 0),o.push({type:"function_call",id:g,call_id:m,name:p.name,arguments:JSON.stringify(p.arguments)})}if(o.length===0)continue;u.push(...o)}else if(s.role==="toolResult"){const o=s.content.filter(p=>p.type==="text").map(p=>p.text).join(`
`),c=s.content.some(p=>p.type==="image"),_=o.length>0,[y]=s.toolCallId.split("|");let f;if(c&&r.input.includes("image")){const p=[];_&&p.push({type:"input_text",text:x(o)});for(const m of s.content)m.type==="image"&&p.push({type:"input_image",detail:"auto",image_url:`data:${m.mimeType};base64,${m.data}`});f=p}else f=x(_?o:"(see attached image)");u.push({type:"function_call_output",call_id:y,output:f})}e++}return u}function N(r,t){const l=t?.strict===void 0?!1:t.strict;return r.map(h=>({type:"function",name:h.name,description:h.description,parameters:h.parameters,strict:l}))}async function O(r,t,l,h,u){let i=null,n=null;const I=t.content,d=()=>I.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=k(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=k(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(c=>c.text).join(`

`)||"",o=e.content?.map(c=>c.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=C(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?k(n.partialJson):k(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($(h,t.usage),u?.applyServiceTierPricing){const s=u.resolveServiceTier?u.resolveServiceTier(e?.service_tier,u.serviceTier):e?.service_tier??u.serviceTier;u.applyServiceTierPricing(t.usage,s)}t.stopReason=P(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 P(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{N as a,W as c,O as p};
