Files
microdao-daarion/services/agent-runtime/messaging_client.py
Apple 6bd769ef40 feat(city-map): Add 2D City Map with coordinates and agent presence
- Add migration 013_city_map_coordinates.sql with map coordinates, zones, and agents table
- Add /city/map API endpoint in city-service
- Add /city/agents and /city/agents/online endpoints
- Extend presence aggregator to include agents[] in snapshot
- Add AgentsSource for fetching agent data from DB
- Create CityMap component with interactive room tiles
- Add useCityMap hook for fetching map data
- Update useGlobalPresence to include agents
- Add map/list view toggle on /city page
- Add agent badges to room cards and map tiles
2025-11-27 07:00:47 -08:00

76 lines
2.7 KiB
Python

import httpx
import os
from models import ChannelMessage
from datetime import datetime
MESSAGING_SERVICE_URL = os.getenv("MESSAGING_SERVICE_URL", "http://messaging-service:7004")
async def get_channel_messages(channel_id: str, limit: int = 50) -> list[ChannelMessage]:
"""
Fetch recent messages from channel
Returns list of ChannelMessage objects for context
"""
try:
async with httpx.AsyncClient(timeout=10.0) as client:
response = await client.get(
f"{MESSAGING_SERVICE_URL}/api/messaging/channels/{channel_id}/messages",
params={"limit": limit}
)
response.raise_for_status()
data = response.json()
messages = []
for msg in data:
try:
messages.append(ChannelMessage(
sender_id=msg.get("sender_id", "unknown"),
sender_type=msg.get("sender_type", "human"),
content=msg.get("body", msg.get("content_preview", "")),
created_at=datetime.fromisoformat(msg.get("created_at", datetime.now().isoformat()).replace('Z', '+00:00'))
))
except Exception as e:
print(f"⚠️ Error parsing message: {e}")
continue
print(f"✅ Fetched {len(messages)} messages from channel {channel_id}")
return messages
except httpx.HTTPStatusError as e:
print(f"⚠️ HTTP error fetching messages: {e.response.status_code}")
return []
except Exception as e:
print(f"⚠️ Error fetching messages: {e}")
return []
async def post_message(agent_id: str, channel_id: str, text: str) -> bool:
"""
Post agent reply to channel
Returns True if successful, False otherwise
"""
try:
async with httpx.AsyncClient(timeout=10.0) as client:
response = await client.post(
f"{MESSAGING_SERVICE_URL}/internal/agents/{agent_id}/post-to-channel",
json={
"channel_id": channel_id,
"text": text
}
)
response.raise_for_status()
print(f"✅ Posted message to channel {channel_id}")
return True
except httpx.HTTPStatusError as e:
print(f"❌ HTTP error posting message: {e.response.status_code}")
if e.response.status_code == 404:
print(f" Endpoint not found. You may need to add it to messaging-service.")
return False
except Exception as e:
print(f"❌ Error posting message: {e}")
return False