11import { BadRequestException , forwardRef , Inject , Injectable , OnModuleDestroy } from '@nestjs/common' ;
22import { PinoLogger } from '@novu/application-generic' ;
3- import type { Chat , Message , Thread } from 'chat' ;
3+ import type { Chat , EmojiValue , Message , ReactionEvent , Thread } from 'chat' ;
44import { Request as ExpressRequest , Response as ExpressResponse } from 'express' ;
55import { LRUCache } from 'lru-cache' ;
66import { AgentEventEnum } from '../dtos/agent-event.enum' ;
77import { AgentPlatformEnum } from '../dtos/agent-platform.enum' ;
88import type { ReplyContentDto } from '../dtos/agent-reply-payload.dto' ;
9+ import { esmImport } from '../utils/esm-import' ;
910import { sendWebResponse , toWebRequest } from '../utils/express-to-web-request' ;
1011import { AgentConfigResolver , ResolvedAgentConfig } from './agent-config-resolver.service' ;
1112import { AgentInboundHandler } from './agent-inbound-handler.service' ;
@@ -26,11 +27,6 @@ import { AgentInboundHandler } from './agent-inbound-handler.service';
2627 * credentials.phoneNumberIdentification → phoneNumberId
2728 */
2829
29- // Chat SDK packages are ESM-only; SWC rewrites import() → require() for CJS output.
30- // Wrapping in new Function prevents SWC from seeing the import() keyword.
31- // eslint-disable-next-line @typescript-eslint/no-implied-eval
32- const esmImport = new Function ( 'specifier' , 'return import(specifier)' ) as ( s : string ) => Promise < any > ;
33-
3430const MAX_CACHED_INSTANCES = 200 ;
3531const INSTANCE_TTL_MS = 1000 * 60 * 30 ;
3632
@@ -133,14 +129,15 @@ export class ChatSdkService implements OnModuleDestroy {
133129 platform : string ,
134130 platformThreadId : string ,
135131 platformMessageId : string ,
136- emoji : string
132+ emojiName : string
137133 ) : Promise < void > {
138134 const config = await this . agentConfigResolver . resolve ( agentId , integrationIdentifier ) ;
139135 const instanceKey = `${ agentId } :${ integrationIdentifier } ` ;
140136 const chat = await this . getOrCreate ( instanceKey , agentId , config . platform , config ) ;
141137
142138 const adapter = chat . getAdapter ( platform ) ;
143- await adapter . removeReaction ( platformThreadId , platformMessageId , emoji ) ;
139+ const resolved = await this . resolveEmoji ( emojiName ) ;
140+ await adapter . removeReaction ( platformThreadId , platformMessageId , resolved ) ;
144141 }
145142
146143 async reactToMessage (
@@ -149,14 +146,25 @@ export class ChatSdkService implements OnModuleDestroy {
149146 platform : string ,
150147 platformThreadId : string ,
151148 platformMessageId : string ,
152- emoji : string
149+ emojiName : string
153150 ) : Promise < void > {
154151 const config = await this . agentConfigResolver . resolve ( agentId , integrationIdentifier ) ;
155152 const instanceKey = `${ agentId } :${ integrationIdentifier } ` ;
156153 const chat = await this . getOrCreate ( instanceKey , agentId , config . platform , config ) ;
157154
158155 const adapter = chat . getAdapter ( platform ) ;
159- await adapter . addReaction ( platformThreadId , platformMessageId , emoji ) ;
156+ const resolved = await this . resolveEmoji ( emojiName ) ;
157+ await adapter . addReaction ( platformThreadId , platformMessageId , resolved ) ;
158+ }
159+
160+ private async resolveEmoji ( name : string ) : Promise < EmojiValue > {
161+ const { getEmoji } = await esmImport ( 'chat' ) ;
162+ const resolved = getEmoji ( name ) ;
163+ if ( ! resolved ) {
164+ throw new Error ( `Unknown emoji name: "${ name } ". Use GET /agents/emoji to list supported options.` ) ;
165+ }
166+
167+ return resolved ;
160168 }
161169
162170 private async getOrCreate (
@@ -379,14 +387,14 @@ export class ChatSdkService implements OnModuleDestroy {
379387 }
380388 } ) ;
381389
382- cached . chat . onReaction ( async ( event : any ) => {
390+ cached . chat . onReaction ( async ( event : ReactionEvent ) => {
383391 try {
384392 await this . inboundHandler . handleReaction ( agentId , cached . config , {
385393 emoji : event . emoji ,
386394 added : event . added ,
387395 messageId : event . messageId ,
388396 message : event . message ,
389- thread : event . thread ,
397+ thread : event . thread as Thread | undefined ,
390398 user : event . user ,
391399 } ) ;
392400 } catch ( err ) {
0 commit comments