Skip to content

Commit 7880106

Browse files
authored
fix(server): emit MESSAGE_SENT event after sending to central server (#6378)
Fixes #5216 - Emits MESSAGE_SENT event after successfully sending agent responses to the central server API. This allows plugins with MESSAGE_SENT event handlers to be triggered for messages submitted to the central channel.
1 parent b655859 commit 7880106

1 file changed

Lines changed: 29 additions & 13 deletions

File tree

packages/server/src/services/message.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import {
77
validateUuid,
88
type Content,
99
type IAgentRuntime,
10+
type Memory,
1011
type Plugin,
1112
type UUID,
1213
ElizaOS,
14+
EventType,
1315
} from '@elizaos/core';
1416
import type { AgentServer } from '../index.js';
1517
import type {
@@ -677,25 +679,28 @@ export class MessageBusService extends Service {
677679
'Agent generated response, sending to bus'
678680
);
679681

680-
await this.runtime.createMemory(
681-
{
682-
id: responseContent.responseId as UUID | undefined,
683-
entityId: this.runtime.agentId,
684-
content: responseContent,
685-
roomId: agentRoomId,
686-
worldId: agentWorldId,
687-
agentId: this.runtime.agentId,
688-
},
689-
'messages'
690-
);
682+
// Create the message memory object for event emission
683+
const memoryData: Memory = {
684+
id: responseContent.responseId as UUID | undefined || createUniqueUuid(this.runtime, `response-${Date.now()}`),
685+
entityId: this.runtime.agentId,
686+
roomId: agentRoomId,
687+
worldId: agentWorldId,
688+
content: responseContent,
689+
agentId: this.runtime.agentId,
690+
createdAt: Date.now(),
691+
};
692+
693+
// Create memory in database and get its ID
694+
const memoryId = await this.runtime.createMemory(memoryData, 'messages');
691695

692696
// Send response to central bus
693697
await this.sendAgentResponseToBus(
694698
agentRoomId,
695699
agentWorldId,
696700
responseContent,
697701
uniqueMemoryId,
698-
message
702+
message,
703+
{ ...memoryData, id: memoryId }
699704
);
700705
},
701706
onError: async (error: Error) => {
@@ -833,7 +838,8 @@ export class MessageBusService extends Service {
833838
agentWorldId: UUID,
834839
content: Content,
835840
inReplyToAgentMemoryId?: UUID,
836-
originalMessage?: MessageServiceMessage
841+
originalMessage?: MessageServiceMessage,
842+
messageMemory?: Memory
837843
) {
838844
try {
839845
const room = await this.runtime.getRoom(agentRoomId);
@@ -931,6 +937,16 @@ export class MessageBusService extends Service {
931937
},
932938
'Error sending response to central server'
933939
);
940+
return;
941+
}
942+
943+
// Emit MESSAGE_SENT event after successfully sending to central server
944+
if (messageMemory) {
945+
await this.runtime.emitEvent(EventType.MESSAGE_SENT, {
946+
runtime: this.runtime,
947+
source: 'central-bus',
948+
message: messageMemory,
949+
});
934950
}
935951
} catch (error) {
936952
logger.error(

0 commit comments

Comments
 (0)