diff --git a/src/ts/process/request.ts b/src/ts/process/request.ts index e61cb9d2..438561a0 100644 --- a/src/ts/process/request.ts +++ b/src/ts/process/request.ts @@ -3,7 +3,7 @@ import type { OpenAIChat } from "."; import { DataBase, setDatabase, type character } from "../database"; import { pluginProcess } from "./plugins"; import { language } from "../../lang"; -import { stringlizeChat } from "./stringlize"; +import { stringlizeChat, unstringlizeChat } from "./stringlize"; import { globalFetch, isTauri } from "../globalApi"; interface requestDataArgument{ @@ -215,7 +215,7 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model' } return { type: "success", - result: da.data.output + result: unstringlizeChat(da.data.output, formated, currentChar?.name ?? '') } } @@ -291,15 +291,9 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model' try { let result:string = isNewAPI ? dat.results[0].text : dat.data[0].substring(proompt.length) - for(const stopStr of stopStrings){ - if(result.endsWith(stopStr)){ - result.substring(0,result.length - stopStr.length) - } - } - return { type: 'success', - result: result + result: unstringlizeChat(result, formated, currentChar?.name ?? '') } } catch (error) { return { diff --git a/src/ts/process/stringlize.ts b/src/ts/process/stringlize.ts index 731178ed..c11c9386 100644 --- a/src/ts/process/stringlize.ts +++ b/src/ts/process/stringlize.ts @@ -10,9 +10,45 @@ export function stringlizeChat(formated:OpenAIChat[], char:string = ''){ if(form.role === 'system'){ resultString.push("system note: " + form.content) } - else{ + else if(form.name){ resultString.push(form.name + ": " + form.content) } + else{ + resultString.push(form.content) + } } return resultString.join('\n\n') + `\n\n${char}:` +} + +export function unstringlizeChat(text:string, formated:OpenAIChat[], char:string = ''){ + let minIndex = -1 + let chunks:string[] = ["system note:"] + if(char){ + chunks.push(`${char}:`) + } + + for(const form of formated){ + if(form.name){ + const chunk = `${form.name}:` + if(!chunks.includes(chunk)){ + chunks.push(chunk) + } + } + } + + for(const chunk of chunks){ + const ind = text.indexOf(chunk) + if(ind === -1){ + continue + } + if(minIndex === -1 || minIndex > ind){ + minIndex = ind + } + } + + if(minIndex !== -1){ + text.substring(0, minIndex) + } + + return text } \ No newline at end of file