@@ -182,13 +182,16 @@ final class PromptFeedback {
182
182
/// Metadata on the generation request's token usage.
183
183
final class UsageMetadata {
184
184
// ignore: public_member_api_docs
185
- UsageMetadata ._(
186
- {this .promptTokenCount,
187
- this .candidatesTokenCount,
188
- this .totalTokenCount,
189
- this .thoughtsTokenCount,
190
- this .promptTokensDetails,
191
- this .candidatesTokensDetails});
185
+ UsageMetadata ._({
186
+ this .promptTokenCount,
187
+ this .candidatesTokenCount,
188
+ this .totalTokenCount,
189
+ this .thoughtsTokenCount,
190
+ this .toolUsePromptTokenCount,
191
+ this .promptTokensDetails,
192
+ this .candidatesTokensDetails,
193
+ this .toolUsePromptTokensDetails,
194
+ });
192
195
193
196
/// Number of tokens in the prompt.
194
197
final int ? promptTokenCount;
@@ -202,19 +205,26 @@ final class UsageMetadata {
202
205
/// Number of tokens present in thoughts output.
203
206
final int ? thoughtsTokenCount;
204
207
208
+ /// The number of tokens used by tools.
209
+ final int ? toolUsePromptTokenCount;
210
+
205
211
/// List of modalities that were processed in the request input.
206
212
final List <ModalityTokenCount >? promptTokensDetails;
207
213
208
214
/// List of modalities that were returned in the response.
209
215
final List <ModalityTokenCount >? candidatesTokensDetails;
216
+
217
+ /// A list of tokens used by tools whose usage was triggered from a prompt,
218
+ /// broken down by modality.
219
+ final List <ModalityTokenCount >? toolUsePromptTokensDetails;
210
220
}
211
221
212
222
/// Response candidate generated from a [GenerativeModel] .
213
223
final class Candidate {
214
224
// ignore: public_member_api_docs
215
225
Candidate (this .content, this .safetyRatings, this .citationMetadata,
216
226
this .finishReason, this .finishMessage,
217
- {this .groundingMetadata});
227
+ {this .groundingMetadata, this .urlContextMetadata });
218
228
219
229
/// Generated content returned from the model.
220
230
final Content content;
@@ -242,6 +252,9 @@ final class Candidate {
242
252
/// Metadata returned to the client when grounding is enabled.
243
253
final GroundingMetadata ? groundingMetadata;
244
254
255
+ /// Metadata returned to the client when the [UrlContext] tool is enabled.
256
+ final UrlContextMetadata ? urlContextMetadata;
257
+
245
258
/// The concatenation of the text parts of [content] , if any.
246
259
///
247
260
/// If this candidate was finished for a reason of [FinishReason.recitation]
@@ -417,6 +430,76 @@ final class GroundingMetadata {
417
430
final List <String > webSearchQueries;
418
431
}
419
432
433
+ /// The status of a URL retrieval.
434
+ ///
435
+ /// > Warning: For Firebase AI Logic, URL Context
436
+ /// is in Public Preview, which means that the feature is not subject to any SLA
437
+ /// or deprecation policy and could change in backwards-incompatible ways.
438
+ enum UrlRetrievalStatus {
439
+ /// Unspecified retrieval status.
440
+ unspecified ('URL_RETRIEVAL_STATUS_UNSPECIFIED' ),
441
+
442
+ /// The URL retrieval was successful.
443
+ success ('URL_RETRIEVAL_STATUS_SUCCESS' ),
444
+
445
+ /// The URL retrieval failed due.
446
+ error ('URL_RETRIEVAL_STATUS_ERROR' ),
447
+
448
+ /// The URL retrieval failed because the content is behind a paywall.
449
+ paywall ('URL_RETRIEVAL_STATUS_PAYWALL' ),
450
+
451
+ /// The URL retrieval failed because the content is unsafe.
452
+ unsafe ('URL_RETRIEVAL_STATUS_UNSAFE' );
453
+
454
+ const UrlRetrievalStatus (this ._jsonString);
455
+ final String _jsonString;
456
+
457
+ // ignore: public_member_api_docs
458
+ String toJson () => _jsonString;
459
+
460
+ // ignore: unused_element
461
+ static UrlRetrievalStatus _parseValue (Object jsonObject) {
462
+ return switch (jsonObject) {
463
+ 'URL_RETRIEVAL_STATUS_UNSPECIFIED' => UrlRetrievalStatus .unspecified,
464
+ 'URL_RETRIEVAL_STATUS_SUCCESS' => UrlRetrievalStatus .success,
465
+ 'URL_RETRIEVAL_STATUS_ERROR' => UrlRetrievalStatus .error,
466
+ 'URL_RETRIEVAL_STATUS_PAYWALL' => UrlRetrievalStatus .paywall,
467
+ 'URL_RETRIEVAL_STATUS_UNSAFE' => UrlRetrievalStatus .unsafe,
468
+ _ => UrlRetrievalStatus
469
+ .unspecified, // Default to unspecified for unknown values.
470
+ };
471
+ }
472
+ }
473
+
474
+ /// Metadata for a single URL retrieved by the [UrlContext] tool.
475
+ ///
476
+ /// > Warning: For Firebase AI Logic, URL Context
477
+ /// is in Public Preview, which means that the feature is not subject to any SLA
478
+ /// or deprecation policy and could change in backwards-incompatible ways.
479
+ final class UrlMetadata {
480
+ // ignore: public_member_api_docs
481
+ UrlMetadata ({this .retrievedUrl, required this .urlRetrievalStatus});
482
+
483
+ /// The retrieved URL.
484
+ final Uri ? retrievedUrl;
485
+
486
+ /// The status of the URL retrieval.
487
+ final UrlRetrievalStatus urlRetrievalStatus;
488
+ }
489
+
490
+ /// Metadata related to the [UrlContext] tool.
491
+ ///
492
+ /// > Warning: For Firebase AI Logic, URL Context
493
+ /// is in Public Preview, which means that the feature is not subject to any SLA
494
+ /// or deprecation policy and could change in backwards-incompatible ways.
495
+ final class UrlContextMetadata {
496
+ // ignore: public_member_api_docs
497
+ UrlContextMetadata ({required this .urlMetadata});
498
+
499
+ /// List of [UrlMetadata] used to provide context to the Gemini model.
500
+ final List <UrlMetadata > urlMetadata;
501
+ }
502
+
420
503
/// Safety rating for a piece of content.
421
504
///
422
505
/// The safety rating contains the category of harm and the harm probability
@@ -1280,6 +1363,11 @@ Candidate _parseCandidate(Object? jsonObject) {
1280
1363
{'groundingMetadata' : final Object groundingMetadata} =>
1281
1364
parseGroundingMetadata (groundingMetadata),
1282
1365
_ => null
1366
+ },
1367
+ urlContextMetadata: switch (jsonObject) {
1368
+ {'urlContextMetadata' : final Object urlContextMetadata} =>
1369
+ parseUrlContextMetadata (urlContextMetadata),
1370
+ _ => null
1283
1371
});
1284
1372
}
1285
1373
@@ -1328,6 +1416,11 @@ UsageMetadata parseUsageMetadata(Object jsonObject) {
1328
1416
{'thoughtsTokenCount' : final int thoughtsTokenCount} => thoughtsTokenCount,
1329
1417
_ => null ,
1330
1418
};
1419
+ final toolUsePromptTokenCount = switch (jsonObject) {
1420
+ {'toolUsePromptTokenCount' : final int toolUsePromptTokenCount} =>
1421
+ toolUsePromptTokenCount,
1422
+ _ => null ,
1423
+ };
1331
1424
final promptTokensDetails = switch (jsonObject) {
1332
1425
{'promptTokensDetails' : final List <Object ?> promptTokensDetails} =>
1333
1426
promptTokensDetails.map (_parseModalityTokenCount).toList (),
@@ -1338,13 +1431,23 @@ UsageMetadata parseUsageMetadata(Object jsonObject) {
1338
1431
candidatesTokensDetails.map (_parseModalityTokenCount).toList (),
1339
1432
_ => null ,
1340
1433
};
1434
+ final toolUsePromptTokensDetails = switch (jsonObject) {
1435
+ {
1436
+ 'toolUsePromptTokensDetails' : final List <Object ?>
1437
+ toolUsePromptTokensDetails
1438
+ } =>
1439
+ toolUsePromptTokensDetails.map (_parseModalityTokenCount).toList (),
1440
+ _ => null ,
1441
+ };
1341
1442
return UsageMetadata ._(
1342
1443
promptTokenCount: promptTokenCount,
1343
1444
candidatesTokenCount: candidatesTokenCount,
1344
1445
totalTokenCount: totalTokenCount,
1345
1446
thoughtsTokenCount: thoughtsTokenCount,
1447
+ toolUsePromptTokenCount: toolUsePromptTokenCount,
1346
1448
promptTokensDetails: promptTokensDetails,
1347
1449
candidatesTokensDetails: candidatesTokensDetails,
1450
+ toolUsePromptTokensDetails: toolUsePromptTokensDetails,
1348
1451
);
1349
1452
}
1350
1453
@@ -1523,6 +1626,33 @@ SearchEntryPoint _parseSearchEntryPoint(Object? jsonObject) {
1523
1626
);
1524
1627
}
1525
1628
1629
+ UrlMetadata _parseUrlMetadata (Object ? jsonObject) {
1630
+ if (jsonObject is ! Map ) {
1631
+ throw unhandledFormat ('UrlMetadata' , jsonObject);
1632
+ }
1633
+ final uriString = jsonObject['retrievedUrl' ] as String ? ;
1634
+ return UrlMetadata (
1635
+ retrievedUrl: uriString != null ? Uri .parse (uriString) : null ,
1636
+ urlRetrievalStatus:
1637
+ UrlRetrievalStatus ._parseValue (jsonObject['urlRetrievalStatus' ]),
1638
+ );
1639
+ }
1640
+
1641
+ /// Parses a [UrlContextMetadata] from a JSON object.
1642
+ ///
1643
+ /// This function is used internally to convert URL context metadata from the API
1644
+ /// response.
1645
+ UrlContextMetadata parseUrlContextMetadata (Object ? jsonObject) {
1646
+ if (jsonObject is ! Map ) {
1647
+ throw unhandledFormat ('UrlContextMetadata' , jsonObject);
1648
+ }
1649
+ return UrlContextMetadata (
1650
+ urlMetadata: (jsonObject['urlMetadata' ] as List <Object ?>? ?? [])
1651
+ .map (_parseUrlMetadata)
1652
+ .toList (),
1653
+ );
1654
+ }
1655
+
1526
1656
/// Supported programming languages for the generated code.
1527
1657
enum CodeLanguage {
1528
1658
/// Unspecified status. This value should not be used.
0 commit comments