@@ -326,6 +326,18 @@ export class AIService {
326
326
throw error ;
327
327
}
328
328
}
329
+ function contentToUuid ( content :string ) :UUID {
330
+ // Step 1: Create a SHA-1 hash of the content
331
+ const hash = crypto . createHash ( 'sha1' )
332
+ . update ( content )
333
+ . digest ( 'hex' ) ; // Outputs a 40-character hex string
334
+
335
+ // Step 2: Format the hash into a UUID-like structure
336
+ // UUID format: 8-4-4-4-12 (e.g., 550e8400-e29b-41d4-a716-446655440000)
337
+ const uuid = `${ hash . slice ( 0 , 8 ) } -${ hash . slice ( 8 , 12 ) } -${ hash . slice ( 12 , 16 ) } -${ hash . slice ( 16 , 20 ) } -${ hash . slice ( 20 , 32 ) } ` ;
338
+
339
+ return uuid as `${string } -${string } -${string } -${string } -${string } `;
340
+ }
329
341
330
342
async function processChunk ( prompt : string , manager : string , runtime : AgentRuntime ) : Promise < string > {
331
343
console . log ( "process chunk" ) ;
@@ -336,15 +348,30 @@ async function processChunk(prompt: string, manager: string, runtime: AgentRunti
336
348
console . log ( "memoryManagers" , runtime . memoryManagers ) ;
337
349
throw new Error ( `Memory manager not found {manager}` ) ;
338
350
}
339
- const memory = await mm . addEmbeddingToMemory ( {
340
- agentId : runtime . agentId ,
341
- userId : runtime . agentId , //|| runtime.userId,
342
- content : { text : prompt } ,
343
- roomId : runtime . agentId ,
344
- embedding : await embed ( runtime , prompt ) ,
345
- } ) ;
346
- await mm . createMemory ( memory ) ;
347
- return "Success" ;
351
+
352
+ const memId = contentToUuid ( prompt ) ;
353
+ const existingResponse =
354
+ await mm . getMemoryById (
355
+ memId
356
+ ) ;
357
+ if ( ! existingResponse ) {
358
+ console . log ( "create new memory" , memId ) ;
359
+ const memory = await mm . addEmbeddingToMemory ( {
360
+ id : memId ,
361
+ agentId : runtime . agentId ,
362
+ userId : runtime . agentId , //|| runtime.userId,
363
+ content : { text : prompt } ,
364
+ roomId : runtime . agentId ,
365
+ embedding : await embed ( runtime , prompt ) ,
366
+ } ) ;
367
+ await mm . createMemory ( memory ) ;
368
+ return "Success" ;
369
+ }
370
+ else {
371
+ console . log ( "existingResponse" , memId , existingResponse ) ;
372
+ return "Success" ;
373
+ }
374
+
348
375
} catch ( error : any ) {
349
376
console . error ( "Failed to process chunk:" , error ) ;
350
377
return "Failed" ;
@@ -353,18 +380,6 @@ async function processChunk(prompt: string, manager: string, runtime: AgentRunti
353
380
354
381
355
382
356
- function contentToUuid ( content :string ) :UUID {
357
- // Step 1: Create a SHA-1 hash of the content
358
- const hash = crypto . createHash ( 'sha1' )
359
- . update ( content )
360
- . digest ( 'hex' ) ; // Outputs a 40-character hex string
361
-
362
- // Step 2: Format the hash into a UUID-like structure
363
- // UUID format: 8-4-4-4-12 (e.g., 550e8400-e29b-41d4-a716-446655440000)
364
- const uuid = `${ hash . slice ( 0 , 8 ) } -${ hash . slice ( 8 , 12 ) } -${ hash . slice ( 12 , 16 ) } -${ hash . slice ( 16 , 20 ) } -${ hash . slice ( 20 , 32 ) } ` ;
365
-
366
- return uuid as `${string } -${string } -${string } -${string } -${string } `;
367
- }
368
383
369
384
// async function process_text(this: any, agentId: string, userName: string, name: string, sUserId: string | undefined,
370
385
// sRoomId: string, text:string, agents: Map<string, any>) {
0 commit comments