#![cfg(feature = "agentic-worker")]
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use greentic_aw_runtime::{FlowInvokeOutcome, FlowInvoker, FlowOperation};
use crate::pack::{PackRuntime, ToolFlowOutcome};
pub struct PackRuntimeFlowInvoker {
packs: Vec<Arc<PackRuntime>>,
#[allow(dead_code)]
tenant: String,
}
impl PackRuntimeFlowInvoker {
pub fn new(packs: Vec<Arc<PackRuntime>>, tenant: String) -> Self {
Self { packs, tenant }
}
}
impl PackRuntimeFlowInvoker {
fn owner(&self, flow_ref: &str) -> Option<&Arc<PackRuntime>> {
self.packs
.iter()
.find(|pack| pack.flow_descriptors().iter().any(|d| d.id == flow_ref))
}
}
fn to_invoke_outcome(outcome: ToolFlowOutcome) -> FlowInvokeOutcome {
match outcome {
ToolFlowOutcome::Completed(value) => FlowInvokeOutcome::Completed(value),
ToolFlowOutcome::Waiting {
snapshot,
presentation,
} => FlowInvokeOutcome::Waiting {
snapshot,
presentation,
},
}
}
impl FlowInvoker for PackRuntimeFlowInvoker {
fn list_flows(&self) -> Vec<FlowOperation> {
let mut out = Vec::new();
for pack in &self.packs {
for descriptor in pack.flow_descriptors() {
out.push(FlowOperation {
flow_ref: descriptor.id.clone(),
description: descriptor
.description
.clone()
.unwrap_or_else(|| descriptor.id.clone()),
parameters: serde_json::json!({ "type": "object" }),
});
}
}
out
}
fn invoke<'a>(
&'a self,
flow_ref: &'a str,
args_json: &'a str,
) -> Pin<Box<dyn Future<Output = Result<serde_json::Value, String>> + Send + 'a>> {
Box::pin(async move {
let input: serde_json::Value = serde_json::from_str(args_json)
.map_err(|e| format!("invalid JSON args for flow '{flow_ref}': {e}"))?;
for pack in &self.packs {
let owns = pack.flow_descriptors().iter().any(|d| d.id == flow_ref);
if owns {
return pack.run_flow_for_tool(flow_ref, input).await;
}
}
Err(format!("flow '{flow_ref}' not found in any loaded pack"))
})
}
fn invoke_interactive<'a>(
&'a self,
flow_ref: &'a str,
args_json: &'a str,
) -> Pin<Box<dyn Future<Output = Result<FlowInvokeOutcome, String>> + Send + 'a>> {
Box::pin(async move {
let input: serde_json::Value = serde_json::from_str(args_json)
.map_err(|e| format!("invalid JSON args for flow '{flow_ref}': {e}"))?;
let pack = self
.owner(flow_ref)
.ok_or_else(|| format!("flow '{flow_ref}' not found in any loaded pack"))?;
pack.run_flow_for_tool_interactive(flow_ref, input)
.await
.map(to_invoke_outcome)
})
}
fn resume<'a>(
&'a self,
flow_ref: &'a str,
snapshot: serde_json::Value,
input: serde_json::Value,
) -> Pin<Box<dyn Future<Output = Result<FlowInvokeOutcome, String>> + Send + 'a>> {
Box::pin(async move {
let pack = self
.owner(flow_ref)
.ok_or_else(|| format!("flow '{flow_ref}' not found in any loaded pack"))?;
pack.resume_flow_for_tool(flow_ref, snapshot, input)
.await
.map(to_invoke_outcome)
})
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use super::*;
#[tokio::test]
async fn invoke_unknown_flow_returns_not_found_error() {
let invoker = PackRuntimeFlowInvoker::new(Vec::new(), "acme".into());
let out = invoker.invoke("nope", "{}").await;
assert!(
out.is_err(),
"unknown flow must Err (folded to error value by the catalog)"
);
assert!(
out.unwrap_err().contains("nope"),
"error message must name the missing flow_ref"
);
}
#[tokio::test]
async fn list_flows_returns_empty_for_no_packs() {
let invoker = PackRuntimeFlowInvoker::new(Vec::new(), "acme".into());
let ops = invoker.list_flows();
assert!(ops.is_empty(), "no packs → no flows exposed");
}
#[tokio::test]
async fn invoke_invalid_json_returns_descriptive_error() {
let invoker = PackRuntimeFlowInvoker::new(Vec::new(), "acme".into());
let out = invoker.invoke("some-flow", "not-json").await;
assert!(out.is_err());
let msg = out.unwrap_err();
assert!(
msg.contains("invalid JSON args"),
"error should mention JSON parse failure, got: {msg}"
);
}
}