/* esm.sh - @assistant-ui/core@0.3.31/dist/model-context/types */
import{nullProtoRecord as f}from"../utils/record.mjs";var S=`

`,b=e=>e>=55296&&e<=56319,x=(e,r)=>{let s="";for(let{system:i}of e){let h=s?S:"",m=r-s.length-h.length,l=m<i.length&&b(i.charCodeAt(m-1))?m-1:m;if(l<=0||(s+=h+i.slice(0,l),i.length>l))break}return s},v=(e,r)=>{if(r.mergeSystem){if(r.maxSystemLength!==void 0)throw new Error("Pass either maxSystemLength or mergeSystem, not both. Apply the cap inside mergeSystem instead.");return r.mergeSystem(e)}return r.maxSystemLength!==void 0?x(e,r.maxSystemLength):e.map(s=>s.system).join(S)},d=e=>{if(!e.overwrite)return e;let{overwrite:r,...s}=e;return s},P=(e,r={})=>{let s=Array.from(e).map(t=>t.getModelContext()).sort((t,o)=>(o.priority??0)-(t.priority??0)),i=f(),h=[],m=s.reduce((t,o)=>{let a=o.priority??0;if(o.system&&h.push({system:o.system,priority:a}),o.tools)for(let[n,y]of Object.entries(o.tools)){let u=t.tools!==void 0&&Object.hasOwn(t.tools,n)?t.tools[n]:void 0;if(u&&u!==y){let g=i[n];if(g===a){if(!y.overwrite)throw new Error(`You tried to define a tool with the name ${n}, but it already exists.`);t.tools[n]=d(y);continue}let p=g>a?u:y,w=g>a?y:u;t.tools[n]=d({...w,...p}),i[n]=Math.max(g,a);continue}t.tools||(t.tools=f()),t.tools[n]=d(y),Object.hasOwn(i,n)||(i[n]=a)}return o.config&&(t.config={...o.config,...t.config}),o.callSettings&&(t.callSettings={...o.callSettings,...t.callSettings}),o.unstable_composerMetadata&&(t.unstable_composerMetadata={...o.unstable_composerMetadata,...t.unstable_composerMetadata}),t},{}),l=v(h,r);return l&&(m.system=l),m};export{P as mergeModelContexts};
//# sourceMappingURL=types.mjs.map