> 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/dynamic-context/static-context-at-connection-time.md).

# 连接时的静态上下文

每个 Convai 角色都有两个字段—— **初始动态信息文本** 和 **初始动态信息保留在上下文中** ——它会在对话连接的那一刻将一段固定的上下文块注入到会话请求中。该上下文会在第一条回复之前只发送给 Convai 一次，并且独立于运行时的动态上下文更新。

对于在对话开始前就已成立且在会话期间不会变化的事实，请使用此机制：设施名称、角色的职责、训练场景类型，或演练的初始条件。对运行时动态上下文（`ConvaiDynamicContextRelay` 或 `IConvaiDynamicContext`）则用于所有会随着会话推进而变化的内容。

### 检查器配置

这两个字段都位于 `ConvaiCharacter` 组件下的 **动态信息（连接请求）** 标题下。

<figure><img src="/files/4903eadedaba2d38e64e9bc4f1459b1ce5104745" alt="Unity Inspector showing the Dynamic Info (Connection Request) section on ConvaiCharacter with Initial Dynamic Info Text and Initial Dynamic Info Keep In Context fields"><figcaption><p>ConvaiCharacter 上的动态信息字段——初始动态信息文本会在会话开始时发送一次；启用“保留在上下文中”后，这些事实将在整个会话期间跨所有 LLM 回合保留。</p></figcaption></figure>

| 字段            | 类型       | 默认值     | 描述                                                                |
| ------------- | -------- | ------- | ----------------------------------------------------------------- |
| 初始动态信息文本      | `string` | *（空）*   | 作为会话连接请求的一部分发送的自由文本块。没有格式限制——可直接写普通句子或键值行。                        |
| 初始动态信息保留在上下文中 | `bool`   | `false` | 当 `true`，Convai 会在整个会话期间的所有 LLM 回合中保留此文本。当 `false`时，该文本只会影响第一条回复。 |

{% hint style="warning" %}
**初始动态信息保留在上下文中默认值为 `false`.** 当禁用时，Convai 仅使用初始上下文文本来影响角色的第一条回复——它不会跨回合保留。如果你希望角色在长会话中始终引用初始事实，请启用此字段。保持禁用是角色在首次交流后看起来“忘记”场景上下文的常见原因。
{% endhint %}

#### 配置示例

对于消防抑制认证演练，请将 **初始动态信息文本** 设置为：

```
设施：Alpha 海上平台
场景：消防抑制认证演练
学员角色：接受评估的操作员
```

启用 **初始动态信息保留在上下文中**.

角色将在整个对话过程中引用这些事实。你无需通过运行时动态上下文重新发送它们——它们会在整个会话期间保持有效。

### 与运行时动态上下文的关系

初始上下文与运行时动态上下文是互补的——而不是替代关系。

|             | 初始动态信息              | 运行时动态上下文                              |
| ----------- | ------------------- | ------------------------------------- |
| **发送时间**    | 一次，在 `ConnectAsync` | 会话期间，按需发送                             |
| **内容**      | 在设计时设置的固定事实         | 运行时设置的实时状态和事件                         |
| **适用于**     | 设施名称、场景类型、角色职责      | 学员位置、设备状态、危险等级                        |
| **可在运行时修改** | 否——每次连接只发送一次        | 是——通过 `SetState`, `AddEvent`, `Reset` |

这两种机制会同时输入到角色的感知中。初始上下文提供稳定基础；运行时动态上下文则在其上叠加实时更新。

```csharp
// 这些是对初始上下文的补充——不是替代
_character.DynamicContext.SetState("Station", "Chemical Storage Bay");
_character.DynamicContext.SetState("HazardLevel", "Extreme");
_character.DynamicContext.AddEvent("Trainee bypassed manual lockout procedure");
```

### 什么 `Reset()` 会清除和不会清除的内容

调用 `Reset()` 在运行时动态上下文层上会清除所有已跟踪的状态和事件。默认情况下，它会保留初始动态信息：

* **初始动态信息文本** 是在连接时发送的，不会被 `Reset()` 修改，除非调用方选择启用： `Reset(removeStatic: true)` 还会请求 Convai 移除当前会话的静态初始动态上下文。默认的 `Reset()` （等同于 `Reset(removeStatic: false)`）不会触及它，而且在会话中途无法重新发送或替换它——只能将其清除。
* **系统提示词事实** 在 Convai 仪表板上的内容不属于动态上下文层。运行时没有任何 SDK 调用会影响它们。
* **会话内 LLM 记忆** ——角色会在同一会话的多轮对话中保留上下文。 `Reset()` 会清除动态上下文跟踪器并向 Convai 发送 Reset 消息，但不会清除模型的会话内对话记忆。

{% hint style="info" %}
要为新会话更改初始上下文，请结束当前对话，更新 **初始动态信息文本** 字段，然后重新连接。初始上下文每次只会发送一次 `ConnectAsync` 调用。
{% endhint %}

### 脚本访问

这两个字段都可通过 C# 属性读取，位于 `ConvaiCharacter`:

```csharp
string initialText = _character.InitialDynamicInfoText;
bool keepInContext = _character.InitialDynamicInfoKeepInContext;
```

这些属性在运行时为只读。请在场景运行前在 Inspector 中设置这些值，或者通过 `SerializedObject` 在自定义 Editor 工具中设置。

### 下一步

{% content-ref url="/pages/743bcc7de155496cbbf02a52a5577aee99f28aab" %}
[中继组件参考](/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/dynamic-context/relay-component-reference.md)
{% endcontent-ref %}

{% content-ref url="/pages/5c3f9bcc544ac6f441fe54139ac1c670eeb5c958" %}
[动态上下文脚本 API](/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/dynamic-context/dynamic-context-scripting-api.md)
{% endcontent-ref %}

{% content-ref url="/pages/bb1aef3496a2be08987c770aa7b8072e7d8c5cd6" %}
[同步行为和时序](/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/dynamic-context/sync-behavior-and-timing.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/dynamic-context/static-context-at-connection-time.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.
