Skip to content

Commit b9e991b

Browse files
committed
update according to comments
1 parent 38e7e86 commit b9e991b

File tree

3 files changed

+52
-23
lines changed

3 files changed

+52
-23
lines changed

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,6 @@ public class GenerateContentResponse(
4444
candidates.first().content.parts.filterIsInstance<FunctionCallPart>()
4545
}
4646

47-
/**
48-
* Convenience field to list all the [CodeExecutionResultPart]s in the response, if they exist.
49-
*/
50-
public val codeExecutionResults: List<CodeExecutionResultPart> by lazy {
51-
candidates.first().content.parts.filterIsInstance<CodeExecutionResultPart>()
52-
}
53-
54-
/** Convenience field to list all the [ExecutableCodePart]s in the response, if they exist. */
55-
public val executableCodeList: List<ExecutableCodePart> by lazy {
56-
candidates.first().content.parts.filterIsInstance<ExecutableCodePart>()
57-
}
58-
5947
/**
6048
* Convenience field representing all the [InlineDataPart]s in the first candidate, if they exist.
6149
*
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: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,6 @@ public class TextPart(public val text: String) : Part {
3939
@Serializable internal data class Internal(val text: String) : InternalPart
4040
}
4141

42-
/* Represents the result of the code execution */
43-
public enum class Outcome {
44-
OUTCOME_UNSPECIFIED,
45-
OUTCOME_OK,
46-
OUTCOME_FAILED,
47-
OUTCOME_DEADLINE_EXCEEDED
48-
}
49-
5042
/**
5143
* Represents the code execution result from the model.
5244
* @property outcome The result of the execution.
@@ -62,7 +54,7 @@ public class CodeExecutionResultPart(public val outcome: Outcome, public val out
6254

6355
@Serializable
6456
internal data class CodeExecutionResult(
65-
@SerialName("outcome") val outcome: Outcome,
57+
@SerialName("outcome") val outcome: Outcome.Internal,
6658
val output: String
6759
)
6860
}
@@ -261,7 +253,7 @@ internal fun Part.toInternal(): InternalPart {
261253
ExecutableCodePart.Internal(ExecutableCodePart.Internal.ExecutableCode(language, code))
262254
is CodeExecutionResultPart ->
263255
CodeExecutionResultPart.Internal(
264-
CodeExecutionResultPart.Internal.CodeExecutionResult(outcome, output)
256+
CodeExecutionResultPart.Internal.CodeExecutionResult(outcome.toInternal(), output)
265257
)
266258
else ->
267259
throw com.google.firebase.ai.type.SerializationException(
@@ -300,7 +292,7 @@ internal fun InternalPart.toPublic(): Part {
300292
is ExecutableCodePart.Internal ->
301293
ExecutableCodePart(executableCode.language, executableCode.code)
302294
is CodeExecutionResultPart.Internal ->
303-
CodeExecutionResultPart(codeExecutionResult.outcome, codeExecutionResult.output)
295+
CodeExecutionResultPart(codeExecutionResult.outcome.toPublic(), codeExecutionResult.output)
304296
else ->
305297
throw com.google.firebase.ai.type.SerializationException(
306298
"Unsupported part type \"${javaClass.simpleName}\" provided. This model may not be supported by this SDK."

0 commit comments

Comments
 (0)