File size: 6,097 Bytes
f778c12 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | import { Option, type Command } from "commander";
import { callGateway } from "../../gateway/call.js";
import { defaultRuntime } from "../../runtime.js";
import { normalizeSpeechProviderId } from "../../tts/provider-registry.js";
import { runCommandWithRuntime } from "../cli-utils.js";
import { emitJsonOrText, formatEnvelopeForText, providerSummaryText } from "./output.js";
import { resolveModelRefOverride, resolveTransport } from "./shared.js";
import {
runTtsConvert,
runTtsPersonas,
runTtsProviders,
runTtsStateMutation,
runTtsVoices,
} from "./tts-runtime.js";
function registerTransportTtsCommand<T>(
command: Command,
defaultTransport: "local" | "gateway",
run: (opts: Record<string, unknown>, transport: "local" | "gateway") => Promise<T>,
formatText: (value: T) => string = (value) => JSON.stringify(value, null, 2),
): void {
command
.option("--local", "Force local execution", false)
.option("--gateway", "Force gateway execution", false)
.option("--json", "Output JSON", false)
.action(async (opts) => {
await runCommandWithRuntime(defaultRuntime, async () => {
const transport = resolveTransport({
local: Boolean(opts.local),
gateway: Boolean(opts.gateway),
supported: ["local", "gateway"],
defaultTransport,
});
const result = await run(opts, transport);
emitJsonOrText(defaultRuntime, Boolean(opts.json), result, formatText);
});
});
}
export function registerTtsCapabilityCommands(capability: Command): void {
const tts = capability.command("tts").description("Text to speech");
registerTransportTtsCommand(
tts
.command("convert")
.description("Convert text to speech")
.requiredOption("--text <text>", "Input text")
.option("--channel <id>", "Channel hint")
.option("--voice <id>", "Voice hint")
.option("--provider <id>", "Speech provider id")
.option("--model <provider/model>", "Model override")
.option("--output <path>", "Output path"),
"local",
async (opts, transport) => {
const modelRef = resolveModelRefOverride(opts.model as string | undefined);
if (opts.model && !modelRef.provider) {
throw new Error("TTS model overrides must use the form <provider/model>.");
}
const provider = normalizeSpeechProviderId(
typeof opts.provider === "string" && opts.provider.trim()
? opts.provider.trim()
: modelRef.provider,
);
const modelProvider = normalizeSpeechProviderId(modelRef.provider);
if (provider && modelProvider && provider !== modelProvider) {
throw new Error("TTS --provider must match the provider in --model.");
}
return await runTtsConvert({
text: String(opts.text),
channel: opts.channel as string | undefined,
provider,
modelId: modelProvider ? modelRef.model : undefined,
voiceId: opts.voice as string | undefined,
output: opts.output as string | undefined,
transport,
});
},
formatEnvelopeForText,
);
tts
.command("voices")
.description("List voices for a TTS provider")
.option("--provider <id>", "Speech provider id")
.option("--json", "Output JSON", false)
.action(async (opts) => {
await runCommandWithRuntime(defaultRuntime, async () => {
const voices = await runTtsVoices(opts.provider as string | undefined);
emitJsonOrText(defaultRuntime, Boolean(opts.json), voices, providerSummaryText);
});
});
registerTransportTtsCommand(
tts
.command("providers")
.description("List speech providers")
.option("--agent <id>", "Agent whose provider state should be inspected"),
"local",
(opts, transport) => runTtsProviders(transport, opts.agent as string | undefined),
);
registerTransportTtsCommand(
tts.command("personas").description("List TTS personas"),
"local",
(_, transport) => runTtsPersonas(transport),
);
tts
.command("status")
.description("Show TTS status")
.option("--gateway", "Force gateway execution", false)
.option("--json", "Output JSON", false)
.action(async (opts) => {
await runCommandWithRuntime(defaultRuntime, async () => {
const transport = resolveTransport({
gateway: Boolean(opts.gateway),
supported: ["gateway"],
defaultTransport: "gateway",
});
const result = await callGateway({
method: "tts.status",
timeoutMs: 30_000,
});
emitJsonOrText(defaultRuntime, Boolean(opts.json), { transport, ...result }, (value) =>
JSON.stringify(value, null, 2),
);
});
});
for (const [commandName, capabilityId] of [
["enable", "tts.enable"],
["disable", "tts.disable"],
] as const) {
registerTransportTtsCommand(
tts
.command(commandName)
.description(`${commandName === "enable" ? "Enable" : "Disable"} TTS`),
"gateway",
(_, transport) => runTtsStateMutation({ capability: capabilityId, transport }),
);
}
registerTransportTtsCommand(
tts
.command("set-provider")
.description("Set the active TTS provider")
.requiredOption("--provider <id>", "Speech provider id"),
"gateway",
(opts, transport) =>
runTtsStateMutation({
capability: "tts.set-provider",
provider: String(opts.provider),
transport,
}),
);
registerTransportTtsCommand(
tts
.command("set-persona")
.description("Set the active TTS persona")
.addOption(new Option("--persona <id>", "TTS persona id").conflicts("off"))
.option("--off", "Disable the active TTS persona", false),
"gateway",
(opts, transport) => {
if (!opts.off && !opts.persona) {
throw new Error("--persona is required unless --off is set");
}
return runTtsStateMutation({
capability: "tts.set-persona",
persona: opts.off ? null : String(opts.persona),
transport,
});
},
);
}
|