Files
mai-bot/dashboard/dist/assets/prompts-Ck1GtTX1.js

2 lines
9.0 KiB
JavaScript

import{r,j as t}from"./router-zNjPR4CY.js";import{C as Q}from"./CodeEditor-4JcGD1og.js";import{B as f}from"./badge-CDs67obV.js";import{x as v,p as j,E as ie,B as g,g as F,C as q,b as J,d as K,I as oe,S as ce,f as le,D as de,i as me,j as ue,k as pe,l as xe}from"./index-CuOHsLf7.js";import{S as he,a as fe,b as ge,c as ve,d as je}from"./select-DGqIoF9r.js";import{S as Ne}from"./separator-B_DSOSdz.js";import{R as Ce,x as ye,a7 as we,a0 as Se,ah as Pe,F as be,S as Fe,L}from"./icons-DTcdLw9j.js";import"./utils-DjBw3JGv.js";import"./misc-BwRzHX8c.js";import"./radix-C-ZuImoP.js";const N="/api/webui/config/prompts";async function $e(){const s=await v(N);return j(s)}async function Re(s,i){const l=await v(`${N}/${encodeURIComponent(s)}/${encodeURIComponent(i)}`);return j(l)}async function De(s,i){const l=await v(`${N}/${encodeURIComponent(s)}/${encodeURIComponent(i)}/default`);return j(l)}async function ke(s,i,l){const a=await v(`${N}/${encodeURIComponent(s)}/${encodeURIComponent(i)}`,{method:"PUT",body:JSON.stringify({content:l})});return j(a)}async function ze(s,i){const l=await v(`${N}/${encodeURIComponent(s)}/${encodeURIComponent(i)}`,{method:"DELETE"});return j(l)}function W(s){return s<1024?`${s} B`:s<1024*1024?`${(s/1024).toFixed(1)} KB`:`${(s/1024/1024).toFixed(1)} MB`}function Oe(){const{toast:s}=ie(),[i,l]=r.useState(null),[a,U]=r.useState("zh-CN"),[n,C]=r.useState(""),[$,p]=r.useState(""),[G,y]=r.useState(""),[w,B]=r.useState(!0),[S,T]=r.useState(!1),[A,_]=r.useState(!1),[H,M]=r.useState(!1),[R,O]=r.useState(!1),[X,P]=r.useState(!1),[Y,Z]=r.useState(""),[D,V]=r.useState(""),[d,ee]=r.useState(!1),k=$!==G,u=r.useMemo(()=>!i||!a?[]:i.files[a]??[],[i,a]),x=r.useMemo(()=>d?u:u.filter(e=>!e.advanced),[u,d]),z=r.useMemo(()=>{const e=D.trim().toLowerCase();return e?x.filter(o=>[o.name,o.display_name,o.description].join(" ").toLowerCase().includes(e)):x},[x,D]),m=u.find(e=>e.name===n),E=m?.customized??!1;r.useEffect(()=>{!n||d||!u.find(o=>o.name===n)?.advanced||C(x[0]?.name??"")},[n,u,d,x]);const h=r.useCallback(async()=>{try{B(!0);const e=await $e();if(!e.success){s({title:"加载 Prompt 目录失败",description:e.error,variant:"destructive"});return}l(e.data);const o=a&&e.data.languages.includes(a)?a:e.data.languages.includes("zh-CN")?"zh-CN":e.data.languages[0]??"";U(o);const c=o?e.data.files[o]??[]:[],I=c.filter(b=>!b.advanced);C(b=>c.some(re=>re.name===b)?b:I[0]?.name??c[0]?.name??"")}catch(e){s({title:"加载 Prompt 目录失败",description:e.message,variant:"destructive"})}finally{B(!1)}},[a,s]);r.useEffect(()=>{h()},[h]),r.useEffect(()=>{if(!a||!n){p(""),y("");return}let e=!1;return(async()=>{try{T(!0);const c=await Re(a,n);if(e)return;if(!c.success){s({title:"读取 Prompt 失败",description:c.error,variant:"destructive"});return}p(c.data.content),y(c.data.content)}catch(c){e||s({title:"读取 Prompt 失败",description:c.message,variant:"destructive"})}finally{e||T(!1)}})(),()=>{e=!0}},[n,a,s]);const te=e=>{U(e),V("");const o=i?.files[e]??[],c=d?o:o.filter(I=>!I.advanced);C(c[0]?.name??"")},se=async()=>{if(!(!a||!n))try{_(!0);const e=await ke(a,n,$);if(!e.success){s({title:"保存 Prompt 失败",description:e.error,variant:"destructive"});return}p(e.data.content),y(e.data.content),s({title:"Prompt 已保存",description:`${a}/${n}`}),h()}catch(e){s({title:"保存 Prompt 失败",description:e.message,variant:"destructive"})}finally{_(!1)}},ae=async()=>{if(!(!a||!n))try{O(!0),P(!0);const e=await De(a,n);if(!e.success){s({title:"读取默认 Prompt 失败",description:e.error,variant:"destructive"}),P(!1);return}Z(e.data.content)}catch(e){s({title:"读取默认 Prompt 失败",description:e.message,variant:"destructive"}),P(!1)}finally{O(!1)}},ne=async()=>{if(!(!a||!n||!E))try{M(!0);const e=await ze(a,n);if(!e.success){s({title:"恢复默认 Prompt 失败",description:e.error,variant:"destructive"});return}p(e.data.content),y(e.data.content),s({title:"已恢复默认 Prompt",description:`${a}/${n}`}),h()}catch(e){s({title:"恢复默认 Prompt 失败",description:e.message,variant:"destructive"})}finally{M(!1)}};return t.jsxs("div",{className:"flex h-[calc(100vh-140px)] flex-col gap-4 p-4 sm:p-6",children:[t.jsxs("div",{className:"flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between",children:[t.jsxs("div",{children:[t.jsx("h1",{className:"text-xl font-bold sm:text-2xl md:text-3xl",children:"Prompt 管理"}),t.jsx("p",{className:"mt-1 text-sm text-muted-foreground",children:"编辑 prompts 目录下不同语言的系统提示词模板"})]}),t.jsxs("div",{className:"flex flex-wrap items-center gap-2",children:[t.jsxs(he,{value:a,onValueChange:te,disabled:w,children:[t.jsx(fe,{className:"w-[160px]",children:t.jsx(ge,{placeholder:"选择语言"})}),t.jsx(ve,{children:(i?.languages??[]).map(e=>t.jsx(je,{value:e,children:e},e))})]}),t.jsxs(g,{variant:"outline",size:"sm",onClick:()=>{h()},disabled:w,children:[t.jsx(Ce,{className:F("mr-2 h-4 w-4",w&&"animate-spin")}),"刷新"]}),t.jsxs(g,{variant:d?"default":"outline",size:"sm",onClick:()=>ee(e=>!e),children:[t.jsx(ye,{className:"mr-2 h-4 w-4"}),d?"隐藏高级":"显示高级"]}),t.jsxs(g,{variant:"outline",size:"sm",onClick:ne,disabled:!E||H||S||!n,children:[t.jsx(we,{className:F("mr-2 h-4 w-4",H&&"animate-spin")}),"恢复默认"]}),t.jsxs(g,{variant:"outline",size:"sm",onClick:ae,disabled:R||S||!n,children:[t.jsx(Se,{className:F("mr-2 h-4 w-4",R&&"animate-pulse")}),"查看默认"]}),t.jsxs(g,{size:"sm",onClick:se,disabled:!k||A||S||!n,children:[t.jsx(Pe,{className:"mr-2 h-4 w-4"}),A?"保存中":k?"保存":"已保存"]})]})]}),t.jsxs("div",{className:"grid min-h-0 flex-1 gap-4 lg:grid-cols-[18rem_minmax(0,1fr)]",children:[t.jsxs(q,{className:"min-h-0 overflow-hidden",children:[t.jsxs(J,{className:"space-y-3 pb-3",children:[t.jsxs(K,{className:"flex items-center gap-2 text-sm",children:[t.jsx(be,{className:"h-4 w-4"}),"Prompt 文件",t.jsx(f,{variant:"secondary",className:"ml-auto",children:z.length})]}),t.jsxs("div",{className:"relative",children:[t.jsx(Fe,{className:"pointer-events-none absolute left-2 top-2.5 h-4 w-4 text-muted-foreground"}),t.jsx(oe,{value:D,onChange:e=>V(e.target.value),placeholder:"搜索文件",className:"pl-8"})]})]}),t.jsx(Ne,{}),t.jsx(ce,{className:"h-full",children:t.jsx("div",{className:"space-y-1 p-2",children:w?t.jsxs("div",{className:"flex items-center justify-center gap-2 p-6 text-sm text-muted-foreground",children:[t.jsx(L,{className:"h-4 w-4 animate-spin"}),"加载中"]}):z.length>0?z.map(e=>t.jsxs("button",{type:"button",onClick:()=>C(e.name),className:F("w-full rounded-md px-3 py-2 text-left text-sm transition-colors","hover:bg-accent hover:text-accent-foreground",n===e.name?"bg-accent text-accent-foreground":"text-muted-foreground"),children:[t.jsxs("div",{className:"flex items-center gap-2",children:[t.jsx("div",{className:"truncate font-medium",title:e.display_name||e.name,children:e.display_name||e.name}),e.advanced&&t.jsx(f,{variant:"outline",className:"shrink-0 text-[10px]",children:"高级"}),e.customized&&t.jsx(f,{variant:"secondary",className:"shrink-0 text-[10px]",children:"自定义"})]}),t.jsxs("div",{className:"mt-0.5 truncate text-xs text-muted-foreground",children:[e.name," · ",W(e.size)]}),e.description&&t.jsx("div",{className:"mt-1 line-clamp-2 text-xs text-muted-foreground",children:e.description})]},e.name)):t.jsx("div",{className:"p-6 text-center text-sm text-muted-foreground",children:"没有可编辑的 Prompt 文件"})})})]}),t.jsxs(q,{className:"min-h-0 overflow-hidden",children:[t.jsx(J,{className:"flex flex-row items-center justify-between gap-3 space-y-0 pb-3",children:t.jsxs("div",{className:"min-w-0",children:[t.jsxs(K,{className:"flex items-center gap-2 truncate text-sm",children:[t.jsx("span",{className:"truncate",children:m?.display_name||n||"未选择文件"}),m?.advanced&&t.jsx(f,{variant:"outline",className:"shrink-0",children:"高级"}),E&&t.jsx(f,{variant:"secondary",className:"shrink-0",children:"自定义"})]}),t.jsxs("p",{className:"mt-1 text-xs text-muted-foreground",children:[a,m?` · ${W(m.size)}`:"",k?" · 有未保存修改":""]}),m?.description&&t.jsx("p",{className:"mt-1 line-clamp-2 text-xs text-muted-foreground",children:m.description})]})}),t.jsx(le,{className:"min-h-0 p-0",children:S?t.jsxs("div",{className:"flex h-[calc(100vh-290px)] items-center justify-center gap-2 text-sm text-muted-foreground",children:[t.jsx(L,{className:"h-4 w-4 animate-spin"}),"读取中"]}):t.jsx(Q,{value:$,onChange:p,language:"text",height:"calc(100vh - 290px)",minHeight:"520px",placeholder:"选择一个 Prompt 文件后开始编辑"})})]})]}),t.jsx(de,{open:X,onOpenChange:P,children:t.jsxs(me,{className:"max-w-[min(96vw,1100px)]",children:[t.jsxs(ue,{children:[t.jsx(pe,{children:"默认 Prompt"}),t.jsxs(xe,{children:[a,"/",n," 的内置模板,只读显示,不会修改或删除自定义内容。"]})]}),R?t.jsxs("div",{className:"flex h-[520px] items-center justify-center gap-2 text-sm text-muted-foreground",children:[t.jsx(L,{className:"h-4 w-4 animate-spin"}),"读取中"]}):t.jsx(Q,{value:Y,readOnly:!0,language:"text",height:"min(62vh, 620px)",minHeight:"420px"})]})})]})}export{Oe as PromptManagementPage};