Firebase Cloud Functions for AI workflows. This package provides reusable Firebase Functions that can be deployed in any Firebase project to enable AI-powered workflow execution.
- 🤖 AI Workflow Execution: Execute AI workflows with message history and file attachments
- 🔄 Real-time Streaming: Stream AI workflow responses in real-time
- 🚀 Production Ready: Comprehensive error handling and validation
- 📦 TypeScript Support: Full type safety with exported interfaces
- ⚡ Scalable: Optimized for Firebase Cloud Functions v2
npm install @crewdle/ai-firebase-functions firebase-admin firebase-functions// In your Firebase Functions index.ts
import {
runWorkflow,
streamWorkflow
} from '@crewdle/ai-firebase-functions';
// Export functions for deployment
export {
runWorkflow,
streamWorkflow
};For workflow functions, you need to set the Crewdle API key:
firebase functions:secrets:set CREWDLE_API_KEYfirebase deploy --only functionsExecutes AI workflows with message history and optional file attachments.
Request:
interface WorkflowRequest {
workflowId: string;
messages: WorkflowMessage[];
files?: WorkflowFile[];
}
interface WorkflowMessage {
role: 'user' | 'assistant';
content: string;
}
interface WorkflowFile {
name: string;
type: string;
content: string; // base64 encoded
}Usage:
const functions = getFunctions();
const runWorkflow = httpsCallable(functions, 'runWorkflow');
const result = await runWorkflow({
workflowId: 'your-workflow-id',
messages: [
{ role: 'user', content: 'Hello, analyze this data' }
],
files: [
{
name: 'data.txt',
type: 'text/plain',
content: 'base64EncodedContent'
}
]
});Streams AI workflow responses in real-time for better user experience.
Usage:
const streamWorkflow = httpsCallable(functions, 'streamWorkflow');
const result = await streamWorkflow({
workflowId: 'your-workflow-id',
messages: [
{ role: 'user', content: 'Write a long story about AI' }
]
});All functions include comprehensive error handling:
try {
const result = await runWorkflow({
workflowId: 'invalid-id',
messages: []
});
} catch (error) {
console.error('Function error:', error.message);
// Handle specific error cases
}- CREWDLE_API_KEY: Required for workflow functions
firebase functions:secrets:set CREWDLE_API_KEY
Ensure your Firebase project has the following services enabled:
- Cloud Functions
Functions are configured for northamerica-northeast1 region by default. You can modify the region in the function definitions:
export const runWorkflow = onCall({
region: "your-preferred-region",
// ... other options
}, handler);Functions are configured with 300-second timeout for AI operations. Adjust as needed:
export const runWorkflow = onCall({
timeoutSeconds: 600, // 10 minutes
// ... other options
}, handler);-
Install dependencies:
npm install
-
Build the package:
npm run build
-
Run local emulators:
npm run serve
Use Firebase emulators for testing:
firebase emulators:start --only functionsConnect your client app to emulators:
import { connectFunctionsEmulator } from 'firebase/functions';
if (process.env.NODE_ENV === 'development') {
connectFunctionsEmulator(functions, 'localhost', 5001);
}- API Keys: Always use Firebase secrets for sensitive API keys
- Input Validation: All functions include input validation
- Error Handling: Errors are sanitized to avoid information leakage
- CORS: Functions are configured for secure cross-origin requests
Full TypeScript definitions are included:
import {
WorkflowMessage,
WorkflowFile,
WorkflowRequest,
WorkflowResponse
} from '@crewdle/ai-firebase-functions';If you're migrating from existing Firebase Functions:
- Install the package
- Replace individual function implementations with imports
- Update your exports in
index.ts - Redeploy functions
- Functions use Firebase Functions v2 by default
- Requires Node.js 18+ runtime
- Uses Firebase secrets instead of environment variables
MIT © Crewdle
For issues and feature requests, please visit our GitHub repository.