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 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
1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"dotenv": "^16.4.7",
"fastembed": "^1.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",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { BuilderContext } from '../context';
import { BuildMonitor } from '../monitor';
import { BuildSequence } from '../types';
import { executeBuildSequence, objectToMarkdown, writeToFile } from './utils';

Expand Down Expand Up @@ -144,5 +146,5 @@ describe('Build Sequence Test', () => {
expect(result.success).toBe(true);
expect(result.metrics).toBeDefined();
console.log(`Logs saved to: ${result.logFolderPath}`);
}, 300000);
}, 60000000);
});
26 changes: 20 additions & 6 deletions backend/src/build-system/handlers/backend/code-generate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import { generateBackendCodePrompt } from './prompt';
import { saveGeneratedCode } from 'src/build-system/utils/files';
import * as path from 'path';
import { formatResponse } from 'src/build-system/utils/strings';
import { chatSyncWithClocker } from 'src/build-system/utils/handler-helper';
import { MessageInterface } from 'src/common/model-provider/types';
import {
FileWriteError,
InvalidParameterError,
MissingConfigurationError,
ResponseParsingError,
} from 'src/build-system/errors';
import { Logger } from '@nestjs/common';

/**
* BackendCodeHandler is responsible for generating the backend codebase
Expand Down Expand Up @@ -44,7 +47,7 @@ export class BackendCodeHandler implements BuildHandler<string> {
);
}

if (typeof databaseSchemas !== 'object') {
if (!databaseSchemas) {
throw new InvalidParameterError(
'databaseSchemas should be a valid object.',
);
Expand All @@ -66,13 +69,24 @@ export class BackendCodeHandler implements BuildHandler<string> {
dependencyFile,
);

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

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

generatedCode = formatResponse(modelResponse);

const uuid = context.getGlobalContext('projectUUID');
saveGeneratedCode(path.join(uuid, 'backend', currentFile), generatedCode);
generatedCode = formatResponse(modelResponse);
if (!generatedCode) {
throw new ResponseParsingError('Response tag extraction failed.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@

import { prompts } from './prompt';
import { formatResponse } from 'src/build-system/utils/strings';
import { MessageInterface } from 'src/common/model-provider/types';
import { chatSyncWithClocker } from 'src/build-system/utils/handler-helper';
import {
FileNotFoundError,
FileModificationError,
ResponseParsingError,
ModelTimeoutError,
TemporaryServiceUnavailableError,
RateLimitExceededError,
} from 'src/build-system/errors';

/**
Expand Down Expand Up @@ -45,7 +44,7 @@
}

let files: string[];
try {

Check failure on line 47 in backend/src/build-system/handlers/backend/file-review/file-review.ts

View workflow job for this annotation

GitHub Actions / autofix

Unnecessary try/catch wrapper
this.logger.log(`Scanning backend directory: ${backendPath}`);
files = await fs.readdir(backendPath);
if (!files.length) {
Expand All @@ -64,11 +63,18 @@
);

let modelResponse: string;

try {

Check failure on line 67 in backend/src/build-system/handlers/backend/file-review/file-review.ts

View workflow job for this annotation

GitHub Actions / autofix

Unnecessary try/catch wrapper
modelResponse = await context.model.chatSync({
model: 'gpt-4o-mini',
messages: [{ content: filePrompt, role: 'system' }],
});
const messages: MessageInterface[] = [
{ content: filePrompt, role: 'system' },
];
modelResponse = await chatSyncWithClocker(
context,
messages,
'gpt-4o-mini',
'generateBackendCode',
this.id,
);
} catch (error) {
throw error;
}
Expand All @@ -81,7 +87,7 @@

for (const fileName of filesToModify) {
const filePath = path.join(backendPath, fileName);
try {

Check failure on line 90 in backend/src/build-system/handlers/backend/file-review/file-review.ts

View workflow job for this annotation

GitHub Actions / autofix

Unnecessary try/catch wrapper
const currentContent = await fs.readFile(filePath, 'utf-8');
const modificationPrompt = prompts.generateFileModificationPrompt(
fileName,
Expand All @@ -91,10 +97,16 @@
backendCode,
);

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

const newContent = formatResponse(response);
if (!newContent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
import { generateBackendOverviewPrompt } from './prompt';
import { Logger } from '@nestjs/common';
import { removeCodeBlockFences } from 'src/build-system/utils/strings';
import { chatSyncWithClocker } from 'src/build-system/utils/handler-helper';
import { MessageInterface } from 'src/common/model-provider/types';
import {
ResponseParsingError,
MissingConfigurationError,
ModelTimeoutError,
TemporaryServiceUnavailableError,
RateLimitExceededError,
ModelUnavailableError,
ResponseParsingError,
} from 'src/build-system/errors';

type BackendRequirementResult = {
Expand Down Expand Up @@ -67,14 +66,21 @@
);

let backendOverview: string;

try {

Check failure on line 70 in backend/src/build-system/handlers/backend/requirements-document/index.ts

View workflow job for this annotation

GitHub Actions / autofix

Unnecessary try/catch wrapper
backendOverview = await context.model.chatSync({
model: 'gpt-4o-mini',
messages: [{ content: overviewPrompt, role: 'system' }],
});
const messages: MessageInterface[] = [
{ content: overviewPrompt, role: 'system' },
];
backendOverview = await chatSyncWithClocker(
context,
messages,
'gpt-4o-mini',
'generateBackendOverviewPrompt',
this.id,
);

if (!backendOverview) {
throw new ModelTimeoutError(
throw new ModelUnavailableError(
'The model did not respond within the expected time.',
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import { removeCodeBlockFences } from 'src/build-system/utils/strings';
import {
MissingConfigurationError,
ResponseParsingError,
ModelTimeoutError,
ModelUnavailableError,
TemporaryServiceUnavailableError,
RateLimitExceededError,
} from 'src/build-system/errors';
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 Down Expand Up @@ -39,13 +41,19 @@ export class DatabaseRequirementHandler implements BuildHandler<string> {
let dbRequirementsContent: string;

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

if (!dbRequirementsContent) {
throw new ModelTimeoutError(
throw new ModelUnavailableError(
'The model did not respond within the expected time.',
);
}
Expand Down
Loading
Loading