diff --git a/core/src/main/java/com/google/adk/workflow/NodeInfo.java b/core/src/main/java/com/google/adk/workflow/NodeInfo.java index 231bf23b4..29e3c101c 100644 --- a/core/src/main/java/com/google/adk/workflow/NodeInfo.java +++ b/core/src/main/java/com/google/adk/workflow/NodeInfo.java @@ -60,7 +60,8 @@ public abstract class NodeInfo { /** * Returns {@code true} if this event's content is the node's output, so no separate output event - * follows. + * follows. On an event without content, it marks the output as a message, which a stored session + * holds as its JSON form. */ @JsonProperty("messageAsOutput") @JsonInclude(JsonInclude.Include.NON_DEFAULT) diff --git a/core/src/main/java/com/google/adk/workflow/NodeRunner.java b/core/src/main/java/com/google/adk/workflow/NodeRunner.java index eced45acc..0e798f934 100644 --- a/core/src/main/java/com/google/adk/workflow/NodeRunner.java +++ b/core/src/main/java/com/google/adk/workflow/NodeRunner.java @@ -34,6 +34,7 @@ import com.google.common.collect.ImmutableSet; import com.google.errorprone.annotations.CanIgnoreReturnValue; import com.google.errorprone.annotations.concurrent.GuardedBy; +import com.google.genai.types.Content; import com.google.genai.types.FinishReason; import io.reactivex.rxjava3.core.Completable; import io.reactivex.rxjava3.core.Flowable; @@ -770,6 +771,9 @@ private Event stamp(Event event) { String author = nodeState.eventAuthor(); NodeInfo nodeInfo = event.nodeInfo().orElseGet(() -> NodeInfo.builder().build()); boolean carriesOutput = event.output().isPresent() || isMessageAsOutput(event); + // Marked, so that a stored session reads the output back as a message, not as a map. + boolean messageInOutput = + event.content().isEmpty() && event.output().orElse(null) instanceof Content; return event.toBuilder() .author(author.isEmpty() ? node.name() : author) .invocationId(eventContext.invocationId()) @@ -779,6 +783,7 @@ private Event stamp(Event event) { nodeInfo.toBuilder() .path(nodeState.nodePath()) .outputFor(carriesOutput ? nodeState.outputFor() : nodeInfo.outputFor()) + .messageAsOutput(nodeInfo.messageAsOutput() || messageInOutput) .build()) .build(); } diff --git a/core/src/main/java/com/google/adk/workflow/ResumeScan.java b/core/src/main/java/com/google/adk/workflow/ResumeScan.java index 47469a63f..d41696178 100644 --- a/core/src/main/java/com/google/adk/workflow/ResumeScan.java +++ b/core/src/main/java/com/google/adk/workflow/ResumeScan.java @@ -58,6 +58,7 @@ final class ResumeScan { private static final String RESULT_KEY = "result"; + private static final ImmutableSet MESSAGE_KEYS = ImmutableSet.of("role", "parts"); private ResumeScan() {} @@ -130,7 +131,9 @@ static Interception intercept( Object output = recovered.output; if (output != null || recovered.route != null || transfer != null) { return Interception.replay( - output instanceof Content content ? processRehydratedOutput(node, content) : output, + recovered.outputFromContent && output instanceof Content content + ? processRehydratedOutput(node, content, recovered.outputDelegated) + : output, recovered.route, branch, transfer); @@ -189,18 +192,23 @@ static Context replayContext( } /** - * Returns the output of a node whose content was its output: the content's text, read as JSON - * when the node has an output schema that does not accept a string, or as the enum value it - * quotes. Blank text is no output. + * Returns the output of a node whose content was its output: the content itself when the node has + * no output schema, is not an {@link LlmAgent} and gave the message itself rather than through a + * {@code delegated} child. Otherwise it is the content's text, read as JSON when the schema does + * not accept a string, or as the enum value it quotes, and blank text is no output. * * @throws IllegalArgumentException if the schema needs JSON and the text is not JSON */ - static @Nullable Object processRehydratedOutput(Node node, Content content) { + static @Nullable Object processRehydratedOutput(Node node, Content content, boolean delegated) { + Optional schema = node.outputSchema(); + // An LlmAgent's output is its text, not the message, and a delegate may be an LlmAgent. + if (schema.isEmpty() && !delegated && !(node instanceof LlmAgent)) { + return content; + } String text = MessageReader.textOf(content).trim(); if (text.isEmpty()) { return null; } - Optional schema = node.outputSchema(); if (schema.isEmpty()) { return text; } @@ -269,6 +277,13 @@ static Context replayContext( */ static final class RecoveredNode { @Nullable Object output; + + /** Whether {@link #output} is a message read from an event's content, not its output field. */ + boolean outputFromContent; + + /** Whether {@link #output} came from a descendant that delegated it to this node. */ + boolean outputDelegated; + boolean failed; @Nullable ImmutableList route; @Nullable String branch; @@ -484,8 +499,14 @@ private ImmutableMap interruptsMatchingBranch(@Nullable S private static void recordOutcome( Event event, boolean isDirect, String ownerPath, RecoveredNode node) { Object eventOutput = event.output().orElse(null); - if (eventOutput == null && event.nodeInfo().map(NodeInfo::messageAsOutput).orElse(false)) { - eventOutput = event.content().orElse(null); + boolean fromContent = false; + if (event.nodeInfo().map(NodeInfo::messageAsOutput).orElse(false)) { + if (eventOutput == null) { + eventOutput = event.content().orElse(null); + fromContent = eventOutput != null; + } else if (event.content().isEmpty()) { + eventOutput = asMessage(eventOutput); + } } boolean isDelegated = eventOutput != null @@ -495,6 +516,8 @@ private static void recordOutcome( } if (eventOutput != null) { node.output = eventOutput; + node.outputFromContent = fromContent; + node.outputDelegated = !isDirect; // A delegate's output event carries the delegate's branch, not this node's. if (isDirect && event.output().isPresent()) { node.branch = event.branch().orElse(null); @@ -516,6 +539,21 @@ private static void recordOutcome( } } + /** + * Returns {@code value} converted to a message if it is a message's JSON form, which is how a + * stored session gives back a message output; returns any other value unchanged. + */ + private static Object asMessage(Object value) { + if (value instanceof Map map && MESSAGE_KEYS.containsAll(map.keySet())) { + try { + return JsonBaseModel.getMapper().convertValue(map, Content.class); + } catch (IllegalArgumentException e) { + return value; + } + } + return value; + } + /** * Records the interrupts {@code event} raises for {@code node} (long-running calls and requests * for input, together with their response schemas) and returns whether it raised any. diff --git a/core/src/main/java/com/google/adk/workflow/Scheduler.java b/core/src/main/java/com/google/adk/workflow/Scheduler.java index 40bf35ec2..732d6a6e9 100644 --- a/core/src/main/java/com/google/adk/workflow/Scheduler.java +++ b/core/src/main/java/com/google/adk/workflow/Scheduler.java @@ -31,6 +31,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; +import com.google.genai.types.Content; import io.reactivex.rxjava3.core.Completable; import io.reactivex.rxjava3.disposables.CompositeDisposable; import io.reactivex.rxjava3.disposables.Disposable; @@ -347,7 +348,9 @@ private Completable checkpoint() { /** * On a resumable invocation, re-emits the output of a node replayed from history on the node's - * branch, without the paths that the output counts for. + * branch, without the paths that the output counts for. A replayed message is marked as one, so a + * later resume reads it back as a message; sent as content, it would appear twice in a later + * agent's history. */ private void reemitReplayedOutput(Context childContext) { Optional output = childContext.requireNodeState().output(); @@ -359,7 +362,10 @@ private void reemitReplayedOutput(Context childContext) { workflowEvent(childContext.invocationContext().branch().orElse(null)) .output(output.get()) .nodeInfo( - NodeInfo.builder().path(childContext.requireNodeState().nodePath()).build())); + NodeInfo.builder() + .path(childContext.requireNodeState().nodePath()) + .messageAsOutput(output.get() instanceof Content) + .build())); } /** Returns the start of an event that the workflow itself emits on {@code branch}. */ diff --git a/core/src/test/java/com/google/adk/workflow/ResumeScanTest.java b/core/src/test/java/com/google/adk/workflow/ResumeScanTest.java index 3b51c4c86..399f83d66 100644 --- a/core/src/test/java/com/google/adk/workflow/ResumeScanTest.java +++ b/core/src/test/java/com/google/adk/workflow/ResumeScanTest.java @@ -203,6 +203,61 @@ public void scan_outputOnAnEventWithAnErrorCode_replaysTheOutput() { assertThat(interception.output()).isEqualTo("cut short"); } + @Test + public void scan_messageInTheOutputFieldOfAStoredEvent_isReadBackAsTheMessage() { + Event resent = + nodeEvent("wf@1/chat@1") + .output(modelText("hi")) + .nodeInfo(NodeInfo.builder().path("wf@1/chat@1").messageAsOutput(true).build()) + .build(); + Event stored = Event.fromJson(resent.toJson()); + + RecoveredNode recovered = + ResumeScan.scan(ImmutableList.of(stored), "wf@1", "inv").get("chat@1"); + + assertThat(stored.output().orElseThrow()).isInstanceOf(Map.class); + assertThat(recovered.output).isEqualTo(modelText("hi")); + } + + @Test + public void scan_eventMarkedAsAMessageWithAPlainOutput_keepsTheOutput() { + Event event = + nodeEvent("wf@1/chat@1") + .output("hi") + .nodeInfo(NodeInfo.builder().path("wf@1/chat@1").messageAsOutput(true).build()) + .build(); + + RecoveredNode recovered = ResumeScan.scan(ImmutableList.of(event), "wf@1", "inv").get("chat@1"); + + assertThat(recovered.output).isEqualTo("hi"); + } + + @Test + public void scan_eventMarkedAsAMessageWithAMapOutput_keepsTheMap() { + Event event = + nodeEvent("wf@1/chat@1") + .output(ImmutableMap.of("v", 1)) + .nodeInfo(NodeInfo.builder().path("wf@1/chat@1").messageAsOutput(true).build()) + .build(); + + RecoveredNode recovered = ResumeScan.scan(ImmutableList.of(event), "wf@1", "inv").get("chat@1"); + + assertThat(recovered.output).isEqualTo(ImmutableMap.of("v", 1)); + } + + @Test + public void scan_eventMarkedAsAMessageWithAMapThatIsNotAMessage_keepsTheMap() { + Event event = + nodeEvent("wf@1/chat@1") + .output(ImmutableMap.of("parts", "x")) + .nodeInfo(NodeInfo.builder().path("wf@1/chat@1").messageAsOutput(true).build()) + .build(); + + RecoveredNode recovered = ResumeScan.scan(ImmutableList.of(event), "wf@1", "inv").get("chat@1"); + + assertThat(recovered.output).isEqualTo(ImmutableMap.of("parts", "x")); + } + @Test public void scan_partialEvent_isIgnored() { ImmutableList events = @@ -418,7 +473,9 @@ public void processRehydratedOutput_outputNoLongerFitsTheSchema_resumesUnvalidat Object output = ResumeScan.processRehydratedOutput( - new MessageOutputNode("chat", "", schema), modelText("{\"town\": \"Paris\"}")); + new MessageOutputNode("chat", "", schema), + modelText("{\"town\": \"Paris\"}"), + /* delegated= */ false); assertThat(output).isEqualTo(ImmutableMap.of("town", "Paris")); } @@ -432,11 +489,11 @@ public void processRehydratedOutput_textThatIsNotJsonForAnObjectSchema_fails() { IllegalArgumentException notJson = assertThrows( IllegalArgumentException.class, - () -> ResumeScan.processRehydratedOutput(node, notJsonText)); + () -> ResumeScan.processRehydratedOutput(node, notJsonText, /* delegated= */ false)); IllegalArgumentException bareWord = assertThrows( IllegalArgumentException.class, - () -> ResumeScan.processRehydratedOutput(node, bareWordText)); + () -> ResumeScan.processRehydratedOutput(node, bareWordText, /* delegated= */ false)); assertThat(notJson) .hasMessageThat() @@ -450,16 +507,31 @@ public void processRehydratedOutput_textThatIsNotJsonForAnObjectSchema_fails() { public void processRehydratedOutput_blankText_isNoOutput() { Object output = ResumeScan.processRehydratedOutput( - new MessageOutputNode("chat", "", COUNT_SCHEMA), modelText(" ")); + new MessageOutputNode("chat", "", COUNT_SCHEMA), + modelText(" "), + /* delegated= */ false); assertThat(output).isNull(); } + @Test + public void processRehydratedOutput_blankTextWithoutSchema_keepsTheMessage() { + Content blank = modelText(" "); + + Object output = + ResumeScan.processRehydratedOutput( + new MessageOutputNode("chat", "", /* schema= */ null), blank, /* delegated= */ false); + + assertThat(output).isEqualTo(blank); + } + @Test public void processRehydratedOutput_jsonNullForAnObjectSchema_isNoOutput() { Object output = ResumeScan.processRehydratedOutput( - new MessageOutputNode("chat", "", AGE_SCHEMA), modelText("null")); + new MessageOutputNode("chat", "", AGE_SCHEMA), + modelText("null"), + /* delegated= */ false); assertThat(output).isNull(); } @@ -468,7 +540,10 @@ public void processRehydratedOutput_jsonNullForAnObjectSchema_isNoOutput() { public void processRehydratedOutput_stringSchema_keepsTheText() { Node node = new MessageOutputNode("chat", "", Schema.builder().type(Type.Known.STRING).build()); - assertThat(ResumeScan.processRehydratedOutput(node, modelText("42"))).isEqualTo("42"); + Object output = + ResumeScan.processRehydratedOutput(node, modelText("42"), /* delegated= */ false); + + assertThat(output).isEqualTo("42"); } @Test diff --git a/core/src/test/java/com/google/adk/workflow/WorkflowResumeTest.java b/core/src/test/java/com/google/adk/workflow/WorkflowResumeTest.java index 12b1c41ea..4afa98fe7 100644 --- a/core/src/test/java/com/google/adk/workflow/WorkflowResumeTest.java +++ b/core/src/test/java/com/google/adk/workflow/WorkflowResumeTest.java @@ -38,6 +38,7 @@ import static com.google.common.collect.MoreCollectors.onlyElement; import static com.google.common.truth.Truth.assertThat; +import com.google.adk.agents.AgentMode; import com.google.adk.agents.Context; import com.google.adk.agents.InvocationContext; import com.google.adk.agents.LlmAgent; @@ -46,6 +47,7 @@ import com.google.adk.models.LlmRequest; import com.google.adk.plugins.BasePlugin; import com.google.adk.plugins.Plugin; +import com.google.adk.testing.TestBaseAgent; import com.google.adk.testing.TestLlm; import com.google.adk.testing.TestNode; import com.google.adk.testing.TestUtils; @@ -899,7 +901,7 @@ public void resume_rolelessAnswerWithoutResumability_reachesTheModel() { } @Test - public void resume_messageOutputNode_isRehydratedToItsText() { + public void resume_messageOutputNode_isRehydratedToItsMessage() { Node chat = new MessageOutputNode("chat", "agent said hi", /* schema= */ null); Node ask = longRunningAsk("ask", "ask_1"); JoinNode join = JoinNode.builder().name("j").build(); @@ -914,8 +916,212 @@ public void resume_messageOutputNode_isRehydratedToItsText() { ImmutableList events = session.turn(answer("ask_1", ImmutableMap.of("v", 1))); + // The successor gets the message it got live, not the message's text. assertThat(outputEventAt(events, "wf@1/j@1").output()) - .hasValue(ImmutableMap.of("chat", "agent said hi", "ask", ImmutableMap.of("v", 1))); + .hasValue( + ImmutableMap.of("chat", modelText("agent said hi"), "ask", ImmutableMap.of("v", 1))); + } + + @Test + public void resume_replayedMessageOutput_isResentAsAMessageForTheNextResume() { + Node chat = new MessageOutputNode("chat", "agent said hi", /* schema= */ null); + Node first = longRunningAsk("first", "ask_1"); + Node second = longRunningAsk("second", "ask_2"); + JoinNode join = JoinNode.builder().name("j").build(); + WorkflowSession session = + new WorkflowSession( + workflow( + new Edge(START, chat), + new Edge(START, first), + new Edge(START, second), + new Edge(chat, join), + new Edge(first, join), + new Edge(second, join))); + session.advance(go()); + + ImmutableList resumed = session.turn(answer("ask_1", ImmutableMap.of("v", 1))); + ImmutableList last = session.turn(answer("ask_2", ImmutableMap.of("v", 2))); + + // The message goes out again as the output, marked as a message, not as content. + Event resent = outputEventAt(resumed, "wf@1/chat@1"); + assertThat(resent.content()).isEmpty(); + assertThat(resent.nodeInfo().orElseThrow().messageAsOutput()).isTrue(); + assertThat(outputEventAt(last, "wf@1/j@1").output()) + .hasValue( + ImmutableMap.of( + "chat", + modelText("agent said hi"), + "first", + ImmutableMap.of("v", 1), + "second", + ImmutableMap.of("v", 2))); + } + + @Test + public void resume_nodeOutputtingAMessage_isRehydratedToTheMessage() { + Node chat = emitter("chat", modelText("agent said hi")); + Node ask = longRunningAsk("ask", "ask_1"); + JoinNode join = JoinNode.builder().name("j").build(); + WorkflowSession session = + new WorkflowSession( + workflow( + new Edge(START, chat), + new Edge(START, ask), + new Edge(chat, join), + new Edge(ask, join))); + + ImmutableList first = session.turn(go()); + ImmutableList events = session.turn(answer("ask_1", ImmutableMap.of("v", 1))); + + // Marked as a message, the output reads back as one from a stored session too. + assertThat(outputEventAt(first, "wf@1/chat@1").nodeInfo().orElseThrow().messageAsOutput()) + .isTrue(); + assertThat(outputEventAt(events, "wf@1/j@1").output()) + .hasValue( + ImmutableMap.of("chat", modelText("agent said hi"), "ask", ImmutableMap.of("v", 1))); + } + + @Test + public void resume_childWorkflowEndingInAMessageNode_outputsTheMessage() { + // The workflow's schema is not checked at run time, so its output is the message itself. + Workflow inner = + Workflow.builder() + .name("inner") + .outputSchema(AGE_SCHEMA) + .edges( + new Edge(START, new MessageOutputNode("chat", "{\"age\": 3}", /* schema= */ null))) + .build(); + Node ask = longRunningAsk("ask", "ask_1"); + JoinNode join = JoinNode.builder().name("j").build(); + WorkflowSession session = + new WorkflowSession( + workflow( + new Edge(START, inner), + new Edge(START, ask), + new Edge(inner, join), + new Edge(ask, join))); + session.advance(go()); + + ImmutableList events = session.turn(answer("ask_1", ImmutableMap.of("v", 1))); + + assertThat(outputEventAt(events, "wf@1/j@1").output()) + .hasValue( + ImmutableMap.of("inner", modelText("{\"age\": 3}"), "ask", ImmutableMap.of("v", 1))); + } + + @Test + public void resume_callerDelegatingToANodeOutputtingAMessage_isRehydratedToTheMessage() { + Node chat = emitter("chat", modelText("hi")); + Node caller = + dispatcher( + "caller", + (ctx, input) -> ctx.runNode(chat, null, USE_AS_OUTPUT).ignoreElement().toFlowable()); + Node ask = longRunningAsk("ask", "ask_1"); + JoinNode join = JoinNode.builder().name("j").build(); + WorkflowSession session = + new WorkflowSession( + workflow( + new Edge(START, caller), + new Edge(START, ask), + new Edge(caller, join), + new Edge(ask, join))); + session.advance(go()); + + ImmutableList events = session.turn(answer("ask_1", ImmutableMap.of("v", 1))); + + assertThat(outputEventAt(events, "wf@1/j@1").output()) + .hasValue(ImmutableMap.of("caller", modelText("hi"), "ask", ImmutableMap.of("v", 1))); + } + + @Test + public void resume_callerDelegatingToASingleTurnAgent_isRehydratedToTheAgentsText() { + LlmAgent agent = + LlmAgent.builder() + .name("agent") + .mode(AgentMode.SINGLE_TURN) + .model(TestLlm.create(TestUtils.createTextLlmResponse("agent said hi"))) + .build(); + Node caller = + dispatcher( + "caller", + (ctx, input) -> ctx.runNode(agent, null, USE_AS_OUTPUT).ignoreElement().toFlowable()); + Node ask = longRunningAsk("ask", "ask_1"); + JoinNode join = JoinNode.builder().name("j").build(); + WorkflowSession session = + new WorkflowSession( + workflow( + new Edge(START, caller), + new Edge(START, ask), + new Edge(caller, join), + new Edge(ask, join))); + session.advance(go()); + + ImmutableList events = session.turn(answer("ask_1", ImmutableMap.of("v", 1))); + + // Live, the caller output the agent's text, so the replay gives the text too. + assertThat(outputEventAt(events, "wf@1/j@1").output()) + .hasValue(ImmutableMap.of("caller", "agent said hi", "ask", ImmutableMap.of("v", 1))); + } + + @Test + public void resume_customAgentMessageOutput_isRehydratedToItsMessage() { + Node agent = + new TestBaseAgent( + "agent", + "description", + () -> + Flowable.just( + newEvent() + .author("agent") + .content(modelText("agent said hi")) + .nodeInfo(NodeInfo.builder().messageAsOutput(true).build()) + .build()), + /* subAgents= */ null, + /* beforeAgentCallbacks= */ null, + /* afterAgentCallbacks= */ null); + Node ask = longRunningAsk("ask", "ask_1"); + JoinNode join = JoinNode.builder().name("j").build(); + WorkflowSession session = + new WorkflowSession( + workflow( + new Edge(START, agent), + new Edge(START, ask), + new Edge(agent, join), + new Edge(ask, join))); + session.advance(go()); + + ImmutableList events = session.turn(answer("ask_1", ImmutableMap.of("v", 1))); + + // Only an LlmAgent outputs its text; any other agent gives the message it output. + assertThat(outputEventAt(events, "wf@1/j@1").output()) + .hasValue( + ImmutableMap.of("agent", modelText("agent said hi"), "ask", ImmutableMap.of("v", 1))); + } + + @Test + public void resume_singleTurnAgentWithoutAnOutputSchema_isRehydratedToItsText() { + LlmAgent agent = + LlmAgent.builder() + .name("agent") + .mode(AgentMode.SINGLE_TURN) + .model(TestLlm.create(TestUtils.createTextLlmResponse("agent said hi"))) + .build(); + Node ask = longRunningAsk("ask", "ask_1"); + JoinNode join = JoinNode.builder().name("j").build(); + WorkflowSession session = + new WorkflowSession( + workflow( + new Edge(START, agent), + new Edge(START, ask), + new Edge(agent, join), + new Edge(ask, join))); + session.advance(go()); + + ImmutableList events = session.turn(answer("ask_1", ImmutableMap.of("v", 1))); + + // Live, an agent's output is its text, so the replay gives the text too. + assertThat(outputEventAt(events, "wf@1/j@1").output()) + .hasValue(ImmutableMap.of("agent", "agent said hi", "ask", ImmutableMap.of("v", 1))); } @Test diff --git a/core/src/test/java/com/google/adk/workflow/WorkflowResumeTesting.java b/core/src/test/java/com/google/adk/workflow/WorkflowResumeTesting.java index 4dad47bdc..0eb17532a 100644 --- a/core/src/test/java/com/google/adk/workflow/WorkflowResumeTesting.java +++ b/core/src/test/java/com/google/adk/workflow/WorkflowResumeTesting.java @@ -284,7 +284,7 @@ public Flowable runNode(Context context, @Nullable Object nodeInput) { } } - /** A node whose model-style message is its output, as an agent node's can be. */ + /** A node whose model-style message is its output. */ record MessageOutputNode(String name, String text, @Nullable Schema schema) implements Node { @Override