restflow_core/services/
agent.rs

1use crate::{AppCore, node::agent::AgentNode, storage::agent::StoredAgent};
2use anyhow::{Context, Result};
3use std::sync::Arc;
4
5pub async fn list_agents(core: &Arc<AppCore>) -> Result<Vec<StoredAgent>> {
6    core.storage
7        .agents
8        .list_agents()
9        .context("Failed to list agents")
10}
11
12pub async fn get_agent(core: &Arc<AppCore>, id: &str) -> Result<StoredAgent> {
13    core.storage
14        .agents
15        .get_agent(id.to_string())
16        .with_context(|| format!("Failed to get agent {}", id))?
17        .ok_or_else(|| anyhow::anyhow!("Agent {} not found", id))
18}
19
20pub async fn create_agent(
21    core: &Arc<AppCore>,
22    name: String,
23    agent: AgentNode,
24) -> Result<StoredAgent> {
25    core.storage
26        .agents
27        .create_agent(name.clone(), agent)
28        .with_context(|| format!("Failed to create agent {}", name))
29}
30
31pub async fn update_agent(
32    core: &Arc<AppCore>,
33    id: &str,
34    name: Option<String>,
35    agent: Option<AgentNode>,
36) -> Result<StoredAgent> {
37    core.storage
38        .agents
39        .update_agent(id.to_string(), name, agent)
40        .with_context(|| format!("Failed to update agent {}", id))
41}
42
43pub async fn delete_agent(core: &Arc<AppCore>, id: &str) -> Result<()> {
44    core.storage
45        .agents
46        .delete_agent(id.to_string())
47        .with_context(|| format!("Failed to delete agent {}", id))
48}
49
50pub async fn execute_agent(core: &Arc<AppCore>, id: &str, input: &str) -> Result<String> {
51    let stored_agent = get_agent(core, id).await?;
52
53    stored_agent
54        .agent
55        .execute(input, Some(&core.storage.secrets))
56        .await
57        .with_context(|| format!("Failed to execute agent {}", id))
58}
59
60pub async fn execute_agent_inline(core: &AppCore, agent: AgentNode, input: &str) -> Result<String> {
61    agent
62        .execute(input, Some(&core.storage.secrets))
63        .await
64        .context("Failed to execute inline agent")
65}