@@ -986,28 +986,37 @@ def export_markdown(self, filename, chatbot):
986
986
filename += ".md"
987
987
save_file (filename , self )
988
988
989
+ def upload_chat_history (self , new_history_file_content = None ):
990
+ logging .debug (f"{ self .user_name } 加载对话历史中……" )
991
+ if new_history_file_content is not None :
992
+ if isinstance (new_history_file_content , bytes ):
993
+ try :
994
+ # Try to parse the content as JSON
995
+ json_content = json .loads (new_history_file_content .decode ('utf-8' ))
996
+
997
+ # If successful, save the content to a file
998
+ new_history_filename = new_auto_history_filename (self .user_name )
999
+ new_history_file_path = os .path .join (HISTORY_DIR , self .user_name , new_history_filename )
1000
+
1001
+ # Ensure the directory exists
1002
+ os .makedirs (os .path .dirname (new_history_file_path ), exist_ok = True )
1003
+
1004
+ # Write the content to the file
1005
+ with open (new_history_file_path , 'w' , encoding = 'utf-8' ) as f :
1006
+ json .dump (json_content , f , ensure_ascii = False , indent = 2 )
1007
+
1008
+ self .history_file_path = new_history_filename
1009
+ logging .info (f"History file uploaded and saved as { new_history_filename } " )
1010
+ except json .JSONDecodeError :
1011
+ logging .error ("Uploaded content is not valid JSON. Using default history." )
1012
+ else :
1013
+ logging .warning ("Unexpected type for new_history_file_content. Using default history." )
1014
+ return * self .load_chat_history (new_history_file_path ), init_history_list (self .user_name )
1015
+
989
1016
def load_chat_history (self , new_history_file_path = None ):
990
1017
logging .debug (f"{ self .user_name } 加载对话历史中……" )
991
1018
if new_history_file_path is not None :
992
- if type (new_history_file_path ) != str :
993
- # copy file from new_history_file_path.name to os.path.join(HISTORY_DIR, self.user_name)
994
- new_history_file_path = new_history_file_path .name
995
- target_path = os .path .join (HISTORY_DIR , self .user_name , new_history_file_path )
996
- # Check if the file is in the history directory
997
- assert os .path .realpath (new_history_file_path ).startswith (os .path .realpath (HISTORY_DIR ))
998
- assert os .path .realpath (target_path ).startswith (os .path .realpath (HISTORY_DIR ))
999
- assert self .user_name in [i [0 ] for i in auth_list ]
1000
- shutil .copyfile (
1001
- new_history_file_path ,
1002
- os .path .join (
1003
- HISTORY_DIR ,
1004
- self .user_name ,
1005
- os .path .basename (new_history_file_path ),
1006
- ),
1007
- )
1008
- self .history_file_path = os .path .basename (new_history_file_path )
1009
- else :
1010
- self .history_file_path = new_history_file_path
1019
+ self .history_file_path = new_history_file_path
1011
1020
try :
1012
1021
if self .history_file_path == os .path .basename (self .history_file_path ):
1013
1022
history_file_path = os .path .join (
0 commit comments