lazy load mcp, limit history
All checks were successful
Build and Push Agent Docker Image / build (push) Successful in 2m5s

This commit is contained in:
2025-12-18 18:23:04 +01:00
parent 4928a894fe
commit 09939111a8

View File

@@ -23,9 +23,26 @@ logger.info(f"Initializing Cavepedia agent with CAVE_MCP_URL={CAVE_MCP_URL}")
def limit_history(ctx: RunContext[None], messages: list[ModelMessage]) -> list[ModelMessage]: def limit_history(ctx: RunContext[None], messages: list[ModelMessage]) -> list[ModelMessage]:
"""Limit conversation history to manage token usage and request size.""" """Limit history and clean up orphaned tool calls to prevent API errors."""
# Keep only the last few messages to avoid large requests hitting Cloudflare limits from pydantic_ai.messages import ModelResponse, ToolCallPart
return messages[-4:]
if not messages:
return messages
# Keep only the last 4 messages
messages = messages[-4:]
# Check if the last message is an assistant response with a tool call
# If so, remove it - it's orphaned (no tool result followed)
if messages:
last_msg = messages[-1]
if isinstance(last_msg, ModelResponse):
has_tool_call = any(isinstance(part, ToolCallPart) for part in last_msg.parts)
if has_tool_call:
logger.warning("Removing orphaned tool call from history")
return messages[:-1]
return messages
def check_mcp_available(url: str, timeout: float = 5.0) -> bool: def check_mcp_available(url: str, timeout: float = 5.0) -> bool:
"""Check if MCP server is reachable via health endpoint.""" """Check if MCP server is reachable via health endpoint."""
@@ -42,9 +59,7 @@ def check_mcp_available(url: str, timeout: float = 5.0) -> bool:
logger.warning(f"MCP server not reachable: {e}") logger.warning(f"MCP server not reachable: {e}")
return False return False
# Check if MCP is available at startup # MCP availability is checked lazily in create_agent()
MCP_AVAILABLE = check_mcp_available(CAVE_MCP_URL)
logger.info(f"MCP server available: {MCP_AVAILABLE}")
AGENT_INSTRUCTIONS = """Caving assistant. Help with exploration, safety, surveying, locations, geology, equipment, history, conservation. AGENT_INSTRUCTIONS = """Caving assistant. Help with exploration, safety, surveying, locations, geology, equipment, history, conservation.
@@ -62,7 +77,10 @@ def create_agent(user_roles: list[str] | None = None):
"""Create an agent with MCP tools configured for the given user roles.""" """Create an agent with MCP tools configured for the given user roles."""
toolsets = [] toolsets = []
if MCP_AVAILABLE and user_roles: # Check MCP availability lazily (each request) to handle startup race conditions
mcp_available = check_mcp_available(CAVE_MCP_URL) if user_roles else False
if mcp_available and user_roles:
try: try:
import json import json
from pydantic_ai.mcp import MCPServerStreamableHTTP from pydantic_ai.mcp import MCPServerStreamableHTTP
@@ -93,7 +111,4 @@ def create_agent(user_roles: list[str] | None = None):
) )
# Create a default agent for health checks etc
agent = create_agent()
logger.info("Agent module initialized successfully") logger.info("Agent module initialized successfully")