diff --git a/src/lang/en.ts b/src/lang/en.ts
index 0126b00d..fb3081e7 100644
--- a/src/lang/en.ts
+++ b/src/lang/en.ts
@@ -118,6 +118,7 @@ export const languageEnglish = {
emotionPrompt: "This option is used to set the prompt that is used to detect emotion. if it is blank, it will use the default prompt.",
removePunctuationHypa: "If enabled, it will remove punctuation before executing HypaMemory.",
additionalParams: "Additional parameters that would be added to the request body. if you want to exclude some parameters, you can put `{{none}}` to the value. if you want to add a header instead of body, you can put `header::` in front of the key like `header::Authorization`. if you want value as json, you can put `json::` in front of the value like `json::{\"key\":\"value\"}`. otherwise, type of the value would be determined automatically.",
+ antiClaudeOverload: "If Claude overload happens, RisuAI would try to prevent it by continuing with same prompt, making it less likely to happen. works only for streamed responses. this could not work for non-official api endpoints.",
},
setup: {
chooseProvider: "Choose AI Provider",
@@ -557,4 +558,5 @@ export const languageEnglish = {
noWaitForTranslate: "No Wait for Translate",
updateRealm: "Update to RisuRealm",
updateRealmDesc: "You are trying to update your character to RisuRealm. this will update your character to RisuRealm, and you can't revert it back.",
+ antiClaudeOverload: "Anti-Claude Overload",
}
\ No newline at end of file
diff --git a/src/lib/Setting/Pages/AdvancedSettings.svelte b/src/lib/Setting/Pages/AdvancedSettings.svelte
index db70c1de..cb87d54e 100644
--- a/src/lib/Setting/Pages/AdvancedSettings.svelte
+++ b/src/lib/Setting/Pages/AdvancedSettings.svelte
@@ -80,6 +80,11 @@
+
+
+
+
+
diff --git a/src/ts/process/request.ts b/src/ts/process/request.ts
index 6e3d1dc6..d964531a 100644
--- a/src/ts/process/request.ts
+++ b/src/ts/process/request.ts
@@ -9,7 +9,7 @@ import { sleep } from "../util";
import { createDeep } from "./deepai";
import { hubURL } from "../characterCards";
import { NovelAIBadWordIds, stringlizeNAIChat } from "./models/nai";
-import { strongBan, tokenizeNum } from "../tokenizer";
+import { strongBan, tokenize, tokenizeNum } from "../tokenizer";
import { runGGUFModel } from "./models/local";
import { risuChatParser } from "../parser";
import { SignatureV4 } from "@smithy/signature-v4";
@@ -1740,13 +1740,16 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model'
result: await textifyReadableStream(res.body)
}
}
+ let rerequesting = false
+ let breakError = ''
const stream = new ReadableStream({
async start(controller){
let text = ''
+ let reader = res.body.getReader()
const decoder = new TextDecoder()
- const parser = createParser((e) => {
+ const parser = createParser(async (e) => {
if(e.type === 'event'){
switch(e.event){
case 'content_block_delta': {
@@ -1760,6 +1763,42 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model'
}
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
+ }
text += "Error:" + JSON.parse(e.data).error?.message
controller.enqueue({
"0": text
@@ -1770,10 +1809,22 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model'
}
}
})
- const reader = res.body.getReader()
while(true){
+ if(rerequesting){
+ if(breakError){
+ controller.enqueue({
+ "0": breakError
+ })
+ break
+ }
+ await sleep(1000)
+ continue
+ }
const {done, value} = await reader.read()
if(done){
+ if(rerequesting){
+ continue
+ }
break
}
parser.feed(decoder.decode(value))
diff --git a/src/ts/storage/database.ts b/src/ts/storage/database.ts
index 07ebaa01..e916cb61 100644
--- a/src/ts/storage/database.ts
+++ b/src/ts/storage/database.ts
@@ -381,6 +381,7 @@ export function setDatabase(data:Database){
data.enabledModules ??= []
data.additionalParams ??= []
data.heightMode ??= 'normal'
+ data.antiClaudeOverload ??= false
changeLanguage(data.language)
DataBase.set(data)
@@ -614,6 +615,7 @@ export interface Database{
heightMode:string
useAdvancedEditor:boolean
noWaitForTranslate:boolean
+ antiClaudeOverload:boolean
}
export interface customscript{