> 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="https://2402152281-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEtUJA212Zc1S9ACc8T4l%2Fuploads%2Fgit-blob-4d0fe88b11dd249ae7ace47cbe6f413c67ce82ab%2Fconvai-character-dynamic-info-inspector.png?alt=media" 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 发送重置消息，但不会清除模型的会话内对话记忆。

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

### 脚本访问

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

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

这些属性在运行时是只读的。请在场景播放前于检视器中设置这些值，或者通过 `SerializedObject` 在自定义编辑器工具中进行设置。

### 下一步

{% 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
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=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
