import{r as n,j as e}from"./router-zNjPR4CY.js";import{B as oe}from"./badge-CDs67obV.js";import{x as Z,z as J,p as Q,al as ce,E as de,B as f,g as B,I as xe,S as M,T as me,O as he,P as U,R as H}from"./index-CuOHsLf7.js";import{S as K,a as V,b as O,c as q,d as p}from"./select-DGqIoF9r.js";import{R as ue,S as fe,ad as pe,c as ge,F as D,aK as je,ag as ve}from"./icons-DTcdLw9j.js";import"./utils-DjBw3JGv.js";import"./misc-BwRzHX8c.js";import"./radix-C-ZuImoP.js";const $="/api/webui/reasoning-process";async function be(a){const l=new URLSearchParams;l.set("stage",a.stage??"planner"),l.set("session",a.session??"auto"),l.set("search",a.search??""),l.set("page",String(a.page??1)),l.set("page_size",String(a.pageSize));const d=await Z(`${$}/files?${l}`,{cache:"no-store"});return J(await Q(d))}async function Ne(a){const l=await Z(`${$}/file?path=${encodeURIComponent(a)}`,{cache:"no-store"});return J(await Q(l))}async function we(a){return ce(`${$}/html?path=${encodeURIComponent(a)}`)}const G=50,g="auto";function W(a,l){const d=a||l*1e3;return new Date(d).toLocaleString("zh-CN",{month:"2-digit",day:"2-digit",hour:"2-digit",minute:"2-digit",second:"2-digit"})}function X(a){return a<1024?`${a} B`:a<1024*1024?`${(a/1024).toFixed(1)} KB`:`${(a/1024/1024).toFixed(1)} MB`}function Ee(){const{toast:a}=de(),[l,d]=n.useState([]),[T,Y]=n.useState([]),[k,ee]=n.useState([]),[o,se]=n.useState("planner"),[x,j]=n.useState(g),[v,te]=n.useState(""),[m,b]=n.useState(1),[ae,ne]=n.useState(0),[E,re]=n.useState(0),[t,N]=n.useState(null),[u,w]=n.useState(""),[le,S]=n.useState("text"),[R,I]=n.useState(""),[c,F]=n.useState(!1),[y,L]=n.useState(!1),[z,A]=n.useState(null),_=Math.max(1,Math.ceil(E/G));n.useEffect(()=>{let s=!1;async function h(){F(!0),A(null);try{const r=await be({stage:o,session:x,search:v,page:m,pageSize:G});if(s)return;d(r.items),Y(r.stages),ee(r.sessions),r.selected_session&&r.selected_session!==x&&j(r.selected_session),re(r.total),N(i=>i&&r.items.some(P=>P.stem===i.stem&&P.stage===i.stage&&P.session_id===i.session_id)?i:r.items[0]??null)}catch(r){s||A(r instanceof Error?r.message:"加载推理过程失败")}finally{s||F(!1)}}return h(),()=>{s=!0}},[m,ae,v,x,o]),n.useEffect(()=>{let s=!1;async function h(){if(!t?.text_path){w("");return}L(!0);try{const i=await Ne(t.text_path);s||w(i.content)}catch(i){s||w(i instanceof Error?i.message:"读取文本失败")}finally{s||L(!1)}}async function r(){if(!t?.html_path){I("");return}const i=await we(t.html_path);s||I(i)}return t?.html_path&&!t.text_path?S("html"):S("text"),h(),r(),()=>{s=!0}},[t]);function C(s){s(),b(1)}async function ie(){if(!u||y){a({title:"暂无可复制内容",description:"请先选择一条包含 txt 的 prompt 记录",variant:"destructive"});return}try{await navigator.clipboard.writeText(u),a({title:"已复制完整 Prompt",description:t?`${t.stage}/${t.session_id}/${t.stem}`:void 0})}catch(s){a({title:"复制失败",description:s instanceof Error?s.message:"请手动选择文本复制",variant:"destructive"})}}return e.jsxs("div",{className:"flex h-full min-h-0 flex-col gap-3 overflow-hidden p-3 lg:p-4",children:[e.jsxs("div",{className:"flex flex-col gap-2 lg:flex-row lg:items-center lg:justify-between",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-xl font-semibold tracking-normal text-foreground",children:"推理过程"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"浏览 logs/maisaka_prompt 下的 prompt 记录"})]}),e.jsxs(f,{variant:"outline",size:"sm",onClick:()=>ne(s=>s+1),disabled:c,children:[e.jsx(ue,{className:B("h-4 w-4",c&&"animate-spin")}),"刷新"]})]}),e.jsxs("div",{className:"grid flex-shrink-0 grid-cols-1 gap-2 md:grid-cols-[180px_240px_1fr]",children:[e.jsxs(K,{value:o,onValueChange:s=>C(()=>{se(s),j(g),N(null)}),children:[e.jsx(V,{children:e.jsx(O,{placeholder:"阶段"})}),e.jsxs(q,{children:[!T.includes(o)&&e.jsx(p,{value:o,children:o}),T.map(s=>e.jsx(p,{value:s,children:s},s))]})]}),e.jsxs(K,{value:x,onValueChange:s=>C(()=>j(s)),disabled:k.length===0&&c,children:[e.jsx(V,{children:e.jsx(O,{placeholder:"会话"})}),e.jsxs(q,{children:[x===g&&e.jsx(p,{value:g,children:"自动选择最近会话"}),k.map(s=>e.jsx(p,{value:s,children:s},s))]})]}),e.jsxs("div",{className:"relative",children:[e.jsx(fe,{className:"pointer-events-none absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground"}),e.jsx(xe,{value:v,onChange:s=>C(()=>te(s.target.value)),className:"pl-9",placeholder:"搜索阶段、会话或文件名"})]})]}),z&&e.jsx("div",{className:"rounded-md border border-destructive/30 bg-destructive/10 px-3 py-2 text-sm text-destructive",children:z}),e.jsxs("div",{className:"grid min-h-0 flex-1 grid-cols-1 gap-3 lg:grid-cols-[360px_1fr]",children:[e.jsxs("div",{className:"flex min-h-0 flex-col overflow-hidden rounded-md border bg-background",children:[e.jsxs("div",{className:"flex h-11 flex-shrink-0 items-center justify-between border-b px-3 text-sm text-muted-foreground",children:[e.jsxs("span",{children:[E," 条记录"]}),e.jsxs("span",{children:["第 ",m," / ",_," 页"]})]}),e.jsx(M,{className:"min-h-0 flex-1",children:e.jsxs("div",{className:"space-y-1 p-2",children:[l.map(s=>{const h=t?.stage===s.stage&&t?.session_id===s.session_id&&t?.stem===s.stem;return e.jsxs("button",{type:"button",onClick:()=>N(s),className:B("flex w-full flex-col gap-2 rounded-md border px-3 py-2 text-left text-sm transition-colors",h?"border-primary bg-primary/10 text-foreground":"border-transparent hover:border-border hover:bg-muted/60"),children:[e.jsxs("div",{className:"flex items-center justify-between gap-2",children:[e.jsx(oe,{variant:"secondary",className:"max-w-[150px] truncate",children:s.stage}),e.jsxs("span",{className:"flex items-center gap-1 text-xs text-muted-foreground",children:[e.jsx(pe,{className:"h-3.5 w-3.5"}),W(s.timestamp,s.modified_at)]})]}),e.jsx("div",{className:"truncate font-medium",children:s.session_id}),e.jsxs("div",{className:"flex items-center justify-between gap-2 text-xs text-muted-foreground",children:[e.jsx("span",{className:"truncate",children:s.stem}),e.jsx("span",{className:"shrink-0",children:X(s.size)})]})]},`${s.stage}/${s.session_id}/${s.stem}`)}),!c&&l.length===0&&e.jsx("div",{className:"px-3 py-10 text-center text-sm text-muted-foreground",children:"没有找到推理过程记录"})]})}),e.jsxs("div",{className:"flex h-12 flex-shrink-0 items-center justify-between border-t px-3",children:[e.jsx(f,{variant:"outline",size:"sm",disabled:m<=1||c,onClick:()=>b(s=>Math.max(1,s-1)),children:"上一页"}),e.jsx(f,{variant:"outline",size:"sm",disabled:m>=_||c,onClick:()=>b(s=>Math.min(_,s+1)),children:"下一页"})]})]}),e.jsxs("div",{className:"flex min-h-0 flex-col overflow-hidden rounded-md border bg-background",children:[e.jsxs("div",{className:"flex min-h-14 flex-shrink-0 flex-col gap-1 border-b px-4 py-3 md:flex-row md:items-center md:justify-between",children:[e.jsxs("div",{className:"min-w-0",children:[e.jsx("div",{className:"truncate text-sm font-medium",children:t?`${t.stage}/${t.session_id}/${t.stem}`:"未选择记录"}),e.jsx("div",{className:"text-xs text-muted-foreground",children:t?`${X(t.size)} · ${W(t.timestamp,t.modified_at)}`:"从左侧列表选择一条记录"})]}),t&&e.jsxs("div",{className:"flex items-center gap-2 text-xs text-muted-foreground",children:[e.jsxs(f,{variant:"outline",size:"sm",className:"h-8 gap-1.5",onClick:ie,disabled:!t.text_path||y||!u,title:"复制完整 Prompt",children:[e.jsx(ge,{className:"h-3.5 w-3.5"}),"复制"]}),t.text_path&&e.jsxs("span",{className:"inline-flex items-center gap-1",children:[e.jsx(D,{className:"h-3.5 w-3.5"}),"txt"]}),t.html_path&&e.jsxs("span",{className:"inline-flex items-center gap-1",children:[e.jsx(je,{className:"h-3.5 w-3.5"}),"html"]})]})]}),e.jsxs(me,{value:le,onValueChange:s=>S(s),className:"flex min-h-0 flex-1 flex-col",children:[e.jsx("div",{className:"flex flex-shrink-0 border-b px-3 py-2",children:e.jsxs(he,{children:[e.jsxs(U,{value:"text",disabled:!t?.text_path,children:[e.jsx(D,{className:"mr-1 h-4 w-4"}),"文本"]}),e.jsxs(U,{value:"html",disabled:!t?.html_path,children:[e.jsx(ve,{className:"mr-1 h-4 w-4"}),"HTML"]})]})}),e.jsx(H,{value:"text",className:"m-0 min-h-0 flex-1 overflow-hidden",children:e.jsx(M,{className:"h-full",children:e.jsx("pre",{className:"min-h-full whitespace-pre-wrap break-words p-4 font-mono text-xs leading-5 text-foreground",children:y?"正在读取...":u||"没有文本内容"})})}),e.jsx(H,{value:"html",className:"m-0 min-h-0 flex-1 overflow-hidden",children:t?.html_path&&R?e.jsx("iframe",{title:"推理过程 HTML 预览",src:R,sandbox:"",className:"h-full w-full border-0 bg-white"}):e.jsx("div",{className:"flex h-full items-center justify-center text-sm text-muted-foreground",children:"没有 HTML 预览"})})]})]})]})]})}export{Ee as ReasoningProcessPage};