Use Responses API for reviews
This commit is contained in:
1 parent
42853077c7
commit
363b218b0e
7 files changed
+54
-22
No files matched your search
@@ -3,7 +3,9 @@ WEBHOOK_SECRET=replace-with-github-webhook-secret
|
|||||||
GITHUB_APP_ID=123456
|
GITHUB_APP_ID=123456
|
||||||
GITHUB_PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\n...\n-----END RSA PRIVATE KEY-----"
|
GITHUB_PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\n...\n-----END RSA PRIVATE KEY-----"
|
||||||
OPENAI_API_KEY=sk-...
|
OPENAI_API_KEY=sk-...
|
||||||
|
OPENAI_BASE_URL=https://api.openai.com/v1
|
||||||
OPENAI_MODEL=gpt-4.1
|
OPENAI_MODEL=gpt-4.1
|
||||||
|
OPENAI_REASONING_EFFORT=medium
|
||||||
|
|
||||||
# Optional controls
|
# Optional controls
|
||||||
BOT_NAME=ghbot
|
BOT_NAME=ghbot
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
dist
|
||||||
|
node_modules
|
||||||
|
.env
|
||||||
|
.env.*
|
||||||
|
.vercel
|
||||||
@@ -56,10 +56,12 @@ Required variables:
|
|||||||
- `GITHUB_APP_ID`: GitHub App ID.
|
- `GITHUB_APP_ID`: GitHub App ID.
|
||||||
- `GITHUB_PRIVATE_KEY`: GitHub App private key. Use escaped `\n` line breaks in a single-line env var.
|
- `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_API_KEY`: API key for the reviewer model.
|
||||||
|
- `OPENAI_BASE_URL`: optional OpenAI-compatible API base URL.
|
||||||
|
|
||||||
Important optional variables:
|
Important optional variables:
|
||||||
|
|
||||||
- `OPENAI_MODEL`: defaults to `gpt-4.1`.
|
- `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.
|
- `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`.
|
- `MERGE_METHOD`: `merge`, `squash`, or `rebase`. Defaults to `squash`.
|
||||||
- `REQUIRE_CHECKS`: defaults to `true`.
|
- `REQUIRE_CHECKS`: defaults to `true`.
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import type { VercelRequest, VercelResponse } from "@vercel/node";
|
import type { VercelRequest, VercelResponse } from "@vercel/node";
|
||||||
import { waitUntil } from "@vercel/functions";
|
import { waitUntil } from "@vercel/functions";
|
||||||
import { createGitHubWebhooks } from "../../src/github/webhookHandler.js";
|
|
||||||
import { logger } from "../../src/logger.js";
|
import { logger } from "../../src/logger.js";
|
||||||
|
|
||||||
export const config = {
|
export const config = {
|
||||||
@@ -9,8 +8,6 @@ export const config = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const webhooks = createGitHubWebhooks();
|
|
||||||
|
|
||||||
export default async function handler(request: VercelRequest, response: VercelResponse) {
|
export default async function handler(request: VercelRequest, response: VercelResponse) {
|
||||||
if (request.method === "GET") {
|
if (request.method === "GET") {
|
||||||
response.status(200).json({ ok: true });
|
response.status(200).json({ ok: true });
|
||||||
@@ -35,6 +32,8 @@ export default async function handler(request: VercelRequest, response: VercelRe
|
|||||||
const payload = await readRawBody(request);
|
const payload = await readRawBody(request);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const { createGitHubWebhooks } = await import("../../src/github/webhookHandler.js");
|
||||||
|
const webhooks = createGitHubWebhooks();
|
||||||
const verified = await webhooks.verify(payload, signature);
|
const verified = await webhooks.verify(payload, signature);
|
||||||
if (!verified) {
|
if (!verified) {
|
||||||
response.status(401).json({ error: "Invalid webhook signature." });
|
response.status(401).json({ error: "Invalid webhook signature." });
|
||||||
|
|||||||
@@ -7,7 +7,11 @@ const configSchema = z.object({
|
|||||||
githubAppId: z.coerce.number().int().positive(),
|
githubAppId: z.coerce.number().int().positive(),
|
||||||
githubPrivateKey: z.string().min(1).transform((value) => value.replace(/\\n/g, "\n")),
|
githubPrivateKey: z.string().min(1).transform((value) => value.replace(/\\n/g, "\n")),
|
||||||
openAiApiKey: z.string().min(1),
|
openAiApiKey: z.string().min(1),
|
||||||
|
openAiBaseUrl: z.string().url().optional(),
|
||||||
openAiModel: z.string().min(1).default("gpt-4.1"),
|
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"),
|
botName: z.string().min(1).default("ghbot"),
|
||||||
lenientApprovalUser: z.string().min(1).default("lezi-fun"),
|
lenientApprovalUser: z.string().min(1).default("lezi-fun"),
|
||||||
autoMerge: z.coerce.boolean().default(false),
|
autoMerge: z.coerce.boolean().default(false),
|
||||||
@@ -22,7 +26,9 @@ export const config = configSchema.parse({
|
|||||||
githubAppId: process.env.GITHUB_APP_ID,
|
githubAppId: process.env.GITHUB_APP_ID,
|
||||||
githubPrivateKey: process.env.GITHUB_PRIVATE_KEY,
|
githubPrivateKey: process.env.GITHUB_PRIVATE_KEY,
|
||||||
openAiApiKey: process.env.OPENAI_API_KEY,
|
openAiApiKey: process.env.OPENAI_API_KEY,
|
||||||
|
openAiBaseUrl: process.env.OPENAI_BASE_URL,
|
||||||
openAiModel: process.env.OPENAI_MODEL,
|
openAiModel: process.env.OPENAI_MODEL,
|
||||||
|
openAiReasoningEffort: process.env.OPENAI_REASONING_EFFORT,
|
||||||
botName: process.env.BOT_NAME,
|
botName: process.env.BOT_NAME,
|
||||||
lenientApprovalUser: process.env.LENIENT_APPROVAL_USER,
|
lenientApprovalUser: process.env.LENIENT_APPROVAL_USER,
|
||||||
autoMerge: process.env.AUTO_MERGE,
|
autoMerge: process.env.AUTO_MERGE,
|
||||||
|
|||||||
@@ -20,7 +20,10 @@ const reviewDecisionSchema = z.object({
|
|||||||
});
|
});
|
||||||
|
|
||||||
export class OpenAiReviewer {
|
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: {
|
async review(input: {
|
||||||
title: string;
|
title: string;
|
||||||
@@ -28,13 +31,14 @@ export class OpenAiReviewer {
|
|||||||
files: PullRequestFile[];
|
files: PullRequestFile[];
|
||||||
mode: ReviewMode;
|
mode: ReviewMode;
|
||||||
}): Promise<ReviewDecision> {
|
}): Promise<ReviewDecision> {
|
||||||
const response = await this.client.chat.completions.create({
|
const response = await this.client.responses.create({
|
||||||
model: config.openAiModel,
|
model: config.openAiModel,
|
||||||
temperature: 0.1,
|
temperature: 0.1,
|
||||||
response_format: {
|
...(config.openAiReasoningEffort ? { reasoning: { effort: config.openAiReasoningEffort } } : {}),
|
||||||
type: "json_schema",
|
text: {
|
||||||
json_schema: {
|
format: {
|
||||||
name: "pull_request_review",
|
name: "pull_request_review",
|
||||||
|
type: "json_schema",
|
||||||
strict: true,
|
strict: true,
|
||||||
schema: {
|
schema: {
|
||||||
type: "object",
|
type: "object",
|
||||||
@@ -64,11 +68,8 @@ export class OpenAiReviewer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
messages: [
|
instructions: buildSystemPrompt(input.mode),
|
||||||
{
|
input: [
|
||||||
role: "system",
|
|
||||||
content: buildSystemPrompt(input.mode)
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
role: "user",
|
role: "user",
|
||||||
content: JSON.stringify({
|
content: JSON.stringify({
|
||||||
@@ -88,7 +89,7 @@ export class OpenAiReviewer {
|
|||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
const raw = response.choices[0]?.message.content;
|
const raw = response.output_text;
|
||||||
if (!raw) {
|
if (!raw) {
|
||||||
throw new Error("OpenAI returned an empty review response.");
|
throw new Error("OpenAI returned an empty review response.");
|
||||||
}
|
}
|
||||||
|
|||||||
+25
-8
@@ -1,13 +1,30 @@
|
|||||||
{
|
{
|
||||||
"functions": {
|
"version": 2,
|
||||||
"api/github/webhooks.ts": {
|
"builds": [
|
||||||
"maxDuration": 60
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"rewrites": [
|
|
||||||
{
|
{
|
||||||
"source": "/webhooks/github",
|
"src": "api/github/webhooks.ts",
|
||||||
"destination": "/api/github/webhooks"
|
"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
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user