-
Notifications
You must be signed in to change notification settings - Fork 65
/
index.js
57 lines (49 loc) · 1.74 KB
/
index.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
import { Octokit } from "@octokit/core";
import express from "express";
import { Readable } from "node:stream";
const app = express()
app.get("/", (req, res) => {
res.send("Ahoy, matey! Welcome to the Blackbeard Pirate GitHub Copilot Extension!")
});
app.post("/", express.json(), async (req, res) => {
// Identify the user, using the GitHub API token provided in the request headers.
const tokenForUser = req.get("X-GitHub-Token");
const octokit = new Octokit({ auth: tokenForUser });
const user = await octokit.request("GET /user");
console.log("User:", user.data.login);
// Parse the request payload and log it.
const payload = req.body;
console.log("Payload:", payload);
// Insert a special pirate-y system message in our message list.
const messages = payload.messages;
messages.unshift({
role: "system",
content: "You are a helpful assistant that replies to user messages as if you were the Blackbeard Pirate.",
});
messages.unshift({
role: "system",
content: `Start every response with the user's name, which is @${user.data.login}`,
});
// Use Copilot's LLM to generate a response to the user's messages, with
// our extra system messages attached.
const copilotLLMResponse = await fetch(
"https://api.githubcopilot.com/chat/completions",
{
method: "POST",
headers: {
authorization: `Bearer ${tokenForUser}`,
"content-type": "application/json",
},
body: JSON.stringify({
messages,
stream: true,
}),
}
);
// Stream the response straight back to the user.
Readable.from(copilotLLMResponse.body).pipe(res);
})
const port = Number(process.env.PORT || '3000')
app.listen(port, () => {
console.log(`Server running on port ${port}`)
});