agent-infra-sdk 0.2.1

Unified Rust SDK for Gateway-backed and local Agent Infra APIs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! Unified client for Agent Infra services.
//!
//! Context, Trace, Runtime Identity, Model, Deploy, Evaluate, and managed
//! Workspace lifecycle APIs use one Gateway endpoint. Workspace data operations
//! can instead bind an in-process [`agent_workspace_contract::Workspace`], so a
//! runtime can access its local filesystem without proxying bytes through the
//! Gateway.
#![cfg_attr(
    not(any(
        feature = "deploy",
        feature = "admin",
        feature = "agents",
        feature = "evaluate",
        feature = "context",
        feature = "workspace",
        feature = "gateway",
        feature = "model",
        feature = "trace",
        feature = "runtime-identity"
    )),
    allow(dead_code, unused_variables)
)]
//!
//! # Usage
//!
//! ```rust,ignore
//! use agent_infra_sdk::InfraClient;
//!
//! let client = InfraClient::new("http://127.0.0.1:5200")?;
//!
//! let messages = client
//!     .context()
//!     .recent_messages("conv-1", Some(50), &[])
//!     .await?;
//! ```

use std::time::Duration;

#[cfg(feature = "admin")]
mod admin;
#[cfg(feature = "agents")]
mod agent;
#[cfg(feature = "context")]
mod compression;
#[cfg(feature = "context")]
mod context;
#[cfg(feature = "deploy")]
mod deploy;
#[cfg(feature = "workspace")]
mod environment;
#[cfg(feature = "evaluate")]
mod evaluate;
#[cfg(feature = "gateway")]
mod gateway;
#[cfg(feature = "model")]
mod model;
mod operation;
#[cfg(feature = "runtime-identity")]
mod runtime_identity;
#[cfg(feature = "trace")]
mod trace;
mod transport;
mod transport_body;
mod transport_support;
#[cfg(feature = "workspace")]
mod workspace;

#[cfg(feature = "admin")]
pub use admin::AdminClient;
#[cfg(feature = "agents")]
pub use agent::AgentClient;
#[cfg(feature = "context")]
pub use agent_context_contract as context_contract;
#[cfg(feature = "agents")]
pub use agent_registry_contract as agent_contract;
#[cfg(feature = "runtime-identity")]
pub use agent_runtime_identity_contract as runtime_identity_contract;
#[cfg(feature = "trace")]
pub use agent_trace_contract as trace_contract;
#[cfg(feature = "workspace")]
pub use agent_workspace_contract as workspace_contract;
#[cfg(feature = "context")]
#[deprecated(note = "prompt compression belongs to runtime/context assembly")]
pub use compression::{ContextCompressionConfig, compress_context};
#[cfg(feature = "context")]
pub use context::ContextClient;
#[cfg(feature = "deploy")]
pub use deploy::DeployClient;
#[cfg(feature = "workspace")]
pub use environment::EnvironmentClient;
#[cfg(feature = "evaluate")]
pub use evaluate::EvaluateClient;
#[cfg(feature = "gateway")]
pub use gateway::{DelegationLeaseCredentials, GatewayClient, GatewayExchangeCredentials};
#[cfg(feature = "gateway")]
pub use infra_api_gateway_contract as gateway_contract;
#[cfg(feature = "model")]
pub use model::{ModelClient, ModelStream};
pub use operation::{
    CancellationToken, OperationHandle, OperationObservation, OperationPoller, OperationProgress,
    WaitOptions,
};
#[cfg(feature = "runtime-identity")]
pub use runtime_identity::RuntimeIdentityClient;
#[cfg(feature = "trace")]
pub use trace::TraceClient;
pub use transport::{
    BearerCredential, CallOptions, ClientOptions, CredentialError, CredentialsProvider,
    InfraClientError, NoopTelemetry, RetryPolicy, StaticCredentials, TelemetryEvent,
    TelemetryObserver, TelemetryPhase,
};
#[cfg(feature = "workspace")]
pub use workspace::WorkspaceClient;

use reqwest::Client;
use transport::ServiceEndpoint;

/// Unified developer client for Agent Infra.
///
/// Gateway-backed domains share one HTTP connection pool. Domain service
/// addresses are intentionally not part of the public configuration surface;
/// only the Workspace data plane may be replaced by an injected port.
#[derive(Clone, Debug)]
pub struct InfraClient {
    #[cfg(feature = "admin")]
    admin: AdminClient,
    #[cfg(feature = "agents")]
    agents: AgentClient,
    #[cfg(feature = "deploy")]
    deploy: DeployClient,
    #[cfg(feature = "evaluate")]
    evaluate: EvaluateClient,
    #[cfg(feature = "gateway")]
    gateway: GatewayClient,
    #[cfg(feature = "model")]
    model: ModelClient,
    #[cfg(feature = "context")]
    context: ContextClient,
    #[cfg(feature = "trace")]
    trace: TraceClient,
    #[cfg(feature = "workspace")]
    workspace: WorkspaceClient,
    #[cfg(feature = "workspace")]
    environment: EnvironmentClient,
    #[cfg(feature = "runtime-identity")]
    runtime_identity: RuntimeIdentityClient,
}

impl InfraClient {
    /// Start configuring a client for one Agent Infra Gateway.
    pub fn builder(gateway_base_url: impl Into<String>) -> InfraClientBuilder {
        InfraClientBuilder::new(gateway_base_url)
    }

    /// Create a client with default transport policy.
    pub fn new(gateway_base_url: impl Into<String>) -> Result<Self, InfraClientError> {
        Self::builder(gateway_base_url).build()
    }

    /// Create an unauthenticated client for the local Gateway.
    pub fn local() -> Result<Self, InfraClientError> {
        Self::new("http://127.0.0.1:5200")
    }

    #[cfg(feature = "admin")]
    pub fn admin(&self) -> &AdminClient {
        &self.admin
    }

    #[cfg(feature = "agents")]
    pub fn agents(&self) -> &AgentClient {
        &self.agents
    }

    #[cfg(feature = "deploy")]
    pub fn deploy(&self) -> &DeployClient {
        &self.deploy
    }

    #[cfg(feature = "evaluate")]
    pub fn evaluate(&self) -> &EvaluateClient {
        &self.evaluate
    }

    #[cfg(feature = "context")]
    pub fn context(&self) -> &ContextClient {
        &self.context
    }

    #[cfg(feature = "workspace")]
    pub fn workspace(&self) -> &WorkspaceClient {
        &self.workspace
    }

    #[cfg(feature = "workspace")]
    pub fn environment(&self) -> &EnvironmentClient {
        &self.environment
    }

    #[cfg(feature = "gateway")]
    pub fn gateway(&self) -> &GatewayClient {
        &self.gateway
    }

    #[cfg(feature = "model")]
    pub fn model(&self) -> &ModelClient {
        &self.model
    }

    #[cfg(feature = "trace")]
    pub fn trace(&self) -> &TraceClient {
        &self.trace
    }

    #[cfg(feature = "runtime-identity")]
    pub fn runtime_identity(&self) -> &RuntimeIdentityClient {
        &self.runtime_identity
    }
}

fn build_http_client(options: &ClientOptions) -> Result<Client, InfraClientError> {
    if options.connect_timeout.is_zero() || options.connect_timeout > Duration::from_secs(60) {
        return Err(InfraClientError::InvalidOptions {
            message: "connect_timeout must be within 1ns..=60s".into(),
        });
    }
    if options.request_timeout.is_zero() || options.request_timeout > Duration::from_secs(600) {
        return Err(InfraClientError::InvalidOptions {
            message: "request_timeout must be within 1ns..=600s".into(),
        });
    }
    if !(1..=1024).contains(&options.max_idle_connections_per_host) {
        return Err(InfraClientError::InvalidOptions {
            message: "max_idle_connections_per_host must be within 1..=1024".into(),
        });
    }
    if options.pool_idle_timeout.is_zero() || options.pool_idle_timeout > Duration::from_secs(600) {
        return Err(InfraClientError::InvalidOptions {
            message: "pool_idle_timeout must be within 1ns..=600s".into(),
        });
    }
    if !(1..=1024 * 1024 * 1024).contains(&options.max_response_bytes) {
        return Err(InfraClientError::InvalidOptions {
            message: "max_response_bytes must be within 1 byte..=1 GiB".into(),
        });
    }
    if !(1..=8).contains(&options.retry.max_attempts)
        || options.retry.base_delay > options.retry.max_delay
        || options.retry.max_delay > Duration::from_secs(30)
    {
        return Err(InfraClientError::InvalidOptions {
            message: "retry requires 1..=8 attempts and 0 <= base_delay <= max_delay <= 30s".into(),
        });
    }
    if options.user_agent.is_empty()
        || options.user_agent.len() > 256
        || reqwest::header::HeaderValue::from_str(&options.user_agent).is_err()
    {
        return Err(InfraClientError::InvalidOptions {
            message: "user_agent must be a valid 1..=256 byte HTTP header value".into(),
        });
    }
    Client::builder()
        .connect_timeout(options.connect_timeout)
        .timeout(options.request_timeout)
        .pool_idle_timeout(options.pool_idle_timeout)
        .pool_max_idle_per_host(options.max_idle_connections_per_host)
        // Service bearer credentials must not be replayed to redirects.
        .redirect(reqwest::redirect::Policy::none())
        .build()
        .map_err(InfraClientError::ClientBuild)
}

/// Builder for the Infra SDK. Gateway-backed clients share one immutable
/// `reqwest::Client`, one connection pool, and one Gateway endpoint.
#[derive(Debug, Clone)]
pub struct InfraClientBuilder {
    gateway: ServiceEndpoint,
    credentials: Option<std::sync::Arc<dyn CredentialsProvider>>,
    options: ClientOptions,
}

impl InfraClientBuilder {
    pub fn new(gateway_base_url: impl Into<String>) -> Self {
        Self {
            gateway: ServiceEndpoint::new(gateway_base_url),
            credentials: None,
            options: ClientOptions::default(),
        }
    }

    pub fn options(mut self, options: ClientOptions) -> Self {
        self.options = options;
        self
    }

    pub fn credentials(mut self, provider: std::sync::Arc<dyn CredentialsProvider>) -> Self {
        self.credentials = Some(provider);
        self
    }

    pub fn request_timeout(mut self, timeout: std::time::Duration) -> Self {
        self.options.request_timeout = timeout;
        self
    }

    pub fn max_response_bytes(mut self, limit: usize) -> Self {
        self.options.max_response_bytes = limit;
        self
    }

    pub fn retry_policy(mut self, retry: RetryPolicy) -> Self {
        self.options.retry = retry;
        self
    }

    pub fn user_agent(mut self, user_agent: impl Into<String>) -> Self {
        self.options.user_agent = user_agent.into();
        self
    }

    pub fn telemetry(mut self, telemetry: std::sync::Arc<dyn TelemetryObserver>) -> Self {
        self.options.telemetry = telemetry;
        self
    }

    /// Allow plaintext service endpoints only when an authenticated service
    /// mesh supplies the transport security boundary.
    pub fn trusted_mesh_http(mut self, trusted: bool) -> Self {
        self.options.trusted_mesh_http = trusted;
        self
    }

    pub fn build(self) -> Result<InfraClient, InfraClientError> {
        let http = build_http_client(&self.options)?;
        let endpoint = endpoint_with_credentials(self.gateway, &self.credentials);
        Ok(InfraClient {
            #[cfg(feature = "admin")]
            admin: AdminClient::new_with_endpoint(
                http.clone(),
                endpoint.clone(),
                self.options.clone(),
            ),
            #[cfg(feature = "agents")]
            agents: AgentClient::new_with_endpoint(
                http.clone(),
                endpoint.clone(),
                self.options.clone(),
            ),
            #[cfg(feature = "deploy")]
            deploy: DeployClient::new_with_endpoint(
                http.clone(),
                endpoint.clone(),
                self.options.clone(),
            ),
            #[cfg(feature = "evaluate")]
            evaluate: EvaluateClient::new_with_endpoint(
                http.clone(),
                endpoint.clone(),
                self.options.clone(),
            ),
            #[cfg(feature = "gateway")]
            gateway: GatewayClient::new_with_endpoint(
                http.clone(),
                endpoint.clone(),
                self.options.clone(),
            ),
            #[cfg(feature = "model")]
            model: ModelClient::new_with_endpoint(
                http.clone(),
                endpoint.clone(),
                self.options.clone(),
            ),
            #[cfg(feature = "context")]
            context: ContextClient::new_with_endpoint(
                http.clone(),
                endpoint.clone(),
                self.options.clone(),
            ),
            #[cfg(feature = "trace")]
            trace: TraceClient::new_with_endpoint(
                http.clone(),
                endpoint.clone(),
                self.options.clone(),
            ),
            #[cfg(feature = "workspace")]
            workspace: WorkspaceClient::new_with_endpoint(
                http.clone(),
                endpoint.clone(),
                self.options.clone(),
            ),
            #[cfg(feature = "workspace")]
            environment: EnvironmentClient::new_with_endpoint(
                http.clone(),
                endpoint.clone(),
                self.options.clone(),
            ),
            #[cfg(feature = "runtime-identity")]
            runtime_identity: RuntimeIdentityClient::new_with_endpoint(
                http,
                endpoint,
                self.options,
            ),
        })
    }
}

fn endpoint_with_credentials(
    mut endpoint: ServiceEndpoint,
    credentials: &Option<std::sync::Arc<dyn CredentialsProvider>>,
) -> ServiceEndpoint {
    if let Some(credentials) = credentials {
        endpoint.credentials = Some(credentials.clone());
    }
    endpoint
}