Skip to content

Commit ab34617

Browse files
kvmiloscopybara-github
authored andcommitted
feat(tokt): convert speech and transcription configs
PiperOrigin-RevId: 1012938559
1 parent 0bb7f4a commit ab34617

4 files changed

Lines changed: 496 additions & 14 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.adk.tokt.codecs
18+
19+
import com.google.adk.kt.types.AudioTranscriptionConfig as KtAudioTranscriptionConfig
20+
import com.google.adk.kt.types.AudioTranscriptionConfigMode as KtAudioTranscriptionConfigMode
21+
import com.google.genai.types.AudioTranscriptionConfig as GenaiAudioTranscriptionConfig
22+
import com.google.genai.types.AudioTranscriptionConfigMode as GenaiAudioTranscriptionConfigMode
23+
import kotlin.jvm.optionals.getOrNull
24+
25+
/**
26+
* Returns the genai [GenaiAudioTranscriptionConfig] view of this ADK Kotlin transcription setting.
27+
*/
28+
internal fun KtAudioTranscriptionConfig.toGenai(): GenaiAudioTranscriptionConfig {
29+
val builder = GenaiAudioTranscriptionConfig.builder()
30+
languageCodes?.let { builder.languageCodes(it) }
31+
customVocabulary?.let { builder.customVocabulary(it) }
32+
diarization?.let { builder.diarization(it) }
33+
wordTimestamp?.let { builder.wordTimestamp(it) }
34+
mode?.let { builder.mode(GenaiAudioTranscriptionConfigMode(it.name)) }
35+
return builder.build()
36+
}
37+
38+
/** Returns the ADK Kotlin view of this genai transcription setting, without [unsupportedFields]. */
39+
internal fun GenaiAudioTranscriptionConfig.toKt(): KtAudioTranscriptionConfig =
40+
KtAudioTranscriptionConfig(
41+
languageCodes = languageCodes().getOrNull(),
42+
customVocabulary = customVocabulary().getOrNull(),
43+
diarization = diarization().getOrNull(),
44+
wordTimestamp = wordTimestamp().getOrNull(),
45+
mode = mode().getOrNull()?.toKt(),
46+
)
47+
48+
/**
49+
* The fields set on this genai transcription setting that [GenaiAudioTranscriptionConfig.toKt]
50+
* cannot carry: the genai-only fields, and a `mode` ADK Kotlin has no constant for.
51+
*/
52+
internal val GenaiAudioTranscriptionConfig.unsupportedFields: List<String>
53+
get() = buildList {
54+
if (languageAuto().isPresent) add("languageAuto")
55+
if (languageHints().isPresent) add("languageHints")
56+
if (adaptationPhrases().isPresent) add("adaptationPhrases")
57+
val javaMode = mode().getOrNull()
58+
if (javaMode != null && javaMode.toKt() == null) add("mode")
59+
}
60+
61+
private fun GenaiAudioTranscriptionConfigMode.toKt(): KtAudioTranscriptionConfigMode? =
62+
enumByNameOrNull<KtAudioTranscriptionConfigMode>(knownEnum().name)

‎tokt/src/main/kotlin/com/google/adk/tokt/codecs/RunConfigCodec.kt‎

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,48 @@ package com.google.adk.tokt.codecs
1919
import com.google.adk.agents.RunConfig as JavaRunConfig
2020
import com.google.adk.kt.agents.RunConfig as KtRunConfig
2121
import com.google.adk.kt.agents.StreamingMode as KtStreamingMode
22+
import com.google.adk.kt.types.Modality as KtModality
23+
import com.google.genai.types.Modality as GenaiModality
2224

2325
/**
2426
* Converts between the Kotlin `kt.agents.RunConfig` and the ADK Java [JavaRunConfig].
2527
*
26-
* Only three settings cross both ways: streaming mode, the LLM call budget, and custom metadata.
27-
* [toJava] leaves the Java-only settings at their defaults; [fromJava] instead rejects a Java
28-
* setting the engine cannot honor rather than dropping it silently.
28+
* Streaming mode, the LLM call budget and custom metadata cross both ways; [toJava] also shows a
29+
* BIDI run's live settings (modalities, speech, transcriptions). [fromJava] rejects a Java setting
30+
* the engine cannot honor rather than dropping it silently.
2931
*/
3032
internal object RunConfigCodec {
3133

32-
/** Returns the Java [JavaRunConfig] view of the Kotlin [config]. */
33-
fun toJava(config: KtRunConfig): JavaRunConfig =
34-
JavaRunConfig.builder()
35-
// Kotlin has no BIDI, so a by-name match always resolves; NONE is the shared default.
36-
.streamingMode(
37-
enumByNameOrNull<JavaRunConfig.StreamingMode>(config.streamingMode.name)
38-
?: JavaRunConfig.StreamingMode.NONE
39-
)
40-
.maxLlmCalls(config.maxLlmCalls)
41-
.customMetadata(config.customMetadata.orEmpty())
42-
.build()
34+
/**
35+
* Returns the Java [JavaRunConfig] view of the Kotlin [config]. Only a BIDI run shows live
36+
* settings, since Kotlin turns transcription on for every run.
37+
*/
38+
fun toJava(config: KtRunConfig): JavaRunConfig {
39+
val builder =
40+
JavaRunConfig.builder()
41+
.streamingMode(
42+
when (config.streamingMode) {
43+
KtStreamingMode.NONE -> JavaRunConfig.StreamingMode.NONE
44+
KtStreamingMode.SSE -> JavaRunConfig.StreamingMode.SSE
45+
KtStreamingMode.BIDI -> JavaRunConfig.StreamingMode.BIDI
46+
}
47+
)
48+
.maxLlmCalls(config.maxLlmCalls)
49+
.customMetadata(config.customMetadata.orEmpty())
50+
if (config.streamingMode == KtStreamingMode.BIDI) {
51+
builder
52+
// A live run defaults its response modalities to AUDIO.
53+
.responseModalities(
54+
config.live.responseModalities
55+
.ifEmpty { listOf(KtModality.AUDIO) }
56+
.map { GenaiModality(it.name) }
57+
)
58+
.speechConfig(config.live.speechConfig?.toGenai())
59+
.outputAudioTranscription(config.live.outputAudioTranscription?.toGenai())
60+
.inputAudioTranscription(config.live.inputAudioTranscription?.toGenai())
61+
}
62+
return builder.build()
63+
}
4364

4465
/** Returns the Kotlin [KtRunConfig] view of the Java [config]. */
4566
@Suppress(
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.adk.tokt.codecs
18+
19+
import com.google.adk.kt.types.MultiSpeakerVoiceConfig as KtMultiSpeakerVoiceConfig
20+
import com.google.adk.kt.types.PrebuiltVoiceConfig as KtPrebuiltVoiceConfig
21+
import com.google.adk.kt.types.ReplicatedVoiceConfig as KtReplicatedVoiceConfig
22+
import com.google.adk.kt.types.SpeakerVoiceConfig as KtSpeakerVoiceConfig
23+
import com.google.adk.kt.types.SpeechConfig as KtSpeechConfig
24+
import com.google.adk.kt.types.VoiceConfig as KtVoiceConfig
25+
import com.google.adk.kt.types.VoiceConsentSignature as KtVoiceConsentSignature
26+
import com.google.genai.types.MultiSpeakerVoiceConfig as GenaiMultiSpeakerVoiceConfig
27+
import com.google.genai.types.PrebuiltVoiceConfig as GenaiPrebuiltVoiceConfig
28+
import com.google.genai.types.ReplicatedVoiceConfig as GenaiReplicatedVoiceConfig
29+
import com.google.genai.types.SpeakerVoiceConfig as GenaiSpeakerVoiceConfig
30+
import com.google.genai.types.SpeechConfig as GenaiSpeechConfig
31+
import com.google.genai.types.VoiceConfig as GenaiVoiceConfig
32+
import com.google.genai.types.VoiceConsentSignature as GenaiVoiceConsentSignature
33+
import kotlin.jvm.optionals.getOrNull
34+
35+
/**
36+
* Returns the genai [GenaiSpeechConfig] view of this ADK Kotlin voice setting; every field crosses.
37+
*/
38+
internal fun KtSpeechConfig.toGenai(): GenaiSpeechConfig {
39+
val builder = GenaiSpeechConfig.builder()
40+
voiceConfig?.let { builder.voiceConfig(it.toGenai()) }
41+
languageCode?.let { builder.languageCode(it) }
42+
multiSpeakerVoiceConfig?.let { builder.multiSpeakerVoiceConfig(it.toGenai()) }
43+
return builder.build()
44+
}
45+
46+
/**
47+
* Returns the ADK Kotlin [KtSpeechConfig] view of this genai voice setting; every field crosses.
48+
*/
49+
internal fun GenaiSpeechConfig.toKt(): KtSpeechConfig =
50+
KtSpeechConfig(
51+
voiceConfig = voiceConfig().getOrNull()?.toKt(),
52+
languageCode = languageCode().getOrNull(),
53+
multiSpeakerVoiceConfig =
54+
multiSpeakerVoiceConfig().getOrNull()?.let { multi ->
55+
KtMultiSpeakerVoiceConfig(
56+
speakerVoiceConfigs = multi.speakerVoiceConfigs().getOrNull()?.map { it.toKt() }
57+
)
58+
},
59+
)
60+
61+
private fun KtVoiceConfig.toGenai(): GenaiVoiceConfig {
62+
val builder = GenaiVoiceConfig.builder()
63+
prebuiltVoiceConfig?.let { builder.prebuiltVoiceConfig(it.toGenai()) }
64+
replicatedVoiceConfig?.let { builder.replicatedVoiceConfig(it.toGenai()) }
65+
voice?.let { builder.voice(it) }
66+
return builder.build()
67+
}
68+
69+
private fun KtReplicatedVoiceConfig.toGenai(): GenaiReplicatedVoiceConfig {
70+
val builder = GenaiReplicatedVoiceConfig.builder()
71+
mimeType?.let { builder.mimeType(it) }
72+
voiceSampleAudio?.let { builder.voiceSampleAudio(it) }
73+
consentAudio?.let { builder.consentAudio(it) }
74+
voiceConsentSignature?.let { builder.voiceConsentSignature(it.toGenai()) }
75+
return builder.build()
76+
}
77+
78+
private fun KtMultiSpeakerVoiceConfig.toGenai(): GenaiMultiSpeakerVoiceConfig {
79+
val builder = GenaiMultiSpeakerVoiceConfig.builder()
80+
speakerVoiceConfigs?.let { configs -> builder.speakerVoiceConfigs(configs.map { it.toGenai() }) }
81+
return builder.build()
82+
}
83+
84+
private fun KtPrebuiltVoiceConfig.toGenai(): GenaiPrebuiltVoiceConfig {
85+
val builder = GenaiPrebuiltVoiceConfig.builder()
86+
voiceName?.let { builder.voiceName(it) }
87+
return builder.build()
88+
}
89+
90+
private fun KtVoiceConsentSignature.toGenai(): GenaiVoiceConsentSignature {
91+
val builder = GenaiVoiceConsentSignature.builder()
92+
signature?.let { builder.signature(it) }
93+
return builder.build()
94+
}
95+
96+
private fun KtSpeakerVoiceConfig.toGenai(): GenaiSpeakerVoiceConfig {
97+
val builder = GenaiSpeakerVoiceConfig.builder()
98+
speaker?.let { builder.speaker(it) }
99+
voiceConfig?.let { builder.voiceConfig(it.toGenai()) }
100+
return builder.build()
101+
}
102+
103+
private fun GenaiVoiceConfig.toKt(): KtVoiceConfig =
104+
KtVoiceConfig(
105+
prebuiltVoiceConfig =
106+
prebuiltVoiceConfig().getOrNull()?.let {
107+
KtPrebuiltVoiceConfig(voiceName = it.voiceName().getOrNull())
108+
},
109+
replicatedVoiceConfig = replicatedVoiceConfig().getOrNull()?.toKt(),
110+
voice = voice().getOrNull(),
111+
)
112+
113+
private fun GenaiReplicatedVoiceConfig.toKt(): KtReplicatedVoiceConfig =
114+
KtReplicatedVoiceConfig(
115+
mimeType = mimeType().getOrNull(),
116+
voiceSampleAudio = voiceSampleAudio().getOrNull(),
117+
consentAudio = consentAudio().getOrNull(),
118+
voiceConsentSignature =
119+
voiceConsentSignature().getOrNull()?.let {
120+
KtVoiceConsentSignature(signature = it.signature().getOrNull())
121+
},
122+
)
123+
124+
private fun GenaiSpeakerVoiceConfig.toKt(): KtSpeakerVoiceConfig =
125+
KtSpeakerVoiceConfig(
126+
speaker = speaker().getOrNull(),
127+
voiceConfig = voiceConfig().getOrNull()?.toKt(),
128+
)

0 commit comments

Comments
 (0)