Skip to content

Commit

Permalink
OV-52: + delete session handler
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiy4 committed Aug 24, 2024
1 parent 33b4c70 commit 6b646bb
Showing 1 changed file with 45 additions and 10 deletions.
55 changes: 45 additions & 10 deletions backend/src/bundles/chat/chat.controller.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { type FastifySessionObject } from '@fastify/session';

import {
type ApiHandlerOptions,
type ApiHandlerResponse,
Expand All @@ -7,11 +9,9 @@ import { ApiPath } from '~/common/enums/enums.js';
import { HttpCode, HTTPMethod } from '~/common/http/http.js';
import { type Logger } from '~/common/logger/logger.js';

import { MAX_TOKEN } from './libs/constants/max-token.constant.js';
import { ChatPath, OpenAIRole } from './libs/enums/enums.js';
import {
type GenerateTextRequestDto,
type SessionChatHistory,
} from './libs/types/types.js';
import { type GenerateTextRequestDto } from './libs/types/types.js';
import { textGenerationValidationSchema } from './libs/validation-schemas/validation-schemas.js';
import { type OpenAIService } from './open-ai.service.js';

Expand All @@ -25,15 +25,15 @@ class ChatController extends BaseController {

this.addRoute({
path: ChatPath.ROOT,
method: HTTPMethod.POST,
method: HTTPMethod.PUT,
validation: {
body: textGenerationValidationSchema,
},
handler: (options) =>
this.generateChatAnswer(
options as ApiHandlerOptions<{
body: GenerateTextRequestDto;
session: SessionChatHistory;
session: FastifySessionObject;
}>,
),
});
Expand All @@ -44,16 +44,49 @@ class ChatController extends BaseController {
handler: (options) =>
this.clearChat(
options as ApiHandlerOptions<{
session: SessionChatHistory;
session: FastifySessionObject;
}>,
),
});

this.addRoute({
path: ChatPath.ROOT,
method: HTTPMethod.POST,
handler: (options) =>
this.deleteSession(
options as ApiHandlerOptions<{
session: FastifySessionObject;
}>,
),
});
}

private deleteSession(
options: ApiHandlerOptions<{
session: FastifySessionObject;
}>,
): ApiHandlerResponse {
const { session } = options;

session.destroy((error) => {
if (error) {
return {
payload: false,
status: HttpCode.INTERNAL_SERVER_ERROR,
};
}
});

return {
payload: true,
status: HttpCode.OK,
};
}

private async generateChatAnswer(
options: ApiHandlerOptions<{
body: GenerateTextRequestDto;
session: SessionChatHistory;
session: FastifySessionObject;
}>,
): Promise<ApiHandlerResponse> {
const { body, session } = options;
Expand All @@ -64,6 +97,8 @@ class ChatController extends BaseController {
OpenAIRole.USER,
);

this.openAIService.deleteOldMessages(session.chatHistory, MAX_TOKEN);

const generatedText = await this.openAIService.generateText(
session.chatHistory,
);
Expand All @@ -82,10 +117,10 @@ class ChatController extends BaseController {

private clearChat(
options: ApiHandlerOptions<{
session: SessionChatHistory;
session: FastifySessionObject;
}>,
): ApiHandlerResponse {
this.openAIService.clearChatHistory(options.session);
this.openAIService.clearChatHistory(options.session.chatHistory);
return {
payload: true,
status: HttpCode.OK,
Expand Down

0 comments on commit 6b646bb

Please sign in to comment.