diff --git a/src/bot.ts b/src/bot.ts index 46bb441..85cb3d0 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -79,8 +79,12 @@ export const createBot = async () => { bot.use( session({ - initial: createInitialSessionData, - storage: mongo.sessionAdapter, + type: 'multi', + custom: { + storage: mongo.sessionAdapter, + initial: createInitialSessionData, + }, + conversation: {}, }), ); diff --git a/src/helpers/gpt.helpers.ts b/src/helpers/gpt.helpers.ts index 5d2249f..a8a52ef 100644 --- a/src/helpers/gpt.helpers.ts +++ b/src/helpers/gpt.helpers.ts @@ -13,18 +13,20 @@ export const convertGPTMessage = ( export const getGPTAnswer = async (ctx: BotContextType, text = '') => { try { - ctx.session.messages.push({ + ctx.session.custom.messages.push({ gptFormat: convertGPTMessage(text), timestamp: Date.now(), }); - const response = await openAI.chat(ctx.session.messages.map(({ gptFormat }) => gptFormat)); + const response = await openAI.chat( + ctx.session.custom.messages.map(({ gptFormat }) => gptFormat), + ); if (!response) { return ''; } - ctx.session.messages.push({ + ctx.session.custom.messages.push({ gptFormat: convertGPTMessage(response.content, MessageRoles.ASSISTANT), timestamp: Date.now(), }); diff --git a/src/helpers/session.helpers.ts b/src/helpers/session.helpers.ts index 462a49f..c96ee88 100644 --- a/src/helpers/session.helpers.ts +++ b/src/helpers/session.helpers.ts @@ -1,10 +1,9 @@ import { SessionCsvIds } from '@bot/constants'; import { SessionModelType, SessionType } from '@bot/types'; -export const createInitialSessionData = (): SessionType => ({ +export const createInitialSessionData = (): SessionType['custom'] => ({ username: null, messages: [], - conversation: {}, }); export const mapUserSessionMessages = (userSession: SessionModelType) => diff --git a/src/middlewares/normalize.middleware.ts b/src/middlewares/normalize.middleware.ts index 4207dad..50b30a1 100644 --- a/src/middlewares/normalize.middleware.ts +++ b/src/middlewares/normalize.middleware.ts @@ -3,13 +3,12 @@ import { BotContextType, GrammyMiddlewareFn } from '@bot/types'; export const normalize = (): GrammyMiddlewareFn => async (ctx, next) => { const username = ctx?.from?.username ?? ''; - const sessionMessages = ctx.session.messages; + const sessionMessages = ctx.session.custom.messages; - ctx.session.username ??= username; - ctx.session.conversation ??= {}; + ctx.session.custom.username ??= username; if (sessionMessages.length >= MAX_SESSION_MESSAGES) { - ctx.session.messages = sessionMessages.slice(Math.floor(MAX_SESSION_MESSAGES / 2.5)); + ctx.session.custom.messages = sessionMessages.slice(Math.floor(MAX_SESSION_MESSAGES / 2.5)); } return next(); diff --git a/src/models/session.model.ts b/src/models/session.model.ts index 8f169e9..9a95080 100644 --- a/src/models/session.model.ts +++ b/src/models/session.model.ts @@ -6,7 +6,6 @@ const schema = new Schema({ value: { username: { type: String }, messages: [], - conversation: { type: Object, default: {} }, }, }); diff --git a/src/services/mongo.service.ts b/src/services/mongo.service.ts index daefe82..4181582 100644 --- a/src/services/mongo.service.ts +++ b/src/services/mongo.service.ts @@ -1,6 +1,6 @@ import { SessionModel, UserModel } from '@bot/models'; import { logger } from '@bot/services'; -import { IMongo } from '@bot/types'; +import { IMongo, SessionType } from '@bot/types'; import { fetchCachedData, removeValueFromMemoryCache, setValueToMemoryCache } from '@bot/utils'; import { ISession, MongoDBAdapter } from '@grammyjs/storage-mongodb'; import mongoose from 'mongoose'; @@ -8,7 +8,7 @@ import mongoose from 'mongoose'; export class MongoService implements IMongo { sessions: mongoose.mongo.Collection; - sessionAdapter: MongoDBAdapter; + sessionAdapter: MongoDBAdapter; constructor() { const sessions = mongoose.connection.collection('sessions'); diff --git a/src/types/bot.types.ts b/src/types/bot.types.ts index e520a1e..a9a9c37 100644 --- a/src/types/bot.types.ts +++ b/src/types/bot.types.ts @@ -5,12 +5,13 @@ import { MenuFlavor } from '@grammyjs/menu'; import { Bot, Context, SessionFlavor } from 'grammy'; export type SessionType = { - username: string | null; - messages: { - gptFormat: { content: string; role: `${MessageRoles}` }; - timestamp: number; - }[]; - conversation: object; + custom: { + username: string | null; + messages: { + gptFormat: { content: string; role: `${MessageRoles}` }; + timestamp: number; + }[]; + }; }; export type BotContextType = Context & diff --git a/src/types/model.types.ts b/src/types/model.types.ts index da07d5f..f5c6cbf 100644 --- a/src/types/model.types.ts +++ b/src/types/model.types.ts @@ -12,7 +12,6 @@ export type SessionModelType = { key: string; value: { username: string; - messages: SessionType['messages']; - conversation: SessionType['conversation']; + messages: SessionType['custom']['messages']; }; };