diff --git a/core/src/main/java/com/google/adk/workflow/MessageReader.java b/core/src/main/java/com/google/adk/workflow/MessageReader.java index 08589ef45..6a46e682c 100644 --- a/core/src/main/java/com/google/adk/workflow/MessageReader.java +++ b/core/src/main/java/com/google/adk/workflow/MessageReader.java @@ -20,6 +20,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectReader; import com.google.adk.JsonBaseModel; import com.google.common.collect.ImmutableList; @@ -28,6 +29,10 @@ import com.google.genai.types.Part; import com.google.genai.types.Schema; import com.google.genai.types.Type; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; import java.util.stream.Stream; import org.jspecify.annotations.Nullable; import org.slf4j.Logger; @@ -42,9 +47,7 @@ final class MessageReader { static final Object NOT_JSON = new Object(); private static final ObjectReader STRICT_JSON = - JsonBaseModel.getMapper() - .readerFor(Object.class) - .with(DeserializationFeature.FAIL_ON_TRAILING_TOKENS); + JsonBaseModel.getMapper().reader().with(DeserializationFeature.FAIL_ON_TRAILING_TOKENS); private MessageReader() {} @@ -113,12 +116,68 @@ static String textOf(Content content) { return json == NOT_JSON ? text : json; } - /** Reads {@code text} as strict JSON, or returns {@link #NOT_JSON} if it is not JSON. */ + /** + * Reads {@code text} as strict JSON into new mutable maps and lists, or returns {@link #NOT_JSON} + * if it is not JSON. A whole number reads as a {@code Long}, or an exact {@code BigInteger} past + * the long range, and a number that is not finite makes the text not JSON. + */ static @Nullable Object readJson(String text) { + JsonNode tree; try { - return STRICT_JSON.readValue(text); + tree = STRICT_JSON.readTree(text); } catch (JsonProcessingException e) { return NOT_JSON; } + // readTree reads blank text as a missing node instead of failing. + return tree.isMissingNode() ? NOT_JSON : plainValue(tree); + } + + /** Returns whether {@code text} is a JSON object, also one {@link #readJson} rejects. */ + static boolean isJsonObject(String text) { + try { + return STRICT_JSON.readTree(text).isObject(); + } catch (JsonProcessingException e) { + return false; + } + } + + /** + * Returns {@code node} as plain Java values, building new maps and lists, or {@link #NOT_JSON} if + * it holds a number that is not finite. + */ + private static @Nullable Object plainValue(JsonNode node) { + if (node.isObject()) { + Map map = new LinkedHashMap<>(); + for (Map.Entry field : node.properties()) { + Object value = plainValue(field.getValue()); + if (value == NOT_JSON) { + return NOT_JSON; + } + map.put(field.getKey(), value); + } + return map; + } + if (node.isArray()) { + List<@Nullable Object> list = new ArrayList<>(node.size()); + for (JsonNode item : node) { + Object value = plainValue(item); + if (value == NOT_JSON) { + return NOT_JSON; + } + list.add(value); + } + return list; + } + if (node.isIntegralNumber()) { + return node.canConvertToLong() ? node.longValue() : node.bigIntegerValue(); + } + if (node.isNumber()) { + double number = node.doubleValue(); + return Double.isFinite(number) ? number : NOT_JSON; + } + if (node.isBoolean()) { + return node.booleanValue(); + } + return node.isNull() ? null : node.asText(); } } diff --git a/core/src/main/java/com/google/adk/workflow/ParallelWorkers.java b/core/src/main/java/com/google/adk/workflow/ParallelWorkers.java index 6ef6af115..7e0d61600 100644 --- a/core/src/main/java/com/google/adk/workflow/ParallelWorkers.java +++ b/core/src/main/java/com/google/adk/workflow/ParallelWorkers.java @@ -16,18 +16,12 @@ package com.google.adk.workflow; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.ObjectReader; -import com.google.adk.JsonBaseModel; import com.google.adk.agents.Context; import com.google.adk.annotations.Experimental; import com.google.adk.telemetry.Tracing; import com.google.common.collect.ImmutableList; import com.google.genai.types.Content; import com.google.genai.types.Part; -import com.google.genai.types.Schema; -import com.google.genai.types.Type; import io.reactivex.rxjava3.core.Flowable; import io.reactivex.rxjava3.core.Maybe; import io.reactivex.rxjava3.core.Single; @@ -82,10 +76,6 @@ public static Node parallelNode(Node node, int maxParallelWorkers) { private static final class ParallelWorkerNode extends BaseNode { - private static final ObjectReader JSON_READER = - JsonBaseModel.getMapper() - .readerFor(Object.class) - .with(DeserializationFeature.FAIL_ON_TRAILING_TOKENS); // No run ID: FanOut reserves IDs in input order, so each item keeps its ID on resume. private static final RunNodeConfig ITEM_CONFIG = RunNodeConfig.builder().useSubBranch(true).build(); @@ -132,24 +122,11 @@ public Flowable runNode(Context context, @Nullable Object nodeInput) { return content; } String text = content.text(); - Object json; - try { - json = JSON_READER.readValue(text); - } catch (JsonProcessingException e) { - return text; - } + Object json = MessageReader.jsonOrText(text); if (json instanceof List list) { return list; } - return worker.inputSchema().map(ParallelWorkerNode::acceptsString).orElse(false) - ? text - : json; - } - - private static boolean acceptsString(Schema schema) { - return schema.type().map(type -> type.knownEnum() == Type.Known.STRING).orElse(false) - || schema.anyOf().orElse(ImmutableList.of()).stream() - .anyMatch(ParallelWorkerNode::acceptsString); + return worker.inputSchema().map(MessageReader::acceptsString).orElse(false) ? text : json; } private final class FanOut { diff --git a/core/src/main/java/com/google/adk/workflow/ToolNodes.java b/core/src/main/java/com/google/adk/workflow/ToolNodes.java index a473e4721..4d9eb5bc6 100644 --- a/core/src/main/java/com/google/adk/workflow/ToolNodes.java +++ b/core/src/main/java/com/google/adk/workflow/ToolNodes.java @@ -18,11 +18,8 @@ import static com.google.common.base.Strings.nullToEmpty; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectReader; import com.google.adk.JsonBaseModel; import com.google.adk.agents.Context; import com.google.adk.annotations.Experimental; @@ -86,11 +83,6 @@ public static Node asNode(BaseTool tool, String name, NodeConfig config) { private static final class ToolNode extends BaseNode { - private static final ObjectReader JSON_READER = - JsonBaseModel.getMapper() - .readerFor(Object.class) - .with(DeserializationFeature.FAIL_ON_TRAILING_TOKENS); - private final BaseTool tool; ToolNode(BaseTool tool, String name, NodeConfig config) { @@ -213,7 +205,8 @@ private Map argumentsFrom(@Nullable Object nodeInput, Parameters return properties; } String param = parameters.forOtherInput(); - if (param == null) { + // As in ADK Kotlin, a JSON object that does not read, such as {"x": 1e400}, fails. + if (param == null || (input instanceof String text && MessageReader.isJsonObject(text))) { throw new IllegalArgumentException( String.format( "The input to tool node '%s' must be a dictionary of tool arguments, an object that" @@ -256,12 +249,8 @@ private static boolean declaresNoResult(BaseTool tool) { if (text.isEmpty()) { return null; } - try { - Object json = JSON_READER.readValue(text); - return json instanceof Map ? json : text; - } catch (JsonProcessingException e) { - return text; - } + Object json = MessageReader.readJson(text); + return json instanceof Map ? json : text; } private static void keepDeltas(Context context, ToolContext toolContext) { diff --git a/core/src/test/java/com/google/adk/workflow/FunctionNodeTest.java b/core/src/test/java/com/google/adk/workflow/FunctionNodeTest.java index f43201225..317708fd2 100644 --- a/core/src/test/java/com/google/adk/workflow/FunctionNodeTest.java +++ b/core/src/test/java/com/google/adk/workflow/FunctionNodeTest.java @@ -34,6 +34,7 @@ import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.SECONDS; +import static java.util.stream.Collectors.joining; import static org.junit.Assert.assertThrows; import com.fasterxml.jackson.annotation.JsonAlias; @@ -5638,6 +5639,77 @@ public void of_recordPortFedAMessageThatIsNotJson_failsWithoutQuotingIt() { + "."); } + @Test + public void of_mapPortFedAMessage_readsAJsonIntegerAsALong() { + // Act + Object output = outputOn(counting(), "{\"count\": 42}"); + + // Assert + assertThat(output).isEqualTo("Long:42"); + } + + @Test + public void of_mapPortFedAMessage_readsAnIntegerPastTheLongRangeExactly() { + // Act + Object output = outputOn(counting(), "{\"count\": 12345678901234567890}"); + + // Assert: ADK Kotlin reads it as a Double, but Java ports can hold an exact BigInteger. + assertThat(output).isEqualTo("BigInteger:12345678901234567890"); + } + + @Test + public void of_mapPortFedAMessage_readsJsonIntegersInAListAsLongs() { + // Arrange + FunctionNode, String> counts = + FunctionNodes.of( + "counts", + new TypeReference>() {}, + String.class, + (ctx, in) -> + ((List) in.get("counts")) + .stream() + .map(count -> count.getClass().getSimpleName() + ":" + count) + .collect(joining(","))); + + // Act + Object output = outputOn(counts, "{\"counts\": [42, 7]}"); + + // Assert + assertThat(output).isEqualTo("Long:42,Long:7"); + } + + @Test + public void of_bigIntegerPortFedAWholeNumberPastTheLongRange_readsItExactly() { + // Act + Object output = outputOn(describing(BigInteger.class), "12345678901234567890"); + + // Assert + assertThat(output).isEqualTo("BigInteger:12345678901234567890"); + } + + @Test + public void of_bigDecimalPortFedAWholeNumberPastTheLongRange_readsItExactly() { + // Act + Object output = outputOn(describing(BigDecimal.class), "12345678901234567890"); + + // Assert + assertThat(output).isEqualTo("BigDecimal:12345678901234567890"); + } + + @Test + public void of_mapPortFedAMessageWithAnInfiniteNumber_failsSinceItReadsTheText() { + // Act: a number that is not finite makes the text not JSON, as in ADK Kotlin. + RunResult inMap = runOn(chain(counting()), "{\"x\": 1e400}"); + RunResult inList = runOn(chain(counting()), "{\"x\": [1, 1e400]}"); + + // Assert + String expected = + "validation error: input of node 'count' does not match" + + " java.util.Map."; + assertThat(inMap.errorMessage("NodeInputValidationException")).isEqualTo(expected); + assertThat(inList.errorMessage("NodeInputValidationException")).isEqualTo(expected); + } + @Test public void of_portsFedScalars_readOnlyTheJsonTypeAQuotedLiteralOrAWholeNumber() { // Arrange @@ -7224,6 +7296,20 @@ private static FunctionNode describing(Class type) { "read", type, String.class, (ctx, in) -> in.getClass().getSimpleName() + ":" + in); } + /** + * Returns a node named {@code count} that outputs its input's "count" as {@code "Class:value"}. + */ + private static FunctionNode, String> counting() { + return FunctionNodes.of( + "count", + new TypeReference>() {}, + String.class, + (ctx, in) -> { + Object count = in.get("count"); + return count.getClass().getSimpleName() + ":" + count; + }); + } + /** Returns a model message with a thought part before its {@code answer}. */ private static Content answerWithThoughts(String answer) { return Content.builder() diff --git a/core/src/test/java/com/google/adk/workflow/ParallelWorkersTest.java b/core/src/test/java/com/google/adk/workflow/ParallelWorkersTest.java index 2fa51865a..ce833e1e5 100644 --- a/core/src/test/java/com/google/adk/workflow/ParallelWorkersTest.java +++ b/core/src/test/java/com/google/adk/workflow/ParallelWorkersTest.java @@ -61,6 +61,7 @@ import io.reactivex.rxjava3.observers.TestObserver; import io.reactivex.rxjava3.schedulers.Schedulers; import io.reactivex.rxjava3.subjects.CompletableSubject; +import java.math.BigInteger; import java.time.Duration; import java.util.Arrays; import java.util.List; @@ -686,16 +687,42 @@ public void runNode_disposed_cancelsRunningItems() throws Exception { @Test public void runNode_textContentWithJsonList_fansOutOverItsElements() { Run run = - run(ParallelWorkers.parallelNode(doubler()), Content.fromParts(Part.fromText("[1, 2, 3]"))); + run(ParallelWorkers.parallelNode(echo()), Content.fromParts(Part.fromText("[1, 2, 3]"))); - assertThat(run.output()).isEqualTo(ImmutableList.of(2, 4, 6)); + assertThat(run.output()).isEqualTo(ImmutableList.of(1L, 2L, 3L)); } @Test public void runNode_textContentWithJsonScalar_runsOnTheParsedValue() { - Run run = run(ParallelWorkers.parallelNode(doubler()), Content.fromParts(Part.fromText("5"))); + Run run = run(ParallelWorkers.parallelNode(echo()), Content.fromParts(Part.fromText("5"))); - assertThat(run.output()).isEqualTo(ImmutableList.of(10)); + assertThat(run.output()).isEqualTo(ImmutableList.of(5L)); + } + + @Test + public void runNode_textContentWithWholeNumbers_readsThemAsLongOrExactBigInteger() { + Run run = + run( + ParallelWorkers.parallelNode(echo()), + Content.fromParts(Part.fromText("[1, 12345678901234567890]"))); + + assertThat(run.output()) + .isEqualTo(ImmutableList.of(1L, new BigInteger("12345678901234567890"))); + } + + @Test + public void runNode_textWithAnInfiniteNumber_runsAsOneRawTextItem() { + Run run = + run(ParallelWorkers.parallelNode(echo()), Content.fromParts(Part.fromText("[1e400]"))); + + assertThat(run.output()).isEqualTo(ImmutableList.of("[1e400]")); + } + + @Test + public void runNode_whitespaceOnlyText_runsAsOneRawTextItem() { + Run run = run(ParallelWorkers.parallelNode(echo()), Content.fromParts(Part.fromText(" "))); + + assertThat(run.output()).isEqualTo(ImmutableList.of(" ")); } @Test @@ -722,9 +749,9 @@ public void runNode_contentWithAThoughtBeforeAJsonList_fansOutOverTheList() { Part.builder().text("Listing the numbers.").thought(true).build(), Part.fromText("[1, 2, 3]")); - Run run = run(ParallelWorkers.parallelNode(doubler()), message); + Run run = run(ParallelWorkers.parallelNode(echo()), message); - assertThat(run.output()).isEqualTo(ImmutableList.of(2, 4, 6)); + assertThat(run.output()).isEqualTo(ImmutableList.of(1L, 2L, 3L)); } @Test @@ -744,7 +771,7 @@ public void runNode_jsonObjectTextForAnObjectItem_getsTheParsedMap() { Run run = run(ParallelWorkers.parallelNode(worker), Content.fromParts(Part.fromText("{\"a\":1}"))); - assertThat(run.output()).isEqualTo(ImmutableList.of(ImmutableMap.of("a", 1))); + assertThat(run.output()).isEqualTo(ImmutableList.of(ImmutableMap.of("a", 1L))); } @Test diff --git a/core/src/test/java/com/google/adk/workflow/ToolNodesTest.java b/core/src/test/java/com/google/adk/workflow/ToolNodesTest.java index 6531eca5c..658fb2f63 100644 --- a/core/src/test/java/com/google/adk/workflow/ToolNodesTest.java +++ b/core/src/test/java/com/google/adk/workflow/ToolNodesTest.java @@ -67,6 +67,7 @@ import io.reactivex.rxjava3.core.Flowable; import io.reactivex.rxjava3.core.Maybe; import io.reactivex.rxjava3.core.Single; +import java.math.BigInteger; import java.time.Duration; import java.util.ArrayList; import java.util.HashMap; @@ -134,7 +135,7 @@ public void runNode_mapInput_passesItAsTheToolsArguments() { public void runNode_jsonObjectText_parsesItIntoArguments() { Object output = run(FakeTool.echo("mock_tool"), " {\"param_a\": 1, \"param_b\": \"value\"} "); - assertThat(output).isEqualTo(ImmutableMap.of("param_a", 1, "param_b", "value")); + assertThat(output).isEqualTo(ImmutableMap.of("param_a", 1L, "param_b", "value")); } @Test @@ -156,7 +157,27 @@ public void runNode_jsonObjectContent_readsItsTextWithoutThoughts() { Part.builder().text("reasoning").thought(true).build(), Part.fromText("{\"x\": 1}")) .build(); - assertThat(run(FakeTool.echo("mock_tool"), content)).isEqualTo(ImmutableMap.of("x", 1)); + Object output = run(FakeTool.echo("mock_tool"), content); + + assertThat(output).isEqualTo(ImmutableMap.of("x", 1L)); + } + + @Test + public void runNode_jsonObjectTextWithAWholeNumberPastTheLongRange_keepsItExact() { + Object output = run(FakeTool.echo("mock_tool"), "{\"x\": 12345678901234567890}"); + + assertThat(output).isEqualTo(ImmutableMap.of("x", new BigInteger("12345678901234567890"))); + } + + @Test + public void runNode_toolWithOneParameter_failsOnJsonObjectTextWithAnInfiniteNumber() { + IllegalArgumentException thrown = + assertThrows( + IllegalArgumentException.class, + () -> run(FakeTool.echo("reader", "path"), "{\"x\": 1e400}")); + + // As in ADK Kotlin, the object does not read, and it does not fill the parameter. + assertThat(thrown).hasMessageThat().contains("but got text that is not a JSON object"); } @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 d08897afa..12b1c41ea 100644 --- a/core/src/test/java/com/google/adk/workflow/WorkflowResumeTest.java +++ b/core/src/test/java/com/google/adk/workflow/WorkflowResumeTest.java @@ -339,7 +339,20 @@ public void resume_textAnswerUnderResult_isReadAsJson() { ImmutableList events = session.turn(answer("ask_1", ImmutableMap.of("result", "{\"v\": 1}"))); - assertThat(replyInput(events)).isEqualTo(ImmutableMap.of("v", 1)); + assertThat(replyInput(events)).isEqualTo(ImmutableMap.of("v", 1L)); + } + + @Test + public void resume_textAnswerUnderResultHoldingAnInfiniteNumber_staysText() { + Node ask = longRunningAsk("ask", "ask_1"); + WorkflowSession session = + new WorkflowSession(workflow(new Edge(START, ask), new Edge(ask, captureInput("reply")))); + session.advance(go()); + + ImmutableList events = session.turn(answer("ask_1", ImmutableMap.of("result", "1e400"))); + + // As in ADK Kotlin, a number that is not finite makes the text not JSON. + assertThat(replyInput(events)).isEqualTo("1e400"); } @Test @@ -972,6 +985,27 @@ public void resume_messageOutputNodeWithAnOutputSchema_isRehydratedToTheParsedVa "chat", ImmutableMap.of("city", "Paris"), "ask", ImmutableMap.of("v", 1))); } + @Test + public void resume_messageOutputNodeWithAnOutputSchema_isRehydratedWithLongIntegers() { + Node chat = new MessageOutputNode("chat", "{\"age\": 42}", AGE_SCHEMA); + 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))); + 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("chat", ImmutableMap.of("age", 42L), "ask", ImmutableMap.of("v", 1))); + } + @Test public void resume_nodeThatFailedInAnEarlierTurn_runsAgain() { AtomicInteger flakyRuns = new AtomicInteger();