|
| 1 | +import { createServer } from "node:http"; |
| 2 | +import { Octokit } from "octokit"; |
| 3 | +import { |
| 4 | + createAckEvent, |
| 5 | + createDoneEvent, |
| 6 | + prompt, |
| 7 | + verifyAndParseRequest, |
| 8 | +} from "@copilot-extensions/preview-sdk"; |
| 9 | + |
| 10 | +// Define the port to listen on |
| 11 | +const PORT = 3000; |
| 12 | + |
| 13 | +// Define the handler function |
| 14 | +async function handler(request, response) { |
| 15 | + console.log(`Received [${request.method}] to [${request.url}]`); |
| 16 | + |
| 17 | + if (request.method !== "POST") { |
| 18 | + // Handle other request methods if necessary |
| 19 | + response.writeHead(405, { "Content-Type": "text/plain" }); |
| 20 | + console.log(`Method ${request.method} not allowed`); |
| 21 | + |
| 22 | + response.end("Method Not Allowed"); |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + // get a token to use |
| 27 | + const tokenForUser = request.headers["x-github-token"]; |
| 28 | + |
| 29 | + // get the user information with the token |
| 30 | + const octokit = new Octokit({ auth: tokenForUser }); |
| 31 | + const user = await octokit.request("GET /user"); |
| 32 | + |
| 33 | + // Collect incoming data chunks to use in the `on("end")` event |
| 34 | + const body = await getBody(request); |
| 35 | + const signature = String(request.headers["github-public-key-signature"]); |
| 36 | + const keyID = String(request.headers["github-public-key-identifier"]); |
| 37 | + |
| 38 | + try { |
| 39 | + const { isValidRequest, payload } = await verifyAndParseRequest( |
| 40 | + body, |
| 41 | + signature, |
| 42 | + keyID, |
| 43 | + { |
| 44 | + token: tokenForUser, |
| 45 | + }, |
| 46 | + ); |
| 47 | + |
| 48 | + if (!isValidRequest) { |
| 49 | + console.error("Request verification failed"); |
| 50 | + response.writeHead(401, { "Content-Type": "text/plain" }); |
| 51 | + response.end("Request could not be verified"); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + // write the acknowledge event to let Copilot know we are handling the request |
| 56 | + // this will also show the message "Copilot is responding" in the chat |
| 57 | + response.write(createAckEvent()); |
| 58 | + |
| 59 | + console.log("Calling the GitHub Copilot API with the user prompt"); |
| 60 | + // the prompt to forward to the Copilot API is the last message in the payload |
| 61 | + const payload_message = payload.messages[payload.messages.length - 1]; |
| 62 | + const { stream } = await prompt.stream(payload_message.content, { |
| 63 | + system: `You are a helpful assistant that replies to user messages as if you were the Blackbeard Pirate. Start every response with the user's name, which is @${user.data.login}`, // extra instructions for the prompt |
| 64 | + messages: payload.messages, // we are giving the prompt the existing messages in this chat conversation for context |
| 65 | + token: tokenForUser, |
| 66 | + }); |
| 67 | + |
| 68 | + // stream the prompt response back to Copilot |
| 69 | + for await (const chunk of stream) { |
| 70 | + response.write(new TextDecoder().decode(chunk)); |
| 71 | + } |
| 72 | + |
| 73 | + // write the done event to let Copilot know we are done handling the request |
| 74 | + response.end(createDoneEvent()); |
| 75 | + console.log("Response sent"); |
| 76 | + } catch (error) { |
| 77 | + console.error("Error:", error); |
| 78 | + response.writeHead(500, { "Content-Type": "text/plain" }); |
| 79 | + response.end("Internal Server Error"); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// Create an HTTP server |
| 84 | +const server = createServer(handler); |
| 85 | + |
| 86 | +// Start the server |
| 87 | +server.listen(PORT); |
| 88 | +console.log(`Server started at http://localhost:${PORT}`); |
| 89 | + |
| 90 | +/** |
| 91 | + * |
| 92 | + * @param {import("node:http").IncomingMessage} request |
| 93 | + * @returns |
| 94 | + */ |
| 95 | +function getBody(request) { |
| 96 | + return new Promise((resolve) => { |
| 97 | + const bodyParts = []; |
| 98 | + let body; |
| 99 | + request |
| 100 | + .on("data", (chunk) => { |
| 101 | + bodyParts.push(chunk); |
| 102 | + }) |
| 103 | + .on("end", () => { |
| 104 | + body = Buffer.concat(bodyParts).toString(); |
| 105 | + resolve(body); |
| 106 | + }); |
| 107 | + }); |
| 108 | +} |
0 commit comments