|
| 1 | +/* |
| 2 | + * 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 | + |
| 17 | +package com.google.adk.workflow; |
| 18 | + |
| 19 | +import com.google.adk.JsonBaseModel; |
| 20 | +import com.google.adk.agents.InvocationContext; |
| 21 | +import com.google.adk.annotations.Experimental; |
| 22 | +import com.google.adk.events.Event; |
| 23 | +import com.google.auto.value.AutoValue; |
| 24 | +import com.google.common.collect.ImmutableSet; |
| 25 | +import com.google.errorprone.annotations.CanIgnoreReturnValue; |
| 26 | +import com.google.genai.types.Content; |
| 27 | +import com.google.genai.types.FunctionCall; |
| 28 | +import com.google.genai.types.Part; |
| 29 | +import com.google.genai.types.Schema; |
| 30 | +import java.util.LinkedHashMap; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.Optional; |
| 33 | +import java.util.UUID; |
| 34 | +import org.jspecify.annotations.Nullable; |
| 35 | + |
| 36 | +/** |
| 37 | + * A node's request for user input, which pauses the workflow until the user answers it. A node |
| 38 | + * emits it from {@link Node#runNode}, and the runtime records it as a long-running {@value |
| 39 | + * #FUNCTION_CALL_NAME} function call whose ID is {@link #interruptId}. The user answers with a |
| 40 | + * function response that carries the same ID. |
| 41 | + */ |
| 42 | +@Experimental |
| 43 | +@AutoValue |
| 44 | +public abstract class RequestInput { |
| 45 | + |
| 46 | + /** The name of the function call that carries a request for input. */ |
| 47 | + public static final String FUNCTION_CALL_NAME = "adk_request_input"; |
| 48 | + |
| 49 | + static final String INTERRUPT_ID_KEY = "interruptId"; |
| 50 | + static final String MESSAGE_KEY = "message"; |
| 51 | + static final String PAYLOAD_KEY = "payload"; |
| 52 | + static final String RESPONSE_SCHEMA_KEY = "response_schema"; |
| 53 | + |
| 54 | + /** Returns the ID of the request, which the answering function response carries. */ |
| 55 | + public abstract String interruptId(); |
| 56 | + |
| 57 | + /** Returns the message to show the user, if any. */ |
| 58 | + public abstract Optional<String> message(); |
| 59 | + |
| 60 | + /** Returns additional data for rendering the request, if any. */ |
| 61 | + public abstract Optional<Object> payload(); |
| 62 | + |
| 63 | + /** |
| 64 | + * Returns the schema the answer must match, if any. An answer of the form {@code {"result": |
| 65 | + * value}} is unwrapped first, and a string value is read as JSON unless the schema accepts a |
| 66 | + * string. |
| 67 | + */ |
| 68 | + public abstract Optional<Schema> responseSchema(); |
| 69 | + |
| 70 | + /** Returns a builder whose interrupt ID is a new random UUID. */ |
| 71 | + public static Builder builder() { |
| 72 | + return new AutoValue_RequestInput.Builder().interruptId(UUID.randomUUID().toString()); |
| 73 | + } |
| 74 | + |
| 75 | + public abstract Builder toBuilder(); |
| 76 | + |
| 77 | + /** |
| 78 | + * Returns the long-running function-call event that records this request. It has the model role, |
| 79 | + * so history rewriting keeps it as the call that the answer responds to. |
| 80 | + * |
| 81 | + * @throws IllegalArgumentException if the payload has no JSON form |
| 82 | + */ |
| 83 | + Event toEvent(InvocationContext invocationContext) { |
| 84 | + Map<String, @Nullable Object> args = new LinkedHashMap<>(); |
| 85 | + args.put(INTERRUPT_ID_KEY, interruptId()); |
| 86 | + args.put(MESSAGE_KEY, message().orElse(null)); |
| 87 | + // Converted here so a payload with no JSON form fails the asking node, not the session service. |
| 88 | + args.put(PAYLOAD_KEY, payload().map(RequestInput::toJsonValue).orElse(null)); |
| 89 | + args.put(RESPONSE_SCHEMA_KEY, responseSchema().map(RequestInput::toJsonValue).orElse(null)); |
| 90 | + FunctionCall call = |
| 91 | + FunctionCall.builder().name(FUNCTION_CALL_NAME).id(interruptId()).args(args).build(); |
| 92 | + return Event.builder() |
| 93 | + .id(invocationContext.newUuid()) |
| 94 | + .timestamp(invocationContext.now().toEpochMilli()) |
| 95 | + .author("") |
| 96 | + .content( |
| 97 | + Content.builder() |
| 98 | + .role("model") |
| 99 | + .parts(Part.builder().functionCall(call).build()) |
| 100 | + .build()) |
| 101 | + .longRunningToolIds(ImmutableSet.of(interruptId())) |
| 102 | + .build(); |
| 103 | + } |
| 104 | + |
| 105 | + private static @Nullable Object toJsonValue(Object value) { |
| 106 | + return JsonBaseModel.getMapper().convertValue(value, Object.class); |
| 107 | + } |
| 108 | + |
| 109 | + /** Builder for {@link RequestInput}. */ |
| 110 | + @AutoValue.Builder |
| 111 | + public abstract static class Builder { |
| 112 | + |
| 113 | + @CanIgnoreReturnValue |
| 114 | + public abstract Builder interruptId(String interruptId); |
| 115 | + |
| 116 | + @CanIgnoreReturnValue |
| 117 | + public abstract Builder message(@Nullable String message); |
| 118 | + |
| 119 | + @CanIgnoreReturnValue |
| 120 | + public abstract Builder payload(@Nullable Object payload); |
| 121 | + |
| 122 | + @CanIgnoreReturnValue |
| 123 | + public abstract Builder responseSchema(@Nullable Schema responseSchema); |
| 124 | + |
| 125 | + public abstract RequestInput build(); |
| 126 | + } |
| 127 | +} |
0 commit comments