Skip to content

Commit 03c0adc

Browse files
committed
improvement(memory): should not be block scoped
1 parent f21eaf1 commit 03c0adc

File tree

13 files changed

+113
-548
lines changed

13 files changed

+113
-548
lines changed

apps/sim/app/api/memory/[id]/route.ts

Lines changed: 10 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { db } from '@sim/db'
2-
import { memory, workflowBlocks } from '@sim/db/schema'
2+
import { memory } from '@sim/db/schema'
33
import { and, eq } from 'drizzle-orm'
44
import { type NextRequest, NextResponse } from 'next/server'
55
import { z } from 'zod'
@@ -10,43 +10,6 @@ import { getWorkflowAccessContext } from '@/lib/workflows/utils'
1010

1111
const logger = createLogger('MemoryByIdAPI')
1212

13-
/**
14-
* Parse memory key into conversationId and blockId
15-
* Key format: conversationId:blockId
16-
*/
17-
function parseMemoryKey(key: string): { conversationId: string; blockId: string } | null {
18-
const parts = key.split(':')
19-
if (parts.length !== 2) {
20-
return null
21-
}
22-
return {
23-
conversationId: parts[0],
24-
blockId: parts[1],
25-
}
26-
}
27-
28-
/**
29-
* Lookup block name from block ID
30-
*/
31-
async function getBlockName(blockId: string, workflowId: string): Promise<string | undefined> {
32-
try {
33-
const result = await db
34-
.select({ name: workflowBlocks.name })
35-
.from(workflowBlocks)
36-
.where(and(eq(workflowBlocks.id, blockId), eq(workflowBlocks.workflowId, workflowId)))
37-
.limit(1)
38-
39-
if (result.length === 0) {
40-
return undefined
41-
}
42-
43-
return result[0].name
44-
} catch (error) {
45-
logger.error('Error looking up block name', { error, blockId, workflowId })
46-
return undefined
47-
}
48-
}
49-
5013
const memoryQuerySchema = z.object({
5114
workflowId: z.string().uuid('Invalid workflow ID format'),
5215
})
@@ -130,7 +93,7 @@ export const dynamic = 'force-dynamic'
13093
export const runtime = 'nodejs'
13194

13295
/**
133-
* GET handler for retrieving a specific memory by ID
96+
* GET handler for retrieving a specific memory by ID (conversationId)
13497
*/
13598
export async function GET(request: NextRequest, { params }: { params: Promise<{ id: string }> }) {
13699
const requestId = generateRequestId()
@@ -188,26 +151,10 @@ export async function GET(request: NextRequest, { params }: { params: Promise<{
188151
}
189152

190153
const mem = memories[0]
191-
const parsed = parseMemoryKey(mem.key)
192-
193-
let enrichedMemory
194-
if (!parsed) {
195-
enrichedMemory = {
196-
conversationId: mem.key,
197-
blockId: 'unknown',
198-
blockName: 'unknown',
199-
data: mem.data,
200-
}
201-
} else {
202-
const { conversationId, blockId } = parsed
203-
const blockName = (await getBlockName(blockId, validatedWorkflowId)) || 'unknown'
204-
205-
enrichedMemory = {
206-
conversationId,
207-
blockId,
208-
blockName,
209-
data: mem.data,
210-
}
154+
155+
const enrichedMemory = {
156+
conversationId: mem.key,
157+
data: mem.data,
211158
}
212159

213160
logger.info(
@@ -424,26 +371,10 @@ export async function PUT(request: NextRequest, { params }: { params: Promise<{
424371
.limit(1)
425372

426373
const mem = updatedMemories[0]
427-
const parsed = parseMemoryKey(mem.key)
428-
429-
let enrichedMemory
430-
if (!parsed) {
431-
enrichedMemory = {
432-
conversationId: mem.key,
433-
blockId: 'unknown',
434-
blockName: 'unknown',
435-
data: mem.data,
436-
}
437-
} else {
438-
const { conversationId, blockId } = parsed
439-
const blockName = (await getBlockName(blockId, validatedWorkflowId)) || 'unknown'
440-
441-
enrichedMemory = {
442-
conversationId,
443-
blockId,
444-
blockName,
445-
data: mem.data,
446-
}
374+
375+
const enrichedMemory = {
376+
conversationId: mem.key,
377+
data: mem.data,
447378
}
448379

449380
logger.info(

0 commit comments

Comments
 (0)