-
Notifications
You must be signed in to change notification settings - Fork 125
/
createPromptFiles.ts
153 lines (133 loc) 路 4.2 KB
/
createPromptFiles.ts
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { PromptFile, ReviewFile } from "../../../../../common/types";
const getChangedIndicesAndLength = (
contentLines: string[],
changedLinesArray: string[]
) => {
const changedIndices: { [index: number]: string } = {};
let totalChangedLinesLength = 0;
let minIndex = Infinity;
let maxIndex = -Infinity;
// Create a map of trimmed content lines to their indices (arrays to handle duplicates)
const trimmedContentLinesMap = new Map<string, number[]>();
contentLines.forEach((line, index) => {
const trimmedLine = line.trim();
const indices = trimmedContentLinesMap.get(trimmedLine) || [];
indices.push(index);
trimmedContentLinesMap.set(trimmedLine, indices);
});
// Process the changed lines
changedLinesArray.forEach((changedLine) => {
const lineContent = changedLine.substring(1).trim();
const indices = trimmedContentLinesMap.get(lineContent);
if (indices && indices.length > 0) {
const index = indices.shift()!; // Take the first occurrence
changedIndices[index] = changedLine;
totalChangedLinesLength += changedLine.length + 1;
minIndex = Math.min(minIndex, index);
maxIndex = Math.max(maxIndex, index);
}
});
return { changedIndices, totalChangedLinesLength, minIndex, maxIndex };
};
const calculateStartAndEnd = (
minIndex: number,
maxIndex: number,
contentLinesLength: number,
maxSurroundingLines?: number
) => {
// Calculate the starting and ending indices with surrounding lines considered
const start = Math.max(minIndex - (maxSurroundingLines || 0), 0);
const end = Math.min(
maxIndex + (maxSurroundingLines || 0),
contentLinesLength - 1
);
return { start, end };
};
const expandRange = (
start: number,
end: number,
contentLines: string[],
remainingSpace: number
) => {
// Define flags to control the alternating expansion
let expandStart = true;
let expandEnd = true;
while (remainingSpace > 0 && (expandStart || expandEnd)) {
if (expandStart && start > 0) {
const length = contentLines[start - 1].length + 1;
if (length <= remainingSpace) {
start--;
remainingSpace -= length;
} else {
expandStart = false;
}
}
if (expandEnd && end < contentLines.length - 1) {
const length = contentLines[end + 1].length + 1;
if (length <= remainingSpace) {
end++;
remainingSpace -= length;
} else {
expandEnd = false;
}
}
if (
(start === 0 || !expandStart) &&
(end === contentLines.length - 1 || !expandEnd)
) {
break;
}
if (start === 0) expandStart = false;
if (end === contentLines.length - 1) expandEnd = false;
}
return { start, end };
};
const createPromptContent = (
start: number,
end: number,
changedIndices: { [index: number]: string },
contentLines: string[]
) => {
let promptContent = start > 0 ? "...\n" : "";
for (let i = start; i <= end; i++) {
promptContent += (changedIndices[i] || contentLines[i]) + "\n";
}
if (end < contentLines.length - 1) {
promptContent += "...\n";
}
return promptContent.trim();
};
export const createPromptFiles = (
files: ReviewFile[],
maxPromptPayloadLength: number,
maxSurroundingLines?: number
): PromptFile[] => {
return files.map((file) => {
const contentLines = file.fileContent.split("\n");
const changedLinesArray = file.changedLines.split("\n");
// Get the changed indices, total length, and min/max indices
const { changedIndices, totalChangedLinesLength, minIndex, maxIndex } =
getChangedIndicesAndLength(contentLines, changedLinesArray);
// Calculate remaining space and start/end positions
let remainingSpace =
maxPromptPayloadLength - totalChangedLinesLength - file.fileName.length;
let { start, end } = calculateStartAndEnd(
minIndex,
maxIndex,
contentLines.length,
maxSurroundingLines
);
// Expand the range and create the prompt content
({ start, end } = expandRange(start, end, contentLines, remainingSpace));
const promptContent = createPromptContent(
start,
end,
changedIndices,
contentLines
);
return {
fileName: file.fileName,
promptContent: promptContent,
};
});
};