-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
262 lines (243 loc) · 7.83 KB
/
utils.js
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import { DynamoDBClient, GetItemCommand } from '@aws-sdk/client-dynamodb';
import logger from './logger.js';
import env from './env.js';
const dynamodb = new DynamoDBClient({ region: 'us-east-1' });
export function base64ToMDX(base64Content) {
try {
// Step 1: Decode base64
const decodedContent = atob(base64Content);
// Step 2: Use the decoded content as MDX
// console.log("Decoded Content:", decodedContent);
// You can further process the decoded content or return it as is
return decodedContent;
} catch (error) {
// console.error('Error decoding base64:', error);
logger.error('Error decoding base64:', error);
return null;
}
}
export async function getRepo(repo, branch = "main", remote = "github", token = undefined) {
// console.log('getRepo', repo, branch, remote, token)
logger.info('getRepo', repo, branch, remote)
try {
const body = JSON.stringify({
"remote": remote, // one of "github", "gitlab" for now
"repository": repo, // formatted as owner/repository
"branch": branch, // optional, defaults to repo default on GH/GL
// "reload": true, // optional, if false will not reprocess if previously successful, default true
// "notify": true // optional, whether to notify the user when finished, default true
})
const repoInfo = await fetch(`${env.GREPTILE_API_URL}/repositories`, {
method: "POST",
body: body,
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + env.GREPTILE_API_KEY,
"X-Github-Token": token,
},
});
const repoInfoJson = await repoInfo.json();
return repoInfoJson;
} catch (error) {
if (env.DEBUG_MODE) {
// console.log("Error:", error);
logger.error("Error:", error);
}
return null;
}
}
export async function getRepoInfo(repo, remote, branch, token=undefined) {
// console.log('getRepoInfo', repo, remote, branch, token)
logger.info('getRepoInfo', repo, remote, branch)
const repoInfo = await fetch(`${env.GREPTILE_API_URL}/repositories/${encodeURIComponent(`${remote}:${branch}:${repo}`)}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + env.GREPTILE_API_KEY,
"X-Github-Token": token,
},
});
const repoInfoJson = await repoInfo.json();
return repoInfoJson;
}
export function parseIdentifier(input) {
// console.log(input)
logger.info('parseIdentifier', input)
if (!isDomain(input)) {
const regex = /^(([^:]*):([^:]*):|[^:]*)([^:]*)$/;
const match = input.match(regex);
if (!match) return null;
const keys = input.split(":");
if (keys.length === 1)
return serializeRepoKey({
remote: "github",
branch: "",
repository: keys[0],
});
if (keys.length === 3) {
let remote = keys[0],
branch = keys[1],
repository = keys[2];
if (remote === "azure" && repository.split("/").length == 2) {
let repository_list = repository.split("/");
repository_list.push(repository_list[1]);
repository = repositoryA_list.join("/");
}
return serializeRepoKey({
remote: remote,
branch: branch,
repository: repository,
});
}
return null; // only 2 entries may be ambiguous (1 might be as well...)
}
if (!input.startsWith("http")) input = "https://" + input;
if (input.endsWith(".git")) input = input.slice(0, -4);
try {
const url = new URL(input);
let remote = (() => {
try {
const services = ["github", "gitlab", "bitbucket", "azure", "visualstudio"];
return (services.find((service) => url.hostname.includes(service)) || null)
} catch (e) {
return null;
}
})();
if (!remote) return null;
let repository, branch, regex, match, org;
switch (remote) {
case "github":
regex =
/([a-zA-Z0-9\._-]+\/[a-zA-Z0-9\%\._-]+)[\/tree\/]*([a-zA-Z0-0\._-]+)?/;
match = url.pathname.match(regex);
repository = decodeURIComponent(match?.[1] || "");
branch = match?.[2];
break;
case "gitlab":
regex =
/([a-zA-Z0-9\._-]+\/[a-zA-Z0-9\%\._-]+)(?:\/\-)?(?:(?:\/tree\/)([a-zA-Z0-0\._-]+))?/;
match = url.pathname.match(regex);
repository = decodeURIComponent(match?.[1] || "");
branch = match?.[2];
break;
case "azure":
regex = /([a-zA-Z0-9\%\.\/_-]+)/;
match = url.pathname.match(regex);
repository =
match?.[1].split("/").filter((x) => x !== "_git" && x !== "") || [];
repository.push(repository?.slice(-1)[0]);
repository = decodeURIComponent(repository.slice(0, 3).join("/"));
branch = url.searchParams.get("version")?.slice(2); // remove 'GB' from the beginning
break;
case "visualstudio":
remote = "azure"
regex = /([a-zA-Z0-9\%\.\/_-]+)/;
org = url.hostname.split(".")[0];
match = url.pathname.match(regex);
repository =
match?.[1].split("/").filter((x) => x !== "_git" && x !== "") || [];
repository = decodeURIComponent([org, ...(repository.slice(0, 2))].join("/"));
branch = url.searchParams.get("version")?.slice(2); // remove 'GB' from the beginning
break;
default:
return url.hostname;
}
if (!repository) return null;
// console.log(remote,branch,repository)
if (typeof branch === "undefined") {
branch = "main";
}
return { remote, branch, repository };
// return serializeRepoKey({
// remote: remote,
// branch: branch || "main",
// repository: repository,
// });
} catch (e) {
return null;
}
};
export function createPayload(repo, payloadContent, external = false) {
const parsedRepo = parseIdentifier(repo);
const payload = {
messages: [
{
id: '1',
role: "user",
content: payloadContent
}
],
repositories: [
{
remote: parsedRepo.remote,
repository: parsedRepo.repository,
branch: parsedRepo.branch,
name: parsedRepo.repository,
external: external,
}
],
jsonMode: true
};
return payload;
}
export async function useChatApi(repository, userQuestion, token=undefined) {
const payload = createPayload(repository, userQuestion);
if (env.DEBUG_MODE)
logger.info('useChatApi (payload)', { payload })
// console.log(payload)
try {
const response = await fetch(`${env.GREPTILE_API_URL}/query`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
"Authorization": "Bearer " + env.GREPTILE_API_KEY,
"X-Github-Token": token,
},
body: JSON.stringify(payload),
})
if (env.DEBUG_MODE) {
logger.info('useChatApi (response)', { response })
// console.log(response)
}
const responseJson = await response.json();
return responseJson;
} catch (error) {
if (env.DEBUG_MODE) {
// console.error('Error:', error.message);
logger.error('Error:', error.message);
}
}
}
export async function getToken(ownerEmail) {
const params = {
TableName: env.USERS_TABLE_NAME,
Key: {
'email': { S: ownerEmail },
},
};
try {
const data = await dynamodb.send(new GetItemCommand(params));
const token = data.Item?.tokens?.M?.github?.M?.accessToken?.S;
// console.log(token)
return token;
} catch (err) {
// console.error(err);
logger.error('error getting token', err);
return undefined
}
}
function serializeRepoKey(repoKey) {
const { remote, branch, repository } = repoKey;
return `${remote}:${branch}:${repository}`;
}
function isDomain(input) {
try {
new URL(input);
const regex = /^(([^:]*):([^:]*):|[^:]*)([^:]*)$/;
const match = input.match(regex);
if (match) return false;
return true;
} catch (e) {
return false;
}
}