diff --git a/packages/core/src/modules/basic-messages/BasicMessagesModule.ts b/packages/core/src/modules/basic-messages/BasicMessagesModule.ts index fc78e14a1e..d2b4a2a566 100644 --- a/packages/core/src/modules/basic-messages/BasicMessagesModule.ts +++ b/packages/core/src/modules/basic-messages/BasicMessagesModule.ts @@ -1,10 +1,11 @@ -import type { ConnectionRecord } from '../connections' import type { BasicMessageTags } from './repository/BasicMessageRecord' import { Lifecycle, scoped } from 'tsyringe' import { Dispatcher } from '../../agent/Dispatcher' import { MessageSender } from '../../agent/MessageSender' +import { createOutboundMessage } from '../../agent/helpers' +import { ConnectionService } from '../connections' import { BasicMessageHandler } from './handlers' import { BasicMessageService } from './services' @@ -13,15 +14,25 @@ import { BasicMessageService } from './services' export class BasicMessagesModule { private basicMessageService: BasicMessageService private messageSender: MessageSender - - public constructor(dispatcher: Dispatcher, basicMessageService: BasicMessageService, messageSender: MessageSender) { + private connectionService: ConnectionService + + public constructor( + dispatcher: Dispatcher, + basicMessageService: BasicMessageService, + messageSender: MessageSender, + connectionService: ConnectionService + ) { this.basicMessageService = basicMessageService this.messageSender = messageSender + this.connectionService = connectionService this.registerHandlers(dispatcher) } - public async sendMessage(connection: ConnectionRecord, message: string) { - const outboundMessage = await this.basicMessageService.send(message, connection) + public async sendMessage(connectionId: string, message: string) { + const connection = await this.connectionService.getById(connectionId) + + const basicMessage = await this.basicMessageService.createMessage(message, connection) + const outboundMessage = createOutboundMessage(connection, basicMessage) await this.messageSender.sendMessage(outboundMessage) } diff --git a/packages/core/src/modules/basic-messages/services/BasicMessageService.ts b/packages/core/src/modules/basic-messages/services/BasicMessageService.ts index 701c0b4a48..31e7b2b232 100644 --- a/packages/core/src/modules/basic-messages/services/BasicMessageService.ts +++ b/packages/core/src/modules/basic-messages/services/BasicMessageService.ts @@ -1,5 +1,4 @@ import type { InboundMessageContext } from '../../../agent/models/InboundMessageContext' -import type { OutboundMessage } from '../../../types' import type { ConnectionRecord } from '../../connections/repository/ConnectionRecord' import type { BasicMessageReceivedEvent } from '../BasicMessageEvents' import type { BasicMessageTags } from '../repository' @@ -7,7 +6,6 @@ import type { BasicMessageTags } from '../repository' import { Lifecycle, scoped } from 'tsyringe' import { EventEmitter } from '../../../agent/EventEmitter' -import { createOutboundMessage } from '../../../agent/helpers' import { BasicMessageEventTypes } from '../BasicMessageEvents' import { BasicMessageRole } from '../BasicMessageRole' import { BasicMessage } from '../messages' @@ -23,21 +21,20 @@ export class BasicMessageService { this.eventEmitter = eventEmitter } - public async send(message: string, connection: ConnectionRecord): Promise> { - const basicMessage = new BasicMessage({ - content: message, - }) + public async createMessage(message: string, connectionRecord: ConnectionRecord) { + const basicMessage = new BasicMessage({ content: message }) const basicMessageRecord = new BasicMessageRecord({ id: basicMessage.id, sentTime: basicMessage.sentTime.toISOString(), content: basicMessage.content, - connectionId: connection.id, + connectionId: connectionRecord.id, role: BasicMessageRole.Sender, }) await this.basicMessageRepository.save(basicMessageRecord) - return createOutboundMessage(connection, basicMessage) + + return basicMessage } /** diff --git a/packages/core/src/modules/routing/__tests__/mediation.test.ts b/packages/core/src/modules/routing/__tests__/mediation.test.ts index 0ec02619bb..e3b4352671 100644 --- a/packages/core/src/modules/routing/__tests__/mediation.test.ts +++ b/packages/core/src/modules/routing/__tests__/mediation.test.ts @@ -135,7 +135,7 @@ describe('mediator establishment', () => { expect(senderRecipientConnection.isReady).toBe(true) const message = 'hello, world' - await senderAgent.basicMessages.sendMessage(senderRecipientConnection, message) + await senderAgent.basicMessages.sendMessage(senderRecipientConnection.id, message) const basicMessage = await waitForBasicMessage(recipientAgent, { content: message, @@ -249,7 +249,7 @@ describe('mediator establishment', () => { expect(senderRecipientConnection.isReady).toBe(true) const message = 'hello, world' - await senderAgent.basicMessages.sendMessage(senderRecipientConnection, message) + await senderAgent.basicMessages.sendMessage(senderRecipientConnection.id, message) const basicMessage = await waitForBasicMessage(recipientAgent, { content: message, diff --git a/packages/core/tests/agents.test.ts b/packages/core/tests/agents.test.ts index 34a00b1084..5923eeab68 100644 --- a/packages/core/tests/agents.test.ts +++ b/packages/core/tests/agents.test.ts @@ -63,7 +63,7 @@ describe('agents', () => { test('send a message to connection', async () => { const message = 'hello, world' - await aliceAgent.basicMessages.sendMessage(aliceConnection, message) + await aliceAgent.basicMessages.sendMessage(aliceConnection.id, message) const basicMessage = await waitForBasicMessage(bobAgent, { content: message,