Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(backend): adding backend requirement document #52

Merged
merged 12 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<BuildResult> {
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<string, string>;
};

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,
},
},
};
}
}
169 changes: 169 additions & 0 deletions backend/src/build-system/node/backend-requirements-document/prompt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
// backend-overview-prompt.ts
export const generateBackendOverviewPrompt = (
projectName: string,
dbRequirements: string,
language: string,
framework: string,
packages: Record<string, string>,
): 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`;
};
3 changes: 0 additions & 3 deletions backend/src/build-system/node/ux-datamap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading