# Copyright 2025 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from google.adk.tools import ToolContext, FunctionTool from google.genai import types def process_document( document_name: str, analysis_query: str, tool_context: ToolContext ) -> dict: """Analyzes a document using context from memory.""" # 1. Load the artifact print(f"Tool: Attempting to load artifact: {document_name}") document_part = tool_context.load_artifact(document_name) if not document_part: return {"status": "error", "message": f"Document '{document_name}' not found."} document_text = document_part.text # Assuming it's text for simplicity print(f"Tool: Loaded document '{document_name}' ({len(document_text)} chars).") # 2. Search memory for related context print(f"Tool: Searching memory for context related to: '{analysis_query}'") memory_response = tool_context.search_memory( f"Context for analyzing document about {analysis_query}" ) memory_context = "\n".join( [ m.events[0].content.parts[0].text for m in memory_response.memories if m.events and m.events[0].content ] ) # Simplified extraction print(f"Tool: Found memory context: {memory_context[:100]}...") # 3. Perform analysis (placeholder) analysis_result = f"Analysis of '{document_name}' regarding '{analysis_query}' using memory context: [Placeholder Analysis Result]" print("Tool: Performed analysis.") # 4. Save the analysis result as a new artifact analysis_part = types.Part.from_text(text=analysis_result) new_artifact_name = f"analysis_{document_name}" version = await tool_context.save_artifact(new_artifact_name, analysis_part) print(f"Tool: Saved analysis result as '{new_artifact_name}' version {version}.") return { "status": "success", "analysis_artifact": new_artifact_name, "version": version, } doc_analysis_tool = FunctionTool(func=process_document) # In an Agent: # Assume artifact 'report.txt' was previously saved. # Assume memory service is configured and has relevant past data. # my_agent = Agent(..., tools=[doc_analysis_tool], artifact_service=..., memory_service=...)