Firebase Cloud Functions for AI workflows for Crewdle applications
npm install @crewdle/ai-firebase-functionsFirebase 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
``bash`
npm install @crewdle/ai-firebase-functions firebase-admin firebase-functions
`typescript
// 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:
`bash`
firebase functions:secrets:set CREWDLE_API_KEY
`bash`
firebase deploy --only functions
#### runWorkflow
Executes AI workflows with message history and optional file attachments.
Request:
`typescript
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:
`typescript
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'
}
]
});
`
#### streamWorkflow
Streams AI workflow responses in real-time for better user experience.
Usage:
`typescript
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:
`typescript`
try {
const result = await runWorkflow({
workflowId: 'invalid-id',
messages: []
});
} catch (error) {
console.error('Function error:', error.message);
// Handle specific error cases
}
1. CREWDLE_API_KEY: Required for workflow functions
`bash`
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:
`typescript`
export const runWorkflow = onCall({
region: "your-preferred-region",
// ... other options
}, handler);
Functions are configured with 300-second timeout for AI operations. Adjust as needed:
`typescript`
export const runWorkflow = onCall({
timeoutSeconds: 600, // 10 minutes
// ... other options
}, handler);
1. Install dependencies:
`bash`
npm install
2. Build the package:
`bash`
npm run build
3. Run local emulators:
`bash`
npm run serve
Use Firebase emulators for testing:
`bash`
firebase emulators:start --only functions
Connect your client app to emulators:
`typescript
import { connectFunctionsEmulator } from 'firebase/functions';
if (process.env.NODE_ENV === 'development') {
connectFunctionsEmulator(functions, 'localhost', 5001);
}
`
1. API Keys: Always use Firebase secrets for sensitive API keys
2. Input Validation: All functions include input validation
3. Error Handling: Errors are sanitized to avoid information leakage
4. CORS: Functions are configured for secure cross-origin requests
Full TypeScript definitions are included:
`typescript`
import {
WorkflowMessage,
WorkflowFile,
WorkflowRequest,
WorkflowResponse
} from '@crewdle/ai-firebase-functions';
If you're migrating from existing Firebase Functions:
1. Install the package
2. Replace individual function implementations with imports
3. Update your exports in index.ts`
4. 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.