-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
312 lines (283 loc) · 9.35 KB
/
main.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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import OpenAI from 'openai';
import { config } from './config.js';
import { ChatCompletion } from 'openai/resources';
import fetch from 'node-fetch';
const API_KEY = 'API_KEY';
const GH_TOKEN = 'GH_TOKEN';
interface Message {
role: string;
content: string;
tool_call_id?: string | null;
name?: string | null;
}
interface Completion {
Content: string | null;
Error?: string | undefined;
TokenUsage: number | undefined;
ToolCalls?: any; // Add this line to include tool calls
}
interface ConnectorResponse {
Completions: Completion[];
ModelType: string;
}
interface ErrorCompletion {
choices: Array<{
message: {
content: string;
};
}>;
error: string;
model: string;
usage: undefined;
}
type GitHubFunction = (...args: any[]) => Promise<any>;
interface AvailableFunctions {
[key: string]: GitHubFunction;
}
const mapToResponse = (
outputs: Array<ChatCompletion | ErrorCompletion>,
model: string,
): ConnectorResponse => {
return {
Completions: outputs.map((output) => {
if ('error' in output) {
return {
Content: null,
TokenUsage: undefined,
Error: output.error,
};
} else {
return {
Content: output.choices[0]?.message?.content,
TokenUsage: output.usage?.total_tokens,
};
}
}),
ModelType: outputs[0].model || model,
};
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const mapErrorToCompletion = (error: any, model: string): ErrorCompletion => {
const errorMessage = error.message || JSON.stringify(error);
return {
choices: [],
error: errorMessage,
model,
usage: undefined,
};
};
async function fetchFromGitHub(url: string, token: string) {
const response = await fetch(url, {
headers: {
'Authorization': `token ${token}`,
"Accept": "application/vnd.github.v3.diff",
}
});
if (!response.ok) {
throw new Error('GitHub API request failed: ' + response.statusText);
}
const data = await response.json();
return JSON.stringify(data);
}
// Function to get user data
async function getUserData(token: string, username: string) {
const url = `https://api.github.com/users/${username}`;
return fetchFromGitHub(url, token);
}
// Function to get repository data for the user
async function getRepositoryData(token: string, username: string) {
const url = `https://api.github.com/users/${username}/repos`;
return fetchFromGitHub(url, token);
}
// Function to get commit history for a specific repository
async function getCommitHistory(token: string, username: string, repoName: string) {
const url = `https://api.github.com/repos/${username}/${repoName}/commits`;
return fetchFromGitHub(url, token);
}
// Function to get diff from a pull request
async function getPullRequestDiff(token: string, username: string, repoName: string, pullRequestNumber: number) {
const url = `https://api.github.com/repos/${username}/${repoName}/pulls/${pullRequestNumber}/files`;
return fetchFromGitHub(url, token);
}
async function main(
model: string,
prompts: string[],
properties: Record<string, unknown>,
settings: Record<string, unknown>,
) {
const openai = new OpenAI({
apiKey: settings?.[API_KEY] as string,
});
const gh_token = settings?.[GH_TOKEN] as string;
const total = prompts.length;
const { prompt, ...restProperties } = properties;
const systemPrompt = (prompt ||
config.properties.find((prop) => prop.id === 'prompt')?.value) as string;
const messageHistory: Message[] = [{ role: 'system', content: systemPrompt }];
const outputs: Array<ChatCompletion | ErrorCompletion> = [];
const tools = [
{
"type": "function",
"function": {
"name": "getUserData",
"description": "Fetch user data from GitHub",
"parameters": {
"type": "object",
"properties": {
"username": {
"type": "string",
"description": "The GitHub username of the user"
},
"token": {
"type": "string",
"description": "The access token for GitHub API authentication"
}
},
"required": ["username", "token"]
}
}
},
{
"type": "function",
"function": {
"name": "getRepositoryData",
"description": "Fetch repository data for a user from GitHub",
"parameters": {
"type": "object",
"properties": {
"username": {
"type": "string",
"description": "The GitHub username of the user whose repositories are to be fetched"
},
"token": {
"type": "string",
"description": "The access token for GitHub API authentication"
}
},
"required": ["username", "token"]
}
}
},
{
"type": "function",
"function": {
"name": "getCommitHistory",
"description": "Fetch the commit history for a specific repository from GitHub",
"parameters": {
"type": "object",
"properties": {
"username": {
"type": "string",
"description": "The GitHub username of the repository owner"
},
"repoName": {
"type": "string",
"description": "The name of the repository"
},
"token": {
"type": "string",
"description": "The access token for GitHub API authentication"
}
},
"required": ["username", "repoName", "token"]
}
}
},
{
"type": "function",
"function": {
"name": "getPullRequestDiff",
"description": "Fetches the differential changes of a specific pull request from a GitHub repository.",
"parameters": {
"type": "object",
"properties": {
"username": {
"type": "string",
"description": "The GitHub username of the repository owner"
},
"repoName": {
"type": "string",
"description": "The name of the repository from which the pull request diff will be fetched"
},
"pullRequestNumber": {
"type": "number",
"description": "The number identifying the specific pull request"
},
"token": {
"type": "string",
"description": "Authentication token used to access the GitHub API"
}
},
"required": ["username", "repoName", "pullRequestNumber", "token"]
}
}
}
];
try {
for (let index = 0; index < total; index++) {
try {
messageHistory.push({ role: 'user', content: prompts[index] });
const chatCompletion = await openai.chat.completions.create({
messages: messageHistory as unknown as [],
model,
tools: tools.map(tool => ({ type: "function", function: tool.function })),
tool_choice: "auto",
...restProperties,
});
const assistantResponse = chatCompletion.choices[0].message.content || 'No response.';
messageHistory.push({ role: 'assistant', content: assistantResponse });
console.log('Chat completion:', chatCompletion);
// Check if the assistant's response contains a tool call
const toolCalls = chatCompletion.choices[0].message.tool_calls;
if (toolCalls) {
const availableFunctions: AvailableFunctions = {
getUserData: getUserData,
getRepositoryData: getRepositoryData,
getCommitHistory: getCommitHistory,
getPullRequestDiff: getPullRequestDiff
};
for (const toolCall of toolCalls) {
const functionName = toolCall.function.name;
const functionToCall = availableFunctions[functionName];
const functionArgs = JSON.parse(toolCall.function.arguments);
if ('token' in functionArgs) {
functionArgs.token = gh_token;
}
console.log('Function arguments:', functionArgs);
const functionResponse = await functionToCall(
functionArgs.token,
functionArgs.username,
functionArgs.repoName,
functionArgs.pullRequestNumber,
);
messageHistory.push({
tool_call_id: toolCall.id,
role: "function",
name: functionName,
content: functionResponse,
});
}
const secondResponse = await openai.chat.completions.create({
model: model,
messages: messageHistory as unknown as [],
...restProperties,
});
const secondAssistantResponse = secondResponse.choices[0].message.content || 'No response.';
outputs.push(secondResponse);
messageHistory.push({ role: 'assistant', content: secondAssistantResponse });
} else {
outputs.push(chatCompletion);
}
} catch (error) {
console.error('Error in main loop:', error);
const completionWithError = mapErrorToCompletion(error, model);
outputs.push(completionWithError);
}
}
return mapToResponse(outputs, model);
} catch (error) {
console.error('Error in main function:', error);
return { Error: error, ModelType: model };
}
}
export { main, config };