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

change structure and refactor a bit #62

Merged
merged 3 commits into from
Jul 19, 2024
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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,7 @@ log
.DS_Store

#csv
*.csv
*.csv

#vscode
.vscode
16 changes: 0 additions & 16 deletions ai_course_bot/ai-chatbot/app/(chat)/layout.tsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { GET, POST } from '@/auth'
export { GET, POST } from '@/tai/utils/auth'
export const runtime = 'edge'
49 changes: 26 additions & 23 deletions ai_course_bot/ai-chatbot/app/api/chat/route.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
import { auth } from '@/auth'
import { auth } from '@/tai/utils/auth'
import { kv } from '@vercel/kv'
import { nanoid } from '@/lib/utils'
import { nanoid } from '@/tai/lib/utils'

export const runtime = 'edge'

export async function POST(req: Request) {
const json = await req.json();
const json = await req.json()

// console.log("[Route.ts] Request Body: \n", json);
const { messages, previewToken } = json;
const { messages, previewToken } = json
var courseId = messages[messages.length - 1].tool_call_id

const userId: string = (await auth())?.user.email ?? "";
const userId: string = (await auth())?.user.email ?? ''

if (courseId == null || userId == "") {
courseId = "default"
}
if (courseId == null || userId == '') {
courseId = 'default'
}

// Assuming your API URL and it might require an API key in headers
var apiHost:string = process.env['ROAR_BACKEND_HOST'] || "http://0.0.0.0:9000";
var apiHost: string =
process.env['ROAR_BACKEND_HOST'] || 'http://0.0.0.0:9000'

if (courseId == "CS 61A") {
if (courseId == 'CS 61A') {
apiHost = process.env['CS_61A_BACKEND_HOST'] || apiHost
} else if (courseId == "EE 106b") {
} else if (courseId == 'EE 106b') {
apiHost = process.env['EE106B_BACKEND_HOST'] || apiHost
}

const apiUrl: string = apiHost + '/api/chat/completions';
const apiUrl: string = apiHost + '/api/chat/completions'

try {
var body = JSON.stringify({
Expand All @@ -36,29 +37,31 @@ export async function POST(req: Request) {
temperature: 0.7,
stream: true,
userId: userId
});
})

// console.log("[Route.ts] Request Body: \n", body);

const apiResponse = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Type': 'application/json'
},
body: body
});
})

if (apiResponse.ok) {
if (apiResponse.ok) {
return new Response(apiResponse.body, {
headers: { 'Content-Type': 'application/json' },
});
headers: { 'Content-Type': 'application/json' }
})
} else {
console.log("[Route.ts] API Response Not Ok");
return new Response('Error fetching data', { status: apiResponse.status });
console.log('[Route.ts] API Response Not Ok')
return new Response('Error fetching data', { status: apiResponse.status })
}
} catch (error) {
console.log("[Route.ts] Error: ", error);
return new Response('Internal Server Error; Server may be down, Please try again later', {headers: { 'Content-Type': 'application/json' }});
console.log('[Route.ts] Error: ', error)
return new Response(
'Internal Server Error; Server may be down, Please try again later',
{ headers: { 'Content-Type': 'application/json' } }
)
}
}

36 changes: 20 additions & 16 deletions ai_course_bot/ai-chatbot/app/api/chat/save/route.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import { nanoid } from '@/lib/utils'
import { nanoid } from '@/tai/lib/utils'
import { kv } from '@vercel/kv'
import { auth } from '@/auth'
import { auth } from '@/tai/utils/auth'

export async function POST(req: Request) {
const json = await req.json();
// console.log("[POST /api/chat/save] Request JSON: \n", json);
const userId: string = (await auth())?.user.id ?? nanoid();
const title = json.messages[0].content.substring(0, 100);
saveChat(title, json.messages, userId, json.id);
return new Response();
const json = await req.json()
// console.log("[POST /api/chat/save] Request JSON: \n", json);
const userId: string = (await auth())?.user.id ?? nanoid()
const title = json.messages[0].content.substring(0, 100)
saveChat(title, json.messages, userId, json.id)
return new Response()
}


// function to save chat
async function saveChat(title: string, messages: any, userId: string, id: string) {
// console.log("[POST /api/chat/save] Saving chat");
// console.log("Title: ", title);
// console.log("Messages: ", messages);
// console.log("UserId: ", userId);
// console.log("Id: ", id);
async function saveChat(
title: string,
messages: any,
userId: string,
id: string
) {
// console.log("[POST /api/chat/save] Saving chat");
// console.log("Title: ", title);
// console.log("Messages: ", messages);
// console.log("UserId: ", userId);
// console.log("Id: ", id);
const createdAt = Date.now()
const path = `/chat/${id}`
const payload = {
Expand All @@ -36,4 +40,4 @@ async function saveChat(title: string, messages: any, userId: string, id: string
})

// console.log("[POST /api/chat/save] Chat saved successfully: ", payload);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { type Metadata } from 'next'
import { notFound, redirect } from 'next/navigation'

import { auth } from '@/auth'
import { getChat } from '@/app/actions'
import { Chat } from '@/components/chat'
import { auth } from '@/tai/utils/auth'
import { getChat } from '@/tai/utils/actions'
import { Chat } from '@/tai/components/chat'

export interface ChatPageProps {
params: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import * as React from 'react'

import { cn } from '@/lib/utils'
import { useAtBottom } from '@/lib/hooks/use-at-bottom'
import { Button, type ButtonProps } from '@/components/ui/button'
import { IconArrowDown } from '@/components/ui/icons'
import { cn } from '@/tai/lib/utils'
import { useAtBottom } from '@/tai/lib/hooks/use-at-bottom'
import { Button, type ButtonProps } from '@/tai/components/ui/button'
import { IconArrowDown } from '@/tai/components/ui/icons'

export function ButtonScrollToBottom({ className, ...props }: ButtonProps) {
const isAtBottom = useAtBottom()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@

import * as React from 'react'

import Link from 'next/link'

import { cn } from '@/lib/utils'
import { SidebarList } from '@/components/sidebar-list'
import { buttonVariants } from '@/components/ui/button'
import { IconPlus } from '@/components/ui/icons'
import { cn } from '@/tai/lib/utils'
import { SidebarList } from '@/tai/components/sidebar-list'
import { buttonVariants } from '@/tai/components/ui/button'
import { IconPlus } from '@/tai/components/ui/icons'
interface ChatHistoryProps {
userId?: string
}
Expand All @@ -15,16 +14,15 @@ export async function ChatHistory({ userId }: ChatHistoryProps) {
return (
<div className="flex flex-col h-full">
<div className="px-2 my-4">

<Link
href="/"
className={cn(
buttonVariants({ variant: 'outline' }),
'h-10 w-full justify-start bg-zinc-50 px-4 shadow-none transition-colors hover:bg-zinc-200/40 dark:bg-zinc-900 dark:hover:bg-zinc-300/10'
)}
>
<IconPlus className="-translate-x-2 stroke-2" />
New Chat
<IconPlus className="-translate-x-2 stroke-2" />
New Chat
</Link>
</div>
<React.Suspense
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type Message } from 'ai'

import { Separator } from '@/components/ui/separator'
import { ChatMessage } from '@/components/chat-message'
import { Separator } from '@/tai/components/ui/separator'
import { ChatMessage } from '@/tai/components/chat-message'

export interface ChatList {
messages: Message[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import { type Message } from 'ai'

import { Button } from '@/components/ui/button'
import { IconCheck, IconCopy } from '@/components/ui/icons'
import { useCopyToClipboard } from '@/lib/hooks/use-copy-to-clipboard'
import { cn } from '@/lib/utils'
import { Button } from '@/tai/components/ui/button'
import { IconCheck, IconCopy } from '@/tai/components/ui/icons'
import { useCopyToClipboard } from '@/tai/lib/hooks/use-copy-to-clipboard'
import { cn } from '@/tai/lib/utils'

interface ChatMessageActionsProps extends React.ComponentProps<'div'> {
message: Message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { Message } from 'ai'
import remarkGfm from 'remark-gfm'
import remarkMath from 'remark-math'

import Image from 'next/image'
import { cn } from '@/lib/utils'
import { CodeBlock } from '@/components/ui/codeblock'
import { MemoizedReactMarkdown } from '@/components/markdown'
import { IconUser } from '@/components/ui/icons'
import { ChatMessageActions } from '@/components/chat-message-actions'
import Image from 'next/image'
import { cn } from '@/tai/lib/utils'
import { CodeBlock } from '@/tai/components/ui/codeblock'
import { MemoizedReactMarkdown } from '@/tai/components/markdown'
import { IconUser } from '@/tai/components/ui/icons'
import { ChatMessageActions } from '@/tai/components/chat-message-actions'

export interface ChatMessageProps {
message: Message
Expand All @@ -30,7 +30,11 @@ export function ChatMessage({ message, ...props }: ChatMessageProps) {
: 'bg-primary text-primary-foreground'
)}
>
{message.role === 'user' ? <IconUser /> : <Image src="/TAI_prompt.png" alt="logo" width={100} height={100} />}
{message.role === 'user' ? (
<IconUser />
) : (
<Image src="/TAI_prompt.png" alt="logo" width={100} height={100} />
)}
</div>
<div className="flex-1 px-1 ml-4 space-y-2 overflow-hidden">
<MemoizedReactMarkdown
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use client';
'use client'
import * as React from 'react'
import { type UseChatHelpers } from 'ai/react'

import { shareChat } from '@/app/actions'
import { Button } from '@/components/ui/button'
import { PromptForm } from '@/components/prompt-form'
import { ButtonScrollToBottom } from '@/components/button-scroll-to-bottom'
import { IconRefresh, IconShare, IconStop } from '@/components/ui/icons'
import { FooterText } from '@/components/footer'
import { ChatShareDialog } from '@/components/chat-share-dialog'
import { loadData } from '@/lib/utils'
import { shareChat } from '@/tai/utils/actions'
import { Button } from '@/tai/components/ui/button'
import { PromptForm } from '@/tai/components/prompt-form'
import { ButtonScrollToBottom } from '@/tai/components/button-scroll-to-bottom'
import { IconRefresh, IconShare, IconStop } from '@/tai/components/ui/icons'
import { FooterText } from '@/tai/components/footer'
import { ChatShareDialog } from '@/tai/components/chat-share-dialog'
import { loadData } from '@/tai/lib/utils'
export interface ChatPanelProps
extends Pick<
UseChatHelpers,
Expand Down Expand Up @@ -88,7 +88,7 @@ export function ChatPanel({
<div className="px-4 py-2 space-y-4 border-t shadow-lg bg-background sm:rounded-t-xl sm:border md:py-4">
<PromptForm
onSubmit={async value => {
var data = loadData('selectedCourse');
var data = loadData('selectedCourse')
await append({
id,
content: value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import * as React from 'react'
import { useInView } from 'react-intersection-observer'

import { useAtBottom } from '@/lib/hooks/use-at-bottom'
import { useAtBottom } from '@/tai/lib/hooks/use-at-bottom'

interface ChatScrollAnchorProps {
trackVisibility?: boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import * as React from 'react'
import { type DialogProps } from '@radix-ui/react-dialog'
import { toast } from 'react-hot-toast'

import { ServerActionResult, type Chat } from '@/lib/types'
import { Button } from '@/components/ui/button'
import { ServerActionResult, type Chat } from '@/tai/lib/types'
import { Button } from '@/tai/components/ui/button'
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle
} from '@/components/ui/dialog'
import { IconSpinner } from '@/components/ui/icons'
import { useCopyToClipboard } from '@/lib/hooks/use-copy-to-clipboard'
} from '@/tai/components/ui/dialog'
import { IconSpinner } from '@/tai/components/ui/icons'
import { useCopyToClipboard } from '@/tai/lib/hooks/use-copy-to-clipboard'

interface ChatShareDialogProps extends DialogProps {
chat: Pick<Chat, 'id' | 'title' | 'messages'>
Expand Down
Loading
Loading