add Langfuse tracing to agent runs and tool calls
This commit is contained in:
47
api/main.py
47
api/main.py
@@ -23,6 +23,23 @@ logging.basicConfig(
|
||||
)
|
||||
|
||||
|
||||
def _get_langfuse():
|
||||
"""Lazy Langfuse client — returns None if not configured."""
|
||||
try:
|
||||
from api.config import get_settings
|
||||
s = get_settings()
|
||||
if not s.langfuse_public_key or not s.langfuse_secret_key:
|
||||
return None
|
||||
from langfuse import Langfuse
|
||||
return Langfuse(
|
||||
public_key=s.langfuse_public_key,
|
||||
secret_key=s.langfuse_secret_key,
|
||||
host=s.langfuse_host,
|
||||
)
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
# ── WebSocket event hub ──
|
||||
|
||||
class EventHub:
|
||||
@@ -136,14 +153,27 @@ async def trigger_fce(req: FCERequest):
|
||||
async def on_event(event):
|
||||
await event_hub.broadcast({"run_id": run_id, **event})
|
||||
|
||||
langfuse = _get_langfuse()
|
||||
trace = langfuse.trace(
|
||||
name="fce", id=run_id,
|
||||
metadata={"flight_id": req.flight_id, "scenario": scenario_manager.active_id},
|
||||
) if langfuse else None
|
||||
|
||||
try:
|
||||
async with connect_servers(["shared", "ops", "passenger"]) as mcp:
|
||||
result = await run_fce(req.flight_id, mcp, on_event=on_event)
|
||||
result = await run_fce(req.flight_id, mcp, on_event=on_event, trace=trace)
|
||||
runs[run_id] = {"status": "completed", "agent": "fce", "result": result}
|
||||
if trace:
|
||||
trace.update(output={"status": "completed", "type": result.get("type", "")})
|
||||
logger.info("agent_complete agent=fce run_id=%s flight=%s", run_id, req.flight_id)
|
||||
except Exception as e:
|
||||
runs[run_id] = {"status": "error", "agent": "fce", "error": str(e)}
|
||||
if trace:
|
||||
trace.update(output={"status": "error", "error": str(e)})
|
||||
logger.error("agent_error agent=fce run_id=%s error=%s", run_id, e)
|
||||
finally:
|
||||
if langfuse:
|
||||
langfuse.flush()
|
||||
|
||||
logger.info("agent_start agent=fce run_id=%s flight=%s", run_id, req.flight_id)
|
||||
asyncio.create_task(_run())
|
||||
@@ -169,14 +199,27 @@ async def trigger_handover(req: HandoverRequest):
|
||||
async def on_event(event):
|
||||
await event_hub.broadcast({"run_id": run_id, **event})
|
||||
|
||||
langfuse = _get_langfuse()
|
||||
trace = langfuse.trace(
|
||||
name="handover", id=run_id,
|
||||
metadata={"hubs": req.hubs, "scenario": scenario_manager.active_id},
|
||||
) if langfuse else None
|
||||
|
||||
try:
|
||||
async with connect_servers(["shared", "ops"]) as mcp:
|
||||
result = await run_handover(hubs=req.hubs, mcp=mcp, on_event=on_event)
|
||||
result = await run_handover(hubs=req.hubs, mcp=mcp, on_event=on_event, trace=trace)
|
||||
runs[run_id] = {"status": "completed", "agent": "handover", "result": result}
|
||||
if trace:
|
||||
trace.update(output={"status": "completed"})
|
||||
logger.info("agent_complete agent=handover run_id=%s hubs=%s", run_id, req.hubs)
|
||||
except Exception as e:
|
||||
runs[run_id] = {"status": "error", "agent": "handover", "error": str(e)}
|
||||
if trace:
|
||||
trace.update(output={"status": "error", "error": str(e)})
|
||||
logger.error("agent_error agent=handover run_id=%s error=%s", run_id, e)
|
||||
finally:
|
||||
if langfuse:
|
||||
langfuse.flush()
|
||||
|
||||
logger.info("agent_start agent=handover run_id=%s hubs=%s", run_id, req.hubs)
|
||||
asyncio.create_task(_run())
|
||||
|
||||
Reference in New Issue
Block a user