@@ -26,7 +26,6 @@ import { processDataStream } from "@ai-sdk/ui-utils";
26
26
import type { CoreMessage , Mastra , StorageThreadType } from "@mastra/core" ;
27
27
import { registerApiRoute } from "@mastra/core/server" ;
28
28
import { Agent as LocalMastraAgent } from "@mastra/core/agent" ;
29
- import type { Context } from "hono" ;
30
29
import { RuntimeContext } from "@mastra/core/runtime-context" ;
31
30
import { randomUUID } from "crypto" ;
32
31
import { Observable } from "rxjs" ;
@@ -175,7 +174,7 @@ export class MastraAgent extends AbstractAgent {
175
174
}
176
175
177
176
protected run ( input : RunAgentInput ) : Observable < BaseEvent > {
178
- console . log ( "STATE INPUT" , input . state ) ;
177
+ // console.log("STATE INPUT", input.state);
179
178
180
179
const finalMessages : Message [ ] = [ ...input . messages ] ;
181
180
let messageId = randomUUID ( ) ;
@@ -189,6 +188,8 @@ export class MastraAgent extends AbstractAgent {
189
188
190
189
return new Observable < BaseEvent > ( ( subscriber ) => {
191
190
const run = async ( ) => {
191
+ console . log ( ">>> RUN STARTED" , input . threadId , input . runId ) ;
192
+ console . log ( ">>> STATE" , JSON . stringify ( input . state ) ) ;
192
193
subscriber . next ( {
193
194
type : EventType . RUN_STARTED ,
194
195
threadId : input . threadId ,
@@ -204,6 +205,8 @@ export class MastraAgent extends AbstractAgent {
204
205
threadId : input . threadId ,
205
206
} ) ;
206
207
208
+ console . log ( ">>> THREAD (getThreadById)" , thread ) ;
209
+
207
210
if ( ! thread ) {
208
211
thread = {
209
212
id : input . threadId ,
@@ -215,15 +218,25 @@ export class MastraAgent extends AbstractAgent {
215
218
} ;
216
219
}
217
220
221
+ // TODO: thread.metadata / use previous memory with spread operator to preserve it
222
+ // TODO: state is empty when coming back from AG-UI
223
+
218
224
// if (thread.resourceId && thread.resourceId !== this.resourceId) {
219
225
// throw new Error(
220
226
// `Thread with id ${input.threadId} resourceId does not match the current resourceId ${this.resourceId}`,
221
227
// );
222
- // }
223
228
224
229
const { messages, ...rest } = input . state ;
225
230
const workingMemory = JSON . stringify ( rest ) ;
226
231
232
+ console . log ( ">>> SAVING THREAD" , {
233
+ ...thread ,
234
+ metadata : {
235
+ ...thread . metadata ,
236
+ workingMemory,
237
+ } ,
238
+ } ) ;
239
+
227
240
// Update thread metadata with new working memory
228
241
await memory . saveThread ( {
229
242
thread : {
@@ -303,7 +316,12 @@ export class MastraAgent extends AbstractAgent {
303
316
if ( memory ) {
304
317
const workingMemory = await memory . getWorkingMemory ( {
305
318
threadId : input . threadId ,
306
- format : "json" ,
319
+ // @ts -ignore
320
+ memoryConfig : {
321
+ workingMemory : {
322
+ enabled : true ,
323
+ } ,
324
+ } ,
307
325
} ) ;
308
326
309
327
console . log ( ">>> workingMemory" , workingMemory ) ;
@@ -519,14 +537,7 @@ export function registerCopilotKit<T extends Record<string, any> | unknown = unk
519
537
resourceId : string ;
520
538
serviceAdapter ?: CopilotServiceAdapter ;
521
539
agents ?: Record < string , AbstractAgent > ;
522
- setContext ?: (
523
- c : Context < {
524
- Variables : {
525
- mastra : Mastra ;
526
- } ;
527
- } > ,
528
- runtimeContext : RuntimeContext < T > ,
529
- ) => void | Promise < void > ;
540
+ setContext ?: ( c : any , runtimeContext : RuntimeContext < T > ) => void | Promise < void > ;
530
541
} ) {
531
542
return registerApiRoute ( path , {
532
543
method : `ALL` ,
0 commit comments