@@ -175,6 +175,8 @@ export class MastraAgent extends AbstractAgent {
175
175
}
176
176
177
177
protected run ( input : RunAgentInput ) : Observable < BaseEvent > {
178
+ console . log ( "STATE INPUT" , input . state ) ;
179
+
178
180
const finalMessages : Message [ ] = [ ...input . messages ] ;
179
181
let messageId = randomUUID ( ) ;
180
182
let assistantMessage : AssistantMessage = {
@@ -194,32 +196,34 @@ export class MastraAgent extends AbstractAgent {
194
196
} as RunStartedEvent ) ;
195
197
196
198
// Handle local agent memory management (from Mastra implementation)
197
- if ( ' metrics' in this . agent ) {
199
+ if ( " metrics" in this . agent ) {
198
200
const memory = this . agent . getMemory ( ) ;
199
201
200
202
if ( memory && input . state && Object . keys ( input . state || { } ) . length > 0 ) {
201
- let thread : StorageThreadType | null = await memory . getThreadById ( { threadId : input . threadId } ) ;
203
+ let thread : StorageThreadType | null = await memory . getThreadById ( {
204
+ threadId : input . threadId ,
205
+ } ) ;
202
206
203
207
if ( ! thread ) {
204
208
thread = {
205
209
id : input . threadId ,
206
- title : '' ,
210
+ title : "" ,
207
211
metadata : { } ,
208
- resourceId : this . resourceId as string ,
212
+ resourceId : this . resourceId ?? input . threadId ,
209
213
createdAt : new Date ( ) ,
210
214
updatedAt : new Date ( ) ,
211
215
} ;
212
216
}
213
217
214
- if ( thread . resourceId && thread . resourceId !== this . resourceId ) {
215
- throw new Error (
216
- `Thread with id ${ input . threadId } resourceId does not match the current resourceId ${ this . resourceId } ` ,
217
- ) ;
218
- }
218
+ // if (thread.resourceId && thread.resourceId !== this.resourceId) {
219
+ // throw new Error(
220
+ // `Thread with id ${input.threadId} resourceId does not match the current resourceId ${this.resourceId}`,
221
+ // );
222
+ // }
219
223
220
224
const { messages, ...rest } = input . state ;
221
225
const workingMemory = JSON . stringify ( rest ) ;
222
-
226
+
223
227
// Update thread metadata with new working memory
224
228
await memory . saveThread ( {
225
229
thread : {
@@ -294,10 +298,16 @@ export class MastraAgent extends AbstractAgent {
294
298
} ;
295
299
subscriber . next ( event ) ;
296
300
297
- if ( ' metrics' in this . agent ) {
301
+ if ( " metrics" in this . agent ) {
298
302
const memory = this . agent . getMemory ( ) ;
299
303
if ( memory ) {
300
- const workingMemory = await memory . getWorkingMemory ( { threadId : input . threadId , format : 'json' } ) ;
304
+ const workingMemory = await memory . getWorkingMemory ( {
305
+ threadId : input . threadId ,
306
+ format : "json" ,
307
+ } ) ;
308
+
309
+ console . log ( ">>> workingMemory" , workingMemory ) ;
310
+
301
311
subscriber . next ( {
302
312
type : EventType . STATE_SNAPSHOT ,
303
313
snapshot : workingMemory ,
@@ -383,9 +393,12 @@ export class MastraAgent extends AbstractAgent {
383
393
384
394
// For local agents, the response should already be a stream
385
395
// Process it using the agent's built-in streaming mechanism
386
- if ( response && typeof response === ' object' ) {
396
+ if ( response && typeof response === " object" ) {
387
397
// If the response has a toDataStreamResponse method, use it
388
- if ( 'toDataStreamResponse' in response && typeof response . toDataStreamResponse === 'function' ) {
398
+ if (
399
+ "toDataStreamResponse" in response &&
400
+ typeof response . toDataStreamResponse === "function"
401
+ ) {
389
402
const dataStreamResponse = response . toDataStreamResponse ( ) ;
390
403
if ( dataStreamResponse && dataStreamResponse . body ) {
391
404
await processDataStream ( {
@@ -396,7 +409,7 @@ export class MastraAgent extends AbstractAgent {
396
409
onFinishMessagePart,
397
410
} ) ;
398
411
} else {
399
- throw new Error ( ' Invalid data stream response from local agent' ) ;
412
+ throw new Error ( " Invalid data stream response from local agent" ) ;
400
413
}
401
414
} else {
402
415
// If it's already a readable stream, process it directly
@@ -409,7 +422,7 @@ export class MastraAgent extends AbstractAgent {
409
422
} ) ;
410
423
}
411
424
} else {
412
- throw new Error ( ' Invalid response from local agent' ) ;
425
+ throw new Error ( " Invalid response from local agent" ) ;
413
426
}
414
427
} catch ( error ) {
415
428
onError ?.( error as Error ) ;
@@ -426,15 +439,15 @@ export class MastraAgent extends AbstractAgent {
426
439
} ) ;
427
440
428
441
// Remote agents should have a processDataStream method
429
- if ( response && typeof response . processDataStream === ' function' ) {
442
+ if ( response && typeof response . processDataStream === " function" ) {
430
443
await response . processDataStream ( {
431
444
onTextPart,
432
445
onToolCallPart,
433
446
onToolResultPart,
434
447
onFinishMessagePart,
435
448
} ) ;
436
449
} else {
437
- throw new Error ( ' Invalid response from remote agent' ) ;
450
+ throw new Error ( " Invalid response from remote agent" ) ;
438
451
}
439
452
} catch ( error ) {
440
453
onError ?.( error as Error ) ;
0 commit comments