Skip to content

Conversation

@AndlerRL
Copy link
Member

@AndlerRL AndlerRL commented Jun 12, 2025

Summary by Sourcery

Enable web search capabilities in AI actions by defining web search tools, integrating them into existing streaming and execution functions, and introducing a multi-step web search agent.

New Features:

  • Define a reusable web search tool in AI actions and a separate tools module.
  • Add factory functions to instantiate various LLM clients and derive model identifiers.
  • Extend streaming and generation functions to optionally use web search as a tool.
  • Implement a multi-step webSearchAgent that iteratively queries the web and synthesizes results.
  • Introduce executeAIWithWebSearch to enable tool-assisted AI execution in ai-executers.actions.
  • Add a WebSearchAgent class for optimized query generation, web search, and LLM-based result synthesis.
  • Provide webSearchTool and wordwareWebSearchTool in a new tools package for external use.

Enhancements:

  • Refactor imports and consolidate message formatting for AI SDK integration.
  • Centralize LLM client and model selection logic across actions.

Chores:

  • Add @ai-sdk/perplexity dependency in package.json.

@AndlerRL AndlerRL self-assigned this Jun 12, 2025
@sourcery-ai
Copy link

sourcery-ai bot commented Jun 12, 2025

Reviewer's Guide

This PR integrates web search functionality across the AI pipeline by defining reusable webSearch tools, abstracting LLM client/model selection, extending existing streaming and generation functions to optionally invoke web search, and introducing a standalone WebSearchAgent module with full multi-step reasoning support.

Sequence Diagram: Basic Web Search Tool Invocation via AI Function

sequenceDiagram
    actor User
    participant App as "Application Logic (e.g., streamTextWithWebSearch)"
    participant LLM as "Language Model"
    participant Tool as "webSearchTool"
    participant SearchAPI as "Web Search API (/api/web-search or direct)"

    User->>App: Submits query
    App->>LLM: Call LLM (e.g., streamText) with messages and webSearchTool
    LLM-->>Tool: execute({query: "search_term"})
    Tool->>SearchAPI: Perform Search (e.g., POST /api/web-search or call Tavily/Google)
    SearchAPI-->>Tool: Search Results
    Tool-->>LLM: Return search results
    LLM->>App: Stream/Return final response incorporating search results
    App->>User: Display response
Loading

Sequence Diagram: WebSearchAgent Class searchAndSynthesize Method

sequenceDiagram
    participant Caller
    participant WSAInstance as "WebSearchAgent (Class Instance)"
    participant LLMQueryGen as "LLM (for Query Generation - generateObject)"
    participant GoogleSearchAPI as "Google Custom Search API"
    participant LLMSynthesis as "LLM (for Synthesis - generateObject)"

    Caller->>WSAInstance: searchAndSynthesize("user_query")

    WSAInstance->>LLMQueryGen: generateObject({prompt_for_queries, schema: searchQuerySchema})
    LLMQueryGen-->>WSAInstance: {queries: ["q1", "q2"]}

    loop For each generated query
        WSAInstance->>GoogleSearchAPI: GET /customsearch/v1 (params: query, apiKey, cx)
        GoogleSearchAPI-->>WSAInstance: Raw Search Results (JSON)
        WSAInstance->>WSAInstance: Format and collect results (as WebSearchResultItem[])
    end

    WSAInstance->>LLMSynthesis: generateObject({prompt_with_all_results, userQuery, schema: webSearchResultSchema})
    LLMSynthesis-->>WSAInstance: {searchResults: "synthesized_text", sources: [...], ...}

    WSAInstance-->>Caller: {searchResults, sources, rawResults}
Loading

File-Level Changes

Change Details Files
Define and integrate webSearch tools
  • Add webSearchTool definition using ai.tool in main ai actions
  • Add webSearchTool definition with Tavily API in executers
  • Create lib/tools/web-search.ts with two tool variants
  • Export webSearchTool for external consumption
apps/masterbots.ai/app/actions/ai-main-call.actions.ts
apps/masterbots.ai/app/actions/ai-executers.actions.ts
apps/masterbots.ai/lib/tools/web-search.ts
Implement webSearchAgent and streaming response support
  • Introduce webSearchAgent with multi-step reasoning loop and tool invocation
  • Add createResponseStreamWithWebSearch to route requests through the agent
  • Stream agent answers as SSE with chunked text encoding
apps/masterbots.ai/app/actions/ai-main-call.actions.ts
Abstract LLM client creation and model selection
  • Add createLLMClient factory for various AiClientType backends
  • Add getModelId helper to choose default model per client
  • Add getModelInstance in executers for SDK consistency
apps/masterbots.ai/app/actions/ai-main-call.actions.ts
apps/masterbots.ai/app/actions/ai-executers.actions.ts
Extend core streaming/generation functions with web search support
  • Add streamTextWithWebSearch, generateTextWithWebSearch and generateObjectWithWebSearch
  • Add executeAIWithWebSearch helper with tool-enabled generateText
  • Pass tools and toolChoice when webSearch flag is true
apps/masterbots.ai/app/actions/ai-main-call.actions.ts
apps/masterbots.ai/app/actions/ai-executers.actions.ts
Introduce standalone WebSearchAgent module
  • Add lib/agents/web-search-agent.ts implementing WebSearchAgent class
  • Define Zod schemas for queries, results and synthesized output
  • Implement generateSearchQueries, performWebSearch, searchAndSynthesize and search methods
apps/masterbots.ai/lib/agents/web-search-agent.ts
Update dependencies for new AI backends
  • Add @ai-sdk/perplexity to package.json dependencies
apps/masterbots.ai/package.json

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@vercel
Copy link

vercel bot commented Jun 12, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
masterbots Error Error Aug 26, 2025 1:28am

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 12, 2025

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat-web-search-tool-v3

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@AndlerRL
Copy link
Member Author

AndlerRL commented Jul 2, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants