Skip to main content

@ai-sdk Integration

Walrus Memory includes an AI SDK integration, exported from @mysten-incubation/memwal/ai, for applications that already use model middleware.

withMemWal​

Source: sdk/ai-integration.md
import { generateText } from "ai";
import { withMemWal } from "@mysten-incubation/memwal/ai";
import { openai } from "@ai-sdk/openai";

const model = withMemWal(openai("gpt-4o"), {
key: process.env.MEMWAL_PRIVATE_KEY!,
accountId: process.env.MEMWAL_ACCOUNT_ID!,
serverUrl: process.env.MEMWAL_SERVER_URL,
namespace: "chatbot-prod",
maxMemories: 5,
autoSave: true,
});

const result = await generateText({
model,
messages: [{ role: "user", content: "What do you know about me?" }],
});

What it does​

Before generation:

  • reads the last user message
  • runs recall() against Walrus Memory
  • filters by relevance (capped by the maxMemories option)
  • injects memory context into the prompt

After generation:

  • optionally runs analyze() on the user message to extract facts
  • saves extracted facts asynchronously

Why namespace matters here​

Set a namespace explicitly for each product surface that uses the middleware. Otherwise recalled and auto-saved memories fall back to "default".

When to use direct SDK calls instead​

Use direct SDK methods when your app needs precise control over:

  • when memory is stored
  • which text is analyzed
  • how recall results are displayed or filtered

Stream responses with streamText​

The wrapped model also works with streamText. Pass it as the model the same way you do for generateText. Unlike the generateText example, the streamText call does not use await: the returned result is the stream handle you consume.

const result = streamText({
model,
messages: [{ role: "user", content: "What do you remember about me?" }],
});