Skip to content

feat(provider): add provider macros#19

Merged
bzp2010 merged 2 commits intomainfrom
bzp/feat-new-gateway-provider-macros
Apr 6, 2026
Merged

feat(provider): add provider macros#19
bzp2010 merged 2 commits intomainfrom
bzp/feat-new-gateway-provider-macros

Conversation

@bzp2010
Copy link
Copy Markdown
Collaborator

@bzp2010 bzp2010 commented Apr 6, 2026

As the 5th part of the major refactoring of the provider, we will add a macro for easily registering OpenAI-compatible providers and create a new deepseek provider to verify this.

Summary by CodeRabbit

  • New Features

    • Added DeepSeek provider with bearer-token auth
    • Added OpenAI provider with streaming usage quirks enabled
    • Provider registry to assemble and expose available providers
    • New API-key validation/access helpers for provider auth
  • Documentation

    • Clarified provider-generation macro behavior and provider definition notes

Copilot AI review requested due to automatic review settings April 6, 2026 17:15
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 6, 2026

📝 Walkthrough

Walkthrough

Adds a declarative provider! macro to generate provider types/impls, a new DeepSeek provider via the macro, a manual OpenAI provider definition, a default provider registry, exposes providers module, and adds API-key extraction helpers on ProviderAuth. (Documentation update included.)

Changes

Cohort / File(s) Summary
Documentation
docs/internals/llm-types.md
Clarifies that OpenAI-compatible providers can be generated via provider!() macro and describes produced types (ProviderMeta, ChatTransform, ProviderCapabilities) and DeepSeek as first macro-generated provider.
Module Exposure
src/gateway/mod.rs
Exports the new providers submodule (pub mod providers;).
Provider Auth Helpers
src/gateway/provider_instance.rs
Adds ProviderAuth::api_key(&self) -> Result<&str> and ProviderAuth::api_key_for(&self, provider: &str) -> Result<&str> with validation error mapping and unit tests.
Provider Macro System
src/gateway/providers/macros.rs
Introduces provider! macro that generates pub struct <Provider>; and implementations for ProviderMeta, conditional ChatTransform, ProviderCapabilities, auth header builders, stream reader kind, quirks wiring, and unit tests; re-exports macro.
Providers Module & Registry
src/gateway/providers/mod.rs
New providers module that declares deepseek, macros, openai, re-exports DeepSeek and OpenAIDef, and adds default_provider_registry() which registers OpenAI and DeepSeek (with tests).
Concrete Providers
src/gateway/providers/deepseek.rs, src/gateway/providers/openai.rs
Adds DeepSeek via provider! (display_name, base_url, bearer auth) and OpenAIDef (manual impls for ProviderMeta, bearer auth header, and ChatTransform default quirks).

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Gateway
    participant ProviderRegistry
    participant Provider
    participant LLM_API

    Client->>Gateway: Send request (provider name, payload)
    Gateway->>ProviderRegistry: lookup(provider name)
    ProviderRegistry-->>Gateway: provider type/instance
    Gateway->>Provider: request API key via ProviderAuth::api_key_for
    Provider-->>Gateway: API key or validation error
    Gateway->>Provider: Provider::build_auth_headers(api_key)
    Provider-->>Gateway: HeaderMap (Authorization)
    Gateway->>LLM_API: Forward request with headers
    LLM_API-->>Gateway: Response
    Gateway-->>Client: Return response
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested reviewers

  • LiteSun
🚥 Pre-merge checks | ✅ 2 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
E2e Test Quality Review ⚠️ Warning PR contains only unit tests with mock providers, not end-to-end tests verifying full business flow integration. Add E2E tests in ./tests/ directory that create ProviderRegistry, construct ProviderInstance, verify complete request flow, test DeepSeek in gateway context, and verify error propagation through full stack.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: adding provider macros as a core feature to simplify OpenAI-compatible provider registration, validated by the new DeepSeek provider implementation.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch bzp/feat-new-gateway-provider-macros

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Introduces a new gateway::providers module with built-in provider definitions and a provider!() macro to generate small OpenAI-compatible provider implementations.

Changes:

  • Added provider!() macro to generate ProviderMeta + default ChatTransform + empty ProviderCapabilities.
  • Added built-in providers (OpenAIDef hand-written, DeepSeek macro-generated) and a default_provider_registry().
  • Added ProviderAuth::api_key() helper and updated internal docs describing the provider/macro layering.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/gateway/providers/openai.rs Adds a concrete OpenAI provider definition + tests (auth headers, default quirks).
src/gateway/providers/deepseek.rs Adds a macro-generated DeepSeek provider definition.
src/gateway/providers/macros.rs Introduces provider!() macro for declarative provider definitions + tests.
src/gateway/providers/mod.rs Exposes provider modules/exports and adds default_provider_registry() + test.
src/gateway/provider_instance.rs Adds ProviderAuth::api_key() helper + unit test.
src/gateway/mod.rs Exposes new gateway::providers module.
docs/internals/llm-types.md Documents the new provider macro and provider layering.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/gateway/providers/mod.rs (1)

10-15: Consider adding documentation for the public function.

The default_provider_registry() function is public and would benefit from a /// documentation comment explaining its purpose and return value.

📝 Proposed documentation
+/// Creates a provider registry pre-populated with all built-in providers.
+///
+/// Returns an error if provider registration fails (e.g., duplicate names).
 pub fn default_provider_registry() -> Result<ProviderRegistry> {
     let builder = ProviderRegistry::builder()
         .register(OpenAIDef)?
         .register(DeepSeek)?;
     Ok(builder.build())
 }

As per coding guidelines: "Use /// documentation comments on public items".

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/gateway/providers/mod.rs` around lines 10 - 15, The public function
default_provider_registry lacks a /// doc comment; add a concise Rust
documentation comment above the function that explains what
default_provider_registry() returns (a Result<ProviderRegistry> containing a
registry pre-registered with OpenAIDef and DeepSeek), describes any error
conditions, and notes its typical usage; reference the ProviderRegistry builder
and the registered providers (OpenAIDef, DeepSeek) in the comment so callers
understand what the default registry contains.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/gateway/providers/mod.rs`:
- Around line 10-15: The public function default_provider_registry lacks a ///
doc comment; add a concise Rust documentation comment above the function that
explains what default_provider_registry() returns (a Result<ProviderRegistry>
containing a registry pre-registered with OpenAIDef and DeepSeek), describes any
error conditions, and notes its typical usage; reference the ProviderRegistry
builder and the registered providers (OpenAIDef, DeepSeek) in the comment so
callers understand what the default registry contains.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: dbb54683-7905-40a2-b733-401eb4fd8ef6

📥 Commits

Reviewing files that changed from the base of the PR and between 7200e16 and 66d2fda.

📒 Files selected for processing (7)
  • docs/internals/llm-types.md
  • src/gateway/mod.rs
  • src/gateway/provider_instance.rs
  • src/gateway/providers/deepseek.rs
  • src/gateway/providers/macros.rs
  • src/gateway/providers/mod.rs
  • src/gateway/providers/openai.rs

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/gateway/providers/macros.rs (1)

1-1: Consider adding documentation for macro usage.

The macro is well-implemented but lacks documentation comments. Adding /// docs with a usage example would help future maintainers understand the expected syntax and available options.

📖 Proposed documentation
+/// Generates a provider marker type with [`ProviderMeta`], [`ProviderCapabilities`],
+/// and [`ChatTransform`] implementations.
+///
+/// # Example
+/// ```ignore
+/// provider!(MyProvider {
+///     display_name: "my-provider",
+///     base_url: "https://api.example.com",
+///     chat_path: "/v1/chat",           // optional, defaults to "/v1/chat/completions"
+///     stream: StreamReaderKind::Sse,   // optional, defaults to Sse
+///     auth: bearer,                    // or: api_key_header("x-api-key")
+///     quirks: {                        // optional block
+///         unsupported_params: &["seed"],
+///     }
+/// });
+/// ```
 macro_rules! provider {

Also applies to: 146-146

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/gateway/providers/macros.rs` at line 1, Add doc comments above the
provider! macro to describe its purpose, expected input syntax, and available
options; specifically add /// comments before the macro_rules! provider
declaration that include a short description and a usage example showing fields
like display_name, base_url, optional chat_path, optional stream (e.g.,
StreamReaderKind::Sse), auth variants (bearer or api_key_header), and the
optional quirks block (unsupported_params). Ensure the docs use triple-slash
comments (///) and keep the example concise and accurate to the macro's accepted
keys so future maintainers can quickly understand how to invoke provider!.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/gateway/providers/macros.rs`:
- Line 1: Add doc comments above the provider! macro to describe its purpose,
expected input syntax, and available options; specifically add /// comments
before the macro_rules! provider declaration that include a short description
and a usage example showing fields like display_name, base_url, optional
chat_path, optional stream (e.g., StreamReaderKind::Sse), auth variants (bearer
or api_key_header), and the optional quirks block (unsupported_params). Ensure
the docs use triple-slash comments (///) and keep the example concise and
accurate to the macro's accepted keys so future maintainers can quickly
understand how to invoke provider!.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 99d02a9e-a130-4489-bed7-bfd9dda4fcbb

📥 Commits

Reviewing files that changed from the base of the PR and between 66d2fda and 60f4e36.

📒 Files selected for processing (3)
  • src/gateway/provider_instance.rs
  • src/gateway/providers/macros.rs
  • src/gateway/providers/openai.rs
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/gateway/provider_instance.rs
  • src/gateway/providers/openai.rs

@bzp2010 bzp2010 merged commit a95ba1a into main Apr 6, 2026
10 checks passed
@bzp2010 bzp2010 deleted the bzp/feat-new-gateway-provider-macros branch April 6, 2026 18:09
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