actflow_agent_sdk/agent.rs
1//! Agent trait definition.
2
3use std::future::Future;
4
5use tokio::sync::mpsc;
6
7use crate::types::{AgentOutput, Inputs};
8
9/// A sender for streaming log messages during agent execution.
10#[derive(Clone)]
11pub struct LogSender {
12 tx: mpsc::Sender<String>,
13}
14
15impl LogSender {
16 pub(crate) fn new(tx: mpsc::Sender<String>) -> Self {
17 Self { tx }
18 }
19
20 /// Send a log message.
21 pub async fn send(&self, msg: impl Into<String>) {
22 let _ = self.tx.send(msg.into()).await;
23 }
24
25 /// Send a log message (non-async, best-effort).
26 pub fn try_send(&self, msg: impl Into<String>) {
27 let _ = self.tx.try_send(msg.into());
28 }
29}
30
31/// Trait for implementing an Actflow agent.
32///
33/// Implement this trait to create your own agent that can be executed
34/// by the Actflow workflow engine.
35///
36/// # Example
37///
38/// ```rust,ignore
39/// use actflow_agent_sdk::{Agent, AgentOutput, Inputs, LogSender};
40///
41/// struct MyAgent;
42///
43/// impl Agent for MyAgent {
44/// async fn run(
45/// &self,
46/// pid: String,
47/// nid: String,
48/// inputs: Inputs,
49/// log: LogSender,
50/// ) -> AgentOutput {
51/// log.send("Processing...").await;
52/// AgentOutput::success(serde_json::json!({"result": "done"}))
53/// }
54/// }
55/// ```
56pub trait Agent: Send + Sync + 'static {
57 /// Execute the agent logic.
58 ///
59 /// # Arguments
60 ///
61 /// * `pid` - Process ID
62 /// * `nid` - Node ID in the workflow
63 /// * `inputs` - Input data for this agent
64 /// * `log` - Log sender for streaming log messages
65 ///
66 /// # Returns
67 ///
68 /// Returns an `AgentOutput` containing the execution status and output data.
69 fn run(
70 &self,
71 pid: String,
72 nid: String,
73 inputs: Inputs,
74 log: LogSender,
75 ) -> impl Future<Output = AgentOutput> + Send;
76
77 /// Called when the agent should shut down.
78 ///
79 /// Override this method to perform cleanup operations.
80 /// Default implementation does nothing.
81 fn shutdown(&self) -> impl Future<Output = ()> + Send {
82 async {}
83 }
84}