@myclaw163/clawclaw-cli 0.6.95 → 0.6.100
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/package.json +1 -1
- package/skills/clawclaw/SKILL.md +26 -10
- package/skills/clawclaw/references/CLAUDE-CODE-MONITOR.md +1 -1
- package/skills/clawclaw/references/COMMANDS.md +17 -5
- package/skills/clawclaw/references/STREAM.md +15 -12
- package/src/commands/account.test.ts +20 -0
- package/src/commands/account.ts +16 -0
- package/src/commands/do.test.ts +27 -1
- package/src/commands/do.ts +61 -0
- package/src/commands/events.test.ts +21 -3
- package/src/commands/events.ts +8 -2
- package/src/commands/game-start-plan.test.ts +158 -0
- package/src/commands/game.ts +670 -64
- package/src/commands/history-player.test.ts +3 -0
- package/src/commands/knowledge.ts +41 -4
- package/src/commands/strategy.ts +11 -0
- package/src/commands/watch.test.ts +272 -0
- package/src/commands/watch.ts +185 -25
- package/src/lib/game-client.ts +34 -0
- package/src/lib/room.test.ts +74 -0
- package/src/lib/room.ts +129 -0
- package/src/lib/server-registry.test.ts +54 -0
- package/src/lib/server-registry.ts +35 -2
- package/src/perception/player-history-store.test.ts +92 -2
- package/src/perception/player-history-store.ts +68 -0
- package/src/pipeline/event-format.test.ts +61 -1
- package/src/pipeline/event-format.ts +88 -0
- package/src/pipeline/event-hints.ts +10 -1
- package/src/runtime/event-daemon.test.ts +43 -0
- package/src/runtime/event-daemon.ts +8 -4
- package/src/runtime/room-lobby-runtime.test.ts +95 -0
- package/src/runtime/room-lobby-runtime.ts +367 -0
- package/src/sdk/index.ts +8 -3
- package/src/strategies/avoid-lone.ts +2 -0
- package/src/strategies/avoid-players.ts +2 -0
- package/src/strategies/corpse-patrol.ts +2 -0
- package/src/strategies/crab-sabotage.ts +2 -0
- package/src/strategies/find-player.ts +2 -0
- package/src/strategies/hide.ts +2 -0
- package/src/strategies/kill-frenzy.ts +2 -0
- package/src/strategies/kill-lone.ts +2 -0
- package/src/strategies/kill-target.ts +2 -0
- package/src/strategies/knowledge-contract.ts +85 -0
- package/src/strategies/lone-kill-task.ts +2 -0
- package/src/strategies/move-room.ts +2 -0
- package/src/strategies/paradise-fish.ts +2 -0
- package/src/strategies/patrol.ts +2 -0
- package/src/strategies/report-patrol.ts +2 -0
- package/src/strategies/shrimp-memory.ts +2 -0
- package/src/strategies/social-task.ts +2 -0
- package/src/strategies/task-kill-report.ts +2 -0
- package/src/strategies/task-only.ts +2 -0
- package/src/strategies/task-report.ts +2 -0
- package/src/strategies/types.ts +37 -1
- package/src/strategies/warrior-memory.ts +2 -0
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { RoomLobbyRuntime, gameStartedFromRoomSnapshot } from './room-lobby-runtime.js';
|
|
3
|
+
|
|
4
|
+
describe('gameStartedFromRoomSnapshot', () => {
|
|
5
|
+
it('returns null for non-playing room snapshots', () => {
|
|
6
|
+
expect(gameStartedFromRoomSnapshot({
|
|
7
|
+
status: 'waiting',
|
|
8
|
+
room_code: '123456',
|
|
9
|
+
game_server_name: 'room-gs',
|
|
10
|
+
})).toBeNull();
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('returns null when a playing snapshot has no game server route', () => {
|
|
14
|
+
expect(gameStartedFromRoomSnapshot({
|
|
15
|
+
status: 'playing',
|
|
16
|
+
room_code: '123456',
|
|
17
|
+
game_id: 'game-1',
|
|
18
|
+
})).toBeNull();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('converts a playing snapshot with a game server name into a game-start event', () => {
|
|
22
|
+
expect(gameStartedFromRoomSnapshot({
|
|
23
|
+
status: 'playing',
|
|
24
|
+
room_code: '123456',
|
|
25
|
+
game_id: 'game-1',
|
|
26
|
+
game_server_name: 'room-gs',
|
|
27
|
+
})).toEqual({
|
|
28
|
+
game_id: 'game-1',
|
|
29
|
+
game_server_name: 'room-gs',
|
|
30
|
+
ws_url: undefined,
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe('RoomLobbyRuntime room.game_finished', () => {
|
|
36
|
+
it('returns the local room snapshot to waiting without ending the room stream', () => {
|
|
37
|
+
const runtime: any = new RoomLobbyRuntime();
|
|
38
|
+
runtime.latestSnapshot = {
|
|
39
|
+
room_code: '123456',
|
|
40
|
+
status: 'playing',
|
|
41
|
+
game_id: 'game-1',
|
|
42
|
+
game_server_name: 'room-gs',
|
|
43
|
+
ws_url: 'ws://game',
|
|
44
|
+
members: [{ seat: 1, name: 'me', is_owner: true }],
|
|
45
|
+
};
|
|
46
|
+
runtime.gameStartedEmitted = true;
|
|
47
|
+
runtime.events = { append: () => {} };
|
|
48
|
+
runtime.writeFeed = () => {};
|
|
49
|
+
|
|
50
|
+
runtime.handleEvent('room.game_finished', { game_id: 'game-1' });
|
|
51
|
+
|
|
52
|
+
expect(runtime.currentRoomSnapshot()).toMatchObject({
|
|
53
|
+
room_code: '123456',
|
|
54
|
+
status: 'waiting',
|
|
55
|
+
members: [{ seat: 1, name: 'me', is_owner: true }],
|
|
56
|
+
});
|
|
57
|
+
expect(runtime.currentRoomSnapshot()).not.toHaveProperty('game_id');
|
|
58
|
+
expect(runtime.currentRoomSnapshot()).not.toHaveProperty('game_server_name');
|
|
59
|
+
expect(runtime.currentRoomSnapshot()).not.toHaveProperty('ws_url');
|
|
60
|
+
expect(runtime.stopped).toBe(false);
|
|
61
|
+
expect(runtime.gameStartedEmitted).toBe(false);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
describe('RoomLobbyRuntime room.snapshot logging', () => {
|
|
66
|
+
it('omits volatile cooldown seconds from room.snapshot event logs only', () => {
|
|
67
|
+
const runtime: any = new RoomLobbyRuntime();
|
|
68
|
+
const appended: any[] = [];
|
|
69
|
+
runtime.events = { append: (event: any) => appended.push(event) };
|
|
70
|
+
runtime.writeFeed = () => {};
|
|
71
|
+
|
|
72
|
+
runtime.handleEvent('room.snapshot', {
|
|
73
|
+
room_code: '123456',
|
|
74
|
+
status: 'waiting',
|
|
75
|
+
cooldown_seconds: 44,
|
|
76
|
+
seconds_remaining: 44,
|
|
77
|
+
cooldown_until: '2026-07-07T03:00:45.000Z',
|
|
78
|
+
members: [],
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const snapshotEvent = appended.find((event) => event.type === 'room.snapshot');
|
|
82
|
+
expect(snapshotEvent).toMatchObject({
|
|
83
|
+
type: 'room.snapshot',
|
|
84
|
+
room_code: '123456',
|
|
85
|
+
status: 'waiting',
|
|
86
|
+
cooldown_until: '2026-07-07T03:00:45.000Z',
|
|
87
|
+
});
|
|
88
|
+
expect(snapshotEvent).not.toHaveProperty('cooldown_seconds');
|
|
89
|
+
expect(snapshotEvent).not.toHaveProperty('seconds_remaining');
|
|
90
|
+
expect(runtime.currentRoomSnapshot()).toMatchObject({
|
|
91
|
+
cooldown_seconds: 44,
|
|
92
|
+
seconds_remaining: 44,
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
});
|
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
import WebSocket from 'ws';
|
|
2
|
+
import { writeFileSync } from 'fs';
|
|
3
|
+
import { join } from 'path';
|
|
4
|
+
import { AuthStore } from '../lib/auth.js';
|
|
5
|
+
import { GameClient } from '../lib/game-client.js';
|
|
6
|
+
import { getProfileStateDir } from '../lib/init-command.js';
|
|
7
|
+
import { EventStore } from '../pipeline/event-store.js';
|
|
8
|
+
import {
|
|
9
|
+
isCurrentAgentRoomOwner,
|
|
10
|
+
isRoomReadyToStart,
|
|
11
|
+
memberCount,
|
|
12
|
+
normalizeRoomSnapshot,
|
|
13
|
+
type RoomSnapshot,
|
|
14
|
+
} from '../lib/room.js';
|
|
15
|
+
|
|
16
|
+
export interface RoomGameStarted {
|
|
17
|
+
game_id?: string;
|
|
18
|
+
game_server_name?: string;
|
|
19
|
+
ws_url?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function gameStartedFromRoomSnapshot(snapshot: RoomSnapshot | null | undefined): RoomGameStarted | null {
|
|
23
|
+
if (!snapshot || snapshot.status !== 'playing') return null;
|
|
24
|
+
const gameServerName = typeof snapshot.game_server_name === 'string' && snapshot.game_server_name.length > 0
|
|
25
|
+
? snapshot.game_server_name
|
|
26
|
+
: undefined;
|
|
27
|
+
const wsUrl = typeof snapshot.ws_url === 'string' && snapshot.ws_url.length > 0
|
|
28
|
+
? snapshot.ws_url
|
|
29
|
+
: undefined;
|
|
30
|
+
if (!gameServerName && !wsUrl) return null;
|
|
31
|
+
return {
|
|
32
|
+
game_id: typeof snapshot.game_id === 'string' ? snapshot.game_id : undefined,
|
|
33
|
+
game_server_name: gameServerName,
|
|
34
|
+
ws_url: wsUrl,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface RoomLobbyRuntimeOptions {
|
|
39
|
+
authStore?: AuthStore;
|
|
40
|
+
onGameStarted?: (event: RoomGameStarted) => void | Promise<void>;
|
|
41
|
+
onStop?: (reason: string) => void;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const HEARTBEAT_INTERVAL_MS = 15_000;
|
|
45
|
+
const PONG_TIMEOUT_MS = 10_000;
|
|
46
|
+
const RECONNECT_MS = 2_000;
|
|
47
|
+
const MAX_RECENT_EVENTS = 20;
|
|
48
|
+
|
|
49
|
+
function now(): string {
|
|
50
|
+
return new Date().toISOString();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function eventWithType(type: string, data: Record<string, any> = {}): Record<string, any> {
|
|
54
|
+
return { ts: now(), type, ...data };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function roomSnapshotForEventLog(snapshot: RoomSnapshot): Record<string, any> {
|
|
58
|
+
const {
|
|
59
|
+
cooldown_seconds: _cooldownSeconds,
|
|
60
|
+
seconds_remaining: _secondsRemaining,
|
|
61
|
+
...rest
|
|
62
|
+
} = snapshot;
|
|
63
|
+
return rest;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function messageDataToString(data: WebSocket.RawData): string {
|
|
67
|
+
if (typeof data === 'string') return data;
|
|
68
|
+
if (Buffer.isBuffer(data)) return data.toString('utf8');
|
|
69
|
+
if (data instanceof ArrayBuffer) return Buffer.from(data).toString('utf8');
|
|
70
|
+
return Buffer.concat(data).toString('utf8');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export class RoomLobbyRuntime {
|
|
74
|
+
private readonly authStore: AuthStore;
|
|
75
|
+
private readonly onGameStarted?: (event: RoomGameStarted) => void | Promise<void>;
|
|
76
|
+
private readonly onStop?: (reason: string) => void;
|
|
77
|
+
private client: GameClient | null = null;
|
|
78
|
+
private events: EventStore | null = null;
|
|
79
|
+
private ws: WebSocket | null = null;
|
|
80
|
+
private heartbeatTimer: ReturnType<typeof setInterval> | null = null;
|
|
81
|
+
private pongTimer: ReturnType<typeof setTimeout> | null = null;
|
|
82
|
+
private reconnectTimer: ReturnType<typeof setTimeout> | null = null;
|
|
83
|
+
private stopped = false;
|
|
84
|
+
private latestSnapshot: RoomSnapshot | null = null;
|
|
85
|
+
private latestFeed: any = null;
|
|
86
|
+
private profileName = '';
|
|
87
|
+
private feedPath = '';
|
|
88
|
+
private recentEvents: Record<string, any>[] = [];
|
|
89
|
+
private readyEmitted = false;
|
|
90
|
+
private gameStartedEmitted = false;
|
|
91
|
+
private terminalPhase: string | null = null;
|
|
92
|
+
|
|
93
|
+
constructor(opts: RoomLobbyRuntimeOptions = {}) {
|
|
94
|
+
this.authStore = opts.authStore ?? new AuthStore();
|
|
95
|
+
this.onGameStarted = opts.onGameStarted;
|
|
96
|
+
this.onStop = opts.onStop;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
snapshot(): any | null {
|
|
100
|
+
return this.latestFeed;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
currentRoomSnapshot(): RoomSnapshot | null {
|
|
104
|
+
return this.latestSnapshot;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async start(): Promise<void> {
|
|
108
|
+
const profile = this.authStore.getActive();
|
|
109
|
+
if (!profile) throw new Error('Not logged in.');
|
|
110
|
+
this.profileName = profile.agentName;
|
|
111
|
+
this.feedPath = join(getProfileStateDir(profile), 'feed.json');
|
|
112
|
+
this.client = GameClient.fromAuth({ authStore: this.authStore });
|
|
113
|
+
this.events = EventStore.forActiveAccount();
|
|
114
|
+
this.append(eventWithType('room_lobby_started', { pid: process.pid }));
|
|
115
|
+
this.writeFeed();
|
|
116
|
+
await this.connect();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
stop(reason = 'manual'): void {
|
|
120
|
+
if (this.stopped) return;
|
|
121
|
+
this.stopped = true;
|
|
122
|
+
this.clearTimers();
|
|
123
|
+
if (this.ws) {
|
|
124
|
+
this.ws.removeAllListeners();
|
|
125
|
+
try { this.ws.close(); } catch {}
|
|
126
|
+
this.ws = null;
|
|
127
|
+
}
|
|
128
|
+
this.append(eventWithType('room_lobby_stopped', { reason }));
|
|
129
|
+
this.onStop?.(reason);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
sendRefresh(): void {
|
|
133
|
+
this.append(eventWithType('room_refresh_requested'));
|
|
134
|
+
this.send({ type: 'room.refresh' });
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
private async connect(): Promise<void> {
|
|
138
|
+
const client = this.client;
|
|
139
|
+
const profile = this.authStore.getActive();
|
|
140
|
+
if (!client || !profile || this.stopped) return;
|
|
141
|
+
|
|
142
|
+
this.clearSocket();
|
|
143
|
+
const ws = new WebSocket(client.lobbyAgentWsUrl, {
|
|
144
|
+
headers: { Authorization: `Bearer ${profile.apiKey}` },
|
|
145
|
+
});
|
|
146
|
+
this.ws = ws;
|
|
147
|
+
await new Promise<void>((resolve, reject) => {
|
|
148
|
+
const settle = (fn: () => void) => {
|
|
149
|
+
ws.off('open', onOpen);
|
|
150
|
+
ws.off('error', onError);
|
|
151
|
+
fn();
|
|
152
|
+
};
|
|
153
|
+
const onOpen = () => settle(resolve);
|
|
154
|
+
const onError = (err: Error) => settle(() => reject(err));
|
|
155
|
+
ws.once('open', onOpen);
|
|
156
|
+
ws.once('error', onError);
|
|
157
|
+
ws.on('message', (data) => this.handleRawMessage(data));
|
|
158
|
+
ws.on('close', () => this.handleClose());
|
|
159
|
+
});
|
|
160
|
+
this.startHeartbeat();
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
private clearSocket(): void {
|
|
164
|
+
if (this.ws) {
|
|
165
|
+
this.ws.removeAllListeners();
|
|
166
|
+
try { this.ws.close(); } catch {}
|
|
167
|
+
this.ws = null;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
private clearTimers(): void {
|
|
172
|
+
if (this.heartbeatTimer) clearInterval(this.heartbeatTimer);
|
|
173
|
+
if (this.pongTimer) clearTimeout(this.pongTimer);
|
|
174
|
+
if (this.reconnectTimer) clearTimeout(this.reconnectTimer);
|
|
175
|
+
this.heartbeatTimer = null;
|
|
176
|
+
this.pongTimer = null;
|
|
177
|
+
this.reconnectTimer = null;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
private startHeartbeat(): void {
|
|
181
|
+
if (this.heartbeatTimer) clearInterval(this.heartbeatTimer);
|
|
182
|
+
this.heartbeatTimer = setInterval(() => {
|
|
183
|
+
this.send({ type: 'ping' });
|
|
184
|
+
if (this.pongTimer) clearTimeout(this.pongTimer);
|
|
185
|
+
this.pongTimer = setTimeout(() => {
|
|
186
|
+
this.append(eventWithType('room_ping_timeout'));
|
|
187
|
+
this.reconnect();
|
|
188
|
+
}, PONG_TIMEOUT_MS);
|
|
189
|
+
}, HEARTBEAT_INTERVAL_MS);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
private handleClose(): void {
|
|
193
|
+
if (this.stopped) return;
|
|
194
|
+
this.append(eventWithType('room_ws_closed'));
|
|
195
|
+
this.reconnect();
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
private reconnect(): void {
|
|
199
|
+
if (this.stopped) return;
|
|
200
|
+
this.clearTimers();
|
|
201
|
+
this.clearSocket();
|
|
202
|
+
this.reconnectTimer = setTimeout(() => {
|
|
203
|
+
this.connect().catch((err) => {
|
|
204
|
+
this.append(eventWithType('room_ws_reconnect_failed', {
|
|
205
|
+
error: err instanceof Error ? err.message : String(err),
|
|
206
|
+
}));
|
|
207
|
+
this.reconnect();
|
|
208
|
+
});
|
|
209
|
+
}, RECONNECT_MS);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
private send(message: Record<string, any>): void {
|
|
213
|
+
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) return;
|
|
214
|
+
this.ws.send(JSON.stringify(message));
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
private handleRawMessage(raw: WebSocket.RawData): void {
|
|
218
|
+
const text = messageDataToString(raw);
|
|
219
|
+
let msg: any;
|
|
220
|
+
try {
|
|
221
|
+
msg = JSON.parse(text);
|
|
222
|
+
} catch {
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
if (msg?.type === 'ping') {
|
|
226
|
+
this.send({ type: 'pong' });
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
if (msg?.type === 'pong') {
|
|
230
|
+
if (this.pongTimer) clearTimeout(this.pongTimer);
|
|
231
|
+
this.pongTimer = null;
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
const type = typeof msg?.type === 'string' ? msg.type : '';
|
|
235
|
+
if (!type) return;
|
|
236
|
+
const data = msg?.data && typeof msg.data === 'object' ? msg.data : {};
|
|
237
|
+
this.handleEvent(type, data);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
private handleEvent(type: string, data: Record<string, any>): void {
|
|
241
|
+
if (type === 'room.snapshot') {
|
|
242
|
+
this.latestSnapshot = normalizeRoomSnapshot(data);
|
|
243
|
+
this.append(eventWithType(type, roomSnapshotForEventLog(this.latestSnapshot)));
|
|
244
|
+
this.emitReadyIfNeeded();
|
|
245
|
+
this.writeFeed();
|
|
246
|
+
this.emitGameStartedIfNeeded(gameStartedFromRoomSnapshot(this.latestSnapshot));
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
this.applyIncrementalEvent(type, data);
|
|
251
|
+
this.append(eventWithType(type, data));
|
|
252
|
+
|
|
253
|
+
if (type === 'room.full') {
|
|
254
|
+
this.emitReadyIfNeeded(true);
|
|
255
|
+
}
|
|
256
|
+
if (type === 'room.game_starting') {
|
|
257
|
+
this.latestSnapshot = { ...(this.latestSnapshot ?? {}), status: 'starting', ...data };
|
|
258
|
+
}
|
|
259
|
+
if (type === 'room.game_started') {
|
|
260
|
+
this.latestSnapshot = { ...(this.latestSnapshot ?? {}), status: 'playing', ...data };
|
|
261
|
+
this.writeFeed();
|
|
262
|
+
this.emitGameStartedIfNeeded(data);
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
if (type === 'room.game_finished') {
|
|
266
|
+
const {
|
|
267
|
+
game_id: _gameId,
|
|
268
|
+
game_server_name: _gameServerName,
|
|
269
|
+
ws_url: _wsUrl,
|
|
270
|
+
...roomState
|
|
271
|
+
} = this.latestSnapshot ?? {};
|
|
272
|
+
this.latestSnapshot = {
|
|
273
|
+
...roomState,
|
|
274
|
+
status: 'waiting',
|
|
275
|
+
};
|
|
276
|
+
this.gameStartedEmitted = false;
|
|
277
|
+
this.terminalPhase = null;
|
|
278
|
+
}
|
|
279
|
+
if (type === 'room.self_left') {
|
|
280
|
+
this.latestSnapshot = null;
|
|
281
|
+
this.terminalPhase = 'room_left';
|
|
282
|
+
this.writeFeed();
|
|
283
|
+
this.stop('self_left');
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
if (type === 'room.cooldown') {
|
|
287
|
+
this.latestSnapshot = {
|
|
288
|
+
...(this.latestSnapshot ?? {}),
|
|
289
|
+
status: 'waiting',
|
|
290
|
+
cooldown_seconds: data.seconds_remaining ?? data.cooldown_seconds,
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
this.emitReadyIfNeeded();
|
|
294
|
+
this.writeFeed();
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
private applyIncrementalEvent(type: string, data: Record<string, any>): void {
|
|
298
|
+
const snapshot = this.latestSnapshot;
|
|
299
|
+
if (!snapshot) return;
|
|
300
|
+
if ((type === 'room.member_joined') && Array.isArray(snapshot.members)) {
|
|
301
|
+
const exists = snapshot.members.some((member) => member.agent_id === data.agent_id);
|
|
302
|
+
if (!exists) snapshot.members = [...snapshot.members, data];
|
|
303
|
+
}
|
|
304
|
+
if ((type === 'room.member_left') && Array.isArray(snapshot.members)) {
|
|
305
|
+
snapshot.members = snapshot.members.filter((member) => member.agent_id !== data.agent_id);
|
|
306
|
+
}
|
|
307
|
+
if (type === 'room.owner_changed' && Array.isArray(snapshot.members)) {
|
|
308
|
+
snapshot.members = snapshot.members.map((member) => ({
|
|
309
|
+
...member,
|
|
310
|
+
is_owner: member.agent_id === data.agent_id,
|
|
311
|
+
}));
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
private emitReadyIfNeeded(force = false): void {
|
|
316
|
+
const ready = isRoomReadyToStart(this.latestSnapshot);
|
|
317
|
+
if (!ready) {
|
|
318
|
+
this.readyEmitted = false;
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
if (this.readyEmitted && !force) return;
|
|
322
|
+
this.readyEmitted = true;
|
|
323
|
+
this.append(eventWithType('room_ready_to_start', {
|
|
324
|
+
room_code: this.latestSnapshot?.room_code,
|
|
325
|
+
member_count: memberCount(this.latestSnapshot),
|
|
326
|
+
next_step: 'Ask the user whether to start now before running ccl game start-room.',
|
|
327
|
+
}));
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
private emitGameStartedIfNeeded(event: RoomGameStarted | null | undefined): void {
|
|
331
|
+
if (!event || this.gameStartedEmitted) return;
|
|
332
|
+
this.gameStartedEmitted = true;
|
|
333
|
+
void this.onGameStarted?.(event);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
private append(event: Record<string, any>): void {
|
|
337
|
+
this.events?.append(event);
|
|
338
|
+
this.recentEvents.push(event);
|
|
339
|
+
if (this.recentEvents.length > MAX_RECENT_EVENTS) this.recentEvents.shift();
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
private writeFeed(): void {
|
|
343
|
+
const room = this.latestSnapshot;
|
|
344
|
+
const phase = this.terminalPhase ?? (room?.status === 'starting' ? 'room_starting'
|
|
345
|
+
: room?.status === 'playing' ? 'room_playing'
|
|
346
|
+
: 'room_waiting');
|
|
347
|
+
this.latestFeed = {
|
|
348
|
+
ts: now(),
|
|
349
|
+
phase,
|
|
350
|
+
you: {
|
|
351
|
+
name: this.profileName,
|
|
352
|
+
seat: room?.my_seat ?? room?.seat,
|
|
353
|
+
is_owner: isCurrentAgentRoomOwner(room),
|
|
354
|
+
},
|
|
355
|
+
room: room ? {
|
|
356
|
+
...room,
|
|
357
|
+
member_count: memberCount(room),
|
|
358
|
+
is_owner: isCurrentAgentRoomOwner(room),
|
|
359
|
+
ready_to_start: isRoomReadyToStart(room),
|
|
360
|
+
} : null,
|
|
361
|
+
recent_events: this.recentEvents.slice(-10),
|
|
362
|
+
};
|
|
363
|
+
try {
|
|
364
|
+
writeFileSync(this.feedPath, JSON.stringify(this.latestFeed, null, 2));
|
|
365
|
+
} catch {}
|
|
366
|
+
}
|
|
367
|
+
}
|
package/src/sdk/index.ts
CHANGED
|
@@ -16,7 +16,8 @@
|
|
|
16
16
|
* EmergencyInfo / Position / GameEvent / MapRoom / GameResult
|
|
17
17
|
* · WsConnectionState / ActionErrorCode (enums)
|
|
18
18
|
* · Strategy / StrategyContext / StrategyEntry / CustomModule /
|
|
19
|
-
* BehaviorDecision / CorpseTarget / RoomTarget
|
|
19
|
+
* BehaviorDecision / CorpseTarget / RoomTarget /
|
|
20
|
+
* KnowledgeValidation / KnowledgeReview(策略侧知识校验/整理契约)
|
|
20
21
|
* · game-utils: dist / firstAvailableTask / nearestKnownCorpse / nearestKnownCorpseNav /
|
|
21
22
|
* hasKnownCorpse / corpseAtScene / nearestReportableCorpse / safePatrolStep /
|
|
22
23
|
* nonTeammatesVisible / matchesTarget / isTargetAlive / nearestVisibleTarget /
|
|
@@ -26,7 +27,7 @@
|
|
|
26
27
|
* SHRIMP_VISION_RANGE / SHRIMP_VISION_EXIT_BUFFER / SHRIMP_VISION_RELEASE_RANGE /
|
|
27
28
|
* FOLLOW_RANGE / PATROL_REACHED_DISTANCE / PROGRESS_INTERVAL_MS)
|
|
28
29
|
* · Knowledge (ctx.knowledge): KnowledgeView / SubjectEntry / FactMeta /
|
|
29
|
-
* TagMeta / KnowledgeFile / KnowledgeScope / KnowledgeSource /
|
|
30
|
+
* TagMeta / KnowledgeFile / KnowledgeSetInput / KnowledgeScope / KnowledgeSource /
|
|
30
31
|
* PlayerRef / SubjectRef / KnowledgeRef
|
|
31
32
|
* · Greeting: parseGreetingArgs / GreetingTracker / MAX_GREETING_PHRASES /
|
|
32
33
|
* GREETING_COOLDOWN_MS
|
|
@@ -47,6 +48,7 @@ export type {
|
|
|
47
48
|
export type {
|
|
48
49
|
Strategy, StrategyContext, StrategyEntry, CustomModule,
|
|
49
50
|
BehaviorDecision, CorpseTarget, RoomTarget,
|
|
51
|
+
KnowledgeValidation, KnowledgeReview,
|
|
50
52
|
} from '../strategies/types.js';
|
|
51
53
|
|
|
52
54
|
// Game utilities (for user strategies)
|
|
@@ -66,7 +68,7 @@ export {
|
|
|
66
68
|
export type { SafeTaskOptions, SafePatrolOptions } from '../strategies/game-utils.js';
|
|
67
69
|
|
|
68
70
|
export type {
|
|
69
|
-
KnowledgeView, SubjectEntry, FactMeta, TagMeta, KnowledgeFile,
|
|
71
|
+
KnowledgeView, SubjectEntry, FactMeta, TagMeta, KnowledgeFile, KnowledgeSetInput,
|
|
70
72
|
KnowledgeScope, KnowledgeSource, PlayerMark, PlayerRef, SubjectRef, KnowledgeRef,
|
|
71
73
|
} from '../lib/knowledge-store.js';
|
|
72
74
|
|
|
@@ -80,6 +82,9 @@ export { parseGreetingArgs, GreetingTracker, MAX_GREETING_PHRASES, GREETING_TEXT
|
|
|
80
82
|
// Player targeting utilities (for exported strategies)
|
|
81
83
|
export { parseTargetArgs, stripRoleIds } from '../strategies/player-targets.js';
|
|
82
84
|
|
|
85
|
+
// Knowledge contract helpers (for exported strategies that declare which knowledge they read)
|
|
86
|
+
export { noKnowledgeContract, playerMarkContract } from '../strategies/knowledge-contract.js';
|
|
87
|
+
|
|
83
88
|
// Goal system (for exported strategies that use the goal framework)
|
|
84
89
|
export { GoalRootStrategy } from '../strategies/goals/goal-root-strategy.js';
|
|
85
90
|
export { TaskOnlyTop } from '../strategies/goals/task-only-top.js';
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import type { StrategyEntry } from './types.js';
|
|
2
|
+
import { noKnowledgeContract } from './knowledge-contract.js';
|
|
2
3
|
import { GoalRootStrategy } from './goals/goal-root-strategy.js';
|
|
3
4
|
import { AvoidLoneTop } from './goals/avoid-lone-top.js';
|
|
4
5
|
|
|
5
6
|
export const strategy: StrategyEntry = {
|
|
6
7
|
id: 'avoid-lone',
|
|
7
8
|
name: '结伴',
|
|
9
|
+
...noKnowledgeContract('结伴'),
|
|
8
10
|
description: '避免落单。视野里只有一个人时寻路躲避主动避开(推演逃点、持续远离);视野里有两人以上时跟住次近的玩家、把可见人数维持在 ≥2(最近的人本就贴着不会丢,次近的人才是跌回一对一的临界);持续无人或没有当前目标时巡逻。',
|
|
9
11
|
create() {
|
|
10
12
|
return new GoalRootStrategy('avoid-lone', () => new AvoidLoneTop(), { resetOnMeetingResume: false });
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { StrategyEntry } from './types.js';
|
|
2
|
+
import { playerMarkContract } from './knowledge-contract.js';
|
|
2
3
|
import { GoalRootStrategy } from './goals/goal-root-strategy.js';
|
|
3
4
|
import { AvoidPlayersTop } from './goals/avoid-players-top.js';
|
|
4
5
|
import { parseTargetArgs } from './player-targets.js';
|
|
@@ -6,6 +7,7 @@ import { parseTargetArgs } from './player-targets.js';
|
|
|
6
7
|
export const strategy: StrategyEntry = {
|
|
7
8
|
id: 'avoid-players',
|
|
8
9
|
name: '避人',
|
|
10
|
+
...playerMarkContract('避人'),
|
|
9
11
|
description: '回避玩家:按房间顺序巡逻,视野里出现回避目标就寻路躲避(推演逃点、持续远离),威胁消失后恢复巡逻。回避名单 = 启动参数(座位号或名字,可多人,可省略)∪ 知识库中标记为 hostile(坏人)的玩家;trusted 与未标记的被怀疑者都不自动回避(要躲就标 hostile 或用启动参数点名)。Agent 可用 `ccl knowledge mark` 动态改判,免重启。',
|
|
10
12
|
create(args?: string[]) {
|
|
11
13
|
const targets = args ? parseTargetArgs(args) : [];
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { StrategyEntry } from './types.js';
|
|
2
|
+
import { noKnowledgeContract } from './knowledge-contract.js';
|
|
2
3
|
import { GoalRootStrategy } from './goals/goal-root-strategy.js';
|
|
3
4
|
import { CorpsePatrolTop } from './goals/corpse-patrol-top.js';
|
|
4
5
|
import { parseGreetingArgs } from './greeting.js';
|
|
@@ -12,6 +13,7 @@ class CorpsePatrolStrategy extends GoalRootStrategy {
|
|
|
12
13
|
export const strategy: StrategyEntry = {
|
|
13
14
|
id: 'corpse-patrol',
|
|
14
15
|
name: '守尸',
|
|
16
|
+
...noKnowledgeContract('守尸'),
|
|
15
17
|
description: '发现尸体就在附近40至100距离内随机游荡,但不报告——故意在尸体旁出没制造嫌疑感。没有尸体时巡逻各房间。传入打招呼话术时,视野内出现人就随机发送一条,之后120秒内不再发言。不做任务,不杀人;但残局(已知存活≤6)出现紧急维修任务时例外,会最高优先级抢着去做,阻止蟹靠破坏倒计时取胜。(天堂鱼默认;进阶版见 paradise-fish)参数:可选:1~3 条打招呼话术。',
|
|
16
18
|
create(args?: string[]) {
|
|
17
19
|
return new CorpsePatrolStrategy(parseGreetingArgs(args, 'corpse-patrol'));
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { StrategyEntry } from './types.js';
|
|
2
|
+
import { noKnowledgeContract } from './knowledge-contract.js';
|
|
2
3
|
import { GoalRootStrategy } from './goals/goal-root-strategy.js';
|
|
3
4
|
import { CrabSabotageTop } from './goals/crab-sabotage-top.js';
|
|
4
5
|
import { ConversationGoal } from './goals/conversation-goal.js';
|
|
@@ -7,6 +8,7 @@ import { SpeechModule, getSpeechConfigForRole } from './speech-module.js';
|
|
|
7
8
|
export const strategy: StrategyEntry = {
|
|
8
9
|
id: 'crab-sabotage',
|
|
9
10
|
name: '破坏蟹',
|
|
11
|
+
...noKnowledgeContract('破坏蟹'),
|
|
10
12
|
description: '优先完成蟹的真实破坏任务(破坏点紧挨已知尸体时会跳过);做完立刻触发紧急警报;警报期照样会被举报、不乱杀,只清真正落单的目标。有紧急事件时跳过破坏流程。视野里只有一个非队友且冷却可用就靠近,进入50距离就立刻出刀,即使正在做破坏任务;刀在冷却时不追不等、改做低优先级任务伪装,冷却好且目标仍落单可见再出刀;正在做任务时不会撇下任务去追猎(贴脸且刀好仍会立刻出刀)。发现尸体且附近有非队友、但当前不能原地出刀时,会靠近并报告尸体。视野里出现两人以上时,暂停攻击10秒并做任务伪装,先做真实任务再做虾的伪装任务;选任务时会跳过紧挨已知尸体(含看见过、已离开视野的)的任务。没有可做任务时按房间巡逻,绝不停在原地。(普通蟹默认)',
|
|
11
13
|
create(args?: string[]) {
|
|
12
14
|
const role = args?.[0] ?? 'crab_generic';
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { StrategyEntry } from './types.js';
|
|
2
|
+
import { noKnowledgeContract } from './knowledge-contract.js';
|
|
2
3
|
import { GoalRootStrategy } from './goals/goal-root-strategy.js';
|
|
3
4
|
import { FindPlayerTop } from './goals/find-player-top.js';
|
|
4
5
|
import { parseTargetArgs } from './player-targets.js';
|
|
@@ -6,6 +7,7 @@ import { parseTargetArgs } from './player-targets.js';
|
|
|
6
7
|
export const strategy: StrategyEntry = {
|
|
7
8
|
id: 'find-player',
|
|
8
9
|
name: '盯人',
|
|
10
|
+
...noKnowledgeContract('盯人'),
|
|
9
11
|
description: '全力寻找并贴身跟踪一个指定目标(座位号或名字,单个)。看到就贴上去近距离尾随(约40距离);跟丢就先奔向最后出现的位置,再按房间顺序不停巡逻直到目标重新出现,绝不放弃。只跟踪、不出刀、不做任务、不报告;目标死亡即停。',
|
|
10
12
|
create(args?: string[]) {
|
|
11
13
|
const targets = args ? parseTargetArgs(args) : [];
|
package/src/strategies/hide.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { StrategyEntry } from './types.js';
|
|
2
|
+
import { noKnowledgeContract } from './knowledge-contract.js';
|
|
2
3
|
import { GoalRootStrategy } from './goals/goal-root-strategy.js';
|
|
3
4
|
import { HideTop } from './goals/hide-top.js';
|
|
4
5
|
|
|
@@ -15,6 +16,7 @@ export function parseHideKillArg(args?: string[]): boolean | null {
|
|
|
15
16
|
export const strategy: StrategyEntry = {
|
|
16
17
|
id: 'hide',
|
|
17
18
|
name: '躲藏',
|
|
19
|
+
...noKnowledgeContract('躲藏'),
|
|
18
20
|
description:
|
|
19
21
|
'躲藏策略:在离线从地图算好的若干「非走廊、≥2 出口可逃、远离任务点/出生点」的藏身角落里,选测地最近且不挨威胁的一个潜伏不动;视野里一出现非队友,就像 shrimp-memory 那样保持距离甩开(KeepAway),并边逃边重算排除该威胁的新藏点,逃完落到新点继续潜伏。蟹不躲队友。可选参数 kill / nokill 控制「被单个对手逼进出刀距离且无路可逃时是否出刀自保」——缺省按角色:蟹/章鱼默认开,武士虾/枪虾(及无刀角色)默认关。',
|
|
20
22
|
create(args?: string[]) {
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import type { StrategyEntry } from './types.js';
|
|
2
|
+
import { noKnowledgeContract } from './knowledge-contract.js';
|
|
2
3
|
import { GoalRootStrategy } from './goals/goal-root-strategy.js';
|
|
3
4
|
import { KillFrenzyTop } from './goals/kill-frenzy-top.js';
|
|
4
5
|
|
|
5
6
|
export const strategy: StrategyEntry = {
|
|
6
7
|
id: 'kill-frenzy',
|
|
7
8
|
name: '屠夫',
|
|
9
|
+
...noKnowledgeContract('屠夫'),
|
|
8
10
|
description:
|
|
9
11
|
'疯狂砍人:见谁杀谁(除队友外所有可见玩家),多人在场也照杀。按房间顺序巡逻,视野里只要有非队友就冲向最近的那个,50内且冷却好就出刀,冷却中保持10距离贴身跟随等冷却。与 kill-lone 不同,不会因为周围人多而暂停攻击。永不停手。',
|
|
10
12
|
create() {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { StrategyEntry } from './types.js';
|
|
2
|
+
import { playerMarkContract } from './knowledge-contract.js';
|
|
2
3
|
import { GoalRootStrategy } from './goals/goal-root-strategy.js';
|
|
3
4
|
import { KillLoneTop } from './goals/kill-lone-top.js';
|
|
4
5
|
import { parseTargetArgs } from './player-targets.js';
|
|
@@ -6,6 +7,7 @@ import { parseTargetArgs } from './player-targets.js';
|
|
|
6
7
|
export const strategy: StrategyEntry = {
|
|
7
8
|
id: 'kill-lone',
|
|
8
9
|
name: '猎独',
|
|
10
|
+
...playerMarkContract('猎独'),
|
|
9
11
|
description: '按照房间顺序进行巡逻,巡逻时遇到人会判断。可传入避免击杀的座位号或名字列表。视野里只有一个非队友且不在避免名单内就靠近,进入50距离就出刀;冷却中贴在10距离附近等。视野里出现两人以上时,暂停攻击10秒并继续巡逻。',
|
|
10
12
|
create(args?: string[]) {
|
|
11
13
|
const protectedTargets = args ? parseTargetArgs(args) : [];
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { StrategyEntry } from './types.js';
|
|
2
|
+
import { noKnowledgeContract } from './knowledge-contract.js';
|
|
2
3
|
import { GoalRootStrategy } from './goals/goal-root-strategy.js';
|
|
3
4
|
import { KillTargetTop } from './goals/kill-target-top.js';
|
|
4
5
|
import { parseTargetArgs } from './player-targets.js';
|
|
@@ -6,6 +7,7 @@ import { parseTargetArgs } from './player-targets.js';
|
|
|
6
7
|
export const strategy: StrategyEntry = {
|
|
7
8
|
id: 'kill-target',
|
|
8
9
|
name: '追杀',
|
|
10
|
+
...noKnowledgeContract('追杀'),
|
|
9
11
|
description: '专门追杀指定目标(传入座位号或名字,可多人)。按照房间顺序进行巡逻,目标进入视野就冲上去,50内且冷却好就出刀;冷却中保持10距离贴身跟随等待。找目标时路过其他玩家不会停步。所有目标死亡后停止。',
|
|
10
12
|
create(args?: string[]) {
|
|
11
13
|
const targets = args ? parseTargetArgs(args) : [];
|