use std::collections::HashMap;
use std::sync::Arc;
use crate::error::{map_task_automation_err as map_err, ManagerError};
use crate::gen::task_automation::apis::configuration::Configuration;
use crate::gen::task_automation::apis::{
account_configuration_api as cfg_api, action_test_api, agent_api, logs_api, manager_api,
metrics_api, scripts_api, secrets_api, usage_api,
};
use crate::gen::task_automation::models;
use crate::retry::{with_retry, RetryPolicy};
pub struct TasksResource {
pub(crate) cfg: Arc<Configuration>,
pub(crate) retry: RetryPolicy,
pub scripts: TaskScriptsResource,
pub secrets: TaskSecretsResource,
pub selection_config: TaskSelectionConfigResource,
pub metrics: TaskMetricsResource,
}
impl TasksResource {
pub async fn list(&self, filter: Option<&str>) -> Result<models::TaskList, ManagerError> {
with_retry(&self.retry, true, || {
manager_api::tasks(self.cfg.as_ref(), filter, None, None)
})
.await
.map_err(map_err)
}
pub async fn create(&self, body: models::SubmitTask) -> Result<models::Task, ManagerError> {
with_retry(&self.retry, false, || {
manager_api::submit_task(self.cfg.as_ref(), body.clone())
})
.await
.map_err(map_err)
}
pub async fn create_from_template(
&self,
template: &str,
variables: serde_json::Value,
) -> Result<models::Task, ManagerError> {
with_retry(&self.retry, false, || {
manager_api::submit_task_template(self.cfg.as_ref(), template, variables.clone(), None)
})
.await
.map_err(map_err)
}
pub async fn get(&self, task_id: &str) -> Result<models::Task, ManagerError> {
with_retry(&self.retry, true, || {
manager_api::task(self.cfg.as_ref(), task_id)
})
.await
.map_err(map_err)
}
pub async fn update(
&self,
task_id: &str,
body: models::Task,
) -> Result<models::Task, ManagerError> {
with_retry(&self.retry, false, || {
manager_api::update_task(self.cfg.as_ref(), task_id, body.clone())
})
.await
.map_err(map_err)
}
pub async fn interrupt(
&self,
task_id: &str,
interrupt_to: models::InterruptionTargetStates,
action: Option<models::ManualActionRequest>,
) -> Result<(), ManagerError> {
with_retry(&self.retry, false, || {
manager_api::manager_interrupt_on_task(
self.cfg.as_ref(),
task_id,
interrupt_to,
action.clone(),
)
})
.await
.map_err(map_err)
}
#[allow(deprecated)]
pub async fn change_state(
&self,
task_id: &str,
state: models::TaskState,
) -> Result<(), ManagerError> {
with_retry(&self.retry, false, || {
manager_api::change_task_state(self.cfg.as_ref(), task_id, state)
})
.await
.map_err(map_err)
}
pub async fn agent_action(
&self,
task_id: &str,
action: models::AgentActions,
manual_action: Option<models::ManualActionRequest>,
) -> Result<models::Task, ManagerError> {
with_retry(&self.retry, false, || {
agent_api::agent_action_on_task(
self.cfg.as_ref(),
task_id,
action,
manual_action.clone(),
)
})
.await
.map_err(map_err)
}
pub async fn set_agent_lock(
&self,
lock_state: models::AgentLocking,
) -> Result<models::AgentLockState, ManagerError> {
with_retry(&self.retry, false, || {
agent_api::change_agent_lock(self.cfg.as_ref(), lock_state)
})
.await
.map_err(map_err)
}
pub async fn usage(&self) -> Result<models::TaskTimeSeries, ManagerError> {
with_retry(&self.retry, true, || {
usage_api::task_usage(self.cfg.as_ref(), None, None, None, None)
})
.await
.map_err(map_err)
}
pub async fn usage_types(&self) -> Result<Vec<String>, ManagerError> {
with_retry(&self.retry, true, || {
usage_api::task_usage_types(self.cfg.as_ref(), None, None)
})
.await
.map_err(map_err)
}
pub async fn logs(&self) -> Result<models::LogsResponse, ManagerError> {
with_retry(&self.retry, true, || {
logs_api::list(self.cfg.as_ref(), None, None, None, None, None, None)
})
.await
.map_err(map_err)
}
pub async fn test_action(
&self,
body: models::TestAction,
) -> Result<models::TestActionResult, ManagerError> {
with_retry(&self.retry, false, || {
action_test_api::testing(self.cfg.as_ref(), body.clone())
})
.await
.map_err(map_err)
}
}
pub struct TaskScriptsResource {
pub(crate) cfg: Arc<Configuration>,
pub(crate) retry: RetryPolicy,
}
impl TaskScriptsResource {
pub async fn list(
&self,
script_type: models::ScriptType,
) -> Result<models::ScriptList, ManagerError> {
with_retry(&self.retry, true, || {
scripts_api::list_scripts(self.cfg.as_ref(), script_type, None, None, None)
})
.await
.map_err(map_err)
}
pub async fn get(
&self,
script_type: models::ScriptType,
code_id: &str,
) -> Result<models::ScriptResponses, ManagerError> {
with_retry(&self.retry, true, || {
scripts_api::get_script(self.cfg.as_ref(), code_id, script_type, None)
})
.await
.map_err(map_err)
}
pub async fn submit(
&self,
script_type: models::ScriptType,
body: models::Script,
) -> Result<models::ScriptResponses, ManagerError> {
with_retry(&self.retry, false, || {
scripts_api::submit_script(self.cfg.as_ref(), script_type, body.clone())
})
.await
.map_err(map_err)
}
pub async fn update(
&self,
script_type: models::ScriptType,
code_id: &str,
body: models::Script,
) -> Result<models::ScriptResponses, ManagerError> {
with_retry(&self.retry, false, || {
scripts_api::update_script(self.cfg.as_ref(), code_id, script_type, body.clone())
})
.await
.map_err(map_err)
}
pub async fn delete(
&self,
script_type: models::ScriptType,
code_id: &str,
) -> Result<(), ManagerError> {
with_retry(&self.retry, false, || {
scripts_api::delete_script(self.cfg.as_ref(), code_id, script_type)
})
.await
.map_err(map_err)
}
}
pub struct TaskSecretsResource {
pub(crate) cfg: Arc<Configuration>,
pub(crate) retry: RetryPolicy,
}
impl TaskSecretsResource {
pub async fn list_prefixes(&self) -> Result<Vec<String>, ManagerError> {
with_retry(&self.retry, true, || {
secrets_api::list_secret_prefixes(self.cfg.as_ref())
})
.await
.map_err(map_err)
}
pub async fn list_keys(&self, prefix: &str) -> Result<Vec<String>, ManagerError> {
with_retry(&self.retry, true, || {
secrets_api::list_secret_keys(self.cfg.as_ref(), prefix)
})
.await
.map_err(map_err)
}
pub async fn create(
&self,
prefix: &str,
secrets: HashMap<String, serde_json::Value>,
) -> Result<(), ManagerError> {
with_retry(&self.retry, false, || {
secrets_api::create_secrets(self.cfg.as_ref(), prefix, Some(secrets.clone()))
})
.await
.map_err(map_err)
}
pub async fn patch(
&self,
prefix: &str,
secrets: HashMap<String, serde_json::Value>,
) -> Result<(), ManagerError> {
with_retry(&self.retry, false, || {
secrets_api::patch_secrets(self.cfg.as_ref(), prefix, Some(secrets.clone()))
})
.await
.map_err(map_err)
}
pub async fn delete_keys(&self, prefix: &str, keys: Vec<String>) -> Result<(), ManagerError> {
with_retry(&self.retry, false, || {
secrets_api::delete_secret_keys(self.cfg.as_ref(), prefix, Some(keys.clone()))
})
.await
.map_err(map_err)
}
}
pub struct TaskSelectionConfigResource {
pub(crate) cfg: Arc<Configuration>,
pub(crate) retry: RetryPolicy,
}
impl TaskSelectionConfigResource {
pub async fn read(&self) -> Result<models::AccountSelectionConfiguration, ManagerError> {
with_retry(&self.retry, true, || {
cfg_api::read_selection_configuration(self.cfg.as_ref())
})
.await
.map_err(map_err)
}
pub async fn create(
&self,
body: models::AccountSelectionConfiguration,
) -> Result<models::AccountSelectionConfiguration, ManagerError> {
with_retry(&self.retry, false, || {
cfg_api::create_selection_configuration(self.cfg.as_ref(), Some(body.clone()))
})
.await
.map_err(map_err)
}
pub async fn update(
&self,
body: models::AccountSelectionConfiguration,
) -> Result<models::AccountSelectionConfiguration, ManagerError> {
with_retry(&self.retry, false, || {
cfg_api::update_selection_configuration(self.cfg.as_ref(), Some(body.clone()))
})
.await
.map_err(map_err)
}
pub async fn delete(&self) -> Result<(), ManagerError> {
with_retry(&self.retry, false, || {
cfg_api::delete_selection_configuration(self.cfg.as_ref())
})
.await
.map_err(map_err)
}
}
pub struct TaskMetricsResource {
pub(crate) cfg: Arc<Configuration>,
pub(crate) retry: RetryPolicy,
}
impl TaskMetricsResource {
pub async fn task_journal(&self, task_id: &str) -> Result<models::TaskJournal, ManagerError> {
with_retry(&self.retry, true, || {
metrics_api::task_journal(self.cfg.as_ref(), task_id)
})
.await
.map_err(map_err)
}
pub async fn agent_journal(
&self,
agent_id: &str,
) -> Result<models::AgentJournal, ManagerError> {
with_retry(&self.retry, true, || {
metrics_api::agent_interactions(self.cfg.as_ref(), agent_id)
})
.await
.map_err(map_err)
}
pub async fn agent_interaction_durations(
&self,
agent_id: &str,
) -> Result<models::InteractionDurations, ManagerError> {
with_retry(&self.retry, true, || {
metrics_api::agent_interaction_duration(self.cfg.as_ref(), agent_id)
})
.await
.map_err(map_err)
}
}