diff --git a/backend/src/build-system/node/backend-requirements-document/index.ts b/backend/src/build-system/node/backend-requirements-document/index.ts new file mode 100644 index 00000000..e0bdc6e8 --- /dev/null +++ b/backend/src/build-system/node/backend-requirements-document/index.ts @@ -0,0 +1,74 @@ +import { BuildHandler, BuildResult } from 'src/build-system/types'; +import { BuilderContext } from 'src/build-system/context'; +import { + generateBackendImplementationPrompt, + generateBackendOverviewPrompt, +} from './prompt'; +import { Logger } from '@nestjs/common'; + +export class BackendRequirementHandler implements BuildHandler { + readonly id = 'op:BACKEND_REQ::STATE:GENERATE'; + + readonly logger: Logger = new Logger('BackendRequirementHandler'); + async run(context: BuilderContext, args: unknown): Promise { + this.logger.log('Generating Backend Requirements Document...'); + + // Validate and extract args + if (!args || typeof args !== 'object') { + throw new Error('Backend configuration is required'); + } + // TODO: init language, framework, packages later in context + const language = context.getData('language') || 'javascript'; + const framework = context.getData('framework') || 'express'; + const packages = context.getData('packages') || {}; + // TODO: adding graphql/restful later + + const { dbRequirements } = args as { + dbRequirements: string; + language: string; + framework: string; + packages: Record; + }; + + const overviewPrompt = generateBackendOverviewPrompt( + context.getData('projectName') || 'Default Project Name', + dbRequirements, + language, + framework, + packages, + ); + + const backendOverview = await context.model.chatSync( + { + content: overviewPrompt, + }, + 'gpt-4o-mini', + ); + + const implementationPrompt = generateBackendImplementationPrompt( + backendOverview, + language, + framework, + ); + + const implementationDetails = await context.model.chatSync( + { + content: implementationPrompt, + }, + 'gpt-4o-mini', + ); + + return { + success: true, + data: { + overview: backendOverview, + implementation: implementationDetails, + config: { + language, + framework, + packages, + }, + }, + }; + } +} diff --git a/backend/src/build-system/node/backend-requirements-document/prompt.ts b/backend/src/build-system/node/backend-requirements-document/prompt.ts new file mode 100644 index 00000000..48e8d71e --- /dev/null +++ b/backend/src/build-system/node/backend-requirements-document/prompt.ts @@ -0,0 +1,169 @@ +// backend-overview-prompt.ts +export const generateBackendOverviewPrompt = ( + projectName: string, + dbRequirements: string, + language: string, + framework: string, + packages: Record, +): string => { + return `You are a Senior Backend Architect specializing in backend systems. Generate the System Overview and API Endpoints specifications based on the following inputs. +. + +### Inputs +Project Name: ${projectName} + +### Technology Stack +- Language: ${language} +- Framework: ${framework} +- Key Packages: +${Object.entries(packages) + .map(([pkg, version]) => ` - ${pkg}@${version}`) + .join('\n')} + +### Database Requirements +${dbRequirements} + +Generate a Backend Overview Document following these guidelines: + +### Instructions and Rules: +1. Design a clear system architecture based on the technology stack +2. Define all necessary API endpoints based on database requirements +3. Follow RESTful or GraphQL conventions as appropriate +4. Consider the relationships between different entities +5. Focus on clean and maintainable API design + +Your reply must start with: "\`\`\`BackendOverview" and end with "\`\`\`". + +Include these sections: + +#### 1. System Overview +- **Project Name**: ${projectName} +- **Technology Stack** + - Core technology choices + - Framework architecture + - Key dependencies and their purposes +- **Architecture Patterns** + - Framework-specific patterns + - Project structure + - Dependency management + - Configuration management + - Service organization + +#### 2. API Endpoints +For each endpoint: +\`\`\` +Route: /api/resource +Method: GET|POST|PUT/DELETE +Purpose: Functional description +Request: + Headers: { + "Authorization": "Bearer {token}" + // Other headers + } + Params: { + // URL parameters + } + Query: { + // Query parameters + } + Body: { + // Request body schema + } +Response: + Success: { + // Success response schema + } + Errors: { + // Error response schemas + } +Required Auth: Yes/No +\`\`\``; +}; + +// backend-implementation-prompt.ts +export const generateBackendImplementationPrompt = ( + backendOverview: string, + language: string, + framework: string, +): string => { + return `You are a Senior Backend Architect specializing in ${language} with ${framework}. Based on the provided Backend Overview, generate detailed implementation requirements for security, error handling, and other technical aspects. + +## Backend Overview: +${backendOverview} + +Generate detailed implementation requirements following these sections: + +Your reply must start with: "\`\`\`BackendImplementation" and end with "\`\`\`". + +#### 3. Implementation Details +For each major component: +- **Request Handlers/Controllers** + - Implementation approach + - Request processing flow + - Response formatting + - Middleware integration + +- **Business Logic Layer/Services** + - Service patterns + - Business rule implementation + - External service integration + - Transaction management + +- **Data Access Layer** + - Database interaction patterns + - Query optimization + - Data mapping strategy + - Cache integration + +- **Middleware Components** + - Authentication middleware + - Validation middleware + - Logging middleware + - Error handling middleware + +#### 4. Security Implementation +- Authentication strategy + - Token management + - Session handling + - Refresh token mechanism +- Authorization rules + - Role-based access control + - Permission management + - Resource ownership +- Input validation + - Request validation + - Data sanitization +- API security + - Rate limiting + - CORS configuration + - Security headers +- Data protection + - Encryption methods + - Secure storage + - PII handling + +#### 5. Error Handling +- Error handling strategy + - Global error handler + - Domain-specific errors + - Operational errors +- Error response format + - Error codes + - Error messages + - Debug information +- Error types and codes + - HTTP status codes + - Application error codes + - Validation errors +- Logging strategy + - Log levels + - Log format + - Log storage + +Focus on: +1. ${language} and ${framework} specific implementation patterns +2. Best practices for each component +3. Security considerations +4. Error handling and logging +5. Performance optimization`; +}; diff --git a/backend/src/build-system/node/ux-datamap/index.ts b/backend/src/build-system/node/ux-datamap/index.ts index 7393a3b3..908ad2f0 100644 --- a/backend/src/build-system/node/ux-datamap/index.ts +++ b/backend/src/build-system/node/ux-datamap/index.ts @@ -12,9 +12,6 @@ export class UXDatamapHandler implements BuildHandler { // extract relevant data from the context const projectName = context.getData('projectName') || 'Default Project Name'; - const uxGoals = context.getData('uxGoals') || 'Default UX Goals'; - - // generate the UX Data Map prompt dynamically const prompt = prompts.generateUXDataMapPrompt( projectName,