Code Review

Review a change's files, leave comments on lines, and send them to an agent as one review.

2 files changed

+12−3

src/api/client.ts

+4−3
1import axios from "axios"
1import { withRetry } from "./retry"
22
33export async function getInvoice(id: string) {
4 const response = await axios.get(`/api/invoices/${id}`)
5 return response.data
4 const response = await withRetry(() => fetch(`/api/invoices/${id}`))
5 if (!response.ok) throw new Error(`Invoice ${id}: ${response.status}`)
6 return response.json()
67}

src/api/retry.ts

+8−0
1/** Calls `run` again on a 429 or 503, waiting 200 ms, then 400, then 800. */
2export async function withRetry(run: () => Promise<Response>, attempts = 3) {
3 for (let attempt = 1; ; attempt++) {
4 const response = await run()
5 if ((response.status !== 429 && response.status !== 503) || attempt >= attempts) return response
6 await new Promise((done) => setTimeout(done, 200 * 2 ** (attempt - 1)))
7 }
8}

Installation

pnpm dlx shadcn@latest add checkfu/uicontext/diff-review

Usage

import type { Outcome } from "./ui/uicontext/components/domain/Outcome.ts"import { DiffSide } from "./ui/uicontext/components/kit/Diff.ts"import { DiffReview, type ReviewComment, type ReviewFile } from "./ui/uicontext/components/kit/DiffReview.ts"
const Message = UiSchema.TaggedEnum({	RequestedAddCommentReview: { path: Schema.String, side: DiffSide, line: Schema.Number, body: Schema.String },	RequestedDeleteCommentReview: { comment: Schema.String },	RequestedSendReview: {},	RequestedLayoutReview: { layout: Schema.Literals(["unified", "split"]) },})// your app keeps the comments: drafts until Send, then sent, then addressed with the agent's replyexport const Review = (h: Html.H<typeof Message.Type>, files: ReadonlyArray<ReviewFile>, comments: ReadonlyArray<ReviewComment>, outcome: Outcome | null) =>	DiffReview(h, {		props: { id: "ws-4", files, comments, can: ["comment", "send"], outcome },		onEmit: {			RequestedAddComment: ({ path, side, line, body }) => Message.RequestedAddCommentReview({ path, side, line, body }),			RequestedDeleteComment: ({ comment }) => Message.RequestedDeleteCommentReview({ comment }),			RequestedSend: () => Message.RequestedSendReview(),			RequestedLayout: ({ layout }) => Message.RequestedLayoutReview({ layout }),		},	})

API reference

Props

DiffReview props
NameTypeDescription
idrequiredstring—
filesrequiredReviewFile[]—
commentsrequiredReviewComment[]—
layout"unified" | "split"—
canrequired("comment" | "send")[]"comment" lets the person write on lines; "send" sends the drafts
sendLabelstring"Send to agent" by default
outcomeOutcome | nullThe app's answer to the last send ("send")
labelstring—
agentIdstring—

Events

DiffReview events
EventPayloadDescription
RequestedLayout{ id: string; layout: "unified" | "split" }—
RequestedAddComment{ id: string; path: string; side: DiffSide; line: number; body: string }Keep a draft comment on a line
RequestedDeleteComment{ id: string; comment: string }—
RequestedSend{ id: string; comments: string[] }Send every draft to the agent as one review; answer with outcome "send"

Agent actions

What agents can do with DiffReview
ActionDescription
RequestedCommentWrite a draft comment on a line of a changed file: its path, side (after for added or unchanged lines) and 1-based line
ClickedSendSend every draft comment to the agent as one review

Accessibility

  • Every changed file is a diff with line numbers; Tab reaches one comment button and the arrow keys move it from line to line.
  • A comment box takes focus as it opens: Mod+Enter adds the comment and Escape closes it. Each comment says whether it's a draft, sent or addressed in words.
  • The drafts bar is a named region with one Send button. Agents read the comments and can write one on a named line.

Source