-
Notifications
You must be signed in to change notification settings - Fork 6
Add onCallGenkit extension
#126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mosuem
wants to merge
21
commits into
firebase:main
Choose a base branch
from
mosuem:addOnCallGenkit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
0401c96
initial
mosuem 4263315
sdk
mosuem f510654
add example
mosuem 41f85e3
remove unused deps
mosuem 02c51cb
newline
mosuem c7dc03b
add readme
mosuem 18fa4f4
fix
mosuem 7f743bb
add more readme
mosuem d3c06e3
fixes
mosuem 1986765
add contextprovider
mosuem 0703d9d
add contextprovider
mosuem 0a884bd
add lints
mosuem 8c3e8c7
add ignore
mosuem 519f708
fix cl
mosuem e715872
update
mosuem b2fadcc
Rename
mosuem 691929f
Merge branch 'main' into addOnCallGenkit
mosuem 4e6aac4
Use published packages
mosuem 1ae6c11
use firebase
mosuem 0aac82d
Merge branch 'main' into addOnCallGenkit
mosuem 93118df
add licenses
mosuem File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # https://dart.dev/guides/libraries/private-files | ||
| # Created by `dart pub` | ||
| .dart_tool/ | ||
|
|
||
| # Avoid committing pubspec.lock for library packages; see | ||
| # https://dart.dev/guides/libraries/private-files#pubspeclock. | ||
| pubspec.lock |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ## 0.1.0 | ||
|
|
||
| - Initial version. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Extension on firebase functions to use genkit. | ||
|
|
||
| Not included in firebase functions itself for more flexibility and reduced deps. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| include: package:dart_flutter_team_lints/analysis_options.yaml | ||
|
|
||
| linter: | ||
| rules: | ||
| - prefer_final_locals | ||
| - unnecessary_parenthesis | ||
| - prefer_expression_function_bodies | ||
| - document_ignores | ||
| - parameter_assignments | ||
| - prefer_final_in_for_each |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Sample of creating a genkit flow and setting it up for an http trigger. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import 'package:firebase_functions/firebase_functions.dart'; | ||
| import 'package:firebase_functions_genkit/firebase_functions_genkit.dart'; | ||
| import 'package:genkit/genkit.dart'; | ||
| import 'package:genkit_google_genai/genkit_google_genai.dart'; | ||
|
|
||
| const name = 'jokeTeller'; | ||
|
|
||
| void main(List<String> args) { | ||
| final gemini = googleAI(); | ||
| final ai = Genkit(plugins: [gemini]); | ||
| final flow = ai.defineFlow( | ||
| name: name, | ||
| inputSchema: .string(), | ||
| outputSchema: .string(), | ||
| streamSchema: .string(), | ||
| fn: (jokeType, context) async { | ||
| final prompt = 'Tell me a $jokeType joke.'; | ||
|
|
||
| /// gemini.model does not have a generic type | ||
| // ignore: inference_failure_on_function_invocation | ||
| final stream = ai.generateStream( | ||
| model: gemini.model('gemini-2.5-flash'), | ||
| prompt: prompt, | ||
| ); | ||
| await stream.forEach((chunk) => context.sendChunk(chunk.text)); | ||
| return stream.result.text; | ||
|
mosuem marked this conversation as resolved.
|
||
| }, | ||
| ); | ||
|
|
||
| fireUp(args, (firebase) { | ||
| firebase.https.onCallGenkit( | ||
| name: name, | ||
| flow: flow, | ||
| contextProvider: (request) => {'auth': request.auth?.token?['email']}, | ||
| ); | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| name: example | ||
|
|
||
| resolution: workspace | ||
|
|
||
| environment: | ||
| sdk: ^3.10.0 | ||
|
|
||
| dependencies: | ||
| firebase_functions: any | ||
| firebase_functions_genkit: any | ||
| genkit: any | ||
| genkit_google_genai: any |
15 changes: 15 additions & 0 deletions
15
firebase_functions_genkit/lib/firebase_functions_genkit.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| export 'src/firebase_functions_genkit_base.dart'; |
62 changes: 62 additions & 0 deletions
62
firebase_functions_genkit/lib/src/firebase_functions_genkit_base.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import 'package:firebase_functions/firebase_functions.dart'; | ||
| import 'package:genkit/genkit.dart'; | ||
|
|
||
| /// It's experimental as we can't semver package:meta | ||
| // ignore: experimental_member_use | ||
| import 'package:meta/meta.dart' show mustBeConst; | ||
|
|
||
| /// Extension on [HttpsNamespace] to provide a seamless integration with Genkit. | ||
| extension GenkitExt on HttpsNamespace { | ||
| /// Registers a Genkit [flow] as a Firebase callable function. | ||
| /// | ||
| /// Automatically handles streaming and non-streaming responses based on | ||
| /// [CallableRequest.acceptsStreaming]. | ||
| /// | ||
| /// Use [contextProvider] to map properties from the Firebase | ||
| /// [CallableRequest] (such as authentication tokens) into the Genkit context. | ||
| void onCallGenkit<Output extends Object, Init>({ | ||
| // Must repeat the name | ||
| /// It's experimental as we can't semver package:meta | ||
| // ignore: experimental_member_use | ||
| @mustBeConst required String name, | ||
| required Flow<Object?, Output, Output, Init> flow, | ||
|
mosuem marked this conversation as resolved.
|
||
|
|
||
| /// It's experimental as we can't semver package:meta | ||
| // ignore: experimental_member_use | ||
| @mustBeConst CallableOptions? options = const CallableOptions(), | ||
| Map<String, dynamic> Function(CallableRequest<Object?>)? contextProvider, | ||
| }) { | ||
| /// This is why we restate the name in the params above | ||
| // ignore: non_const_argument_for_const_parameter | ||
| onCall<Output>(name: name, options: options, (request, response) async { | ||
| if (request.acceptsStreaming) { | ||
| final actionStream = flow.stream( | ||
| request.data, | ||
| context: contextProvider?.call(request), | ||
| ); | ||
| await actionStream.forEach((chunk) => response.sendChunk(chunk)); | ||
| return CallableResult(actionStream.result); | ||
| } else { | ||
| final run = await flow.run( | ||
| request.data, | ||
| context: contextProvider?.call(request), | ||
| ); | ||
| return CallableResult(run.result); | ||
| } | ||
| }); | ||
|
mosuem marked this conversation as resolved.
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| name: firebase_functions_genkit | ||
| description: A starting point for Dart libraries or applications. | ||
| version: 0.1.0 | ||
| repository: https://github.com/firebase/firebase-functions-dart | ||
|
|
||
| publish_to: none | ||
|
|
||
| workspace: | ||
| - example | ||
|
|
||
| environment: | ||
| sdk: ^3.10.0 | ||
|
|
||
| dependencies: | ||
| firebase_functions: ^0.5.0 | ||
| genkit: ^0.12.1 | ||
| meta: ^1.18.1 | ||
|
|
||
| dev_dependencies: | ||
| dart_flutter_team_lints: ^3.5.2 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.