Skip to content

Commit 0bb7f4a

Browse files
kvmiloscopybara-github
authored andcommitted
fix(tokt): merge a Java plugin's replacement onto the original, as ADK Python does
PiperOrigin-RevId: 1012474029
1 parent 30edd99 commit 0bb7f4a

4 files changed

Lines changed: 552 additions & 4 deletions

File tree

‎tokt/src/main/kotlin/com/google/adk/tokt/adapters/JavaPluginToKt.kt‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import com.google.adk.tokt.codecs.ContentCodec
3232
import com.google.adk.tokt.codecs.EventCodec
3333
import com.google.adk.tokt.codecs.LlmRequestCodec
3434
import com.google.adk.tokt.codecs.LlmResponseCodec
35+
import com.google.adk.tokt.codecs.reapplyJavaEvent
36+
import com.google.adk.tokt.codecs.reapplyJavaResponse
3537
import com.google.adk.tokt.context.KtInvocationContextToJavaView
3638
import com.google.adk.tokt.context.javaAgentView
3739
import com.google.adk.tokt.context.ktCallbackContextToJava
@@ -97,7 +99,7 @@ internal class JavaPluginToKt(
9799
override suspend fun onEvent(invocationContext: KtInvocationContext, event: KtEvent): KtEvent {
98100
val javaContext = KtInvocationContextToJavaView(invocationContext, dispatcher)
99101
val replacement = onDispatcher { plugin.onEventCallback(javaContext, EventCodec.toJava(event)) }
100-
return replacement?.let { EventCodec.fromJava(it) } ?: event
102+
return if (replacement != null) reapplyJavaEvent(event, replacement) else event
101103
}
102104

103105
override suspend fun afterRun(invocationContext: KtInvocationContext) {
@@ -157,7 +159,7 @@ internal class JavaPluginToKt(
157159
plugin.afterModelCallback(javaContext, LlmResponseCodec.toJava(response))
158160
}
159161
reconcileActionsToKt(javaContext.eventActions(), context.eventActions)
160-
return if (override != null) LlmResponseCodec.fromJava(override) else response
162+
return if (override != null) reapplyJavaResponse(response, override) else response
161163
}
162164

163165
override suspend fun onModelError(

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ import kotlin.jvm.optionals.getOrNull
3737
* `usageMetadata` ([UsageMetadataCodec]), `groundingMetadata` ([GroundingMetadataCodec]), context
3838
* `cacheMetadata` ([CacheMetadataCodec]), `avgLogProbs`, `modelVersion`, `customMetadata`, a
3939
* workflow node's `output` and `nodeInfo` ([WorkflowCodec]), long-running tool ids, the timestamp,
40-
* and the `isolationScope`. Every other field is dropped.
40+
* and the `isolationScope`. Every other field is dropped; [reapplyJavaEvent] keeps them when a Java
41+
* plugin returns an event.
4142
*/
4243
internal object EventCodec {
4344

@@ -131,3 +132,31 @@ internal object EventCodec {
131132
)
132133
}
133134
}
135+
136+
/**
137+
* Re-applies the event a Java plugin returned onto the original Kotlin [event]: fields [EventCodec]
138+
* converts come from the result, and every other field keeps [event]'s value. `partial`,
139+
* `turnComplete`, `interrupted` and `usageMetadata` keep [event]'s values when the result leaves
140+
* them unset, and `interactionStatus` stays only while the turn is complete. An `output` the plugin
141+
* left as tokt sent it keeps its Kotlin value, such as a `JsonElement`.
142+
*/
143+
internal fun reapplyJavaEvent(event: KtEvent, javaResult: JavaEvent): KtEvent {
144+
val result = EventCodec.fromJava(javaResult)
145+
// fromJava reads unset flags as false, so check the Java values.
146+
val turnComplete = javaResult.turnComplete().getOrNull() ?: event.turnComplete
147+
return result.copy(
148+
partial = javaResult.partial().getOrNull() ?: event.partial,
149+
turnComplete = turnComplete,
150+
interrupted = javaResult.interrupted().getOrNull() ?: event.interrupted,
151+
usageMetadata = result.usageMetadata ?: event.usageMetadata,
152+
output =
153+
if (javaResult.output().getOrNull() == WorkflowCodec.outputToJava(event.output)) event.output
154+
else result.output,
155+
citationMetadata = event.citationMetadata,
156+
turnCompleteReason = event.turnCompleteReason,
157+
interactionStatus = event.interactionStatus.takeIf { turnComplete },
158+
liveSessionId = event.liveSessionId,
159+
liveSessionResumptionUpdate = event.liveSessionResumptionUpdate,
160+
voiceActivity = event.voiceActivity,
161+
)
162+
}

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import kotlin.jvm.optionals.getOrNull
3030
* Carries content, error message, streaming flags, `turnComplete`, the live transcriptions, model
3131
* version, finish reason, usage metadata, grounding and context cache metadata, average log-probs,
3232
* error code, and custom metadata. Every other field, such as citation metadata and logprobs
33-
* results, is dropped.
33+
* results, is dropped; [reapplyJavaResponse] keeps them when a Java plugin returns a response.
3434
*/
3535
internal object LlmResponseCodec {
3636

@@ -80,3 +80,31 @@ internal object LlmResponseCodec {
8080
return builder.build()
8181
}
8282
}
83+
84+
/**
85+
* Re-applies the response a Java plugin returned onto the original Kotlin [response]: fields
86+
* [LlmResponseCodec] converts come from the result, and every other field keeps [response]'s value.
87+
* `partial`, `turnComplete` and `usageMetadata` keep [response]'s values when the result leaves
88+
* them unset. `interactionStatus` stays only while the turn is complete.
89+
*/
90+
internal fun reapplyJavaResponse(
91+
response: KtLlmResponse,
92+
javaResult: JavaLlmResponse,
93+
): KtLlmResponse {
94+
val result = LlmResponseCodec.fromJava(javaResult)
95+
val turnComplete = result.turnComplete ?: response.turnComplete
96+
return result.copy(
97+
// fromJava reads an unset partial as false, so check the Java value.
98+
partial = javaResult.partial().getOrNull() ?: response.partial,
99+
turnComplete = turnComplete,
100+
usageMetadata = result.usageMetadata ?: response.usageMetadata,
101+
citationMetadata = response.citationMetadata,
102+
logprobsResult = response.logprobsResult,
103+
turnCompleteReason = response.turnCompleteReason,
104+
interactionStatus = response.interactionStatus.takeIf { turnComplete == true },
105+
liveSessionId = response.liveSessionId,
106+
liveSessionResumptionUpdate = response.liveSessionResumptionUpdate,
107+
goAway = response.goAway,
108+
voiceActivity = response.voiceActivity,
109+
)
110+
}

0 commit comments

Comments
 (0)