PageSourceSearch

https://helkos.fi/assets/AITutor-BJISBOK6.js

js helkos.fi collected 2026-10-09 22:04:36 UTC 11,538 bytes, 2 lines download raw bytes

1import{j as e}from"./vendor-motion-BYSGJZRG.js";import{u as de,r as a}from"./vendor-react-BAWrOFBJ.js";import{aa as me,s as m,B as r,A as ue,X,C as he,f as pe,L as Z,g as k}from"./index-BD6qdh5D.js";import{I as xe}from"./input-a2GrFMNb.js";import{S as fe}from"./scroll-area-Db5VuWmi.js";import{S as ge,a as je,b as we,c as Ne,d as be}from"./select-CMrhmtbm.js";import{G as ve,M as ye}from"./MathDisplayToggle-gt5Z0G5m.js";import{B as R}from"./bot-Ixao6CGk.js";import{S as Ce}from"./sparkles-ChAUGENS.js";import{C as P}from"./camera-BKZ9y0mA.js";import{I as K}from"./image-rClv8dA4.js";import{U as Se}from"./user-Btw4KzLD.js";import{L as Ie}from"./lightbulb-pW5NcbTe.js";import{E as ke}from"./eye-BFMSnvy5.js";import{S as _e}from"./send-B6lOgiEF.js";import"./vendor-pdf-Cq2FS4Cb.js";import"./vendor-radix-Scc5zhsa.js";import"./vendor-charts-DWQnO0ie.js";import"./chevron-up-CL_jrATs.js";import"./MathRenderer-ULtPGP2E.js";import"./vendor-math-Dadayjg-.js";import"./type-CjswToMQ.js";const Te=["General Math Help","Algebra","Geometry","Calculus","Statistics","Trigonometry","Pre-Algebra","Word Problems","Linear Algebra","Differential Equations"];function es(){var G,J;const Q=de(),{user:c}=me(),[n,u]=a.useState([]),[N,b]=a.useState(""),[i,U]=a.useState(!1),[f,D]=a.useState("General Math Help"),[g,_]=a.useState(null),[h,j]=a.useState(null),[Y,T]=a.useState(!1),v=a.useRef(null),A=a.useRef(null),z=a.useRef(null),d=a.useRef(null),p=a.useRef(null);a.useEffect(()=>{c&&ee()},[c]),a.useEffect(()=>{v.current&&(v.current.scrollTop=v.current.scrollHeight)},[n]),a.useEffect(()=>()=>{p.current&&p.current.getTracks().forEach(s=>s.stop())},[]);const ee=async()=>{if(!c)return;const{data:s}=await m.from("ai_chat_sessions").select("id, topic").eq("student_id",c.id).order("updated_at",{ascending:!1}).limit(1).single();if(s){_(s.id),D(s.topic||"General Math Help");const{data:t}=await m.from("ai_chat_messages").select("role, content").eq("session_id",s.id).order("created_at");t&&u(t)}else{const{data:t}=await m.from("ai_chat_sessions").insert({student_id:c.id,topic:f}).select().single();t&&_(t.id)}},se=async()=>{if(!c)return;const{data:s}=await m.from("ai_chat_sessions").insert({student_id:c.id,topic:f}).select().single();s&&(_(s.id),u([]),j(null))},te=s=>{var y;const t=(y=s.target.files)==null?void 0:y[0];if(!t)return;if(!t.type.startsWith("image/")){k.error("Please select an image file");return}if(t.size>10*1024*1024){k.error("Image must be less than 10MB");return}const o=new FileReader;o.onload=C=>{var w;j((w=C.target)==null?void 0:w.result)},o.readAsDataURL(t)},O=async()=>{try{T(!0);const s=await navigator.mediaDevices.getUserMedia({video:{facingMode:"environment"}});p.current=s,d.current&&(d.current.srcObject=s)}catch(s){console.error("Camera error:",s),k.error("Could not access camera. Please check permissions."),T(!1)}},ae=()=>{if(!d.current)return;const s=document.createElement("canvas");s.width=d.current.videoWidth,s.height=d.current.videoHeight;const t=s.getContext("2d");if(t){t.drawImage(d.current,0,0);const o=s.toDataURL("image/jpeg",.8);j(o)}L()},L=()=>{p.current&&(p.current.getTracks().forEach(s=>s.stop()),p.current=null),T(!1)},re=()=>{j(null)},E=async(s=!1)=>{var w,q,B,F,H;if(!N.trim()&&!h||i||!g)return;const t=s?"Please show me the complete step-by-step solution.":N.trim()||"Please help me with this math problem.",o=h;b(""),j(null),U(!0);const y={role:"user",content:t,imageUrl:o||void 0},C=[...n,y];u(C),await m.from("ai_chat_messages").insert({session_id:g,role:"user",content:o?`[Image attached] ${t}`:t});try{const x=await fetch("https://hgrqtshnwswtcoemunsz.supabase.co/functions/v1/math-tutor-chat",{method:"POST",headers:{"Content-Type":"application/json",Authorization:"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImhncnF0c2hud3N3dGNvZW11bnN6Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTk5MzcxOTQsImV4cCI6MjA3NTUxMzE5NH0._qRKWfvSk8tgwhjLzSAHLeAuBbjlUTWagt96b3c3jFE"},body:JSON.stringify({messages:C.slice(-10).map(l=>({role:l.role,content:l.content})),topic:f,imageData:o,revealSolution:s})});if(!x.ok){const l=await x.json().catch(()=>({}));throw new Error(l.error||"Failed to get response")}const S=(w=x.body)==null?void 0:w.getReader(),ie=new TextDecoder;let I="";for(u(l=>[...l,{role:"assistant",content:""}]);S;){const{done:l,value:oe}=await S.read();if(l)break;const le=ie.decode(oe,{stream:!0}).split(`
2`);for(const W of le)if(W.startsWith("data: ")){const V=W.slice(6).trim();if(V==="[DONE]")continue;try{const $=(F=(B=(q=JSON.parse(V).choices)==null?void 0:q[0])==null?void 0:B.delta)==null?void 0:F.content;$&&(I+=$,u(ce=>{const M=[...ce];return M[M.length-1]={role:"assistant",content:I},M}))}catch{}}}I&&(await m.from("ai_chat_messages").insert({session_id:g,role:"assistant",content:I}),await m.from("ai_chat_sessions").update({updated_at:new Date().toISOString(),topic:f}).eq("id",g))}catch(x){console.error("Chat error:",x),k.error(x.message||"Failed to get response. Please try again."),u(S=>S.slice(0,-1))}finally{U(!1),(H=A.current)==null||H.focus()}},ne=()=>{i||!g||(b("Show me the complete step-by-step solution"),E(!0))};return e.jsxs("div",{className:"min-h-screen bg-background flex flex-col",children:[e.jsx("div",{className:"border-b p-4 bg-gradient-to-r from-primary/5 to-primary/10",children:e.jsxs("div",{className:"max-w-4xl mx-auto flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsx(r,{variant:"ghost",size:"icon",onClick:()=>Q("/tutor/student"),children:e.jsx(ue,{className:"h-5 w-5"})}),e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"bg-gradient-to-br from-primary to-primary/80 p-2.5 rounded-xl shadow-lg",children:e.jsx(R,{className:"h-6 w-6 text-primary-foreground"})}),e.jsxs("div",{children:[e.jsx("h1",{className:"font-bold text-lg",children:"AI Tutor"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Powered by GPT-5 • Step-by-step solutions"})]})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(ve,{compact:!0}),e.jsxs(ge,{value:f,onValueChange:D,children:[e.jsx(je,{className:"w-44",children:e.jsx(we,{})}),e.jsx(Ne,{children:Te.map(s=>e.jsx(be,{value:s,children:s},s))})]}),e.jsxs(r,{variant:"outline",size:"sm",onClick:se,children:[e.jsx(Ce,{className:"h-4 w-4 mr-2"}),"New Chat"]})]})]})}),Y&&e.jsx("div",{className:"fixed inset-0 bg-black/80 z-50 flex flex-col items-center justify-center p-4",children:e.jsxs("div",{className:"relative w-full max-w-lg",children:[e.jsx("video",{ref:d,autoPlay:!0,playsInl
2ine:!0,className:"w-full rounded-lg"}),e.jsxs("div",{className:"flex justify-center gap-4 mt-4",children:[e.jsxs(r,{variant:"destructive",onClick:L,children:[e.jsx(X,{className:"h-4 w-4 mr-2"}),"Cancel"]}),e.jsxs(r,{onClick:ae,children:[e.jsx(P,{className:"h-4 w-4 mr-2"}),"Capture"]})]})]})}),e.jsx(fe,{className:"flex-1 p-4",ref:v,children:e.jsxs("div",{className:"max-w-4xl mx-auto space-y-4",children:[n.length===0&&e.jsx(he,{className:"bg-gradient-to-br from-muted/50 to-muted/30 border-2",children:e.jsxs(pe,{className:"pt-6 text-center",children:[e.jsx("div",{className:"bg-gradient-to-br from-primary to-primary/80 p-4 rounded-2xl w-fit mx-auto mb-4 shadow-lg",children:e.jsx(R,{className:"h-10 w-10 text-primary-foreground"})}),e.jsx("h2",{className:"font-bold text-xl mb-2",children:"AI Tutor"}),e.jsx("p",{className:"text-muted-foreground mb-6 max-w-md mx-auto",children:"I'm your personal math tutor powered by advanced AI. Upload a photo of your work, ask questions, and I'll help you understand step-by-step!"}),e.jsxs("div",{className:"flex flex-wrap gap-3 justify-center mb-6",children:[e.jsxs(r,{variant:"default",onClick:()=>{var s;return(s=z.current)==null?void 0:s.click()},className:"shadow-lg",children:[e.jsx(K,{className:"h-4 w-4 mr-2"}),"Upload Photo"]}),e.jsxs(r,{variant:"outline",onClick:O,children:[e.jsx(P,{className:"h-4 w-4 mr-2"}),"Take Photo"]})]}),e.jsxs("div",{className:"border-t pt-4",children:[e.jsx("p",{className:"text-sm text-muted-foreground mb-3",children:"Or try asking:"}),e.jsx("div",{className:"flex flex-wrap gap-2 justify-center",children:["Solve: 2x² + 5x - 3 = 0","Explain derivatives","How do I factor polynomials?"].map(s=>e.jsx(r,{variant:"secondary",size:"sm",onClick:()=>{var t;b(s),(t=A.current)==null||t.focus()},children:s},s))})]})]})}),n.map((s,t)=>e.jsxs("div",{className:`flex gap-3 ${s.role==="user"?"justify-end":""}`,children:[s.role==="assistant"&&e.jsx("div",{className:"bg-gradient-to-br from-primary to-primary/80 p-2 rounded-xl h-9 w-9 flex items-center justify-center shrink-0 shadow-md",children:e.jsx(R,{className:"h-5 w-5 text-primary-foreground"})}),e.jsxs("div",{className:`max-w-[85%] rounded-2xl ${s.role==="user"?"bg-primary text-primary-foreground p-4":"bg-card border shadow-sm p-4"}`,children:[s.imageUrl&&e.jsx("img",{src:s.imageUrl,alt:"Uploaded math work",className:"max-w-full max-h-48 rounded-lg mb-3"}),s.role==="assistant"?e.jsx(ye,{content:s.content,compact:!0,useGlobalPreference:!0}):e.jsx("p",{className:"whitespace-pre-wrap",children:s.content})]}),s.role==="user"&&e.jsx("div",{className:"bg-secondary p-2 rounded-xl h-9 w-9 flex items-center justify-center shrink-0",children:e.jsx(Se,{className:"h-5 w-5"})})]},t)),i&&((G=n[n.length-1])==null?void 0:G.role)==="user"&&e.jsxs("div",{className:"flex gap-3",children:[e.jsx("div",{className:"bg-gradient-to-br from-primary to-primary/80 p-2 rounded-xl h-9 w-9 flex items-center justify-center shadow-md",children:e.jsx(Z,{className:"h-5 w-5 text-primary-foreground animate-spin"})}),e.jsx("div",{className:"bg-card border shadow-sm p-4 rounded-2xl",children:e.jsxs("p",{className:"text-muted-foreground flex items-center gap-2",children:[e.jsx(Ie,{className:"h-4 w-4"}),"Analyzing and preparing solution..."]})})]}),n.length>0&&((J=n[n.length-1])==null?void 0:J.role)==="assistant"&&!i&&e.jsx("div",{className:"flex justify-center",children:e.jsxs(r,{variant:"outline",size:"sm",onClick:ne,className:"shadow-sm",children:[e.jsx(ke,{className:"h-4 w-4 mr-2"}),"Show me the full solution"]})})]})}),h&&e.jsx("div",{className:"border-t p-3 bg-muted/50",children:e.jsxs("div",{className:"max-w-4xl mx-auto flex items-center gap-3",children:[e.jsxs("div",{className:"relative",children:[e.jsx("img",{src:h,alt:"Selected",className:"h-20 rounded-lg object-cover shadow-md"}),e.jsx(r,{size:"icon",variant:"destructive",className:"absolute -top-2 -right-2 h-6 w-6",onClick:re,children:e.jsx(X,{className:"h-3 w-3"})})]}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"📸 Image ready! Add a question or send to analyze."})]})}),e.jsx("div",{className:"border-t p-4 bg-background/95 backdrop-blur",children:e.jsxs("div",{className:"max-w-4xl mx-auto flex gap-2",children:[e.jsx("input",{type:"file",ref:z,accept:"image/*",className:"hidden",onChange:te}),e.jsx(r,{variant:"outline",size:"icon",onClick:()=>{var s;return(s=z.current)==null?void 0:s.click()},disabled:i,title:"Upload image",children:e.jsx(K,{className:"h-4 w-4"})}),e.jsx(r,{variant:"outline",size:"icon",onClick:O,disabled:i,title:"Take photo",children:e.jsx(P,{className:"h-4 w-4"})}),e.jsx(xe,{ref:A,value:N,onChange:s=>b(s.target.value),onKeyDown:s=>s.key==="Enter"&&!s.shiftKey&&E(!1),placeholder:h?"Ask about this problem...":"Ask a math question...",disabled:i,className:"flex-1"}),e.jsx(r,{onClick:()=>E(!1),disabled:i||!N.trim()&&!h,children:i?e.jsx(Z,{className:"h-4 w-4 animate-spin"}):e.jsx(_e,{className:"h-4 w-4"})})]})})]})}export{es 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.