This Node.js script uses various libraries and functions to generate code based on a given prompt, involving operations such as Git clone, code generation using a Large Language Model (LLM), and output file naming. The makeCode
function takes a prompt and optional repository URL as input, generates code, and returns the code blocks, while also handling errors and edge cases.
npm run import -- "make code file with llm"
const fs = require('fs')
const path = require('path')
const PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE || '';
const {spawnSync} = require('child_process')
const {safeurl} = importer.import("domain cache tools")
async function makeCode(prompt, codeFile, github) {
const llmCode = await importer.import("llm code")
if(!github) {
console.error('Project not specified.')
return
}
// TODO: local pull
if(!fs.existsSync(github)
&& fs.existsSync(path.join(PROFILE_PATH, github))) {
github = path.join(PROFILE_PATH, github)
}
if(github.includes('://')
&& fs.existsSync(path.basename(github).replace('.git', ''))
) {
github = path.join(PROFILE_PATH, path.basename(github).replace('.git', ''))
}
if(!fs.existsSync(github)) {
console.error('Project not found.')
return
}
let baseName = path.basename(github)
await spawnSync('git', ['pull'], {
cwd: github,
timeout: 3000,
stdio: ['pipe', 'pipe', 'pipe']
})
// TODO: compare existing project files
let q1 = 'Given the project files:\n' + fs.readdirSync(github).join('\n')
+ '\nFulfill the following instructions on the code file, only return the block of new code.'
+ '\n' + prompt
+ '\n' + 'Response with only the block of code and no summary or reasoning.'
// TODO: create new files
console.log('User: ' + q1)
let a1 = await llmCode(q1)
console.log('AI: ' + a1)
// try to extract code blocks
let code = a1.matchAll(/```(bash|javascript|code)*\n[\s\S]*?\n```/gi)
// extract code blocks from response
let codeBlocks = ''
for(let match of code) {
codeBlocks += match[0].replace(/^```(bash|javascript|code)*\n|\n```$/gi, '') + '\n'
}
if(!codeBlocks) {
console.log('Error, couldn\'t find code in:' + a1)
return
}
if(!codeFile) {
let q2 = 'Given the project files:\n' + fs.readdirSync(github).join('\n')
+ '\nGenerate a new name for the code file:\n' + codeBlocks
+ '\nOnly respond with the recommended file name and nothing else.'
console.log('User: ' + q2)
let a2 = await llmCode(q2)
console.log('AI: ' + a2)
codeFile = safeurl(a2)
}
// TODO: populate code files with goals
fs.writeFileSync(path.join(github, codeFile), codeBlocks)
// TODO: populate goals with actual code
}
module.exports = makeCode
const fs = require('fs');
const path = require('path');
const childProcess = require('child_process');
const { importer } = require('domain-cache-tools');
const getEnvVariable = (key) => {
return process.env[key] || '';
};
const getProjectPath = (github) => {
if (!fs.existsSync(github) && fs.existsSync(path.join(getEnvVariable('HOME'), github))) {
return path.join(getEnvVariable('HOME'), github);
}
if (github.includes('://') && fs.existsSync(path.basename(github).replace('.git', ''))) {
return path.join(getEnvVariable('HOME'), path.basename(github).replace('.git', ''));
}
return github;
};
async function getProjectFiles(github) {
const projectPath = getProjectPath(github);
if (!fs.existsSync(projectPath)) {
throw new Error('Project not found.');
}
return fs.readdirSync(projectPath);
}
const getPromptLines = (prompt, projectFiles) => {
return `Given the project files:
${projectFiles.join('\n')}
Fulfill the following instructions on the code file, only return the block of new code.
${prompt}
Response with only the block of code and no summary or reasoning.`;
};
async function generateCodeFile(prompt, codeBlocks, projectFiles) {
const llmCode = await importer.import('llm-code');
const promptLines = getPromptLines(prompt, projectFiles);
const response = await llmCode(promptLines);
const codeBlocksResponse = response.matchAll(/```(bash|javascript|code)*\n[\s\S]*?\n```/gi);
const codeBlocksExtracted = Array.from(codeBlocksResponse).map((match) => match[0].replace(/^```(bash|javascript|code)*\n|\n```$/gi, ''));
let codeFile = '';
if (codeBlocks) {
codeFile = codeBlocks.replace(/```(bash|javascript|code)*\n|\n```$/gi, '');
}
if (!codeFile) {
const q2 = `Given the project files:\n${projectFiles.join('\n')}\nGenerate a new name for the code file:\n${codeBlocksExtracted.join('\n')}\nOnly respond with the recommended file name and nothing else.`;
console.log('User:', q2);
let a2 = await llmCode(q2);
console.log('AI:', a2);
codeFile = safeurl(a2);
}
return codeFile;
}
async function makeCode(prompt, codeFile, github) {
if (!github) {
throw new Error('Project not specified.');
}
const projectFiles = await getProjectFiles(github);
const baseName = path.basename(github);
await childProcess.spawnSync('git', ['pull'], {
cwd: github,
timeout: 3000,
stdio: ['pipe', 'pipe', 'pipe'],
});
const codeBlocks = prompt + '\n' + projectFiles.join('\n');
const codeFileGenerated = await generateCodeFile(prompt, codeBlocks, projectFiles);
fs.writeFileSync(path.join(github, codeFileGenerated), codeBlocks);
}
module.exports = makeCode;
Code Breakdown
This is a Node.js script that uses various libraries and functions to generate code based on a given prompt. Here's a high-level overview:
fs
(File System) and path
are used for file system operations.PROFILE_PATH
is set to the user's home directory, using various environment variables.spawnSync
is used to spawn a child process (e.g., Git).importer
is used to import other modules, and safeurl
is imported from domain cache tools
.llmCode
is imported from llm code
, which is assumed to contain a function for generating code.makeCode
FunctionThe main function, makeCode
, takes three parameters:
prompt
: the input prompt.codeFile
: the name of the output code file (optional).github
: the repository URL (optional).Here's a step-by-step breakdown:
spawnSync
to run git pull
on the repository.llmCode
module, which is not shown in this code snippet. This module is assumed to contain a function for generating code based on a given prompt.spawnSync
to run git pull
on the repository, which may not be the most efficient or secure way to handle Git operations.