11// Discord plugin module implements tts behavior.
2- import {
3- getTtsProvider ,
4- resolveAgentDir ,
5- resolveTtsConfig ,
6- resolveTtsPrefsPath ,
7- type ResolvedTtsConfig ,
8- } from "openclaw/plugin-sdk/agent-runtime" ;
2+ import { resolveAgentDir } from "openclaw/plugin-sdk/agent-runtime" ;
93import type { OpenClawConfig , TtsConfig } from "openclaw/plugin-sdk/config-contracts" ;
10- import { parseTtsDirectives } from "openclaw/plugin-sdk/speech" ;
11- import { normalizeOptionalString , uniqueStrings } from "openclaw/plugin-sdk/string-coerce-runtime" ;
4+ import { normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime" ;
125import { getDiscordRuntime } from "../runtime.js" ;
136import { sanitizeVoiceReplyTextForSpeech } from "./sanitize.js" ;
147
@@ -34,58 +27,6 @@ type VoiceReplyAudioResult =
3427 error ?: string ;
3528 } ;
3629
37- function mergeTtsConfig ( base : TtsConfig , override ?: TtsConfig ) : TtsConfig {
38- if ( ! override ) {
39- return base ;
40- }
41- const baseProviders = base . providers ?? { } ;
42- const overrideProviders = override . providers ?? { } ;
43- const mergedProviders = Object . fromEntries (
44- uniqueStrings ( [ ...Object . keys ( baseProviders ) , ...Object . keys ( overrideProviders ) ] ) . map (
45- ( providerId ) => {
46- const baseProvider = baseProviders [ providerId ] ?? { } ;
47- const overrideProvider = overrideProviders [ providerId ] ?? { } ;
48- return [
49- providerId ,
50- {
51- ...baseProvider ,
52- ...overrideProvider ,
53- } ,
54- ] ;
55- } ,
56- ) ,
57- ) ;
58- return {
59- ...base ,
60- ...override ,
61- modelOverrides : {
62- ...base . modelOverrides ,
63- ...override . modelOverrides ,
64- } ,
65- ...( Object . keys ( mergedProviders ) . length === 0 ? { } : { providers : mergedProviders } ) ,
66- } ;
67- }
68-
69- function resolveVoiceTtsConfig ( params : { cfg : OpenClawConfig ; override ?: TtsConfig } ) : {
70- cfg : OpenClawConfig ;
71- resolved : ResolvedTtsConfig ;
72- } {
73- if ( ! params . override ) {
74- return { cfg : params . cfg , resolved : resolveTtsConfig ( params . cfg ) } ;
75- }
76- const base = params . cfg . messages ?. tts ?? { } ;
77- const merged = mergeTtsConfig ( base , params . override ) ;
78- const messages = params . cfg . messages ?? { } ;
79- const cfg = {
80- ...params . cfg ,
81- messages : {
82- ...messages ,
83- tts : merged ,
84- } ,
85- } ;
86- return { cfg, resolved : resolveTtsConfig ( cfg ) } ;
87- }
88-
8930export async function transcribeVoiceAudio ( params : {
9031 cfg : OpenClawConfig ;
9132 agentId : string ;
@@ -106,25 +47,21 @@ export async function synthesizeVoiceReplyAudio(params: {
10647 replyText : string ;
10748 speakerLabel : string ;
10849} ) : Promise < VoiceReplyAudioResult > {
109- const { cfg : ttsCfg , resolved : ttsConfig } = resolveVoiceTtsConfig ( {
50+ const runtime = getDiscordRuntime ( ) ;
51+ const prepared = await runtime . tts . prepareTtsRequest ( {
11052 cfg : params . cfg ,
11153 override : params . override ,
54+ text : params . replyText ,
11255 } ) ;
113- const directive = parseTtsDirectives ( params . replyText , ttsConfig . modelOverrides , {
114- cfg : ttsCfg ,
115- providerConfigs : ttsConfig . providerConfigs ,
116- preferredProviderId : getTtsProvider ( ttsConfig , resolveTtsPrefsPath ( ttsConfig ) ) ,
117- } ) ;
56+ const directive = prepared . directives ;
11857 const rawSpeakText = directive . overrides . ttsText ?? directive . cleanedText . trim ( ) ;
11958 const speakText = sanitizeVoiceReplyTextForSpeech ( rawSpeakText , params . speakerLabel ) ;
12059 if ( ! speakText ) {
12160 return { status : "empty" } ;
12261 }
123-
124- const runtime = getDiscordRuntime ( ) ;
12562 const streamResult = await runtime . tts . textToSpeechStream ?.( {
12663 text : speakText ,
127- cfg : ttsCfg ,
64+ cfg : prepared . cfg ,
12865 channel : "discord" ,
12966 overrides : directive . overrides ,
13067 disableFallback : true ,
@@ -141,7 +78,7 @@ export async function synthesizeVoiceReplyAudio(params: {
14178
14279 const result = await runtime . tts . textToSpeech ( {
14380 text : speakText ,
144- cfg : ttsCfg ,
81+ cfg : prepared . cfg ,
14582 channel : "discord" ,
14683 overrides : directive . overrides ,
14784 } ) ;
0 commit comments