Skip to content

Commit 88f6774

Browse files
authored
Add support for Code Execution (#7265)
This PR integrates code execution functionality into the Firebase AI Logic SDK. This enhancement enables developers to activate code execution in their requests, providing visibility into the code snippets run by the backend and their corresponding results. This aligns with the functionality described in the official [Gemini API documentation on code execution](https://ai.google.dev/gemini-api/docs/code-execution). Changes Tool Class Enhancement: Implements a new function within the Tool class that allows developers to enable the code execution feature in their requests.
1 parent 5822579 commit 88f6774

File tree

5 files changed

+48
-11
lines changed

5 files changed

+48
-11
lines changed

firebase-ai/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Unreleased
22

3+
- [feature] Added support for code execution.
34
- [feature] Introduced `MissingPermissionsException`, which is thrown when the necessary permissions
45
have not been granted by the user.
56
- [feature] Added helper functions to `LiveSession` to allow developers to track the status of the

firebase-ai/api.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ package com.google.firebase.ai.type {
219219
}
220220

221221
public final class CodeExecutionResultPart implements com.google.firebase.ai.type.Part {
222-
ctor public CodeExecutionResultPart(String outcome, String output);
222+
ctor @Deprecated public CodeExecutionResultPart(String outcome, String output);
223+
method public boolean executionSucceeded();
223224
method public String getOutcome();
224225
method public String getOutput();
225226
method public boolean isThought();
@@ -296,7 +297,7 @@ package com.google.firebase.ai.type {
296297
}
297298

298299
public final class ExecutableCodePart implements com.google.firebase.ai.type.Part {
299-
ctor public ExecutableCodePart(String language, String code);
300+
ctor @Deprecated public ExecutableCodePart(String language, String code);
300301
method public String getCode();
301302
method public String getLanguage();
302303
method public boolean isThought();
@@ -1199,12 +1200,14 @@ package com.google.firebase.ai.type {
11991200
}
12001201

12011202
public final class Tool {
1203+
method public static com.google.firebase.ai.type.Tool codeExecution();
12021204
method public static com.google.firebase.ai.type.Tool functionDeclarations(java.util.List<com.google.firebase.ai.type.FunctionDeclaration> functionDeclarations);
12031205
method public static com.google.firebase.ai.type.Tool googleSearch(com.google.firebase.ai.type.GoogleSearch googleSearch = com.google.firebase.ai.type.GoogleSearch());
12041206
field public static final com.google.firebase.ai.type.Tool.Companion Companion;
12051207
}
12061208

12071209
public static final class Tool.Companion {
1210+
method public com.google.firebase.ai.type.Tool codeExecution();
12081211
method public com.google.firebase.ai.type.Tool functionDeclarations(java.util.List<com.google.firebase.ai.type.FunctionDeclaration> functionDeclarations);
12091212
method public com.google.firebase.ai.type.Tool googleSearch(com.google.firebase.ai.type.GoogleSearch googleSearch = com.google.firebase.ai.type.GoogleSearch());
12101213
}

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ internal constructor(
5454
) : InternalPart
5555
}
5656

57+
/**
58+
* Represents the code execution result from the model.
59+
* @property outcome The result of the execution.
60+
* @property output The stdout from the code execution, or an error message if it failed.
61+
* @property isThought Indicates whether the response is a thought.
62+
*/
5763
public class CodeExecutionResultPart
5864
internal constructor(
5965
public val outcome: String,
@@ -62,23 +68,29 @@ internal constructor(
6268
internal val thoughtSignature: String?
6369
) : Part {
6470

71+
@Deprecated("Part of the model response. Do not instantiate directly.")
6572
public constructor(outcome: String, output: String) : this(outcome, output, false, null)
6673

74+
/** Indicates if the code execution was successful */
75+
public fun executionSucceeded(): Boolean = (outcome.lowercase() == "outcome_ok")
76+
6777
@Serializable
6878
internal data class Internal(
6979
@SerialName("codeExecutionResult") val codeExecutionResult: CodeExecutionResult,
7080
val thought: Boolean? = null,
7181
val thoughtSignature: String? = null
7282
) : InternalPart {
7383

74-
@Serializable
75-
internal data class CodeExecutionResult(
76-
@SerialName("outcome") val outcome: String,
77-
val output: String
78-
)
84+
@Serializable internal data class CodeExecutionResult(val outcome: String, val output: String)
7985
}
8086
}
8187

88+
/**
89+
* Represents the code that was executed by the model.
90+
* @property language The programming language of the code.
91+
* @property code The source code to be executed.
92+
* @property isThought Indicates whether the response is a thought.
93+
*/
8294
public class ExecutableCodePart
8395
internal constructor(
8496
public val language: String,
@@ -87,6 +99,7 @@ internal constructor(
8799
internal val thoughtSignature: String?
88100
) : Part {
89101

102+
@Deprecated("Part of the model response. Do not instantiate directly.")
90103
public constructor(language: String, code: String) : this(language, code, false, null)
91104

92105
@Serializable

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ import kotlinx.serialization.json.JsonObject
2626
public class Tool
2727
internal constructor(
2828
internal val functionDeclarations: List<FunctionDeclaration>?,
29-
internal val googleSearch: GoogleSearch?
29+
internal val googleSearch: GoogleSearch?,
30+
internal val codeExecution: JsonObject?,
3031
) {
3132
internal fun toInternal() =
3233
Internal(
3334
functionDeclarations?.map { it.toInternal() } ?: emptyList(),
34-
googleSearch = this.googleSearch?.toInternal()
35+
googleSearch = this.googleSearch?.toInternal(),
36+
codeExecution = this.codeExecution
3537
)
3638
@Serializable
3739
internal data class Internal(
@@ -42,14 +44,22 @@ internal constructor(
4244
)
4345
public companion object {
4446

47+
private val codeExecutionInstance by lazy { Tool(null, null, JsonObject(emptyMap())) }
48+
4549
/**
4650
* Creates a [Tool] instance that provides the model with access to the [functionDeclarations].
4751
*
4852
* @param functionDeclarations The list of functions that this tool allows the model access to.
4953
*/
5054
@JvmStatic
5155
public fun functionDeclarations(functionDeclarations: List<FunctionDeclaration>): Tool {
52-
return Tool(functionDeclarations, null)
56+
return Tool(functionDeclarations, null, null)
57+
}
58+
59+
/** Creates a [Tool] instance that allows the model to use Code Execution. */
60+
@JvmStatic
61+
public fun codeExecution(): Tool {
62+
return codeExecutionInstance
5363
}
5464

5565
/**
@@ -70,7 +80,7 @@ internal constructor(
7080
*/
7181
@JvmStatic
7282
public fun googleSearch(googleSearch: GoogleSearch = GoogleSearch()): Tool {
73-
return Tool(null, googleSearch)
83+
return Tool(null, googleSearch, null)
7484
}
7585
}
7686
}

firebase-ai/src/test/java/com/google/firebase/ai/type/ToolTest.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ internal class ToolTest {
2727

2828
tool.googleSearch.shouldNotBeNull()
2929
tool.functionDeclarations.shouldBeNull()
30+
tool.codeExecution.shouldBeNull()
3031
}
3132

3233
@Test
@@ -36,5 +37,14 @@ internal class ToolTest {
3637

3738
tool.functionDeclarations?.first() shouldBe functionDeclaration
3839
tool.googleSearch.shouldBeNull()
40+
tool.codeExecution.shouldBeNull()
41+
}
42+
43+
@Test
44+
fun `codeExecution() creates a tool with code execution`() {
45+
val tool = Tool.codeExecution()
46+
tool.codeExecution.shouldNotBeNull()
47+
tool.functionDeclarations.shouldBeNull()
48+
tool.googleSearch.shouldBeNull()
3949
}
4050
}

0 commit comments

Comments
 (0)