|
17 | 17 | package com.google.adk.workflow; |
18 | 18 |
|
19 | 19 | import com.fasterxml.jackson.annotation.JsonCreator; |
| 20 | +import com.fasterxml.jackson.annotation.JsonIgnore; |
20 | 21 | import com.fasterxml.jackson.annotation.JsonInclude; |
21 | 22 | import com.fasterxml.jackson.annotation.JsonProperty; |
22 | 23 | import com.fasterxml.jackson.annotation.JsonSetter; |
|
27 | 28 | import com.google.common.collect.ImmutableList; |
28 | 29 | import com.google.errorprone.annotations.CanIgnoreReturnValue; |
29 | 30 | import java.util.List; |
| 31 | +import java.util.Optional; |
30 | 32 |
|
31 | 33 | /** |
32 | 34 | * The identity of the workflow-node activation that emitted an {@link com.google.adk.events.Event}. |
@@ -64,6 +66,53 @@ public abstract class NodeInfo { |
64 | 66 | @JsonInclude(JsonInclude.Include.NON_DEFAULT) |
65 | 67 | public abstract boolean messageAsOutput(); |
66 | 68 |
|
| 69 | + /** |
| 70 | + * Returns the emitting node's name: the last segment of {@link #path()} without its run ID, such |
| 71 | + * as {@code a} for {@code wf@1/a@2}, or empty when the path is empty. ADK Python's {@code |
| 72 | + * NodeInfo.name} returns an empty string instead. |
| 73 | + */ |
| 74 | + @JsonIgnore |
| 75 | + public final Optional<String> name() { |
| 76 | + if (path().isEmpty()) { |
| 77 | + return Optional.empty(); |
| 78 | + } |
| 79 | + String segment = lastSegment(path()); |
| 80 | + int at = segment.lastIndexOf('@'); |
| 81 | + return Optional.of(at < 0 ? segment : segment.substring(0, at)); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Returns the emitting node's run ID: the part after {@code @} in the last segment of {@link |
| 86 | + * #path()}, such as {@code 2} for {@code wf@1/a@2}, or empty when the segment has none. ADK |
| 87 | + * Python's {@code NodeInfo.run_id} returns an empty string instead. |
| 88 | + */ |
| 89 | + @JsonIgnore |
| 90 | + public final Optional<String> runId() { |
| 91 | + return runIdOf(lastSegment(path())); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Returns the run ID of the emitting node's parent: the part after {@code @} in the |
| 96 | + * second-to-last segment of {@link #path()}, such as {@code 1} for {@code wf@1/a@2}. Empty when |
| 97 | + * the path has no parent segment or that segment has no run ID, as in ADK Python's {@code |
| 98 | + * NodeInfo.parent_run_id}. |
| 99 | + */ |
| 100 | + @JsonIgnore |
| 101 | + public final Optional<String> parentRunId() { |
| 102 | + String path = path(); |
| 103 | + int lastSlash = path.lastIndexOf('/'); |
| 104 | + return lastSlash < 0 ? Optional.empty() : runIdOf(lastSegment(path.substring(0, lastSlash))); |
| 105 | + } |
| 106 | + |
| 107 | + private static String lastSegment(String path) { |
| 108 | + return path.substring(path.lastIndexOf('/') + 1); |
| 109 | + } |
| 110 | + |
| 111 | + private static Optional<String> runIdOf(String segment) { |
| 112 | + int at = segment.lastIndexOf('@'); |
| 113 | + return at < 0 ? Optional.empty() : Optional.of(segment.substring(at + 1)); |
| 114 | + } |
| 115 | + |
67 | 116 | public static Builder builder() { |
68 | 117 | return new AutoValue_NodeInfo.Builder() |
69 | 118 | .path("") |
|
0 commit comments