> 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 上的 Dynamic Info 字段——Initial Dynamic Info Text 会在会话开始时传递一次；启用 Keep In Context 可在整个会话期间让这些事实在所有 LLM 回合中保留。</p></figcaption></figure>

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

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

#### 示例配置

对于消防灭火认证演练，将 **初始动态信息文本** 设为：

```
设施：Alpha 海上平台
场景：消防灭火认证演练
受训者角色：接受评估的操作员
```

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

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

### 与运行时 Dynamic Context 的关系

初始上下文和运行时 Dynamic Context 是互补的——而不是替代关系。

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

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

```csharp
// 它们是对初始上下文的补充——而不是替代它
_character.DynamicContext.SetState("Station", "化学品储存区");
_character.DynamicContext.SetState("HazardLevel", "极高");
_character.DynamicContext.AddEvent("受训者绕过了手动锁定程序");
```

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

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

* **初始动态信息文本** 是在连接时发送的，并且不会被 `Reset()` 影响，除非调用方明确启用： `Reset(removeStatic: true)` 还会请求 Convai 删除当前会话的静态初始动态上下文。默认的 `Reset()` （等同于 `Reset(removeStatic: false)`）不会影响它，并且在会话进行中没有办法重新发送或替换它——只能清除它。
* **系统提示事实** 在 Convai 控制台上的内容不属于 Dynamic Context 层。运行时没有任何 SDK 调用会影响它们。
* **会话内 LLM 记忆** ——角色会在同一会话中的多个回合间保留对话上下文。 `Reset()` 会清除 Dynamic Context 跟踪器并向 Convai 发送 Reset 消息，但不会清除模型在会话中的对话记忆。

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

### 脚本访问

这两个字段都可通过位于 `ConvaiCharacter`:

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

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

### 下一步

{% content-ref url="/pages/743bcc7de155496cbbf02a52a5577aee99f28aab" %}
[Relay 组件参考](/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.
