ai-reviewer-core
v1.1.0
Published
Core AI code review logic - platform agnostic
Maintainers
Readme
ai-reviewer-core
Platform-agnostic AI code review library
This is the core library that powers AI code review across different platforms (GitHub Actions, Jenkins, etc.). It provides platform-agnostic logic for parsing diffs, communicating with LLMs, and generating code review comments.
Installation
npm install ai-reviewer-coreQuick Start
const { CodeReviewer, parseDiff } = require('ai-reviewer-core');
// Set up environment
process.env.LLM_API_KEY = 'your-openai-api-key';
process.env.LLM_ENDPOINT = 'https://api.openai.com/v1/chat/completions';
// Create reviewer instance
const reviewer = new CodeReviewer();
// Parse git diff
const diffData = `diff --git a/example.js b/example.js
index 123..456 100644
--- a/example.js
+++ b/example.js
@@ -1,3 +1,4 @@
function hello() {
+ console.log("Hello World");
return "world";
}`;
// Review the changes
async function reviewCode() {
const results = await reviewer.reviewChanges(diffData, {
generateSummary: true
});
console.log('Summary:', results.summary);
console.log('Comments:', results.comments);
console.log('Metadata:', results.metadata);
}
reviewCode();API Reference
CodeReviewer
Main class for reviewing code changes.
const { CodeReviewer } = require('@ai-reviewer/core');
const reviewer = new CodeReviewer();Methods
reviewChanges(diffData, options)
Reviews code changes and generates comments.
Parameters:
diffData(string): Git diff contentoptions(object): Review optionsgenerateSummary(boolean): Whether to generate a summary (default: true)context(object): Additional context for the review
Returns: Promise
summary(string): Generated summary of changescomments(Array): Array of review commentshunks(Array): Parsed diff hunksmetadata(Object): Review metadata (timestamp, counts, etc.)
reviewHunk(hunk, options)
Reviews a single diff hunk.
generateSummary(diffData, options)
Generates a summary of the changes.
filterComments(comments, filters)
Filters comments based on criteria.
parseDiff(diffData)
Parses git diff data into structured hunks.
const { parseDiff } = require('@ai-reviewer/core');
const hunks = parseDiff(diffData);
console.log(hunks);Returns: Array of hunk objects with:
filename(string): File namechanges(Array): Array of changeshunkHeader(Object): Hunk metadata
LLM Integration
const { getReviewFromLLM, getSummaryFromLLM } = require('@ai-reviewer/core');
// Review a hunk
const reviewResponse = await getReviewFromLLM(hunk);
// Generate summary
const summary = await getSummaryFromLLM(diffData);Advanced Usage
Custom LLM Provider
const { LLMCoordinator, OpenAIProvider } = require('@ai-reviewer/core');
// Use existing provider
const coordinator = new LLMCoordinator();
const provider = coordinator.getProvider(); // OpenAI by default
// Or extend for custom provider
class CustomProvider extends OpenAIProvider {
formatRequest(messages, options) {
// Custom formatting
return super.formatRequest(messages, options);
}
}Direct Platform Integration
const results = await reviewer.reviewChanges(diffData);
// Use structured data directly with Git platforms
results.comments.forEach(comment => {
github.createLineComment({
path: comment.filename,
line: comment.line,
body: comment.body // Ready-to-post markdown
});
});
// Optional summary as PR comment
if (results.summary) {
github.createReview({ body: results.summary });
}Environment Variables
LLM_API_KEY- Your LLM API key (required)LLM_ENDPOINT- LLM API endpoint (required)LLM_MODEL- Model to use (optional, defaults to gpt-3.5-turbo)
Platform Integrations
This core library is used by:
- @ai-reviewer/github-action - GitHub Actions integration
- @ai-reviewer/jenkins - Jenkins integration
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Run
npm test - Submit a pull request
Testing
# Run tests
npm test
# Run with coverage
npm run test:coverage
# Watch mode
npm run test:watchBuilding
# Build for distribution
npm run build
# Build CommonJS
npm run build:cjs
# Build ES modules
npm run build:esmLicense
Apache-2.0
Changelog
See CHANGELOG.md for release history.
