sample_tools.py
1 from google.adk.tools import ToolContext 2 from google.genai import types as adk_types 3 import base64 4 import binascii 5 6 7 def calculate_square(number: float) -> float: 8 """Calculates the square of the input number.""" 9 return number * number 10 11 12 def create_file( 13 filename: str, 14 mimeType: str, 15 content: str, 16 return_immediately: bool, 17 tool_context: ToolContext = None, 18 ) -> dict: 19 """ 20 EXAMPLE TOOL: Creates a file using the artifact service. 21 22 This is an *example* tool. The actual built-in tool used by the host 23 has similar functionality but also handles signaling for immediate return. 24 This example tool does NOT perform signaling. 25 26 Args: 27 filename: The name of the file to create. 28 mimeType: The MIME type of the file content (e.g., 'text/plain', 'image/png', 'application/json'). 29 content: The content of the file, potentially base64 encoded if binary. 30 return_immediately: If True, indicates the caller *wants* this artifact 31 returned immediately (but this example tool doesn't act on it). 32 Must be explicitly provided. 33 tool_context: The context provided by the ADK framework. 34 35 Returns: 36 A dictionary confirming the file creation and its version or an error. 37 Includes filename and version as required. 38 """ 39 if not tool_context: 40 print("Error: ToolContext is missing in create_file example tool.") 41 return { 42 "status": "error", 43 "filename": filename, 44 "message": "ToolContext is missing.", 45 } 46 47 try: 48 file_bytes: bytes 49 final_mime_type = mimeType 50 is_likely_binary = ( 51 mimeType 52 and not mimeType.startswith("text/") 53 and mimeType != "application/json" 54 and mimeType != "application/yaml" 55 and mimeType != "application/xml" 56 ) 57 58 if is_likely_binary: 59 try: 60 file_bytes = base64.b64decode(content, validate=True) 61 final_mime_type = mimeType 62 except (binascii.Error, ValueError) as decode_error: 63 print( 64 f"Warning: Failed to base64 decode content for mimeType '{mimeType}'. Treating as text/plain. Error: {decode_error}" 65 ) 66 file_bytes = content.encode("utf-8") 67 final_mime_type = "text/plain" 68 else: 69 file_bytes = content.encode("utf-8") 70 if not mimeType or not ( 71 mimeType.startswith("text/") 72 or mimeType 73 in [ 74 "application/json", 75 "application/yaml", 76 "application/xml", 77 "application/csv", 78 ] 79 ): 80 print( 81 f"Warning: Provided mimeType '{mimeType}' doesn't look like text. Using 'text/plain'." 82 ) 83 final_mime_type = "text/plain" 84 else: 85 final_mime_type = mimeType 86 87 artifact_part = adk_types.Part.from_bytes( 88 data=file_bytes, mime_type=final_mime_type 89 ) 90 version = tool_context.save_artifact(filename=filename, artifact=artifact_part) 91 92 if return_immediately: 93 print( 94 f"Info: Example create_file called with return_immediately=True for '{filename}', but this example tool does not signal." 95 ) 96 97 return { 98 "status": "success", 99 "filename": filename, 100 "version": version, 101 "mimeType": final_mime_type, 102 "message": f"File '{filename}' (version {version}, type {final_mime_type}) saved successfully by example tool.", 103 } 104 except Exception as e: 105 print(f"Error creating file '{filename}' in example tool: {e}") 106 return { 107 "status": "error", 108 "filename": filename, 109 "message": f"Failed to create file in example tool: {e}", 110 }