1use super::error::LcelError;
13use super::runnable_trait::Runnable;
14use super::sequence::RunnableSequence;
15
16pub trait RunnableExt<Input: Send + Sync + 'static, Output: Send + Sync + 'static>:
21 Runnable<Input, Output>
22where
23 Self::Error: Into<LcelError>,
24 Self: Sized + 'static,
25{
26 fn pipe<O2, R2>(self, other: R2) -> RunnableSequence<Input, O2>
46 where
47 O2: Send + Sync + 'static,
48 R2: Runnable<Output, O2> + Send + Sync + 'static,
49 R2::Error: Into<LcelError>,
50 {
51 RunnableSequence::from_pair(self, other)
52 }
53
54 fn into_sequence(self) -> RunnableSequence<Input, Output> {
59 RunnableSequence::from_single(self)
60 }
61}
62
63impl<I, O, R> RunnableExt<I, O> for R
65where
66 I: Send + Sync + 'static,
67 O: Send + Sync + 'static,
68 R: Runnable<I, O>,
69 R::Error: Into<LcelError>,
70 R: Sized + 'static,
71{
72}
73
74#[cfg(test)]
75mod tests {
76 use super::*;
77 use crate::RunnableConfig;
78 use async_trait::async_trait;
79 use futures_util::StreamExt;
80
81 struct Double;
82
83 #[async_trait]
84 impl Runnable<i32, i32> for Double {
85 type Error = std::convert::Infallible;
86
87 async fn invoke(&self, input: i32, _config: Option<RunnableConfig>) -> Result<i32, Self::Error> {
88 Ok(input * 2)
89 }
90 }
91
92 struct AddSuffix;
93
94 #[async_trait]
95 impl Runnable<i32, String> for AddSuffix {
96 type Error = std::convert::Infallible;
97
98 async fn invoke(&self, input: i32, _config: Option<RunnableConfig>) -> Result<String, Self::Error> {
99 Ok(format!("result: {}", input))
100 }
101 }
102
103 #[tokio::test]
104 async fn pipe_creates_sequence() {
105 let chain = Double.pipe(AddSuffix);
106 let result = chain.invoke(5, None).await.unwrap();
107 assert_eq!(result, "result: 10");
108 }
109
110 #[tokio::test]
111 async fn pipe_chain_multiple() {
112 let chain = Double.pipe(Double).pipe(AddSuffix);
114 let result = chain.invoke(3, None).await.unwrap();
115 assert_eq!(result, "result: 12"); }
117
118 #[tokio::test]
119 async fn pipe_stream_works() {
120 let chain = Double.pipe(AddSuffix);
121 let mut stream = chain.stream(5, None).await.unwrap();
122 let result = stream.next().await.unwrap().unwrap();
123 assert_eq!(result, "result: 10");
124 }
125}