Skip to content

[Feature] Refactor web_search into a typed provider contract and module split #293

Closed

Description

@Zheng-Lu

Problem statement / motivation

Phase 1 of #291
Scope: Pure refactor (Zero behavior change, byte-identical output)

Currently, crates/agentic-server-core/src/tool/web_search.rs (807 lines) has three architectural limitations that block adding new search providers cleanly:

  1. Untyped Pass-Through to the LLM:
    execute_search copies raw You.com JSON (results.web[] / results.news[]) verbatim into the model-facing output string (web_search.rs:230-236). WebSearchProviderResponse uses untyped serde_json::Value (:262-265).
  2. Monolithic File:
    A single 800+ line file mixes the OpenAI tool adapter, argument validation, and vendor-specific You.com HTTP request/response handling.
  3. Credential Security Leak:
    YouSearchProvider derives Debug over raw Option<String>, which prints plaintext API keys in debug logs (:267-272).

Proposed solution

This phase establishes the typed contract and modular layout with zero external behavior change, making Phase 2 adding new search provider a pure additive change.

A. Modular File Layout

Split crates/agentic-server-core/src/tool/web_search.rs into:

crates/agentic-server-core/src/tool/web_search/
├── mod.rs   # WebSearchHandler, WebSearchProvider trait, typed structs, public output mapping
├── args.rs  # WebSearchArguments, typed Freshness, domain post-filter helper
└── you.rs   # YouSearchProvider, HTTP request shaping, and response parsing

B. Typed Normalization Contract

Replace serde_json::Value with strongly typed models:

  • WebSearchResult: Contains url, title, description, snippets: Vec<String>, page_age: Option<String>, contents: Option<String> (livecrawl). Serialized JSON keys remain unchanged; cosmetic fields (thumbnails, favicons) are dropped.
  • WebSearchProviderMetadata: { provider, query, latency_ms }.
  • WebSearchProviderResponse: { web: Vec<WebSearchResult>, news: Vec<WebSearchResult>, metadata: WebSearchProviderMetadata }.

C. Configuration & Extension Wiring

  • Add WebSearchProviderKind (#[non_exhaustive], initially You only, with #[default]) in crates/agentic-server-core/src/config.rs.
  • Add WebSearchProviderConfig.provider.
  • Add WebSearchHandler::from_config and hook it into ToolExecutors::from_config (tool/executors.rs:87).
  • Signature Guarantee: Keep all existing public constructors (from_env, from_values, with_api_key, spec_only) completely unchanged.
  • Wrap API credentials in a newtype ApiKey with a redacting Debug impl (ApiKey(<redacted>)).

Decision on #291 made at the moment

Question Default Decision Rationale
Q1 (page_age vs published_at) Keep page_age Guarantees 100% byte-identical serialized output for You.com in Phase 1.
Q4 (Enum Location) config.rs Sits naturally next to SqliteTempStore, matching repository conventions.

Additional context

Scope Boundaries

Included in Phase 1 Explicitly Excluded (Deferred to Phase 2 / Later)
✅ Module split (mod.rs, args.rs, you.rs) ❌ No third-party provider code
✅ Typed WebSearchResult & WebSearchProviderResponse ❌ No new config.toml keys or environment variables
✅ Credential redacting Debug ❌ No default base URL for You.com (#291 Q2)
✅ WebSearchProviderKind::You and from_config ❌ WebSearchProvider trait remains private
✅ Byte-identical output assertion test ❌ No doc updates (except CHANGELOG.md)

Verification Checklist

  • Byte-Identity Test: Serialize handler output for existing mock fixture (tests/web_search_tool_test.rs:291) and assert exact byte equality with pre-refactor output.
  • Fixture Test: Add a redacted real You.com response fixture to verify all non-cosmetic fields are preserved.
  • Public Output Contract: Verify sources_from_output continues to generate identical web_search_call.action.sources.
  • Integration Tests: All existing tests pass unmodified across both crates (including all with_api_key callers).
  • Code Hygiene:
    • cargo clippy --all-targets -- -D warnings (clean)
    • cargo fmt -- --check (clean)
    • pre-commit run --all-files (clean)

Activity

  1. added
    enhancement
    on Sep 13, 2026
  2. added a commit that references this issue on Sep 16, 2026
    4784ffc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions