From c27802ca44cce0e5adf829c8e0415318c6d7f43b Mon Sep 17 00:00:00 2001 From: ztimson Date: Sat, 27 Dec 2025 17:54:03 -0500 Subject: [PATCH] Working AI --- .env | 3 +++ package-lock.json | 8 ++++---- package.json | 2 +- src/review.mjs | 19 ++++++++++--------- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/.env b/.env index 9fbf229..ab04882 100644 --- a/.env +++ b/.env @@ -1,3 +1,6 @@ +; DO NOT MODIFY! this is an example environment file +; Create a copy called .env.local with the needed settings + GIT_HOST= GIT_OWNER= GIT_REPO= diff --git a/package-lock.json b/package-lock.json index 454923e..0dc6f7f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "0.0.0", "license": "ISC", "dependencies": { - "@ztimson/ai-utils": "0.2.3", + "@ztimson/ai-utils": "^0.2.4", "@ztimson/node-utils": "^1.0.7", "@ztimson/utils": "^0.28.3", "dotenv": "^17.2.3" @@ -288,9 +288,9 @@ } }, "node_modules/@ztimson/ai-utils": { - "version": "0.2.3", - "resolved": "https://registry.npmjs.org/@ztimson/ai-utils/-/ai-utils-0.2.3.tgz", - "integrity": "sha512-3BnB7Xx4WaQbcYucaUNN9wCqZlhWZKyp/emEdHa4SBNUcyQAMl3VuVo6Ulle0vScsyOKXhPXN+5ij3sv1dk02w==", + "version": "0.2.4", + "resolved": "https://registry.npmjs.org/@ztimson/ai-utils/-/ai-utils-0.2.4.tgz", + "integrity": "sha512-dCkUCTXlKDztXRta4LrfOmgmhxYwegG/0dEr9jNXgbFb8j0M6bS5y9ZOn3m/tK6NDNdUzHKDBA/QDl17Vu7oyA==", "license": "MIT", "dependencies": { "@anthropic-ai/sdk": "^0.67.0", diff --git a/package.json b/package.json index 6d4d020..696572e 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "license": "ISC", "description": "", "dependencies": { - "@ztimson/ai-utils": "0.2.3", + "@ztimson/ai-utils": "^0.2.4", "@ztimson/node-utils": "^1.0.7", "@ztimson/utils": "^0.28.3", "dotenv": "^17.2.3" diff --git a/src/review.mjs b/src/review.mjs index 7775768..1f00920 100644 --- a/src/review.mjs +++ b/src/review.mjs @@ -4,7 +4,6 @@ import * as os from 'node:os'; import * as path from 'node:path'; import * as fs from 'node:fs'; import * as dotenv from 'dotenv'; -import {execSync} from 'node:child_process'; dotenv.config(); dotenv.config({path: '.env.local', override: true}); @@ -24,10 +23,12 @@ dotenv.config({path: '.env.local', override: true}); const comments = []; const commit = await $`cd ${root} && git log -1 --pretty=format:%H`; - const remote = await $`cd ${root} && git symbolic-ref refs/remotes/origin/HEAD`; - const gitDiff = execSync(`git diff ${remote}...HEAD`, {cwd: root, encoding: 'utf-8'}); + const target = await $`cd ${root} && git rev-parse HEAD`; + const dest = await $`cd ${root} && git symbolic-ref refs/remotes/origin/HEAD`; + const gitDiff = await $`git diff ${dest} ${target}`; + const markdown = !!git; - console.log(`Inspecting: ${root} (${commit})`); + console.log(`Inspecting: ${root} (${commit})\n`); let options = {ollama: {model, host}}; if(host === 'anthropic') options = {anthropic: {model, token}}; @@ -36,7 +37,7 @@ dotenv.config({path: '.env.local', override: true}); ...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.', + 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 summary using 100 words or less in ${markdown ? 'markdown' : 'unstyled text'}.`, tools: [{ name: 'read_file', description: 'Read contents of a file', @@ -56,7 +57,7 @@ dotenv.config({path: '.env.local', override: true}); 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}, + line: {type: 'number', description: 'Line number in new file'}, comment: {type: 'string', description: 'Review comment explaining the issue'} }, fn: (args) => { @@ -75,7 +76,8 @@ dotenv.config({path: '.env.local', override: true}); return process.exit(); } - const summary = await ai.language.ask(gitDiff); + const messages = await ai.language.ask(gitDiff); + const summary = messages.pop().content; if(git) { const res = await fetch(`${git}/api/v1/repos/${owner}/${repo}/pulls/${pr}/reviews`, { method: 'POST', @@ -91,7 +93,6 @@ dotenv.config({path: '.env.local', override: true}); }) }); if(!res.ok) throw new Error(`${res.status} ${await res.text()}`); - } else { - console.log(comments.map(c => `${c.path}:${c.new_position}\n${c.body}`).join('\n\n') + '\n\n' + summary); } + console.log(comments.map(c => `${c.path}${c.new_position ? `:${c.new_position}` : ''}\n${c.body}`).join('\n\n') + '\n\n' + summary); })();