This commit is contained in:
恍兮惚兮 2024-07-24 16:34:54 +08:00
parent 33e715a1d0
commit 6baf3bad5e
3 changed files with 22 additions and 31 deletions

View File

@ -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;
};

View File

@ -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

View File

@ -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"
}
}
},