@@ -11,6 +11,7 @@ import {
11
11
12
12
import { REST , Routes } from "discord.js" ;
13
13
import { DirectClient } from "." ;
14
+ import { stringToUuid } from "@elizaos/core" ;
14
15
15
16
export function createApiRouter (
16
17
agents : Map < string , AgentRuntime > ,
@@ -121,5 +122,66 @@ export function createApiRouter(
121
122
}
122
123
} ) ;
123
124
125
+ router . get ( "/agents/:agentId/:roomId/memories" , async ( req , res ) => {
126
+ const agentId = req . params . agentId ;
127
+ const roomId = stringToUuid ( req . params . roomId ) ;
128
+ let runtime = agents . get ( agentId ) ;
129
+
130
+ // if runtime is null, look for runtime with the same name
131
+ if ( ! runtime ) {
132
+ runtime = Array . from ( agents . values ( ) ) . find (
133
+ ( a ) => a . character . name . toLowerCase ( ) === agentId . toLowerCase ( )
134
+ ) ;
135
+ }
136
+
137
+ if ( ! runtime ) {
138
+ res . status ( 404 ) . send ( "Agent not found" ) ;
139
+ return ;
140
+ }
141
+
142
+ try {
143
+ const memories = await runtime . messageManager . getMemories ( {
144
+ roomId,
145
+ } ) ;
146
+ const response = {
147
+ agentId,
148
+ roomId,
149
+ memories : memories . map ( ( memory ) => ( {
150
+ id : memory . id ,
151
+ userId : memory . userId ,
152
+ agentId : memory . agentId ,
153
+ createdAt : memory . createdAt ,
154
+ content : {
155
+ text : memory . content . text ,
156
+ action : memory . content . action ,
157
+ source : memory . content . source ,
158
+ url : memory . content . url ,
159
+ inReplyTo : memory . content . inReplyTo ,
160
+ attachments : memory . content . attachments ?. map (
161
+ ( attachment ) => ( {
162
+ id : attachment . id ,
163
+ url : attachment . url ,
164
+ title : attachment . title ,
165
+ source : attachment . source ,
166
+ description : attachment . description ,
167
+ text : attachment . text ,
168
+ contentType : attachment . contentType ,
169
+ } )
170
+ ) ,
171
+ } ,
172
+ embedding : memory . embedding ,
173
+ roomId : memory . roomId ,
174
+ unique : memory . unique ,
175
+ similarity : memory . similarity ,
176
+ } ) ) ,
177
+ } ;
178
+
179
+ res . json ( response ) ;
180
+ } catch ( error ) {
181
+ console . error ( "Error fetching memories:" , error ) ;
182
+ res . status ( 500 ) . json ( { error : "Failed to fetch memories" } ) ;
183
+ }
184
+ } ) ;
185
+
124
186
return router ;
125
187
}
0 commit comments