Skip to content

Commit 6590dba

Browse files
authored
Merge 82ba5a4 into be8f480
2 parents be8f480 + 82ba5a4 commit 6590dba

File tree

5 files changed

+94
-9
lines changed

5 files changed

+94
-9
lines changed

firebase-ai/api.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,13 +382,17 @@ package com.google.firebase.ai.type {
382382
public final class GenerateContentResponse {
383383
ctor public GenerateContentResponse(java.util.List<com.google.firebase.ai.type.Candidate> candidates, com.google.firebase.ai.type.PromptFeedback? promptFeedback, com.google.firebase.ai.type.UsageMetadata? usageMetadata);
384384
method public java.util.List<com.google.firebase.ai.type.Candidate> getCandidates();
385+
method public java.util.List<com.google.firebase.ai.type.CodeExecutionResultPart> getCodeExecutionResults();
386+
method public java.util.List<com.google.firebase.ai.type.ExecutableCodePart> getExecutableCodeList();
385387
method public java.util.List<com.google.firebase.ai.type.FunctionCallPart> getFunctionCalls();
386388
method public java.util.List<com.google.firebase.ai.type.InlineDataPart> getInlineDataParts();
387389
method public com.google.firebase.ai.type.PromptFeedback? getPromptFeedback();
388390
method public String? getText();
389391
method public String? getThoughtSummary();
390392
method public com.google.firebase.ai.type.UsageMetadata? getUsageMetadata();
391393
property public final java.util.List<com.google.firebase.ai.type.Candidate> candidates;
394+
property public final java.util.List<com.google.firebase.ai.type.CodeExecutionResultPart> codeExecutionResults;
395+
property public final java.util.List<com.google.firebase.ai.type.ExecutableCodePart> executableCodeList;
392396
property public final java.util.List<com.google.firebase.ai.type.FunctionCallPart> functionCalls;
393397
property public final java.util.List<com.google.firebase.ai.type.InlineDataPart> inlineDataParts;
394398
property public final com.google.firebase.ai.type.PromptFeedback? promptFeedback;
@@ -1184,12 +1188,14 @@ package com.google.firebase.ai.type {
11841188
}
11851189

11861190
public final class Tool {
1191+
method public static com.google.firebase.ai.type.Tool codeExecution();
11871192
method public static com.google.firebase.ai.type.Tool functionDeclarations(java.util.List<com.google.firebase.ai.type.FunctionDeclaration> functionDeclarations);
11881193
method public static com.google.firebase.ai.type.Tool googleSearch(com.google.firebase.ai.type.GoogleSearch googleSearch = com.google.firebase.ai.type.GoogleSearch());
11891194
field public static final com.google.firebase.ai.type.Tool.Companion Companion;
11901195
}
11911196

11921197
public static final class Tool.Companion {
1198+
method public com.google.firebase.ai.type.Tool codeExecution();
11931199
method public com.google.firebase.ai.type.Tool functionDeclarations(java.util.List<com.google.firebase.ai.type.FunctionDeclaration> functionDeclarations);
11941200
method public com.google.firebase.ai.type.Tool googleSearch(com.google.firebase.ai.type.GoogleSearch googleSearch = com.google.firebase.ai.type.GoogleSearch());
11951201
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.google.firebase.ai.type
2+
3+
import com.google.firebase.ai.common.util.FirstOrdinalSerializer
4+
import kotlinx.serialization.KSerializer
5+
import kotlinx.serialization.Serializable
6+
7+
/** Represents the result of the code execution */
8+
public class Outcome private constructor(public val ordinal: Int) {
9+
10+
@Serializable(Internal.Serializer::class)
11+
internal enum class Internal {
12+
OUTCOME_UNSPECIFIED,
13+
OUTCOME_OK,
14+
OUTCOME_FAILED,
15+
OUTCOME_DEADLINE_EXCEEDED;
16+
17+
internal object Serializer : KSerializer<Internal> by FirstOrdinalSerializer(Internal::class)
18+
19+
internal fun toPublic() =
20+
when (this) {
21+
OUTCOME_UNSPECIFIED -> Outcome.OUTCOME_UNSPECIFIED
22+
OUTCOME_OK -> Outcome.OUTCOME_OK
23+
OUTCOME_FAILED -> Outcome.OUTCOME_FAILED
24+
OUTCOME_DEADLINE_EXCEEDED -> Outcome.OUTCOME_DEADLINE_EXCEEDED
25+
}
26+
}
27+
28+
internal fun toInternal() =
29+
when (this) {
30+
OUTCOME_UNSPECIFIED -> Internal.OUTCOME_UNSPECIFIED
31+
OUTCOME_OK -> Internal.OUTCOME_OK
32+
OUTCOME_FAILED -> Internal.OUTCOME_FAILED
33+
else -> Internal.OUTCOME_DEADLINE_EXCEEDED
34+
}
35+
public companion object {
36+
37+
/** Represents that the code execution outcome is unspecified */
38+
@JvmField public val OUTCOME_UNSPECIFIED: Outcome = Outcome(0)
39+
40+
/** Represents that the code execution succeeded */
41+
@JvmField public val OUTCOME_OK: Outcome = Outcome(1)
42+
43+
/** Represents that the code execution failed */
44+
@JvmField public val OUTCOME_FAILED: Outcome = Outcome(2)
45+
46+
/** Represents that the code execution timed out */
47+
@JvmField public val OUTCOME_DEADLINE_EXCEEDED: Outcome = Outcome(3)
48+
}
49+
}

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,21 @@ internal constructor(
5353
) : InternalPart
5454
}
5555

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

64-
public constructor(outcome: String, output: String) : this(outcome, output, false, null)
70+
public constructor(outcome: Outcome, output: String) : this(outcome, output, false, null)
6571

6672
@Serializable
6773
internal data class Internal(
@@ -72,12 +78,18 @@ internal constructor(
7278

7379
@Serializable
7480
internal data class CodeExecutionResult(
75-
@SerialName("outcome") val outcome: String,
81+
@SerialName("outcome") val outcome: Outcome.Internal,
7682
val output: String
7783
)
7884
}
7985
}
8086

87+
/**
88+
* Represents the code that is executed by the model.
89+
* @property language The programming language of the code.
90+
* @property code The source code to be executed.
91+
* @property isThought Indicates whether the response is a thought.
92+
*/
8193
public class ExecutableCodePart
8294
internal constructor(
8395
public val language: String,
@@ -354,7 +366,7 @@ internal fun Part.toInternal(): InternalPart {
354366
)
355367
is CodeExecutionResultPart ->
356368
CodeExecutionResultPart.Internal(
357-
CodeExecutionResultPart.Internal.CodeExecutionResult(outcome, output),
369+
CodeExecutionResultPart.Internal.CodeExecutionResult(outcome.toInternal(), output),
358370
isThought,
359371
thoughtSignature
360372
)
@@ -410,7 +422,7 @@ internal fun InternalPart.toPublic(): Part {
410422
)
411423
is CodeExecutionResultPart.Internal ->
412424
CodeExecutionResultPart(
413-
codeExecutionResult.outcome,
425+
codeExecutionResult.outcome.toPublic(),
414426
codeExecutionResult.output,
415427
thought ?: false,
416428
thoughtSignature

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

Lines changed: 12 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(
@@ -49,7 +51,13 @@ internal constructor(
4951
*/
5052
@JvmStatic
5153
public fun functionDeclarations(functionDeclarations: List<FunctionDeclaration>): Tool {
52-
return Tool(functionDeclarations, null)
54+
return Tool(functionDeclarations, null, null)
55+
}
56+
57+
/** Creates a [Tool] instance that allows the model to use Code Execution. */
58+
@JvmStatic
59+
public fun codeExecution(): Tool {
60+
return Tool(null, null, JsonObject(emptyMap()))
5361
}
5462

5563
/**
@@ -70,7 +78,7 @@ internal constructor(
7078
*/
7179
@JvmStatic
7280
public fun googleSearch(googleSearch: GoogleSearch = GoogleSearch()): Tool {
73-
return Tool(null, googleSearch)
81+
return Tool(null, googleSearch, null)
7482
}
7583
}
7684
}

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)