88# editing tools, save formats, memory, world info, author's note, characters,
99# scenarios and everything Kobold and KoboldAI Lite have to offer.
1010
11+ import copy
1112import ctypes
1213import multiprocessing
1314import os
4748dry_seq_break_max = 128
4849
4950# global vars
50- KcppVersion = "1.83.1 "
51+ KcppVersion = "1.84 "
5152showdebug = True
5253kcpp_instance = None #global running instance
5354global_memory = {"tunnel_url" : "" , "restart_target" :"" , "input_to_exit" :False , "load_complete" :False }
@@ -4796,16 +4797,19 @@ def unload_libs():
47964797
47974798def reload_new_config (filename ): #for changing config after launch
47984799 with open (filename , 'r' , encoding = 'utf-8' , errors = 'ignore' ) as f :
4799- config = json .load (f )
4800- args .istemplate = False
4801- for key , value in config .items (): #do not overwrite certain values
4802- if key not in ["remotetunnel" ,"showgui" ,"port" ,"host" ,"port_param" ,"admin" ,"adminpassword" ,"admindir" ,"ssl" ,"nocertify" ,"benchmark" ,"prompt" ,"config" ]:
4803- setattr (args , key , value )
4804- setattr (args ,"showgui" ,False )
4805- setattr (args ,"benchmark" ,False )
4806- setattr (args ,"prompt" ,"" )
4807- setattr (args ,"config" ,None )
4808- setattr (args ,"launch" ,None )
4800+ try :
4801+ config = json .load (f )
4802+ args .istemplate = False
4803+ for key , value in config .items (): #do not overwrite certain values
4804+ if key not in ["remotetunnel" ,"showgui" ,"port" ,"host" ,"port_param" ,"admin" ,"adminpassword" ,"admindir" ,"ssl" ,"nocertify" ,"benchmark" ,"prompt" ,"config" ]:
4805+ setattr (args , key , value )
4806+ setattr (args ,"showgui" ,False )
4807+ setattr (args ,"benchmark" ,False )
4808+ setattr (args ,"prompt" ,"" )
4809+ setattr (args ,"config" ,None )
4810+ setattr (args ,"launch" ,None )
4811+ except Exception as e :
4812+ print (f"Reload New Config Failed: { e } " )
48094813
48104814def load_config_cli (filename ):
48114815 print ("Loading .kcpps configuration file..." )
@@ -5023,15 +5027,36 @@ def main(launch_args):
50235027 setuptunnel (global_memory , True if args .sdmodel else False )
50245028
50255029 # invoke the main koboldcpp process
5030+ original_args = copy .deepcopy (args )
5031+
50265032 kcpp_instance = multiprocessing .Process (target = kcpp_main_process ,kwargs = {"launch_args" : args , "g_memory" : global_memory , "gui_launcher" : using_gui_launcher })
50275033 kcpp_instance .daemon = True
50285034 kcpp_instance .start ()
50295035
5036+ fault_recovery_mode = False #if a config reload fails, recover back to old settings
5037+
50305038 while True : # keep the manager alive
50315039 try :
50325040 restart_target = ""
50335041 if not kcpp_instance or not kcpp_instance .is_alive ():
5034- break
5042+ if fault_recovery_mode :
5043+ #attempt to recover
5044+ print ("Attempting to recover to safe mode, launching known-good config..." )
5045+ fault_recovery_mode = False
5046+ args = copy .deepcopy (original_args ) #restore known good original launcher args
5047+ if kcpp_instance :
5048+ kcpp_instance .terminate ()
5049+ kcpp_instance .join (timeout = 10 ) # Ensure process is stopped
5050+ kcpp_instance = None
5051+ kcpp_instance = multiprocessing .Process (target = kcpp_main_process ,kwargs = {"launch_args" : args , "g_memory" : global_memory , "gui_launcher" : False })
5052+ kcpp_instance .daemon = True
5053+ kcpp_instance .start ()
5054+ global_memory ["restart_target" ] = ""
5055+ time .sleep (3 )
5056+ else :
5057+ break # kill the program
5058+ if fault_recovery_mode and global_memory ["load_complete" ]:
5059+ fault_recovery_mode = False
50355060 restart_target = global_memory ["restart_target" ]
50365061 if restart_target != "" :
50375062 print (f"Reloading new config: { restart_target } " )
@@ -5047,12 +5072,13 @@ def main(launch_args):
50475072 kcpp_instance .join (timeout = 10 ) # Ensure process is stopped
50485073 kcpp_instance = None
50495074 print ("Restarting KoboldCpp..." )
5075+ fault_recovery_mode = True
50505076 reload_new_config (targetfilepath )
5051- kcpp_instance = multiprocessing .Process (target = kcpp_main_process ,kwargs = {"launch_args" : args , "g_memory" : global_memory , "gui_launcher" : using_gui_launcher })
5077+ kcpp_instance = multiprocessing .Process (target = kcpp_main_process ,kwargs = {"launch_args" : args , "g_memory" : global_memory , "gui_launcher" : False })
50525078 kcpp_instance .daemon = True
50535079 kcpp_instance .start ()
50545080 global_memory ["restart_target" ] = ""
5055- time .sleep (1 )
5081+ time .sleep (3 )
50565082 else :
50575083 time .sleep (0.2 )
50585084 except (KeyboardInterrupt ,SystemExit ):
0 commit comments