@myclaw163/clawclaw-cli 0.6.100 → 0.6.105

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.
@@ -15,6 +15,7 @@ import {
15
15
  } from '../../lib/host-config-patcher.js';
16
16
 
17
17
  export const PLUGIN_ID = 'clawclaw';
18
+ export const PLUGIN_TOOL_GLOB = 'clawclaw:*';
18
19
 
19
20
  export interface ComputeOpenclawPatchOpts {
20
21
  skipPluginsAllow?: boolean;
@@ -43,18 +44,20 @@ export function computeOpenclawPatch(current: unknown, opts: ComputeOpenclawPatc
43
44
  ? { ...(cfg.plugins as Record<string, unknown>) }
44
45
  : {};
45
46
 
46
- const allowResult = unionArrayField(plugins.allow, PLUGIN_ID);
47
- if ('warning' in allowResult) {
48
- warnings.push(`plugins.allow ${allowResult.warning}`);
49
- } else {
50
- if (allowResult.added) {
51
- if (plugins.allow === undefined) {
52
- changes.push(`create plugins.allow with ["${PLUGIN_ID}"]`);
53
- } else {
47
+ // Only append to plugins.allow if it already exists as a restrictive
48
+ // allowlist. Never create it from scratch — that would inadvertently
49
+ // restrict all other plugins (browser, etc.) that were previously
50
+ // allowed by default.
51
+ if (plugins.allow !== undefined) {
52
+ const allowResult = unionArrayField(plugins.allow, PLUGIN_ID);
53
+ if ('warning' in allowResult) {
54
+ warnings.push(`plugins.allow ${allowResult.warning}`);
55
+ } else {
56
+ if (allowResult.added) {
54
57
  changes.push(`add "${PLUGIN_ID}" to plugins.allow`);
55
58
  }
59
+ plugins.allow = allowResult.next;
56
60
  }
57
- plugins.allow = allowResult.next;
58
61
  }
59
62
 
60
63
  const entries: Record<string, unknown> =
@@ -101,6 +104,18 @@ export function computeOpenclawPatch(current: unknown, opts: ComputeOpenclawPatc
101
104
  tools.alsoAllow = alsoAllowResult.next;
102
105
  }
103
106
 
107
+ // Also add the namespace glob so host-side tool-ID matching works even
108
+ // when plugin-group expansion hasn't run yet (e.g. freshly loaded plugin).
109
+ const globResult = unionArrayField(tools.alsoAllow, PLUGIN_TOOL_GLOB);
110
+ if ('warning' in globResult) {
111
+ warnings.push(`tools.alsoAllow ${globResult.warning}`);
112
+ } else {
113
+ if (globResult.added) {
114
+ changes.push(`add "${PLUGIN_TOOL_GLOB}" to tools.alsoAllow`);
115
+ }
116
+ tools.alsoAllow = globResult.next;
117
+ }
118
+
104
119
  cfg.tools = tools;
105
120
  }
106
121
 
@@ -34,7 +34,7 @@ function userStrategiesDir(): string {
34
34
  }
35
35
 
36
36
  function officialManifestFile(): string {
37
- return join(ws, 'runtime', 'official-strategies', 'manifest.json');
37
+ return join(userStrategiesDir(), '.official-manifest.json');
38
38
  }
39
39
 
40
40
  describe('strategy command', () => {
@@ -32,7 +32,7 @@ export function createStrategyCommand(): Command {
32
32
  ensureUserDataLayout(workspaceDir);
33
33
  const strategiesDir = getUserDataStrategiesDir(workspaceDir);
34
34
  return `
35
- Custom strategies:
35
+ Official and custom strategies:
36
36
  Place .ts or .js files in ${strategiesDir}
37
37
  (or $CLAWCLAW_WORKSPACE_DIR/user-data/strategies/)
38
38
  Each file must export a 'strategy' object with id, name (中文别名), description, and create() function.
@@ -74,7 +74,7 @@ Custom strategies:
74
74
  const staleIds = detectStaleOfficialStrategies().filter(id => id !== opts.export);
75
75
  if (staleIds.length > 0) {
76
76
  const ids = staleIds.join(', ');
77
- console.error(`[提示] 还有其他官方策略有更新 (${ids}),运行 \`ccl strategy --export <id> --force\` 重新导出`);
77
+ console.error(`[提示] 还有其他玩家版本与最新官方模板不同 (${ids});如需放弃本地修改并恢复官方版,运行 \`ccl strategy --export <id> --force\``);
78
78
  }
79
79
  } catch {
80
80
  // Silently ignore
@@ -120,13 +120,22 @@ export function createUpgradeCommand(): Command {
120
120
  }
121
121
  console.log(result.version);
122
122
 
123
- // After upgrade, check if any exported strategies are now stale.
123
+ // Materialize new official strategies and update only files that still
124
+ // match their recorded official baseline. User edits are preserved.
124
125
  try {
125
- const { detectStaleOfficialStrategies } = await import('../lib/strategy-export.js');
126
+ const { detectStaleOfficialStrategies, syncOfficialStrategies } = await import('../lib/strategy-export.js');
127
+ const sync = await syncOfficialStrategies();
128
+ if (sync.added.length > 0 || sync.updated.length > 0) {
129
+ console.error(`[策略] 已同步官方策略(新增 ${sync.added.length},更新 ${sync.updated.length})`);
130
+ }
131
+ const preserved = [...sync.preservedModified, ...sync.preservedUntracked, ...sync.preservedDeleted];
132
+ if (preserved.length > 0) {
133
+ console.error(`[策略] 已保留玩家修改或删除的策略:${preserved.join(', ')}`);
134
+ }
126
135
  const staleIds = detectStaleOfficialStrategies();
127
136
  if (staleIds.length > 0) {
128
137
  const ids = staleIds.join(', ');
129
- console.error(`[提示] 官方策略有更新 (${ids}),运行 \`ccl strategy --export <id> --force\` 重新导出`);
138
+ console.error(`[提示] 以下玩家版本与最新官方模板不同 (${ids});如需放弃本地修改并恢复官方版,运行 \`ccl strategy --export <id> --force\``);
130
139
  }
131
140
  } catch {
132
141
  // Silently ignore detection failures
@@ -0,0 +1,387 @@
1
+ import { afterEach, describe, expect, it, vi } from 'vitest';
2
+ import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'fs';
3
+ import { tmpdir } from 'os';
4
+ import { join } from 'path';
5
+ import {
6
+ CustomTtsError,
7
+ loadCustomTtsConfig,
8
+ parseCustomTtsConfig,
9
+ synthesizeCustomTts,
10
+ type CustomTtsConfig,
11
+ } from './custom-tts.js';
12
+ import { getWorkspaceConfigFile } from './init-command.js';
13
+
14
+ const tempDirs: string[] = [];
15
+
16
+ afterEach(() => {
17
+ vi.unstubAllGlobals();
18
+ vi.restoreAllMocks();
19
+ for (const dir of tempDirs.splice(0)) rmSync(dir, { recursive: true, force: true });
20
+ });
21
+
22
+ function config(overrides: Partial<CustomTtsConfig['response']> = {}): CustomTtsConfig {
23
+ return {
24
+ customize_tts: true,
25
+ request: {
26
+ url: 'https://tts.example/v1/speech',
27
+ method: 'POST',
28
+ headers: {
29
+ Authorization: 'Bearer secret',
30
+ 'Content-Type': 'application/json',
31
+ },
32
+ text_field: 'input',
33
+ body: {
34
+ nested: ['fixed value'],
35
+ voice: 'female-01',
36
+ },
37
+ timeout_ms: 5_000,
38
+ },
39
+ response: {
40
+ type: 'binary',
41
+ content_type: 'audio/mpeg',
42
+ filename: 'tts.mp3',
43
+ ...overrides,
44
+ },
45
+ };
46
+ }
47
+
48
+ describe('parseCustomTtsConfig', () => {
49
+ it('treats a missing or false switch as disabled', () => {
50
+ expect(parseCustomTtsConfig({})).toBeNull();
51
+ expect(parseCustomTtsConfig({ customize_tts: false })).toBeNull();
52
+ });
53
+
54
+ it('validates enabled custom TTS settings', () => {
55
+ expect(() => parseCustomTtsConfig({ customize_tts: 'true' })).toThrow(CustomTtsError);
56
+ expect(() => parseCustomTtsConfig({
57
+ customize_tts: true,
58
+ request: { url: 'file:///tmp/tts', text_field: 'input' },
59
+ })).toThrow(/http:\/\/ or https:\/\//);
60
+ expect(() => parseCustomTtsConfig({
61
+ customize_tts: true,
62
+ request: { url: 'https://tts.example/v1/speech' },
63
+ })).toThrow(/request\.text_field or place \{\{text\}\}/);
64
+ expect(() => parseCustomTtsConfig({
65
+ customize_tts: true,
66
+ request: {
67
+ url: 'https://tts.example/v1/speech',
68
+ text_field: '',
69
+ body: { messages: [{ content: '{{text}}' }] },
70
+ },
71
+ })).toThrow(/request\.text_field must be a non-empty string/);
72
+ expect(() => parseCustomTtsConfig({
73
+ customize_tts: true,
74
+ request: { url: 'https://tts.example/v1/speech', text_field: 'input', body: [] },
75
+ })).toThrow(/request\.body must be a JSON object/);
76
+ expect(() => parseCustomTtsConfig({
77
+ customize_tts: true,
78
+ request: { url: 'https://tts.example/v1/speech', text_field: 'input' },
79
+ response: { format: 'png' },
80
+ })).toThrow(/unsupported audio format "png"/);
81
+ expect(() => parseCustomTtsConfig({
82
+ customize_tts: true,
83
+ request: { url: 'https://tts.example/v1/speech', text_field: 'input' },
84
+ response: { type: 'binary', format_path: 'format' },
85
+ })).toThrow(/format_path is only supported/);
86
+ });
87
+
88
+ it('applies request and binary response defaults', () => {
89
+ expect(parseCustomTtsConfig({
90
+ customize_tts: true,
91
+ request: { url: 'https://tts.example/v1/speech', text_field: 'text' },
92
+ })).toEqual({
93
+ customize_tts: true,
94
+ request: {
95
+ url: 'https://tts.example/v1/speech',
96
+ method: 'POST',
97
+ headers: { 'Content-Type': 'application/json' },
98
+ text_field: 'text',
99
+ body: {},
100
+ timeout_ms: 10_000,
101
+ },
102
+ response: { type: 'binary' },
103
+ });
104
+
105
+ expect(parseCustomTtsConfig({
106
+ customize_tts: true,
107
+ request: { url: 'https://tts.example/v1/speech', text_field: 'input' },
108
+ response: { filename: 'voice.mp3' },
109
+ })?.response).toEqual({ type: 'binary', filename: 'voice.mp3' });
110
+
111
+ expect(parseCustomTtsConfig({
112
+ customize_tts: true,
113
+ request: { url: 'https://tts.example/v1/speech', text_field: 'input' },
114
+ response: {
115
+ type: 'json_base64',
116
+ path: 'data.audio',
117
+ format_path: 'data.format',
118
+ },
119
+ })?.response).toEqual({
120
+ type: 'json_base64',
121
+ path: 'data.audio',
122
+ format_path: 'data.format',
123
+ });
124
+ });
125
+
126
+ it('accepts a nested text placeholder without request.text_field', () => {
127
+ expect(parseCustomTtsConfig({
128
+ customize_tts: true,
129
+ request: {
130
+ url: 'https://tts.example/v1/speech',
131
+ body: {
132
+ model: 'mimo-v2.5-tts',
133
+ messages: [{ role: 'assistant', content: '{{text}}' }],
134
+ },
135
+ },
136
+ response: { type: 'json_base64', path: 'choices.0.message.audio.data', format: 'wav' },
137
+ })?.request).toMatchObject({
138
+ body: {
139
+ model: 'mimo-v2.5-tts',
140
+ messages: [{ role: 'assistant', content: '{{text}}' }],
141
+ },
142
+ });
143
+ });
144
+ });
145
+
146
+ describe('loadCustomTtsConfig', () => {
147
+ it('reads only the tts child from workspace config.json without modifying other fields', () => {
148
+ const workspace = mkdtempSync(join(tmpdir(), 'ccl-workspace-config-'));
149
+ tempDirs.push(workspace);
150
+ const path = getWorkspaceConfigFile(workspace);
151
+ const workspaceConfig = {
152
+ future_feature: { enabled: true },
153
+ tts: config(),
154
+ };
155
+ writeFileSync(path, JSON.stringify(workspaceConfig, null, 2), 'utf8');
156
+
157
+ expect(loadCustomTtsConfig(workspace)).toEqual(config());
158
+ expect(JSON.parse(readFileSync(path, 'utf8'))).toEqual(workspaceConfig);
159
+ });
160
+
161
+ it('treats a workspace config without tts as disabled', () => {
162
+ const workspace = mkdtempSync(join(tmpdir(), 'ccl-workspace-config-'));
163
+ tempDirs.push(workspace);
164
+ writeFileSync(getWorkspaceConfigFile(workspace), JSON.stringify({ future_feature: true }), 'utf8');
165
+
166
+ expect(loadCustomTtsConfig(workspace)).toBeNull();
167
+ });
168
+ });
169
+
170
+ describe('synthesizeCustomTts', () => {
171
+ it('injects text into the configured field and returns a binary audio response', async () => {
172
+ let captured: RequestInit | undefined;
173
+ vi.stubGlobal('fetch', vi.fn(async (_url: string, init: RequestInit) => {
174
+ captured = init;
175
+ return new Response(new Uint8Array([1, 2, 3]), {
176
+ status: 200,
177
+ headers: { 'Content-Type': 'audio/mpeg' },
178
+ });
179
+ }));
180
+
181
+ const fieldConfig = config();
182
+ fieldConfig.request.text_field = 'content';
183
+ const result = await synthesizeCustomTts('你好', fieldConfig);
184
+
185
+ expect(JSON.parse(String(captured?.body))).toEqual({
186
+ nested: ['fixed value'],
187
+ voice: 'female-01',
188
+ content: '你好',
189
+ });
190
+ expect((captured?.headers as Record<string, string>).Authorization).toBe('Bearer secret');
191
+ expect([...result.audio]).toEqual([1, 2, 3]);
192
+ expect(result).toMatchObject({ contentType: 'audio/mpeg', filename: 'tts.mp3' });
193
+ });
194
+
195
+ it('replaces nested text placeholders for MIMO-style request bodies', async () => {
196
+ let captured: RequestInit | undefined;
197
+ vi.stubGlobal('fetch', vi.fn(async (_url: string, init: RequestInit) => {
198
+ captured = init;
199
+ return new Response(new Uint8Array([1, 2, 3]), {
200
+ status: 200,
201
+ headers: { 'Content-Type': 'audio/wav' },
202
+ });
203
+ }));
204
+
205
+ const mimoConfig = config({ content_type: undefined, filename: undefined });
206
+ delete mimoConfig.request.text_field;
207
+ mimoConfig.request.body = {
208
+ model: 'mimo-v2.5-tts',
209
+ messages: [{ role: 'assistant', content: '{{text}}' }],
210
+ metadata: { repeated: 'speech={{text}}' },
211
+ };
212
+
213
+ const result = await synthesizeCustomTts('你好,MIMO', mimoConfig);
214
+
215
+ expect(JSON.parse(String(captured?.body))).toEqual({
216
+ model: 'mimo-v2.5-tts',
217
+ messages: [{ role: 'assistant', content: '你好,MIMO' }],
218
+ metadata: { repeated: 'speech=你好,MIMO' },
219
+ });
220
+ expect(mimoConfig.request.body).toEqual({
221
+ model: 'mimo-v2.5-tts',
222
+ messages: [{ role: 'assistant', content: '{{text}}' }],
223
+ metadata: { repeated: 'speech={{text}}' },
224
+ });
225
+ expect(result).toMatchObject({ contentType: 'audio/wav', filename: 'tts.wav' });
226
+ });
227
+
228
+ it('fills both text_field and nested placeholders when both are configured', async () => {
229
+ let captured: RequestInit | undefined;
230
+ vi.stubGlobal('fetch', vi.fn(async (_url: string, init: RequestInit) => {
231
+ captured = init;
232
+ return new Response(new Uint8Array([1]), {
233
+ status: 200,
234
+ headers: { 'Content-Type': 'audio/mpeg' },
235
+ });
236
+ }));
237
+
238
+ const mixedConfig = config();
239
+ mixedConfig.request.body = { messages: [{ content: '{{text}}' }] };
240
+ await synthesizeCustomTts('same speech', mixedConfig);
241
+
242
+ expect(JSON.parse(String(captured?.body))).toEqual({
243
+ messages: [{ content: 'same speech' }],
244
+ input: 'same speech',
245
+ });
246
+ });
247
+
248
+ it('reads base64 audio from a configured JSON path', async () => {
249
+ vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify({
250
+ data: { audio: Buffer.from('audio-data').toString('base64') },
251
+ }), { status: 200, headers: { 'Content-Type': 'application/json' } })));
252
+
253
+ const result = await synthesizeCustomTts('hello', config({
254
+ type: 'json_base64',
255
+ path: 'data.audio',
256
+ content_type: 'audio/wav',
257
+ filename: 'voice.wav',
258
+ }));
259
+
260
+ expect(result.audio.toString()).toBe('audio-data');
261
+ expect(result).toMatchObject({ contentType: 'audio/wav', filename: 'voice.wav' });
262
+ });
263
+
264
+ it('gets base64 audio format from a configured response path', async () => {
265
+ vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify({
266
+ data: {
267
+ audio: Buffer.from('audio-data').toString('base64'),
268
+ format: 'ogg',
269
+ },
270
+ }), { status: 200, headers: { 'Content-Type': 'application/json' } })));
271
+
272
+ const result = await synthesizeCustomTts('hello', config({
273
+ type: 'json_base64',
274
+ path: 'data.audio',
275
+ format_path: 'data.format',
276
+ content_type: undefined,
277
+ filename: undefined,
278
+ }));
279
+
280
+ expect(result.audio.toString()).toBe('audio-data');
281
+ expect(result).toMatchObject({ contentType: 'audio/ogg', filename: 'tts.ogg' });
282
+ });
283
+
284
+ it('gets base64 audio format from a data URL', async () => {
285
+ vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify({
286
+ audio: `data:audio/wav;base64,${Buffer.from('audio-data').toString('base64')}`,
287
+ }), { status: 200, headers: { 'Content-Type': 'application/json' } })));
288
+
289
+ const result = await synthesizeCustomTts('hello', config({
290
+ type: 'json_base64',
291
+ path: 'audio',
292
+ content_type: undefined,
293
+ filename: undefined,
294
+ }));
295
+
296
+ expect(result).toMatchObject({ contentType: 'audio/wav', filename: 'tts.wav' });
297
+ });
298
+
299
+ it('uses a configured format when the TTS response has no format metadata', async () => {
300
+ vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify({
301
+ audio: Buffer.from('audio-data').toString('base64'),
302
+ }), { status: 200, headers: { 'Content-Type': 'application/json' } })));
303
+
304
+ const result = await synthesizeCustomTts('hello', config({
305
+ type: 'json_base64',
306
+ path: 'audio',
307
+ format: 'webm',
308
+ content_type: undefined,
309
+ filename: undefined,
310
+ }));
311
+
312
+ expect(result).toMatchObject({ contentType: 'audio/webm', filename: 'tts.webm' });
313
+ });
314
+
315
+ it('rejects base64 audio when neither the response nor config provides a format', async () => {
316
+ vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify({
317
+ audio: Buffer.from('audio-data').toString('base64'),
318
+ }), { status: 200, headers: { 'Content-Type': 'application/json' } })));
319
+
320
+ await expect(synthesizeCustomTts('hello', config({
321
+ type: 'json_base64',
322
+ path: 'audio',
323
+ content_type: undefined,
324
+ filename: undefined,
325
+ }))).rejects.toThrow(/Cannot determine custom TTS audio format/);
326
+ });
327
+
328
+ it('rejects an unsupported format returned by the TTS service', async () => {
329
+ vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify({
330
+ base64: Buffer.from('not-audio').toString('base64'),
331
+ format: 'png',
332
+ }), { status: 200, headers: { 'Content-Type': 'application/json' } })));
333
+
334
+ await expect(synthesizeCustomTts('hello', config({
335
+ type: 'json_base64',
336
+ path: 'base64',
337
+ format_path: 'format',
338
+ content_type: undefined,
339
+ filename: undefined,
340
+ }))).rejects.toThrow(/unsupported audio format "png"/);
341
+ });
342
+
343
+ it('rejects a binary response that is actually JSON', async () => {
344
+ vi.stubGlobal('fetch', vi.fn(async () => new Response('{"error":"not audio"}', {
345
+ status: 200,
346
+ headers: { 'Content-Type': 'application/json' },
347
+ })));
348
+
349
+ await expect(synthesizeCustomTts('hello', config())).rejects.toThrow(/unsupported audio format "application\/json"/);
350
+ });
351
+
352
+ it('requires config format when a binary response omits Content-Type', async () => {
353
+ vi.stubGlobal('fetch', vi.fn(async () => new Response(new Uint8Array([1, 2, 3]), { status: 200 })));
354
+
355
+ await expect(synthesizeCustomTts('hello', config({
356
+ content_type: undefined,
357
+ filename: undefined,
358
+ }))).rejects.toThrow(/Cannot determine custom TTS audio format/);
359
+ });
360
+
361
+ it('downloads audio returned by a configured JSON URL path', async () => {
362
+ const fetchMock = vi.fn(async (url: string) => {
363
+ if (url === 'https://tts.example/v1/speech') {
364
+ return new Response(JSON.stringify({ result: { audio_url: 'https://cdn.example/voice.ogg' } }), {
365
+ status: 200,
366
+ headers: { 'Content-Type': 'application/json' },
367
+ });
368
+ }
369
+ return new Response(new Uint8Array([4, 5, 6]), {
370
+ status: 200,
371
+ headers: { 'Content-Type': 'audio/ogg' },
372
+ });
373
+ });
374
+ vi.stubGlobal('fetch', fetchMock);
375
+
376
+ const result = await synthesizeCustomTts('hello', config({
377
+ type: 'json_url',
378
+ path: 'result.audio_url',
379
+ content_type: undefined,
380
+ filename: undefined,
381
+ }));
382
+
383
+ expect(fetchMock).toHaveBeenCalledTimes(2);
384
+ expect(result).toMatchObject({ contentType: 'audio/ogg', filename: 'tts.ogg' });
385
+ expect([...result.audio]).toEqual([4, 5, 6]);
386
+ });
387
+ });