@myclaw163/clawclaw-cli 0.6.54
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 +440 -0
- package/bin/clawclaw-cli.mjs +4 -0
- package/package.json +48 -0
- package/personas//347/220/206/346/231/272/346/270/251/345/222/214.md +23 -0
- package/personas//350/200/201/350/260/213/346/267/261/347/256/227.md +22 -0
- package/personas//350/257/232/346/201/263/347/233/264/347/216/207.md +22 -0
- package/personas//350/275/273/346/235/276/346/264/273/346/263/274.md +22 -0
- package/personas//351/207/216/346/200/247/345/217/233/351/200/206.md +23 -0
- package/scripts/postinstall.mjs +20 -0
- package/scripts/sync-bundled-skill.mjs +245 -0
- package/scripts/sync-bundled-skill.test.mjs +152 -0
- package/skills/clawclaw/SKILL.md +240 -0
- package/skills/clawclaw/references/CHATTERBOX.md +142 -0
- package/skills/clawclaw/references/COMMANDS.md +132 -0
- package/skills/clawclaw/references/GAME-MECHANICS.md +186 -0
- package/skills/clawclaw/references/HUB.md +48 -0
- package/skills/clawclaw/references/KNOWLEDGE.md +43 -0
- package/skills/clawclaw/references/STRATEGIES.md +57 -0
- package/skills/clawclaw/references/STREAM.md +58 -0
- package/skills/clawclaw/references/TACTICS.md +65 -0
- package/src/assets/clawclaw-ascii-map.txt +40 -0
- package/src/cli.ts +153 -0
- package/src/commands/_schema.ts +109 -0
- package/src/commands/account.ts +209 -0
- package/src/commands/config.ts +30 -0
- package/src/commands/do.test.ts +37 -0
- package/src/commands/do.ts +95 -0
- package/src/commands/events.ts +22 -0
- package/src/commands/game-map.test.ts +28 -0
- package/src/commands/game-start-plan.test.ts +142 -0
- package/src/commands/game.ts +882 -0
- package/src/commands/history-player.test.ts +102 -0
- package/src/commands/history.ts +573 -0
- package/src/commands/hub.test.ts +96 -0
- package/src/commands/hub.ts +234 -0
- package/src/commands/knowledge.test.ts +19 -0
- package/src/commands/knowledge.ts +168 -0
- package/src/commands/load.test.ts +51 -0
- package/src/commands/load.ts +13 -0
- package/src/commands/meeting-history.test.ts +106 -0
- package/src/commands/memory.ts +40 -0
- package/src/commands/peek.ts +38 -0
- package/src/commands/persona.ts +57 -0
- package/src/commands/setup/codex.ts +248 -0
- package/src/commands/setup/hermes.test.ts +96 -0
- package/src/commands/setup/hermes.ts +76 -0
- package/src/commands/setup/index.ts +13 -0
- package/src/commands/setup/openclaw.test.ts +114 -0
- package/src/commands/setup/openclaw.ts +147 -0
- package/src/commands/skill.ts +128 -0
- package/src/commands/state.ts +46 -0
- package/src/commands/strategy.test.ts +135 -0
- package/src/commands/strategy.ts +189 -0
- package/src/commands/tts.ts +128 -0
- package/src/commands/upgrade.test.ts +91 -0
- package/src/commands/upgrade.ts +154 -0
- package/src/commands/watch.test.ts +973 -0
- package/src/commands/watch.ts +709 -0
- package/src/lib/auth.test.ts +59 -0
- package/src/lib/auth.ts +186 -0
- package/src/lib/command-meta.ts +37 -0
- package/src/lib/game-client.ts +391 -0
- package/src/lib/host-config-patcher.test.ts +130 -0
- package/src/lib/host-config-patcher.ts +151 -0
- package/src/lib/http-keepalive.ts +15 -0
- package/src/lib/http-transport.test.ts +42 -0
- package/src/lib/http-transport.ts +113 -0
- package/src/lib/hub-client.test.ts +56 -0
- package/src/lib/hub-client.ts +88 -0
- package/src/lib/hub-install.test.ts +98 -0
- package/src/lib/hub-install.ts +121 -0
- package/src/lib/hub-reminder.ts +75 -0
- package/src/lib/hub-unzip.test.ts +69 -0
- package/src/lib/hub-unzip.ts +62 -0
- package/src/lib/init-command.test.ts +75 -0
- package/src/lib/init-command.ts +120 -0
- package/src/lib/knowledge-store.test.ts +180 -0
- package/src/lib/knowledge-store.ts +374 -0
- package/src/lib/load-context.test.ts +52 -0
- package/src/lib/load-context.ts +52 -0
- package/src/lib/match-state.test.ts +134 -0
- package/src/lib/match-state.ts +94 -0
- package/src/lib/netease-tts.ts +83 -0
- package/src/lib/normalize.ts +42 -0
- package/src/lib/persona.test.ts +41 -0
- package/src/lib/persona.ts +72 -0
- package/src/lib/server-registry.ts +152 -0
- package/src/lib/skill-version.test.ts +48 -0
- package/src/lib/skill-version.ts +19 -0
- package/src/lib/strategy-export.test.ts +232 -0
- package/src/lib/strategy-export.ts +242 -0
- package/src/lib/tts-keys.ts +7 -0
- package/src/lib/tts-speech.test.ts +63 -0
- package/src/lib/tts-speech.ts +76 -0
- package/src/lib/workspace-argv.test.ts +49 -0
- package/src/lib/workspace-argv.ts +44 -0
- package/src/perception/player-history-store.test.ts +87 -0
- package/src/perception/player-history-store.ts +194 -0
- package/src/pipeline/event-store.ts +124 -0
- package/src/pipeline/pipeline.ts +35 -0
- package/src/runtime/auto-upgrade.test.ts +66 -0
- package/src/runtime/auto-upgrade.ts +31 -0
- package/src/runtime/daemon.ts +100 -0
- package/src/runtime/event-daemon.test.ts +28 -0
- package/src/runtime/event-daemon.ts +371 -0
- package/src/runtime/opening-mover.ts +303 -0
- package/src/runtime/raw-ws-log.test.ts +33 -0
- package/src/runtime/raw-ws-log.ts +32 -0
- package/src/runtime/runtime-logger.ts +99 -0
- package/src/runtime/ws-client.test.ts +47 -0
- package/src/runtime/ws-client.ts +272 -0
- package/src/sdk/action.ts +166 -0
- package/src/sdk/index.ts +110 -0
- package/src/sdk/types.ts +146 -0
- package/src/strategies/avoid-lone.ts +11 -0
- package/src/strategies/avoid-players.knowledge.md +20 -0
- package/src/strategies/avoid-players.ts +15 -0
- package/src/strategies/corpse-patrol.ts +22 -0
- package/src/strategies/crab-sabotage.ts +21 -0
- package/src/strategies/custom-module.test.ts +269 -0
- package/src/strategies/find-player.ts +16 -0
- package/src/strategies/game-utils.test.ts +164 -0
- package/src/strategies/game-utils.ts +721 -0
- package/src/strategies/goals/avoid-lone-top.ts +168 -0
- package/src/strategies/goals/avoid-players-top.test.ts +83 -0
- package/src/strategies/goals/avoid-players-top.ts +121 -0
- package/src/strategies/goals/conversation-goal.ts +51 -0
- package/src/strategies/goals/corpse-patrol-top.ts +91 -0
- package/src/strategies/goals/crab-octopus-reflexes.ts +93 -0
- package/src/strategies/goals/crab-sabotage-top.ts +197 -0
- package/src/strategies/goals/emergency-hunt-goal.ts +28 -0
- package/src/strategies/goals/find-player-top.ts +93 -0
- package/src/strategies/goals/flee-players-goal.ts +53 -0
- package/src/strategies/goals/goal-manager.ts +41 -0
- package/src/strategies/goals/goal-root-strategy.ts +49 -0
- package/src/strategies/goals/goal.ts +28 -0
- package/src/strategies/goals/keep-away-goal.ts +206 -0
- package/src/strategies/goals/kill-frenzy-top.ts +80 -0
- package/src/strategies/goals/kill-lone-top.ts +160 -0
- package/src/strategies/goals/kill-target-goal.ts +59 -0
- package/src/strategies/goals/kill-target-top.ts +109 -0
- package/src/strategies/goals/leaf-goal.ts +25 -0
- package/src/strategies/goals/linger-corpse-goal.ts +79 -0
- package/src/strategies/goals/lone-kill-core.ts +82 -0
- package/src/strategies/goals/lone-kill-goal.ts +24 -0
- package/src/strategies/goals/lone-kill-task-top.test.ts +85 -0
- package/src/strategies/goals/lone-kill-task-top.ts +86 -0
- package/src/strategies/goals/move-room-goal.ts +60 -0
- package/src/strategies/goals/normal-shrimp-top.test.ts +80 -0
- package/src/strategies/goals/normal-shrimp-top.ts +242 -0
- package/src/strategies/goals/paradise-fish-top.test.ts +126 -0
- package/src/strategies/goals/paradise-fish-top.ts +219 -0
- package/src/strategies/goals/patrol-top.ts +57 -0
- package/src/strategies/goals/report-patrol-top.ts +80 -0
- package/src/strategies/goals/safe-task-goal.ts +102 -0
- package/src/strategies/goals/social-task-top.ts +161 -0
- package/src/strategies/goals/task-kill-report-top.ts +163 -0
- package/src/strategies/goals/task-only-top.ts +57 -0
- package/src/strategies/goals/task-or-patrol-goal.ts +41 -0
- package/src/strategies/goals/task-report-top.ts +57 -0
- package/src/strategies/goals/wander-task-goal.ts +33 -0
- package/src/strategies/goals/warrior-shrimp-top.test.ts +86 -0
- package/src/strategies/goals/warrior-shrimp-top.ts +248 -0
- package/src/strategies/greeting.ts +53 -0
- package/src/strategies/kill-frenzy.ts +12 -0
- package/src/strategies/kill-lone.knowledge.md +20 -0
- package/src/strategies/kill-lone.ts +13 -0
- package/src/strategies/kill-target.ts +18 -0
- package/src/strategies/loader.test.ts +678 -0
- package/src/strategies/loader.ts +172 -0
- package/src/strategies/lone-kill-task.ts +21 -0
- package/src/strategies/meeting-gate.test.ts +59 -0
- package/src/strategies/meeting-gate.ts +23 -0
- package/src/strategies/move-room.ts +15 -0
- package/src/strategies/new-events-backfill.ts +98 -0
- package/src/strategies/paradise-fish.knowledge.md +20 -0
- package/src/strategies/paradise-fish.ts +25 -0
- package/src/strategies/pathfind/clawclaw-walkable.bin +0 -0
- package/src/strategies/pathfind/distance-field.ts +150 -0
- package/src/strategies/pathfind/escape-planner.test.ts +197 -0
- package/src/strategies/pathfind/escape-planner.ts +348 -0
- package/src/strategies/pathfind/walkable-grid.ts +117 -0
- package/src/strategies/patrol.ts +11 -0
- package/src/strategies/player-targets.ts +13 -0
- package/src/strategies/report-patrol.ts +11 -0
- package/src/strategies/shrimp-memory.knowledge.md +20 -0
- package/src/strategies/shrimp-memory.ts +25 -0
- package/src/strategies/social-task.test.ts +28 -0
- package/src/strategies/social-task.ts +49 -0
- package/src/strategies/spawn.ts +71 -0
- package/src/strategies/speech-module.ts +123 -0
- package/src/strategies/strategy-loop.ts +757 -0
- package/src/strategies/task-kill-report.ts +17 -0
- package/src/strategies/task-only.ts +11 -0
- package/src/strategies/task-report.ts +22 -0
- package/src/strategies/types.ts +96 -0
- package/src/strategies/warrior-memory.knowledge.md +20 -0
- package/src/strategies/warrior-memory.ts +16 -0
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach } from 'vitest';
|
|
2
|
+
import { mkdtempSync, existsSync, readFileSync } from 'fs';
|
|
3
|
+
import { tmpdir } from 'os';
|
|
4
|
+
import { join } from 'path';
|
|
5
|
+
import {
|
|
6
|
+
startMatch,
|
|
7
|
+
endMatch,
|
|
8
|
+
readMatchState,
|
|
9
|
+
markWaitingEmitted,
|
|
10
|
+
shouldEmitWaiting,
|
|
11
|
+
getWaitedSecs,
|
|
12
|
+
hasMatchTimedOut,
|
|
13
|
+
DEFAULT_WAITING_HEARTBEAT_MS,
|
|
14
|
+
DEFAULT_MATCH_TIMEOUT_MS,
|
|
15
|
+
} from './match-state.js';
|
|
16
|
+
|
|
17
|
+
function tmpDir(): string {
|
|
18
|
+
return mkdtempSync(join(tmpdir(), 'match-state-'));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
describe('match-state — startMatch / readMatchState / endMatch', () => {
|
|
22
|
+
it('startMatch persists started_at with last_waiting_emit=0', () => {
|
|
23
|
+
const dir = tmpDir();
|
|
24
|
+
const now = 1_700_000_000_000;
|
|
25
|
+
const state = startMatch(dir, now);
|
|
26
|
+
expect(state).toEqual({ started_at: now, last_waiting_emit: 0 });
|
|
27
|
+
expect(readMatchState(dir)).toEqual(state);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('startMatch overwrites an existing state (new queue session)', () => {
|
|
31
|
+
const dir = tmpDir();
|
|
32
|
+
startMatch(dir, 1_000_000);
|
|
33
|
+
markWaitingEmitted(dir, 1_500_000);
|
|
34
|
+
const fresh = startMatch(dir, 2_000_000);
|
|
35
|
+
expect(fresh).toEqual({ started_at: 2_000_000, last_waiting_emit: 0 });
|
|
36
|
+
expect(readMatchState(dir)).toEqual(fresh);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('readMatchState returns null when no state file exists', () => {
|
|
40
|
+
expect(readMatchState(tmpDir())).toBeNull();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('readMatchState returns null on corrupt JSON (does not throw)', () => {
|
|
44
|
+
const dir = tmpDir();
|
|
45
|
+
// Simulate a half-written file from a previous crash.
|
|
46
|
+
const fs = require('fs');
|
|
47
|
+
fs.writeFileSync(join(dir, 'match-state.json'), '{"started_at": 1');
|
|
48
|
+
expect(readMatchState(dir)).toBeNull();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('endMatch removes the file; idempotent on missing', () => {
|
|
52
|
+
const dir = tmpDir();
|
|
53
|
+
startMatch(dir, 1_000_000);
|
|
54
|
+
expect(existsSync(join(dir, 'match-state.json'))).toBe(true);
|
|
55
|
+
endMatch(dir);
|
|
56
|
+
expect(existsSync(join(dir, 'match-state.json'))).toBe(false);
|
|
57
|
+
expect(() => endMatch(dir)).not.toThrow();
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe('match-state — atomic write', () => {
|
|
62
|
+
it('does not leave a .tmp sibling after a successful write', () => {
|
|
63
|
+
const dir = tmpDir();
|
|
64
|
+
startMatch(dir, 1_000_000);
|
|
65
|
+
markWaitingEmitted(dir, 1_100_000);
|
|
66
|
+
expect(existsSync(join(dir, 'match-state.json.tmp'))).toBe(false);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
describe('match-state — shouldEmitWaiting', () => {
|
|
71
|
+
it('returns true when last_waiting_emit is 0 (sentinel: never emitted)', () => {
|
|
72
|
+
const state = { started_at: 1_000_000, last_waiting_emit: 0 };
|
|
73
|
+
expect(shouldEmitWaiting(state, 1_000_001)).toBe(true);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('returns true when the elapsed since last emit reaches the heartbeat', () => {
|
|
77
|
+
const state = { started_at: 1_000_000, last_waiting_emit: 1_000_000 };
|
|
78
|
+
expect(shouldEmitWaiting(state, 1_000_000 + DEFAULT_WAITING_HEARTBEAT_MS)).toBe(true);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('returns false when below the heartbeat interval', () => {
|
|
82
|
+
const state = { started_at: 1_000_000, last_waiting_emit: 1_000_000 };
|
|
83
|
+
expect(shouldEmitWaiting(state, 1_000_000 + DEFAULT_WAITING_HEARTBEAT_MS - 1)).toBe(false);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('honors a custom heartbeatMs', () => {
|
|
87
|
+
const state = { started_at: 0, last_waiting_emit: 1_000 };
|
|
88
|
+
expect(shouldEmitWaiting(state, 2_000, 1_000)).toBe(true); // elapsed 1000ms == hb
|
|
89
|
+
expect(shouldEmitWaiting(state, 2_500, 1_000)).toBe(true); // elapsed 1500ms > hb
|
|
90
|
+
expect(shouldEmitWaiting(state, 1_999, 1_000)).toBe(false); // elapsed 999ms < hb
|
|
91
|
+
expect(shouldEmitWaiting(state, 5_000, 5_000)).toBe(false); // elapsed 4000ms < hb 5000
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe('match-state — markWaitingEmitted', () => {
|
|
96
|
+
it('updates last_waiting_emit while preserving started_at', () => {
|
|
97
|
+
const dir = tmpDir();
|
|
98
|
+
startMatch(dir, 1_000_000);
|
|
99
|
+
markWaitingEmitted(dir, 1_200_000);
|
|
100
|
+
expect(readMatchState(dir)).toEqual({ started_at: 1_000_000, last_waiting_emit: 1_200_000 });
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('no-ops when state file is missing', () => {
|
|
104
|
+
const dir = tmpDir();
|
|
105
|
+
expect(() => markWaitingEmitted(dir, 1_000_000)).not.toThrow();
|
|
106
|
+
expect(readMatchState(dir)).toBeNull();
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
describe('match-state — getWaitedSecs / hasMatchTimedOut', () => {
|
|
111
|
+
it('getWaitedSecs returns floor of seconds since started_at', () => {
|
|
112
|
+
const state = { started_at: 1_000_000, last_waiting_emit: 0 };
|
|
113
|
+
expect(getWaitedSecs(state, 1_000_999)).toBe(0);
|
|
114
|
+
expect(getWaitedSecs(state, 1_001_000)).toBe(1);
|
|
115
|
+
expect(getWaitedSecs(state, 1_120_500)).toBe(120);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('getWaitedSecs clamps to 0 if clock skews backwards', () => {
|
|
119
|
+
const state = { started_at: 2_000_000, last_waiting_emit: 0 };
|
|
120
|
+
expect(getWaitedSecs(state, 1_000_000)).toBe(0);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('hasMatchTimedOut flips at the default 10-minute threshold', () => {
|
|
124
|
+
const state = { started_at: 0, last_waiting_emit: 0 };
|
|
125
|
+
expect(hasMatchTimedOut(state, DEFAULT_MATCH_TIMEOUT_MS - 1)).toBe(false);
|
|
126
|
+
expect(hasMatchTimedOut(state, DEFAULT_MATCH_TIMEOUT_MS)).toBe(true);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('honors a custom timeoutMs', () => {
|
|
130
|
+
const state = { started_at: 0, last_waiting_emit: 0 };
|
|
131
|
+
expect(hasMatchTimedOut(state, 999, 1_000)).toBe(false);
|
|
132
|
+
expect(hasMatchTimedOut(state, 1_000, 1_000)).toBe(true);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* match-state — 记录单次匹配等待的进度,用于 `ccl game queue` 跨进程协调
|
|
3
|
+
* `match_waiting` / `match_timeout` 合成事件的发射节奏。
|
|
4
|
+
*
|
|
5
|
+
* 每次 `ccl game queue` 是一个新进程,跨进程的"上次心跳时间"必须落盘。
|
|
6
|
+
* 持久化在 `${profileStateDir}/match-state.json`,生命周期:
|
|
7
|
+
* - `ccl game join` → startMatch() 创建
|
|
8
|
+
* - `ccl game queue` 匹配成功 → endMatch() 删除
|
|
9
|
+
* - `ccl game queue` 心跳到点 → markWaitingEmitted() 更新 last_waiting_emit
|
|
10
|
+
* - 用户主动 leave/quit → endMatch() 删除
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { existsSync, readFileSync, writeFileSync, unlinkSync, renameSync } from 'fs';
|
|
14
|
+
import { join } from 'path';
|
|
15
|
+
|
|
16
|
+
export interface MatchState {
|
|
17
|
+
started_at: number; // ms epoch — 进入队列的时刻
|
|
18
|
+
last_waiting_emit: number; // ms epoch — 最近一次 emit match_waiting 的时刻 (0 = 从未)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** 默认匹配等待心跳间隔:5 分钟 */
|
|
22
|
+
export const DEFAULT_WAITING_HEARTBEAT_MS = 300_000;
|
|
23
|
+
|
|
24
|
+
/** 默认匹配总超时阈值:600 秒 (10 分钟) */
|
|
25
|
+
export const DEFAULT_MATCH_TIMEOUT_MS = 600_000;
|
|
26
|
+
|
|
27
|
+
function statePath(stateDir: string): string {
|
|
28
|
+
return join(stateDir, 'match-state.json');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 原子写: 先写到 .tmp 再 rename。
|
|
33
|
+
* rename 在 NTFS / ext4 / APFS 上都是原子操作,避免半写文件被 JSON.parse 时
|
|
34
|
+
* 误判为"无匹配进行中"而把累计等待计数重置。
|
|
35
|
+
*/
|
|
36
|
+
function atomicWriteJSON(path: string, value: unknown): void {
|
|
37
|
+
const tmp = `${path}.tmp`;
|
|
38
|
+
writeFileSync(tmp, JSON.stringify(value));
|
|
39
|
+
renameSync(tmp, path);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function readMatchState(stateDir: string): MatchState | null {
|
|
43
|
+
const p = statePath(stateDir);
|
|
44
|
+
if (!existsSync(p)) return null;
|
|
45
|
+
try {
|
|
46
|
+
return JSON.parse(readFileSync(p, 'utf8')) as MatchState;
|
|
47
|
+
} catch {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** 开始一轮新匹配。即使旧 state 残留也覆盖。 */
|
|
53
|
+
export function startMatch(stateDir: string, now: number = Date.now()): MatchState {
|
|
54
|
+
const state: MatchState = { started_at: now, last_waiting_emit: 0 };
|
|
55
|
+
atomicWriteJSON(statePath(stateDir), state);
|
|
56
|
+
return state;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** 匹配成功或被取消时清理。幂等。 */
|
|
60
|
+
export function endMatch(stateDir: string): void {
|
|
61
|
+
const p = statePath(stateDir);
|
|
62
|
+
if (!existsSync(p)) return;
|
|
63
|
+
try { unlinkSync(p); } catch {}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function markWaitingEmitted(stateDir: string, now: number = Date.now()): void {
|
|
67
|
+
const cur = readMatchState(stateDir);
|
|
68
|
+
if (!cur) return;
|
|
69
|
+
const next: MatchState = { ...cur, last_waiting_emit: now };
|
|
70
|
+
atomicWriteJSON(statePath(stateDir), next);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** 距离上次 emit match_waiting 是否够久 (或从未 emit)。 */
|
|
74
|
+
export function shouldEmitWaiting(
|
|
75
|
+
state: MatchState,
|
|
76
|
+
now: number = Date.now(),
|
|
77
|
+
heartbeatMs: number = DEFAULT_WAITING_HEARTBEAT_MS,
|
|
78
|
+
): boolean {
|
|
79
|
+
if (state.last_waiting_emit === 0) return true;
|
|
80
|
+
return now - state.last_waiting_emit >= heartbeatMs;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function getWaitedSecs(state: MatchState, now: number = Date.now()): number {
|
|
84
|
+
return Math.max(0, Math.floor((now - state.started_at) / 1000));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** 累计等待是否已到总超时阈值。 */
|
|
88
|
+
export function hasMatchTimedOut(
|
|
89
|
+
state: MatchState,
|
|
90
|
+
now: number = Date.now(),
|
|
91
|
+
timeoutMs: number = DEFAULT_MATCH_TIMEOUT_MS,
|
|
92
|
+
): boolean {
|
|
93
|
+
return now - state.started_at >= timeoutMs;
|
|
94
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
export class NeteaseTTSError extends Error {
|
|
2
|
+
constructor(message: string) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.name = 'NeteaseTTSError';
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface NeteaseTTSOptions {
|
|
9
|
+
apiKey: string;
|
|
10
|
+
text: string;
|
|
11
|
+
voice: string;
|
|
12
|
+
model?: string;
|
|
13
|
+
speed?: number;
|
|
14
|
+
url?: string;
|
|
15
|
+
timeoutMs?: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface NeteaseTTSResult {
|
|
19
|
+
audio: Buffer;
|
|
20
|
+
contentType: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const DEFAULT_TTS_URL = 'https://ai.leihuo.netease.com/v1/audio/speech';
|
|
24
|
+
const DEFAULT_TTS_MODEL = 'speech-2.8-hd';
|
|
25
|
+
const DEFAULT_TTS_SPEED = 1.3;
|
|
26
|
+
const DEFAULT_TIMEOUT_MS = 60_000;
|
|
27
|
+
|
|
28
|
+
async function extractErrorMessage(response: Response): Promise<string> {
|
|
29
|
+
const text = await response.text();
|
|
30
|
+
try {
|
|
31
|
+
const payload = JSON.parse(text);
|
|
32
|
+
const message = payload?.error?.message ?? payload?.message;
|
|
33
|
+
if (typeof message === 'string' && message.trim()) return message;
|
|
34
|
+
} catch {}
|
|
35
|
+
return text.trim() || `TTS request failed with status ${response.status}`;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export async function synthesizeNeteaseTTS(opts: NeteaseTTSOptions): Promise<NeteaseTTSResult> {
|
|
39
|
+
const controller = new AbortController();
|
|
40
|
+
const timer = setTimeout(() => controller.abort(), opts.timeoutMs ?? DEFAULT_TIMEOUT_MS);
|
|
41
|
+
try {
|
|
42
|
+
const response = await fetch(opts.url ?? DEFAULT_TTS_URL, {
|
|
43
|
+
method: 'POST',
|
|
44
|
+
headers: {
|
|
45
|
+
Authorization: `Bearer ${opts.apiKey}`,
|
|
46
|
+
'Content-Type': 'application/json; charset=utf-8',
|
|
47
|
+
},
|
|
48
|
+
body: JSON.stringify({
|
|
49
|
+
model: opts.model ?? DEFAULT_TTS_MODEL,
|
|
50
|
+
input: opts.text,
|
|
51
|
+
voice: opts.voice,
|
|
52
|
+
speed: opts.speed ?? DEFAULT_TTS_SPEED,
|
|
53
|
+
}),
|
|
54
|
+
signal: controller.signal,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
if (!response.ok) {
|
|
58
|
+
throw new NeteaseTTSError(await extractErrorMessage(response));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const contentType = (response.headers.get('content-type') ?? 'audio/mpeg')
|
|
62
|
+
.split(';', 1)[0]
|
|
63
|
+
.trim()
|
|
64
|
+
.toLowerCase();
|
|
65
|
+
if (!contentType.startsWith('audio/')) {
|
|
66
|
+
throw new NeteaseTTSError(`Unexpected TTS content type: ${contentType || 'unknown'}`);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const audio = Buffer.from(await response.arrayBuffer());
|
|
70
|
+
if (audio.length === 0) {
|
|
71
|
+
throw new NeteaseTTSError('TTS returned empty audio');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return { audio, contentType };
|
|
75
|
+
} catch (err: any) {
|
|
76
|
+
if (err instanceof NeteaseTTSError) throw err;
|
|
77
|
+
if (err?.name === 'AbortError') throw new NeteaseTTSError('TTS request timed out');
|
|
78
|
+
throw new NeteaseTTSError(err?.message ?? String(err));
|
|
79
|
+
} finally {
|
|
80
|
+
clearTimeout(timer);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// src/lib/normalize.ts
|
|
2
|
+
import type { GameState } from '../sdk/types.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Normalize raw game state response from server.
|
|
6
|
+
*
|
|
7
|
+
* Handles field renaming (visible_players → players, nearby_corpses → corpses),
|
|
8
|
+
* task field normalization when task data is present, and default empty arrays.
|
|
9
|
+
*/
|
|
10
|
+
export function normalizeGameState(data: any): GameState {
|
|
11
|
+
const result = { ...data };
|
|
12
|
+
|
|
13
|
+
// visible_players → players
|
|
14
|
+
if (result.visible_players !== undefined && result.players === undefined) {
|
|
15
|
+
result.players = result.visible_players;
|
|
16
|
+
}
|
|
17
|
+
// nearby_corpses → corpses
|
|
18
|
+
if (result.nearby_corpses !== undefined && result.corpses === undefined) {
|
|
19
|
+
result.corpses = result.nearby_corpses;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Default empty arrays
|
|
23
|
+
if (result.corpses === undefined) result.corpses = [];
|
|
24
|
+
if (result.players === undefined) result.players = [];
|
|
25
|
+
if (result.new_events === undefined) result.new_events = [];
|
|
26
|
+
if (result.tick === undefined) result.tick = 0;
|
|
27
|
+
|
|
28
|
+
// Normalize task fields: server returns { name }, SDK expects { task_id, task_name }
|
|
29
|
+
if (Array.isArray(result.your_tasks)) {
|
|
30
|
+
result.your_tasks = result.your_tasks.map((t: any) => ({
|
|
31
|
+
task_id: t.task_id ?? t.name ?? '',
|
|
32
|
+
task_name: t.task_name ?? t.name ?? '',
|
|
33
|
+
room: t.room ?? '',
|
|
34
|
+
status: t.status ?? 'normal',
|
|
35
|
+
x: t.x,
|
|
36
|
+
y: t.y,
|
|
37
|
+
is_fake_shrimp: t.is_fake_shrimp ?? false,
|
|
38
|
+
}));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { describe, expect, it, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import { mkdtempSync, rmSync, mkdirSync, writeFileSync, readFileSync } from 'fs';
|
|
3
|
+
import { tmpdir } from 'os';
|
|
4
|
+
import { join } from 'path';
|
|
5
|
+
import {
|
|
6
|
+
applyPersonaPreset,
|
|
7
|
+
findPersonaPreset,
|
|
8
|
+
listPersonaPresets,
|
|
9
|
+
} from './persona.js';
|
|
10
|
+
|
|
11
|
+
describe('persona presets', () => {
|
|
12
|
+
let dir: string;
|
|
13
|
+
|
|
14
|
+
beforeEach(() => {
|
|
15
|
+
dir = mkdtempSync(join(tmpdir(), 'clawclaw-personas-'));
|
|
16
|
+
mkdirSync(join(dir, 'nested'));
|
|
17
|
+
writeFileSync(join(dir, '温和.md'), 'warm');
|
|
18
|
+
writeFileSync(join(dir, '野性.md'), 'wild');
|
|
19
|
+
writeFileSync(join(dir, 'ignore.txt'), 'nope');
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
afterEach(() => {
|
|
23
|
+
rmSync(dir, { recursive: true, force: true });
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('lists markdown presets from the preset directory', () => {
|
|
27
|
+
expect(listPersonaPresets(dir).map(item => item.name).sort()).toEqual(['温和', '野性']);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('finds presets with or without .md suffix', () => {
|
|
31
|
+
expect(findPersonaPreset('温和', dir)?.name).toBe('温和');
|
|
32
|
+
expect(findPersonaPreset('温和.md', dir)?.name).toBe('温和');
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('copies preset content into the active account persona file', () => {
|
|
36
|
+
const target = join(dir, 'account', 'persona.md');
|
|
37
|
+
const preset = applyPersonaPreset('野性', target, dir);
|
|
38
|
+
expect(preset.name).toBe('野性');
|
|
39
|
+
expect(readFileSync(target, 'utf8')).toBe('wild');
|
|
40
|
+
});
|
|
41
|
+
});
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync, copyFileSync } from 'fs';
|
|
2
|
+
import { dirname, join, resolve, basename } from 'path';
|
|
3
|
+
import { fileURLToPath } from 'url';
|
|
4
|
+
import { AuthStore } from './auth.js';
|
|
5
|
+
import { getProfilePersonaFile } from './init-command.js';
|
|
6
|
+
|
|
7
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
8
|
+
const __dirname = dirname(__filename);
|
|
9
|
+
|
|
10
|
+
export interface PersonaPreset {
|
|
11
|
+
id: string;
|
|
12
|
+
name: string;
|
|
13
|
+
path: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function getBundledPersonaDir(): string {
|
|
17
|
+
return resolve(__dirname, '..', '..', 'personas');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function listPersonaPresets(dir = getBundledPersonaDir()): PersonaPreset[] {
|
|
21
|
+
if (!existsSync(dir)) return [];
|
|
22
|
+
return readdirSync(dir, { withFileTypes: true })
|
|
23
|
+
.filter(entry => entry.isFile() && entry.name.toLowerCase().endsWith('.md'))
|
|
24
|
+
.map(entry => {
|
|
25
|
+
const name = entry.name.slice(0, -3);
|
|
26
|
+
return {
|
|
27
|
+
id: name,
|
|
28
|
+
name,
|
|
29
|
+
path: join(dir, entry.name),
|
|
30
|
+
};
|
|
31
|
+
})
|
|
32
|
+
.sort((a, b) => a.name.localeCompare(b.name, 'zh-Hans-CN'));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function findPersonaPreset(name: string, dir = getBundledPersonaDir()): PersonaPreset | null {
|
|
36
|
+
const normalized = normalizePersonaName(name);
|
|
37
|
+
return listPersonaPresets(dir).find(preset =>
|
|
38
|
+
normalizePersonaName(preset.id) === normalized ||
|
|
39
|
+
normalizePersonaName(preset.name) === normalized ||
|
|
40
|
+
normalizePersonaName(basename(preset.path)) === normalized,
|
|
41
|
+
) ?? null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function getActivePersonaFile(authStore = new AuthStore()): string {
|
|
45
|
+
const profile = authStore.getActive();
|
|
46
|
+
if (!profile) throw new Error('Not logged in.');
|
|
47
|
+
return getProfilePersonaFile(profile);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function ensurePersonaFile(path: string): void {
|
|
51
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
52
|
+
if (!existsSync(path)) writeFileSync(path, '');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function readPersonaFile(path: string): string {
|
|
56
|
+
return existsSync(path) ? readFileSync(path, 'utf8') : '';
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function applyPersonaPreset(name: string, targetPath: string, dir = getBundledPersonaDir()): PersonaPreset {
|
|
60
|
+
const preset = findPersonaPreset(name, dir);
|
|
61
|
+
if (!preset) {
|
|
62
|
+
const available = listPersonaPresets(dir).map(item => item.name).join(', ') || 'none';
|
|
63
|
+
throw new Error(`Persona preset "${name}" not found. Available: ${available}`);
|
|
64
|
+
}
|
|
65
|
+
ensurePersonaFile(targetPath);
|
|
66
|
+
copyFileSync(preset.path, targetPath);
|
|
67
|
+
return preset;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function normalizePersonaName(value: string): string {
|
|
71
|
+
return value.trim().replace(/\.md$/i, '').toLowerCase();
|
|
72
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// src/lib/server-registry.ts
|
|
2
|
+
import { AuthStore } from './auth.js';
|
|
3
|
+
|
|
4
|
+
export interface ServerAddresses {
|
|
5
|
+
lobbyUrl: string;
|
|
6
|
+
gameServerUrl: string | null;
|
|
7
|
+
wsUrl: string | null;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export class ServerRegistry {
|
|
11
|
+
private lobbyUrl: string;
|
|
12
|
+
private _gameServerUrl: string | null;
|
|
13
|
+
private _gatewayWsUrl: string | null;
|
|
14
|
+
private agentName: string | undefined;
|
|
15
|
+
/** True when gameServerUrl was pre-configured (e.g. from auth) — prevents
|
|
16
|
+
* discoverGameServer() from clearing it during queue status polling. */
|
|
17
|
+
private _urlPreconfigured: boolean;
|
|
18
|
+
|
|
19
|
+
constructor(opts: { lobbyUrl: string; gameServerUrl?: string; agentName?: string }) {
|
|
20
|
+
this.lobbyUrl = opts.lobbyUrl.replace(/\/$/, '');
|
|
21
|
+
this._gatewayWsUrl = null;
|
|
22
|
+
if (opts.gameServerUrl) {
|
|
23
|
+
const raw = opts.gameServerUrl.replace(/\/$/, '');
|
|
24
|
+
try {
|
|
25
|
+
const u = new URL(raw);
|
|
26
|
+
if (u.pathname && u.pathname !== '/') {
|
|
27
|
+
// Gateway URL — restore _gatewayWsUrl so wsUrl path prefix is preserved
|
|
28
|
+
this._gatewayWsUrl = raw;
|
|
29
|
+
this._gameServerUrl = raw;
|
|
30
|
+
} else {
|
|
31
|
+
this._gameServerUrl = raw.replace(/^https:\/\//, 'http://');
|
|
32
|
+
}
|
|
33
|
+
} catch {
|
|
34
|
+
this._gameServerUrl = raw.replace(/^https:\/\//, 'http://');
|
|
35
|
+
}
|
|
36
|
+
this._urlPreconfigured = true;
|
|
37
|
+
} else {
|
|
38
|
+
this._gameServerUrl = null;
|
|
39
|
+
this._urlPreconfigured = false;
|
|
40
|
+
}
|
|
41
|
+
this.agentName = opts.agentName;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
get addresses(): ServerAddresses {
|
|
45
|
+
return {
|
|
46
|
+
lobbyUrl: this.lobbyUrl,
|
|
47
|
+
gameServerUrl: this._gameServerUrl,
|
|
48
|
+
wsUrl: this.wsUrl,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
get gameServerUrl(): string | null {
|
|
53
|
+
return this._gameServerUrl;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Determine the correct base URL for a given API path.
|
|
58
|
+
*
|
|
59
|
+
* Game Server routes: /api/v1/game/current, /api/v1/game/action, /api/v1/game/map, /api/v1/game/leave, /api/v1/game/role_info
|
|
60
|
+
* Lobby routes: everything else (/api/v1/agents/*, /api/v1/queue/*, etc.)
|
|
61
|
+
*/
|
|
62
|
+
baseUrlFor(path: string): string {
|
|
63
|
+
if (
|
|
64
|
+
path.startsWith('/api/v1/game/current') ||
|
|
65
|
+
path.startsWith('/api/v1/game/action') ||
|
|
66
|
+
path.startsWith('/api/v1/game/map') ||
|
|
67
|
+
path.startsWith('/api/v1/game/leave') ||
|
|
68
|
+
path.startsWith('/api/v1/game/role_info')
|
|
69
|
+
) {
|
|
70
|
+
const base = this._gatewayWsUrl ?? this._gameServerUrl;
|
|
71
|
+
if (!base) throw new Error('No game server allocated');
|
|
72
|
+
return base;
|
|
73
|
+
}
|
|
74
|
+
return this.lobbyUrl;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Extract game server address from a queue response and update gameServerUrl.
|
|
79
|
+
* Prefers ws_url (Gateway route) over raw address:port.
|
|
80
|
+
* Returns true if the address changed.
|
|
81
|
+
*/
|
|
82
|
+
updateFromQueueResponse(data: any): boolean {
|
|
83
|
+
if (!data || typeof data !== 'object') return false;
|
|
84
|
+
const qs = data.data ?? data;
|
|
85
|
+
if (qs.status && qs.status !== 'allocated') {
|
|
86
|
+
// Never clear a pre-configured URL just because queue status changed —
|
|
87
|
+
// the game may already be in progress. Only clear if the URL was
|
|
88
|
+
// originally discovered via queue (not pre-set from auth).
|
|
89
|
+
if (this._urlPreconfigured) return false;
|
|
90
|
+
if (this._gameServerUrl !== null || this._gatewayWsUrl !== null) {
|
|
91
|
+
this._gameServerUrl = null;
|
|
92
|
+
this._gatewayWsUrl = null;
|
|
93
|
+
this.persistGameServerUrl();
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const wsUrl = qs.ws_url;
|
|
100
|
+
if (wsUrl && typeof wsUrl === 'string') {
|
|
101
|
+
const httpUrl = wsUrl.replace(/^wss:/, 'https:').replace(/^ws:/, 'http:');
|
|
102
|
+
if (this._gatewayWsUrl !== httpUrl) {
|
|
103
|
+
this._gatewayWsUrl = httpUrl;
|
|
104
|
+
this._gameServerUrl = httpUrl;
|
|
105
|
+
this.persistGameServerUrl();
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const addr = qs.address ?? qs.game_server?.address;
|
|
112
|
+
const port = qs.port ?? qs.game_server?.port;
|
|
113
|
+
if (addr && port) {
|
|
114
|
+
const url = `http://${addr}:${port}`;
|
|
115
|
+
if (this._gameServerUrl !== url) {
|
|
116
|
+
this._gameServerUrl = url;
|
|
117
|
+
this._gatewayWsUrl = null;
|
|
118
|
+
this.persistGameServerUrl();
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Manually set the game server URL. */
|
|
126
|
+
setGameServerUrl(url: string): void {
|
|
127
|
+
this._gameServerUrl = url.replace(/\/$/, '');
|
|
128
|
+
this._urlPreconfigured = true;
|
|
129
|
+
this.persistGameServerUrl();
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** WS URL derived from gameServerUrl (Gateway or direct). */
|
|
133
|
+
get wsUrl(): string | null {
|
|
134
|
+
if (!this._gameServerUrl) return null;
|
|
135
|
+
try {
|
|
136
|
+
const u = new URL(this._gameServerUrl);
|
|
137
|
+
const protocol = u.protocol === 'https:' || u.protocol === 'wss:' ? 'wss:' : 'ws:';
|
|
138
|
+
const basePath = this._gatewayWsUrl ? u.pathname.replace(/\/$/, '') : '';
|
|
139
|
+
return `${protocol}//${u.host}${basePath}/api/v1/game/stream`;
|
|
140
|
+
} catch {
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Persist gameServerUrl to auth profile (best-effort). */
|
|
146
|
+
private persistGameServerUrl(): void {
|
|
147
|
+
if (!this.agentName) return;
|
|
148
|
+
try {
|
|
149
|
+
new AuthStore().updateGameServerUrl(this._gameServerUrl ?? undefined, this.agentName);
|
|
150
|
+
} catch { /* non-critical */ }
|
|
151
|
+
}
|
|
152
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { mkdtempSync, writeFileSync } from 'fs';
|
|
2
|
+
import { tmpdir } from 'os';
|
|
3
|
+
import { join } from 'path';
|
|
4
|
+
import { describe, expect, it } from 'vitest';
|
|
5
|
+
import { parseSkillVersion, readSkillVersion } from './skill-version.js';
|
|
6
|
+
|
|
7
|
+
describe('parseSkillVersion', () => {
|
|
8
|
+
it('returns version from a normal frontmatter block', () => {
|
|
9
|
+
const md = '---\nname: clawclaw\ndescription: x\nversion: 3.2.8\n---\n\n# body\n';
|
|
10
|
+
expect(parseSkillVersion(md)).toBe('3.2.8');
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('handles CRLF line endings', () => {
|
|
14
|
+
const md = '---\r\nname: x\r\nversion: 1.0.0\r\n---\r\nbody';
|
|
15
|
+
expect(parseSkillVersion(md)).toBe('1.0.0');
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('returns null when frontmatter is missing', () => {
|
|
19
|
+
expect(parseSkillVersion('# just a heading\n')).toBeNull();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('returns null when version key is absent', () => {
|
|
23
|
+
expect(parseSkillVersion('---\nname: x\n---\nbody')).toBeNull();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('ignores a version: outside the frontmatter block', () => {
|
|
27
|
+
const md = '---\nname: x\n---\n\nversion: 9.9.9 (in body)\n';
|
|
28
|
+
expect(parseSkillVersion(md)).toBeNull();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('strips surrounding whitespace from the value', () => {
|
|
32
|
+
const md = '---\nversion: 2.1.0 \n---\nbody';
|
|
33
|
+
expect(parseSkillVersion(md)).toBe('2.1.0');
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe('readSkillVersion', () => {
|
|
38
|
+
it('reads version from a file on disk', () => {
|
|
39
|
+
const dir = mkdtempSync(join(tmpdir(), 'skill-version-'));
|
|
40
|
+
const path = join(dir, 'SKILL.md');
|
|
41
|
+
writeFileSync(path, '---\nversion: 4.0.0\n---\nbody');
|
|
42
|
+
expect(readSkillVersion(path)).toBe('4.0.0');
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('returns null when file does not exist', () => {
|
|
46
|
+
expect(readSkillVersion('/nonexistent/path/SKILL.md')).toBeNull();
|
|
47
|
+
});
|
|
48
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { readFileSync } from 'fs';
|
|
2
|
+
|
|
3
|
+
const FRONTMATTER_RE = /^---\r?\n([\s\S]*?)\r?\n---/;
|
|
4
|
+
const VERSION_RE = /^version:\s*(\S.*?)\s*$/m;
|
|
5
|
+
|
|
6
|
+
export function parseSkillVersion(content: string): string | null {
|
|
7
|
+
const fm = FRONTMATTER_RE.exec(content);
|
|
8
|
+
if (!fm) return null;
|
|
9
|
+
const v = VERSION_RE.exec(fm[1]);
|
|
10
|
+
return v ? v[1] : null;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function readSkillVersion(path: string): string | null {
|
|
14
|
+
try {
|
|
15
|
+
return parseSkillVersion(readFileSync(path, 'utf8'));
|
|
16
|
+
} catch {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
}
|