diff --git a/core/src/main/java/com/google/adk/artifacts/InMemoryArtifactService.java b/core/src/main/java/com/google/adk/artifacts/InMemoryArtifactService.java index 890820196..27b85136d 100644 --- a/core/src/main/java/com/google/adk/artifacts/InMemoryArtifactService.java +++ b/core/src/main/java/com/google/adk/artifacts/InMemoryArtifactService.java @@ -48,11 +48,8 @@ public InMemoryArtifactService() { public Single saveArtifact( String appName, String userId, String sessionId, String filename, Part artifact) { List 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); } @@ -66,11 +63,8 @@ public Single saveArtifact( public Maybe loadArtifact( String appName, String userId, String sessionId, String filename, Optional version) { List 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(); @@ -97,13 +91,7 @@ public Single 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()); } @@ -115,11 +103,7 @@ public Single 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(); } @@ -132,15 +116,19 @@ public Completable deleteArtifact( public Single> 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> getArtifactsMap(String appName, String userId, String sessionId) { + return artifacts + .computeIfAbsent(appName, unused -> new HashMap<>()) + .computeIfAbsent(userId, unused -> new HashMap<>()) + .computeIfAbsent(sessionId, unused -> new HashMap<>()); + } } diff --git a/core/src/main/java/com/google/adk/codeexecutors/CodeExecutorContext.java b/core/src/main/java/com/google/adk/codeexecutors/CodeExecutorContext.java index bad83ebc0..a34102225 100644 --- a/core/src/main/java/com/google/adk/codeexecutors/CodeExecutorContext.java +++ b/core/src/main/java/com/google/adk/codeexecutors/CodeExecutorContext.java @@ -89,7 +89,8 @@ public void setExecutionId(String sessionId) { * @return A list of processed file names in the code executor context. */ public List getProcessedFileNames() { - return (List) this.context.getOrDefault(PROCESSED_FILE_NAMES_KEY, new ArrayList<>()); + return (List) + this.context.computeIfAbsent(PROCESSED_FILE_NAMES_KEY, unused -> new ArrayList<>()); } /** @@ -100,7 +101,7 @@ public List getProcessedFileNames() { public void addProcessedFileNames(List fileNames) { List processedFileNames = (List) - this.context.computeIfAbsent(PROCESSED_FILE_NAMES_KEY, k -> new ArrayList<>()); + this.context.computeIfAbsent(PROCESSED_FILE_NAMES_KEY, unused -> new ArrayList<>()); processedFileNames.addAll(fileNames); } @@ -126,7 +127,7 @@ public List getInputFiles() { public void addInputFiles(List inputFiles) { List> fileMaps = (List>) - 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>() {})); @@ -166,7 +167,7 @@ public int getErrorCount(String invocationId) { public void incrementErrorCount(String invocationId) { Map errorCounts = (Map) - this.sessionState.computeIfAbsent(ERROR_COUNT_KEY, k -> new HashMap<>()); + this.sessionState.computeIfAbsent(ERROR_COUNT_KEY, unused -> new HashMap<>()); errorCounts.put(invocationId, getErrorCount(invocationId) + 1); } @@ -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 errorCounts = (Map) this.sessionState.get(ERROR_COUNT_KEY); if (errorCounts != null) { @@ -198,9 +196,10 @@ public void updateCodeExecutionResult( String invocationId, String code, String resultStdout, String resultStderr) { Map>> codeExecutionResults = (Map>>) - this.sessionState.computeIfAbsent(CODE_EXECUTION_RESULTS_KEY, k -> new HashMap<>()); + this.sessionState.computeIfAbsent( + CODE_EXECUTION_RESULTS_KEY, unused -> new HashMap<>()); List> resultsForInvocation = - codeExecutionResults.computeIfAbsent(invocationId, k -> new ArrayList<>()); + codeExecutionResults.computeIfAbsent(invocationId, unused -> new ArrayList<>()); Map newResult = new HashMap<>(); newResult.put("code", code); newResult.put("result_stdout", resultStdout); @@ -210,6 +209,7 @@ public void updateCodeExecutionResult( } private Map getCodeExecutorContext(Map sessionState) { - return (Map) sessionState.computeIfAbsent(CONTEXT_KEY, k -> new HashMap<>()); + return (Map) + sessionState.computeIfAbsent(CONTEXT_KEY, unused -> new HashMap<>()); } } diff --git a/core/src/main/java/com/google/adk/flows/llmflows/Functions.java b/core/src/main/java/com/google/adk/flows/llmflows/Functions.java index ce7687c3d..3bb57faee 100644 --- a/core/src/main/java/com/google/adk/flows/llmflows/Functions.java +++ b/core/src/main/java/com/google/adk/flows/llmflows/Functions.java @@ -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; @@ -332,7 +331,7 @@ private static Maybe> 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); @@ -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); diff --git a/tutorials/live-audio-single-agent/src/main/java/com/google/adk/tutorials/LiveAudioSingleAgent.java b/tutorials/live-audio-single-agent/src/main/java/com/google/adk/tutorials/LiveAudioSingleAgent.java index 85e8914a7..a1342a936 100644 --- a/tutorials/live-audio-single-agent/src/main/java/com/google/adk/tutorials/LiveAudioSingleAgent.java +++ b/tutorials/live-audio-single-agent/src/main/java/com/google/adk/tutorials/LiveAudioSingleAgent.java @@ -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(); @@ -89,16 +88,17 @@ public static Map 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) {