@ian2018cs/agenthub 0.1.67 → 0.1.68

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -160,7 +160,7 @@ abc123`})]})]}),X.importMode==="form"&&(X.type==="sse"||X.type==="http")&&e.jsxs
160
160
  DEBUG=true`})]}),X.importMode==="form"&&(X.type==="sse"||X.type==="http")&&e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-foreground mb-2",children:"请求头(KEY=value,每行一个)"}),e.jsx("textarea",{value:Object.entries(X.config.headers||{}).map(([_,V])=>`${_}=${V}`).join(`
161
161
  `),onChange:_=>{const V={};_.target.value.split(`
162
162
  `).forEach(le=>{const[Ee,...wt]=le.split("=");Ee&&Ee.trim()&&(V[Ee.trim()]=wt.join("=").trim())}),lt("headers",V)},className:"w-full px-3 py-2 border border-gray-300 dark:border-gray-600 bg-gray-50 dark:bg-gray-800 text-gray-900 dark:text-gray-100 rounded-lg focus:ring-blue-500 focus:border-blue-500",rows:"3",placeholder:`Authorization=Bearer token
163
- X-API-Key=your-key`})]}),e.jsxs("div",{className:"flex justify-end gap-2 pt-4",children:[e.jsx(B,{type:"button",variant:"outline",onClick:Ct,children:"取消"}),e.jsx(B,{type:"submit",disabled:We,className:"bg-purple-600 hover:bg-purple-700 disabled:opacity-50",children:We?"保存中...":pe?"更新服务器":"添加服务器"})]})]})]})})]})]}),e.jsxs("div",{className:"flex flex-col sm:flex-row sm:items-center sm:justify-between p-4 md:p-6 border-t border-border flex-shrink-0 gap-3 pb-safe-area-inset-bottom",children:[e.jsx("div",{className:"flex items-center justify-center sm:justify-start gap-2 order-2 sm:order-1",children:Le==="agents"&&re==="models"?S.saving?e.jsx("span",{className:"text-sm text-muted-foreground",children:"保存中..."}):null:e.jsxs(e.Fragment,{children:[W==="success"&&e.jsxs("div",{className:"text-green-600 dark:text-green-400 text-sm flex items-center gap-1",children:[e.jsx("svg",{className:"w-4 h-4",fill:"currentColor",viewBox:"0 0 20 20",children:e.jsx("path",{fillRule:"evenodd",d:"M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z",clipRule:"evenodd"})}),"设置保存成功!"]}),W==="error"&&e.jsxs("div",{className:"text-red-600 dark:text-red-400 text-sm flex items-center gap-1",children:[e.jsx("svg",{className:"w-4 h-4",fill:"currentColor",viewBox:"0 0 20 20",children:e.jsx("path",{fillRule:"evenodd",d:"M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7 4a1 1 0 11-2 0 1 1 0 012 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z",clipRule:"evenodd"})}),"保存设置失败"]})]})}),e.jsxs("div",{className:"flex items-center gap-3 order-1 sm:order-2",children:[Le==="agents"&&re==="models"&&S.hasChanges&&e.jsx(B,{variant:"outline",onClick:()=>ae.current?.reset(),disabled:S.saving,className:"flex-1 sm:flex-none h-10 touch-manipulation",children:"重置"}),e.jsx(B,{variant:"outline",onClick:r,disabled:T||S.saving,className:"flex-1 sm:flex-none h-10 touch-manipulation",children:"取消"}),e.jsx(B,{onClick:Le==="agents"&&re==="models"?()=>ae.current?.save():Qt,disabled:Le==="agents"&&re==="models"?S.saving||!S.hasChanges:T,className:"flex-1 sm:flex-none h-10 bg-blue-600 hover:bg-blue-700 disabled:opacity-50 touch-manipulation",children:T||S.saving?e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("div",{className:"w-4 h-4 animate-spin rounded-full border-2 border-white border-t-transparent"}),"保存中..."]}):"保存设置"})]})]})]})}),Ie&&e.jsx(Sl,{onClose:()=>Ae(!1),onInstalled:()=>rt(),onOpenRepoManager:()=>{Ae(!1),Be(!0)}}),De&&e.jsx(kl,{onClose:()=>Be(!1),onChanged:()=>{}})]}):null}const El=[{key:"claude_shell",label:"Claude Code Shell",desc:"访问 Claude Code 终端"},{key:"codex_shell",label:"Codex Shell",desc:"访问 Codex 终端"},{key:"gemini_shell",label:"Gemini CLI Shell",desc:"访问 Gemini CLI 终端"},{key:"submit_agent",label:"提交 Agent",desc:"在 Agent 商店中提交新 Agent"},{key:"tab_models",label:"模型设置",desc:"设置 > Agent > 模型 tab"},{key:"tab_skills",label:"技能设置",desc:"设置 > Agent > 技能 tab"},{key:"tab_mcp",label:"MCP 服务器设置",desc:"设置 > Agent > MCP 服务器 tab"},{key:"tab_permissions",label:"权限设置",desc:"设置 > Agent > 权限 tab"},{key:"feishu_binding",label:"绑定飞书账号",desc:"设置 > 账户 > 飞书绑定"},{key:"create_project",label:"新建项目",desc:"侧边栏 > 新建项目按钮"}];function Ml({user:t,onClose:r,onSaved:a}){const[o,n]=s.useState({}),[l,i]=s.useState(!0),[c,u]=s.useState(!1),[v,g]=s.useState(""),[b,p]=s.useState(!1);s.useEffect(()=>{y()},[t.id]);const y=async()=>{i(!0);try{const C=await(await ue.admin.getUserPermissions(t.id)).json();if(C.permissions)n(C.permissions),p(!0);else{const E=await(await ue.admin.getDefaultPermissions()).json();n(E.permissions||{}),p(!1)}}catch(h){console.error("Error fetching permissions:",h),g("加载权限失败")}finally{i(!1)}},f=h=>{n(C=>({...C,[h]:!C[h]})),p(!0)},x=async h=>{h.preventDefault(),g(""),u(!0);try{const C=await ue.admin.updateUserPermissions(t.id,o),m=await C.json();C.ok?(a?.(),r()):g(m.error||"保存失败")}catch(C){console.error("Error saving permissions:",C),g("网络错误,请稍后再试")}finally{u(!1)}},j=async()=>{g(""),u(!0);try{if((await ue.admin.updateUserPermissions(t.id,null)).ok){const m=await(await ue.admin.getDefaultPermissions()).json();n(m.permissions||{}),p(!1)}}catch(h){console.error("Error resetting permissions:",h),g("重置失败")}finally{u(!1)}};return e.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50",children:e.jsxs("div",{className:"bg-card border border-border rounded-lg shadow-lg w-full max-w-md p-6",children:[e.jsxs("div",{className:"flex items-center justify-between mb-4",children:[e.jsxs("div",{children:[e.jsxs("h4",{className:"text-lg font-semibold text-foreground",children:["功能权限 - ",t.username||t.email]}),b?e.jsx("p",{className:"text-xs text-blue-500 mt-0.5",children:"已设置个性化权限"}):e.jsx("p",{className:"text-xs text-muted-foreground mt-0.5",children:"使用系统默认权限"})]}),e.jsx("button",{onClick:r,className:"text-muted-foreground hover:text-foreground",children:e.jsx(Ge,{className:"w-5 h-5"})})]}),l?e.jsx("div",{className:"text-center py-8 text-muted-foreground",children:"加载中..."}):e.jsxs("form",{onSubmit:x,className:"space-y-3",children:[e.jsx("div",{className:"space-y-1 max-h-[400px] overflow-y-auto",children:El.map(h=>e.jsxs("label",{className:"flex items-center justify-between p-2.5 rounded-md hover:bg-accent/50 cursor-pointer transition-colors",children:[e.jsxs("div",{className:"min-w-0 mr-3",children:[e.jsx("div",{className:"text-sm font-medium text-foreground",children:h.label}),e.jsx("div",{className:"text-xs text-muted-foreground",children:h.desc})]}),e.jsx("button",{type:"button",role:"switch","aria-checked":o[h.key]!==!1,onClick:()=>f(h.key),className:`relative inline-flex h-5 w-9 flex-shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none ${o[h.key]!==!1?"bg-blue-600":"bg-gray-300 dark:bg-gray-600"}`,children:e.jsx("span",{className:`pointer-events-none inline-block h-4 w-4 transform rounded-full bg-white shadow ring-0 transition duration-200 ease-in-out ${o[h.key]!==!1?"translate-x-4":"translate-x-0"}`})})]},h.key))}),v&&e.jsx("div",{className:"p-3 bg-red-100 dark:bg-red-900/20 border border-red-300 dark:border-red-800 rounded-md",children:e.jsx("p",{className:"text-sm text-red-700 dark:text-red-400",children:v})}),e.jsxs("div",{className:"flex items-center justify-between pt-2",children:[e.jsxs(B,{type:"button",variant:"ghost",size:"sm",onClick:j,disabled:c||!b,className:"flex items-center gap-1.5 text-muted-foreground",children:[e.jsx(Ra,{className:"w-3.5 h-3.5"}),"重置为默认"]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(B,{type:"button",variant:"outline",onClick:r,disabled:c,children:"取消"}),e.jsx(B,{type:"submit",disabled:c,children:c?"保存中...":"保存"})]})]})]})]})})}function Ll({user:t,onClose:r,onSaved:a}){const[o,n]=s.useState(new Set),[l,i]=s.useState([]),[c,u]=s.useState(""),[v,g]=s.useState(!0),[b,p]=s.useState(!1),[y,f]=s.useState(""),[x,j]=s.useState(!1);s.useEffect(()=>{h()},[t.id]);const h=async()=>{g(!0);try{const[M,A]=await Promise.all([G("/api/agents"),ue.admin.getUserVisibleAgents(t.id)]),T=await M.json(),L=await A.json();i(T.agents||[]),L.agents&&Array.isArray(L.agents)?(n(new Set(L.agents)),j(!0)):(n(new Set),j(!1))}catch(M){console.error("Error fetching agent visibility data:",M),f("加载数据失败")}finally{g(!1)}},C=s.useMemo(()=>{if(!c.trim())return l;const M=c.toLowerCase();return l.filter(A=>(A.display_name||A.name||"").toLowerCase().includes(M)||(A.description||"").toLowerCase().includes(M)||(A.dirName||"").toLowerCase().includes(M))},[l,c]),m=C.length>0&&C.every(M=>o.has(M.dirName||M.name)),E=M=>{n(A=>{const T=new Set(A);return T.has(M)?T.delete(M):T.add(M),T}),j(!0)},k=()=>{n(M=>{const A=new Set(M);return m?C.forEach(T=>A.delete(T.dirName||T.name)):C.forEach(T=>A.add(T.dirName||T.name)),A}),j(!0)},U=async M=>{M.preventDefault(),f(""),p(!0);try{const A=[...o],T=await ue.admin.updateUserVisibleAgents(t.id,A),L=await T.json();T.ok?(a?.(),r()):f(L.error||"保存失败")}catch(A){console.error("Error saving agent visibility:",A),f("网络错误,请稍后再试")}finally{p(!1)}},P=async()=>{f(""),p(!0);try{(await ue.admin.updateUserVisibleAgents(t.id,null)).ok&&(n(new Set),j(!1))}catch(M){console.error("Error resetting agent visibility:",M),f("重置失败")}finally{p(!1)}};return e.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50",children:e.jsxs("div",{className:"bg-card border border-border rounded-lg shadow-lg w-full max-w-md p-6",children:[e.jsxs("div",{className:"flex items-center justify-between mb-4",children:[e.jsxs("div",{children:[e.jsxs("h4",{className:"text-lg font-semibold text-foreground",children:["Agent 可见性 - ",t.username||t.email]}),x?e.jsxs("p",{className:"text-xs text-blue-500 mt-0.5",children:["已设置个性化配置(已选 ",o.size," 个)"]}):e.jsx("p",{className:"text-xs text-muted-foreground mt-0.5",children:"使用默认配置(不可见任何 Agent)"})]}),e.jsx("button",{onClick:r,className:"text-muted-foreground hover:text-foreground",children:e.jsx(Ge,{className:"w-5 h-5"})})]}),v?e.jsx("div",{className:"text-center py-8 text-muted-foreground",children:"加载中..."}):l.length===0?e.jsxs("div",{className:"text-center py-8 text-muted-foreground",children:[e.jsx(so,{className:"w-8 h-8 mx-auto mb-2 opacity-50"}),e.jsx("p",{children:"仓库中暂无 Agent"})]}):e.jsxs("form",{onSubmit:U,className:"space-y-3",children:[e.jsxs("div",{className:"relative",children:[e.jsx(xt,{className:"absolute left-2.5 top-1/2 -translate-y-1/2 w-4 h-4 text-muted-foreground"}),e.jsx("input",{type:"text",value:c,onChange:M=>u(M.target.value),placeholder:"搜索 Agent...",className:"w-full pl-8 pr-3 py-1.5 text-sm bg-background border border-border rounded-md focus:outline-none focus:ring-1 focus:ring-blue-500"})]}),e.jsxs("label",{className:"flex items-center gap-2 px-2.5 py-1.5 text-sm text-muted-foreground cursor-pointer hover:text-foreground",children:[e.jsx("input",{type:"checkbox",checked:m,onChange:k,className:"w-3.5 h-3.5 rounded border-gray-300 dark:border-gray-600 text-blue-600 focus:ring-blue-500"}),c?`全选搜索结果(${C.length})`:`全选(${l.length})`]}),e.jsx("div",{className:"space-y-0.5 max-h-[400px] overflow-y-auto border border-border rounded-md",children:C.length===0?e.jsx("div",{className:"text-center py-6 text-muted-foreground text-sm",children:"未找到匹配的 Agent"}):C.map(M=>{const A=M.dirName||M.name,T=o.has(A);return e.jsxs("label",{className:`flex items-start gap-2.5 px-3 py-2 cursor-pointer transition-colors hover:bg-accent/50 ${T?"bg-blue-50/50 dark:bg-blue-900/10":""}`,children:[e.jsx("input",{type:"checkbox",checked:T,onChange:()=>E(A),className:"w-3.5 h-3.5 mt-0.5 rounded border-gray-300 dark:border-gray-600 text-blue-600 focus:ring-blue-500 flex-shrink-0"}),e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"text-sm font-medium text-foreground truncate",children:M.display_name||M.name}),M.version&&e.jsxs("span",{className:"text-xs px-1.5 py-0.5 bg-gray-100 dark:bg-gray-800 text-muted-foreground rounded flex-shrink-0",children:["v",M.version]})]}),M.description&&e.jsx("p",{className:"text-xs text-muted-foreground truncate mt-0.5",children:M.description})]})]},A)})}),e.jsxs("div",{className:"text-xs text-muted-foreground px-1",children:["已选 ",o.size," / 共 ",l.length," 个 Agent"]}),y&&e.jsx("div",{className:"p-3 bg-red-100 dark:bg-red-900/20 border border-red-300 dark:border-red-800 rounded-md",children:e.jsx("p",{className:"text-sm text-red-700 dark:text-red-400",children:y})}),e.jsxs("div",{className:"flex items-center justify-between pt-2",children:[e.jsxs(B,{type:"button",variant:"ghost",size:"sm",onClick:P,disabled:b||!x,className:"flex items-center gap-1.5 text-muted-foreground",children:[e.jsx(Ra,{className:"w-3.5 h-3.5"}),"重置为默认"]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(B,{type:"button",variant:"outline",onClick:r,disabled:b,children:"取消"}),e.jsx(B,{type:"submit",disabled:b,children:b?"保存中...":"保存"})]})]})]})]})})}function Il({isSuperAdmin:t,onNavigateToUsage:r,onViewUser:a}){const{user:o}=ht(),{showConfirm:n}=nt(),[l,i]=s.useState([]),[c,u]=s.useState({}),[v,g]=s.useState(!0),[b,p]=s.useState(null),[y,f]=s.useState(!1),[x,j]=s.useState(""),[h,C]=s.useState(""),[m,E]=s.useState(""),[k,U]=s.useState(!1),[P,M]=s.useState(null),[A,T]=s.useState({total_limit_usd:"",daily_limit_usd:""}),[L,W]=s.useState(""),[$,oe]=s.useState(!1),[K,ee]=s.useState(null),[ie,ge]=s.useState(""),[fe,pe]=s.useState(""),[Ce,Ie]=s.useState(""),[Ae,De]=s.useState(!1),[Be,X]=s.useState(null),[we,We]=s.useState(""),[ke,Me]=s.useState(""),[Pe,$e]=s.useState(!1),[_e,Ue]=s.useState(null),[He,Le]=s.useState(null),Fe=async()=>{try{const w=await G("/api/admin/users");if(w.ok){const O=await w.json();i(O.users)}}catch(w){console.error("Error fetching users:",w)}finally{g(!1)}},J=async()=>{try{const w=await G("/api/admin/usage/summary");if(w.ok){const O=await w.json(),ye={};for(const Se of O.users)ye[Se.user_uuid]={total_cost:Se.total_cost||0,total_requests:Se.total_requests||0,last_active:Se.last_active};u(ye)}}catch(w){console.error("Error fetching usage data:",w)}};s.useEffect(()=>{Fe(),J()},[]);const ne=w=>!w||w===0?"$0.00":w<.01?`$${w.toFixed(4)}`:`$${w.toFixed(2)}`,re=w=>w.role==="super_admin"?!1:t||w.id===o?.id?!0:w.role==="user",ze=w=>w.id===o?.id?!1:t?w.role!=="super_admin":!["admin","super_admin"].includes(w.role),Y=async(w,O)=>{const ye=O==="active"?"disabled":"active";p(w);try{(await G(`/api/admin/users/${w}`,{method:"PATCH",body:JSON.stringify({status:ye})})).ok&&i(l.map(Qe=>Qe.id===w?{...Qe,status:ye}:Qe))}catch(Se){console.error("Error updating user status:",Se)}finally{p(null)}},ae=async(w,O)=>{if(await n(`确定要删除用户 "${O}"?这将删除其所有数据。`,{title:"删除用户",variant:"danger",confirmText:"确认删除"})){p(w);try{(await G(`/api/admin/users/${w}`,{method:"DELETE"})).ok&&i(l.filter(Se=>Se.id!==w))}catch(ye){console.error("Error deleting user:",ye)}finally{p(null)}}},S=async w=>{if(w.preventDefault(),E(""),!x||!h){E("用户名和密码不能为空");return}if(x.length<3){E("用户名至少3个字符");return}if(h.length<6){E("密码至少6个字符");return}U(!0);try{const O=await ue.admin.createUser(x,h),ye=await O.json();O.ok?(await Fe(),f(!1),j(""),C("")):E(ye.error||"创建用户失败")}catch(O){console.error("Error creating user:",O),E("网络错误,请稍后再试")}finally{U(!1)}},F=()=>{f(!1),j(""),C(""),E("")},de=w=>{M(w),T({total_limit_usd:w.total_limit_usd!==null&&w.total_limit_usd!==void 0?String(w.total_limit_usd):"",daily_limit_usd:w.daily_limit_usd!==null&&w.daily_limit_usd!==void 0?String(w.daily_limit_usd):""}),W("")},me=()=>{M(null),T({total_limit_usd:"",daily_limit_usd:""}),W("")},te=async w=>{w.preventDefault(),W("");const O=A.total_limit_usd.trim()===""?null:parseFloat(A.total_limit_usd),ye=A.daily_limit_usd.trim()===""?null:parseFloat(A.daily_limit_usd);if(O!==null&&(isNaN(O)||O<0)){W("总额度限制必须是正数或留空");return}if(ye!==null&&(isNaN(ye)||ye<0)){W("每日额度限制必须是正数或留空");return}oe(!0);try{const Se=await ue.admin.updateUserLimits(P.id,{total_limit_usd:O,daily_limit_usd:ye}),Qe=await Se.json();Se.ok?(i(l.map(et=>et.id===P.id?{...et,total_limit_usd:O,daily_limit_usd:ye}:et)),me()):W(Qe.error||"更新限制失败")}catch(Se){console.error("Error updating limits:",Se),W("网络错误,请稍后再试")}finally{oe(!1)}},q=w=>w==null?"无限制":`$${w.toFixed(2)}`,he=w=>{ee(w),ge(""),pe(""),Ie("")},se=()=>{ee(null),ge(""),pe(""),Ie("")},Te=async w=>{if(w.preventDefault(),Ie(""),!ie){Ie("请输入新密码");return}if(ie.length<6){Ie("密码至少需要6个字符");return}if(ie!==fe){Ie("两次输入的密码不一致");return}De(!0);try{const O=await ue.admin.resetUserPassword(K.id,ie),ye=await O.json();O.ok?se():Ie(ye.error||"重置密码失败")}catch(O){console.error("Error resetting password:",O),Ie("网络错误,请稍后再试")}finally{De(!1)}},je=w=>{X(w),We(w.role==="admin"?"admin":"user"),Me("")},Ne=()=>{X(null),We(""),Me("")},N=async w=>{w.preventDefault(),Me(""),$e(!0);try{const O=await ue.admin.updateUserRole(Be.id,we),ye=await O.json();O.ok?(i(l.map(Se=>Se.id===Be.id?{...Se,role:we}:Se)),Ne()):Me(ye.error||"更新角色失败")}catch(O){console.error("Error updating role:",O),Me("网络错误,请稍后再试")}finally{$e(!1)}},Z=w=>w==="super_admin"?e.jsx(uo,{className:"w-4 h-4 text-amber-500 flex-shrink-0"}):w==="admin"?e.jsx(Ft,{className:"w-4 h-4 text-blue-500 flex-shrink-0"}):e.jsx(vr,{className:"w-4 h-4 text-gray-400 flex-shrink-0"}),be=w=>w==="super_admin"?e.jsx("span",{className:"inline-block px-1.5 py-0.5 text-xs rounded whitespace-nowrap bg-amber-500/20 text-amber-500",children:"超级管理员"}):w==="admin"?e.jsx("span",{className:"inline-block px-1.5 py-0.5 text-xs rounded whitespace-nowrap bg-blue-500/20 text-blue-400",children:"管理员"}):e.jsx("span",{className:"inline-block px-1.5 py-0.5 text-xs rounded whitespace-nowrap bg-gray-500/20 text-gray-400",children:"用户"});return v?e.jsx("div",{className:"text-center py-8 text-muted-foreground",children:"正在加载用户..."}):e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("h3",{className:"text-lg font-semibold text-foreground",children:"用户管理"}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(B,{variant:"default",size:"sm",onClick:()=>f(!0),className:"flex items-center gap-2",children:[e.jsx(ro,{className:"w-4 h-4"}),"创建用户"]}),r&&e.jsxs(B,{variant:"outline",size:"sm",onClick:r,className:"flex items-center gap-2",children:[e.jsx(ao,{className:"w-4 h-4"}),"使用量统计"]})]})]}),e.jsx("div",{className:"border border-gray-200 dark:border-gray-700 rounded-lg overflow-x-auto",children:e.jsxs("table",{className:"w-full min-w-[580px] table-fixed",children:[e.jsx("thead",{className:"bg-gray-50 dark:bg-gray-900/50",children:e.jsxs("tr",{children:[e.jsx("th",{className:"px-3 py-2 text-left text-xs font-medium text-muted-foreground w-32",children:"用户"}),e.jsx("th",{className:"px-3 py-2 text-center text-xs font-medium text-muted-foreground w-20",children:"角色"}),e.jsx("th",{className:"px-3 py-2 text-center text-xs font-medium text-muted-foreground w-10",children:"状"}),e.jsx("th",{className:"px-3 py-2 text-right text-xs font-medium text-muted-foreground w-36",children:"费用 / 额度"}),e.jsx("th",{className:"px-3 py-2 text-left text-xs font-medium text-muted-foreground w-20",children:"创建"}),e.jsx("th",{className:"px-3 py-2 text-right text-xs font-medium text-muted-foreground",children:"操作"})]})}),e.jsx("tbody",{className:"divide-y divide-gray-200 dark:divide-gray-700",children:l.map(w=>e.jsxs("tr",{className:"hover:bg-gray-50 dark:hover:bg-gray-900/30",children:[e.jsx("td",{className:"px-3 py-2 w-32",children:e.jsxs("div",{className:"flex items-center gap-1.5 min-w-0",children:[Z(w.role),e.jsx("span",{className:"font-medium text-foreground text-sm truncate block min-w-0",title:w.username||w.email,children:w.username||w.email})]})}),e.jsx("td",{className:"px-3 py-2 text-center",children:be(w.role)}),e.jsx("td",{className:"px-3 py-2 text-center",children:e.jsx("span",{className:`inline-block w-2 h-2 rounded-full ${w.status==="active"?"bg-green-500":"bg-red-500"}`,title:w.status==="active"?"活跃":"已禁用"})}),e.jsx("td",{className:"px-3 py-2 text-right",children:e.jsxs("div",{className:"flex flex-col items-end gap-0.5",children:[e.jsx("span",{className:"font-mono text-xs text-foreground whitespace-nowrap",children:ne(c[w.uuid]?.total_cost)}),re(w)?e.jsx("button",{onClick:()=>de(w),className:"text-[11px] text-blue-600 dark:text-blue-400 hover:underline font-mono whitespace-nowrap leading-tight",title:"点击编辑额度限制",children:w.total_limit_usd!==null||w.daily_limit_usd!==null?e.jsxs("span",{children:["总:",q(w.total_limit_usd)," 日:",q(w.daily_limit_usd)]}):e.jsxs("span",{className:"flex items-center gap-0.5 justify-end text-muted-foreground",children:[e.jsx(Rr,{className:"w-3 h-3"}),"设置限额"]})}):e.jsx("span",{className:"text-xs text-muted-foreground",children:"-"})]})}),e.jsx("td",{className:"px-3 py-2 text-xs text-muted-foreground whitespace-nowrap",children:new Date(w.created_at).toLocaleDateString()}),e.jsx("td",{className:"px-3 py-2",children:e.jsxs("div",{className:"flex items-center justify-end gap-0.5",children:[a&&e.jsx(B,{variant:"ghost",size:"sm",className:"h-7 w-7 p-0",onClick:()=>a(w.uuid,w.username||w.email),title:"查看使用详情",children:e.jsx(no,{className:"w-4 h-4 text-indigo-500"})}),t&&w.role!=="super_admin"&&w.id!==o?.id&&e.jsx(B,{variant:"ghost",size:"sm",className:"h-7 w-7 p-0",onClick:()=>je(w),title:"修改角色",children:e.jsx(oo,{className:"w-4 h-4 text-purple-500"})}),w.role==="user"&&ze(w)&&e.jsx(B,{variant:"ghost",size:"sm",className:"h-7 w-7 p-0",onClick:()=>Ue(w),title:"功能权限",children:e.jsx(io,{className:"w-4 h-4 text-green-500"})}),w.role==="user"&&ze(w)&&e.jsx(B,{variant:"ghost",size:"sm",className:"h-7 w-7 p-0",onClick:()=>Le(w),title:"Agent 可见性",children:e.jsx(lo,{className:"w-4 h-4 text-cyan-500"})}),ze(w)&&e.jsxs(e.Fragment,{children:[w.username&&e.jsx(B,{variant:"ghost",size:"sm",className:"h-7 w-7 p-0",onClick:()=>he(w),disabled:b===w.id,title:"重置密码",children:e.jsx(br,{className:"w-4 h-4 text-blue-500"})}),e.jsx(B,{variant:"ghost",size:"sm",className:"h-7 w-7 p-0",onClick:()=>Y(w.id,w.status),disabled:b===w.id,title:w.status==="active"?"禁用用户":"启用用户",children:w.status==="active"?e.jsx(co,{className:"w-4 h-4 text-orange-500"}):e.jsx(mo,{className:"w-4 h-4 text-green-500"})}),e.jsx(B,{variant:"ghost",size:"sm",className:"h-7 w-7 p-0",onClick:()=>ae(w.id,w.username||w.email),disabled:b===w.id,title:"删除用户",children:b===w.id?e.jsx(Mt,{className:"w-4 h-4 text-red-500 animate-spin"}):e.jsx(mt,{className:"w-4 h-4 text-red-500"})})]})]})})]},w.id))})]})}),y&&e.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50",children:e.jsxs("div",{className:"bg-card border border-border rounded-lg shadow-lg w-full max-w-md p-6",children:[e.jsxs("div",{className:"flex items-center justify-between mb-4",children:[e.jsx("h4",{className:"text-lg font-semibold text-foreground",children:"创建用户"}),e.jsx("button",{onClick:F,className:"text-muted-foreground hover:text-foreground",children:e.jsx(Ge,{className:"w-5 h-5"})})]}),e.jsxs("form",{onSubmit:S,className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx("label",{htmlFor:"newUsername",className:"block text-sm font-medium text-foreground mb-1",children:"用户名"}),e.jsx("input",{type:"text",id:"newUsername",value:x,onChange:w=>j(w.target.value),className:"w-full px-3 py-2 border border-border rounded-md bg-background text-foreground focus:outline-none focus:ring-2 focus:ring-blue-500",placeholder:"请输入用户名",disabled:k})]}),e.jsxs("div",{children:[e.jsx("label",{htmlFor:"newPassword",className:"block text-sm font-medium text-foreground mb-1",children:"密码"}),e.jsx("input",{type:"password",id:"newPassword",value:h,onChange:w=>C(w.target.value),className:"w-full px-3 py-2 border border-border rounded-md bg-background text-foreground focus:outline-none focus:ring-2 focus:ring-blue-500",placeholder:"请输入密码",disabled:k})]}),m&&e.jsx("div",{className:"p-3 bg-red-100 dark:bg-red-900/20 border border-red-300 dark:border-red-800 rounded-md",children:e.jsx("p",{className:"text-sm text-red-700 dark:text-red-400",children:m})}),e.jsxs("div",{className:"flex justify-end gap-2",children:[e.jsx(B,{type:"button",variant:"outline",onClick:F,disabled:k,children:"取消"}),e.jsx(B,{type:"submit",disabled:k,children:k?"创建中...":"创建"})]})]})]})}),P&&e.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50",children:e.jsxs("div",{className:"bg-card border border-border rounded-lg shadow-lg w-full max-w-md p-6",children:[e.jsxs("div",{className:"flex items-center justify-between mb-4",children:[e.jsxs("h4",{className:"text-lg font-semibold text-foreground",children:["设置额度限制 - ",P.username||P.email]}),e.jsx("button",{onClick:me,className:"text-muted-foreground hover:text-foreground",children:e.jsx(Ge,{className:"w-5 h-5"})})]}),e.jsxs("form",{onSubmit:te,className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx("label",{htmlFor:"totalLimit",className:"block text-sm font-medium text-foreground mb-1",children:"总额度上限 (USD)"}),e.jsx("input",{type:"number",id:"totalLimit",step:"0.01",min:"0",value:A.total_limit_usd,onChange:w=>T(O=>({...O,total_limit_usd:w.target.value})),className:"w-full px-3 py-2 border border-border rounded-md bg-background text-foreground focus:outline-none focus:ring-2 focus:ring-blue-500",placeholder:"留空表示不限制",disabled:$}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"用户累计使用金额达到此限制后,将无法继续使用"})]}),e.jsxs("div",{children:[e.jsx("label",{htmlFor:"dailyLimit",className:"block text-sm font-medium text-foreground mb-1",children:"每日额度上限 (USD)"}),e.jsx("input",{type:"number",id:"dailyLimit",step:"0.01",min:"0",value:A.daily_limit_usd,onChange:w=>T(O=>({...O,daily_limit_usd:w.target.value})),className:"w-full px-3 py-2 border border-border rounded-md bg-background text-foreground focus:outline-none focus:ring-2 focus:ring-blue-500",placeholder:"留空表示不限制",disabled:$}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"用户每日使用金额达到此限制后,需等待次日重置"})]}),c[P.uuid]&&e.jsx("div",{className:"p-3 bg-gray-100 dark:bg-gray-800 rounded-md",children:e.jsxs("p",{className:"text-sm text-muted-foreground",children:["当前已使用:",e.jsx("span",{className:"font-mono text-foreground",children:ne(c[P.uuid]?.total_cost)})]})}),L&&e.jsx("div",{className:"p-3 bg-red-100 dark:bg-red-900/20 border border-red-300 dark:border-red-800 rounded-md",children:e.jsx("p",{className:"text-sm text-red-700 dark:text-red-400",children:L})}),e.jsxs("div",{className:"flex justify-end gap-2",children:[e.jsx(B,{type:"button",variant:"outline",onClick:me,disabled:$,children:"取消"}),e.jsx(B,{type:"submit",disabled:$,children:$?"保存中...":"保存"})]})]})]})}),K&&e.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50",children:e.jsxs("div",{className:"bg-card border border-border rounded-lg shadow-lg w-full max-w-md p-6",children:[e.jsxs("div",{className:"flex items-center justify-between mb-4",children:[e.jsxs("h4",{className:"text-lg font-semibold text-foreground",children:["重置密码 - ",K.username]}),e.jsx("button",{onClick:se,className:"text-muted-foreground hover:text-foreground",children:e.jsx(Ge,{className:"w-5 h-5"})})]}),e.jsxs("form",{onSubmit:Te,className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx("label",{htmlFor:"resetNewPassword",className:"block text-sm font-medium text-foreground mb-1",children:"新密码"}),e.jsx("input",{type:"password",id:"resetNewPassword",value:ie,onChange:w=>ge(w.target.value),className:"w-full px-3 py-2 border border-border rounded-md bg-background text-foreground focus:outline-none focus:ring-2 focus:ring-blue-500",placeholder:"请输入新密码(至少6个字符)",disabled:Ae})]}),e.jsxs("div",{children:[e.jsx("label",{htmlFor:"resetConfirmPassword",className:"block text-sm font-medium text-foreground mb-1",children:"确认新密码"}),e.jsx("input",{type:"password",id:"resetConfirmPassword",value:fe,onChange:w=>pe(w.target.value),className:"w-full px-3 py-2 border border-border rounded-md bg-background text-foreground focus:outline-none focus:ring-2 focus:ring-blue-500",placeholder:"请再次输入新密码",disabled:Ae})]}),Ce&&e.jsx("div",{className:"p-3 bg-red-100 dark:bg-red-900/20 border border-red-300 dark:border-red-800 rounded-md",children:e.jsx("p",{className:"text-sm text-red-700 dark:text-red-400",children:Ce})}),e.jsxs("div",{className:"flex justify-end gap-2",children:[e.jsx(B,{type:"button",variant:"outline",onClick:se,disabled:Ae,children:"取消"}),e.jsx(B,{type:"submit",disabled:Ae,children:Ae?"重置中...":"重置密码"})]})]})]})}),Be&&e.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50",children:e.jsxs("div",{className:"bg-card border border-border rounded-lg shadow-lg w-full max-w-md p-6",children:[e.jsxs("div",{className:"flex items-center justify-between mb-4",children:[e.jsxs("h4",{className:"text-lg font-semibold text-foreground",children:["修改角色 - ",Be.username||Be.email]}),e.jsx("button",{onClick:Ne,className:"text-muted-foreground hover:text-foreground",children:e.jsx(Ge,{className:"w-5 h-5"})})]}),e.jsxs("form",{onSubmit:N,className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"block text-sm font-medium text-foreground mb-2",children:"选择角色"}),e.jsxs("label",{className:"flex items-center gap-3 p-3 border border-border rounded-md cursor-pointer hover:bg-accent/50 transition-colors",children:[e.jsx("input",{type:"radio",name:"role",value:"admin",checked:we==="admin",onChange:w=>We(w.target.value),className:"text-blue-600",disabled:Pe}),e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Ft,{className:"w-4 h-4 text-blue-500"}),e.jsx("span",{className:"text-sm font-medium text-foreground",children:"管理员"})]}),e.jsx("p",{className:"text-xs text-muted-foreground mt-0.5",children:"可管理普通用户,但不能操作其他管理员"})]})]}),e.jsxs("label",{className:"flex items-center gap-3 p-3 border border-border rounded-md cursor-pointer hover:bg-accent/50 transition-colors",children:[e.jsx("input",{type:"radio",name:"role",value:"user",checked:we==="user",onChange:w=>We(w.target.value),className:"text-blue-600",disabled:Pe}),e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(vr,{className:"w-4 h-4 text-gray-400"}),e.jsx("span",{className:"text-sm font-medium text-foreground",children:"普通用户"})]}),e.jsx("p",{className:"text-xs text-muted-foreground mt-0.5",children:"受额度限制,无管理权限"})]})]})]}),ke&&e.jsx("div",{className:"p-3 bg-red-100 dark:bg-red-900/20 border border-red-300 dark:border-red-800 rounded-md",children:e.jsx("p",{className:"text-sm text-red-700 dark:text-red-400",children:ke})}),e.jsxs("div",{className:"flex justify-end gap-2",children:[e.jsx(B,{type:"button",variant:"outline",onClick:Ne,disabled:Pe,children:"取消"}),e.jsx(B,{type:"submit",disabled:Pe,children:Pe?"保存中...":"保存"})]})]})]})}),_e&&e.jsx(Ml,{user:_e,onClose:()=>Ue(null),onSaved:()=>Fe()}),He&&e.jsx(Ll,{user:He,onClose:()=>Le(null),onSaved:()=>Fe()})]})}function Al(){const[t,r]=s.useState(""),[a,o]=s.useState(""),[n,l]=s.useState(!0),[i,c]=s.useState(!1),[u,v]=s.useState(null);s.useEffect(()=>{g()},[]);const g=async()=>{try{const p=await ue.admin.getDefaultLimits();if(p.ok){const y=await p.json();r(y.default_total_limit_usd!==null?String(y.default_total_limit_usd):""),o(y.default_daily_limit_usd!==null?String(y.default_daily_limit_usd):"")}}catch(p){console.error("Error fetching default limits:",p)}finally{l(!1)}},b=async p=>{p.preventDefault(),c(!0),v(null);const y=t.trim()===""?null:parseFloat(t),f=a.trim()===""?null:parseFloat(a);if(y!==null&&(isNaN(y)||y<0)){v("error"),c(!1);return}if(f!==null&&(isNaN(f)||f<0)){v("error"),c(!1);return}try{(await ue.admin.updateDefaultLimits({default_total_limit_usd:y,default_daily_limit_usd:f})).ok?(v("success"),setTimeout(()=>v(null),2e3)):v("error")}catch(x){console.error("Error updating default limits:",x),v("error")}finally{c(!1)}};return n?e.jsx("div",{className:"text-center py-8 text-muted-foreground",children:"正在加载..."}):e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"text-lg font-semibold text-foreground",children:"新用户默认额度"}),e.jsx("p",{className:"text-sm text-muted-foreground mt-1",children:"新注册用户将自动应用此总额度限制,留空表示不限制。管理员账户不受此限制。"})]}),e.jsx("form",{onSubmit:b,className:"space-y-3",children:e.jsxs("div",{className:"flex items-end gap-3",children:[e.jsxs("div",{className:"flex-1 max-w-xs",children:[e.jsx("label",{className:"block text-sm font-medium text-foreground mb-1",children:"默认总额度上限 (USD)"}),e.jsx("input",{type:"number",step:"0.01",min:"0",value:t,onChange:p=>r(p.target.value),className:"w-full px-3 py-2 border border-border rounded-md bg-background text-foreground focus:outline-none focus:ring-2 focus:ring-blue-500",placeholder:"留空表示不限制",disabled:i})]}),e.jsxs("div",{className:"flex-1 max-w-xs",children:[e.jsx("label",{className:"block text-sm font-medium text-foreground mb-1",children:"默认日限制 (USD)"}),e.jsx("input",{type:"number",step:"0.01",min:"0",value:a,onChange:p=>o(p.target.value),className:"w-full px-3 py-2 border border-border rounded-md bg-background text-foreground focus:outline-none focus:ring-2 focus:ring-blue-500",placeholder:"留空表示不限制",disabled:i})]}),e.jsxs(B,{type:"submit",variant:"default",size:"sm",disabled:i,className:"flex items-center gap-2",children:[e.jsx(Ir,{className:"w-4 h-4"}),i?"保存中...":"保存"]}),u==="success"&&e.jsx("span",{className:"text-sm text-green-600",children:"已保存"}),u==="error"&&e.jsx("span",{className:"text-sm text-red-600",children:"请输入有效的正数"})]})}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"此设置仅影响新注册的用户,不会修改已有用户的额度。"})]})}const Dl=[{key:"claude_shell",label:"Claude Code Shell"},{key:"codex_shell",label:"Codex Shell"},{key:"gemini_shell",label:"Gemini CLI Shell"},{key:"submit_agent",label:"提交 Agent"},{key:"tab_models",label:"模型设置"},{key:"tab_skills",label:"技能设置"},{key:"tab_mcp",label:"MCP 服务器设置"},{key:"tab_permissions",label:"权限设置"},{key:"feishu_binding",label:"绑定飞书账号"},{key:"create_project",label:"新建项目"}];function Tl(){const[t,r]=s.useState({}),[a,o]=s.useState(!0),[n,l]=s.useState(!1),[i,c]=s.useState(null);s.useEffect(()=>{u()},[]);const u=async()=>{try{const b=await ue.admin.getDefaultPermissions();if(b.ok){const p=await b.json();r(p.permissions||{})}}catch(b){console.error("Error fetching default permissions:",b)}finally{o(!1)}},v=b=>{r(p=>({...p,[b]:!p[b]}))},g=async b=>{b.preventDefault(),l(!0),c(null);try{(await ue.admin.updateDefaultPermissions(t)).ok?(c("success"),setTimeout(()=>c(null),2e3)):c("error")}catch(p){console.error("Error updating default permissions:",p),c("error")}finally{l(!1)}};return a?e.jsx("div",{className:"text-center py-8 text-muted-foreground",children:"正在加载..."}):e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"text-lg font-semibold text-foreground",children:"新用户默认功能权限"}),e.jsx("p",{className:"text-sm text-muted-foreground mt-1",children:"配置新注册用户的默认功能权限。已设置个性化权限的用户不受此影响。管理员始终拥有全部权限。"})]}),e.jsxs("form",{onSubmit:g,className:"space-y-3",children:[e.jsx("div",{className:"space-y-1",children:Dl.map(b=>e.jsxs("label",{className:"flex items-center justify-between p-2.5 rounded-md hover:bg-accent/50 cursor-pointer transition-colors",children:[e.jsx("span",{className:"text-sm font-medium text-foreground",children:b.label}),e.jsx("button",{type:"button",role:"switch","aria-checked":t[b.key]!==!1,onClick:()=>v(b.key),className:`relative inline-flex h-5 w-9 flex-shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none ${t[b.key]!==!1?"bg-blue-600":"bg-gray-300 dark:bg-gray-600"}`,children:e.jsx("span",{className:`pointer-events-none inline-block h-4 w-4 transform rounded-full bg-white shadow ring-0 transition duration-200 ease-in-out ${t[b.key]!==!1?"translate-x-4":"translate-x-0"}`})})]},b.key))}),e.jsxs("div",{className:"flex items-center gap-3 pt-2",children:[e.jsxs(B,{type:"submit",variant:"default",size:"sm",disabled:n,className:"flex items-center gap-2",children:[e.jsx(Ir,{className:"w-4 h-4"}),n?"保存中...":"保存"]}),i==="success"&&e.jsx("span",{className:"text-sm text-green-600",children:"已保存"}),i==="error"&&e.jsx("span",{className:"text-sm text-red-600",children:"保存失败"})]})]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"此设置仅影响未设置个性化权限的用户和新注册用户。"})]})}function Rl(){const{showConfirm:t}=nt(),[r,a]=s.useState([]),[o,n]=s.useState(!0),[l,i]=s.useState(null),[c,u]=s.useState(!1),[v,g]=s.useState(""),[b,p]=s.useState(""),[y,f]=s.useState(!1),x=async()=>{try{const m=await ue.admin.getEmailDomains();if(m.ok){const E=await m.json();a(E.domains)}}catch(m){console.error("Error fetching email domains:",m)}finally{n(!1)}};s.useEffect(()=>{x()},[]);const j=async m=>{if(m.preventDefault(),p(""),!v){p("域名不能为空");return}if(!/^[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?)*\.[a-zA-Z]{2,}$/.test(v)){p("域名格式无效,例如: example.com");return}f(!0);try{const k=await ue.admin.addEmailDomain(v.toLowerCase()),U=await k.json();k.ok?(await x(),u(!1),g("")):p(U.error||"添加域名失败")}catch(k){console.error("Error adding domain:",k),p("网络错误,请稍后再试")}finally{f(!1)}},h=async(m,E)=>{if(await t(`确定要删除域名 "${E}"?删除后该域名的邮箱将无法注册。`,{title:"删除域名",variant:"danger",confirmText:"确认删除"})){i(m);try{(await ue.admin.removeEmailDomain(m)).ok&&a(r.filter(U=>U.id!==m))}catch(k){console.error("Error removing domain:",k)}finally{i(null)}}},C=()=>{u(!1),g(""),p("")};return o?e.jsx("div",{className:"text-center py-8 text-muted-foreground",children:"正在加载..."}):e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"text-lg font-semibold text-foreground",children:"邮箱域名白名单"}),e.jsx("p",{className:"text-sm text-muted-foreground mt-1",children:r.length===0?"未设置白名单,所有邮箱域名均可注册":"只有白名单内的域名邮箱可以注册"})]}),e.jsxs(B,{variant:"default",size:"sm",onClick:()=>u(!0),className:"flex items-center gap-2",children:[e.jsx(zt,{className:"w-4 h-4"}),"添加域名"]})]}),r.length>0?e.jsx("div",{className:"border border-gray-200 dark:border-gray-700 rounded-lg overflow-hidden",children:e.jsxs("table",{className:"w-full",children:[e.jsx("thead",{className:"bg-gray-50 dark:bg-gray-900/50",children:e.jsxs("tr",{children:[e.jsx("th",{className:"px-4 py-3 text-left text-sm font-medium text-muted-foreground",children:"域名"}),e.jsx("th",{className:"px-4 py-3 text-left text-sm font-medium text-muted-foreground",children:"添加时间"}),e.jsx("th",{className:"px-4 py-3 text-right text-sm font-medium text-muted-foreground",children:"操作"})]})}),e.jsx("tbody",{className:"divide-y divide-gray-200 dark:divide-gray-700",children:r.map(m=>e.jsxs("tr",{className:"hover:bg-gray-50 dark:hover:bg-gray-900/30",children:[e.jsx("td",{className:"px-4 py-3",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(zs,{className:"w-4 h-4 text-blue-500"}),e.jsx("span",{className:"font-medium text-foreground",children:m.domain})]})}),e.jsx("td",{className:"px-4 py-3 text-sm text-muted-foreground",children:new Date(m.created_at).toLocaleDateString()}),e.jsx("td",{className:"px-4 py-3",children:e.jsx("div",{className:"flex items-center justify-end",children:e.jsx(B,{variant:"ghost",size:"sm",onClick:()=>h(m.id,m.domain),disabled:l===m.id,title:"删除域名",children:e.jsx(mt,{className:"w-4 h-4 text-red-500"})})})})]},m.id))})]})}):e.jsxs("div",{className:"border border-dashed border-gray-300 dark:border-gray-600 rounded-lg p-8 text-center",children:[e.jsx(zs,{className:"w-12 h-12 text-muted-foreground mx-auto mb-4"}),e.jsx("p",{className:"text-muted-foreground",children:"暂无域名白名单,所有邮箱域名均可注册"}),e.jsx("p",{className:"text-sm text-muted-foreground mt-2",children:"添加域名后,只有白名单内的域名邮箱可以注册"})]}),c&&e.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50",children:e.jsxs("div",{className:"bg-card border border-border rounded-lg shadow-lg w-full max-w-md p-6",children:[e.jsxs("div",{className:"flex items-center justify-between mb-4",children:[e.jsx("h4",{className:"text-lg font-semibold text-foreground",children:"添加域名"}),e.jsx("button",{onClick:C,className:"text-muted-foreground hover:text-foreground",children:e.jsx(Ge,{className:"w-5 h-5"})})]}),e.jsxs("form",{onSubmit:j,className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx("label",{htmlFor:"newDomain",className:"block text-sm font-medium text-foreground mb-1",children:"邮箱域名"}),e.jsx("input",{type:"text",id:"newDomain",value:v,onChange:m=>g(m.target.value.toLowerCase()),className:"w-full px-3 py-2 border border-border rounded-md bg-background text-foreground focus:outline-none focus:ring-2 focus:ring-blue-500",placeholder:"例如: example.com",disabled:y}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"输入域名后,该域名下的所有邮箱(如 user@example.com)都可以注册"})]}),b&&e.jsx("div",{className:"p-3 bg-red-100 dark:bg-red-900/20 border border-red-300 dark:border-red-800 rounded-md",children:e.jsx("p",{className:"text-sm text-red-700 dark:text-red-400",children:b})}),e.jsxs("div",{className:"flex justify-end gap-2",children:[e.jsx(B,{type:"button",variant:"outline",onClick:C,disabled:y,children:"取消"}),e.jsx(B,{type:"submit",disabled:y,children:y?"添加中...":"添加"})]})]})]})})]})}const Ds={pending:{label:"待审核",className:"bg-amber-100 text-amber-700 dark:bg-amber-900/30 dark:text-amber-400",icon:ys},approved:{label:"已通过",className:"bg-green-100 text-green-700 dark:bg-green-900/30 dark:text-green-400",icon:Ta},rejected:{label:"已拒绝",className:"bg-red-100 text-red-700 dark:bg-red-900/30 dark:text-red-400",icon:Da}};function wa(t){return t?new Date(t).toLocaleString("zh-CN",{dateStyle:"short",timeStyle:"short"}):"-"}function Pl({submissionId:t}){const[r,a]=s.useState(null),[o,n]=s.useState(!0),[l,i]=s.useState(0);if(s.useEffect(()=>{G(`/api/agents/submissions/${t}/contents`).then(u=>u.json()).then(u=>{a(u.files||[]),i(0)}).catch(()=>a([])).finally(()=>n(!1))},[t]),o)return e.jsxs("div",{className:"flex items-center gap-1.5 py-3 text-xs text-gray-400",children:[e.jsx(ot,{className:"w-3 h-3 animate-spin"})," 加载文件内容..."]});if(!r||r.length===0)return e.jsx("p",{className:"text-xs text-gray-400 py-2",children:"无法读取文件内容"});const c=r[l];return e.jsxs("div",{className:"mt-3",children:[e.jsx("div",{className:"flex items-center gap-1 mb-2 flex-wrap",children:r.map((u,v)=>e.jsxs("button",{onClick:()=>i(v),className:`flex items-center gap-1 px-2 py-1 text-[11px] rounded border transition-colors ${v===l?"bg-blue-50 dark:bg-blue-900/20 border-blue-300 dark:border-blue-700 text-blue-700 dark:text-blue-300":"border-gray-200 dark:border-gray-700 text-gray-500 dark:text-gray-400 hover:border-gray-300"}`,children:[e.jsx(es,{className:"w-3 h-3"}),u.name]},u.name))}),e.jsx("pre",{className:"text-[11px] leading-relaxed bg-gray-900 dark:bg-gray-950 text-gray-100 rounded-lg p-3 overflow-auto max-h-72 whitespace-pre-wrap break-words",children:c?.content||""})]})}function _l(){const{showAlert:t}=nt(),[r,a]=s.useState([]),[o,n]=s.useState(!0),[l,i]=s.useState("pending"),[c,u]=s.useState(null),[v,g]=s.useState(null),[b,p]=s.useState(""),[y,f]=s.useState(null),[x,j]=s.useState(null),[h,C]=s.useState(!1);s.useEffect(()=>{m()},[l]);const m=async()=>{try{n(!0);const M=l?`/api/agents/submissions?status=${l}`:"/api/agents/submissions",A=await G(M);if(A.ok){const T=await A.json();a(T.submissions||[])}else t("加载提交列表失败",{variant:"error"})}catch(M){t(M.message,{variant:"error"})}finally{n(!1)}},E=M=>{j(M.id),C(!1)},k=async(M,A)=>{g(M.id);try{const T=await G(`/api/agents/submissions/${M.id}/approve`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({updateAllUsers:A})}),L=await T.json();if(T.ok){let W=`已通过审核,版本 v${L.version} 已发布到仓库。`;L.updatedUsersCount>0&&(W+=`
163
+ X-API-Key=your-key`})]}),e.jsxs("div",{className:"flex justify-end gap-2 pt-4",children:[e.jsx(B,{type:"button",variant:"outline",onClick:Ct,children:"取消"}),e.jsx(B,{type:"submit",disabled:We,className:"bg-purple-600 hover:bg-purple-700 disabled:opacity-50",children:We?"保存中...":pe?"更新服务器":"添加服务器"})]})]})]})})]})]}),e.jsxs("div",{className:"flex flex-col sm:flex-row sm:items-center sm:justify-between p-4 md:p-6 border-t border-border flex-shrink-0 gap-3 pb-safe-area-inset-bottom",children:[e.jsx("div",{className:"flex items-center justify-center sm:justify-start gap-2 order-2 sm:order-1",children:Le==="agents"&&re==="models"?S.saving?e.jsx("span",{className:"text-sm text-muted-foreground",children:"保存中..."}):null:e.jsxs(e.Fragment,{children:[W==="success"&&e.jsxs("div",{className:"text-green-600 dark:text-green-400 text-sm flex items-center gap-1",children:[e.jsx("svg",{className:"w-4 h-4",fill:"currentColor",viewBox:"0 0 20 20",children:e.jsx("path",{fillRule:"evenodd",d:"M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z",clipRule:"evenodd"})}),"设置保存成功!"]}),W==="error"&&e.jsxs("div",{className:"text-red-600 dark:text-red-400 text-sm flex items-center gap-1",children:[e.jsx("svg",{className:"w-4 h-4",fill:"currentColor",viewBox:"0 0 20 20",children:e.jsx("path",{fillRule:"evenodd",d:"M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7 4a1 1 0 11-2 0 1 1 0 012 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z",clipRule:"evenodd"})}),"保存设置失败"]})]})}),e.jsxs("div",{className:"flex items-center gap-3 order-1 sm:order-2",children:[Le==="agents"&&re==="models"&&S.hasChanges&&e.jsx(B,{variant:"outline",onClick:()=>ae.current?.reset(),disabled:S.saving,className:"flex-1 sm:flex-none h-10 touch-manipulation",children:"重置"}),e.jsx(B,{variant:"outline",onClick:r,disabled:T||S.saving,className:"flex-1 sm:flex-none h-10 touch-manipulation",children:"取消"}),e.jsx(B,{onClick:Le==="agents"&&re==="models"?()=>ae.current?.save():Qt,disabled:Le==="agents"&&re==="models"?S.saving||!S.hasChanges:T,className:"flex-1 sm:flex-none h-10 bg-blue-600 hover:bg-blue-700 disabled:opacity-50 touch-manipulation",children:T||S.saving?e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("div",{className:"w-4 h-4 animate-spin rounded-full border-2 border-white border-t-transparent"}),"保存中..."]}):"保存设置"})]})]})]})}),Ie&&e.jsx(Sl,{onClose:()=>Ae(!1),onInstalled:()=>rt(),onOpenRepoManager:()=>{Ae(!1),Be(!0)}}),De&&e.jsx(kl,{onClose:()=>Be(!1),onChanged:()=>{}})]}):null}const El=[{key:"claude_shell",label:"Claude Code Shell",desc:"访问 Claude Code 终端"},{key:"codex_shell",label:"Codex Shell",desc:"访问 Codex 终端"},{key:"gemini_shell",label:"Gemini CLI Shell",desc:"访问 Gemini CLI 终端"},{key:"submit_agent",label:"提交 Agent",desc:"在 Agent 商店中提交新 Agent"},{key:"tab_models",label:"模型设置",desc:"设置 > Agent > 模型 tab"},{key:"tab_skills",label:"技能设置",desc:"设置 > Agent > 技能 tab"},{key:"tab_mcp",label:"MCP 服务器设置",desc:"设置 > Agent > MCP 服务器 tab"},{key:"tab_permissions",label:"权限设置",desc:"设置 > Agent > 权限 tab"},{key:"feishu_binding",label:"绑定飞书账号",desc:"设置 > 账户 > 飞书绑定"},{key:"create_project",label:"新建项目",desc:"侧边栏 > 新建项目按钮"}];function Ml({user:t,onClose:r,onSaved:a}){const[o,n]=s.useState({}),[l,i]=s.useState(!0),[c,u]=s.useState(!1),[v,g]=s.useState(""),[b,p]=s.useState(!1);s.useEffect(()=>{y()},[t.id]);const y=async()=>{i(!0);try{const C=await(await ue.admin.getUserPermissions(t.id)).json();if(C.permissions)n(C.permissions),p(!0);else{const E=await(await ue.admin.getDefaultPermissions()).json();n(E.permissions||{}),p(!1)}}catch(h){console.error("Error fetching permissions:",h),g("加载权限失败")}finally{i(!1)}},f=h=>{n(C=>({...C,[h]:!C[h]})),p(!0)},x=async h=>{h.preventDefault(),g(""),u(!0);try{const C=await ue.admin.updateUserPermissions(t.id,o),m=await C.json();C.ok?(a?.(),r()):g(m.error||"保存失败")}catch(C){console.error("Error saving permissions:",C),g("网络错误,请稍后再试")}finally{u(!1)}},j=async()=>{g(""),u(!0);try{if((await ue.admin.updateUserPermissions(t.id,null)).ok){const m=await(await ue.admin.getDefaultPermissions()).json();n(m.permissions||{}),p(!1)}}catch(h){console.error("Error resetting permissions:",h),g("重置失败")}finally{u(!1)}};return e.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50",children:e.jsxs("div",{className:"bg-card border border-border rounded-lg shadow-lg w-full max-w-md p-6",children:[e.jsxs("div",{className:"flex items-center justify-between mb-4",children:[e.jsxs("div",{children:[e.jsxs("h4",{className:"text-lg font-semibold text-foreground",children:["功能权限 - ",t.username||t.email]}),b?e.jsx("p",{className:"text-xs text-blue-500 mt-0.5",children:"已设置个性化权限"}):e.jsx("p",{className:"text-xs text-muted-foreground mt-0.5",children:"使用系统默认权限"})]}),e.jsx("button",{onClick:r,className:"text-muted-foreground hover:text-foreground",children:e.jsx(Ge,{className:"w-5 h-5"})})]}),l?e.jsx("div",{className:"text-center py-8 text-muted-foreground",children:"加载中..."}):e.jsxs("form",{onSubmit:x,className:"space-y-3",children:[e.jsx("div",{className:"space-y-1 max-h-[400px] overflow-y-auto",children:El.map(h=>e.jsxs("label",{className:"flex items-center justify-between p-2.5 rounded-md hover:bg-accent/50 cursor-pointer transition-colors",children:[e.jsxs("div",{className:"min-w-0 mr-3",children:[e.jsx("div",{className:"text-sm font-medium text-foreground",children:h.label}),e.jsx("div",{className:"text-xs text-muted-foreground",children:h.desc})]}),e.jsx("button",{type:"button",role:"switch","aria-checked":o[h.key]!==!1,onClick:()=>f(h.key),className:`relative inline-flex h-5 w-9 flex-shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none ${o[h.key]!==!1?"bg-blue-600":"bg-gray-300 dark:bg-gray-600"}`,children:e.jsx("span",{className:`pointer-events-none inline-block h-4 w-4 transform rounded-full bg-white shadow ring-0 transition duration-200 ease-in-out ${o[h.key]!==!1?"translate-x-4":"translate-x-0"}`})})]},h.key))}),v&&e.jsx("div",{className:"p-3 bg-red-100 dark:bg-red-900/20 border border-red-300 dark:border-red-800 rounded-md",children:e.jsx("p",{className:"text-sm text-red-700 dark:text-red-400",children:v})}),e.jsxs("div",{className:"flex items-center justify-between pt-2",children:[e.jsxs(B,{type:"button",variant:"ghost",size:"sm",onClick:j,disabled:c||!b,className:"flex items-center gap-1.5 text-muted-foreground",children:[e.jsx(Ra,{className:"w-3.5 h-3.5"}),"重置为默认"]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(B,{type:"button",variant:"outline",onClick:r,disabled:c,children:"取消"}),e.jsx(B,{type:"submit",disabled:c,children:c?"保存中...":"保存"})]})]})]})]})})}function Ll({user:t,onClose:r,onSaved:a}){const[o,n]=s.useState(new Set),[l,i]=s.useState([]),[c,u]=s.useState(""),[v,g]=s.useState(!0),[b,p]=s.useState(!1),[y,f]=s.useState(""),[x,j]=s.useState(!1);s.useEffect(()=>{h()},[t.id]);const h=async()=>{g(!0);try{const[M,A]=await Promise.all([G("/api/agents"),ue.admin.getUserVisibleAgents(t.id)]),T=await M.json(),L=await A.json();i(T.agents||[]),L.agents&&Array.isArray(L.agents)?(n(new Set(L.agents)),j(!0)):(n(new Set),j(!1))}catch(M){console.error("Error fetching agent visibility data:",M),f("加载数据失败")}finally{g(!1)}},C=s.useMemo(()=>{if(!c.trim())return l;const M=c.toLowerCase();return l.filter(A=>(A.display_name||A.name||"").toLowerCase().includes(M)||(A.description||"").toLowerCase().includes(M)||(A.dirName||"").toLowerCase().includes(M))},[l,c]),m=C.length>0&&C.every(M=>o.has(M.dirName||M.name)),E=M=>{n(A=>{const T=new Set(A);return T.has(M)?T.delete(M):T.add(M),T}),j(!0)},k=()=>{n(M=>{const A=new Set(M);return m?C.forEach(T=>A.delete(T.dirName||T.name)):C.forEach(T=>A.add(T.dirName||T.name)),A}),j(!0)},U=async M=>{M.preventDefault(),f(""),p(!0);try{const A=[...o],T=await ue.admin.updateUserVisibleAgents(t.id,A),L=await T.json();T.ok?(a?.(),r()):f(L.error||"保存失败")}catch(A){console.error("Error saving agent visibility:",A),f("网络错误,请稍后再试")}finally{p(!1)}},P=async()=>{f(""),p(!0);try{(await ue.admin.updateUserVisibleAgents(t.id,null)).ok&&(n(new Set),j(!1))}catch(M){console.error("Error resetting agent visibility:",M),f("重置失败")}finally{p(!1)}};return e.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50",children:e.jsxs("div",{className:"bg-card border border-border rounded-lg shadow-lg w-full max-w-md p-6",children:[e.jsxs("div",{className:"flex items-center justify-between mb-4",children:[e.jsxs("div",{children:[e.jsxs("h4",{className:"text-lg font-semibold text-foreground",children:["Agent 可见性 - ",t.username||t.email]}),x?e.jsxs("p",{className:"text-xs text-blue-500 mt-0.5",children:["已设置个性化配置(已选 ",o.size," 个)"]}):e.jsx("p",{className:"text-xs text-muted-foreground mt-0.5",children:"使用默认配置(不可见任何 Agent)"})]}),e.jsx("button",{onClick:r,className:"text-muted-foreground hover:text-foreground",children:e.jsx(Ge,{className:"w-5 h-5"})})]}),v?e.jsx("div",{className:"text-center py-8 text-muted-foreground",children:"加载中..."}):l.length===0?e.jsxs("div",{className:"text-center py-8 text-muted-foreground",children:[e.jsx(so,{className:"w-8 h-8 mx-auto mb-2 opacity-50"}),e.jsx("p",{children:"仓库中暂无 Agent"})]}):e.jsxs("form",{onSubmit:U,className:"space-y-3",children:[e.jsxs("div",{className:"relative",children:[e.jsx(xt,{className:"absolute left-2.5 top-1/2 -translate-y-1/2 w-4 h-4 text-muted-foreground"}),e.jsx("input",{type:"text",value:c,onChange:M=>u(M.target.value),placeholder:"搜索 Agent...",className:"w-full pl-8 pr-3 py-1.5 text-sm bg-background border border-border rounded-md focus:outline-none focus:ring-1 focus:ring-blue-500"})]}),e.jsxs("label",{className:"flex items-center gap-2 px-2.5 py-1.5 text-sm text-muted-foreground cursor-pointer hover:text-foreground",children:[e.jsx("input",{type:"checkbox",checked:m,onChange:k,className:"w-3.5 h-3.5 rounded border-gray-300 dark:border-gray-600 text-blue-600 focus:ring-blue-500"}),c?`全选搜索结果(${C.length})`:`全选(${l.length})`]}),e.jsx("div",{className:"space-y-0.5 max-h-[400px] overflow-y-auto border border-border rounded-md",children:C.length===0?e.jsx("div",{className:"text-center py-6 text-muted-foreground text-sm",children:"未找到匹配的 Agent"}):C.map(M=>{const A=M.dirName||M.name,T=o.has(A);return e.jsxs("label",{className:`flex items-start gap-2.5 px-3 py-2 cursor-pointer transition-colors hover:bg-accent/50 ${T?"bg-blue-50/50 dark:bg-blue-900/10":""}`,children:[e.jsx("input",{type:"checkbox",checked:T,onChange:()=>E(A),className:"w-3.5 h-3.5 mt-0.5 rounded border-gray-300 dark:border-gray-600 text-blue-600 focus:ring-blue-500 flex-shrink-0"}),e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"text-sm font-medium text-foreground truncate",children:M.display_name||M.name}),M.version&&e.jsxs("span",{className:"text-xs px-1.5 py-0.5 bg-gray-100 dark:bg-gray-800 text-muted-foreground rounded flex-shrink-0",children:["v",M.version]})]}),M.description&&e.jsx("p",{className:"text-xs text-muted-foreground truncate mt-0.5",children:M.description})]})]},A)})}),e.jsxs("div",{className:"text-xs text-muted-foreground px-1",children:["已选 ",o.size," / 共 ",l.length," 个 Agent"]}),y&&e.jsx("div",{className:"p-3 bg-red-100 dark:bg-red-900/20 border border-red-300 dark:border-red-800 rounded-md",children:e.jsx("p",{className:"text-sm text-red-700 dark:text-red-400",children:y})}),e.jsxs("div",{className:"flex items-center justify-between pt-2",children:[e.jsxs(B,{type:"button",variant:"ghost",size:"sm",onClick:P,disabled:b||!x,className:"flex items-center gap-1.5 text-muted-foreground",children:[e.jsx(Ra,{className:"w-3.5 h-3.5"}),"重置为默认"]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(B,{type:"button",variant:"outline",onClick:r,disabled:b,children:"取消"}),e.jsx(B,{type:"submit",disabled:b,children:b?"保存中...":"保存"})]})]})]})]})})}function Il({isSuperAdmin:t,onNavigateToUsage:r,onViewUser:a}){const{user:o}=ht(),{showConfirm:n}=nt(),[l,i]=s.useState([]),[c,u]=s.useState({}),[v,g]=s.useState(!0),[b,p]=s.useState(null),[y,f]=s.useState(!1),[x,j]=s.useState(""),[h,C]=s.useState(""),[m,E]=s.useState(""),[k,U]=s.useState(!1),[P,M]=s.useState(null),[A,T]=s.useState({total_limit_usd:"",daily_limit_usd:""}),[L,W]=s.useState(""),[$,oe]=s.useState(!1),[K,ee]=s.useState(null),[ie,ge]=s.useState(""),[fe,pe]=s.useState(""),[Ce,Ie]=s.useState(""),[Ae,De]=s.useState(!1),[Be,X]=s.useState(null),[we,We]=s.useState(""),[ke,Me]=s.useState(""),[Pe,$e]=s.useState(!1),[_e,Ue]=s.useState(null),[He,Le]=s.useState(null),Fe=async()=>{try{const w=await G("/api/admin/users");if(w.ok){const O=await w.json();i(O.users)}}catch(w){console.error("Error fetching users:",w)}finally{g(!1)}},J=async()=>{try{const w=await G("/api/admin/usage/summary");if(w.ok){const O=await w.json(),ye={};for(const Se of O.users)ye[Se.user_uuid]={total_cost:Se.total_cost||0,total_requests:Se.total_requests||0,last_active:Se.last_active};u(ye)}}catch(w){console.error("Error fetching usage data:",w)}};s.useEffect(()=>{Fe(),J()},[]);const ne=w=>!w||w===0?"$0.00":w<.01?`$${w.toFixed(4)}`:`$${w.toFixed(2)}`,re=w=>w.role==="super_admin"?!1:t||w.id===o?.id?!0:w.role==="user",ze=w=>w.id===o?.id?!1:t?w.role!=="super_admin":!["admin","super_admin"].includes(w.role),Y=async(w,O)=>{const ye=O==="active"?"disabled":"active";p(w);try{(await G(`/api/admin/users/${w}`,{method:"PATCH",body:JSON.stringify({status:ye})})).ok&&i(l.map(Qe=>Qe.id===w?{...Qe,status:ye}:Qe))}catch(Se){console.error("Error updating user status:",Se)}finally{p(null)}},ae=async(w,O)=>{if(await n(`确定要删除用户 "${O}"?这将删除其所有数据。`,{title:"删除用户",variant:"danger",confirmText:"确认删除"})){p(w);try{(await G(`/api/admin/users/${w}`,{method:"DELETE"})).ok&&i(l.filter(Se=>Se.id!==w))}catch(ye){console.error("Error deleting user:",ye)}finally{p(null)}}},S=async w=>{if(w.preventDefault(),E(""),!x||!h){E("用户名和密码不能为空");return}if(x.length<3){E("用户名至少3个字符");return}if(h.length<6){E("密码至少6个字符");return}U(!0);try{const O=await ue.admin.createUser(x,h),ye=await O.json();O.ok?(await Fe(),f(!1),j(""),C("")):E(ye.error||"创建用户失败")}catch(O){console.error("Error creating user:",O),E("网络错误,请稍后再试")}finally{U(!1)}},F=()=>{f(!1),j(""),C(""),E("")},de=w=>{M(w),T({total_limit_usd:w.total_limit_usd!==null&&w.total_limit_usd!==void 0?String(w.total_limit_usd):"",daily_limit_usd:w.daily_limit_usd!==null&&w.daily_limit_usd!==void 0?String(w.daily_limit_usd):""}),W("")},me=()=>{M(null),T({total_limit_usd:"",daily_limit_usd:""}),W("")},te=async w=>{w.preventDefault(),W("");const O=A.total_limit_usd.trim()===""?null:parseFloat(A.total_limit_usd),ye=A.daily_limit_usd.trim()===""?null:parseFloat(A.daily_limit_usd);if(O!==null&&(isNaN(O)||O<0)){W("总额度限制必须是正数或留空");return}if(ye!==null&&(isNaN(ye)||ye<0)){W("每日额度限制必须是正数或留空");return}oe(!0);try{const Se=await ue.admin.updateUserLimits(P.id,{total_limit_usd:O,daily_limit_usd:ye}),Qe=await Se.json();Se.ok?(i(l.map(et=>et.id===P.id?{...et,total_limit_usd:O,daily_limit_usd:ye}:et)),me()):W(Qe.error||"更新限制失败")}catch(Se){console.error("Error updating limits:",Se),W("网络错误,请稍后再试")}finally{oe(!1)}},q=w=>w==null?"无限制":`$${w.toFixed(2)}`,he=w=>{ee(w),ge(""),pe(""),Ie("")},se=()=>{ee(null),ge(""),pe(""),Ie("")},Te=async w=>{if(w.preventDefault(),Ie(""),!ie){Ie("请输入新密码");return}if(ie.length<6){Ie("密码至少需要6个字符");return}if(ie!==fe){Ie("两次输入的密码不一致");return}De(!0);try{const O=await ue.admin.resetUserPassword(K.id,ie),ye=await O.json();O.ok?se():Ie(ye.error||"重置密码失败")}catch(O){console.error("Error resetting password:",O),Ie("网络错误,请稍后再试")}finally{De(!1)}},je=w=>{X(w),We(w.role==="admin"?"admin":"user"),Me("")},Ne=()=>{X(null),We(""),Me("")},N=async w=>{w.preventDefault(),Me(""),$e(!0);try{const O=await ue.admin.updateUserRole(Be.id,we),ye=await O.json();O.ok?(i(l.map(Se=>Se.id===Be.id?{...Se,role:we}:Se)),Ne()):Me(ye.error||"更新角色失败")}catch(O){console.error("Error updating role:",O),Me("网络错误,请稍后再试")}finally{$e(!1)}},Z=w=>w==="super_admin"?e.jsx(uo,{className:"w-4 h-4 text-amber-500 flex-shrink-0"}):w==="admin"?e.jsx(Ft,{className:"w-4 h-4 text-blue-500 flex-shrink-0"}):e.jsx(vr,{className:"w-4 h-4 text-gray-400 flex-shrink-0"}),be=w=>w==="super_admin"?e.jsx("span",{className:"inline-block px-1.5 py-0.5 text-xs rounded whitespace-nowrap bg-amber-500/20 text-amber-500",children:"超级管理员"}):w==="admin"?e.jsx("span",{className:"inline-block px-1.5 py-0.5 text-xs rounded whitespace-nowrap bg-blue-500/20 text-blue-400",children:"管理员"}):e.jsx("span",{className:"inline-block px-1.5 py-0.5 text-xs rounded whitespace-nowrap bg-gray-500/20 text-gray-400",children:"用户"});return v?e.jsx("div",{className:"text-center py-8 text-muted-foreground",children:"正在加载用户..."}):e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("h3",{className:"text-lg font-semibold text-foreground",children:"用户管理"}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(B,{variant:"default",size:"sm",onClick:()=>f(!0),className:"flex items-center gap-2",children:[e.jsx(ro,{className:"w-4 h-4"}),"创建用户"]}),r&&e.jsxs(B,{variant:"outline",size:"sm",onClick:r,className:"flex items-center gap-2",children:[e.jsx(ao,{className:"w-4 h-4"}),"使用量统计"]})]})]}),e.jsx("div",{className:"border border-gray-200 dark:border-gray-700 rounded-lg overflow-x-auto",children:e.jsxs("table",{className:"w-full min-w-[580px] table-fixed",children:[e.jsx("thead",{className:"bg-gray-50 dark:bg-gray-900/50",children:e.jsxs("tr",{children:[e.jsx("th",{className:"px-3 py-2 text-left text-xs font-medium text-muted-foreground w-44",children:"用户"}),e.jsx("th",{className:"px-3 py-2 text-center text-xs font-medium text-muted-foreground w-20",children:"角色"}),e.jsx("th",{className:"px-3 py-2 text-center text-xs font-medium text-muted-foreground w-10",children:"状"}),e.jsx("th",{className:"px-3 py-2 text-right text-xs font-medium text-muted-foreground w-36",children:"费用 / 额度"}),e.jsx("th",{className:"px-3 py-2 text-left text-xs font-medium text-muted-foreground w-20",children:"创建"}),e.jsx("th",{className:"px-3 py-2 text-right text-xs font-medium text-muted-foreground",children:"操作"})]})}),e.jsx("tbody",{className:"divide-y divide-gray-200 dark:divide-gray-700",children:l.map(w=>e.jsxs("tr",{className:"hover:bg-gray-50 dark:hover:bg-gray-900/30",children:[e.jsx("td",{className:"px-3 py-2 w-44",children:e.jsxs("div",{className:"flex items-center gap-1.5 min-w-0",children:[Z(w.role),e.jsx("span",{className:"font-medium text-foreground text-sm truncate block min-w-0",title:w.username||w.email,children:w.username||(w.email?.includes("@")?w.email.split("@")[0]:w.email)})]})}),e.jsx("td",{className:"px-3 py-2 text-center",children:be(w.role)}),e.jsx("td",{className:"px-3 py-2 text-center",children:e.jsx("span",{className:`inline-block w-2 h-2 rounded-full ${w.status==="active"?"bg-green-500":"bg-red-500"}`,title:w.status==="active"?"活跃":"已禁用"})}),e.jsx("td",{className:"px-3 py-2 text-right",children:e.jsxs("div",{className:"flex flex-col items-end gap-0.5",children:[e.jsx("span",{className:"font-mono text-xs text-foreground whitespace-nowrap",children:ne(c[w.uuid]?.total_cost)}),re(w)?e.jsx("button",{onClick:()=>de(w),className:"text-[11px] text-blue-600 dark:text-blue-400 hover:underline font-mono whitespace-nowrap leading-tight",title:"点击编辑额度限制",children:w.total_limit_usd!==null||w.daily_limit_usd!==null?e.jsxs("span",{children:["总:",q(w.total_limit_usd)," 日:",q(w.daily_limit_usd)]}):e.jsxs("span",{className:"flex items-center gap-0.5 justify-end text-muted-foreground",children:[e.jsx(Rr,{className:"w-3 h-3"}),"设置限额"]})}):e.jsx("span",{className:"text-xs text-muted-foreground",children:"-"})]})}),e.jsx("td",{className:"px-3 py-2 text-xs text-muted-foreground whitespace-nowrap",children:new Date(w.created_at).toLocaleDateString()}),e.jsx("td",{className:"px-3 py-2",children:e.jsxs("div",{className:"flex items-center justify-end gap-0.5",children:[a&&e.jsx(B,{variant:"ghost",size:"sm",className:"h-7 w-7 p-0",onClick:()=>a(w.uuid,w.username||w.email),title:"查看使用详情",children:e.jsx(no,{className:"w-4 h-4 text-indigo-500"})}),t&&w.role!=="super_admin"&&w.id!==o?.id&&e.jsx(B,{variant:"ghost",size:"sm",className:"h-7 w-7 p-0",onClick:()=>je(w),title:"修改角色",children:e.jsx(oo,{className:"w-4 h-4 text-purple-500"})}),w.role==="user"&&ze(w)&&e.jsx(B,{variant:"ghost",size:"sm",className:"h-7 w-7 p-0",onClick:()=>Ue(w),title:"功能权限",children:e.jsx(io,{className:"w-4 h-4 text-green-500"})}),w.role==="user"&&ze(w)&&e.jsx(B,{variant:"ghost",size:"sm",className:"h-7 w-7 p-0",onClick:()=>Le(w),title:"Agent 可见性",children:e.jsx(lo,{className:"w-4 h-4 text-cyan-500"})}),ze(w)&&e.jsxs(e.Fragment,{children:[w.username&&e.jsx(B,{variant:"ghost",size:"sm",className:"h-7 w-7 p-0",onClick:()=>he(w),disabled:b===w.id,title:"重置密码",children:e.jsx(br,{className:"w-4 h-4 text-blue-500"})}),e.jsx(B,{variant:"ghost",size:"sm",className:"h-7 w-7 p-0",onClick:()=>Y(w.id,w.status),disabled:b===w.id,title:w.status==="active"?"禁用用户":"启用用户",children:w.status==="active"?e.jsx(co,{className:"w-4 h-4 text-orange-500"}):e.jsx(mo,{className:"w-4 h-4 text-green-500"})}),e.jsx(B,{variant:"ghost",size:"sm",className:"h-7 w-7 p-0",onClick:()=>ae(w.id,w.username||w.email),disabled:b===w.id,title:"删除用户",children:b===w.id?e.jsx(Mt,{className:"w-4 h-4 text-red-500 animate-spin"}):e.jsx(mt,{className:"w-4 h-4 text-red-500"})})]})]})})]},w.id))})]})}),y&&e.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50",children:e.jsxs("div",{className:"bg-card border border-border rounded-lg shadow-lg w-full max-w-md p-6",children:[e.jsxs("div",{className:"flex items-center justify-between mb-4",children:[e.jsx("h4",{className:"text-lg font-semibold text-foreground",children:"创建用户"}),e.jsx("button",{onClick:F,className:"text-muted-foreground hover:text-foreground",children:e.jsx(Ge,{className:"w-5 h-5"})})]}),e.jsxs("form",{onSubmit:S,className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx("label",{htmlFor:"newUsername",className:"block text-sm font-medium text-foreground mb-1",children:"用户名"}),e.jsx("input",{type:"text",id:"newUsername",value:x,onChange:w=>j(w.target.value),className:"w-full px-3 py-2 border border-border rounded-md bg-background text-foreground focus:outline-none focus:ring-2 focus:ring-blue-500",placeholder:"请输入用户名",disabled:k})]}),e.jsxs("div",{children:[e.jsx("label",{htmlFor:"newPassword",className:"block text-sm font-medium text-foreground mb-1",children:"密码"}),e.jsx("input",{type:"password",id:"newPassword",value:h,onChange:w=>C(w.target.value),className:"w-full px-3 py-2 border border-border rounded-md bg-background text-foreground focus:outline-none focus:ring-2 focus:ring-blue-500",placeholder:"请输入密码",disabled:k})]}),m&&e.jsx("div",{className:"p-3 bg-red-100 dark:bg-red-900/20 border border-red-300 dark:border-red-800 rounded-md",children:e.jsx("p",{className:"text-sm text-red-700 dark:text-red-400",children:m})}),e.jsxs("div",{className:"flex justify-end gap-2",children:[e.jsx(B,{type:"button",variant:"outline",onClick:F,disabled:k,children:"取消"}),e.jsx(B,{type:"submit",disabled:k,children:k?"创建中...":"创建"})]})]})]})}),P&&e.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50",children:e.jsxs("div",{className:"bg-card border border-border rounded-lg shadow-lg w-full max-w-md p-6",children:[e.jsxs("div",{className:"flex items-center justify-between mb-4",children:[e.jsxs("h4",{className:"text-lg font-semibold text-foreground",children:["设置额度限制 - ",P.username||P.email]}),e.jsx("button",{onClick:me,className:"text-muted-foreground hover:text-foreground",children:e.jsx(Ge,{className:"w-5 h-5"})})]}),e.jsxs("form",{onSubmit:te,className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx("label",{htmlFor:"totalLimit",className:"block text-sm font-medium text-foreground mb-1",children:"总额度上限 (USD)"}),e.jsx("input",{type:"number",id:"totalLimit",step:"0.01",min:"0",value:A.total_limit_usd,onChange:w=>T(O=>({...O,total_limit_usd:w.target.value})),className:"w-full px-3 py-2 border border-border rounded-md bg-background text-foreground focus:outline-none focus:ring-2 focus:ring-blue-500",placeholder:"留空表示不限制",disabled:$}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"用户累计使用金额达到此限制后,将无法继续使用"})]}),e.jsxs("div",{children:[e.jsx("label",{htmlFor:"dailyLimit",className:"block text-sm font-medium text-foreground mb-1",children:"每日额度上限 (USD)"}),e.jsx("input",{type:"number",id:"dailyLimit",step:"0.01",min:"0",value:A.daily_limit_usd,onChange:w=>T(O=>({...O,daily_limit_usd:w.target.value})),className:"w-full px-3 py-2 border border-border rounded-md bg-background text-foreground focus:outline-none focus:ring-2 focus:ring-blue-500",placeholder:"留空表示不限制",disabled:$}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"用户每日使用金额达到此限制后,需等待次日重置"})]}),c[P.uuid]&&e.jsx("div",{className:"p-3 bg-gray-100 dark:bg-gray-800 rounded-md",children:e.jsxs("p",{className:"text-sm text-muted-foreground",children:["当前已使用:",e.jsx("span",{className:"font-mono text-foreground",children:ne(c[P.uuid]?.total_cost)})]})}),L&&e.jsx("div",{className:"p-3 bg-red-100 dark:bg-red-900/20 border border-red-300 dark:border-red-800 rounded-md",children:e.jsx("p",{className:"text-sm text-red-700 dark:text-red-400",children:L})}),e.jsxs("div",{className:"flex justify-end gap-2",children:[e.jsx(B,{type:"button",variant:"outline",onClick:me,disabled:$,children:"取消"}),e.jsx(B,{type:"submit",disabled:$,children:$?"保存中...":"保存"})]})]})]})}),K&&e.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50",children:e.jsxs("div",{className:"bg-card border border-border rounded-lg shadow-lg w-full max-w-md p-6",children:[e.jsxs("div",{className:"flex items-center justify-between mb-4",children:[e.jsxs("h4",{className:"text-lg font-semibold text-foreground",children:["重置密码 - ",K.username]}),e.jsx("button",{onClick:se,className:"text-muted-foreground hover:text-foreground",children:e.jsx(Ge,{className:"w-5 h-5"})})]}),e.jsxs("form",{onSubmit:Te,className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx("label",{htmlFor:"resetNewPassword",className:"block text-sm font-medium text-foreground mb-1",children:"新密码"}),e.jsx("input",{type:"password",id:"resetNewPassword",value:ie,onChange:w=>ge(w.target.value),className:"w-full px-3 py-2 border border-border rounded-md bg-background text-foreground focus:outline-none focus:ring-2 focus:ring-blue-500",placeholder:"请输入新密码(至少6个字符)",disabled:Ae})]}),e.jsxs("div",{children:[e.jsx("label",{htmlFor:"resetConfirmPassword",className:"block text-sm font-medium text-foreground mb-1",children:"确认新密码"}),e.jsx("input",{type:"password",id:"resetConfirmPassword",value:fe,onChange:w=>pe(w.target.value),className:"w-full px-3 py-2 border border-border rounded-md bg-background text-foreground focus:outline-none focus:ring-2 focus:ring-blue-500",placeholder:"请再次输入新密码",disabled:Ae})]}),Ce&&e.jsx("div",{className:"p-3 bg-red-100 dark:bg-red-900/20 border border-red-300 dark:border-red-800 rounded-md",children:e.jsx("p",{className:"text-sm text-red-700 dark:text-red-400",children:Ce})}),e.jsxs("div",{className:"flex justify-end gap-2",children:[e.jsx(B,{type:"button",variant:"outline",onClick:se,disabled:Ae,children:"取消"}),e.jsx(B,{type:"submit",disabled:Ae,children:Ae?"重置中...":"重置密码"})]})]})]})}),Be&&e.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50",children:e.jsxs("div",{className:"bg-card border border-border rounded-lg shadow-lg w-full max-w-md p-6",children:[e.jsxs("div",{className:"flex items-center justify-between mb-4",children:[e.jsxs("h4",{className:"text-lg font-semibold text-foreground",children:["修改角色 - ",Be.username||Be.email]}),e.jsx("button",{onClick:Ne,className:"text-muted-foreground hover:text-foreground",children:e.jsx(Ge,{className:"w-5 h-5"})})]}),e.jsxs("form",{onSubmit:N,className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"block text-sm font-medium text-foreground mb-2",children:"选择角色"}),e.jsxs("label",{className:"flex items-center gap-3 p-3 border border-border rounded-md cursor-pointer hover:bg-accent/50 transition-colors",children:[e.jsx("input",{type:"radio",name:"role",value:"admin",checked:we==="admin",onChange:w=>We(w.target.value),className:"text-blue-600",disabled:Pe}),e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Ft,{className:"w-4 h-4 text-blue-500"}),e.jsx("span",{className:"text-sm font-medium text-foreground",children:"管理员"})]}),e.jsx("p",{className:"text-xs text-muted-foreground mt-0.5",children:"可管理普通用户,但不能操作其他管理员"})]})]}),e.jsxs("label",{className:"flex items-center gap-3 p-3 border border-border rounded-md cursor-pointer hover:bg-accent/50 transition-colors",children:[e.jsx("input",{type:"radio",name:"role",value:"user",checked:we==="user",onChange:w=>We(w.target.value),className:"text-blue-600",disabled:Pe}),e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(vr,{className:"w-4 h-4 text-gray-400"}),e.jsx("span",{className:"text-sm font-medium text-foreground",children:"普通用户"})]}),e.jsx("p",{className:"text-xs text-muted-foreground mt-0.5",children:"受额度限制,无管理权限"})]})]})]}),ke&&e.jsx("div",{className:"p-3 bg-red-100 dark:bg-red-900/20 border border-red-300 dark:border-red-800 rounded-md",children:e.jsx("p",{className:"text-sm text-red-700 dark:text-red-400",children:ke})}),e.jsxs("div",{className:"flex justify-end gap-2",children:[e.jsx(B,{type:"button",variant:"outline",onClick:Ne,disabled:Pe,children:"取消"}),e.jsx(B,{type:"submit",disabled:Pe,children:Pe?"保存中...":"保存"})]})]})]})}),_e&&e.jsx(Ml,{user:_e,onClose:()=>Ue(null),onSaved:()=>Fe()}),He&&e.jsx(Ll,{user:He,onClose:()=>Le(null),onSaved:()=>Fe()})]})}function Al(){const[t,r]=s.useState(""),[a,o]=s.useState(""),[n,l]=s.useState(!0),[i,c]=s.useState(!1),[u,v]=s.useState(null);s.useEffect(()=>{g()},[]);const g=async()=>{try{const p=await ue.admin.getDefaultLimits();if(p.ok){const y=await p.json();r(y.default_total_limit_usd!==null?String(y.default_total_limit_usd):""),o(y.default_daily_limit_usd!==null?String(y.default_daily_limit_usd):"")}}catch(p){console.error("Error fetching default limits:",p)}finally{l(!1)}},b=async p=>{p.preventDefault(),c(!0),v(null);const y=t.trim()===""?null:parseFloat(t),f=a.trim()===""?null:parseFloat(a);if(y!==null&&(isNaN(y)||y<0)){v("error"),c(!1);return}if(f!==null&&(isNaN(f)||f<0)){v("error"),c(!1);return}try{(await ue.admin.updateDefaultLimits({default_total_limit_usd:y,default_daily_limit_usd:f})).ok?(v("success"),setTimeout(()=>v(null),2e3)):v("error")}catch(x){console.error("Error updating default limits:",x),v("error")}finally{c(!1)}};return n?e.jsx("div",{className:"text-center py-8 text-muted-foreground",children:"正在加载..."}):e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"text-lg font-semibold text-foreground",children:"新用户默认额度"}),e.jsx("p",{className:"text-sm text-muted-foreground mt-1",children:"新注册用户将自动应用此总额度限制,留空表示不限制。管理员账户不受此限制。"})]}),e.jsx("form",{onSubmit:b,className:"space-y-3",children:e.jsxs("div",{className:"flex items-end gap-3",children:[e.jsxs("div",{className:"flex-1 max-w-xs",children:[e.jsx("label",{className:"block text-sm font-medium text-foreground mb-1",children:"默认总额度上限 (USD)"}),e.jsx("input",{type:"number",step:"0.01",min:"0",value:t,onChange:p=>r(p.target.value),className:"w-full px-3 py-2 border border-border rounded-md bg-background text-foreground focus:outline-none focus:ring-2 focus:ring-blue-500",placeholder:"留空表示不限制",disabled:i})]}),e.jsxs("div",{className:"flex-1 max-w-xs",children:[e.jsx("label",{className:"block text-sm font-medium text-foreground mb-1",children:"默认日限制 (USD)"}),e.jsx("input",{type:"number",step:"0.01",min:"0",value:a,onChange:p=>o(p.target.value),className:"w-full px-3 py-2 border border-border rounded-md bg-background text-foreground focus:outline-none focus:ring-2 focus:ring-blue-500",placeholder:"留空表示不限制",disabled:i})]}),e.jsxs(B,{type:"submit",variant:"default",size:"sm",disabled:i,className:"flex items-center gap-2",children:[e.jsx(Ir,{className:"w-4 h-4"}),i?"保存中...":"保存"]}),u==="success"&&e.jsx("span",{className:"text-sm text-green-600",children:"已保存"}),u==="error"&&e.jsx("span",{className:"text-sm text-red-600",children:"请输入有效的正数"})]})}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"此设置仅影响新注册的用户,不会修改已有用户的额度。"})]})}const Dl=[{key:"claude_shell",label:"Claude Code Shell"},{key:"codex_shell",label:"Codex Shell"},{key:"gemini_shell",label:"Gemini CLI Shell"},{key:"submit_agent",label:"提交 Agent"},{key:"tab_models",label:"模型设置"},{key:"tab_skills",label:"技能设置"},{key:"tab_mcp",label:"MCP 服务器设置"},{key:"tab_permissions",label:"权限设置"},{key:"feishu_binding",label:"绑定飞书账号"},{key:"create_project",label:"新建项目"}];function Tl(){const[t,r]=s.useState({}),[a,o]=s.useState(!0),[n,l]=s.useState(!1),[i,c]=s.useState(null);s.useEffect(()=>{u()},[]);const u=async()=>{try{const b=await ue.admin.getDefaultPermissions();if(b.ok){const p=await b.json();r(p.permissions||{})}}catch(b){console.error("Error fetching default permissions:",b)}finally{o(!1)}},v=b=>{r(p=>({...p,[b]:!p[b]}))},g=async b=>{b.preventDefault(),l(!0),c(null);try{(await ue.admin.updateDefaultPermissions(t)).ok?(c("success"),setTimeout(()=>c(null),2e3)):c("error")}catch(p){console.error("Error updating default permissions:",p),c("error")}finally{l(!1)}};return a?e.jsx("div",{className:"text-center py-8 text-muted-foreground",children:"正在加载..."}):e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"text-lg font-semibold text-foreground",children:"新用户默认功能权限"}),e.jsx("p",{className:"text-sm text-muted-foreground mt-1",children:"配置新注册用户的默认功能权限。已设置个性化权限的用户不受此影响。管理员始终拥有全部权限。"})]}),e.jsxs("form",{onSubmit:g,className:"space-y-3",children:[e.jsx("div",{className:"space-y-1",children:Dl.map(b=>e.jsxs("label",{className:"flex items-center justify-between p-2.5 rounded-md hover:bg-accent/50 cursor-pointer transition-colors",children:[e.jsx("span",{className:"text-sm font-medium text-foreground",children:b.label}),e.jsx("button",{type:"button",role:"switch","aria-checked":t[b.key]!==!1,onClick:()=>v(b.key),className:`relative inline-flex h-5 w-9 flex-shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none ${t[b.key]!==!1?"bg-blue-600":"bg-gray-300 dark:bg-gray-600"}`,children:e.jsx("span",{className:`pointer-events-none inline-block h-4 w-4 transform rounded-full bg-white shadow ring-0 transition duration-200 ease-in-out ${t[b.key]!==!1?"translate-x-4":"translate-x-0"}`})})]},b.key))}),e.jsxs("div",{className:"flex items-center gap-3 pt-2",children:[e.jsxs(B,{type:"submit",variant:"default",size:"sm",disabled:n,className:"flex items-center gap-2",children:[e.jsx(Ir,{className:"w-4 h-4"}),n?"保存中...":"保存"]}),i==="success"&&e.jsx("span",{className:"text-sm text-green-600",children:"已保存"}),i==="error"&&e.jsx("span",{className:"text-sm text-red-600",children:"保存失败"})]})]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"此设置仅影响未设置个性化权限的用户和新注册用户。"})]})}function Rl(){const{showConfirm:t}=nt(),[r,a]=s.useState([]),[o,n]=s.useState(!0),[l,i]=s.useState(null),[c,u]=s.useState(!1),[v,g]=s.useState(""),[b,p]=s.useState(""),[y,f]=s.useState(!1),x=async()=>{try{const m=await ue.admin.getEmailDomains();if(m.ok){const E=await m.json();a(E.domains)}}catch(m){console.error("Error fetching email domains:",m)}finally{n(!1)}};s.useEffect(()=>{x()},[]);const j=async m=>{if(m.preventDefault(),p(""),!v){p("域名不能为空");return}if(!/^[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?)*\.[a-zA-Z]{2,}$/.test(v)){p("域名格式无效,例如: example.com");return}f(!0);try{const k=await ue.admin.addEmailDomain(v.toLowerCase()),U=await k.json();k.ok?(await x(),u(!1),g("")):p(U.error||"添加域名失败")}catch(k){console.error("Error adding domain:",k),p("网络错误,请稍后再试")}finally{f(!1)}},h=async(m,E)=>{if(await t(`确定要删除域名 "${E}"?删除后该域名的邮箱将无法注册。`,{title:"删除域名",variant:"danger",confirmText:"确认删除"})){i(m);try{(await ue.admin.removeEmailDomain(m)).ok&&a(r.filter(U=>U.id!==m))}catch(k){console.error("Error removing domain:",k)}finally{i(null)}}},C=()=>{u(!1),g(""),p("")};return o?e.jsx("div",{className:"text-center py-8 text-muted-foreground",children:"正在加载..."}):e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"text-lg font-semibold text-foreground",children:"邮箱域名白名单"}),e.jsx("p",{className:"text-sm text-muted-foreground mt-1",children:r.length===0?"未设置白名单,所有邮箱域名均可注册":"只有白名单内的域名邮箱可以注册"})]}),e.jsxs(B,{variant:"default",size:"sm",onClick:()=>u(!0),className:"flex items-center gap-2",children:[e.jsx(zt,{className:"w-4 h-4"}),"添加域名"]})]}),r.length>0?e.jsx("div",{className:"border border-gray-200 dark:border-gray-700 rounded-lg overflow-hidden",children:e.jsxs("table",{className:"w-full",children:[e.jsx("thead",{className:"bg-gray-50 dark:bg-gray-900/50",children:e.jsxs("tr",{children:[e.jsx("th",{className:"px-4 py-3 text-left text-sm font-medium text-muted-foreground",children:"域名"}),e.jsx("th",{className:"px-4 py-3 text-left text-sm font-medium text-muted-foreground",children:"添加时间"}),e.jsx("th",{className:"px-4 py-3 text-right text-sm font-medium text-muted-foreground",children:"操作"})]})}),e.jsx("tbody",{className:"divide-y divide-gray-200 dark:divide-gray-700",children:r.map(m=>e.jsxs("tr",{className:"hover:bg-gray-50 dark:hover:bg-gray-900/30",children:[e.jsx("td",{className:"px-4 py-3",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(zs,{className:"w-4 h-4 text-blue-500"}),e.jsx("span",{className:"font-medium text-foreground",children:m.domain})]})}),e.jsx("td",{className:"px-4 py-3 text-sm text-muted-foreground",children:new Date(m.created_at).toLocaleDateString()}),e.jsx("td",{className:"px-4 py-3",children:e.jsx("div",{className:"flex items-center justify-end",children:e.jsx(B,{variant:"ghost",size:"sm",onClick:()=>h(m.id,m.domain),disabled:l===m.id,title:"删除域名",children:e.jsx(mt,{className:"w-4 h-4 text-red-500"})})})})]},m.id))})]})}):e.jsxs("div",{className:"border border-dashed border-gray-300 dark:border-gray-600 rounded-lg p-8 text-center",children:[e.jsx(zs,{className:"w-12 h-12 text-muted-foreground mx-auto mb-4"}),e.jsx("p",{className:"text-muted-foreground",children:"暂无域名白名单,所有邮箱域名均可注册"}),e.jsx("p",{className:"text-sm text-muted-foreground mt-2",children:"添加域名后,只有白名单内的域名邮箱可以注册"})]}),c&&e.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50",children:e.jsxs("div",{className:"bg-card border border-border rounded-lg shadow-lg w-full max-w-md p-6",children:[e.jsxs("div",{className:"flex items-center justify-between mb-4",children:[e.jsx("h4",{className:"text-lg font-semibold text-foreground",children:"添加域名"}),e.jsx("button",{onClick:C,className:"text-muted-foreground hover:text-foreground",children:e.jsx(Ge,{className:"w-5 h-5"})})]}),e.jsxs("form",{onSubmit:j,className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx("label",{htmlFor:"newDomain",className:"block text-sm font-medium text-foreground mb-1",children:"邮箱域名"}),e.jsx("input",{type:"text",id:"newDomain",value:v,onChange:m=>g(m.target.value.toLowerCase()),className:"w-full px-3 py-2 border border-border rounded-md bg-background text-foreground focus:outline-none focus:ring-2 focus:ring-blue-500",placeholder:"例如: example.com",disabled:y}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"输入域名后,该域名下的所有邮箱(如 user@example.com)都可以注册"})]}),b&&e.jsx("div",{className:"p-3 bg-red-100 dark:bg-red-900/20 border border-red-300 dark:border-red-800 rounded-md",children:e.jsx("p",{className:"text-sm text-red-700 dark:text-red-400",children:b})}),e.jsxs("div",{className:"flex justify-end gap-2",children:[e.jsx(B,{type:"button",variant:"outline",onClick:C,disabled:y,children:"取消"}),e.jsx(B,{type:"submit",disabled:y,children:y?"添加中...":"添加"})]})]})]})})]})}const Ds={pending:{label:"待审核",className:"bg-amber-100 text-amber-700 dark:bg-amber-900/30 dark:text-amber-400",icon:ys},approved:{label:"已通过",className:"bg-green-100 text-green-700 dark:bg-green-900/30 dark:text-green-400",icon:Ta},rejected:{label:"已拒绝",className:"bg-red-100 text-red-700 dark:bg-red-900/30 dark:text-red-400",icon:Da}};function wa(t){return t?new Date(t).toLocaleString("zh-CN",{dateStyle:"short",timeStyle:"short"}):"-"}function Pl({submissionId:t}){const[r,a]=s.useState(null),[o,n]=s.useState(!0),[l,i]=s.useState(0);if(s.useEffect(()=>{G(`/api/agents/submissions/${t}/contents`).then(u=>u.json()).then(u=>{a(u.files||[]),i(0)}).catch(()=>a([])).finally(()=>n(!1))},[t]),o)return e.jsxs("div",{className:"flex items-center gap-1.5 py-3 text-xs text-gray-400",children:[e.jsx(ot,{className:"w-3 h-3 animate-spin"})," 加载文件内容..."]});if(!r||r.length===0)return e.jsx("p",{className:"text-xs text-gray-400 py-2",children:"无法读取文件内容"});const c=r[l];return e.jsxs("div",{className:"mt-3",children:[e.jsx("div",{className:"flex items-center gap-1 mb-2 flex-wrap",children:r.map((u,v)=>e.jsxs("button",{onClick:()=>i(v),className:`flex items-center gap-1 px-2 py-1 text-[11px] rounded border transition-colors ${v===l?"bg-blue-50 dark:bg-blue-900/20 border-blue-300 dark:border-blue-700 text-blue-700 dark:text-blue-300":"border-gray-200 dark:border-gray-700 text-gray-500 dark:text-gray-400 hover:border-gray-300"}`,children:[e.jsx(es,{className:"w-3 h-3"}),u.name]},u.name))}),e.jsx("pre",{className:"text-[11px] leading-relaxed bg-gray-900 dark:bg-gray-950 text-gray-100 rounded-lg p-3 overflow-auto max-h-72 whitespace-pre-wrap break-words",children:c?.content||""})]})}function _l(){const{showAlert:t}=nt(),[r,a]=s.useState([]),[o,n]=s.useState(!0),[l,i]=s.useState("pending"),[c,u]=s.useState(null),[v,g]=s.useState(null),[b,p]=s.useState(""),[y,f]=s.useState(null),[x,j]=s.useState(null),[h,C]=s.useState(!1);s.useEffect(()=>{m()},[l]);const m=async()=>{try{n(!0);const M=l?`/api/agents/submissions?status=${l}`:"/api/agents/submissions",A=await G(M);if(A.ok){const T=await A.json();a(T.submissions||[])}else t("加载提交列表失败",{variant:"error"})}catch(M){t(M.message,{variant:"error"})}finally{n(!1)}},E=M=>{j(M.id),C(!1)},k=async(M,A)=>{g(M.id);try{const T=await G(`/api/agents/submissions/${M.id}/approve`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({updateAllUsers:A})}),L=await T.json();if(T.ok){let W=`已通过审核,版本 v${L.version} 已发布到仓库。`;L.updatedUsersCount>0&&(W+=`
164
164
  另已强制更新 ${L.updatedUsersCount} 个已安装用户。`),t(W,{title:"审核通过"}),j(null),m()}else t(L.error||"操作失败",{variant:"error"})}catch(T){t(T.message,{variant:"error"})}finally{g(null)}},U=M=>{f(M.id),p("")},P=async M=>{g(M.id);try{const A=await G(`/api/agents/submissions/${M.id}/reject`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({reason:b})});if(A.ok)t("已拒绝该提交。",{title:"审核拒绝"}),f(null),m();else{const T=await A.json();t(T.error||"操作失败",{variant:"error"})}}catch(A){t(A.message,{variant:"error"})}finally{g(null)}};return e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("div",{className:"flex items-center gap-2",children:["pending","approved","rejected",""].map(M=>e.jsx("button",{onClick:()=>i(M),className:`px-3 py-1 text-xs rounded-full border transition-colors ${l===M?"bg-blue-500 text-white border-blue-500":"border-gray-200 dark:border-gray-700 text-gray-600 dark:text-gray-400 hover:border-blue-300"}`,children:M===""?"全部":Ds[M]?.label},M||"all"))}),e.jsxs(B,{variant:"outline",size:"sm",onClick:m,className:"flex items-center gap-1.5",children:[e.jsx(ot,{className:`w-3.5 h-3.5 ${o?"animate-spin":""}`}),"刷新"]})]}),o?e.jsx("div",{className:"text-center py-8 text-gray-400 text-sm",children:"加载中..."}):r.length===0?e.jsxs("div",{className:"text-center py-8 text-gray-400 text-sm border border-dashed border-gray-200 dark:border-gray-700 rounded-lg",children:["暂无",l?Ds[l]?.label:"","提交记录"]}):e.jsx("div",{className:"space-y-2",children:r.map(M=>{const A=Ds[M.status]||Ds.pending,T=A.icon,L=c===M.id,W=v===M.id,$=y===M.id,oe=x===M.id;return e.jsxs("div",{className:"border border-gray-200 dark:border-gray-700 rounded-lg overflow-hidden",children:[e.jsxs("div",{className:"flex items-center gap-3 p-3",children:[e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[e.jsx("span",{className:"font-medium text-sm text-gray-900 dark:text-gray-100",children:M.display_name}),e.jsx("span",{className:"text-xs text-gray-400 font-mono",children:M.agent_name}),e.jsxs(Ye,{className:`text-xs ${A.className} flex items-center gap-1`,children:[e.jsx(T,{className:"w-3 h-3"}),A.label]}),M.version&&e.jsxs("span",{className:"text-xs text-green-600 dark:text-green-400",children:["v",M.version]})]}),e.jsxs("div",{className:"flex items-center gap-4 mt-0.5 text-xs text-gray-400",children:[e.jsxs("span",{children:["提交人: ",M.username||M.email||`uid-${M.user_id}`]}),e.jsxs("span",{children:["提交时间: ",wa(M.submitted_at)]}),M.reviewed_at&&e.jsxs("span",{children:["审核时间: ",wa(M.reviewed_at)]})]})]}),e.jsxs("div",{className:"flex items-center gap-2 flex-shrink-0",children:[M.status==="pending"&&e.jsxs(e.Fragment,{children:[e.jsx(B,{size:"sm",className:"text-xs h-7 px-2.5 bg-green-600 hover:bg-green-700 text-white",onClick:()=>E(M),disabled:W,children:"通过"}),e.jsx(B,{size:"sm",variant:"outline",className:"text-xs h-7 px-2.5 text-red-600 border-red-200 hover:bg-red-50 dark:border-red-800 dark:hover:bg-red-900/20",onClick:()=>U(M),disabled:W,children:"拒绝"})]}),e.jsx("button",{onClick:()=>u(L?null:M.id),className:"text-gray-400 hover:text-gray-600 dark:hover:text-gray-300",children:L?e.jsx(Aa,{className:"w-4 h-4"}):e.jsx(ts,{className:"w-4 h-4"})})]})]}),L&&e.jsxs("div",{className:"border-t border-gray-100 dark:border-gray-700 bg-gray-50 dark:bg-gray-800/50 p-3 space-y-2",children:[M.description&&e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-medium text-gray-500 dark:text-gray-400 mb-1",children:"描述"}),e.jsx("p",{className:"text-xs text-gray-700 dark:text-gray-300",children:M.description})]}),M.update_notes&&e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-medium text-blue-500 dark:text-blue-400 mb-1",children:"更新说明"}),e.jsx("p",{className:"text-xs text-gray-700 dark:text-gray-300",children:M.update_notes})]}),M.status==="rejected"&&M.reject_reason&&e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-medium text-red-500 mb-1",children:"拒绝原因"}),e.jsx("p",{className:"text-xs text-red-600 dark:text-red-400",children:M.reject_reason})]}),e.jsx(Pl,{submissionId:M.id})]}),$&&e.jsxs("div",{className:"border-t border-red-100 dark:border-red-900/30 bg-red-50 dark:bg-red-900/10 p-3",children:[e.jsx("p",{className:"text-xs font-medium text-red-600 dark:text-red-400 mb-2",children:"填写拒绝原因"}),e.jsx("textarea",{value:b,onChange:K=>p(K.target.value),placeholder:"可选:填写拒绝原因告知提交者",rows:2,className:"w-full border border-red-200 dark:border-red-800 rounded px-2 py-1.5 text-xs bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 focus:outline-none resize-none mb-2"}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(B,{size:"sm",className:"text-xs h-7 px-2.5 bg-red-600 hover:bg-red-700 text-white",onClick:()=>P(M),disabled:W,children:W?"处理中...":"确认拒绝"}),e.jsx(B,{size:"sm",variant:"outline",className:"text-xs h-7 px-2.5",onClick:()=>f(null),disabled:W,children:"取消"})]})]}),oe&&e.jsxs("div",{className:"border-t border-green-100 dark:border-green-900/30 bg-green-50 dark:bg-green-900/10 p-3",children:[e.jsx("p",{className:"text-xs font-medium text-green-700 dark:text-green-400 mb-2",children:"确认通过审核?系统将自动递增版本并发布到仓库。"}),e.jsxs("label",{className:"flex items-start gap-2 mb-2 cursor-pointer select-none",children:[e.jsx("input",{type:"checkbox",checked:h,onChange:K=>C(K.target.checked),disabled:W,className:"mt-0.5 cursor-pointer"}),e.jsx("span",{className:"text-xs text-gray-700 dark:text-gray-300",children:"同时强制更新所有已安装该 Agent 的用户(将覆盖本地修改)"})]}),h&&e.jsxs("p",{className:"text-xs text-amber-600 dark:text-amber-400 mb-2 flex items-start gap-1",children:[e.jsx("span",{children:"⚠"}),e.jsx("span",{children:"此操作将强制覆盖所有用户对该 Agent 文件的本地修改,不可撤销。"})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(B,{size:"sm",className:"text-xs h-7 px-2.5 bg-green-600 hover:bg-green-700 text-white flex items-center gap-1.5",onClick:()=>k(M,h),disabled:W,children:W?e.jsxs(e.Fragment,{children:[e.jsx(ot,{className:"w-3 h-3 animate-spin"}),h?"更新中,请稍候...":"处理中..."]}):"确认通过"}),e.jsx(B,{size:"sm",variant:"outline",className:"text-xs h-7 px-2.5",onClick:()=>j(null),disabled:W,children:"取消"})]})]})]},M.id)})})]})}function $l({days:t,formatCost:r,formatNumber:a}){const[o,n]=s.useState(null),l=Math.max(...t.map(i=>i.cost||0),.001);return e.jsxs("div",{className:"relative",children:[e.jsx("div",{className:"flex items-end justify-between gap-1.5 h-40",children:t.map(i=>{const c=(i.cost||0)/l*100,u=new Date(i.date+"T12:00:00").toLocaleDateString("zh-CN",{month:"numeric",day:"numeric"});return e.jsxs("div",{className:"flex-1 flex flex-col items-center gap-1 h-full",onMouseEnter:()=>n(i),onMouseLeave:()=>n(null),children:[e.jsx("div",{className:"flex-1 w-full flex items-end",children:e.jsx("div",{className:"w-full rounded-t bg-gradient-to-t from-blue-500 to-blue-400 transition-all duration-300",style:{height:`${Math.max(c,i.cost>0?2:0)}%`}})}),e.jsx("span",{className:"text-[10px] text-muted-foreground whitespace-nowrap",children:u})]},i.date)})}),o&&e.jsxs("div",{className:"absolute -top-16 left-1/2 -translate-x-1/2 bg-popover border border-border rounded-lg px-3 py-2 text-xs shadow-lg z-10 whitespace-nowrap pointer-events-none",children:[e.jsx("div",{className:"font-medium text-foreground",children:o.date}),e.jsxs("div",{className:"text-muted-foreground",children:["费用:",r(o.cost)]}),e.jsxs("div",{className:"text-muted-foreground",children:["请求:",a(o.requests)]})]})]})}function zl({onBack:t}){const[r,a]=s.useState("week"),[o,n]=s.useState(null),[l,i]=s.useState(!0),[c,u]=s.useState(!1),v=async()=>{try{const h=await G(`/api/admin/usage/dashboard?period=${r}`);if(h.ok){const C=await h.json();n(C)}}catch(h){console.error("Error fetching dashboard data:",h)}finally{i(!1),u(!1)}};s.useEffect(()=>{i(!0),v()},[r]);const g=async()=>{try{u(!0),await G("/api/admin/usage/scan",{method:"POST"}),await v()}catch(h){console.error("Error triggering scan:",h),u(!1)}},b=h=>!h||h===0?"$0.00":h<.01?`$${h.toFixed(4)}`:`$${h.toFixed(2)}`,p=h=>h?h>=1e6?`${(h/1e6).toFixed(1)}M`:h>=1e3?`${(h/1e3).toFixed(1)}K`:h.toString():"0";if(l)return e.jsx("div",{className:"flex items-center justify-center h-64",children:e.jsx("div",{className:"text-muted-foreground",children:"加载仪表板中..."})});const{totals:y,dailyTrend:f,modelDistribution:x,topUsers:j}=o||{};return e.jsxs("div",{className:"space-y-6 p-6",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-4",children:[t&&e.jsxs(B,{variant:"ghost",size:"sm",onClick:t,children:[e.jsx(Vt,{className:"w-4 h-4 mr-2"}),"返回"]}),e.jsx("h2",{className:"text-2xl font-bold text-foreground",children:"使用情况仪表板"})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("div",{className:"flex items-center border border-gray-200 dark:border-gray-700 rounded-lg overflow-hidden",children:["week","month"].map(h=>e.jsx("button",{onClick:()=>a(h),className:`px-4 py-2 text-sm font-medium transition-colors ${r===h?"bg-primary text-primary-foreground":"bg-background text-foreground hover:bg-muted"}`,children:h==="week"?"周":"月"},h))}),e.jsx(B,{variant:"outline",size:"sm",onClick:g,disabled:c,title:"扫描新的使用数据",children:e.jsx(ot,{className:`w-4 h-4 ${c?"animate-spin":""}`})})]})]}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-4 gap-4",children:[e.jsx("div",{className:"bg-card border border-gray-200 dark:border-gray-700 rounded-lg p-4",children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"p-2 bg-green-100 dark:bg-green-900/30 rounded-lg",children:e.jsx(Rr,{className:"w-5 h-5 text-green-600 dark:text-green-400"})}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"总成本"}),e.jsx("p",{className:"text-2xl font-bold text-foreground",children:b(y?.totalCost)})]})]})}),e.jsx("div",{className:"bg-card border border-gray-200 dark:border-gray-700 rounded-lg p-4",children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"p-2 bg-blue-100 dark:bg-blue-900/30 rounded-lg",children:e.jsx(Pa,{className:"w-5 h-5 text-blue-600 dark:text-blue-400"})}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"活跃用户"}),e.jsxs("p",{className:"text-2xl font-bold text-foreground",children:[y?.activeUsers||0," / ",y?.totalUsers||0]})]})]})}),e.jsx("div",{className:"bg-card border border-gray-200 dark:border-gray-700 rounded-lg p-4",children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"p-2 bg-purple-100 dark:bg-purple-900/30 rounded-lg",children:e.jsx(Js,{className:"w-5 h-5 text-purple-600 dark:text-purple-400"})}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"总请求数"}),e.jsx("p",{className:"text-2xl font-bold text-foreground",children:p(y?.totalRequests)})]})]})}),e.jsx("div",{className:"bg-card border border-gray-200 dark:border-gray-700 rounded-lg p-4",children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"p-2 bg-orange-100 dark:bg-orange-900/30 rounded-lg",children:e.jsx(_a,{className:"w-5 h-5 text-orange-600 dark:text-orange-400"})}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"会话数"}),e.jsx("p",{className:"text-2xl font-bold text-foreground",children:p(y?.totalSessions)})]})]})})]}),e.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-6",children:[e.jsxs("div",{className:"bg-card border border-gray-200 dark:border-gray-700 rounded-lg p-4",children:[e.jsx("h3",{className:"text-lg font-semibold text-foreground mb-4",children:"每日成本趋势"}),f&&f.length>0?e.jsx($l,{days:f,formatCost:b,formatNumber:p}):e.jsx("div",{className:"text-center py-8 text-muted-foreground",children:"无可用的使用数据"})]}),e.jsxs("div",{className:"bg-card border border-gray-200 dark:border-gray-700 rounded-lg p-4",children:[e.jsx("h3",{className:"text-lg font-semibold text-foreground mb-4",children:"模型成本"}),x&&x.length>0?e.jsx("div",{className:"space-y-3",children:x.map(h=>{const C=x.reduce((P,M)=>P+(M.cost||0),0)||1,m=(h.cost||0)/C*100,E=(h.normalized_model||h.model||"").toLowerCase(),U=(P=>P.includes("codex")?"bg-orange-500":P.includes("opus")?"bg-purple-500":P.includes("sonnet")?"bg-blue-500":P.includes("haiku")?"bg-green-500":"bg-gray-500")(E);return e.jsxs("div",{className:"space-y-1",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-2 min-w-0",children:[e.jsx(Ye,{variant:"secondary",className:"truncate max-w-[200px]",title:h.model,children:h.model}),e.jsxs("span",{className:"text-sm text-muted-foreground whitespace-nowrap",children:[p(h.requests)," 请求"]})]}),e.jsx("span",{className:"text-sm font-mono text-foreground whitespace-nowrap ml-2",children:b(h.cost)})]}),e.jsx("div",{className:"h-2 bg-gray-100 dark:bg-gray-800 rounded overflow-hidden",children:e.jsx("div",{className:`h-full ${U} rounded`,style:{width:`${m}%`}})})]},h.model)})}):e.jsx("div",{className:"text-center py-8 text-muted-foreground",children:"无可用的使用数据"})]})]}),e.jsxs("div",{className:"bg-card border border-gray-200 dark:border-gray-700 rounded-lg p-4",children:[e.jsx("h3",{className:"text-lg font-semibold text-foreground mb-4",children:"用户成本"}),j&&j.length>0?e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b border-gray-200 dark:border-gray-700",children:[e.jsx("th",{className:"text-left py-2 text-sm font-medium text-muted-foreground",children:"排名"}),e.jsx("th",{className:"text-left py-2 text-sm font-medium text-muted-foreground",children:"用户名"}),e.jsx("th",{className:"text-right py-2 text-sm font-medium text-muted-foreground",children:"请求数"}),e.jsx("th",{className:"text-right py-2 text-sm font-medium text-muted-foreground",children:"成本"})]})}),e.jsx("tbody",{children:j.map((h,C)=>e.jsxs("tr",{className:"border-b border-gray-100 dark:border-gray-800 last:border-0",children:[e.jsxs("td",{className:"py-2 text-sm text-muted-foreground",children:["#",C+1]}),e.jsx("td",{className:"py-2 text-sm font-medium text-foreground",children:h.username}),e.jsx("td",{className:"py-2 text-sm text-right text-muted-foreground",children:p(h.total_requests)}),e.jsx("td",{className:"py-2 text-sm text-right font-mono text-foreground",children:b(h.total_cost)})]},h.user_uuid))})]})}):e.jsx("div",{className:"text-center py-8 text-muted-foreground",children:"无可用的使用数据"})]})]})}const ka={hex:{opus:"#a855f7",sonnet:"#3b82f6",haiku:"#22c55e",codex:"#f97316",default:"#6b7280"},tailwind:{opus:"bg-purple-500",sonnet:"bg-blue-500",haiku:"bg-green-500",codex:"bg-orange-500",default:"bg-gray-500"}};function Na(t=""){const r=t.toLowerCase();return r.includes("codex")?"codex":r.includes("opus")?"opus":r.includes("sonnet")?"sonnet":r.includes("haiku")?"haiku":"default"}function Hr(t){return!t||t===0?"$0.00":t<.01?`$${t.toFixed(4)}`:`$${t.toFixed(2)}`}function Mr(t){return t?t>=1e6?`${(t/1e6).toFixed(1)}M`:t>=1e3?`${(t/1e3).toFixed(1)}K`:t.toString():"0"}function Ol(t){const r=[];for(let a=t-1;a>=0;a--){const o=new Date;o.setDate(o.getDate()-a),r.push(o.toISOString().split("T")[0])}return r}function Fl({data:t}){const[r,a]=s.useState(null),o=140,n=o/2,l=o/2,i=56,c=t.reduce((g,b)=>g+(b.cost||0),0)||1;let u=-Math.PI/2;const v=t.map(g=>{const b=(g.cost||0)/c,p=b*2*Math.PI,y=u+p,f=n+i*Math.cos(u),x=l+i*Math.sin(u),j=n+i*Math.cos(y),h=l+i*Math.sin(y),C=p>Math.PI?1:0,m=b<.001?"":`M ${n} ${l} L ${f} ${x} A ${i} ${i} 0 ${C} 1 ${j} ${h} Z`,E=u+p/2,k={...g,pathD:m,ratio:b,midAngle:E,key:g.model};return u=y,k});return e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsxs("svg",{width:o,height:o,className:"flex-shrink-0",children:[v.map(g=>e.jsx("path",{d:g.pathD,fill:ka.hex[Na(g.model)],opacity:r===null||r===g.key?1:.4,stroke:"white",strokeWidth:"1.5",onMouseEnter:()=>a(g.key),onMouseLeave:()=>a(null),className:"cursor-pointer transition-opacity duration-150"},g.key)),t.length===0&&e.jsx("circle",{cx:n,cy:l,r:i,fill:"#e5e7eb"})]}),e.jsx("div",{className:"flex-1 space-y-1.5 min-w-0",children:v.map(g=>{const b=(g.ratio*100).toFixed(1),p=Na(g.model);return e.jsxs("div",{className:"flex items-center gap-2 min-w-0",onMouseEnter:()=>a(g.key),onMouseLeave:()=>a(null),children:[e.jsx("div",{className:`w-2.5 h-2.5 rounded-full flex-shrink-0 ${ka.tailwind[p]}`}),e.jsx("span",{className:"text-xs text-muted-foreground truncate flex-1",title:g.model,children:g.model}),e.jsxs("span",{className:"text-xs text-muted-foreground flex-shrink-0",children:[b,"%"]}),e.jsx("span",{className:"text-xs font-mono text-foreground flex-shrink-0 w-14 text-right",children:Hr(g.cost)})]},g.key)})})]})}function Wl({days:t}){const[r,a]=s.useState(null),o=Math.max(...t.map(n=>n.cost||0),.001);return e.jsxs("div",{className:"relative",children:[e.jsx("div",{className:"flex items-end justify-between gap-1.5 h-40",children:t.map(n=>{const l=(n.cost||0)/o*100,i=new Date(n.date+"T12:00:00").toLocaleDateString("zh-CN",{month:"numeric",day:"numeric"});return e.jsxs("div",{className:"flex-1 flex flex-col items-center gap-1 h-full",onMouseEnter:()=>a(n),onMouseLeave:()=>a(null),children:[e.jsx("div",{className:"flex-1 w-full flex items-end relative",children:e.jsx("div",{className:"w-full rounded-t bg-gradient-to-t from-blue-500 to-blue-400 transition-all duration-300 min-h-[2px]",style:{height:`${Math.max(l,n.cost>0?2:0)}%`}})}),e.jsx("span",{className:"text-[10px] text-muted-foreground whitespace-nowrap",children:i})]},n.date)})}),r&&e.jsxs("div",{className:"absolute -top-16 left-1/2 -translate-x-1/2 bg-popover border border-border rounded-lg px-3 py-2 text-xs shadow-lg z-10 whitespace-nowrap pointer-events-none",children:[e.jsx("div",{className:"font-medium text-foreground",children:r.date}),e.jsxs("div",{className:"text-muted-foreground",children:["费用:",Hr(r.cost)]}),e.jsxs("div",{className:"text-muted-foreground",children:["请求:",Mr(r.requests)]})]})]})}function Ul({userUuid:t,username:r,onBack:a}){const[o,n]=s.useState(null),[l,i]=s.useState(!0);s.useEffect(()=>{(async()=>{try{const b=await G(`/api/admin/usage/users/${t}?period=week`);b.ok&&n(await b.json())}catch(b){console.error("Error fetching user usage:",b)}finally{i(!1)}})()},[t]);const c=Ol(7),u={};(o?.dailyTrend||[]).forEach(g=>{u[g.date]=g});const v=c.map(g=>({date:g,cost:u[g]?.cost||0,requests:u[g]?.requests||0}));return l?e.jsx("div",{className:"flex items-center justify-center h-64",children:e.jsx("div",{className:"text-muted-foreground",children:"加载用户数据中..."})}):e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsxs(B,{variant:"ghost",size:"sm",onClick:a,children:[e.jsx(Vt,{className:"w-4 h-4 mr-2"}),"返回"]}),e.jsxs("div",{children:[e.jsx("h2",{className:"text-xl font-bold text-foreground",children:r}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"最近 7 天使用情况"})]})]}),e.jsxs("div",{className:"grid grid-cols-3 gap-4",children:[e.jsx("div",{className:"bg-card border border-gray-200 dark:border-gray-700 rounded-lg p-4",children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"p-2 bg-green-100 dark:bg-green-900/30 rounded-lg",children:e.jsx(Rr,{className:"w-5 h-5 text-green-600 dark:text-green-400"})}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"累计成本"}),e.jsx("p",{className:"text-xl font-bold text-foreground",children:Hr(o?.totalCost)})]})]})}),e.jsx("div",{className:"bg-card border border-gray-200 dark:border-gray-700 rounded-lg p-4",children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"p-2 bg-purple-100 dark:bg-purple-900/30 rounded-lg",children:e.jsx(Js,{className:"w-5 h-5 text-purple-600 dark:text-purple-400"})}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"累计请求数"}),e.jsx("p",{className:"text-xl font-bold text-foreground",children:Mr(o?.totalRequests)})]})]})}),e.jsx("div",{className:"bg-card border border-gray-200 dark:border-gray-700 rounded-lg p-4",children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"p-2 bg-orange-100 dark:bg-orange-900/30 rounded-lg",children:e.jsx(_a,{className:"w-5 h-5 text-orange-600 dark:text-orange-400"})}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"累计会话数"}),e.jsx("p",{className:"text-xl font-bold text-foreground",children:Mr(o?.totalSessions)})]})]})})]}),e.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-6",children:[e.jsxs("div",{className:"bg-card border border-gray-200 dark:border-gray-700 rounded-lg p-4",children:[e.jsx("h3",{className:"text-base font-semibold text-foreground mb-4",children:"最近 7 天每日消耗"}),e.jsx(Wl,{days:v})]}),e.jsxs("div",{className:"bg-card border border-gray-200 dark:border-gray-700 rounded-lg p-4",children:[e.jsx("h3",{className:"text-base font-semibold text-foreground mb-4",children:"模型分布"}),o?.modelDistribution&&o.modelDistribution.length>0?e.jsx(Fl,{data:o.modelDistribution}):e.jsx("div",{className:"flex items-center justify-center h-36 text-muted-foreground text-sm",children:"暂无使用数据"})]})]})]})}function Bl({isOpen:t,onClose:r}){const{isAdmin:a,isSuperAdmin:o}=ht(),[n,l]=s.useState("users"),[i,c]=s.useState(!1),[u,v]=s.useState(null);if(!t||!a)return null;const g=[{id:"users",label:"用户管理",icon:Pa},{id:"system",label:"系统设置",icon:po},{id:"agents",label:"Agent 审核",icon:Lt}],b=p=>{l(p),c(!1),v(null)};return e.jsx("div",{className:"modal-backdrop fixed inset-0 flex items-center justify-center z-[9999] md:p-4 bg-background/95",children:e.jsxs("div",{className:"bg-background border border-border md:rounded-lg shadow-xl w-full md:max-w-5xl h-full md:h-[90vh] flex flex-col",children:[e.jsxs("div",{className:"flex items-center justify-between p-4 md:p-6 border-b border-border flex-shrink-0",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(Ft,{className:"w-5 h-5 md:w-6 md:h-6 text-blue-600"}),e.jsx("h2",{className:"text-lg md:text-xl font-semibold text-foreground",children:"管理"})]}),e.jsx(B,{variant:"ghost",size:"sm",onClick:r,className:"text-muted-foreground hover:text-foreground touch-manipulation",children:e.jsx(Ge,{className:"w-5 h-5"})})]}),e.jsxs("div",{className:"flex-1 flex flex-col md:flex-row overflow-hidden",children:[e.jsx("div",{className:"md:hidden border-b border-border",children:e.jsx("div",{className:"flex px-4",children:g.map(p=>e.jsx("button",{onClick:()=>b(p.id),className:`px-4 py-3 text-sm font-medium border-b-2 transition-colors ${n===p.id?"border-blue-600 text-blue-600 dark:text-blue-400":"border-transparent text-muted-foreground hover:text-foreground"}`,children:p.label},p.id))})}),e.jsx("div",{className:"hidden md:flex flex-col w-48 border-r border-border p-3 gap-1 flex-shrink-0",children:g.map(p=>{const y=p.icon;return e.jsxs("button",{onClick:()=>b(p.id),className:`flex items-center gap-2 px-3 py-2 rounded-md text-sm font-medium transition-colors ${n===p.id?"bg-accent text-foreground":"text-muted-foreground hover:text-foreground hover:bg-accent/50"}`,children:[e.jsx(y,{className:"w-4 h-4"}),p.label]},p.id)})}),e.jsxs("div",{className:"flex-1 overflow-y-auto p-4 md:p-6",children:[n==="users"&&e.jsx("div",{className:"space-y-6 md:space-y-8",children:i?e.jsx(zl,{onBack:()=>c(!1)}):u?e.jsx(Ul,{userUuid:u.uuid,username:u.username,onBack:()=>v(null)}):e.jsx(Il,{isSuperAdmin:o,onNavigateToUsage:()=>c(!0),onViewUser:(p,y)=>v({uuid:p,username:y})})}),n==="system"&&e.jsxs("div",{className:"space-y-6 md:space-y-8",children:[e.jsx(Al,{}),e.jsx("div",{className:"border-t border-gray-200 dark:border-gray-700 pt-6 md:pt-8",children:e.jsx(Tl,{})}),e.jsx(Rl,{})]}),n==="agents"&&e.jsx(_l,{})]})]})]})})}function Hl({isOpen:t,limitType:r,onClose:a}){if(!t)return null;const o=r==="total_limit_exceeded";return e.jsx("div",{className:"fixed inset-0 bg-black/60 flex items-center justify-center z-[100]",children:e.jsxs("div",{className:"bg-card border border-border rounded-lg shadow-xl w-full max-w-md p-6 mx-4 animate-in fade-in zoom-in-95 duration-200",children:[e.jsxs("div",{className:"flex items-start justify-between mb-4",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"w-12 h-12 bg-red-100 dark:bg-red-900/30 rounded-full flex items-center justify-center flex-shrink-0",children:e.jsx($t,{className:"w-6 h-6 text-red-600 dark:text-red-400"})}),e.jsx("div",{children:e.jsx("h3",{className:"text-lg font-semibold text-foreground",children:o?"使用上限已达到":"今日使用上限已达到"})})]}),e.jsx("button",{onClick:a,className:"p-1 rounded-md text-muted-foreground hover:text-foreground hover:bg-muted transition-colors",children:e.jsx(Ge,{className:"w-5 h-5"})})]}),e.jsx("div",{className:"mb-6 text-muted-foreground space-y-3",children:o?e.jsxs(e.Fragment,{children:[e.jsx("p",{children:"您的账户已达到使用总额度上限。"}),e.jsx("p",{className:"text-sm",children:"请联系管理员提升您的额度限制,以继续使用 Claude 服务。"})]}):e.jsxs(e.Fragment,{children:[e.jsx("p",{children:"您已达到今日使用额度上限。"}),e.jsx("p",{className:"text-sm",children:"您可以等待明日额度重置后继续使用,或联系管理员提升您的每日额度限制。"})]})}),e.jsx("div",{className:"mb-6 p-3 bg-amber-50 dark:bg-amber-900/20 border border-amber-200 dark:border-amber-800 rounded-lg",children:e.jsx("p",{className:"text-sm text-amber-700 dark:text-amber-300",children:o?e.jsxs(e.Fragment,{children:[e.jsx("strong",{children:"提示:"}),"请联系您的管理员,申请提升使用额度。"]}):e.jsxs(e.Fragment,{children:[e.jsx("strong",{children:"提示:"}),"每日额度将在次日 00:00 自动重置,您也可以联系管理员调整限额。"]})})}),e.jsx("div",{className:"flex justify-end",children:e.jsx(B,{variant:"default",onClick:a,children:"我知道了"})})]})})}const Jl=[{type:"user",content:"为什么上周的活跃用户数突然下降了 12%?",delay:1500},{type:"system",content:null,temporary:!0,delay:2e3},{type:"system",content:`正在分析最近 7 天的用户行为数据与服务端日志...
165
165
 
166
166
  经排查,主要原因如下: