@invect/nestjs
v0.1.3
Published
A NestJS module for executing Invect workflows with batch processing capabilities
Downloads
572
Maintainers
Readme
invect Backend
@invect/nestjs
A NestJS module that provides Invect workflow execution capabilities as a thin wrapper over the core Invect engine.
Installation
npm install @invect/nestjs @invect/coreUsage
Basic Usage
Import the InvectModule in your application module:
import { Module } from '@nestjs/common';
import { InvectModule } from '@invect/nestjs';
@Module({
imports: [
InvectModule.forRoot({
// Invect configuration
database: {
type: 'sqlite',
url: 'file:./dev.db'
},
execution: {
maxConcurrentFlows: 10,
maxConcurrentNodes: 50
},
logging: {
level: 'info'
}
})
],
controllers: [],
providers: [],
})
export class AppModule {}Async Configuration
For dynamic configuration (e.g., from environment variables or config service):
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { InvectModule } from '@invect/nestjs';
@Module({
imports: [
ConfigModule.forRoot(),
InvectModule.forRootAsync({
useFactory: (configService: ConfigService) => ({
database: {
type: 'postgres',
url: configService.get('DATABASE_URL')
},
execution: {
maxConcurrentFlows: configService.get('MAX_CONCURRENT_FLOWS', 10),
maxConcurrentNodes: configService.get('MAX_CONCURRENT_NODES', 50)
},
logging: {
level: configService.get('LOG_LEVEL', 'info')
}
}),
inject: [ConfigService],
}),
],
})
export class AppModule {}Using the Invect Service
If you need to access the Invect core instance programmatically in your own services:
import { Injectable } from '@nestjs/common';
import { InvectService } from '@invect/nestjs';
@Injectable()
export class MyService {
constructor(private readonly invectService: InvectService) {}
async executeMyFlow() {
const core = this.invectService.getCore();
// Use the core Invect instance
const flows = await core.listFlows();
return flows;
}
}API Endpoints
The module automatically provides a REST API with the following endpoints:
Flow Management
GET /flows- List flows with optional filtering and paginationPOST /flows- Create a new flowGET /flows/:id- Get flow by IDPUT /flows/:id- Update flow (not yet implemented)DELETE /flows/:id- Delete flow (not yet implemented)POST /validate-flow- Validate flow definition
Flow Version Management
GET /flows/:id/versions- Get flow versionsPOST /flows/:id/versions- Create flow version
Flow Run Execution
POST /flows/:flowId/run- Start flow executionGET /flow-runs- Get all flow runsGET /flow-runs/:flowRunId- Get specific flow runGET /flows/:flowId/flow-runs- Get flow runs for a specific flowPOST /flow-runs/:flowRunId/resume- Resume paused flow executionPOST /flow-runs/:flowRunId/cancel- Cancel flow execution (not yet implemented)POST /flow-runs/:flowRunId/pause- Pause flow execution (not yet implemented)
Node Execution
GET /flow-runs/:flowRunId/node-executions- Get node executions for a flow runGET /node-executions- Get all node executions
Node Data & Testing
POST /node-data/sql-query- Execute SQL query for testingPOST /node-data/jq-query- Execute JQ query for testingPOST /node-data/model-query- Test model promptGET /node-data/models- Get available AI modelsGET /node-data/databases- Get available databases
Custom Route Prefix
To add a custom route prefix for all Invect endpoints, modify the controller registration:
@Controller('api/v1/invect')
export class CustomInvectController extends InvectController {}Features
- Functionally identical to Express package: Same API endpoints and behavior
- Dependency injection: Invect core instance is properly injected
- Async configuration: Support for dynamic configuration
- Service access: Direct access to Invect core through
InvectService - Error handling: Proper NestJS exception handling
- TypeScript support: Full type safety
License
MIT
Installation
npm install @robase/@invect/nestjsUsage
Import and configure the invectModule in your NestJS application:
import { Module } from '@nestjs/common';
import { invectModule, InvectConfig } from '@robase/@invect/nestjs';
const config: InvectConfig = {
openAIKey: process.env.OPENAI_API_KEY,
anthropicKey: process.env.ANTHROPIC_API_KEY,
modelId: 'claude-3-sonnet-20240229',
databaseType: 'sqlite',
databaseConnectionString: 'file:./dev.db',
// Database for SQL query nodes to execute user queries
DEFAULT_SQL_NODE_DB_CONNECTION_STRING: 'postgresql://user:password@localhost:5432/userdata',
// Optional: Named databases for specific SQL query nodes
sqlQueryDatabases: {
'analytics': 'postgresql://user:password@localhost:5432/analytics',
'reporting': 'postgresql://user:password@localhost:5432/reporting',
'warehouse': 'postgresql://user:password@localhost:5432/warehouse'
}
};
@Module({
imports: [
invectModule.forRoot(config)
],
})
export class AppModule {}Configuration
The InvectConfig interface accepts the following options:
openAIKey?: string- OpenAI API key (optional if anthropicKey is provided)anthropicKey?: string- Anthropic API key (optional if openAIKey is provided)modelId: string- Model identifier to use for text generationdatabaseType: 'sqlite' | 'postgresql' | 'mysql'- Database typedatabaseConnectionString: string- Database connection string for Invect application dataDEFAULT_SQL_NODE_DB_CONNECTION_STRING: string- Database connection string for SQL query nodes (required)sqlQueryDatabases?: Record<string, string>- Named database connections for SQL query nodes (optional)
At least one of openAIKey or anthropicKey must be provided.
Database Configuration
Invect uses two separate database connections:
- Application Database (
databaseConnectionString): Stores Invect's internal data including flows, executions, and traces - SQL Query Node Database (
DEFAULT_SQL_NODE_DB_CONNECTION_STRING): Default database for SQL query nodes to execute user queries against. If not provided, SQL query nodes will fall back to using the environment variableSQL_CONFIG
Using Named Databases
SQL query nodes can specify which database to use via the database_id parameter:
- Default behavior: If no
database_idis specified, the SQL query node usesDEFAULT_SQL_NODE_DB_CONNECTION_STRING - Named database: If
database_idis specified (e.g., 'analytics'), the node uses the corresponding connection string fromsqlQueryDatabases
The frontend provides a dropdown in the SQL query node editor to select from available databases.
Example SQL query node configuration:
{
"type": "sqlQueryNode",
"data": {
"query": "SELECT * FROM users WHERE created_at > NOW() - INTERVAL '7 days'",
"database_type": "postgresql",
"database_id": "analytics"
}
}Features
- Flow Management: Create, update, and manage Invect workflows
- Batch Processing: Execute workflows with optimized batch processing for AI APIs
- Multiple Node Types: Support for template, model, SQL query, and conditional nodes
- Database Support: Compatible with SQLite, PostgreSQL, and MySQL
- Execution Tracking: Full execution history and tracing
- Pause/Resume: Control flow execution with pause and resume capabilities
API Endpoints
Once imported, the module provides the following REST endpoints:
GET /api/flows- List all flowsPOST /api/flows- Create a new flowGET /api/flows/:id- Get flow detailsPOST /api/flows/:id/versions- Create new flow versionPOST /api/executions- Execute a flowGET /api/executions/:id- Get execution details
Database Setup
The module uses Prisma for database operations. Make sure to run migrations after installation:
npx prisma migrate deployLicense
MIT
