Skip to content

Commit

Permalink
added shared/global caching system for faster results
Browse files Browse the repository at this point in the history
  • Loading branch information
taylor hawkes authored and taylor hawkes committed Dec 6, 2022
1 parent caa07a4 commit 05a54be
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 1,771 deletions.
47 changes: 37 additions & 10 deletions src/background/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,31 @@ async function getAccessToken() {
return resp.accessToken;
}

async function cacheAnswer(question,answer) {
const rawResponse = await fetch('https://www.codegrepper.com/api/cache_answer.php', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({term: question, answer:answer})
});
const content = await rawResponse.json();
console.log(content);
}

async function getAnswer(question, callback) {

const accessToken = await getAccessToken();
const resp = await fetch("https://www.codegrepper.com/api/cache_get_answers.php?term="+question)
.then((r) => r.json())
.catch(() => ({}));
if(resp.answers.length){
callback(resp.answers[0].answer);
callback("[DONE_FROM_CACHE]");
return;
}

await fetchSSE("https://chat.openai.com/backend-api/conversation", {
method: "POST",
headers: {
Expand All @@ -45,7 +68,7 @@ async function getAnswer(question, callback) {
parent_message_id: uuidv4(),
}),
onMessage(message) {
console.debug("sse message", message);
//console.debug("sse message", message);
if (message === "[DONE]") {
callback("[DONE]");
return;
Expand All @@ -61,15 +84,19 @@ async function getAnswer(question, callback) {

Browser.runtime.onConnect.addListener((port) => {
port.onMessage.addListener(async (msg) => {
console.debug("received msg", msg);
try {
await getAnswer(msg.question, (answer) => {
port.postMessage({ answer });
});
} catch (err) {
console.error(err);
port.postMessage({ error: err.message });
cache.delete(KEY_ACCESS_TOKEN);
if(msg.action=="getAnswer"){
try {
await getAnswer(msg.question, (answer) => {
port.postMessage({ answer });
});
} catch (err) {
console.error(err);
port.postMessage({ error: err.message });
cache.delete(KEY_ACCESS_TOKEN);
}

} else if(msg.action=="cacheAnswer"){
cacheAnswer(msg.question,msg.answer)
}
});
});
23 changes: 17 additions & 6 deletions src/content-script/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ commando.prototype.displayResult =function(answer,container) {

var codeResults = document.createElement("code");
codeResults.textContent=answer.answer;
codeResults.classList.add("commando_code_block");
codeResults.classList.add("gpt_commando_code_block");

var languageGuess="javascript";
if(answer.language){
Expand All @@ -58,10 +58,10 @@ commando.prototype.displayResult =function(answer,container) {
var codeResultsPre = document.createElement("pre");
codeResultsPre.classList.add("language-"+languageGuess);
codeResultsPre.appendChild(codeResults);
codeResultsPre.classList.add("commando_selectable");
codeResultsPre.classList.add("gpt_commando_selectable");

var codeResultsOuter = document.createElement("div");
codeResultsOuter.classList.add("commando_code_block_outer");
codeResultsOuter.classList.add("gpt_commando_code_block_outer");

var answerOptionsHolder= document.createElement("div");
answerOptionsHolder.classList.add("commando_answers_options_holder");
Expand All @@ -84,7 +84,7 @@ let co=new commando();
async function run(question) {

const container = document.createElement("div");
container.className = "chat-gpt-container";
container.className = "chat_gpt_container_enhanced";

let lastAnswer=false;
const loading=document.createElement("p");
Expand All @@ -103,8 +103,15 @@ async function run(question) {

const port = Browser.runtime.connect();
port.onMessage.addListener(function (msg) {
if(msg.answer=="[DONE]"){
if(msg.answer=="[DONE_FROM_CACHE]"){
parseIntoAnswers(lastAnswer,container,true);
}else if(msg.answer=="[DONE]"){
parseIntoAnswers(lastAnswer,container,true);
port.postMessage({
'action':'cacheAnswer',
'question':question,
'answer':lastAnswer
});
}else if (msg.answer) {
lastAnswer=msg.answer;
if(!doneLoading){
Expand All @@ -120,9 +127,13 @@ async function run(question) {
container.innerHTML = "<p>Failed to load response from ChatGPT</p>";
}
});
port.postMessage({ question });
port.postMessage({
'action':'getAnswer',
'question':question
});
}


let allAnswers=[];
function parseIntoAnswers(content,container,isDone){
if(content.length > 1){
Expand Down
Loading

0 comments on commit 05a54be

Please sign in to comment.