1717package com.google.adk.tokt.codecs
1818
1919import com.google.adk.agents.RunConfig as JavaRunConfig
20+ import com.google.adk.kt.agents.LiveRunConfig as KtLiveRunConfig
2021import com.google.adk.kt.agents.RunConfig as KtRunConfig
2122import 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 */
3036internal 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}
0 commit comments