From ed4d66cadee44eaad1571815c5824d52fe17f700 Mon Sep 17 00:00:00 2001 From: pinocchio-life-like Date: Sat, 28 Dec 2024 01:46:22 +0300 Subject: [PATCH] openAI type fix --- server/src/db/schema.ts | 5 +++++ server/src/drizzle/methods/Conversation.ts | 8 +++++++- server/src/drizzle/methods/User.ts | 13 +++++++++++++ .../src/services/openAi/getAIResponseService.ts | 4 ++-- .../src/services/openAi/getUserChatsService.ts | 5 ++++- server/src/services/openAi/langchain/index.ts | 17 ++++++++--------- tsconfig.json | 8 +++++--- 7 files changed, 44 insertions(+), 16 deletions(-) diff --git a/server/src/db/schema.ts b/server/src/db/schema.ts index defa11c0c..f2de1cec7 100644 --- a/server/src/db/schema.ts +++ b/server/src/db/schema.ts @@ -646,3 +646,8 @@ export type GeoJson = InferSelectModel; export type InsertGeoJson = InferInsertModel; export const insertGeoJsonSchema = createInsertSchema(geojson); export const selectGeoJsonSchema = createSelectSchema(geojson); + +export type Conversation = InferSelectModel; +export type InsertConversation = InferInsertModel; +export const insertConversationSchema = createInsertSchema(conversation); +export const selectConversationSchema = createSelectSchema(conversation); diff --git a/server/src/drizzle/methods/Conversation.ts b/server/src/drizzle/methods/Conversation.ts index b7a4212e6..519db4a53 100644 --- a/server/src/drizzle/methods/Conversation.ts +++ b/server/src/drizzle/methods/Conversation.ts @@ -48,7 +48,13 @@ export class Conversation { } } - async findConversation(userId: string, itemTypeId: string) { + async findConversation({ + userId, + itemTypeId, + }: { + userId: string; + itemTypeId: string; + }) { try { const filter = and( eq(ConversationTable.userId, userId), diff --git a/server/src/drizzle/methods/User.ts b/server/src/drizzle/methods/User.ts index 819c292d8..398ef86dd 100644 --- a/server/src/drizzle/methods/User.ts +++ b/server/src/drizzle/methods/User.ts @@ -355,4 +355,17 @@ export class User { throw new Error(`Failed to find user: ${error.message}`); } } + + async findById(userId: string): Promise { + try { + const user = await DbClient.instance + .select() + .from(UserTable) + .where(eq(UserTable.id, userId)) + .get(); + return user || null; + } catch (error) { + throw new Error(`Failed to find user by ID: ${error.message}`); + } + } } diff --git a/server/src/services/openAi/getAIResponseService.ts b/server/src/services/openAi/getAIResponseService.ts index eccbac0b4..a4b46ad45 100644 --- a/server/src/services/openAi/getAIResponseService.ts +++ b/server/src/services/openAi/getAIResponseService.ts @@ -58,10 +58,10 @@ export const getAIResponseService = async ( throw new Error(`Invalid type: ${itemTypeId}`); } - let conversation: any = await conversationClass.findConversation( + let conversation: any = await conversationClass.findConversation({ userId, itemTypeId, - ); + }); let conversationHistory = conversation ? conversation.history : ''; diff --git a/server/src/services/openAi/getUserChatsService.ts b/server/src/services/openAi/getUserChatsService.ts index b24d3b89d..c02e1f445 100644 --- a/server/src/services/openAi/getUserChatsService.ts +++ b/server/src/services/openAi/getUserChatsService.ts @@ -18,7 +18,10 @@ export const getUserChatsService = async (userId, itemTypeId) => { } const conversation = new Conversation(); - const conversations = await conversation.findConversation(userId, itemTypeId); + const conversations = await conversation.findConversation({ + userId, + itemTypeId, + }); return { conversations }; }; diff --git a/server/src/services/openAi/langchain/index.ts b/server/src/services/openAi/langchain/index.ts index bfcf71b22..e3170b2f3 100644 --- a/server/src/services/openAi/langchain/index.ts +++ b/server/src/services/openAi/langchain/index.ts @@ -1,10 +1,11 @@ import { ChatOpenAI } from 'langchain/chat_models/openai'; import { AIMessage, HumanMessage, SystemMessage } from 'langchain/schema'; -import Conversation from '../../../models/openai/conversationModel'; +import { Conversation } from '../../../drizzle/methods/Conversation'; import { getPackByIdService } from '../../pack/getPackByIdService'; import { getTripByIdService } from '../../trip/getTripByIdService'; import mongoose from 'mongoose'; -import User from '../../../models/userModel'; +import { User } from '../../../drizzle/methods/User'; +import { User as UserType } from '../../../db/schema'; const chatModel = new ChatOpenAI({ openAIApiKey: process.env.OPENAI_API_KEY, // Replace with your OpenAI API key @@ -31,20 +32,18 @@ export const getAIResponseService = async ( const tripInfo = await getTripInformation(tripId); // find last conversation if present - let conversation = await Conversation.findOne({ + let conversation = await new Conversation().findConversation({ userId, - // _id: conversationId, - itemTypeId, + itemTypeId: itemTypeId || '', }); // if conversation is not found, create a new one if (!conversation) { - conversation = new Conversation({ + conversation = await new Conversation().create({ userId, itemTypeId, history: '', }); - await conversation.save(); } let conversationHistory = conversation.history || ''; @@ -171,12 +170,12 @@ async function saveConversationHistory(conversation, conversationHistory) { return conversation; } -export async function validateUser(userId) { +export async function validateUser(userId: string): Promise { if (!mongoose.Types.ObjectId.isValid(userId)) { throw new Error('Invalid userId'); } - const user = await User.findById(userId).exec(); + const user = await new User().findById(userId); if (!user) { throw new Error('User not found'); } diff --git a/tsconfig.json b/tsconfig.json index 4c4fbca20..1c838e68b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,13 +2,15 @@ "compilerOptions": { "strictNullChecks": true, "noUncheckedIndexedAccess": true, - "module": "node16", - "moduleResolution": "node16", + "module": "CommonJS", "paths": { "app/*": ["./packages/app/*"], "@packrat/api/*": ["./packages/api/*"], "@packrat/ui/*": ["./packages/ui/*"], - "server/*": ["./server/*"] + "server/*": ["./server/*"], + "@cloudflare/vitest-pool-workers/config": [ + "./node_modules/@cloudflare/vitest-pool-workers/dist/config" + ] }, "plugins": [ {