This Node.js script uses a Large Language Model (LLM) to generate code by constructing prompts, sending them to the LLM, and extracting code blocks from the response. The script performs tasks such as project path resolution, Git pull, and code interpretation to ensure the code is up-to-date and accurate before sending the prompt to the LLM.
npm run import -- "edit code cell in notebook"
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 updateCode = importer.import("update code cell")
async function generateCell(prompt, codeCell, 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']
})
if(typeof codeCell == 'string') {
codeCell = importer.interpret(codeCell)
}
if(!codeCell) {
console.error('Code cell not found.')
return
}
// TODO: compare existing project files
let q1 = 'Give the project files:\n' + fs.readdirSync(github).join('\n')
+ 'And the existing code for the file ' + codeCell.id + ':\n'
+ codeCell.code
+ '\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
}
// TODO: populate code files with goals
//fs.writeFileSync(path.join(github, codeFile), codeBlocks)
updateCode(codeCell, codeBlocks)
// TODO: populate goals with actual code
}
module.exports = generateCell
'use strict';
const fs = require('fs');
const path = require('path');
const { spawnSync } = require('child_process');
const importer = require('./importer'); // assumes importer is in a separate file
const updateCode = importer.import('update code cell');
const LLMCode = importer.import('llm code');
const PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE || '';
async function generateCell(prompt, codeCell, github) {
if (!github) {
throw new Error('Project not specified.');
}
let githubPath = getProjectPath(github);
if (!fs.existsSync(githubPath)) {
throw new Error('Project not found.');
}
await pullProject(githubPath);
const llmCode = new LLMCode();
if (typeof codeCell ==='string') {
codeCell = await importer.interpret(codeCell);
}
if (!codeCell) {
throw new Error('Code cell not found.');
}
const q1 = createQ1(githubPath, codeCell, prompt);
const a1 = await llmCode.generateCode(q1);
const codeBlocks = extractCodeBlocks(a1);
if (!codeBlocks) {
throw new Error('Error, couldn\'t find code in response.');
}
await updateCode(codeCell, codeBlocks);
}
async function getProjectPath(github) {
if (fs.existsSync(path.join(PROFILE_PATH, github)) &&!github.includes('://')) {
return path.join(PROFILE_PATH, github);
}
return github;
}
async function pullProject(githubPath) {
try {
await spawnSync('git', ['pull'], {
cwd: githubPath,
timeout: 3000,
stdio: ['pipe', 'pipe', 'pipe'],
});
} catch (error) {
console.error('Error pulling project:', error);
throw new Error('Error pulling project.');
}
}
function createQ1(githubPath, codeCell, prompt) {
const projectFiles = fs.readdirSync(githubPath).join('\n');
const existingCode = codeCell.code;
return `Give the project files:\n${projectFiles}And the existing code for the file ${codeCell.id}:\n${existingCode}\nFulfill the following instructions on the code file, only return the block of new code:\n${prompt}\nResponse with only the block of code and no summary or reasoning.`;
}
function extractCodeBlocks(response) {
const regex = /\`\`\`(bash|javascript|code)*\n([\s\S]*?)\n\`\`\`/gi;
const matches = response.matchAll(regex);
if (!matches) {
return null;
}
return matches.reduce((acc, match) => acc + match[2].trim() + '\n', '');
}
module.exports = generateCell;
Breakdown of the Code
This Node.js script appears to be a part of a larger project that utilizes a Large Language Model (LLM) to generate code for a given project. Here's a brief overview of the code:
The script begins by importing the necessary dependencies:
fs
(File System) for interacting with the file systempath
for working with file pathsprocess.env
for accessing environment variableschild_process
for executing system commandsimporter
for importing functions and modules (e.g., updateCode
and llmCode
)generateCell
The generateCell
function is the main entry point of the script. It takes three parameters:
prompt
: a string describing the instructions for the LLM to followcodeCell
: an object containing code-related information (e.g., existing code for a file)github
: a string representing the project repository (e.g., a Git URL)The function performs the following tasks:
git pull
operation to ensure the code is up-to-date.codeCell
parameter is a string, it uses the importer.interpret
function to parse and interpret the code.llmCode
function) and extracts the code blocks from the response.TODO
comment to indicate areas that require further implementation (e.g., local pull and comparing existing project files).spawnSync
function is used to execute the git pull
command, which suggests that the script is designed to interact with Git repositories.importer
module is used to import functions and modules, which suggests that the script is part of a larger framework or system.