Skip to content

Commit

Permalink
fixing codegen cli validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Grzegorz Tańczyk committed Jul 13, 2024
1 parent 4aa19d2 commit ec92c14
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 113 deletions.
5 changes: 4 additions & 1 deletion games/nukes/codegen/find-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ const rootFiles = findFiles(rootDir, false, '.md');
const codegenFiles = findFiles(codegenDir, true, '.js', '.md');
const gameFiles = findFiles(srcDir, true, '.ts', '.tsx', '.md');

export const sourceFiles = codegenOnly ? [...codegenFiles] : [...rootFiles, ...codegenFiles, ...gameFiles];
/** Get source files of the application */
export function getSourceFiles() {
return codegenOnly ? [...codegenFiles] : [...rootFiles, ...codegenFiles, ...gameFiles];
}
9 changes: 5 additions & 4 deletions games/nukes/codegen/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { systemPrompt, codeGenPrompt } from './prompt-gen.js';
import { getSystemPrompt } from './systemprompt.js';
import { getCodeGenPrompt } from './prompt-codegen.js';
import { updateFiles } from './update-files.js';
import { generateContent } from './vertex-ai.js';

Expand All @@ -8,6 +9,7 @@ const allowedParameters = [
'--consider-all-files',
'--allow-file-create',
'--allow-file-delete',
'--codegen-only',
];

const providedParameters = process.argv.slice(2);
Expand All @@ -17,15 +19,14 @@ providedParameters.forEach((param) => {
console.error(`Invalid parameter: ${param}, all parameters must start with --`);
process.exit(1);
}
const parameterName = param.substring(2);
if (!allowedParameters.includes(parameterName)) {
if (!allowedParameters.includes(param)) {
console.error(`Invalid parameter: ${param}, allowed parameters are: ${allowedParameters.join(', ')}`);
process.exit(1);
}
});

console.log('Generating response');
const functionCalls = await generateContent(systemPrompt, codeGenPrompt);
const functionCalls = await generateContent(getSystemPrompt(), getCodeGenPrompt());
console.log('Received function calls:', functionCalls);

// read --dry-run flag from command line
Expand Down
104 changes: 0 additions & 104 deletions games/nukes/codegen/prompt-gen.js

This file was deleted.

9 changes: 6 additions & 3 deletions games/nukes/codegen/read-files.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import fs from 'fs';

import { sourceFiles } from './find-files.js';
import { getSourceFiles } from './find-files.js';

/**
* Read contents of source files and create a map with file path as key and file content as value
*/
function readSourceFiles() {
const sourceCode = {};
for (const file of sourceFiles) {
for (const file of getSourceFiles()) {
sourceCode[file] = fs.readFileSync(file, 'utf-8');
}
return sourceCode;
}

export const sourceCode = readSourceFiles();
/** Print source code of all source files */
export function getSourceCode() {
return readSourceFiles();
}
4 changes: 3 additions & 1 deletion games/nukes/codegen/update-files.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import fs from 'fs';
import path from 'path';

import { sourceFiles } from './find-files.js';
import { getSourceFiles } from './find-files.js';

/**
* @param codegenResults Result of the code generation, a map of file paths to new content
*/
export function updateFiles(functionCalls) {
const sourceFiles = getSourceFiles();

for (const { filePath, newContent } of functionCalls) {
// ignore files which are not located inside project directory (sourceFiles)
if (!sourceFiles.includes(filePath) && !sourceFiles.some((file) => path.dirname(filePath) === path.dirname(file))) {
Expand Down

0 comments on commit ec92c14

Please sign in to comment.