vybekiit 0.7.32 → 0.7.33
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bin.js +5 -2
- package/dist/global-skills/aws-serverless/SKILL.md +168 -32
- package/dist/global-skills/aws-serverless/references/api-gateway.md +17 -0
- package/dist/global-skills/aws-serverless/references/architecture.md +9 -13
- package/dist/global-skills/aws-serverless/references/lambda.md +3 -3
- package/dist/global-skills/aws-serverless/references/orchestration.md +46 -20
- package/dist/global-skills/aws-serverless/references/production.md +1 -1
- package/dist/global-skills/eas-simulator/SKILL.md +15 -15
- package/dist/global-skills/eas-simulator/references/controllers.md +11 -7
- package/dist/global-skills/eas-simulator/references/run-your-app.md +8 -8
- package/dist/global-skills/eas-simulator/references/troubleshooting.md +10 -6
- package/dist/global-skills/email-best-practices/references/sending-reliability.md +17 -22
- package/dist/global-skills/extension-to-functions-codebase/SKILL.md +1 -0
- package/dist/global-skills/firebase-ai-logic-basics/SKILL.md +161 -76
- package/dist/global-skills/firebase-ai-logic-basics/references/flutter_setup.md +37 -0
- package/dist/global-skills/firebase-ai-logic-basics/references/ios_setup.md +97 -3
- package/dist/global-skills/firebase-ai-logic-basics/references/usage_patterns_android.md +88 -3
- package/dist/global-skills/firebase-ai-logic-basics/references/usage_patterns_web.md +91 -3
- package/dist/global-skills/firebase-app-hosting-basics/SKILL.md +5 -1
- package/dist/global-skills/firebase-auth-basics/SKILL.md +1 -0
- package/dist/global-skills/firebase-auth-basics/references/client_sdk_android.md +9 -9
- package/dist/global-skills/firebase-auth-basics/references/client_sdk_web.md +3 -3
- package/dist/global-skills/firebase-auth-basics/references/flutter_setup.md +22 -19
- package/dist/global-skills/firebase-auth-basics/references/security_rules.md +2 -2
- package/dist/global-skills/firebase-basics/SKILL.md +6 -2
- package/dist/global-skills/firebase-basics/references/android_setup.md +101 -37
- package/dist/global-skills/firebase-basics/references/ios_setup.md +57 -0
- package/dist/global-skills/firebase-basics/references/web_setup.md +52 -0
- package/dist/global-skills/firebase-crashlytics/SKILL.md +1 -0
- package/dist/global-skills/firebase-crashlytics/references/android_setup.md +84 -84
- package/dist/global-skills/firebase-crashlytics/references/ios_setup.md +1 -0
- package/dist/global-skills/firebase-data-connect/SKILL.md +1 -0
- package/dist/global-skills/firebase-data-connect/reference/config.md +8 -8
- package/dist/global-skills/firebase-data-connect/reference/realtime.md +4 -1
- package/dist/global-skills/firebase-data-connect/reference/sdk_android.md +33 -34
- package/dist/global-skills/firebase-data-connect/reference/sdk_web.md +8 -3
- package/dist/global-skills/firebase-data-connect/reference/security.md +13 -13
- package/dist/global-skills/firebase-firestore/SKILL.md +17 -7
- package/dist/global-skills/firebase-firestore/references/enterprise/android_sdk_usage.md +32 -32
- package/dist/global-skills/firebase-firestore/references/enterprise/indexes.md +2 -1
- package/dist/global-skills/firebase-firestore/references/standard/android_sdk_usage.md +14 -14
- package/dist/global-skills/firebase-firestore/references/standard/indexes.md +2 -1
- package/dist/global-skills/firebase-hosting-basics/SKILL.md +1 -0
- package/dist/global-skills/firebase-remote-config-basics/SKILL.md +2 -1
- package/dist/global-skills/firebase-remote-config-basics/references/android_setup.md +36 -37
- package/dist/global-skills/firebase-security-rules-auditor/SKILL.md +7 -6
- package/dist/global-skills/firestore-rules-creation/SKILL.md +22 -10
- package/dist/global-skills/neon/SKILL.md +16 -0
- package/dist/global-skills/neon-ai-gateway/SKILL.md +43 -14
- package/dist/global-skills/neon-postgres/SKILL.md +1 -1
- package/dist/global-skills/neon-postgres/references/full-text-search.md +27 -17
- package/dist/global-skills/neon-postgres/references/hybrid-search.md +19 -15
- package/dist/global-skills/neon-postgres/references/lakebase-search-drizzle.md +99 -14
- package/dist/global-skills/neon-postgres/references/vector-search.md +93 -10
- package/dist/global-skills/resend/SKILL.md +4 -15
- package/dist/global-skills/resend/references/domains.md +1 -1
- package/dist/global-skills/resend/references/installation.md +3 -3
- package/dist/global-skills/resend/references/sending/best-practices.md +3 -3
- package/dist/global-skills/resend/references/sending/single-email-examples.md +3 -3
- package/dist/global-skills/stripe-best-practices/SKILL.md +18 -18
- package/dist/global-skills/stripe-best-practices/references/tax.md +26 -14
- package/dist/global-skills/use-railway/SKILL.md +6 -6
- package/dist/global-skills/use-railway/references/iac.md +2 -1
- package/dist/global-skills/use-railway/references/tracing.md +13 -5
- package/dist/global-skills/workers-best-practices/SKILL.md +1 -1
- package/dist/global-skills/wrangler/SKILL.md +3 -3
- package/dist/global-skills/xcode-project-setup/SKILL.md +1 -0
- package/package.json +8 -8
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: firebase-ai-logic-basics
|
|
3
|
-
description:
|
|
3
|
+
description: >-
|
|
4
|
+
Integrates Firebase AI Logic (formerly Vertex AI for Firebase) into mobile and web apps (Android, iOS, Web, and Flutter) to call Gemini models from client code. Use when provisioning or initializing Firebase AI Logic, or adding Gemini features to an app: text generation, multimodal input, chat, streaming, structured output, hybrid on-device inference, App Check protection, or switching models with Remote Config.
|
|
4
5
|
version: 1.0.1
|
|
5
6
|
metadata:
|
|
7
|
+
author: Google LLC
|
|
6
8
|
category: AiAndMachineLearning
|
|
7
9
|
disable-model-invocation: true
|
|
8
10
|
---
|
|
@@ -12,57 +14,98 @@ disable-model-invocation: true
|
|
|
12
14
|
## Overview
|
|
13
15
|
|
|
14
16
|
Firebase AI Logic is a product of Firebase that allows developers to add gen AI
|
|
15
|
-
to their mobile and web apps using client-side SDKs.
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
to their mobile and web apps using client-side SDKs. The app can directly call
|
|
18
|
+
Gemini models via Firebase AI Logic without the developer needing to manage a
|
|
19
|
+
dedicated backend. Firebase AI Logic was previously known as "Vertex AI for
|
|
20
|
+
Firebase".
|
|
19
21
|
|
|
20
22
|
It supports the two Gemini API providers:
|
|
21
23
|
|
|
22
|
-
-
|
|
23
|
-
|
|
24
|
-
-
|
|
25
|
-
|
|
24
|
+
- **Gemini Developer API**: It has a free tier ideal for prototyping, and
|
|
25
|
+
pay-as-you-go for production
|
|
26
|
+
- **Agent Platform Gemini API** (formerly branded Vertex AI): Ideal for scale
|
|
27
|
+
with enterprise-grade production readiness, requires Blaze pricing plan
|
|
26
28
|
|
|
27
29
|
Use the Gemini Developer API as a default, and only Agent Platform Gemini API
|
|
28
30
|
(formerly branded Vertex AI) if the application requires it.
|
|
29
31
|
|
|
30
|
-
|
|
32
|
+
______________________________________________________________________
|
|
33
|
+
|
|
34
|
+
## Setup & initialization
|
|
31
35
|
|
|
32
36
|
### Prerequisites
|
|
33
37
|
|
|
34
|
-
-
|
|
35
|
-
|
|
36
|
-
-
|
|
37
|
-
|
|
38
|
-
-
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
- Identify the platform the user is interested in building on prior to starting:
|
|
39
|
+
Android, iOS, Web, or Flutter.
|
|
40
|
+
- If their platform is unsupported, Direct the user to Firebase Docs to learn
|
|
41
|
+
how to set up AI Logic for their application (share this link with the user
|
|
42
|
+
https://firebase.google.com/docs/ai-logic/get-started)
|
|
43
|
+
- The Firebase CLI commands below run through `npx` on every platform, so they
|
|
44
|
+
need **Node.js 20+** and npm. Use the `firebase-basics` skill to check for
|
|
45
|
+
them and to log in to the Firebase CLI. If Node.js is missing or older than
|
|
46
|
+
version 20, ask the user to install it as that skill describes. Don't install
|
|
47
|
+
system packages yourself, for example with `sudo apt-get`, because that
|
|
48
|
+
changes the user's machine without their consent.
|
|
49
|
+
|
|
50
|
+
### 1. Provision Firebase AI Logic (ALL platforms)
|
|
51
|
+
|
|
52
|
+
Use the **Firebase CLI** to provision Firebase AI Logic for the Firebase
|
|
53
|
+
Project:
|
|
54
|
+
|
|
55
|
+
1. Make sure that you're in a firebase directory (with a `firebase.json`).
|
|
41
56
|
|
|
42
|
-
|
|
57
|
+
1. Verify that you're running Firebase CLI commands against the intended
|
|
58
|
+
Firebase Project. When you run this command, the active project is marked
|
|
59
|
+
with "current":
|
|
43
60
|
|
|
44
|
-
|
|
61
|
+
```bash
|
|
62
|
+
npx -y firebase-tools@latest projects:list
|
|
63
|
+
```
|
|
45
64
|
|
|
46
|
-
|
|
65
|
+
1. Make sure there's at least one Firebase App for the target platform
|
|
66
|
+
registered with the current Firebase Project:
|
|
47
67
|
|
|
48
|
-
|
|
49
|
-
|
|
68
|
+
```bash
|
|
69
|
+
npx -y firebase-tools@latest apps:list
|
|
70
|
+
```
|
|
50
71
|
|
|
51
|
-
|
|
72
|
+
1. Provision Firebase AI Logic for the Firebase Project:
|
|
52
73
|
|
|
53
|
-
|
|
74
|
+
```bash
|
|
75
|
+
npx -y firebase-tools@latest init ailogic
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
This command will enable the Gemini Developer API in the Firebase project.
|
|
79
|
+
|
|
80
|
+
> [!WARNING] **CRITICAL: Provisioning Firebase AI Logic is Required** For all
|
|
81
|
+
> platforms (Android, iOS, Web, or Flutter), even if the app already uses
|
|
82
|
+
> Firebase, you MUST run `npx -y firebase-tools@latest init ailogic` to
|
|
83
|
+
> provision the service. The configuration files of `GoogleService-Info.plist`,
|
|
84
|
+
> `google-services.json`, and `firebase_options.dart` ONLY handle client
|
|
85
|
+
> configuration and do NOT enable the Firebase AI Logic service, leading to
|
|
86
|
+
> `PERMISSION_DENIED` errors.
|
|
54
87
|
|
|
55
|
-
`
|
|
88
|
+
If you can't run `init ailogic` yourself (for example because Node.js is missing
|
|
89
|
+
or the Firebase CLI isn't logged in) do NOT skip it. The app's requests to
|
|
90
|
+
Gemini models will fail until the Firebase AI Logic service is provisioned. Tell
|
|
91
|
+
the user to run this command in their project directory, and include it in your
|
|
92
|
+
final summary:
|
|
56
93
|
|
|
57
|
-
|
|
94
|
+
```bash
|
|
95
|
+
npx -y firebase-tools@latest init ailogic
|
|
96
|
+
```
|
|
58
97
|
|
|
59
|
-
|
|
98
|
+
### 2. Add the SDK and initialize the service in the app
|
|
60
99
|
|
|
61
|
-
|
|
100
|
+
Adding the SDK dependency and initializing the service are platform-specific.
|
|
101
|
+
Before writing code, read the reference for the target platform listed under
|
|
102
|
+
[Initialization Code References](#initialization-code-references).
|
|
62
103
|
|
|
63
104
|
More info in
|
|
64
105
|
[Firebase AI Logic Getting Started](https://firebase.google.com/docs/ai-logic/get-started.md.txt)
|
|
65
106
|
|
|
107
|
+
______________________________________________________________________
|
|
108
|
+
|
|
66
109
|
## Core Capabilities
|
|
67
110
|
|
|
68
111
|
> [!WARNING] **CRITICAL: Use current model names:** Always check the
|
|
@@ -92,16 +135,42 @@ To improve the user experience by showing partial results as they arrive (like a
|
|
|
92
135
|
typing effect), use `generateContentStream` instead of `generateContent` for
|
|
93
136
|
faster display of results.
|
|
94
137
|
|
|
138
|
+
### Text-to-Speech (TTS) Generation
|
|
139
|
+
|
|
140
|
+
Generate spoken audio directly on client devices without a custom speech
|
|
141
|
+
backend. Firebase AI Logic supports speech synthesis using dedicated Gemini TTS
|
|
142
|
+
models:
|
|
143
|
+
|
|
144
|
+
- **Supported Models**: `gemini-3.1-flash-tts-preview`
|
|
145
|
+
- **Capabilities**:
|
|
146
|
+
- Single-speaker voice persona selection (`voiceName`) and multi-speaker
|
|
147
|
+
dialogues (up to 2 distinct speakers) via `SpeechConfig` /
|
|
148
|
+
`MultiSpeakerVoiceConfig`
|
|
149
|
+
- Direct audio responses by setting `responseModalities` to audio
|
|
150
|
+
- Streaming speech responses with `generateContentStream` for low-latency
|
|
151
|
+
playback
|
|
152
|
+
- Audio directives (`[Audio Profile: ...]`, `[Scene: ...]`,
|
|
153
|
+
`[Director's Note: ...]`) and emotional tags (`[whispers]`, `[laughs]`,
|
|
154
|
+
`[slowly]`)
|
|
155
|
+
- Client-side decoding and playback of 24 kHz 16-bit linear PCM (`audio/l16`)
|
|
156
|
+
or WAV container format (`audio/x-wav`)
|
|
157
|
+
|
|
158
|
+
See the [Text-to-Speech (TTS) Generation Guide](references/tts_generation.md)
|
|
159
|
+
for full implementation details, streaming patterns, and copy-paste code
|
|
160
|
+
snippets for iOS (Swift), Android (Kotlin), and Web.
|
|
161
|
+
|
|
95
162
|
### Generate Images with Nano Banana
|
|
96
163
|
|
|
97
164
|
> [!WARNING] **Use current Image model names:** Always check the
|
|
98
165
|
> [Firebase AI Logic Models documentation](https://firebase.google.com/docs/ai-logic/models.md.txt)
|
|
99
166
|
> for the currently supported image generation (Nano Banana) model names.
|
|
100
167
|
|
|
101
|
-
-
|
|
168
|
+
- Requires an upgraded Blaze pay-as-you-go billing plan.
|
|
102
169
|
|
|
103
170
|
### Search Grounding with the built in googleSearch tool
|
|
104
171
|
|
|
172
|
+
______________________________________________________________________
|
|
173
|
+
|
|
105
174
|
## Supported Platforms and Frameworks
|
|
106
175
|
|
|
107
176
|
Supported Platforms and Frameworks include Kotlin and Java for Android, Swift
|
|
@@ -115,23 +184,26 @@ Enforce a specific JSON schema for the response.
|
|
|
115
184
|
|
|
116
185
|
### On-Device AI (Hybrid)
|
|
117
186
|
|
|
118
|
-
Hybrid on-device
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
187
|
+
Hybrid inference runs prompts with an on-device model when one is available and
|
|
188
|
+
falls back to a cloud-hosted model otherwise (or vice versa). Availability and
|
|
189
|
+
setup steps differ by platform, more info in the
|
|
190
|
+
[hybrid and on-device documentation](https://firebase.google.com/docs/ai-logic/hybrid.md.txt),
|
|
191
|
+
which links to each platform's guide.
|
|
192
|
+
|
|
193
|
+
______________________________________________________________________
|
|
123
194
|
|
|
124
195
|
## Security & Production
|
|
125
196
|
|
|
126
|
-
### App Check
|
|
197
|
+
### Firebase App Check
|
|
127
198
|
|
|
128
199
|
> [!WARNING] **Critical Safety Requirement:** In order to use AI Logic safely,
|
|
129
200
|
> you MUST set up App Check on your app. This prevents unauthorized clients from
|
|
130
201
|
> using your API quota and accessing your backend resources.
|
|
131
202
|
|
|
132
203
|
See
|
|
133
|
-
[App Check
|
|
134
|
-
for setup instructions.
|
|
204
|
+
[App Check for Firebase AI Logic](https://firebase.google.com/docs/ai-logic/app-check.md.txt)
|
|
205
|
+
for setup instructions. Each platform uses its own attestation provider, so
|
|
206
|
+
follow the instructions for the target platform.
|
|
135
207
|
|
|
136
208
|
#### App Check Debug Tokens for Local Development & CI/CD
|
|
137
209
|
|
|
@@ -139,57 +211,69 @@ Because App Check attestation providers (like Play Integrity or DeviceCheck)
|
|
|
139
211
|
reject emulators, simulators, or CI environments, you must use **App Check Debug
|
|
140
212
|
Tokens** during development and testing to bypass standard attestation.
|
|
141
213
|
|
|
214
|
+
> [!WARNING] **CRITICAL: Never Hardcode or Commit Debug Tokens** App Check debug
|
|
215
|
+
> tokens allow clients to bypass attestation and access backend resources
|
|
216
|
+
> without a genuine device. Treat them as private secrets. **Never commit debug
|
|
217
|
+
> tokens to version control or hardcode raw token strings in client code** (Web,
|
|
218
|
+
> Android, iOS, or Flutter). Always inject them through local environment
|
|
219
|
+
> variables, gitignored local configurations, or CI secrets. If a token is
|
|
220
|
+
> compromised, revoke it immediately in the Firebase Console.
|
|
221
|
+
|
|
142
222
|
##### Local Development (Auto-Generated)
|
|
143
223
|
|
|
144
|
-
1.
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
224
|
+
1. Configure your code's App Check provider to use the debug factory. The API is
|
|
225
|
+
platform-specific; see the App Check section of the platform reference or the
|
|
226
|
+
App Check documentation linked above.
|
|
227
|
+
1. Run your app in the emulator/localhost.
|
|
228
|
+
1. Look at your runtime debugger console / Logcat logs for the generated UUID:
|
|
229
|
+
- *Example:* `AppCheck debug token: "123a4567-b89c-12d3-e456-789012345678"`
|
|
230
|
+
1. Register this token in the Firebase Console under **Security > App Check >
|
|
231
|
+
Apps > Manage debug tokens**.
|
|
232
|
+
|
|
233
|
+
> **💡 Tip (Prevent Debug Token Churn):** Simulator or emulator resets, fresh
|
|
234
|
+
> installs, and clearing browser data can make the SDK generate a new debug
|
|
235
|
+
> token that you must register again. To keep a stable debug token without
|
|
236
|
+
> hardcoding it, see the App Check section of the platform reference.
|
|
155
237
|
|
|
156
238
|
##### CI/CD Pipelines (Pre-Provisioned)
|
|
157
239
|
|
|
158
|
-
1.
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
process.env.APP_CHECK_DEBUG_TOKEN`).
|
|
240
|
+
1. Generate and register a new debug token in the Firebase Console under
|
|
241
|
+
**Security > App Check > Apps > Manage debug tokens**.
|
|
242
|
+
1. Add this token string as an encrypted secret in your CI system (e.g.
|
|
243
|
+
`APP_CHECK_DEBUG_TOKEN`).
|
|
244
|
+
1. Configure your build to pass this secret as an environment variable to the
|
|
245
|
+
SDK during test execution.
|
|
165
246
|
|
|
166
|
-
### Remote Config
|
|
247
|
+
### Firebase Remote Config
|
|
167
248
|
|
|
168
249
|
Consider that you do not need to hardcode model names (e.g., a specific model
|
|
169
250
|
version string). Use Firebase Remote Config to update model versions dynamically
|
|
170
251
|
without deploying new client code. See
|
|
171
252
|
[Changing model names remotely](https://firebase.google.com/docs/ai-logic/change-model-name-remotely.md.txt)
|
|
172
253
|
|
|
173
|
-
|
|
174
|
-
> (Flutter, Android, iOS, Web), you MUST run `npx firebase-tools init ailogic`
|
|
175
|
-
> to provision the service. `flutterfire configure` ONLY handles client
|
|
176
|
-
> configuration and does NOT enable the AI service, leading to
|
|
177
|
-
> `PERMISSION_DENIED` errors.
|
|
254
|
+
______________________________________________________________________
|
|
178
255
|
|
|
179
256
|
## Initialization Code References
|
|
180
257
|
|
|
181
|
-
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
258
|
+
- **Android (Kotlin)**
|
|
259
|
+
|
|
260
|
+
- Gemini API Provider: Gemini Developer API
|
|
261
|
+
- Reference: [usage_patterns_android.md](references/usage_patterns_android.md)
|
|
262
|
+
|
|
263
|
+
- **iOS (Swift)**
|
|
264
|
+
|
|
265
|
+
- Gemini API Provider: Gemini Developer API
|
|
266
|
+
- Reference: [ios_setup.md](references/ios_setup.md)
|
|
267
|
+
|
|
268
|
+
- **Web Modular API**
|
|
269
|
+
|
|
270
|
+
- Gemini API Provider: Gemini Developer API
|
|
271
|
+
- Reference: [usage_patterns_web.md](references/usage_patterns_web.md)
|
|
272
|
+
|
|
273
|
+
- **Flutter (Dart)**
|
|
274
|
+
|
|
275
|
+
- Gemini API Provider: Gemini Developer API
|
|
276
|
+
- Reference: [flutter_setup.md](references/flutter_setup.md)
|
|
193
277
|
|
|
194
278
|
> [!WARNING] **CRITICAL: Use current model names:** Always check the
|
|
195
279
|
> [Firebase AI Logic Models documentation](https://firebase.google.com/docs/ai-logic/models.md.txt)
|
|
@@ -198,8 +282,9 @@ without deploying new client code. See
|
|
|
198
282
|
|
|
199
283
|
## References
|
|
200
284
|
|
|
201
|
-
[
|
|
285
|
+
[Android (Kotlin) SDK usage patterns](references/usage_patterns_android.md)
|
|
202
286
|
[iOS SDK code examples and usage patterns](references/ios_setup.md)
|
|
203
|
-
[
|
|
287
|
+
[Web SDK setup, code examples, and usage patterns](references/usage_patterns_web.md)
|
|
204
288
|
|
|
205
|
-
[
|
|
289
|
+
[Flutter SDK code examples and usage patterns](references/flutter_setup.md)
|
|
290
|
+
[Client-side Text-to-Speech (TTS) generation](references/tts_generation.md)
|
|
@@ -96,3 +96,40 @@ final chat = model.startChat(history: [
|
|
|
96
96
|
|
|
97
97
|
final response = await chat.sendMessage(Content.text('What is CBT?'));
|
|
98
98
|
```
|
|
99
|
+
|
|
100
|
+
## App Check Debug Provider
|
|
101
|
+
|
|
102
|
+
For App Check debug tokens during local development and CI/CD, add the plugin
|
|
103
|
+
with `flutter pub add firebase_app_check`, then activate the debug providers
|
|
104
|
+
right after `Firebase.initializeApp()`:
|
|
105
|
+
|
|
106
|
+
```dart
|
|
107
|
+
import 'package:firebase_app_check/firebase_app_check.dart';
|
|
108
|
+
import 'package:flutter/foundation.dart';
|
|
109
|
+
|
|
110
|
+
// In main(), after `await Firebase.initializeApp();`
|
|
111
|
+
if (kDebugMode) {
|
|
112
|
+
await FirebaseAppCheck.instance.activate(
|
|
113
|
+
providerAndroid: const AndroidDebugProvider(),
|
|
114
|
+
providerApple: const AppleDebugProvider(),
|
|
115
|
+
providerWeb: WebDebugProvider(),
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
- Use `providerAndroid`, `providerApple`, and `providerWeb`. The older
|
|
121
|
+
`androidProvider` and `appleProvider` parameters (`AndroidProvider.debug`,
|
|
122
|
+
`AppleProvider.debug`) still appear in many examples but are deprecated.
|
|
123
|
+
- Only use the debug providers in debug builds, as gated by `kDebugMode` above.
|
|
124
|
+
Release builds must activate the production providers (Play Integrity, App
|
|
125
|
+
Attest or DeviceCheck, and reCAPTCHA Enterprise) instead; see
|
|
126
|
+
[App Check for Firebase AI Logic](https://firebase.google.com/docs/ai-logic/app-check.md.txt).
|
|
127
|
+
- **CI/CD**: Pass the pre-provisioned token to all three debug providers, e.g.
|
|
128
|
+
`debugToken: const String.fromEnvironment('APP_CHECK_DEBUG_TOKEN')`, and build
|
|
129
|
+
with `--dart-define=APP_CHECK_DEBUG_TOKEN=<token>`.
|
|
130
|
+
- **Flutter web**: `providerWeb` is required on web; without it, `activate()`
|
|
131
|
+
throws. `WebDebugProvider` turns on the web debug token itself, so
|
|
132
|
+
`web/index.html` doesn't need `self.FIREBASE_APPCHECK_DEBUG_TOKEN`.
|
|
133
|
+
- If the app uses `firebase_ai` 3.11.0 or lower, also set the `appCheck`
|
|
134
|
+
parameter of `FirebaseAI.googleAI(...)` to `FirebaseAppCheck.instance`. Newer
|
|
135
|
+
versions don't need this.
|
|
@@ -3,17 +3,46 @@
|
|
|
3
3
|
## 1. Import and Initialize
|
|
4
4
|
|
|
5
5
|
Ensure you have installed the `FirebaseAILogic` SDK via Swift Package Manager.
|
|
6
|
+
Use the `xcode-project-setup` skill to automate adding the SPM dependency to the
|
|
7
|
+
Xcode project.
|
|
6
8
|
|
|
7
9
|
```swift
|
|
8
10
|
import FirebaseAILogic
|
|
9
11
|
|
|
10
|
-
//
|
|
11
|
-
|
|
12
|
+
// To enable replay protection for generative or preview models, pass
|
|
13
|
+
// useLimitedUseAppCheckTokens: true with your backend:
|
|
14
|
+
let ai = FirebaseAI.firebaseAI(
|
|
15
|
+
backend: .googleAI(),
|
|
16
|
+
useLimitedUseAppCheckTokens: true
|
|
17
|
+
)
|
|
12
18
|
|
|
13
19
|
// [AGENT] Replace "<latest_supported_model>" with the latest model from https://firebase.google.com/docs/ai-logic/models.md.txt
|
|
14
20
|
let model = ai.generativeModel(modelName: "<latest_supported_model>")
|
|
15
21
|
```
|
|
16
22
|
|
|
23
|
+
### App Check Replay Protection
|
|
24
|
+
|
|
25
|
+
Generative and preview models enforce replay protection with 5-minute
|
|
26
|
+
limited-use App Check tokens. If you call a protected model without enabling
|
|
27
|
+
limited-use tokens, the request fails with:
|
|
28
|
+
|
|
29
|
+
```text
|
|
30
|
+
HTTP 403: "To access this model, you must enforce Firebase App Check"
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
To resolve this error, initialize `FirebaseAI` with
|
|
34
|
+
`useLimitedUseAppCheckTokens: true`:
|
|
35
|
+
|
|
36
|
+
```swift
|
|
37
|
+
let ai = FirebaseAI.firebaseAI(
|
|
38
|
+
backend: .googleAI(),
|
|
39
|
+
useLimitedUseAppCheckTokens: true
|
|
40
|
+
)
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
This ensures the SDK fetches a fresh, short-lived limited-use token for each
|
|
44
|
+
request rather than reusing a standard cached App Check token.
|
|
45
|
+
|
|
17
46
|
## 2. SwiftUI Integration (Best Practices)
|
|
18
47
|
|
|
19
48
|
Use the `@Observable` pattern to manage AI state and provide a smooth UX with
|
|
@@ -33,7 +62,10 @@ import FirebaseAILogic
|
|
|
33
62
|
@Observable
|
|
34
63
|
final class AIViewModel {
|
|
35
64
|
// [AGENT] Replace with the latest model from https://firebase.google.com/docs/ai-logic/models.md.txt
|
|
36
|
-
private lazy var model = FirebaseAI.firebaseAI(
|
|
65
|
+
private lazy var model = FirebaseAI.firebaseAI(
|
|
66
|
+
backend: .googleAI(),
|
|
67
|
+
useLimitedUseAppCheckTokens: true
|
|
68
|
+
).generativeModel(modelName: "<latest_supported_model>")
|
|
37
69
|
|
|
38
70
|
var responseText: String = ""
|
|
39
71
|
var isFetching: Bool = false
|
|
@@ -98,6 +130,68 @@ let model = FirebaseAI.firebaseAI().generativeModel(
|
|
|
98
130
|
)
|
|
99
131
|
```
|
|
100
132
|
|
|
133
|
+
## 4. App Check Debug Provider
|
|
134
|
+
|
|
135
|
+
For App Check debug tokens during local development and CI/CD, add the
|
|
136
|
+
`FirebaseAppCheck` product to the app target (with the `xcode-project-setup`
|
|
137
|
+
skill), then set the debug provider factory in debug builds *before*
|
|
138
|
+
`FirebaseApp.configure()`. Otherwise the debug provider isn't used.
|
|
139
|
+
|
|
140
|
+
```swift
|
|
141
|
+
import FirebaseAppCheck
|
|
142
|
+
import FirebaseCore
|
|
143
|
+
|
|
144
|
+
// In your app's init(), before configuring Firebase:
|
|
145
|
+
#if DEBUG
|
|
146
|
+
AppCheck.setAppCheckProviderFactory(AppCheckDebugProviderFactory())
|
|
147
|
+
#endif
|
|
148
|
+
FirebaseApp.configure()
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
For CI/CD, add an `AppCheckDebugToken` environment variable to the Xcode test
|
|
152
|
+
scheme with a value like `$(APP_CHECK_DEBUG_TOKEN)`, then pass the token from
|
|
153
|
+
the CI test command:
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
xcodebuild test -scheme <scheme> -workspace <project>.xcworkspace \
|
|
157
|
+
APP_CHECK_DEBUG_TOKEN=<token>
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Debug Token Persistence
|
|
161
|
+
|
|
162
|
+
When running on simulators or during development, the Firebase iOS SDK generates
|
|
163
|
+
a new debug token UUID whenever `NSUserDefaults` is cleared (e.g., simulator
|
|
164
|
+
reset or fresh install). To avoid invalidating tokens registered in the Firebase
|
|
165
|
+
Console and prevent token churn, persist a stable token by setting the
|
|
166
|
+
`AppCheckDebugToken` environment variable, which the SDK reads automatically at
|
|
167
|
+
runtime:
|
|
168
|
+
|
|
169
|
+
> [!WARNING] **CRITICAL: Never Hardcode or Commit Debug Tokens** Debug tokens
|
|
170
|
+
> grant access to backend resources without device attestation. Never commit
|
|
171
|
+
> debug tokens to version control or hardcode token strings in source code.
|
|
172
|
+
|
|
173
|
+
- **In Xcode Scheme (Recommended):** Edit Scheme -> Run -> Arguments ->
|
|
174
|
+
Environment Variables -> Add `AppCheckDebugToken = <YOUR_DEBUG_TOKEN>`. Keep
|
|
175
|
+
user schemes (`xcuserdata/`) unshared and gitignored.
|
|
176
|
+
- **In Code (Safe Dynamic Loading Only):** If setting the environment variable
|
|
177
|
+
in code before configuring `AppCheckDebugProviderFactory`, load the token
|
|
178
|
+
dynamically from a gitignored local file or environment rather than hardcoding
|
|
179
|
+
the token literal:
|
|
180
|
+
|
|
181
|
+
```swift
|
|
182
|
+
#if DEBUG
|
|
183
|
+
// ✅ SAFE: Load from gitignored local file or process environment
|
|
184
|
+
// Note: loadGitIgnoredDebugToken() is a placeholder for your custom helper (e.g., reading from a gitignored plist)
|
|
185
|
+
if let debugToken = loadGitIgnoredDebugToken() {
|
|
186
|
+
setenv("AppCheckDebugToken", debugToken, 0)
|
|
187
|
+
}
|
|
188
|
+
let providerFactory = AppCheckDebugProviderFactory()
|
|
189
|
+
AppCheck.setAppCheckProviderFactory(providerFactory)
|
|
190
|
+
#endif
|
|
191
|
+
|
|
192
|
+
FirebaseApp.configure() // Configure Firebase AFTER setting the App Check provider factory
|
|
193
|
+
```
|
|
194
|
+
|
|
101
195
|
# Advanced Features
|
|
102
196
|
|
|
103
197
|
### Chat Session (Multi-turn)
|
|
@@ -38,9 +38,9 @@ In your Activity or Fragment, initialize the `FirebaseAI` service and generate
|
|
|
38
38
|
content using a Gemini model:
|
|
39
39
|
|
|
40
40
|
```kotlin
|
|
41
|
-
import com.google.firebase.ai.FirebaseAI
|
|
42
41
|
import com.google.firebase.Firebase
|
|
43
42
|
import com.google.firebase.ai.ai
|
|
43
|
+
import com.google.firebase.ai.type.GenerativeBackend
|
|
44
44
|
|
|
45
45
|
class MainActivity : AppCompatActivity() {
|
|
46
46
|
|
|
@@ -48,8 +48,11 @@ class MainActivity : AppCompatActivity() {
|
|
|
48
48
|
super.onCreate(savedInstanceState)
|
|
49
49
|
setContentView(R.layout.activity_main)
|
|
50
50
|
|
|
51
|
-
// Initialize
|
|
52
|
-
val ai = Firebase.ai
|
|
51
|
+
// Initialize with replay protection using limited-use App Check tokens:
|
|
52
|
+
val ai = Firebase.ai(
|
|
53
|
+
backend = GenerativeBackend.googleAI(),
|
|
54
|
+
useLimitedUseAppCheckTokens = true
|
|
55
|
+
)
|
|
53
56
|
|
|
54
57
|
// [AGENT] Replace "<latest_supported_model>" with the latest model from https://firebase.google.com/docs/ai-logic/models.md.txt
|
|
55
58
|
val model = ai.generativeModel("<latest_supported_model>")
|
|
@@ -67,6 +70,30 @@ class MainActivity : AppCompatActivity() {
|
|
|
67
70
|
}
|
|
68
71
|
```
|
|
69
72
|
|
|
73
|
+
#### App Check Replay Protection on Android
|
|
74
|
+
|
|
75
|
+
Generative and preview models enforce replay protection using short-lived
|
|
76
|
+
(5-minute) limited-use App Check tokens to prevent replay attacks. If you call a
|
|
77
|
+
model enforcing replay protection without limited-use tokens, the request is
|
|
78
|
+
rejected with:
|
|
79
|
+
|
|
80
|
+
```text
|
|
81
|
+
HTTP 403: "To access this model, you must enforce Firebase App Check"
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
To resolve this on Android, pass `useLimitedUseAppCheckTokens = true` when
|
|
85
|
+
calling `Firebase.ai`:
|
|
86
|
+
|
|
87
|
+
```kotlin
|
|
88
|
+
val ai = Firebase.ai(
|
|
89
|
+
backend = GenerativeBackend.googleAI(),
|
|
90
|
+
useLimitedUseAppCheckTokens = true
|
|
91
|
+
)
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
This ensures the SDK requests fresh limited-use tokens (via Play Integrity or
|
|
95
|
+
the debug provider) for each request instead of reusing cached tokens.
|
|
96
|
+
|
|
70
97
|
#### Jetpack Compose (Modern)
|
|
71
98
|
|
|
72
99
|
Initialize inside a `ComponentActivity` and use `setContent`:
|
|
@@ -155,3 +182,61 @@ lifecycleScope.launch {
|
|
|
155
182
|
}
|
|
156
183
|
}
|
|
157
184
|
```
|
|
185
|
+
|
|
186
|
+
______________________________________________________________________
|
|
187
|
+
|
|
188
|
+
### 6. App Check Debug Provider
|
|
189
|
+
|
|
190
|
+
For App Check debug tokens during local development and CI/CD, install
|
|
191
|
+
`DebugAppCheckProviderFactory.getInstance()` as the App Check provider factory
|
|
192
|
+
in debug builds:
|
|
193
|
+
|
|
194
|
+
```kotlin
|
|
195
|
+
if (BuildConfig.DEBUG) {
|
|
196
|
+
Firebase.appCheck.installAppCheckProviderFactory(
|
|
197
|
+
DebugAppCheckProviderFactory.getInstance()
|
|
198
|
+
)
|
|
199
|
+
}
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
#### Debug Token Persistence
|
|
203
|
+
|
|
204
|
+
Emulator resets or clearing app storage erase `SharedPreferences`, causing
|
|
205
|
+
`DebugAppCheckProviderFactory` to print a new debug token in Logcat. To persist
|
|
206
|
+
a stable debug token across emulator resets and fresh installs without
|
|
207
|
+
hardcoding secrets:
|
|
208
|
+
|
|
209
|
+
> [!WARNING] **CRITICAL: Never Hardcode or Commit Debug Tokens** Never hardcode
|
|
210
|
+
> debug token strings in `build.gradle.kts` or Kotlin source files. Store the
|
|
211
|
+
> token in gitignored `local.properties` and inject it dynamically.
|
|
212
|
+
|
|
213
|
+
1. In gitignored `local.properties`:
|
|
214
|
+
|
|
215
|
+
```properties
|
|
216
|
+
APP_CHECK_DEBUG_TOKEN=<YOUR_DEBUG_TOKEN>
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
1. In `app/build.gradle.kts`:
|
|
220
|
+
|
|
221
|
+
```kotlin
|
|
222
|
+
val localProperties = java.util.Properties().apply {
|
|
223
|
+
val localPropertiesFile = rootProject.file("local.properties")
|
|
224
|
+
if (localPropertiesFile.exists()) {
|
|
225
|
+
load(localPropertiesFile.inputStream())
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
val appCheckDebugToken = localProperties.getProperty("APP_CHECK_DEBUG_TOKEN")
|
|
229
|
+
?: System.getenv("APP_CHECK_DEBUG_TOKEN") ?: ""
|
|
230
|
+
|
|
231
|
+
android {
|
|
232
|
+
defaultConfig {
|
|
233
|
+
// For normal emulator runs (injects into AndroidManifest)
|
|
234
|
+
manifestPlaceholders["firebaseAppCheckDebugSecret"] = appCheckDebugToken
|
|
235
|
+
|
|
236
|
+
// For instrumentation tests
|
|
237
|
+
if (appCheckDebugToken.isNotEmpty()) {
|
|
238
|
+
testInstrumentationRunnerArguments["firebaseAppCheckDebugSecret"] = appCheckDebugToken
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
```
|