Skip to content

Commit

Permalink
chore: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
windingwind committed Dec 25, 2023
1 parent 64af592 commit 1ce796e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 39 deletions.
72 changes: 40 additions & 32 deletions src/modules/services/gemini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,49 @@ const geminiTranslate = async function (
.replaceAll("${sourceText}", sourceText);
}

const xhr = await Zotero.HTTP.request("POST", apiURL+`?key=${data.secret}`, {
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
contents: [{
parts: [{
text: transformContent(data.langfrom, data.langto, data.raw)
}]
}]
}),
responseType: "text",
requestObserver: (xmlhttp: XMLHttpRequest) => {
let preLength = 0;
let result = "";
xmlhttp.onprogress = (e: any) => {
// Only concatenate the new strings
const newResponse = JSON.parse(e.target.response.slice(preLength));
result = newResponse.candidates[0].content.parts[0].text
// Clear timeouts caused by stream transfers
if (e.target.timeout) {
e.target.timeout = 0;
}
const xhr = await Zotero.HTTP.request(
"POST",
apiURL + `?key=${data.secret}`,
{
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
contents: [
{
parts: [
{
text: transformContent(data.langfrom, data.langto, data.raw),
},
],
},
],
}),
responseType: "text",
requestObserver: (xmlhttp: XMLHttpRequest) => {
let preLength = 0;
let result = "";
xmlhttp.onprogress = (e: any) => {
// Only concatenate the new strings
const newResponse = JSON.parse(e.target.response.slice(preLength));
result = newResponse.candidates[0].content.parts[0].text;
// Clear timeouts caused by stream transfers
if (e.target.timeout) {
e.target.timeout = 0;
}

// Remove \n\n from the beginning of the data
data.result = result.replace(/^\n\n/, "");
preLength = e.target.response.length;
// Remove \n\n from the beginning of the data
data.result = result.replace(/^\n\n/, "");
preLength = e.target.response.length;

if (data.type === "text") {
addon.hooks.onReaderPopupRefresh();
addon.hooks.onReaderTabPanelRefresh();
}
};
if (data.type === "text") {
addon.hooks.onReaderPopupRefresh();
addon.hooks.onReaderTabPanelRefresh();
}
};
},
},
});
);
if (xhr?.status !== 200) {
throw `Request error: ${xhr?.status}`;
}
Expand Down
8 changes: 2 additions & 6 deletions src/modules/settings/gemini.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { getPref, setPref } from "../../utils/prefs";
import { getString } from "../../utils/locale";

async function gptStatusCallback(
prefix: "gemini",
status: boolean,
) {
async function gptStatusCallback(prefix: "gemini", status: boolean) {
const addonPrefix = prefix.toLocaleLowerCase();
const dialog = new ztoolkit.Dialog(2, 1);
const dialogData: { [key: string | number]: any } = {
Expand Down Expand Up @@ -70,7 +67,7 @@ async function gptStatusCallback(
false,
)
.addButton(getString(`service-${addonPrefix}-dialog-save`), "save")
.addButton(getString(`service-${addonPrefix}-dialog-close`), "close")
.addButton(getString(`service-${addonPrefix}-dialog-close`), "close");

dialog.open(getString(`service-${addonPrefix}-dialog-title`));

Expand All @@ -91,4 +88,3 @@ export async function geminiStatusCallback(status: boolean) {
const prefix = "gemini";
gptStatusCallback(prefix, status);
}

2 changes: 1 addition & 1 deletion src/modules/settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const secretStatusButtonData: {
fail: "service-azuregpt-secret-fail",
},
callback: azureGPTStatusCallback,
},
},
gemini: {
labels: {
pass: "service-gemini-secret-pass",
Expand Down

0 comments on commit 1ce796e

Please sign in to comment.