1import{h as H,ab as J,u as K,r as n,s as Q,t as V,j as e,L as M}from"./index-hs4a0mYW.js";import{u as G}from"./useQuery-C-iGd3mh.js";import{u as U}from"./useServerFn-DgMZqDJQ.js";import{D as W,u as X,C as Y,a as Z,b as ee,S as se,d as te,P as re,e as ae,f as oe,g as ne,M as S,c as $,h as ie}from"./mermaid-GHXKKRXX-BW650IWT.js";import{g as ce}from"./personas.functions-CzTXKnLO.js";import{l as P,g as b,c as j,s as A,t as de,d as le}from"./threads-DvShSnNU.js";import{B as me}from"./button-Cc6vISkn.js";import{A as ue}from"./arrow-left-bJxZenjV.js";import{B as q}from"./book-open-DWMrRwV0.js";import{S as R}from"./sparkles-BZGgZXIM.js";import{T as pe}from"./trash-2-Bg71gBw1.js";import{T as he}from"./triangle-alert-Y3Bwbf1c.js";import"./check-rFTGGwOD.js";import"./input-XaHB6AeD.js";import"./textarea-DM5dDCD0.js";import"./loader-circle-CKo3nrvz.js";import"./index.browser-BY9c7rfI.js";const xe=[["path",{d:"M22 17a2 2 0 0 1-2 2H6.828a2 2 0 0 0-1.414.586l-2.202 2.202A.71.71 0 0 1 2 21.286V5a2 2 0 0 1 2-2h16a2 2 0 0 1 2 2z",key:"18887p"}],["path",{d:"M12 8v6",key:"1ib9pf"}],["path",{d:"M9 11h6",key:"1fldmi"}]],fe=H("message-square-plus",xe);function De(){const{personaId:t,threadId:r}=J.useParams(),o=K(),i=U(ce),{data:c}=G({queryKey:["persona",t],queryFn:()=>i({data:{id:t}})}),[N,u]=n.useState(0),v=n.useMemo(()=>P(t),[t,N]),B=n.useMemo(()=>{const s=b(r);if(s&&s.personaId===t)return s;const a=j(t);return queueMicrotask(()=>o({to:"/personas/$personaId/chat/$threadId",params:{personaId:t,threadId:a.id},replace:!0})),a},[r,t,o]),D=n.useMemo(()=>new W({api:"/api/chat",prepareSendMessagesRequest:async({messages:s,body:a})=>{const{data:m}=await Q.auth.getSession(),T=m.session?.access_token,C={};return T&&(C.Authorization=`Bearer ${T}`),{body:{messages:s,personaId:t,...a??{}},headers:C}}}),[t]),[x,w]=n.useState(""),{messages:d,sendMessage:E,status:l,error:y,setMessages:F}=X({id:r,messages:B.messages,transport:D,onError:s=>V.error(s.message||"Chat error")}),k=n.useRef("");n.useEffect(()=>{const s=JSON.stringify(d);if(s===k.current||(k.current=s,l==="submitted"||l==="streaming"))return;const a=b(r);a&&(A({...a,messages:d,title:a.title==="New conversation"?de(d):a.title}),u(m=>m+1))},[d,l,r]);const f=n.useRef(null);n.useEffect(()=>{f.current?.focus()},[r]);const p=l==="submitted"||l==="streaming",L=()=>{const s=x.trim();!s||p||(w(""),E({text:s}),setTimeout(()=>f.current?.focus(),0))},O=()=>{const s=j(t);u(a=>a+1),o({to:"/personas/$personaId/chat/$threadId",params:{personaId:t,threadId:s.id}})},z=s=>{if(confirm("Delete this conversation?")){if(le(s),s===r){const m=P(t)[0]??j(t);o({to:"/personas/$personaId/chat/$threadId",params:{personaId:t,threadId:m.id},replace:!0})}u(a=>a+1)}},_=()=>{F([]);const s=b(r);s&&A({...s,messages:[],title:"New conversation"}),u(a=>a+1)},I=c?.persona,g=I?.name??"Persona",h=I?.type??"book";return e.jsxs("div",{className:"h-[calc(100vh-4rem)] flex",children:[e.jsxs("aside",{className:"hidden md:flex w-72 flex-col border-r border-border bg-surface-muted",children:[e.jsxs("div",{className:"p-4 border-b border-border",children:[e.jsxs(M,{to:"/personas/$personaId",params:{personaId:t},className:"inline-flex items-center text-xs text-muted-foreground hover:text-foreground",children:[e.jsx(ue,{className:"h-3 w-3 mr-1","aria-hidden":!0}),"Back to persona"]}),e.jsxs("div",{className:"mt-3 flex items-center gap-2",children:[e.jsx("div",{className:"inline-flex h-8 w-8 items-center justify-center rounded-md bg-primary text-primary-foreground",children:h==="book"?e.jsx(q,{className:"h-4 w-4"}):e.jsx(R,{className:"h-4 w-4"})}),e.jsxs("div",{className:"min-w-0",children:[e.jsx("p",{className:"text-xs uppercase tracking-wide text-muted-foreground",children:h}),e.jsx("p",{className:"font-semibold text-primary truncate",children:g})]})]}),e.jsxs(me,{onClick:O,className:"mt-4 w-full",size:"sm",children:[e.jsx(fe,{className:"h-4 w-4 mr-1.5","aria-hidden":!0}),"New conversation"]})]}),e.jsx("nav",{className:"flex-1 overflow-y-auto p-2","aria-label":"Conversations",children:v.length===0?e.jsx("p",{className:"text-xs text-muted-foreground p-3",children:"No conversations yet."}):e.jsx("ul",{className:"space-y-1",children:v.map(s=>e.jsxs("li",{className:"flex items-stretch gap-1",children:[e.jsx(M,{to:"/pers
1onas/$personaId/chat/$threadId",params:{personaId:t,threadId:s.id},className:`flex-1 rounded-md px-3 py-2 text-sm truncate ${s.id===r?"bg-surface text-primary font-medium":"text-muted-foreground hover:bg-surface hover:text-foreground"}`,children:s.title}),e.jsx("button",{type:"button","aria-label":"Delete conversation",onClick:()=>z(s.id),className:"px-2 rounded-md text-muted-foreground hover:text-destructive hover:bg-surface",children:e.jsx(pe,{className:"h-3.5 w-3.5","aria-hidden":!0})})]},s.id))})})]}),e.jsxs("div",{className:"flex-1 flex flex-col min-w-0",children:[e.jsxs("div",{role:"note",className:"border-b border-border bg-accent/15 px-4 py-2 text-xs text-foreground flex items-start gap-2",children:[e.jsx(he,{className:"h-3.5 w-3.5 mt-0.5 flex-none text-accent-foreground","aria-hidden":!0}),e.jsx("p",{children:"This is an AI representation grounded in the uploaded material. It is not the living person, and it answers only from the source."})]}),e.jsxs(Y,{className:"flex-1",children:[e.jsxs(Z,{className:"max-w-3xl mx-auto w-full",children:[d.length===0?e.jsx(ee,{title:`Talk to ${g}`,description:h==="book"?"Ask about anything in the book. Out-of-scope questions will be declined.":"Ask about the author's documented perspective. Out-of-scope questions will be declined.",icon:h==="book"?e.jsx(q,{className:"h-8 w-8"}):e.jsx(R,{className:"h-8 w-8"})}):d.map(s=>e.jsx(ge,{message:s},s.id)),p&&e.jsx("div",{className:"px-1",children:e.jsx(se,{children:"Thinking..."})}),y&&e.jsx("p",{className:"text-sm text-destructive px-1",children:y.message})]}),e.jsx(te,{})]}),e.jsx("div",{className:"border-t border-border bg-background p-4",children:e.jsxs("div",{className:"max-w-3xl mx-auto",children:[e.jsxs(re,{onSubmit:L,children:[e.jsx(ae,{ref:f,value:x,onChange:s=>w(s.target.value),placeholder:`Ask ${g}...`,disabled:p}),e.jsxs(oe,{className:"justify-between",children:[e.jsx("button",{type:"button",onClick:_,className:"text-xs text-muted-foreground hover:text-foreground",children:"Clear conversation"}),e.jsx(ne,{status:l,disabled:!x.trim()||p})]})]}),e.jsx("p",{className:"mt-2 text-[11px] text-muted-foreground text-center",children:"Answers cite the part of the source they draw from. The persona will decline what the source does not cover."})]})})]})]})}function ge({message:t}){const r=t.parts.map(c=>c.type==="text"?c.text:"").join("");if(t.role!=="assistant")return e.jsx(S,{from:t.role,children:e.jsx($,{children:e.jsx("p",{className:"whitespace-pre-wrap",children:r})})});const{body:o,citation:i}=be(r);return e.jsx(S,{from:"assistant",children:e.jsxs($,{children:[e.jsx(ie,{children:o}),i&&e.jsxs("p",{className:"mt-3 text-xs text-muted-foreground border-t border-border pt-2",children:[e.jsx("span",{className:"font-medium text-foreground",children:"From:"})," ",i]})]})})}function be(t){const r=t.split(` 2`);for(let o=r.length-1;o>=0;o--){const i=r[o].trim();if(!i)continue;const c=i.match(/^From:\s*(.+)$/i);if(c)return{body:r.slice(0,o).join(` 3`).trim(),citation:c[1].trim()};break}return{body:t,citation:null}}export{De as component};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.