-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
151 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,9 @@ | ||
"use client"; | ||
|
||
import React from "react"; | ||
|
||
import { ChatRequestOptions } from "ai"; | ||
import { useChat } from "ai/react"; | ||
import { toast } from "sonner"; | ||
import useLocalStorageState from "use-local-storage-state"; | ||
import { v4 as uuidv4 } from "uuid"; | ||
|
||
import { ChatLayout } from "@/components/chat/chat-layout"; | ||
import { ChatOptions } from "@/components/chat/chat-options"; | ||
import { basePath } from "@/lib/utils"; | ||
import ChatPage from "@/components/chat/chat-page"; | ||
|
||
export default function Home() { | ||
const { | ||
messages, | ||
input, | ||
handleInputChange, | ||
handleSubmit, | ||
isLoading, | ||
error, | ||
stop, | ||
setMessages, | ||
} = useChat({ | ||
api: basePath + "/api/chat", | ||
onError: (error) => { | ||
toast.error("Something went wrong: " + error); | ||
}, | ||
}); | ||
const [chatId, setChatId] = React.useState<string>(""); | ||
const [chatOptions, setChatOptions] = useLocalStorageState<ChatOptions>( | ||
"chatOptions", | ||
{ | ||
defaultValue: { | ||
selectedModel: "", | ||
systemPrompt: "", | ||
temperature: 0.9, | ||
}, | ||
} | ||
); | ||
|
||
React.useEffect(() => { | ||
if (!isLoading && !error && chatId && messages.length > 0) { | ||
// Save messages to local storage | ||
localStorage.setItem(`chat_${chatId}`, JSON.stringify(messages)); | ||
// Trigger the storage event to update the sidebar component | ||
window.dispatchEvent(new Event("storage")); | ||
} | ||
}, [messages, chatId, isLoading, error]); | ||
|
||
const onSubmit = (e: React.FormEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
|
||
if (messages.length === 0) { | ||
// Generate a random id for the chat | ||
const id = uuidv4(); | ||
setChatId(id); | ||
} | ||
|
||
setMessages([...messages]); | ||
|
||
// Prepare the options object with additional body data, to pass the model. | ||
const requestOptions: ChatRequestOptions = { | ||
options: { | ||
body: { | ||
chatOptions: chatOptions, | ||
}, | ||
}, | ||
}; | ||
|
||
// Call the handleSubmit function with the options | ||
handleSubmit(e, requestOptions); | ||
}; | ||
|
||
return ( | ||
<main className="flex h-[calc(100dvh)] flex-col items-center "> | ||
<ChatLayout | ||
chatId="" | ||
chatOptions={chatOptions} | ||
setChatOptions={setChatOptions} | ||
messages={messages} | ||
input={input} | ||
handleInputChange={handleInputChange} | ||
handleSubmit={onSubmit} | ||
isLoading={isLoading} | ||
error={error} | ||
stop={stop} | ||
navCollapsedSize={10} | ||
defaultLayout={[30, 160]} | ||
/> | ||
</main> | ||
); | ||
return <ChatPage chatId={chatId} setChatId={setChatId} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
"use client"; | ||
|
||
import React from "react"; | ||
|
||
import { ChatRequestOptions } from "ai"; | ||
import { useChat } from "ai/react"; | ||
import { toast } from "sonner"; | ||
import useLocalStorageState from "use-local-storage-state"; | ||
import { v4 as uuidv4 } from "uuid"; | ||
|
||
import { ChatLayout } from "@/components/chat/chat-layout"; | ||
import { ChatOptions } from "@/components/chat/chat-options"; | ||
import { basePath } from "@/lib/utils"; | ||
|
||
interface ChatPageProps { | ||
chatId: string; | ||
setChatId: React.Dispatch<React.SetStateAction<string>>; | ||
} | ||
export default function ChatPage({ chatId, setChatId }: ChatPageProps) { | ||
const { | ||
messages, | ||
input, | ||
handleInputChange, | ||
handleSubmit, | ||
isLoading, | ||
error, | ||
stop, | ||
setMessages, | ||
} = useChat({ | ||
api: basePath + "/api/chat", | ||
onError: (error) => { | ||
toast.error("Something went wrong: " + error); | ||
}, | ||
}); | ||
const [chatOptions, setChatOptions] = useLocalStorageState<ChatOptions>( | ||
"chatOptions", | ||
{ | ||
defaultValue: { | ||
selectedModel: "", | ||
systemPrompt: "", | ||
temperature: 0.9, | ||
}, | ||
} | ||
); | ||
|
||
React.useEffect(() => { | ||
if (chatId) { | ||
const item = localStorage.getItem(`chat_${chatId}`); | ||
if (item) { | ||
setMessages(JSON.parse(item)); | ||
} | ||
} else { | ||
setMessages([]); | ||
} | ||
}, [setMessages, chatId]); | ||
|
||
React.useEffect(() => { | ||
if (!isLoading && !error && chatId && messages.length > 0) { | ||
// Save messages to local storage | ||
localStorage.setItem(`chat_${chatId}`, JSON.stringify(messages)); | ||
// Trigger the storage event to update the sidebar component | ||
window.dispatchEvent(new Event("storage")); | ||
} | ||
}, [messages, chatId, isLoading, error]); | ||
|
||
const onSubmit = (e: React.FormEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
|
||
if (messages.length === 0) { | ||
// Generate a random id for the chat | ||
const id = uuidv4(); | ||
setChatId(id); | ||
} | ||
|
||
setMessages([...messages]); | ||
|
||
// Prepare the options object with additional body data, to pass the model. | ||
const requestOptions: ChatRequestOptions = { | ||
options: { | ||
body: { | ||
chatOptions: chatOptions, | ||
}, | ||
}, | ||
}; | ||
|
||
// Call the handleSubmit function with the options | ||
handleSubmit(e, requestOptions); | ||
}; | ||
|
||
return ( | ||
<main className="flex h-[calc(100dvh)] flex-col items-center "> | ||
<ChatLayout | ||
chatId={chatId} | ||
setChatId={setChatId} | ||
chatOptions={chatOptions} | ||
setChatOptions={setChatOptions} | ||
messages={messages} | ||
input={input} | ||
handleInputChange={handleInputChange} | ||
handleSubmit={onSubmit} | ||
isLoading={isLoading} | ||
error={error} | ||
stop={stop} | ||
navCollapsedSize={10} | ||
defaultLayout={[30, 160]} | ||
/> | ||
</main> | ||
); | ||
} |
Oops, something went wrong.