I’ve been working on an AI-assisted coding tool called aiCoder aicoderproject.com, which is designed to streamline the integration of LLM-generated code snippets into existing projects. The tool leverages Abstract Syntax Trees (ASTs) to intelligently merge AI-suggested modifications into source files while preserving code structure, formatting, and readability.
Why Use ASTs for Merging Code?
Traditional methods of inserting AI-generated code often involve simple text-based appending, regex modifications, or brute-force overwrites. These approaches fail when:
- Existing functions need to be updated without affecting other parts of the file.
- AI-generated snippets include imports, class methods, or function definitions that must be intelligently merged.
- Formatting and structural integrity must be maintained.
By leveraging ASTs, AI Coder ensures that LLM-generated code seamlessly integrates into an existing project, handling edge cases and avoiding conflicts.
How It Works
- Parse the Source Code: Using a JavaScript parser like Esprima, AI Coder converts the source file into an AST representation.
- Parse the LLM-Generated Snippet: The AI-generated code is also converted into an AST.
- Compare & Merge:
- If a function already exists, the tool replaces only that function instead of duplicating or appending it.
- If a class exists but lacks the new method, the method is added in the appropriate location. If the method already exists replace that method with the new one leaving the rest of the class intact.
- If an import statement is needed, it gets inserted without duplication.
- If variables/constants are introduced, they are added at the correct scope.
- Regenerate the Source Code: The modified AST is converted back to a readable source file, maintaining formatting and comments.
Slide deck discussing the AST based merging approach.
Current Features
- Class-Aware Merging: AI Coder recognizes existing class structures and integrates new methods without disrupting them.
- Function Replacement: Ensures that function updates replace the correct function rather than adding duplicates.
- Import Management: Prevents redundant imports while ensuring dependencies are included.
Get Involved
AI Coder is open-source, and you can check out the GitHub repository here: GitHub - mmiscool/aiCoder: A tool to write JS libraries using AI. The first and only tool that is using ASTs to perform surgical changes to existing code files with out mangling the code. https://aicoderproject.com/ . I’d love to hear feedback from other developers—especially those interested in AI-assisted coding, AST manipulation, or programmatic code refactoring.
Would love to discuss this further! How do you currently handle AI-generated code integrations? Looking forward to your thoughts.