@myclaw163/clawclaw-cli 0.1.0
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 +434 -0
- package/bin/clawclaw-cli.mjs +4 -0
- package/package.json +48 -0
- package/personas//345/217/243/346/230/257/345/277/203/351/235/236.md +50 -0
- package/personas//345/277/240/345/277/203/350/200/201/344/273/206.md +51 -0
- package/personas//346/227/240/344/272/272/350/256/276.md +1 -0
- package/personas//350/264/253/345/230/264/346/215/237/345/217/213.md +48 -0
- package/personas//350/275/257/350/220/214/350/205/271/351/273/221.md +51 -0
- package/scripts/check-skill-command-surface.mjs +118 -0
- package/scripts/check_monitor.cjs +435 -0
- package/scripts/check_monitor.test.mjs +212 -0
- package/scripts/enable_ccl_permissions.cjs +164 -0
- package/scripts/enable_ccl_permissions.test.mjs +163 -0
- package/scripts/enable_monitor.cjs +677 -0
- package/scripts/enable_monitor.test.mjs +201 -0
- package/scripts/find-hide-spots.py +157 -0
- package/scripts/postinstall.mjs +20 -0
- package/scripts/sync-bundled-skill.mjs +254 -0
- package/scripts/sync-bundled-skill.test.mjs +152 -0
- package/skills/clawclaw/.gitkeep +0 -0
- package/skills/clawclaw/SKILL.md +275 -0
- package/skills/clawclaw/references/CHATTERBOX.md +142 -0
- package/skills/clawclaw/references/CLAUDE-CODE-MONITOR.md +224 -0
- package/skills/clawclaw/references/COMMANDS.md +165 -0
- package/skills/clawclaw/references/GAME-MECHANICS.md +195 -0
- package/skills/clawclaw/references/HUB.md +48 -0
- package/skills/clawclaw/references/KNOWLEDGE.md +42 -0
- package/skills/clawclaw/references/STRATEGIES.md +59 -0
- package/skills/clawclaw/references/STREAM.md +75 -0
- package/skills/clawclaw/references/TACTICS.md +65 -0
- package/src/assets/clawclaw-ascii-map.txt +40 -0
- package/src/cli.ts +113 -0
- package/src/commands/_schema.ts +124 -0
- package/src/commands/account.test.ts +391 -0
- package/src/commands/account.ts +338 -0
- package/src/commands/competition.ts +42 -0
- package/src/commands/config.ts +30 -0
- package/src/commands/data.test.ts +33 -0
- package/src/commands/data.ts +22 -0
- package/src/commands/do.test.ts +117 -0
- package/src/commands/do.ts +195 -0
- package/src/commands/events.test.ts +144 -0
- package/src/commands/events.ts +268 -0
- package/src/commands/game-map.test.ts +28 -0
- package/src/commands/game-monitor-status.test.ts +58 -0
- package/src/commands/game-start-plan.test.ts +356 -0
- package/src/commands/game.ts +2046 -0
- package/src/commands/history-player.test.ts +105 -0
- package/src/commands/history.ts +573 -0
- package/src/commands/hub.test.ts +134 -0
- package/src/commands/hub.ts +249 -0
- package/src/commands/knowledge.test.ts +13 -0
- package/src/commands/knowledge.ts +176 -0
- package/src/commands/load.test.ts +107 -0
- package/src/commands/load.ts +26 -0
- package/src/commands/meeting-history.test.ts +106 -0
- package/src/commands/memory.ts +40 -0
- package/src/commands/peek.ts +45 -0
- package/src/commands/persona.ts +62 -0
- package/src/commands/setup/codex.ts +266 -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 +124 -0
- package/src/commands/setup/openclaw.ts +162 -0
- package/src/commands/skill.ts +128 -0
- package/src/commands/state.ts +46 -0
- package/src/commands/strategy.test.ts +172 -0
- package/src/commands/strategy.ts +170 -0
- package/src/commands/upgrade.test.ts +82 -0
- package/src/commands/upgrade.ts +157 -0
- package/src/commands/watch.test.ts +1313 -0
- package/src/commands/watch.ts +824 -0
- package/src/lib/account-guidance.test.ts +32 -0
- package/src/lib/account-guidance.ts +62 -0
- package/src/lib/auth.test.ts +116 -0
- package/src/lib/auth.ts +189 -0
- package/src/lib/command-meta.ts +37 -0
- package/src/lib/competition.ts +79 -0
- package/src/lib/custom-tts.test.ts +387 -0
- package/src/lib/custom-tts.ts +463 -0
- package/src/lib/game-client-tts.test.ts +151 -0
- package/src/lib/game-client.test.ts +82 -0
- package/src/lib/game-client.ts +472 -0
- package/src/lib/game-context.ts +92 -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 +61 -0
- package/src/lib/http-transport.ts +118 -0
- package/src/lib/hub-client.test.ts +57 -0
- package/src/lib/hub-client.ts +88 -0
- package/src/lib/hub-install.test.ts +157 -0
- package/src/lib/hub-install.ts +213 -0
- package/src/lib/hub-reminder.ts +69 -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 +79 -0
- package/src/lib/init-command.ts +136 -0
- package/src/lib/knowledge-store.test.ts +170 -0
- package/src/lib/knowledge-store.ts +369 -0
- package/src/lib/load-context.test.ts +62 -0
- package/src/lib/load-context.ts +49 -0
- package/src/lib/match-state.test.ts +134 -0
- package/src/lib/match-state.ts +94 -0
- package/src/lib/normalize.ts +42 -0
- package/src/lib/persona.test.ts +84 -0
- package/src/lib/persona.ts +120 -0
- package/src/lib/room.test.ts +74 -0
- package/src/lib/room.ts +129 -0
- package/src/lib/server-registry.test.ts +54 -0
- package/src/lib/server-registry.ts +185 -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 +314 -0
- package/src/lib/strategy-export.ts +352 -0
- package/src/lib/user-data.test.ts +98 -0
- package/src/lib/user-data.ts +370 -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 +177 -0
- package/src/perception/player-history-store.ts +262 -0
- package/src/pipeline/event-format.test.ts +327 -0
- package/src/pipeline/event-format.ts +595 -0
- package/src/pipeline/event-hints.ts +204 -0
- package/src/pipeline/event-store.test.ts +28 -0
- package/src/pipeline/event-store.ts +193 -0
- package/src/pipeline/pipeline.ts +35 -0
- package/src/pipeline/player-projection.test.ts +168 -0
- package/src/pipeline/player-projection.ts +370 -0
- package/src/runtime/auto-upgrade.test.ts +66 -0
- package/src/runtime/auto-upgrade.ts +31 -0
- package/src/runtime/event-daemon.test.ts +268 -0
- package/src/runtime/event-daemon.ts +531 -0
- package/src/runtime/owner-control.ts +150 -0
- package/src/runtime/raw-ws-log.test.ts +33 -0
- package/src/runtime/raw-ws-log.ts +32 -0
- package/src/runtime/room-lobby-runtime.test.ts +95 -0
- package/src/runtime/room-lobby-runtime.ts +367 -0
- package/src/runtime/runtime-logger.ts +107 -0
- package/src/runtime/ws-client.test.ts +169 -0
- package/src/runtime/ws-client.ts +280 -0
- package/src/sdk/action.ts +155 -0
- package/src/sdk/index.ts +112 -0
- package/src/sdk/types.ts +163 -0
- package/src/strategies/avoid-lone.ts +14 -0
- package/src/strategies/avoid-players.knowledge.md +19 -0
- package/src/strategies/avoid-players.ts +18 -0
- package/src/strategies/corpse-patrol.ts +21 -0
- package/src/strategies/crab-sabotage.ts +24 -0
- package/src/strategies/custom-module.test.ts +270 -0
- package/src/strategies/find-player.ts +19 -0
- package/src/strategies/game-utils.test.ts +242 -0
- package/src/strategies/game-utils.ts +853 -0
- package/src/strategies/goals/anchor-linger.ts +77 -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 +113 -0
- package/src/strategies/goals/crab-octopus-reflexes.ts +101 -0
- package/src/strategies/goals/crab-sabotage-top.ts +226 -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/follow-companion-goal.ts +106 -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/hide-top.ts +197 -0
- package/src/strategies/goals/keep-away-goal.ts +221 -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 +27 -0
- package/src/strategies/goals/linger-corpse-goal.ts +35 -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 +133 -0
- package/src/strategies/goals/move-room-goal.ts +60 -0
- package/src/strategies/goals/normal-shrimp-top.test.ts +97 -0
- package/src/strategies/goals/normal-shrimp-top.ts +248 -0
- package/src/strategies/goals/paradise-fish-top.test.ts +126 -0
- package/src/strategies/goals/paradise-fish-top.ts +224 -0
- package/src/strategies/goals/patrol-top.ts +57 -0
- package/src/strategies/goals/report-patrol-top.ts +86 -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 +87 -0
- package/src/strategies/goals/warrior-shrimp-top.ts +267 -0
- package/src/strategies/greeting.ts +53 -0
- package/src/strategies/hide-spots.ts +59 -0
- package/src/strategies/hide.ts +26 -0
- package/src/strategies/kill-frenzy.ts +15 -0
- package/src/strategies/kill-lone.knowledge.md +17 -0
- package/src/strategies/kill-lone.ts +16 -0
- package/src/strategies/kill-target.ts +21 -0
- package/src/strategies/knowledge-contract.ts +85 -0
- package/src/strategies/loader.test.ts +710 -0
- package/src/strategies/loader.ts +183 -0
- package/src/strategies/lone-kill-task.ts +24 -0
- package/src/strategies/meeting-gate.test.ts +59 -0
- package/src/strategies/meeting-gate.ts +28 -0
- package/src/strategies/move-room.ts +18 -0
- package/src/strategies/new-events-backfill.ts +98 -0
- package/src/strategies/off-route-points.ts +105 -0
- package/src/strategies/paradise-fish.knowledge.md +19 -0
- package/src/strategies/paradise-fish.ts +24 -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 +355 -0
- package/src/strategies/pathfind/walkable-grid.ts +117 -0
- package/src/strategies/patrol.ts +14 -0
- package/src/strategies/player-targets.ts +13 -0
- package/src/strategies/report-patrol.ts +14 -0
- package/src/strategies/scanner.ts +238 -0
- package/src/strategies/shrimp-memory.knowledge.md +19 -0
- package/src/strategies/shrimp-memory.ts +24 -0
- package/src/strategies/social-task.test.ts +28 -0
- package/src/strategies/social-task.ts +52 -0
- package/src/strategies/spawn.ts +82 -0
- package/src/strategies/speech-module.ts +123 -0
- package/src/strategies/strategy-loop.test.ts +15 -0
- package/src/strategies/strategy-loop.ts +748 -0
- package/src/strategies/task-kill-report.ts +20 -0
- package/src/strategies/task-only.ts +14 -0
- package/src/strategies/task-report.ts +21 -0
- package/src/strategies/types.ts +155 -0
- package/src/strategies/warrior-memory.knowledge.md +21 -0
- package/src/strategies/warrior-memory.ts +19 -0
|
@@ -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
|
+
});
|