Overview
TheConnection class is the foundational building block for all ACP communication. It manages bidirectional JSON-RPC 2.0 message exchange over ACP streams, handling request/response matching, notifications, and cancellation semantics.
Class Definition
- Bidirectional JSON-RPC 2.0 communication
- Request/response correlation using unique IDs
- Notification handling (one-way messages)
- Request cancellation support
- Automatic message queuing and delivery
Constructor
Connection
Creates a new connection instance.A function that handles incoming requests. Signature:
Future<dynamic> Function(String method, dynamic params)A function that handles incoming notifications. Signature:
Future<void> Function(String method, dynamic params)The bidirectional message stream for communication
Type Aliases
RequestHandler
Type alias for request handler functions.NotificationHandler
Type alias for notification handler functions.Methods
sendRequest
Sends a request and returns a future that completes with the response.- Generates a unique request ID
- Sends the JSON-RPC request message
- Tracks the pending response
- Returns a future that resolves when the response arrives
The JSON-RPC method name
Optional parameters for the request (typically a Map or List)
The response result, typed according to the generic parameter
sendNotification
Sends a notification (no response expected).The JSON-RPC method name
Optional parameters for the notification
sendCancelRequestNotification
Sends the protocol-level cancellation notification$/cancel_request.
The cancellation notification with request ID and optional metadata
cancelPendingRequest
Cancels a pending outbound request and notifies the peer.true if the request ID was still pending locally.
This method:
- Removes the pending request from tracking
- Rejects the requestβs future with a cancellation error
- Sends a
$/cancel_requestnotification to the peer
The ID of the request to cancel
Optional metadata to include in the cancellation notification
true if the request was pending and was cancelled, false if it was already completed or not foundMessage Processing
TheConnection class automatically processes incoming messages and routes them appropriately:
Request Handling
Incoming requests (messages with bothmethod and id) are:
- Validated and parsed
- Passed to the
requestHandlerfunction - Responded to with either a success result or error
Notification Handling
Incoming notifications (messages withmethod but no id) are:
- Validated and parsed
- Passed to the
notificationHandlerfunction - No response is sent
Response Handling
Incoming responses (messages withid but no method) are:
- Matched to pending requests by ID
- Used to resolve or reject the corresponding request future
- Removed from pending request tracking
Error Handling
TheConnection class provides robust error handling:
RequestError Class
Represents an error in ACP/JSON-RPC communication.The JSON-RPC error code
Human-readable error message
Additional error data
Standard Error Factories
Error Response
JSON-RPC 2.0 error response structure.Usage Example
Advanced Features
Request Cancellation
TheConnection class implements the ACP cancellation protocol:
- Local cancellation tracking: Maintains a set of locally cancelled request IDs
- Notification sending: Sends
$/cancel_requestto notify the peer - Response filtering: Ignores responses for cancelled requests
Message Queuing
All outbound messages are queued to ensure ordered delivery:See Also
- AgentSideConnection - High-level agent connection wrapper
- ClientSideConnection - High-level client connection wrapper
- Agent - Agent interface for handling client requests
- Client - Client interface for handling agent requests