feat: connect chatbot to conversation database
This commit is contained in:
@@ -1,7 +1,16 @@
|
||||
import { Body, Controller, Post, Res } from '@nestjs/common';
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
HttpCode,
|
||||
HttpStatus,
|
||||
Post,
|
||||
Res,
|
||||
} from '@nestjs/common';
|
||||
import { ChatbotService } from './providers/chatbot.service';
|
||||
import { Public } from '../auth/decorators/public.decorator';
|
||||
import type { Response } from 'express';
|
||||
import { ChatDTO } from './dtos/chat.dto';
|
||||
import { ActiveUser } from '@/common/decorators/active-user.decorator';
|
||||
import { User } from '../users/entities/user.entity';
|
||||
|
||||
@Controller('chatbot')
|
||||
export class ChatbotController {
|
||||
@@ -13,24 +22,31 @@ export class ChatbotController {
|
||||
) {}
|
||||
|
||||
@Post('chat')
|
||||
// TEMP:
|
||||
@Public()
|
||||
public async chat(@Body('message') message: string) {
|
||||
return this.chatbotService.chat(message);
|
||||
@HttpCode(HttpStatus.OK)
|
||||
public async chat(@Body() chatDto: ChatDTO, @ActiveUser() user: User) {
|
||||
return this.chatbotService.chat(
|
||||
user,
|
||||
chatDto.message,
|
||||
chatDto.conversationId,
|
||||
);
|
||||
}
|
||||
|
||||
@Post('stream')
|
||||
// TEMP:
|
||||
@Public()
|
||||
@HttpCode(HttpStatus.OK)
|
||||
public async streamChat(
|
||||
@Body('message') message: string,
|
||||
@Body() chatDto: ChatDTO,
|
||||
@ActiveUser() user: User,
|
||||
@Res() res: Response,
|
||||
) {
|
||||
res.setHeader('Content-Type', 'text/event-stream');
|
||||
res.setHeader('Cache-Control', 'no-cache');
|
||||
res.setHeader('Connection', 'keep-alive');
|
||||
|
||||
for await (const content of this.chatbotService.streamChat(message)) {
|
||||
for await (const content of this.chatbotService.streamChat(
|
||||
user,
|
||||
chatDto.message,
|
||||
chatDto.conversationId,
|
||||
)) {
|
||||
res.write(
|
||||
`data: ${JSON.stringify({
|
||||
message: content,
|
||||
@@ -40,30 +56,4 @@ export class ChatbotController {
|
||||
|
||||
res.end();
|
||||
}
|
||||
|
||||
// @Sse('stream')
|
||||
// @Public()
|
||||
// public streamChat(
|
||||
// @Body('message') message: string,
|
||||
// ): Observable<MessageEvent> {
|
||||
// return new Observable((subscriber) => {
|
||||
// const run = async () => {
|
||||
// try {
|
||||
// for await (const content of this.chatbotService.streamChat(message)) {
|
||||
// subscriber.next({
|
||||
// data: {
|
||||
// message: content,
|
||||
// },
|
||||
// });
|
||||
// }
|
||||
|
||||
// subscriber.complete();
|
||||
// } catch (err) {
|
||||
// subscriber.error(err);
|
||||
// }
|
||||
// };
|
||||
|
||||
// void run();
|
||||
// });
|
||||
// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user