use gadk_agent::{AgentError, Context, InvocationContext};
use gadk_genai::{Content, FunctionCall, Part};
use gadk_session::{Event, RequestInput};
use serde_json::{Map, Value};
use crate::error::WorkflowErrorKind;
use crate::workflow::Emit;
use crate::workflow::scheduler::wf_error;
pub const WORKFLOW_INPUT_FUNCTION_CALL_NAME: &str = "adk_request_input";
pub fn new_request_input_event(ctx: &InvocationContext, mut req: RequestInput) -> Event {
if req.interrupt_id.is_empty() {
req.interrupt_id = ctx.platform().new_uuid();
if req.interrupt_id.is_empty() {
req.interrupt_id = gadk_platform::Platform::default().new_uuid();
}
}
let mut ev = Event::new(ctx.platform(), ctx.invocation_id());
let mut args = Map::new();
args.insert(
"interruptId".into(),
Value::String(req.interrupt_id.clone()),
);
args.insert("message".into(), Value::String(req.message.clone()));
args.insert("payload".into(), req.payload.clone().unwrap_or(Value::Null));
if let Some(schema) = &req.response_schema {
args.insert("responseSchema".into(), schema.clone());
}
ev.response.content = Some(Content {
role: "model".into(),
parts: vec![Part {
function_call: Some(FunctionCall {
id: req.interrupt_id.clone(),
name: WORKFLOW_INPUT_FUNCTION_CALL_NAME.into(),
args,
..FunctionCall::default()
}),
..Part::default()
}],
});
ev.long_running_tool_ids = vec![req.interrupt_id.clone()];
ev.author = ctx.agent().name().to_string();
ev.branch = ctx.branch();
ev.requested_input = Some(req);
ev
}
pub async fn resume_or_request_input(
ctx: &Context,
emit: &Emit,
req: RequestInput,
) -> Result<Option<Value>, AgentError> {
if let Some(reply) = ctx.resumed_input(&req.interrupt_id) {
return Ok(Some(reply));
}
emit(new_request_input_event(ctx.as_invocation(), req)).await?;
Err(wf_error(WorkflowErrorKind::NodeInterrupted, ""))
}