Skip to content

Commit c628ffe

Browse files
committed
Improve accessors
1 parent 35c6cc2 commit c628ffe

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ public class GenerateContentResponse(
3939
* [thinking](https://firebase.google.com/docs/ai-logic/thinking?api=dev).
4040
*/
4141
public val text: String? by lazy {
42-
candidates.first().nonThoughtParts().filterIsInstance<TextPart>().joinToString(" ") { it.text }
42+
candidates.firstOrNull()?.nonThoughtParts()?.filterIsInstance<TextPart>()?.joinToString(" ") {
43+
it.text
44+
}
4345
}
4446

4547
/**
@@ -49,7 +51,7 @@ public class GenerateContentResponse(
4951
* [thinking](https://firebase.google.com/docs/ai-logic/thinking?api=dev).
5052
*/
5153
public val functionCalls: List<FunctionCallPart> by lazy {
52-
candidates.first().nonThoughtParts().filterIsInstance<FunctionCallPart>()
54+
candidates.firstOrNull()?.nonThoughtParts()?.filterIsInstance<FunctionCallPart>().orEmpty()
5355
}
5456

5557
/**
@@ -59,7 +61,7 @@ public class GenerateContentResponse(
5961
* Learn more about [thinking](https://firebase.google.com/docs/ai-logic/thinking?api=dev).
6062
*/
6163
public val thoughtSummary: String? by lazy {
62-
candidates.first().thoughtParts().filterIsInstance<TextPart>().joinToString(" ")
64+
candidates.firstOrNull()?.thoughtParts()?.filterIsInstance<TextPart>()?.joinToString(" ")
6365
}
6466

6567
/**
@@ -71,10 +73,14 @@ public class GenerateContentResponse(
7173
* [thinking](https://firebase.google.com/docs/ai-logic/thinking?api=dev).
7274
*/
7375
public val inlineDataParts: List<InlineDataPart> by lazy {
74-
candidates.first().nonThoughtParts().let { parts ->
75-
parts.filterIsInstance<ImagePart>().map { it.toInlineDataPart() } +
76-
parts.filterIsInstance<InlineDataPart>()
77-
}
76+
candidates
77+
.firstOrNull()
78+
?.nonThoughtParts()
79+
?.let { parts ->
80+
parts.filterIsInstance<ImagePart>().map { it.toInlineDataPart() } +
81+
parts.filterIsInstance<InlineDataPart>()
82+
}
83+
.orEmpty()
7884
}
7985

8086
private fun Candidate.thoughtParts(): List<Part> = content.parts.filter { it.isThought }

0 commit comments

Comments
 (0)