import { useSession } from '@components/Contexts/SessionContext' import { sendActivityAIChatMessage, startActivityAIChatSession } from '@services/ai/ai'; import Avvvatars from 'avvvatars-react'; import { motion, AnimatePresence } from 'framer-motion'; import { FlaskConical, Keyboard, MessageCircle, Sparkle, Sparkles, X } from 'lucide-react' import Image from 'next/image'; import { send } from 'process'; import learnhouseAI_icon from "public/learnhouse_ai_simple.png"; import learnhouseAI_logo_black from "public/learnhouse_ai_black_logo.png"; import React, { useEffect, useRef } from 'react' type AIActivityAskProps = { activity: any; } function AIActivityAsk(props: AIActivityAskProps) { const [isAIModalOpen, setIsAIModalOpen] = React.useState(false); return (
setIsAIModalOpen(true)} style={{ background: 'conic-gradient(from 32deg at 53.75% 50%, rgb(35, 40, 93) 4deg, rgba(20, 0, 52, 0.95) 59deg, rgba(164, 45, 238, 0.88) 281deg)', }} className="rounded-full px-5 drop-shadow-md flex items-center space-x-1 p-2.5 text-sm text-white hover:cursor-pointer transition delay-150 duration-300 ease-in-out hover:scale-105"> {" "} {" "} Ask AI
) } type Message = { sender: string; message: any; type: 'ai' | 'user'; } type ActivityChatMessageBoxProps = { activity: any; isAIModalOpen?: boolean; setIsAIModalOpen?: any; } function ActivityChatMessageBox(props: ActivityChatMessageBoxProps) { const session = useSession() as any; const [messages, setMessages] = React.useState([]) as [Message[], any]; const [aichat_uuid, setAichat_uuid] = React.useState(''); const [isWaitingForResponse, setIsWaitingForResponse] = React.useState(false); const [chatInputValue, setChatInputValue] = React.useState('') as [string, any]; // TODO : come up with a better way to handle this const inputClass = isWaitingForResponse ? 'ring-1 ring-inset ring-white/10 bg-transparent w-full rounded-lg outline-none px-4 py-2 text-white text-sm placeholder:text-white/30 opacity-30 ' : 'ring-1 ring-inset ring-white/10 bg-transparent w-full rounded-lg outline-none px-4 py-2 text-white text-sm placeholder:text-white/30'; useEffect(() => { if (props.isAIModalOpen) { document.body.style.overflow = 'hidden'; } else { document.body.style.overflow = 'unset'; } }, [props.isAIModalOpen]); function handleKeyDown(event: React.KeyboardEvent) { if (event.key === 'Enter') { // Perform the sending action here sendMessage(event.currentTarget.value); } } const handleChange = (event: React.ChangeEvent) => { setChatInputValue(event.target.value); } const sendMessage = async (message: string) => { if (aichat_uuid) { setMessages((messages: any) => [...messages, { sender: session.user.user_uuid, message: message, type: 'user' }]); setIsWaitingForResponse(true); const response = await sendActivityAIChatMessage(message, aichat_uuid, props.activity.activity_uuid) setIsWaitingForResponse(false); setChatInputValue(''); setMessages((messages: any) => [...messages, { sender: 'ai', message: response.message, type: 'ai' }]); } else { setMessages((messages: any) => [...messages, { sender: session.user.user_uuid, message: message, type: 'user' }]); setIsWaitingForResponse(true); const response = await startActivityAIChatSession(message, props.activity.activity_uuid) setAichat_uuid(response.aichat_uuid); setIsWaitingForResponse(false); setChatInputValue(''); setMessages((messages: any) => [...messages, { sender: 'ai', message: response.message, type: 'ai' }]); } } function closeModal() { props.setIsAIModalOpen(false); } const messagesEndRef = useRef(null); useEffect(() => { if (messagesEndRef.current) { messagesEndRef.current.scrollIntoView({ behavior: 'smooth' }); } }, [messages, session]); return ( {props.isAIModalOpen && (
Learnhouse AI
Experimental
{messages.length > 0 ? (
{messages.map((message: Message, index: number) => { return ( ) })}
) : ( )}
sendMessage(chatInputValue)} />
)} ) } type AIMessageProps = { message: Message; animated: boolean; } function AIMessage(props: AIMessageProps) { const session = useSession() as any; const words = props.message.message.split(' '); return (

{words.map((word: string, i: number) => ( {word + ' '} ))}

) } const AIMessagePlaceHolder = (props: { activity_uuid: string, sendMessage: any }) => { const session = useSession() as any; return (

Hello {session.user.username}, How can we help today ?

props.sendMessage('Explain this Course in Simple examples.')} className='py-1.5 px-6 text-xs font-semibold text-white/30 rounded-full shadow-2xl bg-black/20 mb-3 outline outline-gray-400/5 cursor-pointer'>Explain in simple examples props.sendMessage('Generate flashcards about this course and this lecture.')} className='py-1.5 px-6 text-xs font-semibold text-white/30 rounded-full shadow-2xl bg-black/20 mb-3 outline outline-gray-400/5 cursor-pointer'>Generate flashcards props.sendMessage('Break down this Course in concepts.')} className='py-1.5 px-6 text-xs font-semibold text-white/30 rounded-full shadow-2xl bg-black/20 mb-3 outline outline-gray-400/5 cursor-pointer'>Break down in concepts
) } export default AIActivityAsk