Skip to content

Commit 4f3b02d

Browse files
MiloszSobczykcopybara-github
authored andcommitted
feat: carry code-execution, media resolution and more config across the Java/Kotlin interop
PiperOrigin-RevId: 990117647
1 parent 9fe5830 commit 4f3b02d

4 files changed

Lines changed: 398 additions & 38 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
<kotlin.version>2.1.20</kotlin.version>
7676
<dokka.version>2.2.0</dokka.version>
7777
<kotlinx-coroutines.version>1.11.0</kotlinx-coroutines.version>
78-
<adk-kotlin.version>1.1.0</adk-kotlin.version>
78+
<adk-kotlin.version>1.2.0</adk-kotlin.version>
7979
<reactor-core.version>3.7.0</reactor-core.version>
8080
<wiremock.version>2.35.1</wiremock.version>
8181
<assertj.version>3.27.7</assertj.version>

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
package com.google.adk.tokt.codecs
1818

1919
import com.google.adk.kt.types.FunctionCallingConfig as KtFunctionCallingConfig
20+
import com.google.adk.kt.types.FunctionCallingConfigMode as KtFunctionCallingConfigMode
2021
import com.google.adk.kt.types.GenerateContentConfig as KtConfig
2122
import com.google.adk.kt.types.GenerationConfigRoutingConfig as KtRoutingConfig
2223
import com.google.adk.kt.types.GenerationConfigRoutingConfigAutoRoutingMode as KtAutoRoutingMode
2324
import com.google.adk.kt.types.GenerationConfigRoutingConfigManualRoutingMode as KtManualRoutingMode
2425
import com.google.adk.kt.types.GoogleMaps as KtGoogleMaps
2526
import com.google.adk.kt.types.GoogleSearch as KtGoogleSearch
27+
import com.google.adk.kt.types.HarmBlockMethod as KtHarmBlockMethod
2628
import com.google.adk.kt.types.HarmBlockThreshold as KtHarmBlockThreshold
2729
import com.google.adk.kt.types.HarmCategory as KtHarmCategory
2830
import com.google.adk.kt.types.MediaResolution as KtMediaResolution
@@ -40,12 +42,14 @@ import com.google.adk.kt.types.VertexAISearchDataStoreSpec as KtVertexAISearchDa
4042
import com.google.adk.kt.types.VertexRagStore as KtVertexRagStore
4143
import com.google.adk.kt.types.VertexRagStoreRagResource as KtVertexRagStoreRagResource
4244
import com.google.genai.types.FunctionCallingConfig as GenaiFunctionCallingConfig
45+
import com.google.genai.types.FunctionCallingConfigMode as GenaiFunctionCallingConfigMode
4346
import com.google.genai.types.GenerateContentConfig as GenaiConfig
4447
import com.google.genai.types.GenerationConfigRoutingConfig as GenaiRoutingConfig
4548
import com.google.genai.types.GenerationConfigRoutingConfigAutoRoutingMode as GenaiAutoRoutingMode
4649
import com.google.genai.types.GenerationConfigRoutingConfigManualRoutingMode as GenaiManualRoutingMode
4750
import com.google.genai.types.GoogleMaps as GenaiGoogleMaps
4851
import com.google.genai.types.GoogleSearch as GenaiGoogleSearch
52+
import com.google.genai.types.HarmBlockMethod as GenaiHarmBlockMethod
4953
import com.google.genai.types.HarmBlockThreshold as GenaiHarmBlockThreshold
5054
import com.google.genai.types.HarmCategory as GenaiHarmCategory
5155
import com.google.genai.types.MediaResolution as GenaiMediaResolution
@@ -69,9 +73,9 @@ import kotlin.jvm.optionals.getOrNull
6973
*
7074
* Carries the system instruction, cached content name, tools (function declarations, Google Search,
7175
* Google Maps, and both the Vertex AI Search and Vertex RAG store retrieval kinds), the common
72-
* generation parameters, thinking config, model routing config, tool config, and safety settings.
73-
* genai fields the Kotlin types cannot represent (`FunctionCallingConfig.mode`,
74-
* `SafetySetting.method`) are not mapped.
76+
* generation parameters (including seed and response modalities), thinking config, model routing
77+
* config, tool config (with the function-calling mode), and safety settings (with the block
78+
* method).
7579
*/
7680
internal object GenerateContentConfigCodec {
7781

@@ -89,6 +93,8 @@ internal object GenerateContentConfigCodec {
8993
config.stopSequences?.let { builder.stopSequences(it) }
9094
config.responseMimeType?.let { builder.responseMimeType(it) }
9195
config.responseSchema?.let { builder.responseSchema(SchemaCodec.toJava(it)) }
96+
config.responseModalities?.let { builder.responseModalities(it) }
97+
config.seed?.let { builder.seed(it) }
9298
config.thinkingConfig?.let { builder.thinkingConfig(thinkingConfigToJava(it)) }
9399
config.labels?.let { builder.labels(it) }
94100
config.presencePenalty?.let { builder.presencePenalty(it) }
@@ -124,6 +130,8 @@ internal object GenerateContentConfigCodec {
124130
stopSequences = config.stopSequences().getOrNull(),
125131
responseMimeType = config.responseMimeType().getOrNull(),
126132
responseSchema = config.responseSchema().getOrNull()?.let { SchemaCodec.fromJava(it) },
133+
responseModalities = config.responseModalities().getOrNull(),
134+
seed = config.seed().getOrNull(),
127135
thinkingConfig = config.thinkingConfig().getOrNull()?.let { thinkingConfigFromJava(it) },
128136
labels = config.labels().getOrNull(),
129137
presencePenalty = config.presencePenalty().getOrNull(),
@@ -321,8 +329,8 @@ internal object GenerateContentConfigCodec {
321329
enumByNameOrNull<KtThinkingLevel>(config.thinkingLevel().getOrNull()?.knownEnum()?.name),
322330
)
323331

324-
// The Kotlin FunctionCallingConfig models allowedFunctionNames and streamFunctionCallArguments;
325-
// genai's `mode` has no Kotlin field and is dropped.
332+
// genai ToolConfig.retrievalConfig and includeServerSideToolInvocations have no Kotlin
333+
// counterpart and are dropped.
326334
private fun toolConfigToJava(config: KtToolConfig): GenaiToolConfig {
327335
val builder = GenaiToolConfig.builder()
328336
config.functionCallingConfig?.let {
@@ -342,6 +350,7 @@ internal object GenerateContentConfigCodec {
342350
): GenaiFunctionCallingConfig {
343351
val builder = GenaiFunctionCallingConfig.builder()
344352
config.allowedFunctionNames?.let { builder.allowedFunctionNames(it) }
353+
config.mode?.let { builder.mode(GenaiFunctionCallingConfigMode(it.name)) }
345354
config.streamFunctionCallArguments?.let { builder.streamFunctionCallArguments(it) }
346355
return builder.build()
347356
}
@@ -351,15 +360,22 @@ internal object GenerateContentConfigCodec {
351360
): KtFunctionCallingConfig =
352361
KtFunctionCallingConfig(
353362
allowedFunctionNames = config.allowedFunctionNames().getOrNull(),
363+
// genai falls back to FUNCTION_CALLING_CONFIG_MODE_UNSPECIFIED for an unrecognized mode, a
364+
// name the Kotlin enum lacks; map a present-but-unknown mode to MODE_UNSPECIFIED rather than
365+
// dropping it, while an absent mode stays null.
366+
mode =
367+
config.mode().getOrNull()?.let {
368+
enumByNameOrNull<KtFunctionCallingConfigMode>(it.knownEnum()?.name)
369+
?: KtFunctionCallingConfigMode.MODE_UNSPECIFIED
370+
},
354371
streamFunctionCallArguments = config.streamFunctionCallArguments().getOrNull(),
355372
)
356373

357-
// The Kotlin SafetySetting models category + threshold; genai's `method` has no Kotlin field and
358-
// is dropped (a Kotlin gap, not a bridge one).
359374
private fun safetySettingToJava(setting: KtSafetySetting): GenaiSafetySetting {
360375
val builder = GenaiSafetySetting.builder()
361376
setting.category?.let { builder.category(GenaiHarmCategory(it.name)) }
362377
setting.threshold?.let { builder.threshold(GenaiHarmBlockThreshold(it.name)) }
378+
setting.method?.let { builder.method(GenaiHarmBlockMethod(it.name)) }
363379
return builder.build()
364380
}
365381

@@ -369,5 +385,6 @@ internal object GenerateContentConfigCodec {
369385
enumByNameOrNull<KtHarmCategory>(setting.category().getOrNull()?.knownEnum()?.name),
370386
threshold =
371387
enumByNameOrNull<KtHarmBlockThreshold>(setting.threshold().getOrNull()?.knownEnum()?.name),
388+
method = enumByNameOrNull<KtHarmBlockMethod>(setting.method().getOrNull()?.knownEnum()?.name),
372389
)
373390
}

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

Lines changed: 105 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,61 +18,75 @@ package com.google.adk.tokt.codecs
1818

1919
import com.google.adk.kt.logging.LoggerFactory
2020
import com.google.adk.kt.types.Blob as KtBlob
21+
import com.google.adk.kt.types.CodeExecutionResult as KtCodeExecutionResult
22+
import com.google.adk.kt.types.ExecutableCode as KtExecutableCode
2123
import com.google.adk.kt.types.FileData as KtFileData
2224
import com.google.adk.kt.types.FunctionCall as KtFunctionCall
2325
import com.google.adk.kt.types.FunctionResponse as KtFunctionResponse
26+
import com.google.adk.kt.types.Language as KtLanguage
27+
import com.google.adk.kt.types.Outcome as KtOutcome
2428
import com.google.adk.kt.types.Part as KtPart
29+
import com.google.adk.kt.types.PartMediaResolution as KtPartMediaResolution
30+
import com.google.adk.kt.types.PartMediaResolutionLevel as KtPartMediaResolutionLevel
2531
import com.google.adk.kt.types.PartialArg as KtPartialArg
2632
import com.google.adk.kt.types.PartialArgValue as KtPartialArgValue
2733
import com.google.adk.kt.types.ToolCall as KtToolCall
2834
import com.google.adk.kt.types.ToolResponse as KtToolResponse
2935
import com.google.adk.kt.types.ToolType as KtToolType
3036
import com.google.adk.kt.types.VideoMetadata as KtVideoMetadata
3137
import com.google.genai.types.Blob as GenaiBlob
38+
import com.google.genai.types.CodeExecutionResult as GenaiCodeExecutionResult
39+
import com.google.genai.types.ExecutableCode as GenaiExecutableCode
3240
import com.google.genai.types.FileData as GenaiFileData
3341
import com.google.genai.types.FunctionCall as GenaiFunctionCall
3442
import com.google.genai.types.FunctionResponse as GenaiFunctionResponse
43+
import com.google.genai.types.Language as GenaiLanguage
3544
import com.google.genai.types.NullValue as GenaiNullValue
45+
import com.google.genai.types.Outcome as GenaiOutcome
3646
import com.google.genai.types.Part as GenaiPart
47+
import com.google.genai.types.PartMediaResolution as GenaiPartMediaResolution
48+
import com.google.genai.types.PartMediaResolutionLevel as GenaiPartMediaResolutionLevel
3749
import com.google.genai.types.PartialArg as GenaiPartialArg
3850
import com.google.genai.types.ToolCall as GenaiToolCall
3951
import com.google.genai.types.ToolResponse as GenaiToolResponse
4052
import com.google.genai.types.VideoMetadata as GenaiVideoMetadata
41-
import java.util.concurrent.ConcurrentHashMap
53+
import java.util.concurrent.atomic.AtomicBoolean
4254
import kotlin.jvm.optionals.getOrNull
4355
import kotlin.time.toJavaDuration
4456
import kotlin.time.toKotlinDuration
4557

4658
/**
4759
* Converts a [Part][KtPart] between the genai type ADK Java exposes and the ADK Kotlin type.
4860
* Carries text, inline binary data ([KtBlob]), file references ([KtFileData]), function
49-
* call/response parts, server-side tool call/response parts ([KtToolCall] / [KtToolResponse]), the
50-
* model "thought" marker/signature, video metadata, and part metadata. Shared by [ContentCodec] and
51-
* the artifact service.
61+
* call/response parts, server-side tool call/response parts ([KtToolCall] / [KtToolResponse]),
62+
* code-execution parts ([KtExecutableCode] / [KtCodeExecutionResult]), the model "thought"
63+
* marker/signature, video metadata, media resolution ([KtPartMediaResolution]), and part metadata.
64+
* Shared by [ContentCodec] and the artifact service.
5265
*
53-
* Returns null for an empty part and for one carrying only a field the Kotlin [KtPart] has no
54-
* counterpart for - `executableCode`, `codeExecutionResult` - which [ContentCodec] then drops from
55-
* the content. The loss is forced, so it is logged once per kind rather than silent; see
56-
* [com.google.adk.tokt.JavaAdkToKt].
66+
* Returns null for an empty part and for one carrying only fields the Kotlin [KtPart] has no
67+
* counterpart for (such as `audioTranscription`, `mediaProcessing` or `speechMetadata`), which
68+
* [ContentCodec] then drops from the content. The loss is forced, so it is logged rather than
69+
* silent; see [com.google.adk.tokt.JavaAdkToKt].
5770
*/
5871
internal object PartCodec {
5972

6073
private val logger = LoggerFactory.getLogger(PartCodec::class)
6174

62-
/** Kinds already reported: a code-execution turn emits many parts, and one warning is enough. */
63-
private val warnedUnmappedKinds = ConcurrentHashMap.newKeySet<String>()
75+
/** An empty part, to tell a genuinely empty part from one carrying only an unmapped field. */
76+
private val emptyGenaiPart = GenaiPart.builder().build()
6477

65-
private fun warnUnmappedKind(part: GenaiPart) {
66-
val kind =
67-
when {
68-
part.executableCode().isPresent -> "executableCode"
69-
part.codeExecutionResult().isPresent -> "codeExecutionResult"
70-
else -> return // A genuinely empty part carries no information to lose.
71-
}
72-
if (warnedUnmappedKinds.add(kind)) {
78+
/** One dropped-part warning per interop session is enough to flag the loss without spamming. */
79+
private val unmappedPartWarned = AtomicBoolean(false)
80+
81+
private fun warnUnmappedPart(part: GenaiPart) {
82+
// Detect an unmapped-but-non-empty part by value rather than by naming each accessor, so this
83+
// stays correct as the genai Part gains fields and does not depend on any one field existing.
84+
if (part == emptyGenaiPart) return // A genuinely empty part carries no information to lose.
85+
if (unmappedPartWarned.compareAndSet(false, true)) {
7386
logger.warn {
74-
"Dropping a genai Part that carries only $kind: the ADK Kotlin Part has no equivalent, so" +
75-
" it cannot cross the Java -> Kotlin interop and will be missing from the event stream."
87+
"Dropping a genai Part that carries only fields the ADK Kotlin Part has no equivalent for" +
88+
" (such as audioTranscription, mediaProcessing or speechMetadata): it cannot cross the" +
89+
" Java -> Kotlin interop and will be missing from the event stream."
7690
}
7791
}
7892
}
@@ -101,13 +115,19 @@ internal object PartCodec {
101115
val thoughtSignature = part.thoughtSignature().getOrNull()
102116
val partMetadata = part.partMetadata().getOrNull()
103117
val videoMetadata = part.videoMetadata().getOrNull()?.let { videoMetadataFromJava(it) }
118+
val mediaResolution = part.mediaResolution().getOrNull()?.let { mediaResolutionFromJava(it) }
119+
val executableCode = part.executableCode().getOrNull()
120+
val codeExecutionResult = part.codeExecutionResult().getOrNull()
104121
val base =
105122
when {
106123
functionCall != null -> KtPart(functionCall = functionCallFromJava(functionCall))
107124
functionResponse != null ->
108125
KtPart(functionResponse = functionResponseFromJava(functionResponse))
109126
toolCall != null -> KtPart(toolCall = toolCallFromJava(toolCall))
110127
toolResponse != null -> KtPart(toolResponse = toolResponseFromJava(toolResponse))
128+
executableCode != null -> KtPart(executableCode = executableCodeFromJava(executableCode))
129+
codeExecutionResult != null ->
130+
KtPart(codeExecutionResult = codeExecutionResultFromJava(codeExecutionResult))
111131
inlineData != null -> KtPart(inlineData = blobFromJava(inlineData))
112132
fileData != null -> KtPart(fileData = fileDataFromJava(fileData))
113133
text != null -> KtPart(text = text)
@@ -116,9 +136,10 @@ internal object PartCodec {
116136
thought != null ||
117137
thoughtSignature != null ||
118138
partMetadata != null ||
119-
videoMetadata != null -> KtPart()
139+
videoMetadata != null ||
140+
mediaResolution != null -> KtPart()
120141
else -> {
121-
warnUnmappedKind(part)
142+
warnUnmappedPart(part)
122143
return null
123144
}
124145
}
@@ -127,6 +148,7 @@ internal object PartCodec {
127148
thoughtSignature = thoughtSignature,
128149
partMetadata = partMetadata,
129150
videoMetadata = videoMetadata,
151+
mediaResolution = mediaResolution,
130152
)
131153
}
132154

@@ -139,13 +161,19 @@ internal object PartCodec {
139161
val inlineData = part.inlineData
140162
val fileData = part.fileData
141163
val text = part.text
164+
val executableCode = part.executableCode
165+
val codeExecutionResult = part.codeExecutionResult
142166
val builder =
143167
when {
144168
functionCall != null -> GenaiPart.builder().functionCall(functionCallToJava(functionCall))
145169
functionResponse != null ->
146170
GenaiPart.builder().functionResponse(functionResponseToJava(functionResponse))
147171
toolCall != null -> GenaiPart.builder().toolCall(toolCallToJava(toolCall))
148172
toolResponse != null -> GenaiPart.builder().toolResponse(toolResponseToJava(toolResponse))
173+
executableCode != null ->
174+
GenaiPart.builder().executableCode(executableCodeToJava(executableCode))
175+
codeExecutionResult != null ->
176+
GenaiPart.builder().codeExecutionResult(codeExecutionResultToJava(codeExecutionResult))
149177
inlineData != null -> GenaiPart.builder().inlineData(blobToJava(inlineData))
150178
fileData != null -> GenaiPart.builder().fileData(fileDataToJava(fileData))
151179
text != null -> GenaiPart.builder().text(text)
@@ -154,13 +182,15 @@ internal object PartCodec {
154182
part.thought != null ||
155183
part.thoughtSignature != null ||
156184
part.partMetadata != null ||
157-
part.videoMetadata != null -> GenaiPart.builder()
185+
part.videoMetadata != null ||
186+
part.mediaResolution != null -> GenaiPart.builder()
158187
else -> return null
159188
}
160189
part.thought?.let { builder.thought(it) }
161190
part.thoughtSignature?.let { builder.thoughtSignature(it) }
162191
part.partMetadata?.let { builder.partMetadata(it) }
163192
part.videoMetadata?.let { builder.videoMetadata(videoMetadataToJava(it)) }
193+
part.mediaResolution?.let { builder.mediaResolution(mediaResolutionToJava(it)) }
164194
return builder.build()
165195
}
166196

@@ -179,6 +209,26 @@ internal object PartCodec {
179209
return builder.build()
180210
}
181211

212+
private fun mediaResolutionFromJava(resolution: GenaiPartMediaResolution): KtPartMediaResolution =
213+
KtPartMediaResolution(
214+
// genai maps an unrecognized level to PART_MEDIA_RESOLUTION_LEVEL_UNSPECIFIED, a name the
215+
// Kotlin enum lacks; map a present-but-unknown level to MEDIA_RESOLUTION_UNSPECIFIED rather
216+
// than dropping it, while an absent level stays null.
217+
level =
218+
resolution.level().getOrNull()?.let {
219+
enumByNameOrNull<KtPartMediaResolutionLevel>(it.knownEnum()?.name)
220+
?: KtPartMediaResolutionLevel.MEDIA_RESOLUTION_UNSPECIFIED
221+
},
222+
numTokens = resolution.numTokens().getOrNull(),
223+
)
224+
225+
private fun mediaResolutionToJava(resolution: KtPartMediaResolution): GenaiPartMediaResolution {
226+
val builder = GenaiPartMediaResolution.builder()
227+
resolution.level?.let { builder.level(GenaiPartMediaResolutionLevel(it.name)) }
228+
resolution.numTokens?.let { builder.numTokens(it) }
229+
return builder.build()
230+
}
231+
182232
private fun blobFromJava(blob: GenaiBlob): KtBlob =
183233
KtBlob(
184234
mimeType = blob.mimeType().getOrNull(),
@@ -251,6 +301,8 @@ internal object PartCodec {
251301
return builder.build()
252302
}
253303

304+
// genai FunctionResponse.willContinue/scheduling/parts have no Kotlin counterpart and are
305+
// dropped.
254306
private fun functionResponseFromJava(response: GenaiFunctionResponse): KtFunctionResponse =
255307
KtFunctionResponse(
256308
name = response.name().getOrNull() ?: "",
@@ -264,6 +316,36 @@ internal object PartCodec {
264316
return builder.build()
265317
}
266318

319+
private fun executableCodeFromJava(code: GenaiExecutableCode): KtExecutableCode =
320+
KtExecutableCode(
321+
code = code.code().getOrNull(),
322+
language = enumByNameOrNull<KtLanguage>(code.language().getOrNull()?.knownEnum()?.name),
323+
id = code.id().getOrNull(),
324+
)
325+
326+
private fun executableCodeToJava(code: KtExecutableCode): GenaiExecutableCode {
327+
val builder = GenaiExecutableCode.builder()
328+
code.code?.let { builder.code(it) }
329+
code.language?.let { builder.language(GenaiLanguage(it.name)) }
330+
code.id?.let { builder.id(it) }
331+
return builder.build()
332+
}
333+
334+
private fun codeExecutionResultFromJava(result: GenaiCodeExecutionResult): KtCodeExecutionResult =
335+
KtCodeExecutionResult(
336+
outcome = enumByNameOrNull<KtOutcome>(result.outcome().getOrNull()?.knownEnum()?.name),
337+
output = result.output().getOrNull(),
338+
id = result.id().getOrNull(),
339+
)
340+
341+
private fun codeExecutionResultToJava(result: KtCodeExecutionResult): GenaiCodeExecutionResult {
342+
val builder = GenaiCodeExecutionResult.builder()
343+
result.outcome?.let { builder.outcome(GenaiOutcome(it.name)) }
344+
result.output?.let { builder.output(it) }
345+
result.id?.let { builder.id(it) }
346+
return builder.build()
347+
}
348+
267349
// toolType is a Kotlin value class over the wire string, so it round-trips an unknown type too.
268350
private fun toolCallFromJava(call: GenaiToolCall): KtToolCall =
269351
KtToolCall(

0 commit comments

Comments
 (0)