> 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 角色都有两个字段—— **Initial Dynamic Info Text** 和 **Initial Dynamic Info Keep In Context** ——在对话连接时，将一段固定的上下文注入会话请求。此上下文会在首次响应前发送给 Convai 一次，并且与运行时 Dynamic Context 更新相互独立。

此机制适用于对话开始前已确定、且在会话期间不会变化的信息：设施名称、角色职责、培训场景类型或演练的初始条件。对于会话过程中不断变化的信息，请使用运行时 Dynamic Context（`ConvaiDynamicContextRelay` 或 `IConvaiDynamicContext`）。

### Inspector 配置

这两个字段都位于 `ConvaiCharacter` 组件的 **Dynamic Info (Connection Request)** 标题下。

<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 上的 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 %}

#### 配置示例

对于消防认证演练，将 **Initial Dynamic Info Text** 设为：

```
设施：Alpha 海上平台
场景：消防认证演练
受训人员角色：接受考核的操作员
```

启用 **Initial Dynamic Info Keep In Context**.

角色会在整个对话过程中引用这些信息。无需通过运行时 Dynamic Context 重新发送——这些信息会在整个会话期间持续保留。

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

初始上下文和运行时 Dynamic Context 相辅相成，而非互相替代。

|             | Initial Dynamic Info | 运行时动态上下文                               |
| ----------- | -------------------- | -------------------------------------- |
| **发送时机**    | 在 `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()` 影响，除非调用方选择清除： `Reset(removeStatic: true)` 还会请求 Convai 清除当前会话的静态初始动态上下文。默认值 `Reset()` （等同于 `Reset(removeStatic: false)`）不会清除它；并且无法在会话中途重新发送或替换它，只能将其清除。
* **系统提示词事实** 在 Convai 控制面板中配置，不属于动态上下文层。运行时没有任何 SDK 调用会影响它们。
* **会话内 LLM 记忆** —角色会在同一会话的不同轮次之间保留对话上下文。 `Reset()` 会清除动态上下文跟踪器，并向 Convai 发送 Reset 消息，但不会清除模型的会话内对话记忆。

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

### 脚本访问

可以通过 `ConvaiCharacter`:

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

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

### 后续步骤

{% content-ref url="/pages/743bcc7de155496cbbf02a52a5577aee99f28aab" %}
[中继组件参考](https://docs.convai.com/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](https://docs.convai.com/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" %}
[同步行为与时序](https://docs.convai.com/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.
