> Discover all available pages from the documentation index: https://mastra.zisheng.pro/llms.txt # Memory File-based Agent 从默认导出 [`Memory`](https://mastra.zisheng.pro/reference/memory/memory-class) 实例的 `memory.ts` 文件获取 [memory](https://mastra.zisheng.pro/docs/memory/overview)。本页介绍基于文件的约定;有关消息历史、语义召回、storage 和处理器,请参阅 memory 文档。 如果没有 `memory.ts` 或 `config.memory`,Agent 默认没有 memory。除非你自行传入先前的上下文,否则每次 `generate()` 或 `stream()` 调用开始时都不会记住对话状态。 ## 快速开始 在 Agent 的 `config.ts` 旁创建 `memory.ts`: ```typescript import { Memory } from '@mastra/memory' export default new Memory() ``` 导出的实例将成为 Agent 的 `memory`。如果你的应用在主 Mastra 实例上配置了 storage Provider,memory 数据会存储在那里。有关更多信息,请参阅 [storage](https://mastra.zisheng.pro/docs/storage/overview)。 调用 Agent 时使用相同的 `resource` 和 `thread` 值,即可继续对话: ```typescript const response = await weatherAgent.generate('Remember that I prefer Celsius.', { memory: { resource: 'user-123', thread: 'weather-chat', }, }) ``` ## 配置 memory 如果默认行为无法满足需求,请向 `new Memory()` 传入选项。 ```typescript import { Memory } from '@mastra/memory' export default new Memory({ options: { lastMessages: 20, workingMemory: { enabled: true, scope: 'resource', }, }, }) ``` 有关构造函数选项,请查看 [`Memory` 参考](https://mastra.zisheng.pro/reference/memory/memory-class)。以下页面介绍相关的 memory 功能: - [Storage](https://mastra.zisheng.pro/docs/storage/overview):配置 memory 数据的持久化。 - [语义召回](https://mastra.zisheng.pro/docs/memory/semantic-recall):按语义检索相关的过往消息。 - [Memory 处理器](https://mastra.zisheng.pro/docs/memory/memory-processors):在 memory 将消息添加到模型上下文之前,对消息进行筛选、裁剪或转换。 ## 与 config 的优先级 `config.memory` 优先于 `memory.ts`。如果两者都不存在,组装后的 File-based Agent 将没有 memory。有关完整的合并表,请参阅 [`config.ts` 优先级](https://mastra.zisheng.pro/reference/file-based-agents/config)。