> For the complete documentation index, see [llms.txt](https://docs.convai.com/api-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/long-term-memory/how-long-term-memory-works.md).

# 长期记忆的工作原理

长期记忆（LTM）为 Convai 角色提供一个持久的、按用户划分的知识存储，可跨独立会话保留。一名安全培训员记得一位学员上周通过了模块 3。一位企业入职助手知道某位员工已经查看了哪些政策。一份医疗培训指南记得某位用户更喜欢详细解释而不是摘要。本页解释其底层机制——LTM 存储什么、它如何按用户划分记忆，以及事实何时进出会话。

### 会话生命周期

每个 LTM 会话都会经历四个阶段：

```mermaid
sequenceDiagram
    participant App as 你的应用程序
    participant SDK as Convai Unity SDK
    participant Convai as Convai

    App->>SDK: SetEndUserIdentityProvider(provider)
    App->>SDK: ConnectAsync()
    SDK->>Convai: connect(end_user_id, character_id)
    Convai-->>SDK: 将已存储的记忆注入上下文
    注释 SDK,Convai：会话在记忆上下文中继续进行
    SDK->>Convai: 会话结束
    Convai-->>Convai: 提取并存储新事实
```

**连接：** SDK 会发送 `end_user_id` 和 `character_id` 到 Convai，在会话开始时。Convai 会将这对标识解析为已存储的记忆集合，并在生成第一条回复前把任何现有事实注入角色的上下文中。

**对话：** 角色会在知晓注入事实的情况下作出回应——它不需要被告知用户的姓名、角色或历史，因为这些上下文已经存在。对话中分享的新信息不会实时存储；它会保留在会话缓冲区中。

**会话结束：** 当会话关闭时，Convai 会处理对话转录并提取有意义的事实（姓名、角色、偏好、已完成的任务、陈述的历史）作为自然语言 `MemoryRecord` 条目。这些内容存储在 `end_user_id + character_id` 键下。

**下次连接：** 在下一次会话中，这些新事实会可用，并与此前存储的内容一起注入。

### 记忆作用域

每条记忆记录都仅作用于一个用户–角色对。

```
end_user_id  +  character_id  →  一组 MemoryRecord 条目
```

为用户存储的事实 `一个` 在角色 `X` 对用户始终不可见 `B` 也不会对角色 `Y`可见。如果你的应用使用多个角色，那么每个角色都会为每个用户构建自己独立的记忆集合。不存在跨角色的记忆共享。

该 `end_user_id` 是由你的 `IEndUserIdentityProvider` （或者由默认的 `DeviceEndUserIdProvider`）提供的稳定字符串标识符。如果该标识符在会话之间发生变化——例如，因为 `PlayerPrefs` 已被清除——Convai 会将新值视为新用户。之前标识符下的记忆不会继承过来。

### 记忆记录

每条已存储的事实都是一个 `MemoryRecord` ，带有自然语言 `Memory` 字符串和自动分配的 `Id`.

示例 `Memory` 值：

* `“用户的名字是 Jordan。”`
* `“Jordan 于 2025-03-12 完成了受限空间进入认证。”`
* `“Jordan 更喜欢逐步解释而不是摘要。”`

Convai 会以第三人称写入这些事实。角色将它们作为背景上下文，而不是逐字背诵的脚本。

### 去重

当新会话产生的事实在语义上与现有记忆等同时，Convai 会更新现有的 `MemoryRecord` ，而不是创建重复项。例如，如果用户将自己的名字从 `“Jordan”` 到 `“Jordan Kim”`改为，现有的姓名记录会就地更新。 `事件` 字段在 `MemoryAddResult` 返回 `“update”` 用于去重写入，以及 `“add”` 用于新记录。

### LTM 不做什么

理解 LTM 的局限可以避免错误假设：

* **LTM 不支持实时查找。** 记忆注入发生在连接时，而不是在对话过程中按需进行。角色无法在会话中途查询记忆存储。
* **LTM 不保证每个说出的细节都会被存储。** Convai 会提取其判断为有意义且持久的事实。简短的偶然性陈述可能不会被保留。
* **LTM 不会在角色之间共享记忆。** 每个角色都针对每个用户拥有独立的记忆集合。
* **LTM 默认未启用。** 记忆处于禁用状态（`MemorySettings.IsEnabled = false`），直到你在 Convai 控制台或通过脚本 API 明确启用它。

### 下一步

{% content-ref url="/pages/d051c20fa3bb87461147abd07ac16642c7956362" %}
[长期记忆快速入门](/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/long-term-memory/quick-start.md)
{% endcontent-ref %}

{% content-ref url="/pages/c12fc9539ece3543a049cf5c334655c9bb0a6a3c" %}
[最终用户身份](/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/long-term-memory/end-user-identity.md)
{% endcontent-ref %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/long-term-memory/how-long-term-memory-works.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
