Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions apps/api/src/app/agents/agents.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ import {
} from '@nestjs/common';
import { ApiExcludeController, ApiOperation } from '@nestjs/swagger';
import { ProductFeature, RequirePermissions } from '@novu/application-generic';
import { ApiRateLimitCategoryEnum, DirectionEnum, PermissionsEnum, ProductFeatureKeyEnum, UserSessionData } from '@novu/shared';
import {
ApiRateLimitCategoryEnum,
DirectionEnum,
PermissionsEnum,
ProductFeatureKeyEnum,
UserSessionData,
} from '@novu/shared';
import { RequireAuthentication } from '../auth/framework/auth.decorator';
import { ExternalApiAccessible } from '../auth/framework/external-api.decorator';
import { ThrottlerCategory } from '../rate-limiting/guards';
Expand All @@ -40,6 +46,7 @@ import {
UpdateAgentIntegrationRequestDto,
UpdateAgentRequestDto,
} from './dtos';
import { SendAgentTestEmailRequestDto } from './dtos/send-agent-test-email-request.dto';
import { AgentConversationEnabledGuard } from './guards/agent-conversation-enabled.guard';
import { AddAgentIntegrationCommand } from './usecases/add-agent-integration/add-agent-integration.command';
import { AddAgentIntegration } from './usecases/add-agent-integration/add-agent-integration.usecase';
Expand Down Expand Up @@ -166,6 +173,7 @@ export class AgentsController {
organizationId: user.organizationId,
agentIdentifier: identifier,
integrationIdentifier: body.integrationIdentifier,
providerId: body.providerId,
})
);
}
Expand Down Expand Up @@ -274,14 +282,16 @@ export class AgentsController {
@RequirePermissions(PermissionsEnum.AGENT_WRITE)
sendAgentTestEmail(
@UserSession() user: UserSessionData,
@Param('identifier') identifier: string
@Param('identifier') identifier: string,
@Body() body: SendAgentTestEmailRequestDto
): Promise<{ success: boolean }> {
return this.sendAgentTestEmailUsecase.execute(
SendAgentTestEmailCommand.create({
userId: user._id,
environmentId: user.environmentId,
organizationId: user.organizationId,
agentIdentifier: identifier,
targetAddress: body.targetAddress,
})
);
}
Expand Down
19 changes: 15 additions & 4 deletions apps/api/src/app/agents/dtos/add-agent-integration-request.dto.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString } from 'class-validator';
import { ApiPropertyOptional } from '@nestjs/swagger';
import { IsNotEmpty, IsString, ValidateIf } from 'class-validator';

export class AddAgentIntegrationRequestDto {
@ApiProperty({
@ApiPropertyOptional({
description: 'The integration identifier (same as in the integration store), not the internal document _id.',
})
@ValidateIf((o) => !o.providerId)
@IsString()
@IsNotEmpty()
integrationIdentifier: string;
integrationIdentifier?: string;

@ApiPropertyOptional({
description:
'Provider ID to auto-create a dedicated integration (e.g. novu-agent-email). ' +
'When set, the server creates the integration if one does not already exist for this agent.',
})
@ValidateIf((o) => !o.integrationIdentifier)
@IsString()
@IsNotEmpty()
providerId?: string;
Comment thread
ChmaraX marked this conversation as resolved.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsEmail, IsNotEmpty } from 'class-validator';

export class SendAgentTestEmailRequestDto {
@ApiProperty({ description: 'Full inbound email address to send the test to (e.g. support@acme.com)' })
@IsEmail()
@IsNotEmpty()
targetAddress: string;
}
34 changes: 19 additions & 15 deletions apps/api/src/app/agents/services/chat-sdk.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,15 +306,23 @@ export class ChatSdkService implements OnModuleDestroy {
token: c.token ?? null,
phoneNumberIdentification: c.phoneNumberIdentification ?? null,
connectionAccessToken: connectionAccessToken ?? null,
replyDomain: c.replyDomain ?? null,
outboundIntegrationId: c.outboundIntegrationId ?? null,
});
}

private buildSendEmailCallback(
config: ResolvedAgentConfig,
outboundIntegrationId: string | undefined
): (params: { to: string; subject: string; html: string; text?: string; inReplyTo?: string; references?: string; messageId?: string }) => Promise<{ messageId: string }> {
): (params: {
from: string;
to: string;
subject: string;
html: string;
text?: string;
inReplyTo?: string;
references?: string;
messageId?: string;
}) => Promise<{ messageId: string }> {
return async (params) => {
if (!outboundIntegrationId) {
throw new BadRequestException(
Expand Down Expand Up @@ -350,22 +358,21 @@ export class ChatSdkService implements OnModuleDestroy {

const decrypted = decryptCredentials(integration.credentials);
const mailFactory = new MailFactory();
const handler = mailFactory.getHandler(
{ ...integration, credentials: decrypted },
config.credentials.replyDomain
);
const handler = mailFactory.getHandler({ ...integration, credentials: decrypted }, params.from);

const mailOptions: IEmailOptions = {
to: [params.to],
subject: params.subject,
html: params.html,
text: params.text,
from: config.credentials.replyDomain,
from: params.from,
senderName: config.credentials.senderName || undefined,
headers: {
...(params.messageId ? { 'Message-ID': wrapMsgId(params.messageId) } : {}),
...(params.inReplyTo ? { 'In-Reply-To': wrapMsgId(params.inReplyTo) } : {}),
...(params.references ? { References: params.references.split(/\s+/).filter(Boolean).map(wrapMsgId).join(' ') } : {}),
...(params.references
? { References: params.references.split(/\s+/).filter(Boolean).map(wrapMsgId).join(' ') }
: {}),
},
};

Expand Down Expand Up @@ -474,20 +481,17 @@ export class ChatSdkService implements OnModuleDestroy {
};
}
case AgentPlatformEnum.EMAIL: {
const { replyDomain, senderName, outboundIntegrationId } = credentials;
const { senderName, outboundIntegrationId } = credentials;

if (!replyDomain || !credentials.secretKey) {
throw new BadRequestException(
'Email agent integration requires replyDomain and secretKey credentials'
);
if (!credentials.secretKey) {
throw new BadRequestException('Email agent integration requires secretKey credentials');
}

const { createNovuEmailAdapter } = await esmImport('@novu/chat-adapter-email');

return {
email: createNovuEmailAdapter({
fromAddress: replyDomain,
fromName: senderName,
senderName,
signingSecret: credentials.secretKey,
sendEmail: this.buildSendEmailCallback(config, outboundIntegrationId),
}),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IsNotEmpty, IsString } from 'class-validator';
import { IsNotEmpty, IsOptional, IsString } from 'class-validator';

import { EnvironmentWithUserCommand } from '../../../shared/commands/project.command';

Expand All @@ -8,6 +8,10 @@ export class AddAgentIntegrationCommand extends EnvironmentWithUserCommand {
agentIdentifier: string;

@IsString()
@IsNotEmpty()
integrationIdentifier: string;
@IsOptional()
integrationIdentifier?: string;

@IsString()
@IsOptional()
providerId?: string;
}
Loading
Loading