forked from copilot-extensions/blackbeard-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (57 loc) · 2.18 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
58
59
60
61
62
63
64
65
import { Octokit } from "@octokit/core";
import express from "express";
import { Readable } from "node:stream";
const app = express()
app.get("/", (req, res) => {
res.send("喵喵~ 欢迎👏来到 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);
const copilot_references = payload.messages[payload.messages.length - 1].copilot_references;
console.log("Copilot references:", copilot_references);
// 遍历copilot_references内容,如果type是github.repository,那么输出refInfo信息
copilot_references.forEach(reference => {
if (reference.type === "github.repository") {
console.log("Repository reference info:", reference.data.refInfo);
}
});
// Insert a special pirate-y system message in our message list.
const messages = payload.messages;
messages.unshift({
role: "system",
content: "你是一位乐于助人的助手,回复用户消息,就像你是一只可爱的小猫一样。并且回答问题时候会加一些emoji。",
});
messages.unshift({
role: "system",
content: `每个回复都以用户的名字开头,即 @${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}`)
});