refactor(chatbot): change context logic, adding context to chat request instead of sending on page change

This commit is contained in:
Nic D 2026-01-15 12:18:57 -05:00
parent 63cc2a4458
commit 11d841e868
3 changed files with 46 additions and 41 deletions

View File

@ -17,18 +17,18 @@ export class ChatbotController {
return await this.chatbotService.pingExternalApi(body, email); return await this.chatbotService.pingExternalApi(body, email);
} }
@Post('context') // @Post('context')
@ModuleAccessAllowed(ModulesEnum.chatbot) // @ModuleAccessAllowed(ModulesEnum.chatbot)
async sendContext(@Body() body: PageContextDto): Promise<string> { // async sendContext(@Body() body: PageContextDto): Promise<string> {
const sendPageContext = await this.chatbotService.sendPageContext(body); // const sendPageContext = await this.chatbotService.sendPageContext(body);
return sendPageContext; // return sendPageContext;
} // }
// Will have to modify later on to accomodate newer versions of User Auth/User type Structure // Will have to modify later on to accomodate newer versions of User Auth/User type Structure
@Post('user') // @Post('user')
@ModuleAccessAllowed(ModulesEnum.chatbot) // @ModuleAccessAllowed(ModulesEnum.chatbot)
async sendUserCredentials(@Access('email') email: string,): Promise<boolean> { // async sendUserCredentials(@Access('email') email: string,): Promise<boolean> {
const sendUserContext = await this.chatbotService.sendUserContext(email); // const sendUserContext = await this.chatbotService.sendUserContext(email);
return sendUserContext; // return sendUserContext;
} // }
} }

View File

@ -11,7 +11,10 @@ export class ChatbotService {
sessionId: string = 'testing'; sessionId: string = 'testing';
async pingExternalApi(body: UserMessageDto, email: string): Promise<Message> { async pingExternalApi(body: UserMessageDto, email: string): Promise<Message> {
const { data } = await firstValueFrom(this.httpService.post('https://n8nai.targo.ca/webhook/chatty-Mcbot', { userInput: body.userInput, userId: email, sessionId: email })); const { data } = await firstValueFrom(this.httpService.post(
'https://n8nai.targo.ca/webhook/chatty-Mcbot',
{ userInput: body.userInput, userId: email, sessionId: this.sessionId, pageContext: body.pageContext ?? undefined }
));
return { return {
text: data[0].output, text: data[0].output,
@ -19,33 +22,33 @@ export class ChatbotService {
}; };
} }
async sendPageContext(body: PageContextDto) { // async sendPageContext(body: PageContextDto, email: string) {
const { data } = await firstValueFrom( // const { data } = await firstValueFrom(
this.httpService.post( // this.httpService.post(
'https://n8nai.targo.ca/webhook/chatty-Mcbot', // 'https://n8nai.targo.ca/webhook/chatty-Mcbot',
{ features: body, userId: this.sessionId, userInput: '' }, // { features: body, userId: email, userInput: '' },
), // ),
); // );
return data; // return data;
} // }
// Will have to modify later on to accomodate newer versions of User Auth/User type Structure // Will have to modify later on to accomodate newer versions of User Auth/User type Structure
async sendUserContext(user_email: string) { // async sendUserContext(user_email: string) {
if (!this.sessionId) { // if (!this.sessionId) {
this.sessionId = 'SessionId = ' + user_email; // this.sessionId = 'SessionId = ' + user_email;
} // }
const response = await firstValueFrom( // const response = await firstValueFrom(
this.httpService.post( // this.httpService.post(
'https://n8nai.targo.ca/webhook/chatty-Mcbot', // 'https://n8nai.targo.ca/webhook/chatty-Mcbot',
{ // {
userId: this.sessionId, // userId: this.sessionId,
userInput: '', // userInput: '',
features: '', // features: '',
}, // },
{ headers: { 'Content-Tyoe': 'application/json' } }, // { headers: { 'Content-Tyoe': 'application/json' } },
), // ),
); // );
return response.data; // return response.data;
} // }
} }

View File

@ -1,9 +1,11 @@
import { Transform } from 'class-transformer'; import { Transform, Type } from 'class-transformer';
import { IsNotEmpty, IsString } from 'class-validator'; import { IsNotEmpty, IsOptional, IsString } from 'class-validator';
import { PageContextDto } from './page-context.dto';
export class UserMessageDto { export class UserMessageDto {
@IsString() @IsString()
@IsNotEmpty() @IsNotEmpty()
@Transform(({ value }) => value.trim()) @Transform(({ value }) => value.trim())
userInput!: string; userInput!: string;
@IsOptional() @Type(() => PageContextDto) pageContext?: PageContextDto | undefined;
} }