Skip to content

Commit 65546cf

Browse files
icecrasher321waleedlatif1
authored andcommitted
fix dedup logic
1 parent d682691 commit 65546cf

File tree

3 files changed

+21
-29
lines changed

3 files changed

+21
-29
lines changed

apps/sim/executor/handlers/agent/agent-handler.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -711,28 +711,29 @@ export class AgentBlockHandler implements BlockHandler {
711711

712712
// 2. Handle native memory: seed on first run, then fetch and append new user input
713713
if (memoryEnabled && ctx.workspaceId) {
714-
const hasExisting = await memoryService.hasMemory(ctx.workspaceId, inputs.conversationId!)
715-
716-
if (!hasExisting && conversationMessages.length > 0) {
717-
await memoryService.seedMemory(ctx, inputs, conversationMessages)
718-
}
719-
720714
const memoryMessages = await memoryService.fetchMemoryMessages(ctx, inputs)
715+
const hasExisting = memoryMessages.length > 0
721716

722-
messages.push(...memoryMessages)
723-
724-
// When memory exists, append the new user message from conversationMessages
725-
// (the User field in the agent config, e.g. <start.input>)
726-
if (hasExisting && conversationMessages.length > 0) {
727-
const latestUserFromInput = conversationMessages.filter((m) => m.role === 'user').pop()
728-
if (latestUserFromInput) {
729-
const lastUserInMemory = memoryMessages.filter((m) => m.role === 'user').pop()
730-
const isNewUserMessage =
731-
!lastUserInMemory || lastUserInMemory.content !== latestUserFromInput.content
717+
if (!hasExisting && conversationMessages.length > 0) {
718+
const taggedMessages = conversationMessages.map((m) =>
719+
m.role === 'user' ? { ...m, executionId: ctx.executionId } : m
720+
)
721+
await memoryService.seedMemory(ctx, inputs, taggedMessages)
722+
messages.push(...taggedMessages)
723+
} else {
724+
messages.push(...memoryMessages)
732725

733-
if (isNewUserMessage) {
734-
messages.push(latestUserFromInput)
735-
await memoryService.appendToMemory(ctx, inputs, latestUserFromInput)
726+
if (hasExisting && conversationMessages.length > 0) {
727+
const latestUserFromInput = conversationMessages.filter((m) => m.role === 'user').pop()
728+
if (latestUserFromInput) {
729+
const userMessageInThisRun = memoryMessages.some(
730+
(m) => m.role === 'user' && m.executionId === ctx.executionId
731+
)
732+
if (!userMessageInThisRun) {
733+
const taggedMessage = { ...latestUserFromInput, executionId: ctx.executionId }
734+
messages.push(taggedMessage)
735+
await memoryService.appendToMemory(ctx, inputs, taggedMessage)
736+
}
736737
}
737738
}
738739
}

apps/sim/executor/handlers/agent/memory.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,6 @@ import { PROVIDER_DEFINITIONS } from '@/providers/models'
1212
const logger = createLogger('Memory')
1313

1414
export class Memory {
15-
async hasMemory(workspaceId: string, conversationId: string): Promise<boolean> {
16-
const result = await db
17-
.select({ id: memory.id })
18-
.from(memory)
19-
.where(and(eq(memory.workspaceId, workspaceId), eq(memory.key, conversationId)))
20-
.limit(1)
21-
22-
return result.length > 0
23-
}
24-
2515
async fetchMemoryMessages(ctx: ExecutionContext, inputs: AgentInputs): Promise<Message[]> {
2616
if (!inputs.memoryType || inputs.memoryType === 'none') {
2717
return []

apps/sim/executor/handlers/agent/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export interface ToolInput {
4141
export interface Message {
4242
role: 'system' | 'user' | 'assistant'
4343
content: string
44+
executionId?: string
4445
function_call?: any
4546
tool_calls?: any[]
4647
}

0 commit comments

Comments
 (0)