Skip to content

Commit 3fffa68

Browse files
committed
Optionally enable Firebase telemetry on Genkit init
1 parent 0ce9ffa commit 3fffa68

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

src/init/features/genkit/index.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,21 @@ export async function genkitSetup(
382382
default: true,
383383
}))
384384
) {
385-
generateSampleFile(modelOptions[model].plugin, plugins, projectDir, genkitInfo.templateVersion);
385+
386+
logger.info("Telemetry data can be used to monitor and gain insights into your AI features. There may be a cost associated with using this feature. See https://firebase.google.com/docs/genkit/observability/telemetry-collection.");
387+
const enableTelemetry =
388+
options.nonInteractive ||
389+
(await confirm({
390+
message: "Would like you to enable telemetry collection?",
391+
default: true
392+
}));
393+
394+
generateSampleFile(
395+
modelOptions[model].plugin,
396+
plugins,
397+
projectDir,
398+
genkitInfo.templateVersion,
399+
enableTelemetry);
386400
}
387401
}
388402

@@ -507,6 +521,7 @@ function generateSampleFile(
507521
configPlugins: string[],
508522
projectDir: string,
509523
templateVersion: string,
524+
enableTelemetry: boolean,
510525
): void {
511526
let modelImport = "";
512527
if (modelPlugin && pluginToInfo[modelPlugin].model) {
@@ -534,6 +549,7 @@ function generateSampleFile(
534549
? pluginToInfo[modelPlugin].model || pluginToInfo[modelPlugin].modelStr || ""
535550
: "'' /* TODO: Set a model. */",
536551
),
552+
enableTelemetry,
537553
);
538554
logLabeledBullet("genkit", "Generating sample file");
539555
try {
@@ -622,16 +638,24 @@ async function updatePackageJson(nonInteractive: boolean, projectDir: string): P
622638
}
623639
}
624640

625-
function renderConfig(pluginNames: string[], template: string): string {
641+
function renderConfig(pluginNames: string[], template: string, enableTelemetry: boolean): string {
626642
const imports = pluginNames
627-
.map((pluginName) => generateImportStatement(pluginToInfo[pluginName].imports, pluginName))
628-
.join("\n");
643+
.map((pluginName) => generateImportStatement(pluginToInfo[pluginName].imports, pluginName));
644+
if (enableTelemetry) {
645+
imports.push(generateImportStatement("enableFirebaseTelemetry", "@genkit-ai/firebase"));
646+
}
647+
const telemetryBlock = enableTelemetry
648+
? "\n// Collect telemetry data to enable production monitoring.\n" +
649+
"// See https://firebase.google.com/docs/genkit/observability/telemetry-collection.\n" +
650+
"enableFirebaseTelemetry();\n"
651+
: "";
629652
const plugins =
630653
pluginNames.map((pluginName) => ` ${pluginToInfo[pluginName].init},`).join("\n") ||
631654
" /* Add your plugins here. */";
632655
return template
633-
.replace("$GENKIT_CONFIG_IMPORTS", imports)
634-
.replace("$GENKIT_CONFIG_PLUGINS", plugins);
656+
.replace("$GENKIT_CONFIG_IMPORTS", imports.join("\n"))
657+
.replace("$GENKIT_CONFIG_PLUGINS", plugins)
658+
.replace("$GENKIT_ENABLE_TELEMETRY", telemetryBlock);
635659
}
636660

637661
function generateImportStatement(imports: string, name: string): string {

templates/genkit/firebase.1.0.0.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const ai = genkit({
2020
$GENKIT_CONFIG_PLUGINS
2121
],
2222
});
23-
23+
$GENKIT_ENABLE_TELEMETRY
2424
// Define a simple flow that prompts an LLM to generate menu suggestions.
2525
const menuSuggestionFlow = ai.defineFlow({
2626
name: "menuSuggestionFlow",

0 commit comments

Comments
 (0)