cackerman commited on
Commit
0e308b4
·
verified ·
1 Parent(s): c04bba5

Update tom_web_app.py

Browse files
Files changed (1) hide show
  1. tom_web_app.py +23 -16
tom_web_app.py CHANGED
@@ -262,58 +262,65 @@ class Session:
262
  self.placeholder = PRESS_ENTER
263
  self.transcript += f"\n{PRESS_ENTER}\n"
264
 
265
- def get_session(resp=None) -> Tuple[str, Session, Any]:
266
  sid = request.cookies.get("sid")
 
267
  if not sid or sid not in SESSIONS:
268
  sid = uuid.uuid4().hex
269
- sess = Session()
270
- SESSIONS[sid] = sess
271
- if resp is None:
272
- resp = make_response()
273
- resp.set_cookie("sid", sid, httponly=True, samesite="Lax")
274
- else:
275
- sess = SESSIONS[sid]
276
- return sid, sess, resp
277
 
278
  @app.get("/")
279
  def index():
 
280
  resp = make_response(render_template("index.html"))
281
- _, sess, resp = get_session(resp)
 
282
  return resp
283
 
284
  @app.get("/state")
285
  def state():
286
- _, sess, _ = get_session()
287
- return jsonify({
288
  "transcript": sess.transcript,
289
  "mode": sess.mode,
290
  "primary_label": sess.primary_label,
291
  "placeholder": sess.placeholder
292
  })
 
 
 
293
 
294
  @app.post("/primary")
295
  def primary():
296
- _, sess, _ = get_session()
297
  sess.start_or_continue()
298
- return jsonify({
299
  "transcript": sess.transcript,
300
  "mode": sess.mode,
301
  "primary_label": sess.primary_label,
302
  "placeholder": sess.placeholder
303
  })
 
 
 
304
 
305
  @app.post("/action")
306
  def action():
307
- _, sess, _ = get_session()
308
  data = request.get_json(force=True) or {}
309
  text = (data.get("text") or "").strip()
310
  sess.submit_action(text)
311
- return jsonify({
312
  "transcript": sess.transcript,
313
  "mode": sess.mode,
314
  "primary_label": sess.primary_label,
315
  "placeholder": sess.placeholder
316
  })
 
 
 
317
 
318
  if __name__ == "__main__":
319
  import os
 
262
  self.placeholder = PRESS_ENTER
263
  self.transcript += f"\n{PRESS_ENTER}\n"
264
 
265
+ def get_session():
266
  sid = request.cookies.get("sid")
267
+ set_cookie = False
268
  if not sid or sid not in SESSIONS:
269
  sid = uuid.uuid4().hex
270
+ SESSIONS[sid] = Session()
271
+ set_cookie = True
272
+ return sid, SESSIONS[sid], set_cookie
 
 
 
 
 
273
 
274
  @app.get("/")
275
  def index():
276
+ sid, sess, set_cookie = get_session()
277
  resp = make_response(render_template("index.html"))
278
+ if set_cookie:
279
+ resp.set_cookie("sid", sid, httponly=True, samesite="Lax")
280
  return resp
281
 
282
  @app.get("/state")
283
  def state():
284
+ sid, sess, set_cookie = get_session()
285
+ resp = jsonify({
286
  "transcript": sess.transcript,
287
  "mode": sess.mode,
288
  "primary_label": sess.primary_label,
289
  "placeholder": sess.placeholder
290
  })
291
+ if set_cookie:
292
+ resp.set_cookie("sid", sid, httponly=True, samesite="Lax")
293
+ return resp
294
 
295
  @app.post("/primary")
296
  def primary():
297
+ sid, sess, set_cookie = get_session()
298
  sess.start_or_continue()
299
+ resp = jsonify({
300
  "transcript": sess.transcript,
301
  "mode": sess.mode,
302
  "primary_label": sess.primary_label,
303
  "placeholder": sess.placeholder
304
  })
305
+ if set_cookie:
306
+ resp.set_cookie("sid", sid, httponly=True, samesite="Lax")
307
+ return resp
308
 
309
  @app.post("/action")
310
  def action():
311
+ sid, sess, set_cookie = get_session()
312
  data = request.get_json(force=True) or {}
313
  text = (data.get("text") or "").strip()
314
  sess.submit_action(text)
315
+ resp = jsonify({
316
  "transcript": sess.transcript,
317
  "mode": sess.mode,
318
  "primary_label": sess.primary_label,
319
  "placeholder": sess.placeholder
320
  })
321
+ if set_cookie:
322
+ resp.set_cookie("sid", sid, httponly=True, samesite="Lax")
323
+ return resp
324
 
325
  if __name__ == "__main__":
326
  import os