Pepguy commited on
Commit
d204a95
·
verified ·
1 Parent(s): ae2ec35

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -0
app.py CHANGED
@@ -174,6 +174,29 @@ def update_user_history(uid, prompt, response):
174
  def index():
175
  return render_template_string(HTML)
176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
  @app.route("/generate", methods=["POST"])
178
  def gen():
179
  uid = request.form.get("user_id", "").strip()
 
174
  def index():
175
  return render_template_string(HTML)
176
 
177
+
178
+ app.route("/remote")
179
+ def remote_saving():
180
+ now = time.time()
181
+ for uid, data in list(user_memory.items()):
182
+ if now - data.get("last_sync", 0) >= FLUSH_INTERVAL and data["history"]:
183
+ try:
184
+ # Only sync the most recent MAX_HISTORY_TURNS entries
185
+ history_to_sync = data["history"][-MAX_HISTORY_TURNS:]
186
+ payload = {"user_id": uid, "history": history_to_sync}
187
+ print(f"🔄 Attempting to sync {uid} to {LAMBDA_URL}")
188
+ resp = requests.post(LAMBDA_URL, json=payload, timeout=5)
189
+ resp.raise_for_status()
190
+ user_memory[uid]["last_sync"] = now
191
+ app.logger.info(f"Synced memory for {uid} ({len(history_to_sync)} turns)")
192
+ print(f"✅ Successfully synced memory for {uid} ({len(history_to_sync)} turns)")
193
+ return jsonify({"done": True}), 200
194
+ except Exception as e:
195
+ app.logger.warning(f"Failed sync for {uid}: {e}")
196
+ print(f"❌ Failed sync for {uid}: {e}")
197
+ return jsonify({"error": str(e)}), 500
198
+
199
+
200
  @app.route("/generate", methods=["POST"])
201
  def gen():
202
  uid = request.form.get("user_id", "").strip()