@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.
Files changed (44) hide show
  1. package/docs/custom-modules/perception.md +4 -4
  2. package/docs/runtime/README.md +1 -1
  3. package/docs/runtime/behavior-runtime.md +1 -1
  4. package/docs/runtime/official-perception-modules.md +23 -30
  5. package/docs/runtime/perception-replay.md +1 -1
  6. package/package.json +1 -1
  7. package/skills/clawclaw/SKILL.md +5 -1
  8. package/skills/clawclaw/references/BEHAVIORS.md +16 -1
  9. package/skills/clawclaw/references/CUSTOM-BEHAVIOR-DEVELOPMENT.md +310 -0
  10. package/skills/clawclaw/references/CUSTOM-MODULE-API.md +684 -0
  11. package/skills/clawclaw/references/CUSTOM-MODULES.md +125 -0
  12. package/skills/clawclaw/references/CUSTOM-PERCEPTION-DEVELOPMENT.md +331 -0
  13. package/src/runtime/clawclaw/behavior-decision.ts +4 -1
  14. package/src/runtime/clawclaw/owner.ts +64 -1
  15. package/src/runtime/clawclaw/plugins/assembly.ts +4 -0
  16. package/src/runtime/clawclaw/plugins/behavior/behavior-analysis-thread-host.ts +35 -2
  17. package/src/runtime/clawclaw/plugins/behavior/behavior-catalog.ts +11 -7
  18. package/src/runtime/clawclaw/plugins/behavior/behavior-normalization.ts +3 -2
  19. package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.ts +46 -50
  20. package/src/runtime/clawclaw/plugins/behavior/behavior-runner.ts +39 -9
  21. package/src/runtime/clawclaw/plugins/behavior/thread-behavior-executor.ts +167 -7
  22. package/src/runtime/clawclaw/plugins/perception/module-runner.ts +86 -89
  23. package/src/runtime/clawclaw/plugins/perception/perception-analysis-thread-pool.ts +157 -41
  24. package/src/runtime/clawclaw/plugins/perception/perception-analysis-thread-worker.ts +28 -4
  25. package/src/runtime/clawclaw/plugins/perception/perception-module-loader.ts +3 -1
  26. package/src/runtime/clawclaw/plugins/perception/perception-module-output.ts +30 -0
  27. package/src/runtime/clawclaw/plugins/perception/perception-plugin.ts +114 -17
  28. package/src/runtime/clawclaw/plugins/perception/perception-reducer-thread-pool.ts +660 -0
  29. package/src/runtime/clawclaw/plugins/perception/perception-reducer-thread-worker.ts +262 -0
  30. package/src/runtime/clawclaw/plugins/trigger/trigger-module-loader.ts +41 -12
  31. package/src/runtime/clawclaw/plugins/trigger/trigger-plugin.ts +252 -20
  32. package/src/runtime/clawclaw/plugins/trigger/trigger-thread-pool.ts +496 -0
  33. package/src/runtime/clawclaw/plugins/trigger/trigger-thread-worker.ts +164 -0
  34. package/src/runtime/clawclaw-event-catalog.ts +1 -0
  35. package/src/runtime/framework/events/memory-event-repository.ts +12 -0
  36. package/src/runtime/framework/modules/runtime-module-output.ts +24 -0
  37. package/src/runtime/framework/persistence/memory-first-persistence.ts +3 -0
  38. package/src/runtime/framework/plugins/contracts.ts +3 -0
  39. package/src/runtime/framework/plugins/runtime-input-journal.ts +12 -0
  40. package/src/runtime/framework/plugins/runtime-plugin-scheduler.ts +150 -23
  41. package/src/runtime/framework/runtime.ts +18 -0
  42. package/src/runtime/framework/state/history/memory-state-history-repository.ts +10 -0
  43. package/src/runtime/observability/redaction.ts +28 -1
  44. package/src/sdk/perception-plugin/index.ts +13 -8
@@ -0,0 +1,684 @@
1
+ # 自定义模块公共 API 参考
2
+
3
+ 这是一份查接口、字段和约束的参考手册,不重复讲模块生命周期、执行模式选择或完整开发流程。需要教程时分别阅读:
4
+
5
+ - [开发 Behavior](./CUSTOM-BEHAVIOR-DEVELOPMENT.md)
6
+ - [开发 Perception](./CUSTOM-PERCEPTION-DEVELOPMENT.md)
7
+ - [自定义模块总览](./CUSTOM-MODULES.md)
8
+
9
+ 本文按模块组织;每个模块内部依次说明 Runtime 主动输入、只读查询 API、允许输出和 Manifest。两类模块共用的数据结构统一放在文末。
10
+
11
+ | 模块 | Runtime 主动输入 | 只读查询 | 允许输出 |
12
+ |---|---|---|---|
13
+ | Behavior | 当前 `GameState`、待处理 Event、Activation 参数 | 地图、路径距离、最近观察 | Action、Status、自定义 Event、结束结果 |
14
+ | Perception | `ObservationFrame` | 静态地图 | 感知 Event |
15
+
16
+ 所有输入和查询结果都是当前回调内的只读值。任何模块都不能直接修改权威 `GameState`。
17
+
18
+ ## Behavior API
19
+
20
+ 导入入口与当前版本:
21
+
22
+ ```ts
23
+ import {
24
+ BEHAVIOR_PLUGIN_API_VERSION,
25
+ ClawclawBehavior,
26
+ } from '@myclaw163/clawclaw-cli/behavior-plugin';
27
+
28
+ // BEHAVIOR_PLUGIN_API_VERSION === 3
29
+ ```
30
+
31
+ ### Runtime 输入:`BehaviorInput<TInput>`
32
+
33
+ ```ts
34
+ interface BehaviorInput<TInput extends Readonly<Record<string, unknown>>> {
35
+ readonly epochId: number;
36
+ readonly scope: Readonly<{ sessionId: string; segmentId: string }>;
37
+ readonly nowMs: number;
38
+ readonly gameState: Readonly<GameState>;
39
+ readonly events: readonly Readonly<GameEvent>[];
40
+ readonly input: Readonly<TInput>;
41
+ readonly continuation?: Readonly<BehaviorContinuation>;
42
+ }
43
+ ```
44
+
45
+ | 字段 | 含义 |
46
+ |---|---|
47
+ | `epochId` | Runtime 输入序号;不是后端游戏 tick |
48
+ | `scope` | 当前 Runtime session 与 segment |
49
+ | `nowMs` | Runtime 接受本轮输入的时间,适合确定性的超时计算 |
50
+ | `gameState` | 当前玩家视角的权威 GameSync 快照 |
51
+ | `events` | 该 Activation 尚未消费的有序 Event 片段 |
52
+ | `input` | 按 Manifest 校验并经可选 `normalize()` 处理后的 Activation 参数 |
53
+ | `continuation` | 仅 long 委派的 short 结束并恢复后出现一次;包含 Runtime 生成的 child 终态 |
54
+
55
+ `events` 在 Behavior 真正执行一轮时才消费;因 `everyTicks` 跳过的输入不会丢失 Event。
56
+
57
+ ### 查询:`BehaviorReadApi`
58
+
59
+ ```ts
60
+ interface BehaviorReadApi {
61
+ readonly spatial: SpatialApi;
62
+ readonly navigation: {
63
+ pathDistance(from: Readonly<Position>, to: Readonly<Position>): Promise<number | null>;
64
+ pathDistances(
65
+ queries: readonly Readonly<{ from: Readonly<Position>; to: Readonly<Position> }>[]
66
+ ): Promise<readonly (number | null)[]>;
67
+ };
68
+ readonly observation: {
69
+ lastSeenPlayer(targetSeat: number, maxAgeMs?: number):
70
+ Readonly<ObservedPlayer> | undefined;
71
+ lastSeenCorpse(targetSeat?: number):
72
+ Readonly<ObservedCorpse> | undefined;
73
+ };
74
+ }
75
+ ```
76
+
77
+ #### `api.spatial`
78
+
79
+ ```ts
80
+ interface SpatialApi {
81
+ readonly xPositive: 'right';
82
+ readonly yPositive: 'down';
83
+ readonly playerBodyLengthPx: number;
84
+ readonly rooms: readonly Readonly<SpatialRoom>[];
85
+ roomAt(x: number, y: number): Readonly<SpatialRoom> | null;
86
+ sideInRoom(position: Readonly<SpatialPosition>): string | null;
87
+ placeLabel(position: Readonly<SpatialPosition>): string;
88
+ transitionIsMapped(from: string | null, to: string | null): boolean;
89
+ }
90
+
91
+ interface SpatialPosition extends Position {
92
+ readonly room: string | null;
93
+ }
94
+
95
+ interface SpatialRoom {
96
+ readonly name: string;
97
+ readonly polygon: readonly (readonly [number, number])[];
98
+ readonly bbox: Readonly<{ left: number; top: number; right: number; bottom: number }>;
99
+ }
100
+ ```
101
+
102
+ 这组接口只提供静态地图事实与确定性的空间派生,不会暴露隐藏玩家或其他动态状态。
103
+
104
+ #### `api.navigation.pathDistance()`
105
+
106
+ ```ts
107
+ const distance = await api.navigation.pathDistance(from, to);
108
+ ```
109
+
110
+ - `from`、`to`:`{ x: number, y: number }`;
111
+ - 返回本地导航网格的路径距离;不可达或无法求值时返回 `null`;
112
+ - 结果不承诺等于服务器最终路线,也不包含玩家等动态障碍。
113
+
114
+ #### `api.navigation.pathDistances()`
115
+
116
+ ```ts
117
+ const distances = await api.navigation.pathDistances([
118
+ { from, to: targetA },
119
+ { from, to: targetB },
120
+ ]);
121
+ ```
122
+
123
+ 批量结果与查询数组一一对应;每一项都是 `number | null`。
124
+
125
+ #### `api.observation.lastSeenPlayer()`
126
+
127
+ ```ts
128
+ interface ObservedPlayer extends Position {
129
+ readonly name: string;
130
+ readonly seat: number;
131
+ readonly room?: string;
132
+ readonly distance?: number;
133
+ readonly isAlive?: boolean;
134
+ readonly observedAtMs: number;
135
+ readonly ageMs: number;
136
+ }
137
+
138
+ const player = api.observation.lastSeenPlayer(targetSeat, maxAgeMs);
139
+ ```
140
+
141
+ - `targetSeat`:`1..10` 的数字座位;
142
+ - `maxAgeMs`:可选的最大观察年龄;
143
+ - 没有符合条件的记录时返回 `undefined`。
144
+
145
+ #### `api.observation.lastSeenCorpse()`
146
+
147
+ ```ts
148
+ interface ObservedCorpse extends Position {
149
+ readonly id: string;
150
+ readonly name?: string;
151
+ readonly seat?: number;
152
+ readonly room?: string;
153
+ readonly distance?: number;
154
+ readonly observedAtMs: number;
155
+ readonly ageMs: number;
156
+ }
157
+
158
+ const corpse = api.observation.lastSeenCorpse(targetSeat);
159
+ ```
160
+
161
+ 省略 `targetSeat` 时返回最近观察到的尸体;没有记录时返回 `undefined`。最近观察是历史事实,使用前必须结合 `ageMs` 与当前 `gameState` 复核。
162
+
163
+ ### 输出:`BehaviorDecision`
164
+
165
+ ```ts
166
+ interface BehaviorDecision {
167
+ readonly actions?: readonly Readonly<GameAction>[];
168
+ readonly status?: Readonly<BehaviorStatus>;
169
+ readonly events?: readonly Readonly<BehaviorEventProposal>[];
170
+ readonly delegate?: Readonly<BehaviorDelegation>;
171
+ readonly finish?: Readonly<BehaviorFinish>;
172
+ }
173
+ ```
174
+
175
+ 五个字段都可省略。返回 `undefined` 或空对象表示本轮没有新效果,不会自动结束 Activation。Decision 不接受未声明字段,并且必须能被 `structuredClone`。
176
+
177
+ #### Action 通用规则
178
+
179
+ - 每个 Action 都可带可选的 `thinking_content?: string`;
180
+ - Action 只是一项请求,仍会经过控制权、阶段、角色和安全检查;
181
+ - 未列出的字段会被拒绝,不会透传给后端;
182
+ - 玩家控制目标只使用数字座位,不使用姓名;
183
+ - Behavior 不读取 HTTP ACK 或 Action receipt,应通过后续权威 State/Event 判断业务目标是否完成。
184
+
185
+ #### `move`:移动到房间
186
+
187
+ ```ts
188
+ {
189
+ action: 'move',
190
+ target: '控制室',
191
+ stop_on_player: true,
192
+ thinking_content: '前往控制室',
193
+ }
194
+ ```
195
+
196
+ `target` 必须是非空房间名;不能同时提供 `target_x`、`target_y`。
197
+
198
+ #### `move`:移动到坐标
199
+
200
+ ```ts
201
+ {
202
+ action: 'move',
203
+ target_x: 640,
204
+ target_y: 360,
205
+ stop_on_player: false,
206
+ thinking_content: '靠近目标位置',
207
+ }
208
+ ```
209
+
210
+ `target_x`、`target_y` 必须同时存在且为有限数字;不能同时提供 `target`。
211
+
212
+ #### `kill`
213
+
214
+ ```ts
215
+ {
216
+ action: 'kill',
217
+ target_seat: 3,
218
+ thinking_content: '攻击 3 号',
219
+ }
220
+ ```
221
+
222
+ `target_seat` 必须是 `1..10` 的整数。Behavior 应先自行追踪目标、复核可见性、距离、角色能力和冷却,再提交击杀请求,并从后续 State/Event 确认结果。
223
+
224
+ #### `speech`
225
+
226
+ ```ts
227
+ {
228
+ action: 'speech',
229
+ text: '我在控制室看到可疑移动。',
230
+ thinking_content: '分享观察',
231
+ }
232
+ ```
233
+
234
+ `text` 必须是非空字符串。
235
+
236
+ #### `task`
237
+
238
+ ```ts
239
+ { action: 'task' }
240
+ // 或
241
+ { action: 'task', task_name: '校准航行路线' }
242
+ ```
243
+
244
+ `task_name` 可省略;提供时必须是非空字符串。任务开始不等于完成,完成条件应读取任务状态或 `task_completed` Event。蟹破坏任务同样使用 `task`:完成后服务端自动触发对应紧急任务;被队友打断时会收到私有 `task_interrupted`。
245
+
246
+ #### `report`
247
+
248
+ ```ts
249
+ { action: 'report' }
250
+ ```
251
+
252
+ `report` 没有目标字段;后端报告当前范围内可报告的尸体。Behavior 可用尸体座位选择导航目标,但不能把座位或姓名放进 Action payload。
253
+
254
+ #### `think`
255
+
256
+ ```ts
257
+ { action: 'think', thinking_content: '等待更多信息' }
258
+ ```
259
+
260
+ 会议投票不属于 `GameAction`。Agent 或用户在会议阶段使用现有 `ccl vote`,由 Action Plugin 直接提交后端;Behavior 不预占 `meeting_vote` 控制域。
261
+
262
+ #### `BehaviorStatus`
263
+
264
+ ```ts
265
+ interface BehaviorStatus {
266
+ readonly summary: string;
267
+ readonly activity?: string;
268
+ readonly room?: string;
269
+ readonly task?: string;
270
+ readonly targetPlayer?: string;
271
+ readonly data?: Readonly<Record<string, unknown>>;
272
+ }
273
+ ```
274
+
275
+ `summary` 必须是非空字符串,其余文本字段提供时也必须非空。Status 会发布到 Runtime 的 `behavior.status` 视图;它不是 `GameState` patch,也不是模块私有持久化存储。
276
+
277
+ #### `BehaviorEventProposal`
278
+
279
+ ```ts
280
+ interface BehaviorEventProposal {
281
+ readonly type: string;
282
+ readonly payload: Readonly<Record<string, unknown>>;
283
+ readonly occurredAt?: string;
284
+ }
285
+ ```
286
+
287
+ `type` 必须先列在 `manifest.produces` 中;`payload` 必须是普通 JSON 对象。Event 应表示模块已经确认成立的事实。
288
+
289
+ #### `BehaviorDelegation`
290
+
291
+ ```ts
292
+ interface BehaviorDelegation {
293
+ readonly behavior: string;
294
+ readonly input?: Readonly<Record<string, unknown>>;
295
+ }
296
+ ```
297
+
298
+ 只有当前 long Activation 可以使用 `delegate` 启动一个 short。Runtime 会先通过 Behavior Catalog 校验目标与输入,再改变控制栈。`delegate` 不能与 `actions` 或 `finish` 同时返回;short 使用会让当前 Activation failed。委派 short 的终态通过 long 恢复后的 `continuation.delegatedBehavior` 一次性返回。
299
+
300
+ #### `BehaviorFinish`
301
+
302
+ ```ts
303
+ interface BehaviorFinish {
304
+ readonly outcome: 'completed' | 'failed';
305
+ readonly reason: string;
306
+ readonly summary?: string;
307
+ }
308
+ ```
309
+
310
+ `reason` 必须是非空、稳定、适合程序判断的原因。Runtime 不允许同一 Decision 同时首次提交 Action 并 `finish` 或 `delegate`;应先请求 Action,再在后续权威输入确认目标后结束或委派。
311
+
312
+ ### Manifest:`ClawclawBehaviorManifest`
313
+
314
+ | 字段 | 类型 | 约束 |
315
+ |---|---|---|
316
+ | `apiVersion` | `3` | 必须等于 `BEHAVIOR_PLUGIN_API_VERSION` |
317
+ | `id` | `string` | 稳定且唯一 |
318
+ | `name` / `description` | `string` | 非空展示文本 |
319
+ | `localizations` | object,可选 | 本地化资源文本 |
320
+ | `inputs` | `BehaviorInputFieldSpec[]` | Activation 参数 schema |
321
+ | `execution` | `'parallel' \| 'serial'`,可选 | 省略为 parallel |
322
+ | `everyTicks` | `number`,可选 | 正整数;第一次 eligible tick 总会运行 |
323
+ | `produces` | `string[]`,可选 | 允许发布的自定义 Event type |
324
+ | `runtime` | `BehaviorRuntimeDefaults`,可选 | finite Activation 的默认运行预算 |
325
+
326
+ ```ts
327
+ interface BehaviorRuntimeDefaults {
328
+ readonly defaultMs: number;
329
+ readonly deadlineOutcome?: 'completed' | 'failed';
330
+ readonly controlGraceMs?: number;
331
+ }
332
+ ```
333
+
334
+ `defaultMs` 必须是 `1000..120000` 的安全整数;`controlGraceMs` 必须是 `0..30000` 的安全整数。
335
+
336
+ ### 参数字段:`BehaviorInputFieldSpec`
337
+
338
+ 每个字段都必须包含 `id`、`type` 和非空 `description`。`id` 必须匹配 `[A-Za-z][A-Za-z0-9_.-]{0,79}`。
339
+
340
+ | `type` | 值 | 可用约束 |
341
+ |---|---|---|
342
+ | `string` | 非空字符串 | `required`、`default` |
343
+ | `number` | 有限数字 | `required`、`default`、`min`、`max` |
344
+ | `boolean` | `true` / `false` | `required`、`default` |
345
+ | `enum` | `values` 中的字符串 | `required`、`default`、非空且不重复的 `values` |
346
+ | `string_list` | 字符串数组;每项非空 | `required`、`default`、`maxItems` |
347
+ | `player_seat` | `1..10` 的整数 | `required`、`default` |
348
+ | `player_seats` | 每项为 `1..10` 整数的数组 | `required`、`default`、`maxItems` |
349
+
350
+ `required: true` 不能同时声明 `default`。Runtime 不做字符串到数字等隐式转换;Activation 输入中的未知字段会被拒绝。
351
+ `string_list` 或 `player_seats` 标记为 required 时,数组本身也不能为空。
352
+
353
+ ### 可选参数整理:`BehaviorNormalizationApi`
354
+
355
+ `normalize(input, api)` 只在一次 Activation 启动前调用一次。它可以使用:
356
+
357
+ ```ts
358
+ interface BehaviorNormalizationApi {
359
+ readonly manifest: Readonly<ClawclawBehaviorManifest>;
360
+ timing(input: Readonly<Record<string, unknown>>):
361
+ { readonly timeout_ms?: number; readonly duration_ms?: number };
362
+ string(value: unknown, field: string): string;
363
+ optionalString(value: unknown, field: string): string | undefined;
364
+ number(
365
+ value: unknown,
366
+ field: string,
367
+ constraints?: Readonly<{ min?: number; max?: number; integer?: boolean }>,
368
+ ): number;
369
+ stringArray(
370
+ value: unknown,
371
+ field: string,
372
+ constraints?: Readonly<{ maxItems?: number }>,
373
+ ): readonly string[];
374
+ choice<T extends string>(value: unknown, field: string, values: readonly T[]): T;
375
+ }
376
+ ```
377
+
378
+ 这里没有动态 `GameState` 或 Action 能力;动态判断放在 `decide()` 中。
379
+
380
+ ## Perception API
381
+
382
+ 导入入口与当前版本:
383
+
384
+ ```ts
385
+ import {
386
+ PERCEPTION_PLUGIN_API_VERSION,
387
+ ClawclawPerception,
388
+ ClawclawParallelPerception,
389
+ } from '@myclaw163/clawclaw-cli/perception-plugin';
390
+
391
+ // PERCEPTION_PLUGIN_API_VERSION === 2
392
+ ```
393
+
394
+ ### `PerceptionFrame`
395
+
396
+ `PerceptionFrame` 是共享 `ObservationFrame` 的类型别名,字段见[共享观察帧](#共享观察帧)。普通 Perception 的入口是:
397
+
398
+ ```ts
399
+ update(
400
+ frame: Readonly<PerceptionFrame>,
401
+ api: PerceptionApi,
402
+ ): readonly PerceptionEventProposal[] | void | Promise<readonly PerceptionEventProposal[] | void>;
403
+ ```
404
+
405
+ 需要 Worker 分析时继承 `ClawclawParallelPerception<TAnalysis>`:
406
+
407
+ ```ts
408
+ interface PerceptionAnalysisInput {
409
+ readonly frame: Readonly<PerceptionFrame>;
410
+ readonly history: readonly Readonly<PerceptionFrame>[];
411
+ readonly gapBefore: boolean;
412
+ }
413
+
414
+ analyze(input: Readonly<PerceptionAnalysisInput>, api: PerceptionApi): TAnalysis | Promise<TAnalysis>;
415
+
416
+ commit(
417
+ frame: Readonly<PerceptionFrame>,
418
+ analysis: Readonly<TAnalysis>,
419
+ api: PerceptionApi,
420
+ ): readonly PerceptionEventProposal[] | void | Promise<readonly PerceptionEventProposal[] | void>;
421
+ ```
422
+
423
+ `history` 只包含旧帧,按时间升序且不包含当前 `frame`;整个窗口最多 10 帧。`gapBefore` 表示已存在前帧,但无法证明轨迹连续。`update()` 与有序 `commit()` 都在持久 Worker reducer lane 中执行,不依赖 Owner 主线程全局状态。执行模型与状态放置规则见[开发 Perception](./CUSTOM-PERCEPTION-DEVELOPMENT.md)。
424
+
425
+ ### 查询:`PerceptionApi`
426
+
427
+ ```ts
428
+ interface PerceptionApi {
429
+ readonly spatial: SpatialApi;
430
+ }
431
+ ```
432
+
433
+ `SpatialApi` 与 Behavior 的 `api.spatial` 相同。Perception 不提供 navigation 或 observation 查询;它应只根据当前/历史观察帧和静态地图形成视觉事实。
434
+
435
+ ### 输出:`PerceptionEventProposal`
436
+
437
+ ```ts
438
+ interface PerceptionEventProposal {
439
+ readonly type: string;
440
+ readonly dedupeKey: string;
441
+ readonly dedupeScope?: 'segment' | 'session';
442
+ readonly payload: Readonly<Record<string, unknown>>;
443
+ readonly occurredAt?: string;
444
+ }
445
+ ```
446
+
447
+ | 字段 | 约束 |
448
+ |---|---|
449
+ | `type` | 非空,且必须列在 `manifest.produces` |
450
+ | `dedupeKey` | 当前语义片段的稳定、非空身份 |
451
+ | `dedupeScope` | 省略时为 `segment`;可显式选 `segment` 或 `session` |
452
+ | `payload` | 普通、可 clone 的 JSON 对象 |
453
+ | `occurredAt` | 可选的事件发生时间字符串 |
454
+
455
+ Perception 不能返回 Action、Status、State patch 或 Behavior Activation。
456
+
457
+ ### 边界回调
458
+
459
+ ```ts
460
+ interface PerceptionBoundary {
461
+ readonly kind:
462
+ | 'events_requested'
463
+ | 'phase_changed'
464
+ | 'trajectory_gap'
465
+ | 'segment_ending'
466
+ | 'shutdown';
467
+ readonly mode: 'snapshot' | 'close';
468
+ readonly through: Readonly<PerceptionFrame> | null;
469
+ }
470
+
471
+ flush?(boundary: Readonly<PerceptionBoundary>, api: PerceptionApi):
472
+ readonly PerceptionEventProposal[] | void | Promise<readonly PerceptionEventProposal[] | void>;
473
+
474
+ reset?(reason: 'phase_changed' | 'trajectory_gap' | 'segment_ending' | 'shutdown' | 'replay'):
475
+ void | Promise<void>;
476
+
477
+ debugSnapshot?(): unknown;
478
+ ```
479
+
480
+ `flush()` 只在 Runtime 要求报告未关闭的长语义片段时调用;`debugSnapshot()` 仅进入诊断和离线 replay,不会成为 Agent Event。
481
+
482
+ ### Manifest:`ClawclawPerceptionManifest`
483
+
484
+ | 字段 | 类型 | 约束 |
485
+ |---|---|---|
486
+ | `apiVersion` | `2` | 必须等于 `PERCEPTION_PLUGIN_API_VERSION` |
487
+ | `id` / `name` / `description` | `string` | 稳定 ID 与非空说明 |
488
+ | `localizations` | object,可选 | 本地化资源文本 |
489
+ | `subjectScope` | `'others' \| 'self_and_others' \| 'visible_pairs' \| 'corpses'` | 描述 Event 主体范围 |
490
+ | `produces` | `string[]` | 允许输出的 Event type |
491
+ | `eventMetadata` | object,可选 | 每种 Event 的名称、说明和本地化 |
492
+ | `execution` | `'parallel_analysis'`,可选 | 启用 `analyze/commit` 接口 |
493
+ | `boundaryBaseline` | `'current_only'`,可选 | 声明边界重建能力 |
494
+
495
+ ## 共享数据结构
496
+
497
+ ### 基础引用
498
+
499
+ ```ts
500
+ interface Position {
501
+ readonly x: number;
502
+ readonly y: number;
503
+ }
504
+
505
+ interface PlayerRef {
506
+ readonly seat: number;
507
+ readonly name: string;
508
+ }
509
+ ```
510
+
511
+ 业务身份与控制目标使用 `seat`;`name` 只用于展示。
512
+
513
+ ### `GameState`
514
+
515
+ ```ts
516
+ interface GameState {
517
+ readonly game_id: string;
518
+ readonly tick: number;
519
+ readonly phase?: 'preparing' | 'wandering' | 'meeting' | 'game_over';
520
+ readonly you?: Readonly<GameSelf>;
521
+ readonly players?: readonly Readonly<KnownPlayer>[];
522
+ readonly visible_players?: readonly Readonly<VisiblePlayer>[];
523
+ readonly visible_corpses?: readonly Readonly<VisibleCorpse>[];
524
+ readonly task_progress?: Readonly<{ completed: number; goal: number }>;
525
+ readonly emergency?: Readonly<GameEmergency>;
526
+ readonly crab_sabotage?: Readonly<GameCrabSabotage>;
527
+ readonly meeting?: Readonly<GameMeeting>;
528
+ readonly octopus_time?: Readonly<GameOctopusTime>;
529
+ readonly winner?: Readonly<GameWinner>;
530
+ readonly settlement?: Readonly<Record<string, unknown>>;
531
+ }
532
+ ```
533
+
534
+ 除 `game_id` 和 `tick` 外,字段可能因阶段、角色权限或降级输入而缺失。缺失表示当前快照没有提供该事实,不等于 `false`、空数组或全场不存在。
535
+
536
+ #### `GameSelf`
537
+
538
+ | 字段 | 类型 |
539
+ |---|---|
540
+ | `player` | `PlayerRef` |
541
+ | `x` / `y` | `number` |
542
+ | `room` / `role` / `faction` | `string` |
543
+ | `is_alive` | `boolean` |
544
+ | `completed_task_count` | `number` |
545
+ | `kill_cooldown_secs` | `number` |
546
+ | `kills_remaining` | `number \| null` |
547
+ | `activity` | `GamePlayerActivity` |
548
+ | `crab_teammates` | `PlayerRef[]` |
549
+ | `tasks` | `GameTask[]` |
550
+
551
+ 所有字段都可缺失。`GamePlayerActivity.kind` 为 `idle | moving | task`,并可带 `task_name`、`remaining_secs`、`target_x`、`target_y`。
552
+
553
+ #### `GameTask`
554
+
555
+ | 字段 | 类型 | 是否必有 |
556
+ |---|---|:---:|
557
+ | `task_id` | `string` | |
558
+ | `name` / `room` | `string` | ✓ |
559
+ | `x` / `y` | `number` | ✓ |
560
+ | `is_fake_shrimp` | `boolean` | ✓ |
561
+ | `status` | `'normal' \| 'in_progress' \| 'completed'` | |
562
+ | `task_note` | `string` | |
563
+
564
+ #### 玩家与尸体
565
+
566
+ ```ts
567
+ interface KnownPlayer extends PlayerRef {
568
+ readonly known_status: {
569
+ readonly is_alive?: boolean;
570
+ readonly death_cause?: 'killed' | 'exiled';
571
+ readonly killer?:
572
+ | { readonly kind: 'player'; readonly player: Readonly<PlayerRef> }
573
+ | { readonly kind: 'system'; readonly name: string };
574
+ readonly reported_in_current_meeting?: boolean;
575
+ };
576
+ readonly tier?: string;
577
+ readonly tier_display?: string;
578
+ }
579
+
580
+ interface VisiblePlayer extends Position {
581
+ readonly player: Readonly<PlayerRef>;
582
+ readonly room: string;
583
+ readonly distance?: number;
584
+ readonly is_alive?: boolean;
585
+ }
586
+
587
+ interface VisibleCorpse extends Position {
588
+ readonly corpse_id: string;
589
+ readonly player?: Readonly<PlayerRef>;
590
+ readonly room: string;
591
+ readonly distance?: number;
592
+ }
593
+ ```
594
+
595
+ `players` 是当前有权知道的 roster/公开状态;`visible_players` 和 `visible_corpses` 才是当前真正可见对象。
596
+
597
+ #### 紧急、会议与终局
598
+
599
+ | 类型 | 字段 |
600
+ |---|---|
601
+ | `GameEmergency` | `active`;可选 `task_name`、`room`、`x`、`y`、`remaining_secs` |
602
+ | `GameCrabSabotage` | `status`;可选 `cooldown_remaining_secs` |
603
+ | `GameMeeting` | `active`;可选 `sub_phase`、`caller`、`speech_order`、`current_speaker`、`deadline_at_ms`、`server_time_ms`、`speeches`、`speech_skipped`、`votes_submitted` |
604
+ | `GameOctopusTime` | `active`;可选 `remaining_secs` |
605
+ | `GameWinner` | faction 胜利,或带具体 `player`、`role` 的 neutral 胜利 |
606
+
607
+ `GameCrabSabotage.status` 可能为 `ready`、`cooldown_charged` 或 `emergency_active`。`ready` 表示可执行自己持有的蟹破坏任务,另外两个状态都不得发起新破坏。
608
+
609
+ ### 共享观察帧
610
+
611
+ ```ts
612
+ interface ObservationFrame {
613
+ readonly frameKey: string;
614
+ readonly frameSeq: number;
615
+ readonly tick: number;
616
+ readonly timeMs: number;
617
+ readonly tickDelta: number | null;
618
+ readonly continuousFromPrevious: boolean;
619
+ readonly gameState: Readonly<GameState>;
620
+ readonly observation: Readonly<VisualObservation>;
621
+ }
622
+
623
+ interface VisualObservation {
624
+ readonly players: readonly VisualPlayer[];
625
+ readonly corpses: readonly VisualCorpse[];
626
+ readonly visibleSetComplete: boolean;
627
+ }
628
+ ```
629
+
630
+ `frameSeq` 是 CCL State History 序号,不是游戏 tick。`visibleSetComplete: false` 时,某对象未出现在本帧不能被解释为已经离开视野。
631
+
632
+ ```ts
633
+ interface VisualPlayer {
634
+ readonly id: string;
635
+ readonly name?: string;
636
+ readonly seat?: number;
637
+ readonly observer: boolean;
638
+ readonly alive?: boolean;
639
+ readonly x: number;
640
+ readonly y: number;
641
+ readonly room: string | null;
642
+ }
643
+
644
+ interface VisualCorpse {
645
+ readonly id: string;
646
+ readonly playerId?: string;
647
+ readonly name?: string;
648
+ readonly seat?: number;
649
+ readonly x: number;
650
+ readonly y: number;
651
+ readonly room: string | null;
652
+ }
653
+ ```
654
+
655
+ ### `GameEvent`
656
+
657
+ ```ts
658
+ interface GameEvent {
659
+ readonly eventId: string;
660
+ readonly epochId?: number;
661
+ readonly type: string;
662
+ readonly occurredAt: string;
663
+ readonly payload: Readonly<Record<string, unknown>>;
664
+ }
665
+ ```
666
+
667
+ 按 `type` 和结构化 `payload` 判断业务含义,不要解析面向玩家的展示文案。Event payload 由生产者定义;模块必须只消费自己识别且已校验结构的 type。官方视觉 Event 的 type、公共 payload 字段和观察边界见[开发 Perception](./CUSTOM-PERCEPTION-DEVELOPMENT.md)。
668
+
669
+ ## 公共导入与约束
670
+
671
+ ```ts
672
+ import type {
673
+ BehaviorInput,
674
+ GameEvent,
675
+ GameState,
676
+ ObservationFrame,
677
+ } from '@myclaw163/clawclaw-cli/module-input';
678
+ ```
679
+
680
+ - 不要从包内 `src/...` 路径导入;它们不是公共 API;
681
+ - Manifest 关键字段必须是可静态扫描的字面量;
682
+ - 输入、输出、Worker 分析值和 `debugSnapshot()` 返回值必须支持 `structuredClone`;
683
+ - 查询 API 只在当前回调活动期间有效,不要保存后延迟调用;
684
+ - 自定义模块是本机可信代码,不是安全沙箱,只安装可信来源。