Skip to main content

gadk_engine/workflow/
request_input.rs

1// Copyright 2026 Chris Olson
2// Portions derived from ADK-Go (https://github.com/google/adk-go), Copyright 2026 Google LLC.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//     http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Human-in-the-loop input requests.
17
18use 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
27/// The function-call name of an input request (a wire identifier shared with other ADK
28/// runtimes).
29pub const WORKFLOW_INPUT_FUNCTION_CALL_NAME: &str = "adk_request_input";
30
31/// An event that pauses the workflow for human input. An empty interrupt id gets a fresh
32/// platform UUID; when the provider returns an empty string a random UUID is used, so the key is
33/// never empty. The request rides as a long-running `adk_request_input` function call whose id
34/// is the interrupt id.
35pub 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            // The scheduler ignores an empty ID, so the node would not pause for input.
40            // The default platform draws a random v4 UUID.
41            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    // A context always has an agent.
69    ev.author = ctx.agent().name().to_string();
70    ev.branch = ctx.branch();
71    ev.requested_input = Some(req);
72    ev
73}
74
75/// The reply when the node is re-run after the human answered;
76/// otherwise emits the request and returns the NodeInterrupted sentinel (an emit failure is
77/// returned instead).
78pub 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}