Skip to content

Commit 752dd2b

Browse files
authored
refactor(cli): consolidate repeated command registration (#119434)
1 parent f0a7497 commit 752dd2b

4 files changed

Lines changed: 202 additions & 307 deletions

File tree

‎src/cli/capability-cli/tts.ts‎

Lines changed: 99 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,13 @@ import {
1818
runTtsVoices,
1919
} from "./tts-runtime.js";
2020

21-
export function registerTtsCapabilityCommands(capability: Command): void {
22-
const tts = capability.command("tts").description("Text to speech");
23-
24-
tts
25-
.command("convert")
26-
.description("Convert text to speech")
27-
.requiredOption("--text <text>", "Input text")
28-
.option("--channel <id>", "Channel hint")
29-
.option("--voice <id>", "Voice hint")
30-
.option("--provider <id>", "Speech provider id")
31-
.option("--model <provider/model>", "Model override")
32-
.option("--output <path>", "Output path")
21+
function registerTransportTtsCommand(
22+
command: Command,
23+
defaultTransport: "local" | "gateway",
24+
run: (opts: Record<string, unknown>, transport: "local" | "gateway") => Promise<unknown>,
25+
formatText: (value: unknown) => string = (value) => JSON.stringify(value, null, 2),
26+
): void {
27+
command
3328
.option("--local", "Force local execution", false)
3429
.option("--gateway", "Force gateway execution", false)
3530
.option("--json", "Output JSON", false)
@@ -39,33 +34,54 @@ export function registerTtsCapabilityCommands(capability: Command): void {
3934
local: Boolean(opts.local),
4035
gateway: Boolean(opts.gateway),
4136
supported: ["local", "gateway"],
42-
defaultTransport: "local",
37+
defaultTransport,
4338
});
44-
const modelRef = resolveModelRefOverride(opts.model as string | undefined);
45-
if (opts.model && !modelRef.provider) {
46-
throw new Error("TTS model overrides must use the form <provider/model>.");
47-
}
48-
const provider = normalizeSpeechProviderId(
49-
typeof opts.provider === "string" && opts.provider.trim()
50-
? opts.provider.trim()
51-
: modelRef.provider,
52-
);
53-
const modelProvider = normalizeSpeechProviderId(modelRef.provider);
54-
if (provider && modelProvider && provider !== modelProvider) {
55-
throw new Error("TTS --provider must match the provider in --model.");
56-
}
57-
const result = await runTtsConvert({
58-
text: String(opts.text),
59-
channel: opts.channel as string | undefined,
60-
provider,
61-
modelId: modelProvider ? modelRef.model : undefined,
62-
voiceId: opts.voice as string | undefined,
63-
output: opts.output as string | undefined,
64-
transport,
65-
});
66-
emitJsonOrText(defaultRuntime, Boolean(opts.json), result, formatEnvelopeForText);
39+
const result = await run(opts, transport);
40+
emitJsonOrText(defaultRuntime, Boolean(opts.json), result, formatText);
6741
});
6842
});
43+
}
44+
45+
export function registerTtsCapabilityCommands(capability: Command): void {
46+
const tts = capability.command("tts").description("Text to speech");
47+
48+
registerTransportTtsCommand(
49+
tts
50+
.command("convert")
51+
.description("Convert text to speech")
52+
.requiredOption("--text <text>", "Input text")
53+
.option("--channel <id>", "Channel hint")
54+
.option("--voice <id>", "Voice hint")
55+
.option("--provider <id>", "Speech provider id")
56+
.option("--model <provider/model>", "Model override")
57+
.option("--output <path>", "Output path"),
58+
"local",
59+
async (opts, transport) => {
60+
const modelRef = resolveModelRefOverride(opts.model as string | undefined);
61+
if (opts.model && !modelRef.provider) {
62+
throw new Error("TTS model overrides must use the form <provider/model>.");
63+
}
64+
const provider = normalizeSpeechProviderId(
65+
typeof opts.provider === "string" && opts.provider.trim()
66+
? opts.provider.trim()
67+
: modelRef.provider,
68+
);
69+
const modelProvider = normalizeSpeechProviderId(modelRef.provider);
70+
if (provider && modelProvider && provider !== modelProvider) {
71+
throw new Error("TTS --provider must match the provider in --model.");
72+
}
73+
return await runTtsConvert({
74+
text: String(opts.text),
75+
channel: opts.channel as string | undefined,
76+
provider,
77+
modelId: modelProvider ? modelRef.model : undefined,
78+
voiceId: opts.voice as string | undefined,
79+
output: opts.output as string | undefined,
80+
transport,
81+
});
82+
},
83+
formatEnvelopeForText,
84+
);
6985

7086
tts
7187
.command("voices")
@@ -79,47 +95,16 @@ export function registerTtsCapabilityCommands(capability: Command): void {
7995
});
8096
});
8197

82-
tts
83-
.command("providers")
84-
.description("List speech providers")
85-
.option("--local", "Force local execution", false)
86-
.option("--gateway", "Force gateway execution", false)
87-
.option("--json", "Output JSON", false)
88-
.action(async (opts) => {
89-
await runCommandWithRuntime(defaultRuntime, async () => {
90-
const transport = resolveTransport({
91-
local: Boolean(opts.local),
92-
gateway: Boolean(opts.gateway),
93-
supported: ["local", "gateway"],
94-
defaultTransport: "local",
95-
});
96-
const result = await runTtsProviders(transport);
97-
emitJsonOrText(defaultRuntime, Boolean(opts.json), result, (value) =>
98-
JSON.stringify(value, null, 2),
99-
);
100-
});
101-
});
102-
103-
tts
104-
.command("personas")
105-
.description("List TTS personas")
106-
.option("--local", "Force local execution", false)
107-
.option("--gateway", "Force gateway execution", false)
108-
.option("--json", "Output JSON", false)
109-
.action(async (opts) => {
110-
await runCommandWithRuntime(defaultRuntime, async () => {
111-
const transport = resolveTransport({
112-
local: Boolean(opts.local),
113-
gateway: Boolean(opts.gateway),
114-
supported: ["local", "gateway"],
115-
defaultTransport: "local",
116-
});
117-
const result = await runTtsPersonas(transport);
118-
emitJsonOrText(defaultRuntime, Boolean(opts.json), result, (value) =>
119-
JSON.stringify(value, null, 2),
120-
);
121-
});
122-
});
98+
for (const [name, description, run] of [
99+
["providers", "List speech providers", runTtsProviders],
100+
["personas", "List TTS personas", runTtsPersonas],
101+
] as const) {
102+
registerTransportTtsCommand(
103+
tts.command(name).description(description),
104+
"local",
105+
(_, transport) => run(transport),
106+
);
107+
}
123108

124109
tts
125110
.command("status")
@@ -147,84 +132,45 @@ export function registerTtsCapabilityCommands(capability: Command): void {
147132
["enable", "tts.enable"],
148133
["disable", "tts.disable"],
149134
] as const) {
150-
tts
151-
.command(commandName)
152-
.description(`${commandName === "enable" ? "Enable" : "Disable"} TTS`)
153-
.option("--local", "Force local execution", false)
154-
.option("--gateway", "Force gateway execution", false)
155-
.option("--json", "Output JSON", false)
156-
.action(async (opts) => {
157-
await runCommandWithRuntime(defaultRuntime, async () => {
158-
const transport = resolveTransport({
159-
local: Boolean(opts.local),
160-
gateway: Boolean(opts.gateway),
161-
supported: ["local", "gateway"],
162-
defaultTransport: "gateway",
163-
});
164-
const result = await runTtsStateMutation({
165-
capability: capabilityId,
166-
transport,
167-
});
168-
emitJsonOrText(defaultRuntime, Boolean(opts.json), result, (value) =>
169-
JSON.stringify(value, null, 2),
170-
);
171-
});
172-
});
135+
registerTransportTtsCommand(
136+
tts
137+
.command(commandName)
138+
.description(`${commandName === "enable" ? "Enable" : "Disable"} TTS`),
139+
"gateway",
140+
(_, transport) => runTtsStateMutation({ capability: capabilityId, transport }),
141+
);
173142
}
174143

175-
tts
176-
.command("set-provider")
177-
.description("Set the active TTS provider")
178-
.requiredOption("--provider <id>", "Speech provider id")
179-
.option("--local", "Force local execution", false)
180-
.option("--gateway", "Force gateway execution", false)
181-
.option("--json", "Output JSON", false)
182-
.action(async (opts) => {
183-
await runCommandWithRuntime(defaultRuntime, async () => {
184-
const transport = resolveTransport({
185-
local: Boolean(opts.local),
186-
gateway: Boolean(opts.gateway),
187-
supported: ["local", "gateway"],
188-
defaultTransport: "gateway",
189-
});
190-
const result = await runTtsStateMutation({
191-
capability: "tts.set-provider",
192-
provider: String(opts.provider),
193-
transport,
194-
});
195-
emitJsonOrText(defaultRuntime, Boolean(opts.json), result, (value) =>
196-
JSON.stringify(value, null, 2),
197-
);
198-
});
199-
});
144+
registerTransportTtsCommand(
145+
tts
146+
.command("set-provider")
147+
.description("Set the active TTS provider")
148+
.requiredOption("--provider <id>", "Speech provider id"),
149+
"gateway",
150+
(opts, transport) =>
151+
runTtsStateMutation({
152+
capability: "tts.set-provider",
153+
provider: String(opts.provider),
154+
transport,
155+
}),
156+
);
200157

201-
tts
202-
.command("set-persona")
203-
.description("Set the active TTS persona")
204-
.option("--persona <id>", "TTS persona id")
205-
.option("--off", "Disable the active TTS persona", false)
206-
.option("--local", "Force local execution", false)
207-
.option("--gateway", "Force gateway execution", false)
208-
.option("--json", "Output JSON", false)
209-
.action(async (opts) => {
210-
await runCommandWithRuntime(defaultRuntime, async () => {
211-
const transport = resolveTransport({
212-
local: Boolean(opts.local),
213-
gateway: Boolean(opts.gateway),
214-
supported: ["local", "gateway"],
215-
defaultTransport: "gateway",
216-
});
217-
if (!opts.off && !opts.persona) {
218-
throw new Error("--persona is required unless --off is set");
219-
}
220-
const result = await runTtsStateMutation({
221-
capability: "tts.set-persona",
222-
persona: opts.off ? null : String(opts.persona),
223-
transport,
224-
});
225-
emitJsonOrText(defaultRuntime, Boolean(opts.json), result, (value) =>
226-
JSON.stringify(value, null, 2),
227-
);
158+
registerTransportTtsCommand(
159+
tts
160+
.command("set-persona")
161+
.description("Set the active TTS persona")
162+
.option("--persona <id>", "TTS persona id")
163+
.option("--off", "Disable the active TTS persona", false),
164+
"gateway",
165+
(opts, transport) => {
166+
if (!opts.off && !opts.persona) {
167+
throw new Error("--persona is required unless --off is set");
168+
}
169+
return runTtsStateMutation({
170+
capability: "tts.set-persona",
171+
persona: opts.off ? null : String(opts.persona),
172+
transport,
228173
});
229-
});
174+
},
175+
);
230176
}

0 commit comments

Comments
 (0)