Merge pull request #53 from VE-FORBRYDERNE/patch

Lua API fixes
This commit is contained in:
henk717 2022-01-01 04:45:49 +01:00 committed by GitHub
commit 7a59f133d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 83 additions and 60 deletions

View File

@ -134,7 +134,7 @@ class vars:
corescript = "default.lua" # Filename of corescript to load corescript = "default.lua" # Filename of corescript to load
# badwords = [] # Array of str/chr values that should be removed from output # badwords = [] # Array of str/chr values that should be removed from output
badwordsids = [[13460], [6880], [50256], [42496], [4613], [17414], [22039], [16410], [27], [29], [38430], [37922], [15913], [24618], [28725], [58], [47175], [36937], [26700], [12878], [16471], [37981], [5218], [29795], [13412], [45160], [3693], [49778], [4211], [20598], [36475], [33409], [44167], [32406], [29847], [29342], [42669], [685], [25787], [7359], [3784], [5320], [33994], [33490], [34516], [43734], [17635], [24293], [9959], [23785], [21737], [28401], [18161], [26358], [32509], [1279], [38155], [18189], [26894], [6927], [14610], [23834], [11037], [14631], [26933], [46904], [22330], [25915], [47934], [38214], [1875], [14692], [41832], [13163], [25970], [29565], [44926], [19841], [37250], [49029], [9609], [44438], [16791], [17816], [30109], [41888], [47527], [42924], [23984], [49074], [33717], [31161], [49082], [30138], [31175], [12240], [14804], [7131], [26076], [33250], [3556], [38381], [36338], [32756], [46581], [17912], [49146]] # Tokenized array of badwords used to prevent AI artifacting badwordsids = [[13460], [6880], [50256], [42496], [4613], [17414], [22039], [16410], [27], [29], [38430], [37922], [15913], [24618], [28725], [58], [47175], [36937], [26700], [12878], [16471], [37981], [5218], [29795], [13412], [45160], [3693], [49778], [4211], [20598], [36475], [33409], [44167], [32406], [29847], [29342], [42669], [685], [25787], [7359], [3784], [5320], [33994], [33490], [34516], [43734], [17635], [24293], [9959], [23785], [21737], [28401], [18161], [26358], [32509], [1279], [38155], [18189], [26894], [6927], [14610], [23834], [11037], [14631], [26933], [46904], [22330], [25915], [47934], [38214], [1875], [14692], [41832], [13163], [25970], [29565], [44926], [19841], [37250], [49029], [9609], [44438], [16791], [17816], [30109], [41888], [47527], [42924], [23984], [49074], [33717], [31161], [49082], [30138], [31175], [12240], [14804], [7131], [26076], [33250], [3556], [38381], [36338], [32756], [46581], [17912], [49146]] # Tokenized array of badwords used to prevent AI artifacting
deletewi = -1 # Temporary storage for index to delete deletewi = None # Temporary storage for UID to delete
wirmvwhtsp = False # Whether to remove leading whitespace from WI entries wirmvwhtsp = False # Whether to remove leading whitespace from WI entries
widepth = 3 # How many historical actions to scan for WI hits widepth = 3 # How many historical actions to scan for WI hits
mode = "play" # Whether the interface is in play, memory, or edit mode mode = "play" # Whether the interface is in play, memory, or edit mode
@ -1249,7 +1249,7 @@ def lua_get_numseqs():
def lua_set_numseqs(numseqs): def lua_set_numseqs(numseqs):
assert type(numseqs) in (int, float) and numseqs >= 1 assert type(numseqs) in (int, float) and numseqs >= 1
print(colors.GREEN + f"{lua_log_format_name(vars.lua_koboldbridge.logging_name)} set numseqs to {int(numseqs)}" + colors.END) print(colors.GREEN + f"{lua_log_format_name(vars.lua_koboldbridge.logging_name)} set numseqs to {int(numseqs)}" + colors.END)
vars.genamt = int(numseqs) vars.numseqs = int(numseqs)
#==================================================================# #==================================================================#
# Check if a setting exists with the given name # Check if a setting exists with the given name
@ -1411,7 +1411,7 @@ def lua_set_chunk(k, v):
if(vars.lua_koboldbridge.userstate == "genmod"): if(vars.lua_koboldbridge.userstate == "genmod"):
del vars._actions[chunk-1] del vars._actions[chunk-1]
vars.lua_deleted.add(chunk) vars.lua_deleted.add(chunk)
if(vars._actions is not vars.actions): if(not hasattr(vars, "_actions") or vars._actions is not vars.actions):
del vars.actions[chunk-1] del vars.actions[chunk-1]
else: else:
if(k == 0): if(k == 0):
@ -2333,6 +2333,11 @@ def calcsubmitbudget(actionlen, winfo, mem, anotetxt, actions, submission=None,
lnsp = vars.sp.shape[0] if vars.sp is not None else 0 lnsp = vars.sp.shape[0] if vars.sp is not None else 0
if("tokenizer" not in globals()):
from transformers import GPT2TokenizerFast
global tokenizer
tokenizer = GPT2TokenizerFast.from_pretrained("gpt2", cache_dir="cache/")
# Calculate token budget # Calculate token budget
prompttkns = tokenizer.encode(vars.comregex_ai.sub('', vars.prompt), max_length=int(2e9), truncation=True) prompttkns = tokenizer.encode(vars.comregex_ai.sub('', vars.prompt), max_length=int(2e9), truncation=True)
lnprompt = len(prompttkns) lnprompt = len(prompttkns)
@ -3113,6 +3118,7 @@ def inlineedit(chunk, data):
return return
vars.prompt = data vars.prompt = data
else: else:
if(chunk-1 in vars.actions):
vars.actions[chunk-1] = data vars.actions[chunk-1] = data
update_story_chunk(chunk) update_story_chunk(chunk)
@ -3132,6 +3138,7 @@ def inlinedelete(chunk):
emit('from_server', {'cmd': 'errmsg', 'data': "Cannot delete the prompt."}) emit('from_server', {'cmd': 'errmsg', 'data': "Cannot delete the prompt."})
emit('from_server', {'cmd': 'editmode', 'data': 'false'}, broadcast=True) emit('from_server', {'cmd': 'editmode', 'data': 'false'}, broadcast=True)
else: else:
if(chunk-1 in vars.actions):
del vars.actions[chunk-1] del vars.actions[chunk-1]
remove_story_chunk(chunk) remove_story_chunk(chunk)
emit('from_server', {'cmd': 'editmode', 'data': 'false'}, broadcast=True) emit('from_server', {'cmd': 'editmode', 'data': 'false'}, broadcast=True)
@ -3198,19 +3205,24 @@ def addwifolder():
addwiitem(folder_uid=uid) addwiitem(folder_uid=uid)
#==================================================================# #==================================================================#
# Move the WI entry with number src so that it immediately precedes # Move the WI entry with UID src so that it immediately precedes
# the WI entry with number dst # the WI entry with UID dst
#==================================================================# #==================================================================#
def movewiitem(dst, src): def movewiitem(dst, src):
if(vars.worldinfo[src]["folder"] is not None): if(vars.worldinfo_u[src]["folder"] is not None):
for i, e in enumerate(vars.wifolders_u[vars.worldinfo[src]["folder"]]): for i, e in enumerate(vars.wifolders_u[vars.worldinfo_u[src]["folder"]]):
if(e is vars.worldinfo[src]): if(e is vars.worldinfo_u[src]):
vars.wifolders_u[vars.worldinfo[src]["folder"]].pop(i) vars.wifolders_u[vars.worldinfo_u[src]["folder"]].pop(i)
break break
if(vars.worldinfo[dst]["folder"] is not None): if(vars.worldinfo_u[dst]["folder"] is not None):
vars.wifolders_u[vars.worldinfo[dst]["folder"]].append(vars.worldinfo[src]) vars.wifolders_u[vars.worldinfo_u[dst]["folder"]].append(vars.worldinfo_u[src])
vars.worldinfo[src]["folder"] = vars.worldinfo[dst]["folder"] vars.worldinfo_u[src]["folder"] = vars.worldinfo_u[dst]["folder"]
vars.worldinfo.insert(dst - (dst >= src), vars.worldinfo.pop(src)) for i, e in enumerate(vars.worldinfo):
if(e is vars.worldinfo_u[src]):
_src = i
elif(e is vars.worldinfo_u[dst]):
_dst = i
vars.worldinfo.insert(_dst - (_dst >= _src), vars.worldinfo.pop(_src))
sendwi() sendwi()
#==================================================================# #==================================================================#
@ -3292,38 +3304,38 @@ def stablesortwi():
#==================================================================# #==================================================================#
def commitwi(ar): def commitwi(ar):
for ob in ar: for ob in ar:
vars.worldinfo[ob["num"]]["key"] = ob["key"] ob["uid"] = int(ob["uid"])
vars.worldinfo[ob["num"]]["keysecondary"] = ob["keysecondary"] vars.worldinfo_u[ob["uid"]]["key"] = ob["key"]
vars.worldinfo[ob["num"]]["content"] = ob["content"] vars.worldinfo_u[ob["uid"]]["keysecondary"] = ob["keysecondary"]
vars.worldinfo[ob["num"]]["comment"] = ob.get("comment", "") vars.worldinfo_u[ob["uid"]]["content"] = ob["content"]
vars.worldinfo[ob["num"]]["folder"] = ob.get("folder", None) vars.worldinfo_u[ob["uid"]]["comment"] = ob.get("comment", "")
vars.worldinfo[ob["num"]]["selective"] = ob["selective"] vars.worldinfo_u[ob["uid"]]["folder"] = ob.get("folder", None)
vars.worldinfo[ob["num"]]["constant"] = ob.get("constant", False) vars.worldinfo_u[ob["uid"]]["selective"] = ob["selective"]
# Was this a deletion request? If so, remove the requested index vars.worldinfo_u[ob["uid"]]["constant"] = ob.get("constant", False)
if(vars.deletewi >= 0):
if(vars.worldinfo[vars.deletewi]["folder"] is not None):
for i, e in enumerate(vars.wifolders_u[vars.worldinfo[vars.deletewi]["folder"]]):
if(e is vars.worldinfo[vars.deletewi]):
vars.wifolders_u[vars.worldinfo[vars.deletewi]["folder"]].pop(i)
del vars.worldinfo_u[vars.worldinfo[vars.deletewi]["uid"]]
del vars.worldinfo[vars.deletewi]
# Send the new WI array structure
sendwi()
# And reset deletewi index
vars.deletewi = -1
else:
stablesortwi() stablesortwi()
vars.worldinfo_i = [wi for wi in vars.worldinfo if wi["init"]] vars.worldinfo_i = [wi for wi in vars.worldinfo if wi["init"]]
#==================================================================# #==================================================================#
# #
#==================================================================# #==================================================================#
def deletewi(num): def deletewi(uid):
if(num < len(vars.worldinfo)): if(uid in vars.worldinfo_u):
# Store index of deletion request # Store UID of deletion request
vars.deletewi = num vars.deletewi = uid
# Get contents of WI HTML inputs if(vars.deletewi is not None):
requestwi() if(vars.worldinfo_u[vars.deletewi]["folder"] is not None):
for i, e in enumerate(vars.wifolders_u[vars.worldinfo_u[vars.deletewi]["folder"]]):
if(e is vars.worldinfo_u[vars.deletewi]):
vars.wifolders_u[vars.worldinfo_u[vars.deletewi]["folder"]].pop(i)
for i, e in enumerate(vars.worldinfo):
if(e is vars.worldinfo_u[vars.deletewi]):
del vars.worldinfo[i]
break
del vars.worldinfo_u[vars.deletewi]
# Send the new WI array structure
sendwi()
# And reset deletewi
vars.deletewi = None
#==================================================================# #==================================================================#
# #

View File

@ -279,6 +279,7 @@ return function(_python, _bridged)
end end
end end
debug.getmetatable(io.stdout).__index.close = _new_close(io.stdout.close) debug.getmetatable(io.stdout).__index.close = _new_close(io.stdout.close)
debug.getmetatable(io.stdout).__close = _new_close(io.stdout.close)
---@param filename string ---@param filename string
---@return boolean ---@return boolean
@ -1859,8 +1860,13 @@ return function(_python, _bridged)
bridged.load_callback(filename, modulenames[i]) bridged.load_callback(filename, modulenames[i])
koboldbridge.logging_name = modulenames[i] koboldbridge.logging_name = modulenames[i]
koboldbridge.filename = filename koboldbridge.filename = filename
local f, err = old_loadfile(join_folder_and_filename(bridged.userscript_path, filename), "t", koboldbridge.get_universe(filename))
if err ~= nil then
error(err)
return
end
---@type KoboldUserScript ---@type KoboldUserScript
local _userscript = old_loadfile(join_folder_and_filename(bridged.userscript_path, filename), "t", koboldbridge.get_universe(filename))() local _userscript = f()
koboldbridge.logging_name = nil koboldbridge.logging_name = nil
koboldbridge.filename = nil koboldbridge.filename = nil
local userscript = deepcopy(KoboldUserScriptModule) local userscript = deepcopy(KoboldUserScriptModule)
@ -1899,8 +1905,13 @@ return function(_python, _bridged)
---@return nil ---@return nil
function koboldbridge.load_corescript(filename) function koboldbridge.load_corescript(filename)
local f, err = old_loadfile(join_folder_and_filename(bridged.corescript_path, filename), "t", koboldbridge.get_universe(0))
if err ~= nil then
error(err)
return
end
---@type KoboldCoreScript ---@type KoboldCoreScript
local corescript = old_loadfile(join_folder_and_filename(bridged.corescript_path, filename), "t", koboldbridge.get_universe(0))() local corescript = f()
koboldbridge.inmod = corescript.inmod koboldbridge.inmod = corescript.inmod
koboldbridge.genmod = corescript.genmod koboldbridge.genmod = corescript.genmod
koboldbridge.outmod = corescript.outmod koboldbridge.outmod = corescript.outmod
@ -1915,6 +1926,9 @@ return function(_python, _bridged)
koboldbridge.generating = true koboldbridge.generating = true
koboldbridge.generated_cols = 0 koboldbridge.generated_cols = 0
koboldbridge.generated = {} koboldbridge.generated = {}
if koboldbridge.inmod ~= nil then
r = koboldbridge.inmod()
end
for i = 1, kobold.settings.numseqs do for i = 1, kobold.settings.numseqs do
koboldbridge.generated[i] = {} koboldbridge.generated[i] = {}
end end
@ -1922,9 +1936,6 @@ return function(_python, _bridged)
for i = 1, kobold.num_outputs do for i = 1, kobold.num_outputs do
koboldbridge.outputs[i] = {} koboldbridge.outputs[i] = {}
end end
if koboldbridge.inmod ~= nil then
r = koboldbridge.inmod()
end
return r return r
end end

View File

@ -224,7 +224,7 @@ function addWiLine(ob) {
var current_wifolder_element = ob.folder === null ? $(".wisortable-body:not([folder-uid])").last() : $(".wisortable-body[folder-uid="+ob.folder+"]"); var current_wifolder_element = ob.folder === null ? $(".wisortable-body:not([folder-uid])").last() : $(".wisortable-body[folder-uid="+ob.folder+"]");
if(ob.init) { if(ob.init) {
if(ob.selective){ if(ob.selective){
current_wifolder_element.append("<div class=\"wilistitem wilistitem-selective "+(ob.constant ? "wilistitem-constant" : "")+"\" num=\""+ob.num+"\" id=\"wilistitem"+ob.num+"\">\ current_wifolder_element.append("<div class=\"wilistitem wilistitem-selective "+(ob.constant ? "wilistitem-constant" : "")+"\" num=\""+ob.num+"\" uid=\""+ob.uid+"\" id=\"wilistitem"+ob.num+"\">\
<div class=\"wicomment\">\ <div class=\"wicomment\">\
<textarea class=\"form-control\" placeholder=\"Comment\" id=\"wicomment"+ob.num+"\">"+ob.comment+"</textarea>\ <textarea class=\"form-control\" placeholder=\"Comment\" id=\"wicomment"+ob.num+"\">"+ob.comment+"</textarea>\
</div>\ </div>\
@ -262,7 +262,7 @@ function addWiLine(ob) {
</div>\ </div>\
</div>"); </div>");
} else { } else {
current_wifolder_element.append("<div class=\"wilistitem "+(ob.constant ? "wilistitem-constant" : "")+"\" num=\""+ob.num+"\" id=\"wilistitem"+ob.num+"\">\ current_wifolder_element.append("<div class=\"wilistitem "+(ob.constant ? "wilistitem-constant" : "")+"\" num=\""+ob.num+"\" uid=\""+ob.uid+"\" id=\"wilistitem"+ob.num+"\">\
<div class=\"wicomment\">\ <div class=\"wicomment\">\
<textarea class=\"form-control\" placeholder=\"Comment\" id=\"wicomment"+ob.num+"\">"+ob.comment+"</textarea>\ <textarea class=\"form-control\" placeholder=\"Comment\" id=\"wicomment"+ob.num+"\">"+ob.comment+"</textarea>\
</div>\ </div>\
@ -311,7 +311,7 @@ function addWiLine(ob) {
}); });
} else { } else {
// Show WI line item with form fields hidden (uninitialized) // Show WI line item with form fields hidden (uninitialized)
current_wifolder_element.append("<div class=\"wilistitem wilistitem-uninitialized wisortable-excluded\" num=\""+ob.num+"\" id=\"wilistitem"+ob.num+"\">\ current_wifolder_element.append("<div class=\"wilistitem wilistitem-uninitialized wisortable-excluded\" num=\""+ob.num+"\" uid=\""+ob.uid+"\" id=\"wilistitem"+ob.num+"\">\
<div class=\"wicomment\">\ <div class=\"wicomment\">\
<textarea class=\"form-control hidden\" placeholder=\"Comment\" id=\"wicomment"+ob.num+"\">"+ob.comment+"</textarea>\ <textarea class=\"form-control hidden\" placeholder=\"Comment\" id=\"wicomment"+ob.num+"\">"+ob.comment+"</textarea>\
</div>\ </div>\
@ -391,7 +391,7 @@ function addWiLine(ob) {
hideWiDeleteConfirm(ob.num); hideWiDeleteConfirm(ob.num);
}); });
$("#btn_widel"+ob.num).on("click", function () { $("#btn_widel"+ob.num).on("click", function () {
socket.send({'cmd': 'widelete', 'data': ob.num}); socket.send({'cmd': 'widelete', 'data': ob.uid});
}); });
$("#selective-key-"+ob.num).on("click", function () { $("#selective-key-"+ob.num).on("click", function () {
var element = $("#selective-key-"+ob.num); var element = $("#selective-key-"+ob.num);
@ -773,7 +773,7 @@ function returnWiList(ar) {
} else { } else {
folder = parseInt(folder); folder = parseInt(folder);
} }
var ob = {"key": "", "keysecondary": "", "content": "", "comment": "", "folder": null, "num": ar[i], "selective": false, "constant": false}; var ob = {"key": "", "keysecondary": "", "content": "", "comment": "", "folder": null, "uid": parseInt($("#wilistitem"+ar[i]).attr("uid")), "selective": false, "constant": false};
ob.selective = $("#wikeyprimary"+ar[i]).css("display") != "none" ob.selective = $("#wikeyprimary"+ar[i]).css("display") != "none"
ob.key = ob.selective ? $("#wikeyprimary"+ar[i]).val() : $("#wikey"+ar[i]).val(); ob.key = ob.selective ? $("#wikeyprimary"+ar[i]).val() : $("#wikey"+ar[i]).val();
ob.keysecondary = $("#wikeysecondary"+ar[i]).val(); ob.keysecondary = $("#wikeysecondary"+ar[i]).val();
@ -1205,16 +1205,16 @@ function sortableOnStop(event, ui) {
// entry was dropped and which WI entry comes immediately after the // entry was dropped and which WI entry comes immediately after the
// dropped position so that the server can internally move around // dropped position so that the server can internally move around
// the WI entries // the WI entries
var next_sibling = ui.item.next(".wilistitem").attr("num"); var next_sibling = ui.item.next(".wilistitem").attr("uid");
if(next_sibling === undefined) { if(next_sibling === undefined) {
next_sibling = ui.item.next().next().attr("num"); next_sibling = ui.item.next().next().attr("uid");
} }
next_sibling = parseInt(next_sibling); next_sibling = parseInt(next_sibling);
if(Number.isNaN(next_sibling)) { if(Number.isNaN(next_sibling)) {
$(this).sortable("cancel"); $(this).sortable("cancel");
return; return;
} }
socket.send({'cmd': 'wimoveitem', 'destination': next_sibling, 'data': parseInt(ui.item.attr("num"))}); socket.send({'cmd': 'wimoveitem', 'destination': next_sibling, 'data': parseInt(ui.item.attr("uid"))});
} else { } else {
// Do the same thing for WI folders // Do the same thing for WI folders
var next_sibling = ui.item.next(".wisortable-container").attr("folder-uid"); var next_sibling = ui.item.next(".wisortable-container").attr("folder-uid");

View File

@ -17,7 +17,7 @@
<script src="static/bootstrap.min.js"></script> <script src="static/bootstrap.min.js"></script>
<script src="static/bootstrap-toggle.min.js"></script> <script src="static/bootstrap-toggle.min.js"></script>
<script src="static/rangy-core.min.js"></script> <script src="static/rangy-core.min.js"></script>
<script src="static/application.js?ver=1.16.4t"></script> <script src="static/application.js?ver=1.16.4u"></script>
</head> </head>
<body> <body>
<input type="file" id="remote-save-select" accept="application/json" style="display:none"> <input type="file" id="remote-save-select" accept="application/json" style="display:none">