|  | @@ -1,5 +1,5 @@
 | 
		
	
		
			
			| 1 | 1 |  from flask import ( Flask, render_template, request, abort, redirect,
 | 
		
	
		
			
			| 2 |  | -    send_from_directory, url_for, session)
 | 
		
	
		
			
			|  | 2 | +    send_from_directory, url_for, session, flash, get_flashed_messages)
 | 
		
	
		
			
			| 3 | 3 |  from flask_session import Session
 | 
		
	
		
			
			| 4 | 4 |  import openai
 | 
		
	
		
			
			| 5 | 5 |  from dotenv import load_dotenv
 | 
		
	
	
		
			
			|  | @@ -203,14 +203,22 @@ def choices(topic):
 | 
		
	
		
			
			| 203 | 203 |  @application.route("/save", methods=['GET', 'POST'])
 | 
		
	
		
			
			| 204 | 204 |  def save():
 | 
		
	
		
			
			| 205 | 205 |      if request.method=="POST":
 | 
		
	
		
			
			| 206 |  | -        filename=request.form["filename"].rsplit("/",1)[-1]
 | 
		
	
		
			
			|  | 206 | +        filename = request.form["filename"].rsplit("/",1)[-1]
 | 
		
	
		
			
			| 207 | 207 |          if filename:
 | 
		
	
		
			
			|  | 208 | +            path = "archive/{}.json"
 | 
		
	
		
			
			|  | 209 | +            is_overwrite = os.path.isfile(path)
 | 
		
	
		
			
			| 208 | 210 |              moment = {
 | 
		
	
		
			
			| 209 | 211 |                  key: session.get(key, [])
 | 
		
	
		
			
			| 210 | 212 |                  for key in ["messages", "history"]
 | 
		
	
		
			
			| 211 | 213 |              }
 | 
		
	
		
			
			| 212 | 214 |              print(moment)
 | 
		
	
		
			
			| 213 |  | -            json.dump(moment, open("archive/{}.json".format(filename),"w"), indent=4)
 | 
		
	
		
			
			|  | 215 | +            json.dump(moment, open(path,"w"), indent=4)
 | 
		
	
		
			
			|  | 216 | +            if is_overwrite:
 | 
		
	
		
			
			|  | 217 | +                flash("Successfully saved to {}.json".format(filename))
 | 
		
	
		
			
			|  | 218 | +            else:
 | 
		
	
		
			
			|  | 219 | +                flash("Successfully overwritten {}.json".format(filename))
 | 
		
	
		
			
			|  | 220 | +        else:
 | 
		
	
		
			
			|  | 221 | +            flash("Invalid filename. Save aborted.", "error")
 | 
		
	
		
			
			| 214 | 222 |          return redirect(url_for("home"))
 | 
		
	
		
			
			| 215 | 223 |      else:
 | 
		
	
		
			
			| 216 | 224 |          return render_template("save.html",
 | 
		
	
	
		
			
			|  | @@ -218,6 +226,20 @@ def save():
 | 
		
	
		
			
			| 218 | 226 |                    "moment-%Y-%m-%d-%H.%M.%S"),
 | 
		
	
		
			
			| 219 | 227 |                  files = [os.path.basename(path).rsplit(".",1)[0] for path in glob("archive/*.json")])
 | 
		
	
		
			
			| 220 | 228 |  
 | 
		
	
		
			
			|  | 229 | +@application.route("/load", methods=['GET', 'POST'])
 | 
		
	
		
			
			|  | 230 | +def load():
 | 
		
	
		
			
			|  | 231 | +    if request.method=="POST":
 | 
		
	
		
			
			|  | 232 | +        filename = request.form["filename"]
 | 
		
	
		
			
			|  | 233 | +        try:
 | 
		
	
		
			
			|  | 234 | +            moment = json.load(open("archive/{}.json".format(filename)))
 | 
		
	
		
			
			|  | 235 | +            session["messages"] = moment["messages"]
 | 
		
	
		
			
			|  | 236 | +            session["history"] = moment["history"]
 | 
		
	
		
			
			|  | 237 | +        except Exception as e:
 | 
		
	
		
			
			|  | 238 | +            flash(repr(e))
 | 
		
	
		
			
			|  | 239 | +        return redirect(url_for("home")+"#/oldest")
 | 
		
	
		
			
			|  | 240 | +    else:
 | 
		
	
		
			
			|  | 241 | +        return render_template("load.html",
 | 
		
	
		
			
			|  | 242 | +                files = [os.path.basename(path).rsplit(".",1)[0] for path in glob("archive/*.json")])
 | 
		
	
		
			
			| 221 | 243 |  
 | 
		
	
		
			
			| 222 | 244 |  @application.get("/chat-editor")
 | 
		
	
		
			
			| 223 | 245 |  def chat_editor():
 |