vibe-action 0.0.1

Command router — execute shell commands and LLM prompts via simple YAML actions.
//! LLM cluster executor.
//! Sends prompts to vibe-cluster and returns model responses.

use anyhow::Result;
use vibe_cluster::Prompt;

use crate::configs::app::AppConfig;

/// Distributed cluster execution response payload.
pub struct ClusterResult {
    /// The generated text prompt or command payload to be executed.
    pub prompt: String,
    /// Normalized text response generated by the cluster node model.
    pub result: String,
}

pub struct Cluster;

impl Cluster {
    /// Execute an LLM prompt batch concurrently across the distributed cluster.
    pub async fn exec(prompts: &[String]) -> Result<Vec<ClusterResult>> {
        let config = AppConfig::instance()?;
        let cluster = config.create_cluster()?;

        let cluster_prompts: Vec<Prompt> = prompts
            .iter()
            .map(|p| Prompt {
                key: None,
                system: None,
                user: p.clone(),
            })
            .collect();

        let cluster_results = cluster
            .batch_call(&cluster_prompts, |_, _, _| {})
            .await
            .map_err(|e| anyhow::anyhow!("Cluster batch call failed: {}", e))?;

        let mut final_results = Vec::with_capacity(cluster_results.len());

        for result in cluster_results {
            if let Some(err) = result.error {
                anyhow::bail!("Cluster node execution failed: {}", err);
            }
            if let Some(text) = result.text {
                final_results.push(ClusterResult {
                    prompt: result.prompt.user,
                    result: vibe_cluster::normalize_text(&text),
                });
            }
        }

        Ok(final_results)
    }
}