mirror of
https://github.com/supermemoryai/supermemory.git
synced 2026-10-12 03:48:08 +00:00
Rewrites 339 TypeScript calls across 50 pages from the rc.5 `method({ namespace, body })` form to the shipped `method(namespace, { ... })` form, and aligns field names with the live v5 spec: `attach` to `include`, `authUrl` to `authorization`, `lastSync` to `latestRun`, `deletedCount` to `count`, and the paginated `namespaces.list()`.
Renames container tags to namespaces across concepts, connectors, integrations and snippets. The namespace pages keep container tag in the description, search keywords and a rename note so old searches still land, and the v3 reference page points at v5.
The migration guide's SDK table now covers both 5.0.0 SDKs, and the SDK integration page uses the real client options (`baseUrl`, `timeoutInSeconds`, `maxRetries`) and error classes.
204 lines
5.4 KiB
Text
204 lines
5.4 KiB
Text
---
|
|
title: "Convex"
|
|
sidebarTitle: "Convex"
|
|
description: "Add persistent memory to Convex apps with Supermemory"
|
|
icon: "/icons/hugeicons/database-01.svg"
|
|
---
|
|
|
|
Convex apps don't have built-in memory for AI. Supermemory fixes that. You get a memory layer that stores conversations, builds user profiles, and gives your AI context about who it's talking to.
|
|
|
|
## What you can do
|
|
|
|
- Store user interactions and retrieve them in future sessions
|
|
- Build automatic user profiles from conversations
|
|
- Search memories to give your AI relevant context
|
|
- Keep everything in your Convex database for full visibility
|
|
|
|
## Setup
|
|
|
|
Install the packages:
|
|
|
|
```bash
|
|
npm install supermemory convex
|
|
```
|
|
|
|
For the AI chat example, also install the AI SDK packages:
|
|
|
|
```bash
|
|
npm install @supermemory/tools @ai-sdk/openai ai
|
|
```
|
|
|
|
Set up your environment variable in Convex:
|
|
|
|
```bash
|
|
npx convex env set SUPERMEMORY_API_KEY your-supermemory-api-key
|
|
```
|
|
|
|
<Note>Get your Supermemory API key from [console.supermemory.ai](https://console.supermemory.ai).</Note>
|
|
|
|
## Basic integration
|
|
|
|
Create simple helper functions for each Supermemory operation:
|
|
|
|
```typescript
|
|
// convex/memory.ts
|
|
import { action } from "./_generated/server";
|
|
import { v } from "convex/values";
|
|
import { Supermemory } from "supermemory";
|
|
|
|
const memory = new Supermemory({ apiKey: process.env.SUPERMEMORY_API_KEY });
|
|
|
|
// Get user profile
|
|
export const getProfile = action({
|
|
args: { userId: v.string() },
|
|
handler: async (ctx, { userId }) => {
|
|
return await memory.profile(userId);
|
|
},
|
|
});
|
|
|
|
// Add a memory
|
|
export const addMemory = action({
|
|
args: { userId: v.string(), content: v.string() },
|
|
handler: async (ctx, { userId, content }) => {
|
|
return await memory.add(userId, {
|
|
content,
|
|
dreaming: "instant", // memories are ready as soon as status is done
|
|
});
|
|
},
|
|
});
|
|
|
|
// Search memories
|
|
export const searchMemories = action({
|
|
args: { userId: v.string(), query: v.string(), limit: v.optional(v.number()) },
|
|
handler: async (ctx, { userId, query, limit }) => {
|
|
return await memory.search(userId, {
|
|
query,
|
|
searchMode: "hybrid",
|
|
limit: limit ?? 10,
|
|
});
|
|
},
|
|
});
|
|
```
|
|
|
|
---
|
|
|
|
## Example: AI chat with memory
|
|
|
|
A chat endpoint using the Supermemory AI SDK middleware. It automatically injects context and saves memories.
|
|
|
|
```typescript
|
|
// convex/chat.ts
|
|
import { action } from "./_generated/server";
|
|
import { v } from "convex/values";
|
|
import { generateText } from "ai";
|
|
import { openai } from "@ai-sdk/openai";
|
|
import { withSupermemory } from "@supermemory/tools/ai-sdk";
|
|
|
|
export const chat = action({
|
|
args: { userId: v.string(), message: v.string() },
|
|
handler: async (ctx, { userId, message }) => {
|
|
// Wrap the model - automatically injects context and saves memories
|
|
const model = withSupermemory(openai("gpt-4o-mini"), {
|
|
namespace: userId,
|
|
id: `convex-chat-${userId}`,
|
|
mode: "full",
|
|
addMemory: "always",
|
|
});
|
|
|
|
const { text } = await generateText({
|
|
model,
|
|
system: "You are a helpful assistant.",
|
|
prompt: message,
|
|
});
|
|
|
|
return text;
|
|
},
|
|
});
|
|
```
|
|
|
|
---
|
|
|
|
## Storing memories in Convex tables
|
|
|
|
Keep a local copy of memories in your Convex database for full visibility:
|
|
|
|
```typescript
|
|
// convex/schema.ts
|
|
import { defineSchema, defineTable } from "convex/server";
|
|
import { v } from "convex/values";
|
|
|
|
export default defineSchema({
|
|
memories: defineTable({
|
|
userId: v.string(),
|
|
content: v.string(),
|
|
createdAt: v.number(),
|
|
}).index("by_user", ["userId"]),
|
|
});
|
|
```
|
|
|
|
```typescript
|
|
// convex/memory.ts
|
|
import { action, mutation, query } from "./_generated/server";
|
|
import { api } from "./_generated/api";
|
|
import { v } from "convex/values";
|
|
import { Supermemory } from "supermemory";
|
|
|
|
const memory = new Supermemory({ apiKey: process.env.SUPERMEMORY_API_KEY });
|
|
|
|
// Store in Convex
|
|
export const storeMemory = mutation({
|
|
args: { userId: v.string(), content: v.string() },
|
|
handler: async (ctx, { userId, content }) => {
|
|
return await ctx.db.insert("memories", {
|
|
userId,
|
|
content,
|
|
createdAt: Date.now(),
|
|
});
|
|
},
|
|
});
|
|
|
|
// Add memory to both Supermemory and Convex
|
|
export const addMemory = action({
|
|
args: { userId: v.string(), content: v.string() },
|
|
handler: async (ctx, { userId, content }) => {
|
|
// Add to Supermemory
|
|
await memory.add(userId, { content });
|
|
|
|
// Store in Convex
|
|
// Note: in production, handle partial failures — if the Convex mutation
|
|
// fails after the Supermemory write succeeds, the two stores will be out of sync.
|
|
await ctx.runMutation(api.memory.storeMemory, { userId, content });
|
|
},
|
|
});
|
|
|
|
// List memories from Convex
|
|
export const listMemories = query({
|
|
args: { userId: v.string() },
|
|
handler: async (ctx, { userId }) => {
|
|
return await ctx.db
|
|
.query("memories")
|
|
.withIndex("by_user", q => q.eq("userId", userId))
|
|
.order("desc")
|
|
.take(50);
|
|
},
|
|
});
|
|
```
|
|
|
|
---
|
|
|
|
## Related docs
|
|
|
|
<CardGroup cols={2}>
|
|
<Card title="User profiles" icon="/icons/hugeicons/user.svg" href="/recall/user-profiles">
|
|
How automatic profiling works
|
|
</Card>
|
|
<Card title="Search" icon="/icons/hugeicons/search-01.svg" href="/recall/search">
|
|
Filtering and search modes
|
|
</Card>
|
|
<Card title="Vercel AI SDK" icon="/icons/hugeicons/triangle.svg" href="/integrations/ai-sdk">
|
|
Memory middleware for Next.js
|
|
</Card>
|
|
<Card title="LangChain" icon="/icons/hugeicons/link-01.svg" href="/integrations/langchain">
|
|
Memory for LangChain apps
|
|
</Card>
|
|
</CardGroup>
|