import{r as a,E as j,F as E,G as T,j as t,g as M,u as U,m as I,B as O}from"./index-B48HCCvn.js";function P(s){const{sessionId:c,onOutput:k,onExit:x,onError:b}=s,[p,g]=a.useState(()=>j.isConnected()),[l,d]=a.useState(!1),m=a.useRef({onOutput:k,onExit:x,onError:b});m.current={onOutput:k,onExit:x,onError:b},a.useEffect(()=>{const n=j.getSocket();g(n.connected);const u=()=>g(!0),v=()=>{g(!1),d(!1)},C=r=>{r.sessionId===c&&m.current.onOutput?.(r.data)},w=r=>{r.sessionId===c&&(d(!1),m.current.onExit?.(r.exitCode))},S=r=>{r.sessionId===c&&m.current.onError?.(r.message)};return n.on("connect",u),n.on("disconnect",v),n.on(E.SESSION_STDOUT,C),n.on(E.SESSION_EXIT,w),n.on(E.SESSION_ERROR,S),g(n.connected),()=>{n.off("connect",u),n.off("disconnect",v),n.off(E.SESSION_STDOUT,C),n.off(E.SESSION_EXIT,w),n.off(E.SESSION_ERROR,S),d(!1)}},[c]);const i=a.useCallback(()=>new Promise(n=>{if(!j.getSocket().connected){n(!1);return}d(!0),n(!0)}),[]),y=a.useCallback(()=>{d(!1)},[]),N=a.useCallback(n=>{j.getSocket().emit(T.INPUT,{sessionId:c,data:n})},[c]),h=a.useCallback((n,u)=>{j.getSocket().emit(T.RESIZE,{sessionId:c,cols:n,rows:u})},[c]);return{isConnected:p,isAttached:l,attach:i,detach:y,sendInput:N,resize:h}}function $({className:s,...c}){return t.jsx("div",{"data-slot":"card",className:M("bg-card text-card-foreground flex flex-col gap-6 rounded-xl border py-6 shadow-sm",s),...c})}function B(){const{t:s}=U(),[c,k]=a.useState([]),[x,b]=a.useState(""),[p,g]=a.useState(""),[l,d]=a.useState(null),[m,i]=a.useState([]),[y,N]=a.useState(!1),[h,n]=a.useState(""),u=a.useRef(null),v=a.useCallback(e=>{i(o=>{const f=o[o.length-1];return f&&f.role==="agent"?[...o.slice(0,-1),{...f,content:f.content+e}]:[...o,{role:"agent",content:e,timestamp:new Date}]})},[]),C=a.useCallback(e=>{i(o=>[...o,{role:"agent",content:`
[进程退出，退出码: ${e}]`,timestamp:new Date}]),d(null)},[]),w=a.useCallback(e=>{i(o=>[...o,{role:"agent",content:`
[错误: ${e}]`,timestamp:new Date}])},[]),{isConnected:S,isAttached:r,attach:D}=P({sessionId:l||"",onOutput:v,onExit:C,onError:w});a.useEffect(()=>{I.get("/demo/agents").then(e=>{k(e.agents);const o=e.agents.find(f=>f.available);o&&b(o.type)})},[]),a.useEffect(()=>{u.current?.scrollIntoView({behavior:"smooth"})},[m]),a.useEffect(()=>{l&&S&&!r&&D()},[l,S,r,D]);const R=async()=>{if(!(!x||!p.trim())){N(!0),i([{role:"user",content:p,timestamp:new Date}]);try{const e=await I.post("/demo/start",{agentType:x,prompt:p.trim()});d(e.sessionId),g("")}catch(e){i(o=>[...o,{role:"agent",content:s("启动失败: {message}",{message:e instanceof Error?e.message:s("未知错误")}),timestamp:new Date}])}finally{N(!1)}}},A=async()=>{if(!l||!h.trim())return;const e=h.trim();i(o=>[...o,{role:"user",content:e,timestamp:new Date}]),n("");try{await I.post(`/demo/${l}/message`,{message:e})}catch(o){i(f=>[...f,{role:"agent",content:s("发送失败: {message}",{message:o instanceof Error?o.message:s("未知错误")}),timestamp:new Date}])}},_=async()=>{if(l)try{await I.post(`/demo/${l}/stop`),i(e=>[...e,{role:"agent",content:`
[${s("会话已停止")}]`,timestamp:new Date}]),d(null)}catch(e){console.error("Stop failed:",e)}};return t.jsxs("div",{className:"flex flex-col h-full max-w-4xl mx-auto p-4 gap-4",children:[t.jsx("h1",{className:"text-2xl font-bold",children:s("Agent 交互演示")}),!l&&t.jsxs($,{className:"p-4",children:[t.jsx("h2",{className:"text-lg font-semibold mb-3",children:s("选择 Agent")}),t.jsx("div",{className:"flex gap-2 flex-wrap mb-4",children:c.map(e=>t.jsxs(O,{variant:x===e.type?"default":"outline",disabled:!e.available,onClick:()=>b(e.type),children:[e.name,e.available&&e.version&&` (${e.version})`,!e.available&&s(" (不可用)")]},e.type))}),t.jsxs("div",{className:"flex gap-2",children:[t.jsx("input",{type:"text",value:p,onChange:e=>g(e.target.value),onKeyDown:e=>e.key==="Enter"&&!e.nativeEvent.isComposing&&e.nativeEvent.keyCode!==229&&R(),placeholder:s("输入你的问题或任务..."),className:"flex-1 px-3 py-2 border rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"}),t.jsx(O,{onClick:R,disabled:y||!x||!p.trim(),children:s(y?"启动中...":"开始")})]})]}),t.jsxs($,{className:"flex-1 p-4 overflow-hidden flex flex-col",children:[t.jsxs("div",{className:"flex justify-between items-center mb-3",children:[t.jsx("h2",{className:"text-lg font-semibold",children:s("对话")}),l&&t.jsxs("div",{className:"flex items-center gap-2",children:[t.jsx("span",{className:"text-sm text-gray-500",children:s(S?r?"已连接":"连接中...":"未连接")}),t.jsx(O,{variant:"outline",size:"sm",onClick:_,children:s("停止")})]})]}),t.jsxs("div",{className:"flex-1 overflow-y-auto space-y-3",children:[m.length===0?t.jsx("div",{className:"text-gray-400 text-center py-8",children:s("选择 Agent 并输入问题开始对话")}):m.map((e,o)=>t.jsxs("div",{className:`p-3 rounded-lg ${e.role==="user"?"bg-blue-100 ml-8":"bg-gray-100 mr-8"}`,children:[t.jsx("div",{className:"text-xs text-gray-500 mb-1",children:e.role==="user"?s("你"):"Agent"}),t.jsx("pre",{className:"whitespace-pre-wrap font-mono text-sm",children:e.content})]},o)),t.jsx("div",{ref:u})]}),l&&t.jsxs("div",{className:"flex gap-2 mt-3 pt-3 border-t",children:[t.jsx("input",{type:"text",value:h,onChange:e=>n(e.target.value),onKeyDown:e=>e.key==="Enter"&&!e.repeat&&!e.nativeEvent.isComposing&&e.nativeEvent.keyCode!==229&&A(),placeholder:s("发送后续消息..."),className:"flex-1 px-3 py-2 border rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"}),t.jsx(O,{onClick:A,disabled:!h.trim(),children:s("发送")})]})]})]})}export{B as DemoPage};
