-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathconstants.ts
More file actions
137 lines (119 loc) · 3.13 KB
/
Copy pathconstants.ts
File metadata and controls
137 lines (119 loc) · 3.13 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { Node } from "@babel/traverse";
import { JavascriptParser } from "./context/language/javascript-parser";
import { ChatCompletionMessageParam } from "groq-sdk/resources/chat/completions";
import Parser from "tree-sitter";
import { PythonParser } from "./context/language/python-parser";
export interface PRFile {
sha: string;
filename: string;
status:
| "added"
| "removed"
| "renamed"
| "changed"
| "modified"
| "copied"
| "unchanged";
additions: number;
deletions: number;
changes: number;
blob_url: string;
raw_url: string;
contents_url: string;
patch?: string;
previous_filename?: string;
patchTokenLength?: number;
old_contents?: string;
current_contents?: string;
}
export interface BuilderResponse {
comment: string;
structuredComments: any[];
}
export interface Builders {
convoBuilder: (diff: string) => ChatCompletionMessageParam[];
responseBuilder: (feedbacks: string[]) => Promise<BuilderResponse>;
}
export interface PatchInfo {
hunks: {
oldStart: number;
oldLines: number;
newStart: number;
newLines: number;
lines: string[];
}[];
}
export interface PRSuggestion {
describe: string;
type: string;
comment: string;
code: string;
filename: string;
toString: () => string;
identity: () => string;
}
export interface CodeSuggestion {
file: string;
line_start: number;
line_end: number;
correction: string;
comment: string;
}
export interface ChatMessage {
role: string;
content: string;
}
export interface Review {
review: BuilderResponse;
suggestions: CodeSuggestion[];
}
export interface BranchDetails {
name: string;
sha: string;
url: string;
}
export const sleep = async (ms: number) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
export const processGitFilepath = (filepath: string) => {
// Remove the leading '/' if it exists
return filepath.startsWith("/") ? filepath.slice(1) : filepath;
};
export const isBabelNode = (node: Node | Parser.SyntaxNode): node is Node =>{
if (node === null) {
return false;
}
return (node as Parser.SyntaxNode).childCount === undefined;
}
export interface EnclosingContext {
enclosingContext: Node | Parser.SyntaxNode | null;
}
export interface AbstractParser {
findEnclosingContext(
file: string,
lineStart: number,
lineEnd: number
): EnclosingContext;
dryRun(file: string): { valid: boolean; error: string };
}
const EXTENSIONS_TO_PARSERS: Map<string, AbstractParser> = new Map([
["ts", new JavascriptParser()],
["tsx", new JavascriptParser()],
["js", new JavascriptParser()],
["jsx", new JavascriptParser()],
["py", new PythonParser]
]);
export const getParserForExtension = (filename: string) => {
const fileExtension = filename.split(".").pop().toLowerCase();
return EXTENSIONS_TO_PARSERS.get(fileExtension) || null;
};
export const assignLineNumbers = (contents: string): string => {
const lines = contents.split("\n");
let lineNumber = 1;
const linesWithNumbers = lines.map((line) => {
const numberedLine = `${lineNumber}: ${line}`;
lineNumber++;
return numberedLine;
});
return linesWithNumbers.join("\n");
};