> Discover all available pages from the documentation index: https://mastra.zisheng.pro/ja/llms.txt # Memory クラス `Memory` クラスは、Mastra で会話履歴とスレッドベースのメッセージストレージを管理するための信頼性の高いシステムを提供します。会話の永続化、セマンティック検索、効率的なメッセージ取得が可能です。会話履歴にはストレージ Provider の設定が必要です。セマンティックリコールを有効にする場合は、ベクトルストアと Embedder も指定する必要があります。 ## 使用例 ```typescript import { Memory } from '@mastra/memory' import { Agent } from '@mastra/core/agent' export const agent = new Agent({ id: 'test-agent', name: 'test-agent', instructions: 'You are an agent with memory.', model: 'openai/gpt-5.6-sol', memory: new Memory({ options: { workingMemory: { enabled: true, }, }, }), }) ``` > **注記:** Agent で `workingMemory` を有効にするには、メインの Mastra インスタンスにストレージ Provider を設定する必要があります。詳細は [Mastra クラス](https://mastra.zisheng.pro/ja/reference/core/mastra-class)を参照してください。 ## コンストラクターのパラメーター **storage** (`MastraCompositeStore`): Memory データを永続化するためのストレージ実装。指定しない場合のデフォルトは new DefaultStorage({ config: { url: "file:memory.db" } })。 **vector** (`MastraVector | false`): セマンティック検索機能に使用するベクトルストア。ベクトル操作を無効にするには false を設定します。 **embedder** (`EmbeddingModel | EmbeddingModelV2`): ベクトル埋め込みに使用する Embedder インスタンス。セマンティックリコールを有効にする場合は必須です。 **options** (`MemoryConfig`): Memory の設定オプション。 **options.lastMessages** (`number | false`): コンテキストに含める最新メッセージの数。メッセージ履歴機能全体を無効にするには false を設定します(メッセージはコンテキストに読み込まれず、保存もされません)。制限なくすべてのメッセージを取得するには Number.MAX\_SAFE\_INTEGER を使用します。新しいメッセージを保存せずに読み込むには、readOnly オプションを使用します。 **options.readOnly** (`boolean`): true の場合、新しいメッセージを Memory に保存せず、ワーキングメモリを読み取り専用コンテキストとして提供します(updateWorkingMemory Tool は含まれません)。プレビュー、内部ルーティング Agent、Memory を参照するだけで変更すべきでないサブ Agent などの読み取り専用操作に便利です。 **options.semanticRecall** (`boolean | { topK: number; messageRange: number | { before: number; after: number }; scope?: 'thread' | 'resource' }`): メッセージ履歴のセマンティック検索を有効にします。真偽値または設定オプションを含むオブジェクトを指定できます。有効にするには、ベクトルストアと Embedder の両方を設定する必要があります。topK のデフォルトは 4、messageRange のデフォルトは {before: 1, after: 1} です。 **options.workingMemory** (`WorkingMemory`): ワーキングメモリ機能の設定。{ enabled: boolean; template?: string; schema?: ZodObject\ | JSONSchema7; scope?: 'thread' | 'resource' }、または無効にする場合は { enabled: boolean } を指定できます。 **options.observationalMemory** (`boolean | ObservationalMemoryOptions`): 長いコンテキストを扱う Agent Memory として Observational Memory を有効にします。デフォルト設定を使う場合は true、トークン予算、モデル、スコープをカスタマイズする場合は設定オブジェクトを渡します。設定の詳細は Observational Memory リファレンスを参照してください。 **options.generateTitle** (`boolean | { model: DynamicArgument; instructions?: DynamicArgument }`): 会話トランスクリプトからのスレッドタイトル自動生成を制御します。真偽値、またはカスタムモデルと指示を含むオブジェクトを指定できます。 ## 戻り値 **memory** (`Memory`): 指定した設定を持つ新しい Memory インスタンス。 ## 詳細な使用例 ```typescript import { Memory } from '@mastra/memory' import { Agent } from '@mastra/core/agent' import { LibSQLStore, LibSQLVector } from '@mastra/libsql' export const agent = new Agent({ name: 'test-agent', instructions: 'You are an agent with memory.', model: 'openai/gpt-5.6-sol', memory: new Memory({ storage: new LibSQLStore({ id: 'test-agent-storage', url: 'file:./working-memory.db', }), vector: new LibSQLVector({ id: 'test-agent-vector', url: 'file:./vector-memory.db', }), options: { lastMessages: 10, semanticRecall: { topK: 3, messageRange: 2, scope: 'resource', }, workingMemory: { enabled: true, }, generateTitle: true, }, }), }) ``` ## インデックス設定を使用する PostgreSQL ```typescript import { Memory } from '@mastra/memory' import { Agent } from '@mastra/core/agent' import { ModelRouterEmbeddingModel } from '@mastra/core/llm' import { PgStore, PgVector } from '@mastra/pg' export const agent = new Agent({ name: 'pg-agent', instructions: 'You are an agent with optimized PostgreSQL memory.', model: 'openai/gpt-5.6-sol', memory: new Memory({ storage: new PgStore({ id: 'pg-agent-storage', connectionString: process.env.DATABASE_URL, }), vector: new PgVector({ id: 'pg-agent-vector', connectionString: process.env.DATABASE_URL, }), embedder: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'), options: { lastMessages: 20, semanticRecall: { topK: 5, messageRange: 3, scope: 'resource', indexConfig: { type: 'hnsw', // Use HNSW for better performance metric: 'dotproduct', // Optimal for OpenAI embeddings m: 16, // Number of bi-directional links efConstruction: 64, // Construction-time candidate list size }, }, workingMemory: { enabled: true, }, }, }), }) ``` ### 関連項目 - [Memory 入門](https://mastra.zisheng.pro/ja/docs/memory/overview) - [セマンティックリコール](https://mastra.zisheng.pro/ja/docs/memory/semantic-recall) - [ワーキングメモリ](https://mastra.zisheng.pro/ja/docs/memory/working-memory) - [Observational Memory](https://mastra.zisheng.pro/ja/docs/memory/observational-memory) - [Memory Processor](https://mastra.zisheng.pro/ja/docs/memory/memory-processors) - [createThread](https://mastra.zisheng.pro/ja/reference/memory/createThread) - [recall](https://mastra.zisheng.pro/ja/reference/memory/recall) - [getThreadById](https://mastra.zisheng.pro/ja/reference/memory/getThreadById) - [listThreads](https://mastra.zisheng.pro/ja/reference/memory/listThreads) - [deleteMessages](https://mastra.zisheng.pro/ja/reference/memory/deleteMessages) - [cloneThread](https://mastra.zisheng.pro/ja/reference/memory/cloneThread) - [Clone ユーティリティメソッド](https://mastra.zisheng.pro/ja/reference/memory/clone-utilities)