Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add memory reference guide #1687

Merged
merged 4 commits into from
Feb 4, 2025
Merged
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
1 change: 1 addition & 0 deletions docs/src/pages/docs/reference/_meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const meta = {
core: "Core",
cli: "CLI",
llm: "LLM",
memory: "Memory",
rag: "RAG",
tools: "Tools",
tts: "TTS",
Expand Down
108 changes: 108 additions & 0 deletions docs/src/pages/docs/reference/memory/Memory.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Memory Class Reference

The `Memory` class provides a robust system for managing conversation history and thread-based message storage in Mastra. It enables persistent storage of conversations, semantic search capabilities, and efficient message retrieval.

## Usage Example

```typescript
import { Memory } from "@mastra/memory";
import { PostgresStorage } from "@mastra/storage-pg";

const memory = new Memory({
storage: new PostgresStorage({
connectionString: process.env.DATABASE_URL,
}),
});
```

## Parameters

<PropertiesTable
content={[
{
name: "storage",
type: "MastraStorage",
description: "Storage implementation for persisting memory data",
isOptional: false,
},
{
name: "vector",
type: "MastraVector",
description: "Vector store for semantic search capabilities",
isOptional: true,
},
{
name: "embeddingOptions",
type: "EmbeddingOptions",
description: "Configuration for text embeddings",
isOptional: true,
},
{
name: "options",
type: "MemoryConfig",
description: "General memory configuration options",
isOptional: true,
},
]}
/>

### options

<PropertiesTable
content={[
{
name: "lastMessages",
type: "number | false",
description: "Number of most recent messages to retrieve. Set to false to disable.",
isOptional: true,
defaultValue: "40",
},
{
name: "historySearch",
type: "boolean | object",
description: "Enable semantic search in message history. Automatically enabled when vector store is provided.",
isOptional: true,
defaultValue: "false (true if vector store provided)",
},
{
name: "topK",
type: "number",
description: "Number of similar messages to retrieve when using semantic search",
isOptional: true,
defaultValue: "2",
},
{
name: "messageRange",
type: "number | { before: number; after: number }",
description: "Range of messages to include around semantic search results",
isOptional: true,
defaultValue: "{ before: 2, after: 2 }",
},
]}
/>

## Additional Notes

### Vector Search Configuration

When using vector search capabilities, ensure you configure both the vector store and appropriate search options:

```typescript
const memory = new Memory({
storage: new PostgresStorage({ /* config */ }),
vector: new PineconeVector({ /* config */ }),
options: {
historySearch: {
topK: 5,
messageRange: { before: 2, after: 2 }
}
}
});
```

### Related

- [createThread](/reference/memory/createThread.mdx)
- [getMessages](/reference/memory/getMessages.mdx)
- [addMessage](/reference/memory/addMessage.mdx)
- [rememberMessages](/reference/memory/rememberMessages.mdx)
9 changes: 9 additions & 0 deletions docs/src/pages/docs/reference/memory/_meta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const meta = {
Memory: "Memory Class",
createThread: ".createThread()",
getMessages: ".getMessages()",
addMessage: ".addMessage()",
rememberMessages: ".rememberMessages()"
};

export default meta;
129 changes: 129 additions & 0 deletions docs/src/pages/docs/reference/memory/addMessage.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# addMessage

Adds a new message to an existing thread. Supports different message types including text messages and tool-related messages.

## Usage Example

```typescript
import { Memory } from "@mastra/memory";

const memory = new Memory({ /* config */ });

// Add a simple text message
await memory.addMessage({
threadId: "thread-123",
content: "Hello, how can I help you?",
role: "assistant",
type: "text"
});

// Add a tool call message
await memory.addMessage({
threadId: "thread-123",
content: "Searching for weather data...",
role: "assistant",
type: "tool-call",
toolNames: ["weatherAPI"],
toolCallArgs: [{ location: "San Francisco" }],
toolCallIds: ["call-123"]
});
```

## Parameters

<PropertiesTable
content={[
{
name: "threadId",
type: "string",
description: "The ID of the thread to add the message to",
isOptional: false,
},
{
name: "content",
type: "UserContent | AssistantContent",
description: "The content of the message",
isOptional: false,
},
{
name: "role",
type: "'user' | 'assistant'",
description: "The role of the message sender",
isOptional: false,
},
{
name: "type",
type: "'text' | 'tool-call' | 'tool-result'",
description: "The type of message being added",
isOptional: false,
},
{
name: "toolNames",
type: "string[]",
description: "Names of tools being called (for tool-call type)",
isOptional: true,
},
{
name: "toolCallArgs",
type: "Record<string, unknown>[]",
description: "Arguments for tool calls",
isOptional: true,
},
{
name: "toolCallIds",
type: "string[]",
description: "IDs of tool calls",
isOptional: true,
},
{
name: "config",
type: "MemoryConfig",
description: "Additional configuration for message handling",
isOptional: true,
},
]}
/>

## Returns

<PropertiesTable
content={[
{
name: "id",
type: "string",
description: "The ID of the created message",
},
{
name: "threadId",
type: "string",
description: "The ID of the thread the message was added to",
},
{
name: "content",
type: "UserContent | AssistantContent | ToolContent",
description: "The content of the message",
},
{
name: "role",
type: "'user' | 'assistant' | 'tool'",
description: "The role of the message sender",
},
{
name: "createdAt",
type: "Date",
description: "Timestamp when the message was created",
},
]}
/>

## Additional Notes

When adding tool-related messages, make sure to:
1. Set appropriate `type` ('tool-call' or 'tool-result')
2. Include relevant tool metadata (names, args, IDs)
3. Maintain consistency between tool calls and their results

### Related

- [Memory](/reference/memory/Memory.mdx)
- [getMessages](/reference/memory/getMessages.mdx)
93 changes: 93 additions & 0 deletions docs/src/pages/docs/reference/memory/createThread.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# createThread

Creates a new conversation thread in the memory system. Each thread represents a distinct conversation or context and can contain multiple messages.

## Usage Example

```typescript
import { Memory } from "@mastra/memory";

const memory = new Memory({ /* config */ });
const thread = await memory.createThread({
resourceId: "user-123",
title: "Support Conversation",
metadata: {
category: "support",
priority: "high"
}
});
```

## Parameters

<PropertiesTable
content={[
{
name: "resourceId",
type: "string",
description: "Identifier for the resource this thread belongs to (e.g., user ID, project ID)",
isOptional: false,
},
{
name: "threadId",
type: "string",
description: "Optional custom ID for the thread. If not provided, one will be generated.",
isOptional: true,
},
{
name: "title",
type: "string",
description: "Optional title for the thread",
isOptional: true,
},
{
name: "metadata",
type: "Record<string, unknown>",
description: "Optional metadata to associate with the thread",
isOptional: true,
},
]}
/>

## Returns

<PropertiesTable
content={[
{
name: "id",
type: "string",
description: "Unique identifier of the created thread",
},
{
name: "resourceId",
type: "string",
description: "Resource ID associated with the thread",
},
{
name: "title",
type: "string",
description: "Title of the thread (if provided)",
},
{
name: "createdAt",
type: "Date",
description: "Timestamp when the thread was created",
},
{
name: "updatedAt",
type: "Date",
description: "Timestamp when the thread was last updated",
},
{
name: "metadata",
type: "Record<string, unknown>",
description: "Additional metadata associated with the thread",
},
]}
/>

### Related

- [Memory](/reference/memory/Memory.mdx)
- [getThreadById](/reference/memory/getThreadById.mdx)
- [getThreadsByResourceId](/reference/memory/getThreadsByResourceId.mdx)
Loading
Loading