From 363b218b0edf423b2435d5d62dac1b156bdb5f9a Mon Sep 17 00:00:00 2001 From: lezi-fun <177434121+lezi-fun@users.noreply.github.com> Date: Tue, 12 May 2026 22:23:19 +0800 Subject: [PATCH] Use Responses API for reviews --- .env.example | 2 ++ .vercelignore | 5 +++++ README.md | 2 ++ api/github/webhooks.ts | 5 ++--- src/config.ts | 6 ++++++ src/review/openaiReviewer.ts | 23 ++++++++++++----------- vercel.json | 33 +++++++++++++++++++++++++-------- 7 files changed, 54 insertions(+), 22 deletions(-) create mode 100644 .vercelignore diff --git a/.env.example b/.env.example index ef0532b..b63f9c5 100644 --- a/.env.example +++ b/.env.example @@ -3,7 +3,9 @@ WEBHOOK_SECRET=replace-with-github-webhook-secret GITHUB_APP_ID=123456 GITHUB_PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\n...\n-----END RSA PRIVATE KEY-----" OPENAI_API_KEY=sk-... +OPENAI_BASE_URL=https://api.openai.com/v1 OPENAI_MODEL=gpt-4.1 +OPENAI_REASONING_EFFORT=medium # Optional controls BOT_NAME=ghbot diff --git a/.vercelignore b/.vercelignore new file mode 100644 index 0000000..a835de6 --- /dev/null +++ b/.vercelignore @@ -0,0 +1,5 @@ +dist +node_modules +.env +.env.* +.vercel diff --git a/README.md b/README.md index 9e67d39..4d04584 100644 --- a/README.md +++ b/README.md @@ -56,10 +56,12 @@ Required variables: - `GITHUB_APP_ID`: GitHub App ID. - `GITHUB_PRIVATE_KEY`: GitHub App private key. Use escaped `\n` line breaks in a single-line env var. - `OPENAI_API_KEY`: API key for the reviewer model. +- `OPENAI_BASE_URL`: optional OpenAI-compatible API base URL. Important optional variables: - `OPENAI_MODEL`: defaults to `gpt-4.1`. +- `OPENAI_REASONING_EFFORT`: optional reasoning effort, one of `low`, `medium`, or `high`. Use `default` or leave unset to omit the parameter. - `AUTO_MERGE`: defaults to `false`. Set to `true` only after testing on a non-critical repo. - `MERGE_METHOD`: `merge`, `squash`, or `rebase`. Defaults to `squash`. - `REQUIRE_CHECKS`: defaults to `true`. diff --git a/api/github/webhooks.ts b/api/github/webhooks.ts index 8561dc1..fd03931 100644 --- a/api/github/webhooks.ts +++ b/api/github/webhooks.ts @@ -1,6 +1,5 @@ import type { VercelRequest, VercelResponse } from "@vercel/node"; import { waitUntil } from "@vercel/functions"; -import { createGitHubWebhooks } from "../../src/github/webhookHandler.js"; import { logger } from "../../src/logger.js"; export const config = { @@ -9,8 +8,6 @@ export const config = { } }; -const webhooks = createGitHubWebhooks(); - export default async function handler(request: VercelRequest, response: VercelResponse) { if (request.method === "GET") { response.status(200).json({ ok: true }); @@ -35,6 +32,8 @@ export default async function handler(request: VercelRequest, response: VercelRe const payload = await readRawBody(request); try { + const { createGitHubWebhooks } = await import("../../src/github/webhookHandler.js"); + const webhooks = createGitHubWebhooks(); const verified = await webhooks.verify(payload, signature); if (!verified) { response.status(401).json({ error: "Invalid webhook signature." }); diff --git a/src/config.ts b/src/config.ts index 7074d9c..befd26d 100644 --- a/src/config.ts +++ b/src/config.ts @@ -7,7 +7,11 @@ const configSchema = z.object({ githubAppId: z.coerce.number().int().positive(), githubPrivateKey: z.string().min(1).transform((value) => value.replace(/\\n/g, "\n")), openAiApiKey: z.string().min(1), + openAiBaseUrl: z.string().url().optional(), openAiModel: z.string().min(1).default("gpt-4.1"), + openAiReasoningEffort: z.enum(["default", "low", "medium", "high"]).optional().transform((value) => { + return value === "default" ? undefined : value; + }), botName: z.string().min(1).default("ghbot"), lenientApprovalUser: z.string().min(1).default("lezi-fun"), autoMerge: z.coerce.boolean().default(false), @@ -22,7 +26,9 @@ export const config = configSchema.parse({ githubAppId: process.env.GITHUB_APP_ID, githubPrivateKey: process.env.GITHUB_PRIVATE_KEY, openAiApiKey: process.env.OPENAI_API_KEY, + openAiBaseUrl: process.env.OPENAI_BASE_URL, openAiModel: process.env.OPENAI_MODEL, + openAiReasoningEffort: process.env.OPENAI_REASONING_EFFORT, botName: process.env.BOT_NAME, lenientApprovalUser: process.env.LENIENT_APPROVAL_USER, autoMerge: process.env.AUTO_MERGE, diff --git a/src/review/openaiReviewer.ts b/src/review/openaiReviewer.ts index ab3c0fc..3a7b54c 100644 --- a/src/review/openaiReviewer.ts +++ b/src/review/openaiReviewer.ts @@ -20,7 +20,10 @@ const reviewDecisionSchema = z.object({ }); export class OpenAiReviewer { - private readonly client = new OpenAI({ apiKey: config.openAiApiKey }); + private readonly client = new OpenAI({ + apiKey: config.openAiApiKey, + baseURL: config.openAiBaseUrl + }); async review(input: { title: string; @@ -28,13 +31,14 @@ export class OpenAiReviewer { files: PullRequestFile[]; mode: ReviewMode; }): Promise { - const response = await this.client.chat.completions.create({ + const response = await this.client.responses.create({ model: config.openAiModel, temperature: 0.1, - response_format: { - type: "json_schema", - json_schema: { + ...(config.openAiReasoningEffort ? { reasoning: { effort: config.openAiReasoningEffort } } : {}), + text: { + format: { name: "pull_request_review", + type: "json_schema", strict: true, schema: { type: "object", @@ -64,11 +68,8 @@ export class OpenAiReviewer { } } }, - messages: [ - { - role: "system", - content: buildSystemPrompt(input.mode) - }, + instructions: buildSystemPrompt(input.mode), + input: [ { role: "user", content: JSON.stringify({ @@ -88,7 +89,7 @@ export class OpenAiReviewer { ] }); - const raw = response.choices[0]?.message.content; + const raw = response.output_text; if (!raw) { throw new Error("OpenAI returned an empty review response."); } diff --git a/vercel.json b/vercel.json index 468a37c..56d24df 100644 --- a/vercel.json +++ b/vercel.json @@ -1,13 +1,30 @@ { - "functions": { - "api/github/webhooks.ts": { - "maxDuration": 60 - } - }, - "rewrites": [ + "version": 2, + "builds": [ { - "source": "/webhooks/github", - "destination": "/api/github/webhooks" + "src": "api/github/webhooks.ts", + "use": "@vercel/node", + "config": { + "maxDuration": 60 + } + } + ], + "routes": [ + { + "src": "^/$", + "dest": "/api/github/webhooks.ts" + }, + { + "src": "^/api/github/webhooks$", + "dest": "/api/github/webhooks.ts" + }, + { + "src": "^/webhooks/github$", + "dest": "/api/github/webhooks.ts" + }, + { + "src": "^/(.*)$", + "status": 404 } ] }