-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Expand file tree
/
Copy pathagents-webhook.controller.ts
More file actions
92 lines (87 loc) · 3.01 KB
/
agents-webhook.controller.ts
File metadata and controls
92 lines (87 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import {
Body,
Controller,
Get,
HttpCode,
HttpException,
HttpStatus,
Param,
Post,
Req,
Res,
UseGuards,
} from '@nestjs/common';
import { ApiExcludeController } from '@nestjs/swagger';
import { UserSessionData } from '@novu/shared';
import { Request, Response } from 'express';
import { RequireAuthentication } from '../auth/framework/auth.decorator';
import { ExternalApiAccessible } from '../auth/framework/external-api.decorator';
import { UserSession } from '../shared/framework/user.decorator';
import { AgentReplyPayloadDto } from './dtos/agent-reply-payload.dto';
import { AgentConversationEnabledGuard } from './guards/agent-conversation-enabled.guard';
import { ChatSdkService } from './services/chat-sdk.service';
import { HandleAgentReplyCommand, Signal } from './usecases/handle-agent-reply/handle-agent-reply.command';
import { HandleAgentReply } from './usecases/handle-agent-reply/handle-agent-reply.usecase';
@Controller('/agents')
@UseGuards(AgentConversationEnabledGuard)
@ApiExcludeController()
export class AgentsWebhookController {
constructor(
private chatSdkService: ChatSdkService,
private handleAgentReplyUsecase: HandleAgentReply
) {}
@Post('/:agentId/reply')
@HttpCode(HttpStatus.OK)
@RequireAuthentication()
@ExternalApiAccessible()
async handleAgentReply(
@UserSession() user: UserSessionData,
@Param('agentId') agentId: string,
@Body() body: AgentReplyPayloadDto
) {
return this.handleAgentReplyUsecase.execute(
HandleAgentReplyCommand.create({
userId: user._id,
environmentId: user.environmentId,
organizationId: user.organizationId,
conversationId: body.conversationId,
agentIdentifier: agentId,
integrationIdentifier: body.integrationIdentifier,
reply: body.reply,
update: body.update,
resolve: body.resolve,
signals: body.signals as Signal[],
})
);
}
@Get('/:agentId/webhook/:integrationIdentifier')
async handleWebhookVerification(
@Param('agentId') agentId: string,
@Param('integrationIdentifier') integrationIdentifier: string,
@Req() req: Request,
@Res() res: Response
) {
return this.routeWebhook(agentId, integrationIdentifier, req, res);
}
@Post('/:agentId/webhook/:integrationIdentifier')
@HttpCode(HttpStatus.OK)
async handleInboundWebhook(
@Param('agentId') agentId: string,
@Param('integrationIdentifier') integrationIdentifier: string,
@Req() req: Request,
@Res() res: Response
) {
return this.routeWebhook(agentId, integrationIdentifier, req, res);
}
private async routeWebhook(agentId: string, integrationIdentifier: string, req: Request, res: Response) {
try {
await this.chatSdkService.handleWebhook(agentId, integrationIdentifier, req, res);
} catch (err) {
if (err instanceof HttpException) {
res.status(err.getStatus()).json(err.getResponse());
} else {
res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({ error: 'Internal server error' });
}
}
}
}