PageSourceSearch

https://www.retrofitpolicylab.uk/assets/ResearchChat-CNumEkIE.js

js retrofitpolicylab.uk collected 2026-10-07 18:54:52 UTC 6,446 bytes, 1 lines download raw bytes

1import{r,K as k,N as I,j as e,E,F as T,b as v,O as A,Q as B,R as M,U as L,i as _,V as D,W as P,I as q,B as z,L as O,Y as $,Z as F}from"./index-ncH4CRZS.js";import{E as V}from"./EditablePageHeader-Cw3n4TQ4.js";import{C as H,a as W}from"./chatMarkdown-DHPwZdpi.js";import{s as U,u as N,a as K,p as Q,A as Y,C as G}from"./ChatErrorBoundary-CEgzZ0LN.js";import{C as J}from"./CitationHandler-CFikURSe.js";import{P as Z}from"./PageActionBar-D-zJVYPa.js";import"./PageHeader-C09UPEO0.js";import"./SafeHtml-C47PcLaD.js";import"./purify.es-BFmuJLeH.js";import"./dialog-D5nIJMtd.js";import"./RichTextEditor-OS7N0jWs.js";import"./isEqual-BC5tzDZV.js";import"./tabs-DSB4Hz_q.js";import"./scroll-area-Cs-Jq6s9.js";import"./index-BdQq_4o_.js";import"./useMediaLibrary-BzTsksnM.js";import"./textarea-LTZgJtsi.js";import"./articleContent-CGNC_AoC.js";import"./useKnowledgeBaseDocuments-DD4iq5GU.js";import"./index-Cr_6DO-H.js";import"./index-BrEOoKkn.js";const X="https://qvfunkbhskijperzluju.supabase.co",ee=()=>{const[i,o]=r.useState([]),[c,n]=r.useState(!1),[w,u]=r.useState(!1),[y,l]=r.useState(!1),{toast:h}=k(),C=r.useCallback(async m=>{if(!m.trim()||c)return;n(!0),u(!1);const p={id:`user-${Date.now()}`,role:"user",content:m,created_at:new Date().toISOString()};o(t=>[...t,p]);const b=i.map(t=>({role:t.role,content:t.content})),x=`assistant-${Date.now()}`;let g="";try{const{data:{session:t}}=await I.auth.getSession();if(!(t!=null&&t.access_token))throw new Error("You must be logged in to use the research assistant.");const a=await fetch(`${X}/functions/v1/research-chat-v2`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${t.access_token}`},body:JSON.stringify({message:m,conversationHistory:b})});if(!a.ok){const s=await a.json().catch(()=>({}));throw a.status===429?new Error("Rate limit exceeded. Please try again later."):a.status===402?new Error("API quota exceeded. Please contact support."):new Error(s.error||`Request failed: ${a.status}`)}const d=a.headers.get("x-ai-request-id")??void 0;u(!0),o(s=>[...s,{id:x,role:"assistant",content:"",created_at:new Date().toISOString(),requestId:d}]),await U(a,{onDelta:s=>{g+=s,o(f=>f.map(j=>j.id===x?{...j,content:g}:j))},onDebugError:s=>{console.debug("Research assistant SSE parse error on final flush:",s)}})}catch(t){console.error("Error sending message:",t),h({title:"Error",description:t.message||"Failed to get a response from RetrofitBot. Please try again.",variant:"destructive"}),o(a=>a.filter(d=>d.id!==p.id&&d.id!==x))}finally{n(!1),u(!1)}},[i,c,h]),S=r.useCallback(()=>{l(!0),o([]),l(!1)},[]);return{messages:i,sendMessage:C,clearChat:S,isLoading:!1,isSending:c,isStreaming:w,isClearing:y}},se=()=>{const[i,o]=r.useState(""),c=r.useRef(null),{messages:n,sendMessage:w,clearChat:u,isLoading:y,isSending:l,isStreaming:h,isClearing:C}=ee(),S=N("research_bot_intro_message","Welcome! I'm RetrofitBot, your research assistant for energy retrofit studies."),m=N("research_bot_welcome_message","Ask me about our research findings, interview data, and thematic analyses."),[p,b]=r.useState(null),x=async(s,f)=>{await navigator.clipboard.writeText(f),b(s),setTimeout(()=>b(null),2e3)},g=r.useRef(0),t=()=>{var s;(s=c.current)==null||s.scrollIntoView({behavior:"smooth"})};r.useEffect(()=>{n.length>g.current&&(t(),g.current=n.length)},[n.length]);const a=()=>{i.trim()&&!l&&(w(i.trim()),o(""))};if(y)return e.jsx(E,{className:"w-full max-w-4xl mx-auto hover:shadow-none hover:translate-y-0",children:e.jsxs(T,{className:"flex items-center justify-center p-8",children:[e.jsx(v,{size:40,className:"mr-2"}),e.jsx("span",{children:"Loading RetrofitBot..."})]})});const d=l&&!h;return e.jsx(H,{title:"Research assistant",onClear:u,clearDisabled:C||n.length===0,inputValue:i,onInputChange:o,onSend:a,sending:l,sendDisabled:!i.trim(),placeholder:"Ask about energy retrofit research...",children:e.jsxs("div",{className:"space-y-4",children:[n.length===0?e.jsx(W,{title:"Research assistant",summary:K(S)||"Welcome! I'm RetrofitBot, your research assistant for energy retrofit studies.",icon:e.jsx(A,{className:"h-7 w-7"}),suggestions:Q(m),onSuggestion:o}):n.map((s,f)=>{const j=f===n.length-1,R=s.role==="assistant"&&!!s.content&&!(j&&h);return e.jsxs("div",{className:`flex ${s.role==="user"?"justify-end":"justify-start"}`,children:[s.role==="assistant"&&s.content&&e.jsx(B,{children:e.jsxs(M,{children:[e.jsx(L,{asChild:!0,children:e.jsx("button",{onClick:()=>x(s.id,s.content),className:"self-start mt-3 mr-2 p-1 rounded hover:bg-muted/80 text-muted-foreground hover:text-foreground transition-colors",children:p===s.id?e.jsx(_,{className:"h-4 w-4 text-green-500"}):e.jsx(D,{className:"h-4 w-4"})})}),e.jsx(P,{children:e.jsx("p",{children:p===s.id?"Copied!":"Copy response"})})]})}),e.jsx("div",{className:`max-w-[80%] rounded-lg p-3 ${s.role==="user"?"bg-primary text-primary-foreground":"bg-brand-lightest-grey"}`,children:s.role==="user"?e.jsx("p",{className:"text-sm whitespace-pre-wrap",children:s.content}):e.jsxs(e.Fragment,{children:[s.content?e.jsx("div",{className:"max-w-3xl",children:e.jsx(J,{content:s.content})}):e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(v,{size:18}),e.jsx("span",{className:"text-sm text-muted-foreground",children:"Generating response..."})]}),R&&e.jsx(Y,{requestId:s.requestId,copyText:s.content})]})})]},s.id)}),d&&e.jsx("div",{className:"flex justify-start",children:e.jsx("div",{className:"bg-brand-lightest-grey rounded-lg p-3 max-w-[80%]",children:e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(v,{size:18}),e.jsx("span",{className:"text-sm",children:"Searching research f
1iles..."})]})})}),e.jsx("div",{ref:c})]})})},Se=()=>e.jsxs(q,{children:[e.jsx(Z,{backTo:"/research",backLabel:"Research",children:e.jsx(z,{asChild:!0,variant:"outline",size:"sm",className:"gap-2",children:e.jsxs(O,{to:"/knowledge-base",children:[e.jsx($,{className:"h-4 w-4"}),"Knowledge base"]})})}),e.jsxs("div",{className:"max-w-7xl mx-auto px-4 sm:px-6 lg:px-8",children:[e.jsx(V,{pageSlug:"research-chat",fallbackTitle:"Research Assistant",titleSuffix:e.jsx(F,{className:"ml-2 bg-amber-500 text-white border-amber-500 tracking-normal",children:"Beta"}),fallbackDescription:"Ask questions about our energy retrofit research database"}),e.jsx("div",{className:"mt-8",children:e.jsx(G,{fallbackTitle:"Research Assistant temporarily unavailable.",children:e.jsx(se,{})})})]})]});export{Se 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.