Skip to content

Commit ca51a53

Browse files
kvmiloscopybara-github
authored andcommitted
refactor(tokt): convert an ADK Java RunConfig for a live run
PiperOrigin-RevId: 994231843
1 parent 0bb7f4a commit ca51a53

4 files changed

Lines changed: 770 additions & 23 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
* Converts a live transcription setting between the ADK Kotlin [KtAudioTranscriptionConfig] and the
27+
* genai [GenaiAudioTranscriptionConfig] that the ADK Java `RunConfig` carries. The genai-only
28+
* fields, and a `mode` Kotlin has no constant for, cannot cross, so [unsupportedFields] names the
29+
* ones a Java caller set.
30+
*/
31+
internal object AudioTranscriptionConfigCodec {
32+
33+
/** Returns the genai [GenaiAudioTranscriptionConfig] view of the Kotlin [config]. */
34+
fun toJava(config: KtAudioTranscriptionConfig): GenaiAudioTranscriptionConfig =
35+
with(config) {
36+
val builder = GenaiAudioTranscriptionConfig.builder()
37+
languageCodes?.let { builder.languageCodes(it) }
38+
customVocabulary?.let { builder.customVocabulary(it) }
39+
diarization?.let { builder.diarization(it) }
40+
wordTimestamp?.let { builder.wordTimestamp(it) }
41+
mode?.let { builder.mode(GenaiAudioTranscriptionConfigMode(it.name)) }
42+
builder.build()
43+
}
44+
45+
/** Returns the Kotlin [KtAudioTranscriptionConfig] view of the genai [config]. */
46+
fun fromJava(config: GenaiAudioTranscriptionConfig): KtAudioTranscriptionConfig =
47+
KtAudioTranscriptionConfig(
48+
languageCodes = config.languageCodes().getOrNull(),
49+
customVocabulary = config.customVocabulary().getOrNull(),
50+
diarization = config.diarization().getOrNull(),
51+
wordTimestamp = config.wordTimestamp().getOrNull(),
52+
mode = config.mode().getOrNull()?.let { modeFromJava(it) },
53+
)
54+
55+
/** Returns the fields set on [config] that [fromJava] cannot carry. */
56+
fun unsupportedFields(config: GenaiAudioTranscriptionConfig): List<String> = buildList {
57+
if (config.languageAuto().isPresent) add("languageAuto")
58+
if (config.languageHints().isPresent) add("languageHints")
59+
if (config.adaptationPhrases().isPresent) add("adaptationPhrases")
60+
val mode = config.mode().getOrNull()
61+
if (mode != null && modeFromJava(mode) == null) add("mode")
62+
}
63+
64+
private fun modeFromJava(
65+
mode: GenaiAudioTranscriptionConfigMode
66+
): KtAudioTranscriptionConfigMode? =
67+
enumByNameOrNull<KtAudioTranscriptionConfigMode>(mode.knownEnum().name)
68+
}

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

Lines changed: 128 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,48 +17,69 @@
1717
package com.google.adk.tokt.codecs
1818

1919
import com.google.adk.agents.RunConfig as JavaRunConfig
20+
import com.google.adk.kt.agents.LiveRunConfig as KtLiveRunConfig
2021
import com.google.adk.kt.agents.RunConfig as KtRunConfig
2122
import com.google.adk.kt.agents.StreamingMode as KtStreamingMode
23+
import com.google.adk.kt.annotations.ExperimentalLiveApi
24+
import com.google.adk.kt.types.AudioTranscriptionConfig as KtAudioTranscriptionConfig
25+
import com.google.adk.kt.types.Modality as KtModality
26+
import com.google.genai.types.Modality as GenaiModality
2227

2328
/**
2429
* Converts between the Kotlin `kt.agents.RunConfig` and the ADK Java [JavaRunConfig].
2530
*
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.
31+
* Streaming mode, the LLM call budget and custom metadata cross both ways; [toJava] also shows a
32+
* BIDI run's live settings (modalities, speech, transcriptions), and [fromJavaLive] reads them for
33+
* a live run. [fromJava] and [fromJavaLive] reject a Java setting the engine cannot honor rather
34+
* than dropping it silently.
2935
*/
3036
internal object RunConfigCodec {
3137

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()
38+
/**
39+
* Returns the Java [JavaRunConfig] view of the Kotlin [config]. Only a BIDI run shows live
40+
* settings, since Kotlin turns transcription on for every run.
41+
*/
42+
fun toJava(config: KtRunConfig): JavaRunConfig {
43+
val builder =
44+
JavaRunConfig.builder()
45+
.streamingMode(
46+
when (config.streamingMode) {
47+
KtStreamingMode.NONE -> JavaRunConfig.StreamingMode.NONE
48+
KtStreamingMode.SSE -> JavaRunConfig.StreamingMode.SSE
49+
KtStreamingMode.BIDI -> JavaRunConfig.StreamingMode.BIDI
50+
}
51+
)
52+
.maxLlmCalls(config.maxLlmCalls)
53+
.customMetadata(config.customMetadata.orEmpty())
54+
if (config.streamingMode == KtStreamingMode.BIDI) {
55+
builder
56+
// A live run defaults its response modalities to AUDIO.
57+
.responseModalities(
58+
config.live.responseModalities
59+
.ifEmpty { listOf(KtModality.AUDIO) }
60+
.map { GenaiModality(it.name) }
61+
)
62+
.speechConfig(config.live.speechConfig?.let { SpeechConfigCodec.toJava(it) })
63+
.outputAudioTranscription(
64+
config.live.outputAudioTranscription?.let { AudioTranscriptionConfigCodec.toJava(it) }
65+
)
66+
.inputAudioTranscription(
67+
config.live.inputAudioTranscription?.let { AudioTranscriptionConfigCodec.toJava(it) }
68+
)
69+
}
70+
return builder.build()
71+
}
4372

4473
/** Returns the Kotlin [KtRunConfig] view of the Java [config]. */
45-
@Suppress(
46-
"deprecation"
47-
) // Reads the deprecated groupFunctionResponsesInHistoryOverride to reject it.
4874
fun fromJava(config: JavaRunConfig): KtRunConfig {
4975
val unsupported = buildList {
5076
if (config.streamingMode() == JavaRunConfig.StreamingMode.BIDI) add("streamingMode=BIDI")
5177
if (config.saveInputBlobsAsArtifacts()) add("saveInputBlobsAsArtifacts")
52-
if (config.toolExecutionMode() != JavaRunConfig.ToolExecutionMode.NONE)
53-
add("toolExecutionMode")
78+
addUnsupportedByEveryRun(config)
5479
if (config.responseModalities().isNotEmpty()) add("responseModalities")
5580
if (config.speechConfig() != null) add("speechConfig")
56-
if (config.avatarConfig() != null) add("avatarConfig")
5781
if (config.outputAudioTranscription() != null) add("outputAudioTranscription")
5882
if (config.inputAudioTranscription() != null) add("inputAudioTranscription")
59-
if (config.groupFunctionResponsesInHistoryOverride().isPresent)
60-
add("groupFunctionResponsesInHistoryOverride")
61-
if (config.dollarEscapedPlaceholders()) add("dollarEscapedPlaceholders")
6283
}
6384
require(unsupported.isEmpty()) {
6485
"RunConfig settings not supported by the ADK Kotlin engine: $unsupported"
@@ -71,4 +92,88 @@ internal object RunConfigCodec {
7192
customMetadata = config.customMetadata().takeIf { it.isNotEmpty() },
7293
)
7394
}
95+
96+
/**
97+
* Returns the Kotlin [KtRunConfig] for a live run, applying the ADK Java runner's live defaults:
98+
* no modality means AUDIO, and a missing transcription setting turns transcription on, except
99+
* output transcription when the modalities include text. Rejects a Java setting the engine cannot
100+
* honor, as [fromJava] does, but ignores `saveInputBlobsAsArtifacts`, which ADK Java's own live
101+
* run never reads.
102+
*/
103+
@OptIn(ExperimentalLiveApi::class)
104+
fun fromJavaLive(config: JavaRunConfig): KtRunConfig {
105+
val output = config.outputAudioTranscription()
106+
val input = config.inputAudioTranscription()
107+
val unsupported = buildList {
108+
addUnsupportedByEveryRun(config)
109+
if (output != null) {
110+
addAll(
111+
AudioTranscriptionConfigCodec.unsupportedFields(output).map {
112+
"outputAudioTranscription.$it"
113+
}
114+
)
115+
}
116+
if (input != null) {
117+
addAll(
118+
AudioTranscriptionConfigCodec.unsupportedFields(input).map {
119+
"inputAudioTranscription.$it"
120+
}
121+
)
122+
}
123+
}
124+
require(unsupported.isEmpty()) {
125+
"RunConfig settings not supported by the ADK Kotlin engine: $unsupported"
126+
}
127+
val modalities =
128+
config.responseModalities().map { modality ->
129+
// genai reads any name it does not know as MODALITY_UNSPECIFIED.
130+
requireNotNull(
131+
enumByNameOrNull<KtModality>(modality.knownEnum().name)?.takeIf {
132+
it != KtModality.MODALITY_UNSPECIFIED
133+
}
134+
) {
135+
"Response modality not supported by the ADK Kotlin engine"
136+
}
137+
}
138+
// The ADK Java runner's live defaults (Runner.newInvocationContextForLive).
139+
val responseModalities = modalities.ifEmpty { listOf(KtModality.AUDIO) }
140+
val textRequested = KtModality.TEXT in responseModalities
141+
return KtRunConfig(
142+
// ADK Java's runLive keeps the given mode; BIDI is what shows Java plugins the live settings.
143+
streamingMode = KtStreamingMode.BIDI,
144+
maxLlmCalls = config.maxLlmCalls(),
145+
customMetadata = config.customMetadata().ifEmpty { null },
146+
live =
147+
KtLiveRunConfig(
148+
responseModalities = responseModalities,
149+
speechConfig = config.speechConfig()?.let { SpeechConfigCodec.fromJava(it) },
150+
outputAudioTranscription =
151+
when {
152+
output != null -> AudioTranscriptionConfigCodec.fromJava(output)
153+
textRequested -> null
154+
else -> KtAudioTranscriptionConfig()
155+
},
156+
inputAudioTranscription =
157+
if (input != null) {
158+
AudioTranscriptionConfigCodec.fromJava(input)
159+
} else {
160+
KtAudioTranscriptionConfig()
161+
},
162+
),
163+
)
164+
}
165+
166+
/** Adds the Java settings that neither a turn-based nor a live run can honor. */
167+
// Reads the deprecated groupFunctionResponsesInHistoryOverride to reject it.
168+
@Suppress("deprecation")
169+
private fun MutableList<String>.addUnsupportedByEveryRun(config: JavaRunConfig) {
170+
if (config.toolExecutionMode() != JavaRunConfig.ToolExecutionMode.NONE) {
171+
add("toolExecutionMode")
172+
}
173+
if (config.avatarConfig() != null) add("avatarConfig")
174+
if (config.groupFunctionResponsesInHistoryOverride().isPresent) {
175+
add("groupFunctionResponsesInHistoryOverride")
176+
}
177+
if (config.dollarEscapedPlaceholders()) add("dollarEscapedPlaceholders")
178+
}
74179
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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+
* Converts a live voice setting, with its voices and speakers, between the ADK Kotlin
37+
* [KtSpeechConfig] and the genai [GenaiSpeechConfig] that the ADK Java `RunConfig` carries. Every
38+
* field exists on both sides, so nothing is lost in either direction.
39+
*/
40+
internal object SpeechConfigCodec {
41+
42+
/** Returns the genai [GenaiSpeechConfig] view of the Kotlin [config]. */
43+
fun toJava(config: KtSpeechConfig): GenaiSpeechConfig =
44+
with(config) {
45+
val builder = GenaiSpeechConfig.builder()
46+
voiceConfig?.let { builder.voiceConfig(voiceToJava(it)) }
47+
languageCode?.let { builder.languageCode(it) }
48+
multiSpeakerVoiceConfig?.let { builder.multiSpeakerVoiceConfig(multiSpeakerToJava(it)) }
49+
builder.build()
50+
}
51+
52+
/** Returns the Kotlin [KtSpeechConfig] view of the genai [config]. */
53+
fun fromJava(config: GenaiSpeechConfig): KtSpeechConfig =
54+
KtSpeechConfig(
55+
voiceConfig = config.voiceConfig().getOrNull()?.let { voiceFromJava(it) },
56+
languageCode = config.languageCode().getOrNull(),
57+
multiSpeakerVoiceConfig =
58+
config.multiSpeakerVoiceConfig().getOrNull()?.let { multi ->
59+
KtMultiSpeakerVoiceConfig(
60+
speakerVoiceConfigs =
61+
multi.speakerVoiceConfigs().getOrNull()?.map { speakerFromJava(it) }
62+
)
63+
},
64+
)
65+
66+
private fun voiceToJava(voiceConfig: KtVoiceConfig): GenaiVoiceConfig =
67+
with(voiceConfig) {
68+
val builder = GenaiVoiceConfig.builder()
69+
prebuiltVoiceConfig?.let { builder.prebuiltVoiceConfig(prebuiltToJava(it)) }
70+
replicatedVoiceConfig?.let { builder.replicatedVoiceConfig(replicatedToJava(it)) }
71+
voice?.let { builder.voice(it) }
72+
builder.build()
73+
}
74+
75+
private fun replicatedToJava(replicated: KtReplicatedVoiceConfig): GenaiReplicatedVoiceConfig =
76+
with(replicated) {
77+
val builder = GenaiReplicatedVoiceConfig.builder()
78+
mimeType?.let { builder.mimeType(it) }
79+
voiceSampleAudio?.let { builder.voiceSampleAudio(it) }
80+
consentAudio?.let { builder.consentAudio(it) }
81+
voiceConsentSignature?.let { builder.voiceConsentSignature(consentToJava(it)) }
82+
builder.build()
83+
}
84+
85+
private fun multiSpeakerToJava(multi: KtMultiSpeakerVoiceConfig): GenaiMultiSpeakerVoiceConfig {
86+
val builder = GenaiMultiSpeakerVoiceConfig.builder()
87+
multi.speakerVoiceConfigs?.let { configs ->
88+
builder.speakerVoiceConfigs(configs.map { speakerToJava(it) })
89+
}
90+
return builder.build()
91+
}
92+
93+
private fun prebuiltToJava(prebuilt: KtPrebuiltVoiceConfig): GenaiPrebuiltVoiceConfig {
94+
val builder = GenaiPrebuiltVoiceConfig.builder()
95+
prebuilt.voiceName?.let { builder.voiceName(it) }
96+
return builder.build()
97+
}
98+
99+
private fun consentToJava(consent: KtVoiceConsentSignature): GenaiVoiceConsentSignature {
100+
val builder = GenaiVoiceConsentSignature.builder()
101+
consent.signature?.let { builder.signature(it) }
102+
return builder.build()
103+
}
104+
105+
private fun speakerToJava(speakerConfig: KtSpeakerVoiceConfig): GenaiSpeakerVoiceConfig =
106+
with(speakerConfig) {
107+
val builder = GenaiSpeakerVoiceConfig.builder()
108+
speaker?.let { builder.speaker(it) }
109+
voiceConfig?.let { builder.voiceConfig(voiceToJava(it)) }
110+
builder.build()
111+
}
112+
113+
private fun voiceFromJava(voice: GenaiVoiceConfig): KtVoiceConfig =
114+
KtVoiceConfig(
115+
prebuiltVoiceConfig =
116+
voice.prebuiltVoiceConfig().getOrNull()?.let {
117+
KtPrebuiltVoiceConfig(voiceName = it.voiceName().getOrNull())
118+
},
119+
replicatedVoiceConfig =
120+
voice.replicatedVoiceConfig().getOrNull()?.let { replicatedFromJava(it) },
121+
voice = voice.voice().getOrNull(),
122+
)
123+
124+
private fun replicatedFromJava(replicated: GenaiReplicatedVoiceConfig): KtReplicatedVoiceConfig =
125+
KtReplicatedVoiceConfig(
126+
mimeType = replicated.mimeType().getOrNull(),
127+
voiceSampleAudio = replicated.voiceSampleAudio().getOrNull(),
128+
consentAudio = replicated.consentAudio().getOrNull(),
129+
voiceConsentSignature =
130+
replicated.voiceConsentSignature().getOrNull()?.let {
131+
KtVoiceConsentSignature(signature = it.signature().getOrNull())
132+
},
133+
)
134+
135+
private fun speakerFromJava(speaker: GenaiSpeakerVoiceConfig): KtSpeakerVoiceConfig =
136+
KtSpeakerVoiceConfig(
137+
speaker = speaker.speaker().getOrNull(),
138+
voiceConfig = speaker.voiceConfig().getOrNull()?.let { voiceFromJava(it) },
139+
)
140+
}

0 commit comments

Comments
 (0)