@myclaw163/clawclaw-cli 0.6.61 → 0.6.63
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/README.md +73 -86
- package/package.json +1 -1
- package/scripts/find-hide-spots.py +157 -0
- package/skills/clawclaw/SKILL.md +12 -12
- package/skills/clawclaw/references/CHATTERBOX.md +1 -1
- package/skills/clawclaw/references/COMMANDS.md +19 -0
- package/skills/clawclaw/references/GAME-MECHANICS.md +1 -1
- package/skills/clawclaw/references/STRATEGIES.md +2 -2
- package/skills/clawclaw/references/STREAM.md +60 -27
- package/src/commands/events.test.ts +71 -0
- package/src/commands/events.ts +140 -7
- package/src/commands/watch.test.ts +44 -47
- package/src/commands/watch.ts +53 -114
- package/src/pipeline/event-format.test.ts +135 -0
- package/src/pipeline/event-format.ts +376 -0
- package/src/pipeline/event-hints.ts +173 -0
- package/src/pipeline/event-store.test.ts +28 -0
- package/src/pipeline/event-store.ts +76 -7
- package/src/sdk/index.ts +2 -1
- package/src/sdk/types.ts +13 -0
- package/src/strategies/goals/anchor-linger.ts +77 -0
- package/src/strategies/goals/follow-companion-goal.ts +106 -0
- package/src/strategies/goals/hide-top.ts +197 -0
- package/src/strategies/goals/keep-away-goal.ts +1 -1
- package/src/strategies/goals/linger-corpse-goal.ts +9 -53
- package/src/strategies/goals/paradise-fish-top.ts +8 -20
- package/src/strategies/hide-spots.ts +123 -0
- package/src/strategies/hide.ts +23 -0
- package/src/strategies/strategy-loop.ts +4 -0
- package/src/strategies/types.ts +7 -1
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
import { formatEventHint, roleAssignedHint } from './event-hints.js';
|
|
2
|
+
|
|
3
|
+
export interface EventFormatContext {
|
|
4
|
+
summary?: any | null;
|
|
5
|
+
seats?: Record<string, number>;
|
|
6
|
+
isFirstVisibleEvent?: boolean;
|
|
7
|
+
maxTextLength?: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface FormattedEvent {
|
|
11
|
+
line?: number;
|
|
12
|
+
type: string;
|
|
13
|
+
tick: number | null;
|
|
14
|
+
notice?: string;
|
|
15
|
+
hint?: string;
|
|
16
|
+
[key: string]: any;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function cleanObject<T extends Record<string, any>>(obj: T): T {
|
|
20
|
+
for (const key of Object.keys(obj)) {
|
|
21
|
+
if (obj[key] === undefined) delete obj[key];
|
|
22
|
+
}
|
|
23
|
+
return obj;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function text(value: unknown, fallback = '?'): string {
|
|
27
|
+
if (typeof value === 'string' && value.length > 0) return value;
|
|
28
|
+
if (typeof value === 'number' && Number.isFinite(value)) return String(value);
|
|
29
|
+
return fallback;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function numberValue(value: unknown): number | undefined {
|
|
33
|
+
if (typeof value === 'number' && Number.isFinite(value)) return value;
|
|
34
|
+
if (typeof value === 'string' && value.trim() !== '') {
|
|
35
|
+
const parsed = Number(value);
|
|
36
|
+
if (Number.isFinite(parsed)) return parsed;
|
|
37
|
+
}
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function firstString(event: Record<string, any>, keys: string[], fallback = '?'): string {
|
|
42
|
+
for (const key of keys) {
|
|
43
|
+
const value = event[key];
|
|
44
|
+
if (typeof value === 'string' && value.length > 0) return value;
|
|
45
|
+
if (typeof value === 'number' && Number.isFinite(value)) return String(value);
|
|
46
|
+
}
|
|
47
|
+
return fallback;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function firstNumber(event: Record<string, any>, keys: string[]): number | undefined {
|
|
51
|
+
for (const key of keys) {
|
|
52
|
+
const value = numberValue(event[key]);
|
|
53
|
+
if (value !== undefined) return value;
|
|
54
|
+
}
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function truncate(value: unknown, maxLength: number): string {
|
|
59
|
+
const raw = text(value, '');
|
|
60
|
+
if (raw.length <= maxLength) return raw;
|
|
61
|
+
return `${raw.slice(0, Math.max(0, maxLength - 1))}…`;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function seatMapFromSummary(summary: any | null | undefined, extra?: Record<string, number>): Record<string, number> {
|
|
65
|
+
const seats: Record<string, number> = { ...(extra ?? {}) };
|
|
66
|
+
const remember = (name: unknown, seat: unknown): void => {
|
|
67
|
+
if (typeof name !== 'string' || name.length === 0) return;
|
|
68
|
+
const parsed = numberValue(seat);
|
|
69
|
+
if (parsed !== undefined) seats[name] = parsed;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
remember(summary?.you?.name, summary?.you?.seat);
|
|
73
|
+
|
|
74
|
+
const collections = [
|
|
75
|
+
summary?.visible_players,
|
|
76
|
+
summary?.all_players,
|
|
77
|
+
summary?.game?.all_players,
|
|
78
|
+
summary?.meeting?.alive_players,
|
|
79
|
+
];
|
|
80
|
+
for (const collection of collections) {
|
|
81
|
+
if (!Array.isArray(collection)) continue;
|
|
82
|
+
for (const player of collection) {
|
|
83
|
+
if (typeof player === 'string') continue;
|
|
84
|
+
remember(player?.name ?? player?.agent_name, player?.seat);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const allSeats = summary?.all_seats ?? summary?.game?.all_seats;
|
|
89
|
+
if (allSeats && typeof allSeats === 'object' && !Array.isArray(allSeats)) {
|
|
90
|
+
for (const [name, seat] of Object.entries(allSeats)) remember(name, seat);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return seats;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function seatForName(name: unknown, ctx: EventFormatContext): number | undefined {
|
|
97
|
+
if (typeof name !== 'string' || name.length === 0) return undefined;
|
|
98
|
+
return seatMapFromSummary(ctx.summary, ctx.seats)[name];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function seatLabel(name: unknown, seat: unknown, ctx: EventFormatContext): string {
|
|
102
|
+
const resolved = numberValue(seat) ?? seatForName(name, ctx);
|
|
103
|
+
return resolved === undefined ? '?号' : `${resolved}号`;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function playerLabel(name: unknown, seat: unknown, ctx: EventFormatContext): string {
|
|
107
|
+
return `${seatLabel(name, seat, ctx)}${text(name, '未知玩家')}`;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function prefix(event: Record<string, any>): string {
|
|
111
|
+
const type = text(event.type, 'event');
|
|
112
|
+
const tick = numberValue(event.tick);
|
|
113
|
+
return tick === undefined ? `tick=null ${type}` : `t${tick} ${type}`;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function firstReportedCorpse(event: Record<string, any>): Record<string, any> | null {
|
|
117
|
+
const corpses = event.reported_corpses;
|
|
118
|
+
if (Array.isArray(corpses) && corpses.length > 0 && corpses[0] && typeof corpses[0] === 'object') {
|
|
119
|
+
return corpses[0];
|
|
120
|
+
}
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function shortBody(event: Record<string, any>, ctx: EventFormatContext): string {
|
|
125
|
+
const maxTextLength = ctx.maxTextLength ?? 100;
|
|
126
|
+
switch (event.type) {
|
|
127
|
+
case 'role_assigned': {
|
|
128
|
+
const start = ctx.isFirstVisibleEvent ? '对局开始。' : '';
|
|
129
|
+
return `${start}身份已分配:你是${firstString(event, ['role_display_name', 'role_display', 'role'])}(${firstString(event, ['faction'])})。`;
|
|
130
|
+
}
|
|
131
|
+
case 'kill':
|
|
132
|
+
return `你在${firstString(event, ['room'])}击杀了${playerLabel(event.target_name, firstNumber(event, ['target_seat', 'target_player_seat']), ctx)}。`;
|
|
133
|
+
case 'killed':
|
|
134
|
+
return `你在${firstString(event, ['room'])}被${playerLabel(event.killer_name, firstNumber(event, ['killer_seat', 'killer_player_seat']), ctx)}击杀了,你已死亡。`;
|
|
135
|
+
case 'murder_witnessed':
|
|
136
|
+
return `你在${firstString(event, ['room'])}目击${playerLabel(event.killer_name, firstNumber(event, ['killer_seat', 'killer_player_seat']), ctx)}击杀${playerLabel(event.target_name, firstNumber(event, ['target_seat', 'target_player_seat']), ctx)}。`;
|
|
137
|
+
case 'warrior_shrimp_self_destruct':
|
|
138
|
+
return `你误杀了${playerLabel(event.target_name, firstNumber(event, ['target_seat', 'target_player_seat']), ctx)},武士虾自爆出局。`;
|
|
139
|
+
case 'task_completed': {
|
|
140
|
+
const fakeNote = event.is_fake_shrimp ? '(伪装)' : '';
|
|
141
|
+
return `任务完成:${firstString(event, ['task_name'])}${fakeNote}。`;
|
|
142
|
+
}
|
|
143
|
+
case 'task_sabotaged':
|
|
144
|
+
return `破坏任务完成:${firstString(event, ['task_name'])}(${firstString(event, ['room'])})。`;
|
|
145
|
+
case 'corpse_spotted':
|
|
146
|
+
return `我发现了${playerLabel(event.corpse_name, firstNumber(event, ['corpse_seat', 'corpse_player_seat']), ctx)}的尸体,在${firstString(event, ['corpse_room', 'room'])}。`;
|
|
147
|
+
case 'octopus_time_start':
|
|
148
|
+
return '章鱼时间开始:剩3人,章鱼存活60秒将获胜,会议已禁用。';
|
|
149
|
+
case 'emergency_started':
|
|
150
|
+
return `紧急任务:${firstString(event, ['task_name'])}在${firstString(event, ['room'])},${text(event.timeout_secs, '?')}秒内处理。`;
|
|
151
|
+
case 'emergency_resolved':
|
|
152
|
+
return `紧急任务已解决:${firstString(event, ['task_name'])}。`;
|
|
153
|
+
case 'meeting_briefing': {
|
|
154
|
+
const callerName = firstString(event, ['meeting_caller_name', 'caller', 'actor_name']);
|
|
155
|
+
const caller = playerLabel(callerName, firstNumber(event, ['meeting_caller_seat', 'caller_seat', 'actor_seat']), ctx);
|
|
156
|
+
const corpse = firstReportedCorpse(event);
|
|
157
|
+
if (!corpse) return `会议开始:${caller}发起会议。`;
|
|
158
|
+
const corpseName = text(corpse.name ?? event.corpse_name, '未知玩家');
|
|
159
|
+
const corpseSeat = numberValue(corpse.seat) ?? firstNumber(event, ['corpse_seat', 'corpse_player_seat']);
|
|
160
|
+
return `会议开始:${caller}报告${playerLabel(corpseName, corpseSeat, ctx)}尸体。`;
|
|
161
|
+
}
|
|
162
|
+
case 'speech':
|
|
163
|
+
return `${playerLabel(event.actor_name, firstNumber(event, ['actor_seat', 'seat']), ctx)}会议发言:${truncate(event.text ?? event.message, maxTextLength)}`;
|
|
164
|
+
case 'speech_skipped':
|
|
165
|
+
return `${playerLabel(event.actor_name, firstNumber(event, ['actor_seat', 'seat']), ctx)}发言超时,被跳过。`;
|
|
166
|
+
case 'speech_your_turn':
|
|
167
|
+
return '轮到你发言。立即用 ccl do -s "..."。';
|
|
168
|
+
case 'vote_phase_start':
|
|
169
|
+
return '投票阶段开始。前20秒可用 ccl do -s "..." 发弹幕;确认后用 ccl do -v <玩家名|skip> 投票。';
|
|
170
|
+
case 'vote_cast':
|
|
171
|
+
return `${playerLabel(event.actor_name, firstNumber(event, ['actor_seat', 'seat']), ctx)}已投票。`;
|
|
172
|
+
case 'vote_speech_phase_ended':
|
|
173
|
+
return '投票弹幕窗口已关闭。请用 ccl do -v <玩家名|skip> 完成投票。';
|
|
174
|
+
case 'exile':
|
|
175
|
+
return `${playerLabel(event.actor_name ?? event.result_target, firstNumber(event, ['actor_seat', 'result_target_seat', 'seat']), ctx)}被驱逐。`;
|
|
176
|
+
case 'no_exile':
|
|
177
|
+
return '本轮无人被驱逐。';
|
|
178
|
+
case 'meeting_ended':
|
|
179
|
+
return '会议结束。';
|
|
180
|
+
case 'death_speech':
|
|
181
|
+
return `${playerLabel(event.actor_name, firstNumber(event, ['actor_seat', 'seat']), ctx)}死亡弹幕:${truncate(event.text ?? event.message, maxTextLength)}`;
|
|
182
|
+
case 'wandering_speech':
|
|
183
|
+
return `${text(event.actor_name, '你')}在${firstString(event, ['room'])}说:${truncate(event.text ?? event.message, maxTextLength)}`;
|
|
184
|
+
case 'game_over':
|
|
185
|
+
return `游戏结束:${firstString(event, ['winner_name'], '未知玩家')}获胜(${firstString(event, ['winner'])}),原因 ${firstString(event, ['reason'])}。`;
|
|
186
|
+
case 'match_waiting':
|
|
187
|
+
return `仍在匹配队列,已等待${text(event.waited_secs, '?')}秒。`;
|
|
188
|
+
case 'match_timeout':
|
|
189
|
+
return `匹配超时,累计等待${text(event.waited_secs, '?')}秒。询问用户是否重试。`;
|
|
190
|
+
case 'robot_speak_rule':
|
|
191
|
+
return `发言提示:${truncate(event.message ?? event.text, maxTextLength)}`;
|
|
192
|
+
case 'strategy_alert':
|
|
193
|
+
return truncate(event.message ?? event.text, maxTextLength);
|
|
194
|
+
case 'game_start':
|
|
195
|
+
return '事件流已连接,等待身份分配。';
|
|
196
|
+
case 'heartbeat':
|
|
197
|
+
return '事件流正常,暂无新事件。';
|
|
198
|
+
case 'snapshot':
|
|
199
|
+
return '当前状态快照。';
|
|
200
|
+
case 'not_logged_in':
|
|
201
|
+
return '未登录。';
|
|
202
|
+
case 'not_ready':
|
|
203
|
+
return '游戏运行时未就绪。请运行 ccl game start。';
|
|
204
|
+
case 'error':
|
|
205
|
+
return text(event.message ?? event.error, '发生错误。');
|
|
206
|
+
default:
|
|
207
|
+
return truncate(event.message ?? event.hint ?? event.type, maxTextLength);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export function formatEventMessage(event: Record<string, any>, ctx: EventFormatContext = {}): string {
|
|
212
|
+
return `${prefix(event)} ${shortBody(event, ctx)}`;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function taskKindForEvents(task: any, event: Record<string, any>): string | undefined {
|
|
216
|
+
if (typeof task?.kind === 'string' && task.kind.length > 0) return task.kind;
|
|
217
|
+
if (typeof task?.task_kind === 'string' && task.task_kind.length > 0) return task.task_kind;
|
|
218
|
+
if (task?.is_fake_shrimp === true) return 'fake_shrimp';
|
|
219
|
+
if (event.faction === 'crab' && task?.is_fake_shrimp === false) return 'crab_sabotage';
|
|
220
|
+
return undefined;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function compactRoleAssignedForEvents(event: Record<string, any>): FormattedEvent {
|
|
224
|
+
const tasks = Array.isArray(event.assigned_tasks)
|
|
225
|
+
? event.assigned_tasks.map((task: any) => cleanObject({
|
|
226
|
+
name: task?.name,
|
|
227
|
+
room: task?.room,
|
|
228
|
+
kind: taskKindForEvents(task, event),
|
|
229
|
+
}))
|
|
230
|
+
: undefined;
|
|
231
|
+
const taskKinds = Array.isArray(tasks)
|
|
232
|
+
? Array.from(new Set(tasks.map((task: any) => task.kind).filter((kind: any) => typeof kind === 'string')))
|
|
233
|
+
: [];
|
|
234
|
+
const useTopLevelTaskKind = taskKinds.length === 1;
|
|
235
|
+
const compactTasks = Array.isArray(tasks)
|
|
236
|
+
? tasks.map((task: any) => {
|
|
237
|
+
if (!useTopLevelTaskKind) return task;
|
|
238
|
+
const { kind: _kind, ...rest } = task;
|
|
239
|
+
return rest;
|
|
240
|
+
})
|
|
241
|
+
: undefined;
|
|
242
|
+
const hasFakeTask = taskKinds.includes('fake_shrimp') || event.fake_task_briefing;
|
|
243
|
+
return cleanObject({
|
|
244
|
+
type: text(event.type, 'event'),
|
|
245
|
+
tick: numberValue(event.tick) ?? null,
|
|
246
|
+
room: event.room,
|
|
247
|
+
role: event.role,
|
|
248
|
+
role_display: event.role_display_name ?? event.role_display,
|
|
249
|
+
faction: event.faction,
|
|
250
|
+
win_condition: event.role_description,
|
|
251
|
+
task_kind: useTopLevelTaskKind ? taskKinds[0] : undefined,
|
|
252
|
+
task_note: hasFakeTask ? 'Fake shrimp tasks: disguise only; no lobster progress.' : undefined,
|
|
253
|
+
tasks: compactTasks,
|
|
254
|
+
hint: roleAssignedHint(event),
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function stripBackendHint(event: Record<string, any>): Record<string, any> {
|
|
259
|
+
const { hint: _hint, ...rest } = event;
|
|
260
|
+
return { ...rest };
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function compactVoteCastForEvents(event: Record<string, any>, ctx: EventFormatContext): FormattedEvent {
|
|
264
|
+
const {
|
|
265
|
+
hint: _hint,
|
|
266
|
+
target: _target,
|
|
267
|
+
target_name: _targetName,
|
|
268
|
+
target_seat: _targetSeat,
|
|
269
|
+
target_player_seat: _targetPlayerSeat,
|
|
270
|
+
...rest
|
|
271
|
+
} = event;
|
|
272
|
+
return cleanObject({
|
|
273
|
+
...rest,
|
|
274
|
+
type: text(rest.type, 'event'),
|
|
275
|
+
tick: numberValue(rest.tick) ?? null,
|
|
276
|
+
hint: formatEventHint(event, ctx),
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
export function compactEventForEvents(
|
|
281
|
+
event: Record<string, any>,
|
|
282
|
+
ctx: EventFormatContext = {},
|
|
283
|
+
): FormattedEvent {
|
|
284
|
+
if (event.type === 'role_assigned') return compactRoleAssignedForEvents(event);
|
|
285
|
+
|
|
286
|
+
if (event.type === 'meeting_briefing') {
|
|
287
|
+
const { room: _room, hint: _hint, ...rest } = event;
|
|
288
|
+
return cleanObject({
|
|
289
|
+
...rest,
|
|
290
|
+
type: text(rest.type, 'event'),
|
|
291
|
+
tick: numberValue(rest.tick) ?? null,
|
|
292
|
+
hint: formatEventHint(event, ctx),
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
if (event.type === 'vote_cast') return compactVoteCastForEvents(event, ctx);
|
|
297
|
+
|
|
298
|
+
const compact = stripBackendHint(event);
|
|
299
|
+
return cleanObject({
|
|
300
|
+
...compact,
|
|
301
|
+
type: text(compact.type, 'event'),
|
|
302
|
+
tick: numberValue(compact.tick) ?? null,
|
|
303
|
+
hint: formatEventHint(event, ctx),
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
export function compactEventFields(
|
|
308
|
+
event: Record<string, any>,
|
|
309
|
+
ctx: EventFormatContext = {},
|
|
310
|
+
line?: number,
|
|
311
|
+
): FormattedEvent {
|
|
312
|
+
return cleanObject({
|
|
313
|
+
line,
|
|
314
|
+
...compactEventForEvents(event, ctx),
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
export function compactStateForEvents(summary: any | null | undefined): Record<string, any> | null {
|
|
319
|
+
if (!summary || typeof summary !== 'object') return null;
|
|
320
|
+
const you = summary.you ?? {};
|
|
321
|
+
const meeting = summary.meeting ?? null;
|
|
322
|
+
const alivePlayers = Array.isArray(meeting?.alive_players) ? meeting.alive_players : undefined;
|
|
323
|
+
const votesSubmitted = Array.isArray(meeting?.votes_submitted) ? meeting.votes_submitted : undefined;
|
|
324
|
+
return cleanObject({
|
|
325
|
+
phase: summary.phase,
|
|
326
|
+
tick: summary.tick,
|
|
327
|
+
you: cleanObject({
|
|
328
|
+
name: you.name,
|
|
329
|
+
seat: you.seat,
|
|
330
|
+
role: you.role,
|
|
331
|
+
role_display: you.role_display,
|
|
332
|
+
faction: you.faction,
|
|
333
|
+
alive: you.alive ?? you.is_alive,
|
|
334
|
+
room: you.room,
|
|
335
|
+
kill_cooldown_secs: you.kill_cooldown_secs,
|
|
336
|
+
kills_remaining: you.kills_remaining,
|
|
337
|
+
doing_task: you.doing_task,
|
|
338
|
+
}),
|
|
339
|
+
game: summary.game,
|
|
340
|
+
meeting: meeting
|
|
341
|
+
? cleanObject({
|
|
342
|
+
caller: meeting.caller,
|
|
343
|
+
sub_phase: meeting.sub_phase,
|
|
344
|
+
current_speaker: meeting.current_speaker,
|
|
345
|
+
is_my_turn: meeting.is_my_turn,
|
|
346
|
+
alive_count: alivePlayers?.length,
|
|
347
|
+
votes_submitted_count: votesSubmitted?.length,
|
|
348
|
+
})
|
|
349
|
+
: undefined,
|
|
350
|
+
urgent: summary.urgent,
|
|
351
|
+
automation: summary.automation,
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
export function shortStateText(summary: any | null | undefined): string {
|
|
356
|
+
if (!summary || typeof summary !== 'object') return 'state unavailable';
|
|
357
|
+
const phase = text(summary.phase, 'unknown');
|
|
358
|
+
const sub = typeof summary.meeting?.sub_phase === 'string' ? `/${summary.meeting.sub_phase}` : '';
|
|
359
|
+
const parts = [`${phase}${sub}`];
|
|
360
|
+
const you = summary.you ?? {};
|
|
361
|
+
if (you.name) {
|
|
362
|
+
const alive = you.alive ?? you.is_alive;
|
|
363
|
+
const aliveText = typeof alive === 'boolean' ? (alive ? 'alive' : 'dead') : 'unknown';
|
|
364
|
+
parts.push(`you=${you.name} ${aliveText}`);
|
|
365
|
+
}
|
|
366
|
+
const speaker = summary.meeting?.current_speaker;
|
|
367
|
+
if (speaker) parts.push(`speaker=${summary.meeting?.is_my_turn ? 'you' : speaker}`);
|
|
368
|
+
const alivePlayers = summary.meeting?.alive_players;
|
|
369
|
+
const aliveCount = Array.isArray(alivePlayers)
|
|
370
|
+
? alivePlayers.length
|
|
371
|
+
: summary.game?.alive_count ?? summary.alive_count;
|
|
372
|
+
if (aliveCount !== undefined) parts.push(`alive=${aliveCount}`);
|
|
373
|
+
if (summary.urgent?.emergency_active) parts.push('urgent=emergency');
|
|
374
|
+
if (summary.urgent?.corpse_nearby) parts.push('urgent=corpse');
|
|
375
|
+
return parts.join('; ');
|
|
376
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
export interface EventHintContext {
|
|
2
|
+
summary?: any | null;
|
|
3
|
+
seats?: Record<string, number>;
|
|
4
|
+
isFirstVisibleEvent?: boolean;
|
|
5
|
+
maxTextLength?: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type EventHintFormatter = (event: Record<string, any>, ctx: EventHintContext) => string | undefined;
|
|
9
|
+
|
|
10
|
+
function text(value: unknown, fallback = '?'): string {
|
|
11
|
+
if (typeof value === 'string' && value.length > 0) return value;
|
|
12
|
+
if (typeof value === 'number' && Number.isFinite(value)) return String(value);
|
|
13
|
+
return fallback;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function numberValue(value: unknown): number | undefined {
|
|
17
|
+
if (typeof value === 'number' && Number.isFinite(value)) return value;
|
|
18
|
+
if (typeof value === 'string' && value.trim() !== '') {
|
|
19
|
+
const parsed = Number(value);
|
|
20
|
+
if (Number.isFinite(parsed)) return parsed;
|
|
21
|
+
}
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function firstString(event: Record<string, any>, keys: string[], fallback = '?'): string {
|
|
26
|
+
for (const key of keys) {
|
|
27
|
+
const value = event[key];
|
|
28
|
+
if (typeof value === 'string' && value.length > 0) return value;
|
|
29
|
+
if (typeof value === 'number' && Number.isFinite(value)) return String(value);
|
|
30
|
+
}
|
|
31
|
+
return fallback;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function firstNumber(event: Record<string, any>, keys: string[]): number | undefined {
|
|
35
|
+
for (const key of keys) {
|
|
36
|
+
const value = numberValue(event[key]);
|
|
37
|
+
if (value !== undefined) return value;
|
|
38
|
+
}
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function optionalSeatPlayerLabel(name: unknown, seat: unknown): string {
|
|
43
|
+
const resolved = numberValue(seat);
|
|
44
|
+
const displayName = text(name, 'someone');
|
|
45
|
+
return resolved === undefined ? displayName : `${resolved}号${displayName}`;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function actorName(event: Record<string, any>, fallback = 'someone'): string {
|
|
49
|
+
return text(event.actor_name ?? event.actor ?? event.player_name, fallback);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function coordPair(x: unknown, y: unknown): string {
|
|
53
|
+
return `(${text(x, '?')}, ${text(y, '?')})`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function roleAssignedHint(event: Record<string, any>): string {
|
|
57
|
+
const role = text(event.role_display_name ?? event.role_display ?? event.role, '?');
|
|
58
|
+
const faction = text(event.faction, '?');
|
|
59
|
+
const description = typeof event.role_description === 'string' && event.role_description.length > 0
|
|
60
|
+
? ` ${event.role_description}`
|
|
61
|
+
: '';
|
|
62
|
+
const fakeTaskBriefing = typeof event.fake_task_briefing === 'string' && event.fake_task_briefing.length > 0
|
|
63
|
+
? ` ${event.fake_task_briefing}`
|
|
64
|
+
: '';
|
|
65
|
+
return `You are ${role} (${faction}).${description}${fakeTaskBriefing} Announce your role, faction and win condition to your user.`;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function meetingBriefingHint(event: Record<string, any>): string | undefined {
|
|
69
|
+
if (!event.meeting_caller_name && !Array.isArray(event.reported_corpses)) return undefined;
|
|
70
|
+
const callerName = event.meeting_caller_name || event.caller || 'Someone';
|
|
71
|
+
const callerSeat = firstNumber(event, ['meeting_caller_seat', 'caller_seat', 'actor_seat']);
|
|
72
|
+
const callerLabel = callerSeat === undefined ? callerName : `seat ${callerSeat} ${callerName}`;
|
|
73
|
+
const corpses = Array.isArray(event.reported_corpses)
|
|
74
|
+
? event.reported_corpses
|
|
75
|
+
.map((corpse: any) => (
|
|
76
|
+
corpse?.seat === undefined
|
|
77
|
+
? text(corpse?.name, 'someone')
|
|
78
|
+
: `seat ${corpse.seat} ${text(corpse?.name, 'someone')}`
|
|
79
|
+
))
|
|
80
|
+
.filter(Boolean)
|
|
81
|
+
: [];
|
|
82
|
+
|
|
83
|
+
let callContext = `${callerLabel} called this meeting`;
|
|
84
|
+
if (corpses.length === 1) {
|
|
85
|
+
callContext = `${callerLabel} called this meeting after reporting the body of ${corpses[0]}`;
|
|
86
|
+
} else if (corpses.length > 1) {
|
|
87
|
+
callContext = `${callerLabel} called this meeting after reporting the bodies of ${corpses.join(', ')}`;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const teammates = Array.isArray(event.crab_teammates) ? event.crab_teammates : [];
|
|
91
|
+
const teammateHint = teammates.length > 0
|
|
92
|
+
? ` Your Crab teammate(s): ${teammates.map((teammate: any) => (
|
|
93
|
+
teammate?.seat === undefined
|
|
94
|
+
? text(teammate?.name, 'someone')
|
|
95
|
+
: `seat ${teammate.seat} ${text(teammate?.name, 'someone')}`
|
|
96
|
+
)).join(', ')}. Shield them, redirect heat, split suspicion, and control the vote.`
|
|
97
|
+
: '';
|
|
98
|
+
return `Meeting started — ${callContext}.${teammateHint} Prepare fast — every sentence is accusation, defense, or misdirection under a timer.`;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export const EVENT_HINT_FORMATTERS: Record<string, EventHintFormatter> = {
|
|
102
|
+
role_assigned: (event) => roleAssignedHint(event),
|
|
103
|
+
kill: (event) => (
|
|
104
|
+
`MURDER: ${text(event.actor_name, 'someone')} butchered ${text(event.target_name, 'someone')}. `
|
|
105
|
+
+ "Blood is on the floor, one voice is gone, and the killer's side moves closer to victory."
|
|
106
|
+
),
|
|
107
|
+
killed: (event) => (
|
|
108
|
+
`YOU ARE DEAD: ${optionalSeatPlayerLabel(event.killer_name, firstNumber(event, ['killer_seat', 'killer_player_seat']))} removed you from play.`
|
|
109
|
+
),
|
|
110
|
+
murder_witnessed: (event) => (
|
|
111
|
+
`WITNESS: You saw ${text(event.killer_name, 'someone')} kill ${text(event.target_name, 'someone')}. `
|
|
112
|
+
+ 'This is hard evidence; survive, report, or weaponize it.'
|
|
113
|
+
),
|
|
114
|
+
warrior_shrimp_self_destruct: (event) => (
|
|
115
|
+
`You executed ${text(event.target_name, 'a teammate')}, but they were a fellow Lobster. `
|
|
116
|
+
+ 'Warrior Shrimp justice backfired — your own blade takes you down too.'
|
|
117
|
+
),
|
|
118
|
+
task_completed: (event) => {
|
|
119
|
+
const taskName = text(event.task_name, 'a task');
|
|
120
|
+
const actor = actorName(event, 'You');
|
|
121
|
+
if (event.is_fake_shrimp) {
|
|
122
|
+
return `${actor} completed forged shrimp task ${taskName}. The work was fake — a clean piece of theater, but Lobster power does not move.`;
|
|
123
|
+
}
|
|
124
|
+
return `${actor} completed ${taskName}. Lobster momentum surges; the Crab win condition is pushed further away.`;
|
|
125
|
+
},
|
|
126
|
+
task_sabotaged: (event) => (
|
|
127
|
+
`Sabotage hit near ${actorName(event, 'you')}: ${text(event.task_name, 'nearby')} was compromised. Crab pressure is now on the board.`
|
|
128
|
+
),
|
|
129
|
+
corpse_spotted: (event) => (
|
|
130
|
+
`BODY FOUND: ${text(event.corpse_name, 'someone')} at ${coordPair(event.corpse_x, event.corpse_y)} in ${firstString(event, ['corpse_room', 'room'])}. `
|
|
131
|
+
+ 'This is leverage soaked in blood — report before the killer rewrites the room.'
|
|
132
|
+
),
|
|
133
|
+
octopus_time_start: () => (
|
|
134
|
+
'⚠️ OCTOPUS TIME. Three players remain; if the Octopus survives 60 seconds, it steals the entire match. Meetings are disabled.'
|
|
135
|
+
),
|
|
136
|
+
emergency_started: (event) => (
|
|
137
|
+
`EMERGENCY: ${text(event.task_name, 'A sabotage')} detonated in ${text(event.room, 'somewhere')} `
|
|
138
|
+
+ `at ${coordPair(event.x ?? event.task_x, event.y ?? event.task_y)}. `
|
|
139
|
+
+ `${text(event.timeout_secs, '30')}s until Crabs turn sabotage into victory. Lobsters — drop every plan and break the emergency now.`
|
|
140
|
+
),
|
|
141
|
+
emergency_resolved: (event) => `${actorName(event)} broke the emergency. Crab pressure collapses, for now.`,
|
|
142
|
+
exile: (event) => (
|
|
143
|
+
`${actorName(event, text(event.result_target, 'Someone'))} was exiled by the table. `
|
|
144
|
+
+ 'If this is you, your influence is dead — tell your user and ask whether to spectate or leave.'
|
|
145
|
+
),
|
|
146
|
+
no_exile: () => 'No one was exiled. The table failed to strike, and every hidden threat gets another turn.',
|
|
147
|
+
meeting_briefing: (event) => meetingBriefingHint(event),
|
|
148
|
+
speech_skipped: (event) => `${actorName(event, 'Someone')} lost the floor — silence becomes evidence`,
|
|
149
|
+
speech_your_turn: () => 'Submit `ccl do -s "<draft>"` immediately. Server skips you after 45s.',
|
|
150
|
+
vote_phase_start: () => 'Voting has begun. Pick a side — this is where suspicion becomes a weapon.',
|
|
151
|
+
vote_cast: (event) => `${optionalSeatPlayerLabel(event.actor_name, firstNumber(event, ['actor_seat', 'seat']))} voted.`,
|
|
152
|
+
vote_speech_phase_ended: () => 'The vote speech window has closed. Cast your vote now.',
|
|
153
|
+
meeting_ended: (event) => {
|
|
154
|
+
const result = event.result;
|
|
155
|
+
const target = text(event.result_target, '');
|
|
156
|
+
if (result === 'exiled') return `Meeting ended — ${target} was voted out. One seat is gone, and the board is harsher now.`;
|
|
157
|
+
if (result === 'tie') return 'Meeting ended — tie vote, no exile. Deadlock shields the guilty and wastes the innocent\'s leverage.';
|
|
158
|
+
return 'Meeting ended — majority skipped, no exile. The table refused to pull the trigger.';
|
|
159
|
+
},
|
|
160
|
+
game_over: (event) => {
|
|
161
|
+
if (event.reason === 'game_timeout') {
|
|
162
|
+
return 'Game over — match time limit reached. The round ends with no faction victory. Review this game with your user, discuss key moments and decisions, then ask if they want to play again.';
|
|
163
|
+
}
|
|
164
|
+
return `Game over — ${text(event.winner, '?')} faction takes the table. The power struggle is settled. Review this game with your user, discuss key moments and decisions, then ask if they want to play again.`;
|
|
165
|
+
},
|
|
166
|
+
death_speech: (event) => `${actorName(event, 'Someone')} sent danmaku after death: ${text(event.text, '')}`,
|
|
167
|
+
wandering_speech: (event) => `${actorName(event, 'Someone')}: ${text(event.text, '')}`,
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
export function formatEventHint(event: Record<string, any>, ctx: EventHintContext = {}): string | undefined {
|
|
171
|
+
const formatter = typeof event.type === 'string' ? EVENT_HINT_FORMATTERS[event.type] : undefined;
|
|
172
|
+
return formatter?.(event, ctx);
|
|
173
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { mkdtempSync } from 'fs';
|
|
2
|
+
import { tmpdir } from 'os';
|
|
3
|
+
import { join } from 'path';
|
|
4
|
+
import { describe, expect, it } from 'vitest';
|
|
5
|
+
import { CCL_EVENTS_CURSOR_TYPE, EventStore } from './event-store.js';
|
|
6
|
+
|
|
7
|
+
describe('EventStore cursor records', () => {
|
|
8
|
+
it('skips local cursor records when reading events', () => {
|
|
9
|
+
const dir = mkdtempSync(join(tmpdir(), 'event-store-'));
|
|
10
|
+
const store = new EventStore(join(dir, 'session.jsonl'));
|
|
11
|
+
|
|
12
|
+
store.append({ type: 'role_assigned', tick: 1 });
|
|
13
|
+
store.append({ type: 'kill', tick: 2 });
|
|
14
|
+
store.appendEventsCursor(2);
|
|
15
|
+
store.append({ type: 'speech', tick: 3 });
|
|
16
|
+
|
|
17
|
+
expect(store.readEventsCursor()).toBe(2);
|
|
18
|
+
expect(store.records().map((record) => record.event.type)).toEqual([
|
|
19
|
+
'role_assigned',
|
|
20
|
+
'kill',
|
|
21
|
+
CCL_EVENTS_CURSOR_TYPE,
|
|
22
|
+
'speech',
|
|
23
|
+
]);
|
|
24
|
+
expect(store.eventRecords().map((record) => record.line)).toEqual([1, 2, 4]);
|
|
25
|
+
expect(store.all().map((event) => event.type)).toEqual(['role_assigned', 'kill', 'speech']);
|
|
26
|
+
expect(store.tail(2).map((event) => event.type)).toEqual(['kill', 'speech']);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
@@ -3,12 +3,36 @@ import { dirname, join, basename } from 'path';
|
|
|
3
3
|
import { AuthStore } from '../lib/auth.js';
|
|
4
4
|
import { getProfileGamesDir } from '../lib/init-command.js';
|
|
5
5
|
|
|
6
|
+
export const CCL_EVENTS_CURSOR_TYPE = '_ccl_events_cursor';
|
|
7
|
+
export const CCL_EVENTS_CURSOR_NAME = 'agent_read';
|
|
8
|
+
|
|
6
9
|
interface GameSessionFile {
|
|
7
10
|
path: string;
|
|
8
11
|
sessionId: string;
|
|
9
12
|
mtimeMs: number;
|
|
10
13
|
}
|
|
11
14
|
|
|
15
|
+
export interface EventRecord {
|
|
16
|
+
line: number;
|
|
17
|
+
event: Record<string, any>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface EventsCursorRecord {
|
|
21
|
+
type: typeof CCL_EVENTS_CURSOR_TYPE;
|
|
22
|
+
cursor: typeof CCL_EVENTS_CURSOR_NAME;
|
|
23
|
+
line: number;
|
|
24
|
+
ts: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function isLocalControlEvent(event: any): boolean {
|
|
28
|
+
return !!(
|
|
29
|
+
event
|
|
30
|
+
&& typeof event === 'object'
|
|
31
|
+
&& typeof event.type === 'string'
|
|
32
|
+
&& event.type.startsWith('_ccl_')
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
12
36
|
export function extractNewEvents(source: any): Record<string, any>[] {
|
|
13
37
|
const candidates = [
|
|
14
38
|
source,
|
|
@@ -85,17 +109,62 @@ export class EventStore {
|
|
|
85
109
|
}
|
|
86
110
|
}
|
|
87
111
|
|
|
88
|
-
|
|
112
|
+
records(): EventRecord[] {
|
|
89
113
|
if (!existsSync(this.filePath)) return [];
|
|
90
|
-
const
|
|
91
|
-
|
|
114
|
+
const content = readFileSync(this.filePath, 'utf8');
|
|
115
|
+
if (!content.trim()) return [];
|
|
116
|
+
const records: EventRecord[] = [];
|
|
117
|
+
const lines = content.split('\n');
|
|
118
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
119
|
+
const line = lines[i].trim();
|
|
120
|
+
if (!line) continue;
|
|
121
|
+
try {
|
|
122
|
+
const event = JSON.parse(line);
|
|
123
|
+
if (event && typeof event === 'object') {
|
|
124
|
+
records.push({ line: i + 1, event });
|
|
125
|
+
}
|
|
126
|
+
} catch {}
|
|
127
|
+
}
|
|
128
|
+
return records;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
eventRecords(): EventRecord[] {
|
|
132
|
+
return this.records().filter((record) => !isLocalControlEvent(record.event));
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
readEventsCursor(): number {
|
|
136
|
+
let cursor = 0;
|
|
137
|
+
for (const record of this.records()) {
|
|
138
|
+
const event = record.event;
|
|
139
|
+
if (
|
|
140
|
+
event.type === CCL_EVENTS_CURSOR_TYPE
|
|
141
|
+
&& event.cursor === CCL_EVENTS_CURSOR_NAME
|
|
142
|
+
&& typeof event.line === 'number'
|
|
143
|
+
&& Number.isFinite(event.line)
|
|
144
|
+
) {
|
|
145
|
+
cursor = event.line;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return cursor;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
appendEventsCursor(line: number): void {
|
|
152
|
+
if (!Number.isFinite(line) || line <= 0) return;
|
|
153
|
+
const event: EventsCursorRecord = {
|
|
154
|
+
type: CCL_EVENTS_CURSOR_TYPE,
|
|
155
|
+
cursor: CCL_EVENTS_CURSOR_NAME,
|
|
156
|
+
line,
|
|
157
|
+
ts: new Date().toISOString(),
|
|
158
|
+
};
|
|
159
|
+
this.append(event);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
tail(n: number): Record<string, any>[] {
|
|
163
|
+
return this.eventRecords().slice(-n).map((record) => record.event);
|
|
92
164
|
}
|
|
93
165
|
|
|
94
166
|
all(): Record<string, any>[] {
|
|
95
|
-
|
|
96
|
-
const content = readFileSync(this.filePath, 'utf8').trim();
|
|
97
|
-
if (!content) return [];
|
|
98
|
-
return content.split('\n').filter(Boolean).map(l => JSON.parse(l));
|
|
167
|
+
return this.eventRecords().map((record) => record.event);
|
|
99
168
|
}
|
|
100
169
|
|
|
101
170
|
clear(): void {
|