llm scaffold | generate code | edit code cell in notebook | Search

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.

Run example

npm run import -- "make code file with llm"

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

What the code could have been:

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:

Dependencies and Variables

makeCode Function

The main function, makeCode, takes three parameters:

Here's a step-by-step breakdown:

  1. Check if GitHub repository is specified: If no repository is provided, the function returns an error message.
  2. Resolve repository path: If the repository path is not an absolute path, the function attempts to resolve it to an absolute path using various possible directory locations.
  3. Check if repository exists: If the repository does not exist, the function returns an error message.
  4. Pull new changes from Git: The function uses spawnSync to run git pull on the repository.
  5. Generate code: The function creates a prompt for the LLM (Large Language Model) code generator, which includes the project files and the input prompt. The LLM code generator returns a response, which is expected to contain code blocks.
  6. Extract code blocks: The function uses regular expressions to extract code blocks from the LLM response.
  7. Generate new code file name: If no output file name is provided, the function creates a prompt for the LLM code generator to suggest a new file name based on the code blocks.
  8. Return code blocks: The function returns the extracted code blocks.

Notes