Spaces:
Paused
Paused
File size: 1,867 Bytes
a5784e9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | import json
from typing import List, Optional
from models import Message
def extract_json_from_text(text: str) -> Optional[str]:
"""Attempt to extract the first JSON object string from plain text."""
if not text:
return None
# Simple heuristic: find the first '{' and the last matching '}'
try:
start = text.find("{")
end = text.rfind("}")
if start != -1 and end != -1 and end > start:
candidate = text[start : end + 1].strip()
json.loads(candidate)
return candidate
except Exception:
return None
return None
def get_latest_user_text(messages: List[Message]) -> str:
"""Extract the text content of the most recent user message (concatenating multiple text segments)."""
for msg in reversed(messages):
if msg.role == "user":
content = msg.content
if isinstance(content, str):
return content
elif isinstance(content, list):
parts: List[str] = []
for it in content:
if isinstance(it, dict):
if it.get("type") == "text":
text_raw = it.get("text", "")
if isinstance(text_raw, str):
parts.append(text_raw)
else:
parts.append(str(text_raw))
elif hasattr(it, "type") and it.type == "text":
text_attr = getattr(it, "text", "")
if isinstance(text_attr, str):
parts.append(text_attr)
else:
parts.append(str(text_attr))
return "\n".join(p for p in parts if p)
else:
return ""
return ""
|