|
| 1 | +const { parseJSONMessage, runCodex, truncate } = require('./codex.cjs'); |
| 2 | + |
| 3 | +const MAX_PATCH_CHARS = 24_000; |
| 4 | +const MAX_OTHER_FILES = 40; |
| 5 | + |
| 6 | +const reviewAssistantSchema = { |
| 7 | + additionalProperties: false, |
| 8 | + properties: { |
| 9 | + reply: { type: 'string' }, |
| 10 | + version: { const: 1, type: 'number' }, |
| 11 | + }, |
| 12 | + required: ['version', 'reply'], |
| 13 | + type: 'object', |
| 14 | +}; |
| 15 | + |
| 16 | +const getFileDigest = (file) => ({ |
| 17 | + oldPath: file.oldPath, |
| 18 | + path: file.path, |
| 19 | + status: file.status, |
| 20 | + summaries: file.sections |
| 21 | + .map((section) => section.summary?.reason) |
| 22 | + .filter((summary) => typeof summary === 'string' && summary.trim()), |
| 23 | +}); |
| 24 | + |
| 25 | +const buildReviewAssistantInput = (state, request) => { |
| 26 | + const comment = request?.comment ?? {}; |
| 27 | + const file = state.files.find((candidate) => candidate.path === comment.filePath); |
| 28 | + const section = file?.sections.find((candidate) => candidate.id === comment.sectionId); |
| 29 | + |
| 30 | + return { |
| 31 | + comment: { |
| 32 | + body: typeof comment.body === 'string' ? comment.body : '', |
| 33 | + filePath: comment.filePath, |
| 34 | + lineNumber: comment.lineNumber, |
| 35 | + side: comment.side, |
| 36 | + }, |
| 37 | + focus: file |
| 38 | + ? { |
| 39 | + file: getFileDigest(file), |
| 40 | + patchExcerpt: section |
| 41 | + ? truncate(section.patch || section.summary?.reason || '', MAX_PATCH_CHARS) |
| 42 | + : 'No patch context available.', |
| 43 | + section: section |
| 44 | + ? { |
| 45 | + binary: section.binary, |
| 46 | + kind: section.kind, |
| 47 | + loadState: section.loadState, |
| 48 | + summary: section.summary?.reason, |
| 49 | + } |
| 50 | + : null, |
| 51 | + } |
| 52 | + : null, |
| 53 | + nearbyFiles: state.files |
| 54 | + .filter((candidate) => candidate.path !== comment.filePath) |
| 55 | + .slice(0, MAX_OTHER_FILES) |
| 56 | + .map(getFileDigest), |
| 57 | + root: state.root, |
| 58 | + source: state.source, |
| 59 | + walkthroughNote: request?.walkthroughNote ?? null, |
| 60 | + }; |
| 61 | +}; |
| 62 | + |
| 63 | +const buildReviewAssistantPrompt = (state, request) => `You are Codex inside Codiff. |
| 64 | +
|
| 65 | +A human reviewer wrote a rough inline review note and clicked Ask Codex. |
| 66 | +Reply as a concise assistant in the same inline conversation. |
| 67 | +Use only the repository change digest below; do not inspect the repository or run shell commands. |
| 68 | +If there is walkthrough context, use it as review orientation, not as proof. |
| 69 | +You are the code-review expert in this conversation, so explain the change directly. |
| 70 | +
|
| 71 | +Your job: |
| 72 | +- Turn vague unease into coherent, actionable review feedback. |
| 73 | +- If the note asks "why", explain why this change is needed based on the diff. |
| 74 | +- If useful, suggest a clearer review comment the human could use. |
| 75 | +- Prefer questions and concrete risks over accusations. |
| 76 | +- Do not hedge. Avoid words and phrases like "appears", "seems", "might", "likely", "probably", "I think", "I suspect", and "the intent". |
| 77 | +- Say "This change introduces...", "This change moves...", or "This is needed because..." instead of "This change appears to...". |
| 78 | +- If the diff does not provide enough evidence, state the concrete uncertainty after the explanation. |
| 79 | +- Do not say the change is correct unless the diff proves it. |
| 80 | +- Do not invent bugs, unstated requirements, or files outside the digest. |
| 81 | +- Keep the reply under 180 words. |
| 82 | +- Markdown is allowed. |
| 83 | +
|
| 84 | +Repository change digest: |
| 85 | +${JSON.stringify(buildReviewAssistantInput(state, request), null, 2)} |
| 86 | +`; |
| 87 | + |
| 88 | +const cleanReply = (value, fallback = '') => |
| 89 | + (typeof value === 'string' ? value : fallback).replace(/\n{3,}/g, '\n\n').trim(); |
| 90 | + |
| 91 | +const normalizeReviewAssistantReply = (input) => ({ |
| 92 | + reply: cleanReply(input?.reply, 'Codex could not produce a useful reply.'), |
| 93 | + version: 1, |
| 94 | +}); |
| 95 | + |
| 96 | +const readReviewAssistantReply = async (state, request) => { |
| 97 | + try { |
| 98 | + const response = await runCodex( |
| 99 | + state.root, |
| 100 | + buildReviewAssistantPrompt(state, request), |
| 101 | + reviewAssistantSchema, |
| 102 | + 'review-assistant.json', |
| 103 | + 'Codex review reply timed out.', |
| 104 | + ); |
| 105 | + const parsed = parseJSONMessage(response); |
| 106 | + |
| 107 | + return { |
| 108 | + reply: normalizeReviewAssistantReply(parsed).reply, |
| 109 | + status: 'ready', |
| 110 | + }; |
| 111 | + } catch (error) { |
| 112 | + if (error && error.code === 'ENOENT') { |
| 113 | + return { |
| 114 | + reason: |
| 115 | + 'Codex is not installed locally. Install and use Codex, then ask from this comment again.', |
| 116 | + status: 'unavailable', |
| 117 | + }; |
| 118 | + } |
| 119 | + |
| 120 | + return { |
| 121 | + reason: error instanceof Error ? error.message : String(error), |
| 122 | + status: 'unavailable', |
| 123 | + }; |
| 124 | + } |
| 125 | +}; |
| 126 | + |
| 127 | +module.exports = { |
| 128 | + buildReviewAssistantInput, |
| 129 | + buildReviewAssistantPrompt, |
| 130 | + normalizeReviewAssistantReply, |
| 131 | + readReviewAssistantReply, |
| 132 | + reviewAssistantSchema, |
| 133 | +}; |
0 commit comments