iflow-cli-sdk-rust 0.1.6

Rust SDK for iFlow CLI using Agent Client Protocol
Documentation
//! Test to verify iFlow response handling

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>> {
    // Initialize logging with environment variable support
    tracing_subscriber::fmt()
        .with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
        .init();

    println!("๐Ÿงช Testing iFlow response handling...");

    // Use LocalSet for spawn_local compatibility
    let local = tokio::task::LocalSet::new();
    local
        .run_until(async {
            // Configure client options with auto-start enabled for stdio mode
            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");

            // Send a simple test message
            let prompt = "Hello! Please reply with 'Hello back!' to confirm you're working.";
            println!("๐Ÿ“ค Sending: {}", prompt);
            
            // Handle the send_message result to catch timeout errors
            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());
                }
            }

            // Wait for response with timeout
            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");
                }
            }

            // Disconnect
            println!("\n๐Ÿ”Œ Disconnecting...");
            client.disconnect().await?;
            println!("๐Ÿ‘‹ Disconnected from iFlow");

            Ok(())
        })
        .await
}