"use strict";const n=require("react/jsx-runtime"),B=require("../../utils/tailwindUtil.cjs.js"),E=require("../../utils/utils.cjs.js"),j=require("lucide-react"),R=require("react"),O=require("../TodoList.cjs.js"),$=require("../ui/AgentMarkdown.cjs.js"),J=require("./BranchSwitcher.cjs.js"),U=require("./MessageActions.cjs.js");function W(t){if(!t)return null;const e=t.additional_kwargs;return e&&typeof e.reasoning_content=="string"&&e.reasoning_content.length>0?e.reasoning_content:null}function D({reasoningText:t,toolStatuses:e,isStreaming:s,fontSize:x}){const[g,a]=R.useState(!1);R.useEffect(()=>{a(!!s)},[s]);const y=e&&e.length>0,p=!!t&&t.length>0;return!y&&!p&&!s?null:n.jsxs("div",{className:"my-1",children:[n.jsxs("button",{type:"button",onClick:()=>a(d=>!d),className:"inline-flex items-center gap-1.5 text-xs text-zinc-400/80 transition-colors hover:text-zinc-300",children:[s?n.jsx(j.LoaderCircle,{className:"size-3 animate-spin"}):n.jsx(j.Sparkles,{className:"size-3"}),n.jsx("span",{children:s?"Thinking":"Thought"}),n.jsx(j.ChevronRight,{className:B.cn("size-3 transition-transform duration-200",g&&"rotate-90")})]}),g&&n.jsxs("div",{className:"mt-1.5 border-l-2 border-white/10 pl-4 space-y-1",children:[y&&e.map(d=>n.jsxs("div",{className:"flex items-center gap-2 text-sm text-zinc-300 py-0.5",children:[!d.isCompleted&&n.jsx(j.LoaderCircle,{className:"size-3.5 animate-spin text-zinc-400"}),n.jsx(j.Wrench,{className:"size-3.5 text-zinc-500"}),n.jsx("span",{children:d.label})]},d.key)),p&&n.jsx("div",{className:"text-sm text-zinc-300",children:n.jsx($.AgentMarkdown,{fontSize:x,children:t})})]})]})}function G(t){if(!t||typeof t!="object")return null;const e=t,s=typeof e.visible=="boolean"?e.visible:typeof e.show=="boolean"?e.show:typeof e.display=="boolean"?e.display:typeof e.hidden=="boolean"?!e.hidden:!0;return{id:typeof e.id=="string"?e.id:void 0,name:typeof e.name=="string"?e.name:void 0,label:typeof e.label=="string"?e.label:void 0,event:typeof e.event=="string"?e.event:void 0,visible:s}}function H(t){if(!t)return[];const e=t.additional_kwargs;return!e||!Array.isArray(e.tool_status)?[]:e.tool_status.map(G).filter(s=>s!==null)}function Q(t){if(!t)return!1;const e=t.additional_kwargs;return e?Object.prototype.hasOwnProperty.call(e,"tool_status"):!1}function z(t){return t?t.toLowerCase().replace(/\s+/g," ").replace(/\s*event:\s*metadata\s*$/i,"").trim():""}function V(t){return!(t.visible===!1||z(t.label).length===0||t.event?.toLowerCase()==="metadata"||/\bevent:\s*metadata\b/i.test(t.label??""))}function X(t,e){if(!e)return t;if(typeof e=="string"){const s=e.trim();return s.length===0?t:`${t} ${s.slice(0,80)}${s.length>80?"...":""}`}if(typeof e=="object")try{const s=JSON.stringify(e);return`${t} ${s.length>80?`${s.slice(0,80)}...`:s}`}catch{return t}return t}function Y(t,e,s,x){if(!t||t.length===0)return null;const g=t,a=g.filter(i=>i.type==="tool"),y=g.some(i=>Q(i)),p=new Set(a.map(i=>i.tool_call_id).filter(i=>typeof i=="string"&&i.length>0)),d=new Set(a.map(i=>i.name).filter(i=>typeof i=="string"&&i.length>0)),f=[],u=[],h=[],m=new Set;let c=0;const k=i=>{if(e)for(const o of H(i)){if(!V(o))continue;const r=z(o.label)||z(o.name)||"tool";if(m.has(r))continue;m.add(r);const N=o.label??o.name??"Running tool",b=(o.id?p.has(o.id):!1)||(o.name?d.has(o.name):!1)||!s;f.push({key:r,label:N,isCompleted:b})}};for(const i of g){if(i.type!=="ai")continue;const o=i.content,r=W(i);if(k(i),typeof o=="string"){r?u.push(r):o.length>0&&(h.push(n.jsx("div",{className:"py-1",children:n.jsx($.AgentMarkdown,{fontSize:x,children:o})},`text-content-${i.id??c}`)),c++);continue}if(!Array.isArray(o)){r&&u.push(r);continue}const N=o;let b="",q=!1;const A=()=>{b.length>0&&(h.push(n.jsx("div",{className:"py-1",children:n.jsx($.AgentMarkdown,{fontSize:x,children:b})},`text-${i.id??c}-${c}`)),b="",c++)};for(let v=0;v<N.length;v++){const l=N[v];if(l.type==="text"&&typeof l.text=="string"){b+=l.text;continue}if(l.type==="reasoning"&&typeof l.reasoning=="string"||l.type==="thinking"&&typeof l.thinking=="string"){A(),q=!0;const T=l.reasoning??l.thinking;u.push(T);continue}if(e&&!y&&l.type==="tool_use"){A();const T=typeof l.id=="string"?l.id:void 0,K=typeof l.name=="string"?l.name:"tool",C=z(K)||`tool-${v}`;if(!m.has(C)){m.add(C);const I=X(K,l.input),L=T?p.has(T):!s;f.push({key:C,label:I,isCompleted:L})}}}!q&&r?(b="",u.push(r)):A()}if(e&&!s&&!y&&f.length===0&&h.length===0&&a.length>0)for(const i of a)f.push({key:`tool-msg-${i.id??c}`,label:"Tool call completed",isCompleted:!0}),c++;const w=[],S=h.length>0,_=u.join(`

`),F=e&&f.length>0||_.length>0,M=!!s&&!S;return(F||M)&&w.push(n.jsx(D,{reasoningText:_||void 0,toolStatuses:e?f:void 0,isStreaming:M,fontSize:x},"agent-activity")),w.push(...h),w.length>0?n.jsx(n.Fragment,{children:w}):null}function Z({agentName:t,fontSize:e,message:s,groupedMessages:x,showToolActivity:g=!0,isStreaming:a=!1,onRegenerate:y,feedback:p,onFeedback:d,branch:f,branchOptions:u,onBranchSelect:h,todos:m}){const c=E.getContentString(s?.content),k=Y(x??[s],g,a,e);return n.jsxs("div",{className:"agent-message flex flex-col gap-1 w-full group",children:[n.jsxs("div",{className:"flex items-center gap-3 w-full",children:[n.jsx("div",{className:"rounded-full size-8 shrink-0 bg-zinc-800 flex items-center justify-center p-2","data-alt":"AI Avatar",children:n.jsx(j.BotMessageSquare,{className:"text-xs",color:"white"})}),n.jsx("span",{className:"text-zinc-500 text-sm",children:t||"Agent"})]}),n.jsxs("div",{className:"flex flex-1 flex-col gap-1 items-start min-w-0",children:[m&&m.length>0&&n.jsx(O,{todos:m}),n.jsx("div",{className:"text-content text-foreground",style:e?{fontSize:e}:void 0,children:k||n.jsxs(n.Fragment,{children:[n.jsx("div",{className:"flex items-center gap-2 text-zinc-500",children:n.jsx("span",{children:"Thinking..."})}),a&&c&&n.jsx("span",{className:"inline-block w-2 h-4 ml-1 bg-zinc-400 animate-pulse"})]})}),!a&&f&&u&&h&&u.length>1&&n.jsx(J.BranchSwitcher,{branch:f,branchOptions:u,onSelect:h,isLoading:a}),!a&&c&&n.jsx(U.MessageActions,{message:s,onRegenerate:y,feedback:p,onFeedback:d,className:""})]})]})}const P=R.memo(Z,(t,e)=>e.isStreaming?!1:t.message.id===e.message.id&&t.groupedMessages?.length===e.groupedMessages?.length&&t.showToolActivity===e.showToolActivity&&t.isStreaming===e.isStreaming&&t.feedback===e.feedback&&t.onRegenerate===e.onRegenerate&&t.onFeedback===e.onFeedback&&t.branch===e.branch&&t.branchOptions?.length===e.branchOptions?.length&&t.onBranchSelect===e.onBranchSelect&&t.todos===e.todos);module.exports=P;
//# sourceMappingURL=AgentMessage.cjs.js.map
