From 6baf3bad5e7be72f1aa06b54d1995e60fffdb32c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <101191390+HIllya51@users.noreply.github.com> Date: Wed, 24 Jul 2024 16:34:54 +0800 Subject: [PATCH] div --- .../{hookchatgptfetch.js => dev_chatgpt.js} | 29 +++---------------- .../LunaTranslator/translator/dev_chatgpt.py | 11 +++---- .../defaultconfig/translatorsetting.json | 13 ++++++++- 3 files changed, 22 insertions(+), 31 deletions(-) rename LunaTranslator/LunaTranslator/translator/{hookchatgptfetch.js => dev_chatgpt.js} (63%) diff --git a/LunaTranslator/LunaTranslator/translator/hookchatgptfetch.js b/LunaTranslator/LunaTranslator/translator/dev_chatgpt.js similarity index 63% rename from LunaTranslator/LunaTranslator/translator/hookchatgptfetch.js rename to LunaTranslator/LunaTranslator/translator/dev_chatgpt.js index 2c0e5454..79e8b067 100644 --- a/LunaTranslator/LunaTranslator/translator/hookchatgptfetch.js +++ b/LunaTranslator/LunaTranslator/translator/dev_chatgpt.js @@ -7,24 +7,11 @@ window.fetch = function (input, init) { if (!input.includes("conversation")) return fetchPromise; hasdone = false; thistext = '' - // 构造一个自定义的可读流 - //controller.close()会导致真正的fetch被终止 - // 不太会js,只能让他假等待了,ui里面会显示在等待,但luna里面能读到,而且按钮也能正确按下 - const customReadableStream = new ReadableStream({ - start(controller) { - setTimeout(() => { - - controller.close(); - }, 99999999999); - - }, - }); fetchPromise.then(response => { - const contentType = response.headers.get('content-type'); - + const clone = response.clone() + const contentType = clone.headers.get('content-type'); if (contentType && contentType.includes('text/event-stream')) { - console.log(response.body) - const reader = response.body.getReader(); + const reader = clone.body.getReader(); reader.read().then(function processStream({ done, value }) { @@ -55,13 +42,5 @@ window.fetch = function (input, init) { console.error('Fetch error:', error); }); - return new Promise((resolve, reject) => { - resolve(new Response(customReadableStream, { - status: 200, - headers: { - 'Content-type': 'text/event-stream' - } - })); - - }); + return fetchPromise; }; diff --git a/LunaTranslator/LunaTranslator/translator/dev_chatgpt.py b/LunaTranslator/LunaTranslator/translator/dev_chatgpt.py index 9a8c598e..13cc5bc8 100644 --- a/LunaTranslator/LunaTranslator/translator/dev_chatgpt.py +++ b/LunaTranslator/LunaTranslator/translator/dev_chatgpt.py @@ -28,7 +28,7 @@ class TS(basetransdev): if self.Runtime_evaluate("window.injectedjs")["result"]["type"] != "undefined": return with open( - os.path.join(os.path.dirname(__file__), "hookchatgptfetch.js"), + os.path.join(os.path.dirname(__file__), "dev_chatgpt.js"), "r", encoding="utf8", ) as ff: @@ -41,12 +41,13 @@ class TS(basetransdev): self.Runtime_evaluate("hasdone=false") self.Runtime_evaluate('thistext=""') - content = ( - "You are a translator. Please help me translate the following {} text into {}, and you should only tell me the translation.\n".format( + if self.config["use_custom_prompt"]: + prompt = self.config["custom_prompt"] + else: + prompt = "You are a translator. Please help me translate the following {} text into {}, and you should only tell me the translation.\n".format( self.srclang, self.tgtlang ) - + content - ) + content = prompt + content self.Runtime_evaluate( 'textarea=document.querySelector("#prompt-textarea");textarea.value="";event = new Event("input", {{bubbles: true, cancelable: true }});textarea.dispatchEvent(event);textarea=document.querySelector("textarea");textarea.value=`{}`;event = new Event("input", {{bubbles: true, cancelable: true }});textarea.dispatchEvent(event);'.format( content diff --git a/LunaTranslator/files/defaultconfig/translatorsetting.json b/LunaTranslator/files/defaultconfig/translatorsetting.json index 85070ea9..ddaf27d6 100644 --- a/LunaTranslator/files/defaultconfig/translatorsetting.json +++ b/LunaTranslator/files/defaultconfig/translatorsetting.json @@ -734,12 +734,23 @@ }, "dev_chatgpt": { "args": { - "usingstream": true + "usingstream": true, + "use_custom_prompt": false, + "custom_prompt": "" }, "argstype": { "usingstream": { "name": "流式输出", "type": "switch" + }, + "custom_prompt": { + "name": "自定义promt", + "type": "multiline", + "refswitch": "use_custom_prompt" + }, + "use_custom_prompt": { + "type": "switch_ref", + "name": "使用自定义promt" } } },