1import{j as e,A as U,m as y}from"./vendor-motion-DeqZC-4t.js";import{r as a}from"./vendor-react-DhMA_kEV.js";import{M as V}from"./MarkdownWithLinks-D16hDIsJ.js";import{b as q,B as J,I as F}from"./App-CVGAgymZ.js";import{C as K}from"./ChatPlayButton-CiUTHdVJ.js";import{a5 as Q,aA as Y,ae as H,S as G,L as R,an as ee}from"./vendor-icons-xAlReevQ.js";import"./index-CG3MEsji.js";import"./index-BvfMybbr.js";import"./index-WqAUWWUN.js";import"./vendor-supabase-CHGq0V2O.js";import"./vendor-query-cy1x5wk5.js";import"./vendor-charts-DKUbyPp_.js";import"./logChunkLoadFailure-BWuYLh8E.js";import"./useTextToSpeech-Dwj4mo-i.js";import"./voiceClone-kQzdWElW.js";const te="https://pfehtiyeqoqqpmeqladh.supabase.co/functions/v1/topic-chat";function je({title:r,summary:j,category:b,sectionTitles:v,slug:N}){var I;const[s,c]=a.useState([]),[l,w]=a.useState(""),[n,k]=a.useState(!1),[p,h]=a.useState(!1),d=a.useRef(null),X=a.useRef(null),z=q().ownerVoiceId||null;a.useEffect(()=>{d.current&&(d.current.scrollTop=d.current.scrollHeight)},[s]);const L=[`What's the key takeaway about ${r}?`,`Why does ${r} matter?`,`What's controversial about ${r}?`],x=a.useCallback(async t=>{var S,A,M,$;const o=(t||l).trim();if(!o||n)return;const C=[...s,{role:"user",content:o}];c(C),w(""),k(!0),h(!0);try{const i=await fetch(te,{method:"POST",headers:{"Content-Type":"application/json",Authorization:"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InBmZWh0aXllcW9xcXBtZXFsYWRoIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NzM1OTUwNzksImV4cCI6MjA4OTE3MTA3OX0._uv3xZvd8aDCXgi_m9UPX9t4SgV1L6lKWDZrjlm3DQU"},body:JSON.stringify({messages:C,topicContext:{title:r,summary:j,category:b,sectionTitles:v,path:`/entry/${N}`,contentType:"entry",enableActions:!0}})});if(!i.ok)throw new Error(`${i.status}`);const m=(S=i.body)==null?void 0:S.getReader();if(!m)throw new Error("No stream");const P=new TextDecoder;let E="";for(c(f=>[...f,{role:"assistant",content:""}]);;){const{done:f,value:B}=await m.read();if(f)break;const D=P.decode(B);for(const O of D.split(` 2`)){if(!O.startsWith("data: "))continue;const T=O.slice(6);if(T!=="[DONE]")try{const W=($=(M=(A=JSON.parse(T).choices)==null?void 0:A[0])==null?void 0:M.delta)==null?void 0:$.content;if(W){E+=W;const Z=E.replace(/<think>[\s\S]*?<\/think>/g,"").trim();c(_=>{const g=[..._];return g[g.length-1]={role:"assistant",content:Z},g})}}catch{}}}}catch(i){console.error("PageChat error:",i),c(m=>[...m,{role:"assistant",content:"Sorry, I couldn't process that. Please try again."}])}finally{k(!1)}},[l,n,s,r,j,b,v,N]),u=s.length>0;return e.jsxs("div",{className:"brutal-card bg-card border-2 border-border overflow-hidden",children:[e.jsx("div",{className:"bg-gradient-to-r from-primary/10 via-accent/10 to-primary/5 px-5 py-4 border-b border-border",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"w-9 h-9 rounded-full bg-primary/20 flex items-center justify-center",children:e.jsx(Q,{className:"w-4.5 h-4.5 text-primary"})}),e.jsxs("div",{children:[e.jsx("h3",{className:"font-display font-bold text-sm",children:"Page Assistant"}),e.jsx("p",{className:"text-[10px] text-muted-foreground font-mono",children:"Ask questions · Improve this page · Collaborate"})]})]}),u&&e.jsx(J,{variant:"ghost",size:"sm",onClick:()=>h(!p),className:"h-7 w-7 p-0",children:p?e.jsx(Y,{className:"w-4 h-4"}):e.jsx(H,{className:"w-4 h-4"})})]})}),e.jsx(U,{mode:"wait",children:u?p?e.jsx(y.div,{initial:{height:0,opacity:0},animate:{height:"auto",opacity:1},exit:{height:0,opacity:0},className:"overflow-hidden",children:e.jsxs("div",{ref:d,className:"max-h-[300px] overflow-y-auto px-4 py-3 space-y-3",children:[s.map((t,o)=>e.jsxs("div",{className:`flex gap-2 ${t.role==="user"?"justify-end":"justify-start"}`,children:[t.role==="assistant"&&e.jsx("div",{className:"w-6 h-6 rounded-full bg-primary/20 flex items-center justify-center flex-shrink-0 mt-0.5",children:e.jsx(G,{className:"w-3 h-3 text-primary"})}),e.jsxs("div",{className:`relative max-w-[80%] ${t.role==="assistant"?"pb-1":""}`,children:[e.jsx("div",{className:`rounded-xl px-3 py-2 text-sm ${t.role==="user"?"bg-primary text-primary-foreground":"bg-muted/60 text-foreground"}`,children:t.role==="assistant"?e.jsx("div",{className:"prose prose-sm dark:prose-invert max-w-none [&_p]:mb-1 [&_p]:leading-snug [&_a]:text-accent [&_a]:underline",children:e.jsx(V,{children:t.content||"..."})}):e.jsx("p",{children:t.content})}),t.role==="assistant"&&t.content&&e.jsx(K,{text:t.content,voiceId:z,className:"absolute -bottom-2 -right-2"})]})]},o)),n&&((I=s[s.length-1])==null?void 0:I.role)==="user"&&e.jsxs("div",{className:"flex gap-2",children:[e.jsx("div",{className:"w-6 h-6 rounded-full bg-primary/20 flex items-center justify-center flex-shrink-0",children:e.jsx(R,{className:"w-3 h-3 text-primary animate-spin"})}),e.jsx("div",{className:"bg-muted/60 rounded-xl px-3 py-2 text-sm text-muted-foreground",children:"Thinking..."})]})]})},"chat"):e.jsx(y.div,{className:"px-4 py-2",children:e.jsxs("button",{onClick:()=>h(!0),className:"text-xs text-muted-foreground hover:text-foreground transition-colors",children:[s.length," message",s.length!==1?"s":""," â tap to expand"]})},"collapsed"):e.jsxs(y.div,{initial:{opacity:0}
2,animate:{opacity:1},exit:{opacity:0},className:"px-5 py-4",children:[e.jsxs("p",{className:"text-sm text-muted-foreground mb-3",children:["ð I know everything about ",e.jsx("strong",{className:"text-foreground",children:r}),". Ask me anything â and if you spot something wrong, our conversation helps improve this page for everyone."]}),e.jsx("div",{className:"flex flex-wrap gap-1.5 mb-3",children:L.map((t,o)=>e.jsx("button",{onClick:()=>x(t),className:"text-[11px] px-2.5 py-1.5 rounded-full border border-border bg-muted/50 hover:bg-primary/10 hover:border-primary/30 transition-colors text-muted-foreground hover:text-foreground truncate max-w-[220px]",children:t},o))})]},"welcome")}),e.jsxs("div",{className:"px-4 py-3 border-t border-border flex gap-2",children:[e.jsx(F,{ref:X,value:l,onChange:t=>w(t.target.value),onKeyDown:t=>t.key==="Enter"&&!t.shiftKey&&x(),placeholder:u?"Follow up...":`Ask about ${r}...`,disabled:n,className:"flex-1 h-9 text-sm"}),e.jsx(J,{size:"sm",onClick:()=>x(),disabled:!l.trim()||n,className:"h-9 w-9 p-0",children:n?e.jsx(R,{className:"w-4 h-4 animate-spin"}):e.jsx(ee,{className:"w-4 h-4"})})]})]})}export{je as default};
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.