Skip to content

Slipstream Package

The azad.slipstream package provides network communication and protocol handling for the Azad agent. It enables the agent to communicate with clients and other components over WebSockets.

Core Components

Base

The Base module defines the core interfaces and classes for network communication. It includes:

  • Protocol interface for implementing different network protocols
  • ProtocolHandler interface for handling protocol messages
  • Decorators for request/response and request/stream patterns

Client

The Client module provides client-side network communication. It includes:

  • Classes for connecting to the agent server
  • Methods for sending requests and receiving responses
  • Utilities for handling streaming responses

Server

The Server module provides server-side network communication. It includes:

  • Classes for creating and managing server instances
  • Methods for handling client connections
  • Utilities for routing messages to the appropriate handlers

WebSocket

The WebSocket module provides WebSocket-specific network communication. It includes:

  • WebSocketNetworkProtocol class for WebSocket communication
  • Methods for starting and stopping the WebSocket server
  • Utilities for handling WebSocket connections and messages

How Slipstream Works

The Slipstream package implements a request/response and request/stream pattern for network communication:

  1. Request/Response: A client sends a request to the server and receives a single response.
  2. Request/Stream: A client sends a request to the server and receives a stream of responses.

These patterns are implemented using decorators on handler methods:

  • @request_response: Indicates that a method handles a request and returns a single response.
  • @request_stream: Indicates that a method handles a request and returns a stream of responses.

When a client connects to the server:

  1. The server creates a WebSocketNetworkProtocol instance
  2. The protocol creates a ProtocolHandler instance (typically an AzadAgent)
  3. The protocol routes client messages to the appropriate handler methods
  4. The handler processes the messages and returns responses
  5. The protocol sends the responses back to the client

Connection States

The Slipstream package manages connection states to ensure reliable communication:

  • ConnectionState.CONNECTING: The connection is being established
  • ConnectionState.CONNECTED: The connection is established and ready for communication
  • ConnectionState.DISCONNECTING: The connection is being closed
  • ConnectionState.DISCONNECTED: The connection is closed

Error Handling

The Slipstream package provides error handling for network communication:

  • ConnectionClosedError: Raised when a connection is closed unexpectedly
  • ConnectionLostError: Raised when a connection is lost due to network issues

These errors are caught and handled appropriately to ensure graceful degradation of service.