Skip to main content

netget/protocol/
connect_context.rs

1//! Client connect context
2//!
3//! Provides all the necessary context for connecting a protocol client.
4
5use crate::llm::OllamaClient;
6use crate::protocol::StartupParams;
7use crate::state::app_state::AppState;
8use crate::state::ClientId;
9use std::sync::Arc;
10use tokio::sync::mpsc;
11
12/// Context passed to protocol clients during connection
13///
14/// Contains all the dependencies and configuration needed to connect to a remote server.
15#[derive(Clone)]
16pub struct ConnectContext {
17    /// Remote server address (hostname:port or IP:port)
18    /// Example: "example.com:80" or "192.168.1.1:6379"
19    pub remote_addr: String,
20
21    /// LLM client for generating requests and interpreting responses
22    pub llm_client: OllamaClient,
23
24    /// Application state
25    pub state: Arc<AppState>,
26
27    /// Channel for sending status updates to UI
28    pub status_tx: mpsc::UnboundedSender<String>,
29
30    /// Unique identifier for this client instance
31    pub client_id: ClientId,
32
33    /// Optional type-safe startup parameters specific to the protocol
34    ///
35    /// Parameters can only be accessed if they were declared in the protocol's
36    /// `get_startup_parameters()` implementation. Attempting to access undeclared
37    /// parameters will panic at runtime.
38    ///
39    /// For example:
40    /// - HTTP Client: request_headers, user_agent, follow_redirects
41    /// - SSH Client: username, password, private_key_path
42    /// - MySQL Client: username, password, database
43    pub startup_params: Option<StartupParams>,
44}
45
46impl ConnectContext {
47    /// Create a new connect context
48    pub fn new(
49        remote_addr: String,
50        llm_client: OllamaClient,
51        state: Arc<AppState>,
52        status_tx: mpsc::UnboundedSender<String>,
53        client_id: ClientId,
54    ) -> Self {
55        Self {
56            remote_addr,
57            llm_client,
58            state,
59            status_tx,
60            client_id,
61            startup_params: None,
62        }
63    }
64
65    /// Create connect context with startup parameters
66    pub fn with_params(
67        remote_addr: String,
68        llm_client: OllamaClient,
69        state: Arc<AppState>,
70        status_tx: mpsc::UnboundedSender<String>,
71        client_id: ClientId,
72        startup_params: StartupParams,
73    ) -> Self {
74        Self {
75            remote_addr,
76            llm_client,
77            state,
78            status_tx,
79            client_id,
80            startup_params: Some(startup_params),
81        }
82    }
83}