Skip to content
Merged
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
655 changes: 655 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@radix-ui/react-dialog": "^1.1.6",
"@radix-ui/react-label": "^2.1.2",
"@radix-ui/react-navigation-menu": "^1.2.5",
"@radix-ui/react-popover": "^1.1.15",
"@radix-ui/react-slot": "^1.1.2",
"@radix-ui/react-tabs": "^1.1.3",
"class-variance-authority": "^0.7.1",
Expand All @@ -35,13 +36,13 @@
"devDependencies": {
"@eslint/eslintrc": "^3",
"@tailwindcss/postcss": "^4",
"@trivago/prettier-plugin-sort-imports": "^5.2.0",
"@types/node": "^20",
"@types/react": "19.0.12",
"@types/react-dom": "^19",
"eslint": "^9",
"eslint-config-next": "15.2.1",
"tailwindcss": "^4",
"typescript": "5.8.2",
"@trivago/prettier-plugin-sort-imports": "^5.2.0"
"typescript": "5.8.2"
}
}
24 changes: 24 additions & 0 deletions src/app/eme/chat-bubble.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Message } from "@/constants/eme"
export function ChatBubble({ msg, index }: {msg: Message, index: number}) {
return (
<div
key={`${msg.id}-${index}`}
className={`flex py-1 ${msg.sender === 'bot' ? 'justify-start' : 'justify-end'}`}
>
<div
className={
"rounded-2xl p-3 text-sm md:text-base max-w-[80%] " +
(msg.sender === 'bot'
? "bg-neutral-950 border-2 border-zinc-800 text-white shadow-sm"
: "bg-white/90 border border-zinc-300 text-black shadow-sm")
}
role="article"
aria-label={msg.sender === 'bot' ? 'Bot message' : 'User message'}
>
<div className="whitespace-pre-wrap leading-relaxed">
{msg.text}
</div>
</div>
</div>
)
}
34 changes: 34 additions & 0 deletions src/app/eme/fetch-eme.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { EME_API_BASE_URL } from "@/constants/eme";

export interface UseEmeResult {
generation: string,
isLoading: boolean,
isError: boolean,
}

export async function fetchEmeResponse(message: string): Promise<ReadableStream<Uint8Array>> {

const response = await fetch(`${EME_API_BASE_URL}/chat`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message }),
});

if (!response.ok) throw new Error(`eme responded with status ${response.status}`);

if (!response.body) throw new Error(`No response body`);

return response.body;

// const body = await res.json();
// const generation = typeof body === 'string' ? body : body?.generation ?? String(body);
// return { generation };
}

export async function fetchEmeHealth(): Promise<{ status: string}> {
const response = await fetch(`${EME_API_BASE_URL}/health`);

if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);

return response.json();
}
167 changes: 167 additions & 0 deletions src/app/eme/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
"use client"
import { useState } from "react";
import { Message, thinkingMessage } from "@/constants/eme";
import { fetchEmeResponse, fetchEmeHealth } from "./fetch-eme";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
import { Button } from "@/components/ui/button";
import { ChatBubble } from "./chat-bubble";

export default function emePage() {
const [messages, setMessages] = useState<Message[]>([])
const [prompt, setPrompt] = useState("")
const [isLoading, setIsLoading] = useState(false)
const [isStreaming, setIsStreaming] = useState(false)

const isHealthCheck = false;

const submitPrompt = async (e: React.FormEvent) => {
e.preventDefault();

if(isHealthCheck) {
healthCheck();
return;
}

if (isLoading || prompt.trim() === "") return;

const userMsg: Message = {
id: Date.now().toString(),
text: prompt,
sender: "user",
timestamp: new Date(),
};

setMessages(prev => [...prev, userMsg]);
setIsLoading(true);
setPrompt("");

try {
const stream = await fetchEmeResponse(prompt);
const reader = stream.getReader();
const decoder = new TextDecoder();

let botResponse = '';
const botMsg: Message = {
id: Date.now().toString(),
text: '',
sender: "bot",
timestamp: new Date(),
};

setIsStreaming(true);
setMessages(prev => [...prev, botMsg]);
setIsLoading(false);

while (true) {
const { done, value} = await reader.read();
if (done) break;

const chunk = decoder.decode(value, { stream: true});
botResponse += chunk;

// Update message with accumulated text
setMessages((prev) =>
prev.map((msg) => (msg.id === botMsg.id ? { ...msg, text: botResponse } : msg)),
);
}
} catch (error) {
console.error("Error retrieving eme output:", error);

const errorMsg: Message = {
id: (Date.now() + 1).toString(),
text: 'Sorry, I encountered an error. Please try again.',
sender: 'bot',
timestamp: new Date(),
}
setMessages((prev) => [...prev, errorMsg]);
} finally {
setIsLoading(false);
setIsStreaming(false);
}
}

const healthCheck = async () => {
try {
const response = await fetchEmeHealth();
console.log("Health is: ", response);
} catch(error) {
console.error("Error retrieving eme health: ", error);
}
}

return (
<div className="flex flex-col justify-center items-center px-30 py-40 space-y-4">
<h1 className="text-white text-center md:text-left text-5xl md:text-7xl font-bold tracking-tight leading-tight ">
eme
</h1>
<Popover>
<PopoverTrigger asChild className="text-m leading-relaxed text-gray-400 font-mono text-center">
<Button>About</Button>
</PopoverTrigger>
<PopoverContent
className="flex flex-col items-center justify-center w-[clamp(22rem,70vw,40rem)] border-gray-800 z-50 bg-black/95 shadow-xl p-6 outline-none"
>
<div className="z-50 outline-none flex flex-col space-y-10 w-full">
<p className="text-m leading-relaxed text-gray-400 font-mono text-center">
eme is the EMCO chatbot that answers your questions about navigating CS at Northwestern. We hope it can be another point of reference for underclass students getting started.
</p>
<p className="text-m leading-relaxed text-gray-400 font-mono text-center">
Answers are based on historical EMCO GroupMe messages. eme uses {" "}
<a href="https://en.wikipedia.org/wiki/Retrieval-augmented_generation" className="underline">
RAG
</a>
{" "} to pull relevant messages into its context.
</p>
<p className="text-m leading-relaxed text-gray-400 font-mono text-center">
NOTE: eme can make mistakes. Emerging Coders, its affiliated members and Executive Board do not endorse its messages.
</p>
</div>
</PopoverContent>
</Popover>

<div className="bg-[#202020] py-6 px-10 rounded w-3/4">
<div className="w-full space-y-3">
<div className="flex flex-col">
{messages.length === 0 &&
<p className="text-m leading-relaxed text-gray-400 font-mono text-center py-20">
Ask me something like "should I take CS214 and CS211 at the same time?"
</p>
}
</div>
{messages.map((msg, i) => (
<ChatBubble key={msg.id} msg={msg} index={i} />
))}
{isLoading &&
<ChatBubble
msg={thinkingMessage}
index={messages.length + 1}
/>
}
</div>

<div className="flex flex-col w-full my-6">
<form
onSubmit={submitPrompt}
className="flex flex-row space-x-4"
>
<input
type="text"
value={prompt}
placeholder="Ask eme a question..."
onChange={(e) => setPrompt(e.target.value)}
className="w-full bg-black border-zinc-800 text-white focus-visible:ring-purple-500/30 px-5 py-2 rounded focus:outline-none"
disabled={isLoading}
/>
<button
type="submit"
className="bg-white text-black font-bold text-base font-mono py-4 px-6 rounded-md hover:bg-gray-200 transition-colors flex items-center mx-auto md:mx-0 disabled:opacity-50 disabled:cursor-not-allowed cursor-pointer"
disabled={!prompt.trim() || isLoading}
>
Ask
</button>
</form>
</div>
</div>
</div>
)
}
2 changes: 1 addition & 1 deletion src/app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
}

:root {
--background: oklch(1 0 0);
--backgrdound: oklch(1 0 0);
--foreground: oklch(0.141 0.005 285.823);
--card: oklch(1 0 0);
--card-foreground: oklch(0.141 0.005 285.823);
Expand Down
6 changes: 6 additions & 0 deletions src/components/navigation/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ export default function Header() {
</NavigationMenuItem>
</NavigationMenuList>
</NavigationMenu>
<Link
href='/eme'
className='text-white/90 hover:text-white transition-colors px-3 py-2 text-base font-mono'
>
EME
</Link>
<Link
href='/contact'
className='text-white/90 hover:text-white transition-colors px-3 py-2 text-base font-mono'
Expand Down
48 changes: 48 additions & 0 deletions src/components/ui/popover.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"use client"

import * as React from "react"
import * as PopoverPrimitive from "@radix-ui/react-popover"

import { cn } from "@/lib/utils"

function Popover({
...props
}: React.ComponentProps<typeof PopoverPrimitive.Root>) {
return <PopoverPrimitive.Root data-slot="popover" {...props} />
}

function PopoverTrigger({
...props
}: React.ComponentProps<typeof PopoverPrimitive.Trigger>) {
return <PopoverPrimitive.Trigger data-slot="popover-trigger" {...props} />
}

function PopoverContent({
className,
align = "center",
sideOffset = 4,
...props
}: React.ComponentProps<typeof PopoverPrimitive.Content>) {
return (
<PopoverPrimitive.Portal>
<PopoverPrimitive.Content
data-slot="popover-content"
align={align}
sideOffset={sideOffset}
className={cn(
"bg-popover text-popover-foreground data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 z-50 w-72 origin-(--radix-popover-content-transform-origin) rounded-md border p-4 shadow-md outline-hidden",
className
)}
{...props}
/>
</PopoverPrimitive.Portal>
)
}

function PopoverAnchor({
...props
}: React.ComponentProps<typeof PopoverPrimitive.Anchor>) {
return <PopoverPrimitive.Anchor data-slot="popover-anchor" {...props} />
}

export { Popover, PopoverTrigger, PopoverContent, PopoverAnchor }
29 changes: 29 additions & 0 deletions src/constants/eme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export const EME_API_BASE_URL = 'https://eme-yr7i.onrender.com'

export interface Message {
id: string;
text: string;
sender: 'user' | 'bot';
timestamp: Date;
}

export const botDummyMessage: Message = {
id: Date.now().toString(),
text: "CS336 is generally considered to be a challenging course. Students have mentioned that it involves a mix of coding and proofs, which can make it harder compared to other classes. However, many also note that if you engage with the material and focus on understanding concepts like greedy algorithms and dynamic programming, you can gain a solid grasp of these important topics. It's recommended to take it if you're looking to prepare for technical interviews, but be prepared for the workload and complexity. If you're unsure, you might want to ask others about their specific experiences or tips for succeeding in the course.",
sender: 'bot',
timestamp: new Date()
};

export const userDummyMessage: Message = {
id: Date.now().toString(),
text: "Is CS336 a difficult course?",
sender: 'user',
timestamp: new Date()
};

export const thinkingMessage: Message = {
id: Date.now().toString(),
text: "eme is thinking...",
sender: 'bot',
timestamp: new Date()
};
Loading