diff --git a/package-lock.json b/package-lock.json index 444bd1d..497888c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,7 @@ "name": "hackyeah", "version": "0.1.0", "dependencies": { + "body-parser": "^1.20.2", "connect-pg-simple": "^9.0.0", "cookie-parser": "~1.4.4", "cors": "^2.8.5", @@ -910,12 +911,12 @@ } }, "node_modules/body-parser": { - "version": "1.20.1", - "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", - "integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==", + "version": "1.20.2", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.2.tgz", + "integrity": "sha512-ml9pReCu3M61kGlqoTm2umSXTlRTuGTx0bfYj+uIUKKYycG5NtSbeetV3faSU6R7ajOPw0g/J1PvK4qNy7s5bA==", "dependencies": { "bytes": "3.1.2", - "content-type": "~1.0.4", + "content-type": "~1.0.5", "debug": "2.6.9", "depd": "2.0.0", "destroy": "1.2.0", @@ -923,7 +924,7 @@ "iconv-lite": "0.4.24", "on-finished": "2.4.1", "qs": "6.11.0", - "raw-body": "2.5.1", + "raw-body": "2.5.2", "type-is": "~1.6.18", "unpipe": "1.0.0" }, @@ -1522,6 +1523,29 @@ } ] }, + "node_modules/express/node_modules/body-parser": { + "version": "1.20.1", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", + "integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==", + "dependencies": { + "bytes": "3.1.2", + "content-type": "~1.0.4", + "debug": "2.6.9", + "depd": "2.0.0", + "destroy": "1.2.0", + "http-errors": "2.0.0", + "iconv-lite": "0.4.24", + "on-finished": "2.4.1", + "qs": "6.11.0", + "raw-body": "2.5.1", + "type-is": "~1.6.18", + "unpipe": "1.0.0" + }, + "engines": { + "node": ">= 0.8", + "npm": "1.2.8000 || >= 1.4.16" + } + }, "node_modules/express/node_modules/cookie": { "version": "0.5.0", "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.5.0.tgz", @@ -1549,6 +1573,20 @@ "node": ">= 0.8" } }, + "node_modules/express/node_modules/raw-body": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz", + "integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==", + "dependencies": { + "bytes": "3.1.2", + "http-errors": "2.0.0", + "iconv-lite": "0.4.24", + "unpipe": "1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/express/node_modules/safe-buffer": { "version": "5.2.1", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", @@ -2681,9 +2719,9 @@ } }, "node_modules/raw-body": { - "version": "2.5.1", - "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz", - "integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==", + "version": "2.5.2", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.2.tgz", + "integrity": "sha512-8zGqypfENjCIqGhgXToC8aB2r7YrBX+AQAfIPs/Mlk+BtPTztOvTS01NRW/3Eh60J+a48lt8qsCzirQ6loCVfA==", "dependencies": { "bytes": "3.1.2", "http-errors": "2.0.0", diff --git a/package.json b/package.json index cf1d502..c7fbcc0 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "build": "tsc" }, "dependencies": { + "body-parser": "^1.20.2", "connect-pg-simple": "^9.0.0", "cookie-parser": "~1.4.4", "cors": "^2.8.5", diff --git a/src/app.ts b/src/app.ts index 813fcc7..471a010 100644 --- a/src/app.ts +++ b/src/app.ts @@ -12,7 +12,8 @@ import pg from "pg"; import connectPg from "connect-pg-simple"; import path from "path"; import { simplifiedRepos } from "./utils"; -import { promptGpt } from "./gpt"; +import { createFileQuestionsPrompt, promptGpt } from "./gpt"; +import bodyParser from "body-parser"; const ghClientId = process.env.GITHUB_CLIENT_ID; const ghClientSecret = process.env.GITHUB_CLIENT_SECRET; @@ -125,7 +126,9 @@ globalEm.then((em) => { app.use(passport.initialize()); app.use(passport.session()); - app.use(cors()); + // app.use(cors()); + + app.use(bodyParser.json()); // Serve static react app app.use(express.static(path.join(__dirname, "../public"))); @@ -192,7 +195,7 @@ globalEm.then((em) => { } ); - app.get("/users/me", isAuthenticated, (req, res) => { + app.get("/api/users/me", isAuthenticated, (req, res) => { const user = req.user as User; const octokit = new Octokit({ auth: user.ghToken, @@ -233,29 +236,30 @@ globalEm.then((em) => { if (!repoTree.data.tree.length) { throw new Error("Cannot get tree data"); } - let repoJson = {}; + const files = repoTree.data.tree.filter((treeItem) => { + return ( + treeItem.type === "blob" && + treeItem.path?.endsWith(".js") && + treeItem.url + ); + }); const filesContent = await Promise.all( - repoTree.data.tree - .filter((treeItem) => { - return ( - treeItem.type === "blob" && - treeItem.path?.endsWith(".js") && - treeItem.url - ); - }) - .map(async ({ url }) => { - // Download file content - const fileContent = await octokit.request(url as any); - // Parse file content - const conentDecoded = Buffer.from( - fileContent.data.content, - "base64" - ).toString(); - return conentDecoded; - }) + files.map(async ({ url }) => { + // Download file content + const fileContent = await octokit.request(url as any); + // Parse file content + const conentDecoded = Buffer.from( + fileContent.data.content, + "base64" + ).toString(); + return conentDecoded; + }) ); console.log("JS files contents", filesContent); - return ["Some mock question"]; + const prompt = createFileQuestionsPrompt(filesContent[0]); + const gptRes = await promptGpt(prompt); + console.log(gptRes); + return gptRes.content; } app.get("/quiz-questions/:repoId", isAuthenticated, (req, res) => { @@ -271,6 +275,7 @@ globalEm.then((em) => { }); app.post("/prompt-gpt", (req, res) => { + console.log(req.body); const prompt = req.body.prompt; if (!prompt) { res.status(400).send({ error: "Missing prompt" }); diff --git a/src/gpt.ts b/src/gpt.ts index 6182e36..12686d7 100644 --- a/src/gpt.ts +++ b/src/gpt.ts @@ -12,22 +12,22 @@ const openai = new OpenAI({ apiKey: apiKey, }); -// function createFileQuestionsPrompt(fileContent) { -// return ` -// I have a following JavaScript file: +export function createFileQuestionsPrompt(fileContent: string) { + return ` + I have a following JavaScript file: -// ======== FILE STARTS HERE ======== -// ${fileContent} -// ======== FILE ENDS HERE ========== + ======== FILE STARTS HERE ======== + ${fileContent} + ======== FILE ENDS HERE ========== -// Based on this file, please generate a question that can be asked -// in order to test whether I understand the contents of the file. -// `; -// } + Based on this file, please generate 4 questions (each in a new line) + that can be asked in order to test whether I understand the contents of the file. + `; +} export async function promptGpt(prompt: string) { const resp = await openai.chat.completions.create({ - messages: [{ role: "user", content: "Say this is a test" }], + messages: [{ role: "user", content: prompt }], model: "gpt-3.5-turbo", }); return resp.choices[0].message;