Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions packages/tools/src/vercel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import type { PromptTemplate, MemoryPromptData } from "./memory-prompt"
const DEFAULT_MEMORY_RETRIEVAL_TIMEOUT_MS = 5000

interface WrapVercelLanguageModelOptions {
/** The container tag/identifier for memory search (e.g., user ID, project ID) */
containerTag: string
/** Optional conversation ID to group messages for contextual memory generation */
conversationId?: string
/** Enable detailed logging of memory search and injection */
Expand Down Expand Up @@ -73,8 +75,8 @@ interface WrapVercelLanguageModelOptions {
* detection of `model.specificationVersion`.
*
* @param model - The language model to wrap with supermemory capabilities (V2 or V3)
* @param containerTag - The container tag/identifier for memory search (e.g., user ID, project ID)
* @param options - Optional configuration options for the middleware
* @param options - Configuration options for Supermemory integration
* @param options.containerTag - Required. The container tag/identifier for memory search (e.g., user ID, project ID)
* @param options.conversationId - Optional conversation ID to group messages into a single document for contextual memory generation
* @param options.verbose - Optional flag to enable detailed logging of memory search and injection process (default: false)
* @param options.mode - Optional mode for memory search: "profile", "query", or "full" (default: "profile")
Expand All @@ -90,7 +92,8 @@ interface WrapVercelLanguageModelOptions {
* import { withSupermemory } from "@supermemory/tools/ai-sdk"
* import { openai } from "@ai-sdk/openai"
*
* const modelWithMemory = withSupermemory(openai("gpt-4"), "user-123", {
* const modelWithMemory = withSupermemory(openai("gpt-4"), {
* containerTag: "user-123",
* conversationId: "conversation-456",
* mode: "full",
* addMemory: "always"
Expand All @@ -107,10 +110,9 @@ interface WrapVercelLanguageModelOptions {
*/
const wrapVercelLanguageModel = <T extends LanguageModel>(
model: T,
containerTag: string,
options?: WrapVercelLanguageModelOptions,
options: WrapVercelLanguageModelOptions,
): T => {
const providedApiKey = options?.apiKey ?? process.env.SUPERMEMORY_API_KEY
const providedApiKey = options.apiKey ?? process.env.SUPERMEMORY_API_KEY

if (!providedApiKey) {
throw new Error(
Expand All @@ -119,18 +121,18 @@ const wrapVercelLanguageModel = <T extends LanguageModel>(
}

const ctx = createSupermemoryContext({
containerTag,
containerTag: options.containerTag,
apiKey: providedApiKey,
conversationId: options?.conversationId,
verbose: options?.verbose ?? false,
mode: options?.mode ?? "profile",
addMemory: options?.addMemory ?? "never",
baseUrl: options?.baseUrl,
promptTemplate: options?.promptTemplate,
conversationId: options.conversationId,
verbose: options.verbose ?? false,
mode: options.mode ?? "profile",
addMemory: options.addMemory ?? "never",
baseUrl: options.baseUrl,
promptTemplate: options.promptTemplate,
memoryRetrievalTimeoutMs: DEFAULT_MEMORY_RETRIEVAL_TIMEOUT_MS,
})

const skipMemoryOnError = options?.skipMemoryOnError ?? true
const skipMemoryOnError = options.skipMemoryOnError ?? true

// Proxy keeps prototype/getter fields (e.g. provider, modelId) that `{ ...model }` drops.
return new Proxy(model, {
Expand Down
3 changes: 2 additions & 1 deletion packages/tools/test/ai-sdk-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { generateText } from "ai"
import { withSupermemory } from "../src/ai-sdk"
import { openai } from "@ai-sdk/openai"

const modelWithMemory = withSupermemory(openai("gpt-5"), "user_id_life", {
const modelWithMemory = withSupermemory(openai("gpt-5"), {
containerTag: "user_id_life",
verbose: true,
mode: "query", // options are profile, query, full (default is profile)
addMemory: "always", // options are always, never (default is never)
Expand Down
3 changes: 2 additions & 1 deletion packages/tools/test/chatapp/app/api/chat/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { gateway, streamText, type ModelMessage } from "ai"
import { withSupermemory } from "@supermemory/tools/ai-sdk"

const model = withSupermemory(gateway("google/gemini-2.5-flash"), "user-1", {
const model = withSupermemory(gateway("google/gemini-2.5-flash"), {
containerTag: "user-1",
apiKey: process.env.SUPERMEMORY_API_KEY ?? "",
mode: "full",
addMemory: "always",
Expand Down
7 changes: 2 additions & 5 deletions packages/tools/test/chatapp/app/api/stream/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const SUPERMEMORY_USER_ID = "user-1"
const gatewayModel = gateway("google/gemini-2.5-flash")

const supermemoryOptions = {
containerTag: SUPERMEMORY_USER_ID,
apiKey: process.env.SUPERMEMORY_API_KEY ?? "",
mode: "full" as const,
addMemory: "always" as const,
Expand All @@ -31,11 +32,7 @@ export async function POST(req: Request) {
})
: gatewayModel

const model = withSupermemory(
innerModel,
SUPERMEMORY_USER_ID,
supermemoryOptions,
)
const model = withSupermemory(innerModel, supermemoryOptions)

const result = streamText({
model,
Expand Down
Loading
Loading