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

chore(backend): chore fix naming and chat issue #60

Merged
merged 2 commits into from
Nov 29, 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
4 changes: 3 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json",
"test:integration": "RUN_INTEGRATION_TESTS=true jest",
"check-types": "tsc --noEmit"
},
"dependencies": {
Expand All @@ -42,6 +43,7 @@
"graphql": "^16.9.0",
"graphql-subscriptions": "^2.0.0",
"graphql-ws": "^5.16.0",
"markdown-to-txt": "^2.0.1",
"reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1",
"sqlite3": "^5.1.7",
Expand Down Expand Up @@ -75,4 +77,4 @@
"tsconfig-paths": "^4.2.0",
"typescript": "^5.1.3"
}
}
}
7 changes: 5 additions & 2 deletions backend/src/build-system/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class BuilderContext {
id: string,
) {
this.handlerManager = BuildHandlerManager.getInstance();
this.model = ModelProvider.getInstance();
new Logger(`builder-context-${id}`);
}

Expand Down Expand Up @@ -92,9 +93,11 @@ export class BuilderContext {
this.data[key] = value;
}

getData<Key extends keyof ContextData>(key: Key): ContextData[Key] {
getData<Key extends keyof ContextData>(
key: Key,
): ContextData[Key] | undefined {
if (!(key in this.data)) {
throw new Error(`Data key "${key}" is not set or does not exist.`);
return undefined;
}
return this.data[key];
}
Expand Down
40 changes: 40 additions & 0 deletions backend/src/build-system/node/file-arch/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { BuildHandler, BuildResult } from 'src/build-system/types';
import { BuilderContext } from 'src/build-system/context';
import { generateFileArchPrompt } from './prompt';
import { Logger } from '@nestjs/common';

export class FileArchGenerateHandler implements BuildHandler {
readonly id = 'op:File_Arch::STATE:GENERATE';
private readonly logger: Logger = new Logger('FileArchGenerateHandler');

// TODO: adding page by page analysis
async run(context: BuilderContext, ...args: any[]): Promise<BuildResult> {
this.logger.log('Generating File Architecture Document...');

const fileStructure = args[0] as string;
const dataMapStruct = args[1] as string;

if (!fileStructure || !dataMapStruct) {
return {
success: false,
error: new Error(
'Missing required parameters: fileStructure or pageByPageAnalysis',
),
};
}

const prompt = generateFileArchPrompt(fileStructure, dataMapStruct);

const fileArchContent = await context.model.chatSync(
{
content: prompt,
},
'gpt-4o-mini',
);

return {
success: true,
data: fileArchContent,
};
}
}
65 changes: 65 additions & 0 deletions backend/src/build-system/node/file-arch/prompt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
export const generateFileArchPrompt = (
fileStructure: string,
pageByPageAnalysis: string,
): string => {
return `You are a File Architecture Analyzer. Your task is to analyze the given project directory structure and the detailed page-by-page analysis, then output a JSON object detailing the file dependencies. The output JSON must be wrapped in <GENERATEDCODE></GENERATEDCODE> tags.

### Directory Structure Input
The following is the project's directory structure. Use this to identify files and folders.

\`\`\`
${fileStructure}
\`\`\`

### Page-by-Page Analysis Input
The following is a detailed analysis of each page. Use this information to understand specific roles, interactions, and dependencies.

\`\`\`
${pageByPageAnalysis}
\`\`\`

### Instructions
1. **Analyze the Inputs**:
- Use the directory structure to identify all files and folders.
- Leverage the page-by-page analysis to understand the roles and interactions of different components and pages.
- Determine the role of each file based on its path and the provided analysis (e.g., page, component, context, hook, styles).
- Identify direct dependencies for each file by considering typical imports based on roles, naming conventions, and the provided analysis.

2. **Generate File Dependency JSON**:
- For each file, list its direct dependencies as an array of relative paths in the \`dependsOn\` field.
- Use relative paths for dependencies whenever possible. For example:
- If a file \`index.tsx\` references a CSS file \`index.css\` in the same folder, the dependency should be listed as \`"./index.css"\`.
- If a file references another file in its parent folder, use \`"../filename"\`.
- Only use absolute paths (e.g., starting with \`src/\`) when no shorter relative path is available.
- Include CSS/SCSS files as dependencies for any JavaScript or TypeScript files that reference them (e.g., through imports or implied usage).
- Include files that have no dependencies with an empty \`dependsOn\` array.
- Organize the output in a \`files\` object where keys are file paths, and values are their dependency objects.

3. **Output Requirements**:
- The JSON object must strictly follow this structure:
\`\`\`json
{
"files": {
"path/to/file1": {
"dependsOn": ["path/to/dependency1", "path/to/dependency2"]
},
"path/to/file2": {
"dependsOn": []
}
}
}
\`\`\`
- Wrap the JSON output with \`<GENERATEDCODE></GENERATEDCODE>\` tags.

### Notes
- **CSS Dependencies**: Any file that relies on a CSS/SCSS module file (e.g., \`Header.module.css\`) must list it in the \`dependsOn\` array.
- **Use Relative Paths When Possible**: Ensure dependencies are listed using the shortest possible path (e.g., \`"./filename"\` for files in the same folder).
- **Dependency Inclusion Rule**: All files, except for \`src/index.ts\`, must be depended upon by at least one other file. This means they should appear in the \`dependsOn\` array of at least one other file.
- The \`dependsOn\` field should reflect logical dependencies inferred from both the directory structure and the page-by-page analysis.
- Use common project patterns to deduce dependencies (e.g., pages depend on components, contexts, hooks, and styles).
- Include all files in the output, even if they have no dependencies.
- Ensure the JSON output is properly formatted and wrapped with \`<GENERATEDCODE></GENERATEDCODE>\` tags.

### Output
Return only the JSON object wrapped in \`<GENERATEDCODE></GENERATEDCODE>\` tags.`;
};
2 changes: 1 addition & 1 deletion backend/src/build-system/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,5 @@ export interface BuildHandler {
* @param model model provider for the build
* @param args the request arguments
*/
run(context: BuilderContext, args: unknown): Promise<BuildResult>;
run(context: BuilderContext, ...args: any[]): Promise<BuildResult>;
}
Loading
Loading