Web Search Capabilities
Relevant source files
The following files were used as context for generating this wiki page:
The Web Search Capability in OS Chat provides AI models with real-time access to the internet to verify facts, find recent events, and retrieve dynamic data that exceeds their training cutoff. This system is architected around a factory pattern that supports multiple providers including Exa, Tavily, and Brave Search APIs, ensuring reliability through fallback mechanisms.
The capability is integrated both in real-time chat sessions and autonomous background agents. It includes sophisticated result processing such as content truncation, markdown formatting, and credit-based metering to manage API costs and usage limits.
Sources: src/lib/tools/search.ts:101-103, README.md:58-59, src/lib/prompt_config.ts:186-193
Architecture and Tool Integration
The search system is implemented as a structured tool using the Vercel AI SDK. It defines a rigorous input schema using Zod to allow the AI model to parameterize its searches based on specific needs, such as date ranges or specific domains.
Search Tool Logic Flow
The following diagram illustrates how a search request is processed from the AI's intent to the final processed results.
The search implementation uses a fallback strategy to ensure high availability across different API providers.
Sources: src/lib/tools/search.ts:47-97, src/lib/tools/search.ts:121-125
Core Components
| Component | Description | Location |
|---|---|---|
searchTool | The primary tool definition exported for use in the AI SDK. | src/lib/tools/search.ts:161 |
searchWithFallback | Logic that attempts searches across multiple providers. | src/lib/tools/search.ts:122 |
createMeteredSearchTool | A wrapper that tracks and increments user credits upon successful searches. | src/lib/tools/search-credit-metering.ts |
processResults | Utility to map raw provider data into the application's internal SearchResult format. | src/lib/tools/search.ts:39-44 |
Search Methodology and Usage Guidelines
The system enforces specific guidelines through the SEARCH_PROMPT_INSTRUCTIONS to ensure efficient use of web search. Models are instructed to use search for dynamic facts (news, stock prices), "latest" requests, and cross-checking high-stakes information.
Search Parameter Schema
The AI model can configure the following parameters when invoking a search:
| Parameter | Type | Description |
|---|---|---|
query | string | The semantic search query string. |
maxResults | number | Limit of results (Default: 3, up to 10 for research). |
scrapeContent | boolean | Whether to fetch full text content (Default: true). |
includeDomains | string[] | Restrict search to specific trusted domains. |
category | enum | Specialized search types like news, pdf, or github. |
startPublishedDate | string | Filter for results after a specific YYYY-MM-DD. |
Sources: src/lib/tools/search.ts:51-97, src/lib/prompt_config.ts:203-214
Execution in Autonomous Agents
Web search is a critical component for background agents. In convex/scheduled_ai.ts, the search tool is initialized with credit metering to ensure autonomous agents do not exceed user quotas.
The agent uses the createMeteredSearchTool which takes a callback to incrementStandardCreditsInternal in the database whenever a search is performed.
Sources: convex/scheduled_ai.ts:182-192, src/lib/tools/search-credit-metering.ts
Result Processing and Formatting
To prevent overwhelming the model's context window, search results undergo aggressive truncation and formatting.
- Content Truncation: Text is truncated to a maximum character limit (configured in
SEARCH_CONFIG.maxTextCharacters). If truncated, an ellipsis is appended. - Markdown Formatting: Results are converted into a standard format featuring a title link, a description, and the content wrapped in a blockquote.
export const formatMarkdown = (result: SearchResult): string => {
let markdown = `### [${result.title}](https://github.com/ajanraj/OpenChat/blob/HEAD/%24%7Bresult.url%7D)\n${result.description}`;
if (result.content) {
const truncatedContent = truncateContent(result.content);
markdown += `\n\n> ${truncatedContent}`;
}
return markdown;
};
Sources: src/lib/tools/search.ts:13-37, src/lib/tools/tests/search.test.ts:44-55
Usage Restrictions
The system explicitly restricts the use of web search for certain categories of queries to save resources and improve response speed. These include:
- Basic facts or general knowledge already present in the model's training data.
- Historical information that has not changed.
- Mathematical calculations or coding syntax the model is already confident about.
Sources: src/lib/prompt_config.ts:227-234
Summary
The Web Search Capability is a robust, fallback-oriented system that enables OS Chat to provide up-to-date information. By utilizing a factory pattern for providers and strict credit metering for background execution, it balances high-performance information retrieval with cost-effective resource management.