mirror of
https://github.com/supermemoryai/supermemory.git
synced 2026-09-07 08:26:15 +00:00
create memory adding option in vercel sdk (#484)
### TL;DR Added support for automatically saving user messages to Supermemory. ### What changed? - Added a new `addMemory` option to `wrapVercelLanguageModel` that accepts either "always" or "never" (defaults to "never") - Implemented the `addMemoryTool` function to save user messages to Supermemory - Modified the middleware to check the `addMemory` setting and save the last user message when appropriate - Initialized the Supermemory client in the middleware to enable memory storage ### How to test? 1. Set the `SUPERMEMORY_API_KEY` environment variable 2. Use the `wrapVercelLanguageModel` function with the new `addMemory: "always"` option 3. Send a user message through the model 4. Verify that the message is saved to Supermemory with the specified container tag ### Why make this change? This change enables automatic memory creation from user messages, which improves the system's ability to build a knowledge base without requiring explicit memory creation calls. This is particularly useful for applications that want to automatically capture and store user interactions for future reference.
This commit is contained in:
parent
177b5e3419
commit
91a2aa5fdb
3 changed files with 53 additions and 5 deletions
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"name": "@supermemory/tools",
|
||||
"type": "module",
|
||||
"version": "1.2.0",
|
||||
"version": "1.2.13",
|
||||
"description": "Memory tools for AI SDK and OpenAI function calling with supermemory",
|
||||
"scripts": {
|
||||
"build": "tsdown",
|
||||
|
|
|
|||
|
|
@ -37,7 +37,11 @@ import { createSupermemoryMiddleware } from "./middleware"
|
|||
const wrapVercelLanguageModel = (
|
||||
model: LanguageModelV2,
|
||||
containerTag: string,
|
||||
options?: { verbose?: boolean; mode?: "profile" | "query" | "full" },
|
||||
options?: {
|
||||
verbose?: boolean;
|
||||
mode?: "profile" | "query" | "full";
|
||||
addMemory?: "always" | "never";
|
||||
},
|
||||
): LanguageModelV2 => {
|
||||
const SUPERMEMORY_API_KEY = process.env.SUPERMEMORY_API_KEY
|
||||
|
||||
|
|
@ -47,10 +51,11 @@ const wrapVercelLanguageModel = (
|
|||
|
||||
const verbose = options?.verbose ?? false
|
||||
const mode = options?.mode ?? "profile"
|
||||
const addMemory = options?.addMemory ?? "never"
|
||||
|
||||
const wrappedModel = wrapLanguageModel({
|
||||
model,
|
||||
middleware: createSupermemoryMiddleware(containerTag, verbose, mode),
|
||||
middleware: createSupermemoryMiddleware(containerTag, verbose, mode, addMemory),
|
||||
})
|
||||
|
||||
return wrappedModel
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import type {
|
|||
LanguageModelV2Middleware,
|
||||
LanguageModelV2Message,
|
||||
} from "@ai-sdk/provider"
|
||||
import Supermemory from "supermemory"
|
||||
import { createLogger, type Logger } from "./logger"
|
||||
import { convertProfileToMarkdown, type ProfileStructure } from "./util"
|
||||
|
||||
|
|
@ -137,18 +138,60 @@ const addSystemPrompt = async (
|
|||
}
|
||||
}
|
||||
|
||||
const addMemoryTool = async (
|
||||
client: Supermemory,
|
||||
containerTag: string,
|
||||
content: string,
|
||||
logger: Logger,
|
||||
): Promise<void> => {
|
||||
try {
|
||||
const response = await client.memories.add({
|
||||
content,
|
||||
containerTags: [containerTag],
|
||||
})
|
||||
|
||||
logger.info("Memory saved successfully", {
|
||||
containerTag,
|
||||
contentLength: content.length,
|
||||
memoryId: response.id,
|
||||
})
|
||||
} catch (error) {
|
||||
logger.error("Error saving memory", {
|
||||
error: error instanceof Error ? error.message : "Unknown error",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export const createSupermemoryMiddleware = (
|
||||
containerTag: string,
|
||||
verbose = false,
|
||||
mode: "profile" | "query" | "full" = "profile",
|
||||
addMemory: "always" | "never" = "never"
|
||||
): LanguageModelV2Middleware => {
|
||||
const logger = createLogger(verbose)
|
||||
|
||||
const SUPERMEMORY_API_KEY = process.env.SUPERMEMORY_API_KEY
|
||||
if (!SUPERMEMORY_API_KEY) {
|
||||
throw new Error("SUPERMEMORY_API_KEY is not set")
|
||||
}
|
||||
|
||||
const client = new Supermemory({
|
||||
apiKey: SUPERMEMORY_API_KEY,
|
||||
})
|
||||
|
||||
return {
|
||||
transformParams: async ({ params }) => {
|
||||
const userMessage = getLastUserMessage(params)
|
||||
|
||||
// Add userMessage to memories based on addMemory setting
|
||||
if (addMemory === "always" && userMessage && userMessage.trim()) {
|
||||
addMemoryTool(client, containerTag, userMessage, logger).catch((error) => {
|
||||
logger.error("Failed to create memories", { error })
|
||||
})
|
||||
}
|
||||
|
||||
if (mode !== "profile") {
|
||||
const lastUserMessage = getLastUserMessage(params)
|
||||
if (!lastUserMessage) {
|
||||
if (!userMessage) {
|
||||
logger.debug("No user message found, skipping memory search")
|
||||
return params
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue