function T(o,t,n){return{temperature:t?.temperature,maxTokens:t?.maxTokens??(o.maxTokens>0?Math.min(o.maxTokens,32e3):void 0),signal:t?.signal,apiKey:n||t?.apiKey,transport:t?.transport,cacheRetention:t?.cacheRetention,sessionId:t?.sessionId,headers:t?.headers,onPayload:t?.onPayload,onResponse:t?.onResponse,timeoutMs:t?.timeoutMs,maxRetries:t?.maxRetries,maxRetryDelayMs:t?.maxRetryDelayMs,metadata:t?.metadata}}function R(o){return o==="xhigh"?"high":o}function S(o,t,n,l){const m={...{minimal:1024,low:2048,medium:8192,high:16384},...l},u=1024,f=R(n);let s=m[f];const d=Math.min(o+s,t);return d<=s&&(s=Math.max(0,d-u)),{maxTokens:d,thinkingBudget:s}}const M="(image omitted: model does not support images)",I="(tool image omitted: model does not support images)";function y(o,t){const n=[];let l=!1;for(const h of o){if(h.type==="image"){l||n.push({type:"text",text:t}),l=!0;continue}n.push(h),l=h.text===t}return n}function C(o,t){return t.input.includes("image")?o:o.map(n=>n.role==="user"&&Array.isArray(n.content)?{...n,content:y(n.content,M)}:n.role==="toolResult"?{...n,content:y(n.content,I)}:n)}function O(o,t,n){const l=new Map,m=C(o,t).map(r=>{if(r.role==="user")return r;if(r.role==="toolResult"){const e=l.get(r.toolCallId);return e&&e!==r.toolCallId?{...r,toolCallId:e}:r}if(r.role==="assistant"){const e=r,i=e.provider===t.provider&&e.api===t.api&&e.model===t.id,g=e.content.flatMap(a=>{if(a.type==="thinking")return a.redacted?i?a:[]:i&&a.thinkingSignature?a:!a.thinking||a.thinking.trim()===""?[]:i?a:{type:"text",text:a.thinking};if(a.type==="text")return i?a:{type:"text",text:a.text};if(a.type==="toolCall"){const c=a;let p=c;if(!i&&c.thoughtSignature&&(p={...c},delete p.thoughtSignature),!i&&n){const x=n(c.id,t,e);x!==c.id&&(l.set(c.id,x),p={...p,id:x})}return p}return a});return{...e,content:g}}return r}),u=[];let f=[],s=new Set;const d=()=>{if(f.length>0){for(const r of f)s.has(r.id)||u.push({role:"toolResult",toolCallId:r.id,toolName:r.name,content:[{type:"text",text:"No result provided"}],isError:!0,timestamp:Date.now()});f=[],s=new Set}};for(let r=0;r<m.length;r++){const e=m[r];if(e.role==="assistant"){d();const i=e;if(i.stopReason==="error"||i.stopReason==="aborted")continue;const g=i.content.filter(a=>a.type==="toolCall");g.length>0&&(f=g,s=new Set),u.push(e)}else e.role==="toolResult"?(s.add(e.toolCallId),u.push(e)):(e.role==="user"&&d(),u.push(e))}return d(),u}export{S as a,T as b,O as t};
