use std::collections::{BTreeSet, HashSet};
use std::sync::Arc;
use futures::StreamExt;
use gadk_agent::{Agent, AgentError, Context, ContextDelta, InvocationContext};
use gadk_genai::{Content, ROLE_USER};
use gadk_session::Session;
use serde_json::Value;
use super::run_node::is_llm_agent;
use crate::llm::node_wrapper::run_llm_agent_as_node;
use crate::llm::processors::go_json_marshal;
use crate::tool::utils::function_responses;
use crate::workflow::{Emit, Node, NodeConfig, dynamic_node};
use crate::{WorkflowError, WorkflowErrorKind};
pub(crate) fn new_agent_node(a: Arc<dyn Agent>) -> Arc<dyn Node> {
let mut cfg = NodeConfig {
emits_own_span: true,
..Default::default()
};
if is_llm_agent(&*a) {
cfg.rerun_on_resume = Some(true);
}
let name = a.name().to_string();
dynamic_node::<Value, Value, _, _>(
&name,
move |ctx: Context, input: Value, emit: Emit| {
run_agent_node_body(a.clone(), ctx, input, emit)
},
cfg,
)
}
fn node_interrupted() -> AgentError {
WorkflowError::new(
WorkflowErrorKind::NodeInterrupted,
WorkflowError::sentinel_text(WorkflowErrorKind::NodeInterrupted),
)
.into()
}
async fn run_agent_node_body(
a: Arc<dyn Agent>,
ctx: Context,
input: Value,
emit: Emit,
) -> Result<Value, AgentError> {
let session = ctx.session();
let resolved = answered_interrupts(Some(&*session), ctx.user_content()).unwrap_or_default();
let is_resume = !resolved.is_empty();
if is_llm_agent(&*a) {
run_llm_agent_body(a, ctx, input, is_resume, emit).await
} else {
run_generic_agent_body(a, ctx, input, resolved, emit).await
}
}
async fn run_llm_agent_body(
a: Arc<dyn Agent>,
ctx: Context,
input: Value,
is_resume: bool,
emit: Emit,
) -> Result<Value, AgentError> {
let node_input = if is_resume { Value::Null } else { input };
let mut events = run_llm_agent_as_node(ctx, a, node_input);
let mut paused = false;
let mut last_output = Value::Null;
while let Some(item) = events.next().await {
let event = item?;
if !event.long_running_tool_ids.is_empty() {
paused = true;
}
if let Some(out) = &event.output {
last_output = out.clone();
}
emit(event).await?;
}
if paused {
return Err(node_interrupted());
}
Ok(last_output)
}
async fn run_generic_agent_body(
a: Arc<dyn Agent>,
ctx: Context,
input: Value,
resolved: BTreeSet<String>,
emit: Emit,
) -> Result<Value, AgentError> {
let user_content = if resolved.is_empty() {
input_to_user_content(&input) } else {
resume_user_content(ctx.user_content(), &resolved)
};
let agent_ctx = new_agent_context(&ctx, a.clone(), user_content);
let mut events = a.run(agent_ctx);
let mut paused = false;
while let Some(item) = events.next().await {
let event = item?;
if !event.long_running_tool_ids.is_empty() {
paused = true;
}
emit(event).await?;
}
if paused {
return Err(node_interrupted());
}
Ok(Value::Null)
}
pub(crate) fn answered_interrupts(
sess: Option<&dyn Session>,
user_content: Option<&Content>,
) -> Option<BTreeSet<String>> {
let sess = sess?;
let responses = function_responses(user_content);
if responses.is_empty() {
return None;
}
let long_running: HashSet<String> = sess
.events()
.into_iter()
.flat_map(|e| e.long_running_tool_ids)
.collect();
Some(
responses
.into_iter()
.filter(|fr| !fr.id.is_empty() && long_running.contains(&fr.id))
.map(|fr| fr.id.clone())
.collect(),
)
}
pub(crate) fn resume_user_content(
user_content: Option<&Content>,
resolved: &BTreeSet<String>,
) -> Option<Content> {
let uc = user_content?;
let parts: Vec<_> = uc
.parts
.iter()
.filter(|p| {
p.function_response
.as_ref()
.is_some_and(|fr| resolved.contains(&fr.id))
})
.cloned()
.collect();
if parts.is_empty() {
return None;
}
Some(Content {
role: uc.role.clone(),
parts,
})
}
fn new_agent_context(
ctx: &Context,
a: Arc<dyn Agent>,
user_content: Option<Content>,
) -> InvocationContext {
ctx.with_delta(ContextDelta {
agent: Some(a),
user_content: Some(user_content),
run_id: Some(String::new()),
path: Some(String::new()),
output_for_ancestors: Some(Vec::new()),
..Default::default()
})
.as_invocation()
.clone()
}
fn input_to_user_content(input: &Value) -> Option<Content> {
match input {
Value::Null => None,
Value::String(s) if s.is_empty() => None,
Value::String(s) => Some(Content::from_text(s.clone(), ROLE_USER)),
Value::Object(m)
if m.get("parts").is_some_and(Value::is_array)
&& m.keys().all(|k| k == "parts" || k == "role") =>
{
match serde_json::from_value::<Content>(input.clone()) {
Ok(c) => Some(c),
Err(_) => Some(Content::from_text(go_json_marshal(input), ROLE_USER)),
}
}
other => Some(Content::from_text(go_json_marshal(other), ROLE_USER)),
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn input_to_user_content_matches_go() {
assert_eq!(input_to_user_content(&Value::Null), None);
assert_eq!(input_to_user_content(&json!("")), None);
assert_eq!(
input_to_user_content(&json!("hi")),
Some(Content::from_text("hi", ROLE_USER))
);
let c = Content::from_text("x", "custom");
assert_eq!(
input_to_user_content(&serde_json::to_value(&c).unwrap()),
Some(c)
);
assert_eq!(
input_to_user_content(&json!({"b": 1.0, "a": "<"})),
Some(Content::from_text("{\"a\":\"\\u003c\",\"b\":1}", ROLE_USER))
);
}
}