@myclaw163/clawclaw-cli 0.7.5 → 0.7.7
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/desktop-event-push.md +154 -0
- package/docs/runtime/README.md +1 -0
- package/docs/runtime/official-perception-modules.md +346 -0
- package/package.json +2 -1
- package/src/commands/game/index.test.ts +167 -1
- package/src/commands/game/index.ts +125 -36
- package/src/lib/game-client.test.ts +15 -0
- package/src/lib/game-client.ts +4 -0
- package/src/lib/participation-status.ts +113 -0
- package/src/lib/service-status.test.ts +56 -0
- package/src/lib/service-status.ts +111 -0
- package/src/pipeline/event-format.test.ts +30 -0
- package/src/pipeline/event-format.ts +26 -1
- package/src/pipeline/event-hints.test.ts +18 -3
- package/src/pipeline/event-hints.ts +11 -0
- package/src/pipeline/game-sync-event-normalization.test.ts +58 -1
- package/src/pipeline/game-sync-event-normalization.ts +51 -0
- package/src/runtime/clawclaw/agent-event-policy.test.ts +4 -0
- package/src/runtime/clawclaw/agent-event-policy.ts +1 -0
- package/src/runtime/clawclaw/owner.ts +62 -3
- package/src/runtime/clawclaw/plugins/mailbox/agent-mailbox-coordinator.test.ts +189 -0
- package/src/runtime/clawclaw/plugins/mailbox/agent-mailbox-coordinator.ts +532 -0
- package/src/runtime/clawclaw/plugins/mailbox/desktop-event-push.integration.test.ts +363 -0
- package/src/runtime/clawclaw/plugins/mailbox/desktop-event-socket.test.ts +236 -0
- package/src/runtime/clawclaw/plugins/mailbox/desktop-event-socket.ts +411 -0
- package/src/runtime/clawclaw/plugins/mailbox/index.ts +3 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-agent-event-batch.test.ts +166 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-event-batch-delivery.test.ts +194 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-event-batch-delivery.ts +262 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-plugin.ts +28 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-service.test.ts +49 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-service.ts +77 -259
- package/src/runtime/clawclaw/plugins/perception/contracts.ts +4 -0
- package/src/runtime/clawclaw/plugins/perception/modules/co-movement-module.ts +6 -0
- package/src/runtime/clawclaw/plugins/perception/modules/follow-module.ts +6 -0
- package/src/runtime/clawclaw/plugins/perception/modules/motion-module.ts +6 -0
- package/src/runtime/clawclaw/plugins/perception/modules/route-module.ts +6 -0
- package/src/runtime/clawclaw/plugins/perception/modules/spatial-module.ts +6 -0
- package/src/runtime/clawclaw/plugins/perception/modules/stop-module.ts +6 -0
- package/src/runtime/clawclaw/plugins/perception/modules/visibility-module.ts +6 -0
- package/src/runtime/clawclaw/plugins/perception/perception-plugin.test.ts +11 -0
- package/src/runtime/clawclaw/plugins/perception/perception-plugin.ts +8 -0
- package/src/runtime/clawclaw/plugins/perception/perception-source.test.ts +12 -0
- package/src/runtime/clawclaw/plugins/perception/perception-source.ts +37 -1
- package/src/runtime/clawclaw/plugins/runtime-plugins.test.ts +5 -0
- package/src/runtime/clawclaw/plugins/source/room-transport.ts +2 -1
- package/src/runtime/clawclaw/plugins/source/source-plugin.test.ts +201 -1
- package/src/runtime/clawclaw/plugins/source/source-plugin.ts +113 -24
- package/src/runtime/clawclaw/plugins/source/types.ts +1 -0
- package/src/runtime/clawclaw/plugins/strategy/examples/task-only-strategy.ts +6 -0
- package/src/runtime/clawclaw/plugins/strategy/strategy-executor-types.ts +4 -0
- package/src/runtime/clawclaw/plugins/strategy/strategy-module-loader.ts +4 -0
- package/src/runtime/clawclaw/plugins/strategy/strategy-plugin.test.ts +36 -0
- package/src/runtime/clawclaw/plugins/strategy/strategy-plugin.ts +13 -0
- package/src/runtime/clawclaw/plugins/strategy/strategy-source.test.ts +22 -0
- package/src/runtime/clawclaw/plugins/strategy/strategy-source.ts +46 -16
- package/src/runtime/clawclaw-event-catalog.test.ts +2 -0
- package/src/runtime/clawclaw-event-catalog.ts +1 -0
- package/src/sdk/perception-plugin/index.ts +14 -0
- package/src/sdk/resource-metadata.test.ts +40 -0
- package/src/sdk/resource-metadata.ts +63 -0
- package/src/sdk/strategy-plugin/index.ts +14 -0
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
import { randomUUID } from 'node:crypto';
|
|
2
1
|
import {
|
|
3
2
|
presentCanonicalEventForCclEvents,
|
|
4
3
|
} from '../../../../pipeline/canonical-event-presentation.js';
|
|
5
4
|
import { compactStateForEvents } from '../../../../pipeline/event-format.js';
|
|
6
5
|
import {
|
|
7
6
|
DEFAULT_EVENT_CONSUMER_ID,
|
|
8
|
-
type CanonicalEvent,
|
|
9
|
-
type ConsumerLease,
|
|
10
7
|
type EventRepository,
|
|
11
8
|
} from '../../../framework/events/index.js';
|
|
12
9
|
import {
|
|
@@ -15,21 +12,25 @@ import {
|
|
|
15
12
|
} from '../../../framework/mailbox/index.js';
|
|
16
13
|
import type { RuntimeReadyView } from '../../../framework/runtime.js';
|
|
17
14
|
import { clawclawMonitorDelayMs } from '../../agent-event-policy.js';
|
|
15
|
+
import { AgentMailboxCoordinator } from './agent-mailbox-coordinator.js';
|
|
18
16
|
import {
|
|
19
17
|
composeClawclawMonitorNotification,
|
|
20
18
|
monitorItemPayload,
|
|
21
19
|
} from './mailbox-presentation.js';
|
|
22
20
|
|
|
23
|
-
const EVENTS_LEASE_TTL_MS = 30_000;
|
|
24
|
-
const EVENTS_PREPARE_WAIT_MS = 5_000;
|
|
25
21
|
const MONITOR_FORMATTER_VERSION = 'clawclaw.monitor.v2';
|
|
22
|
+
const CCL_EVENTS_LEASE_TTL_MS = 30_000;
|
|
26
23
|
|
|
27
24
|
type ClawclawMailboxRepository = EventRepository & MailboxRepository;
|
|
28
25
|
|
|
29
26
|
export interface ClawclawMailboxServiceOptions {
|
|
30
27
|
repository: ClawclawMailboxRepository;
|
|
31
28
|
readyView: () => RuntimeReadyView;
|
|
29
|
+
/** One shared coordinator must be injected when a push adapter is active. */
|
|
30
|
+
agentMailbox?: AgentMailboxCoordinator;
|
|
32
31
|
consumerId?: string;
|
|
32
|
+
/** Disable the legacy short Monitor adapter for this Owner binding. */
|
|
33
|
+
monitorEnabled?: boolean;
|
|
33
34
|
now?: () => number;
|
|
34
35
|
diagnostic?: (name: string, attributes?: Record<string, unknown>) => void;
|
|
35
36
|
}
|
|
@@ -44,81 +45,11 @@ export interface PreparedClawclawEventsResponse {
|
|
|
44
45
|
deliveryToken?: string;
|
|
45
46
|
}
|
|
46
47
|
|
|
47
|
-
interface PreparedDelivery {
|
|
48
|
-
lease: ConsumerLease;
|
|
49
|
-
eventIds: string[];
|
|
50
|
-
readyEpochSeq: number;
|
|
51
|
-
sessionId: string;
|
|
52
|
-
segmentIds: string[];
|
|
53
|
-
}
|
|
54
|
-
|
|
55
48
|
interface MonitorDelayWindow {
|
|
56
49
|
scopeKey: string;
|
|
57
50
|
deadlineMs: number;
|
|
58
51
|
}
|
|
59
52
|
|
|
60
|
-
function nonEmptyString(value: unknown): string | undefined {
|
|
61
|
-
return typeof value === 'string' && value.trim().length > 0 ? value : undefined;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function compactStrategyStateForEvents(
|
|
65
|
-
value: unknown,
|
|
66
|
-
runtime: string | undefined,
|
|
67
|
-
lastError: string | undefined,
|
|
68
|
-
phase: string | undefined,
|
|
69
|
-
alive: boolean | undefined,
|
|
70
|
-
): Record<string, unknown> | null {
|
|
71
|
-
const status = value && typeof value === 'object' && !Array.isArray(value)
|
|
72
|
-
? value as Record<string, unknown>
|
|
73
|
-
: {};
|
|
74
|
-
const id = runtime === 'not_selected' ? undefined : nonEmptyString(status.strategy);
|
|
75
|
-
const stageDoing = alive === false
|
|
76
|
-
? '角色已死亡,自动策略已停止。'
|
|
77
|
-
: phase === 'meeting'
|
|
78
|
-
? '会议阶段,自动策略已暂停。'
|
|
79
|
-
: phase === 'game_over'
|
|
80
|
-
? '本局结束,自动策略已停止。'
|
|
81
|
-
: phase === 'preparing'
|
|
82
|
-
? '等待进入游走阶段。'
|
|
83
|
-
: undefined;
|
|
84
|
-
const runtimeDoing = runtime === 'error'
|
|
85
|
-
? '策略执行出错,等待恢复。'
|
|
86
|
-
: runtime === 'not_selected'
|
|
87
|
-
? '尚未选择自动策略。'
|
|
88
|
-
: undefined;
|
|
89
|
-
const doing = stageDoing
|
|
90
|
-
?? runtimeDoing
|
|
91
|
-
?? nonEmptyString(status.summary)
|
|
92
|
-
?? (runtime === 'waiting_for_state' ? '等待最新游戏状态后继续。' : undefined);
|
|
93
|
-
const parameters = runtime !== 'not_selected' && status.parameters
|
|
94
|
-
&& typeof status.parameters === 'object'
|
|
95
|
-
&& !Array.isArray(status.parameters)
|
|
96
|
-
&& Object.keys(status.parameters).length > 0
|
|
97
|
-
? structuredClone(status.parameters)
|
|
98
|
-
: undefined;
|
|
99
|
-
const availability = status.dataAvailability
|
|
100
|
-
&& typeof status.dataAvailability === 'object'
|
|
101
|
-
&& !Array.isArray(status.dataAvailability)
|
|
102
|
-
? status.dataAvailability as Record<string, unknown>
|
|
103
|
-
: {};
|
|
104
|
-
const issue = runtime === 'error'
|
|
105
|
-
? lastError ?? 'strategy_error'
|
|
106
|
-
: runtime === 'not_selected'
|
|
107
|
-
? 'not_selected'
|
|
108
|
-
: availability.tasks === 'unavailable'
|
|
109
|
-
? 'tasks_unavailable'
|
|
110
|
-
: runtime && runtime !== 'active'
|
|
111
|
-
? runtime
|
|
112
|
-
: undefined;
|
|
113
|
-
const result = {
|
|
114
|
-
...(id ? { id } : {}),
|
|
115
|
-
...(doing ? { doing } : {}),
|
|
116
|
-
...(parameters ? { params: parameters } : {}),
|
|
117
|
-
...(issue ? { issue } : {}),
|
|
118
|
-
};
|
|
119
|
-
return Object.keys(result).length > 0 ? result : null;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
53
|
function unavailableEventsOutput(): Record<string, unknown> {
|
|
123
54
|
return {
|
|
124
55
|
schema: 'ccl.events.v5',
|
|
@@ -127,16 +58,26 @@ function unavailableEventsOutput(): Record<string, unknown> {
|
|
|
127
58
|
};
|
|
128
59
|
}
|
|
129
60
|
|
|
130
|
-
|
|
61
|
+
function cclEventsCompatibilityError(error: unknown): never {
|
|
62
|
+
if (error instanceof Error && error.message === 'agent_mailbox_delivery_busy') {
|
|
63
|
+
throw new Error('mailbox_events_delivery_busy');
|
|
64
|
+
}
|
|
65
|
+
if (error instanceof Error && error.message === 'agent_mailbox_stopped') {
|
|
66
|
+
throw new Error('clawclaw_mailbox_stopped');
|
|
67
|
+
}
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Legacy Monitor and `ccl events` adapters over the Agent Mailbox core. */
|
|
131
72
|
export class ClawclawMailboxService {
|
|
132
73
|
private readonly repository: ClawclawMailboxRepository;
|
|
133
74
|
private readonly readyView: () => RuntimeReadyView;
|
|
75
|
+
private readonly agentMailbox: AgentMailboxCoordinator;
|
|
76
|
+
private readonly ownsAgentMailbox: boolean;
|
|
134
77
|
private readonly consumerId: string;
|
|
78
|
+
private readonly monitorEnabled: boolean;
|
|
135
79
|
private readonly now: () => number;
|
|
136
80
|
private readonly diagnostic?: ClawclawMailboxServiceOptions['diagnostic'];
|
|
137
|
-
private readonly prepared = new Map<string, PreparedDelivery>();
|
|
138
|
-
private readonly preparedSlotWaiters = new Set<() => void>();
|
|
139
|
-
private prepareTail: Promise<void> = Promise.resolve();
|
|
140
81
|
private delayWindow?: MonitorDelayWindow;
|
|
141
82
|
private lastObservedReadyEpochSeq = 0;
|
|
142
83
|
private stopped = false;
|
|
@@ -145,8 +86,19 @@ export class ClawclawMailboxService {
|
|
|
145
86
|
this.repository = options.repository;
|
|
146
87
|
this.readyView = options.readyView;
|
|
147
88
|
this.consumerId = options.consumerId ?? DEFAULT_EVENT_CONSUMER_ID;
|
|
89
|
+
this.monitorEnabled = options.monitorEnabled ?? true;
|
|
148
90
|
this.now = options.now ?? Date.now;
|
|
149
91
|
this.diagnostic = options.diagnostic;
|
|
92
|
+
this.ownsAgentMailbox = !options.agentMailbox;
|
|
93
|
+
this.agentMailbox = options.agentMailbox ?? new AgentMailboxCoordinator({
|
|
94
|
+
repository: options.repository,
|
|
95
|
+
readyView: options.readyView,
|
|
96
|
+
presentEvent: presentCanonicalEventForCclEvents,
|
|
97
|
+
compactState: compactStateForEvents,
|
|
98
|
+
consumerId: this.consumerId,
|
|
99
|
+
now: this.now,
|
|
100
|
+
diagnostic: this.diagnostic,
|
|
101
|
+
});
|
|
150
102
|
}
|
|
151
103
|
|
|
152
104
|
prepareEvents(
|
|
@@ -154,11 +106,9 @@ export class ClawclawMailboxService {
|
|
|
154
106
|
readyView?: RuntimeReadyView,
|
|
155
107
|
): PreparedClawclawEventsResponse {
|
|
156
108
|
this.assertRunning();
|
|
157
|
-
this.releaseExpiredPrepared();
|
|
158
109
|
const ready = this.captureReadyView(readyView);
|
|
159
110
|
if (ready.epochSeq <= 0) return { output: unavailableEventsOutput() };
|
|
160
111
|
|
|
161
|
-
const state = this.compactReadyState(ready);
|
|
162
112
|
if (request.eventType) {
|
|
163
113
|
const event = this.repository.listCommittedEvents({
|
|
164
114
|
sessionId: ready.scope.sessionId,
|
|
@@ -176,135 +126,77 @@ export class ClawclawMailboxService {
|
|
|
176
126
|
output: {
|
|
177
127
|
schema: 'ccl.events.v5',
|
|
178
128
|
query: { type: request.eventType },
|
|
179
|
-
state,
|
|
129
|
+
state: this.agentMailbox.stateForReadyView(ready),
|
|
180
130
|
found: !!presented,
|
|
181
131
|
event: presented,
|
|
182
132
|
},
|
|
183
133
|
};
|
|
184
134
|
}
|
|
185
135
|
|
|
186
|
-
|
|
187
|
-
const lease = this.repository.acquireLease({
|
|
188
|
-
consumerId: this.consumerId,
|
|
189
|
-
channel: 'details',
|
|
190
|
-
ownerId: `mailbox-events:${randomUUID()}`,
|
|
191
|
-
ttlMs: EVENTS_LEASE_TTL_MS,
|
|
192
|
-
nowMs: this.now(),
|
|
193
|
-
});
|
|
194
|
-
if (!lease) throw new Error('mailbox_events_delivery_busy');
|
|
195
|
-
|
|
136
|
+
let prepared;
|
|
196
137
|
try {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
order: 'desc',
|
|
204
|
-
}, this.consumerId);
|
|
205
|
-
const selected = candidates.map((row) => (
|
|
206
|
-
presentCanonicalEventForCclEvents(row.event, ready.state.state)
|
|
207
|
-
));
|
|
208
|
-
const selectedRows = candidates;
|
|
209
|
-
const output = {
|
|
210
|
-
schema: 'ccl.events.v5',
|
|
211
|
-
state,
|
|
212
|
-
events: selected,
|
|
213
|
-
};
|
|
214
|
-
|
|
215
|
-
if (selectedRows.length === 0) {
|
|
216
|
-
this.repository.releaseLease(lease);
|
|
217
|
-
return { output };
|
|
218
|
-
}
|
|
219
|
-
const deliveryToken = randomUUID();
|
|
220
|
-
this.prepared.set(deliveryToken, {
|
|
221
|
-
lease,
|
|
222
|
-
eventIds: selectedRows.map((row) => row.event.eventId),
|
|
223
|
-
readyEpochSeq: ready.epochSeq,
|
|
224
|
-
sessionId: ready.scope.sessionId,
|
|
225
|
-
segmentIds: [...new Set(selectedRows.map((row) => row.event.segmentId))],
|
|
226
|
-
});
|
|
227
|
-
this.diagnostic?.('runtime.mailbox.delivery_prepared', {
|
|
228
|
-
consumer: this.consumerId,
|
|
229
|
-
delivery_token: deliveryToken,
|
|
230
|
-
session_id: ready.scope.sessionId,
|
|
231
|
-
segment_ids: [...new Set(selectedRows.map((row) => row.event.segmentId))],
|
|
232
|
-
ready_epoch_seq: ready.epochSeq,
|
|
233
|
-
event_count: selectedRows.length,
|
|
138
|
+
prepared = this.agentMailbox.prepareEvents({
|
|
139
|
+
readyView: ready,
|
|
140
|
+
bypassDelay: true,
|
|
141
|
+
deliveryVia: request.mode === 'backlog' ? 'ccl_events_backlog' : 'ccl_events',
|
|
142
|
+
ownerIdPrefix: 'ccl_events',
|
|
143
|
+
leaseTtlMs: CCL_EVENTS_LEASE_TTL_MS,
|
|
234
144
|
});
|
|
235
|
-
return { output, deliveryToken };
|
|
236
145
|
} catch (error) {
|
|
237
|
-
|
|
238
|
-
throw error;
|
|
146
|
+
return cclEventsCompatibilityError(error);
|
|
239
147
|
}
|
|
148
|
+
return {
|
|
149
|
+
output: {
|
|
150
|
+
schema: 'ccl.events.v5',
|
|
151
|
+
...prepared.batch,
|
|
152
|
+
},
|
|
153
|
+
...(prepared.deliveryToken ? { deliveryToken: prepared.deliveryToken } : {}),
|
|
154
|
+
};
|
|
240
155
|
}
|
|
241
156
|
|
|
242
|
-
/** Serializes
|
|
243
|
-
prepareEventsWhenAvailable(
|
|
157
|
+
/** Serializes pull delivery while preserving the caller's immutable Ready View. */
|
|
158
|
+
async prepareEventsWhenAvailable(
|
|
244
159
|
request: PrepareClawclawEventsRequest = {},
|
|
245
160
|
readyView?: RuntimeReadyView,
|
|
246
|
-
timeoutMs =
|
|
161
|
+
timeoutMs = 5_000,
|
|
247
162
|
): Promise<PreparedClawclawEventsResponse> {
|
|
248
|
-
if (request.eventType) return
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
163
|
+
if (request.eventType) return this.prepareEvents(request, readyView);
|
|
164
|
+
this.assertRunning();
|
|
165
|
+
const ready = this.captureReadyView(readyView);
|
|
166
|
+
if (ready.epochSeq <= 0) return { output: unavailableEventsOutput() };
|
|
167
|
+
let prepared;
|
|
168
|
+
try {
|
|
169
|
+
prepared = await this.agentMailbox.prepareEventsWhenAvailable({
|
|
170
|
+
readyView: ready,
|
|
171
|
+
bypassDelay: true,
|
|
172
|
+
deliveryVia: request.mode === 'backlog' ? 'ccl_events_backlog' : 'ccl_events',
|
|
173
|
+
ownerIdPrefix: 'ccl_events',
|
|
174
|
+
leaseTtlMs: CCL_EVENTS_LEASE_TTL_MS,
|
|
175
|
+
}, timeoutMs);
|
|
176
|
+
} catch (error) {
|
|
177
|
+
return cclEventsCompatibilityError(error);
|
|
178
|
+
}
|
|
179
|
+
return {
|
|
180
|
+
output: {
|
|
181
|
+
schema: 'ccl.events.v5',
|
|
182
|
+
...prepared.batch,
|
|
183
|
+
},
|
|
184
|
+
...(prepared.deliveryToken ? { deliveryToken: prepared.deliveryToken } : {}),
|
|
185
|
+
};
|
|
255
186
|
}
|
|
256
187
|
|
|
257
188
|
ackEvents(deliveryToken: string): boolean {
|
|
258
|
-
|
|
259
|
-
if (!prepared) return false;
|
|
260
|
-
this.prepared.delete(deliveryToken);
|
|
261
|
-
const deliveredAt = new Date(this.now()).toISOString();
|
|
262
|
-
const changed = this.repository.transitionDetails({
|
|
263
|
-
eventIds: prepared.eventIds,
|
|
264
|
-
consumerId: this.consumerId,
|
|
265
|
-
from: ['pending', 'deferred'],
|
|
266
|
-
to: 'delivered',
|
|
267
|
-
deliveredAt,
|
|
268
|
-
deliveryVia: 'ccl_events',
|
|
269
|
-
lease: prepared.lease,
|
|
270
|
-
nowMs: this.now(),
|
|
271
|
-
});
|
|
272
|
-
this.repository.releaseLease(prepared.lease);
|
|
273
|
-
this.notifyPreparedSlotAvailable();
|
|
274
|
-
const accepted = changed === prepared.eventIds.length;
|
|
275
|
-
this.diagnostic?.('runtime.mailbox.delivery_acked', {
|
|
276
|
-
consumer: this.consumerId,
|
|
277
|
-
delivery_token: deliveryToken,
|
|
278
|
-
session_id: prepared.sessionId,
|
|
279
|
-
segment_ids: prepared.segmentIds,
|
|
280
|
-
ready_epoch_seq: prepared.readyEpochSeq,
|
|
281
|
-
event_count: prepared.eventIds.length,
|
|
282
|
-
accepted,
|
|
283
|
-
});
|
|
284
|
-
return accepted;
|
|
189
|
+
return this.agentMailbox.ack(deliveryToken);
|
|
285
190
|
}
|
|
286
191
|
|
|
287
192
|
releaseEvents(deliveryToken: string): boolean {
|
|
288
|
-
|
|
289
|
-
if (!prepared) return false;
|
|
290
|
-
this.prepared.delete(deliveryToken);
|
|
291
|
-
const released = this.repository.releaseLease(prepared.lease);
|
|
292
|
-
this.notifyPreparedSlotAvailable();
|
|
293
|
-
this.diagnostic?.('runtime.mailbox.delivery_released', {
|
|
294
|
-
consumer: this.consumerId,
|
|
295
|
-
delivery_token: deliveryToken,
|
|
296
|
-
session_id: prepared.sessionId,
|
|
297
|
-
segment_ids: prepared.segmentIds,
|
|
298
|
-
ready_epoch_seq: prepared.readyEpochSeq,
|
|
299
|
-
event_count: prepared.eventIds.length,
|
|
300
|
-
released,
|
|
301
|
-
});
|
|
302
|
-
return released;
|
|
193
|
+
return this.agentMailbox.release(deliveryToken);
|
|
303
194
|
}
|
|
304
195
|
|
|
305
196
|
/** Called by the Monitor Driver at its own cadence, never by each epoch. */
|
|
306
197
|
prepareMonitor(force = false): number {
|
|
307
198
|
this.assertRunning();
|
|
199
|
+
if (!this.monitorEnabled) return 0;
|
|
308
200
|
const ready = this.captureReadyView();
|
|
309
201
|
if (ready.epochSeq <= 0) return 0;
|
|
310
202
|
const scopeKey = `${ready.scope.sessionId}:${ready.scope.segmentId}`;
|
|
@@ -378,7 +270,7 @@ export class ClawclawMailboxService {
|
|
|
378
270
|
if (this.stopped) return;
|
|
379
271
|
await this.flush();
|
|
380
272
|
this.stopped = true;
|
|
381
|
-
|
|
273
|
+
if (this.ownsAgentMailbox) await this.agentMailbox.stop();
|
|
382
274
|
}
|
|
383
275
|
|
|
384
276
|
private captureReadyView(readyView?: RuntimeReadyView): RuntimeReadyView {
|
|
@@ -395,80 +287,6 @@ export class ClawclawMailboxService {
|
|
|
395
287
|
return ready;
|
|
396
288
|
}
|
|
397
289
|
|
|
398
|
-
private compactReadyState(ready: RuntimeReadyView): Record<string, any> | null {
|
|
399
|
-
if (!ready.state.state) return null;
|
|
400
|
-
const state = compactStateForEvents(ready.state.state);
|
|
401
|
-
if (!state) return null;
|
|
402
|
-
const strategyState = ready.pluginStates['strategy.status'];
|
|
403
|
-
const strategyOutcome = ready.pluginOutcomes['clawclaw.strategy'];
|
|
404
|
-
if (!strategyState && !strategyOutcome) return state;
|
|
405
|
-
const runtime = strategyOutcome?.outcome === 'failed'
|
|
406
|
-
? 'error'
|
|
407
|
-
: strategyOutcome?.reason === 'strategy_inactive_stage'
|
|
408
|
-
? 'inactive_stage'
|
|
409
|
-
: strategyOutcome?.reason === 'strategy_waiting_for_current_state'
|
|
410
|
-
? 'waiting_for_state'
|
|
411
|
-
: strategyOutcome?.reason === 'strategy_not_selected'
|
|
412
|
-
? 'not_selected'
|
|
413
|
-
: strategyOutcome
|
|
414
|
-
? 'active'
|
|
415
|
-
: undefined;
|
|
416
|
-
const lastError = strategyOutcome?.outcome === 'failed'
|
|
417
|
-
? strategyOutcome.reason
|
|
418
|
-
: undefined;
|
|
419
|
-
const strategy = compactStrategyStateForEvents(
|
|
420
|
-
strategyState?.value,
|
|
421
|
-
runtime,
|
|
422
|
-
lastError,
|
|
423
|
-
nonEmptyString(state.phase),
|
|
424
|
-
typeof state.you?.alive === 'boolean' ? state.you.alive : undefined,
|
|
425
|
-
);
|
|
426
|
-
if (!strategy) return state;
|
|
427
|
-
return {
|
|
428
|
-
...state,
|
|
429
|
-
strategy,
|
|
430
|
-
};
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
private releaseExpiredPrepared(): void {
|
|
434
|
-
const nowMs = this.now();
|
|
435
|
-
for (const [token, prepared] of this.prepared) {
|
|
436
|
-
if (prepared.lease.expiresAt > nowMs) continue;
|
|
437
|
-
this.prepared.delete(token);
|
|
438
|
-
this.repository.releaseLease(prepared.lease);
|
|
439
|
-
}
|
|
440
|
-
this.notifyPreparedSlotAvailable();
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
private waitForPreparedSlot(timeoutMs: number): Promise<void> {
|
|
444
|
-
this.assertRunning();
|
|
445
|
-
this.releaseExpiredPrepared();
|
|
446
|
-
if (this.prepared.size === 0) return Promise.resolve();
|
|
447
|
-
const boundedTimeoutMs = Math.max(1, Math.trunc(timeoutMs));
|
|
448
|
-
return new Promise((resolve, reject) => {
|
|
449
|
-
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
450
|
-
const cleanup = (): void => {
|
|
451
|
-
if (timer) clearTimeout(timer);
|
|
452
|
-
this.preparedSlotWaiters.delete(available);
|
|
453
|
-
};
|
|
454
|
-
const available = (): void => {
|
|
455
|
-
cleanup();
|
|
456
|
-
resolve();
|
|
457
|
-
};
|
|
458
|
-
this.preparedSlotWaiters.add(available);
|
|
459
|
-
timer = setTimeout(() => {
|
|
460
|
-
cleanup();
|
|
461
|
-
reject(new Error('mailbox_events_delivery_busy'));
|
|
462
|
-
}, boundedTimeoutMs);
|
|
463
|
-
timer.unref?.();
|
|
464
|
-
});
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
private notifyPreparedSlotAvailable(): void {
|
|
468
|
-
if (this.prepared.size > 0) return;
|
|
469
|
-
for (const waiter of [...this.preparedSlotWaiters]) waiter();
|
|
470
|
-
}
|
|
471
|
-
|
|
472
290
|
private assertRunning(): void {
|
|
473
291
|
if (this.stopped) throw new Error('clawclaw_mailbox_stopped');
|
|
474
292
|
}
|
|
@@ -2,8 +2,12 @@ export {
|
|
|
2
2
|
ClawclawPerception,
|
|
3
3
|
ClawclawParallelPerception,
|
|
4
4
|
PERCEPTION_PLUGIN_API_VERSION,
|
|
5
|
+
hasValidClawclawResourceLocalizations,
|
|
6
|
+
resolveClawclawResourceMetadata,
|
|
5
7
|
} from '../../../../sdk/perception-plugin/index.js';
|
|
6
8
|
export type {
|
|
9
|
+
ClawclawResolvedResourceMetadata,
|
|
10
|
+
ClawclawResourceLocalizations,
|
|
7
11
|
ClawclawPerceptionClass,
|
|
8
12
|
ClawclawPerceptionManifest,
|
|
9
13
|
PerceptionApi,
|
|
@@ -57,6 +57,12 @@ export class CoMovementModule extends ClawclawParallelPerception<CoMovementAnaly
|
|
|
57
57
|
id: 'visual.co_movement',
|
|
58
58
|
name: 'Co-movement',
|
|
59
59
|
description: 'Reports visible player pairs moving close together in the same direction.',
|
|
60
|
+
localizations: {
|
|
61
|
+
'zh-CN': {
|
|
62
|
+
name: '共同移动',
|
|
63
|
+
description: '感知同时可见的玩家对近距离同方向稳定移动。',
|
|
64
|
+
},
|
|
65
|
+
},
|
|
60
66
|
subjectScope: 'visible_pairs',
|
|
61
67
|
produces: [
|
|
62
68
|
'visual.players_moving_together',
|
|
@@ -64,6 +64,12 @@ export class FollowModule extends ClawclawParallelPerception<FollowAnalysis> {
|
|
|
64
64
|
id: 'visual.follow',
|
|
65
65
|
name: 'Following',
|
|
66
66
|
description: 'Reports a stable front/back following relation between visible players.',
|
|
67
|
+
localizations: {
|
|
68
|
+
'zh-CN': {
|
|
69
|
+
name: '跟随关系',
|
|
70
|
+
description: '感知同时可见玩家之间稳定的前后跟随关系。',
|
|
71
|
+
},
|
|
72
|
+
},
|
|
67
73
|
subjectScope: 'visible_pairs',
|
|
68
74
|
produces: ['visual.following', 'visual.following_ended'],
|
|
69
75
|
execution: 'parallel_analysis',
|
|
@@ -80,6 +80,12 @@ export class MotionModule extends ClawclawParallelPerception<MotionAnalysis> {
|
|
|
80
80
|
id: 'visual.motion',
|
|
81
81
|
name: 'Player movement',
|
|
82
82
|
description: 'Reports movement slices and meaningful direction changes for self and visible players.',
|
|
83
|
+
localizations: {
|
|
84
|
+
'zh-CN': {
|
|
85
|
+
name: '玩家移动',
|
|
86
|
+
description: '感知自己和可见玩家的连续移动片段及显著方向变化。',
|
|
87
|
+
},
|
|
88
|
+
},
|
|
83
89
|
subjectScope: 'self_and_others',
|
|
84
90
|
produces: ['visual.player_moved', 'visual.player_motion_changed'],
|
|
85
91
|
execution: 'parallel_analysis',
|
|
@@ -60,6 +60,12 @@ export class RouteModule extends ClawclawParallelPerception<RouteAnalysis> {
|
|
|
60
60
|
id: 'visual.route',
|
|
61
61
|
name: 'Route shape',
|
|
62
62
|
description: 'Reports turns, reversals, and repeated local direction changes.',
|
|
63
|
+
localizations: {
|
|
64
|
+
'zh-CN': {
|
|
65
|
+
name: '路线形态',
|
|
66
|
+
description: '感知转弯、折返和连续多次局部方向变化。',
|
|
67
|
+
},
|
|
68
|
+
},
|
|
63
69
|
subjectScope: 'self_and_others',
|
|
64
70
|
produces: [
|
|
65
71
|
'visual.player_turned',
|
|
@@ -28,6 +28,12 @@ export class SpatialModule extends ClawclawParallelPerception<SpatialAnalysis> {
|
|
|
28
28
|
id: 'visual.spatial',
|
|
29
29
|
name: 'Spatial changes',
|
|
30
30
|
description: 'Reports verified room transitions and movement between coarse room regions.',
|
|
31
|
+
localizations: {
|
|
32
|
+
'zh-CN': {
|
|
33
|
+
name: '空间变化',
|
|
34
|
+
description: '感知已验证的房间切换和房间内粗粒度方位变化。',
|
|
35
|
+
},
|
|
36
|
+
},
|
|
31
37
|
subjectScope: 'self_and_others',
|
|
32
38
|
produces: ['visual.player_area_changed', 'visual.player_position_changed'],
|
|
33
39
|
execution: 'parallel_analysis',
|
|
@@ -46,6 +46,12 @@ export class StopModule extends ClawclawParallelPerception<StopAnalysis> {
|
|
|
46
46
|
id: 'visual.stop',
|
|
47
47
|
name: 'Player stops',
|
|
48
48
|
description: 'Reports stable stops and confirmed movement resumption.',
|
|
49
|
+
localizations: {
|
|
50
|
+
'zh-CN': {
|
|
51
|
+
name: '玩家停留',
|
|
52
|
+
description: '感知自己和可见玩家的稳定停留及确认恢复移动。',
|
|
53
|
+
},
|
|
54
|
+
},
|
|
49
55
|
subjectScope: 'self_and_others',
|
|
50
56
|
produces: [
|
|
51
57
|
'visual.player_stopped',
|
|
@@ -25,6 +25,12 @@ export class VisibilityModule extends ClawclawParallelPerception<VisibilityAnaly
|
|
|
25
25
|
id: 'visual.visibility',
|
|
26
26
|
name: 'Visibility changes',
|
|
27
27
|
description: 'Reports other players entering, leaving, or quickly reappearing in view.',
|
|
28
|
+
localizations: {
|
|
29
|
+
'zh-CN': {
|
|
30
|
+
name: '视野变化',
|
|
31
|
+
description: '感知其他玩家进入、离开或短暂离开后重新出现在视野中。',
|
|
32
|
+
},
|
|
33
|
+
},
|
|
28
34
|
subjectScope: 'others',
|
|
29
35
|
produces: [
|
|
30
36
|
'visual.player_entered',
|
|
@@ -344,6 +344,17 @@ describe('ClawClaw Perception plugin', () => {
|
|
|
344
344
|
expect.objectContaining({ id: 'visual.motion', subjectScope: 'self_and_others' }),
|
|
345
345
|
expect.objectContaining({ id: 'visual.follow', subjectScope: 'visible_pairs' }),
|
|
346
346
|
]));
|
|
347
|
+
expect(plugin.localizedModuleDescriptors('zh-CN')).toEqual(expect.arrayContaining([
|
|
348
|
+
{
|
|
349
|
+
id: 'visual.visibility',
|
|
350
|
+
name: '视野变化',
|
|
351
|
+
description: '感知其他玩家进入、离开或短暂离开后重新出现在视野中。',
|
|
352
|
+
},
|
|
353
|
+
expect.objectContaining({ id: 'visual.motion', name: '玩家移动' }),
|
|
354
|
+
]));
|
|
355
|
+
expect(plugin.localizedModuleDescriptors('en-US')).toEqual(expect.arrayContaining([
|
|
356
|
+
expect.objectContaining({ id: 'visual.visibility', name: 'Visibility changes' }),
|
|
357
|
+
]));
|
|
347
358
|
});
|
|
348
359
|
|
|
349
360
|
it('starts, analyzes, commits, and stops all seven official Worker modules together', async () => {
|
|
@@ -13,6 +13,9 @@ import { isClawclawRuntimeState } from '../../runtime-state.js';
|
|
|
13
13
|
import {
|
|
14
14
|
PERCEPTION_PLUGIN_API_VERSION,
|
|
15
15
|
ClawclawPerception,
|
|
16
|
+
hasValidClawclawResourceLocalizations,
|
|
17
|
+
resolveClawclawResourceMetadata,
|
|
18
|
+
type ClawclawResolvedResourceMetadata,
|
|
16
19
|
type ClawclawPerceptionClass,
|
|
17
20
|
type ClawclawPerceptionManifest,
|
|
18
21
|
type PerceptionApi,
|
|
@@ -205,6 +208,7 @@ export function validatePerceptionManifest(
|
|
|
205
208
|
|| !/^[a-z0-9][a-z0-9._-]*$/.test(manifest.id)
|
|
206
209
|
|| !nonEmpty(manifest.name)
|
|
207
210
|
|| !nonEmpty(manifest.description)
|
|
211
|
+
|| !hasValidClawclawResourceLocalizations(manifest.localizations)
|
|
208
212
|
|| !['others', 'self_and_others', 'visible_pairs', 'corpses'].includes(manifest.subjectScope)
|
|
209
213
|
|| !Array.isArray(manifest.produces)
|
|
210
214
|
|| manifest.produces.length === 0
|
|
@@ -772,6 +776,10 @@ export class ClawclawPerceptionPlugin implements RuntimeEpochPlugin<Record<strin
|
|
|
772
776
|
}));
|
|
773
777
|
}
|
|
774
778
|
|
|
779
|
+
localizedModuleDescriptors(locale: string): ClawclawResolvedResourceMetadata[] {
|
|
780
|
+
return this.modules.map(({ manifest }) => resolveClawclawResourceMetadata(manifest, locale));
|
|
781
|
+
}
|
|
782
|
+
|
|
775
783
|
moduleStateSnapshot(): Record<string, unknown> {
|
|
776
784
|
return Object.fromEntries(this.modules.map(({ manifest, runner }) => [
|
|
777
785
|
manifest.id,
|
|
@@ -37,6 +37,12 @@ describe('ClawClaw perception source discovery', () => {
|
|
|
37
37
|
id: 'visual.custom_linger',
|
|
38
38
|
name: 'Custom linger',
|
|
39
39
|
description: 'Reports a long stay.',
|
|
40
|
+
localizations: {
|
|
41
|
+
'zh-CN': {
|
|
42
|
+
name: '长时间停留',
|
|
43
|
+
description: '感知玩家长时间停留。',
|
|
44
|
+
},
|
|
45
|
+
},
|
|
40
46
|
subjectScope: 'others',
|
|
41
47
|
produces: ['visual.custom_linger'],
|
|
42
48
|
} as const;
|
|
@@ -51,6 +57,12 @@ describe('ClawClaw perception source discovery', () => {
|
|
|
51
57
|
id: 'visual.custom_linger',
|
|
52
58
|
name: 'Custom linger',
|
|
53
59
|
description: 'Reports a long stay.',
|
|
60
|
+
localizations: {
|
|
61
|
+
'zh-CN': {
|
|
62
|
+
name: '长时间停留',
|
|
63
|
+
description: '感知玩家长时间停留。',
|
|
64
|
+
},
|
|
65
|
+
},
|
|
54
66
|
subjectScope: 'others',
|
|
55
67
|
produces: ['visual.custom_linger'],
|
|
56
68
|
},
|