-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.html
235 lines (215 loc) · 8.38 KB
/
index.html
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>RefactorGPT</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
max-width: 960px;
}
.indent {
margin-left: 20px;
}
button {
background-color: darkslateblue;
border: none;
color: white;
padding: 10px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
input[type=text], textarea, select {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
box-sizing: border-box;
}
</style>
</head>
<body>
<h1>RefactorGPT</h1>
<label style="font-weight: bold;" for="repo-url">GitHub Repo URL:</label>
<form id="repo-form" style="display: flex; align-items: center;">
<input style="max-width:500px;margin:0 10px;padding: 11px;" type="text" id="repo-url" name="repo-url" required>
<button type="submit">Get File Tree</button>
</form>
<br />
<div style="display: flex; justify-content: space-between;">
<div>
<h2>Select Relevant Files:</h2>
<div id="file-tree"></div>
</div>
<div>
<h2>Merged Files Preview:</h2>
<textarea id="output" rows="20" cols="80" readonly></textarea>
</div>
</div>
<br />
<br />
<h2>Instruction:</h2>
<textarea id="instruction" name="instruction" rows="20" cols="80" placeholder="rewrite this entire application" required></textarea>
<br />
<div style="display: flex; align-items: center; justify-content: space-between;">
<div>
<label for="models">Select Model:</label>
<select id="models" name="models">
<option>gpt-4-turbo-preview/option>
<option>gpt-3.5-turbo</option>
</select>
</div>
</div>
<div style="display: flex; align-items: center;">
<button id="send-to-openai" style="margin-right: 10px">Refactor!!!</button>
<label style="font-weight: bold;" for="openai-key">OpenAI Key:</label>
<input style="max-width:500px;margin:0 10px;padding: 11px;" type="password" id="openai-key" name="openai-key">
<button id="save-api-key">Save Key</button>
</div>
<br />
<h2>OpenAI Response:</h2>
<textarea id="openai-response" rows="20" cols="80" readonly></textarea>
</div>
<br />
<br />
<script>
document.getElementById('repo-form').addEventListener('submit', async (event) => {
event.preventDefault();
const repoUrl = document.getElementById('repo-url').value.split('github.com/')[1];
const apiUrl = `https://api.github.com/repos/${repoUrl}/contents`;
try {
const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error('GitHub API request failed');
}
const fileTree = await response.json();
displayFileTree(fileTree);
} catch (error) {
alert('Unable to fetch repo. Please confirm that its visibility is set to public and that you\'re not overusing the GitHub API.');
}
});
async function displayFileTree(fileTree, parentElement = null, indentLevel = 0) {
if (!parentElement) {
parentElement = document.getElementById('file-tree');
parentElement.innerHTML = '';
}
for (const file of fileTree) {
const fileElement = document.createElement('div');
if (indentLevel > 0) {
fileElement.classList.add('indent');
}
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.id = file.name;
checkbox.setAttribute('data-url', file.url);
checkbox.setAttribute('data-type', file.type);
const label = document.createElement('label');
label.htmlFor = file.name;
label.textContent = file.name;
fileElement.appendChild(checkbox);
fileElement.appendChild(label);
if (file.type === 'dir') {
try {
const response = await fetch(file.url);
if (!response.ok) {
throw new Error('GitHub API request failed');
}
const childFileTree = await response.json();
await displayFileTree(childFileTree, fileElement, indentLevel + 1);
} catch (error) {
alert('Unable to fetch repo. Please confirm that its visibility is set to public and that you\'re not overusing the GitHub API.');
return;
}
checkbox.addEventListener('change', () => {
const childCheckboxes = fileElement.querySelectorAll('input[type="checkbox"]');
for (const childCheckbox of childCheckboxes) {
childCheckbox.checked = checkbox.checked;
}
updateMergedFilesPreview();
});
} else {
checkbox.addEventListener('change', updateMergedFilesPreview);
}
parentElement.appendChild(fileElement);
}
}
async function updateMergedFilesPreview() {
const checkboxes = document.querySelectorAll('#file-tree input[type="checkbox"]');
const outputTextarea = document.getElementById('output');
outputTextarea.value = '';
for (const checkbox of checkboxes) {
if (checkbox.checked && checkbox.getAttribute('data-type') === 'file') {
const fileUrl = checkbox.getAttribute('data-url');
try {
const response = await fetch(fileUrl);
if (!response.ok) {
throw new Error('GitHub API request failed');
}
const fileContent = await response.json();
const decodedContent = atob(fileContent.content);
const fullPath = fileContent.path;
outputTextarea.value += `######## ${fullPath}\n\n${decodedContent}\n\n`;
} catch (error) {
alert('Unable to fetch repo. Please confirm that its visibility is set to public and that you\'re not overusing the GitHub API.');
return;
}
}
}
}
document.getElementById('save-api-key').addEventListener('click', () => {
const apiKey = document.getElementById('openai-key').value;
localStorage.setItem('openai-api-key', apiKey);
});
document.getElementById('send-to-openai').addEventListener('click', async () => {
const apiKey = localStorage.getItem('openai-api-key') || document.getElementById('openai-key').value;
if (!apiKey) {
alert('Please enter an API key.');
return;
}
const instruction = document.getElementById('instruction').value;
const outputTextarea = document.getElementById('output');
const openaiResponseTextarea = document.getElementById('openai-response');
const model = document.getElementById('models').value;
const messages = outputTextarea.value.split('########').map(content => {
return { role: "user", content: '######## ' + content };
});
messages.push({ role: "user", content: instruction });
const sendToOpenAIButton = document.getElementById('send-to-openai');
sendToOpenAIButton.disabled = true;
sendToOpenAIButton.textContent = 'Loading...';
const response = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`,
},
body: JSON.stringify({
model: model,
messages: messages,
temperature: 0,
}),
});
const completion = await response.json();
openaiResponseTextarea.value = completion.choices[0].message.content;
sendToOpenAIButton.disabled = false;
sendToOpenAIButton.textContent = 'Send to OpenAI';
});
// Load saved API key from localStorage
const savedApiKey = localStorage.getItem('openai-api-key');
if (savedApiKey) {
document.getElementById('openai-key').value = savedApiKey;
}
document.getElementById('save-api-key').addEventListener('click', () => {
const apiKey = document.getElementById('openai-key').value;
localStorage.setItem('openai-api-key', apiKey);
});
</script>
</body>
</html>