Skip to content

Commit 30edd99

Browse files
kvmiloscopybara-github
authored andcommitted
feat(tokt): carry transcriptions and turnComplete across the interop
PiperOrigin-RevId: 1012448017
1 parent e3a9859 commit 30edd99

4 files changed

Lines changed: 325 additions & 7 deletions

File tree

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ import kotlin.jvm.optionals.getOrNull
3333
* skipSummarization, endOfAgent - the requested tool confirmations, the context-compaction summary,
3434
* the resumable-run `agentState`, the rewind marker `rewindBeforeInvocationId`, and the workflow
3535
* `route`), the streaming/error signals (`partial`, `turnComplete`, `interrupted`, `errorMessage`,
36-
* `errorCode`, `finishReason`), the token `usageMetadata` ([UsageMetadataCodec]),
37-
* `groundingMetadata` ([GroundingMetadataCodec]) and context `cacheMetadata`
38-
* ([CacheMetadataCodec]), a workflow node's `output` and `nodeInfo` ([WorkflowCodec]), long-running
39-
* tool ids, the timestamp, and the `isolationScope`.
36+
* `errorCode`, `finishReason`), the live transcriptions ([TranscriptionCodec]), the token
37+
* `usageMetadata` ([UsageMetadataCodec]), `groundingMetadata` ([GroundingMetadataCodec]), context
38+
* `cacheMetadata` ([CacheMetadataCodec]), `avgLogProbs`, `modelVersion`, `customMetadata`, a
39+
* workflow node's `output` and `nodeInfo` ([WorkflowCodec]), long-running tool ids, the timestamp,
40+
* and the `isolationScope`. Every other field is dropped.
4041
*/
4142
internal object EventCodec {
4243

@@ -57,6 +58,8 @@ internal object EventCodec {
5758
if (event.partial) builder.partial(true)
5859
if (event.turnComplete) builder.turnComplete(true)
5960
if (event.interrupted) builder.interrupted(true)
61+
event.inputTranscription?.let { builder.inputTranscription(TranscriptionCodec.toJava(it)) }
62+
event.outputTranscription?.let { builder.outputTranscription(TranscriptionCodec.toJava(it)) }
6063
event.errorMessage?.let { builder.errorMessage(it) }
6164
event.errorCode?.let { builder.errorCode(GenaiFinishReason(it)) }
6265
event.finishReason?.let { builder.finishReason(it.toGenai()) }
@@ -106,6 +109,10 @@ internal object EventCodec {
106109
partial = event.partial().getOrNull() ?: false,
107110
turnComplete = event.turnComplete().getOrNull() ?: false,
108111
interrupted = event.interrupted().getOrNull() ?: false,
112+
inputTranscription =
113+
event.inputTranscription().getOrNull()?.let { TranscriptionCodec.fromJava(it) },
114+
outputTranscription =
115+
event.outputTranscription().getOrNull()?.let { TranscriptionCodec.fromJava(it) },
109116
errorMessage = event.errorMessage().getOrNull(),
110117
// toString() preserves the raw value; knownEnum().name would collapse an unrecognized code to
111118
// FINISH_REASON_UNSPECIFIED (errorCode is a free-form String on the Kotlin side).

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ import kotlin.jvm.optionals.getOrNull
2727
* ([JavaModelToKt][com.google.adk.tokt.adapters.JavaModelToKt]); Kotlin to Java hands a response to
2828
* a bridged Java plugin's `afterModel`.
2929
*
30-
* Carries content, error message, streaming flags, model version, finish reason, usage metadata,
31-
* grounding and context cache metadata, average log-probs, error code, and custom metadata.
32-
* Citation metadata and logprobs results have no ADK Java facade field, so they are not mapped.
30+
* Carries content, error message, streaming flags, `turnComplete`, the live transcriptions, model
31+
* version, finish reason, usage metadata, grounding and context cache metadata, average log-probs,
32+
* error code, and custom metadata. Every other field, such as citation metadata and logprobs
33+
* results, is dropped.
3334
*/
3435
internal object LlmResponseCodec {
3536

@@ -40,6 +41,11 @@ internal object LlmResponseCodec {
4041
errorMessage = response.errorMessage().getOrNull(),
4142
partial = response.partial().getOrNull() ?: false,
4243
interrupted = response.interrupted().getOrNull() ?: false,
44+
turnComplete = response.turnComplete().getOrNull(),
45+
inputTranscription =
46+
response.inputTranscription().getOrNull()?.let { TranscriptionCodec.fromJava(it) },
47+
outputTranscription =
48+
response.outputTranscription().getOrNull()?.let { TranscriptionCodec.fromJava(it) },
4349
modelVersion = response.modelVersion().getOrNull(),
4450
finishReason = response.finishReason().getOrNull()?.toKt(),
4551
usageMetadata = response.usageMetadata().getOrNull()?.let { UsageMetadataCodec.fromJava(it) },
@@ -59,6 +65,9 @@ internal object LlmResponseCodec {
5965
val builder =
6066
JavaLlmResponse.builder().partial(response.partial).interrupted(response.interrupted)
6167
response.content?.let { builder.content(ContentCodec.toJava(it)) }
68+
response.turnComplete?.let { builder.turnComplete(it) }
69+
response.inputTranscription?.let { builder.inputTranscription(TranscriptionCodec.toJava(it)) }
70+
response.outputTranscription?.let { builder.outputTranscription(TranscriptionCodec.toJava(it)) }
6271
response.errorMessage?.let { builder.errorMessage(it) }
6372
response.modelVersion?.let { builder.modelVersion(it) }
6473
response.finishReason?.let { builder.finishReason(it.toGenai()) }
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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.Transcription as KtTranscription
20+
import com.google.adk.kt.types.WordInfo as KtWordInfo
21+
import com.google.genai.types.Transcription as GenaiTranscription
22+
import com.google.genai.types.WordInfo as GenaiWordInfo
23+
import kotlin.jvm.optionals.getOrNull
24+
25+
/**
26+
* Converts an audio transcription, with its per-word timings, between the ADK Kotlin
27+
* [KtTranscription] and the genai [GenaiTranscription] that ADK Java events and LLM responses
28+
* carry. The genai `startOffset` and `endOffset` have no ADK Kotlin field, so they are dropped.
29+
*/
30+
internal object TranscriptionCodec {
31+
32+
/** Returns the genai [GenaiTranscription] view of the Kotlin [transcription]. */
33+
fun toJava(transcription: KtTranscription): GenaiTranscription =
34+
with(transcription) {
35+
val builder = GenaiTranscription.builder()
36+
text?.let { builder.text(it) }
37+
finished?.let { builder.finished(it) }
38+
languageCode?.let { builder.languageCode(it) }
39+
speakerLabel?.let { builder.speakerLabel(it) }
40+
words?.let { builder.words(it.map { w -> wordToJava(w) }) }
41+
builder.build()
42+
}
43+
44+
/** Returns the Kotlin [KtTranscription] view of the genai [transcription]. */
45+
fun fromJava(transcription: GenaiTranscription): KtTranscription =
46+
KtTranscription(
47+
text = transcription.text().getOrNull(),
48+
finished = transcription.finished().getOrNull(),
49+
languageCode = transcription.languageCode().getOrNull(),
50+
speakerLabel = transcription.speakerLabel().getOrNull(),
51+
words = transcription.words().getOrNull()?.map { wordFromJava(it) },
52+
)
53+
54+
private fun wordToJava(info: KtWordInfo): GenaiWordInfo =
55+
with(info) {
56+
val builder = GenaiWordInfo.builder()
57+
word?.let { builder.word(it) }
58+
startOffset?.let { builder.startOffset(it) }
59+
endOffset?.let { builder.endOffset(it) }
60+
builder.build()
61+
}
62+
63+
private fun wordFromJava(word: GenaiWordInfo): KtWordInfo =
64+
KtWordInfo(
65+
word = word.word().getOrNull(),
66+
startOffset = word.startOffset().getOrNull(),
67+
endOffset = word.endOffset().getOrNull(),
68+
)
69+
}

0 commit comments

Comments
 (0)