-
Notifications
You must be signed in to change notification settings - Fork 0
/
aiHint.js
261 lines (237 loc) · 8.58 KB
/
aiHint.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
'use strict';
const fs = require('fs');
const modelAI = process.env.AI_MODEL || 'models/gemini-pro';
const aiUrl = 'https://generativelanguage.googleapis.com/v1beta/';
const ghUrl = process.env.GITHUB_API_URL;
const aiApiKey = process.env.AI_TOKEN;
const ghApiKey = process.env.GITHUB_TOKEN;
const repo = process.env.GITHUB_REPOSITORY;
const issue = process.env.ISSUE_NUMBER;
const rPath = process.env.RULES_PATH;
const sRepo = process.env.SEARCH_REPO || repo;
const botName = process.env.BOT_NAME || 'AI Hint';
let maxTokens, maxOutLen;
const setFailed = (msg) => {
process.exitCode = 1;
console.log(`::error::${msg}`);
}
const getURL = async (url, apiKey, noerr) => {
let h = {};
if (apiKey)
h = {headers: {'Authorization': `Bearer ${apiKey}`}};
const r = await fetch(url, h);
const json = await r.json();
if (r.ok) return json;
if (noerr) return '';
if (json.message)
throw new Error(json.message); else
if (json.error)
throw new Error(json.error.message); else
throw new Error(`Response status: ${r.status}`);
}
const GAIConfig = async () => {
const r = await getURL(aiUrl + modelAI + '?key=' + aiApiKey);
maxTokens = r.inputTokenLimit;
maxOutLen = r.outputTokenLimit;
}
const tokensLen = async (str) => {
const url = aiUrl + modelAI + ':countTokens?key=' + aiApiKey;
const r = await fetch(url, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
contents: [{parts:[{text:str}]}]
})
})
if (!r.ok)
throw new Error(`Response status: ${r.status}`);
const json = await r.json();
return json.totalTokens;
}
const strTokensTrim = async (str, limit) => {
let toks = await tokensLen(str);
while (toks > limit) {
str = str.slice(0, Math.floor(str.length * limit / toks) - 1);
toks = await tokensLen(str);
}
return str;
}
const sendGAIRequest = async (apiKey, model, prompt, max_tokens) => {
const url = aiUrl + model + ':generateContent?key=' + apiKey;
const r = await fetch(url, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
model: model,
contents: [{parts:[{text:prompt}]}],
generationConfig: {
temperature: 0, topK: 1, topP: 1,
maxOutputTokens: max_tokens,
stopSequences: []
},
safetySettings: [
{category: 'HARM_CATEGORY_HARASSMENT', threshold: 'BLOCK_MEDIUM_AND_ABOVE'},
{category: 'HARM_CATEGORY_HATE_SPEECH', threshold: 'BLOCK_MEDIUM_AND_ABOVE'},
{category: 'HARM_CATEGORY_SEXUALLY_EXPLICIT', threshold: 'BLOCK_MEDIUM_AND_ABOVE'},
{category: 'HARM_CATEGORY_DANGEROUS_CONTENT', threshold: 'BLOCK_MEDIUM_AND_ABOVE'}
]
})
});
const json = await r.json();
if (json.candidates)
return json.candidates[0].content.parts[0].text;
if (json.error)
throw new Error(json.error.message); else
throw new Error(`Response status: ${r.status}`);
}
const fetchCommit = async (i) => {
let data = `{Commit: ${i.commit.author.name}\nSha: ${i.sha}\n${i.commit.message}\n\n`;
if (i.commit.comment_count > 0) {
const c = await getURL(i.comments_url, ghApiKey);
if (c.forEach)
c.forEach(i => data += `${i.user.login}:\n${i.body}\n\n`);
}
return data + '}';
}
const fetchIssue = async (i) => {
let data = '{';
let state = '';
if (i.closed_at) state = 'Closed ';
if (i.pull_request) {
if (i.pull_request.merged_at) state = 'Merged ';
data += state + 'Pull Request: ';
} else
data += state + 'Issue: ';
data += `${i.title}\nNumber: ${i.number}\n${i.user.login}:\n${i.body}\n\n`;
if (i.comments > 0) {
const c = await getURL(i.comments_url, ghApiKey);
if (c.forEach)
c.forEach(i => data += `${i.user.login}:\n${i.body}\n\n`);
}
return data + '}';
}
const getLabels = async (repo) => {
let labels = 'Labels: {\n';
const r = await getURL(`${ghUrl}/repos/${repo}/labels`, ghApiKey);
for (const i of r)
labels += `"${i.name}" ${i.description}\n\n`;
return labels + '}';
}
const extractLinks = (str, repo) => {
const getIssue = async (i) => {
const url = `${ghUrl}/repos/${(i[2] || repo)}/`;
const r = await getURL(url + `issues/${i[4]}`, ghApiKey, true);
if (r.id)
return await fetchIssue(r);
return '';
}
const getCommit = async (c) => {
const url = `${ghUrl}/repos/${repo}/commits/${c[0]}`;
const r = await getURL(url, ghApiKey, true);
if (r.sha)
return await fetchCommit(r);
return '';
}
const promises = {};
let regex = /(\S*\/)*([\w-]+\/[\w-]+)*(#|\/issues\/)(\d+)/gs;
for (const issue of str.matchAll(regex))
promises[issue[4]] = getIssue(issue);
regex = /[A-Fa-f0-9]{6,}/gs;
for (const commit of str.matchAll(regex))
promises[commit[0]] = getCommit(commit);
return promises;
}
const getIssue = async (apiKey, repo, issue) => {
console.log(`${repo}#${issue}`);
const url = `${ghUrl}/repos/${repo}/issues/${issue}`;
const r = await getURL(url, apiKey);
return `${r.title}\n${r.body}`;
}
const newIssueComment = async (apiKey, repo, issue, text) => {
const url = `${ghUrl}/repos/${repo}/issues/${issue}/comments`;
const r = await fetch(url, {
method: 'POST',
headers: {'Authorization': `Bearer ${apiKey}`},
body: JSON.stringify({body: text})
});
if (!r.ok)
throw new Error(`Response status: ${r.status}`);
return await r.json();
}
const editIssueComment = async (apiKey, repo, commentId, text) => {
const url = `${ghUrl}/repos/${repo}/issues/comments/${commentId}`;
const r = await fetch(url, {
method: 'PATCH',
headers: {'Authorization': `Bearer ${apiKey}`},
body: JSON.stringify({body: text})
});
if (!r.ok)
throw new Error(`Response status: ${r.status}`);
return await r.json();
}
const getRules = async (apiKey, rPath) => {
if (rPath && rPath.startsWith('./'))
return fs.readFileSync(rPath, 'utf8');
let url = 'https://raw.githubusercontent.com/';
if (rPath)
url += `${process.env.GITHUB_REPOSITORY}/${process.env.GITHUB_REF_NAME}/${rPath}`; else
url += `${process.env.GITHUB_ACTION_REPOSITORY}/${process.env.GITHUB_ACTION_REF}/Rules.txt`;
const r = await fetch(url, {headers: {'Authorization': `Bearer ${apiKey}`}});
if (!r.ok)
throw new Error(`Response status: ${r.status}`);
return await r.text();
}
const makeHint = async () => {
if (typeof aiApiKey !== 'string' || aiApiKey.trim().length !== 39)
throw new Error('Invalid AI API key');
const [labels, rDesc, q, rules] = await Promise.all([
getLabels(sRepo), getURL(`${ghUrl}/repos/${sRepo}`, ghApiKey),
getIssue(ghApiKey, repo, issue), getRules(ghApiKey, rPath), GAIConfig()
]);
const [rSearch, rAnswer] = rules.split('---');
const curDate = new Date().toISOString();
let prompt = `${labels}\n\n${rSearch}\nDate: ${curDate}`
+ `\nRepo: {${rDesc.description}}\nQuestion: {${q}}\nGithub search:\n`;
let r = await sendGAIRequest(aiApiKey, modelAI, prompt, 100);
if (r.includes('#reject#')) return;
let iPrompt = r.match(/Issues:(.+)/); iPrompt = iPrompt ? iPrompt[1] : '';
let cPrompt = r.match(/Commits:(.+)/); cPrompt = cPrompt ? cPrompt[1] : '';
console.log(`Commits:${cPrompt}\nIssues:${iPrompt}`);
let data = '';
let cr = {}; let ir = {};
const promises = extractLinks(q, sRepo);
if (cPrompt) {
const per_page = Math.floor(maxTokens / 450);
const url = `${ghUrl}/search/commits?per_page=${per_page}&q=repo:${sRepo}${cPrompt}`;
cr = getURL(url, ghApiKey);
}
if (iPrompt) {
const per_page = Math.floor(maxTokens / 900);
const url = `${ghUrl}/search/issues?per_page=${per_page}&q=repo:${sRepo}${iPrompt}`;
ir = getURL(url);
}
[cr, ir] = await Promise.all([cr, ir]);
if (cr.items)
for (const i of cr.items)
if (!promises[i.sha])
promises[i.sha] = fetchCommit(i);
if (ir.items)
for (const i of ir.items)
if (!promises[i.id])
promises[i.id] = fetchIssue(i);
data += (await Promise.all(Object.values(promises))).join('');
prompt = `\n{Prompt:\n${q}}\n\nDate: ${curDate}\n${rAnswer}\nAnswer:\n`;
data = await strTokensTrim(data, maxTokens - (await tokensLen(prompt)));
console.log(`Data: ${data.length} chars.`);
prompt = data + prompt;
prompt = await sendGAIRequest(aiApiKey, modelAI, prompt, maxOutLen);
prompt = `${botName}:\n${prompt}`;
r = (await getURL(`${ghUrl}/repos/${repo}/issues/${issue}/comments`, ghApiKey))[0];
if (!r)
return await newIssueComment(ghApiKey, repo, issue, prompt);
if (r.user.type === 'Bot' && r.body.startsWith(botName))
return await editIssueComment(ghApiKey, repo, r.id, prompt);
}
makeHint()
.then(comment => console.log('Comment id:', comment ? comment.id : 'rejected'))
.catch(error => setFailed(error.stack));