improvement(memory): return memories in add, adjusted other memory tools

This commit is contained in:
Emir Karabeg
2025-05-20 12:46:15 -07:00
parent 6170b80086
commit 33123c6361
4 changed files with 60 additions and 45 deletions

View File

@@ -206,6 +206,8 @@ export async function POST(request: NextRequest) {
)
.limit(1)
let statusCode = 201 // Default status code for new memory
if (existingMemory.length > 0) {
logger.info(`[${requestId}] Memory with key ${key} exists, checking if we can append`)
@@ -263,48 +265,60 @@ export async function POST(request: NextRequest) {
)
)
// Fetch the updated memory
const updatedMemory = await db
.select()
.from(memory)
.where(
and(
eq(memory.key, key),
eq(memory.workflowId, workflowId)
)
)
.limit(1)
statusCode = 200 // Status code for updated memory
} else {
// Insert the new memory
const newMemory = {
id: `mem_${crypto.randomUUID().replace(/-/g, '')}`,
workflowId,
key,
type,
data: type === 'agent' ? Array.isArray(data) ? data : [data] : data,
createdAt: new Date(),
updatedAt: new Date()
}
logger.info(`[${requestId}] Memory appended successfully: ${key} for workflow: ${workflowId}`)
await db.insert(memory).values(newMemory)
logger.info(`[${requestId}] Memory created successfully: ${key} for workflow: ${workflowId}`)
}
// Fetch all memories with the same key for this workflow to return the complete list
const allMemories = await db
.select()
.from(memory)
.where(
and(
eq(memory.key, key),
eq(memory.workflowId, workflowId),
isNull(memory.deletedAt)
)
)
.orderBy(memory.createdAt)
if (allMemories.length === 0) {
// This shouldn't happen but handle it just in case
logger.warn(`[${requestId}] No memories found after creating/updating memory: ${key}`)
return NextResponse.json(
{
success: true,
data: updatedMemory[0]
success: false,
error: {
message: 'Failed to retrieve memory after creation/update',
},
},
{ status: 200 }
{ status: 500 }
)
}
// Insert the new memory
const newMemory = {
id: `mem_${crypto.randomUUID().replace(/-/g, '')}`,
workflowId,
key,
type,
data: type === 'agent' ? Array.isArray(data) ? data : [data] : data,
createdAt: new Date(),
updatedAt: new Date()
}
// Get the memory object to return
const memoryRecord = allMemories[0];
await db.insert(memory).values(newMemory)
logger.info(`[${requestId}] Memory created successfully: ${key} for workflow: ${workflowId}`)
logger.info(`[${requestId}] Memory operation successful: ${key} for workflow: ${workflowId}`)
return NextResponse.json(
{
success: true,
data: newMemory
data: memoryRecord
},
{ status: 201 }
{ status: statusCode }
)
} catch (error: any) {

View File

@@ -130,29 +130,34 @@ export const memoryAddTool: ToolConfig<any, MemoryResponse> = {
transformResponse: async (response): Promise<MemoryResponse> => {
try {
const result = await response.json()
if (!response.ok) {
const errorMessage = result.error?.message || 'Failed to add memory'
throw new Error(errorMessage)
}
let errorMessage = result.error?.message || 'Failed to add memory'
const data = result.data || result
const isNewMemory = response.status === 201
// Extract the memories from the response based on memory type
let memories
if (data.type === 'agent') {
// For agent memories, return the full array of message objects
memories = Array.isArray(data.data) ? data.data : [data.data]
} else {
// For raw memories, return the raw data object
memories = data.data
}
return {
success: true,
output: {
memories: data.data,
message: isNewMemory ? 'Memory created successfully' : 'Memory appended successfully'
memories,
},
error: errorMessage
}
} catch (error: any) {
return {
success: false,
output: {
memories: undefined,
message: `Failed to add memory: ${error.message || 'Unknown error occurred'}`
},
error
}
}
},

View File

@@ -54,15 +54,13 @@ export const memoryDeleteTool: ToolConfig<any, MemoryResponse> = {
return {
success: true,
output: {
memory: undefined,
message: `Deleted memory.`
message: `Memory deleted successfully.`
},
}
} catch (error: any) {
return {
success: false,
output: {
memory: undefined,
message: `Failed to delete memory: ${error.message || 'Unknown error'}`
},
error: `Failed to delete memory: ${error.message || 'Unknown error'}`
@@ -74,7 +72,6 @@ export const memoryDeleteTool: ToolConfig<any, MemoryResponse> = {
return {
success: false,
output: {
memory: undefined,
message: errorMessage
},
error: errorMessage

View File

@@ -2,9 +2,8 @@ import { ToolResponse } from '../types'
export interface MemoryResponse extends ToolResponse {
output: {
memory?: any
memories?: any[]
message: string
message?: string
}
}