page-29065d9c5dc8f2fb.js 7.9 KB

1
  1. (self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[235],{5228:function(e,t,s){Promise.resolve().then(s.bind(s,5679))},5679:function(e,t,s){"use strict";s.r(t),s.d(t,{default:function(){return i}});var r=s(7437),a=s(2265),n=s(8652),o=s(5475);function i(){let[e,t]=(0,a.useState)(!0),[s,i]=(0,a.useState)(""),[l,u]=(0,a.useState)(""),[c,d]=(0,a.useState)(""),[h,g]=(0,a.useState)(""),{login:y,register:b,isLoading:m}=(0,n.aC)(),f=(0,o.useRouter)(),p=async t=>{t.preventDefault(),g("");try{e?await y(s,c):await b(s,l,c),f.push("/")}catch(e){g(e instanceof Error?e.message:"操作失败")}};return(0,r.jsx)("div",{className:"min-h-screen flex items-center justify-center bg-gray-50 dark:bg-gray-900 px-4",children:(0,r.jsxs)("div",{className:"max-w-md w-full bg-white dark:bg-gray-800 rounded-lg shadow-md p-8",children:[(0,r.jsxs)("h1",{className:"text-2xl font-bold text-center mb-6 text-gray-800 dark:text-white",children:[e?"登录":"注册"," Novel Platform"]}),h&&(0,r.jsx)("div",{className:"bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4",children:h}),(0,r.jsxs)("form",{onSubmit:p,className:"space-y-4",children:[(0,r.jsxs)("div",{children:[(0,r.jsx)("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1",children:"邮箱"}),(0,r.jsx)("input",{type:"email",value:s,onChange:e=>i(e.target.value),required:!0,className:"w-full px-3 py-2 border dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-700 dark:text-white"})]}),!e&&(0,r.jsxs)("div",{children:[(0,r.jsx)("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1",children:"用户名"}),(0,r.jsx)("input",{type:"text",value:l,onChange:e=>u(e.target.value),required:!0,className:"w-full px-3 py-2 border dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-700 dark:text-white"})]}),(0,r.jsxs)("div",{children:[(0,r.jsx)("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1",children:"密码"}),(0,r.jsx)("input",{type:"password",value:c,onChange:e=>d(e.target.value),required:!0,className:"w-full px-3 py-2 border dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-700 dark:text-white"})]}),(0,r.jsx)("button",{type:"submit",disabled:m,className:"w-full py-2 px-4 bg-blue-500 text-white rounded-lg hover:bg-blue-600 disabled:bg-gray-300 disabled:cursor-not-allowed transition-colors",children:m?"处理中...":e?"登录":"注册"})]}),(0,r.jsx)("div",{className:"mt-4 text-center",children:(0,r.jsx)("button",{type:"button",onClick:()=>t(!e),className:"text-blue-500 hover:text-blue-600 text-sm",children:e?"没有账号?去注册":"已有账号?去登录"})})]})})}},8652:function(e,t,s){"use strict";s.d(t,{aC:function(){return u},RJ:function(){return l}});var r=s(2265);let a=s(5126).n9;class n{setSession(e){this.sessionId=e}setMcpTokens(e){this.mcpTokens={...e}}getHeaders(){let e={"Content-Type":"application/json"};return this.sessionId&&(e["X-Session-ID"]=this.sessionId),Object.keys(this.mcpTokens).length>0&&(e["X-MCP-Tokens"]=JSON.stringify(this.mcpTokens)),e}async request(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},s="".concat(this.baseUrl).concat(e),r=await fetch(s,{...t,headers:{...this.getHeaders(),...t.headers}});if(!r.ok){let e=await r.json().catch(()=>({error:r.statusText}));throw Error(e.error||e.detail||"API request failed")}return r.json()}async health(){return this.request("/api/health")}async chat(e){var t;let s=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];return this.request("/api/chat",{method:"POST",body:(t={message:e,history:s},a.stringify(t))})}chatStream(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[],s=new URL("".concat(this.baseUrl,"/api/chat/stream")),r={};return this.sessionId&&(r["X-Session-ID"]=this.sessionId),Object.keys(this.mcpTokens).length>0&&(r["X-MCP-Tokens"]=JSON.stringify(this.mcpTokens)),new o(s.toString(),{headers:r,method:"POST",body:JSON.stringify({message:e,history:t})})}async chatStreamFetch(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[],s=arguments.length>2?arguments[2]:void 0,r=arguments.length>3?arguments[3]:void 0,a=arguments.length>4?arguments[4]:void 0,n="".concat(this.baseUrl,"/api/chat/stream"),o=new AbortController;try{var i;let r=await fetch(n,{method:"POST",headers:this.getHeaders(),body:JSON.stringify({message:e,history:t}),signal:o.signal});if(!r.ok)throw Error("HTTP error! status: ".concat(r.status));let l=null===(i=r.body)||void 0===i?void 0:i.getReader(),u=new TextDecoder;if(!l)throw Error("Response body is null");let c="";for(;;){let{done:e,value:t}=await l.read();if(e){null==a||a();break}let r=(c+=u.decode(t,{stream:!0})).split("\n");for(let e of(c=r.pop()||"",r))if(e.startsWith("data: ")){let t=e.slice(6);try{s(new MessageEvent("message",{data:t}))}catch(e){console.error("Error parsing SSE data:",e)}}}}catch(e){e instanceof Error&&"AbortError"!==e.name&&(null==r||r(e))}return()=>o.abort()}async listMcpServers(){return this.request("/api/mcp/servers")}async listMcpTools(){return this.request("/api/mcp/tools")}async login(e,t){let s=await this.request("/api/auth/login",{method:"POST",body:JSON.stringify({email:e,password:t})});return s.session_id&&this.setSession(s.session_id),s}async adminLogin(e,t){let s=await this.request("/api/auth/admin-login",{method:"POST",body:JSON.stringify({email:e,password:t})});return s.session_id&&this.setSession(s.session_id),s}async register(e,t,s){return this.request("/api/auth/register",{method:"POST",body:JSON.stringify({email:e,username:t,password:s})})}async logout(){let e=await this.request("/api/auth/logout",{method:"POST",body:JSON.stringify({session_id:this.sessionId})});return this.sessionId=null,this.mcpTokens={},e}async authStatus(){return this.request("/api/auth/status")}constructor(e="http://localhost:8080"){this.sessionId=null,this.mcpTokens={},this.baseUrl=e}}class o{addEventListener(e,t){"message"===e&&(this.onmessage=t)}close(){var e;null===(e=this.eventSource)||void 0===e||e.close()}constructor(e,t){this.onmessage=null,this.onerror=null,this.eventSource=null,this.url=e,this.headers=t.headers}}let i=new n;function l(){let[e,t]=(0,r.useState)(!1),[s,a]=(0,r.useState)(""),[n,o]=(0,r.useState)([]),[l,u]=(0,r.useState)(null),c=(0,r.useRef)(null),d=(0,r.useCallback)(async function(e){let s=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];t(!0),a(""),o([]),u(null);try{c.current=await i.chatStreamFetch(e,s,e=>{try{let t=JSON.parse(e.data);h(t)}catch(e){console.error("Failed to parse SSE data:",e)}},e=>{u(e.message)},()=>{t(!1)})}catch(e){u(e instanceof Error?e.message:"Unknown error"),t(!1)}},[]),h=e=>{switch(e.type){case"token":e.text&&a(t=>t+e.text);break;case"tool_call":e.tool&&o(t=>[...t,{tool:e.tool,result:e.args}]);break;case"tool_done":e.tool&&o(t=>{let s=[...t],r=s.findLastIndex(t=>t.tool===e.tool);return r>=0&&(s[r]={tool:e.tool,result:e.result}),s});break;case"complete":e.response&&!s&&a(e.response),t(!1);break;case"error":u(e.error||"Unknown error"),t(!1)}};return{sendMessage:d,abort:(0,r.useCallback)(()=>{var e;null===(e=c.current)||void 0===e||e.call(c),t(!1)},[]),isLoading:e,response:s,toolCalls:n,error:l}}function u(){let[e,t]=(0,r.useState)(!1),[s,a]=(0,r.useState)(null),[n,o]=(0,r.useState)(!1),l=(0,r.useCallback)(async(e,s)=>{o(!0);try{let r=await i.login(e,s);if(r.success)return t(!0),a(r.username),r;throw Error("Login failed")}finally{o(!1)}},[]);return{isAuthenticated:e,username:s,isLoading:n,login:l,register:(0,r.useCallback)(async(e,t,s)=>{o(!0);try{return await i.register(e,t,s)}finally{o(!1)}},[]),logout:(0,r.useCallback)(async()=>{o(!0);try{await i.logout(),t(!1),a(null)}finally{o(!1)}},[]),checkStatus:(0,r.useCallback)(async()=>{try{let e=await i.authStatus();return t(e.authenticated),a(e.username||null),e}catch(e){return t(!1),a(null),null}},[])}}}},function(e){e.O(0,[126,971,117,744],function(){return e(e.s=5228)}),_N_E=e.O()}]);