Arre uses Firebase Cloud Functions (2nd generation) for serverless backend operations. Functions handle AI processing, external API integrations, and operations that require server-side execution.
if (!request.auth) { throw new HttpsError('unauthenticated', 'User must be logged in.');}
2
Parse File Content
Extracts text based on MIME type:
functions/index.js:26-44
const parseFileContent = async (fileBase64, mimeType) => { const buffer = Buffer.from(fileBase64, 'base64'); if (mimeType === 'application/pdf') { const data = await pdf(buffer); return data.text; } if (mimeType === 'text/csv' || mimeType === 'application/vnd.ms-excel') { const records = parse(buffer.toString('utf-8'), { columns: true, skip_empty_lines: true }); return JSON.stringify(records, null, 2); } // Fallback for text files return buffer.toString('utf-8');};
3
AI Prompt Engineering
Constructs prompt for Gemini AI:
functions/index.js:72-83
const systemPrompt = ` You are an expert productivity assistant for the "Arre" app. Your goal is to extract actionable tasks from the provided document content. Rules: 1. Return ONLY a valid JSON array of strings. No markdown, no explanations. 2. Each string must be a clear, actionable task title. 3. If specific instructions are provided, prioritize them. 4. Limit to the top 10 most important tasks if the document is large. User Instructions: ${instructions || "Extract all tasks."}`;
4
Call Gemini API
Sends request to Google Gemini 2.5 Flash:
functions/index.js:86-92
const model = getGeminiModel(geminiApiKey.value());const result = await model.generateContent([ systemPrompt, { text: `Document Content:\n${extractedText.substring(0, 30000)}` }]);const response = result.response;const text = response.text();
5
Parse and Validate Response
Cleans AI response and validates JSON:
functions/index.js:98-104
const jsonString = text.replace(/```json/g, '').replace(/```/g, '').trim();const tasks = JSON.parse(jsonString);if (!Array.isArray(tasks)) { throw new Error("AI did not return an array.");}return { tasks };
Markdown-formatted briefing with overview, today’s tasks, and upcoming priorities.
Good morning! You have 5 tasks on your plate today.**Today's Focus:**- Complete the Q3 report for your Work project- Schedule team meeting- Review code changes**Tomorrow:**Keep an eye on the project deadline approaching on Friday.
Check authentication and validate all input parameters before processing:
if (!request.auth) { throw new HttpsError('unauthenticated', 'User must be logged in.');}if (!request.data.requiredField) { throw new HttpsError('invalid-argument', 'Missing required field.');}
2
Use Structured Error Handling
Return HttpsError for client-friendly error messages:
try { // Function logic} catch (error) { console.error('Function Error:', error); throw new HttpsError('internal', 'Operation failed.', error.message);}
3
Set Appropriate Timeouts
AI operations need longer timeouts (60s), while API calls can use 30s.
4
Log for Debugging
Use console.log and console.error - logs appear in Cloud Functions dashboard.