Spaces:
Paused
Paused
Update chat_handler.py
Browse files- chat_handler.py +16 -18
chat_handler.py
CHANGED
|
@@ -197,31 +197,22 @@ async def chat(body: ChatRequest, x_session_id: str = Header(...)):
|
|
| 197 |
|
| 198 |
log(f"π¬ User input: {user_input}")
|
| 199 |
log(f"π Session state: {session.state}, last_intent: {session.last_intent}")
|
| 200 |
-
log(f"π
|
| 201 |
|
| 202 |
session.add_turn("user", user_input)
|
| 203 |
-
|
| 204 |
-
#
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
cfg = get_config()
|
| 208 |
-
|
| 209 |
-
# Get project config
|
| 210 |
-
project = next((p for p in cfg.projects if p.name == session.project_name), None)
|
| 211 |
-
if not project:
|
| 212 |
-
raise HTTPException(500, "Project configuration lost")
|
| 213 |
-
|
| 214 |
-
version = next((v for v in project.versions if v.published), None)
|
| 215 |
if not version:
|
| 216 |
-
raise HTTPException(500, "
|
| 217 |
|
| 218 |
# Handle based on state
|
| 219 |
if session.state == "await_param":
|
| 220 |
log(f"π Handling parameter followup for missing: {session.awaiting_parameters}")
|
| 221 |
-
answer = await _handle_parameter_followup(session, user_input
|
| 222 |
else:
|
| 223 |
log("π Handling new message")
|
| 224 |
-
answer = await _handle_new_message(session, user_input
|
| 225 |
|
| 226 |
session.add_turn("assistant", answer)
|
| 227 |
return ChatResponse(session_id=session.session_id, answer=answer)
|
|
@@ -236,8 +227,15 @@ async def chat(body: ChatRequest, x_session_id: str = Header(...)):
|
|
| 236 |
return ChatResponse(session_id=x_session_id, answer=error_msg)
|
| 237 |
|
| 238 |
# βββββββββββββββββββββββββ MESSAGE HANDLERS βββββββββββββββββββββββββ #
|
| 239 |
-
async def _handle_new_message(session: Session, user_input: str
|
| 240 |
"""Handle new message (not parameter followup)"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
# Build intent detection prompt
|
| 242 |
prompt = build_intent_prompt(
|
| 243 |
version.general_prompt,
|
|
@@ -264,7 +262,7 @@ async def _handle_new_message(session: Session, user_input: str, version) -> str
|
|
| 264 |
# Parse intent
|
| 265 |
intent_name, tail = _safe_intent_parse(raw)
|
| 266 |
|
| 267 |
-
#
|
| 268 |
valid_intents = {intent.name for intent in version.intents}
|
| 269 |
if intent_name not in valid_intents:
|
| 270 |
log(f"β οΈ Invalid intent: {intent_name} (valid: {valid_intents})")
|
|
|
|
| 197 |
|
| 198 |
log(f"π¬ User input: {user_input}")
|
| 199 |
log(f"π Session state: {session.state}, last_intent: {session.last_intent}")
|
| 200 |
+
log(f"π Session version: {session.version_number}")
|
| 201 |
|
| 202 |
session.add_turn("user", user_input)
|
| 203 |
+
|
| 204 |
+
# Get version config from session
|
| 205 |
+
version = session.get_version_config()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
if not version:
|
| 207 |
+
raise HTTPException(500, "Version configuration lost")
|
| 208 |
|
| 209 |
# Handle based on state
|
| 210 |
if session.state == "await_param":
|
| 211 |
log(f"π Handling parameter followup for missing: {session.awaiting_parameters}")
|
| 212 |
+
answer = await _handle_parameter_followup(session, user_input) # version parametresi kaldΔ±rΔ±ldΔ±
|
| 213 |
else:
|
| 214 |
log("π Handling new message")
|
| 215 |
+
answer = await _handle_new_message(session, user_input) # version parametresi kaldΔ±rΔ±ldΔ±
|
| 216 |
|
| 217 |
session.add_turn("assistant", answer)
|
| 218 |
return ChatResponse(session_id=session.session_id, answer=answer)
|
|
|
|
| 227 |
return ChatResponse(session_id=x_session_id, answer=error_msg)
|
| 228 |
|
| 229 |
# βββββββββββββββββββββββββ MESSAGE HANDLERS βββββββββββββββββββββββββ #
|
| 230 |
+
async def _handle_new_message(session: Session, user_input: str) -> str:
|
| 231 |
"""Handle new message (not parameter followup)"""
|
| 232 |
+
|
| 233 |
+
# Get version config from session
|
| 234 |
+
version = session.get_version_config()
|
| 235 |
+
if not version:
|
| 236 |
+
log("β Version config not found")
|
| 237 |
+
return "Bir hata oluΕtu. LΓΌtfen tekrar deneyin."
|
| 238 |
+
|
| 239 |
# Build intent detection prompt
|
| 240 |
prompt = build_intent_prompt(
|
| 241 |
version.general_prompt,
|
|
|
|
| 262 |
# Parse intent
|
| 263 |
intent_name, tail = _safe_intent_parse(raw)
|
| 264 |
|
| 265 |
+
# Validate intent against version's intents
|
| 266 |
valid_intents = {intent.name for intent in version.intents}
|
| 267 |
if intent_name not in valid_intents:
|
| 268 |
log(f"β οΈ Invalid intent: {intent_name} (valid: {valid_intents})")
|