Start on new sidebar (#456)

Co-authored-by: shadcn <m@shadcn.com>
This commit is contained in:
Jared Palmer 2024-10-24 16:35:51 -04:00 committed by GitHub
parent 7faa5f1c9f
commit a68eb2a011
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
41 changed files with 2350 additions and 800 deletions

View file

@ -1,38 +1,47 @@
import { convertToCoreMessages, Message, streamText } from "ai";
import { z } from "zod";
import { convertToCoreMessages, Message, streamText } from 'ai';
import { z } from 'zod';
import { customModel } from "@/ai";
import { auth } from "@/app/(auth)/auth";
import { deleteChatById, getChatById, saveChat } from "@/db/queries";
import { customModel } from '@/ai';
import { auth } from '@/app/(auth)/auth';
import { deleteChatById, getChatById, saveChat } from '@/db/queries';
import { Model, models } from '@/lib/model';
export async function POST(request: Request) {
const { id, messages }: { id: string; messages: Array<Message> } =
const {
id,
messages,
model,
}: { id: string; messages: Array<Message>; model: Model['name'] } =
await request.json();
const session = await auth();
if (!session) {
return new Response("Unauthorized", { status: 401 });
return new Response('Unauthorized', { status: 401 });
}
if (!models.find((m) => m.name === model)) {
return new Response('Model not found', { status: 404 });
}
const coreMessages = convertToCoreMessages(messages);
const result = await streamText({
model: customModel,
model: customModel(model),
system:
"you are a friendly assistant! keep your responses concise and helpful.",
'you are a friendly assistant! keep your responses concise and helpful.',
messages: coreMessages,
maxSteps: 5,
tools: {
getWeather: {
description: "Get the current weather at a location",
description: 'Get the current weather at a location',
parameters: z.object({
latitude: z.number(),
longitude: z.number(),
}),
execute: async ({ latitude, longitude }) => {
const response = await fetch(
`https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}&current=temperature_2m&hourly=temperature_2m&daily=sunrise,sunset&timezone=auto`,
`https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}&current=temperature_2m&hourly=temperature_2m&daily=sunrise,sunset&timezone=auto`
);
const weatherData = await response.json();
@ -49,13 +58,13 @@ export async function POST(request: Request) {
userId: session.user.id,
});
} catch (error) {
console.error("Failed to save chat");
console.error('Failed to save chat');
}
}
},
experimental_telemetry: {
isEnabled: true,
functionId: "stream-text",
functionId: 'stream-text',
},
});
@ -64,30 +73,30 @@ export async function POST(request: Request) {
export async function DELETE(request: Request) {
const { searchParams } = new URL(request.url);
const id = searchParams.get("id");
const id = searchParams.get('id');
if (!id) {
return new Response("Not Found", { status: 404 });
return new Response('Not Found', { status: 404 });
}
const session = await auth();
if (!session || !session.user) {
return new Response("Unauthorized", { status: 401 });
return new Response('Unauthorized', { status: 401 });
}
try {
const chat = await getChatById({ id });
if (chat.userId !== session.user.id) {
return new Response("Unauthorized", { status: 401 });
return new Response('Unauthorized', { status: 401 });
}
await deleteChatById({ id });
return new Response("Chat deleted", { status: 200 });
return new Response('Chat deleted', { status: 200 });
} catch (error) {
return new Response("An error occurred while processing your request", {
return new Response('An error occurred while processing your request', {
status: 500,
});
}