Skip to content

Refactor A2A connection#104

Merged
foxminchan merged 1 commit into
mainfrom
refac/cleanup-a2a
Aug 6, 2025
Merged

Refactor A2A connection#104
foxminchan merged 1 commit into
mainfrom
refac/cleanup-a2a

Conversation

@foxminchan

@foxminchan foxminchan commented Aug 6, 2025

Copy link
Copy Markdown
Owner

Pull Request Description

Checklist

  • My code follows the DDD principles
  • I've used the latest C# features and idioms
  • I've added/updated unit tests for business logic
  • Service boundaries are maintained (no cross-service direct dependencies)
  • Repository pattern is used for data access
  • Domain Events are used for cross-service communication
  • I've registered new services in AppHost project (if applicable)
  • I've updated both entities and DTOs (if applicable)

Summary by CodeRabbit

  • New Features

    • Added support for registering HTTP service references with customizable health check paths and failure statuses.
    • Introduced new HTTP service references for "RatingAgent" and "SummarizeAgent" to improve agent communication and monitoring.
  • Bug Fixes

    • Improved validation for service registration addresses and health check paths.
  • Refactor

    • Simplified agent creation by removing manual service endpoint resolution and relying on named HTTP clients.
    • Removed the unused agent card functionality from the SentimentAgent.

@coderabbitai

coderabbitai Bot commented Aug 6, 2025

Copy link
Copy Markdown

Walkthrough

This update introduces new overloads and validation logic to the AddHttpServiceReference methods, standardizing how HTTP service references and health checks are registered. It refactors agent classes to use named HttpClient instances for service endpoint resolution, removing manual service discovery. Some unused agent metadata code is also removed.

Changes

Cohort / File(s) Change Summary
Service Reference Extension Updates
src/Aspire/BookWorm.ServiceDefaults/Kestrel/ServiceReferenceExtensions.cs
Adds a new overload for AddHttpServiceReference supporting health check path validation and updates the generic overload for consistent health check handling.
Agent Service Registration (Chat)
src/Services/Chat/BookWorm.Chat/Agents/Extensions.cs
Registers a new HTTP service reference for "RatingAgent" with health check configuration in the agent extension method.
Agent Service Registration (Rating)
src/Services/Rating/BookWorm.Rating/Agents/Extensions.cs
Registers a new HTTP service reference for "SummarizeAgent" with health check configuration; adds necessary namespace import.
Agent Endpoint Refactor (Chat)
src/Services/Chat/BookWorm.Chat/Agents/BookAgent.cs
Refactors agent creation to use a named HttpClient ("RatingAgent") for endpoint resolution, removing dependency on ServiceEndpointResolver.
Agent Endpoint Refactor (Rating)
src/Services/Rating/BookWorm.Rating/Agents/RatingAgent.cs
Refactors agent creation to use a named HttpClient ("SummarizeAgent") for endpoint resolution, removing dependency on ServiceEndpointResolver.
Agent Metadata Removal
src/Services/Chat/BookWorm.Chat/Agents/SentimentAgent.cs
Removes the static GetAgentCard method and unused variables/imports, eliminating agent metadata construction logic.

Sequence Diagram(s)

Agent Creation with Named HttpClient

sequenceDiagram
    participant Kernel
    participant IHttpClientFactory
    participant BookAgent/RatingAgent
    participant A2AClientFactory

    Kernel->>IHttpClientFactory: CreateClient("AgentName")
    IHttpClientFactory-->>Kernel: HttpClient (BaseAddress configured)
    Kernel->>BookAgent/RatingAgent: Pass HttpClient
    BookAgent/RatingAgent->>A2AClientFactory: CreateAgentAsync(endpoint = HttpClient.BaseAddress)
    A2AClientFactory-->>BookAgent/RatingAgent: Agent Instance
Loading

Service Reference Registration with Health Check

sequenceDiagram
    participant IServiceCollection
    participant ServiceReferenceExtensions

    IServiceCollection->>ServiceReferenceExtensions: AddHttpServiceReference(name, address, status, healthPath)
    ServiceReferenceExtensions->>ServiceReferenceExtensions: Validate address and healthPath
    ServiceReferenceExtensions->>IServiceCollection: Register HttpClient (BaseAddress)
    ServiceReferenceExtensions->>IServiceCollection: Register HealthCheck (BaseAddress + healthPath)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~15 minutes

Suggested labels

.NET, AI

Poem

Refactored agents, endpoints anew,
Named clients now guide what agents do.
Health checks aligned, with paths precise,
Old metadata gone—was that the price?
Clean code marches on, with structure and grace,
In BookWorm’s world, improvements embrace!
🐛📚✨

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 26da202 and 0aa2000.

📒 Files selected for processing (6)
  • src/Aspire/BookWorm.ServiceDefaults/Kestrel/ServiceReferenceExtensions.cs (2 hunks)
  • src/Services/Chat/BookWorm.Chat/Agents/BookAgent.cs (1 hunks)
  • src/Services/Chat/BookWorm.Chat/Agents/Extensions.cs (2 hunks)
  • src/Services/Chat/BookWorm.Chat/Agents/SentimentAgent.cs (0 hunks)
  • src/Services/Rating/BookWorm.Rating/Agents/Extensions.cs (2 hunks)
  • src/Services/Rating/BookWorm.Rating/Agents/RatingAgent.cs (1 hunks)
💤 Files with no reviewable changes (1)
  • src/Services/Chat/BookWorm.Chat/Agents/SentimentAgent.cs
🧰 Additional context used
📓 Path-based instructions (2)
**/*.cs

📄 CodeRabbit Inference Engine (.github/copilot-instructions.md)

**/*.cs: Always use the latest version C#, currently C# 13 features.
Apply code-formatting style defined in .editorconfig.
Prefer file-scoped namespace declarations and single-line using directives.
Insert a newline before the opening curly brace of any code block (e.g., after if, for, while, foreach, using, try, etc.).
Ensure that the final return statement of a method is on its own line.
Use pattern matching and switch expressions wherever possible.
Use nameof instead of string literals when referring to member names.
Place private class declarations at the bottom of the file.
Use var when the type is obvious.
Use Primary Constructor for classes with immutable properties.
Use Expression-bodied members for methods and properties.
Declare variables non-nullable, and check for null at entry points.
Always use is null or is not null instead of == null or != null.
Trust the C# null annotations and don't add null checks when the type system says a value cannot be null.

Files:

  • src/Services/Chat/BookWorm.Chat/Agents/Extensions.cs
  • src/Services/Rating/BookWorm.Rating/Agents/Extensions.cs
  • src/Services/Chat/BookWorm.Chat/Agents/BookAgent.cs
  • src/Services/Rating/BookWorm.Rating/Agents/RatingAgent.cs
  • src/Aspire/BookWorm.ServiceDefaults/Kestrel/ServiceReferenceExtensions.cs
src/Aspire/**

⚙️ CodeRabbit Configuration File

src/Aspire/**: .NET Aspire orchestration review:

  • Service discovery and configuration
  • Health checks and observability
  • Resource allocation and scaling
  • Development environment setup

Files:

  • src/Aspire/BookWorm.ServiceDefaults/Kestrel/ServiceReferenceExtensions.cs
🧠 Learnings (1)
📚 Learning: for c# code, prefer file-scoped namespace declarations and single-line using directives to improve r...
Learnt from: CR
PR: foxminchan/BookWorm#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-06-23T18:48:43.455Z
Learning: For C# code, prefer file-scoped namespace declarations and single-line using directives to improve readability and reduce nesting.

Applied to files:

  • src/Services/Chat/BookWorm.Chat/Agents/BookAgent.cs
  • src/Services/Rating/BookWorm.Rating/Agents/RatingAgent.cs
🔇 Additional comments (6)
src/Aspire/BookWorm.ServiceDefaults/Kestrel/ServiceReferenceExtensions.cs (2)

50-87: Excellent addition of non-generic HTTP service reference registration.

This new overload provides flexibility for registering named HTTP clients without requiring a specific client type, which is perfect for A2A scenarios. The URI validation, health check integration, and use of IHttpMessageHandlerFactory for proper handler lifecycle management demonstrates solid enterprise patterns.


102-111: Good consistency in validation logic.

The addition of health path validation to the existing generic method maintains consistency with the new overload. The validation correctly checks for relative URI format when a health path is provided.

src/Services/Chat/BookWorm.Chat/Agents/Extensions.cs (1)

18-22: Proper service registration following established patterns.

The HTTP service reference registration uses appropriate constants for URL construction and sets a reasonable failure status of Degraded for cross-service dependencies. This aligns well with the centralized configuration approach.

src/Services/Rating/BookWorm.Rating/Agents/Extensions.cs (1)

20-24: Consistent service registration pattern across services.

The "SummarizeAgent" registration mirrors the approach used in the Chat service, maintaining consistency in how cross-service HTTP references are configured. The degraded health status is appropriate for service dependencies.

src/Services/Rating/BookWorm.Rating/Agents/RatingAgent.cs (1)

50-57: Clean refactoring from manual service discovery to named HTTP clients.

The switch from ServiceEndpointResolver to using named HTTP clients from IHttpClientFactory is a solid architectural improvement. It centralizes configuration and eliminates runtime service discovery complexity. The use of BaseAddress! with the null-forgiving operator is safe here since the service registration ensures the base address is set.

src/Services/Chat/BookWorm.Chat/Agents/BookAgent.cs (1)

47-55: Consistent refactoring pattern matching RatingAgent approach.

The refactoring maintains consistency with the RatingAgent implementation by replacing manual service endpoint resolution with named HTTP clients. This creates a uniform pattern across agents and leverages the centralized service configuration effectively.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch refac/cleanup-a2a

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.
    • Explain this complex logic.
    • 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. Examples:
    • @coderabbitai explain this code block.
  • 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 src/utils.ts and explain its main purpose.
    • @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 comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

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.

Documentation and Community

  • 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.

@netlify

netlify Bot commented Aug 6, 2025

Copy link
Copy Markdown

Deploy Preview for bookwormdev canceled.

Name Link
🔨 Latest commit 0aa2000
🔍 Latest deploy log https://app.netlify.com/projects/bookwormdev/deploys/689329e44a3494000862f93d

@foxminchan foxminchan self-assigned this Aug 6, 2025
@github-actions

github-actions Bot commented Aug 6, 2025

Copy link
Copy Markdown

Test Results

949 tests  ±0   949 ✅ ±0   10m 40s ⏱️ - 2m 32s
  8 suites ±0     0 💤 ±0 
  8 files   ±0     0 ❌ ±0 

Results for commit 0aa2000. ± Comparison against base commit 26da202.

♻️ This comment has been updated with latest results.

@sonarqubecloud

sonarqubecloud Bot commented Aug 6, 2025

Copy link
Copy Markdown

@foxminchan foxminchan changed the title [WIP] Refactor A2A connection Refactor A2A connection Aug 6, 2025
@foxminchan
foxminchan marked this pull request as ready for review August 6, 2025 13:03
Copilot AI review requested due to automatic review settings August 6, 2025 13:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors Agent-to-Agent (A2A) connection handling by replacing direct service discovery with HTTP service references. The change eliminates manual service endpoint resolution in favor of a more standardized approach using named HTTP clients.

Key changes:

  • Replaced ServiceEndpointResolver with named HttpClient configuration for agent connections
  • Added new HTTP service reference registration for inter-agent communication
  • Enhanced ServiceReferenceExtensions with HTTP service reference support

Reviewed Changes

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

Show a summary per file
File Description
src/Services/Rating/BookWorm.Rating/Agents/RatingAgent.cs Updated to use named HttpClient instead of service discovery for SummarizeAgent connection
src/Services/Rating/BookWorm.Rating/Agents/Extensions.cs Added HTTP service reference registration for SummarizeAgent
src/Services/Chat/BookWorm.Chat/Agents/SentimentAgent.cs Removed unused A2A agent card functionality
src/Services/Chat/BookWorm.Chat/Agents/Extensions.cs Added HTTP service reference registration for RatingAgent
src/Services/Chat/BookWorm.Chat/Agents/BookAgent.cs Updated to use named HttpClient instead of service discovery for RatingAgent connection
src/Aspire/BookWorm.ServiceDefaults/Kestrel/ServiceReferenceExtensions.cs Added new HTTP service reference methods with health check support

Comment thread src/Services/Rating/BookWorm.Rating/Agents/RatingAgent.cs
Comment thread src/Services/Chat/BookWorm.Chat/Agents/BookAgent.cs
@foxminchan
foxminchan merged commit 3c57fcc into main Aug 6, 2025
17 checks passed
@foxminchan
foxminchan deleted the refac/cleanup-a2a branch August 6, 2025 13:07
@coderabbitai coderabbitai Bot mentioned this pull request Aug 8, 2025
8 tasks
@coderabbitai coderabbitai Bot mentioned this pull request Oct 17, 2025
11 tasks
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