> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-HK/llms.txt # MessageHistory `MessageHistory` 是一個 **hybrid processor**,同時負責擷取及持久保存訊息記錄。在輸入時,它會從儲存空間擷取歷史訊息,並加到對話開首。在輸出時,它會將新訊息持久保存至儲存空間。 ## 使用範例 ```typescript import { MessageHistory } from '@mastra/core/processors' const processor = new MessageHistory({ storage: memoryStorage, lastMessages: 50, }) ``` ## 建構函數參數 **options** (`MessageHistoryOptions`): 訊息記錄 Processor 的設定選項 **options.storage** (`MemoryStorage`): 用於擷取及持久保存訊息的儲存空間執行個體 **options.lastMessages** (`number`): 要擷取的歷史訊息數目上限。如未指定,則擷取所有訊息 ## 傳回值 **id** (`string`): Processor 識別符,設定為 'message-history' **name** (`string`): Processor 顯示名稱,設定為 'MessageHistory' **processInput** (`(args: { messages: MastraDBMessage[]; messageList: MessageList; abort: (reason?: string) => never; tracingContext?: TracingContext; requestContext?: RequestContext }) => Promise`): 從儲存空間擷取歷史訊息,並將其加入訊息清單 **processOutputResult** (`(args: { messages: MastraDBMessage[]; messageList: MessageList; abort: (reason?: string) => never; tracingContext?: TracingContext; requestContext?: RequestContext }) => Promise`): 將新訊息(用戶輸入及助理回應)持久保存至儲存空間,系統訊息除外 ## 進階使用範例 ```typescript import { Agent } from '@mastra/core/agent' import { MessageHistory } from '@mastra/core/processors' import { PostgresStorage } from '@mastra/pg' const storage = new PostgresStorage({ connectionString: process.env.DATABASE_URL, }) export const agent = new Agent({ id: 'memory-agent', name: 'memory-agent', instructions: 'You are a helpful assistant with conversation memory', model: 'openai/gpt-5.6-sol', inputProcessors: [ new MessageHistory({ storage, lastMessages: 100, }), ], outputProcessors: [ new MessageHistory({ storage, }), ], }) ``` ## 行為 ### 輸入處理 1. 從請求內容中擷取 `threadId` 2. 從儲存空間擷取歷史訊息(按建立日期降序排列) 3. 篩除系統訊息(它們不應儲存在資料庫中) 4. 將歷史訊息與傳入訊息合併,並按 ID 避免重複 5. 為歷史訊息加入 `source: 'memory'` 標籤 ### 輸出處理 1. 從請求內容中擷取 `threadId` 2. 如記憶設定中已設定 `readOnly`,則略過持久保存 3. 從訊息中篩除未完成的 Tool 呼叫 4. 將新的用戶輸入及助理回應訊息持久保存至儲存空間 5. 更新 Thread 的 `updatedAt` 時間戳記 ## 相關內容 - [Guardrails](https://mastra.zisheng.pro/zh-HK/docs/agents/guardrails)