import{r as t,g as w,j as e,i as N,a as k,s as C}from"./index-BQMOOig6.js";function S(){const[o,i]=t.useState([]),[n,x]=t.useState(""),[l,u]=t.useState(!1),[d,c]=t.useState(!1),[g,r]=t.useState(null),b=t.useRef(null),m=()=>{b.current?.scrollIntoView({behavior:"smooth"})};t.useEffect(()=>{m()},[o]),t.useEffect(()=>{w().then(s=>c(s.initialized)).catch(()=>c(!1))},[]);const p=async()=>{r(null);try{await N(),c(!0)}catch{r("初始化失败，请确保后端服务正在运行")}},h=async()=>{if(!n.trim()||l)return;const s={id:Date.now().toString(),role:"user",content:n.trim(),timestamp:new Date};i(a=>[...a,s]),x(""),u(!0),r(null);try{const a=await C(s.content),j={id:(Date.now()+1).toString(),role:"agent",content:a.result,timestamp:new Date,steps:a.steps,success:a.success};i(v=>[...v,j])}catch{r("发送失败，请检查网络连接")}finally{u(!1)}},y=async()=>{await k(),i([]),r(null)},f=s=>{s.key==="Enter"&&!s.shiftKey&&(s.preventDefault(),h())};return e.jsx("div",{className:"h-full flex items-center justify-center p-4",children:e.jsxs("div",{className:"flex flex-col w-full max-w-2xl h-[600px] border border-gray-200 dark:border-gray-700 rounded-2xl shadow-lg bg-white dark:bg-gray-800",children:[e.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-gray-200 dark:border-gray-700 rounded-t-2xl",children:[e.jsx("h1",{className:"text-xl font-semibold",children:"AutoGLM Chat"}),e.jsxs("div",{className:"flex gap-2",children:[d?e.jsx("span",{className:"px-3 py-1 bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200 rounded-full text-sm",children:"已初始化"}):e.jsx("button",{onClick:p,className:"px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 transition-colors",children:"初始化 Agent"}),e.jsx("button",{onClick:y,className:"px-4 py-2 bg-gray-200 dark:bg-gray-700 rounded-lg hover:bg-gray-300 dark:hover:bg-gray-600 transition-colors",children:"重置"})]})]}),g&&e.jsx("div",{className:"mx-4 mt-4 p-3 bg-red-100 dark:bg-red-900 text-red-700 dark:text-red-200 rounded-lg",children:g}),e.jsxs("div",{className:"flex-1 overflow-y-auto p-4 space-y-4",children:[o.length===0&&e.jsxs("div",{className:"text-center text-gray-500 dark:text-gray-400 mt-8",children:[e.jsx("p",{className:"text-lg",children:"欢迎使用 AutoGLM Chat"}),e.jsx("p",{className:"text-sm mt-2",children:"输入任务描述，让 AI 帮你操作手机"})]}),o.map(s=>e.jsx("div",{className:`flex ${s.role==="user"?"justify-end":"justify-start"}`,children:e.jsxs("div",{className:`max-w-[70%] rounded-2xl px-4 py-3 ${s.role==="user"?"bg-blue-500 text-white":s.success===!1?"bg-red-100 dark:bg-red-900 text-red-800 dark:text-red-200":"bg-gray-200 dark:bg-gray-700"}`,children:[e.jsx("p",{className:"whitespace-pre-wrap",children:s.content}),s.role==="agent"&&s.steps!==void 0&&e.jsxs("p",{className:"text-xs mt-2 opacity-70",children:["执行步数: ",s.steps]})]})},s.id)),l&&e.jsx("div",{className:"flex justify-start",children:e.jsx("div",{className:"bg-gray-200 dark:bg-gray-700 rounded-2xl px-4 py-3",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("div",{className:"w-2 h-2 bg-gray-500 rounded-full animate-bounce"}),e.jsx("div",{className:"w-2 h-2 bg-gray-500 rounded-full animate-bounce",style:{animationDelay:"0.1s"}}),e.jsx("div",{className:"w-2 h-2 bg-gray-500 rounded-full animate-bounce",style:{animationDelay:"0.2s"}}),e.jsx("span",{className:"ml-2 text-sm text-gray-500",children:"正在执行任务..."})]})})}),e.jsx("div",{ref:b})]}),e.jsx("div",{className:"p-4 border-t border-gray-200 dark:border-gray-700 rounded-b-2xl",children:e.jsxs("div",{className:"flex gap-2",children:[e.jsx("input",{type:"text",value:n,onChange:s=>x(s.target.value),onKeyDown:f,placeholder:d?"输入任务描述...":"请先初始化 Agent",disabled:!d||l,className:"flex-1 px-4 py-3 border border-gray-300 dark:border-gray-600 rounded-xl bg-white dark:bg-gray-800 focus:outline-none focus:ring-2 focus:ring-blue-500 disabled:opacity-50 disabled:cursor-not-allowed"}),e.jsx("button",{onClick:h,disabled:!d||l||!n.trim(),className:"px-6 py-3 bg-blue-500 text-white rounded-xl hover:bg-blue-600 transition-colors disabled:opacity-50 disabled:cursor-not-allowed",children:"发送"})]})})]})})}export{S as component};
