AI-Studio/app/MindWork AI Studio/Tools/ToolCallingSystem/ToolSettingsValueParser.cs
Peer Hogeterp 7cedd03adb
Some checks are pending
Build and Release / Determine run mode (push) Waiting to run
Build and Release / Read metadata (push) Blocked by required conditions
Build and Release / Sync Flatpak repo (push) Blocked by required conditions
Build and Release / Collect Flatpak artifacts (push) Blocked by required conditions
Build and Release / Verify (push) Waiting to run
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-apple-darwin, osx-arm64, macos-latest, aarch64-apple-darwin, dmg,app,updater, dmg) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-pc-windows-msvc.exe, win-arm64, windows-latest, aarch64-pc-windows-msvc, nsis,updater, nsis) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-unknown-linux-gnu, linux-arm64, ubuntu-22.04-arm, aarch64-unknown-linux-gnu, appimage,updater, appimage) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-apple-darwin, osx-x64, macos-latest, x86_64-apple-darwin, dmg,app,updater, dmg) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-pc-windows-msvc.exe, win-x64, windows-latest, x86_64-pc-windows-msvc, nsis,updater, nsis) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-unknown-linux-gnu, linux-x64, ubuntu-22.04, x86_64-unknown-linux-gnu, appimage,updater, appimage) (push) Blocked by required conditions
Build and Release / Prepare & create release (push) Blocked by required conditions
Build and Release / Publish release (push) Blocked by required conditions
Added a free address choice to the Read Web Page tool (#997)
Co-authored-by: Thorsten Sommer <SommerEngineering@users.noreply.github.com>
2026-09-27 21:46:16 +02:00

75 lines
2.7 KiB
C#

namespace AIStudio.Tools.ToolCallingSystem;
internal static class ToolSettingsValueParser
{
public static int? ReadOptionalPositiveInt(IReadOnlyDictionary<string, string> settingsValues, string key)
{
if (!settingsValues.TryGetValue(key, out var value) || string.IsNullOrWhiteSpace(value))
return null;
return int.TryParse(value, out var parsedValue) && parsedValue > 0 ? parsedValue : null;
}
public static bool TryReadOptionalPositiveInt(
IReadOnlyDictionary<string, string> settingsValues,
string key,
string invalidValueErrorFormat,
out int? value,
out string error)
{
value = null;
error = string.Empty;
if (!settingsValues.TryGetValue(key, out var rawValue) || string.IsNullOrWhiteSpace(rawValue))
return true;
if (int.TryParse(rawValue, out var parsedValue) && parsedValue > 0)
{
value = parsedValue;
return true;
}
error = string.Format(invalidValueErrorFormat, key);
return false;
}
public static bool TryReadBoundedOptionalPositiveInt(
IReadOnlyDictionary<string, string> settingsValues,
string key,
int maximum,
string invalidValueErrorFormat,
string maximumErrorFormat,
out int? value,
out string error)
{
if (!TryReadOptionalPositiveInt(settingsValues, key, invalidValueErrorFormat, out value, out error))
return false;
if (value is null || value <= maximum)
return true;
error = string.Format(maximumErrorFormat, key, maximum);
return false;
}
/// <summary>
/// Checks that a stored value is one the option source still offers.
/// </summary>
/// <remarks>
/// An empty value passes: whether the field may be empty is decided by the settings schema's
/// required list, which the tool settings service checks before this method runs.<br/><br/>
/// The error text comes from the caller, like the other checks here, so that it is translated
/// in the tool which reports it. It receives the key as {0} and the stored value as {1}.
/// </remarks>
public static bool TryValidateOptionValue(IReadOnlyDictionary<string, string> settingsValues, string key, string optionSource, string invalidValueErrorFormat, out string error)
{
error = string.Empty;
var value = settingsValues.GetValueOrDefault(key);
if (string.IsNullOrWhiteSpace(value) || ToolSettingsOptionSources.GetValues(optionSource).Contains(value))
return true;
error = string.Format(invalidValueErrorFormat, key, value);
return false;
}
}