2026-04-09 13:37:53 +00:00
using System.Text.Json ;
2026-04-10 08:43:13 +00:00
using AIStudio.Tools.PluginSystem ;
2026-04-13 16:44:36 +00:00
namespace AIStudio.Tools.ToolCallingSystem.ToolCallingImplementations ;
2026-04-09 13:37:53 +00:00
public sealed class GetCurrentWeatherTool : IToolImplementation
{
2026-05-12 15:20:24 +00:00
private static string TB ( string fallbackEN ) = > I18N . I . T ( fallbackEN , typeof ( GetCurrentWeatherTool ) . Namespace , nameof ( GetCurrentWeatherTool ) ) ;
2026-04-09 13:37:53 +00:00
public string ImplementationKey = > "get_current_weather" ;
2026-04-10 08:43:13 +00:00
public string Icon = > Icons . Material . Filled . Cloud ;
2026-04-09 13:37:53 +00:00
public IReadOnlySet < string > SensitiveTraceArgumentNames = > new HashSet < string > ( StringComparer . Ordinal ) ;
2026-05-12 15:20:24 +00:00
public string GetDisplayName ( ) = > TB ( "Current Weather" ) ;
2026-04-10 08:43:13 +00:00
2026-05-12 15:20:24 +00:00
public string GetDescription ( ) = > TB ( "Use this demo tool to retrieve the current weather for a given city and state. It is primarily meant to demonstrate tool calling and tool settings in AI Studio." ) ;
2026-04-10 08:43:13 +00:00
public string GetSettingsFieldLabel ( string fieldName , ToolSettingsFieldDefinition fieldDefinition ) = > fieldName switch
{
2026-05-12 15:20:24 +00:00
"demoLabel" = > TB ( "Demo Label" ) ,
_ = > TB ( fieldDefinition . Title ) ,
2026-04-10 08:43:13 +00:00
} ;
public string GetSettingsFieldDescription ( string fieldName , ToolSettingsFieldDefinition fieldDefinition ) = > fieldName switch
{
2026-05-12 15:20:24 +00:00
"demoLabel" = > TB ( "Required demo setting for validating tool settings in tests. It does not affect the weather result." ) ,
_ = > TB ( fieldDefinition . Description ) ,
2026-04-10 08:43:13 +00:00
} ;
2026-04-09 13:37:53 +00:00
public Task < ToolExecutionResult > ExecuteAsync ( JsonElement arguments , ToolExecutionContext context , CancellationToken token = default )
{
var city = arguments . TryGetProperty ( "city" , out var cityValue ) ? cityValue . GetString ( ) ? ? string . Empty : string . Empty ;
var state = arguments . TryGetProperty ( "state" , out var stateValue ) ? stateValue . GetString ( ) ? ? string . Empty : string . Empty ;
var unit = arguments . TryGetProperty ( "unit" , out var unitValue ) ? unitValue . GetString ( ) ? ? string . Empty : string . Empty ;
if ( unit is not ( "celsius" or "fahrenheit" ) )
throw new ArgumentException ( $"Invalid unit '{unit}'." ) ;
return Task . FromResult ( new ToolExecutionResult
{
TextContent = $"The weather in {city}, {state} is 85 degrees {unit}. It is partly cloudy with highs in the 90's." ,
} ) ;
}
}