> 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/end-user-management.md).

# 管理终端用户记录

从 Convai Editor 窗口或脚本中浏览并管理终端用户记录，涵盖编辑器工具、所有方法和分页。

Convai Unity SDK 会将每个连接了启用记忆角色的用户跟踪为一个 **终端用户记录**。每条记录会存储用户的稳定标识符、上次活动时间戳，以及你在连接期间发送的任何元数据。你可以在 Unity 编辑器中浏览和删除这些记录，或通过 `client.EndUsers`.

{% hint style="warning" %}
**Beta API。** 方法签名是稳定的，但在未来的 SDK 更新中可能会更改。请在生产环境中固定你的 SDK 版本，并在升级前查看更新日志。
{% endhint %}

***

{% tabs %}
{% tab title="编辑器工具" %}
从 Unity 菜单栏访问该面板： **Convai → 长期记忆**.

该面板会显示与你的 API 密钥关联的所有终端用户记录。它以每批 200 条的方式加载记录，并在大型数据集时通过基于游标的分页自动获取后续页面。

**可用操作：**

| 操作      | 方式                   |
| ------- | -------------------- |
| 刷新列表    | 点击 **刷新**            |
| 选择单个记录  | 点击某条记录行              |
| 全选或取消全选 | 点击 **全选** / **取消全选** |
| 删除所选记录  | 点击 **删除**            |

<figure><img src="https://2402152281-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEtUJA212Zc1S9ACc8T4l%2Fuploads%2FcTJ9o3r47D0DwlJq3Nn7%2Fimage.png?alt=media&amp;token=8af97138-41e0-4395-914e-89fbf73fc5ac" alt="Unity menu bar showing Convai → Long Term Memory navigation path"><figcaption><p>从 Unity 菜单栏打开终端用户管理面板：Convai → 长期记忆。</p></figcaption></figure>

从编辑器中删除终端用户记录会移除 **该记录以及该用户在所有角色中的所有记忆记录**。此操作无法撤销。删除前会显示确认对话框。

<figure><img src="https://2402152281-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEtUJA212Zc1S9ACc8T4l%2Fuploads%2FDF6WIWV66SbC86DOigfT%2Fimage.png?alt=media&amp;token=97069e95-7620-487e-a56b-0a32bd24125c" alt="End-user records with name and session count"><figcaption><p>带有名称和会话次数的终端用户记录。</p></figcaption></figure>
{% endtab %}

{% tab title="脚本编程" %}
通过 `client.EndUsers` 在 `ConvaiRestClient` 实例上访问终端用户操作。

```csharp
using var client = new ConvaiRestClient(ConvaiSettings.Instance.ApiKey);
```

完整示例请参见下方各个方法。
{% endtab %}
{% endtabs %}

***

### `EndUserDetails` 字段

每条终端用户记录由以下内容表示： `EndUserDetails`:

| 属性               | 类型                           | 描述                                                                                                                   |
| ---------------- | ---------------------------- | -------------------------------------------------------------------------------------------------------------------- |
| `EndUserId`      | `string`                     | SDK 在连接时发送的稳定标识符                                                                                                     |
| `LastActiveTs`   | `string`                     | 上次会话的 ISO 8601 时间戳                                                                                                   |
| `LastLtmUsageTs` | `string`                     | 上次 LTM 交互的 ISO 8601 时间戳                                                                                              |
| `Metadata`       | `Dictionary<string, object>` | 由以下内容发送的键值数据 `IEndUserMetadataProvider`                                                                              |
| `DisplayName`    | `string`                     | **计算属性** — 读取 `Metadata["name"]` 如果存在且非空；否则 `"User {EndUserId[..8]}"`，并可选地附加相对最近活动时间（例如， `"User a1b2c3d4 (3d ago)"`) |
| `ShortId`        | `string`                     | **计算属性** — 截断形式的 `EndUserId` ，用于紧凑显示（例如， `"a1b2c3d4...ef01"`)                                                        |

`DisplayName` 以及 `ShortId` 是 C# 计算属性，不是存储字段。它们不会出现在 JSON API 响应中。

***

### 脚本 API

#### 列出终端用户

通过基于游标的分页检索所有终端用户记录。默认限制为每页 50 条记录。

```csharp
using Convai.RestAPI;
using Convai.RestAPI.Internal;
using System.Collections.Generic;
using UnityEngine;

public class EndUserLister : MonoBehaviour
{
    [ContextMenu("List All End Users")]
    private async void ListAllEndUsers()
    {
        using var client = new ConvaiRestClient(ConvaiSettings.Instance.ApiKey);

        string cursor = null;
        bool hasMore = true;
        var allUsers = new List<EndUserDetails>();

        while (hasMore)
        {
            var response = await client.EndUsers.ListAsync(limit: 50, cursor: cursor);

            if (response.EndUsers != null)
                allUsers.AddRange(response.EndUsers);

            hasMore = response.HasMore;
            cursor = response.NextCursor;

            if (!hasMore || string.IsNullOrEmpty(cursor))
                break;
        }

        Debug.Log($"Total end users: {allUsers.Count}");
        foreach (var user in allUsers)
            Debug.Log($"  {user.EndUserId} — last active: {user.LastActiveTs}");
    }
}
```

**`EndUsersListResponse` 字段：**

| 属性           | 类型                     | 描述                          |
| ------------ | ---------------------- | --------------------------- |
| `EndUsers`   | `List<EndUserDetails>` | 本页记录                        |
| `TotalCount` | `int`                  | 终端用户记录总数                    |
| `NextCursor` | `string`               | 下一页的游标令牌； `null` 当不再存在后续页面时 |
| `HasMore`    | `bool`                 | 是否存在更多页面                    |

你还可以使用以下参数按活动日期筛选 `activeAfter` 以及 `activeBefore` （ISO 8601 字符串）：

```csharp
var response = await client.EndUsers.ListAsync(
    limit: 50,
    activeAfter: "2025-01-01T00:00:00Z",
    activeBefore: "2025-06-01T00:00:00Z");
```

***

#### 获取单个终端用户

通过以下方式检索某个特定用户的详细信息： `endUserId`.

```csharp
var user = await client.EndUsers.GetAsync("target-end-user-id");
Debug.Log($"Last active: {user.LastActiveTs}");
Debug.Log($"Display name: {user.DisplayName}");
```

***

#### 更新用户元数据

更新某个用户的一个或多个元数据键。补丁操作会保留你未包含的键——不会替换整个元数据对象。

```csharp
var patch = new Dictionary<string, object>
{
    { "name", "Jordan Kim" },
    { "department", "Facilities Management" }
};

var updated = await client.EndUsers.UpdateMetadataAsync("target-end-user-id", patch);
Debug.Log($"Updated metadata for {updated.EndUserId}.");
```

***

#### 删除终端用户

{% hint style="danger" %}
`DeleteAsync` 会移除终端用户记录 **以及该用户在所有角色中的所有记忆记录**。不同于 `MemoryService.DeleteAllAsync`（它的作用范围仅限于一个角色），此操作会全局移除该用户。此操作无法撤销。
{% endhint %}

```csharp
var result = await client.EndUsers.DeleteAsync("target-end-user-id");

if (result.Deleted)
    Debug.Log($"End user {result.EndUserId} deleted.");
else
    Debug.LogWarning("Deletion returned false — user may not have existed.");
```

***

### `DeleteAllAsync` 对比 `DeleteAsync`

| 操作                                                     | 范围          | 移除内容              |
| ------------------------------------------------------ | ----------- | ----------------- |
| `client.Memory.DeleteAllAsync(characterId, endUserId)` | 一个用户 + 一个角色 | 该用户-角色对的所有记忆记录    |
| `client.EndUsers.DeleteAsync(endUserId)`               | 一个用户 + 所有角色 | 该用户记录以及跨所有角色的所有记忆 |

使用 `DeleteAllAsync` 当你想重置某个用户在特定角色中的记忆，同时保留其在其他角色中的记录时使用。使用 `EndUsers.DeleteAsync` 当你需要将某个用户从系统中彻底移除时。

***

### 下一步

{% content-ref url="/pages/0b7e1084a14c3bcf13fec179ad83b458f07ecff4" %}
[记忆管理 API](/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/long-term-memory/memory-management-api.md)
{% endcontent-ref %}

{% content-ref url="/pages/9c55a31cd790424665e521972bff5affc2dbe7f9" %}
[长期记忆脚本参考](/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/long-term-memory/long-term-memory-scripting-reference.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, and the optional `goal` query parameter:

```
GET https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/long-term-memory/end-user-management.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
