feat: format Agent results as readable markdown list
Renders extracted items as markdown links with details instead of raw JSON. Handles common patterns: list of dicts with title/link. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -69,11 +69,47 @@ async def _poll_task(run_id: str) -> dict:
|
||||
|
||||
|
||||
def _format_extraction(data: dict) -> str:
|
||||
"""Format extracted data for Matrix message."""
|
||||
"""Format extracted data as readable markdown for Matrix."""
|
||||
extracted = data.get("extracted_information") or data.get("extracted_data")
|
||||
if not extracted:
|
||||
return "No data extracted."
|
||||
if isinstance(extracted, dict):
|
||||
|
||||
# Handle list of items (most common: news, listings, results)
|
||||
items = None
|
||||
if isinstance(extracted, list):
|
||||
items = extracted
|
||||
elif isinstance(extracted, dict):
|
||||
# Look for the first list value in the dict (e.g. {"news": [...]})
|
||||
for v in extracted.values():
|
||||
if isinstance(v, list) and v:
|
||||
items = v
|
||||
break
|
||||
|
||||
if items and isinstance(items[0], dict):
|
||||
lines = []
|
||||
for item in items:
|
||||
# Try common field names for title/link
|
||||
title = item.get("title") or item.get("name") or item.get("headline") or ""
|
||||
link = item.get("link") or item.get("url") or item.get("href") or ""
|
||||
# Build a line with remaining fields as details
|
||||
skip = {"title", "name", "headline", "link", "url", "href"}
|
||||
details = " · ".join(
|
||||
str(v) for k, v in item.items()
|
||||
if k not in skip and v
|
||||
)
|
||||
if title and link:
|
||||
line = f"- [{title}]({link})"
|
||||
elif title:
|
||||
line = f"- {title}"
|
||||
else:
|
||||
line = f"- {json.dumps(item, ensure_ascii=False)}"
|
||||
if details:
|
||||
line += f" \n {details}"
|
||||
lines.append(line)
|
||||
return "\n".join(lines)
|
||||
|
||||
# Fallback: compact JSON
|
||||
if isinstance(extracted, (dict, list)):
|
||||
return json.dumps(extracted, indent=2, ensure_ascii=False)
|
||||
return str(extracted)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user