-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathgetDiff.ts
More file actions
94 lines (79 loc) · 2.74 KB
/
Copy pathgetDiff.ts
File metadata and controls
94 lines (79 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { getDiff, GetDiffResult } from '@/features/git';
import { getDiffRequestSchema } from '@/features/git/schemas';
import { isServiceError } from '@/lib/utils';
import description from './getDiff.txt';
import { logger } from './logger';
import { ToolDefinition } from './types';
import { CodeHostType } from '@sourcebot/db';
import { getRepoInfoByName } from '@/actions';
export type GetDiffRepoInfo = {
name: string;
displayName: string;
codeHostType: CodeHostType;
};
export type GetDiffMetadata = GetDiffResult & {
repo: string;
repoInfo: GetDiffRepoInfo;
base: string;
head: string;
};
function formatDiffAsGitDiff(result: GetDiffResult): string {
let output = '';
for (const file of result.files) {
const oldPath = file.oldPath ?? '/dev/null';
const newPath = file.newPath ?? '/dev/null';
output += `--- a/${oldPath}\n`;
output += `+++ b/${newPath}\n`;
for (const hunk of file.hunks) {
const oldStart = hunk.oldRange.start;
const oldLines = hunk.oldRange.lines;
const newStart = hunk.newRange.start;
const newLines = hunk.newRange.lines;
output += `@@ -${oldStart},${oldLines} +${newStart},${newLines} @@`;
if (hunk.heading) {
output += ` ${hunk.heading}`;
}
output += '\n';
output += hunk.body;
if (!hunk.body.endsWith('\n')) {
output += '\n';
}
}
}
return output;
}
export const getDiffDefinition: ToolDefinition<'get_diff', typeof getDiffRequestSchema.shape, GetDiffMetadata> = {
name: 'get_diff',
title: 'Get diff',
isReadOnly: true,
isIdempotent: true,
description,
inputSchema: getDiffRequestSchema,
execute: async ({ repo, base, head }, _context) => {
logger.debug('get_diff', { repo, base, head });
const response = await getDiff({ repo, base, head });
if (isServiceError(response)) {
throw new Error(response.message);
}
const repoInfoResult = await getRepoInfoByName(repo);
if (isServiceError(repoInfoResult) || !repoInfoResult) {
throw new Error(`Repository "${repo}" not found.`);
}
const repoInfo: GetDiffRepoInfo = {
name: repoInfoResult.name,
displayName: repoInfoResult.displayName ?? repoInfoResult.name,
codeHostType: repoInfoResult.codeHostType,
};
const gitDiffOutput = formatDiffAsGitDiff(response);
return {
output: gitDiffOutput,
metadata: {
...response,
repo,
repoInfo,
base,
head,
},
};
},
};