Skip to content

Commit 4f245f6

Browse files
Doris26copybara-github
authored andcommitted
refactor: Add YAML snake_case to camelCase preprocessing and GenerateContentConfig support
This CL introduces a YAML preprocessor that converts snake_case keys to camelCase, aligning YAML configurations with Java naming conventions. It also integrates GenerateContentConfig into LlmAgent, allowing detailed control over content generation parameters. Jackson @JsonProperty annotations are removed as they are no longer necessary due to the preprocessing. PiperOrigin-RevId: 803533936
1 parent bc3ae43 commit 4f245f6

12 files changed

Lines changed: 862 additions & 66 deletions

File tree

core/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@
8181
<artifactId>auto-value-annotations</artifactId>
8282
<scope>provided</scope>
8383
</dependency>
84+
<dependency>
85+
<groupId>com.google.guava</groupId>
86+
<artifactId>guava</artifactId>
87+
<version>33.0.0-jre</version>
88+
</dependency>
8489
<dependency>
8590
<groupId>com.google.errorprone</groupId>
8691
<artifactId>error_prone_annotations</artifactId>

core/src/main/java/com/google/adk/agents/BaseAgentConfig.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package com.google.adk.agents;
1818

19-
import com.fasterxml.jackson.annotation.JsonProperty;
2019
import java.util.List;
2120

2221
/**
@@ -66,7 +65,6 @@ public AgentRefConfig(String name, String className, String staticField) {
6665
this.staticField = staticField;
6766
}
6867

69-
@JsonProperty("name")
7068
public String name() {
7169
return name;
7270
}
@@ -75,7 +73,6 @@ public void setName(String name) {
7573
this.name = name;
7674
}
7775

78-
@JsonProperty("config_path")
7976
public String configPath() {
8077
return configPath;
8178
}
@@ -84,7 +81,6 @@ public void setConfigPath(String configPath) {
8481
this.configPath = configPath;
8582
}
8683

87-
@JsonProperty("class_name")
8884
public String className() {
8985
return className;
9086
}
@@ -93,7 +89,6 @@ public void setClassName(String className) {
9389
this.className = className;
9490
}
9591

96-
@JsonProperty("static_field")
9792
public String staticField() {
9893
return staticField;
9994
}
@@ -133,7 +128,6 @@ public BaseAgentConfig(String name, String description, String agentClass) {
133128
this.agentClass = agentClass;
134129
}
135130

136-
@JsonProperty(value = "name", required = true)
137131
public String name() {
138132
return name;
139133
}
@@ -142,7 +136,6 @@ public void setName(String name) {
142136
this.name = name;
143137
}
144138

145-
@JsonProperty("description")
146139
public String description() {
147140
return description;
148141
}
@@ -151,7 +144,6 @@ public void setDescription(String description) {
151144
this.description = description;
152145
}
153146

154-
@JsonProperty("agent_class")
155147
public String agentClass() {
156148
return agentClass;
157149
}
@@ -160,7 +152,6 @@ public void setAgentClass(String agentClass) {
160152
this.agentClass = agentClass;
161153
}
162154

163-
@JsonProperty("sub_agents")
164155
public List<AgentRefConfig> subAgents() {
165156
return subAgents;
166157
}

core/src/main/java/com/google/adk/agents/ConfigAgentUtils.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717
package com.google.adk.agents;
1818

1919
import com.fasterxml.jackson.databind.DeserializationFeature;
20+
import com.fasterxml.jackson.databind.MapperFeature;
2021
import com.fasterxml.jackson.databind.ObjectMapper;
22+
import com.fasterxml.jackson.databind.json.JsonMapper;
2123
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
2224
import com.google.adk.utils.ComponentRegistry;
2325
import com.google.common.collect.ImmutableList;
2426
import java.io.File;
25-
import java.io.FileInputStream;
2627
import java.io.IOException;
27-
import java.io.InputStream;
28+
import java.nio.charset.StandardCharsets;
2829
import java.nio.file.Files;
2930
import java.nio.file.Path;
3031
import java.nio.file.Paths;
@@ -187,11 +188,18 @@ private static BaseAgent resolveSubAgentFromConfigPath(
187188
*/
188189
private static <T extends BaseAgentConfig> T loadConfigAsType(
189190
String configPath, Class<T> configClass) throws ConfigurationException {
190-
try (InputStream inputStream = new FileInputStream(configPath)) {
191-
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
192-
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
193-
mapper.enable(com.fasterxml.jackson.databind.MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS);
194-
return mapper.readValue(inputStream, configClass);
191+
try {
192+
String yamlContent = Files.readString(Paths.get(configPath), StandardCharsets.UTF_8);
193+
194+
// Preprocess YAML to convert snake_case to camelCase
195+
String processedYaml = YamlPreprocessor.preprocessYaml(yamlContent);
196+
197+
ObjectMapper mapper =
198+
JsonMapper.builder(new YAMLFactory())
199+
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
200+
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS)
201+
.build();
202+
return mapper.readValue(processedYaml, configClass);
195203
} catch (IOException e) {
196204
throw new ConfigurationException("Failed to load or parse config file: " + configPath, e);
197205
}

core/src/main/java/com/google/adk/agents/LlmAgent.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,11 @@ public static LlmAgent fromConfig(LlmAgentConfig config, String configAbsPath)
965965
builder.includeContents(config.includeContents());
966966
}
967967

968+
// Set optional generateContentConfig
969+
if (config.generateContentConfig() != null) {
970+
builder.generateContentConfig(config.generateContentConfig());
971+
}
972+
968973
// Build and return the agent
969974
LlmAgent agent = builder.build();
970975
logger.info(

core/src/main/java/com/google/adk/agents/LlmAgentConfig.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
package com.google.adk.agents;
1818

19-
import com.fasterxml.jackson.annotation.JsonProperty;
2019
import com.google.adk.agents.LlmAgent.IncludeContents;
2120
import com.google.adk.tools.BaseTool.ToolConfig;
21+
import com.google.genai.types.GenerateContentConfig;
2222
import java.util.List;
2323

2424
/**
@@ -34,14 +34,14 @@ public class LlmAgentConfig extends BaseAgentConfig {
3434
private String outputKey;
3535
private List<ToolConfig> tools;
3636
private IncludeContents includeContents;
37+
private GenerateContentConfig generateContentConfig;
3738

3839
public LlmAgentConfig() {
3940
super();
4041
setAgentClass("LlmAgent");
4142
}
4243

43-
// Non-standard accessors with JsonProperty annotations
44-
@JsonProperty("model")
44+
// Accessors
4545
public String model() {
4646
return model;
4747
}
@@ -50,7 +50,6 @@ public void setModel(String model) {
5050
this.model = model;
5151
}
5252

53-
@JsonProperty(value = "instruction", required = true)
5453
public String instruction() {
5554
return instruction;
5655
}
@@ -59,7 +58,6 @@ public void setInstruction(String instruction) {
5958
this.instruction = instruction;
6059
}
6160

62-
@JsonProperty("disallow_transfer_to_parent")
6361
public Boolean disallowTransferToParent() {
6462
return disallowTransferToParent;
6563
}
@@ -68,7 +66,6 @@ public void setDisallowTransferToParent(Boolean disallowTransferToParent) {
6866
this.disallowTransferToParent = disallowTransferToParent;
6967
}
7068

71-
@JsonProperty("disallow_transfer_to_peers")
7269
public Boolean disallowTransferToPeers() {
7370
return disallowTransferToPeers;
7471
}
@@ -77,7 +74,6 @@ public void setDisallowTransferToPeers(Boolean disallowTransferToPeers) {
7774
this.disallowTransferToPeers = disallowTransferToPeers;
7875
}
7976

80-
@JsonProperty("output_key")
8177
public String outputKey() {
8278
return outputKey;
8379
}
@@ -86,7 +82,6 @@ public void setOutputKey(String outputKey) {
8682
this.outputKey = outputKey;
8783
}
8884

89-
@JsonProperty("tools")
9085
public List<ToolConfig> tools() {
9186
return tools;
9287
}
@@ -95,12 +90,19 @@ public void setTools(List<ToolConfig> tools) {
9590
this.tools = tools;
9691
}
9792

98-
@JsonProperty("include_contents")
9993
public IncludeContents includeContents() {
10094
return includeContents;
10195
}
10296

10397
public void setIncludeContents(IncludeContents includeContents) {
10498
this.includeContents = includeContents;
10599
}
100+
101+
public GenerateContentConfig generateContentConfig() {
102+
return generateContentConfig;
103+
}
104+
105+
public void setGenerateContentConfig(GenerateContentConfig generateContentConfig) {
106+
this.generateContentConfig = generateContentConfig;
107+
}
106108
}

0 commit comments

Comments
 (0)