fix: chat query (#1170)

Co-authored-by: josh <josh@afterima.ge>
This commit is contained in:
Nicklas Scharpff 2025-09-09 00:29:04 +02:00 committed by GitHub
parent d3b3449eb8
commit f2320bf321
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 879 additions and 145 deletions

View file

@ -33,6 +33,7 @@ import { generateUUID } from '../utils';
import { generateHashedPassword } from './utils';
import type { VisibilityType } from '@/components/visibility-selector';
import { ChatSDKError } from '../errors';
import { LanguageModelV2Usage } from '@ai-sdk/provider';
// Optionally, if not using email/pass login, you can
// use the Drizzle adapter for Auth.js / NextAuth
@ -202,6 +203,10 @@ export async function getChatsByUserId({
export async function getChatById({ id }: { id: string }) {
try {
const [selectedChat] = await db.select().from(chat).where(eq(chat.id, id));
if (!selectedChat) {
return null;
}
return selectedChat;
} catch (error) {
throw new ChatSDKError('bad_request:database', 'Failed to get chat by id');
@ -469,10 +474,32 @@ export async function updateChatVisiblityById({
}
}
export async function updateChatLastContextById({
chatId,
context,
}: {
chatId: string;
// Store raw LanguageModelUsage to keep it simple
context: LanguageModelV2Usage;
}) {
try {
return await db
.update(chat)
.set({ lastContext: context })
.where(eq(chat.id, chatId));
} catch (error) {
console.warn('Failed to update lastContext for chat', chatId, error);
return;
}
}
export async function getMessageCountByUserId({
id,
differenceInHours,
}: { id: string; differenceInHours: number }) {
}: {
id: string;
differenceInHours: number;
}) {
try {
const twentyFourHoursAgo = new Date(
Date.now() - differenceInHours * 60 * 60 * 1000,