Use Responses API for reviews

This commit is contained in:
lezi-fun committed 2026-05-12 22:23:19 +08:00
1 parent 42853077c7
commit 363b218b0e
7 files changed
+54 -22

No files matched your search

+2
View File
@@ -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
+5
View File
@@ -0,0 +1,5 @@
dist
node_modules
.env
.env.*
.vercel
+2
View File
@@ -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`.
+2 -3
View File
@@ -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." });
+6
View File
@@ -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,
+12 -11
View File
@@ -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<ReviewDecision> {
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.");
}
+25 -8
View File
@@ -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
}
]
}