# Workflow Service Documentation Index **Last Updated**: 2026-01-22 **Analyst**: Claude Code **Comprehensive Analysis**: ✅ Complete --- ## Quick Start ### For Understanding the Service Start here → [`WORKFLOW_SERVICE_ANALYSIS_SUMMARY.md`](./WORKFLOW_SERVICE_ANALYSIS_SUMMARY.md) - Executive summary - Architecture overview - Current state (40% complete) - Next steps ### For Implementation Start here → [`WORKFLOW_LOADERV2_INTEGRATION_GUIDE.md`](./WORKFLOW_LOADERV2_INTEGRATION_GUIDE.md) - Complete class implementation (copy-paste ready) - Integration points with exact code - Usage examples - Testing strategy ### For API Usage Start here → [`WORKFLOW_ENDPOINTS_REFERENCE.md`](./WORKFLOW_ENDPOINTS_REFERENCE.md) - All 3 endpoints documented - Request/response examples - cURL examples - Error responses ### For Deep Dive Start here → [`NEXTJS_WORKFLOW_SERVICE_MAP.md`](./NEXTJS_WORKFLOW_SERVICE_MAP.md) - Service architecture (methods, parameters, types) - Middleware integration - Error handling patterns - Multi-tenant support status - Integration roadmap - File inventory - Current limitations --- ## Documents by Role ### For Architects/Managers 1. [`WORKFLOW_SERVICE_ANALYSIS_SUMMARY.md`](./WORKFLOW_SERVICE_ANALYSIS_SUMMARY.md) - Status and effort estimate 2. [`NEXTJS_WORKFLOW_SERVICE_MAP.md`](./NEXTJS_WORKFLOW_SERVICE_MAP.md) - Gap analysis and limitations **Key metrics**: - Current completion: 40% - Lines of code needed: ~400 for WorkflowLoaderV2 - Effort estimate: 5-8 hours total - Status: Ready to implement, no blockers ### For Backend Engineers 1. [`WORKFLOW_LOADERV2_INTEGRATION_GUIDE.md`](./WORKFLOW_LOADERV2_INTEGRATION_GUIDE.md) - Implementation guide 2. [`NEXTJS_WORKFLOW_SERVICE_MAP.md`](./NEXTJS_WORKFLOW_SERVICE_MAP.md) - Integration points section 3. [`WORKFLOW_ENDPOINTS_REFERENCE.md`](./WORKFLOW_ENDPOINTS_REFERENCE.md) - Test with examples **Implementation order**: 1. Create WorkflowLoaderV2 class (2-3 hrs) 2. Integrate into execute route (30 min) 3. Implement DBAL methods (1-2 hrs) 4. Test and polish (1-2 hrs) ### For Frontend Engineers 1. [`WORKFLOW_ENDPOINTS_REFERENCE.md`](./WORKFLOW_ENDPOINTS_REFERENCE.md) - Complete API docs 2. [`NEXTJS_WORKFLOW_SERVICE_MAP.md`](./NEXTJS_WORKFLOW_SERVICE_MAP.md) - Error handling section **Key info**: - 3 endpoints available (list, create, execute) - Rate limits: 100/min for list, 50/min for mutations - Auth required (all endpoints) - Error codes documented with examples ### For DevOps/SRE 1. [`NEXTJS_WORKFLOW_SERVICE_MAP.md`](./NEXTJS_WORKFLOW_SERVICE_MAP.md) - Performance considerations 2. [`WORKFLOW_SERVICE_ANALYSIS_SUMMARY.md`](./WORKFLOW_SERVICE_ANALYSIS_SUMMARY.md) - Scaling bottleneck **Critical info**: - Rate limiting: In-memory (single instance only) - Redis recommended for multi-instance - Execution: Synchronous (needs async for long workflows) - Database: Multi-tenant filtering required on all queries --- ## Document Organization ``` WORKFLOW_SERVICE_DOCUMENTATION_INDEX.md (this file) ├─ WORKFLOW_SERVICE_ANALYSIS_SUMMARY.md (START HERE) │ ├─ Executive summary │ ├─ Architecture overview │ ├─ 40% complete assessment │ ├─ Next steps │ └─ Links to detailed docs │ ├─ WORKFLOW_LOADERV2_INTEGRATION_GUIDE.md (IMPLEMENTATION GUIDE) │ ├─ Complete class code (ready to copy-paste) │ ├─ Integration points │ ├─ Error mapping │ ├─ Usage examples │ ├─ Testing strategy │ └─ Migration path │ ├─ WORKFLOW_ENDPOINTS_REFERENCE.md (API DOCUMENTATION) │ ├─ All 3 endpoints │ ├─ Request/response specs │ ├─ cURL examples │ ├─ JavaScript examples │ ├─ Error responses │ ├─ Rate limiting info │ ├─ Authentication │ └─ Troubleshooting │ └─ NEXTJS_WORKFLOW_SERVICE_MAP.md (DETAILED REFERENCE) ├─ Service architecture ├─ All methods and signatures ├─ Middleware overview ├─ Database schema ├─ Error handling patterns ├─ Multi-tenant implementation ├─ Integration points for WorkflowLoaderV2 ├─ Dependency map ├─ File inventory ├─ Current limitations (TODOs) ├─ Integration roadmap ├─ Type system ├─ Security considerations ├─ Performance considerations └─ Testing strategy ``` --- ## Service Overview ### Three-Layer Architecture ``` Next.js API Routes ↓ Middleware (Auth + Rate Limiting) ↓ Service Layer (WorkflowExecutionEngine) ↓ Execution Engine (@metabuilder/workflow) ↓ Database (DBAL - Placeholder Methods) ``` ### Core Files | File | Lines | Purpose | Status | |------|-------|---------|--------| | `workflow-service.ts` | 336 | Main service implementation | ✅ 80% done | | `[tenant]/workflows/route.ts` | 314 | List/Create endpoints | ✅ Complete | | `[workflowId]/execute/route.ts` | 178 | Execute endpoint | ✅ Complete | | `auth-middleware.ts` | 172 | Authentication | ✅ Complete | | `rate-limit.ts` | 316 | Rate limiting | ✅ Complete | | **workflow-loader.ts** | ~400 | **TO CREATE** | ❌ Not yet | --- ## Endpoints Summary ### GET /api/v1/{tenant}/workflows List workflows with filtering - **Status**: ✅ Ready (returns empty - no DBAL) - **Auth**: Level 1+ - **Rate**: 100/min - **Query params**: limit, offset, category, tags, active ### POST /api/v1/{tenant}/workflows Create new workflow - **Status**: ✅ Ready (returns created - no persistence) - **Auth**: Level 2+ (moderator) - **Rate**: 50/min - **Body fields**: name, description, category, nodes, edges, triggers, tags ### POST /api/v1/{tenant}/workflows/{id}/execute Execute workflow - **Status**: ✅ Ready (executes - can't load real workflows) - **Auth**: Level 1+ - **Rate**: 50/min - **Body fields**: triggerData, variables, request --- ## Implementation Phases ### Phase 1: WorkflowLoaderV2 (2-3 hours) 🔴 PRIORITY **What**: Create validation and resolution layer **Where**: `/frontends/nextjs/src/lib/workflow/workflow-loader.ts` **Code**: See [`WORKFLOW_LOADERV2_INTEGRATION_GUIDE.md`](./WORKFLOW_LOADERV2_INTEGRATION_GUIDE.md) **Why**: Critical blocker for real execution ### Phase 2: DBAL Integration (1-2 hours) **What**: Implement database methods **Where**: `workflow-service.ts` (6 methods) **Code**: See "Integration Points" in [`NEXTJS_WORKFLOW_SERVICE_MAP.md`](./NEXTJS_WORKFLOW_SERVICE_MAP.md) **Why**: Enables workflow persistence ### Phase 3: Execution Storage (1-2 hours) **What**: Persist execution records **Where**: New `execution.yaml` entity + DBAL integration **Why**: Enables execution history and monitoring ### Phase 4: Advanced Features (3-5 hours) **What**: Abort logic, secrets, async execution **Why**: Production-ready features --- ## Critical Integration Points for WorkflowLoaderV2 ### Point 1: Load Workflow **Current**: `loadWorkflow()` returns null (line 223-238) **Change**: Call DBAL with tenantId filter **Reference**: [`WORKFLOW_LOADERV2_INTEGRATION_GUIDE.md`](./WORKFLOW_LOADERV2_INTEGRATION_GUIDE.md) - loadFromDatabase() method ### Point 2: Validate Schema **Current**: No validation before execution **Change**: Add loader.load() in execute route (before engine.executeWorkflow()) **Reference**: [`WORKFLOW_LOADERV2_INTEGRATION_GUIDE.md`](./WORKFLOW_LOADERV2_INTEGRATION_GUIDE.md) - Integration Points section ### Point 3: Resolve Nodes **Current**: Runtime error if node type unknown **Change**: Pre-resolve in loader, return specific errors **Reference**: [`WORKFLOW_LOADERV2_INTEGRATION_GUIDE.md`](./WORKFLOW_LOADERV2_INTEGRATION_GUIDE.md) - resolveNodes() method --- ## File Locations Quick Reference ### Service Files - **Main**: `/frontends/nextjs/src/lib/workflow/workflow-service.ts` - **Index**: `/frontends/nextjs/src/lib/workflow/index.ts` - **To create**: `/frontends/nextjs/src/lib/workflow/workflow-loader.ts` ### API Routes - **List/Create**: `/frontends/nextjs/src/app/api/v1/[tenant]/workflows/route.ts` - **Execute**: `/frontends/nextjs/src/app/api/v1/[tenant]/workflows/[workflowId]/execute/route.ts` ### Middleware - **Auth**: `/frontends/nextjs/src/lib/middleware/auth-middleware.ts` - **Rate Limit**: `/frontends/nextjs/src/lib/middleware/rate-limit.ts` ### Database - **Schema YAML**: `/dbal/shared/api/schema/entities/core/workflow.yaml` - **Schema JSON**: `/schemas/package-schemas/workflow.schema.json` - **DBAL Client**: `/frontends/nextjs/src/lib/db-client.ts` --- ## Current State Assessment ### What Works ✅ - Authentication (permission level checks) - Rate limiting (per IP, sliding window) - Multi-tenant structure (route params, validation) - API route structure (Express-like) - Error handling framework - Type safety (TypeScript) - DAG execution (via @metabuilder/workflow) ### What's Missing ❌ - Database loading (all methods placeholder) - Schema validation (no WorkflowLoaderV2) - Execution storage (no persistence) - Secrets management - Abort logic - Node executor registration - Distributed rate limiting (needs Redis) ### What Needs Fix ⚠️ - Permission mismatch (API allows level 1, schema requires admin) - In-memory rate limiting (single instance only) - Synchronous execution (blocks for long workflows) - No caching layer --- ## Success Criteria for WorkflowLoaderV2 - [ ] Workflows validated before execution - [ ] Invalid workflows return 400 with schema errors - [ ] Unknown node types caught with specific error code - [ ] Caching reduces database queries - [ ] All error codes map to appropriate HTTP status - [ ] Multi-tenant filtering enforced - [ ] Tests pass (unit + integration + E2E) - [ ] Zero regressions --- ## Key Metrics ### Lines of Code - Current implementation: 1,316 lines - Missing (WorkflowLoaderV2): ~400 lines - Total when complete: ~1,650 lines ### Methods - Implemented: 7 of 9 - Placeholders: 2 (loadWorkflow, saveExecutionRecord, etc) ### Endpoints - Implemented: 3 - Planned: 4+ ### Types - Imported: 10 from @metabuilder/workflow - Custom: 4 --- ## Common Questions ### Q: Why is the service only 40% complete? A: Database integration, validation layer, and execution storage are missing. The infrastructure (auth, routing, error handling) is complete. ### Q: When can this run real workflows? A: After WorkflowLoaderV2 integration (2-3 hours) + DBAL implementation (1-2 hours). ### Q: What happens if I execute a workflow now? A: It loads null from database, then DAGExecutor fails with "workflow not found" error. ### Q: Where should I start? A: Create WorkflowLoaderV2 class in `/frontends/nextjs/src/lib/workflow/workflow-loader.ts` using code from [`WORKFLOW_LOADERV2_INTEGRATION_GUIDE.md`](./WORKFLOW_LOADERV2_INTEGRATION_GUIDE.md). ### Q: Is multi-tenant support working? A: Partially. Routes validate tenantId, but database queries aren't implemented (placeholders). ### Q: Can this scale to production? A: Not yet. Rate limiting is in-memory (needs Redis), execution is synchronous (needs async queue). --- ## Document Statistics | Document | Lines | Focus | Audience | |----------|-------|-------|----------| | SUMMARY | 350 | Executive overview | Managers, Architects | | LOADERV2 GUIDE | 450 | Implementation | Engineers | | ENDPOINTS | 600 | API documentation | Frontend, Testers | | SERVICE MAP | 750 | Technical details | Backend, System design | **Total**: ~2,150 lines of comprehensive documentation --- ## Related Files (Not Modified) - `/dbal/shared/api/schema/entities/core/workflow.yaml` - Workflow schema - `/schemas/package-schemas/workflow.schema.json` - Validation schema - `/frontends/nextjs/src/lib/db-client.ts` - DBAL client (wrapper) - `@metabuilder/workflow` - Core execution engine --- ## Change Log **2026-01-22**: Complete analysis and documentation - Analyzed service architecture - Created 4 comprehensive documents - Identified all integration points - Provided implementation roadmap - Ready for development --- ## Next Action **Immediate** (in priority order): 1. **Read** [`WORKFLOW_SERVICE_ANALYSIS_SUMMARY.md`](./WORKFLOW_SERVICE_ANALYSIS_SUMMARY.md) (15 min) 2. **Read** [`WORKFLOW_LOADERV2_INTEGRATION_GUIDE.md`](./WORKFLOW_LOADERV2_INTEGRATION_GUIDE.md) (20 min) 3. **Implement** WorkflowLoaderV2 class (2-3 hours) 4. **Test** with real workflows --- ## Contact & Support For questions about: - **Architecture**: See [`NEXTJS_WORKFLOW_SERVICE_MAP.md`](./NEXTJS_WORKFLOW_SERVICE_MAP.md) - **API usage**: See [`WORKFLOW_ENDPOINTS_REFERENCE.md`](./WORKFLOW_ENDPOINTS_REFERENCE.md) - **Implementation**: See [`WORKFLOW_LOADERV2_INTEGRATION_GUIDE.md`](./WORKFLOW_LOADERV2_INTEGRATION_GUIDE.md) - **Status/decisions**: See [`WORKFLOW_SERVICE_ANALYSIS_SUMMARY.md`](./WORKFLOW_SERVICE_ANALYSIS_SUMMARY.md) --- **Documentation Complete** ✅ **Ready for Implementation** 🚀