generated from ztimson/template
This commit is contained in:
119
src/review.mjs
119
src/review.mjs
@@ -1,58 +1,79 @@
|
||||
/**
|
||||
* Variables:
|
||||
* HOST - AI provider (ollama/anthropic/openai)
|
||||
* MODEL - AI model name
|
||||
* TOKEN - Access token (Omit with ollama)
|
||||
* ROOT - Path to project
|
||||
*/
|
||||
import {Ai} from '@ztimson/ai-utils';
|
||||
import {$} from '@ztimson/node-utils';
|
||||
import * as os from 'node:os';
|
||||
import * as path from 'node:path';
|
||||
import * as fs from 'node:fs';
|
||||
|
||||
const gitDiff = await $`git diff HEAD^..HEAD`;
|
||||
const root = process.env['ROOT'] || process.cwd(),
|
||||
host = process.env['HOST'],
|
||||
model = process.env['MODEL'],
|
||||
token = process.env['TOKEN'];
|
||||
(async () => {
|
||||
const
|
||||
git = process.env['GIT_HOST'],
|
||||
owner = process.env['GIT_OWNER'],
|
||||
repo = process.env['GIT_REPO'],
|
||||
auth = process.env['GIT_TOKEN'],
|
||||
pr = process.env['PULL_REQUEST'],
|
||||
root = process.argv[2] || process.cwd(),
|
||||
host = process.env['AI_HOST'],
|
||||
model = process.env['AI_MODEL'],
|
||||
token = process.env['AI_TOKEN'];
|
||||
|
||||
let options = {ollama: {model, host}};
|
||||
if(host === 'anthropic') options = {anthropic: {model, token}};
|
||||
else if(host === 'openai') options = {openAi: {model, token}};
|
||||
const comments = [];
|
||||
const commit = (await $`git log -1 --pretty=format:%H`).trim();
|
||||
const gitDiff = await $`git diff HEAD^..HEAD`;
|
||||
|
||||
const ai = new Ai({
|
||||
...options,
|
||||
model: [host, model],
|
||||
path: process.env['path'] || os.tmpdir(),
|
||||
system: 'You are a code reviewer, use the following git diff to find any bugs and recommend changes using the `recommend` tool.',
|
||||
tools: [{
|
||||
name: 'read_file',
|
||||
description: 'Read contents of a file',
|
||||
args: {
|
||||
path: {type: 'string', description: 'Path to file relative to project root'}
|
||||
},
|
||||
fn: (args) => fs.readFileSync(path.join(root, args.path), 'utf-8')
|
||||
}, {
|
||||
name: 'get_diff',
|
||||
description: 'Check a file for differences using git',
|
||||
args: {
|
||||
path: {type: 'string', description: 'Path to file relative to project root'}
|
||||
},
|
||||
fn: async (args) => await $`git diff HEAD^..HEAD -- ${path.join(root, args.path)}`
|
||||
}, {
|
||||
name: 'recommend',
|
||||
description: 'Make review recommendation',
|
||||
args: {
|
||||
file: {type: 'string', description: 'File path'},
|
||||
line: {type: 'number', description: 'Line number', optional: true},
|
||||
comment: {type: 'string', description: 'Review comment'}
|
||||
},
|
||||
fn: (args) => {
|
||||
console.log(`💬 ${args.file}${args.line ? `:${args.line}` : ''} - ${args.comment}`);
|
||||
// TODO: Add fetch to gitea to add comment to PR
|
||||
}
|
||||
}]
|
||||
});
|
||||
let options = {ollama: {model, host}};
|
||||
if(host === 'anthropic') options = {anthropic: {model, token}};
|
||||
else if(host === 'openai') options = {openAi: {model, token}};
|
||||
const ai = new Ai({
|
||||
...options,
|
||||
model: [host, model],
|
||||
path: process.env['path'] || os.tmpdir(),
|
||||
system: 'You are a code reviewer. Analyze the git diff and use the `recommend` tool for EACH issue you find. You must call `recommend` exactly once for every bug or improvement opportunity. After making all recommendations, provide a brief summary.',
|
||||
tools: [{
|
||||
name: 'read_file',
|
||||
description: 'Read contents of a file',
|
||||
args: {
|
||||
path: {type: 'string', description: 'Path to file relative to project root'}
|
||||
},
|
||||
fn: (args) => fs.readFileSync(path.join(root, args.path), 'utf-8')
|
||||
}, {
|
||||
name: 'get_diff',
|
||||
description: 'Check a file for differences using git',
|
||||
args: {
|
||||
path: {type: 'string', description: 'Path to file relative to project root'}
|
||||
},
|
||||
fn: async (args) => await $`git diff HEAD^..HEAD -- ${path.join(root, args.path)}`
|
||||
}, {
|
||||
name: 'recommend',
|
||||
description: 'REQUIRED: Call this once for every bug, improvement, or concern identified in the review.',
|
||||
args: {
|
||||
file: {type: 'string', description: 'File path'},
|
||||
line: {type: 'number', description: 'Line number in new file', optional: true},
|
||||
comment: {type: 'string', description: 'Review comment explaining the issue'}
|
||||
},
|
||||
fn: (args) => {
|
||||
comments.push({
|
||||
path: args.file,
|
||||
new_position: args.line,
|
||||
body: args.comment,
|
||||
});
|
||||
return 'Comment recorded, continue reviewing';
|
||||
}
|
||||
}]
|
||||
});
|
||||
|
||||
await ai.language.ask(gitDiff);
|
||||
const summary = await ai.language.ask(gitDiff);
|
||||
const res = await fetch(`${git}/api/v1/repos/${owner}/${repo}/pulls/${pr}/reviews`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `token ${auth}`,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
body: summary,
|
||||
commit_id: commit,
|
||||
event: 'COMMENT',
|
||||
comments,
|
||||
})
|
||||
});
|
||||
if(!res.ok) throw new Error(`${res.status} ${await res.text()}`);
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user