Catch Lua errors properly

This commit is contained in:
Gnome Ann
2021-12-13 02:32:09 -05:00
parent 5f06d20085
commit 82e149ee02
2 changed files with 54 additions and 9 deletions

View File

@ -89,6 +89,7 @@ class vars:
tfs = 1.0 # Default generator tfs (tail-free sampling) tfs = 1.0 # Default generator tfs (tail-free sampling)
numseqs = 1 # Number of sequences to ask the generator to create numseqs = 1 # Number of sequences to ask the generator to create
gamestarted = False # Whether the game has started (disables UI elements) gamestarted = False # Whether the game has started (disables UI elements)
serverstarted = False # Whether or not the Flask server has started
prompt = "" # Prompt prompt = "" # Prompt
memory = "" # Text submitted to memory field memory = "" # Text submitted to memory field
authornote = "" # Text submitted to Author's Note field authornote = "" # Text submitted to Author's Note field
@ -992,8 +993,14 @@ def load_lua_scripts():
vars.lua_koboldbridge.load_corescript("default.lua") vars.lua_koboldbridge.load_corescript("default.lua")
vars.lua_koboldbridge.load_userscripts(filenames, modulenames, descriptions) vars.lua_koboldbridge.load_userscripts(filenames, modulenames, descriptions)
except lupa.LuaError as e: except lupa.LuaError as e:
print(e, file=sys.stderr) vars.lua_koboldbridge.obliterate_multiverse()
exit(1) if(vars.serverstarted):
emit('from_server', {'cmd': 'errmsg', 'data': 'Lua script error, please check console.'}, broadcast=True)
print("{0}{1}{2}".format(colors.RED, "***LUA ERROR***: ", colors.END), end="", file=sys.stderr)
print("{0}{1}{2}".format(colors.RED, e, colors.END), file=sys.stderr)
print("{0}{1}{2}".format(colors.YELLOW, "Lua engine stopped; please open 'Userscripts' and press Load to reinitialize scripts.", colors.END), file=sys.stderr)
if(vars.serverstarted):
set_aibusy(0)
#==================================================================# #==================================================================#
# Print message that originates from the userscript with the given name # Print message that originates from the userscript with the given name
@ -1285,13 +1292,29 @@ def lua_is_custommodel():
#==================================================================# #==================================================================#
def execute_inmod(): def execute_inmod():
vars.lua_logname = ... vars.lua_logname = ...
try:
vars.lua_koboldbridge.execute_inmod() vars.lua_koboldbridge.execute_inmod()
except lupa.LuaError as e:
vars.lua_koboldbridge.obliterate_multiverse()
emit('from_server', {'cmd': 'errmsg', 'data': 'Lua script error, please check console.'}, broadcast=True)
print("{0}{1}{2}".format(colors.RED, "***LUA ERROR***: ", colors.END), end="", file=sys.stderr)
print("{0}{1}{2}".format(colors.RED, e, colors.END), file=sys.stderr)
print("{0}{1}{2}".format(colors.YELLOW, "Lua engine stopped; please open 'Userscripts' and press Load to reinitialize scripts.", colors.END), file=sys.stderr)
set_aibusy(0)
def execute_genmod(): def execute_genmod():
vars.lua_koboldbridge.execute_genmod() vars.lua_koboldbridge.execute_genmod()
def execute_outmod(): def execute_outmod():
try:
vars.lua_koboldbridge.execute_outmod() vars.lua_koboldbridge.execute_outmod()
except lupa.LuaError as e:
vars.lua_koboldbridge.obliterate_multiverse()
emit('from_server', {'cmd': 'errmsg', 'data': 'Lua script error, please check console.'}, broadcast=True)
print("{0}{1}{2}".format(colors.RED, "***LUA ERROR***: ", colors.END), end="", file=sys.stderr)
print("{0}{1}{2}".format(colors.RED, e, colors.END), file=sys.stderr)
print("{0}{1}{2}".format(colors.YELLOW, "Lua engine stopped; please open 'Userscripts' and press Load to reinitialize scripts.", colors.END), file=sys.stderr)
set_aibusy(0)
#==================================================================# #==================================================================#
# Lua runtime startup # Lua runtime startup
@ -1339,7 +1362,9 @@ try:
) )
except lupa.LuaError as e: except lupa.LuaError as e:
print(colors.RED + "ERROR!" + colors.END) print(colors.RED + "ERROR!" + colors.END)
print(e, file=sys.stderr) vars.lua_koboldbridge.obliterate_multiverse()
print("{0}{1}{2}".format(colors.RED, "***LUA ERROR***: ", colors.END), end="", file=sys.stderr)
print("{0}{1}{2}".format(colors.RED, e, colors.END), file=sys.stderr)
exit(1) exit(1)
print(colors.GREEN + "OK!" + colors.END) print(colors.GREEN + "OK!" + colors.END)
@ -2227,8 +2252,15 @@ def generate(txt, minimum, maximum, found_entries=None):
numseqs = 1 numseqs = 1
except Exception as e: except Exception as e:
if(issubclass(type(e), lupa.LuaError)):
vars.lua_koboldbridge.obliterate_multiverse()
emit('from_server', {'cmd': 'errmsg', 'data': 'Lua script error, please check console.'}, broadcast=True)
print("{0}{1}{2}".format(colors.RED, "***LUA ERROR***: ", colors.END), end="", file=sys.stderr)
print("{0}{1}{2}".format(colors.RED, e, colors.END), file=sys.stderr)
print("{0}{1}{2}".format(colors.YELLOW, "Lua engine stopped; please open 'Userscripts' and press Load to reinitialize scripts.", colors.END), file=sys.stderr)
else:
emit('from_server', {'cmd': 'errmsg', 'data': 'Error occured during generator call, please check console.'}, broadcast=True) emit('from_server', {'cmd': 'errmsg', 'data': 'Error occured during generator call, please check console.'}, broadcast=True)
print("{0}{1}{2}".format(colors.RED, e, colors.END)) print("{0}{1}{2}".format(colors.RED, e, colors.END), file=sys.stderr)
set_aibusy(0) set_aibusy(0)
return return
@ -2430,8 +2462,15 @@ def tpumtjgenerate(txt, minimum, maximum, found_entries=None):
) )
except Exception as e: except Exception as e:
if(issubclass(type(e), lupa.LuaError)):
vars.lua_koboldbridge.obliterate_multiverse()
emit('from_server', {'cmd': 'errmsg', 'data': 'Lua script error, please check console.'}, broadcast=True)
print("{0}{1}{2}".format(colors.RED, "***LUA ERROR***: ", colors.END), end="", file=sys.stderr)
print("{0}{1}{2}".format(colors.RED, e, colors.END), file=sys.stderr)
print("{0}{1}{2}".format(colors.YELLOW, "Lua engine stopped; please open 'Userscripts' and press Load to reinitialize scripts.", colors.END), file=sys.stderr)
else:
emit('from_server', {'cmd': 'errmsg', 'data': 'Error occured during generator call, please check console.'}, broadcast=True) emit('from_server', {'cmd': 'errmsg', 'data': 'Error occured during generator call, please check console.'}, broadcast=True)
print("{0}{1}{2}".format(colors.RED, e, colors.END)) print("{0}{1}{2}".format(colors.RED, e, colors.END), file=sys.stderr)
set_aibusy(0) set_aibusy(0)
return return
@ -3767,9 +3806,11 @@ if __name__ == "__main__":
with open('cloudflare.log', 'w') as cloudflarelog: with open('cloudflare.log', 'w') as cloudflarelog:
cloudflarelog.write("KoboldAI has finished loading and is available in the following link : " + cloudflare) cloudflarelog.write("KoboldAI has finished loading and is available in the following link : " + cloudflare)
print("\n" + format(colors.GREEN) + "KoboldAI has finished loading and is available in the following link : " + cloudflare + format(colors.END)) print("\n" + format(colors.GREEN) + "KoboldAI has finished loading and is available in the following link : " + cloudflare + format(colors.END))
vars.serverstarted = True
socketio.run(app, host='0.0.0.0', port=5000) socketio.run(app, host='0.0.0.0', port=5000)
else: else:
import webbrowser import webbrowser
webbrowser.open_new('http://localhost:5000') webbrowser.open_new('http://localhost:5000')
print("{0}\nServer started!\nYou may now connect with a browser at http://127.0.0.1:5000/{1}".format(colors.GREEN, colors.END)) print("{0}\nServer started!\nYou may now connect with a browser at http://127.0.0.1:5000/{1}".format(colors.GREEN, colors.END))
vars.serverstarted = True
socketio.run(app) socketio.run(app)

View File

@ -1556,6 +1556,10 @@ return function(_python, _bridged)
function koboldbridge.obliterate_multiverse() function koboldbridge.obliterate_multiverse()
envs = {} envs = {}
koboldbridge.num_userscripts = 0
koboldbridge.inmod = nil
koboldbridge.genmod = nil
koboldbridge.outmod = nil
end end