cackerman commited on
Commit
e6334a3
·
verified ·
1 Parent(s): 91e6663

Update tom_web_app.py

Browse files
Files changed (1) hide show
  1. tom_web_app.py +20 -26
tom_web_app.py CHANGED
@@ -263,13 +263,13 @@ class Session:
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():
@@ -281,46 +281,40 @@ def index():
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
 
263
  self.transcript += f"\n{PRESS_ENTER}\n"
264
 
265
  def get_session():
266
+ sid = request.args.get("sid") or request.headers.get("X-Session-Id") or request.cookies.get("sid")
267
+ new_sid = False
268
  if not sid or sid not in SESSIONS:
269
  sid = uuid.uuid4().hex
270
+ SESSIONS[sid] = Session()
271
+ new_sid = True
272
+ return sid, SESSIONS[sid], new_sid
273
 
274
  @app.get("/")
275
  def index():
 
281
 
282
  @app.get("/state")
283
  def state():
284
+ sid, sess, _ = get_session()
285
+ return jsonify({
286
+ "sid": sid,
287
+ "transcript": sess.transcript,
288
+ "mode": sess.mode,
289
+ "primary_label": sess.primary_label,
290
+ "placeholder": sess.placeholder
291
  })
 
 
 
292
 
293
  @app.post("/primary")
294
  def primary():
295
+ sid, sess, _ = get_session()
296
  sess.start_or_continue()
297
+ return jsonify({
298
+ "sid": sid,
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
+ sid, 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
+ "sid": sid,
313
  "transcript": sess.transcript,
314
  "mode": sess.mode,
315
  "primary_label": sess.primary_label,
316
  "placeholder": sess.placeholder
317
+ })
 
 
 
318
 
319
  if __name__ == "__main__":
320
  import os