|
| 1 | +package org.a2aproject.sdk.compat03.client.adapter.jsonrpc; |
| 2 | + |
| 3 | +import java.math.BigDecimal; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.LinkedHashMap; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.Objects; |
| 9 | + |
| 10 | +import org.a2aproject.sdk.compat03.spec.DataPart_v0_3; |
| 11 | +import org.a2aproject.sdk.compat03.spec.FilePart_v0_3; |
| 12 | +import org.a2aproject.sdk.compat03.spec.MessageSendParams_v0_3; |
| 13 | +import org.a2aproject.sdk.compat03.spec.Message_v0_3; |
| 14 | +import org.a2aproject.sdk.compat03.spec.Part_v0_3; |
| 15 | +import org.a2aproject.sdk.compat03.spec.TaskIdParams_v0_3; |
| 16 | +import org.a2aproject.sdk.compat03.spec.TextPart_v0_3; |
| 17 | +import org.a2aproject.sdk.spec.A2AClientException; |
| 18 | +import org.jspecify.annotations.Nullable; |
| 19 | + |
| 20 | +/** Retains original JSON values in fields that native protobuf interceptors have not changed. */ |
| 21 | +final class JSONRPCCompat03PayloadSupport { |
| 22 | + private JSONRPCCompat03PayloadSupport() { |
| 23 | + } |
| 24 | + |
| 25 | + static MessageSendParams_v0_3 preserveUnchangedValues(MessageSendParams_v0_3 original, |
| 26 | + MessageSendParams_v0_3 before, MessageSendParams_v0_3 after) { |
| 27 | + Message_v0_3 source = original.message(); |
| 28 | + Message_v0_3 baseline = before.message(); |
| 29 | + Message_v0_3 modified = after.message(); |
| 30 | + List<Part_v0_3<?>> parts = modified.parts(); |
| 31 | + if (baseline.parts().equals(modified.parts())) { |
| 32 | + parts = source.parts(); |
| 33 | + } else if (source.parts().size() == 1 && baseline.parts().size() == 1 && modified.parts().size() == 1) { |
| 34 | + parts = List.of(restorePart(source.parts().get(0), baseline.parts().get(0), modified.parts().get(0))); |
| 35 | + } else if (hasLossyNumbers(source.parts(), baseline.parts())) { |
| 36 | + throw ambiguousArray(); |
| 37 | + } |
| 38 | + Message_v0_3 message = new Message_v0_3(modified.role(), parts, modified.messageId(), modified.contextId(), |
| 39 | + modified.taskId(), modified.referenceTaskIds(), |
| 40 | + restoreMap(source.metadata(), baseline.metadata(), modified.metadata()), modified.extensions()); |
| 41 | + return new MessageSendParams_v0_3(message, after.configuration(), |
| 42 | + restoreMap(original.metadata(), before.metadata(), after.metadata())); |
| 43 | + } |
| 44 | + |
| 45 | + static TaskIdParams_v0_3 preserveUnchangedValues(TaskIdParams_v0_3 original, |
| 46 | + TaskIdParams_v0_3 before, TaskIdParams_v0_3 after) { |
| 47 | + return new TaskIdParams_v0_3(after.id(), restoreMap(original.metadata(), before.metadata(), after.metadata())); |
| 48 | + } |
| 49 | + |
| 50 | + private static Part_v0_3<?> restorePart(Part_v0_3<?> original, Part_v0_3<?> before, Part_v0_3<?> after) { |
| 51 | + if (before.equals(after)) { |
| 52 | + return original; |
| 53 | + } |
| 54 | + @Nullable Map<String, Object> metadata = restoreMap(partMetadata(original), partMetadata(before), partMetadata(after)); |
| 55 | + if (after instanceof DataPart_v0_3 modified) { |
| 56 | + Map<String, Object> data = modified.data(); |
| 57 | + if (original instanceof DataPart_v0_3 source && before instanceof DataPart_v0_3 baseline) { |
| 58 | + data = Objects.requireNonNull(restoreMap(source.data(), baseline.data(), modified.data())); |
| 59 | + } |
| 60 | + return new DataPart_v0_3(data, metadata); |
| 61 | + } |
| 62 | + if (after instanceof TextPart_v0_3 modified) { |
| 63 | + return new TextPart_v0_3(modified.text(), metadata); |
| 64 | + } |
| 65 | + FilePart_v0_3 modified = (FilePart_v0_3) after; |
| 66 | + return new FilePart_v0_3(modified.file(), metadata); |
| 67 | + } |
| 68 | + |
| 69 | + private static @Nullable Map<String, Object> partMetadata(Part_v0_3<?> part) { |
| 70 | + if (part instanceof DataPart_v0_3 data) { |
| 71 | + return data.metadata(); |
| 72 | + } |
| 73 | + if (part instanceof TextPart_v0_3 text) { |
| 74 | + return text.metadata(); |
| 75 | + } |
| 76 | + return ((FilePart_v0_3) part).metadata(); |
| 77 | + } |
| 78 | + |
| 79 | + @SuppressWarnings("unchecked") |
| 80 | + private static @Nullable Map<String, Object> restoreMap(@Nullable Map<String, Object> original, |
| 81 | + @Nullable Map<String, Object> before, @Nullable Map<String, Object> after) { |
| 82 | + return (Map<String, Object>) restore(original, before, after); |
| 83 | + } |
| 84 | + |
| 85 | + private static @Nullable Object restore(@Nullable Object original, @Nullable Object before, @Nullable Object after) { |
| 86 | + // Struct/Value uses doubles. Restore unchanged values directly from the original objects, |
| 87 | + // avoiding another JSON parse that could itself round large integers or decimal numbers. |
| 88 | + if (Objects.equals(before, after)) { |
| 89 | + return original; |
| 90 | + } |
| 91 | + if (original instanceof Map<?, ?> source && before instanceof Map<?, ?> baseline |
| 92 | + && after instanceof Map<?, ?> modified) { |
| 93 | + Map<Object, @Nullable Object> result = new LinkedHashMap<>(); |
| 94 | + modified.forEach((key, value) -> result.put(key, source.containsKey(key) && baseline.containsKey(key) |
| 95 | + ? restore(source.get(key), baseline.get(key), value) : value)); |
| 96 | + return result; |
| 97 | + } |
| 98 | + if (original instanceof List<?> source && before instanceof List<?> baseline && after instanceof List<?> modified) { |
| 99 | + if (source.size() == 1 && baseline.size() == 1 && modified.size() == 1) { |
| 100 | + // Array slots have no identity. Only a singleton has an unambiguous slot. |
| 101 | + List<@Nullable Object> result = new ArrayList<>(); |
| 102 | + result.add(restore(source.get(0), baseline.get(0), modified.get(0))); |
| 103 | + return result; |
| 104 | + } |
| 105 | + if (hasLossyNumbers(source, baseline)) { |
| 106 | + throw ambiguousArray(); |
| 107 | + } |
| 108 | + } |
| 109 | + return after; |
| 110 | + } |
| 111 | + |
| 112 | + private static boolean hasLossyNumbers(@Nullable Object original, @Nullable Object projected) { |
| 113 | + if (original instanceof Number source && projected instanceof Number baseline) { |
| 114 | + return !Double.isFinite(baseline.doubleValue()) |
| 115 | + || new BigDecimal(source.toString()).compareTo(new BigDecimal(baseline.toString())) != 0; |
| 116 | + } |
| 117 | + if (original instanceof DataPart_v0_3 source && projected instanceof DataPart_v0_3 baseline) { |
| 118 | + return hasLossyNumbers(source.metadata(), baseline.metadata()) |
| 119 | + || hasLossyNumbers(source.data(), baseline.data()); |
| 120 | + } |
| 121 | + if (original instanceof TextPart_v0_3 source && projected instanceof TextPart_v0_3 baseline) { |
| 122 | + return hasLossyNumbers(source.metadata(), baseline.metadata()); |
| 123 | + } |
| 124 | + if (original instanceof FilePart_v0_3 source && projected instanceof FilePart_v0_3 baseline) { |
| 125 | + return hasLossyNumbers(source.metadata(), baseline.metadata()); |
| 126 | + } |
| 127 | + if (original instanceof Map<?, ?> source && projected instanceof Map<?, ?> baseline) { |
| 128 | + for (var entry : source.entrySet()) { |
| 129 | + if (hasLossyNumbers(entry.getValue(), baseline.get(entry.getKey()))) { |
| 130 | + return true; |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + if (original instanceof List<?> source && projected instanceof List<?> baseline) { |
| 135 | + for (int i = 0; i < source.size() && i < baseline.size(); i++) { |
| 136 | + if (hasLossyNumbers(source.get(i), baseline.get(i))) { |
| 137 | + return true; |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + return false; |
| 142 | + } |
| 143 | + |
| 144 | + private static A2AClientException ambiguousArray() { |
| 145 | + return new A2AClientException("Cannot preserve exact JSON numbers after a protobuf interceptor edits " |
| 146 | + + "a non-singleton array. Keep that array unchanged or use strings for exact numeric identifiers."); |
| 147 | + } |
| 148 | +} |
0 commit comments