watsonx_rs/orchestrate/
agent.rs

1//! Agent management operations
2
3use crate::error::{Error, Result};
4use super::types::Agent;
5use super::OrchestrateClient;
6
7impl OrchestrateClient {
8    /// List all agents (Watson Orchestrate API)
9    pub async fn list_agents(&self) -> Result<Vec<Agent>> {
10        let token = self.access_token.as_ref().ok_or_else(|| {
11            Error::Authentication("Not authenticated. Set access token (Bearer token) first.".to_string())
12        })?;
13
14        let base_url = self.config.get_base_url();
15        
16        // Try different endpoint paths
17        let endpoints = vec![
18            format!("{}/agents", base_url),
19            format!("{}/orchestrate/agents", base_url),
20            format!("{}/assistants", base_url),
21            format!("{}/orchestrate/assistants", base_url),
22        ];
23
24        for url in endpoints {
25            let response = self
26                .client
27                .get(&url)
28                .header("Authorization", format!("Bearer {}", token))
29                .header("Content-Type", "application/json")
30                .header("X-Instance-ID", &self.config.instance_id)
31                .send()
32                .await
33                .map_err(|e| Error::Network(e.to_string()))?;
34
35            if response.status().is_success() {
36                // Parse the JSON array response directly
37                let agents: Vec<Agent> = response
38                    .json()
39                    .await
40                    .map_err(|e| Error::Serialization(e.to_string()))?;
41                return Ok(agents);
42            }
43        }
44
45        // If all endpoints failed, return error with diagnostic info
46        Err(Error::Api(format!(
47            "Failed to list agents: All endpoint paths returned 404. Tried: {}/agents, {}/orchestrate/agents, {}/assistants, {}/orchestrate/assistants",
48            base_url, base_url, base_url, base_url
49        )))
50    }
51
52    /// Get a specific agent by ID
53    pub async fn get_agent(&self, agent_id: &str) -> Result<Agent> {
54        let api_key = self.access_token.as_ref().ok_or_else(|| {
55            Error::Authentication("Not authenticated. Set access token (API key) first.".to_string())
56        })?;
57
58        let base_url = self.config.get_base_url();
59        let url = format!("{}/agents/{}", base_url, agent_id);
60
61        let response = self
62            .client
63            .get(&url)
64            .header("Authorization", format!("Bearer {}", api_key))
65            .header("Content-Type", "application/json")
66            .send()
67            .await
68            .map_err(|e| Error::Network(e.to_string()))?;
69
70        if !response.status().is_success() {
71            let status = response.status();
72            let error_text = response
73                .text()
74                .await
75                .unwrap_or_else(|_| "Unknown error".to_string());
76            return Err(Error::Api(format!(
77                "Failed to get agent {}: {} - {}",
78                agent_id, status, error_text
79            )));
80        }
81
82        let agent: Agent = response
83            .json()
84            .await
85            .map_err(|e| Error::Serialization(e.to_string()))?;
86
87        Ok(agent)
88    }
89}