1. Search Flow Overview
The web search implementation in the repository is a multi-step process that converts user queries into optimized search queries, retrieves results from various providers or local scraping, processes the retrieved web pages into structured Markdown-like elements, and identifies the most relevant content using embedding-based similarity scoring. This pipeline is designed to efficiently provide contextually relevant, concise information for user prompts.
2. Key Search Components
Search Execution Pattern
- Web searches are executed via a modular provider-based system, supporting both local scraping (Google) and external APIs like Serper, SearXNG, and Bing.
- The first available provider is selected based on environment variables, falling back to local scraping if none are configured (
./endpoints.ts).
- Search providers fetch results in JSON format, which is further processed into a unified format (
WebSearchSource).
Query Construction Approach
- Queries are dynamically generated based on the user’s prompt and conversational history using the
TASK_MODEL via the generateQuery function (./generateQuery.ts).
- The system uses predefined examples for matching patterns between user queries and assistant-generated search terms.
- Query refinement integrates site-specific filters (allow/block lists) or domain-specific constraints to adjust the search scope (
buildQueryFromSiteFilters in ./search.ts).
Result Processing Mechanism
- Raw search results are fetched and filtered based on blocklists, allowlists, and relevant domains (
filterByBlockList in ./search.ts).
- Pages are scraped using Playwright for their HTML content, which is then parsed into a hierarchical Markdown structure (
htmlToMarkdownTree in ./markdown/tree.ts).
- The Markdown structure is flattened, chunked, and converted into embeddings to calculate their similarity to the original query (
findContextSources in ./embed/embed.ts).
Context Management Method
- Context is preserved by embedding textual elements from scraped pages and selecting those most relevant to the query using vector similarity (inner product).
- The embedding-based similarity scoring ensures that only high-relevance content is included, with a minimum character threshold of 3,000 and a soft maximum of 8,000 characters (
findContextSources).
- Relevant results are integrated back into the assistant’s workflow as context sources for answering user queries.
3. Search Implementation Steps
-
Query Formation:
- Extract user messages and conversational history (
generateQuery.ts).
- Dynamically generate a search query tailored to user intent.
- Apply allow/block list filters or domain constraints (
buildQueryFromSiteFilters).
-
Search Execution:
- Execute the query using the first available provider (
./endpoints.ts).
- Fetch JSON search results via API or scrape Google search results locally (
./search/endpoints).
-
Result Processing:
- Scrape and parse the top search results using Playwright (
scrape/scrape.ts).
- Convert HTML content into Markdown trees (
markdown/tree.ts).
- Chunk and sanitize content for embedding-based similarity scoring.
-
Context Integration:
- Embed Markdown elements and find those most similar to the query (
embed/embed.ts).
- Preserve high-relevance content and integrate it as context for subsequent queries.
- Return context sources to the assistant for generating responses.
4. Notable Search Features
Query Refinement Techniques
- Uses historical context and user intent to generate precise search queries.
- Dynamically integrates block/allow list constraints to refine search scope.
Result Filtering Methods
- Filters results based on blocklisted domains or URLs (
filterByBlockList).
- Removes duplicate or invalid URLs using utility functions (
isURL, isURLStringLocal).
Context Preservation Strategies
- Embeds scraped content into vector space and selects the most relevant segments using similarity scoring (
findContextSources).
- Ensures results are compact yet contextually rich, adhering to character limits (3,000–8,000 characters).
Search Iteration Patterns
- Iterates through search results to scrape and process up to 8 pages while embedding only the top 5 relevant pages for efficiency.
- Incorporates feedback loops from search errors or insufficient results to adjust queries or retry with different providers.
5. Search-Specific Dependencies
Internal Dependencies:
- Playwright: For scraping and rendering web pages (
scrape/scrape.ts).
- HTML to Markdown Conversion:
htmlToMarkdownTree for parsing HTML and structuring content hierarchically.
- Utilities like
chunkElements and sanitizeString for breaking down and cleaning web content.
- Embedding Models:
getSentenceSimilarity for vector similarity scoring.
getCombinedSentenceSimilarity for efficient chunking and embedding when using CPU-based models.
External API Providers:
- Serper: Google-like API for search results.
- You.com API: AI-powered search results.
- SearXNG: Privacy-respecting metasearch engine.
- Bing API: Microsoft’s search engine integration.
Environment Variables:
USE_LOCAL_WEBSEARCH: Enables local Google scraping.
- API keys for Serper, Bing, SearXNG, and others (
SERPER_API_KEY, SEARXNG_QUERY_URL, etc.).
WEBSEARCH_BLOCKLIST and WEBSEARCH_ALLOWLIST: For domain-based filtering.
This analysis highlights the repository’s robust, modular, and scalable web search implementation, offering flexibility in query refinement, result processing, and context management—key for building LLM-powered web search agents.