Add option to `compute_context()` to not scan story

This commit is contained in:
Gnome Ann 2022-01-04 19:26:59 -05:00
parent 6edc6387f4
commit fbf5062074
3 changed files with 28 additions and 12 deletions

View File

@ -1152,8 +1152,10 @@ def lua_encode(string):
# Computes context given a submission, Lua array of entry UIDs and a Lua array
# of folder UIDs
#==================================================================#
def lua_compute_context(submission, entries, folders):
def lua_compute_context(submission, entries, folders, kwargs):
assert type(submission) is str
if(kwargs is None):
kwargs = vars.lua_state.table()
actions = vars._actions if vars.lua_koboldbridge.userstate == "genmod" else vars.actions
allowed_entries = None
allowed_folders = None
@ -1169,8 +1171,20 @@ def lua_compute_context(submission, entries, folders):
while(folders[i] is not None):
allowed_folders.add(int(folders[i]))
i += 1
winfo, mem, anotetxt, _ = calcsubmitbudgetheader(submission, allowed_entries=allowed_entries, allowed_folders=allowed_folders, force_use_txt=True)
txt, _, _ = calcsubmitbudget(len(actions), winfo, mem, anotetxt, actions)
winfo, mem, anotetxt, _ = calcsubmitbudgetheader(
submission,
allowed_entries=allowed_entries,
allowed_folders=allowed_folders,
force_use_txt=True,
scan_story=kwargs["scan_story"] if kwargs["scan_story"] != None else True,
)
txt, _, _ = calcsubmitbudget(
len(actions),
winfo,
mem,
anotetxt,
actions,
)
return tokenizer.decode(txt)
#==================================================================#
@ -3370,7 +3384,7 @@ def deletewifolder(uid):
#==================================================================#
# Look for WI keys in text to generator
#==================================================================#
def checkworldinfo(txt, allowed_entries=None, allowed_folders=None, force_use_txt=False):
def checkworldinfo(txt, allowed_entries=None, allowed_folders=None, force_use_txt=False, scan_story=True):
original_txt = txt
# Dont go any further if WI is empty
@ -3381,7 +3395,7 @@ def checkworldinfo(txt, allowed_entries=None, allowed_folders=None, force_use_tx
ln = len(vars.actions)
# Don't bother calculating action history if widepth is 0
if(vars.widepth > 0):
if(vars.widepth > 0 and scan_story):
depth = vars.widepth
# If this is not a continue, add 1 to widepth since submitted
# text is already in action history @ -1
@ -3423,7 +3437,7 @@ def checkworldinfo(txt, allowed_entries=None, allowed_folders=None, force_use_tx
found_entries.add(id(wi))
continue
if(wi["key"] != ""):
if(len(wi["key"].strip()) > 0 and (not wi.get("selective", False) or len(wi.get("keysecondary", "").strip()) > 0)):
# Split comma-separated keys
keys = wi["key"].split(",")
keys_secondary = wi.get("keysecondary", "").split(",")

View File

@ -383,8 +383,9 @@ return function(_python, _bridged)
end
---@param submission? string
---@param kwargs? table<string, any>
---@return string
function KoboldWorldInfoEntry:compute_context(submission)
function KoboldWorldInfoEntry:compute_context(submission, kwargs)
if not check_validity(self) then
return ""
elseif submission == nil then
@ -393,7 +394,7 @@ return function(_python, _bridged)
error("`compute_context` takes a string or nil as argument #1, but got a " .. type(submission))
return ""
end
return bridged.compute_context(submission, {self.uid}, nil)
return bridged.compute_context(submission, {self.uid}, nil, kwargs)
end
---@generic K
@ -484,8 +485,9 @@ return function(_python, _bridged)
---@param submission? string
---@param entries? KoboldWorldInfoEntry|table<any, KoboldWorldInfoEntry>
---@param kwargs? table<string, any>
---@return string
function KoboldWorldInfoFolder:compute_context(submission, entries)
function KoboldWorldInfoFolder:compute_context(submission, entries, kwargs)
if not check_validity(self) then
return ""
elseif submission == nil then
@ -513,7 +515,7 @@ return function(_python, _bridged)
if self.name == "KoboldWorldInfoFolder" then
folders = {rawget(self, "_uid")}
end
return bridged.compute_context(submission, _entries, folders)
return bridged.compute_context(submission, _entries, folders, kwargs)
end
---@return boolean

View File

@ -26,7 +26,7 @@ local example_config = [[;-- Location scanner
;-- Usage instructions:
;--
;-- 1. Create a world info folder with name containing the string
;-- "<||ls||>" (without the double quotes). The comment can be anything as
;-- "<||ls||>" (without the double quotes). The name can be anything as
;-- long as it contains that inside it somewhere -- for example, you could
;-- set the name to "Locations <||ls||>".
;--
@ -124,7 +124,7 @@ function userscript.inmod()
key = e.key,
keysecondary = e.keysecondary,
}
e.constant = e.constant or (not repeated and e:compute_context("") ~= e:compute_context(location))
e.constant = e.constant or (not repeated and e:compute_context("", {scan_story=false}) ~= e:compute_context(location, {scan_story=false}))
e.key = ""
e.keysecondary = ""
end