Skip to content

Commit 0c1302f

Browse files
authored
fix function calling issue (#7208)
Add support for code execution block but dont document it yet. This is a more cleaner way to address the serialization exception rather than just swallowing the exception.
1 parent 803857d commit 0c1302f

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

firebase-ai/api.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,14 @@ package com.google.firebase.ai.type {
210210
property public final java.util.List<com.google.firebase.ai.type.Citation> citations;
211211
}
212212

213+
public final class CodeExecutionResultPart implements com.google.firebase.ai.type.Part {
214+
ctor public CodeExecutionResultPart(String outcome, String output);
215+
method public String getOutcome();
216+
method public String getOutput();
217+
property public final String outcome;
218+
property public final String output;
219+
}
220+
213221
public final class Content {
214222
ctor public Content(String? role = "user", java.util.List<? extends com.google.firebase.ai.type.Part> parts);
215223
ctor public Content(java.util.List<? extends com.google.firebase.ai.type.Part> parts);
@@ -277,6 +285,14 @@ package com.google.firebase.ai.type {
277285
property public final int width;
278286
}
279287

288+
public final class ExecutableCodePart implements com.google.firebase.ai.type.Part {
289+
ctor public ExecutableCodePart(String language, String code);
290+
method public String getCode();
291+
method public String getLanguage();
292+
property public final String code;
293+
property public final String language;
294+
}
295+
280296
public final class FileDataPart implements com.google.firebase.ai.type.Part {
281297
ctor public FileDataPart(String uri, String mimeType);
282298
method public String getMimeType();

firebase-ai/src/main/kotlin/com/google/firebase/ai/type/LiveServerMessage.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ public class LiveServerToolCall(public val functionCalls: List<FunctionCallPart>
135135
toolCall.functionCalls.map { functionCall ->
136136
FunctionCallPart(
137137
name = functionCall.name,
138-
args = functionCall.args.orEmpty().mapValues { it.value ?: JsonNull }
138+
args = functionCall.args.orEmpty().mapValues { it.value ?: JsonNull },
139+
id = functionCall.id
139140
)
140141
}
141142
)

firebase-ai/src/main/kotlin/com/google/firebase/ai/type/LiveSession.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ internal constructor(
152152
while (true) {
153153
val response = session.incoming.tryReceive()
154154
if (response.isClosed || !startedReceiving.get()) break
155-
156155
response
157156
.getOrNull()
158157
?.let {
@@ -161,7 +160,6 @@ internal constructor(
161160
)
162161
}
163162
?.let { emit(it.toPublic()) }
164-
165163
yield()
166164
}
167165
}

firebase-ai/src/main/kotlin/com/google/firebase/ai/type/Part.kt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,35 @@ public class TextPart(public val text: String) : Part {
3939
@Serializable internal data class Internal(val text: String) : InternalPart
4040
}
4141

42+
public class CodeExecutionResultPart(public val outcome: String, public val output: String) : Part {
43+
44+
@Serializable
45+
internal data class Internal(
46+
@SerialName("codeExecutionResult") val codeExecutionResult: CodeExecutionResult
47+
) : InternalPart {
48+
49+
@Serializable
50+
internal data class CodeExecutionResult(
51+
@SerialName("outcome") val outcome: String,
52+
val output: String
53+
)
54+
}
55+
}
56+
57+
public class ExecutableCodePart(public val language: String, public val code: String) : Part {
58+
59+
@Serializable
60+
internal data class Internal(@SerialName("executableCode") val executableCode: ExecutableCode) :
61+
InternalPart {
62+
63+
@Serializable
64+
internal data class ExecutableCode(
65+
@SerialName("language") val language: String,
66+
val code: String
67+
)
68+
}
69+
}
70+
4271
/**
4372
* Represents image data sent to and received from requests. The image is converted client-side to
4473
* JPEG encoding at 80% quality before being sent to the server.
@@ -176,6 +205,8 @@ internal object PartSerializer :
176205
val jsonObject = element.jsonObject
177206
return when {
178207
"text" in jsonObject -> TextPart.Internal.serializer()
208+
"executableCode" in jsonObject -> ExecutableCodePart.Internal.serializer()
209+
"codeExecutionResult" in jsonObject -> CodeExecutionResultPart.Internal.serializer()
179210
"functionCall" in jsonObject -> FunctionCallPart.Internal.serializer()
180211
"functionResponse" in jsonObject -> FunctionResponsePart.Internal.serializer()
181212
"inlineData" in jsonObject -> InlineDataPart.Internal.serializer()
@@ -207,6 +238,12 @@ internal fun Part.toInternal(): InternalPart {
207238
)
208239
is FileDataPart ->
209240
FileDataPart.Internal(FileDataPart.Internal.FileData(mimeType = mimeType, fileUri = uri))
241+
is ExecutableCodePart ->
242+
ExecutableCodePart.Internal(ExecutableCodePart.Internal.ExecutableCode(language, code))
243+
is CodeExecutionResultPart ->
244+
CodeExecutionResultPart.Internal(
245+
CodeExecutionResultPart.Internal.CodeExecutionResult(outcome, output)
246+
)
210247
else ->
211248
throw com.google.firebase.ai.type.SerializationException(
212249
"The given subclass of Part (${javaClass.simpleName}) is not supported in the serialization yet."
@@ -241,6 +278,10 @@ internal fun InternalPart.toPublic(): Part {
241278
is FunctionResponsePart.Internal ->
242279
FunctionResponsePart(functionResponse.name, functionResponse.response, functionResponse.id)
243280
is FileDataPart.Internal -> FileDataPart(fileData.mimeType, fileData.fileUri)
281+
is ExecutableCodePart.Internal ->
282+
ExecutableCodePart(executableCode.language, executableCode.code)
283+
is CodeExecutionResultPart.Internal ->
284+
CodeExecutionResultPart(codeExecutionResult.outcome, codeExecutionResult.output)
244285
else ->
245286
throw com.google.firebase.ai.type.SerializationException(
246287
"Unsupported part type \"${javaClass.simpleName}\" provided. This model may not be supported by this SDK."

0 commit comments

Comments
 (0)