runagent 0.1.49

RunAgent SDK for Rust - Client SDK for interacting with deployed AI agents
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
//! Main RunAgent client for interacting with deployed agents

use crate::client::rest_client::RestClient;
use crate::client::socket_client::SocketClient;
use crate::types::{RunAgentError, RunAgentResult};
use crate::utils::serializer::CoreSerializer;
use futures::Stream;
use serde_json::Value;
use std::collections::HashMap;
use std::pin::Pin;

#[cfg(feature = "db")]
use crate::db::DatabaseService;

/// Main client for interacting with RunAgent deployments
pub struct RunAgentClient {
    agent_id: String,
    entrypoint_tag: String,
    local: bool,
    rest_client: RestClient,
    socket_client: SocketClient,
    serializer: CoreSerializer,
    agent_architecture: Option<Value>,
    extra_params: Option<HashMap<String, Value>>,

    /// User ID for persistent memory (matches Python SDK RunAgentClient.user_id)
    user_id: Option<String>,
    /// Enable persistent memory for this user (matches Python SDK RunAgentClient.persistent_memory)
    persistent_memory: bool,

    #[cfg(feature = "db")]
    #[allow(dead_code)] // Reserved for future use
    db_service: Option<DatabaseService>,
}

/// Configuration for creating a RunAgent client
///
/// All fields except `agent_id` and `entrypoint_tag` are optional.
///
/// # Direct Construction
///
/// ```rust,no_run
/// use runagent::{RunAgentClient, RunAgentClientConfig};
///
/// #[tokio::main]
/// async fn main() -> runagent::RunAgentResult<()> {
///     let client = RunAgentClient::new(RunAgentClientConfig {
///         agent_id: "agent-id".to_string(),
///         entrypoint_tag: "entrypoint".to_string(),
///         local: None,
///         host: None,
///         port: None,
///         api_key: Some("key".to_string()),
///         base_url: Some("http://localhost:8333/".to_string()),
///         extra_params: None,
///         enable_registry: None,
///         user_id: None,
///         persistent_memory: None,
///     }).await?;
///     Ok(())
/// }
/// ```
///
/// # Builder Pattern (Alternative)
///
/// ```rust,no_run
/// use runagent::{RunAgentClient, RunAgentClientConfig};
/// use std::env;
///
/// #[tokio::main]
/// async fn main() -> runagent::RunAgentResult<()> {
///     let client = RunAgentClient::new(
///         RunAgentClientConfig::new("agent-id", "entrypoint")
///             .with_api_key(env::var("RUNAGENT_API_KEY").unwrap_or_else(|_| "key".to_string()))
///             .with_base_url("http://localhost:8333/")
///     ).await?;
///     Ok(())
/// }
/// ```
#[derive(Debug, Clone)]
pub struct RunAgentClientConfig {
    /// Agent ID (required)
    pub agent_id: String,
    /// Entrypoint tag (required)
    pub entrypoint_tag: String,
    /// Whether this is a local agent (default: false)
    pub local: Option<bool>,
    /// Host for local agents (optional, will lookup from DB if not provided and local=true)
    pub host: Option<String>,
    /// Port for local agents (optional, will lookup from DB if not provided and local=true)
    pub port: Option<u16>,
    /// API key for remote agents (optional, can also use RUNAGENT_API_KEY env var)
    pub api_key: Option<String>,
    /// Base URL for remote agents (optional, defaults to https://backend.run-agent.ai)
    pub base_url: Option<String>,
    /// Extra parameters for future use
    pub extra_params: Option<HashMap<String, Value>>,
    /// Enable database registry lookup (default: true for local agents)
    pub enable_registry: Option<bool>,
    /// User ID for persistent memory
    pub user_id: Option<String>,
    /// Enable persistent memory for this user
    pub persistent_memory: Option<bool>,
}

#[allow(clippy::derivable_impls)]
impl Default for RunAgentClientConfig {
    fn default() -> Self {
        Self {
            agent_id: String::new(),       // Dummy - will be overridden
            entrypoint_tag: String::new(), // Dummy - will be overridden
            local: None,
            host: None,
            port: None,
            api_key: None,
            base_url: None,
            extra_params: None,
            enable_registry: None,
            user_id: None,
            persistent_memory: None,
        }
    }
}

impl RunAgentClientConfig {
    /// Create a new config with required fields
    pub fn new(agent_id: impl Into<String>, entrypoint_tag: impl Into<String>) -> Self {
        Self {
            agent_id: agent_id.into(),
            entrypoint_tag: entrypoint_tag.into(),
            local: None,
            host: None,
            port: None,
            api_key: None,
            base_url: None,
            extra_params: None,
            enable_registry: None,
            user_id: None,
            persistent_memory: None,
        }
    }

    /// Set local flag
    pub fn with_local(mut self, local: bool) -> Self {
        self.local = Some(local);
        self
    }

    /// Set host and port for local agents
    pub fn with_address(mut self, host: impl Into<String>, port: u16) -> Self {
        self.host = Some(host.into());
        self.port = Some(port);
        self
    }

    /// Set API key
    pub fn with_api_key(mut self, api_key: impl Into<String>) -> Self {
        self.api_key = Some(api_key.into());
        self
    }

    /// Set base URL
    pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
        self.base_url = Some(base_url.into());
        self
    }

    /// Set extra parameters
    pub fn with_extra_params(mut self, extra_params: HashMap<String, Value>) -> Self {
        self.extra_params = Some(extra_params);
        self
    }

    /// Enable or disable registry lookup
    pub fn with_enable_registry(mut self, enable: bool) -> Self {
        self.enable_registry = Some(enable);
        self
    }

    /// Set user ID for persistent memory
    pub fn with_user_id(mut self, user_id: impl Into<String>) -> Self {
        self.user_id = Some(user_id.into());
        self
    }

    /// Enable or disable persistent memory for this user
    pub fn with_persistent_memory(mut self, persistent: bool) -> Self {
        self.persistent_memory = Some(persistent);
        self
    }
}

impl RunAgentClient {
    /// Create a new RunAgent client from configuration
    ///
    /// This is the single entry point for creating clients.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use runagent::{RunAgentClient, RunAgentClientConfig};
    /// use std::env;
    ///
    /// #[tokio::main]
    /// async fn main() -> runagent::RunAgentResult<()> {
    ///     // Local agent with explicit address
    ///     let client = RunAgentClient::new(RunAgentClientConfig::new("agent-id", "entrypoint")
    ///         .with_local(true)
    ///         .with_address("127.0.0.1", 8450)
    ///         .with_enable_registry(false)
    ///     ).await?;
    ///     
    ///     // Remote agent
    ///     let client = RunAgentClient::new(RunAgentClientConfig::new("agent-id", "entrypoint")
    ///         .with_api_key(env::var("RUNAGENT_API_KEY").unwrap_or_else(|_| "key".to_string()))
    ///     ).await?;
    ///     Ok(())
    /// }
    /// ```
    pub async fn new(config: RunAgentClientConfig) -> RunAgentResult<Self> {
        use crate::constants::{DEFAULT_BASE_URL, ENV_RUNAGENT_API_KEY, ENV_RUNAGENT_BASE_URL};

        let local = config.local.unwrap_or(false);
        let enable_registry = config.enable_registry.unwrap_or(local);

        // Resolve host/port for local agents
        let (host, port) = if local {
            // If host/port provided, use them
            if let (Some(h), Some(p)) = (&config.host, &config.port) {
                (Some(h.clone()), Some(*p))
            } else if enable_registry {
                // Try database lookup if enabled
                #[cfg(feature = "db")]
                {
                    let db_service = DatabaseService::new(None).await?;
                    if let Some(agent_info) = db_service.get_agent(&config.agent_id).await? {
                        tracing::info!(
                            "🔍 Found agent in database: {}:{}",
                            agent_info.host,
                            agent_info.port
                        );
                        (Some(agent_info.host), Some(agent_info.port as u16))
                    } else {
                        (config.host.clone(), config.port)
                    }
                }
                #[cfg(not(feature = "db"))]
                {
                    (config.host.clone(), config.port)
                }
            } else {
                (config.host.clone(), config.port)
            }
        } else {
            (None, None)
        };

        // Resolve API key (config > env var)
        let api_key = config
            .api_key
            .or_else(|| std::env::var(ENV_RUNAGENT_API_KEY).ok());

        // Resolve base URL (config > env var > default)
        let base_url = config
            .base_url
            .or_else(|| std::env::var(ENV_RUNAGENT_BASE_URL).ok())
            .unwrap_or_else(|| DEFAULT_BASE_URL.to_string());

        if !local {
            tracing::info!("🌐 Connecting to remote agent at {}", base_url);
            if api_key.is_some() {
                tracing::debug!("🔑 API key provided");
            } else {
                tracing::warn!("⚠️  No API key provided - using default limits");
            }
        }

        let serializer = CoreSerializer::new(10.0)?;
        #[cfg(feature = "db")]
        let db_service: Option<DatabaseService> = None;
        #[cfg(not(feature = "db"))]
        let db_service: Option<DatabaseService> = None;

        let (rest_client, socket_client) = if local {
            let host = host.ok_or_else(|| {
                RunAgentError::validation(
                    "Host is required for local clients. Provide host/port in config or enable registry for database lookup.",
                )
            })?;
            let port = port.ok_or_else(|| {
                RunAgentError::validation(
                    "Port is required for local clients. Provide host/port in config or enable registry for database lookup.",
                )
            })?;

            tracing::info!("🔌 Using address: {}:{}", host, port);

            let agent_base_url = format!("http://{}:{}", host, port);
            let agent_socket_url = format!("ws://{}:{}", host, port);

            let rest_client = RestClient::new(&agent_base_url, None, Some("/api/v1"))?;
            let socket_client = SocketClient::new(&agent_socket_url, None, Some("/api/v1"))?;

            (rest_client, socket_client)
        } else {
            Self::create_remote_clients(Some(&base_url), api_key)?
        };

        let mut client = Self {
            agent_id: config.agent_id,
            entrypoint_tag: config.entrypoint_tag,
            local,
            rest_client,
            socket_client,
            serializer,
            agent_architecture: None,
            extra_params: config.extra_params,
            user_id: config.user_id,
            persistent_memory: config.persistent_memory.unwrap_or(false),

            #[cfg(feature = "db")]
            db_service,
        };

        client.initialize_architecture().await?;

        Ok(client)
    }

    async fn initialize_architecture(&mut self) -> RunAgentResult<()> {
        let architecture = self.get_agent_architecture_internal().await?;
        self.agent_architecture = Some(architecture);
        self.validate_entrypoint()?;
        Ok(())
    }

    async fn get_agent_architecture_internal(&self) -> RunAgentResult<Value> {
        self.rest_client
            .get_agent_architecture(&self.agent_id)
            .await
    }

    fn validate_entrypoint(&self) -> RunAgentResult<()> {
        if let Some(ref architecture) = self.agent_architecture {
            if let Some(entrypoints) = architecture.get("entrypoints").and_then(|e| e.as_array()) {
                let found = entrypoints.iter().any(|ep| {
                    ep.get("tag")
                        .and_then(|t| t.as_str())
                        .map(|t| t == self.entrypoint_tag)
                        .unwrap_or(false)
                });

                if !found {
                    let available: Vec<String> = entrypoints
                        .iter()
                        .filter_map(|ep| ep.get("tag").and_then(|t| t.as_str()))
                        .map(|s| s.to_string())
                        .collect();
                    tracing::error!(
                        "Entrypoint `{}` not found for agent {}. Available: {:?}",
                        self.entrypoint_tag,
                        self.agent_id,
                        available
                    );
                    return Err(RunAgentError::validation(format!(
                        "Entrypoint `{}` not found in agent {}",
                        self.entrypoint_tag, self.agent_id
                    )));
                }
            }
        }
        Ok(())
    }

    /// Run the agent with keyword arguments only
    pub async fn run(&self, input_kwargs: &[(&str, Value)]) -> RunAgentResult<Value> {
        self.run_with_args(&[], input_kwargs).await
    }

    /// Run the agent with the given input
    pub async fn run_with_args(
        &self,
        input_args: &[Value],
        input_kwargs: &[(&str, Value)],
    ) -> RunAgentResult<Value> {
        if self.entrypoint_tag.ends_with("_stream") {
            return Err(RunAgentError::validation(
                "Use run_stream for streaming entrypoints".to_string(),
            ));
        }

        let input_kwargs_map: HashMap<String, Value> = input_kwargs
            .iter()
            .map(|(k, v)| (k.to_string(), v.clone()))
            .collect();

        let response = self
            .rest_client
            .run_agent(
                &self.agent_id,
                &self.entrypoint_tag,
                input_args,
                &input_kwargs_map,
                self.user_id.as_deref(),
                self.persistent_memory,
            )
            .await?;

        if response
            .get("success")
            .and_then(|s| s.as_bool())
            .unwrap_or(false)
        {
            // Process response data
            let mut payload: Option<Value> = None;

            if let Some(data) = response.get("data") {
                // Case 1: data is a string (simplified payload - could be JSON string with {type, payload})
                if data.as_str().is_some() {
                    // Check for generator object BEFORE processing (case-insensitive)
                    if let Some(data_str) = data.as_str() {
                        let lower_str = data_str.to_lowercase();
                        if lower_str.contains("generator object")
                            || lower_str.contains("<generator")
                        {
                            let streaming_tag = format!("{}_stream", self.entrypoint_tag);
                            return Err(RunAgentError::validation(format!(
                                "Agent returned a generator object instead of content. This entrypoint appears to be a streaming function.\n\
                                Try using the streaming endpoint: `{}`\n\
                                Or use `run_stream()` method instead of `run()`.",
                                streaming_tag
                            )));
                        }
                    }
                    // Use common deserializer preparation logic
                    let prepared = self.serializer.prepare_for_deserialization(data.clone());
                    payload = Some(prepared);
                }
                // Case 2: data has result_data.data (legacy detailed execution payload)
                else if let Some(result_data) = data.get("result_data") {
                    if let Some(output_data) = result_data.get("data") {
                        // Check for generator object in nested data (case-insensitive)
                        if let Some(output_str) = output_data.as_str() {
                            let lower_str = output_str.to_lowercase();
                            if lower_str.contains("generator object")
                                || lower_str.contains("<generator")
                            {
                                let streaming_tag = format!("{}_stream", self.entrypoint_tag);
                                return Err(RunAgentError::validation(format!(
                                    "Agent returned a generator object instead of content. This entrypoint appears to be a streaming function.\n\
                                    Try using the streaming endpoint: `{}`\n\
                                    Or use `run_stream()` method instead of `run()`.",
                                    streaming_tag
                                )));
                            }
                        }
                        payload = Some(output_data.clone());
                    }
                }
                // Case 3: data is an object (could be {type, payload} structure)
                else if data.is_object() {
                    payload = Some(data.clone());
                }
            }
            // Case 4: Fallback to output_data (backward compatibility)
            else if let Some(output_data) = response.get("output_data") {
                // Check for generator object in output_data (case-insensitive)
                if let Some(output_str) = output_data.as_str() {
                    let lower_str = output_str.to_lowercase();
                    if lower_str.contains("generator object") || lower_str.contains("<generator") {
                        let streaming_tag = format!("{}_stream", self.entrypoint_tag);
                        return Err(RunAgentError::validation(format!(
                            "Agent returned a generator object instead of content. This entrypoint appears to be a streaming function.\n\
                            Try using the streaming endpoint: `{}`\n\
                            Or use `run_stream()` method instead of `run()`.",
                            streaming_tag
                        )));
                    }
                }
                payload = Some(output_data.clone());
            }

            // Deserialize the payload using serializer (handles {type, payload} structure)
            if let Some(payload_val) = payload {
                // Check for generator object warning (case-insensitive, after deserialization)
                if let Some(content_str) = payload_val.as_str() {
                    let lower_str = content_str.to_lowercase();
                    if lower_str.contains("generator object") || lower_str.contains("<generator") {
                        // Check if there's a streaming version of this entrypoint
                        let streaming_tag = format!("{}_stream", self.entrypoint_tag);
                        return Err(RunAgentError::validation(format!(
                            "Agent returned a generator object instead of content. This entrypoint appears to be a streaming function.\n\
                            Try using the streaming endpoint: `{}`\n\
                            Or use `run_stream()` method instead of `run()`.",
                            streaming_tag
                        )));
                    }
                }
                // Deserialize the payload - this should extract payload from {type, payload} structure
                let deserialized = self.serializer.deserialize_object(payload_val)?;
                return Ok(deserialized);
            }
            Ok(Value::Null)
        } else {
            // Handle new error format with ErrorDetail object (matching Python SDK)
            if let Some(error_info) = response.get("error") {
                if let Some(error_obj) = error_info.as_object() {
                    if let (Some(message), Some(code)) = (
                        error_obj.get("message").and_then(|m| m.as_str()),
                        error_obj.get("code").and_then(|c| c.as_str()),
                    ) {
                        return Err(RunAgentError::server(format!("[{}] {}", code, message)));
                    }
                }
                // Fallback to old format
                if let Some(error_msg) = error_info.as_str() {
                    return Err(RunAgentError::server(error_msg));
                }
            }
            Err(RunAgentError::server("Unknown error"))
        }
    }

    /// Run the agent and return a stream of responses
    pub async fn run_stream(
        &self,
        input_kwargs: &[(&str, Value)],
    ) -> RunAgentResult<Pin<Box<dyn Stream<Item = RunAgentResult<Value>> + Send>>> {
        self.run_stream_with_args(&[], input_kwargs).await
    }

    /// Run the agent with streaming and both positional and keyword arguments
    pub async fn run_stream_with_args(
        &self,
        input_args: &[Value],
        input_kwargs: &[(&str, Value)],
    ) -> RunAgentResult<Pin<Box<dyn Stream<Item = RunAgentResult<Value>> + Send>>> {
        if !self.entrypoint_tag.ends_with("_stream") {
            return Err(RunAgentError::validation(
                "Use run() for non-stream entrypoints".to_string(),
            ));
        }

        let input_kwargs_map: HashMap<String, Value> = input_kwargs
            .iter()
            .map(|(k, v)| (k.to_string(), v.clone()))
            .collect();

        self.socket_client
            .run_stream(
                &self.agent_id,
                &self.entrypoint_tag,
                input_args,
                &input_kwargs_map,
                self.user_id.as_deref(),
                self.persistent_memory,
            )
            .await
    }

    /// Get the agent's architecture information
    pub async fn get_agent_architecture(&self) -> RunAgentResult<Value> {
        self.rest_client
            .get_agent_architecture(&self.agent_id)
            .await
    }

    /// Check if the agent is available
    pub async fn health_check(&self) -> RunAgentResult<bool> {
        match self.rest_client.health_check().await {
            Ok(_) => Ok(true),
            Err(_) => Ok(false),
        }
    }

    /// Get agent information
    pub fn agent_id(&self) -> &str {
        &self.agent_id
    }

    /// Get entrypoint tag
    pub fn entrypoint_tag(&self) -> &str {
        &self.entrypoint_tag
    }

    /// Get any extra params supplied during initialization
    pub fn extra_params(&self) -> Option<&HashMap<String, Value>> {
        self.extra_params.as_ref()
    }

    /// Get user ID used for persistent memory, if any
    pub fn user_id(&self) -> Option<&str> {
        self.user_id.as_deref()
    }

    /// Check if persistent memory is enabled for this client
    pub fn persistent_memory(&self) -> bool {
        self.persistent_memory
    }

    /// Check if using local deployment
    pub fn is_local(&self) -> bool {
        self.local
    }
}

impl RunAgentClient {
    fn create_remote_clients(
        base_url_override: Option<&str>,
        api_key_override: Option<String>,
    ) -> RunAgentResult<(RestClient, SocketClient)> {
        if let Some(base_url) = base_url_override {
            let rest_client = RestClient::new(base_url, api_key_override.clone(), Some("/api/v1"))?;
            let socket_base = if base_url.starts_with("https://") {
                base_url.replace("https://", "wss://")
            } else if base_url.starts_with("http://") {
                base_url.replace("http://", "ws://")
            } else {
                format!("wss://{}", base_url)
            };
            let socket_client = SocketClient::new(&socket_base, api_key_override, Some("/api/v1"))?;
            Ok((rest_client, socket_client))
        } else {
            let rest_client = RestClient::default()?;
            let socket_client = SocketClient::default()?;
            Ok((rest_client, socket_client))
        }
    }
}