Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changes via feature branch #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/app/(auth)/auth/components/ExamplePrompts.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React from 'react';

type Prompt = {
text: string;
icon: string;
};

const prompts: Prompt[] = [
{ text: "Set a medicine reminder", icon: "💊" },
{ text: "Nutrition tips for seniors", icon: "🥗" },
];

interface ExamplePromptsProps {
onPromptClick: (prompt: string) => void;
}

export function ExamplePrompts({ onPromptClick }: ExamplePromptsProps) {
return (
<div className="my-4">
<h2 className="text-lg font-semibold mb-2">Example Prompts</h2>
<div className="grid grid-cols-2 gap-2 sm:grid-cols-3">
{prompts.map((prompt, index) => (
<button
key={index}
onClick={() => onPromptClick(prompt.text)}
className="flex items-center justify-center p-2 bg-gray-100 hover:bg-gray-200 rounded-lg transition-colors duration-200"
>
<span className="mr-2 text-xl">{prompt.icon}</span>
<span className="text-sm text-left">{prompt.text}</span>
</button>
))}
</div>
</div>
);
}
98 changes: 98 additions & 0 deletions src/components/Chat.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import React, { useState, useRef, useEffect } from 'react';
import { Loader2, Send, Upload } from "lucide-react";
import { ExamplePrompts } from './ExamplePrompts';
import { getLLMResponse } from '~/lib/llm-api';

type Message = {
id: string;
content: string;
role: 'user' | 'assistant';
attachment?: string;
};

export function Chat() {
const [messages, setMessages] = useState<Message[]>([]);
const [input, setInput] = useState('');
const [isLoading, setIsLoading] = useState(false);
const fileInputRef = useRef<HTMLInputElement>(null);
const chatEndRef = useRef<HTMLDivElement>(null);

useEffect(() => {
chatEndRef.current?.scrollIntoView({ behavior: 'smooth' });
}, [messages]);

const handlePromptClick = async (prompt: string) => {
await processMessage({
id: Date.now().toString(),
content: prompt,
role: 'user',
});
};

const handleSend = async () => {
if (input.trim() === '' && !fileInputRef.current?.files?.length) return;

const newUserMessage: Message = {
id: Date.now().toString(),
content: input,
role: 'user',
};

if (fileInputRef.current?.files?.length) {
const file = fileInputRef.current.files[0];
const reader = new FileReader();
reader.onload = async (e) => {
newUserMessage.attachment = e.target?.result as string;
await processMessage(newUserMessage);
};
reader.readAsDataURL(file);
} else {
await processMessage(newUserMessage);
}

setInput('');
if (fileInputRef.current) fileInputRef.current.value = '';
};

const processMessage = async (userMessage: Message) => {
setMessages(prev => [...prev, userMessage]);
setIsLoading(true);

try {
const assistantResponse = await getLLMResponse(userMessage.content, userMessage.attachment);
const newAssistantMessage: Message = {
id: (Date.now() + 1).toString(),
content: assistantResponse,
role: 'assistant',
};
setMessages(prev => [...prev, newAssistantMessage]);
} catch (error) {
console.error('Error getting LLM response:', error);
// Handle error (e.g., show error message to user)
} finally {
setIsLoading(false);
}
};

return (
<div className="flex flex-col h-[600px] border rounded-lg overflow-hidden">
<div className="flex-grow p-4 overflow-y-auto">
{messages.map((message) => (
<div key={message.id} className={`flex ${message.role === 'user' ? 'justify-end' : 'justify-start'} mb-4`}>
<div className={`flex items-start max-w-[70%] ${message.role === 'user' ? 'flex-row-reverse' : 'flex-row'}`}>
<div className={`p-3 rounded-lg ${message.role === 'user' ? 'bg-blue-500 text-white' : 'bg-gray-200'}`}>
{message.attachment && <img src={message.attachment} alt="Uploaded" className="max-w-full mb-2 rounded" />}
<p>{message.content}</p>
</div>
</div>
</div>
))}
<div ref={chatEndRef} />
</div>
<ExamplePrompts onPromptClick={handlePromptClick} isLoading={isLoading} />
<div className="p-4 border-t">
{/* ... (input and send button code) */}
</div>
</div>
);
}