@polka-codes/cli 0.10.21 → 0.10.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,675 +0,0 @@
1
- import {
2
- annotateDiffWithLineNumbers,
3
- gitDiff_default
4
- } from "./chunk-UEEU3SCC.js";
5
- import {
6
- CODE_REVIEW_SYSTEM_PROMPT,
7
- formatReviewToolInput,
8
- getDefaultContext,
9
- parseGitDiffNameStatus,
10
- parseGitDiffNumStat,
11
- parseGitStatus,
12
- printChangedFiles,
13
- reviewOutputSchema
14
- } from "./chunk-2LRQ2QH6.js";
15
-
16
- // src/workflows/review.workflow.ts
17
- import path from "path";
18
- import { agentWorkflow, listFiles, readBinaryFile, readFile, searchFiles } from "@polka-codes/core";
19
-
20
- // src/workflows/git-file-tools.ts
21
- import { z } from "zod";
22
-
23
- // src/utils/shell.ts
24
- function isWindows() {
25
- return process.platform === "win32";
26
- }
27
- function quoteForShell(str) {
28
- if (isWindows()) {
29
- const escaped = str.replace(/"/g, '""');
30
- return `"${escaped}"`;
31
- } else {
32
- return `'${str.replace(/'/g, "'\\''")}'`;
33
- }
34
- }
35
-
36
- // src/workflows/git-file-tools.ts
37
- function getMediaType(path2) {
38
- const lastDotIndex = path2.lastIndexOf(".");
39
- const ext = lastDotIndex > 0 && lastDotIndex < path2.length - 1 ? path2.slice(lastDotIndex + 1).toLowerCase() : void 0;
40
- const mediaTypes = {
41
- // Images
42
- png: "image/png",
43
- jpg: "image/jpeg",
44
- jpeg: "image/jpeg",
45
- gif: "image/gif",
46
- svg: "image/svg+xml",
47
- webp: "image/webp",
48
- ico: "image/x-icon",
49
- bmp: "image/bmp",
50
- // Fonts
51
- woff: "font/woff",
52
- woff2: "font/woff2",
53
- ttf: "font/ttf",
54
- otf: "font/otf",
55
- eot: "application/vnd.ms-fontobject",
56
- // Documents
57
- pdf: "application/pdf",
58
- // Audio
59
- mp3: "audio/mpeg",
60
- wav: "audio/wav",
61
- ogg: "audio/ogg",
62
- // Video
63
- mp4: "video/mp4",
64
- webm: "video/webm",
65
- avi: "video/x-msvideo"
66
- };
67
- return mediaTypes[ext || ""] || "application/octet-stream";
68
- }
69
- function extractTargetCommit(range, pr) {
70
- if (pr) {
71
- return null;
72
- }
73
- if (!range || range.trim() === "") {
74
- return null;
75
- }
76
- const parts = range.split(/\.\.\.?/);
77
- if (parts.length > 1) {
78
- return parts[1].trim() || null;
79
- }
80
- const trimmed = range.trim();
81
- return trimmed || null;
82
- }
83
- function createGitReadFile(commit) {
84
- const toolInfo = {
85
- name: "readFile",
86
- description: `Read file contents from git commit ${commit}. Use this to examine files at the specific commit being reviewed.`,
87
- parameters: z.object({
88
- path: z.preprocess((val) => {
89
- if (!val) return [];
90
- const values = Array.isArray(val) ? val : [val];
91
- return values.flatMap((i) => typeof i === "string" ? i.split(",") : []).filter((s) => s.length > 0);
92
- }, z.array(z.string())).describe("The path of the file to read (relative to git root)").meta({ usageValue: "Comma separated paths here" })
93
- }).meta({
94
- examples: [
95
- {
96
- description: "Read the contents of a file at the commit",
97
- input: {
98
- path: "src/main.ts"
99
- }
100
- }
101
- ]
102
- })
103
- };
104
- const handler = async (provider, args) => {
105
- if (!provider.executeCommand) {
106
- return {
107
- success: false,
108
- message: {
109
- type: "error-text",
110
- value: "Not possible to execute command."
111
- }
112
- };
113
- }
114
- const { path: paths } = toolInfo.parameters.parse(args);
115
- const results = [];
116
- for (const filePath of paths) {
117
- const quotedCommit = quoteForShell(commit);
118
- const quotedPath = quoteForShell(filePath);
119
- const result = await provider.executeCommand(`git show ${quotedCommit}:${quotedPath}`, false);
120
- if (result.exitCode === 0) {
121
- results.push(`<read_file_file_content path="${filePath}">${result.stdout}</read_file_file_content>`);
122
- } else {
123
- results.push(`<read_file_file_content path="${filePath}" file_not_found="true"></read_file_file_content>`);
124
- }
125
- }
126
- return {
127
- success: true,
128
- message: {
129
- type: "text",
130
- value: results.join("\n")
131
- }
132
- };
133
- };
134
- return {
135
- ...toolInfo,
136
- handler
137
- };
138
- }
139
- function createGitListFiles(commit) {
140
- const toolInfo = {
141
- name: "listFiles",
142
- description: `List files and directories at git commit ${commit}. Shows the file tree as it existed at that commit.`,
143
- parameters: z.object({
144
- path: z.string().optional().describe("The path of the directory to list (relative to git root). Default is root.").meta({ usageValue: "Directory path here (optional)" }),
145
- maxCount: z.coerce.number().optional().default(2e3).describe("The maximum number of files to list. Default to 2000").meta({ usageValue: "Maximum number of files to list (optional)" })
146
- })
147
- };
148
- const handler = async (provider, args) => {
149
- if (!provider.executeCommand) {
150
- return {
151
- success: false,
152
- message: {
153
- type: "error-text",
154
- value: "Not possible to execute command."
155
- }
156
- };
157
- }
158
- const parsed = toolInfo.parameters.parse(args);
159
- const path2 = parsed.path || ".";
160
- const quotedCommit = quoteForShell(commit);
161
- const quotedPath = quoteForShell(path2);
162
- const result = await provider.executeCommand(`git ls-tree -r --name-only ${quotedCommit} ${quotedPath}`, false);
163
- if (result.exitCode !== 0) {
164
- return {
165
- success: false,
166
- message: {
167
- type: "error-text",
168
- value: `Failed to list files at commit ${commit}: ${result.stderr}`
169
- }
170
- };
171
- }
172
- const files = result.stdout.trim().split("\n").filter((f) => f.length > 0);
173
- const truncated = files.length > parsed.maxCount;
174
- const displayFiles = truncated ? files.slice(0, parsed.maxCount) : files;
175
- return {
176
- success: true,
177
- message: {
178
- type: "text",
179
- value: `<list_files_path>${path2}</list_files_path>
180
- <list_files_files>
181
- ${displayFiles.join("\n")}
182
- </list_files_files>
183
- <list_files_truncated>${truncated}</list_files_truncated>`
184
- }
185
- };
186
- };
187
- return {
188
- ...toolInfo,
189
- handler
190
- };
191
- }
192
- function createGitReadBinaryFile(commit) {
193
- const toolInfo = {
194
- name: "readBinaryFile",
195
- description: `Read binary file contents from git commit ${commit} and return as base64 encoded data. Use for images, fonts, and other binary files.`,
196
- parameters: z.object({
197
- url: z.string().describe("The URL or path of the binary file to read (relative to git root)")
198
- })
199
- };
200
- const handler = async (provider, args) => {
201
- if (!provider.executeCommand) {
202
- return {
203
- success: false,
204
- message: {
205
- type: "error-text",
206
- value: "Not possible to execute command."
207
- }
208
- };
209
- }
210
- const { url } = toolInfo.parameters.parse(args);
211
- const quotedCommit = quoteForShell(commit);
212
- const quotedUrl = quoteForShell(url);
213
- const checkResult = await provider.executeCommand(`git cat-file -e ${quotedCommit}:${quotedUrl}`, false);
214
- if (checkResult.exitCode !== 0) {
215
- return {
216
- success: true,
217
- message: {
218
- type: "text",
219
- value: `<read_binary_file_file_content url="${url}" file_not_found="true"></read_binary_file_file_content>`
220
- }
221
- };
222
- }
223
- const isWindows2 = process.platform === "win32";
224
- const command = isWindows2 ? `cmd /c "git show ${quotedCommit}:${quotedUrl} | base64 -w 0"` : `sh -c "git show ${quotedCommit}:${quotedUrl} | base64"`;
225
- const result = await provider.executeCommand(command, false);
226
- if (result.exitCode === 0) {
227
- const base64Data = result.stdout.replace(/\n/g, "");
228
- return {
229
- success: true,
230
- message: {
231
- type: "content",
232
- value: [
233
- {
234
- type: "media",
235
- url,
236
- data: base64Data,
237
- mediaType: getMediaType(url)
238
- }
239
- ]
240
- }
241
- };
242
- } else {
243
- const isBase64Error = result.stderr.includes("not recognized") || result.stderr.includes("command not found") || result.stderr.includes("base64");
244
- const errorMessage = isBase64Error ? `Failed to read binary file: base64 command not found. On Windows, ensure Git Bash or Unix tools are installed and in PATH.` : `Failed to read binary file: ${result.stderr}`;
245
- return {
246
- success: false,
247
- message: {
248
- type: "error-text",
249
- value: errorMessage
250
- }
251
- };
252
- }
253
- };
254
- return {
255
- ...toolInfo,
256
- handler
257
- };
258
- }
259
- function createGitAwareTools(commit) {
260
- return {
261
- readFile: createGitReadFile(commit),
262
- listFiles: createGitListFiles(commit),
263
- readBinaryFile: createGitReadBinaryFile(commit)
264
- };
265
- }
266
- function createGitAwareDiff(commit) {
267
- const toolInfo = {
268
- name: "git_diff",
269
- description: `Get the git diff for commit ${commit}. Shows the exact changes introduced by this specific commit. Use this to inspect what changed in each file. Always specify a file path.`,
270
- parameters: z.object({
271
- file: z.string().describe("Get the diff for a specific file within the commit. This parameter is required.").meta({ usageValue: "File path here (required)" }),
272
- contextLines: z.coerce.number().int().min(0).optional().default(5).describe("Number of context lines to include around changes.").meta({ usageValue: "Context lines count (optional)" }),
273
- includeLineNumbers: z.preprocess((val) => {
274
- if (typeof val === "string") {
275
- const lower = val.toLowerCase();
276
- if (lower === "false") return false;
277
- if (lower === "true") return true;
278
- }
279
- return val;
280
- }, z.boolean().optional().default(true)).describe("Annotate the diff with line numbers for additions and deletions.").meta({ usageValue: "true or false (optional)" })
281
- })
282
- };
283
- const handler = async (provider, args) => {
284
- if (!provider.executeCommand) {
285
- return {
286
- success: false,
287
- message: {
288
- type: "error-text",
289
- value: "Not possible to execute command."
290
- }
291
- };
292
- }
293
- const { file, contextLines, includeLineNumbers } = toolInfo.parameters.parse(args);
294
- const quotedCommit = quoteForShell(commit);
295
- let command = `git show --no-color --format= -U${contextLines} ${quotedCommit}`;
296
- if (file) {
297
- const quotedFile = quoteForShell(file);
298
- command = `git show --no-color --format= -U${contextLines} ${quotedCommit} -- ${quotedFile}`;
299
- }
300
- try {
301
- const result = await provider.executeCommand(command, false);
302
- if (result.exitCode === 0) {
303
- if (!result.stdout.trim()) {
304
- return {
305
- success: true,
306
- message: {
307
- type: "text",
308
- value: "No diff found."
309
- }
310
- };
311
- }
312
- let diffOutput = result.stdout;
313
- if (includeLineNumbers) {
314
- diffOutput = annotateDiffWithLineNumbers(diffOutput);
315
- }
316
- return {
317
- success: true,
318
- message: {
319
- type: "text",
320
- value: `<diff file="${file ?? "all"}">
321
- ${diffOutput}
322
- </diff>`
323
- }
324
- };
325
- }
326
- return {
327
- success: false,
328
- message: {
329
- type: "error-text",
330
- value: `\`${command}\` exited with code ${result.exitCode}:
331
- ${result.stderr}`
332
- }
333
- };
334
- } catch (error) {
335
- return {
336
- success: false,
337
- message: {
338
- type: "error-text",
339
- value: error instanceof Error ? error.message : String(error)
340
- }
341
- };
342
- }
343
- };
344
- return {
345
- ...toolInfo,
346
- handler
347
- };
348
- }
349
-
350
- // src/workflows/review.workflow.ts
351
- var reviewWorkflow = async (input, context) => {
352
- const { step, tools, logger } = context;
353
- const { pr, range, context: userContext, files } = input;
354
- let changeInfo;
355
- const gitRootResult = await tools.executeCommand({ command: "git", args: ["rev-parse", "--show-toplevel"] });
356
- if (gitRootResult.exitCode !== 0) {
357
- throw new Error("Failed to determine git repository root. Ensure you are in a git repository.");
358
- }
359
- const gitRoot = gitRootResult.stdout.trim();
360
- const normalizeFilePath = (filePath) => {
361
- const absolutePath = path.resolve(process.cwd(), filePath);
362
- const relativePath = path.relative(gitRoot, absolutePath);
363
- return relativePath.split(path.sep).join("/");
364
- };
365
- const normalizedFiles = files?.map(normalizeFilePath);
366
- const filterFiles = (changedFiles) => {
367
- if (!normalizedFiles || normalizedFiles.length === 0) {
368
- return changedFiles;
369
- }
370
- return changedFiles.filter((file) => normalizedFiles.includes(file.path));
371
- };
372
- const filterAndWarn = (changedFiles, source) => {
373
- const filteredFiles = filterFiles(changedFiles);
374
- if (normalizedFiles && normalizedFiles.length > 0) {
375
- const foundFiles = new Set(filteredFiles.map((f) => f.path));
376
- const missingFiles = normalizedFiles.filter((f) => !foundFiles.has(f));
377
- if (missingFiles.length > 0) {
378
- if (filteredFiles.length === 0) {
379
- logger.warn(`Warning: None of the specified files were found in ${source}: ${missingFiles.join(", ")}`);
380
- } else {
381
- logger.warn(`Warning: Some files not found in ${source}: ${missingFiles.join(", ")}`);
382
- }
383
- }
384
- }
385
- return filteredFiles;
386
- };
387
- if (pr) {
388
- const ghCheckResult = await tools.executeCommand({ command: "gh", args: ["--version"] });
389
- if (ghCheckResult.exitCode !== 0) {
390
- throw new Error("Error: GitHub CLI (gh) is not installed. Please install it from https://cli.github.com/");
391
- }
392
- await step(`Checking out PR #${pr}...`, async () => {
393
- const checkoutResult = await tools.executeCommand({
394
- command: "gh",
395
- args: ["pr", "checkout", pr.toString()]
396
- });
397
- if (checkoutResult.exitCode !== 0) {
398
- logger.error(checkoutResult.stderr);
399
- throw new Error(`Error checking out PR #${pr}. Make sure the PR number is correct and you have access to the repository.`);
400
- }
401
- });
402
- const prDetails = await step("Fetching pull request details...", async () => {
403
- const result2 = await tools.executeCommand({
404
- command: "gh",
405
- args: ["pr", "view", pr.toString(), "--json", "title,body,commits,baseRefName,baseRefOid"]
406
- });
407
- try {
408
- return JSON.parse(result2.stdout);
409
- } catch (error) {
410
- throw new Error(`Failed to parse PR details from GitHub CLI: ${error instanceof Error ? error.message : String(error)}`);
411
- }
412
- });
413
- const prCommitRange = `${prDetails.baseRefOid}...HEAD`;
414
- logger.info(`Reviewing PR #${pr} (commit range: ${prCommitRange})`);
415
- const commitMessages = prDetails.commits.map((c) => c.messageBody).join("\n---\n");
416
- const allChangedFiles = await step("Getting file changes...", async () => {
417
- const diffResult = await tools.executeCommand({
418
- command: "git",
419
- args: ["--no-pager", "diff", "--name-status", "--no-color", prCommitRange]
420
- });
421
- if (diffResult.exitCode !== 0) {
422
- logger.warn("Warning: Could not retrieve file changes list");
423
- return [];
424
- }
425
- const files2 = parseGitDiffNameStatus(diffResult.stdout);
426
- const statResult = await tools.executeCommand({
427
- command: "git",
428
- args: ["--no-pager", "diff", "--numstat", "--no-color", prCommitRange]
429
- });
430
- if (statResult.exitCode === 0) {
431
- const stats = parseGitDiffNumStat(statResult.stdout);
432
- for (const file of files2) {
433
- if (stats[file.path]) {
434
- file.insertions = stats[file.path].insertions;
435
- file.deletions = stats[file.path].deletions;
436
- }
437
- }
438
- }
439
- return files2;
440
- });
441
- const changedFiles = filterAndWarn(allChangedFiles, `PR #${pr}`);
442
- printChangedFiles(logger, changedFiles);
443
- if (changedFiles.length === 0) {
444
- return {
445
- overview: normalizedFiles ? `No changes to review. The specified file(s) were not found in PR #${pr}: ${normalizedFiles.join(", ")}` : `No changes to review in PR #${pr}.`,
446
- specificReviews: []
447
- };
448
- }
449
- changeInfo = {
450
- commitRange: prCommitRange,
451
- pullRequestTitle: prDetails.title,
452
- pullRequestDescription: prDetails.body,
453
- commitMessages,
454
- changedFiles,
455
- context: userContext
456
- };
457
- } else if (range) {
458
- await step(`Reviewing git range '${range}'...`, async () => {
459
- });
460
- logger.info(`Reviewing commit range: ${range}`);
461
- const allRangeChangedFiles = await step("Getting file changes...", async () => {
462
- const diffResult = await tools.executeCommand({
463
- command: "git",
464
- args: ["--no-pager", "diff", "--name-status", "--no-color", range]
465
- });
466
- if (diffResult.exitCode !== 0) {
467
- logger.warn("Warning: Could not retrieve file changes list");
468
- return [];
469
- }
470
- const files2 = parseGitDiffNameStatus(diffResult.stdout);
471
- const statResult = await tools.executeCommand({
472
- command: "git",
473
- args: ["--no-pager", "diff", "--numstat", "--no-color", range]
474
- });
475
- if (statResult.exitCode === 0) {
476
- const stats = parseGitDiffNumStat(statResult.stdout);
477
- for (const file of files2) {
478
- if (stats[file.path]) {
479
- file.insertions = stats[file.path].insertions;
480
- file.deletions = stats[file.path].deletions;
481
- }
482
- }
483
- }
484
- return files2;
485
- });
486
- const rangeChangedFiles = filterAndWarn(allRangeChangedFiles, `range '${range}'`);
487
- printChangedFiles(logger, rangeChangedFiles);
488
- if (rangeChangedFiles.length === 0) {
489
- return {
490
- overview: normalizedFiles ? `No changes to review. The specified file(s) were not found in range '${range}': ${normalizedFiles.join(", ")}` : `No changes to review in range '${range}'.`,
491
- specificReviews: []
492
- };
493
- }
494
- const logResult = await tools.executeCommand({
495
- command: "git",
496
- args: ["log", "--format=%B", range]
497
- });
498
- const commitMessages = logResult.exitCode === 0 ? logResult.stdout.trim() : "";
499
- changeInfo = {
500
- commitRange: range,
501
- changedFiles: rangeChangedFiles,
502
- commitMessages,
503
- context: userContext
504
- };
505
- } else {
506
- const statusResult = await tools.executeCommand({ command: "git", args: ["status", "--porcelain=v1"] });
507
- const gitStatus = statusResult.stdout;
508
- const statusLines = gitStatus.split("\n").filter((line) => line);
509
- const hasLocalChanges = statusLines.length > 0;
510
- if (hasLocalChanges) {
511
- const hasStagedChanges = statusLines.some((line) => line[0] !== " " && line[0] !== "?");
512
- const hasUnstagedChanges = statusLines.some((line) => line[1] !== " " && line[1] !== "?");
513
- const hasUntrackedChanges = statusLines.some((line) => line.startsWith("??"));
514
- const reviewTargets = [];
515
- if (hasStagedChanges) {
516
- reviewTargets.push("staged files");
517
- }
518
- if (hasUnstagedChanges) {
519
- reviewTargets.push("unstaged files");
520
- }
521
- if (hasUntrackedChanges) {
522
- reviewTargets.push("untracked files");
523
- }
524
- if (reviewTargets.length > 0) {
525
- logger.info(`Reviewing local changes: ${reviewTargets.join(", ")}.`);
526
- } else {
527
- logger.info("Reviewing local changes.");
528
- }
529
- const allChangedFiles = parseGitStatus(gitStatus);
530
- const unstagedStatResult = await tools.executeCommand({
531
- command: "git",
532
- args: ["diff", "--numstat", "--no-color"]
533
- });
534
- const unstagedStats = unstagedStatResult.exitCode === 0 ? parseGitDiffNumStat(unstagedStatResult.stdout) : {};
535
- const stagedStatResult = await tools.executeCommand({
536
- command: "git",
537
- args: ["diff", "--numstat", "--cached", "--no-color"]
538
- });
539
- const stagedStats = stagedStatResult.exitCode === 0 ? parseGitDiffNumStat(stagedStatResult.stdout) : {};
540
- for (const file of allChangedFiles) {
541
- const unstaged = unstagedStats[file.path] || { insertions: 0, deletions: 0 };
542
- const staged = stagedStats[file.path] || { insertions: 0, deletions: 0 };
543
- if (unstaged.insertions > 0 || unstaged.deletions > 0 || staged.insertions > 0 || staged.deletions > 0) {
544
- file.insertions = unstaged.insertions + staged.insertions;
545
- file.deletions = unstaged.deletions + staged.deletions;
546
- }
547
- }
548
- const changedFiles = filterAndWarn(allChangedFiles, "local changes");
549
- printChangedFiles(logger, changedFiles);
550
- if (changedFiles.length === 0) {
551
- return {
552
- overview: normalizedFiles ? `No changes to review. The specified file(s) were not found in local changes: ${normalizedFiles.join(", ")}` : "No changes to review.",
553
- specificReviews: []
554
- };
555
- }
556
- changeInfo = {
557
- staged: hasStagedChanges,
558
- changedFiles,
559
- context: userContext
560
- };
561
- } else {
562
- await step("No local changes detected. Falling back to branch diff...", async () => {
563
- });
564
- const ghCheckResult = await tools.executeCommand({ command: "gh", args: ["--version"] });
565
- if (ghCheckResult.exitCode !== 0) {
566
- throw new Error(
567
- "Error: GitHub CLI (gh) is not installed, and there are no local changes to review. Please install it from https://cli.github.com/ to review branch changes."
568
- );
569
- }
570
- const defaultBranchResult = await tools.executeCommand({
571
- command: "gh",
572
- args: ["repo", "view", "--json", "defaultBranchRef", "--jq", ".defaultBranchRef.name"]
573
- });
574
- const defaultBranch = defaultBranchResult.stdout.trim();
575
- const currentBranchResult = await tools.executeCommand({ command: "git", args: ["rev-parse", "--abbrev-ref", "HEAD"] });
576
- const currentBranch = currentBranchResult.stdout.trim();
577
- if (currentBranch === defaultBranch) {
578
- await step(`No changes to review. You are on the default branch ('${defaultBranch}').`, async () => {
579
- });
580
- logger.info(`No changes to review. You are on the default branch ('${defaultBranch}').`);
581
- return {
582
- overview: `No changes to review. You are on the default branch ('${defaultBranch}').`,
583
- specificReviews: []
584
- };
585
- }
586
- const branchCommitRange = `${defaultBranch}...${currentBranch}`;
587
- logger.info(`Reviewing branch changes (commit range: ${branchCommitRange})`);
588
- const allBranchChangedFiles = await step("Getting file changes...", async () => {
589
- const diffResult = await tools.executeCommand({
590
- command: "git",
591
- args: ["--no-pager", "diff", "--name-status", "--no-color", branchCommitRange]
592
- });
593
- if (diffResult.exitCode !== 0) {
594
- logger.warn("Warning: Could not retrieve file changes list");
595
- return [];
596
- }
597
- const files2 = parseGitDiffNameStatus(diffResult.stdout);
598
- const statResult = await tools.executeCommand({
599
- command: "git",
600
- args: ["--no-pager", "diff", "--numstat", "--no-color", branchCommitRange]
601
- });
602
- if (statResult.exitCode === 0) {
603
- const stats = parseGitDiffNumStat(statResult.stdout);
604
- for (const file of files2) {
605
- if (stats[file.path]) {
606
- file.insertions = stats[file.path].insertions;
607
- file.deletions = stats[file.path].deletions;
608
- }
609
- }
610
- }
611
- return files2;
612
- });
613
- const branchChangedFiles = filterAndWarn(allBranchChangedFiles, "branch changes");
614
- printChangedFiles(logger, branchChangedFiles);
615
- if (branchChangedFiles.length === 0) {
616
- return {
617
- overview: normalizedFiles ? `No changes to review. The specified file(s) were not found in branch changes: ${normalizedFiles.join(", ")}` : `No changes to review. The current branch has no differences from ${defaultBranch}.`,
618
- specificReviews: []
619
- };
620
- }
621
- changeInfo = {
622
- commitRange: branchCommitRange,
623
- changedFiles: branchChangedFiles,
624
- context: userContext
625
- };
626
- }
627
- }
628
- if (!changeInfo) {
629
- return { overview: "No changes to review.", specificReviews: [] };
630
- }
631
- if (!changeInfo?.changedFiles || changeInfo.changedFiles.length === 0) {
632
- return { overview: "No changes to review.", specificReviews: [] };
633
- }
634
- const targetCommit = extractTargetCommit(range, pr);
635
- const isRange = range?.includes("..");
636
- const finalChangeInfo = targetCommit && !isRange ? { ...changeInfo, targetCommit } : changeInfo;
637
- const fileTools = targetCommit && !isRange ? createGitAwareTools(targetCommit) : { readFile, listFiles, readBinaryFile };
638
- const reviewTools = targetCommit && !isRange ? [fileTools.readFile, fileTools.readBinaryFile, fileTools.listFiles, createGitAwareDiff(targetCommit)] : [readFile, readBinaryFile, searchFiles, listFiles, gitDiff_default];
639
- const result = await step("review", async () => {
640
- const { context: defaultContext } = await getDefaultContext(input.config, "review");
641
- const memoryContext = await tools.getMemoryContext();
642
- const reviewInput = formatReviewToolInput(finalChangeInfo);
643
- const fullContent = `${reviewInput}
644
-
645
- ${defaultContext}
646
- ${memoryContext}`;
647
- return await agentWorkflow(
648
- {
649
- systemPrompt: CODE_REVIEW_SYSTEM_PROMPT,
650
- userMessage: [
651
- {
652
- role: "user",
653
- content: fullContent
654
- }
655
- ],
656
- tools: reviewTools,
657
- outputSchema: reviewOutputSchema
658
- },
659
- context
660
- );
661
- });
662
- if (result.type === "Exit") {
663
- const reviewResult = result.object;
664
- if (!reviewResult) {
665
- return { overview: "AI agent failed to produce a review.", specificReviews: [] };
666
- }
667
- return reviewResult;
668
- }
669
- return { overview: `Agent workflow exited with an unexpected status: ${result.type}`, specificReviews: [] };
670
- };
671
-
672
- export {
673
- quoteForShell,
674
- reviewWorkflow
675
- };