From e0b3ffee44af2490a4437c4d6bccad60dfa6b601 Mon Sep 17 00:00:00 2001 From: Jeremy Schoemaker Date: Sun, 23 Aug 2026 12:30:20 -0500 Subject: [PATCH 1/2] fix: invoke FirebaseProvider's onFunctionCalls callback The onFunctionCalls constructor parameter added in #99 was accepted but never stored or invoked after GeminiProvider and VertexProvider were consolidated into FirebaseProvider. Store the callback and invoke it once per turn with the function calls collected from that turn, and add a regression test that drives a stubbed streaming response through the provider. --- .../implementations/firebase_provider.dart | 10 +- pubspec.yaml | 2 + test/firebase_provider_test.dart | 162 ++++++++++++++++++ 3 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 test/firebase_provider_test.dart diff --git a/lib/src/providers/implementations/firebase_provider.dart b/lib/src/providers/implementations/firebase_provider.dart index 0449f03..b8150b7 100644 --- a/lib/src/providers/implementations/firebase_provider.dart +++ b/lib/src/providers/implementations/firebase_provider.dart @@ -27,6 +27,9 @@ class FirebaseProvider extends LlmProvider with ChangeNotifier { /// /// [_chatGenerationConfig] is an optional configuration for controlling the /// model's generation behavior. + /// + /// [onFunctionCalls] is an optional callback invoked with the function calls + /// collected from a single turn of the model's response. FirebaseProvider({ required this._model, void Function(Iterable)? onFunctionCalls, @@ -34,11 +37,13 @@ class FirebaseProvider extends LlmProvider with ChangeNotifier { this._chatSafetySettings, this._chatGenerationConfig, this._onFunctionCall, - }) : _history = history?.toList() ?? [] { + }) : _onFunctionCalls = onFunctionCalls, + _history = history?.toList() ?? [] { _chat = _startChat(history); } final GenerativeModel _model; + final void Function(Iterable)? _onFunctionCalls; final List? _chatSafetySettings; final GenerationConfig? _chatGenerationConfig; final List _history; @@ -119,6 +124,9 @@ class FirebaseProvider extends LlmProvider with ChangeNotifier { break; } + // Notify the caller of the function calls collected in this turn + _onFunctionCalls?.call(functionCalls); + // Add newline between responses yield '\n'; diff --git a/pubspec.yaml b/pubspec.yaml index bf61461..3854fbc 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -34,6 +34,8 @@ dependencies: waveform_recorder: ^1.8.0 dev_dependencies: + firebase_core: ^4.13.0 + firebase_core_platform_interface: ^8.1.0 flutter_lints: ^6.0.0 flutter_test: sdk: flutter diff --git a/test/firebase_provider_test.dart b/test/firebase_provider_test.dart new file mode 100644 index 0000000..7b40fd6 --- /dev/null +++ b/test/firebase_provider_test.dart @@ -0,0 +1,162 @@ +// Copyright 2024 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// ignore_for_file: implementation_imports + +import 'package:firebase_ai/firebase_ai.dart'; +import 'package:firebase_ai/src/base_model.dart'; +import 'package:firebase_ai/src/client.dart'; +import 'package:firebase_core/firebase_core.dart'; +import 'package:firebase_core_platform_interface/test.dart'; +import 'package:flutter_ai_toolkit/flutter_ai_toolkit.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + TestFirebaseCoreHostApi.setUp(_MockFirebaseApp()); + + late FirebaseApp app; + setUpAll(() async { + app = await Firebase.initializeApp(); + }); + + group('FirebaseProvider.onFunctionCalls', () { + test( + 'invokes onFunctionCalls with the calls collected in a turn', + () async { + final client = + _StubApiClient() + ..streamResponses.addAll([ + [_functionCallResponse], + [_textResponse], + ]); + + final recordedCalls = []; + final provider = FirebaseProvider( + model: _createModel(app, client), + onFunctionCalls: recordedCalls.addAll, + onFunctionCall: (functionCall) async => {'temperature': 60}, + ); + + await provider + .sendMessageStream('What is the temperature?') + .drain(); + + expect(recordedCalls, hasLength(1)); + expect(recordedCalls.single.name, 'getTemperature'); + expect(recordedCalls.single.args, {'city': 'Portland'}); + }, + ); + + test( + 'does not invoke onFunctionCalls when no function calls are made', + () async { + final client = _StubApiClient()..streamResponses.add([_textResponse]); + + final recordedCalls = []; + final provider = FirebaseProvider( + model: _createModel(app, client), + onFunctionCalls: recordedCalls.addAll, + ); + + await provider.sendMessageStream('Just say something').drain(); + + expect(recordedCalls, isEmpty); + }, + ); + }); +} + +GenerativeModel _createModel(FirebaseApp app, ApiClient client) => + createModelWithClient( + app: app, + location: 'us-central1', + model: 'some-model', + client: client, + useAgentPlatform: false, + ); + +const _functionCallResponse = { + 'candidates': [ + { + 'content': { + 'role': 'model', + 'parts': [ + { + 'functionCall': { + 'name': 'getTemperature', + 'args': {'city': 'Portland'}, + }, + }, + ], + }, + 'finishReason': 'STOP', + }, + ], +}; + +const _textResponse = { + 'candidates': [ + { + 'content': { + 'role': 'model', + 'parts': [ + {'text': 'The temperature is 60F.'}, + ], + }, + 'finishReason': 'STOP', + }, + ], +}; + +final class _StubApiClient implements ApiClient { + final streamResponses = >>[]; + + @override + Future> makeRequest( + Uri uri, + Map body, + ) => throw UnimplementedError(); + + @override + Stream> streamRequest( + Uri uri, + Map body, + ) => Stream.fromIterable(streamResponses.removeAt(0)); +} + +class _MockFirebaseApp implements TestFirebaseCoreHostApi { + @override + Future initializeApp( + String appName, + CoreFirebaseOptions initializeAppRequest, + ) async => CoreInitializeResponse( + name: appName, + options: initializeAppRequest, + pluginConstants: {}, + ); + + @override + Future> initializeCore() async => [ + CoreInitializeResponse( + name: defaultFirebaseAppName, + options: CoreFirebaseOptions( + apiKey: '123', + projectId: '123', + appId: '123', + messagingSenderId: '123', + ), + pluginConstants: {}, + ), + ]; + + @override + Future optionsFromResource() async => + CoreFirebaseOptions( + apiKey: '123', + projectId: '123', + appId: '123', + messagingSenderId: '123', + ); +} From 2fdb66c7c0eb02419bebc7b478349405d876a99e Mon Sep 17 00:00:00 2001 From: Jeremy Schoemaker Date: Tue, 1 Sep 2026 16:13:41 -0500 Subject: [PATCH 2/2] Rename onFunctionCalls to willCallFunctions --- .../implementations/firebase_provider.dart | 13 +++++++------ test/firebase_provider_test.dart | 10 +++++----- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/src/providers/implementations/firebase_provider.dart b/lib/src/providers/implementations/firebase_provider.dart index b8150b7..7c9a771 100644 --- a/lib/src/providers/implementations/firebase_provider.dart +++ b/lib/src/providers/implementations/firebase_provider.dart @@ -28,22 +28,23 @@ class FirebaseProvider extends LlmProvider with ChangeNotifier { /// [_chatGenerationConfig] is an optional configuration for controlling the /// model's generation behavior. /// - /// [onFunctionCalls] is an optional callback invoked with the function calls - /// collected from a single turn of the model's response. + /// [willCallFunctions] is an optional callback invoked with the function + /// calls collected from a single turn of the model's response, before those + /// calls are executed. FirebaseProvider({ required this._model, - void Function(Iterable)? onFunctionCalls, + void Function(Iterable)? willCallFunctions, Iterable? history, this._chatSafetySettings, this._chatGenerationConfig, this._onFunctionCall, - }) : _onFunctionCalls = onFunctionCalls, + }) : _willCallFunctions = willCallFunctions, _history = history?.toList() ?? [] { _chat = _startChat(history); } final GenerativeModel _model; - final void Function(Iterable)? _onFunctionCalls; + final void Function(Iterable)? _willCallFunctions; final List? _chatSafetySettings; final GenerationConfig? _chatGenerationConfig; final List _history; @@ -125,7 +126,7 @@ class FirebaseProvider extends LlmProvider with ChangeNotifier { } // Notify the caller of the function calls collected in this turn - _onFunctionCalls?.call(functionCalls); + _willCallFunctions?.call(functionCalls); // Add newline between responses yield '\n'; diff --git a/test/firebase_provider_test.dart b/test/firebase_provider_test.dart index 7b40fd6..0e13349 100644 --- a/test/firebase_provider_test.dart +++ b/test/firebase_provider_test.dart @@ -21,9 +21,9 @@ void main() { app = await Firebase.initializeApp(); }); - group('FirebaseProvider.onFunctionCalls', () { + group('FirebaseProvider.willCallFunctions', () { test( - 'invokes onFunctionCalls with the calls collected in a turn', + 'invokes willCallFunctions with the calls collected in a turn', () async { final client = _StubApiClient() @@ -35,7 +35,7 @@ void main() { final recordedCalls = []; final provider = FirebaseProvider( model: _createModel(app, client), - onFunctionCalls: recordedCalls.addAll, + willCallFunctions: recordedCalls.addAll, onFunctionCall: (functionCall) async => {'temperature': 60}, ); @@ -50,14 +50,14 @@ void main() { ); test( - 'does not invoke onFunctionCalls when no function calls are made', + 'does not invoke willCallFunctions when no function calls are made', () async { final client = _StubApiClient()..streamResponses.add([_textResponse]); final recordedCalls = []; final provider = FirebaseProvider( model: _createModel(app, client), - onFunctionCalls: recordedCalls.addAll, + willCallFunctions: recordedCalls.addAll, ); await provider.sendMessageStream('Just say something').drain();