@myclaw163/clawclaw-cli 0.8.14 → 0.8.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/custom-modules/perception.md +4 -4
- package/docs/runtime/README.md +1 -1
- package/docs/runtime/behavior-runtime.md +1 -1
- package/docs/runtime/official-perception-modules.md +23 -30
- package/docs/runtime/perception-replay.md +1 -1
- package/package.json +1 -1
- package/skills/clawclaw/SKILL.md +5 -1
- package/skills/clawclaw/references/BEHAVIORS.md +16 -1
- package/skills/clawclaw/references/CUSTOM-BEHAVIOR-DEVELOPMENT.md +310 -0
- package/skills/clawclaw/references/CUSTOM-MODULE-API.md +684 -0
- package/skills/clawclaw/references/CUSTOM-MODULES.md +125 -0
- package/skills/clawclaw/references/CUSTOM-PERCEPTION-DEVELOPMENT.md +331 -0
- package/src/runtime/clawclaw/behavior-decision.ts +4 -1
- package/src/runtime/clawclaw/owner.ts +64 -1
- package/src/runtime/clawclaw/plugins/assembly.ts +4 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-analysis-thread-host.ts +35 -2
- package/src/runtime/clawclaw/plugins/behavior/behavior-catalog.ts +11 -7
- package/src/runtime/clawclaw/plugins/behavior/behavior-normalization.ts +3 -2
- package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.ts +46 -50
- package/src/runtime/clawclaw/plugins/behavior/behavior-runner.ts +39 -9
- package/src/runtime/clawclaw/plugins/behavior/thread-behavior-executor.ts +167 -7
- package/src/runtime/clawclaw/plugins/perception/module-runner.ts +86 -89
- package/src/runtime/clawclaw/plugins/perception/perception-analysis-thread-pool.ts +157 -41
- package/src/runtime/clawclaw/plugins/perception/perception-analysis-thread-worker.ts +28 -4
- package/src/runtime/clawclaw/plugins/perception/perception-module-loader.ts +3 -1
- package/src/runtime/clawclaw/plugins/perception/perception-module-output.ts +30 -0
- package/src/runtime/clawclaw/plugins/perception/perception-plugin.ts +114 -17
- package/src/runtime/clawclaw/plugins/perception/perception-reducer-thread-pool.ts +660 -0
- package/src/runtime/clawclaw/plugins/perception/perception-reducer-thread-worker.ts +262 -0
- package/src/runtime/clawclaw/plugins/trigger/trigger-module-loader.ts +41 -12
- package/src/runtime/clawclaw/plugins/trigger/trigger-plugin.ts +252 -20
- package/src/runtime/clawclaw/plugins/trigger/trigger-thread-pool.ts +496 -0
- package/src/runtime/clawclaw/plugins/trigger/trigger-thread-worker.ts +164 -0
- package/src/runtime/clawclaw-event-catalog.ts +1 -0
- package/src/runtime/framework/events/memory-event-repository.ts +12 -0
- package/src/runtime/framework/modules/runtime-module-output.ts +24 -0
- package/src/runtime/framework/persistence/memory-first-persistence.ts +3 -0
- package/src/runtime/framework/plugins/contracts.ts +3 -0
- package/src/runtime/framework/plugins/runtime-input-journal.ts +12 -0
- package/src/runtime/framework/plugins/runtime-plugin-scheduler.ts +150 -23
- package/src/runtime/framework/runtime.ts +18 -0
- package/src/runtime/framework/state/history/memory-state-history-repository.ts +10 -0
- package/src/runtime/observability/redaction.ts +28 -1
- package/src/sdk/perception-plugin/index.ts +13 -8
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# 自定义模块开发
|
|
2
|
+
|
|
3
|
+
本文是当前已上线范围的 Agent 开发入口,只介绍 Behavior 和 Perception。现有模块满足需求时,直接选择和配置即可;需要新的玩法或新的视觉事实时,可以编写社区模块。
|
|
4
|
+
|
|
5
|
+
## 可以自定义什么
|
|
6
|
+
|
|
7
|
+
CCL 当前对社区提供两种模块:
|
|
8
|
+
|
|
9
|
+
| 模块 | 它负责什么 | 适合实现的例子 |
|
|
10
|
+
|---|---|---|
|
|
11
|
+
| Behavior | 根据当前目标决定角色接下来做什么 | 一直做任务、去某处、追猎某个座位、逃跑、巡逻、报告尸体 |
|
|
12
|
+
| Perception | 从连续视觉帧中识别结构化事实 | 玩家进入视野、停止、改变路线、出现尸体、形成跟随关系 |
|
|
13
|
+
|
|
14
|
+
它们分别回答两个问题:
|
|
15
|
+
|
|
16
|
+
```text
|
|
17
|
+
Perception:我观察到了什么?
|
|
18
|
+
Behavior:为了当前目标,角色下一步做什么?
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Behavior 可以作为长期玩法运行,也可以完成一次短任务,两种方式使用同一个 `ClawclawBehavior` 接口。
|
|
22
|
+
|
|
23
|
+
两种模块的 `apiVersion` 按各自公开契约独立演进,不是整套框架共用的版本号。当前 Behavior 使用 API v3,Perception 使用 API v2;数字不同不表示能力高低。
|
|
24
|
+
|
|
25
|
+
## 官方已经支持什么
|
|
26
|
+
|
|
27
|
+
### Behavior
|
|
28
|
+
|
|
29
|
+
官方 Behavior 从当前账号 Hub loadout 获取;CLI 包不再把官方 Behavior 物化到本地目录。
|
|
30
|
+
|
|
31
|
+
查看当前账号的准确清单和参数:
|
|
32
|
+
|
|
33
|
+
```powershell
|
|
34
|
+
ccl behavior list
|
|
35
|
+
ccl behavior info <id>
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Perception
|
|
39
|
+
|
|
40
|
+
官方 Perception 从当前账号 Hub loadout 获取;准确清单以本次 `ccl load` 为准。
|
|
41
|
+
|
|
42
|
+
```powershell
|
|
43
|
+
ccl perception --list
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
完整 Event type、公共 payload 字段和观察边界见[开发 Perception](./CUSTOM-PERCEPTION-DEVELOPMENT.md)。
|
|
47
|
+
|
|
48
|
+
## 应该从哪篇开始
|
|
49
|
+
|
|
50
|
+
- 想控制角色行动:阅读[开发 Behavior](./CUSTOM-BEHAVIOR-DEVELOPMENT.md)
|
|
51
|
+
- 想从视觉中识别事实:阅读[开发 Perception](./CUSTOM-PERCEPTION-DEVELOPMENT.md)
|
|
52
|
+
- 查准确字段和签名:阅读[API 参考](./CUSTOM-MODULE-API.md)
|
|
53
|
+
|
|
54
|
+
两篇教程都先解释模块用途和整体结构,再给最小示例,随后讲输入、查询、输出、状态和高级调度。
|
|
55
|
+
|
|
56
|
+
## 两类模块怎样配合
|
|
57
|
+
|
|
58
|
+
```mermaid
|
|
59
|
+
flowchart LR
|
|
60
|
+
Server["游戏服务器"] --> Frame["ObservationFrame 与后端 Event"]
|
|
61
|
+
Frame --> Perception["Perception<br/>识别视觉事实"]
|
|
62
|
+
Frame --> Behavior
|
|
63
|
+
Perception --> VisualEvents["visual.* Event"]
|
|
64
|
+
VisualEvents --> Behavior
|
|
65
|
+
Behavior --> Decision["Decision"]
|
|
66
|
+
Decision --> Runtime["Runtime 校验并有序提交"]
|
|
67
|
+
Runtime --> Server
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
同一 Runtime epoch 的处理是:
|
|
71
|
+
|
|
72
|
+
1. Perception 读取观察帧并提交可持久化的视觉事实 Event;
|
|
73
|
+
2. Behavior 读取当前玩家视角 State、待处理 Event 和 Activation 参数;
|
|
74
|
+
3. Runtime 在上游视觉事实提交后运行当前 Behavior;
|
|
75
|
+
4. Runtime 校验并提交 Behavior 的 Action、Status、Event 或完成结果;
|
|
76
|
+
5. 动作是否真正成功,以服务器后续返回的新 State/Event 为准。
|
|
77
|
+
|
|
78
|
+
## Behavior 的两种运行方式
|
|
79
|
+
|
|
80
|
+
| 运行层 | 谁启动 | 用途 |
|
|
81
|
+
|---|---|---|
|
|
82
|
+
| `long` | 用户、Agent 或 idle fallback policy | 持续做任务、巡逻或执行一种长期玩法 |
|
|
83
|
+
| `short` | 用户或 Agent | 完成当前一件事,结束后把决策窗口交还 Agent |
|
|
84
|
+
|
|
85
|
+
控制顺序为 `short > long`。short 结束后默认等待 `idle_ms=500`,再恢复 long 或启动角色 fallback。
|
|
86
|
+
|
|
87
|
+
long 不等于 fallback。用户和 Agent 都可以立即启动 long;fallback 只是“空闲 500ms 后自动启动某个 long Behavior”的配置。
|
|
88
|
+
|
|
89
|
+
现有 `ccl speak`、`ccl vote`、`ccl aside` 等独立通信命令本轮仍直接经过 Action Plugin 与后端交互,不属于 Behavior Activation。
|
|
90
|
+
|
|
91
|
+
## 一个模块文件由什么组成
|
|
92
|
+
|
|
93
|
+
每个自定义模块都是默认导出的 TypeScript 或 JavaScript 类,包含:
|
|
94
|
+
|
|
95
|
+
1. **基类**:说明这是 Behavior 还是 Perception;
|
|
96
|
+
2. **Manifest**:静态可读的 ID、说明、参数和调度声明;
|
|
97
|
+
3. **回调**:Runtime 交付输入,模块返回本轮结果。
|
|
98
|
+
|
|
99
|
+
| 模块 | 基类 | 必须实现 |
|
|
100
|
+
|---|---|---|
|
|
101
|
+
| Behavior | `ClawclawBehavior` | `decide(input, api)` |
|
|
102
|
+
| Perception | `ClawclawPerception` | `update(frame, api)` |
|
|
103
|
+
|
|
104
|
+
开发者不需要连接服务器、维护 Action 队列、排序 Event 或修改游戏状态。Runtime 负责这些边界。
|
|
105
|
+
|
|
106
|
+
## 共同的数据边界
|
|
107
|
+
|
|
108
|
+
- `GameState` 是当前玩家视角的权威快照,不是全知世界;
|
|
109
|
+
- Behavior 使用只读输入和查询能力;
|
|
110
|
+
- Perception 不直接发 Action;
|
|
111
|
+
- Action 是请求,不是成功事实;
|
|
112
|
+
- Status 是公开说明,不会修改 `GameState`;
|
|
113
|
+
- Event 是公开结构化事实,不是模块私有 KV;
|
|
114
|
+
- 当前公共 API 不提供跨 Runtime 重启的模块私有持久化。
|
|
115
|
+
|
|
116
|
+
## 文件放在哪里
|
|
117
|
+
|
|
118
|
+
```text
|
|
119
|
+
<workspace>/user-data/accounts/<account-hash>/capabilities/behaviors/*.ts
|
|
120
|
+
<workspace>/user-data/accounts/<account-hash>/capabilities/perceptions/*.ts
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
`ccl behavior list/info`、`ccl perception --list` 和 `ccl load` 在判断第三方模块是否静态可识别时,只读取源码文本和字面量 Manifest,不执行第三方源码。
|
|
124
|
+
|
|
125
|
+
模块在实际启用或激活时才会 import。自定义模块与 CCL 进程拥有相同权限,不是安全沙箱;只运行自己编写或已经审查的代码。
|
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
# 开发 Perception
|
|
2
|
+
|
|
3
|
+
## Perception 是什么
|
|
4
|
+
|
|
5
|
+
Perception 把当前玩家真实看到的信息整理成结构化 Event。它适合识别连续画面中的事实,例如:
|
|
6
|
+
|
|
7
|
+
- 玩家进入、离开或重新出现在视野中;
|
|
8
|
+
- 玩家开始移动、停止移动或改变方向;
|
|
9
|
+
- 玩家进入新房间、转弯、折返或徘徊;
|
|
10
|
+
- 两名玩家共同移动或形成跟随关系;
|
|
11
|
+
- 一具尸体出现、持续可见或离开视野。
|
|
12
|
+
|
|
13
|
+
Perception 不决定“接下来应该做什么”,也不能直接执行 Action。它产生的 Event 可以被 Agent、Behavior、`ccl events` 和诊断工具消费。
|
|
14
|
+
|
|
15
|
+
## 官方已经提供什么
|
|
16
|
+
|
|
17
|
+
当前 8 个官方模块默认覆盖:
|
|
18
|
+
|
|
19
|
+
| 模块 | 识别的事实 |
|
|
20
|
+
|---|---|
|
|
21
|
+
| `visual.visibility` | 玩家进入、离开、重新进入视野 |
|
|
22
|
+
| `visual.corpse_visibility` | 尸体进入或离开视野 |
|
|
23
|
+
| `visual.motion` | 移动片段和方向变化 |
|
|
24
|
+
| `visual.stop` | 停止和恢复移动 |
|
|
25
|
+
| `visual.spatial` | 房间、区域和房间内方位变化 |
|
|
26
|
+
| `visual.route` | 转弯、折返和徘徊 |
|
|
27
|
+
| `visual.co_movement` | 两人共同移动及结束 |
|
|
28
|
+
| `visual.follow` | 稳定跟随关系及结束 |
|
|
29
|
+
|
|
30
|
+
只有现有模块不能表达你需要的新视觉事实时,才需要自定义 Perception。
|
|
31
|
+
|
|
32
|
+
### 官方 Event type
|
|
33
|
+
|
|
34
|
+
| 模块 | 产出的 Event type |
|
|
35
|
+
|---|---|
|
|
36
|
+
| `visual.visibility` | `visual.player_entered`、`visual.player_left`、`visual.player_reappeared` |
|
|
37
|
+
| `visual.corpse_visibility` | `visual.corpse_entered`、`visual.corpse_left` |
|
|
38
|
+
| `visual.motion` | `visual.player_moved`、`visual.player_motion_changed` |
|
|
39
|
+
| `visual.stop` | `visual.player_stopped`、`visual.player_resumed` |
|
|
40
|
+
| `visual.spatial` | `visual.player_area_changed`、`visual.player_position_changed` |
|
|
41
|
+
| `visual.route` | `visual.player_turned`、`visual.player_reversed`、`visual.player_wandering` |
|
|
42
|
+
| `visual.co_movement` | `visual.players_moving_together`、`visual.players_stopped_moving_together` |
|
|
43
|
+
| `visual.follow` | `visual.following`、`visual.following_ended` |
|
|
44
|
+
|
|
45
|
+
官方视觉 Event 的 payload 使用统一外层:`from_tick`、`to_tick`、`actors`、面向展示的 `text`,以及按 type 定义的结构化 `data`。移动片段还可能包含 `status`。业务代码应判断 Event `type` 和 `data`,不要解析 `text`。玩家标识使用 `seat:<座位号>`;关系 Event 的 `actors` 顺序由具体 type 定义,例如跟随关系按 `[follower, leader]` 排列。
|
|
46
|
+
|
|
47
|
+
官方模块只处理当前 Agent 实际可见的自己、`visible_players`、`visible_corpses` 及其坐标和房间。它们不继续追踪不可见玩家,也不推断阵营、身份、意图、任务完成或击杀;输入没有明确声明完整可见集合时,缺席只表示未知,不能生成离开视野 Event。
|
|
48
|
+
|
|
49
|
+
## 一个 Perception 文件的结构
|
|
50
|
+
|
|
51
|
+
普通感知模块是 `ClawclawPerception` 的子类:
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import { ClawclawPerception } from '@myclaw163/clawclaw-cli/perception-plugin';
|
|
55
|
+
|
|
56
|
+
export default class MyPerception extends ClawclawPerception {
|
|
57
|
+
static readonly manifest = {
|
|
58
|
+
// 模块注册信息、观察主体和可能输出的 Event
|
|
59
|
+
} as const;
|
|
60
|
+
|
|
61
|
+
update(frame, api) {
|
|
62
|
+
// 读取当前视觉帧,返回本帧确认的 Event
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### `ClawclawPerception`
|
|
68
|
+
|
|
69
|
+
公开的 ordered Perception 基类。Runtime 按视觉帧顺序调用同一个实例,因此可以使用类字段保存短期轨迹状态。
|
|
70
|
+
|
|
71
|
+
### `static manifest`
|
|
72
|
+
|
|
73
|
+
感知模块的机器可读注册信息:
|
|
74
|
+
|
|
75
|
+
| 字段 | 作用 |
|
|
76
|
+
|---|---|
|
|
77
|
+
| `apiVersion` | 当前写 `2` |
|
|
78
|
+
| `id` | 稳定且唯一的模块 ID |
|
|
79
|
+
| `name` / `description` | 展示名称和识别目标 |
|
|
80
|
+
| `localizations` | 可选的本地化名称与说明 |
|
|
81
|
+
| `subjectScope` | Event 描述其他玩家、自身与他人、玩家对,还是尸体 |
|
|
82
|
+
| `produces` | 这个模块可能发布的 Event type |
|
|
83
|
+
| `eventMetadata` | 可选,为 `produces` 中的 Event 提供展示名称、说明与本地化 |
|
|
84
|
+
| `execution` | 可选;耗时分析模块写 `parallel_analysis` |
|
|
85
|
+
| `boundaryBaseline` | 可选;声明并行 reducer 能否仅用当前帧安全建立边界基线 |
|
|
86
|
+
|
|
87
|
+
### 普通模式:`update(frame, api)`
|
|
88
|
+
|
|
89
|
+
每个视觉帧调用一次:
|
|
90
|
+
|
|
91
|
+
- `frame` 提供同一份玩家视角 `GameState`、视觉对象、帧序号和连续性;
|
|
92
|
+
- `api` 提供静态地图和空间查询;
|
|
93
|
+
- 返回值是本帧确认成立的 `PerceptionEventProposal[]`。
|
|
94
|
+
|
|
95
|
+
这是普通 Perception 的唯一循环回调。分析可能超过一个游戏 tick 时,不再实现这条路径,而是改用文末的 `ClawclawParallelPerception` 和 `analyze()` / `commit()`。
|
|
96
|
+
|
|
97
|
+
## 最小可运行示例
|
|
98
|
+
|
|
99
|
+
下面的模块在当前连续轨迹中第一次看见某个其他玩家时发布 Event:
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
import {
|
|
103
|
+
ClawclawPerception,
|
|
104
|
+
type PerceptionApi,
|
|
105
|
+
type PerceptionEventProposal,
|
|
106
|
+
type PerceptionFrame,
|
|
107
|
+
} from '@myclaw163/clawclaw-cli/perception-plugin';
|
|
108
|
+
|
|
109
|
+
export default class FirstSightingPerception extends ClawclawPerception {
|
|
110
|
+
static readonly manifest = {
|
|
111
|
+
apiVersion: 2,
|
|
112
|
+
id: 'visual.first_sighting',
|
|
113
|
+
name: '第一次看见玩家',
|
|
114
|
+
description: '在当前连续轨迹中第一次看见其他玩家时发布事件。',
|
|
115
|
+
subjectScope: 'others',
|
|
116
|
+
produces: ['visual.first_sighting'],
|
|
117
|
+
} as const;
|
|
118
|
+
|
|
119
|
+
private readonly seen = new Set<string>();
|
|
120
|
+
|
|
121
|
+
update(
|
|
122
|
+
frame: PerceptionFrame,
|
|
123
|
+
_api: PerceptionApi,
|
|
124
|
+
): readonly PerceptionEventProposal[] {
|
|
125
|
+
const events: PerceptionEventProposal[] = [];
|
|
126
|
+
|
|
127
|
+
for (const player of frame.observation.players) {
|
|
128
|
+
if (player.observer || this.seen.has(player.id)) continue;
|
|
129
|
+
this.seen.add(player.id);
|
|
130
|
+
events.push({
|
|
131
|
+
type: 'visual.first_sighting',
|
|
132
|
+
dedupeKey: `first-sighting:${player.id}`,
|
|
133
|
+
payload: {
|
|
134
|
+
player_id: player.id,
|
|
135
|
+
name: player.name,
|
|
136
|
+
seat: player.seat,
|
|
137
|
+
room: player.room,
|
|
138
|
+
},
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return events;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
reset(): void {
|
|
146
|
+
this.seen.clear();
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
这个例子展示了完整基本结构:继承基类、声明 Event、按帧读取玩家、用实例字段记录轨迹状态、在 reset 时清空状态。
|
|
152
|
+
|
|
153
|
+
## 每帧会收到什么
|
|
154
|
+
|
|
155
|
+
`update()` 收到一个 `PerceptionFrame`:
|
|
156
|
+
|
|
157
|
+
| 字段 | 内容 |
|
|
158
|
+
|---|---|
|
|
159
|
+
| `gameState` | 与 Behavior 相同的玩家视角 GameState |
|
|
160
|
+
| `observation` | 从该 State 派生的稳定视觉对象结构 |
|
|
161
|
+
| `frameKey` / `frameSeq` | 当前持久 State frame 的身份和本地顺序 |
|
|
162
|
+
| `tick` / `timeMs` | 视觉采样对应的游戏 tick 和时间 |
|
|
163
|
+
| `tickDelta` | 相对上一帧的 tick 差 |
|
|
164
|
+
| `continuousFromPrevious` | 是否能证明本帧与上一帧连续 |
|
|
165
|
+
|
|
166
|
+
Perception 把公共 `ObservationFrame` 称为 `PerceptionFrame`,用它把连续视觉样本变成可持久化事实。
|
|
167
|
+
|
|
168
|
+
### `frame.gameState`
|
|
169
|
+
|
|
170
|
+
动态游戏事实从这里读取,例如 phase、自身 activity、任务、会议和可见对象。它不是全知状态。
|
|
171
|
+
|
|
172
|
+
### `frame.observation`
|
|
173
|
+
|
|
174
|
+
它包含:
|
|
175
|
+
|
|
176
|
+
```ts
|
|
177
|
+
interface VisualObservation {
|
|
178
|
+
players: readonly VisualPlayer[];
|
|
179
|
+
corpses: readonly VisualCorpse[];
|
|
180
|
+
visibleSetComplete: boolean;
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
`VisualPlayer` 和 `VisualCorpse` 提供稳定 ID、坐标、房间及可选身份。`observer: true` 的玩家是当前玩家自己。
|
|
185
|
+
|
|
186
|
+
`observation` 只是从同一份 GameState 派生的视觉便利结构,不是第二种 State。`visibleSetComplete: false` 时,本帧未出现的对象不能被解释为“已经离开视野”。
|
|
187
|
+
|
|
188
|
+
### 帧序号与连续性
|
|
189
|
+
|
|
190
|
+
- `frameSeq` 是 CCL State History 的本地顺序,不等于游戏 tick;
|
|
191
|
+
- `tick` 是这次视觉采样的游戏 tick;
|
|
192
|
+
- `continuousFromPrevious: false` 表示中间可能断线、跳帧或跨边界;
|
|
193
|
+
- 需要比较轨迹的算法必须在不连续时停止推断,并等待新基线。
|
|
194
|
+
|
|
195
|
+
完整字段见 [PerceptionFrame 参考](./CUSTOM-MODULE-API.md#perceptionframe)。
|
|
196
|
+
|
|
197
|
+
## 可以查询什么
|
|
198
|
+
|
|
199
|
+
Perception 只有 `api.spatial`,用于静态地图和确定性空间推导:
|
|
200
|
+
|
|
201
|
+
- 房间多边形、包围盒和坐标轴方向;
|
|
202
|
+
- `roomAt(x, y)`:坐标所在房间;
|
|
203
|
+
- `sideInRoom(position)`:房间内方位;
|
|
204
|
+
- `placeLabel(position)`:可读位置名称;
|
|
205
|
+
- `transitionIsMapped(from, to)`:是否存在已知房间连接。
|
|
206
|
+
|
|
207
|
+
它没有 Navigation、最近观察查询或 Action API。历史视觉证据来自当前帧、实例字段,或者高级并行模式提供的 `history`。
|
|
208
|
+
|
|
209
|
+
## 可以输出什么
|
|
210
|
+
|
|
211
|
+
Perception 只能返回 Event proposal:
|
|
212
|
+
|
|
213
|
+
```ts
|
|
214
|
+
{
|
|
215
|
+
type: 'visual.first_sighting',
|
|
216
|
+
dedupeKey: 'first-sighting:player:2',
|
|
217
|
+
payload: { player_id: 'player:2' },
|
|
218
|
+
occurredAt: '2026-08-14T12:00:00.000Z', // 可选
|
|
219
|
+
}
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
| 字段 | 作用 |
|
|
223
|
+
|---|---|
|
|
224
|
+
| `type` | 必须在 `manifest.produces` 中声明 |
|
|
225
|
+
| `dedupeKey` | 同一输入和输出位置重放时使用的稳定语义键 |
|
|
226
|
+
| `payload` | 结构化业务事实 |
|
|
227
|
+
| `dedupeScope` | 可选,`segment` 或 `session` |
|
|
228
|
+
| `occurredAt` | 可选的事实发生时间 |
|
|
229
|
+
|
|
230
|
+
`dedupeKey` 不会自动阻止模块在后续 frame 再次发布同类事实。需要“只发布一次”时,应像最小示例一样在实例或 reducer 中维护状态。
|
|
231
|
+
|
|
232
|
+
Perception 不能返回 Action、Behavior Status 或 GameState patch。
|
|
233
|
+
|
|
234
|
+
## 怎样保存跨帧信息
|
|
235
|
+
|
|
236
|
+
普通 `update()` 模式会在同一个实例上按帧调用,因此可以用类字段保存轨迹状态。状态应只描述当前连续视觉片段。
|
|
237
|
+
|
|
238
|
+
### `reset()`
|
|
239
|
+
|
|
240
|
+
phase 变化、轨迹 gap、segment 结束、shutdown 或 replay restart 时,Runtime 会调用 `reset()` 或重建实例。模块必须清空所有依赖旧轨迹的数据。
|
|
241
|
+
|
|
242
|
+
### `flush()`
|
|
243
|
+
|
|
244
|
+
长片段模块可以实现 `flush(boundary, api)`,在 Runtime 请求快照或关闭当前轨迹时输出已经成立的片段。`events_requested` 是快照,不会自动 reset;关闭边界会在之后重置轨迹。
|
|
245
|
+
|
|
246
|
+
### `debugSnapshot()`
|
|
247
|
+
|
|
248
|
+
返回开发者诊断状态,供离线 replay 和诊断查看。它不会变成 Agent Event,也不会用于恢复模块。
|
|
249
|
+
|
|
250
|
+
这些都不是磁盘持久化。API v2 没有跨 Runtime 重启恢复感知私有状态的 KV。
|
|
251
|
+
|
|
252
|
+
## 高级:耗时分析
|
|
253
|
+
|
|
254
|
+
普通 `update()` 应保持短小。如果分析可能超过一个游戏 tick,可以改用 `ClawclawParallelPerception<TAnalysis>`,并在 Manifest 声明:
|
|
255
|
+
|
|
256
|
+
```ts
|
|
257
|
+
execution: 'parallel_analysis'
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
两种执行模式互斥:普通模块继承 `ClawclawPerception` 并实现 `update()`;高级模块继承 `ClawclawParallelPerception` 并实现 `analyze()` 与 `commit()`,不会再调用 `update()`。
|
|
261
|
+
|
|
262
|
+
两种模式的模块顶层代码、constructor、`update/commit/flush/reset` 都运行在持久 Worker lane,不运行在 Runtime Owner 主线程。不要依赖 Owner 的 `globalThis`、主线程 ID 或其他进程内单例;输入输出需要支持 `structuredClone`。这不改变公开回调签名和帧顺序。
|
|
263
|
+
|
|
264
|
+
高级模式分两步:
|
|
265
|
+
|
|
266
|
+
### `analyze(input, api)`
|
|
267
|
+
|
|
268
|
+
- 在 Worker 中计算,可以与其他 frame 乱序完成;
|
|
269
|
+
- 每个 frame 使用新 analyzer 实例,不能依赖 `this` 保存状态;
|
|
270
|
+
- 输入含当前 `frame`、升序 `history` 和 `gapBefore`;总窗口最多 10 帧,即当前 frame 加最多 9 个旧 frame;
|
|
271
|
+
- 返回值必须能被 `structuredClone`;
|
|
272
|
+
- 只产生分析结果,不能发布 Event。
|
|
273
|
+
|
|
274
|
+
### `commit(frame, analysis, api)`
|
|
275
|
+
|
|
276
|
+
- 在一个持久 Worker reducer 实例上按 frame 顺序调用;
|
|
277
|
+
- 只有这里可以更新跨帧类字段并发布 Event;
|
|
278
|
+
- 必须保持短小;
|
|
279
|
+
- 某些已完成的 analyze 可能因窗口或边界变化而不进入 commit,不能假设每次分析都会生效。
|
|
280
|
+
|
|
281
|
+
开发者不用维护乱序结果队列;Runtime 负责按帧提交,并在轨迹边界 reset reducer 状态。只有 reset 或 commit 失败等异常情况下,Runtime 才会重建 reducer 实例。
|
|
282
|
+
|
|
283
|
+
每个 reducer Worker lane 的 V8 old-generation 默认限制为 64MB;最多 10 个 lane。启用不超过 10 个感知器时通常一模块一 lane,更多模块会共享 lane。单次 analysis、Event 输出和调试快照各自最多 1MB;超限按该模块失败处理,不终止 Runtime。
|
|
284
|
+
|
|
285
|
+
### 边界基线:`boundaryBaseline`
|
|
286
|
+
|
|
287
|
+
并行模块如果满足下面这个严格条件,可以在 Manifest 声明:
|
|
288
|
+
|
|
289
|
+
```ts
|
|
290
|
+
boundaryBaseline: 'current_only'
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
它表示 `analysis` 中已经包含当前帧自身的完整观察,`commit()` 在 reducer 刚被 reset 后不依赖 `history` 或旧实例状态,也能仅凭当前帧安全建立新基线。Runtime 会允许这次边界 commit 更新 reducer,但抑制它返回的 Event,避免把“建立基线”误报成新变化。
|
|
294
|
+
|
|
295
|
+
只有确实满足这个不变量时才能声明。需要前后帧关系才能建立状态的算法应省略 `boundaryBaseline`,等待 Runtime 提供边界后的安全窗口;不要为了更早收到 `commit()` 而错误声明。
|
|
296
|
+
|
|
297
|
+
Behavior 在当前 epoch 的 Perception 结果提交后运行,可以消费已经形成的视觉事实。需要复用的帧算法应抽成纯工具库。
|
|
298
|
+
|
|
299
|
+
## 启用、查看和复放
|
|
300
|
+
|
|
301
|
+
把文件放在:
|
|
302
|
+
|
|
303
|
+
```text
|
|
304
|
+
<workspace>/user-data/accounts/<account-hash>/capabilities/perceptions/
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
```powershell
|
|
308
|
+
ccl perception --list
|
|
309
|
+
ccl perception --enable visual.first_sighting
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
重启 Runtime 后触发对应画面,再查看:
|
|
313
|
+
|
|
314
|
+
```powershell
|
|
315
|
+
ccl events visual.first_sighting
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
源码开发环境还提供只读实时观察与离线复放命令:
|
|
319
|
+
|
|
320
|
+
```powershell
|
|
321
|
+
npm run watch:perception
|
|
322
|
+
npm run replay:perception -- --tick 580 --output ..\tmp\perception-580.json
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
复放不会消费 Agent 的 Event,也不会修改 Canonical Event Ledger。第三方模块会像在线运行一样被加载,因此只对受信任的本地模块运行。
|
|
326
|
+
|
|
327
|
+
## 继续阅读
|
|
328
|
+
|
|
329
|
+
- [自定义模块总览](./CUSTOM-MODULES.md)
|
|
330
|
+
- [API 参考](./CUSTOM-MODULE-API.md)
|
|
331
|
+
- [开发 Behavior](./CUSTOM-BEHAVIOR-DEVELOPMENT.md)
|
|
@@ -2,6 +2,7 @@ import type {
|
|
|
2
2
|
BehaviorDecision,
|
|
3
3
|
FiniteBehaviorDecision,
|
|
4
4
|
} from '../../sdk/behavior-plugin/index.js';
|
|
5
|
+
import { assertRuntimeModuleOutputSize } from '../framework/modules/runtime-module-output.js';
|
|
5
6
|
|
|
6
7
|
const DECISION_KEYS = new Set(['actions', 'status', 'events', 'delegate', 'finish']);
|
|
7
8
|
const STATUS_KEYS = new Set(['summary', 'activity', 'room', 'task', 'targetPlayer', 'data']);
|
|
@@ -85,5 +86,7 @@ export function normalizeBehaviorDecision(
|
|
|
85
86
|
throw new Error('behavior_finish_summary_invalid');
|
|
86
87
|
}
|
|
87
88
|
}
|
|
88
|
-
|
|
89
|
+
const normalized = Object.freeze(structuredClone(source)) as BehaviorDecision | FiniteBehaviorDecision;
|
|
90
|
+
assertRuntimeModuleOutputSize(normalized, 'behavior_output');
|
|
91
|
+
return normalized;
|
|
89
92
|
}
|
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
writeFileSync,
|
|
11
11
|
} from 'node:fs';
|
|
12
12
|
import { join } from 'node:path';
|
|
13
|
+
import { getHeapStatistics } from 'node:v8';
|
|
13
14
|
import type { AuthProfile } from '../../lib/auth.js';
|
|
14
15
|
import { GameClient } from '../../lib/game-client.js';
|
|
15
16
|
import { getProfileRuntimeStateDir, getWorkspaceDir } from '../../lib/init-command.js';
|
|
@@ -118,6 +119,8 @@ import {
|
|
|
118
119
|
getUserDataAccountTriggersDir,
|
|
119
120
|
} from '../../lib/user-data.js';
|
|
120
121
|
|
|
122
|
+
const FULL_DIAGNOSTIC_SAMPLE_INTERVAL_MS = 5_000;
|
|
123
|
+
|
|
121
124
|
export interface ClawclawOwnerOptions {
|
|
122
125
|
profile: AuthProfile;
|
|
123
126
|
intent: ClawclawSourceStartIntent;
|
|
@@ -679,6 +682,7 @@ export async function runClawclawOwner(
|
|
|
679
682
|
let relayAbort: AbortController | undefined;
|
|
680
683
|
let relayPromise: Promise<void> | undefined;
|
|
681
684
|
let parentWatch: ParentProcessWatchHandle | undefined;
|
|
685
|
+
let diagnosticSampleTimer: ReturnType<typeof setInterval> | undefined;
|
|
682
686
|
let localStopRequested = false;
|
|
683
687
|
let forceExitTimer: ReturnType<typeof setTimeout> | undefined;
|
|
684
688
|
const channelPolicy = channelOwnerDeliveryPolicy(options.channelDelivery);
|
|
@@ -696,7 +700,8 @@ export async function runClawclawOwner(
|
|
|
696
700
|
force: options.forceExistingRuntime === true,
|
|
697
701
|
});
|
|
698
702
|
telemetry = createOwnerTelemetryRecorder(options.profile);
|
|
699
|
-
|
|
703
|
+
const fullDiagnosticsEnabled = resolveOwnerObservabilityLevel() === 'full';
|
|
704
|
+
if (fullDiagnosticsEnabled) {
|
|
700
705
|
const retention = observabilityRetention();
|
|
701
706
|
rawWsLog = new BufferedRawWsLog(
|
|
702
707
|
ownerRawWsLogPath(getProfileRuntimeStateDir(options.profile)),
|
|
@@ -784,8 +789,11 @@ export async function runClawclawOwner(
|
|
|
784
789
|
...perceptionConfig.perception,
|
|
785
790
|
modules: perceptionModules,
|
|
786
791
|
moduleSources: perceptionSources,
|
|
792
|
+
diagnosticsEnabled: fullDiagnosticsEnabled,
|
|
787
793
|
},
|
|
788
794
|
behaviorCatalog,
|
|
795
|
+
behaviorDiagnosticsEnabled: fullDiagnosticsEnabled,
|
|
796
|
+
triggerDiagnosticsEnabled: fullDiagnosticsEnabled,
|
|
789
797
|
fallbackBehaviors,
|
|
790
798
|
behaviorEventTypes,
|
|
791
799
|
triggers,
|
|
@@ -808,6 +816,60 @@ export async function runClawclawOwner(
|
|
|
808
816
|
visualEventTypes: assembly.perception.produces.events,
|
|
809
817
|
behaviorEventTypes: assembly.behavior.produces?.events ?? [],
|
|
810
818
|
});
|
|
819
|
+
if (fullDiagnosticsEnabled) {
|
|
820
|
+
const sample = (): void => {
|
|
821
|
+
try {
|
|
822
|
+
const memory = process.memoryUsage();
|
|
823
|
+
const heap = getHeapStatistics();
|
|
824
|
+
const behaviorStatus = assembly.behavior.status();
|
|
825
|
+
const effectiveBehaviorId = behaviorStatus.current?.behaviorId
|
|
826
|
+
?? behaviorStatus.stack.long?.behaviorId
|
|
827
|
+
?? behaviorStatus.stack.fallback?.behaviorId;
|
|
828
|
+
const effectiveBehavior = effectiveBehaviorId
|
|
829
|
+
? behaviorSourceById.get(effectiveBehaviorId)
|
|
830
|
+
: undefined;
|
|
831
|
+
telemetry?.log('debug', 'runtime.diagnostics.sample', {
|
|
832
|
+
sample_interval_ms: FULL_DIAGNOSTIC_SAMPLE_INTERVAL_MS,
|
|
833
|
+
process_memory: {
|
|
834
|
+
rss_bytes: memory.rss,
|
|
835
|
+
heap_total_bytes: memory.heapTotal,
|
|
836
|
+
heap_used_bytes: memory.heapUsed,
|
|
837
|
+
external_bytes: memory.external,
|
|
838
|
+
array_buffers_bytes: memory.arrayBuffers,
|
|
839
|
+
heap_size_limit_bytes: heap.heap_size_limit,
|
|
840
|
+
},
|
|
841
|
+
scheduler: runtime?.runtimeEpochDiagnosticSnapshot(),
|
|
842
|
+
retained_records: runtime?.runtimeRetainedRecordCounts(),
|
|
843
|
+
behavior: {
|
|
844
|
+
...assembly.behavior.diagnosticSnapshot(),
|
|
845
|
+
effective_source: effectiveBehavior
|
|
846
|
+
? {
|
|
847
|
+
id: effectiveBehavior.id,
|
|
848
|
+
execution: effectiveBehavior.execution,
|
|
849
|
+
every_ticks: effectiveBehavior.everyTicks,
|
|
850
|
+
source: effectiveBehavior.management.source,
|
|
851
|
+
managed_by: effectiveBehavior.management.managedBy,
|
|
852
|
+
resource_id: effectiveBehavior.management.resourceId ?? null,
|
|
853
|
+
version_id: effectiveBehavior.management.versionId ?? null,
|
|
854
|
+
resolution: effectiveBehavior.management.resolution ?? null,
|
|
855
|
+
file: effectiveBehavior.sourcePath.split(/[\\/]/).at(-1) ?? null,
|
|
856
|
+
}
|
|
857
|
+
: null,
|
|
858
|
+
},
|
|
859
|
+
perception: assembly.perception.diagnosticSnapshot(),
|
|
860
|
+
trigger: assembly.trigger.diagnosticSnapshot(),
|
|
861
|
+
}, {
|
|
862
|
+
component: 'runtime.diagnostics',
|
|
863
|
+
scope: runtime?.publicScope(),
|
|
864
|
+
});
|
|
865
|
+
} catch {
|
|
866
|
+
// Full diagnostics must remain observational and never affect gameplay.
|
|
867
|
+
}
|
|
868
|
+
};
|
|
869
|
+
sample();
|
|
870
|
+
diagnosticSampleTimer = setInterval(sample, FULL_DIAGNOSTIC_SAMPLE_INTERVAL_MS);
|
|
871
|
+
diagnosticSampleTimer.unref?.();
|
|
872
|
+
}
|
|
811
873
|
if (!isMailboxRepository(runtime.repository)) {
|
|
812
874
|
throw new Error('The configured Runtime repository does not support durable Mailbox delivery.');
|
|
813
875
|
}
|
|
@@ -1399,6 +1461,7 @@ export async function runClawclawOwner(
|
|
|
1399
1461
|
}
|
|
1400
1462
|
return completion;
|
|
1401
1463
|
} finally {
|
|
1464
|
+
if (diagnosticSampleTimer) clearInterval(diagnosticSampleTimer);
|
|
1402
1465
|
process.off('SIGINT', onSignal);
|
|
1403
1466
|
process.off('SIGTERM', onSignal);
|
|
1404
1467
|
if (forceExitTimer) clearTimeout(forceExitTimer);
|
|
@@ -37,6 +37,8 @@ export interface ClawclawPluginAssemblyOptions {
|
|
|
37
37
|
epochPlugins?: readonly RuntimeEpochPlugin<Record<string, any>, Record<string, unknown>>[];
|
|
38
38
|
perception?: ClawclawPerceptionConfig;
|
|
39
39
|
behaviorCatalog?: Pick<ClawclawBehaviorCatalog, 'has' | 'prepare'>;
|
|
40
|
+
behaviorDiagnosticsEnabled?: boolean;
|
|
41
|
+
triggerDiagnosticsEnabled?: boolean;
|
|
40
42
|
fallbackBehaviors?: ClawclawRoleFallbackBehaviors;
|
|
41
43
|
triggers?: readonly ClawclawTriggerRegistration[];
|
|
42
44
|
/** All enabled Behavior Event declarations frozen into the startup plugin graph. */
|
|
@@ -106,6 +108,7 @@ export function assembleClawclawPlugins(
|
|
|
106
108
|
?? new ClawclawTriggerPlugin({
|
|
107
109
|
after: [],
|
|
108
110
|
triggers: options.triggers,
|
|
111
|
+
diagnosticsEnabled: options.triggerDiagnosticsEnabled,
|
|
109
112
|
});
|
|
110
113
|
if (!configuredTrigger) plugins.push(trigger);
|
|
111
114
|
|
|
@@ -125,6 +128,7 @@ export function assembleClawclawPlugins(
|
|
|
125
128
|
CLAWCLAW_TRIGGER_PLUGIN_ID,
|
|
126
129
|
],
|
|
127
130
|
eventTypes: options.behaviorEventTypes,
|
|
131
|
+
diagnosticsEnabled: options.behaviorDiagnosticsEnabled,
|
|
128
132
|
});
|
|
129
133
|
if (!configuredBehavior) plugins.push(behavior);
|
|
130
134
|
|