Skip to content

Commit 7982916

Browse files
committed
add new callback
1 parent 6dd69fb commit 7982916

9 files changed

Lines changed: 121 additions & 9 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@NullMarked
2+
package com.javaaidev.adk.artifact.file;
3+
4+
import org.jspecify.annotations.NullMarked;

callback/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
<groupId>com.google.adk</groupId>
2424
<artifactId>google-adk</artifactId>
2525
</dependency>
26+
<dependency>
27+
<groupId>org.jspecify</groupId>
28+
<artifactId>jspecify</artifactId>
29+
</dependency>
2630
</dependencies>
2731

2832
</project>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.javaaidev.adk.callback;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.google.adk.agents.CallbackContext;
5+
import com.google.adk.agents.Callbacks.AfterAgentCallback;
6+
import com.google.genai.types.Content;
7+
import io.reactivex.rxjava3.core.Maybe;
8+
import java.util.Map;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
12+
/**
13+
* Convert context state to a different type, only supports JSON string and Map
14+
*
15+
* @param sourceContextKey Context key of the source
16+
* @param targetJavaType Java type of the target
17+
* @param targetContextKey Context key of the target
18+
* @param escalateOnConversionError Should escalate when conversion failed
19+
*/
20+
public record ContextStateConvertAfterAgentCallback(
21+
String sourceContextKey,
22+
Class<?> targetJavaType,
23+
String targetContextKey,
24+
boolean escalateOnConversionError)
25+
implements AfterAgentCallback {
26+
27+
private static final Logger LOGGER =
28+
LoggerFactory.getLogger(ContextStateConvertAfterAgentCallback.class);
29+
30+
@Override
31+
public Maybe<Content> call(CallbackContext callbackContext) {
32+
var source = callbackContext.state().get(sourceContextKey);
33+
try {
34+
Object result = null;
35+
if (source instanceof String json) {
36+
result = JsonUtils.fromJson(json, targetJavaType);
37+
} else if (source instanceof Map<?, ?> map) {
38+
result = JsonUtils.fromValue(map, targetJavaType);
39+
}
40+
if (result != null) {
41+
callbackContext.state().put(targetContextKey, result);
42+
}
43+
} catch (JsonProcessingException | IllegalArgumentException e) {
44+
LOGGER.error("Failed to convert state key {} to type {}", sourceContextKey, targetJavaType);
45+
if (escalateOnConversionError) {
46+
callbackContext.eventActions().setEscalate(true);
47+
}
48+
}
49+
return Maybe.empty();
50+
}
51+
}

callback/src/main/java/com/javaaidev/adk/callback/ContextStateInjectionBeforeAgentCallback.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@
55
import com.google.genai.types.Content;
66
import io.reactivex.rxjava3.core.Maybe;
77
import java.util.Map;
8-
import java.util.Objects;
98

10-
/** Inject context state before running an agent */
11-
public class ContextStateInjectionBeforeAgentCallback implements BeforeAgentCallback {
12-
13-
private final Maybe<Map<String, Object>> stateDelta;
14-
15-
public ContextStateInjectionBeforeAgentCallback(Maybe<Map<String, Object>> stateDelta) {
16-
this.stateDelta = Objects.requireNonNull(stateDelta, "State delta cannot be null");
17-
}
9+
/**
10+
* Inject context state before running an agent
11+
*
12+
* @param stateDelta Context state to inject
13+
*/
14+
public record ContextStateInjectionBeforeAgentCallback(Maybe<Map<String, Object>> stateDelta)
15+
implements BeforeAgentCallback {
1816

1917
@Override
2018
public Maybe<Content> call(CallbackContext callbackContext) {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.javaaidev.adk.callback;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.fasterxml.jackson.databind.SerializationFeature;
6+
import org.apache.commons.lang3.StringUtils;
7+
8+
public class JsonUtils {
9+
10+
public static final ObjectMapper DEFAULT_MAPPER =
11+
new ObjectMapper().findAndRegisterModules().enable(SerializationFeature.INDENT_OUTPUT);
12+
13+
public static Object fromJson(String json, Class<?> javaType) throws JsonProcessingException {
14+
return DEFAULT_MAPPER.readValue(cleanJson(json), javaType);
15+
}
16+
17+
public static Object fromValue(Object value, Class<?> javaType) {
18+
return DEFAULT_MAPPER.convertValue(value, javaType);
19+
}
20+
21+
public static String toJson(Object value) {
22+
if (value == null) {
23+
return "{}";
24+
}
25+
try {
26+
return DEFAULT_MAPPER.writeValueAsString(value);
27+
} catch (JsonProcessingException e) {
28+
return value.toString();
29+
}
30+
}
31+
32+
public static String cleanJson(String json) {
33+
if (StringUtils.isBlank(json)) {
34+
return json;
35+
}
36+
return json.replaceAll("(?s)```json\\s*", "").replaceAll("(?s)```", "").trim();
37+
}
38+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@NullMarked
2+
package com.javaaidev.adk.callback;
3+
4+
import org.jspecify.annotations.NullMarked;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@NullMarked
2+
package com.javaaidev.adk.memory.vectorstore;
3+
4+
import org.jspecify.annotations.NullMarked;

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@
6464
<artifactId>google-adk</artifactId>
6565
<version>${adk.version}</version>
6666
</dependency>
67+
<dependency>
68+
<groupId>org.jspecify</groupId>
69+
<artifactId>jspecify</artifactId>
70+
<version>1.0.0</version>
71+
</dependency>
6772
<dependency>
6873
<groupId>ch.qos.logback</groupId>
6974
<artifactId>logback-classic</artifactId>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@NullMarked
2+
package com.javaaidev.adk.session.file;
3+
4+
import org.jspecify.annotations.NullMarked;

0 commit comments

Comments
 (0)