Skip to content

Crewdle/ai-firebase-functions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

@crewdle/ai-firebase-functions

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.

Features

  • πŸ€– 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

Installation

npm install @crewdle/ai-firebase-functions firebase-admin firebase-functions

Quick Start

1. Import Functions

// In your Firebase Functions index.ts
import {
  runWorkflow,
  streamWorkflow
} from '@crewdle/ai-firebase-functions';

// Export functions for deployment
export {
  runWorkflow,
  streamWorkflow
};

2. Set Required Environment Variables

For workflow functions, you need to set the Crewdle API key:

firebase functions:secrets:set CREWDLE_API_KEY

3. Deploy Functions

firebase deploy --only functions

Available Functions

runWorkflow

Executes 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'
    }
  ]
});

streamWorkflow

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' }
  ]
});

Error Handling

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
}

Environment Configuration

Required Secrets

  1. CREWDLE_API_KEY: Required for workflow functions
    firebase functions:secrets:set CREWDLE_API_KEY

Firebase Project Setup

Ensure your Firebase project has the following services enabled:

  • Cloud Functions

Advanced Usage

Custom Deployment Regions

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);

Timeout Configuration

Functions are configured with 300-second timeout for AI operations. Adjust as needed:

export const runWorkflow = onCall({
  timeoutSeconds: 600, // 10 minutes
  // ... other options
}, handler);

Development

Local Development

  1. Install dependencies:

    npm install
  2. Build the package:

    npm run build
  3. Run local emulators:

    npm run serve

Testing

Use Firebase emulators for testing:

firebase emulators:start --only functions

Connect your client app to emulators:

import { connectFunctionsEmulator } from 'firebase/functions';

if (process.env.NODE_ENV === 'development') {
  connectFunctionsEmulator(functions, 'localhost', 5001);
}

Security Considerations

  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

TypeScript Support

Full TypeScript definitions are included:

import {
  WorkflowMessage,
  WorkflowFile,
  WorkflowRequest,
  WorkflowResponse
} from '@crewdle/ai-firebase-functions';

Migration Guide

From Existing 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

Breaking Changes

  • Functions use Firebase Functions v2 by default
  • Requires Node.js 18+ runtime
  • Uses Firebase secrets instead of environment variables

License

MIT Β© Crewdle

Support

For issues and feature requests, please visit our GitHub repository.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors