16
16
17
17
using System ;
18
18
using System . Collections . Generic ;
19
- using System . Collections . ObjectModel ;
20
19
using System . Linq ;
21
20
using Google . MiniJSON ;
22
21
using Firebase . AI . Internal ;
@@ -27,14 +26,14 @@ namespace Firebase.AI {
27
26
/// The model's response to a generate content request.
28
27
/// </summary>
29
28
public readonly struct GenerateContentResponse {
30
- private readonly ReadOnlyCollection < Candidate > _candidates ;
29
+ private readonly IReadOnlyList < Candidate > _candidates ;
31
30
32
31
/// <summary>
33
32
/// A list of candidate response content, ordered from best to worst.
34
33
/// </summary>
35
- public IEnumerable < Candidate > Candidates {
34
+ public IReadOnlyList < Candidate > Candidates {
36
35
get {
37
- return _candidates ?? new ReadOnlyCollection < Candidate > ( new List < Candidate > ( ) ) ;
36
+ return _candidates ?? new List < Candidate > ( ) ;
38
37
}
39
38
}
40
39
@@ -64,16 +63,16 @@ public string Text {
64
63
/// <summary>
65
64
/// Returns function calls found in any `Part`s of the first candidate of the response, if any.
66
65
/// </summary>
67
- public IEnumerable < ModelContent . FunctionCallPart > FunctionCalls {
66
+ public IReadOnlyList < ModelContent . FunctionCallPart > FunctionCalls {
68
67
get {
69
- return Candidates . FirstOrDefault ( ) . Content . Parts . OfType < ModelContent . FunctionCallPart > ( ) ;
68
+ return Candidates . FirstOrDefault ( ) . Content . Parts . OfType < ModelContent . FunctionCallPart > ( ) . ToList ( ) ;
70
69
}
71
70
}
72
71
73
72
// Hidden constructor, users don't need to make this.
74
73
private GenerateContentResponse ( List < Candidate > candidates , PromptFeedback ? promptFeedback ,
75
74
UsageMetadata ? usageMetadata ) {
76
- _candidates = new ReadOnlyCollection < Candidate > ( candidates ?? new List < Candidate > ( ) ) ;
75
+ _candidates = candidates ;
77
76
PromptFeedback = promptFeedback ;
78
77
UsageMetadata = usageMetadata ;
79
78
}
@@ -132,7 +131,7 @@ public enum BlockReason {
132
131
/// A metadata struct containing any feedback the model had on the prompt it was provided.
133
132
/// </summary>
134
133
public readonly struct PromptFeedback {
135
- private readonly ReadOnlyCollection < SafetyRating > _safetyRatings ;
134
+ private readonly IReadOnlyList < SafetyRating > _safetyRatings ;
136
135
137
136
/// <summary>
138
137
/// The reason a prompt was blocked, if it was blocked.
@@ -145,9 +144,9 @@ public readonly struct PromptFeedback {
145
144
/// <summary>
146
145
/// The safety ratings of the prompt.
147
146
/// </summary>
148
- public IEnumerable < SafetyRating > SafetyRatings {
147
+ public IReadOnlyList < SafetyRating > SafetyRatings {
149
148
get {
150
- return _safetyRatings ?? new ReadOnlyCollection < SafetyRating > ( new List < SafetyRating > ( ) ) ;
149
+ return _safetyRatings ?? new List < SafetyRating > ( ) ;
151
150
}
152
151
}
153
152
@@ -156,7 +155,7 @@ private PromptFeedback(BlockReason? blockReason, string blockReasonMessage,
156
155
List < SafetyRating > safetyRatings ) {
157
156
BlockReason = blockReason ;
158
157
BlockReasonMessage = blockReasonMessage ;
159
- _safetyRatings = new ReadOnlyCollection < SafetyRating > ( safetyRatings ?? new List < SafetyRating > ( ) ) ;
158
+ _safetyRatings = safetyRatings ;
160
159
}
161
160
162
161
private static BlockReason ParseBlockReason ( string str ) {
@@ -191,37 +190,37 @@ internal static PromptFeedback FromJson(Dictionary<string, object> jsonDict) {
191
190
/// section within the Service Specific Terms).
192
191
/// </summary>
193
192
public readonly struct GroundingMetadata {
194
- private readonly ReadOnlyCollection < string > _webSearchQueries ;
195
- private readonly ReadOnlyCollection < GroundingChunk > _groundingChunks ;
196
- private readonly ReadOnlyCollection < GroundingSupport > _groundingSupports ;
193
+ private readonly IReadOnlyList < string > _webSearchQueries ;
194
+ private readonly IReadOnlyList < GroundingChunk > _groundingChunks ;
195
+ private readonly IReadOnlyList < GroundingSupport > _groundingSupports ;
197
196
198
197
/// <summary>
199
198
/// A list of web search queries that the model performed to gather the grounding information.
200
199
/// These can be used to allow users to explore the search results themselves.
201
200
/// </summary>
202
- public IEnumerable < string > WebSearchQueries {
201
+ public IReadOnlyList < string > WebSearchQueries {
203
202
get {
204
- return _webSearchQueries ?? new ReadOnlyCollection < string > ( new List < string > ( ) ) ;
203
+ return _webSearchQueries ?? new List < string > ( ) ;
205
204
}
206
205
}
207
206
208
207
/// <summary>
209
208
/// A list of `GroundingChunk` structs. Each chunk represents a piece of retrieved content
210
209
/// (e.g., from a web page) that the model used to ground its response.
211
210
/// </summary>
212
- public IEnumerable < GroundingChunk > GroundingChunks {
211
+ public IReadOnlyList < GroundingChunk > GroundingChunks {
213
212
get {
214
- return _groundingChunks ?? new ReadOnlyCollection < GroundingChunk > ( new List < GroundingChunk > ( ) ) ;
213
+ return _groundingChunks ?? new List < GroundingChunk > ( ) ;
215
214
}
216
215
}
217
216
218
217
/// <summary>
219
218
/// A list of `GroundingSupport` structs. Each object details how specific segments of the
220
219
/// model's response are supported by the `groundingChunks`.
221
220
/// </summary>
222
- public IEnumerable < GroundingSupport > GroundingSupports {
221
+ public IReadOnlyList < GroundingSupport > GroundingSupports {
223
222
get {
224
- return _groundingSupports ?? new ReadOnlyCollection < GroundingSupport > ( new List < GroundingSupport > ( ) ) ;
223
+ return _groundingSupports ?? new List < GroundingSupport > ( ) ;
225
224
}
226
225
}
227
226
@@ -234,9 +233,9 @@ public IEnumerable<GroundingSupport> GroundingSupports {
234
233
235
234
private GroundingMetadata ( List < string > webSearchQueries , List < GroundingChunk > groundingChunks ,
236
235
List < GroundingSupport > groundingSupports , SearchEntryPoint ? searchEntryPoint ) {
237
- _webSearchQueries = new ReadOnlyCollection < string > ( webSearchQueries ?? new List < string > ( ) ) ;
238
- _groundingChunks = new ReadOnlyCollection < GroundingChunk > ( groundingChunks ?? new List < GroundingChunk > ( ) ) ;
239
- _groundingSupports = new ReadOnlyCollection < GroundingSupport > ( groundingSupports ?? new List < GroundingSupport > ( ) ) ;
236
+ _webSearchQueries = webSearchQueries ;
237
+ _groundingChunks = groundingChunks ;
238
+ _groundingSupports = groundingSupports ;
240
239
SearchEntryPoint = searchEntryPoint ;
241
240
}
242
241
@@ -347,7 +346,7 @@ internal static WebGroundingChunk FromJson(Dictionary<string, object> jsonDict)
347
346
/// retrieved grounding chunks.
348
347
/// </summary>
349
348
public readonly struct GroundingSupport {
350
- private readonly ReadOnlyCollection < int > _groundingChunkIndices ;
349
+ private readonly IReadOnlyList < int > _groundingChunkIndices ;
351
350
352
351
/// <summary>
353
352
/// Specifies the segment of the model's response content that this grounding support pertains
@@ -363,15 +362,15 @@ public readonly struct GroundingSupport {
363
362
/// means that `groundingChunks[1]`, `groundingChunks[3]`, `groundingChunks[4]` are the
364
363
/// retrieved content supporting this part of the response.
365
364
/// </summary>
366
- public IEnumerable < int > GroundingChunkIndices {
365
+ public IReadOnlyList < int > GroundingChunkIndices {
367
366
get {
368
- return _groundingChunkIndices ?? new ReadOnlyCollection < int > ( new List < int > ( ) ) ;
367
+ return _groundingChunkIndices ?? new List < int > ( ) ;
369
368
}
370
369
}
371
370
372
371
private GroundingSupport ( Segment segment , List < int > groundingChunkIndices ) {
373
372
Segment = segment ;
374
- _groundingChunkIndices = new ReadOnlyCollection < int > ( groundingChunkIndices ?? new List < int > ( ) ) ;
373
+ _groundingChunkIndices = groundingChunkIndices ;
375
374
}
376
375
377
376
internal static GroundingSupport FromJson ( Dictionary < string , object > jsonDict ) {
@@ -459,17 +458,17 @@ public readonly struct UsageMetadata {
459
458
/// </summary>
460
459
public int TotalTokenCount { get ; }
461
460
462
- private readonly ReadOnlyCollection < ModalityTokenCount > _promptTokensDetails ;
463
- public IEnumerable < ModalityTokenCount > PromptTokensDetails {
461
+ private readonly IReadOnlyList < ModalityTokenCount > _promptTokensDetails ;
462
+ public IReadOnlyList < ModalityTokenCount > PromptTokensDetails {
464
463
get {
465
- return _promptTokensDetails ?? new ReadOnlyCollection < ModalityTokenCount > ( new List < ModalityTokenCount > ( ) ) ;
464
+ return _promptTokensDetails ?? new List < ModalityTokenCount > ( ) ;
466
465
}
467
466
}
468
467
469
- private readonly ReadOnlyCollection < ModalityTokenCount > _candidatesTokensDetails ;
470
- public IEnumerable < ModalityTokenCount > CandidatesTokensDetails {
468
+ private readonly IReadOnlyList < ModalityTokenCount > _candidatesTokensDetails ;
469
+ public IReadOnlyList < ModalityTokenCount > CandidatesTokensDetails {
471
470
get {
472
- return _candidatesTokensDetails ?? new ReadOnlyCollection < ModalityTokenCount > ( new List < ModalityTokenCount > ( ) ) ;
471
+ return _candidatesTokensDetails ?? new List < ModalityTokenCount > ( ) ;
473
472
}
474
473
}
475
474
@@ -480,11 +479,8 @@ private UsageMetadata(int promptTC, int candidatesTC, int thoughtsTC, int totalT
480
479
CandidatesTokenCount = candidatesTC ;
481
480
ThoughtsTokenCount = thoughtsTC ;
482
481
TotalTokenCount = totalTC ;
483
- _promptTokensDetails =
484
- new ReadOnlyCollection < ModalityTokenCount > ( promptDetails ?? new List < ModalityTokenCount > ( ) ) ;
485
- _candidatesTokensDetails =
486
- new ReadOnlyCollection < ModalityTokenCount > ( candidateDetails ?? new List < ModalityTokenCount > ( ) ) ;
487
-
482
+ _promptTokensDetails = promptDetails ;
483
+ _candidatesTokensDetails = candidateDetails ;
488
484
}
489
485
490
486
/// <summary>
0 commit comments