diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..e69de29 diff --git a/dist/lib/DocFileData.js b/dist/lib/DocFileData.js deleted file mode 100644 index 78f18b8..0000000 --- a/dist/lib/DocFileData.js +++ /dev/null @@ -1,21 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -class DocFileData { - constructor(tool, path, patternFilename, patternDescription) { - this.tool = tool; - this.path = path; - this.patternFilename = patternFilename; - this.patternDescription = patternDescription; - } - toDict(keyPrefix = "init__") { - const output = {}; - for (const key in this) { - if (this.hasOwnProperty(key)) { - const snakeCaseKey = key.replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase(); - output[keyPrefix + snakeCaseKey] = this[key]; - } - } - return output; - } -} -exports.default = DocFileData; diff --git a/dist/lib/GoogleSearch.js b/dist/lib/GoogleSearch.js deleted file mode 100644 index e4385c1..0000000 --- a/dist/lib/GoogleSearch.js +++ /dev/null @@ -1,49 +0,0 @@ -"use strict"; -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -const axios_1 = __importDefault(require("axios")); -const cheerio_1 = __importDefault(require("cheerio")); -const Scraper_1 = __importDefault(require("./Scraper")); -class GoogleSearch { - constructor() { - this.scraper = new Scraper_1.default(); - } - execute(query) { - return __awaiter(this, void 0, void 0, function* () { - const resultUrls = yield this._search(query); - const resultData = this.scraper.scrape(resultUrls); - return JSON.stringify(resultData); - }); - } - _search(query) { - return __awaiter(this, void 0, void 0, function* () { - const response = yield axios_1.default.get('https://www.google.com/search', { - params: { q: query }, - headers: { - 'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0' - } - }); - const $ = cheerio_1.default.load(response.data); - const links = $('#search span>a'); // TODO This is flaky - const resultUrls = []; - links.each((index, element) => { - if (index < 3) { // TODO improve this - resultUrls.push($(element).attr('href') || ''); - } - }); - return resultUrls; - }); - } -} -exports.default = GoogleSearch; diff --git a/dist/lib/Gpt.js b/dist/lib/Gpt.js deleted file mode 100644 index 13c9d99..0000000 --- a/dist/lib/Gpt.js +++ /dev/null @@ -1,40 +0,0 @@ -"use strict"; -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -Object.defineProperty(exports, "__esModule", { value: true }); -const openai_1 = require("@langchain/openai"); -const prompts_1 = require("@langchain/core/prompts"); -const output_parsers_1 = require("@langchain/core/output_parsers"); -class Gpt { - constructor(openaiApiKey, model = "gpt-4o") { - this.model = new openai_1.ChatOpenAI({ model, apiKey: openaiApiKey }); - } - execute(promptHuman_1) { - return __awaiter(this, arguments, void 0, function* (promptHuman, promptData = {}, promptSystem) { - const promptTemplateMessages = []; - if (promptSystem) { - promptTemplateMessages.push(["system", promptSystem]); - } - promptTemplateMessages.push(["human", promptHuman]); - return yield this.doPromptFromMessages(promptTemplateMessages, promptData); - }); - } - doPromptFromMessages(promptTemplateMessages_1) { - return __awaiter(this, arguments, void 0, function* (promptTemplateMessages, promptData = {}) { - const prompt = prompts_1.ChatPromptTemplate.fromMessages(promptTemplateMessages); - const outputParser = new output_parsers_1.StringOutputParser(); - const chain = prompt - .pipe(this.model) - .pipe(outputParser); - return yield chain.invoke(promptData); - }); - } -} -exports.default = Gpt; diff --git a/dist/lib/PatPatBot.js b/dist/lib/PatPatBot.js deleted file mode 100644 index bddd987..0000000 --- a/dist/lib/PatPatBot.js +++ /dev/null @@ -1,63 +0,0 @@ -"use strict"; -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -const init_1 = require("./init"); -const PromptTemplate_1 = __importDefault(require("./PromptTemplate")); -const string_format_1 = __importDefault(require("string-format")); -class PatPatBot { - constructor(gpt, search) { - this.gpt = gpt; - this.search = search; - this.promptTemplates = this.loadPromptTemplates(); - this.promptData = {}; - } - setSourceDocData(docFileData) { - this.promptData = docFileData.toDict(); - } - processSourceDoc() { - return __awaiter(this, void 0, void 0, function* () { - for (const promptTemplate of this.promptTemplates) { - if (promptTemplate.isSearch()) { - yield this.doSearch(promptTemplate); - } - else if (promptTemplate.isGpt) { - yield this.doPrompt(promptTemplate); - } - } - }); - } - getPromptData(promptId) { - return this.promptData[promptId]; - } - setPromptData(promptId, result) { - this.promptData[promptId] = result; - } - doSearch(promptTemplate) { - return __awaiter(this, void 0, void 0, function* () { - const prompt = (0, string_format_1.default)(promptTemplate.promptHuman, this.promptData); - const result = yield this.search.execute(prompt); - this.setPromptData(promptTemplate.id, result); - }); - } - doPrompt(promptTemplate) { - return __awaiter(this, void 0, void 0, function* () { - const result = yield this.gpt.execute(promptTemplate.promptHuman, this.promptData, promptTemplate.promptSystem); - this.setPromptData(promptTemplate.id, result); - }); - } - loadPromptTemplates() { - return init_1.FILES_PROMPTS.map(filePath => PromptTemplate_1.default.fromFilePath(filePath)); - } -} -exports.default = PatPatBot; diff --git a/dist/lib/PromptTemplate.js b/dist/lib/PromptTemplate.js deleted file mode 100644 index ae433e2..0000000 --- a/dist/lib/PromptTemplate.js +++ /dev/null @@ -1,48 +0,0 @@ -"use strict"; -var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - var desc = Object.getOwnPropertyDescriptor(m, k); - if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { - desc = { enumerable: true, get: function() { return m[k]; } }; - } - Object.defineProperty(o, k2, desc); -}) : (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - o[k2] = m[k]; -})); -var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { - Object.defineProperty(o, "default", { enumerable: true, value: v }); -}) : function(o, v) { - o["default"] = v; -}); -var __importStar = (this && this.__importStar) || function (mod) { - if (mod && mod.__esModule) return mod; - var result = {}; - if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); - __setModuleDefault(result, mod); - return result; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -const fs = __importStar(require("fs")); -const path = __importStar(require("path")); -class PromptTemplate { - constructor(promptId, promptSystem, promptHuman, tool) { - this.id = promptId; - this.promptSystem = promptSystem; - this.promptHuman = promptHuman; - this.tool = tool; - } - isSearch() { - return this.tool === "google"; - } - get isGpt() { - return this.tool === 'gpt'; - } - static fromFilePath(filePath) { - var _a; - const promptFileData = JSON.parse(fs.readFileSync(filePath, 'utf-8')); - const promptId = ((_a = path.basename(filePath).match(/\d{2}-(.+)\.json$/)) === null || _a === void 0 ? void 0 : _a[1]) || ''; - return new PromptTemplate(promptId, promptFileData.prompt_system || "", promptFileData.prompt_human || "", promptFileData.tool || ""); - } -} -exports.default = PromptTemplate; diff --git a/dist/lib/Repository.js b/dist/lib/Repository.js deleted file mode 100644 index f276c27..0000000 --- a/dist/lib/Repository.js +++ /dev/null @@ -1,29 +0,0 @@ -"use strict"; -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -const path_1 = require("path"); -const fs_1 = require("fs"); -const glob_1 = require("glob"); -const DocFileData_1 = __importDefault(require("./DocFileData")); -class Repository { - constructor(name, docsGlob, replaceName = "codacy-") { - this.name = replaceName ? name.replace(replaceName, "") : name; - this.docFileData = this.loadDocFileData(docsGlob); - } - get docs() { - return this.docFileData; - } - static fromSettings(name, docsGlob) { - return new Repository(name, docsGlob); - } - loadDocFileData(docsGlob) { - return (0, glob_1.globSync)(docsGlob).map(doc => new DocFileData_1.default(this.name, doc, (0, path_1.basename)(doc), (0, fs_1.readFileSync)(doc, 'utf-8'))); - } - static repoNameFromUrl(url) { - const match = url.match(/.*\/(.*)\.git/); - return match ? match[1] : ''; - } -} -exports.default = Repository; diff --git a/dist/lib/Scraper.js b/dist/lib/Scraper.js deleted file mode 100644 index 2482fef..0000000 --- a/dist/lib/Scraper.js +++ /dev/null @@ -1,76 +0,0 @@ -"use strict"; -var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - var desc = Object.getOwnPropertyDescriptor(m, k); - if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { - desc = { enumerable: true, get: function() { return m[k]; } }; - } - Object.defineProperty(o, k2, desc); -}) : (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - o[k2] = m[k]; -})); -var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { - Object.defineProperty(o, "default", { enumerable: true, value: v }); -}) : function(o, v) { - o["default"] = v; -}); -var __importStar = (this && this.__importStar) || function (mod) { - if (mod && mod.__esModule) return mod; - var result = {}; - if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); - __setModuleDefault(result, mod); - return result; -}; -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -const axios_1 = __importDefault(require("axios")); -const cheerio = __importStar(require("cheerio")); -class Scraper { - scrape(urls) { - return __awaiter(this, void 0, void 0, function* () { - const results = yield this.scrapeAsync(urls); - return results; - }); - } - scrapeAsync(urls) { - return __awaiter(this, void 0, void 0, function* () { - const tasks = urls.map(url => this.fetchAndParse(url)); - return Promise.all(tasks); - }); - } - fetchAndParse(url) { - return __awaiter(this, void 0, void 0, function* () { - let html = ""; - try { - html = yield this.fetch(url); - } - catch (error) { - // TODO: Log this error - } - return this.parseHtml(html); - }); - } - fetch(url) { - return __awaiter(this, void 0, void 0, function* () { - const response = yield axios_1.default.get(url); - return response.data; - }); - } - parseHtml(html) { - const $ = cheerio.load(html); - return $.text(); - } -} -exports.default = Scraper; diff --git a/dist/lib/init.js b/dist/lib/init.js deleted file mode 100644 index e5f6ee8..0000000 --- a/dist/lib/init.js +++ /dev/null @@ -1,19 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.FILES_PROMPTS = exports.FILES_DOCS = exports.ENV_REPO_NAME = exports.ENV_DOCS_GLOB = exports.ENV_OPENAI_API_KEY = void 0; -const dotenv_1 = require("dotenv"); -const glob_1 = require("glob"); -const path_1 = require("path"); -// Load environment variables -(0, dotenv_1.config)(); -const PATH_PROJECT = (0, path_1.dirname)((0, path_1.dirname)((0, path_1.dirname)(__filename))); -const ENV_OPENAI_API_KEY = process.env.OPENAI_API_KEY; -exports.ENV_OPENAI_API_KEY = ENV_OPENAI_API_KEY; -const ENV_DOCS_GLOB = process.env.INPUT_DOCS_GLOB; -exports.ENV_DOCS_GLOB = ENV_DOCS_GLOB; -const ENV_REPO_NAME = process.env.REPO_NAME; -exports.ENV_REPO_NAME = ENV_REPO_NAME; -const FILES_DOCS = glob_1.glob.sync(ENV_DOCS_GLOB || ''); -exports.FILES_DOCS = FILES_DOCS; -const FILES_PROMPTS = glob_1.glob.sync((0, path_1.join)(PATH_PROJECT, "prompts", "*.json")).sort(); -exports.FILES_PROMPTS = FILES_PROMPTS; diff --git a/dist/main.js b/dist/main.js deleted file mode 100644 index b57aca8..0000000 --- a/dist/main.js +++ /dev/null @@ -1,21 +0,0 @@ -"use strict"; -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -const init_1 = require("./lib/init"); -const GoogleSearch_1 = __importDefault(require("./lib/GoogleSearch")); -const Gpt_1 = __importDefault(require("./lib/Gpt")); -const PatPatBot_1 = __importDefault(require("./lib/PatPatBot")); -const Repository_1 = __importDefault(require("./lib/Repository")); -function run() { - const bot = new PatPatBot_1.default(new Gpt_1.default(init_1.ENV_OPENAI_API_KEY || ''), new GoogleSearch_1.default()); - const repo = new Repository_1.default(init_1.ENV_REPO_NAME || '', init_1.ENV_DOCS_GLOB || ''); - repo.docs.slice(0, 1).map(doc => { - bot.setSourceDocData(doc); - bot.processSourceDoc().then(() => { - console.log(bot.getPromptData('examine')); - }); - }); -} -run();