Better way to have LLMs modify existing code using ASTs

2 days ago 5

snipsplicer

1.0.17 • Public • Published 9 days ago

SnipSplicer is a library for manipulating code files in various languages allowing for surgical file edits using snippets supplied by LLMs. This tool uses AST (abstract syntax trees) to automatically merge LLM generate code snippets in to existing code. Because the process we use to merge LLM generate snippets is deterministic it is extremely relegable.

  • Whole code file is regenerated each time a change is made
  • LLMs some times like to forget things and delete large chunks of your original file when regenerating the whole file each time.
  • LLM output is not guaranteed to be syntactically correct.
  • By merging snippets that follow specific rules we can surgically modify the original code with out regenerating the whole file.
  • Code is never accidentally deleted as any function or method not included in the snippet is touched.
  • Using ASTs to merge snippets with original code makes it impossible to merge syntactically incorrect code preventing corruption.
import { mergeCode } from "snipsplicer"; let codeFileContents =` export class exampleClass { exampleMethod() { return 'example'; } exampleMethod2() { return 'example2'; } exampleMethod3() { return 'example3'; } } `; let snippetToMerge = ` export class exampleClass { exampleMethod() { // we make some changes here console.log('do something else'); // how about a friendly alert alert('hello world'); return 'example'; } } `; const resultingCodeAfterMerge = mergeCode("javascript", codeFileContents, snippetToMerge);
export class exampleClass { exampleMethod() { // we make some changes here console.log('do something else'); // how about a friendly alert alert('hello world'); return 'example'; } exampleMethod2() { return 'example2'; } exampleMethod3() { return 'example3'; } }
import { mergeToolsPromptStrings } from 'snipsplicer'; console.log(mergeToolsPromptStrings.complete); // complete prompt with snippet generation rules for all supported languages. console.log(mergeToolsPromptStrings.html); // html snippet formatting rules console.log(mergeToolsPromptStrings.javascript); // javascript snippet formatting rules console.log(mergeToolsPromptStrings.css); // css snippet formatting rules

Package Sidebar

Install

Weekly Downloads

Collaborators

  • mmiscool

Read Entire Article