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: adding detail log in each handler #90

Merged
merged 13 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 9 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
4 changes: 3 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,23 @@
"@types/fs-extra": "^11.0.4",
"@types/normalize-path": "^3.0.2",
"@types/toposort": "^2.0.7",
"toposort": "^2.0.2",
"axios": "^1.7.7",
"bcrypt": "^5.1.1",
"class-validator": "^0.14.1",
"fs-extra": "^11.2.0",
"gpt-3-encoder": "^1.1.4",
NarwhalChen marked this conversation as resolved.
Show resolved Hide resolved
"graphql": "^16.9.0",
"graphql-subscriptions": "^2.0.0",
"graphql-ws": "^5.16.0",
"lodash": "^4.17.21",
"markdown-to-txt": "^2.0.1",
"normalize-path": "^3.0.0",
"openai": "^4.77.0",
NarwhalChen marked this conversation as resolved.
Show resolved Hide resolved
"reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1",
"sqlite3": "^5.1.7",
"subscriptions-transport-ws": "^0.11.0",
"toposort": "^2.0.2",
"typeorm": "^0.3.20",
"uuid": "^10.0.0"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BuildMonitor } from '../monitor';
import { BuildSequence } from '../types';
import { executeBuildSequence, objectToMarkdown, writeToFile } from './utils';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import {
parseGenerateTag,
removeCodeBlockFences,
} from 'src/build-system/utils/strings';
import { BuildMonitor } from 'src/build-system/monitor';
import { chatSyncWithClocker } from 'src/build-system/utils/handler-helper';
import { MessageInterface } from 'src/common/model-provider/types';

/**
* BackendCodeHandler is responsible for generating the backend codebase
Expand Down Expand Up @@ -63,10 +66,8 @@ export class BackendCodeHandler implements BuildHandler<string> {

try {
// Invoke the language model to generate the backend code
const modelResponse = await context.model.chatSync({
model: 'gpt-4o-mini',
messages: [{ content: backendCodePrompt, role: 'system' }],
});
let messages: MessageInterface[] = [{content: backendCodePrompt, role: 'system'}];
const modelResponse = await chatSyncWithClocker(context, messages, 'gpt-4o-mini', 'generateBackendCode', this.id);

const generatedCode = formatResponse(modelResponse);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import * as path from 'path';

import { prompts } from './prompt';
import { formatResponse } from 'src/build-system/utils/strings';
import { BuildMonitor } from 'src/build-system/monitor';
import { MessageInterface } from 'src/common/model-provider/types';
import { chatSyncWithClocker } from 'src/build-system/utils/handler-helper';

// TODO(Sma1lboy): we need a better way to handle handler pre requirements
/**
Expand Down Expand Up @@ -48,10 +51,8 @@ export class BackendFileReviewHandler implements BuildHandler<string> {
backendCode,
);

const modelResponse = await context.model.chatSync({
model: 'gpt-4o-mini',
messages: [{ content: filePrompt, role: 'system' }],
});
let messages: MessageInterface[] = [{content: filePrompt, role: 'system'}];
const modelResponse = await chatSyncWithClocker(context, messages, 'gpt-4o-mini', 'generateBackendCode', this.id);

const filesToModify = this.parseFileIdentificationResponse(modelResponse);
this.logger.log(`Files to modify: ${filesToModify.join(', ')}`);
Expand All @@ -72,11 +73,10 @@ export class BackendFileReviewHandler implements BuildHandler<string> {
backendCode,
);

const startTime = new Date();
// Get modified content
const response = await context.model.chatSync({
model: 'gpt-4o-mini',
messages: [{ content: modificationPrompt, role: 'system' }],
});
let messages: MessageInterface[] = [{content: modificationPrompt, role: 'system'}];
const response = await chatSyncWithClocker(context, messages, 'gpt-4o-mini', 'generateFileModification', this.id);

// Extract new content and write back
const newContent = formatResponse(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import {
} from './prompt';
import { Logger } from '@nestjs/common';
import { removeCodeBlockFences } from 'src/build-system/utils/strings';
import { BuildMonitor } from 'src/build-system/monitor';
import { chatSyncWithClocker } from 'src/build-system/utils/handler-helper';
import { MessageInterface } from 'src/common/model-provider/types';

type BackendRequirementResult = {
overview: string;
Expand Down Expand Up @@ -54,10 +57,8 @@ export class BackendRequirementHandler

let backendOverview: string;
try {
backendOverview = await context.model.chatSync({
model: 'gpt-4o-mini',
messages: [{ content: overviewPrompt, role: 'system' }],
});
let messages: MessageInterface[] = [{content: overviewPrompt, role: 'system'}];
backendOverview = await chatSyncWithClocker(context, messages, 'gpt-4o-mini', 'generateBackendOverviewPrompt', this.id);
} catch (error) {
this.logger.error('Error generating backend overview:', error);
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { ModelProvider } from 'src/common/model-provider';
import { prompts } from './prompt';
import { Logger } from '@nestjs/common';
import { removeCodeBlockFences } from 'src/build-system/utils/strings';
import { BuildMonitor } from 'src/build-system/monitor';
import { chatSyncWithClocker } from 'src/build-system/utils/handler-helper';
import { MessageInterface } from 'src/common/model-provider/types';

export class DatabaseRequirementHandler implements BuildHandler<string> {
readonly id = 'op:DATABASE_REQ';
Expand All @@ -19,11 +22,9 @@ export class DatabaseRequirementHandler implements BuildHandler<string> {
projectName,
datamapDoc,
);
const model = ModelProvider.getInstance();
const dbRequirementsContent = await model.chatSync({
model: 'gpt-4o-mini',
messages: [{ content: prompt, role: 'system' }],
});
let messages: MessageInterface[] = [{content: prompt, role: 'system'}];
const dbRequirementsContent = await chatSyncWithClocker(context, messages, 'gpt-4o-mini', 'generateDatabaseRequirementPrompt', this.id);

return {
success: true,
data: removeCodeBlockFences(dbRequirementsContent),
Expand Down
21 changes: 9 additions & 12 deletions backend/src/build-system/handlers/database/schemas/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import { prompts } from './prompt';
import { saveGeneratedCode } from 'src/build-system/utils/files';
import * as path from 'path';
import { formatResponse } from 'src/build-system/utils/strings';
import { BuildMonitor } from 'src/build-system/monitor';
import { chatSyncWithClocker } from 'src/build-system/utils/handler-helper';
import { MessageInterface } from 'src/common/model-provider/types';

/**
* DBSchemaHandler is responsible for generating database schemas based on provided requirements.
Expand Down Expand Up @@ -70,10 +73,8 @@ export class DBSchemaHandler implements BuildHandler {

let dbAnalysis: string;
try {
const analysisResponse = await context.model.chatSync({
model: 'gpt-4o-mini',
messages: [{ content: analysisPrompt, role: 'system' }],
});
let messages: MessageInterface[] = [{content: analysisPrompt, role: 'system'}];
const analysisResponse = await chatSyncWithClocker(context, messages, 'gpt-4o-mini', 'analyzeDatabaseRequirements', this.id);
dbAnalysis = analysisResponse;
} catch (error) {
this.logger.error('Error during database requirements analysis:', error);
Expand Down Expand Up @@ -103,10 +104,8 @@ export class DBSchemaHandler implements BuildHandler {

let schemaContent: string;
try {
const schemaResponse = await context.model.chatSync({
model: 'gpt-4o-mini',
messages: [{ content: schemaPrompt, role: 'system' }],
});
let messages: MessageInterface[] = [{content: schemaPrompt, role: 'system'}];
const schemaResponse = await chatSyncWithClocker(context, messages, 'gpt-4o-mini', 'generateBackendCode', this.id);
schemaContent = formatResponse(schemaResponse);
} catch (error) {
this.logger.error('Error during schema generation:', error);
Expand All @@ -126,10 +125,8 @@ export class DBSchemaHandler implements BuildHandler {

let validationResponse: string;
try {
const validationResult = await context.model.chatSync({
model: 'gpt-4o-mini',
messages: [{ content: validationPrompt, role: 'system' }],
});
let messages: MessageInterface[] = [{content: validationPrompt, role: 'system'}];
const validationResult = await chatSyncWithClocker(context, messages, 'gpt-4o-mini', 'generateBackendCode', this.id);
validationResponse = formatResponse(validationResult);
} catch (error) {
this.logger.error('Error during schema validation:', error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import {
formatResponse,
parseGenerateTag,
} from 'src/build-system/utils/strings';
import { BuildMonitor } from 'src/build-system/monitor';
import { chatSyncWithClocker } from 'src/build-system/utils/handler-helper';
import { MessageInterface } from 'src/common/model-provider/types';

export class FileArchGenerateHandler implements BuildHandler<string> {
readonly id = 'op:FILE:ARCH';
Expand Down Expand Up @@ -56,11 +59,9 @@ export class FileArchGenerateHandler implements BuildHandler<string> {
};
}
try {
fileArchContent = await context.model.chatSync({
model: 'gpt-4o-mini',
messages: [{ content: prompt, role: 'system' }],
});

let messages: MessageInterface[] = [{content: prompt, role: 'system'}];
fileArchContent = await chatSyncWithClocker(context, messages, 'gpt-4o-mini', 'generateFileArch', this.id);

const tagContent = parseGenerateTag(fileArchContent);
jsonData = extractJsonFromText(tagContent);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { BuilderContext } from 'src/build-system/context';
import { prompts } from './prompt';
import { Logger } from '@nestjs/common';
import { removeCodeBlockFences } from 'src/build-system/utils/strings';
import { BuildMonitor } from 'src/build-system/monitor';
import { chatSyncWithClocker } from 'src/build-system/utils/handler-helper';
import { MessageInterface } from 'src/common/model-provider/types';

/**
* FileStructureHandler is responsible for generating the project's file and folder structure
Expand Down Expand Up @@ -84,10 +87,8 @@ export class FileStructureHandler implements BuildHandler<FileStructOutput> {
let fileStructureContent: string;
try {
// Invoke the language model to generate the file structure content
fileStructureContent = await context.model.chatSync({
model: 'gpt-4o-mini',
messages: [{ content: prompt, role: 'system' }],
});
let messages: MessageInterface[] = [{content: prompt, role: 'system'}];
fileStructureContent = await chatSyncWithClocker(context, messages, 'gpt-4o-mini', 'generateCommonFileStructure', this.id);
} catch (error) {
this.logger.error('Error during file structure generation:', error);
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { prompts } from './prompt';
import { ModelProvider } from 'src/common/model-provider';
import { Logger } from '@nestjs/common';
import { removeCodeBlockFences } from 'src/build-system/utils/strings';
import { BuildMonitor } from 'src/build-system/monitor';
import { chatSyncWithClocker } from 'src/build-system/utils/handler-helper';
import { MessageInterface } from 'src/common/model-provider/types';

export class PRDHandler implements BuildHandler {
readonly id = 'op:PRD';
Expand All @@ -26,20 +29,19 @@ export class PRDHandler implements BuildHandler {
);

// Send the prompt to the LLM server and process the response
const prdContent = await this.generatePRDFromLLM(prompt);
const prdContent = await this.generatePRDFromLLM(context, prompt);

return {
success: true,
data: removeCodeBlockFences(prdContent),
};
}

private async generatePRDFromLLM(prompt: string): Promise<string> {
private async generatePRDFromLLM(context: BuilderContext, prompt: string): Promise<string> {
const modelProvider = ModelProvider.getInstance();
const prdContent = await modelProvider.chatSync({
model: 'gpt-4o-mini',
messages: [{ content: prompt, role: 'system' }],
});

let messages: MessageInterface[] = [{content: prompt, role: 'system'}];
const prdContent = await chatSyncWithClocker(context, messages, 'gpt-4o-mini', 'generatePRDFromLLM', this.id);
this.logger.log('Received full PRD content from LLM server.');
return prdContent;
}
Expand Down
9 changes: 5 additions & 4 deletions backend/src/build-system/handlers/ux/datamap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { ModelProvider } from 'src/common/model-provider';
import { prompts } from './prompt';
import { Logger } from '@nestjs/common';
import { removeCodeBlockFences } from 'src/build-system/utils/strings';
import { BuildMonitor } from 'src/build-system/monitor';
import { chatSyncWithClocker } from 'src/build-system/utils/handler-helper';
import { MessageInterface } from 'src/common/model-provider/types';

/**
* Handler for generating the UX Data Map document.
Expand All @@ -23,10 +26,8 @@ export class UXDatamapHandler implements BuildHandler<string> {
'web', // TODO: change platform dynamically if needed
);

const uxDatamapContent = await context.model.chatSync({
model: 'gpt-4o-mini',
messages: [{ content: prompt, role: 'system' }],
});
let messages: MessageInterface[] = [{content: prompt, role: 'system'}];
const uxDatamapContent = await chatSyncWithClocker(context, messages, 'gpt-4o-mini', 'generateUXDataMap', this.id);
Logger.log('UX Data Map Content: ', uxDatamapContent);

return {
Expand Down
14 changes: 7 additions & 7 deletions backend/src/build-system/handlers/ux/sitemap-document/uxsmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { prompts } from './prompt';
import { ModelProvider } from 'src/common/model-provider';
import { Logger } from '@nestjs/common';
import { removeCodeBlockFences } from 'src/build-system/utils/strings';
import { BuildMonitor } from 'src/build-system/monitor';
import { MessageInterface } from 'src/common/model-provider/types';
import { chatSyncWithClocker } from 'src/build-system/utils/handler-helper';

export class UXSMDHandler implements BuildHandler<string> {
readonly id = 'op:UX:SMD';
Expand All @@ -26,7 +29,7 @@ export class UXSMDHandler implements BuildHandler<string> {
);

// Send the prompt to the LLM server and process the response
const uxsmdContent = await this.generateUXSMDFromLLM(prompt);
const uxsmdContent = await this.generateUXSMDFromLLM(context, prompt);

// Store the generated document in the context
context.setGlobalContext('uxsmdDocument', uxsmdContent);
Expand All @@ -38,15 +41,12 @@ export class UXSMDHandler implements BuildHandler<string> {
};
}

private async generateUXSMDFromLLM(prompt: string): Promise<string> {
private async generateUXSMDFromLLM(context: BuilderContext, prompt: string): Promise<string> {
const modelProvider = ModelProvider.getInstance();
const model = 'gpt-4o-mini';

const uxsmdContent = await modelProvider.chatSync({
model,
messages: [{ content: prompt, role: 'system' }],
});

let messages: MessageInterface[] = [{content: prompt, role: 'system'}];
const uxsmdContent = await chatSyncWithClocker(context, messages, 'gpt-4o-mini', 'generateUXSMDFromLLM', this.id);
this.logger.log('Received full UXSMD content from LLM server.');
return uxsmdContent;
}
Expand Down
10 changes: 6 additions & 4 deletions backend/src/build-system/handlers/ux/sitemap-structure/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { ModelProvider } from 'src/common/model-provider';
import { prompts } from './prompt';
import { Logger } from '@nestjs/common';
import { removeCodeBlockFences } from 'src/build-system/utils/strings';
import { BuildMonitor } from 'src/build-system/monitor';
import { chatSyncWithClocker } from 'src/build-system/utils/handler-helper';
import { MessageInterface } from 'src/common/model-provider/types';

// UXSMS: UX Sitemap Structure
export class UXSitemapStructureHandler implements BuildHandler<string> {
Expand Down Expand Up @@ -31,10 +34,9 @@ export class UXSitemapStructureHandler implements BuildHandler<string> {
'web', // TODO: Change platform dynamically if necessary
);

const uxStructureContent = await context.model.chatSync({
model: 'gpt-4o-mini',
messages: [{ content: prompt, role: 'system' }],
});
let messages: MessageInterface[] = [{content: prompt, role: 'system'}];
const uxStructureContent = await chatSyncWithClocker(context, messages, 'gpt-4o-mini', 'generateUXSiteMapStructre', this.id);


return {
success: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { BuildHandler, BuildResult } from 'src/build-system/types';
import { ModelProvider } from 'src/common/model-provider';
import { prompts } from './prompt';
import { removeCodeBlockFences } from 'src/build-system/utils/strings';
import { BuildMonitor } from 'src/build-system/monitor';
import { chatSyncWithClocker } from 'src/build-system/utils/handler-helper';
import { MessageInterface } from 'src/common/model-provider/types';

export class Level2UXSitemapStructureHandler implements BuildHandler<string> {
readonly id = 'op:UX:SMS:LEVEL2';
Expand Down Expand Up @@ -49,11 +52,10 @@ export class Level2UXSitemapStructureHandler implements BuildHandler<string> {
'web', // TODO: Replace with dynamic platform if necessary
);

const refinedContent = await modelProvider.chatSync({
model: 'gpt-4o-mini',
messages: [{ content: prompt, role: 'system' }],
});

const startTime = new Date();
let messages: MessageInterface[] = [{content: prompt, role: 'system'}];
const refinedContent = await chatSyncWithClocker(context, messages, 'gpt-4o-mini', 'generateLevel2UXSiteMapStructre', this.id);

refinedSections.push({
title: section.title,
content: refinedContent,
Expand Down
2 changes: 1 addition & 1 deletion backend/src/build-system/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class ProjectEventLogger {
const events = await this.readExistingEvents();
events.push(event);
await fs.promises.writeFile(filePath, JSON.stringify(events, null, 2));
this.logger.log(`Event logged: ${event.type}`);
this.logger.log(`Event logged: ${event.type} to ${filePath}`);
} catch (error) {
this.logger.error('Failed to log event:', error);
throw error;
Expand Down
Loading
Loading