use futures::stream::StreamExt;
use iflow_cli_sdk_rust::{IFlowClient, IFlowOptions, Message};
use iflow_cli_sdk_rust::error::IFlowError;
use std::time::Duration;
use tokio::time::timeout;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.init();
println!("๐งช Testing iFlow response handling...");
let local = tokio::task::LocalSet::new();
local
.run_until(async {
let options = IFlowOptions::new().with_timeout(30.0).with_process_config(
iflow_cli_sdk_rust::types::ProcessConfig::new()
.enable_auto_start()
.stdio_mode(),
);
let mut client = IFlowClient::new(Some(options));
println!("๐ Connecting to iFlow...");
client.connect().await?;
println!("โ
Connected to iFlow");
let prompt = "Hello! Please reply with 'Hello back!' to confirm you're working.";
println!("๐ค Sending: {}", prompt);
match client.send_message(prompt, None).await {
Ok(()) => {
println!("โ
Message sent successfully");
}
Err(IFlowError::Timeout(msg)) => {
eprintln!("โฐ Timeout error occurred: {}", msg);
eprintln!("This may be due to processing delays.");
eprintln!("Consider increasing the timeout or checking the iFlow process.");
}
Err(e) => {
eprintln!("โ Error sending message: {}", e);
return Err(e.into());
}
}
println!("โณ Waiting for response...");
let mut message_stream = client.messages();
let result = timeout(Duration::from_secs(10), async {
let mut response_received = false;
while let Some(message) = message_stream.next().await {
match message {
Message::Assistant { content } => {
println!("๐ Received assistant response: {}", content);
if content.contains("Hello back") || content.contains("hello") {
println!("โ
SUCCESS: Received expected response!");
response_received = true;
break;
}
}
Message::User { content } => {
println!("๐ค User message echo: {}", content);
if content.contains("Hello!") {
println!("โน๏ธ Received our own message echo");
}
}
Message::TaskFinish { reason } => {
println!("๐ Task finished: {:?}", reason);
break;
}
Message::Error {
code,
message,
details: _,
} => {
println!("โ Error {}: {}", code, message);
break;
}
_ => {
println!("๐จ Other message type: {:?}", message);
}
}
}
response_received
})
.await;
match result {
Ok(true) => {
println!("๐ TEST PASSED: Received expected response from iFlow!");
}
Ok(false) => {
println!("โ ๏ธ TEST INCONCLUSIVE: No matching response received");
}
Err(_) => {
println!("โฐ TEST FAILED: Timeout waiting for response");
}
}
println!("\n๐ Disconnecting...");
client.disconnect().await?;
println!("๐ Disconnected from iFlow");
Ok(())
})
.await
}