From d93fd7b191719d89da8d5cdba3985c2d13dfbf81 Mon Sep 17 00:00:00 2001 From: kwaroran Date: Mon, 15 Apr 2024 23:58:57 +0900 Subject: [PATCH] Refactor loadLoreBookPrompt function in lorebook.ts --- src/ts/process/request.ts | 104 +++++++++++++++++++------------------- 1 file changed, 53 insertions(+), 51 deletions(-) diff --git a/src/ts/process/request.ts b/src/ts/process/request.ts index bc0d32e8..6288eb69 100644 --- a/src/ts/process/request.ts +++ b/src/ts/process/request.ts @@ -1796,64 +1796,66 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model' let reader = res.body.getReader() const decoder = new TextDecoder() const parser = createParser(async (e) => { - if(e.type === 'event'){ - switch(e.event){ - case 'content_block_delta': { - if(e.data){ - text += JSON.parse(e.data).delta?.text - controller.enqueue({ - "0": text - }) - } - break - } - case 'error': { - if(e.data){ - const errormsg:string = JSON.parse(e.data).error?.message - if(errormsg && errormsg.toLocaleLowerCase().includes('overload') && db.antiClaudeOverload){ - console.log('Overload detected, retrying...') - reader.cancel() - rerequesting = true - await sleep(2000) - body.max_tokens -= await tokenize(text) - if(body.max_tokens < 0){ - body.max_tokens = 0 - } - if(body.messages.at(-1)?.role !== 'assistant'){ - body.messages.push({ - role: 'assistant', - content: '' - }) - } - body.messages[body.messages.length-1].content += text - const res = await fetchNative(replacerURL, { - body: JSON.stringify(body), - headers: { - "Content-Type": "application/json", - "x-api-key": apiKey, - "anthropic-version": "2023-06-01", - "accept": "application/json", - }, - method: "POST", - chatId: arg.chatId + try { + if(e.type === 'event'){ + switch(e.event){ + case 'content_block_delta': { + if(e.data){ + text += JSON.parse(e.data).delta?.text + controller.enqueue({ + "0": text }) - if(res.status !== 200){ - breakError = 'Error: ' + await textifyReadableStream(res.body) + } + break + } + case 'error': { + if(e.data){ + const errormsg:string = JSON.parse(e.data).error?.message + if(errormsg && errormsg.toLocaleLowerCase().includes('overload') && db.antiClaudeOverload){ + console.log('Overload detected, retrying...') + reader.cancel() + rerequesting = true + await sleep(2000) + body.max_tokens -= await tokenize(text) + if(body.max_tokens < 0){ + body.max_tokens = 0 + } + if(body.messages.at(-1)?.role !== 'assistant'){ + body.messages.push({ + role: 'assistant', + content: '' + }) + } + body.messages[body.messages.length-1].content += text + const res = await fetchNative(replacerURL, { + body: JSON.stringify(body), + headers: { + "Content-Type": "application/json", + "x-api-key": apiKey, + "anthropic-version": "2023-06-01", + "accept": "application/json", + }, + method: "POST", + chatId: arg.chatId + }) + if(res.status !== 200){ + breakError = 'Error: ' + await textifyReadableStream(res.body) + break + } + reader = res.body.getReader() + rerequesting = false break } - reader = res.body.getReader() - rerequesting = false - break + text += "Error:" + JSON.parse(e.data).error?.message + controller.enqueue({ + "0": text + }) } - text += "Error:" + JSON.parse(e.data).error?.message - controller.enqueue({ - "0": text - }) + break } - break } } - } + } catch (error) {} }) while(true){ if(rerequesting){