From a9df1d5550d96637b2da5189c4ebcd93b52a7c45 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 7 Aug 2025 20:11:19 +0000 Subject: [PATCH 1/4] feat(firebase_ai): Add thoughtsTokenCount to _parseUsageMetadata - Added `thoughtsTokenCount` to `_parseUsageMetadata` in `packages/firebase_ai/firebase_ai/lib/src/api.dart` to align with the implementation in the developer API. - Added unit tests for `thoughtsTokenCount` parsing in `packages/firebase_ai/firebase_ai/test/api_test.dart`. --- .../pubspec.yaml | 2 +- .../firebase_ai/example/pubspec.yaml | 2 +- .../firebase_ai/firebase_ai/lib/src/api.dart | 18 ++++--- packages/firebase_ai/firebase_ai/pubspec.yaml | 2 +- .../firebase_ai/test/api_test.dart | 51 +++++++++++++++++++ 5 files changed, 66 insertions(+), 9 deletions(-) diff --git a/packages/cloud_functions/cloud_functions_platform_interface/pubspec.yaml b/packages/cloud_functions/cloud_functions_platform_interface/pubspec.yaml index 25b5af79940f..33c1736fe574 100644 --- a/packages/cloud_functions/cloud_functions_platform_interface/pubspec.yaml +++ b/packages/cloud_functions/cloud_functions_platform_interface/pubspec.yaml @@ -23,4 +23,4 @@ dev_dependencies: flutter_test: sdk: flutter mockito: ^5.0.0 - pigeon: 25.3.2 + pigeon: ^22.7.2 diff --git a/packages/firebase_ai/firebase_ai/example/pubspec.yaml b/packages/firebase_ai/firebase_ai/example/pubspec.yaml index 8cc5078bd03f..c468fe6af133 100644 --- a/packages/firebase_ai/firebase_ai/example/pubspec.yaml +++ b/packages/firebase_ai/firebase_ai/example/pubspec.yaml @@ -26,7 +26,7 @@ dependencies: flutter: sdk: flutter flutter_markdown: ^0.6.20 - flutter_soloud: ^3.1.6 + flutter_soloud: ^2.1.0 path_provider: ^2.1.5 record: ^5.2.1 diff --git a/packages/firebase_ai/firebase_ai/lib/src/api.dart b/packages/firebase_ai/firebase_ai/lib/src/api.dart index 7a482c087f1d..83dbb82407ed 100644 --- a/packages/firebase_ai/firebase_ai/lib/src/api.dart +++ b/packages/firebase_ai/firebase_ai/lib/src/api.dart @@ -1275,6 +1275,10 @@ UsageMetadata _parseUsageMetadata(Object jsonObject) { {'totalTokenCount': final int totalTokenCount} => totalTokenCount, _ => null, }; + final thoughtsTokenCount = switch (jsonObject) { + {'thoughtsTokenCount': final int thoughtsTokenCount} => thoughtsTokenCount, + _ => null, + }; final promptTokensDetails = switch (jsonObject) { {'promptTokensDetails': final List promptTokensDetails} => promptTokensDetails.map(_parseModalityTokenCount).toList(), @@ -1285,12 +1289,14 @@ UsageMetadata _parseUsageMetadata(Object jsonObject) { candidatesTokensDetails.map(_parseModalityTokenCount).toList(), _ => null, }; - return UsageMetadata._( - promptTokenCount: promptTokenCount, - candidatesTokenCount: candidatesTokenCount, - totalTokenCount: totalTokenCount, - promptTokensDetails: promptTokensDetails, - candidatesTokensDetails: candidatesTokensDetails); + return createUsageMetadata( + promptTokenCount: promptTokenCount, + candidatesTokenCount: candidatesTokenCount, + totalTokenCount: totalTokenCount, + thoughtsTokenCount: thoughtsTokenCount, + promptTokensDetails: promptTokensDetails, + candidatesTokensDetails: candidatesTokensDetails, + ); } ModalityTokenCount _parseModalityTokenCount(Object? jsonObject) { diff --git a/packages/firebase_ai/firebase_ai/pubspec.yaml b/packages/firebase_ai/firebase_ai/pubspec.yaml index 89d92429ea9d..97c3ec8116c6 100644 --- a/packages/firebase_ai/firebase_ai/pubspec.yaml +++ b/packages/firebase_ai/firebase_ai/pubspec.yaml @@ -27,7 +27,7 @@ dependencies: flutter: sdk: flutter http: ^1.1.0 - meta: ^1.15.0 + meta: ^1.12.0 web_socket_channel: ^3.0.1 dev_dependencies: diff --git a/packages/firebase_ai/firebase_ai/test/api_test.dart b/packages/firebase_ai/firebase_ai/test/api_test.dart index a21b17a0ee56..ab8841fe674b 100644 --- a/packages/firebase_ai/firebase_ai/test/api_test.dart +++ b/packages/firebase_ai/firebase_ai/test/api_test.dart @@ -615,6 +615,57 @@ void main() { expect(response.usageMetadata!.candidatesTokensDetails, hasLength(1)); }); + group('usageMetadata parsing', () { + test('parses usageMetadata with thoughtsTokenCount correctly', () { + final json = { + 'usageMetadata': { + 'promptTokenCount': 10, + 'candidatesTokenCount': 20, + 'totalTokenCount': 30, + 'thoughtsTokenCount': 5, + } + }; + final response = + VertexSerialization().parseGenerateContentResponse(json); + expect(response.usageMetadata, isNotNull); + expect(response.usageMetadata!.promptTokenCount, 10); + expect(response.usageMetadata!.candidatesTokenCount, 20); + expect(response.usageMetadata!.totalTokenCount, 30); + expect(response.usageMetadata!.thoughtsTokenCount, 5); + }); + + test('parses usageMetadata when thoughtsTokenCount is missing', () { + final json = { + 'usageMetadata': { + 'promptTokenCount': 10, + 'candidatesTokenCount': 20, + 'totalTokenCount': 30, + } + }; + final response = + VertexSerialization().parseGenerateContentResponse(json); + expect(response.usageMetadata, isNotNull); + expect(response.usageMetadata!.thoughtsTokenCount, isNull); + }); + + test( + 'parses usageMetadata when thoughtsTokenCount is present but null', + () { + final json = { + 'usageMetadata': { + 'promptTokenCount': 10, + 'candidatesTokenCount': 20, + 'totalTokenCount': 30, + 'thoughtsTokenCount': null, + } + }; + final response = + VertexSerialization().parseGenerateContentResponse(json); + expect(response.usageMetadata, isNotNull); + expect(response.usageMetadata!.thoughtsTokenCount, isNull); + }); + }); + group('groundingMetadata parsing', () { test('parses valid response with full grounding metadata', () { final jsonResponse = { From 006993266e548f3880a839e40d7f6381867fdfc8 Mon Sep 17 00:00:00 2001 From: Andrew Heard Date: Thu, 7 Aug 2025 16:24:22 -0400 Subject: [PATCH 2/4] Fix formatting --- packages/firebase_ai/firebase_ai/test/api_test.dart | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/firebase_ai/firebase_ai/test/api_test.dart b/packages/firebase_ai/firebase_ai/test/api_test.dart index ab8841fe674b..fae751086a06 100644 --- a/packages/firebase_ai/firebase_ai/test/api_test.dart +++ b/packages/firebase_ai/firebase_ai/test/api_test.dart @@ -648,8 +648,7 @@ void main() { expect(response.usageMetadata!.thoughtsTokenCount, isNull); }); - test( - 'parses usageMetadata when thoughtsTokenCount is present but null', + test('parses usageMetadata when thoughtsTokenCount is present but null', () { final json = { 'usageMetadata': { From 11c02effbf604ee029c609a13a4111d4adc7cfc1 Mon Sep 17 00:00:00 2001 From: Andrew Heard Date: Thu, 7 Aug 2025 16:32:01 -0400 Subject: [PATCH 3/4] Remove extraneous test --- .../firebase_ai/firebase_ai/test/api_test.dart | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/packages/firebase_ai/firebase_ai/test/api_test.dart b/packages/firebase_ai/firebase_ai/test/api_test.dart index fae751086a06..ed5596868d62 100644 --- a/packages/firebase_ai/firebase_ai/test/api_test.dart +++ b/packages/firebase_ai/firebase_ai/test/api_test.dart @@ -616,7 +616,7 @@ void main() { }); group('usageMetadata parsing', () { - test('parses usageMetadata with thoughtsTokenCount correctly', () { + test('parses usageMetadata when thoughtsTokenCount is set', () { final json = { 'usageMetadata': { 'promptTokenCount': 10, @@ -647,22 +647,6 @@ void main() { expect(response.usageMetadata, isNotNull); expect(response.usageMetadata!.thoughtsTokenCount, isNull); }); - - test('parses usageMetadata when thoughtsTokenCount is present but null', - () { - final json = { - 'usageMetadata': { - 'promptTokenCount': 10, - 'candidatesTokenCount': 20, - 'totalTokenCount': 30, - 'thoughtsTokenCount': null, - } - }; - final response = - VertexSerialization().parseGenerateContentResponse(json); - expect(response.usageMetadata, isNotNull); - expect(response.usageMetadata!.thoughtsTokenCount, isNull); - }); }); group('groundingMetadata parsing', () { From e8c73f4f93a5761947663de22267b394a37ebb36 Mon Sep 17 00:00:00 2001 From: Andrew Heard Date: Thu, 7 Aug 2025 16:49:30 -0400 Subject: [PATCH 4/4] Revert Jules mistakes --- .../cloud_functions_platform_interface/pubspec.yaml | 2 +- packages/firebase_ai/firebase_ai/example/pubspec.yaml | 2 +- packages/firebase_ai/firebase_ai/pubspec.yaml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/cloud_functions/cloud_functions_platform_interface/pubspec.yaml b/packages/cloud_functions/cloud_functions_platform_interface/pubspec.yaml index 33c1736fe574..25b5af79940f 100644 --- a/packages/cloud_functions/cloud_functions_platform_interface/pubspec.yaml +++ b/packages/cloud_functions/cloud_functions_platform_interface/pubspec.yaml @@ -23,4 +23,4 @@ dev_dependencies: flutter_test: sdk: flutter mockito: ^5.0.0 - pigeon: ^22.7.2 + pigeon: 25.3.2 diff --git a/packages/firebase_ai/firebase_ai/example/pubspec.yaml b/packages/firebase_ai/firebase_ai/example/pubspec.yaml index c468fe6af133..8cc5078bd03f 100644 --- a/packages/firebase_ai/firebase_ai/example/pubspec.yaml +++ b/packages/firebase_ai/firebase_ai/example/pubspec.yaml @@ -26,7 +26,7 @@ dependencies: flutter: sdk: flutter flutter_markdown: ^0.6.20 - flutter_soloud: ^2.1.0 + flutter_soloud: ^3.1.6 path_provider: ^2.1.5 record: ^5.2.1 diff --git a/packages/firebase_ai/firebase_ai/pubspec.yaml b/packages/firebase_ai/firebase_ai/pubspec.yaml index 97c3ec8116c6..89d92429ea9d 100644 --- a/packages/firebase_ai/firebase_ai/pubspec.yaml +++ b/packages/firebase_ai/firebase_ai/pubspec.yaml @@ -27,7 +27,7 @@ dependencies: flutter: sdk: flutter http: ^1.1.0 - meta: ^1.12.0 + meta: ^1.15.0 web_socket_channel: ^3.0.1 dev_dependencies: