mirror of
https://github.com/supermemoryai/supermemory.git
synced 2026-11-15 09:11:45 +00:00
Merge 7d91fb4d55 into 81350cb08a
This commit is contained in:
commit
b3c0b8d03e
2 changed files with 59 additions and 33 deletions
|
|
@ -16,7 +16,6 @@ import {
|
|||
createLogger,
|
||||
normalizeBaseUrl,
|
||||
validateApiKey,
|
||||
MemoryCache,
|
||||
buildMemoriesText,
|
||||
extractQueryText,
|
||||
wrapMemoryContext,
|
||||
|
|
@ -51,7 +50,6 @@ interface ProcessorContext {
|
|||
addMemory: "always" | "never"
|
||||
logger: Logger
|
||||
promptTemplate?: PromptTemplate
|
||||
memoryCache: MemoryCache<string>
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -73,7 +71,6 @@ function createProcessorContext(
|
|||
addMemory: options.addMemory ?? "always",
|
||||
logger,
|
||||
promptTemplate: options.promptTemplate,
|
||||
memoryCache: new MemoryCache<string>(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -140,8 +137,8 @@ export class SupermemoryInputProcessor implements Processor {
|
|||
const { messages, messageList, requestContext } = args
|
||||
|
||||
// Mastra owns tagged system messages by tag. Clear the previous value on
|
||||
// every invocation so empty, skipped, cached, fresh, and error paths cannot
|
||||
// leave stale Supermemory context behind.
|
||||
// every invocation so empty, skipped, fresh, and error paths cannot leave
|
||||
// stale Supermemory context behind.
|
||||
messageList.clearSystemMessages("supermemory")
|
||||
|
||||
try {
|
||||
|
|
@ -159,19 +156,6 @@ export class SupermemoryInputProcessor implements Processor {
|
|||
}
|
||||
|
||||
const effectiveThreadId = getEffectiveId(this.ctx, requestContext)
|
||||
const turnKey = MemoryCache.makeTurnKey(
|
||||
this.ctx.namespace,
|
||||
effectiveThreadId,
|
||||
this.ctx.mode,
|
||||
queryText || "",
|
||||
)
|
||||
|
||||
const cachedMemories = this.ctx.memoryCache.get(turnKey)
|
||||
if (cachedMemories) {
|
||||
this.ctx.logger.debug("Using cached memories", { turnKey })
|
||||
messageList.addSystem(wrapMemoryContext(cachedMemories), "supermemory")
|
||||
return messageList
|
||||
}
|
||||
|
||||
this.ctx.logger.info("Starting memory search", {
|
||||
namespace: this.ctx.namespace,
|
||||
|
|
@ -190,7 +174,6 @@ export class SupermemoryInputProcessor implements Processor {
|
|||
})
|
||||
|
||||
if (memories) {
|
||||
this.ctx.memoryCache.set(turnKey, memories)
|
||||
messageList.addSystem(wrapMemoryContext(memories), "supermemory")
|
||||
this.ctx.logger.debug("Injected memories into system prompt", {
|
||||
length: memories.length,
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import {
|
|||
profileBody,
|
||||
requestJson,
|
||||
requestPath,
|
||||
searchBody,
|
||||
} from "../v5-fetch"
|
||||
import {
|
||||
RequestContext,
|
||||
|
|
@ -194,10 +195,14 @@ describe("SupermemoryInputProcessor", () => {
|
|||
expect(systemCall?.args[1]).toBe("supermemory")
|
||||
})
|
||||
|
||||
it("should use cached memories on second call with same message", async () => {
|
||||
fetchMock.mockImplementation(async () =>
|
||||
jsonResponse(createMockProfileResponse(["Cached memory"])),
|
||||
)
|
||||
it("should fetch fresh profile memories for each new message", async () => {
|
||||
let callCount = 0
|
||||
fetchMock.mockImplementation(async () => {
|
||||
callCount++
|
||||
return jsonResponse(
|
||||
createMockProfileResponse([`Fact from call ${callCount}`]),
|
||||
)
|
||||
})
|
||||
|
||||
const processor = new SupermemoryInputProcessor({
|
||||
namespace: TEST_CONFIG.namespace,
|
||||
|
|
@ -206,29 +211,67 @@ describe("SupermemoryInputProcessor", () => {
|
|||
mode: "profile",
|
||||
})
|
||||
|
||||
const firstList = createMockMessageList()
|
||||
await processor.processInput({
|
||||
messages: [createMessage("user", "Hello")],
|
||||
systemMessages: [],
|
||||
messageList: firstList,
|
||||
abort: vi.fn() as never,
|
||||
retryCount: 0,
|
||||
})
|
||||
|
||||
const secondList = createMockMessageList()
|
||||
await processor.processInput({
|
||||
messages: [createMessage("user", "What do you know about me?")],
|
||||
systemMessages: [],
|
||||
messageList: secondList,
|
||||
abort: vi.fn() as never,
|
||||
retryCount: 0,
|
||||
})
|
||||
|
||||
expect(fetchMock).toHaveBeenCalledTimes(2)
|
||||
const injected = secondList.calls.find((c) => c.method === "addSystem")
|
||||
expect(injected?.args[0]).toContain("Fact from call 2")
|
||||
expect(injected?.args[0]).not.toContain("Fact from call 1")
|
||||
})
|
||||
|
||||
it("should fetch fresh search memories when the same message is repeated", async () => {
|
||||
let callCount = 0
|
||||
fetchMock.mockImplementation(async () => {
|
||||
callCount++
|
||||
return jsonResponse(searchBody([`Memory from call ${callCount}`]))
|
||||
})
|
||||
|
||||
const processor = new SupermemoryInputProcessor({
|
||||
namespace: TEST_CONFIG.namespace,
|
||||
id: TEST_CONFIG.id,
|
||||
apiKey: TEST_CONFIG.apiKey,
|
||||
mode: "query",
|
||||
})
|
||||
|
||||
const messages: MastraDBMessage[] = [createMessage("user", "Hello")]
|
||||
|
||||
const args1: ProcessInputArgs = {
|
||||
await processor.processInput({
|
||||
messages,
|
||||
systemMessages: [],
|
||||
messageList: createMockMessageList(),
|
||||
abort: vi.fn() as never,
|
||||
retryCount: 0,
|
||||
}
|
||||
})
|
||||
|
||||
await processor.processInput(args1)
|
||||
expect(fetchMock).toHaveBeenCalledTimes(1)
|
||||
|
||||
const args2: ProcessInputArgs = {
|
||||
const secondList = createMockMessageList()
|
||||
await processor.processInput({
|
||||
messages,
|
||||
systemMessages: [],
|
||||
messageList: createMockMessageList(),
|
||||
messageList: secondList,
|
||||
abort: vi.fn() as never,
|
||||
retryCount: 0,
|
||||
}
|
||||
})
|
||||
|
||||
await processor.processInput(args2)
|
||||
expect(fetchMock).toHaveBeenCalledTimes(1)
|
||||
expect(fetchMock).toHaveBeenCalledTimes(2)
|
||||
const injected = secondList.calls.find((c) => c.method === "addSystem")
|
||||
expect(injected?.args[0]).toContain("Memory from call 2")
|
||||
expect(injected?.args[0]).not.toContain("Memory from call 1")
|
||||
})
|
||||
|
||||
it("should refetch memories for different user message", async () => {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue