diff --git a/.env.example b/.env.example index afb98b7..2e7e42b 100644 --- a/.env.example +++ b/.env.example @@ -1,9 +1,9 @@ PORT=3000 GITHUB_TOKEN=ghp_your_token_here -OPENAI_API_KEY=sk-... -OPENAI_BASE_URL=https://api.openai.com/v1 -OPENAI_MODEL=gpt-4.1 -OPENAI_REASONING_EFFORT=medium +CODEX_API_KEY=sk-... +CODEX_BASE_URL=https://api.openai.com/v1 +CODEX_MODEL=gpt-5.4 +CODEX_REASONING_EFFORT=high # Optional controls BOT_NAME=github-actions[bot] diff --git a/.github/workflows/review.yml b/.github/workflows/review.yml index a92839e..0359cca 100644 --- a/.github/workflows/review.yml +++ b/.github/workflows/review.yml @@ -35,15 +35,17 @@ jobs: with: node-version: 24 cache: npm + - name: Install Codex CLI + run: npm install -g @openai/codex - run: npm ci - run: npm run build - run: node dist/src/actions/runReview.js env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} - OPENAI_BASE_URL: ${{ secrets.OPENAI_BASE_URL }} - OPENAI_MODEL: ${{ secrets.OPENAI_MODEL }} - OPENAI_REASONING_EFFORT: ${{ secrets.OPENAI_REASONING_EFFORT }} + CODEX_API_KEY: ${{ secrets.CODEX_API_KEY }} + CODEX_BASE_URL: ${{ vars.CODEX_BASE_URL || 'https://api.openai.com/v1' }} + CODEX_MODEL: ${{ vars.CODEX_MODEL || 'gpt-5.4' }} + CODEX_REASONING_EFFORT: ${{ vars.CODEX_REASONING_EFFORT || 'high' }} BOT_NAME: github-actions[bot] LENIENT_APPROVAL_USER: lezi-fun AUTO_MERGE: ${{ vars.AUTO_MERGE || 'false' }} diff --git a/README.md b/README.md index 1682d62..6bdd245 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ # ghbot -GitHub Actions bot that reviews pull requests with an AI reviewer, leaves inline review comments for problems, approves clean PRs, and can merge them when repository checks are green. +GitHub Actions bot that reviews pull requests by calling Codex CLI, leaves inline review comments for problems, approves clean PRs, and can merge them when repository checks are green. ## Behavior - Triggers on `pull_request_target`: `opened`, `synchronize`, `reopened`, `ready_for_review`. - When a new PR is opened, posts an immediate comment that review has started. - Fetches changed files and patches from the pull request. -- Asks the reviewer for a structured decision: +- Asks Codex CLI for a structured decision: - `safeToMerge=true` only when there are no blocking findings. - `blocking` findings become request-changes reviews. - `suggestion` findings are included as non-blocking suggestions. @@ -18,7 +18,7 @@ GitHub Actions bot that reviews pull requests with an AI reviewer, leaves inline - If the reviewer detects clearly malicious code, the bot comments with the reason and closes the pull request. - Posts inline comments only on valid added diff lines. - Approves clean pull requests. -- Retries transient OpenAI and GitHub API request failures up to 5 times before giving up. +- Retries transient Codex CLI and GitHub API request failures up to 5 times before giving up. - If `AUTO_MERGE=true`, merges only after: - the AI reviewer says the PR is safe, - GitHub reports the PR as mergeable, @@ -28,19 +28,20 @@ GitHub Actions bot that reviews pull requests with an AI reviewer, leaves inline Add these repository secrets: -- `OPENAI_API_KEY` -- `OPENAI_BASE_URL` -- `OPENAI_MODEL` -- `OPENAI_REASONING_EFFORT` +- `CODEX_API_KEY` Optional repository variables: +- `CODEX_BASE_URL`: defaults to `https://api.openai.com/v1` +- `CODEX_MODEL`: defaults to `gpt-5.4` +- `CODEX_REASONING_EFFORT`: `minimal`, `low`, `medium`, `high`, or `xhigh`, defaults to `high` - `AUTO_MERGE`: defaults to `false` - `MERGE_METHOD`: `merge`, `squash`, or `rebase`, defaults to `squash` - `REQUIRE_CHECKS`: defaults to `true` - `MAX_PATCH_CHARS`: defaults to `120000` The workflow uses the repository `GITHUB_TOKEN`, so you do not need a GitHub App, webhook endpoint, Vercel deployment, or a self-hosted listener. +It installs Codex CLI inside the GitHub Actions runner and uses that CLI for the review itself. The workflow writes an isolated Codex `config.toml` at runtime so each run gets an explicit model, provider base URL, and reasoning setting. ## Required workflow permissions @@ -95,10 +96,10 @@ See [.env.example](/Users/home/Projects/ghbot/.env.example:1) for local testing Important variables: - `GITHUB_TOKEN`: GitHub token used by the workflow or local simulation -- `OPENAI_API_KEY`: API key for the reviewer model -- `OPENAI_BASE_URL`: optional OpenAI-compatible API base URL -- `OPENAI_MODEL`: defaults to `gpt-4.1` -- `OPENAI_REASONING_EFFORT`: optional reasoning effort, one of `low`, `medium`, or `high` +- `CODEX_API_KEY`: API key used by Codex CLI +- `CODEX_BASE_URL`: optional Codex/OpenAI-compatible base URL +- `CODEX_MODEL`: defaults to `gpt-5.4` +- `CODEX_REASONING_EFFORT`: optional, one of `minimal`, `low`, `medium`, `high`, `xhigh` - `BOT_NAME`: defaults to `ghbot`, but the workflow sets it to `github-actions[bot]` - `LENIENT_APPROVAL_USER`: defaults to `lezi-fun` - `AUTO_MERGE`: defaults to `false` diff --git a/package-lock.json b/package-lock.json index eda6c6f..57a6229 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,6 @@ "dependencies": { "@octokit/rest": "^21.0.2", "dotenv": "^16.4.7", - "openai": "^4.77.0", "pino": "^9.5.0", "zod": "^3.24.1" }, @@ -659,51 +658,12 @@ "version": "22.19.19", "resolved": "https://registry.npmjs.org/@types/node/-/node-22.19.19.tgz", "integrity": "sha512-dyh/xO2Fh5bYrfWaaqGrRQQGkNdmYw6AmaAUvYeUMNTWQtvb796ikLdmTchRmOlOiIJ1TDXfWgVx1QkUlQ6Hew==", + "dev": true, "license": "MIT", "dependencies": { "undici-types": "~6.21.0" } }, - "node_modules/@types/node-fetch": { - "version": "2.6.13", - "resolved": "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-2.6.13.tgz", - "integrity": "sha512-QGpRVpzSaUs30JBSGPjOg4Uveu384erbHBoT1zeONvyCfwQxIkUshLAOqN/k9EjGviPRmWTTe6aH2qySWKTVSw==", - "license": "MIT", - "dependencies": { - "@types/node": "*", - "form-data": "^4.0.4" - } - }, - "node_modules/abort-controller": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/abort-controller/-/abort-controller-3.0.0.tgz", - "integrity": "sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==", - "license": "MIT", - "dependencies": { - "event-target-shim": "^5.0.0" - }, - "engines": { - "node": ">=6.5" - } - }, - "node_modules/agentkeepalive": { - "version": "4.6.0", - "resolved": "https://registry.npmjs.org/agentkeepalive/-/agentkeepalive-4.6.0.tgz", - "integrity": "sha512-kja8j7PjmncONqaTsB8fQ+wE2mSU2DJ9D4XKoJ5PFWIdRMa6SLSN1ff4mOr4jCbfRSsxR4keIiySJU0N9T5hIQ==", - "license": "MIT", - "dependencies": { - "humanize-ms": "^1.2.1" - }, - "engines": { - "node": ">= 8.0.0" - } - }, - "node_modules/asynckit": { - "version": "0.4.0", - "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", - "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", - "license": "MIT" - }, "node_modules/atomic-sleep": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/atomic-sleep/-/atomic-sleep-1.0.0.tgz", @@ -719,40 +679,6 @@ "integrity": "sha512-Nik3Sc0ncrMK4UUdXQmAnRtzmNQTAAXmXIopizwZ1W1t8QmfJj+zL4OA2I7XPTPW5z5TDqv4hRo/JzouDJnX3A==", "license": "Apache-2.0" }, - "node_modules/call-bind-apply-helpers": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/call-bind-apply-helpers/-/call-bind-apply-helpers-1.0.2.tgz", - "integrity": "sha512-Sp1ablJ0ivDkSzjcaJdxEunN5/XvksFJ2sMBFfq6x0ryhQV/2b/KwFe21cMpmHtPOSij8K99/wSfoEuTObmuMQ==", - "license": "MIT", - "dependencies": { - "es-errors": "^1.3.0", - "function-bind": "^1.1.2" - }, - "engines": { - "node": ">= 0.4" - } - }, - "node_modules/combined-stream": { - "version": "1.0.8", - "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", - "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", - "license": "MIT", - "dependencies": { - "delayed-stream": "~1.0.0" - }, - "engines": { - "node": ">= 0.8" - } - }, - "node_modules/delayed-stream": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", - "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", - "license": "MIT", - "engines": { - "node": ">=0.4.0" - } - }, "node_modules/dotenv": { "version": "16.6.1", "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.6.1.tgz", @@ -765,65 +691,6 @@ "url": "https://dotenvx.com" } }, - "node_modules/dunder-proto": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz", - "integrity": "sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==", - "license": "MIT", - "dependencies": { - "call-bind-apply-helpers": "^1.0.1", - "es-errors": "^1.3.0", - "gopd": "^1.2.0" - }, - "engines": { - "node": ">= 0.4" - } - }, - "node_modules/es-define-property": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz", - "integrity": "sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g==", - "license": "MIT", - "engines": { - "node": ">= 0.4" - } - }, - "node_modules/es-errors": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/es-errors/-/es-errors-1.3.0.tgz", - "integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==", - "license": "MIT", - "engines": { - "node": ">= 0.4" - } - }, - "node_modules/es-object-atoms": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/es-object-atoms/-/es-object-atoms-1.1.1.tgz", - "integrity": "sha512-FGgH2h8zKNim9ljj7dankFPcICIK9Cp5bm+c2gQSYePhpaG5+esrLODihIorn+Pe6FGJzWhXQotPv73jTaldXA==", - "license": "MIT", - "dependencies": { - "es-errors": "^1.3.0" - }, - "engines": { - "node": ">= 0.4" - } - }, - "node_modules/es-set-tostringtag": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/es-set-tostringtag/-/es-set-tostringtag-2.1.0.tgz", - "integrity": "sha512-j6vWzfrGVfyXxge+O0x5sh6cvxAog0a/4Rdd2K36zCMV5eJ+/+tOAngRO8cODMNWbVRdVlmGZQL2YS3yR8bIUA==", - "license": "MIT", - "dependencies": { - "es-errors": "^1.3.0", - "get-intrinsic": "^1.2.6", - "has-tostringtag": "^1.0.2", - "hasown": "^2.0.2" - }, - "engines": { - "node": ">= 0.4" - } - }, "node_modules/esbuild": { "version": "0.27.0", "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.27.0.tgz", @@ -866,15 +733,6 @@ "@esbuild/win32-x64": "0.27.0" } }, - "node_modules/event-target-shim": { - "version": "5.0.1", - "resolved": "https://registry.npmjs.org/event-target-shim/-/event-target-shim-5.0.1.tgz", - "integrity": "sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==", - "license": "MIT", - "engines": { - "node": ">=6" - } - }, "node_modules/fast-content-type-parse": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/fast-content-type-parse/-/fast-content-type-parse-2.0.1.tgz", @@ -891,41 +749,6 @@ ], "license": "MIT" }, - "node_modules/form-data": { - "version": "4.0.5", - "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.5.tgz", - "integrity": "sha512-8RipRLol37bNs2bhoV67fiTEvdTrbMUYcFTiy3+wuuOnUog2QBHCZWXDRijWQfAkhBj2Uf5UnVaiWwA5vdd82w==", - "license": "MIT", - "dependencies": { - "asynckit": "^0.4.0", - "combined-stream": "^1.0.8", - "es-set-tostringtag": "^2.1.0", - "hasown": "^2.0.2", - "mime-types": "^2.1.12" - }, - "engines": { - "node": ">= 6" - } - }, - "node_modules/form-data-encoder": { - "version": "1.7.2", - "resolved": "https://registry.npmjs.org/form-data-encoder/-/form-data-encoder-1.7.2.tgz", - "integrity": "sha512-qfqtYan3rxrnCk1VYaA4H+Ms9xdpPqvLZa6xmMgFvhO32x7/3J/ExcTd6qpxM0vH2GdMI+poehyBZvqfMTto8A==", - "license": "MIT" - }, - "node_modules/formdata-node": { - "version": "4.4.1", - "resolved": "https://registry.npmjs.org/formdata-node/-/formdata-node-4.4.1.tgz", - "integrity": "sha512-0iirZp3uVDjVGt9p49aTaqjk84TrglENEDuqfdlZQ1roC9CWlPk6Avf8EEnZNcAqPonwkG35x4n3ww/1THYAeQ==", - "license": "MIT", - "dependencies": { - "node-domexception": "1.0.0", - "web-streams-polyfill": "4.0.0-beta.3" - }, - "engines": { - "node": ">= 12.20" - } - }, "node_modules/fsevents": { "version": "2.3.3", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", @@ -941,52 +764,6 @@ "node": "^8.16.0 || ^10.6.0 || >=11.0.0" } }, - "node_modules/function-bind": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", - "integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==", - "license": "MIT", - "funding": { - "url": "https://github.com/sponsors/ljharb" - } - }, - "node_modules/get-intrinsic": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.3.0.tgz", - "integrity": "sha512-9fSjSaos/fRIVIp+xSJlE6lfwhES7LNtKaCBIamHsjr2na1BiABJPo0mOjjz8GJDURarmCPGqaiVg5mfjb98CQ==", - "license": "MIT", - "dependencies": { - "call-bind-apply-helpers": "^1.0.2", - "es-define-property": "^1.0.1", - "es-errors": "^1.3.0", - "es-object-atoms": "^1.1.1", - "function-bind": "^1.1.2", - "get-proto": "^1.0.1", - "gopd": "^1.2.0", - "has-symbols": "^1.1.0", - "hasown": "^2.0.2", - "math-intrinsics": "^1.1.0" - }, - "engines": { - "node": ">= 0.4" - }, - "funding": { - "url": "https://github.com/sponsors/ljharb" - } - }, - "node_modules/get-proto": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/get-proto/-/get-proto-1.0.1.tgz", - "integrity": "sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g==", - "license": "MIT", - "dependencies": { - "dunder-proto": "^1.0.1", - "es-object-atoms": "^1.0.0" - }, - "engines": { - "node": ">= 0.4" - } - }, "node_modules/get-tsconfig": { "version": "4.14.0", "resolved": "https://registry.npmjs.org/get-tsconfig/-/get-tsconfig-4.14.0.tgz", @@ -1000,142 +777,6 @@ "url": "https://github.com/privatenumber/get-tsconfig?sponsor=1" } }, - "node_modules/gopd": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/gopd/-/gopd-1.2.0.tgz", - "integrity": "sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg==", - "license": "MIT", - "engines": { - "node": ">= 0.4" - }, - "funding": { - "url": "https://github.com/sponsors/ljharb" - } - }, - "node_modules/has-symbols": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.1.0.tgz", - "integrity": "sha512-1cDNdwJ2Jaohmb3sg4OmKaMBwuC48sYni5HUw2DvsC8LjGTLK9h+eb1X6RyuOHe4hT0ULCW68iomhjUoKUqlPQ==", - "license": "MIT", - "engines": { - "node": ">= 0.4" - }, - "funding": { - "url": "https://github.com/sponsors/ljharb" - } - }, - "node_modules/has-tostringtag": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/has-tostringtag/-/has-tostringtag-1.0.2.tgz", - "integrity": "sha512-NqADB8VjPFLM2V0VvHUewwwsw0ZWBaIdgo+ieHtK3hasLz4qeCRjYcqfB6AQrBggRKppKF8L52/VqdVsO47Dlw==", - "license": "MIT", - "dependencies": { - "has-symbols": "^1.0.3" - }, - "engines": { - "node": ">= 0.4" - }, - "funding": { - "url": "https://github.com/sponsors/ljharb" - } - }, - "node_modules/hasown": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.3.tgz", - "integrity": "sha512-ej4AhfhfL2Q2zpMmLo7U1Uv9+PyhIZpgQLGT1F9miIGmiCJIoCgSmczFdrc97mWT4kVY72KA+WnnhJ5pghSvSg==", - "license": "MIT", - "dependencies": { - "function-bind": "^1.1.2" - }, - "engines": { - "node": ">= 0.4" - } - }, - "node_modules/humanize-ms": { - "version": "1.2.1", - "resolved": "https://registry.npmjs.org/humanize-ms/-/humanize-ms-1.2.1.tgz", - "integrity": "sha512-Fl70vYtsAFb/C06PTS9dZBo7ihau+Tu/DNCk/OyHhea07S+aeMWpFFkUaXRa8fI+ScZbEI8dfSxwY7gxZ9SAVQ==", - "license": "MIT", - "dependencies": { - "ms": "^2.0.0" - } - }, - "node_modules/math-intrinsics": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz", - "integrity": "sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g==", - "license": "MIT", - "engines": { - "node": ">= 0.4" - } - }, - "node_modules/mime-db": { - "version": "1.52.0", - "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", - "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", - "license": "MIT", - "engines": { - "node": ">= 0.6" - } - }, - "node_modules/mime-types": { - "version": "2.1.35", - "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", - "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", - "license": "MIT", - "dependencies": { - "mime-db": "1.52.0" - }, - "engines": { - "node": ">= 0.6" - } - }, - "node_modules/ms": { - "version": "2.1.3", - "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", - "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", - "license": "MIT" - }, - "node_modules/node-domexception": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz", - "integrity": "sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==", - "deprecated": "Use your platform's native DOMException instead", - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/jimmywarting" - }, - { - "type": "github", - "url": "https://paypal.me/jimmywarting" - } - ], - "license": "MIT", - "engines": { - "node": ">=10.5.0" - } - }, - "node_modules/node-fetch": { - "version": "2.6.9", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.9.tgz", - "integrity": "sha512-DJm/CJkZkRjKKj4Zi4BsKVZh3ValV5IR5s7LVZnW+6YMh0W1BfNA8XSs6DLMGYlId5F3KnA70uu2qepcR08Qqg==", - "license": "MIT", - "dependencies": { - "whatwg-url": "^5.0.0" - }, - "engines": { - "node": "4.x || >=6.0.0" - }, - "peerDependencies": { - "encoding": "^0.1.0" - }, - "peerDependenciesMeta": { - "encoding": { - "optional": true - } - } - }, "node_modules/on-exit-leak-free": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/on-exit-leak-free/-/on-exit-leak-free-2.1.2.tgz", @@ -1145,51 +786,6 @@ "node": ">=14.0.0" } }, - "node_modules/openai": { - "version": "4.104.0", - "resolved": "https://registry.npmjs.org/openai/-/openai-4.104.0.tgz", - "integrity": "sha512-p99EFNsA/yX6UhVO93f5kJsDRLAg+CTA2RBqdHK4RtK8u5IJw32Hyb2dTGKbnnFmnuoBv5r7Z2CURI9sGZpSuA==", - "license": "Apache-2.0", - "dependencies": { - "@types/node": "^18.11.18", - "@types/node-fetch": "^2.6.4", - "abort-controller": "^3.0.0", - "agentkeepalive": "^4.2.1", - "form-data-encoder": "1.7.2", - "formdata-node": "^4.3.2", - "node-fetch": "^2.6.7" - }, - "bin": { - "openai": "bin/cli" - }, - "peerDependencies": { - "ws": "^8.18.0", - "zod": "^3.23.8" - }, - "peerDependenciesMeta": { - "ws": { - "optional": true - }, - "zod": { - "optional": true - } - } - }, - "node_modules/openai/node_modules/@types/node": { - "version": "18.19.130", - "resolved": "https://registry.npmjs.org/@types/node/-/node-18.19.130.tgz", - "integrity": "sha512-GRaXQx6jGfL8sKfaIDD6OupbIHBr9jv7Jnaml9tB7l4v068PAOXqfcujMMo5PhbIs6ggR1XODELqahT2R8v0fg==", - "license": "MIT", - "dependencies": { - "undici-types": "~5.26.4" - } - }, - "node_modules/openai/node_modules/undici-types": { - "version": "5.26.5", - "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", - "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", - "license": "MIT" - }, "node_modules/pino": { "version": "9.14.0", "resolved": "https://registry.npmjs.org/pino/-/pino-9.14.0.tgz", @@ -1304,12 +900,6 @@ "real-require": "^0.2.0" } }, - "node_modules/tr46": { - "version": "0.0.3", - "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", - "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==", - "license": "MIT" - }, "node_modules/tsx": { "version": "4.21.0", "resolved": "https://registry.npmjs.org/tsx/-/tsx-4.21.0.tgz", @@ -1348,6 +938,7 @@ "version": "6.21.0", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.21.0.tgz", "integrity": "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==", + "dev": true, "license": "MIT" }, "node_modules/universal-user-agent": { @@ -1356,31 +947,6 @@ "integrity": "sha512-TmnEAEAsBJVZM/AADELsK76llnwcf9vMKuPz8JflO1frO8Lchitr0fNaN9d+Ap0BjKtqWqd/J17qeDnXh8CL2A==", "license": "ISC" }, - "node_modules/web-streams-polyfill": { - "version": "4.0.0-beta.3", - "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-4.0.0-beta.3.tgz", - "integrity": "sha512-QW95TCTaHmsYfHDybGMwO5IJIM93I/6vTRk+daHTWFPhwh+C8Cg7j7XyKrwrj8Ib6vYXe0ocYNrmzY4xAAN6ug==", - "license": "MIT", - "engines": { - "node": ">= 14" - } - }, - "node_modules/webidl-conversions": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", - "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==", - "license": "BSD-2-Clause" - }, - "node_modules/whatwg-url": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", - "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", - "license": "MIT", - "dependencies": { - "tr46": "~0.0.3", - "webidl-conversions": "^3.0.0" - } - }, "node_modules/zod": { "version": "3.25.76", "resolved": "https://registry.npmjs.org/zod/-/zod-3.25.76.tgz", diff --git a/package.json b/package.json index 9ade891..6fd63e0 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,6 @@ "dependencies": { "@octokit/rest": "^21.0.2", "dotenv": "^16.4.7", - "openai": "^4.77.0", "pino": "^9.5.0", "zod": "^3.24.1" }, diff --git a/src/config.ts b/src/config.ts index 700b18e..53e652d 100644 --- a/src/config.ts +++ b/src/config.ts @@ -5,10 +5,6 @@ const optionalString = z.preprocess((value) => { return value === "" ? undefined : value; }, z.string().optional()); -const optionalNonEmptyString = z.preprocess((value) => { - return value === "" ? undefined : value; -}, z.string().min(1).optional()); - const envBoolean = z.preprocess((value) => { if (typeof value !== "string") { return value; @@ -33,14 +29,12 @@ const envBoolean = z.preprocess((value) => { const configSchema = z.object({ port: z.coerce.number().int().positive().default(3000), githubToken: z.string().min(1), - openAiApiKey: z.string().min(1), - openAiBaseUrl: optionalString.pipe(z.string().url().optional()), - openAiModel: optionalNonEmptyString.default("gpt-4.1"), - openAiReasoningEffort: z.preprocess((value) => { + codexModel: optionalString.default("gpt-5.4"), + codexReasoningEffort: z.preprocess((value) => { return value === "" ? undefined : value; - }, z.enum(["default", "low", "medium", "high"]).optional()).transform((value) => { - return value === "default" ? undefined : value; - }), + }, z.enum(["minimal", "low", "medium", "high", "xhigh"]).optional()), + codexBaseUrl: optionalString, + codexApiKey: z.string().min(1), botName: z.string().min(1).default("ghbot"), lenientApprovalUser: z.string().min(1).default("lezi-fun"), autoMerge: envBoolean.default(false), @@ -53,10 +47,10 @@ const configSchema = z.object({ export const config = configSchema.parse({ port: process.env.PORT, githubToken: process.env.GITHUB_TOKEN, - openAiApiKey: process.env.OPENAI_API_KEY, - openAiBaseUrl: process.env.OPENAI_BASE_URL, - openAiModel: process.env.OPENAI_MODEL, - openAiReasoningEffort: process.env.OPENAI_REASONING_EFFORT, + codexModel: process.env.CODEX_MODEL, + codexReasoningEffort: process.env.CODEX_REASONING_EFFORT, + codexBaseUrl: process.env.CODEX_BASE_URL, + codexApiKey: process.env.CODEX_API_KEY, botName: process.env.BOT_NAME, lenientApprovalUser: process.env.LENIENT_APPROVAL_USER, autoMerge: process.env.AUTO_MERGE, diff --git a/src/review/codexCliReviewer.ts b/src/review/codexCliReviewer.ts new file mode 100644 index 0000000..980502c --- /dev/null +++ b/src/review/codexCliReviewer.ts @@ -0,0 +1,234 @@ +import fs from "node:fs/promises"; +import os from "node:os"; +import path from "node:path"; +import { execFile } from "node:child_process"; +import { promisify } from "node:util"; +import { z } from "zod"; +import { config } from "../config.js"; +import { logger } from "../logger.js"; +import { withRetry } from "../retry.js"; +import type { PullRequestFile, ReviewDecision, ReviewMode } from "../types.js"; + +const execFileAsync = promisify(execFile); + +const reviewDecisionSchema = z.object({ + safeToMerge: z.boolean(), + shouldClosePullRequest: z.boolean(), + closeReason: z.string(), + summary: z.string(), + findings: z.array( + z.object({ + path: z.string(), + line: z.number().int().positive(), + severity: z.enum(["blocking", "suggestion"]), + title: z.string(), + body: z.string() + }) + ) +}); + +export class CodexCliReviewer { + async review(input: { + title: string; + body: string | null; + files: PullRequestFile[]; + mode: ReviewMode; + }): Promise { + return withRetry("codex.exec.review", async () => { + const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "ghbot-codex-")); + const schemaPath = path.join(tempDir, "review-schema.json"); + const outputPath = path.join(tempDir, "review-output.json"); + const codexHome = path.join(tempDir, "codex-home"); + + try { + await fs.mkdir(codexHome, { recursive: true }); + await fs.writeFile(path.join(codexHome, "config.toml"), buildCodexConfig(), "utf8"); + await fs.writeFile(schemaPath, JSON.stringify(buildSchema(), null, 2), "utf8"); + + const prompt = buildPrompt(input); + const args = [ + "exec", + "--skip-git-repo-check", + "--sandbox", + "workspace-write", + "--ask-for-approval", + "never", + "--output-schema", + schemaPath, + "--output-last-message", + outputPath, + "-C", + process.cwd(), + "--ephemeral" + ]; + + args.push(prompt); + + logger.info( + { + mode: input.mode, + fileCount: input.files.length, + model: config.codexModel, + reasoningEffort: config.codexReasoningEffort, + baseUrl: normalizeBaseUrl(config.codexBaseUrl) + }, + "Running Codex CLI review." + ); + + try { + await execFileAsync("codex", args, { + cwd: process.cwd(), + env: { + ...process.env, + CODEX_HOME: codexHome, + CODEX_API_KEY: config.codexApiKey + }, + maxBuffer: 10 * 1024 * 1024 + }); + } catch (error) { + logger.error( + { + error, + mode: input.mode, + fileCount: input.files.length, + model: config.codexModel, + reasoningEffort: config.codexReasoningEffort, + baseUrl: normalizeBaseUrl(config.codexBaseUrl) + }, + "Codex CLI review command failed." + ); + throw error; + } + + const raw = await fs.readFile(outputPath, "utf8"); + return reviewDecisionSchema.parse(JSON.parse(raw)); + } finally { + await fs.rm(tempDir, { recursive: true, force: true }); + } + }); + } +} + +function buildCodexConfig(): string { + const lines = [ + `model = ${toTomlString(config.codexModel)}`, + 'model_provider = "ghbot"', + 'approval_policy = "never"', + 'sandbox_mode = "workspace-write"' + ]; + + if (config.codexReasoningEffort) { + lines.push(`model_reasoning_effort = ${toTomlString(config.codexReasoningEffort)}`); + } + + lines.push("", "[model_providers.ghbot]"); + lines.push('name = "ghbot"'); + lines.push(`base_url = ${toTomlString(normalizeBaseUrl(config.codexBaseUrl))}`); + lines.push('env_key = "CODEX_API_KEY"'); + lines.push('wire_api = "responses"'); + lines.push("requires_openai_auth = false"); + + return `${lines.join("\n")}\n`; +} + +function normalizeBaseUrl(value: string | undefined): string { + const baseUrl = value?.trim(); + if (!baseUrl) { + return "https://api.openai.com/v1"; + } + + return baseUrl.replace(/\/+$/, ""); +} + +function toTomlString(value: string): string { + return JSON.stringify(value); +} + +function buildSchema() { + return { + type: "object", + additionalProperties: false, + required: ["safeToMerge", "shouldClosePullRequest", "closeReason", "summary", "findings"], + properties: { + safeToMerge: { type: "boolean" }, + shouldClosePullRequest: { type: "boolean" }, + closeReason: { type: "string" }, + summary: { type: "string" }, + findings: { + type: "array", + items: { + type: "object", + additionalProperties: false, + required: ["path", "line", "severity", "title", "body"], + properties: { + path: { type: "string" }, + line: { type: "integer", minimum: 1 }, + severity: { type: "string", enum: ["blocking", "suggestion"] }, + title: { type: "string" }, + body: { type: "string" } + } + } + } + } + }; +} + +function buildPrompt(input: { + title: string; + body: string | null; + files: PullRequestFile[]; + mode: ReviewMode; +}): string { + return [ + buildSystemPrompt(input.mode), + "", + "Pull request payload:", + JSON.stringify( + { + pullRequest: { + title: input.title, + body: input.body ?? "" + }, + files: input.files.map((file) => ({ + path: file.filename, + status: file.status, + additions: file.additions, + deletions: file.deletions, + patch: file.patch ?? "" + })) + }, + null, + 2 + ) + ].join("\n"); +} + +function buildSystemPrompt(mode: ReviewMode): string { + const commonRules = [ + "You are a senior software engineer reviewing a GitHub pull request.", + "Return JSON that exactly matches the provided schema.", + "Only set safeToMerge=true when there are no blocking findings.", + "Set shouldClosePullRequest=true only for clearly malicious code: backdoors, credential theft, token exfiltration, destructive commands, malware, hidden persistence, privilege escalation, supply-chain compromise, or intentionally abusive behavior.", + "Do not set shouldClosePullRequest=true for ordinary bugs, crashes, failing tests, incomplete code, suspicious-but-unproven code, or low-quality changes.", + "When shouldClosePullRequest=true, explain the evidence in closeReason. Otherwise closeReason must be an empty string.", + "For each finding, choose a line number that exists on an added line in the supplied patch whenever possible.", + "Do not invent files, line numbers, test results, or runtime behavior." + ]; + + if (mode === "lenient") { + return [ + ...commonRules, + "This is a lenient review requested by the pull request flow.", + "Only report issues that are dangerous, affect runtime behavior, can cause errors, can crash, can break builds/tests, can lose data, or create clear security problems.", + "Do not block on style, naming, subjective maintainability, small refactors, missing comments, formatting, or non-dangerous best-practice preferences.", + "Use suggestion severity only when the issue is still runtime-relevant but not necessarily blocking." + ].join(" "); + } + + return [ + ...commonRules, + "This is a strict review.", + "Find correctness bugs, security issues, data-loss risks, broken tests, bad error handling, and maintainability problems.", + "Use suggestion severity for non-blocking style or clarity improvements." + ].join(" "); +} diff --git a/src/review/openaiReviewer.ts b/src/review/openaiReviewer.ts deleted file mode 100644 index 79a7e59..0000000 --- a/src/review/openaiReviewer.ts +++ /dev/null @@ -1,131 +0,0 @@ -import { OpenAI } from "openai"; -import { z } from "zod"; -import { config } from "../config.js"; -import { withRetry } from "../retry.js"; -import type { PullRequestFile, ReviewDecision, ReviewMode } from "../types.js"; - -const reviewDecisionSchema = z.object({ - safeToMerge: z.boolean(), - shouldClosePullRequest: z.boolean(), - closeReason: z.string(), - summary: z.string(), - findings: z.array( - z.object({ - path: z.string(), - line: z.number().int().positive(), - severity: z.enum(["blocking", "suggestion"]), - title: z.string(), - body: z.string() - }) - ) -}); - -export class OpenAiReviewer { - private readonly client = new OpenAI({ - apiKey: config.openAiApiKey, - baseURL: config.openAiBaseUrl - }); - - async review(input: { - title: string; - body: string | null; - files: PullRequestFile[]; - mode: ReviewMode; - }): Promise { - const response = await withRetry("openai.responses.create", async () => { - return this.client.responses.create({ - model: config.openAiModel, - temperature: 0.1, - ...(config.openAiReasoningEffort ? { reasoning: { effort: config.openAiReasoningEffort } } : {}), - text: { - format: { - name: "pull_request_review", - type: "json_schema", - strict: true, - schema: { - type: "object", - additionalProperties: false, - required: ["safeToMerge", "shouldClosePullRequest", "closeReason", "summary", "findings"], - properties: { - safeToMerge: { type: "boolean" }, - shouldClosePullRequest: { type: "boolean" }, - closeReason: { type: "string" }, - summary: { type: "string" }, - findings: { - type: "array", - items: { - type: "object", - additionalProperties: false, - required: ["path", "line", "severity", "title", "body"], - properties: { - path: { type: "string" }, - line: { type: "integer", minimum: 1 }, - severity: { type: "string", enum: ["blocking", "suggestion"] }, - title: { type: "string" }, - body: { type: "string" } - } - } - } - } - } - } - }, - instructions: buildSystemPrompt(input.mode), - input: [ - { - role: "user", - content: JSON.stringify({ - pullRequest: { - title: input.title, - body: input.body ?? "" - }, - files: input.files.map((file) => ({ - path: file.filename, - status: file.status, - additions: file.additions, - deletions: file.deletions, - patch: file.patch ?? "" - })) - }) - } - ] - }); - }); - - const raw = response.output_text; - if (!raw) { - throw new Error("OpenAI returned an empty review response."); - } - - return reviewDecisionSchema.parse(JSON.parse(raw)); - } -} - -function buildSystemPrompt(mode: ReviewMode): string { - const commonRules = [ - "You are a senior software engineer reviewing a GitHub pull request.", - "Only set safeToMerge=true when there are no blocking findings.", - "Set shouldClosePullRequest=true only for clearly malicious code: backdoors, credential theft, token exfiltration, destructive commands, malware, hidden persistence, privilege escalation, supply-chain compromise, or intentionally abusive behavior.", - "Do not set shouldClosePullRequest=true for ordinary bugs, crashes, failing tests, incomplete code, suspicious-but-unproven code, or low-quality changes.", - "When shouldClosePullRequest=true, explain the evidence in closeReason. Otherwise closeReason must be an empty string.", - "For each finding, choose a line number that exists on an added line in the supplied patch whenever possible.", - "Do not invent files, line numbers, test results, or runtime behavior." - ]; - - if (mode === "lenient") { - return [ - ...commonRules, - "This is a lenient review requested by the pull request author.", - "Only report issues that are dangerous, affect runtime behavior, can cause errors, can crash, can break builds/tests, can lose data, or create clear security problems.", - "Do not block on style, naming, subjective maintainability, small refactors, missing comments, formatting, or non-dangerous best-practice preferences.", - "Use suggestion severity only when the issue is still runtime-relevant but not necessarily blocking." - ].join(" "); - } - - return [ - ...commonRules, - "This is a strict review.", - "Find correctness bugs, security issues, data-loss risks, broken tests, bad error handling, and maintainability problems.", - "Use suggestion severity for non-blocking style or clarity improvements." - ].join(" "); -} diff --git a/src/review/processor.ts b/src/review/processor.ts index b4d2df8..5fb4174 100644 --- a/src/review/processor.ts +++ b/src/review/processor.ts @@ -5,11 +5,11 @@ import { collectValidNewLines, toDiffPosition } from "../github/diff.js"; import { logger } from "../logger.js"; import { withRetry } from "../retry.js"; import type { PullRequestFile, PullRequestRef, ReviewDecision, ReviewFinding, ReviewMode } from "../types.js"; +import { CodexCliReviewer } from "./codexCliReviewer.js"; import { formatReviewBody } from "./format.js"; -import { OpenAiReviewer } from "./openaiReviewer.js"; import { compactFilesForReview } from "./prompt.js"; -const reviewer = new OpenAiReviewer(); +const reviewer = new CodexCliReviewer(); const CHECK_RUN_NAME = "ghbot review"; export const LENIENT_COMMENT_COMMAND = "/lenient-check"; const ADMIN_RESPONSE_WINDOW_MS = 24 * 60 * 60 * 1000;