CatoG commited on
Commit
76680b1
·
1 Parent(s): eef02d9
Files changed (1) hide show
  1. app.py +7 -6
app.py CHANGED
@@ -4,7 +4,7 @@ import uuid
4
  import random
5
  import warnings
6
  import traceback
7
- import threading
8
  from datetime import datetime, timezone
9
  from typing import Dict, List, Optional, Tuple
10
 
@@ -105,8 +105,8 @@ LLM_CACHE: Dict[str, object] = {}
105
  AGENT_CACHE: Dict[Tuple[str, Tuple[str, ...]], object] = {}
106
  RUNTIME_HEALTH: Dict[str, str] = {}
107
 
108
- # Thread-local storage so each Gradio request can carry the real client IP
109
- _request_context = threading.local()
110
 
111
 
112
  # ============================================================
@@ -408,7 +408,7 @@ def generate_uuid(_: str = "") -> str:
408
  @tool
409
  def get_user_location(_: str = "") -> str:
410
  """Determine the user's precise physical location using browser GPS/WiFi coordinates or IP fallback."""
411
- location_data = getattr(_request_context, "client_ip", "") or ""
412
 
413
  # Precise coordinates from browser geolocation API
414
  if location_data and not location_data.startswith("ip:"):
@@ -579,6 +579,7 @@ def build_debug_report(
579
  lines.append(f"model_id: {model_id}")
580
  lines.append(f"user_message: {message}")
581
  lines.append(f"selected_tools: {selected_tools}")
 
582
  lines.append(f"message_count: {len(messages)}")
583
  lines.append(f"chart_path: {chart_path}")
584
  lines.append("")
@@ -629,8 +630,8 @@ def build_debug_report(
629
  def run_agent(message, history, selected_tools, model_id, client_ip: str = ""):
630
  history = history or []
631
 
632
- # Store location data in thread-local so get_user_location can read it
633
- _request_context.client_ip = client_ip.strip() if client_ip else ""
634
 
635
  if not message or not str(message).strip():
636
  return history, "No input provided.", "", None, model_status_text(model_id), "No input provided."
 
4
  import random
5
  import warnings
6
  import traceback
7
+ from contextvars import ContextVar
8
  from datetime import datetime, timezone
9
  from typing import Dict, List, Optional, Tuple
10
 
 
105
  AGENT_CACHE: Dict[Tuple[str, Tuple[str, ...]], object] = {}
106
  RUNTIME_HEALTH: Dict[str, str] = {}
107
 
108
+ # ContextVar propagates into LangChain worker threads automatically (unlike threading.local)
109
+ _client_location: ContextVar[str] = ContextVar("client_location", default="")
110
 
111
 
112
  # ============================================================
 
408
  @tool
409
  def get_user_location(_: str = "") -> str:
410
  """Determine the user's precise physical location using browser GPS/WiFi coordinates or IP fallback."""
411
+ location_data = _client_location.get()
412
 
413
  # Precise coordinates from browser geolocation API
414
  if location_data and not location_data.startswith("ip:"):
 
579
  lines.append(f"model_id: {model_id}")
580
  lines.append(f"user_message: {message}")
581
  lines.append(f"selected_tools: {selected_tools}")
582
+ lines.append(f"client_location_value: {repr(_client_location.get())}")
583
  lines.append(f"message_count: {len(messages)}")
584
  lines.append(f"chart_path: {chart_path}")
585
  lines.append("")
 
630
  def run_agent(message, history, selected_tools, model_id, client_ip: str = ""):
631
  history = history or []
632
 
633
+ # Store location data via ContextVar so LangChain worker threads can read it
634
+ _client_location.set(client_ip.strip() if client_ip else "")
635
 
636
  if not message or not str(message).strip():
637
  return history, "No input provided.", "", None, model_status_text(model_id), "No input provided."