-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathchatbot-page.tsx
executable file
·117 lines (103 loc) · 3.64 KB
/
chatbot-page.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
'use client'
import { ChatList } from '@/components/chat-list'
import { useChat, type Message } from 'ai/react'
import cn from 'mxcn'
import { useState, useCallback } from 'react'
import { toast } from 'sonner'
import { ChatInput } from './chat-input'
import { Opening } from './opening'
import { Suggestions } from './suggestions'
export interface ChatProps extends React.ComponentProps<'div'> {
id?: string // Optional: Thread ID if you want to persist the chat in a DB
initialMessages?: Message[] // Optional: Messages to pre-populate the chat from DB
}
export function Chatbot({ id, initialMessages, className }: ChatProps) {
const [threadId, setThreadId] = useState<null | string>(null)
const [memorySets, setMemorySets] = useState<any[]>([])
const [selectedMemory, setSelectedMemory] = useState('')
const [userApiKey, setUserApiKey] = useState<string>('');
const [ownerLogin, setOwnerLogin] = useState<string>('');
const { messages, append, reload, stop, isLoading, input, setInput } =
useChat({
api: '/api/chat',
initialMessages,
body: { threadId },
onResponse(response) {
if (response.status !== 200) {
console.log('✨ ~ response:', response)
toast.error(response.statusText)
}
// Get Thread ID from response header
const lbThreadId = response.headers.get('lb-thread-id')
setThreadId(lbThreadId)
}
})
const fetchMemorySets = useCallback(async () => {
if (!userApiKey || !ownerLogin) {
toast.error('Please set both API Key and Owner Login first');
return;
}
try {
const response = await fetch('/api/chat?action=getMemorySets', {
method: 'POST',
body: JSON.stringify({ userApiKey, ownerLogin,
pipe: {
name: "career-prep-coach",
description: "Your AI-powered personal interview coach",
status: "private",
config: {
memorysets: [] // This will be populated when memory is selected
},
}
})
})
if (!response.ok) throw new Error('Failed to fetch memory sets')
const data = await response.json()
setMemorySets(data.memorySets || [])
toast.success('Memory sets refreshed successfully')
} catch (error) {
console.error('Error fetching memory sets:', error)
toast.error('Failed to fetch memory sets')
}
}, [userApiKey, ownerLogin])
const handleMemorySelect = useCallback((memoryUrl: string) => {
setSelectedMemory(memoryUrl)
}, [])
const sendSuggestedPrompt = (prompt: string) => {
setInput(prompt)
}
return (
<div className="min-h-screen">
<div className={cn('pb-36 pt-4 md:pt-10', className)}>
{messages.length ? (
<>
<ChatList messages={messages} />
</>
) : (
<>
<Opening />
<Suggestions sendSuggestedPrompt={sendSuggestedPrompt} />
</>
)}
<ChatInput
id={id}
isLoading={isLoading}
stop={stop}
append={append}
reload={reload}
messages={messages}
input={input}
setInput={setInput}
memorySets={memorySets}
selectedMemory={selectedMemory}
refreshMemorySets={fetchMemorySets}
onMemorySelect={handleMemorySelect}
userApiKey={userApiKey}
setUserApiKey={setUserApiKey}
ownerLogin={ownerLogin}
setOwnerLogin={setOwnerLogin}
/>
</div>
</div>
)
}