j-js commited on
Commit
1f49ca6
·
verified ·
1 Parent(s): 34a1df8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -5
app.py CHANGED
@@ -1,7 +1,8 @@
1
  from __future__ import annotations
2
 
3
  import os
4
- from typing import Any, Dict
 
5
 
6
  from fastapi import FastAPI, Request
7
  from fastapi.middleware.cors import CORSMiddleware
@@ -14,7 +15,7 @@ from ui_html import HOME_HTML
14
  from utils import clamp01, get_user_text
15
 
16
 
17
- app = FastAPI(title="GMAT Solver v3", version="3.0.0")
18
 
19
  app.add_middleware(
20
  CORSMiddleware,
@@ -25,6 +26,77 @@ app.add_middleware(
25
  )
26
 
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  @app.get("/health")
29
  def health() -> Dict[str, Any]:
30
  return {"ok": True, "app": "GMAT Solver v3"}
@@ -54,18 +126,31 @@ async def chat(request: Request) -> JSONResponse:
54
  except Exception:
55
  req = ChatRequest()
56
 
57
- user_text = get_user_text(req, raw_body)
 
 
 
58
  tone = clamp01(req_data.get("tone", req.tone), 0.5)
59
  verbosity = clamp01(req_data.get("verbosity", req.verbosity), 0.5)
60
  transparency = clamp01(req_data.get("transparency", req.transparency), 0.5)
61
- help_mode = detect_help_mode(user_text, req_data.get("help_mode", req.help_mode))
 
 
 
 
62
 
63
  result = generate_response(
64
- raw_user_text=user_text,
65
  tone=tone,
66
  verbosity=verbosity,
67
  transparency=transparency,
68
  help_mode=help_mode,
 
 
 
 
 
 
69
  )
70
 
71
  return JSONResponse(
 
1
  from __future__ import annotations
2
 
3
  import os
4
+ import re
5
+ from typing import Any, Dict, Tuple
6
 
7
  from fastapi import FastAPI, Request
8
  from fastapi.middleware.cors import CORSMiddleware
 
15
  from utils import clamp01, get_user_text
16
 
17
 
18
+ app = FastAPI(title="GMAT Solver v3", version="3.1.0")
19
 
20
  app.add_middleware(
21
  CORSMiddleware,
 
26
  )
27
 
28
 
29
+ def split_unity_message(full_text: str) -> Tuple[str, str]:
30
+ """
31
+ Splits the Unity payload into:
32
+ - hidden_context
33
+ - actual_user_message
34
+
35
+ Expected Unity format:
36
+ <hidden context>
37
+
38
+ USER_MESSAGE:
39
+ <user text>
40
+
41
+ Falls back safely if marker is missing.
42
+ """
43
+ if not full_text:
44
+ return "", ""
45
+
46
+ marker = "USER_MESSAGE:"
47
+ idx = full_text.find(marker)
48
+
49
+ if idx == -1:
50
+ return "", full_text.strip()
51
+
52
+ hidden_context = full_text[:idx].strip()
53
+ actual_user_message = full_text[idx + len(marker):].strip()
54
+ return hidden_context, actual_user_message
55
+
56
+
57
+ def extract_game_context_fields(hidden_context: str) -> Dict[str, str]:
58
+ """
59
+ Pull simple fields out of the hidden Unity context so downstream logic
60
+ can use the real question/options instead of the whole raw blob.
61
+ """
62
+ fields = {
63
+ "category": "",
64
+ "difficulty": "",
65
+ "question": "",
66
+ "options": "",
67
+ }
68
+
69
+ if not hidden_context:
70
+ return fields
71
+
72
+ category_match = re.search(r"Category:\s*(.+)", hidden_context)
73
+ difficulty_match = re.search(r"Difficulty:\s*(.+)", hidden_context)
74
+ question_match = re.search(r"Question:\s*(.+)", hidden_context, re.DOTALL)
75
+ options_match = re.search(
76
+ r"Options:\s*(.+?)(?:\nPlayer balance:|\nLast outcome:|$)",
77
+ hidden_context,
78
+ re.DOTALL,
79
+ )
80
+
81
+ if category_match:
82
+ fields["category"] = category_match.group(1).strip()
83
+
84
+ if difficulty_match:
85
+ fields["difficulty"] = difficulty_match.group(1).strip()
86
+
87
+ if question_match:
88
+ question_text = question_match.group(1).strip()
89
+ question_text = question_text.split("\nOptions:")[0].strip()
90
+ question_text = question_text.split("\nPlayer balance:")[0].strip()
91
+ question_text = question_text.split("\nLast outcome:")[0].strip()
92
+ fields["question"] = question_text
93
+
94
+ if options_match:
95
+ fields["options"] = options_match.group(1).strip()
96
+
97
+ return fields
98
+
99
+
100
  @app.get("/health")
101
  def health() -> Dict[str, Any]:
102
  return {"ok": True, "app": "GMAT Solver v3"}
 
126
  except Exception:
127
  req = ChatRequest()
128
 
129
+ full_text = get_user_text(req, raw_body)
130
+ hidden_context, actual_user_message = split_unity_message(full_text)
131
+ game_fields = extract_game_context_fields(hidden_context)
132
+
133
  tone = clamp01(req_data.get("tone", req.tone), 0.5)
134
  verbosity = clamp01(req_data.get("verbosity", req.verbosity), 0.5)
135
  transparency = clamp01(req_data.get("transparency", req.transparency), 0.5)
136
+
137
+ help_mode = detect_help_mode(
138
+ actual_user_message,
139
+ req_data.get("help_mode", req.help_mode),
140
+ )
141
 
142
  result = generate_response(
143
+ raw_user_text=actual_user_message,
144
  tone=tone,
145
  verbosity=verbosity,
146
  transparency=transparency,
147
  help_mode=help_mode,
148
+ hidden_context=hidden_context,
149
+ chat_history=req_data.get("chat_history", []),
150
+ question_text=game_fields["question"],
151
+ options_text=game_fields["options"],
152
+ question_category=game_fields["category"],
153
+ question_difficulty=game_fields["difficulty"],
154
  )
155
 
156
  return JSONResponse(