Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 64 additions & 5 deletions core/src/main/java/com/google/adk/workflow/MessageReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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() {}

Expand Down Expand Up @@ -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<String, @Nullable Object> map = new LinkedHashMap<>();
for (Map.Entry<String, JsonNode> 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();
}
}
27 changes: 2 additions & 25 deletions core/src/main/java/com/google/adk/workflow/ParallelWorkers.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 {
Expand Down
19 changes: 4 additions & 15 deletions core/src/main/java/com/google/adk/workflow/ToolNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -213,7 +205,8 @@ private Map<String, Object> 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"
Expand Down Expand Up @@ -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) {
Expand Down
86 changes: 86 additions & 0 deletions core/src/test/java/com/google/adk/workflow/FunctionNodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Map<String, Object>, String> counts =
FunctionNodes.of(
"counts",
new TypeReference<Map<String, Object>>() {},
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<java.lang.String,java.lang.Object>.";
assertThat(inMap.errorMessage("NodeInputValidationException")).isEqualTo(expected);
assertThat(inList.errorMessage("NodeInputValidationException")).isEqualTo(expected);
}

@Test
public void of_portsFedScalars_readOnlyTheJsonTypeAQuotedLiteralOrAWholeNumber() {
// Arrange
Expand Down Expand Up @@ -7224,6 +7296,20 @@ private static <T> FunctionNode<T, String> describing(Class<T> 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<Map<String, Object>, String> counting() {
return FunctionNodes.of(
"count",
new TypeReference<Map<String, Object>>() {},
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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Loading
Loading