"use strict";const u=require("react/jsx-runtime"),oe=require("../providers/Stream.cjs.js"),ae=require("../providers/CustomComponentProvider.cjs.js"),ie=require("../utils/logger.cjs.js"),y=require("../utils/utils.cjs.js"),o=require("react"),ce=require("./messages/AgentMessage.cjs.js"),ue=require("./messages/CustomComponentRender.cjs.js"),le=require("./messages/HumanMessage.cjs.js"),$=require("./Thinking.cjs.js");function de({setIsFirstMessage:j,enableToolCallIndicator:w=!0,chatBodyProps:A}){const i=oe.useStreamContext(),{interruptComponents:U}=ae.useCustomComponents(),T=i.messages,d=i.isLoading,[z,W]=o.useState({});ie.logger.debug("ChatBody render - messages count:",T,"isLoading:",d);const l=o.useMemo(()=>T??[],[T]),L=o.useMemo(()=>{const e=new Set,n=i.values?.ui;if(!Array.isArray(n))return e;for(const r of n){const s=r,c=s.metadata&&typeof s.metadata=="object"?s.metadata:void 0,h=[typeof s.id=="string"?s.id:void 0,typeof c?.id=="string"?c.id:void 0,typeof c?.message_id=="string"?c.message_id:void 0];for(const f of h)f&&e.add(f)}return e},[i.values?.ui]),v=o.useRef(null),H=o.useRef(0),b=o.useRef(!0),R=o.useRef(new Map),q=o.useCallback(e=>Array.isArray(e)?e.map((n,r)=>{if(!n||typeof n!="object")return null;const s=n,c=s.status;return typeof s.content!="string"||c!=="pending"&&c!=="in_progress"&&c!=="completed"?null:{id:s.id&&typeof s.id=="string"?s.id:`todo-${r}`,content:s.content,status:c,updatedAt:s.updatedAt instanceof Date||typeof s.updatedAt=="string"||typeof s.updatedAt=="number"?s.updatedAt:void 0,run_id:typeof s.run_id=="string"?s.run_id:void 0,runId:typeof s.runId=="string"?s.runId:void 0,messageId:typeof s.messageId=="string"?s.messageId:void 0,checkpoint:typeof s.checkpoint=="string"?s.checkpoint:void 0}}).filter(n=>n!==null):[],[]),I=o.useCallback((e,n)=>{W(r=>({...r,[e]:n}))},[]),F=o.useCallback(async e=>{if(!e){console.error("No parent checkpoint available for regeneration");return}await i.submit(void 0,{checkpoint:e,streamMode:["values"],streamSubgraphs:!0,streamResumable:!0})},[i]),B=o.useCallback(e=>{i.setBranch(e)},[i]),x=o.useCallback(e=>{i.submit(null,{command:{resume:e},streamMode:["values","messages-tuple","updates","custom"],streamSubgraphs:!0})},[i]),K=o.useMemo(()=>{const e=i.interrupt;if(!e)return null;const r=e.value?.actionRequests??[];return{approve:()=>{x({decisions:r.map(()=>({type:"approve"}))})},reject:s=>{x({decisions:r.map(()=>({type:"reject",message:s||void 0}))})},edit:s=>{const c=r[0];x({decisions:[{type:"edit",editedAction:{name:c?.name??"",args:{...c?.args,...s}}}]})}}},[i.interrupt,x]),p=o.useCallback(()=>{if(v.current){let e=v.current.parentElement;for(;e;){const n=window.getComputedStyle(e);if(n.overflowY==="auto"||n.overflowY==="scroll")return e;e=e.parentElement}}return null},[]);o.useEffect(()=>{l.length>0&&j&&j(!1)},[l.length,j]);const O=o.useCallback((e,n,r)=>{const s=d&&n===r.length-1&&e.type==="ai";if(!e.content.length&&!y.isAiWithToolCalls(e)&&!s||e.additional_kwargs?.hidden)return null;const c=e.id??`msg-${n}`,h=t=>typeof t.id=="string"&&L.has(t.id);if(e.type==="human")return u.jsx(le,{message:e,fontSize:A?.fontSize},c);if(y.isToolMessage(e))return null;if(e.type==="ai"&&n>0){let t=n-1;for(;t>=0&&y.isToolMessage(r[t]);)t--;if(t>=0&&r[t].type==="ai"&&!h(e))return null}const f=[e];let M=n+1;for(;M<r.length;){const t=r[M];if(y.isToolMessage(t))f.push(t),M++;else if(t.type==="ai"){if(h(t))break;f.push(t),M++}else break}const g=f.filter(t=>t.type==="ai"),_=g.some(t=>Array.isArray(t.content)),m=new Set,k=_?g.flatMap(t=>Array.isArray(t.content)?t.content.filter(a=>{if(a.type==="text"&&typeof a.text=="string"){if(m.has(a.text))return!1;m.add(a.text)}return!0}):typeof t.content=="string"&&t.content.length>0?m.has(t.content)?[]:(m.add(t.content),[{type:"text",text:t.content}]):[]):(()=>{const t=[];for(const a of g){const C=typeof a.content=="string"?a.content:"";C&&!m.has(C)&&(m.add(C),t.push(C))}return t.join(`

`)})(),V=_?k.map(t=>t.type==="text"?t.text:"").filter(Boolean).join(" "):k,J={...e,content:k},D=e?i.getMessagesMetadata(e):void 0,Q=_?k.some(t=>t.type==="thinking"&&t.thinking?.length>0||t.type==="reasoning"&&t.reasoning?.length>0):!1,X=g.some(t=>{const a=t.additional_kwargs;return typeof a?.reasoning_content=="string"&&a.reasoning_content.length>0}),Z=g.some(t=>{const a=t.additional_kwargs;return Array.isArray(a?.tool_status)&&a.tool_status.length>0}),P=f.some(t=>y.isToolMessage(t)),ee=w&&(g.some(t=>y.isAiWithToolCalls(t))||Z||P),G=!!V||Q||X||ee,te=M>=r.length,S=d&&te,E=g.map(t=>t.id).filter(t=>typeof t=="string"&&t.length>0).join("|")||c,ne=(()=>{const t=R.current.get(E);if(!S&&t&&t.length>0)return t;if(S){const a=q(i.values?.todos);if(a.length>0)return R.current.set(E,a),a}for(let a=g.length-1;a>=0;a--){const re=i.getMessagesMetadata(g[a])?.firstSeenState?.values,N=q(re?.todos);if(N.length>0)return(!t||t.length===0)&&R.current.set(E,N),t??N}return t&&t.length>0?t:[]})(),se=S&&!G;return u.jsxs(o.Fragment,{children:[se&&u.jsx($,{}),G&&u.jsx(ce,{agentName:A?.agentName,fontSize:A?.fontSize,message:J,groupedMessages:f,showToolActivity:w,isStreaming:S,onRegenerate:F,feedback:e.id?z[e.id]:void 0,onFeedback:I,branch:D?.branch,branchOptions:D?.branchOptions,onBranchSelect:B,todos:ne}),g.map(t=>u.jsx(ue,{message:t,thread:i},t.id))]},c)},[d,i,q,w,F,z,I,B,L]),Y=o.useMemo(()=>l.map((e,n)=>{const r=O(e,n,l);return r&&!r.key?o.cloneElement(r,{key:e.id??`msg-${n}`}):r}),[l,O]);return o.useEffect(()=>{const e=p();if(!e)return;const n=()=>{const{scrollTop:r,scrollHeight:s,clientHeight:c}=e,h=s-r-c;b.current=h<100};return e.addEventListener("scroll",n),()=>e.removeEventListener("scroll",n)},[p]),o.useEffect(()=>{const e=p();if(!e)return;const n=l.length;n>H.current&&(e.scrollTop=e.scrollHeight,b.current=!0),H.current=n},[l,p]),o.useEffect(()=>{const e=p();if(!e||!d)return;const n=new MutationObserver(()=>{b.current&&e&&(e.scrollTop=e.scrollHeight)});return v.current&&n.observe(v.current,{childList:!0,subtree:!0,characterData:!0}),()=>n.disconnect()},[d,p]),o.useLayoutEffect(()=>{const e=p();d&&b.current&&e&&(e.scrollTop=e.scrollHeight)}),u.jsx("div",{ref:v,className:"flex flex-col gap-4 rounded-4xl p-2",children:l.length===0?u.jsx("div",{className:"flex items-center justify-center h-full text-zinc-500",children:d?u.jsxs("div",{className:"flex items-center gap-2",children:[u.jsx("div",{className:"animate-spin h-5 w-5 border-2 border-zinc-500 border-t-transparent rounded-full"}),u.jsx("span",{children:"Loading conversation..."})]}):"Start a conversation..."}):u.jsxs(u.Fragment,{children:[Y,d&&l.length>0&&l[l.length-1].type==="human"&&u.jsx($,{}),!d&&i.interrupt&&K&&(()=>{const e=i.interrupt.value,n=e?.actionRequests?.[0]?.name,r=n?U[n]:void 0;return r?u.jsx(r,{interrupt:e,actions:K}):null})()]})})}module.exports=de;
//# sourceMappingURL=ChatBody.cjs.js.map
