fix: Prevent error with loadLoreBooks() in short context length (#857)

# PR Checklist
- [ ] Have you checked if it works normally in all models? *Ignore this
if it doesn't use models.*
- [x] Have you checked if it works normally in all web, local, and node
hosted versions? If it doesn't, have you blocked it in those versions?
- [x] Have you added type definitions?

# Description

When a user uses short max context length and `reserve` is bigger than
the length, `loadLoreBooksMain` returns a `nil` but `loadLoreBooks`
tries to decode it as a JSON.

It will return a stringified empty array instead of a nil.
This commit is contained in:
kwaroran
2025-05-24 20:23:15 +09:00
committed by GitHub

View File

@@ -516,7 +516,7 @@ export async function runLua(code:string, arg:{
return JSON.stringify(found.map((b) => ({ ...b, content: risuChatParser(b.content, { chara: selectedChar }) })))
})
luaEngine.global.set('loadLoreBooksMain', async (id:string, usedContext:number) => {
luaEngine.global.set('loadLoreBooksMain', async (id:string, reserve:number) => {
if(!LuaLowLevelIds.has(id)){
return
}
@@ -530,9 +530,9 @@ export async function runLua(code:string, arg:{
}
const fullLoreBooks = (await loadLoreBookV3Prompt()).actives
const maxContext = db.maxContext - usedContext
const maxContext = db.maxContext - reserve
if (maxContext < 0) {
return
return JSON.stringify([])
}
let totalTokens = 0