'use client'; /*eslint-disable*/ import MessageBoxChat from '@/components/MessageBoxChat'; import DashboardLayout from '@/components/layout'; import Bg from '@/public/img/ai-chat/bg-image.png'; import { ChatBody } from '@/types/types'; import { Database } from '@/types_db'; import { Accordion, AccordionButton, AccordionIcon, AccordionItem, AccordionPanel, useColorModeValue, Box, Icon, Flex, Text, Input, Button, Image } from '@chakra-ui/react'; import { User } from '@supabase/supabase-js'; import { useState } from 'react'; import { MdAutoAwesome, MdBolt, MdEdit, MdPerson } from 'react-icons/md'; type Subscription = Database['public']['Tables']['subscriptions']['Row']; type Product = Database['public']['Tables']['products']['Row']; type Price = Database['public']['Tables']['prices']['Row']; interface ProductWithPrices extends Product { prices: Price[]; } interface PriceWithProduct extends Price { products: Product | null; } interface SubscriptionWithProduct extends Subscription { prices: PriceWithProduct | null; } interface Props { user: User | null | undefined; products: ProductWithPrices[]; subscription: SubscriptionWithProduct | null; userDetails: { [x: string]: any } | null; } export default function AiChat(props: Props) { // *** If you use .env.local variable for your API key, method which we recommend, use the apiKey variable commented below // Input States const [inputOnSubmit, setInputOnSubmit] = useState(''); const [inputMessage, setInputMessage] = useState(''); // Response message const [outputCode, setOutputCode] = useState(''); // ChatGPT model const [model, setModel] = useState('gpt-3.5-turbo'); // Loading state const [loading, setLoading] = useState(false); const gray = useColorModeValue('gray.500', 'white'); const brandColor = useColorModeValue('brand.500', 'white'); const opacityBg = useColorModeValue('white', 'whiteAlpha.100'); const gradientBg = useColorModeValue( 'linear-gradient(180deg, #FBFBFF 0%, #CACAFF 100%)', 'linear-gradient(180deg, rgba(255, 255, 255, 0.07) 0%, rgba(255, 255, 255, 0.31) 100%)' ); const gradientBg2 = useColorModeValue( 'linear-gradient(15.46deg, #4A25E1 26.3%, #7B5AFF 86.4%)', 'linear-gradient(180deg, rgba(255, 255, 255, 0.07) 0%, rgba(255, 255, 255, 0.31) 100%)' ); const shadow = useColorModeValue( '14px 27px 45px rgba(112, 144, 176, 0.2)', 'unset' ); const textColor = useColorModeValue('#120F43', 'white'); const placeholderColor = useColorModeValue( { color: 'gray.500' }, { color: 'whiteAlpha.600' } ); const borderColor = useColorModeValue('gray.200', 'whiteAlpha.200'); // API Key const handleTranslate = async () => { const apiKey = localStorage.getItem('apiKey'); setInputOnSubmit(inputMessage); // Chat post conditions(maximum number of characters, valid message etc.) const maxCodeLength = model === 'gpt-3.5-turbo' ? 700 : 700; if (!apiKey?.includes('sk-')) { alert('Please enter an API key.'); return; } if (!inputMessage) { alert('Please enter your subject.'); return; } if (inputMessage.length > maxCodeLength) { alert( `Please enter code less than ${maxCodeLength} characters. You are currently at ${inputMessage.length} characters.` ); return; } setOutputCode(' '); setLoading(true); const controller = new AbortController(); const body: ChatBody = { inputMessage, model, apiKey }; // -------------- Fetch -------------- const response = await fetch('/api/chatAPI', { method: 'POST', headers: { 'Content-Type': 'application/json' }, signal: controller.signal, body: JSON.stringify(body) }); if (!response.ok) { setLoading(false); if (response) { alert( 'Something went wrong went fetching from the API. Make sure to use a valid API key.' ); } return; } const data = response.body; if (!data) { setLoading(false); alert('Something went wrong'); return; } const reader = data.getReader(); const decoder = new TextDecoder(); let done = false; while (!done) { setLoading(true); const { value, done: doneReading } = await reader.read(); done = doneReading; const chunkValue = decoder.decode(value); setOutputCode((prevCode) => prevCode + chunkValue); } setLoading(false); }; // -------------- Copy Response -------------- // const copyToClipboard = (text: string) => { // const el = document.createElement('textarea'); // el.value = text; // document.body.appendChild(el); // el.select(); // document.execCommand('copy'); // document.body.removeChild(el); // }; const handleChange = (Event: any) => { setInputMessage(Event.target.value); }; return (  {/* Model Change */} setModel('gpt-3.5-turbo')} > GPT-3.5 setModel('gpt-4-1106-preview')} > GPT-4 No plugins added This is a cool text example. {/* Main Box */} {inputOnSubmit} {/* Chat Input */} Free Research Preview. ChatGPT may produce inaccurate information about people, places, or facts. Consider checking important information. ); }