UNPKG

@mastra/core

Version:

Mastra is a framework for building AI-powered applications and agents with a modern TypeScript stack.

63 lines (40 loc) 2.29 kB
# withMastra() Wraps an AI SDK model with Mastra processors and/or memory. ## Usage example ```typescript import { openai } from '@ai-sdk/openai' import { generateText } from 'ai' import { withMastra } from '@mastra/ai-sdk' import type { Processor } from '@mastra/core/processors' const loggingProcessor: Processor<'logger'> = { id: 'logger', async processInput({ messages }) { console.log('Input:', messages.length, 'messages') return messages }, } const model = withMastra(openai('gpt-5.4'), { inputProcessors: [loggingProcessor], }) const { text } = await generateText({ model, prompt: 'What is 2 + 2?', }) ``` ## Parameters **model** (`LanguageModelV2 | LanguageModelV3`): Any AI SDK v5 or v6 language model (e.g., \`openai('gpt-5.4')\`, \`anthropic('claude-opus-4-6')\`). **options** (`WithMastraOptions`): Configuration object for processors and memory. **options.inputProcessors** (`InputProcessor[]`): Input processors to run before the LLM call. **options.outputProcessors** (`OutputProcessor[]`): Output processors to run on the LLM response. **options.memory** (`WithMastraMemoryOptions`): Memory configuration - enables automatic message history persistence. **options.memory.storage** (`MemoryStorage`): Memory storage domain for message persistence. Get it from a composite store using \`await storage.getStore('memory')\`. **options.memory.threadId** (`string`): Thread ID for conversation persistence. **options.memory.resourceId** (`string`): Resource ID (user/session identifier). **options.memory.lastMessages** (`number | false`): Number of recent messages to retrieve, or false to disable. **options.memory.semanticRecall** (`WithMastraSemanticRecallOptions`): Semantic recall configuration (RAG-based memory retrieval). **options.memory.workingMemory** (`MemoryConfig['workingMemory']`): Working memory configuration (persistent user data). **options.memory.readOnly** (`boolean`): Read-only mode - prevents saving new messages. ## Returns A wrapped model compatible with `generateText`, `streamText`, `generateObject`, and `streamObject`. ## Streaming behavior Output processors that implement `processOutputResult` run after the stream finishes. Consume the stream to completion to persist message history and semantic recall.