adjusted the abort to 15s and fixed can't click back after error

This commit is contained in:
yoannchb-pro
2023-06-20 13:57:36 -04:00
parent 68e7c07d4f
commit 6f40acd45f
6 changed files with 27 additions and 22 deletions
+11 -8
View File
@@ -79,7 +79,7 @@
function getChatGPTResponse(config, question) {
return __awaiter(this, void 0, void 0, function* () {
const controller = new AbortController();
const timeoutControler = setTimeout(() => controller.abort(), 10000);
const timeoutControler = setTimeout(() => controller.abort(), 15000);
const req = yield fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
@@ -139,7 +139,7 @@
* @returns
*/
function createQuestion(config, questionContainer) {
let question = questionContainer.innerText;
let question = questionContainer.innerText; //TODO: textContent better for reply ??
/* Make tables more readable for chat-gpt */
const tables = questionContainer.querySelectorAll(".qtext table");
for (const table of tables) {
@@ -369,10 +369,9 @@
error,
}));
const haveError = typeof response === "object" && "error" in response;
const isAbortError = haveError && response.error.name === "AbortError";
if (config.cursor)
hiddenButton.style.cursor =
config.infinite || isAbortError ? "pointer" : "initial";
config.infinite || haveError ? "pointer" : "initial";
if (haveError) {
console.error(response.error);
return;
@@ -407,11 +406,15 @@
if (handler(config, inputList, response))
return;
}
/** In the case we can't auto complete the question */
/* In the case we can't auto complete the question */
handleClipboard(config, response);
/* Better then set once on the event because if there is an error the user can click an other time on the question */
if (!config.infinite)
hiddenButton.removeEventListener("click", injectionFunction);
});
}
let injectionFunction = null;
const pressedKeys = [];
const listeners = [];
/**
@@ -457,9 +460,9 @@
const hiddenButton = form.querySelector(".qtext");
if (config.cursor)
hiddenButton.style.cursor = "pointer";
const fn = reply.bind(null, config, hiddenButton, form, query);
listeners.push({ element: hiddenButton, fn });
hiddenButton.addEventListener("click", fn, { once: !config.infinite });
injectionFunction = reply.bind(null, config, hiddenButton, form, query);
listeners.push({ element: hiddenButton, fn: injectionFunction });
hiddenButton.addEventListener("click", injectionFunction);
}
if (config.title)
titleIndications("Injected");
File diff suppressed because one or more lines are too long
+5 -4
View File
@@ -2,6 +2,7 @@ import Config from "../types/config";
import titleIndications from "../utils/title-indications";
import reply from "./reply";
let injectionFunction: (this: HTMLElement, ev: MouseEvent) => any = null;
const pressedKeys: string[] = [];
const listeners: {
element: HTMLElement;
@@ -52,12 +53,12 @@ function setUpMoodleGpt(config: Config) {
if (config.cursor) hiddenButton.style.cursor = "pointer";
const fn = reply.bind(null, config, hiddenButton, form, query);
listeners.push({ element: hiddenButton, fn });
hiddenButton.addEventListener("click", fn, { once: !config.infinite });
injectionFunction = reply.bind(null, config, hiddenButton, form, query);
listeners.push({ element: hiddenButton, fn: injectionFunction });
hiddenButton.addEventListener("click", injectionFunction);
}
if (config.title) titleIndications("Injected");
}
export default codeListener;
export { injectionFunction, codeListener };
+1 -1
View File
@@ -12,7 +12,7 @@ async function getChatGPTResponse(
question: string
): Promise<string> {
const controller = new AbortController();
const timeoutControler = setTimeout(() => controller.abort(), 10000);
const timeoutControler = setTimeout(() => controller.abort(), 15000);
const req = await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
+8 -7
View File
@@ -8,6 +8,7 @@ import handleTextbox from "./questions/textbox";
import handleClipboard from "./questions/clipboard";
import handleNumber from "./questions/number";
import handleContentEditable from "./questions/contenteditable";
import { injectionFunction } from "./code-listener";
/**
* Reply to the question
@@ -33,18 +34,14 @@ async function reply(
error,
})
);
const haveError = typeof response === "object" && "error" in response;
const isAbortError = haveError && response.error.name === "AbortError";
if (config.cursor)
hiddenButton.style.cursor =
config.infinite || isAbortError ? "pointer" : "initial";
config.infinite || haveError ? "pointer" : "initial";
if (haveError) {
if (isAbortError) {
//TODO: We need to inject back the event
}
console.error(response.error);
return;
}
@@ -83,8 +80,12 @@ async function reply(
if (handler(config, inputList, response)) return;
}
/** In the case we can't auto complete the question */
/* In the case we can't auto complete the question */
handleClipboard(config, response);
/* Better then set once on the event because if there is an error the user can click an other time on the question */
if (!config.infinite)
hiddenButton.removeEventListener("click", injectionFunction);
}
export default reply;
+1 -1
View File
@@ -1,4 +1,4 @@
import codeListener from "./core/code-listener";
import { codeListener } from "./core/code-listener";
chrome.storage.sync.get(["moodleGPT"]).then(function (storage) {
const config = storage.moodleGPT;