Web Search Technical Reference
Complete technical reference for crocbot’s web search subsystem. Designed to be exhaustive for maintainers and extensible for adding new providers.Table of Contents
- Architecture Overview
- Supported Providers
- Configuration Schema
- Provider: Brave Search
- Provider: Perplexity Sonar
- Model Selection Logic
- API Key Resolution
- Caching Layer
- Tool Parameters
- Response Formats
- Error Handling
- Adding a New Provider
Architecture Overview
Source Files
| File | Purpose |
|---|---|
src/agents/tools/web-search.ts | Main implementation |
src/agents/tools/web-shared.ts | Caching, timeouts, utilities |
src/config/types.tools.ts | TypeScript type definitions |
src/agents/tools/web-search.test.ts | Unit tests |
Supported Providers
| Provider | Type | Default | Best For |
|---|---|---|---|
| Brave | Traditional search engine | Yes | Structured results, free tier, speed |
| Perplexity | AI-synthesized search | No | Complex questions, citations, deep research |
Configuration Schema
Full TypeScript definition fromsrc/config/types.tools.ts:
JSON Config Example (Complete)
Provider: Brave Search
Endpoint
Authentication
| Method | Value |
|---|---|
| Header | X-Subscription-Token: {apiKey} |
| Accept | application/json |
Request Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
q | string | Yes | Search query |
count | number | No | Results count (1-10) |
country | string | No | 2-letter country code (e.g., “DE”, “US”, “ALL”) |
search_lang | string | No | ISO language code for results (e.g., “de”, “en”) |
ui_lang | string | No | ISO language code for UI elements |
freshness | string | No | Time filter (see below) |
Freshness Parameter
| Value | Meaning |
|---|---|
pd | Past 24 hours (past day) |
pw | Past week |
pm | Past month |
py | Past year |
YYYY-MM-DDtoYYYY-MM-DD | Custom date range |
/^(\d{4}-\d{2}-\d{2})to(\d{4}-\d{2}-\d{2})$/
Response Type
API Key Sources
Resolution order:tools.web.search.apiKey(config)BRAVE_API_KEY(environment variable)
Notes
- Use the “Data for Search” plan, NOT “Data for AI”
- Free tier available at https://brave.com/search/api/
freshnessparameter is Brave-exclusive (returns error for Perplexity)
Provider: Perplexity Sonar
Endpoints
| Source | Base URL |
|---|---|
| Perplexity Direct | https://api.perplexity.ai |
| OpenRouter Proxy | https://openrouter.ai/api/v1 |
{baseUrl}/chat/completions
Authentication
Request Body
Response Type
Available Models
| Model ID | Description | Use Case |
|---|---|---|
perplexity/sonar | Fast Q&A with web search | Quick lookups, simple questions |
perplexity/sonar-pro | Multi-step reasoning with web search | Complex questions (default) |
perplexity/sonar-reasoning-pro | Chain-of-thought deep analysis | Research, in-depth investigation |
API Key Sources
Resolution order (with source tracking):tools.web.search.perplexity.apiKey(config) → source:"config"PERPLEXITY_API_KEY(env) → source:"perplexity_env"OPENROUTER_API_KEY(env) → source:"openrouter_env"
Model Selection Logic
Current Behavior
The model is statically configured. There is no automatic selection based on query complexity.What This Means
| Query Type | Expected Model | Actual Behavior |
|---|---|---|
| Simple lookup | sonar | Uses configured model (default: sonar-pro) |
| Complex question | sonar-pro | Uses configured model |
| Deep research | sonar-reasoning-pro | Uses configured model |
sonar-reasoning-pro for complex queries.
Manual Override Options
Users who want deep research must either:- Configure globally: Set
tools.web.search.perplexity.modelto"perplexity/sonar-reasoning-pro" - Create multiple agents: Configure different agents with different models
- Future enhancement: Implement query analysis to auto-select model (see Adding a New Provider)
Recommended Configuration by Use Case
| Use Case | Model Config |
|---|---|
| Telegram bot (general) | perplexity/sonar-pro (default) |
| Research assistant | perplexity/sonar-reasoning-pro |
| Quick lookups only | perplexity/sonar |
| Cost optimization | perplexity/sonar |
API Key Resolution
Brave
Perplexity (with Base URL inference)
Base URL Resolution Matrix
| API Key Source | Key Prefix | Inferred Base URL |
|---|---|---|
PERPLEXITY_API_KEY env | any | https://api.perplexity.ai |
OPENROUTER_API_KEY env | any | https://openrouter.ai/api/v1 |
| Config | pplx-* | https://api.perplexity.ai |
| Config | sk-or-* | https://openrouter.ai/api/v1 |
| Config | unknown | https://openrouter.ai/api/v1 (safe fallback) |
Explicit baseUrl | any | Uses explicit value |
Caching Layer
Constants
Cache Key Format
normalizeCacheKey(key).trim().toLowerCase()
Cache Entry Structure
Eviction Policy
- TTL-based expiration: Entries older than
cacheTtlMinutesare removed on read - LRU eviction: When cache reaches 100 entries, oldest entry is removed before insert
Cache Response Flag
Cached responses includecached: true in the result payload.
Tool Parameters
Schema Definition
Parameter Defaults
| Parameter | Default | Source |
|---|---|---|
count | 5 | DEFAULT_SEARCH_COUNT |
country | (none) | Provider default |
search_lang | (none) | Provider default |
ui_lang | (none) | Provider default |
freshness | (none) | No filter |
timeoutSeconds | 30 | DEFAULT_TIMEOUT_SECONDS |
Response Formats
Brave Response
Perplexity Response
Cached Response
Any response can include"cached": true if served from cache.
Error Handling
Missing API Key
Invalid Freshness
Unsupported Freshness (Perplexity)
API Errors
Adding a New Provider
Step 1: Update Provider List
Insrc/agents/tools/web-search.ts:
Step 2: Add Config Types
Insrc/config/types.tools.ts:
Step 3: Add API Key Resolution
Step 4: Add Provider Handler
Step 5: Update Dispatcher
InrunWebSearch():
Step 6: Update Cache Key
Step 7: Add Missing Key Error
Step 8: Update Description
Step 9: Write Tests
Insrc/agents/tools/web-search.test.ts:
Step 10: Update Documentation
- Add
docs/tools/newprovider.md - Update
docs/tools/web.mdwith new provider option - Update this technical reference
Quick Reference
Environment Variables
| Variable | Provider | Purpose |
|---|---|---|
BRAVE_API_KEY | Brave | API authentication |
PERPLEXITY_API_KEY | Perplexity | Direct API authentication |
OPENROUTER_API_KEY | Perplexity (via OR) | OpenRouter proxy authentication |
Defaults Summary
| Setting | Default Value |
|---|---|
| Provider | brave |
| Max results | 5 |
| Max allowed results | 10 |
| Timeout | 30 seconds |
| Cache TTL | 15 minutes |
| Cache max entries | 100 |
| Perplexity model | perplexity/sonar-pro |
| Perplexity base URL | (inferred from key) |
Config Paths
| Path | Type | Description |
|---|---|---|
tools.web.search.enabled | boolean | Enable/disable tool |
tools.web.search.provider | string | "brave" or "perplexity" |
tools.web.search.apiKey | string | Brave API key |
tools.web.search.maxResults | number | Default result count |
tools.web.search.timeoutSeconds | number | Request timeout |
tools.web.search.cacheTtlMinutes | number | Cache duration |
tools.web.search.perplexity.apiKey | string | Perplexity/OpenRouter key |
tools.web.search.perplexity.baseUrl | string | API endpoint |
tools.web.search.perplexity.model | string | Model ID |
TODO / Future Enhancements
- Dynamic model selection: Analyze query complexity to auto-select Perplexity model
- Hybrid search: Combine Brave structured results with Perplexity synthesis
- Google Custom Search: Add as alternative traditional search provider
- Bing Search: Add as alternative traditional search provider
- SearXNG: Add self-hosted meta-search option
- Query rewriting: Improve search quality with query expansion
Last updated: 2026-02-01
