@@ -73,9 +73,8 @@ export class MastraAgent extends AbstractAgent {
7373 subscriber . next ( runStartedEvent ) ;
7474
7575 // Handle local agent memory management (from Mastra implementation)
76- if ( "metrics" in this . agent ) {
77- // @ts -ignore
78- const memory = this . agent . getMemory ( ) ;
76+ if ( this . isLocalMastraAgent ( this . agent ) ) {
77+ const memory = await this . agent . getMemory ( ) ;
7978
8079 if ( memory && input . state && Object . keys ( input . state || { } ) . length > 0 ) {
8180 let thread : StorageThreadType | null = await memory . getThreadById ( {
@@ -163,14 +162,12 @@ export class MastraAgent extends AbstractAgent {
163162 subscriber . error ( error ) ;
164163 } ,
165164 onRunFinished : async ( ) => {
166- if ( "metrics" in this . agent ) {
165+ if ( this . isLocalMastraAgent ( this . agent ) ) {
167166 try {
168- // @ts -ignore
169- const memory = this . agent . getMemory ( ) ;
167+ const memory = await this . agent . getMemory ( ) ;
170168 if ( memory ) {
171169 const workingMemory = await memory . getWorkingMemory ( {
172170 threadId : input . threadId ,
173- // @ts -ignore
174171 memoryConfig : {
175172 workingMemory : {
176173 enabled : true ,
@@ -219,6 +216,10 @@ export class MastraAgent extends AbstractAgent {
219216 } ) ;
220217 }
221218
219+ isLocalMastraAgent ( agent : LocalMastraAgent | RemoteMastraAgent ) : agent is LocalMastraAgent {
220+ return "getMemory" in agent ;
221+ }
222+
222223 /**
223224 * Streams in process or remote mastra agent.
224225 * @param input - The input for the mastra agent.
@@ -251,13 +252,7 @@ export class MastraAgent extends AbstractAgent {
251252 const convertedMessages = convertAGUIMessagesToMastra ( messages ) ;
252253 const runtimeContext = this . runtimeContext ;
253254
254- function isLocalMastraAgent (
255- agent : LocalMastraAgent | RemoteMastraAgent ,
256- ) : agent is LocalMastraAgent {
257- return "metrics" in agent ;
258- }
259-
260- if ( isLocalMastraAgent ( this . agent ) ) {
255+ if ( this . isLocalMastraAgent ( this . agent ) ) {
261256 // Local agent - use the agent's stream method directly
262257 try {
263258 const response = await this . agent . stream ( convertedMessages , {
0 commit comments