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

Memory examples and docs improvements #1688

Merged
merged 25 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
update reference docs
  • Loading branch information
tyler barnes authored and tyler barnes committed Feb 4, 2025
commit 04ec775df09cc2b5c2ccc6b55f42e2af99b809fe
43 changes: 43 additions & 0 deletions docs/src/pages/docs/reference/memory/getThreadById.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# getThreadById Reference

The `getThreadById` function retrieves a specific thread by its ID from storage.

## Usage Example

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

const memory = new Memory(config);

const thread = await memory.getThreadById({ threadId: "thread-123" });
```

## Parameters

<PropertiesTable
content={[
{
name: "threadId",
type: "string",
description: "The ID of the thread to be retrieved.",
isOptional: false,
},
]}
/>

## Returns

<PropertiesTable
content={[
{
name: "StorageThreadType | null",
type: "Promise",
description:
"A promise that resolves to the thread associated with the given ID, or null if not found.",
},
]}
/>

### Related

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

The `getThreadsByResourceId` function retrieves all threads associated with a specific resource ID from storage.

## Usage Example

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

const memory = new Memory(config);

const threads = await memory.getThreadsByResourceId({
resourceId: "resource-123",
});
```

## Parameters

<PropertiesTable
content={[
{
name: "resourceId",
type: "string",
description: "The ID of the resource whose threads are to be retrieved.",
isOptional: false,
},
]}
/>

## Returns

<PropertiesTable
content={[
{
name: "StorageThreadType[]",
type: "Promise",
description:
"A promise that resolves to an array of threads associated with the given resource ID.",
},
]}
/>

### Related

- [Memory](/docs/reference/memory/Memory.mdx)
45 changes: 25 additions & 20 deletions docs/src/pages/docs/reference/memory/query.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,44 @@ Retrieves messages from a specific thread, with support for pagination and filte
```typescript
import { Memory } from "@mastra/memory";

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

// Get last 50 messages
const { messages, uiMessages } = await memory.query({
threadId: "thread-123",
selectBy: {
last: 50
}
last: 50,
},
});

// Get messages with context around specific messages
const { messages: contextMessages } = await memory.query({
threadId: "thread-123",
selectBy: {
include: [
{
id: "msg-123" // Get just this message (no context)
{
id: "msg-123", // Get just this message (no context)
},
{
id: "msg-456", // Get this message with custom context
withPreviousMessages: 3, // 3 messages before
withNextMessages: 1 // 1 message after
}
id: "msg-456", // Get this message with custom context
withPreviousMessages: 3, // 3 messages before
withNextMessages: 1, // 1 message after
},
],
vectorSearchString: "What did we discuss about architecture?" // Messages will have default context of 2 before and 2 after
}
},
});

// Semantic search in messages
const { messages: searchResults } = await memory.query({
const { messages } = await memory.query({
threadId: "thread-123",
selectBy: {
vectorSearchString: "What was discussed about deployment?"
vectorSearchString: "What was discussed about deployment?",
},
threadConfig: {
historySearch: true
}
historySearch: true,
},
});
```

Expand All @@ -54,7 +55,8 @@ const { messages: searchResults } = await memory.query({
{
name: "threadId",
type: "string",
description: "The unique identifier of the thread to retrieve messages from",
description:
"The unique identifier of the thread to retrieve messages from",
isOptional: false,
},
{
Expand Down Expand Up @@ -85,7 +87,8 @@ const { messages: searchResults } = await memory.query({
{
name: "last",
type: "number | false",
description: "Number of most recent messages to retrieve. Set to false to disable limit. Note: threadConfig.lastMessages (default: 40) will override this if smaller.",
description:
"Number of most recent messages to retrieve. Set to false to disable limit. Note: threadConfig.lastMessages (default: 40) will override this if smaller.",
isOptional: true,
defaultValue: "40",
},
Expand All @@ -111,13 +114,15 @@ const { messages: searchResults } = await memory.query({
{
name: "withPreviousMessages",
type: "number",
description: "Number of messages to include before this message. Defaults to 2 when using vector search, 0 otherwise.",
description:
"Number of messages to include before this message. Defaults to 2 when using vector search, 0 otherwise.",
isOptional: true,
},
{
name: "withNextMessages",
type: "number",
description: "Number of messages to include after this message. Defaults to 2 when using vector search, 0 otherwise.",
description:
"Number of messages to include after this message. Defaults to 2 when using vector search, 0 otherwise.",
isOptional: true,
},
]}
Expand All @@ -143,10 +148,10 @@ const { messages: searchResults } = await memory.query({
## Additional Notes

The `query` function returns two different message formats:

- `messages`: Core message format used internally
- `uiMessages`: Formatted messages suitable for UI display, including proper threading of tool calls and results

### Related

- [Memory](/docs/reference/memory/Memory.mdx)
- [addMessage](/docs/reference/memory/addMessage.mdx)