1use async_trait::async_trait;
7use lc_core::runnables::{LcelError, Runnable, RunnableConfig};
8use std::sync::Arc;
9
10use crate::base::AgentExecutor;
11
12pub struct AgentRunnable {
21 executor: Arc<AgentExecutor>,
22}
23
24impl AgentRunnable {
25 pub fn new(executor: Arc<AgentExecutor>) -> Self {
27 Self { executor }
28 }
29}
30
31#[async_trait]
32impl Runnable<String, String> for AgentRunnable {
33 type Error = LcelError;
34
35 async fn invoke(
36 &self,
37 input: String,
38 _config: Option<RunnableConfig>,
39 ) -> Result<String, LcelError> {
40 self.executor.invoke(input).await.map_err(|e| LcelError::Agent(e.to_string()))
41 }
42
43 }
46
47impl From<crate::base::AgentError> for LcelError {
49 fn from(err: crate::base::AgentError) -> Self {
50 LcelError::Agent(err.to_string())
51 }
52}
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57
58 #[test]
59 fn agent_error_into_lcel_error() {
60 let agent_err = crate::base::AgentError::MaxIterationsReached;
61 let lcel_err: LcelError = agent_err.into();
62 assert!(matches!(lcel_err, LcelError::Agent(_)));
63 assert!(lcel_err.to_string().contains("Max iterations"));
64 }
65}