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; if (!input.includes("conversation")) return fetchPromise;
hasdone = false; hasdone = false;
thistext = '' thistext = ''
// 构造一个自定义的可读流
//controller.close()会导致真正的fetch被终止
// 不太会js只能让他假等待了ui里面会显示在等待但luna里面能读到而且按钮也能正确按下
const customReadableStream = new ReadableStream({
start(controller) {
setTimeout(() => {
controller.close();
}, 99999999999);
},
});
fetchPromise.then(response => { 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')) { if (contentType && contentType.includes('text/event-stream')) {
console.log(response.body) const reader = clone.body.getReader();
const reader = response.body.getReader();
reader.read().then(function processStream({ done, value }) { reader.read().then(function processStream({ done, value }) {
@ -55,13 +42,5 @@ window.fetch = function (input, init) {
console.error('Fetch error:', error); console.error('Fetch error:', error);
}); });
return new Promise((resolve, reject) => { return fetchPromise;
resolve(new Response(customReadableStream, {
status: 200,
headers: {
'Content-type': 'text/event-stream'
}
}));
});
}; };

View File

@ -28,7 +28,7 @@ class TS(basetransdev):
if self.Runtime_evaluate("window.injectedjs")["result"]["type"] != "undefined": if self.Runtime_evaluate("window.injectedjs")["result"]["type"] != "undefined":
return return
with open( with open(
os.path.join(os.path.dirname(__file__), "hookchatgptfetch.js"), os.path.join(os.path.dirname(__file__), "dev_chatgpt.js"),
"r", "r",
encoding="utf8", encoding="utf8",
) as ff: ) as ff:
@ -41,12 +41,13 @@ class TS(basetransdev):
self.Runtime_evaluate("hasdone=false") self.Runtime_evaluate("hasdone=false")
self.Runtime_evaluate('thistext=""') self.Runtime_evaluate('thistext=""')
content = ( if self.config["use_custom_prompt"]:
"You are a translator. Please help me translate the following {} text into {}, and you should only tell me the translation.\n".format( 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 self.srclang, self.tgtlang
) )
+ content content = prompt + content
)
self.Runtime_evaluate( 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( '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 content

View File

@ -734,12 +734,23 @@
}, },
"dev_chatgpt": { "dev_chatgpt": {
"args": { "args": {
"usingstream": true "usingstream": true,
"use_custom_prompt": false,
"custom_prompt": ""
}, },
"argstype": { "argstype": {
"usingstream": { "usingstream": {
"name": "流式输出", "name": "流式输出",
"type": "switch" "type": "switch"
},
"custom_prompt": {
"name": "自定义promt",
"type": "multiline",
"refswitch": "use_custom_prompt"
},
"use_custom_prompt": {
"type": "switch_ref",
"name": "使用自定义promt"
} }
} }
}, },