Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a new Node.js sample demonstrating how to use Firebase AI Logic triggers with Cloud Sensitive Data Protection (DLP) to redact sensitive data from prompts and model responses. Feedback suggests improving code quality by avoiding global ESLint rule disables and optimizing performance by caching the projectId promise globally rather than fetching it on every function invocation.
| rules: { | ||
| "no-console": "off", | ||
| "no-unused-vars": "off", | ||
| "no-undef": "off", | ||
| "no-empty": "off", | ||
| "no-useless-escape": "off", | ||
| "no-prototype-builtins": "off", | ||
| "no-redeclare": "off", | ||
| "no-constant-condition": "off", | ||
| "no-case-declarations": "off" | ||
| } |
There was a problem hiding this comment.
Disabling a large number of important ESLint rules globally is not recommended as it can hide potential bugs and reduces code quality. While this might be acceptable for a simple sample, it's better to be more selective. For example, no-unused-vars and no-undef can catch critical errors.
Consider removing these global disables. If specific lines of code need to bypass a rule, use inline comments like // eslint-disable-next-line <rule-name> for those specific cases. This makes exceptions explicit and maintains a higher standard of code quality for the rest of the project.
| let dlp = new DlpServiceClient(); | ||
|
|
||
| /** | ||
| * Redacts sensitive data from a text string using Cloud Sensitive Data Protection (DLP). | ||
| * | ||
| * Inspects for basic sensitive infoTypes (email, phone number, credit card number, SSN) | ||
| * and replaces detected values with their infoType placeholder (e.g. "[EMAIL_ADDRESS]"). | ||
| * | ||
| * @param {string} text - The raw input text string to inspect and redact. | ||
| * @returns {Promise<string>} The redacted text, or the original text if empty or unchanged. | ||
| */ | ||
| export async function redactSensitiveData(text) { | ||
| if (typeof text !== "string" || !text.trim()) { | ||
| return text; | ||
| } | ||
|
|
||
| const projectId = await dlp.getProjectId(); |
There was a problem hiding this comment.
For better performance, the projectId should be fetched only once during a cold start, not on every function invocation. The projectId for a given function deployment will not change. You can initialize the promise to get the project ID in the global scope and then await it inside the function.
let dlp = new DlpServiceClient();
// Initialize the projectId promise once in the global scope.
const projectIdPromise = dlp.getProjectId();
/**
* Redacts sensitive data from a text string using Cloud Sensitive Data Protection (DLP).
*
* Inspects for basic sensitive infoTypes (email, phone number, credit card number, SSN)
* and replaces detected values with their infoType placeholder (e.g. "[EMAIL_ADDRESS]").
*
* @param {string} text - The raw input text string to inspect and redact.
* @returns {Promise<string>} The redacted text, or the original text if empty or unchanged.
*/
export async function redactSensitiveData(text) {
if (typeof text !== "string" || !text.trim()) {
return text;
}
const projectId = await projectIdPromise;
Description
Adds a 2nd gen Firebase Functions sample in
Node/ai-logic-sensitive-datademonstrating how to intercept and redact sensitive data (PII) using Firebase AI Logic triggers (beforeGenerateContentandafterGenerateContent) and Cloud Sensitive Data Protection (DLP).Key Features
redactPrompt): InterceptsbeforeGenerateContentrequests and redacts sensitive data (emails, phone numbers, credit card numbers, SSNs) from prompt contents and system instructions before reaching the Gemini API.redactResponse): InterceptsafterGenerateContentresponses and inspects/redacts sensitive data generated by the model before returning to the client.@google-cloud/dlpdeidentifyContentwithreplaceWithInfoTypeConfig.includecoderegion tags:ai_logic_importsai_logic_redact_helperai_logic_before_generate_contentai_logic_after_generate_contentai_logic_pre_requestai_logic_sensitive_data_allnode --test), ESLint passing, and zero type errors.