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