/* esm.sh - @assistant-ui/core@0.3.31/dist/runtime/utils/tool-call-tree */
function*g(r,i){let t=[{type:"messages",values:r,index:0}],n,c=e=>{if(!n){n=new WeakSet;for(let s of t)s.type==="messages"&&n.add(s.values)}return n.has(e.values)?!1:(n.add(e.values),t.push(e),!0)};for(;t.length>0;){let e=t[t.length-1];if(e.index>=e.values.length){t.pop(),e.type==="messages"&&n?.delete(e.values);continue}if(e.type==="messages"){let l=e.values[e.index++];if(l?.role!=="assistant"||!Array.isArray(l.content))continue;t.push({type:"content",values:l.content,messageId:l.id,index:0});continue}let s=e.values[e.index++];!s||s.type!=="tool-call"||(yield{part:s,messageId:e.messageId},s.messages?.length&&i?.shouldDescend?.(s)!==!1&&c({type:"messages",values:s.messages,index:0}))}}function*h(r,i){for(let t of r)if(!(!t||t.type!=="tool-call")&&(yield t,t.messages?.length&&i?.shouldDescend?.(t)!==!1))for(let n of g(t.messages,i))yield n.part}function f(r,i){let t=[{type:"content",values:r,next:[],index:0,changed:!1}],n,c=e=>(n??=new WeakSet,n.has(e.values)?!1:(n.add(e.values),t.push(e),!0));for(;t.length>0;){let e=t[t.length-1];if(e.type==="content"){if(e.index>=e.values.length){t.pop();let p=e.changed?e.next:e.values;if(!e.parent)return{content:p,changed:e.changed};let{frame:u,message:d}=e.parent;e.changed?(u.next.push({...d,content:p}),u.changed=!0):u.next.push(d);continue}let a=e.values[e.index++];if(a.type!=="tool-call"){e.next.push(a);continue}let o=i(a);if(o.messages===void 0){e.next.push(o),o!==a&&(e.changed=!0);continue}c({type:"messages",values:o.messages,originalPart:a,mappedPart:o,parent:e,next:[],index:0,changed:!1})||(e.next.push(o),o!==a&&(e.changed=!0));continue}if(e.index>=e.values.length){t.pop(),n?.delete(e.values);let a=e.changed?{...e.mappedPart,messages:e.next}:e.mappedPart;e.parent.next.push(a),a!==e.originalPart&&(e.parent.changed=!0);continue}let s=e.values[e.index++];if(s.role!=="assistant"||!Array.isArray(s.content)){e.next.push(s);continue}let l=s;t.push({type:"content",values:l.content,parent:{frame:e,message:l},next:[],index:0,changed:!1})}throw new Error("Tool-call tree traversal ended without a root result")}export{h as iterateToolCallParts,f as mapToolCallPartsDeep,g as walkToolCallTree};
//# sourceMappingURL=tool-call-tree.mjs.map