2024-10-30 16:01:24 +05:30
|
|
|
import {
|
2024-11-15 12:18:17 -05:00
|
|
|
type Message,
|
2024-12-17 16:16:03 +05:30
|
|
|
createDataStreamResponse,
|
2025-01-23 01:53:41 +05:30
|
|
|
smoothStream,
|
2024-10-30 16:01:24 +05:30
|
|
|
streamText,
|
|
|
|
|
} from 'ai';
|
2024-10-24 16:35:51 -04:00
|
|
|
import { auth } from '@/app/(auth)/auth';
|
2025-01-23 01:19:48 +05:30
|
|
|
import { systemPrompt } from '@/lib/ai/prompts';
|
2024-10-30 16:01:24 +05:30
|
|
|
import {
|
|
|
|
|
deleteChatById,
|
|
|
|
|
getChatById,
|
|
|
|
|
saveChat,
|
2024-11-05 17:15:51 +03:00
|
|
|
saveMessages,
|
2024-11-15 10:13:21 -05:00
|
|
|
} from '@/lib/db/queries';
|
2024-11-05 17:15:51 +03:00
|
|
|
import {
|
|
|
|
|
generateUUID,
|
|
|
|
|
getMostRecentUserMessage,
|
|
|
|
|
sanitizeResponseMessages,
|
|
|
|
|
} from '@/lib/utils';
|
|
|
|
|
import { generateTitleFromUserMessage } from '../../actions';
|
2025-01-23 01:19:48 +05:30
|
|
|
import { createDocument } from '@/lib/ai/tools/create-document';
|
|
|
|
|
import { updateDocument } from '@/lib/ai/tools/update-document';
|
|
|
|
|
import { requestSuggestions } from '@/lib/ai/tools/request-suggestions';
|
|
|
|
|
import { getWeather } from '@/lib/ai/tools/get-weather';
|
2025-03-04 17:25:46 -08:00
|
|
|
import { isProductionEnvironment } from '@/lib/constants';
|
|
|
|
|
import { NextResponse } from 'next/server';
|
|
|
|
|
import { myProvider } from '@/lib/ai/providers';
|
2024-10-30 16:01:24 +05:30
|
|
|
|
|
|
|
|
export const maxDuration = 60;
|
|
|
|
|
|
2024-10-11 18:00:22 +05:30
|
|
|
export async function POST(request: Request) {
|
2025-03-04 17:25:46 -08:00
|
|
|
try {
|
|
|
|
|
const {
|
|
|
|
|
id,
|
|
|
|
|
messages,
|
|
|
|
|
selectedChatModel,
|
|
|
|
|
}: {
|
|
|
|
|
id: string;
|
|
|
|
|
messages: Array<Message>;
|
|
|
|
|
selectedChatModel: string;
|
|
|
|
|
} = await request.json();
|
|
|
|
|
|
|
|
|
|
const session = await auth();
|
|
|
|
|
|
|
|
|
|
if (!session || !session.user || !session.user.id) {
|
|
|
|
|
return new Response('Unauthorized', { status: 401 });
|
|
|
|
|
}
|
2024-11-05 17:15:51 +03:00
|
|
|
|
2025-03-04 17:25:46 -08:00
|
|
|
const userMessage = getMostRecentUserMessage(messages);
|
2024-11-05 17:15:51 +03:00
|
|
|
|
2025-03-04 17:25:46 -08:00
|
|
|
if (!userMessage) {
|
|
|
|
|
return new Response('No user message found', { status: 400 });
|
|
|
|
|
}
|
2024-11-05 17:15:51 +03:00
|
|
|
|
2025-03-04 17:25:46 -08:00
|
|
|
const chat = await getChatById({ id });
|
2024-11-05 17:15:51 +03:00
|
|
|
|
2025-03-04 17:25:46 -08:00
|
|
|
if (!chat) {
|
|
|
|
|
const title = await generateTitleFromUserMessage({
|
|
|
|
|
message: userMessage,
|
2024-12-17 16:16:03 +05:30
|
|
|
});
|
2025-03-04 17:25:46 -08:00
|
|
|
await saveChat({ id, userId: session.user.id, title });
|
|
|
|
|
}
|
2024-11-05 17:15:51 +03:00
|
|
|
|
2025-03-04 17:25:46 -08:00
|
|
|
await saveMessages({
|
|
|
|
|
messages: [{ ...userMessage, createdAt: new Date(), chatId: id }],
|
|
|
|
|
});
|
2025-02-19 19:09:03 -06:00
|
|
|
|
2025-03-04 17:25:46 -08:00
|
|
|
return createDataStreamResponse({
|
|
|
|
|
execute: (dataStream) => {
|
|
|
|
|
const result = streamText({
|
|
|
|
|
model: myProvider.languageModel(selectedChatModel),
|
|
|
|
|
system: systemPrompt({ selectedChatModel }),
|
|
|
|
|
messages,
|
|
|
|
|
maxSteps: 5,
|
|
|
|
|
experimental_activeTools:
|
|
|
|
|
selectedChatModel === 'chat-model-reasoning'
|
|
|
|
|
? []
|
|
|
|
|
: [
|
|
|
|
|
'getWeather',
|
|
|
|
|
'createDocument',
|
|
|
|
|
'updateDocument',
|
|
|
|
|
'requestSuggestions',
|
|
|
|
|
],
|
|
|
|
|
experimental_transform: smoothStream({ chunking: 'word' }),
|
|
|
|
|
experimental_generateMessageId: generateUUID,
|
|
|
|
|
tools: {
|
|
|
|
|
getWeather,
|
|
|
|
|
createDocument: createDocument({ session, dataStream }),
|
|
|
|
|
updateDocument: updateDocument({ session, dataStream }),
|
|
|
|
|
requestSuggestions: requestSuggestions({
|
|
|
|
|
session,
|
|
|
|
|
dataStream,
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
onFinish: async ({ response, reasoning }) => {
|
|
|
|
|
if (session.user?.id) {
|
|
|
|
|
try {
|
|
|
|
|
const sanitizedResponseMessages = sanitizeResponseMessages({
|
|
|
|
|
messages: response.messages,
|
|
|
|
|
reasoning,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await saveMessages({
|
|
|
|
|
messages: sanitizedResponseMessages.map((message) => {
|
|
|
|
|
return {
|
|
|
|
|
id: message.id,
|
|
|
|
|
chatId: id,
|
|
|
|
|
role: message.role,
|
|
|
|
|
content: message.content,
|
|
|
|
|
createdAt: new Date(),
|
|
|
|
|
};
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Failed to save chat');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
experimental_telemetry: {
|
|
|
|
|
isEnabled: isProductionEnvironment,
|
|
|
|
|
functionId: 'stream-text',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
result.consumeStream();
|
|
|
|
|
|
|
|
|
|
result.mergeIntoDataStream(dataStream, {
|
|
|
|
|
sendReasoning: true,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
onError: () => {
|
|
|
|
|
return 'Oops, an error occured!';
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return NextResponse.json({ error }, { status: 400 });
|
|
|
|
|
}
|
2024-10-11 18:00:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function DELETE(request: Request) {
|
|
|
|
|
const { searchParams } = new URL(request.url);
|
2024-10-24 16:35:51 -04:00
|
|
|
const id = searchParams.get('id');
|
2024-10-11 18:00:22 +05:30
|
|
|
|
|
|
|
|
if (!id) {
|
2024-10-24 16:35:51 -04:00
|
|
|
return new Response('Not Found', { status: 404 });
|
2024-10-11 18:00:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const session = await auth();
|
|
|
|
|
|
|
|
|
|
if (!session || !session.user) {
|
2024-10-24 16:35:51 -04:00
|
|
|
return new Response('Unauthorized', { status: 401 });
|
2024-10-11 18:00:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const chat = await getChatById({ id });
|
|
|
|
|
|
|
|
|
|
if (chat.userId !== session.user.id) {
|
2024-10-24 16:35:51 -04:00
|
|
|
return new Response('Unauthorized', { status: 401 });
|
2024-10-11 18:00:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await deleteChatById({ id });
|
|
|
|
|
|
2024-10-24 16:35:51 -04:00
|
|
|
return new Response('Chat deleted', { status: 200 });
|
2024-10-11 18:00:22 +05:30
|
|
|
} catch (error) {
|
2024-10-24 16:35:51 -04:00
|
|
|
return new Response('An error occurred while processing your request', {
|
2024-10-11 18:00:22 +05:30
|
|
|
status: 500,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|