diff --git a/app/api/chat/route.ts b/app/api/chat/route.ts index 1866dcf..0de4e36 100644 --- a/app/api/chat/route.ts +++ b/app/api/chat/route.ts @@ -1,4 +1,4 @@ -import { streamText } from '@ai-sdk/react'; +import { streamText, UIMessage, convertToModelMessages } from 'ai'; import { google } from '@ai-sdk/google'; import { cookies } from 'next/headers'; import { NodeSuggestionSchema } from '@/lib/ai-schemas'; @@ -15,14 +15,14 @@ export async function POST(req: Request) { return new Response('Unauthorized', { status: 401 }); } - const { messages, data } = await req.json(); + const { messages, data }: { messages: UIMessage[]; data?: { persona?: string } } = await req.json(); // Get the 'persona' from the custom 'data' object const { persona } = z .object({ persona: z.string().optional().default('Socratic'), }) - .parse(data); + .parse(data || {}); // Dynamically create the system prompt based on persona const systemPrompt = `You are a ${persona} thought partner. @@ -33,10 +33,10 @@ idea is fully formed. For all other conversation, just respond as a helpful AI.`; // Use the Vercel AI SDK's streamText function with tools - const result = await streamText({ + const result = streamText({ model: google('gemini-1.5-flash'), system: systemPrompt, - messages: messages, + messages: convertToModelMessages(messages), // Provide the schema as a 'tool' to the model tools: { @@ -47,6 +47,6 @@ For all other conversation, just respond as a helpful AI.`; }, }); - // Return the streaming response - return result.toAIStreamResponse(); + // Return the streaming response (v5 API) + return result.toUIMessageStreamResponse(); } diff --git a/app/chat/page.tsx b/app/chat/page.tsx index ae2d316..56f09cf 100644 --- a/app/chat/page.tsx +++ b/app/chat/page.tsx @@ -13,44 +13,18 @@ import { Text, } from '@mantine/core'; import { useRouter } from 'next/navigation'; -import { useEffect, useRef } from 'react'; -import { NodeSuggestion } from '@/lib/ai-schemas'; +import { useEffect, useRef, useState } from 'react'; import { MicrophoneRecorder } from '@/components/MicrophoneRecorder'; export default function ChatPage() { const router = useRouter(); const viewport = useRef(null); + const [input, setInput] = useState(''); - const { - messages, - input, - handleInputChange, - handleSubmit, - setInput, - isLoading, - } = useChat({ + const { messages, sendMessage, isLoading } = useChat({ api: '/api/chat', - // Send the persona in the 'data' property - data: { - persona: 'Socratic', // This could be a