Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import com.google.adk.tokt.codecs.ContentCodec
import com.google.adk.tokt.codecs.EventCodec
import com.google.adk.tokt.codecs.LlmRequestCodec
import com.google.adk.tokt.codecs.LlmResponseCodec
import com.google.adk.tokt.codecs.reapplyJavaEvent
import com.google.adk.tokt.codecs.reapplyJavaResponse
import com.google.adk.tokt.context.KtInvocationContextToJavaView
import com.google.adk.tokt.context.javaAgentView
import com.google.adk.tokt.context.ktCallbackContextToJava
Expand Down Expand Up @@ -97,7 +99,7 @@ internal class JavaPluginToKt(
override suspend fun onEvent(invocationContext: KtInvocationContext, event: KtEvent): KtEvent {
val javaContext = KtInvocationContextToJavaView(invocationContext, dispatcher)
val replacement = onDispatcher { plugin.onEventCallback(javaContext, EventCodec.toJava(event)) }
return replacement?.let { EventCodec.fromJava(it) } ?: event
return replacement?.let { reapplyJavaEvent(event, it) } ?: event
}

override suspend fun afterRun(invocationContext: KtInvocationContext) {
Expand Down Expand Up @@ -157,7 +159,7 @@ internal class JavaPluginToKt(
plugin.afterModelCallback(javaContext, LlmResponseCodec.toJava(response))
}
reconcileActionsToKt(javaContext.eventActions(), context.eventActions)
return if (override != null) LlmResponseCodec.fromJava(override) else response
return if (override != null) reapplyJavaResponse(response, override) else response
}

override suspend fun onModelError(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.adk.tokt.codecs

import com.google.adk.kt.types.AudioTranscriptionConfig as KtAudioTranscriptionConfig
import com.google.adk.kt.types.AudioTranscriptionConfigMode as KtAudioTranscriptionConfigMode
import com.google.genai.types.AudioTranscriptionConfig as GenaiAudioTranscriptionConfig
import com.google.genai.types.AudioTranscriptionConfigMode as GenaiAudioTranscriptionConfigMode
import kotlin.jvm.optionals.getOrNull

/**
* Converts a live transcription setting between the ADK Kotlin [KtAudioTranscriptionConfig] and the
* genai [GenaiAudioTranscriptionConfig] that the ADK Java `RunConfig` carries. The genai-only
* fields, and a `mode` Kotlin has no constant for, cannot cross, so [unsupportedFields] names the
* ones a Java caller set.
*/
internal object AudioTranscriptionConfigCodec {

/** Returns the genai [GenaiAudioTranscriptionConfig] view of the Kotlin [config]. */
fun toJava(config: KtAudioTranscriptionConfig): GenaiAudioTranscriptionConfig {
val builder = GenaiAudioTranscriptionConfig.builder()
config.languageCodes?.let { builder.languageCodes(it) }
config.customVocabulary?.let { builder.customVocabulary(it) }
config.diarization?.let { builder.diarization(it) }
config.wordTimestamp?.let { builder.wordTimestamp(it) }
config.mode?.let { builder.mode(GenaiAudioTranscriptionConfigMode(it.name)) }
return builder.build()
}

/** Returns the Kotlin [KtAudioTranscriptionConfig] view of the genai [config]. */
fun fromJava(config: GenaiAudioTranscriptionConfig): KtAudioTranscriptionConfig =
KtAudioTranscriptionConfig(
languageCodes = config.languageCodes().getOrNull(),
customVocabulary = config.customVocabulary().getOrNull(),
diarization = config.diarization().getOrNull(),
wordTimestamp = config.wordTimestamp().getOrNull(),
mode = config.mode().getOrNull()?.let(::modeFromJava),
)

/** The fields set on [config] that [fromJava] cannot carry. */
fun unsupportedFields(config: GenaiAudioTranscriptionConfig): List<String> = buildList {
if (config.languageAuto().isPresent) add("languageAuto")
if (config.languageHints().isPresent) add("languageHints")
if (config.adaptationPhrases().isPresent) add("adaptationPhrases")
config.mode().getOrNull()?.let { if (modeFromJava(it) == null) add("mode") }
}

private fun modeFromJava(
mode: GenaiAudioTranscriptionConfigMode
): KtAudioTranscriptionConfigMode? =
enumByNameOrNull<KtAudioTranscriptionConfigMode>(mode.knownEnum().name)
}
40 changes: 38 additions & 2 deletions tokt/src/main/kotlin/com/google/adk/tokt/codecs/EventCodec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ import kotlin.jvm.optionals.getOrNull
* skipSummarization, endOfAgent - the requested tool confirmations, the context-compaction summary,
* the resumable-run `agentState`, and the rewind marker `rewindBeforeInvocationId`), the
* streaming/error signals (`partial`, `turnComplete`, `interrupted`, `errorMessage`, `errorCode`,
* `finishReason`), the token `usageMetadata` ([UsageMetadataCodec]) and `groundingMetadata`
* ([GroundingMetadataCodec]), long-running tool ids, and the timestamp.
* `finishReason`), the live transcriptions ([TranscriptionCodec]), the token `usageMetadata`
* ([UsageMetadataCodec]), `groundingMetadata` ([GroundingMetadataCodec]), `avgLogProbs`,
* `modelVersion`, `customMetadata`, long-running tool ids, and the timestamp. Every other field is
* dropped; [reapplyJavaEvent] keeps them when a Java plugin returns an event.
*/
internal object EventCodec {

Expand All @@ -54,6 +56,8 @@ internal object EventCodec {
if (event.partial) builder.partial(true)
if (event.turnComplete) builder.turnComplete(true)
if (event.interrupted) builder.interrupted(true)
event.inputTranscription?.let { builder.inputTranscription(TranscriptionCodec.toJava(it)) }
event.outputTranscription?.let { builder.outputTranscription(TranscriptionCodec.toJava(it)) }
event.errorMessage?.let { builder.errorMessage(it) }
event.errorCode?.let { builder.errorCode(GenaiFinishReason(it)) }
event.finishReason?.let { builder.finishReason(it.toGenai()) }
Expand Down Expand Up @@ -98,6 +102,10 @@ internal object EventCodec {
partial = event.partial().getOrNull() ?: false,
turnComplete = event.turnComplete().getOrNull() ?: false,
interrupted = event.interrupted().getOrNull() ?: false,
inputTranscription =
event.inputTranscription().getOrNull()?.let { TranscriptionCodec.fromJava(it) },
outputTranscription =
event.outputTranscription().getOrNull()?.let { TranscriptionCodec.fromJava(it) },
errorMessage = event.errorMessage().getOrNull(),
// toString() preserves the raw value; knownEnum().name would collapse an unrecognized code to
// FINISH_REASON_UNSPECIFIED (errorCode is a free-form String on the Kotlin side).
Expand All @@ -113,3 +121,31 @@ internal object EventCodec {
)
}
}

/**
* Re-applies the event a Java plugin returned onto the original Kotlin [event]: fields [EventCodec]
* converts come from the result, and every other field keeps [event]'s value. `partial`,
* `turnComplete`, `interrupted` and `usageMetadata` keep [event]'s values when the result leaves
* them unset, as ADK Python does. `interactionStatus` stays only while the turn is complete.
*/
internal fun reapplyJavaEvent(event: KtEvent, javaResult: JavaEvent): KtEvent {
val result = EventCodec.fromJava(javaResult)
// fromJava reads unset flags as false, so check the Java values.
val turnComplete = javaResult.turnComplete().getOrNull() ?: event.turnComplete
return result.copy(
partial = javaResult.partial().getOrNull() ?: event.partial,
turnComplete = turnComplete,
interrupted = javaResult.interrupted().getOrNull() ?: event.interrupted,
usageMetadata = result.usageMetadata ?: event.usageMetadata,
actions = result.actions.copy(route = event.actions.route),
citationMetadata = event.citationMetadata,
cacheMetadata = event.cacheMetadata,
output = event.output,
nodeInfo = event.nodeInfo,
turnCompleteReason = event.turnCompleteReason,
interactionStatus = event.interactionStatus.takeIf { turnComplete },
liveSessionId = event.liveSessionId,
liveSessionResumptionUpdate = event.liveSessionResumptionUpdate,
voiceActivity = event.voiceActivity,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ internal object GroundingMetadataCodec {
/** Returns the Kotlin [KtGroundingMetadata] view of the genai [metadata]. */
fun fromJava(metadata: GenaiGroundingMetadata): KtGroundingMetadata =
KtGroundingMetadata(
imageSearchQueries = metadata.imageSearchQueries().getOrNull().orEmpty(),
imageSearchQueries = metadata.imageSearchQueries().getOrNull(),
groundingChunks = metadata.groundingChunks().getOrNull()?.map { chunkFromJava(it) },
groundingSupports = metadata.groundingSupports().getOrNull()?.map { supportFromJava(it) },
webSearchQueries = metadata.webSearchQueries().getOrNull(),
Expand All @@ -62,7 +62,8 @@ internal object GroundingMetadataCodec {

/** Returns the genai [GenaiGroundingMetadata] view of the Kotlin [metadata]. */
fun toJava(metadata: KtGroundingMetadata): GenaiGroundingMetadata {
val builder = GenaiGroundingMetadata.builder().imageSearchQueries(metadata.imageSearchQueries)
val builder = GenaiGroundingMetadata.builder()
metadata.imageSearchQueries?.let { builder.imageSearchQueries(it) }
metadata.groundingChunks?.let { builder.groundingChunks(it.map { c -> chunkToJava(c) }) }
metadata.groundingSupports?.let { builder.groundingSupports(it.map { s -> supportToJava(s) }) }
metadata.webSearchQueries?.let { builder.webSearchQueries(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ import kotlin.jvm.optionals.getOrNull
* ([JavaModelToKt][com.google.adk.tokt.adapters.JavaModelToKt]); Kotlin to Java hands a response to
* a bridged Java plugin's `afterModel`.
*
* Carries content, error message, streaming flags, model version, finish reason, usage metadata,
* grounding metadata, average log-probs, error code, and custom metadata. Citation metadata and
* logprobs results have no ADK Java facade field, so they are not mapped.
* Carries content, error message, streaming flags, `turnComplete`, the live transcriptions, model
* version, finish reason, usage metadata, grounding metadata, average log-probs, error code, and
* custom metadata. Every other field, such as citation metadata and logprobs results, is dropped;
* [reapplyJavaResponse] keeps them when a Java plugin returns a response.
*/
internal object LlmResponseCodec {

Expand All @@ -40,6 +41,11 @@ internal object LlmResponseCodec {
errorMessage = response.errorMessage().getOrNull(),
partial = response.partial().getOrNull() ?: false,
interrupted = response.interrupted().getOrNull() ?: false,
turnComplete = response.turnComplete().getOrNull(),
inputTranscription =
response.inputTranscription().getOrNull()?.let { TranscriptionCodec.fromJava(it) },
outputTranscription =
response.outputTranscription().getOrNull()?.let { TranscriptionCodec.fromJava(it) },
modelVersion = response.modelVersion().getOrNull(),
finishReason = response.finishReason().getOrNull()?.toKt(),
usageMetadata = response.usageMetadata().getOrNull()?.let { UsageMetadataCodec.fromJava(it) },
Expand All @@ -58,6 +64,9 @@ internal object LlmResponseCodec {
val builder =
JavaLlmResponse.builder().partial(response.partial).interrupted(response.interrupted)
response.content?.let { builder.content(ContentCodec.toJava(it)) }
response.turnComplete?.let { builder.turnComplete(it) }
response.inputTranscription?.let { builder.inputTranscription(TranscriptionCodec.toJava(it)) }
response.outputTranscription?.let { builder.outputTranscription(TranscriptionCodec.toJava(it)) }
response.errorMessage?.let { builder.errorMessage(it) }
response.modelVersion?.let { builder.modelVersion(it) }
response.finishReason?.let { builder.finishReason(it.toGenai()) }
Expand All @@ -69,3 +78,32 @@ internal object LlmResponseCodec {
return builder.build()
}
}

/**
* Re-applies the response a Java plugin returned onto the original Kotlin [response]: fields
* [LlmResponseCodec] converts come from the result, and every other field keeps [response]'s value.
* `partial`, `turnComplete` and `usageMetadata` keep [response]'s values when the result leaves
* them unset, as ADK Python does. `interactionStatus` stays only while the turn is complete.
*/
internal fun reapplyJavaResponse(
response: KtLlmResponse,
javaResult: JavaLlmResponse,
): KtLlmResponse {
val result = LlmResponseCodec.fromJava(javaResult)
val turnComplete = result.turnComplete ?: response.turnComplete
return result.copy(
// fromJava reads an unset partial as false, so check the Java value.
partial = javaResult.partial().getOrNull() ?: response.partial,
turnComplete = turnComplete,
usageMetadata = result.usageMetadata ?: response.usageMetadata,
citationMetadata = response.citationMetadata,
logprobsResult = response.logprobsResult,
cacheMetadata = response.cacheMetadata,
turnCompleteReason = response.turnCompleteReason,
interactionStatus = response.interactionStatus.takeIf { turnComplete == true },
liveSessionId = response.liveSessionId,
liveSessionResumptionUpdate = response.liveSessionResumptionUpdate,
goAway = response.goAway,
voiceActivity = response.voiceActivity,
)
}
Loading
Loading