Are you an LLM? Read llms.txt for a summary of the docs, or llms-full.txt for the full context.
Skip to content
Framework integrations

Vercel AI SDK Integration

Middleware that wraps any AI SDK model with automatic memory injection and interaction capture.

@mubit-ai/ai-sdk provides mubitMemoryMiddleware, a middleware factory you pass to Vercel AI SDK's wrapLanguageModel(). The wrapped model auto-injects relevant lessons before each call, captures the interaction, and reflects on session end.

npm install @mubit-ai/ai-sdk

Minimal usage

vercel_ai_sdk_minimal.mjs
import { wrapLanguageModel, generateText } from "ai";
import { openai } from "@ai-sdk/openai";
import { mubitMemoryMiddleware } from "@mubit-ai/ai-sdk";
 
const model = wrapLanguageModel({
  model: openai("gpt-4o"),
  middleware: mubitMemoryMiddleware({
    apiKey: process.env.MUBIT_API_KEY,
    sessionId: "session-1",
    agentId: "support-agent",
  }),
});
 
const { text } = await generateText({
  model,
  prompt: "How do I reset my password?",
});
// Lessons auto-injected before call; interaction auto-captured after.

Full example: FAQ bot with cross-session learning

A multi-session FAQ bot with a knowledge-base tool. Session 2 benefits from a lesson captured in Session 1.

faq_bot/index.mjs
import { generateText, tool, wrapLanguageModel } from "ai";
import { openai } from "@ai-sdk/openai";
import { z } from "zod";
import { mubitMemoryMiddleware, MubitClient } from "@mubit-ai/ai-sdk";
 
const mubitClient = new MubitClient({ apiKey: process.env.MUBIT_API_KEY });
 
const lookupKnowledgeBase = tool({
  description: "Search the knowledge base for help articles",
  parameters: z.object({ query: z.string() }),
  execute: async ({ query }) => findBestArticle(query),
});
 
// Session 1: answer a couple of FAQ questions
const model1 = wrapLanguageModel({
  model: openai("gpt-4o-mini"),
  middleware: mubitMemoryMiddleware({
    sessionId: "faq-session-1",
    agentId: "faq-bot",
    mubitClient,
  }),
});
 
for (const question of [
  "How do I reset my password?",
  "What are system requirements?",
]) {
  const { text } = await generateText({
    model: model1,
    tools: { lookupKnowledgeBase },
    maxSteps: 3,
    prompt: question,
  });
  console.log(`Q: ${question}\nA: ${text}`);
}
 
// Ingest a lesson between sessions
await mubitClient.control.ingest({
  run_id: "faq-session-1",
  items: [
    {
      item_id: "faq-2fa-cross-sell",
      content_type: "text/plain",
      text: "Users who ask about password reset often also need 2FA help.",
      intent: "lesson",
    },
  ],
});
 
// Session 2: new session benefits from memory
const model2 = wrapLanguageModel({
  model: openai("gpt-4o-mini"),
  middleware: mubitMemoryMiddleware({
    sessionId: "faq-session-2",
    agentId: "faq-bot",
    mubitClient,
  }),
});
 
const { text } = await generateText({
  model: model2,
  tools: { lookupKnowledgeBase },
  maxSteps: 3,
  prompt: "I'm having trouble logging in",
});
// Response references password / 2FA from Session 1.

Extended MuBit features

The middleware focuses on inject + capture. The full MAS surface is on the MubitClient you pass in (or that the middleware constructs internally — pass your own to share it across middlewares):

await mubitClient.registerAgent({ session_id, agent_id, role, read_scopes, write_scopes });
await mubitClient.handoff({ session_id, task_id, from_agent_id, to_agent_id, content, requested_action });
await mubitClient.feedback({ session_id, handoff_id, verdict, from_agent_id });
await mubitClient.checkpoint({ session_id, context_snapshot, label });
await mubitClient.recordOutcome({ session_id, reference_id, outcome, rationale });
await mubitClient.reflect({ session_id });
await mubitClient.surfaceStrategies({ session_id, lesson_types, max_strategies });
await mubitClient.diagnose({ session_id, error_text });
await mubitClient.archive({ session_id, content, artifact_kind });
await mubitClient.dereference({ session_id, reference_id });
await mubitClient.getContext({ session_id, query, mode, max_token_budget });

Gotchas

  • maxSteps matters: tool-calling roundtrips count as separate model calls. The middleware injects lessons for each.
  • Streaming responses (streamText) are supported; the assembled output is captured on stream completion.
  • Reflection on session end is opt-in. Pass autoReflect: true to the middleware, or call mubitClient.reflect(...) yourself when the session ends.
  • No CommonJS: @mubit-ai/ai-sdk ships ESM-only.

Version compatibility

@mubit-ai/ai-sdkmubit-sdk (Node)ai
0.5.x>= 0.6.0>= 4.0.0