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
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,8 @@ public InMemoryArtifactService() {
public Single<Integer> saveArtifact(
String appName, String userId, String sessionId, String filename, Part artifact) {
List<Part> versions =
artifacts
.computeIfAbsent(appName, k -> new HashMap<>())
.computeIfAbsent(userId, k -> new HashMap<>())
.computeIfAbsent(sessionId, k -> new HashMap<>())
.computeIfAbsent(filename, k -> new ArrayList<>());
getArtifactsMap(appName, userId, sessionId)
.computeIfAbsent(filename, unused -> new ArrayList<>());
versions.add(artifact);
return Single.just(versions.size() - 1);
}
Expand All @@ -66,11 +63,8 @@ public Single<Integer> saveArtifact(
public Maybe<Part> loadArtifact(
String appName, String userId, String sessionId, String filename, Optional<Integer> version) {
List<Part> versions =
artifacts
.getOrDefault(appName, new HashMap<>())
.getOrDefault(userId, new HashMap<>())
.getOrDefault(sessionId, new HashMap<>())
.getOrDefault(filename, new ArrayList<>());
getArtifactsMap(appName, userId, sessionId)
.computeIfAbsent(filename, unused -> new ArrayList<>());

if (versions.isEmpty()) {
return Maybe.empty();
Expand All @@ -97,13 +91,7 @@ public Single<ListArtifactsResponse> listArtifactKeys(
String appName, String userId, String sessionId) {
return Single.just(
ListArtifactsResponse.builder()
.filenames(
ImmutableList.copyOf(
artifacts
.getOrDefault(appName, new HashMap<>())
.getOrDefault(userId, new HashMap<>())
.getOrDefault(sessionId, new HashMap<>())
.keySet()))
.filenames(ImmutableList.copyOf(getArtifactsMap(appName, userId, sessionId).keySet()))
.build());
}

Expand All @@ -115,11 +103,7 @@ public Single<ListArtifactsResponse> listArtifactKeys(
@Override
public Completable deleteArtifact(
String appName, String userId, String sessionId, String filename) {
artifacts
.getOrDefault(appName, new HashMap<>())
.getOrDefault(userId, new HashMap<>())
.getOrDefault(sessionId, new HashMap<>())
.remove(filename);
getArtifactsMap(appName, userId, sessionId).remove(filename);
return Completable.complete();
}

Expand All @@ -132,15 +116,19 @@ public Completable deleteArtifact(
public Single<ImmutableList<Integer>> listVersions(
String appName, String userId, String sessionId, String filename) {
int size =
artifacts
.getOrDefault(appName, new HashMap<>())
.getOrDefault(userId, new HashMap<>())
.getOrDefault(sessionId, new HashMap<>())
.getOrDefault(filename, new ArrayList<>())
getArtifactsMap(appName, userId, sessionId)
.computeIfAbsent(filename, unused -> new ArrayList<>())
.size();
if (size == 0) {
return Single.just(ImmutableList.of());
}
return Single.just(IntStream.range(0, size).boxed().collect(toImmutableList()));
}

private Map<String, List<Part>> getArtifactsMap(String appName, String userId, String sessionId) {
return artifacts
.computeIfAbsent(appName, unused -> new HashMap<>())
.computeIfAbsent(userId, unused -> new HashMap<>())
.computeIfAbsent(sessionId, unused -> new HashMap<>());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public void setExecutionId(String sessionId) {
* @return A list of processed file names in the code executor context.
*/
public List<String> getProcessedFileNames() {
return (List<String>) this.context.getOrDefault(PROCESSED_FILE_NAMES_KEY, new ArrayList<>());
return (List<String>)
this.context.computeIfAbsent(PROCESSED_FILE_NAMES_KEY, unused -> new ArrayList<>());
}

/**
Expand All @@ -100,7 +101,7 @@ public List<String> getProcessedFileNames() {
public void addProcessedFileNames(List<String> fileNames) {
List<String> processedFileNames =
(List<String>)
this.context.computeIfAbsent(PROCESSED_FILE_NAMES_KEY, k -> new ArrayList<>());
this.context.computeIfAbsent(PROCESSED_FILE_NAMES_KEY, unused -> new ArrayList<>());
processedFileNames.addAll(fileNames);
}

Expand All @@ -126,7 +127,7 @@ public List<File> getInputFiles() {
public void addInputFiles(List<File> inputFiles) {
List<Map<String, Object>> fileMaps =
(List<Map<String, Object>>)
this.sessionState.computeIfAbsent(INPUT_FILE_KEY, k -> new ArrayList<>());
this.sessionState.computeIfAbsent(INPUT_FILE_KEY, unused -> new ArrayList<>());
for (File inputFile : inputFiles) {
fileMaps.add(
objectMapper.convertValue(inputFile, new TypeReference<Map<String, Object>>() {}));
Expand Down Expand Up @@ -166,7 +167,7 @@ public int getErrorCount(String invocationId) {
public void incrementErrorCount(String invocationId) {
Map<String, Integer> errorCounts =
(Map<String, Integer>)
this.sessionState.computeIfAbsent(ERROR_COUNT_KEY, k -> new HashMap<>());
this.sessionState.computeIfAbsent(ERROR_COUNT_KEY, unused -> new HashMap<>());
errorCounts.put(invocationId, getErrorCount(invocationId) + 1);
}

Expand All @@ -176,9 +177,6 @@ public void incrementErrorCount(String invocationId) {
* @param invocationId The invocation ID to reset the error count for.
*/
public void resetErrorCount(String invocationId) {
if (!this.sessionState.containsKey(ERROR_COUNT_KEY)) {
return;
}
Map<String, Integer> errorCounts =
(Map<String, Integer>) this.sessionState.get(ERROR_COUNT_KEY);
if (errorCounts != null) {
Expand All @@ -198,9 +196,10 @@ public void updateCodeExecutionResult(
String invocationId, String code, String resultStdout, String resultStderr) {
Map<String, List<Map<String, Object>>> codeExecutionResults =
(Map<String, List<Map<String, Object>>>)
this.sessionState.computeIfAbsent(CODE_EXECUTION_RESULTS_KEY, k -> new HashMap<>());
this.sessionState.computeIfAbsent(
CODE_EXECUTION_RESULTS_KEY, unused -> new HashMap<>());
List<Map<String, Object>> resultsForInvocation =
codeExecutionResults.computeIfAbsent(invocationId, k -> new ArrayList<>());
codeExecutionResults.computeIfAbsent(invocationId, unused -> new ArrayList<>());
Map<String, Object> newResult = new HashMap<>();
newResult.put("code", code);
newResult.put("result_stdout", resultStdout);
Expand All @@ -210,6 +209,7 @@ public void updateCodeExecutionResult(
}

private Map<String, Object> getCodeExecutorContext(Map<String, Object> sessionState) {
return (Map<String, Object>) sessionState.computeIfAbsent(CONTEXT_KEY, k -> new HashMap<>());
return (Map<String, Object>)
sessionState.computeIfAbsent(CONTEXT_KEY, unused -> new HashMap<>());
}
}
10 changes: 2 additions & 8 deletions core/src/main/java/com/google/adk/flows/llmflows/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Function;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -332,7 +331,7 @@ private static Maybe<Map<String, Object>> processFunctionLive(
ActiveStreamingTool activeTool =
invocationContext
.activeStreamingTools()
.getOrDefault(tool.name(), new ActiveStreamingTool(subscription));
.computeIfAbsent(tool.name(), unused -> new ActiveStreamingTool(subscription));
activeTool.task(subscription);
invocationContext.activeStreamingTools().put(tool.name(), activeTool);

Expand Down Expand Up @@ -607,12 +606,7 @@ private static Event buildResponseEvent(
.invocationId(invocationContext.invocationId())
.author(invocationContext.agent().name())
.branch(invocationContext.branch())
.content(
Optional.of(
Content.builder()
.role("user")
.parts(Collections.singletonList(partFunctionResponse))
.build()))
.content(Content.builder().role("user").parts(partFunctionResponse).build())
.actions(toolContext.eventActions())
.build();
Tracing.traceToolResponse(invocationContext, event.id(), event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ public class LiveAudioSingleAgent {
.model("gemini-2.0-flash-live-001")
.description("A helpful weather assistant that provides weather information.")
.instruction(
"You are a friendly weather assistant. When users ask about weather, "
+ "you MUST call the getWeather tool with the location name. "
+ "Extract the location from the user's question. "
+ "ALWAYS use the getWeather tool to get accurate information - never make up weather data. "
+ "After getting the tool result, provide a friendly and descriptive response. "
+ "For general conversation or greetings, respond naturally and helpfully. "
+ "Do NOT use code execution for anything.")
"You are a friendly weather assistant. When users ask about weather, you MUST call"
+ " the getWeather tool with the location name. Extract the location from the"
+ " user's question. ALWAYS use the getWeather tool to get accurate information -"
+ " never make up weather data. After getting the tool result, provide a friendly"
+ " and descriptive response. For general conversation or greetings, respond"
+ " naturally and helpfully. Do NOT use code execution for anything.")
.tools(FunctionTool.create(LiveAudioSingleAgent.class, "getWeather"))
.build();

Expand Down Expand Up @@ -89,16 +88,17 @@ public static Map<String, String> getWeather(

String normalizedLocation = location.toLowerCase().trim();

return weatherData.getOrDefault(
return weatherData.computeIfAbsent(
normalizedLocation,
Map.of(
"status",
"error",
"report",
String.format(
"Weather information for '%s' is not available. Try New York, London, Tokyo, or"
+ " Sydney.",
location)));
unused ->
Map.of(
"status",
"error",
"report",
String.format(
"Weather information for '%s' is not available. Try New York, London, Tokyo,"
+ " or Sydney.",
location)));
}

public static void main(String[] args) {
Expand Down