Ở ví dụ minh họa, agent sẽ dùng GitHub làm kho chứa mã từ xa và có khả năng phân tích, nhận xét mã và đăng nhận xét trực tiếp lên PR.
npm install @radix-ui/react-checkbox @radix-ui/react-alert @hookform/resolvers zod react-hook-form class-variance-authority clsx tailwind-merge lucide-react react-markdown react-syntax-highlighter @langchain/openai @tailwindcss/typographynpm install --save-dev @types/react-syntax-highlighter
shadcn/ui
để tạo thành phần UI:npx shadcn@latest initnpx shadcn@latest add button card form input checkbox alert
.env.local
với biến môi trường:GITHUB_TOKEN=your_github_personal_access_tokenGAIA_API_KEY=your_ai_api_keyGAIA_API_BASE_URL=your_ai_api_base_urlGAIA_MODEL=DeepSeek-R1-Distill-Llama-8B-Q5_K_M
app/actions.ts
khai báo các hàm kiểu fetchPullRequestDiff
, analyzeDiff
, postGithubComment
và hàm chính reviewPullRequest
.Tên Hàm | Mục Đích |
---|---|
fetchPullRequestDiff | Lấy bản diff của PR từ GitHub dưới dạng văn bản với header chấp nhận định dạng diff |
analyzeDiff | Gửi bản diff đến LLM qua LangChain và Gaia để phân tích, tạo feedback dưới định dạng Markdown |
postGithubComment | Gửi phản hồi (feedback) thành bình luận trực tiếp lên PR trên GitHub |
// Ví dụ fetch PR diffconst url = `https://api.github.com/repos/${owner}/${repo}/pulls/${prNumber}`;const response = await fetch(url, { headers: { Authorization: `Bearer ${GITHUB_TOKEN}`, Accept: "application/vnd.github.v3.diff" } });...
components/theme-provider.tsx
để quản lý chủ đề sáng/tối với Next Themes.react-hook-form
kết hợp với Zod validation cho form nhập liệu GitHub owner, repo, số PR và tuỳ chọn đăng comment.<Form onSubmit={form.handleSubmit(onSubmit)}> <Input placeholder="Owner" {...fieldOwner} /> <Input placeholder="Repo" {...fieldRepo} /> <Input type="number" placeholder="PR Number" {...fieldPrNumber} /> <Checkbox checked={postComment} onChange={togglePostComment} /> <Button disabled={isLoading}>{isLoading ? "Đang phân tích..." : "Đánh giá PR"}</Button></Form>
app/page.tsx
để sử dụng component ReviewForm
.app/layout.tsx
để bọc ứng dụng bởi ThemeProvider
và thêm font.tailwind.config.ts
để hỗ trợ dark mode và bố cục chuẩn.npm run dev
.