@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.
Files changed (198) hide show
  1. package/README.md +440 -0
  2. package/bin/clawclaw-cli.mjs +4 -0
  3. package/package.json +48 -0
  4. package/personas//347/220/206/346/231/272/346/270/251/345/222/214.md +23 -0
  5. package/personas//350/200/201/350/260/213/346/267/261/347/256/227.md +22 -0
  6. package/personas//350/257/232/346/201/263/347/233/264/347/216/207.md +22 -0
  7. package/personas//350/275/273/346/235/276/346/264/273/346/263/274.md +22 -0
  8. package/personas//351/207/216/346/200/247/345/217/233/351/200/206.md +23 -0
  9. package/scripts/postinstall.mjs +20 -0
  10. package/scripts/sync-bundled-skill.mjs +245 -0
  11. package/scripts/sync-bundled-skill.test.mjs +152 -0
  12. package/skills/clawclaw/SKILL.md +240 -0
  13. package/skills/clawclaw/references/CHATTERBOX.md +142 -0
  14. package/skills/clawclaw/references/COMMANDS.md +132 -0
  15. package/skills/clawclaw/references/GAME-MECHANICS.md +186 -0
  16. package/skills/clawclaw/references/HUB.md +48 -0
  17. package/skills/clawclaw/references/KNOWLEDGE.md +43 -0
  18. package/skills/clawclaw/references/STRATEGIES.md +57 -0
  19. package/skills/clawclaw/references/STREAM.md +58 -0
  20. package/skills/clawclaw/references/TACTICS.md +65 -0
  21. package/src/assets/clawclaw-ascii-map.txt +40 -0
  22. package/src/cli.ts +153 -0
  23. package/src/commands/_schema.ts +109 -0
  24. package/src/commands/account.ts +209 -0
  25. package/src/commands/config.ts +30 -0
  26. package/src/commands/do.test.ts +37 -0
  27. package/src/commands/do.ts +95 -0
  28. package/src/commands/events.ts +22 -0
  29. package/src/commands/game-map.test.ts +28 -0
  30. package/src/commands/game-start-plan.test.ts +142 -0
  31. package/src/commands/game.ts +882 -0
  32. package/src/commands/history-player.test.ts +102 -0
  33. package/src/commands/history.ts +573 -0
  34. package/src/commands/hub.test.ts +96 -0
  35. package/src/commands/hub.ts +234 -0
  36. package/src/commands/knowledge.test.ts +19 -0
  37. package/src/commands/knowledge.ts +168 -0
  38. package/src/commands/load.test.ts +51 -0
  39. package/src/commands/load.ts +13 -0
  40. package/src/commands/meeting-history.test.ts +106 -0
  41. package/src/commands/memory.ts +40 -0
  42. package/src/commands/peek.ts +38 -0
  43. package/src/commands/persona.ts +57 -0
  44. package/src/commands/setup/codex.ts +248 -0
  45. package/src/commands/setup/hermes.test.ts +96 -0
  46. package/src/commands/setup/hermes.ts +76 -0
  47. package/src/commands/setup/index.ts +13 -0
  48. package/src/commands/setup/openclaw.test.ts +114 -0
  49. package/src/commands/setup/openclaw.ts +147 -0
  50. package/src/commands/skill.ts +128 -0
  51. package/src/commands/state.ts +46 -0
  52. package/src/commands/strategy.test.ts +135 -0
  53. package/src/commands/strategy.ts +189 -0
  54. package/src/commands/tts.ts +128 -0
  55. package/src/commands/upgrade.test.ts +91 -0
  56. package/src/commands/upgrade.ts +154 -0
  57. package/src/commands/watch.test.ts +973 -0
  58. package/src/commands/watch.ts +709 -0
  59. package/src/lib/auth.test.ts +59 -0
  60. package/src/lib/auth.ts +186 -0
  61. package/src/lib/command-meta.ts +37 -0
  62. package/src/lib/game-client.ts +391 -0
  63. package/src/lib/host-config-patcher.test.ts +130 -0
  64. package/src/lib/host-config-patcher.ts +151 -0
  65. package/src/lib/http-keepalive.ts +15 -0
  66. package/src/lib/http-transport.test.ts +42 -0
  67. package/src/lib/http-transport.ts +113 -0
  68. package/src/lib/hub-client.test.ts +56 -0
  69. package/src/lib/hub-client.ts +88 -0
  70. package/src/lib/hub-install.test.ts +98 -0
  71. package/src/lib/hub-install.ts +121 -0
  72. package/src/lib/hub-reminder.ts +75 -0
  73. package/src/lib/hub-unzip.test.ts +69 -0
  74. package/src/lib/hub-unzip.ts +62 -0
  75. package/src/lib/init-command.test.ts +75 -0
  76. package/src/lib/init-command.ts +120 -0
  77. package/src/lib/knowledge-store.test.ts +180 -0
  78. package/src/lib/knowledge-store.ts +374 -0
  79. package/src/lib/load-context.test.ts +52 -0
  80. package/src/lib/load-context.ts +52 -0
  81. package/src/lib/match-state.test.ts +134 -0
  82. package/src/lib/match-state.ts +94 -0
  83. package/src/lib/netease-tts.ts +83 -0
  84. package/src/lib/normalize.ts +42 -0
  85. package/src/lib/persona.test.ts +41 -0
  86. package/src/lib/persona.ts +72 -0
  87. package/src/lib/server-registry.ts +152 -0
  88. package/src/lib/skill-version.test.ts +48 -0
  89. package/src/lib/skill-version.ts +19 -0
  90. package/src/lib/strategy-export.test.ts +232 -0
  91. package/src/lib/strategy-export.ts +242 -0
  92. package/src/lib/tts-keys.ts +7 -0
  93. package/src/lib/tts-speech.test.ts +63 -0
  94. package/src/lib/tts-speech.ts +76 -0
  95. package/src/lib/workspace-argv.test.ts +49 -0
  96. package/src/lib/workspace-argv.ts +44 -0
  97. package/src/perception/player-history-store.test.ts +87 -0
  98. package/src/perception/player-history-store.ts +194 -0
  99. package/src/pipeline/event-store.ts +124 -0
  100. package/src/pipeline/pipeline.ts +35 -0
  101. package/src/runtime/auto-upgrade.test.ts +66 -0
  102. package/src/runtime/auto-upgrade.ts +31 -0
  103. package/src/runtime/daemon.ts +100 -0
  104. package/src/runtime/event-daemon.test.ts +28 -0
  105. package/src/runtime/event-daemon.ts +371 -0
  106. package/src/runtime/opening-mover.ts +303 -0
  107. package/src/runtime/raw-ws-log.test.ts +33 -0
  108. package/src/runtime/raw-ws-log.ts +32 -0
  109. package/src/runtime/runtime-logger.ts +99 -0
  110. package/src/runtime/ws-client.test.ts +47 -0
  111. package/src/runtime/ws-client.ts +272 -0
  112. package/src/sdk/action.ts +166 -0
  113. package/src/sdk/index.ts +110 -0
  114. package/src/sdk/types.ts +146 -0
  115. package/src/strategies/avoid-lone.ts +11 -0
  116. package/src/strategies/avoid-players.knowledge.md +20 -0
  117. package/src/strategies/avoid-players.ts +15 -0
  118. package/src/strategies/corpse-patrol.ts +22 -0
  119. package/src/strategies/crab-sabotage.ts +21 -0
  120. package/src/strategies/custom-module.test.ts +269 -0
  121. package/src/strategies/find-player.ts +16 -0
  122. package/src/strategies/game-utils.test.ts +164 -0
  123. package/src/strategies/game-utils.ts +721 -0
  124. package/src/strategies/goals/avoid-lone-top.ts +168 -0
  125. package/src/strategies/goals/avoid-players-top.test.ts +83 -0
  126. package/src/strategies/goals/avoid-players-top.ts +121 -0
  127. package/src/strategies/goals/conversation-goal.ts +51 -0
  128. package/src/strategies/goals/corpse-patrol-top.ts +91 -0
  129. package/src/strategies/goals/crab-octopus-reflexes.ts +93 -0
  130. package/src/strategies/goals/crab-sabotage-top.ts +197 -0
  131. package/src/strategies/goals/emergency-hunt-goal.ts +28 -0
  132. package/src/strategies/goals/find-player-top.ts +93 -0
  133. package/src/strategies/goals/flee-players-goal.ts +53 -0
  134. package/src/strategies/goals/goal-manager.ts +41 -0
  135. package/src/strategies/goals/goal-root-strategy.ts +49 -0
  136. package/src/strategies/goals/goal.ts +28 -0
  137. package/src/strategies/goals/keep-away-goal.ts +206 -0
  138. package/src/strategies/goals/kill-frenzy-top.ts +80 -0
  139. package/src/strategies/goals/kill-lone-top.ts +160 -0
  140. package/src/strategies/goals/kill-target-goal.ts +59 -0
  141. package/src/strategies/goals/kill-target-top.ts +109 -0
  142. package/src/strategies/goals/leaf-goal.ts +25 -0
  143. package/src/strategies/goals/linger-corpse-goal.ts +79 -0
  144. package/src/strategies/goals/lone-kill-core.ts +82 -0
  145. package/src/strategies/goals/lone-kill-goal.ts +24 -0
  146. package/src/strategies/goals/lone-kill-task-top.test.ts +85 -0
  147. package/src/strategies/goals/lone-kill-task-top.ts +86 -0
  148. package/src/strategies/goals/move-room-goal.ts +60 -0
  149. package/src/strategies/goals/normal-shrimp-top.test.ts +80 -0
  150. package/src/strategies/goals/normal-shrimp-top.ts +242 -0
  151. package/src/strategies/goals/paradise-fish-top.test.ts +126 -0
  152. package/src/strategies/goals/paradise-fish-top.ts +219 -0
  153. package/src/strategies/goals/patrol-top.ts +57 -0
  154. package/src/strategies/goals/report-patrol-top.ts +80 -0
  155. package/src/strategies/goals/safe-task-goal.ts +102 -0
  156. package/src/strategies/goals/social-task-top.ts +161 -0
  157. package/src/strategies/goals/task-kill-report-top.ts +163 -0
  158. package/src/strategies/goals/task-only-top.ts +57 -0
  159. package/src/strategies/goals/task-or-patrol-goal.ts +41 -0
  160. package/src/strategies/goals/task-report-top.ts +57 -0
  161. package/src/strategies/goals/wander-task-goal.ts +33 -0
  162. package/src/strategies/goals/warrior-shrimp-top.test.ts +86 -0
  163. package/src/strategies/goals/warrior-shrimp-top.ts +248 -0
  164. package/src/strategies/greeting.ts +53 -0
  165. package/src/strategies/kill-frenzy.ts +12 -0
  166. package/src/strategies/kill-lone.knowledge.md +20 -0
  167. package/src/strategies/kill-lone.ts +13 -0
  168. package/src/strategies/kill-target.ts +18 -0
  169. package/src/strategies/loader.test.ts +678 -0
  170. package/src/strategies/loader.ts +172 -0
  171. package/src/strategies/lone-kill-task.ts +21 -0
  172. package/src/strategies/meeting-gate.test.ts +59 -0
  173. package/src/strategies/meeting-gate.ts +23 -0
  174. package/src/strategies/move-room.ts +15 -0
  175. package/src/strategies/new-events-backfill.ts +98 -0
  176. package/src/strategies/paradise-fish.knowledge.md +20 -0
  177. package/src/strategies/paradise-fish.ts +25 -0
  178. package/src/strategies/pathfind/clawclaw-walkable.bin +0 -0
  179. package/src/strategies/pathfind/distance-field.ts +150 -0
  180. package/src/strategies/pathfind/escape-planner.test.ts +197 -0
  181. package/src/strategies/pathfind/escape-planner.ts +348 -0
  182. package/src/strategies/pathfind/walkable-grid.ts +117 -0
  183. package/src/strategies/patrol.ts +11 -0
  184. package/src/strategies/player-targets.ts +13 -0
  185. package/src/strategies/report-patrol.ts +11 -0
  186. package/src/strategies/shrimp-memory.knowledge.md +20 -0
  187. package/src/strategies/shrimp-memory.ts +25 -0
  188. package/src/strategies/social-task.test.ts +28 -0
  189. package/src/strategies/social-task.ts +49 -0
  190. package/src/strategies/spawn.ts +71 -0
  191. package/src/strategies/speech-module.ts +123 -0
  192. package/src/strategies/strategy-loop.ts +757 -0
  193. package/src/strategies/task-kill-report.ts +17 -0
  194. package/src/strategies/task-only.ts +11 -0
  195. package/src/strategies/task-report.ts +22 -0
  196. package/src/strategies/types.ts +96 -0
  197. package/src/strategies/warrior-memory.knowledge.md +20 -0
  198. package/src/strategies/warrior-memory.ts +16 -0
@@ -0,0 +1,232 @@
1
+ // src/lib/strategy-export.test.ts
2
+ import { describe, it, expect, beforeEach, afterEach } from 'vitest';
3
+ import { existsSync, mkdirSync, writeFileSync, rmSync, readFileSync } from 'fs';
4
+ import { join } from 'path';
5
+ import { tmpdir } from 'os';
6
+ import {
7
+ rewriteBuiltinImports,
8
+ exportStrategy,
9
+ detectStaleOfficialStrategies,
10
+ readManifest,
11
+ } from './strategy-export.js';
12
+
13
+ describe('strategy-export', () => {
14
+ let testWorkspace: string;
15
+ let originalEnv: string | undefined;
16
+
17
+ beforeEach(() => {
18
+ testWorkspace = join(tmpdir(), `ccl-test-${Date.now()}`);
19
+ mkdirSync(testWorkspace, { recursive: true });
20
+ originalEnv = process.env.CLAWCLAW_WORKSPACE_DIR;
21
+ process.env.CLAWCLAW_WORKSPACE_DIR = testWorkspace;
22
+ });
23
+
24
+ afterEach(() => {
25
+ if (originalEnv !== undefined) {
26
+ process.env.CLAWCLAW_WORKSPACE_DIR = originalEnv;
27
+ } else {
28
+ delete process.env.CLAWCLAW_WORKSPACE_DIR;
29
+ }
30
+ rmSync(testWorkspace, { recursive: true, force: true });
31
+ });
32
+
33
+ describe('rewriteBuiltinImports', () => {
34
+ it('should rewrite ALL relative imports to @myclaw163/clawclaw-cli', () => {
35
+ const source = `
36
+ import type { GameState } from '../sdk/types.js';
37
+ import { Action } from '../sdk/action.js';
38
+ import { dist, KILL_RANGE } from './game-utils.js';
39
+ import { GoalRootStrategy } from './goals/goal-root-strategy.js';
40
+ import { TaskOnlyTop } from './goals/task-only-top.js';
41
+ import { parseGreetingArgs } from './greeting.js';
42
+ import { TTS_TEXT_MAX_LENGTH } from '../lib/tts-keys.js';
43
+
44
+ export const strategy = { id: 'test', description: 'test', create: () => ({ name: 'test', decide: () => [] }) };
45
+ `;
46
+ const rewritten = rewriteBuiltinImports(source, 'test', '0.6.31');
47
+
48
+ expect(rewritten).toContain('// Exported from test@0.6.31');
49
+ // All relative imports should be rewritten
50
+ expect(rewritten).not.toContain("'../sdk/types.js'");
51
+ expect(rewritten).not.toContain("'../sdk/action.js'");
52
+ expect(rewritten).not.toContain("'./game-utils.js'");
53
+ expect(rewritten).not.toContain("'./goals/goal-root-strategy.js'");
54
+ expect(rewritten).not.toContain("'./goals/task-only-top.js'");
55
+ expect(rewritten).not.toContain("'./greeting.js'");
56
+ expect(rewritten).not.toContain("'../lib/tts-keys.js'");
57
+ // Verify rewritten imports use @myclaw163/clawclaw-cli
58
+ expect(rewritten).toContain("from '@myclaw163/clawclaw-cli'");
59
+ });
60
+
61
+ it('should preserve user imports from clawclaw-cli', () => {
62
+ const source = `
63
+ import { Action } from 'clawclaw-cli';
64
+ import type { GameState } from '../sdk/types.js';
65
+
66
+ export const strategy = { id: 'test', description: 'test', create: () => ({ name: 'test', decide: () => [] }) };
67
+ `;
68
+ const rewritten = rewriteBuiltinImports(source, 'test', '0.6.31');
69
+ expect(rewritten).toContain("import { Action } from 'clawclaw-cli'");
70
+ expect(rewritten).toContain("from 'clawclaw-cli'");
71
+ });
72
+ });
73
+
74
+ describe('exportStrategy', () => {
75
+ it('should export a formerly goal-based strategy with its knowledge sidecar', async () => {
76
+ await exportStrategy('kill-lone');
77
+
78
+ const strategiesDir = join(testWorkspace, 'strategies');
79
+ expect(existsSync(join(strategiesDir, 'kill-lone.ts'))).toBe(true);
80
+ expect(existsSync(join(strategiesDir, 'kill-lone.knowledge.md'))).toBe(true);
81
+ });
82
+
83
+ it('should export a valid strategy', async () => {
84
+ await exportStrategy('task-only');
85
+
86
+ const strategyFile = join(testWorkspace, 'strategies', 'task-only.ts');
87
+ const manifestFile = join(testWorkspace, 'strategies', '.official', 'manifest.json');
88
+
89
+ expect(existsSync(strategyFile)).toBe(true);
90
+ expect(existsSync(manifestFile)).toBe(true);
91
+
92
+ const strategy = readFileSync(strategyFile, 'utf8');
93
+ expect(strategy).toContain('// Exported from task-only@');
94
+ expect(strategy).toContain("from '@myclaw163/clawclaw-cli'");
95
+
96
+ const manifest = readManifest();
97
+ expect(manifest['task-only']).toBeDefined();
98
+ expect(manifest['task-only'].sourceHash).toBeDefined();
99
+ });
100
+
101
+ it('should reject strategy with unexported SDK symbol', async () => {
102
+ // Create a fake builtin strategy that imports a symbol not in the SDK
103
+ const fakeDir = join(testWorkspace, 'fake-builtin');
104
+ mkdirSync(fakeDir, { recursive: true });
105
+ const fakeFile = join(fakeDir, 'broken-strategy.ts');
106
+ writeFileSync(fakeFile, `
107
+ import { TotallyMissingSymbol } from 'clawclaw-cli';
108
+ export const strategy = {
109
+ id: 'broken-strategy',
110
+ description: 'test',
111
+ create: () => ({ name: 'broken', decide: () => [TotallyMissingSymbol] }),
112
+ };
113
+ `, 'utf8');
114
+
115
+ // Temporarily override locateBuiltinSource to point to our fake
116
+ const { rewriteBuiltinImports } = await import('./strategy-export.js');
117
+ const rewritten = rewriteBuiltinImports(
118
+ readFileSync(fakeFile, 'utf8'),
119
+ 'broken-strategy',
120
+ '0.0.0'
121
+ );
122
+
123
+ // Validate should catch the missing symbol
124
+ const { validateExportable } = await import('./strategy-export.js') as any;
125
+ if (typeof validateExportable === 'function') {
126
+ const result = await validateExportable(rewritten);
127
+ expect(result.ok).toBe(false);
128
+ expect(result.error).toBeDefined();
129
+ }
130
+ });
131
+
132
+ it('should refuse to overwrite untracked files without --force', async () => {
133
+ const strategiesDir = join(testWorkspace, 'strategies');
134
+ mkdirSync(strategiesDir, { recursive: true });
135
+ writeFileSync(join(strategiesDir, 'task-only.ts'), '// user file', 'utf8');
136
+
137
+
138
+ await expect(exportStrategy('task-only')).rejects.toThrow(
139
+ 'already exists and is not tracked. Use --force to overwrite.'
140
+ );
141
+ });
142
+
143
+ it('should overwrite untracked files with --force', async () => {
144
+ const strategiesDir = join(testWorkspace, 'strategies');
145
+ mkdirSync(strategiesDir, { recursive: true });
146
+ writeFileSync(join(strategiesDir, 'task-only.ts'), '// user file', 'utf8');
147
+
148
+ await exportStrategy('task-only', { force: true });
149
+
150
+ const strategy = readFileSync(join(strategiesDir, 'task-only.ts'), 'utf8');
151
+ expect(strategy).toContain('// Exported from task-only@');
152
+ });
153
+
154
+ it('should allow re-exporting tracked strategies without --force', async () => {
155
+ // First export
156
+ await exportStrategy('task-only');
157
+
158
+ const strategiesDir = join(testWorkspace, 'strategies');
159
+ const strategyFile = join(strategiesDir, 'task-only.ts');
160
+
161
+ // Modify the file
162
+ writeFileSync(strategyFile, '// user customized', 'utf8');
163
+
164
+ // Re-export without --force (should succeed because it's tracked)
165
+ await exportStrategy('task-only');
166
+
167
+ const strategy = readFileSync(strategyFile, 'utf8');
168
+ expect(strategy).toContain('// Exported from task-only@');
169
+ });
170
+
171
+ it('should reject non-existent strategies', async () => {
172
+ await expect(exportStrategy('non-existent-strategy')).rejects.toThrow(
173
+ "Built-in strategy 'non-existent-strategy' not found."
174
+ );
175
+ });
176
+ });
177
+
178
+ describe('detectStaleOfficialStrategies', () => {
179
+ it('should return empty array when no strategies are exported', () => {
180
+ expect(detectStaleOfficialStrategies()).toEqual([]);
181
+ });
182
+
183
+ it('should return empty array when all strategies are up to date', async () => {
184
+ await exportStrategy('task-only');
185
+ expect(detectStaleOfficialStrategies()).toEqual([]);
186
+ });
187
+
188
+ it('should return stale IDs when source hash mismatch', async () => {
189
+ await exportStrategy('task-only');
190
+
191
+ // Manually modify the manifest to simulate a stale hash
192
+ const manifestFile = join(testWorkspace, 'strategies', '.official', 'manifest.json');
193
+ const manifest = JSON.parse(readFileSync(manifestFile, 'utf8'));
194
+ manifest['task-only'].sourceHash = 'stale-hash-value';
195
+ writeFileSync(manifestFile, JSON.stringify(manifest, null, 2), 'utf8');
196
+
197
+ expect(detectStaleOfficialStrategies()).toEqual(['task-only']);
198
+ });
199
+
200
+ it('should return multiple stale IDs', async () => {
201
+ await exportStrategy('task-only');
202
+ await exportStrategy('kill-lone');
203
+
204
+ // Make both stale
205
+ const manifestFile = join(testWorkspace, 'strategies', '.official', 'manifest.json');
206
+ const manifest = JSON.parse(readFileSync(manifestFile, 'utf8'));
207
+ manifest['task-only'].sourceHash = 'stale-1';
208
+ manifest['kill-lone'].sourceHash = 'stale-2';
209
+ writeFileSync(manifestFile, JSON.stringify(manifest, null, 2), 'utf8');
210
+
211
+ const stale = detectStaleOfficialStrategies();
212
+ expect(stale).toContain('task-only');
213
+ expect(stale).toContain('kill-lone');
214
+ expect(stale).toHaveLength(2);
215
+ });
216
+
217
+ it('should detect stale when sidecar hash changes', async () => {
218
+ await exportStrategy('kill-lone');
219
+
220
+ // Verify kill-lone has a sidecar hash (it has a .knowledge.md)
221
+ const manifestFile = join(testWorkspace, 'strategies', '.official', 'manifest.json');
222
+ const manifest = JSON.parse(readFileSync(manifestFile, 'utf8'));
223
+ expect(manifest['kill-lone'].sidecarHash).toBeDefined();
224
+
225
+ // Make sidecar stale
226
+ manifest['kill-lone'].sidecarHash = 'stale-sidecar';
227
+ writeFileSync(manifestFile, JSON.stringify(manifest, null, 2), 'utf8');
228
+
229
+ expect(detectStaleOfficialStrategies()).toEqual(['kill-lone']);
230
+ });
231
+ });
232
+ });
@@ -0,0 +1,242 @@
1
+ // src/lib/strategy-export.ts
2
+ // Export official strategies to workspace for customization.
3
+ import { existsSync, readFileSync, writeFileSync, mkdirSync, renameSync, mkdtempSync, rmSync } from 'fs';
4
+ import { join, dirname } from 'path';
5
+ import { tmpdir } from 'os';
6
+ import { fileURLToPath } from 'url';
7
+ import { createHash } from 'crypto';
8
+ import { getWorkspaceDir } from './init-command.js';
9
+ import { importUserFile } from '../strategies/loader.js';
10
+
11
+ // ── Constants ──────────────────────────────────────────────────────
12
+ const OFFICIAL_DIR = '.official';
13
+ const MANIFEST_FILE = 'manifest.json';
14
+
15
+ // ── Types ──────────────────────────────────────────────────────────
16
+ export interface ManifestEntry {
17
+ sourceHash: string;
18
+ sidecarHash?: string;
19
+ }
20
+
21
+ export type Manifest = Record<string, ManifestEntry>;
22
+
23
+ // ── Helpers ────────────────────────────────────────────────────────
24
+ function strategiesDir(): string {
25
+ return join(getWorkspaceDir(), 'strategies');
26
+ }
27
+
28
+ function officialDir(): string {
29
+ return join(strategiesDir(), OFFICIAL_DIR);
30
+ }
31
+
32
+ function manifestPath(): string {
33
+ return join(officialDir(), MANIFEST_FILE);
34
+ }
35
+
36
+ export function readManifest(): Manifest {
37
+ const path = manifestPath();
38
+ if (!existsSync(path)) return {};
39
+ try {
40
+ return JSON.parse(readFileSync(path, 'utf8')) as Manifest;
41
+ } catch {
42
+ return {};
43
+ }
44
+ }
45
+
46
+ function writeManifest(manifest: Manifest): void {
47
+ mkdirSync(officialDir(), { recursive: true });
48
+ writeFileSync(manifestPath(), JSON.stringify(manifest, null, 2) + '\n', 'utf8');
49
+ }
50
+
51
+ function locateBuiltinSource(id: string): string | null {
52
+ const currentDir = dirname(fileURLToPath(import.meta.url));
53
+ const strategiesDir = join(currentDir, '..', 'strategies');
54
+ const filePath = join(strategiesDir, `${id}.ts`);
55
+ return existsSync(filePath) ? filePath : null;
56
+ }
57
+
58
+ function hashContent(content: string): string {
59
+ return createHash('sha256').update(content).digest('hex').slice(0, 16);
60
+ }
61
+
62
+ export function rewriteBuiltinImports(source: string, id: string, version: string): string {
63
+ // Rewrite ALL relative imports (./... and ../...) to '@myclaw163/clawclaw-cli'.
64
+ // The SDK must export every symbol used; validateExportable catches gaps.
65
+ const rewritten = source.replace(/from\s+['"]\.\.?\/[^'"]+['"]/g, `from '@myclaw163/clawclaw-cli'`);
66
+ const provenance = `// Exported from ${id}@${version} — customize freely\n`;
67
+ return provenance + rewritten;
68
+ }
69
+
70
+ async function validateExportable(rewritten: string): Promise<{ ok: boolean; error?: string }> {
71
+ try {
72
+ const tmpDir = mkdtempSync(join(tmpdir(), 'ccl-export-validate-'));
73
+ const tmpFile = join(tmpDir, 'strategy.ts');
74
+ try {
75
+ writeFileSync(tmpFile, rewritten, 'utf8');
76
+ const mod = await importUserFile(tmpFile);
77
+ const entry = mod.strategy as any;
78
+ if (!entry || typeof entry !== 'object') {
79
+ return { ok: false, error: 'Missing or invalid strategy export' };
80
+ }
81
+ if (typeof entry.id !== 'string') {
82
+ return { ok: false, error: 'strategy.id must be a string' };
83
+ }
84
+ if (typeof entry.description !== 'string') {
85
+ return { ok: false, error: 'strategy.description must be a string' };
86
+ }
87
+ if (typeof entry.create !== 'function') {
88
+ return { ok: false, error: 'strategy.create must be a function' };
89
+ }
90
+ // Try calling create() to catch runtime errors.
91
+ // Some strategies require args (move-room, social-task, etc.) — those throw
92
+ // "requires X" errors, which are expected and not validation failures.
93
+ try {
94
+ entry.create();
95
+ } catch (err: any) {
96
+ const msg = err?.message ?? String(err);
97
+ if (msg.includes('requires') || msg.includes('argument') || msg.includes('param')) {
98
+ // Expected: strategy needs args, but the import resolution worked.
99
+ } else {
100
+ return { ok: false, error: `create() failed: ${msg}` };
101
+ }
102
+ }
103
+ // Static check: parse imports from 'clawclaw-cli' and verify each symbol
104
+ // is actually exported by the SDK. esbuild/tsx don't validate named exports
105
+ // at link time (missing symbols resolve to undefined, not errors).
106
+ const sdkExports = await getSdkExports();
107
+ const importPattern = /import\s+(?:type\s+)?\{([^}]+)\}\s+from\s+['"](?:@myclaw163\/)?clawclaw-cli['"]/g;
108
+ let match;
109
+ while ((match = importPattern.exec(rewritten)) !== null) {
110
+ const names = match[1].split(',').map(s => s.trim().replace(/^type\s+/, '').replace(/\s+as\s+.*$/, '').trim()).filter(Boolean);
111
+ for (const name of names) {
112
+ if (!sdkExports.has(name)) {
113
+ return { ok: false, error: `'${name}' is not exported by @myclaw163/clawclaw-cli SDK` };
114
+ }
115
+ }
116
+ }
117
+ return { ok: true };
118
+ } finally {
119
+ rmSync(tmpDir, { recursive: true, force: true });
120
+ }
121
+ } catch (err: any) {
122
+ return { ok: false, error: err?.message ?? String(err) };
123
+ }
124
+ }
125
+
126
+ // Cache SDK exports to avoid re-parsing on every validation.
127
+ let sdkExportsCache: Set<string> | null = null;
128
+
129
+ async function getSdkExports(): Promise<Set<string>> {
130
+ if (sdkExportsCache) return sdkExportsCache;
131
+ const sdkPath = join(dirname(fileURLToPath(import.meta.url)), '..', 'sdk', 'index.ts');
132
+ const source = readFileSync(sdkPath, 'utf8');
133
+ const exports = new Set<string>();
134
+ // Match: export { X, Y, Z } from '...'
135
+ const exportPattern = /export\s+(?:type\s+)?\{([^}]+)\}/g;
136
+ let match;
137
+ while ((match = exportPattern.exec(source)) !== null) {
138
+ const names = match[1].split(',').map(s => s.trim().replace(/^type\s+/, '').replace(/\s+as\s+.*$/, '').trim()).filter(Boolean);
139
+ for (const name of names) {
140
+ exports.add(name);
141
+ }
142
+ }
143
+ sdkExportsCache = exports;
144
+ return exports;
145
+ }
146
+
147
+ function writeAtomic(path: string, content: string): void {
148
+ const tmpPath = path + '.tmp';
149
+ writeFileSync(tmpPath, content, 'utf8');
150
+ renameSync(tmpPath, path);
151
+ }
152
+
153
+ function readPackageVersion(): string {
154
+ try {
155
+ const currentDir = dirname(fileURLToPath(import.meta.url));
156
+ const pkgDir = join(currentDir, '..', '..');
157
+ const pkgPath = join(pkgDir, 'package.json');
158
+ const pkg = JSON.parse(readFileSync(pkgPath, 'utf8'));
159
+ return pkg.version ?? '0.0.0';
160
+ } catch {
161
+ return '0.0.0';
162
+ }
163
+ }
164
+
165
+ // ── Main API ───────────────────────────────────────────────────────
166
+ export async function exportStrategy(id: string, { force = false } = {}): Promise<void> {
167
+ const sourcePath = locateBuiltinSource(id);
168
+ if (!sourcePath) {
169
+ throw new Error(`Built-in strategy '${id}' not found.`);
170
+ }
171
+
172
+ const strategyFile = join(strategiesDir(), `${id}.ts`);
173
+ const manifest = readManifest();
174
+
175
+ if (existsSync(strategyFile) && !manifest[id] && !force) {
176
+ throw new Error(`${strategyFile} already exists and is not tracked. Use --force to overwrite.`);
177
+ }
178
+
179
+ const source = readFileSync(sourcePath, 'utf8');
180
+ const version = readPackageVersion();
181
+ const rewritten = rewriteBuiltinImports(source, id, version);
182
+
183
+ const validation = await validateExportable(rewritten);
184
+ if (!validation.ok) {
185
+ throw new Error(`Validation failed: ${validation.error}`);
186
+ }
187
+
188
+ // Compute content hashes for staleness detection.
189
+ const sourceHash = hashContent(source);
190
+ const sidecarSource = sourcePath.replace(/\.ts$/, '.knowledge.md');
191
+ const sidecarHash = existsSync(sidecarSource) ? hashContent(readFileSync(sidecarSource, 'utf8')) : undefined;
192
+
193
+ // Write metadata first, then strategy file last.
194
+ mkdirSync(officialDir(), { recursive: true });
195
+
196
+ const entry: ManifestEntry = { sourceHash };
197
+ if (sidecarHash !== undefined) entry.sidecarHash = sidecarHash;
198
+ manifest[id] = entry;
199
+ writeManifest(manifest);
200
+
201
+ mkdirSync(strategiesDir(), { recursive: true });
202
+ writeAtomic(strategyFile, rewritten);
203
+
204
+ // Copy the knowledge sidecar (<id>.knowledge.md) so the exported strategy
205
+ // keeps its contract — otherwise `ccl strategy --info` returns null for it.
206
+ if (sidecarHash !== undefined) {
207
+ writeAtomic(join(strategiesDir(), `${id}.knowledge.md`), readFileSync(sidecarSource, 'utf8'));
208
+ }
209
+ }
210
+
211
+ export function detectStaleOfficialStrategies(): string[] {
212
+ const manifest = readManifest();
213
+ const staleIds: string[] = [];
214
+
215
+ for (const [id, entry] of Object.entries(manifest)) {
216
+ const sourcePath = locateBuiltinSource(id);
217
+ if (!sourcePath) {
218
+ // Built-in strategy was removed — mark as stale so user can clean up.
219
+ staleIds.push(id);
220
+ continue;
221
+ }
222
+
223
+ const currentSourceHash = hashContent(readFileSync(sourcePath, 'utf8'));
224
+ if (currentSourceHash !== entry.sourceHash) {
225
+ staleIds.push(id);
226
+ continue;
227
+ }
228
+
229
+ const sidecarSource = sourcePath.replace(/\.ts$/, '.knowledge.md');
230
+ if (existsSync(sidecarSource)) {
231
+ const currentSidecarHash = hashContent(readFileSync(sidecarSource, 'utf8'));
232
+ if (currentSidecarHash !== entry.sidecarHash) {
233
+ staleIds.push(id);
234
+ }
235
+ } else if (entry.sidecarHash !== undefined) {
236
+ // Sidecar was removed in the new version.
237
+ staleIds.push(id);
238
+ }
239
+ }
240
+
241
+ return staleIds;
242
+ }
@@ -0,0 +1,7 @@
1
+ /** NetEase Leihuo TTS provider id (stored under each account's `tts.keys.leihuo`). */
2
+ export const TTS_PROVIDER_LEIHUO = 'leihuo';
3
+
4
+ export const DEFAULT_TTS_VOICE = 'female-shaonv';
5
+
6
+ /** NetEase TTS input limit for CLI-side synthesis. */
7
+ export const TTS_TEXT_MAX_LENGTH = 100;
@@ -0,0 +1,63 @@
1
+ import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest';
2
+ import { mkdtempSync, rmSync } from 'fs';
3
+ import { tmpdir } from 'os';
4
+ import { join } from 'path';
5
+ import { AuthStore } from './auth.js';
6
+ import { TTS_PROVIDER_LEIHUO } from './tts-keys.js';
7
+ import { synthesizeNeteaseTTS } from './netease-tts.js';
8
+ import { maybeSynthesizeSpeechAudioUrl } from './tts-speech.js';
9
+
10
+ vi.mock('./netease-tts.js', () => ({
11
+ synthesizeNeteaseTTS: vi.fn(async () => ({
12
+ audio: Buffer.from('audio'),
13
+ contentType: 'audio/mpeg',
14
+ })),
15
+ }));
16
+
17
+ describe('maybeSynthesizeSpeechAudioUrl', () => {
18
+ let dir: string;
19
+ let authFile: string;
20
+
21
+ beforeEach(() => {
22
+ dir = mkdtempSync(join(tmpdir(), 'clawclaw-tts-'));
23
+ authFile = join(dir, '.auth.json');
24
+ });
25
+
26
+ afterEach(() => {
27
+ rmSync(dir, { recursive: true, force: true });
28
+ vi.clearAllMocks();
29
+ });
30
+
31
+ it('returns existing url without calling TTS', async () => {
32
+ const client = { uploadAudio: vi.fn() } as any;
33
+ const url = await maybeSynthesizeSpeechAudioUrl('hi', 'https://example.com/a.mp3', client);
34
+ expect(url).toBe('https://example.com/a.mp3');
35
+ expect(client.uploadAudio).not.toHaveBeenCalled();
36
+ });
37
+
38
+ it('returns undefined when no TTS key is configured', async () => {
39
+ const store = new AuthStore(authFile);
40
+ store.addProfile({ agentName: 'lobster-1', apiKey: 'claw_1', serverUrl: 'https://example.com' });
41
+ const client = { uploadAudio: vi.fn() } as any;
42
+ const url = await maybeSynthesizeSpeechAudioUrl('hi', undefined, client, { authStore: store });
43
+ expect(url).toBeUndefined();
44
+ });
45
+
46
+ it('synthesizes and uploads when leihuo key is configured', async () => {
47
+ const store = new AuthStore(authFile);
48
+ store.addProfile({ agentName: 'lobster-1', apiKey: 'claw_1', serverUrl: 'https://example.com' });
49
+ store.setTtsKey(TTS_PROVIDER_LEIHUO, 'sk-test');
50
+ store.setTtsDefaultVoice('male-qn-qingse');
51
+ const client = {
52
+ uploadAudio: vi.fn(async () => ({ audio_url: 'https://cdn.example.com/a.mp3' })),
53
+ } as any;
54
+ const url = await maybeSynthesizeSpeechAudioUrl('你好', undefined, client, { authStore: store });
55
+ expect(url).toBe('https://cdn.example.com/a.mp3');
56
+ expect(client.uploadAudio).toHaveBeenCalledOnce();
57
+ expect(synthesizeNeteaseTTS).toHaveBeenCalledWith(expect.objectContaining({
58
+ apiKey: 'sk-test',
59
+ text: '你好',
60
+ voice: 'male-qn-qingse',
61
+ }));
62
+ });
63
+ });
@@ -0,0 +1,76 @@
1
+ import { AuthStore } from './auth.js';
2
+ import type { GameClient } from './game-client.js';
3
+ import { synthesizeNeteaseTTS } from './netease-tts.js';
4
+ import {
5
+ DEFAULT_TTS_VOICE,
6
+ TTS_PROVIDER_LEIHUO,
7
+ TTS_TEXT_MAX_LENGTH,
8
+ } from './tts-keys.js';
9
+
10
+ export interface SynthesizeSpeechAudioOptions {
11
+ voice?: string;
12
+ provider?: string;
13
+ authStore?: AuthStore;
14
+ }
15
+
16
+ /**
17
+ * Synthesize speech audio via the configured TTS provider and upload to OSS.
18
+ * @throws when the provider key is missing, text is too long, or synthesis/upload fails.
19
+ */
20
+ export async function synthesizeAndUploadSpeechAudio(
21
+ text: string,
22
+ client: GameClient,
23
+ opts: SynthesizeSpeechAudioOptions = {},
24
+ ): Promise<string> {
25
+ const store = opts.authStore ?? new AuthStore();
26
+ const provider = opts.provider ?? TTS_PROVIDER_LEIHUO;
27
+ const apiKey = store.getTtsKey(provider);
28
+ if (!apiKey) {
29
+ throw new Error(
30
+ `TTS API key for provider "${provider}" is not configured. Use \`clawclaw-cli tts config <apiKey>\` first.`,
31
+ );
32
+ }
33
+ if (text.length > TTS_TEXT_MAX_LENGTH) {
34
+ throw new Error(`TTS text must be ${TTS_TEXT_MAX_LENGTH} characters or fewer.`);
35
+ }
36
+
37
+ const audio = await synthesizeNeteaseTTS({
38
+ apiKey,
39
+ text,
40
+ voice: opts.voice ?? store.getTtsDefaultVoice() ?? DEFAULT_TTS_VOICE,
41
+ });
42
+ const upload = await client.uploadAudio(audio.audio, 'tts-audio.mp3', audio.contentType);
43
+ return upload.audio_url;
44
+ }
45
+
46
+ /**
47
+ * When the active account has a Leihuo TTS key, synthesize and upload audio for speech.
48
+ * Returns undefined if no key, a URL was already provided, or auto-TTS was skipped (e.g. text too long).
49
+ */
50
+ export async function maybeSynthesizeSpeechAudioUrl(
51
+ text: string,
52
+ existingUrl: string | undefined,
53
+ client: GameClient,
54
+ opts: SynthesizeSpeechAudioOptions = {},
55
+ ): Promise<string | undefined> {
56
+ if (existingUrl) return existingUrl;
57
+
58
+ const store = opts.authStore ?? new AuthStore();
59
+ const provider = opts.provider ?? TTS_PROVIDER_LEIHUO;
60
+ const apiKey = store.getTtsKey(provider);
61
+ if (!apiKey) return undefined;
62
+
63
+ if (text.length > TTS_TEXT_MAX_LENGTH) {
64
+ console.error(
65
+ `Speech is ${text.length} characters; auto TTS supports up to ${TTS_TEXT_MAX_LENGTH}. Sending without audio.`,
66
+ );
67
+ return undefined;
68
+ }
69
+
70
+ try {
71
+ return await synthesizeAndUploadSpeechAudio(text, client, opts);
72
+ } catch (err: any) {
73
+ console.error(`Auto TTS failed: ${err?.message ?? String(err)}. Sending speech without audio.`);
74
+ return undefined;
75
+ }
76
+ }
@@ -0,0 +1,49 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { CLAWCLAW_WORKSPACE_DIR, applyWorkspaceArgv, extractWorkspaceDirArgv } from './workspace-argv.js';
3
+
4
+ describe('extractWorkspaceDirArgv', () => {
5
+ it('supports --workspace-dir X', () => {
6
+ expect(extractWorkspaceDirArgv(['node', 'cli', '--workspace-dir', 'tmp/workspace'])).toBe('tmp/workspace');
7
+ });
8
+
9
+ it('supports --workspace-dir=X', () => {
10
+ expect(extractWorkspaceDirArgv(['node', 'cli', '--workspace-dir=tmp/workspace'])).toBe('tmp/workspace');
11
+ });
12
+
13
+ it('returns undefined when the option is not provided', () => {
14
+ expect(extractWorkspaceDirArgv(['node', 'cli', 'do'])).toBeUndefined();
15
+ });
16
+
17
+ it('returns undefined for an empty value', () => {
18
+ expect(extractWorkspaceDirArgv(['node', 'cli', '--workspace-dir='])).toBeUndefined();
19
+ expect(extractWorkspaceDirArgv(['node', 'cli', '--workspace-dir', ' '])).toBeUndefined();
20
+ });
21
+ });
22
+
23
+ describe('applyWorkspaceArgv', () => {
24
+ it('writes an absolute workspace dir using the injected cwd', () => {
25
+ const env: NodeJS.ProcessEnv = {};
26
+
27
+ expect(applyWorkspaceArgv(['node', 'cli', '--workspace-dir', 'tmp/workspace'], env, 'D:\\open-claw-kill')).toBe(
28
+ 'D:\\open-claw-kill\\tmp\\workspace',
29
+ );
30
+ expect(env[CLAWCLAW_WORKSPACE_DIR]).toBe('D:\\open-claw-kill\\tmp\\workspace');
31
+ });
32
+
33
+ it('lets explicit argv override an existing workspace env value', () => {
34
+ const env: NodeJS.ProcessEnv = { [CLAWCLAW_WORKSPACE_DIR]: 'D:\\old\\workspace' };
35
+
36
+ expect(applyWorkspaceArgv(['node', 'cli', '--workspace-dir', 'new/workspace'], env, 'D:\\open-claw-kill')).toBe(
37
+ 'D:\\open-claw-kill\\new\\workspace',
38
+ );
39
+ expect(env[CLAWCLAW_WORKSPACE_DIR]).toBe('D:\\open-claw-kill\\new\\workspace');
40
+ });
41
+
42
+ it('leaves env untouched when the option is absent or empty', () => {
43
+ const env: NodeJS.ProcessEnv = { EXISTING: 'keep' };
44
+
45
+ expect(applyWorkspaceArgv(['node', 'cli', 'do'], env, 'D:\\open-claw-kill')).toBeUndefined();
46
+ expect(applyWorkspaceArgv(['node', 'cli', '--workspace-dir='], env, 'D:\\open-claw-kill')).toBeUndefined();
47
+ expect(env).toEqual({ EXISTING: 'keep' });
48
+ });
49
+ });