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:
Christian Gick
2026-03-19 11:01:18 +02:00
parent b4425fc9e9
commit 6ee3e74b1d

View File

@@ -69,11 +69,47 @@ async def _poll_task(run_id: str) -> dict:
def _format_extraction(data: dict) -> str: 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") extracted = data.get("extracted_information") or data.get("extracted_data")
if not extracted: if not extracted:
return "No data 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 json.dumps(extracted, indent=2, ensure_ascii=False)
return str(extracted) return str(extracted)