Skip to content

Commit 798c0ea

Browse files
author
thorwebdev
committed
feat: llama3 chat completion example.
1 parent 5a8fcfe commit 798c0ea

File tree

9 files changed

+2533
-2288
lines changed

9 files changed

+2533
-2288
lines changed

app/chat/[id]/page.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,11 @@ export default async function ChatPage({ params }: ChatPageProps) {
4949
notFound()
5050
}
5151

52-
return <Chat id={chat.id} initialMessages={chat.messages} />
52+
return (
53+
<Chat
54+
id={chat.id}
55+
initialMessages={chat.messages}
56+
supaAccessToken={session.access_token}
57+
/>
58+
)
5359
}

app/page.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import { auth } from '@/auth'
2+
import { cookies } from 'next/headers'
13
import { nanoid } from '@/lib/utils'
24
import { Chat } from '@/components/chat'
35

46
export const runtime = 'edge'
57

6-
export default function IndexPage() {
8+
export default async function IndexPage() {
79
const id = nanoid()
10+
const cookieStore = cookies()
11+
const session = await auth({ cookieStore })
812

9-
return <Chat id={id} />
13+
return <Chat id={id} supaAccessToken={session?.access_token ?? ''} />
1014
}

components/chat.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,15 @@ const IS_PREVIEW = process.env.VERCEL_ENV === 'preview'
2525
export interface ChatProps extends React.ComponentProps<'div'> {
2626
initialMessages?: Message[]
2727
id?: string
28+
supaAccessToken: string
2829
}
2930

30-
export function Chat({ id, initialMessages, className }: ChatProps) {
31+
export function Chat({
32+
id,
33+
initialMessages,
34+
supaAccessToken,
35+
className
36+
}: ChatProps) {
3137
const [previewToken, setPreviewToken] = useLocalStorage<string | null>(
3238
'ai-token',
3339
null
@@ -36,6 +42,8 @@ export function Chat({ id, initialMessages, className }: ChatProps) {
3642
const [previewTokenInput, setPreviewTokenInput] = useState(previewToken ?? '')
3743
const { messages, append, reload, stop, isLoading, input, setInput } =
3844
useChat({
45+
api: 'http://127.0.0.1:54321/functions/v1/vercel-ai-chat', // Route chat to supabase edge function
46+
headers: { Authorization: `Bearer ${supaAccessToken}` }, // Auth header to secure the function invocation
3947
initialMessages,
4048
id,
4149
body: {

0 commit comments

Comments
 (0)