-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathinit.ts
More file actions
283 lines (277 loc) · 10.7 KB
/
init.ts
File metadata and controls
283 lines (277 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import { z } from "zod";
import { tool } from "../../tool";
import { toContent } from "../../util";
import { DEFAULT_RULES } from "../../../init/features/database";
import { actuate, Setup, SetupInfo } from "../../../init/index";
import { freeTrialTermsLink } from "../../../dataconnect/freeTrial";
import { requireGeminiToS } from "../../errors";
import { FirebaseError } from "../../../error";
import {
parseAppId,
validateProjectNumberMatch,
validateAppExists,
} from "../../../init/features/ailogic/utils";
import { getFirebaseProject } from "../../../management/projects";
import { FDC_DEFAULT_REGION } from "../../../init/features/dataconnect";
export const init = tool(
"core",
{
name: "init",
description:
"Use this to initialize selected Firebase services in the workspace (Cloud Firestore database, Firebase Data Connect, Firebase Realtime Database, Firebase AI Logic). All services are optional; specify only the products you want to set up. " +
"You can initialize new features into an existing project directory, but re-initializing an existing feature may overwrite configuration. " +
"To deploy the initialized features, run the `firebase deploy` command after `firebase_init` tool.",
inputSchema: z.object({
features: z.object({
database: z
.object({
rules_filename: z
.string()
.optional()
.default("database.rules.json")
.describe("The file to use for Realtime Database Security Rules."),
rules: z
.string()
.optional()
.default(DEFAULT_RULES)
.describe("The security rules to use for Realtime Database Security Rules."),
})
.optional()
.describe(
"Provide this object to initialize Firebase Realtime Database in this project directory.",
),
firestore: z
.object({
database_id: z
.string()
.optional()
.default("(default)")
.describe("The database ID to use for Firestore."),
location_id: z
.string()
.optional()
.default("nam5")
.describe("The GCP region ID to set up the Firestore database."),
rules_filename: z
.string()
.optional()
.default("firestore.rules")
.describe("The file to use for Firestore Security Rules."),
rules: z
.string()
.optional()
.describe(
"The security rules to use for Firestore Security Rules. Default to open rules that expire in 30 days.",
),
})
.optional()
.describe("Provide this object to initialize Cloud Firestore in this project directory."),
dataconnect: z
.object({
app_description: z
.string()
.optional()
.describe(
"Provide a description of the app you are trying to build. If present, Gemini will help generate Data Connect Schema, Connector and seed data",
),
service_id: z
.string()
.optional()
.describe(
"The Firebase Data Connect service ID to initialize. Default to match the current folder name.",
),
location_id: z
.string()
.optional()
.default(FDC_DEFAULT_REGION)
.describe("The GCP region ID to set up the Firebase Data Connect service."),
cloudsql_instance_id: z
.string()
.optional()
.describe(
"The GCP Cloud SQL instance ID to use in the Firebase Data Connect service. By default, use <serviceId>-fdc. " +
"\nSet `provision_cloudsql` to true to start Cloud SQL provisioning.",
),
cloudsql_database: z
.string()
.optional()
.default("fdcdb")
.describe("The Postgres database ID to use in the Firebase Data Connect service."),
provision_cloudsql: z
.boolean()
.optional()
.default(false)
.describe(
"If true, provision the Cloud SQL instance if `cloudsql_instance_id` does not exist already. " +
`\nThe first Cloud SQL instance in the project will use the Data Connect no-cost trial. See its terms of service: ${freeTrialTermsLink()}.`,
),
})
.optional()
.describe(
"Provide this object to initialize Firebase Data Connect with Cloud SQL Postgres in this project directory.\n" +
"It installs Data Connect Generated SDKs in all detected apps in the folder.",
),
storage: z
.object({
rules_filename: z
.string()
.optional()
.default("storage.rules")
.describe("The file to use for Firebase Storage Security Rules."),
rules: z
.string()
.optional()
.describe(
"The security rules to use for Firebase Storage Security Rules. Default to closed rules that deny all access.",
),
})
.optional()
.describe(
"Provide this object to initialize Firebase Storage in this project directory.",
),
ailogic: z
.object({
app_id: z
.string()
.describe(
"Firebase app ID (format: 1:PROJECT_NUMBER:PLATFORM:APP_ID). Must be an existing app in your Firebase project.",
),
})
.optional()
.describe("Enable Firebase AI Logic feature for existing app"),
hosting: z
.object({
site_id: z
.string()
.optional()
.describe(
"The ID of the hosting site to configure. If omitted and there is a default hosting site, that will be used.",
),
public_directory: z
.string()
.optional()
.default("public")
.describe(
"The directory containing public files that will be served. If using a build tool, this likely should be the output directory of that tool.",
),
single_page_app: z
.boolean()
.optional()
.default(false)
.describe("Configure as a single-page app."),
})
.optional()
.describe(
"Provide this object to initialize Firebase Hosting in this project directory.",
),
}),
}),
annotations: {
title: "Initialize Firebase Products",
readOnlyHint: false,
idempotentHint: true,
},
_meta: {
requiresProject: false, // Can start from scratch.
requiresAuth: false, // Will throw error if the specific feature needs it.
},
},
async ({ features }, { projectId, config, rc }) => {
const featuresList: string[] = [];
const featureInfo: SetupInfo = {};
if (features.database) {
featuresList.push("database");
featureInfo.database = {
rulesFilename: features.database.rules_filename,
rules: features.database.rules,
writeRules: true,
};
}
if (features.firestore) {
featuresList.push("firestore");
featureInfo.firestore = {
databaseId: features.firestore.database_id,
locationId: features.firestore.location_id,
rulesFilename: features.firestore.rules_filename,
rules: features.firestore.rules || "",
writeRules: true,
indexesFilename: "",
indexes: "",
writeIndexes: true,
};
}
if (features.dataconnect) {
if (features.dataconnect.app_description) {
// If app description is provided, ensure the Gemini in Firebase API is enabled.
const err = await requireGeminiToS(projectId);
if (err) return err;
}
featuresList.push("dataconnect");
featureInfo.dataconnectSource = "mcp_init";
featureInfo.dataconnect = {
flow: "",
appDescription: features.dataconnect.app_description || "",
serviceId: features.dataconnect.service_id || "",
locationId: features.dataconnect.location_id || "",
cloudSqlInstanceId: features.dataconnect.cloudsql_instance_id || "",
cloudSqlDatabase: features.dataconnect.cloudsql_database || "",
shouldProvisionCSQL: !!features.dataconnect.provision_cloudsql,
};
featureInfo.dataconnectSdk = {
// Add FDC generated SDKs to all apps detected.
apps: [],
};
}
if (features.ailogic) {
// AI Logic requires a project
if (!projectId) {
throw new FirebaseError(
"AI Logic feature requires a Firebase project. Please specify a project ID.",
{ exit: 1 },
);
}
// Validate AI Logic app for MCP flow
const appInfo = parseAppId(features.ailogic.app_id);
const projectInfo = await getFirebaseProject(projectId);
validateProjectNumberMatch(appInfo, projectInfo);
const appData = await validateAppExists(appInfo, projectId);
featuresList.push("ailogic");
featureInfo.ailogic = {
appId: features.ailogic.app_id,
displayName: appData.displayName,
};
}
if (features.hosting) {
featuresList.push("hosting");
featureInfo.hosting = {
newSiteId: features.hosting.site_id,
public: features.hosting.public_directory,
spa: features.hosting.single_page_app,
};
}
const setup: Setup = {
config: config?.src,
rcfile: rc?.data,
projectId: projectId,
features: [...featuresList],
featureInfo: featureInfo,
instructions: [],
};
// Set force to true to avoid prompting the user for confirmation.
await actuate(setup, config, { force: true });
config.writeProjectFile("firebase.json", setup.config);
config.writeProjectFile(".firebaserc", setup.rcfile);
if (featureInfo.dataconnectSdk && !featureInfo.dataconnectSdk.apps.length) {
setup.instructions.push(
`No app is found in the current folder. We recommend you create an app (web, ios, android) first, then re-run the 'firebase_init' MCP tool with the same input without app_description to add Data Connect SDKs to your apps.
Consider popular commands like 'npx create-react-app my-app', 'npx create-next-app my-app', 'flutter create my-app', etc`,
);
}
return toContent(
`Successfully setup those features: ${featuresList.join(", ")}
To get started:
- ${setup.instructions.join("\n\n- ")}
`,
);
},
);