watsonx_rs/orchestrate/
run.rs1use crate::error::{Error, Result};
4use super::types::RunInfo;
5use super::OrchestrateClient;
6
7impl OrchestrateClient {
8 pub async fn get_run(&self, run_id: &str) -> Result<RunInfo> {
10 let api_key = self.access_token.as_ref().ok_or_else(|| {
11 Error::Authentication("Not authenticated. Set access token (API key) first.".to_string())
12 })?;
13
14 let base_url = self.config.get_base_url();
15 let url = format!("{}/runs/{}", base_url, run_id);
16
17 let response = self
18 .client
19 .get(&url)
20 .header("Authorization", format!("Bearer {}", api_key))
21 .header("Content-Type", "application/json")
22 .send()
23 .await
24 .map_err(|e| Error::Network(e.to_string()))?;
25
26 if !response.status().is_success() {
27 let status = response.status();
28 let error_text = response
29 .text()
30 .await
31 .unwrap_or_else(|_| "Unknown error".to_string());
32 return Err(Error::Api(format!(
33 "Failed to get run {}: {} - {}",
34 run_id, status, error_text
35 )));
36 }
37
38 let run: RunInfo = response
39 .json()
40 .await
41 .map_err(|e| Error::Serialization(e.to_string()))?;
42
43 Ok(run)
44 }
45
46 pub async fn list_runs(&self, agent_id: Option<&str>) -> Result<Vec<RunInfo>> {
48 let api_key = self.access_token.as_ref().ok_or_else(|| {
49 Error::Authentication("Not authenticated. Set access token (API key) first.".to_string())
50 })?;
51
52 let base_url = self.config.get_base_url();
53 let url = if let Some(agent_id) = agent_id {
54 format!("{}/runs?agent_id={}", base_url, agent_id)
55 } else {
56 format!("{}/runs", base_url)
57 };
58
59 let response = self
60 .client
61 .get(&url)
62 .header("Authorization", format!("Bearer {}", api_key))
63 .header("Content-Type", "application/json")
64 .send()
65 .await
66 .map_err(|e| Error::Network(e.to_string()))?;
67
68 if !response.status().is_success() {
69 let status = response.status();
70 let error_text = response
71 .text()
72 .await
73 .unwrap_or_else(|_| "Unknown error".to_string());
74 return Err(Error::Api(format!(
75 "Failed to list runs: {} - {}",
76 status, error_text
77 )));
78 }
79
80 let text = response
81 .text()
82 .await
83 .map_err(|e| Error::Serialization(e.to_string()))?;
84
85 if let Ok(runs) = serde_json::from_str::<Vec<RunInfo>>(&text) {
86 return Ok(runs);
87 }
88
89 if let Ok(obj) = serde_json::from_str::<serde_json::Value>(&text) {
90 if let Some(runs_array) = obj.get("runs").and_then(|r| r.as_array()) {
91 let runs: Result<Vec<RunInfo>> = runs_array
92 .iter()
93 .map(|run| {
94 serde_json::from_value::<RunInfo>(run.clone())
95 .map_err(|e| Error::Serialization(e.to_string()))
96 })
97 .collect();
98 return runs;
99 }
100 }
101
102 Ok(Vec::new())
103 }
104
105 pub async fn cancel_run(&self, run_id: &str) -> Result<()> {
107 let api_key = self.access_token.as_ref().ok_or_else(|| {
108 Error::Authentication("Not authenticated. Set access token (API key) first.".to_string())
109 })?;
110
111 let base_url = self.config.get_base_url();
112 let url = format!("{}/runs/{}/cancel", base_url, run_id);
113
114 let response = self
115 .client
116 .post(&url)
117 .header("Authorization", format!("Bearer {}", api_key))
118 .header("Content-Type", "application/json")
119 .send()
120 .await
121 .map_err(|e| Error::Network(e.to_string()))?;
122
123 if !response.status().is_success() {
124 let status = response.status();
125 let error_text = response
126 .text()
127 .await
128 .unwrap_or_else(|_| "Unknown error".to_string());
129 return Err(Error::Api(format!(
130 "Failed to cancel run {}: {} - {}",
131 run_id, status, error_text
132 )));
133 }
134
135 Ok(())
136 }
137}