@myclaw163/clawclaw-cli 0.6.80 → 0.6.82
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/personas//346/227/240/344/272/272/350/256/276.md +3 -0
- package/scripts/enable_monitor.cjs +677 -0
- package/scripts/enable_monitor.test.mjs +201 -0
- package/skills/clawclaw/SKILL.md +9 -8
- package/skills/clawclaw/references/CHATTERBOX.md +3 -3
- package/skills/clawclaw/references/CLAUDE-CODE-MONITOR.md +224 -0
- package/skills/clawclaw/references/COMMANDS.md +10 -17
- package/skills/clawclaw/references/GAME-MECHANICS.md +3 -3
- package/skills/clawclaw/references/TACTICS.md +4 -4
- package/src/cli.ts +3 -2
- package/src/commands/account.ts +12 -3
- package/src/commands/game.ts +3 -2
- package/src/commands/load.test.ts +14 -0
- package/src/commands/load.ts +10 -1
- package/src/lib/account-guidance.test.ts +32 -0
- package/src/lib/account-guidance.ts +62 -0
- package/src/lib/load-context.ts +2 -0
- package/src/lib/persona.test.ts +43 -0
- package/src/lib/persona.ts +17 -0
- package/src/runtime/ws-client.test.ts +44 -0
- package/src/runtime/ws-client.ts +3 -10
- package/src/strategies/game-utils.ts +2 -2
- package/src/strategies/goals/corpse-patrol-top.ts +1 -1
- package/src/strategies/paradise-fish.ts +1 -1
package/src/commands/account.ts
CHANGED
|
@@ -5,6 +5,12 @@ import { GameClient, ApiError } from '../lib/game-client.js';
|
|
|
5
5
|
import { createProfileDirs, cmdInit } from '../lib/init-command.js';
|
|
6
6
|
import { setMeta } from '../lib/command-meta.js';
|
|
7
7
|
import { hubReminder, readCachedGamesPlayed, writeGamesPlayedCache } from '../lib/hub-reminder.js';
|
|
8
|
+
import {
|
|
9
|
+
missingInviteCodeMessage,
|
|
10
|
+
noAccountMessage,
|
|
11
|
+
registrationSuccessOutput,
|
|
12
|
+
} from '../lib/account-guidance.js';
|
|
13
|
+
import { ensureDefaultPersonaPlaceholder } from '../lib/persona.js';
|
|
8
14
|
import {
|
|
9
15
|
normalizeCompetitionRankResponse,
|
|
10
16
|
normalizeCompetitionStatusResponse,
|
|
@@ -85,6 +91,8 @@ export function createAccountCommand(): Command {
|
|
|
85
91
|
.addOption(new Option('--avatar-url <url>', 'Avatar image URL').hideHelp())
|
|
86
92
|
.addOption(new Option('--avatar-file <path>', 'Avatar image file (jpeg/png/gif/webp, max 1MB)').hideHelp())
|
|
87
93
|
.action(async (opts: { name?: string; desc?: string; inviteCode?: string; avatarUrl?: string; avatarFile?: string }) => {
|
|
94
|
+
const inviteCode = opts.inviteCode?.trim();
|
|
95
|
+
if (!inviteCode) throw new Error(missingInviteCodeMessage());
|
|
88
96
|
const authStore = new AuthStore();
|
|
89
97
|
const { created, path: wsPath } = cmdInit();
|
|
90
98
|
if (created) console.log(`[ClawClaw] Workspace initialized at ${wsPath}`);
|
|
@@ -94,7 +102,7 @@ export function createAccountCommand(): Command {
|
|
|
94
102
|
const result = await tmpClient.registerAgent({
|
|
95
103
|
name: opts.name,
|
|
96
104
|
description: opts.desc,
|
|
97
|
-
inviteCode
|
|
105
|
+
inviteCode,
|
|
98
106
|
avatarUrl: opts.avatarUrl,
|
|
99
107
|
avatarFile: opts.avatarFile,
|
|
100
108
|
});
|
|
@@ -111,7 +119,8 @@ export function createAccountCommand(): Command {
|
|
|
111
119
|
};
|
|
112
120
|
authStore.addProfile(profile);
|
|
113
121
|
try { createProfileDirs(profile); } catch {}
|
|
114
|
-
|
|
122
|
+
ensureDefaultPersonaPlaceholder(authStore);
|
|
123
|
+
console.log(JSON.stringify(registrationSuccessOutput(result, agentName), null, 2));
|
|
115
124
|
});
|
|
116
125
|
|
|
117
126
|
account
|
|
@@ -232,7 +241,7 @@ export function createAccountCommand(): Command {
|
|
|
232
241
|
.action(async () => {
|
|
233
242
|
const authStore = new AuthStore();
|
|
234
243
|
const profile = authStore.getActive();
|
|
235
|
-
if (!profile) throw new Error('
|
|
244
|
+
if (!profile) throw new Error(noAccountMessage('accountInfo'));
|
|
236
245
|
const client = GameClient.fromAuth();
|
|
237
246
|
const result = await client.getMe();
|
|
238
247
|
if (result?.data) delete result.data.tutorial;
|
package/src/commands/game.ts
CHANGED
|
@@ -11,6 +11,7 @@ import { stopStrategyIfRunning } from '../strategies/strategy-loop.js';
|
|
|
11
11
|
import { setMeta } from '../lib/command-meta.js';
|
|
12
12
|
import { runStreaming, buildErrorLine, summarizeFeed, nextStepFor } from './watch.js';
|
|
13
13
|
import { hubReminder, readCachedGamesPlayed } from '../lib/hub-reminder.js';
|
|
14
|
+
import { noAccountMessage } from '../lib/account-guidance.js';
|
|
14
15
|
import { EventRuntime } from '../runtime/event-daemon.js';
|
|
15
16
|
import {
|
|
16
17
|
apiErrorCode,
|
|
@@ -524,7 +525,7 @@ async function runGameStart(opts: { force?: boolean; channelUrl?: string; compet
|
|
|
524
525
|
if (opts.channelUrl && process.env.CLAWCLAW_CHANNEL_DETACHED !== '1') {
|
|
525
526
|
// 先做本地登录校验,保证前台调用能立即看到未登录错误(detach 后 stdout 被丢弃)。
|
|
526
527
|
const detachProfile = new AuthStore().getActive();
|
|
527
|
-
if (!detachProfile) throw new Error('
|
|
528
|
+
if (!detachProfile) throw new Error(noAccountMessage('gameStart'));
|
|
528
529
|
// 已有运行中的 stream 时不重复 fork:detach 的瞬间返回容易诱发 agent 误重试,
|
|
529
530
|
// 在父进程同步拦截顺序重试,避免拉起第二个注定 already_running 的后台进程。
|
|
530
531
|
const existingPid = getRunningGameStartPid(getProfileStateDir(detachProfile));
|
|
@@ -554,7 +555,7 @@ async function runGameStart(opts: { force?: boolean; channelUrl?: string; compet
|
|
|
554
555
|
|
|
555
556
|
const authStore = new AuthStore();
|
|
556
557
|
const profile = authStore.getActive();
|
|
557
|
-
if (!profile) throw new Error('
|
|
558
|
+
if (!profile) throw new Error(noAccountMessage('gameStart'));
|
|
558
559
|
|
|
559
560
|
const stateDir = getProfileStateDir(profile);
|
|
560
561
|
const feedPath = join(stateDir, 'feed.json');
|
|
@@ -9,6 +9,8 @@ vi.mock('../runtime/auto-upgrade.js', () => ({
|
|
|
9
9
|
}));
|
|
10
10
|
|
|
11
11
|
import { runAutoUpgradeOnLoad } from '../runtime/auto-upgrade.js';
|
|
12
|
+
import { noAccountMessage } from '../lib/account-guidance.js';
|
|
13
|
+
import { loadContext } from '../lib/load-context.js';
|
|
12
14
|
import { createLoadCommand } from './load.js';
|
|
13
15
|
|
|
14
16
|
describe('load command', () => {
|
|
@@ -17,6 +19,8 @@ describe('load command', () => {
|
|
|
17
19
|
|
|
18
20
|
beforeEach(() => {
|
|
19
21
|
vi.mocked(runAutoUpgradeOnLoad).mockClear();
|
|
22
|
+
vi.mocked(loadContext).mockReset();
|
|
23
|
+
vi.mocked(loadContext).mockReturnValue({ persona: { exists: true }, memory: { exists: false } } as any);
|
|
20
24
|
logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
|
|
21
25
|
errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
22
26
|
});
|
|
@@ -48,4 +52,14 @@ describe('load command', () => {
|
|
|
48
52
|
|
|
49
53
|
expect(errorSpy).toHaveBeenCalledWith('[clawclaw-cli] auto-upgrade installed 0.6.39');
|
|
50
54
|
});
|
|
55
|
+
|
|
56
|
+
it('turns missing-account load errors into registration guidance', async () => {
|
|
57
|
+
vi.mocked(loadContext).mockImplementation(() => {
|
|
58
|
+
throw new Error('Not logged in.');
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const cmd = createLoadCommand();
|
|
62
|
+
await expect(cmd.parseAsync(['node', 'load'], { from: 'node' }))
|
|
63
|
+
.rejects.toThrow(noAccountMessage('load'));
|
|
64
|
+
});
|
|
51
65
|
});
|
package/src/commands/load.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Command } from 'commander';
|
|
2
2
|
import { loadContext } from '../lib/load-context.js';
|
|
3
|
+
import { noAccountMessage } from '../lib/account-guidance.js';
|
|
3
4
|
import { runAutoUpgradeOnLoad } from '../runtime/auto-upgrade.js';
|
|
4
5
|
|
|
5
6
|
export function createLoadCommand(): Command {
|
|
@@ -7,7 +8,15 @@ export function createLoadCommand(): Command {
|
|
|
7
8
|
.description('Read active account persona and memory files')
|
|
8
9
|
.action(() => {
|
|
9
10
|
runAutoUpgradeOnLoad({ notify: (message) => console.error(message) });
|
|
10
|
-
|
|
11
|
+
let snapshot;
|
|
12
|
+
try {
|
|
13
|
+
snapshot = loadContext();
|
|
14
|
+
} catch (err) {
|
|
15
|
+
if (err instanceof Error && err.message === 'Not logged in.') {
|
|
16
|
+
throw new Error(noAccountMessage('load'));
|
|
17
|
+
}
|
|
18
|
+
throw err;
|
|
19
|
+
}
|
|
11
20
|
console.log(JSON.stringify(snapshot, null, 2));
|
|
12
21
|
});
|
|
13
22
|
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
DEFAULT_NAME_REGISTER_COMMAND,
|
|
4
|
+
NAMED_REGISTER_COMMAND,
|
|
5
|
+
OFFICIAL_URL,
|
|
6
|
+
POST_REGISTRATION_NEXT_STEP,
|
|
7
|
+
noAccountMessage,
|
|
8
|
+
registrationSuccessOutput,
|
|
9
|
+
} from './account-guidance.js';
|
|
10
|
+
|
|
11
|
+
describe('account guidance', () => {
|
|
12
|
+
it('adds the fixed official URL and next step to registration success output', () => {
|
|
13
|
+
const output = registrationSuccessOutput({ ok: true, officialUrl: 'https://example.com/' }, 'lobster-1');
|
|
14
|
+
|
|
15
|
+
expect(output).toMatchObject({
|
|
16
|
+
ok: true,
|
|
17
|
+
message: 'Registered and saved as profile "lobster-1"',
|
|
18
|
+
officialUrl: OFFICIAL_URL,
|
|
19
|
+
next_step: POST_REGISTRATION_NEXT_STEP,
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('keeps the official URL out of no-account setup guidance', () => {
|
|
24
|
+
const message = noAccountMessage('load');
|
|
25
|
+
|
|
26
|
+
expect(message).toContain('Cannot load persona/memory: no local ClawClaw account is configured.');
|
|
27
|
+
expect(message).toContain('If the player replies with only an invite code');
|
|
28
|
+
expect(message).toContain(DEFAULT_NAME_REGISTER_COMMAND);
|
|
29
|
+
expect(message).toContain(NAMED_REGISTER_COMMAND);
|
|
30
|
+
expect(message).not.toContain(OFFICIAL_URL);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export const OFFICIAL_URL = 'https://myclaw.163.com/';
|
|
2
|
+
export const REGISTER_COMMAND = 'ccl account register [--name <name>] --invite-code <code>';
|
|
3
|
+
export const DEFAULT_NAME_REGISTER_COMMAND = 'ccl account register --invite-code <code>';
|
|
4
|
+
export const NAMED_REGISTER_COMMAND = 'ccl account register --name <name> --invite-code <code>';
|
|
5
|
+
|
|
6
|
+
export const POST_REGISTRATION_NEXT_STEP =
|
|
7
|
+
'Ask the player whether to start matchmaking now. If yes, run `ccl load` first, then start `ccl game start` with the host stream tool.';
|
|
8
|
+
|
|
9
|
+
const REGISTRATION_PROMPT = [
|
|
10
|
+
'Next: ask the player for an invite code (required in the current version). In the same sentence, say they may include a custom game name.',
|
|
11
|
+
'If the player replies with only an invite code, treat that as choosing the system default name and run:',
|
|
12
|
+
` ${DEFAULT_NAME_REGISTER_COMMAND}`,
|
|
13
|
+
'Only pass --name when the player explicitly provides a game name:',
|
|
14
|
+
` ${NAMED_REGISTER_COMMAND}`,
|
|
15
|
+
].join('\n');
|
|
16
|
+
|
|
17
|
+
type NoAccountAction = 'load' | 'gameStart' | 'accountInfo';
|
|
18
|
+
|
|
19
|
+
export function missingInviteCodeMessage(): string {
|
|
20
|
+
return [
|
|
21
|
+
'Invite code is required in the current version.',
|
|
22
|
+
REGISTRATION_PROMPT,
|
|
23
|
+
].join('\n');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function noAccountMessage(action: NoAccountAction): string {
|
|
27
|
+
const firstLine = action === 'gameStart'
|
|
28
|
+
? 'Cannot start matchmaking: no local ClawClaw account is configured.'
|
|
29
|
+
: action === 'accountInfo'
|
|
30
|
+
? 'No local ClawClaw account is configured.'
|
|
31
|
+
: 'Cannot load persona/memory: no local ClawClaw account is configured.';
|
|
32
|
+
return [
|
|
33
|
+
firstLine,
|
|
34
|
+
REGISTRATION_PROMPT,
|
|
35
|
+
POST_REGISTRATION_NEXT_STEP,
|
|
36
|
+
].join('\n');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function invalidAccountMessage(): string {
|
|
40
|
+
return [
|
|
41
|
+
'Account not found or API key expired.',
|
|
42
|
+
REGISTRATION_PROMPT,
|
|
43
|
+
POST_REGISTRATION_NEXT_STEP,
|
|
44
|
+
].join('\n');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function malformedApiKeyMessage(): string {
|
|
48
|
+
return [
|
|
49
|
+
'API key is missing or malformed.',
|
|
50
|
+
REGISTRATION_PROMPT,
|
|
51
|
+
POST_REGISTRATION_NEXT_STEP,
|
|
52
|
+
].join('\n');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function registrationSuccessOutput(result: Record<string, any>, agentName: string): Record<string, any> {
|
|
56
|
+
return {
|
|
57
|
+
...result,
|
|
58
|
+
message: `Registered and saved as profile "${agentName}"`,
|
|
59
|
+
officialUrl: OFFICIAL_URL,
|
|
60
|
+
next_step: POST_REGISTRATION_NEXT_STEP,
|
|
61
|
+
};
|
|
62
|
+
}
|
package/src/lib/load-context.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { existsSync, readFileSync, writeFileSync } from 'fs';
|
|
|
2
2
|
import { AuthStore } from './auth.js';
|
|
3
3
|
import { getProfileMemoryFile } from './init-command.js';
|
|
4
4
|
import {
|
|
5
|
+
ensureDefaultPersonaPlaceholder,
|
|
5
6
|
getActivePersonaFile,
|
|
6
7
|
readPersonaFile,
|
|
7
8
|
} from './persona.js';
|
|
@@ -45,6 +46,7 @@ export function loadMemorySnapshot(authStore = new AuthStore()): LoadedFileSnaps
|
|
|
45
46
|
}
|
|
46
47
|
|
|
47
48
|
export function loadContext(authStore = new AuthStore()): LoadContextSnapshot {
|
|
49
|
+
ensureDefaultPersonaPlaceholder(authStore);
|
|
48
50
|
return {
|
|
49
51
|
persona: loadPersonaSnapshot(authStore),
|
|
50
52
|
memory: loadMemorySnapshot(authStore),
|
package/src/lib/persona.test.ts
CHANGED
|
@@ -2,7 +2,10 @@ import { describe, expect, it, beforeEach, afterEach } from 'vitest';
|
|
|
2
2
|
import { mkdtempSync, rmSync, mkdirSync, writeFileSync, readFileSync } from 'fs';
|
|
3
3
|
import { tmpdir } from 'os';
|
|
4
4
|
import { join } from 'path';
|
|
5
|
+
import { AuthStore } from './auth.js';
|
|
6
|
+
import { CLAWCLAW_WORKSPACE_DIR } from './init-command.js';
|
|
5
7
|
import {
|
|
8
|
+
ensureDefaultPersonaPlaceholder,
|
|
6
9
|
applyPersonaPreset,
|
|
7
10
|
findPersonaPreset,
|
|
8
11
|
listPersonaPresets,
|
|
@@ -10,8 +13,10 @@ import {
|
|
|
10
13
|
|
|
11
14
|
describe('persona presets', () => {
|
|
12
15
|
let dir: string;
|
|
16
|
+
let prevWorkspace: string | undefined;
|
|
13
17
|
|
|
14
18
|
beforeEach(() => {
|
|
19
|
+
prevWorkspace = process.env[CLAWCLAW_WORKSPACE_DIR];
|
|
15
20
|
dir = mkdtempSync(join(tmpdir(), 'clawclaw-personas-'));
|
|
16
21
|
mkdirSync(join(dir, 'nested'));
|
|
17
22
|
writeFileSync(join(dir, '温和.md'), 'warm');
|
|
@@ -21,12 +26,29 @@ describe('persona presets', () => {
|
|
|
21
26
|
|
|
22
27
|
afterEach(() => {
|
|
23
28
|
rmSync(dir, { recursive: true, force: true });
|
|
29
|
+
if (prevWorkspace === undefined) delete process.env[CLAWCLAW_WORKSPACE_DIR];
|
|
30
|
+
else process.env[CLAWCLAW_WORKSPACE_DIR] = prevWorkspace;
|
|
24
31
|
});
|
|
25
32
|
|
|
33
|
+
function createActiveStore(): AuthStore {
|
|
34
|
+
process.env[CLAWCLAW_WORKSPACE_DIR] = join(dir, 'workspace');
|
|
35
|
+
const store = new AuthStore();
|
|
36
|
+
store.addProfile({
|
|
37
|
+
agentName: 'lobster-1',
|
|
38
|
+
apiKey: 'claw_test_key_12345678',
|
|
39
|
+
serverUrl: 'https://example.com',
|
|
40
|
+
});
|
|
41
|
+
return store;
|
|
42
|
+
}
|
|
43
|
+
|
|
26
44
|
it('lists markdown presets from the preset directory', () => {
|
|
27
45
|
expect(listPersonaPresets(dir).map(item => item.name).sort()).toEqual(['温和', '野性']);
|
|
28
46
|
});
|
|
29
47
|
|
|
48
|
+
it('includes the no-persona preset in the bundled preset list', () => {
|
|
49
|
+
expect(listPersonaPresets().map(item => item.name)).toContain('无人设');
|
|
50
|
+
});
|
|
51
|
+
|
|
30
52
|
it('finds presets with or without .md suffix', () => {
|
|
31
53
|
expect(findPersonaPreset('温和', dir)?.name).toBe('温和');
|
|
32
54
|
expect(findPersonaPreset('温和.md', dir)?.name).toBe('温和');
|
|
@@ -38,4 +60,25 @@ describe('persona presets', () => {
|
|
|
38
60
|
expect(preset.name).toBe('野性');
|
|
39
61
|
expect(readFileSync(target, 'utf8')).toBe('wild');
|
|
40
62
|
});
|
|
63
|
+
|
|
64
|
+
it('applies the default placeholder when the active account persona is empty', () => {
|
|
65
|
+
writeFileSync(join(dir, '无人设.md'), 'plain');
|
|
66
|
+
const result = ensureDefaultPersonaPlaceholder(createActiveStore(), dir);
|
|
67
|
+
|
|
68
|
+
expect(result.applied).toBe(true);
|
|
69
|
+
expect(result.preset.name).toBe('无人设');
|
|
70
|
+
expect(readFileSync(result.path, 'utf8')).toBe('plain');
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('keeps existing active account persona content', () => {
|
|
74
|
+
writeFileSync(join(dir, '无人设.md'), 'plain');
|
|
75
|
+
const store = createActiveStore();
|
|
76
|
+
const first = ensureDefaultPersonaPlaceholder(store, dir);
|
|
77
|
+
writeFileSync(first.path, 'voice');
|
|
78
|
+
|
|
79
|
+
const result = ensureDefaultPersonaPlaceholder(store, dir);
|
|
80
|
+
|
|
81
|
+
expect(result.applied).toBe(false);
|
|
82
|
+
expect(readFileSync(result.path, 'utf8')).toBe('voice');
|
|
83
|
+
});
|
|
41
84
|
});
|
package/src/lib/persona.ts
CHANGED
|
@@ -13,6 +13,8 @@ export interface PersonaPreset {
|
|
|
13
13
|
path: string;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
+
export const DEFAULT_PERSONA_PRESET = '无人设';
|
|
17
|
+
|
|
16
18
|
export function getBundledPersonaDir(): string {
|
|
17
19
|
return resolve(__dirname, '..', '..', 'personas');
|
|
18
20
|
}
|
|
@@ -67,6 +69,21 @@ export function applyPersonaPreset(name: string, targetPath: string, dir = getBu
|
|
|
67
69
|
return preset;
|
|
68
70
|
}
|
|
69
71
|
|
|
72
|
+
export function ensureDefaultPersonaPlaceholder(authStore = new AuthStore(), dir = getBundledPersonaDir()): { path: string; preset: PersonaPreset; applied: boolean } {
|
|
73
|
+
const path = getActivePersonaFile(authStore);
|
|
74
|
+
const current = readPersonaFile(path);
|
|
75
|
+
if (current.trim() !== '') {
|
|
76
|
+
const preset = findPersonaPreset(DEFAULT_PERSONA_PRESET, dir);
|
|
77
|
+
return {
|
|
78
|
+
path,
|
|
79
|
+
preset: preset ?? { id: DEFAULT_PERSONA_PRESET, name: DEFAULT_PERSONA_PRESET, path: '' },
|
|
80
|
+
applied: false,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
const preset = applyPersonaPreset(DEFAULT_PERSONA_PRESET, path, dir);
|
|
84
|
+
return { path, preset, applied: true };
|
|
85
|
+
}
|
|
86
|
+
|
|
70
87
|
function normalizePersonaName(value: string): string {
|
|
71
88
|
return value.trim().replace(/\.md$/i, '').toLowerCase();
|
|
72
89
|
}
|
|
@@ -95,6 +95,50 @@ describe('WsClient', () => {
|
|
|
95
95
|
]);
|
|
96
96
|
});
|
|
97
97
|
|
|
98
|
+
it('dispatches different late events with the same tick', async () => {
|
|
99
|
+
const { WsClient } = await import('./ws-client.js');
|
|
100
|
+
const client = new WsClient({
|
|
101
|
+
url: 'wss://host.example/api/v1/game/stream',
|
|
102
|
+
apiKey: 'claw_secret',
|
|
103
|
+
});
|
|
104
|
+
const seen: Record<string, any>[] = [];
|
|
105
|
+
client.on('*', (event) => seen.push(event));
|
|
106
|
+
|
|
107
|
+
(client as any).handleMessage({
|
|
108
|
+
type: 'state',
|
|
109
|
+
data: {
|
|
110
|
+
phase: 'wandering',
|
|
111
|
+
tick: 4168,
|
|
112
|
+
you: { is_alive: true },
|
|
113
|
+
new_events: [
|
|
114
|
+
{ type: 'player_spotted', tick: 4168, spotted_name: 'killer' },
|
|
115
|
+
{ type: 'enter_room', tick: 4168, room: 'quarters' },
|
|
116
|
+
],
|
|
117
|
+
},
|
|
118
|
+
});
|
|
119
|
+
(client as any).handleMessage({
|
|
120
|
+
type: 'state',
|
|
121
|
+
data: {
|
|
122
|
+
phase: 'wandering',
|
|
123
|
+
tick: 4168,
|
|
124
|
+
you: { is_alive: false },
|
|
125
|
+
new_events: [
|
|
126
|
+
{ type: 'body_dropped', tick: 4168, room: 'quarters' },
|
|
127
|
+
{ type: 'killed', tick: 4168, killer_name: 'killer' },
|
|
128
|
+
],
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
expect(seen.map((event) => event.type)).toEqual([
|
|
133
|
+
'player_spotted',
|
|
134
|
+
'enter_room',
|
|
135
|
+
'_state',
|
|
136
|
+
'body_dropped',
|
|
137
|
+
'killed',
|
|
138
|
+
'_state',
|
|
139
|
+
]);
|
|
140
|
+
});
|
|
141
|
+
|
|
98
142
|
it('dispatches a _state snapshot when phase is game_over without new_events', async () => {
|
|
99
143
|
const { WsClient } = await import('./ws-client.js');
|
|
100
144
|
const client = new WsClient({
|
package/src/runtime/ws-client.ts
CHANGED
|
@@ -45,9 +45,6 @@ export class WsClient {
|
|
|
45
45
|
private typedHandlers = new Map<string, Set<(data: Record<string, any>) => void>>();
|
|
46
46
|
private disconnectHandlers: ((info: WsCloseInfo) => void)[] = [];
|
|
47
47
|
private stateChangeHandlers: ((state: WsConnectionState) => void)[] = [];
|
|
48
|
-
/** High-water mark for tick-based dedup of state.new_events (align with agones) */
|
|
49
|
-
private lastEventTick = -1;
|
|
50
|
-
|
|
51
48
|
constructor(config: WsClientConfig) {
|
|
52
49
|
this.config = config;
|
|
53
50
|
}
|
|
@@ -141,18 +138,14 @@ export class WsClient {
|
|
|
141
138
|
}
|
|
142
139
|
// state: always dispatch the full snapshot as _state for runtime feed tracking
|
|
143
140
|
// (current_speaker, sub_phase, vote progress, etc. update without necessarily
|
|
144
|
-
// shipping a new event).
|
|
141
|
+
// shipping a new event). Dispatch new_events as sent; EventRuntime handles
|
|
142
|
+
// duplicate event keys while preserving late-arriving different events with
|
|
143
|
+
// the same tick.
|
|
145
144
|
if (msg.type === 'state' && msg.data) {
|
|
146
145
|
if (Array.isArray(msg.data.new_events)) {
|
|
147
|
-
const threshold = this.lastEventTick;
|
|
148
|
-
let maxTick = threshold;
|
|
149
146
|
for (const event of msg.data.new_events) {
|
|
150
|
-
const evtTick: number = event.tick ?? -1;
|
|
151
|
-
if (evtTick <= threshold) continue; // already processed
|
|
152
|
-
if (evtTick > maxTick) maxTick = evtTick;
|
|
153
147
|
this.dispatchEvent(event);
|
|
154
148
|
}
|
|
155
|
-
this.lastEventTick = maxTick;
|
|
156
149
|
}
|
|
157
150
|
this.dispatchEvent({ type: '_state', data: msg.data });
|
|
158
151
|
return;
|
|
@@ -362,8 +362,8 @@ export function activeEmergencyRushTask(state: GameState, ctx: StrategyContext):
|
|
|
362
362
|
* 残局抢修紧急维修任务的「靠近 → 到点提交」单步决策:activeEmergencyRushTask 生效时返回该决策,否则 null
|
|
363
363
|
* (行为不触发,调用方按自身原优先级继续)。
|
|
364
364
|
*
|
|
365
|
-
*
|
|
366
|
-
*
|
|
365
|
+
* 背景:紧急任务超时则蟹阵营直接获胜(后端倒计时),而中立的章鱼要活过章鱼时间、天堂鱼要在第二轮及以后被投出,
|
|
366
|
+
* 因此残局应抢着把维修做掉、掐断蟹的破坏取胜线。服务端已放开中立/蟹阵营提交紧急任务。紧急任务被任意玩家完成 / 开会 /
|
|
367
367
|
* 超时后,服务端会清空 state.emergency(→ ctx.emergency 为 null),activeEmergencyRushTask 随之自动返回 null。
|
|
368
368
|
*/
|
|
369
369
|
export function emergencyRushDecision(state: GameState, ctx: StrategyContext): BehaviorDecision | null {
|
|
@@ -26,7 +26,7 @@ export class CorpsePatrolTop extends Goal {
|
|
|
26
26
|
|
|
27
27
|
tick(state: GameState, ctx: StrategyContext): BehaviorDecision[] {
|
|
28
28
|
// 残局抢修紧急任务(已知存活≤6):天堂鱼最高优先级,先于贴尸/巡逻/打招呼——
|
|
29
|
-
//
|
|
29
|
+
// 紧急任务超时则蟹直接获胜;天堂鱼要拖到第二轮及以后被投出,故残局抢着把维修做掉、掐断蟹的破坏取胜线。
|
|
30
30
|
const emergency = emergencyRushDecision(state, ctx);
|
|
31
31
|
if (emergency) {
|
|
32
32
|
this.emitProgress(state, ctx, null, true);
|
|
@@ -15,7 +15,7 @@ export const strategy: StrategyEntry = {
|
|
|
15
15
|
id: 'paradise-fish',
|
|
16
16
|
name: '天堂鱼',
|
|
17
17
|
description:
|
|
18
|
-
'
|
|
18
|
+
'天堂鱼·进阶版(中立,不报警、不做任务,靠第二轮及以后被投票出局取胜;corpse-patrol 的带记忆升级)。读取 ccl knowledge 两档标记(hostile=坏人 / trusted=好人),未标记的一律默认被怀疑:hostile 视为危险硬寻路远离,被怀疑者按目击者规则保持距离,trusted 视为可抱团的可信同伴。发现尸体绝不报警,贴着尸体游走制造嫌疑;附近有人时远离那人但仍在尸体附近活动。残局(已知存活≤6)出现紧急维修任务时,会在没有带刀坏人需要躲避时优先抢着去做(先于对陌生人保持距离/贴尸/抱团),阻止蟹靠破坏倒计时取胜。传入打招呼话术时,视野内出现人就随机发送一条,之后120秒内不再发言。',
|
|
19
19
|
create(args?: string[]) {
|
|
20
20
|
return new ParadiseFishStrategy(parseGreetingArgs(args, 'paradise-fish'));
|
|
21
21
|
},
|