Add save to Prisma (#1)
This commit is contained in:
commit
3b8bb9dea4
3 changed files with 97 additions and 76 deletions
|
|
@ -1,7 +1,6 @@
|
||||||
import { OpenAIStream, openai } from "@/lib/openai";
|
import { OpenAIStream, openai } from "@/lib/openai";
|
||||||
import { getServerSession } from "@/lib/session/get-server-session";
|
import { getServerSession } from "@/lib/session/get-server-session";
|
||||||
|
import { prisma } from "@/lib/prisma";
|
||||||
export const runtime = "edge";
|
|
||||||
|
|
||||||
export async function POST(req: Request) {
|
export async function POST(req: Request) {
|
||||||
const json = await req.json();
|
const json = await req.json();
|
||||||
|
|
@ -20,7 +19,25 @@ export async function POST(req: Request) {
|
||||||
|
|
||||||
const stream = await OpenAIStream(res);
|
const stream = await OpenAIStream(res);
|
||||||
|
|
||||||
return new Response(stream, {
|
let fullResponse = "";
|
||||||
|
const decoder = new TextDecoder();
|
||||||
|
const saveToPrisma = new TransformStream({
|
||||||
|
transform: async (chunk, controller) => {
|
||||||
|
controller.enqueue(chunk);
|
||||||
|
fullResponse += decoder.decode(chunk);
|
||||||
|
},
|
||||||
|
flush: async () => {
|
||||||
|
await prisma.chat.upsert({
|
||||||
|
where: {
|
||||||
|
id: json.id,
|
||||||
|
},
|
||||||
|
create: json,
|
||||||
|
update: json,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return new Response(stream.pipeThrough(saveToPrisma), {
|
||||||
status: 200,
|
status: 200,
|
||||||
headers: { "Content-Type": "text/event-stream" },
|
headers: { "Content-Type": "text/event-stream" },
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ export interface ChatProps {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function Chat({
|
export function Chat({
|
||||||
id: _id,
|
id,
|
||||||
// create,
|
// create,
|
||||||
messages,
|
messages,
|
||||||
}: ChatProps) {
|
}: ChatProps) {
|
||||||
|
|
@ -22,7 +22,7 @@ export function Chat({
|
||||||
const { isLoading, messageList, appendUserMessage, reloadLastMessage } =
|
const { isLoading, messageList, appendUserMessage, reloadLastMessage } =
|
||||||
usePrompt({
|
usePrompt({
|
||||||
messages,
|
messages,
|
||||||
_id,
|
id,
|
||||||
// onCreate: (id: string) => {
|
// onCreate: (id: string) => {
|
||||||
// router.push(`/chat/${id}`);
|
// router.push(`/chat/${id}`);
|
||||||
// },
|
// },
|
||||||
|
|
|
||||||
|
|
@ -4,10 +4,10 @@ import { nanoid } from "@/lib/utils";
|
||||||
|
|
||||||
export function usePrompt({
|
export function usePrompt({
|
||||||
messages = [],
|
messages = [],
|
||||||
_id,
|
id,
|
||||||
}: {
|
}: {
|
||||||
messages?: Message[];
|
messages?: Message[];
|
||||||
_id: string | undefined | null;
|
id: string | undefined;
|
||||||
}) {
|
}) {
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
const [messageList, setMessageList] = useState(messages);
|
const [messageList, setMessageList] = useState(messages);
|
||||||
|
|
@ -23,7 +23,8 @@ export function usePrompt({
|
||||||
messageListRef.current = messageList;
|
messageListRef.current = messageList;
|
||||||
}, [messageList]);
|
}, [messageList]);
|
||||||
|
|
||||||
const appendUserMessage = useCallback(async (content: string | Message) => {
|
const appendUserMessage = useCallback(
|
||||||
|
async (content: string | Message) => {
|
||||||
// Prevent multiple requests at once
|
// Prevent multiple requests at once
|
||||||
if (isLoadingRef.current) return;
|
if (isLoadingRef.current) return;
|
||||||
|
|
||||||
|
|
@ -49,6 +50,7 @@ export function usePrompt({
|
||||||
const response = await fetch(`/api/generate`, {
|
const response = await fetch(`/api/generate`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
|
id: id || nanoid(10),
|
||||||
messages: [...messageListSnapshot, userMsg].map((m) => ({
|
messages: [...messageListSnapshot, userMsg].map((m) => ({
|
||||||
role: m.role,
|
role: m.role,
|
||||||
content: m.content,
|
content: m.content,
|
||||||
|
|
@ -99,7 +101,9 @@ export function usePrompt({
|
||||||
} finally {
|
} finally {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
}, []);
|
},
|
||||||
|
[id]
|
||||||
|
);
|
||||||
|
|
||||||
const reloadLastMessage = useCallback(async () => {
|
const reloadLastMessage = useCallback(async () => {
|
||||||
// Prevent multiple requests at once
|
// Prevent multiple requests at once
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue