gadk_engine/workflow/
request_input.rs1use gadk_agent::{AgentError, Context, InvocationContext};
19use gadk_genai::{Content, FunctionCall, Part};
20use gadk_session::{Event, RequestInput};
21use serde_json::{Map, Value};
22
23use crate::error::WorkflowErrorKind;
24use crate::workflow::Emit;
25use crate::workflow::scheduler::wf_error;
26
27pub const WORKFLOW_INPUT_FUNCTION_CALL_NAME: &str = "adk_request_input";
30
31pub fn new_request_input_event(ctx: &InvocationContext, mut req: RequestInput) -> Event {
36 if req.interrupt_id.is_empty() {
37 req.interrupt_id = ctx.platform().new_uuid();
38 if req.interrupt_id.is_empty() {
39 req.interrupt_id = gadk_platform::Platform::default().new_uuid();
42 }
43 }
44 let mut ev = Event::new(ctx.platform(), ctx.invocation_id());
45 let mut args = Map::new();
46 args.insert(
47 "interruptId".into(),
48 Value::String(req.interrupt_id.clone()),
49 );
50 args.insert("message".into(), Value::String(req.message.clone()));
51 args.insert("payload".into(), req.payload.clone().unwrap_or(Value::Null));
52 if let Some(schema) = &req.response_schema {
53 args.insert("responseSchema".into(), schema.clone());
54 }
55 ev.response.content = Some(Content {
56 role: "model".into(),
57 parts: vec![Part {
58 function_call: Some(FunctionCall {
59 id: req.interrupt_id.clone(),
60 name: WORKFLOW_INPUT_FUNCTION_CALL_NAME.into(),
61 args,
62 ..FunctionCall::default()
63 }),
64 ..Part::default()
65 }],
66 });
67 ev.long_running_tool_ids = vec![req.interrupt_id.clone()];
68 ev.author = ctx.agent().name().to_string();
70 ev.branch = ctx.branch();
71 ev.requested_input = Some(req);
72 ev
73}
74
75pub async fn resume_or_request_input(
79 ctx: &Context,
80 emit: &Emit,
81 req: RequestInput,
82) -> Result<Option<Value>, AgentError> {
83 if let Some(reply) = ctx.resumed_input(&req.interrupt_id) {
84 return Ok(Some(reply));
85 }
86 emit(new_request_input_event(ctx.as_invocation(), req)).await?;
87 Err(wf_error(WorkflowErrorKind::NodeInterrupted, ""))
88}