From 07392c13a4c22240899bd7fc536a99202144d44c Mon Sep 17 00:00:00 2001 From: Paul Walko Date: Wed, 17 Dec 2025 20:57:22 +0100 Subject: [PATCH] more optmizations --- mcp/server.py | 11 ++++++++--- web/agent/src/agent.py | 8 ++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/mcp/server.py b/mcp/server.py index b067027..8491441 100644 --- a/mcp/server.py +++ b/mcp/server.py @@ -59,11 +59,10 @@ def embed(text, input_type): assert resp.embeddings.float_ is not None return resp.embeddings.float_[0] -def search(query, roles: list[str], limit: int = 5) -> list[dict]: +def search(query, roles: list[str], limit: int = 3, max_content_length: int = 1500) -> list[dict]: query_embedding = embed(query, 'search_query') if not roles: - # No roles = no results return [] rows = conn.execute( @@ -71,7 +70,13 @@ def search(query, roles: list[str], limit: int = 5) -> list[dict]: (roles, query_embedding, limit) ).fetchall() - return [{'key': row['key'], 'content': row['content']} for row in rows] + docs = [] + for row in rows: + content = row['content'] or '' + if len(content) > max_content_length: + content = content[:max_content_length] + '...[truncated, use get_document_page for full text]' + docs.append({'key': row['key'], 'content': content}) + return docs @mcp.tool def get_cave_location(cave: str, state: str, county: str) -> list[dict]: diff --git a/web/agent/src/agent.py b/web/agent/src/agent.py index 8a8a112..6336070 100644 --- a/web/agent/src/agent.py +++ b/web/agent/src/agent.py @@ -23,9 +23,9 @@ logger.info("Initializing Cavepedia agent...") def limit_history(ctx: RunContext[None], messages: list[ModelMessage]) -> list[ModelMessage]: - """Limit conversation history to manage token usage and request size.""" - # Keep only the last few messages to avoid large requests hitting Cloudflare limits - return messages[-4:] + """Limit conversation history to manage token usage.""" + # Keep last 8 messages for context, but not unlimited + return messages[-8:] def check_mcp_available(url: str, timeout: float = 5.0) -> bool: """Check if MCP server is reachable via health endpoint.""" @@ -54,7 +54,7 @@ Rules: 4. Can create ascii diagrams/maps. 5. Be direct—no sycophantic phrases. 6. Keep responses concise. -7. Use tools sparingly—one search usually suffices. Answer from your knowledge when possible.""" +7. Search ONCE, then answer with what you found. Do not search repeatedly for the same topic.""" def create_agent(user_roles: list[str] | None = None):