KI-Konzil/backend/tools/web_search.py
Claude 001649a364
Implement Phase 4: tools, God Mode, and missing features
Backend:
- Add Tavily web search tool wrapper (tools/web_search.py)
- Add PDF reader + ChromaDB vector store tool (tools/pdf_reader.py)
- Bind tools to LLM calls via .bind_tools() in dynamic_graph_builder
- Implement God Mode using LangGraph interrupt_before + MemorySaver
- Add approve/reject/modify API endpoints for God Mode
- Add PDF upload endpoint with ingestion pipeline
- Add persistent run history (CouncilRun model + run_service + API)
- Add Alembic migration for council_runs table
- Enhance WebSocket to emit run_paused and run_resumed events
- Add tests for tools, God Mode, and run history

Frontend:
- Add God Mode approval UI (GodModePanel component)
- Add Auto-Pilot / God Mode toggle in Konferenzzimmer
- Add functional PDF upload handler
- Add Conditional Edge editor (EdgeSettingsPanel component)
- Add edge click selection in ArchitectCanvas
- Update Zustand store with edge selection and update actions
- Update types for God Mode, execution modes, and WS events
- Update API client with God Mode, PDF upload, and blueprint run endpoints
- Update WebSocket hook for paused/resumed events
- Add Vitest config and frontend tests (store, parser, types, API)

https://claude.ai/code/session_017U6idFgaqnYTXzPxA7mxMv
2026-02-21 10:53:12 +00:00

61 lines
1.7 KiB
Python

"""
Web Search Tool — Tavily Search API wrapper for agent nodes.
Provides a LangChain-compatible tool that agents can use to search the web
for current information. Requires the TAVILY_API_KEY environment variable.
"""
import os
from typing import Optional
from langchain_core.tools import tool
@tool
def web_search(query: str, max_results: int = 5) -> str:
"""
Search the web for current information on a topic.
Args:
query: The search query string.
max_results: Maximum number of results to return (default 5).
Returns:
A formatted string with search results including titles, URLs, and snippets.
"""
from tavily import TavilyClient
api_key = os.environ.get("TAVILY_API_KEY")
if not api_key:
return "[Web Search Error] TAVILY_API_KEY environment variable is not set."
client = TavilyClient(api_key=api_key)
try:
response = client.search(
query=query,
max_results=max_results,
search_depth="basic",
)
except Exception as exc: # noqa: BLE001
return f"[Web Search Error] {exc}"
results = response.get("results", [])
if not results:
return f"No results found for: {query}"
formatted = []
for i, r in enumerate(results, 1):
title = r.get("title", "No title")
url = r.get("url", "")
content = r.get("content", "No content available")
formatted.append(f"{i}. **{title}**\n URL: {url}\n {content}")
return "\n\n".join(formatted)
def create_web_search_tool() -> Optional[tool]:
"""Factory that returns the web_search tool if Tavily is configured."""
if os.environ.get("TAVILY_API_KEY"):
return web_search
return None