diff --git a/app/MindWork AI Studio/Assistants/AssistantBase.razor b/app/MindWork AI Studio/Assistants/AssistantBase.razor index 3268612d7..c45ac7d95 100644 --- a/app/MindWork AI Studio/Assistants/AssistantBase.razor +++ b/app/MindWork AI Studio/Assistants/AssistantBase.razor @@ -151,6 +151,11 @@ } + @if (this.SettingsManager.IsToolSelectionVisible(this.Component)) + { + + } + diff --git a/app/MindWork AI Studio/Assistants/AssistantBase.razor.cs b/app/MindWork AI Studio/Assistants/AssistantBase.razor.cs index 632722ab8..bc1a7387c 100644 --- a/app/MindWork AI Studio/Assistants/AssistantBase.razor.cs +++ b/app/MindWork AI Studio/Assistants/AssistantBase.razor.cs @@ -3,6 +3,7 @@ using AIStudio.Settings; using AIStudio.Dialogs.Settings; using AIStudio.Tools.Services; +using AIStudio.Tools.ToolCallingSystem; using Microsoft.AspNetCore.Components; @@ -93,6 +94,7 @@ public abstract partial class AssistantBase : AssistantLowerBase wher protected ChatThread? chatThread; protected IContent? lastUserPrompt; protected CancellationTokenSource? cancellationTokenSource; + protected HashSet selectedToolIds = []; private readonly Timer formChangeTimer = new(TimeSpan.FromSeconds(1.6)); @@ -124,6 +126,7 @@ protected override async Task OnInitializedAsync() this.providerSettings = this.SettingsManager.GetPreselectedProvider(this.Component); this.currentProfile = this.SettingsManager.GetPreselectedProfile(this.Component); this.currentChatTemplate = this.SettingsManager.GetPreselectedChatTemplate(this.Component); + this.selectedToolIds = this.SettingsManager.GetDefaultToolIds(this.Component); } protected override async Task OnParametersSetAsync() @@ -223,6 +226,7 @@ protected void CreateChatThread() ChatId = Guid.NewGuid(), Name = string.Format(this.TB("Assistant - {0}"), this.Title), Blocks = [], + RuntimeComponent = this.Component, }; } @@ -239,6 +243,7 @@ protected Guid CreateChatThread(Guid workspaceId, string name) ChatId = chatId, Name = name, Blocks = [], + RuntimeComponent = this.Component, }; return chatId; @@ -250,6 +255,12 @@ protected virtual void ResetProviderAndProfileSelection() this.currentProfile = this.SettingsManager.GetPreselectedProfile(this.Component); this.currentChatTemplate = this.SettingsManager.GetPreselectedChatTemplate(this.Component); } + + protected Task SelectedToolIdsChanged(HashSet updatedToolIds) + { + this.selectedToolIds = ToolSelectionRules.NormalizeSelection(updatedToolIds); + return Task.CompletedTask; + } protected DateTimeOffset AddUserRequest(string request, bool hideContentFromUser = false, params List attachments) { @@ -297,6 +308,10 @@ protected async Task AddAIResponseAsync(DateTimeOffset time, bool hideCo { this.chatThread.Blocks.Add(this.resultingContentBlock); this.chatThread.SelectedProvider = this.providerSettings.Id; + this.chatThread.RuntimeComponent = this.Component; + this.chatThread.RuntimeSelectedToolIds = this.SettingsManager.IsToolSelectionVisible(this.Component) + ? ToolSelectionRules.NormalizeSelection(this.selectedToolIds) + : []; } this.isProcessing = true; diff --git a/app/MindWork AI Studio/Assistants/I18N/allTexts.lua b/app/MindWork AI Studio/Assistants/I18N/allTexts.lua index c94b4b7ae..8bb520922 100644 --- a/app/MindWork AI Studio/Assistants/I18N/allTexts.lua +++ b/app/MindWork AI Studio/Assistants/I18N/allTexts.lua @@ -1684,21 +1684,42 @@ UI_TEXT_CONTENT["AISTUDIO::CHAT::CHATROLEEXTENSIONS::T601166687"] = "AI" -- Edit Message UI_TEXT_CONTENT["AISTUDIO::CHAT::CONTENTBLOCKCOMPONENT::T1183581066"] = "Edit Message" +-- Result +UI_TEXT_CONTENT["AISTUDIO::CHAT::CONTENTBLOCKCOMPONENT::T1347088452"] = "Result" + -- Do you really want to remove this message? UI_TEXT_CONTENT["AISTUDIO::CHAT::CONTENTBLOCKCOMPONENT::T1347427447"] = "Do you really want to remove this message?" -- Yes, remove the AI response and edit it UI_TEXT_CONTENT["AISTUDIO::CHAT::CONTENTBLOCKCOMPONENT::T1350385882"] = "Yes, remove the AI response and edit it" +-- Failed +UI_TEXT_CONTENT["AISTUDIO::CHAT::CONTENTBLOCKCOMPONENT::T1434043348"] = "Failed" + +-- Tool Calls ({0}) +UI_TEXT_CONTENT["AISTUDIO::CHAT::CONTENTBLOCKCOMPONENT::T1493057571"] = "Tool Calls ({0})" + +-- Executed +UI_TEXT_CONTENT["AISTUDIO::CHAT::CONTENTBLOCKCOMPONENT::T1564757972"] = "Executed" + -- Yes, regenerate it UI_TEXT_CONTENT["AISTUDIO::CHAT::CONTENTBLOCKCOMPONENT::T1603883875"] = "Yes, regenerate it" +-- No result +UI_TEXT_CONTENT["AISTUDIO::CHAT::CONTENTBLOCKCOMPONENT::T1684269223"] = "No result" + -- Yes, remove it UI_TEXT_CONTENT["AISTUDIO::CHAT::CONTENTBLOCKCOMPONENT::T1820166585"] = "Yes, remove it" -- Number of sources UI_TEXT_CONTENT["AISTUDIO::CHAT::CONTENTBLOCKCOMPONENT::T1848978959"] = "Number of sources" +-- Show {0} tool calls +UI_TEXT_CONTENT["AISTUDIO::CHAT::CONTENTBLOCKCOMPONENT::T1981771421"] = "Show {0} tool calls" + +-- Show tool call for {0} +UI_TEXT_CONTENT["AISTUDIO::CHAT::CONTENTBLOCKCOMPONENT::T2004842583"] = "Show tool call for {0}" + -- Do you really want to edit this message? In order to edit this message, the AI response will be deleted. UI_TEXT_CONTENT["AISTUDIO::CHAT::CONTENTBLOCKCOMPONENT::T2018431076"] = "Do you really want to edit this message? In order to edit this message, the AI response will be deleted." @@ -1708,6 +1729,9 @@ UI_TEXT_CONTENT["AISTUDIO::CHAT::CONTENTBLOCKCOMPONENT::T2093355991"] = "Removes -- Regenerate Message UI_TEXT_CONTENT["AISTUDIO::CHAT::CONTENTBLOCKCOMPONENT::T2308444540"] = "Regenerate Message" +-- Arguments +UI_TEXT_CONTENT["AISTUDIO::CHAT::CONTENTBLOCKCOMPONENT::T2738624831"] = "Arguments" + -- Number of attachments UI_TEXT_CONTENT["AISTUDIO::CHAT::CONTENTBLOCKCOMPONENT::T3018847255"] = "Number of attachments" @@ -1717,9 +1741,15 @@ UI_TEXT_CONTENT["AISTUDIO::CHAT::CONTENTBLOCKCOMPONENT::T3175548294"] = "Cannot -- Edit UI_TEXT_CONTENT["AISTUDIO::CHAT::CONTENTBLOCKCOMPONENT::T3267849393"] = "Edit" +-- Unknown +UI_TEXT_CONTENT["AISTUDIO::CHAT::CONTENTBLOCKCOMPONENT::T3424652889"] = "Unknown" + -- Regenerate UI_TEXT_CONTENT["AISTUDIO::CHAT::CONTENTBLOCKCOMPONENT::T3587744975"] = "Regenerate" +-- Blocked +UI_TEXT_CONTENT["AISTUDIO::CHAT::CONTENTBLOCKCOMPONENT::T3816336467"] = "Blocked" + -- Do you really want to regenerate this message? UI_TEXT_CONTENT["AISTUDIO::CHAT::CONTENTBLOCKCOMPONENT::T3878878761"] = "Do you really want to regenerate this message?" @@ -1729,9 +1759,15 @@ UI_TEXT_CONTENT["AISTUDIO::CHAT::CONTENTBLOCKCOMPONENT::T4070211974"] = "Remove -- No, keep it UI_TEXT_CONTENT["AISTUDIO::CHAT::CONTENTBLOCKCOMPONENT::T4188329028"] = "No, keep it" +-- No tool calls +UI_TEXT_CONTENT["AISTUDIO::CHAT::CONTENTBLOCKCOMPONENT::T4224149521"] = "No tool calls" + -- Export Chat to Microsoft Word UI_TEXT_CONTENT["AISTUDIO::CHAT::CONTENTBLOCKCOMPONENT::T861873672"] = "Export Chat to Microsoft Word" +-- No arguments +UI_TEXT_CONTENT["AISTUDIO::CHAT::CONTENTBLOCKCOMPONENT::T931993614"] = "No arguments" + -- The local image file does not exist. Skipping the image. UI_TEXT_CONTENT["AISTUDIO::CHAT::IIMAGESOURCEEXTENSIONS::T255679918"] = "The local image file does not exist. Skipping the image." @@ -1882,15 +1918,6 @@ UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::CONFIGURATIONMINCONFIDENCESELECTION::T252 -- Select a minimum confidence level UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::CONFIGURATIONMINCONFIDENCESELECTION::T2579793544"] = "Select a minimum confidence level" --- You have selected 1 preview feature. -UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::CONFIGURATIONMULTISELECT::T1384241824"] = "You have selected 1 preview feature." - --- No preview features selected. -UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::CONFIGURATIONMULTISELECT::T2809641588"] = "No preview features selected." - --- You have selected {0} preview features. -UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::CONFIGURATIONMULTISELECT::T3513450626"] = "You have selected {0} preview features." - -- Preselected provider UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::CONFIGURATIONPROVIDERSELECTION::T1469984996"] = "Preselected provider" @@ -2590,6 +2617,33 @@ UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::SETTINGS::SETTINGSPANELPROVIDERS::T900237 -- Export configuration UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::SETTINGS::SETTINGSPANELPROVIDERS::T975426229"] = "Export configuration" +-- Settings +UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::SETTINGS::SETTINGSPANELTOOLS::T1258653480"] = "Settings" + +-- Description +UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::SETTINGS::SETTINGSPANELTOOLS::T1725856265"] = "Description" + +-- Icon +UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::SETTINGS::SETTINGSPANELTOOLS::T1759955728"] = "Icon" + +-- Configure global settings for each tool. Tool defaults for chat and assistants are configured in the corresponding feature settings. +UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::SETTINGS::SETTINGSPANELTOOLS::T176751696"] = "Configure global settings for each tool. Tool defaults for chat and assistants are configured in the corresponding feature settings." + +-- This tool still needs to be configured. +UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::SETTINGS::SETTINGSPANELTOOLS::T1958939818"] = "This tool still needs to be configured." + +-- Missing required settings: {0} +UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::SETTINGS::SETTINGSPANELTOOLS::T2588115579"] = "Missing required settings: {0}" + +-- Name +UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::SETTINGS::SETTINGSPANELTOOLS::T266367750"] = "Name" + +-- Tool Settings +UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::SETTINGS::SETTINGSPANELTOOLS::T3730473128"] = "Tool Settings" + +-- State +UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::SETTINGS::SETTINGSPANELTOOLS::T502047894"] = "State" + -- No transcription provider configured yet. UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::SETTINGS::SETTINGSPANELTRANSCRIPTION::T1079350363"] = "No transcription provider configured yet." @@ -2659,6 +2713,63 @@ UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::THIRDPARTYCOMPONENT::T1392042694"] = "Ope -- License: UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::THIRDPARTYCOMPONENT::T1908172666"] = "License:" +-- Tool selection is hidden +UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::TOOLDEFAULTSCONFIGURATION::T2096103917"] = "Tool selection is hidden" + +-- You have selected 1 tool. +UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::TOOLDEFAULTSCONFIGURATION::T2493128368"] = "You have selected 1 tool." + +-- Choose which tools should be preselected for new runs of this assistant. +UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::TOOLDEFAULTSCONFIGURATION::T2696618758"] = "Choose which tools should be preselected for new runs of this assistant." + +-- Default tools for this assistant +UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::TOOLDEFAULTSCONFIGURATION::T3253667950"] = "Default tools for this assistant" + +-- Tool selection is visible +UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::TOOLDEFAULTSCONFIGURATION::T3384582069"] = "Tool selection is visible" + +-- Show tool selection in this assistant? +UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::TOOLDEFAULTSCONFIGURATION::T3494508870"] = "Show tool selection in this assistant?" + +-- You have selected {0} tools. +UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::TOOLDEFAULTSCONFIGURATION::T3729156356"] = "You have selected {0} tools." + +-- No tools selected. +UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::TOOLDEFAULTSCONFIGURATION::T3934845540"] = "No tools selected." + +-- Default tools for chat +UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::TOOLDEFAULTSCONFIGURATION::T907403808"] = "Default tools for chat" + +-- Choose which tools should be preselected for new chats. +UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::TOOLDEFAULTSCONFIGURATION::T948842182"] = "Choose which tools should be preselected for new chats." + +-- This tool is currently required because Web Search is enabled. +UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::TOOLSELECTION::T1351725609"] = "This tool is currently required because Web Search is enabled." + +-- Tool changes are locked while a response is running. Your current selection is shown below and applies again from the next message once the run is finished. +UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::TOOLSELECTION::T1688023907"] = "Tool changes are locked while a response is running. Your current selection is shown below and applies again from the next message once the run is finished." + +-- Enabling this tool also enables Read Web Page. +UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::TOOLSELECTION::T3023833839"] = "Enabling this tool also enables Read Web Page." + +-- Required settings are missing. Configure this tool before enabling it. +UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::TOOLSELECTION::T3119156561"] = "Required settings are missing. Configure this tool before enabling it." + +-- The selected provider or model does not support tool calling. +UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::TOOLSELECTION::T3364063757"] = "The selected provider or model does not support tool calling." + +-- Close +UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::TOOLSELECTION::T3448155331"] = "Close" + +-- No tools are available in this context. +UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::TOOLSELECTION::T3904490680"] = "No tools are available in this context." + +-- Tool Selection +UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::TOOLSELECTION::T749664565"] = "Tool Selection" + +-- Select tools +UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::TOOLSELECTION::T998515990"] = "Select tools" + -- You'll interact with the AI systems using your voice. To achieve this, we want to integrate voice input (speech-to-text) and output (text-to-speech). However, later on, it should also have a natural conversation flow, i.e., seamless conversation. UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::VISION::T1015366320"] = "You'll interact with the AI systems using your voice. To achieve this, we want to integrate voice input (speech-to-text) and output (text-to-speech). However, later on, it should also have a natural conversation flow, i.e., seamless conversation." @@ -4696,6 +4807,12 @@ UI_TEXT_CONTENT["AISTUDIO::DIALOGS::SETTINGS::SETTINGSDIALOGSLIDEBUILDER::T13933 -- Preselect aspects for the LLM to focus on when generating slides, such as bullet points or specific topics to emphasize. UI_TEXT_CONTENT["AISTUDIO::DIALOGS::SETTINGS::SETTINGSDIALOGSLIDEBUILDER::T1528169602"] = "Preselect aspects for the LLM to focus on when generating slides, such as bullet points or specific topics to emphasize." +-- Slide Planner Assistant options are preselected +UI_TEXT_CONTENT["AISTUDIO::DIALOGS::SETTINGS::SETTINGSDIALOGSLIDEBUILDER::T1549358578"] = "Slide Planner Assistant options are preselected" + +-- No Slide Planner Assistant options are preselected +UI_TEXT_CONTENT["AISTUDIO::DIALOGS::SETTINGS::SETTINGSDIALOGSLIDEBUILDER::T1694374279"] = "No Slide Planner Assistant options are preselected" + -- Choose whether the assistant should use the app default profile, no profile, or a specific profile. UI_TEXT_CONTENT["AISTUDIO::DIALOGS::SETTINGS::SETTINGSDIALOGSLIDEBUILDER::T1766361623"] = "Choose whether the assistant should use the app default profile, no profile, or a specific profile." @@ -4705,9 +4822,6 @@ UI_TEXT_CONTENT["AISTUDIO::DIALOGS::SETTINGS::SETTINGSDIALOGSLIDEBUILDER::T20146 -- Which audience organizational level should be preselected? UI_TEXT_CONTENT["AISTUDIO::DIALOGS::SETTINGS::SETTINGSDIALOGSLIDEBUILDER::T216511105"] = "Which audience organizational level should be preselected?" --- Preselect Slide Planner Assistant options? -UI_TEXT_CONTENT["AISTUDIO::DIALOGS::SETTINGS::SETTINGSDIALOGSLIDEBUILDER::T227645894"] = "Preselect Slide Planner Assistant options?" - -- Preselect a profile UI_TEXT_CONTENT["AISTUDIO::DIALOGS::SETTINGS::SETTINGSDIALOGSLIDEBUILDER::T2322771068"] = "Preselect a profile" @@ -4724,26 +4838,23 @@ UI_TEXT_CONTENT["AISTUDIO::DIALOGS::SETTINGS::SETTINGSDIALOGSLIDEBUILDER::T25714 UI_TEXT_CONTENT["AISTUDIO::DIALOGS::SETTINGS::SETTINGSDIALOGSLIDEBUILDER::T2645589441"] = "Preselect the audience age group" -- Assistant: Slide Planner Assistant Options -UI_TEXT_CONTENT["AISTUDIO::DIALOGS::SETTINGS::SETTINGSDIALOGSLIDEBUILDER::T3215549988"] = "Assistant: Slide Planner Assistant Options" +UI_TEXT_CONTENT["AISTUDIO::DIALOGS::SETTINGS::SETTINGSDIALOGSLIDEBUILDER::T3226042276"] = "Assistant: Slide Planner Assistant Options" -- Which audience expertise should be preselected? UI_TEXT_CONTENT["AISTUDIO::DIALOGS::SETTINGS::SETTINGSDIALOGSLIDEBUILDER::T3228597992"] = "Which audience expertise should be preselected?" +-- Preselect Slide Planner Assistant options? +UI_TEXT_CONTENT["AISTUDIO::DIALOGS::SETTINGS::SETTINGSDIALOGSLIDEBUILDER::T339924858"] = "Preselect Slide Planner Assistant options?" + -- Close UI_TEXT_CONTENT["AISTUDIO::DIALOGS::SETTINGS::SETTINGSDIALOGSLIDEBUILDER::T3448155331"] = "Close" -- Preselect important aspects UI_TEXT_CONTENT["AISTUDIO::DIALOGS::SETTINGS::SETTINGSDIALOGSLIDEBUILDER::T3705987833"] = "Preselect important aspects" --- No Slide Planner Assistant options are preselected -UI_TEXT_CONTENT["AISTUDIO::DIALOGS::SETTINGS::SETTINGSDIALOGSLIDEBUILDER::T4214398691"] = "No Slide Planner Assistant options are preselected" - -- Preselect the audience profile UI_TEXT_CONTENT["AISTUDIO::DIALOGS::SETTINGS::SETTINGSDIALOGSLIDEBUILDER::T861397972"] = "Preselect the audience profile" --- Slide Planner Assistant options are preselected -UI_TEXT_CONTENT["AISTUDIO::DIALOGS::SETTINGS::SETTINGSDIALOGSLIDEBUILDER::T93124146"] = "Slide Planner Assistant options are preselected" - -- Which audience age group should be preselected? UI_TEXT_CONTENT["AISTUDIO::DIALOGS::SETTINGS::SETTINGSDIALOGSLIDEBUILDER::T956845877"] = "Which audience age group should be preselected?" @@ -5002,6 +5113,18 @@ UI_TEXT_CONTENT["AISTUDIO::DIALOGS::SETTINGS::SETTINGSDIALOGWRITINGEMAILS::T3547 -- Preselect e-mail options? UI_TEXT_CONTENT["AISTUDIO::DIALOGS::SETTINGS::SETTINGSDIALOGWRITINGEMAILS::T3832719342"] = "Preselect e-mail options?" +-- Save +UI_TEXT_CONTENT["AISTUDIO::DIALOGS::SETTINGS::TOOLSETTINGSDIALOG::T1294818664"] = "Save" + +-- Tool Settings +UI_TEXT_CONTENT["AISTUDIO::DIALOGS::SETTINGS::TOOLSETTINGSDIALOG::T3730473128"] = "Tool Settings" + +-- The selected tool could not be loaded. +UI_TEXT_CONTENT["AISTUDIO::DIALOGS::SETTINGS::TOOLSETTINGSDIALOG::T3907843187"] = "The selected tool could not be loaded." + +-- Cancel +UI_TEXT_CONTENT["AISTUDIO::DIALOGS::SETTINGS::TOOLSETTINGSDIALOG::T900713019"] = "Cancel" + -- Save UI_TEXT_CONTENT["AISTUDIO::DIALOGS::SHORTCUTDIALOG::T1294818664"] = "Save" @@ -6724,6 +6847,105 @@ UI_TEXT_CONTENT["AISTUDIO::TOOLS::SOURCEEXTENSIONS::T4174900468"] = "Sources pro -- Sources provided by the AI UI_TEXT_CONTENT["AISTUDIO::TOOLS::SOURCEEXTENSIONS::T4261248356"] = "Sources provided by the AI" +-- Tool +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::ITOOLIMPLEMENTATION::T3517012711"] = "Tool" + +-- Tool description +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::ITOOLIMPLEMENTATION::T4056470505"] = "Tool description" + +-- Current Weather", typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)); public string GetDescription() => I18N.I.T("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.", typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)); public string GetSettingsFieldLabel(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { "demoLabel" => I18N.I.T("Demo Label", typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)), _ => I18N.I.T(fieldDefinition.Title, typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)), }; public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { "demoLabel" => I18N.I.T("Required demo setting for validating tool settings in tests. It does not affect the weather result.", typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)), }; public Task 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 +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::TOOLCALLINGIMPLEMENTATIONS::GETCURRENTWEATHERTOOL::T1597702905"] = "Current Weather\", typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)); public string GetDescription() => I18N.I.T(\"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.\", typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)); public string GetSettingsFieldLabel(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { \"demoLabel\" => I18N.I.T(\"Demo Label\", typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)), _ => I18N.I.T(fieldDefinition.Title, typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)), }; public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { \"demoLabel\" => I18N.I.T(\"Required demo setting for validating tool settings in tests. It does not affect the weather result.\", typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)), }; public Task 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" + +-- 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.", typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)); public string GetSettingsFieldLabel(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { "demoLabel" => I18N.I.T("Demo Label", typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)), _ => I18N.I.T(fieldDefinition.Title, typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)), }; public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { "demoLabel" => I18N.I.T("Required demo setting for validating tool settings in tests. It does not affect the weather result.", typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)), }; public Task 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 +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::TOOLCALLINGIMPLEMENTATIONS::GETCURRENTWEATHERTOOL::T2152408159"] = "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.\", typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)); public string GetSettingsFieldLabel(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { \"demoLabel\" => I18N.I.T(\"Demo Label\", typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)), _ => I18N.I.T(fieldDefinition.Title, typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)), }; public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { \"demoLabel\" => I18N.I.T(\"Required demo setting for validating tool settings in tests. It does not affect the weather result.\", typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)), }; public Task 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" + +-- Required demo setting for validating tool settings in tests. It does not affect the weather result.", typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)), }; public Task 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 +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::TOOLCALLINGIMPLEMENTATIONS::GETCURRENTWEATHERTOOL::T25380508"] = "Required demo setting for validating tool settings in tests. It does not affect the weather result.\", typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)), }; public Task 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" + +-- Demo Label", typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)), _ => I18N.I.T(fieldDefinition.Title, typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)), }; public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { "demoLabel" => I18N.I.T("Required demo setting for validating tool settings in tests. It does not affect the weather result.", typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)), }; public Task 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 +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::TOOLCALLINGIMPLEMENTATIONS::GETCURRENTWEATHERTOOL::T3346467484"] = "Demo Label\", typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)), _ => I18N.I.T(fieldDefinition.Title, typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)), }; public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { \"demoLabel\" => I18N.I.T(\"Required demo setting for validating tool settings in tests. It does not affect the weather result.\", typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)), }; public Task 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" + +-- Optional HTTP timeout for loading a web page in seconds.", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), "maxContentCharacters" => I18N.I.T("Optional global truncation limit for extracted Markdown returned to the model.", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { if (!TryReadOptionalPositiveInt(settingsValues, "timeoutSeconds", out _, out var timeoutError)) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = timeoutError, }); } if (!TryReadOptionalPositiveInt(settingsValues, "maxContentCharacters", out _, out var contentError)) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = contentError, }); } return Task.FromResult(null); } public async Task ExecuteAsync(JsonElement arguments, ToolExecutionContext context, CancellationToken token = default) { var urlText = ReadRequiredString(arguments, "url +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::TOOLCALLINGIMPLEMENTATIONS::READWEBPAGETOOL::T1169117578"] = "Optional HTTP timeout for loading a web page in seconds.\", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), \"maxContentCharacters\" => I18N.I.T(\"Optional global truncation limit for extracted Markdown returned to the model.\", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { if (!TryReadOptionalPositiveInt(settingsValues, \"timeoutSeconds\", out _, out var timeoutError)) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = timeoutError, }); } if (!TryReadOptionalPositiveInt(settingsValues, \"maxContentCharacters\", out _, out var contentError)) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = contentError, }); } return Task.FromResult(null); } public async Task ExecuteAsync(JsonElement arguments, ToolExecutionContext context, CancellationToken token = default) { var urlText = ReadRequiredString(arguments, \"url" + +-- Read Web Page", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)); public string GetDescription() => I18N.I.T("Load a single web page, extract its main HTML content, and return structured working material for the model. Use the result to synthesize a natural-language answer instead of exposing the raw payload to the user.", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)); public string GetSettingsFieldLabel(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { "timeoutSeconds" => I18N.I.T("Timeout Seconds", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), "maxContentCharacters" => I18N.I.T("Maximum Content Characters", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), _ => I18N.I.T(fieldDefinition.Title, typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), }; public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { "timeoutSeconds" => I18N.I.T("Optional HTTP timeout for loading a web page in seconds.", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), "maxContentCharacters" => I18N.I.T("Optional global truncation limit for extracted Markdown returned to the model.", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { if (!TryReadOptionalPositiveInt(settingsValues, "timeoutSeconds", out _, out var timeoutError)) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = timeoutError, }); } if (!TryReadOptionalPositiveInt(settingsValues, "maxContentCharacters", out _, out var contentError)) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = contentError, }); } return Task.FromResult(null); } public async Task ExecuteAsync(JsonElement arguments, ToolExecutionContext context, CancellationToken token = default) { var urlText = ReadRequiredString(arguments, "url +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::TOOLCALLINGIMPLEMENTATIONS::READWEBPAGETOOL::T1310829237"] = "Read Web Page\", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)); public string GetDescription() => I18N.I.T(\"Load a single web page, extract its main HTML content, and return structured working material for the model. Use the result to synthesize a natural-language answer instead of exposing the raw payload to the user.\", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)); public string GetSettingsFieldLabel(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { \"timeoutSeconds\" => I18N.I.T(\"Timeout Seconds\", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), \"maxContentCharacters\" => I18N.I.T(\"Maximum Content Characters\", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), _ => I18N.I.T(fieldDefinition.Title, typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), }; public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { \"timeoutSeconds\" => I18N.I.T(\"Optional HTTP timeout for loading a web page in seconds.\", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), \"maxContentCharacters\" => I18N.I.T(\"Optional global truncation limit for extracted Markdown returned to the model.\", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { if (!TryReadOptionalPositiveInt(settingsValues, \"timeoutSeconds\", out _, out var timeoutError)) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = timeoutError, }); } if (!TryReadOptionalPositiveInt(settingsValues, \"maxContentCharacters\", out _, out var contentError)) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = contentError, }); } return Task.FromResult(null); } public async Task ExecuteAsync(JsonElement arguments, ToolExecutionContext context, CancellationToken token = default) { var urlText = ReadRequiredString(arguments, \"url" + +-- Optional global truncation limit for extracted Markdown returned to the model.", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { if (!TryReadOptionalPositiveInt(settingsValues, "timeoutSeconds", out _, out var timeoutError)) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = timeoutError, }); } if (!TryReadOptionalPositiveInt(settingsValues, "maxContentCharacters", out _, out var contentError)) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = contentError, }); } return Task.FromResult(null); } public async Task ExecuteAsync(JsonElement arguments, ToolExecutionContext context, CancellationToken token = default) { var urlText = ReadRequiredString(arguments, "url +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::TOOLCALLINGIMPLEMENTATIONS::READWEBPAGETOOL::T2765372972"] = "Optional global truncation limit for extracted Markdown returned to the model.\", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { if (!TryReadOptionalPositiveInt(settingsValues, \"timeoutSeconds\", out _, out var timeoutError)) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = timeoutError, }); } if (!TryReadOptionalPositiveInt(settingsValues, \"maxContentCharacters\", out _, out var contentError)) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = contentError, }); } return Task.FromResult(null); } public async Task ExecuteAsync(JsonElement arguments, ToolExecutionContext context, CancellationToken token = default) { var urlText = ReadRequiredString(arguments, \"url" + +-- Maximum Content Characters", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), _ => I18N.I.T(fieldDefinition.Title, typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), }; public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { "timeoutSeconds" => I18N.I.T("Optional HTTP timeout for loading a web page in seconds.", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), "maxContentCharacters" => I18N.I.T("Optional global truncation limit for extracted Markdown returned to the model.", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { if (!TryReadOptionalPositiveInt(settingsValues, "timeoutSeconds", out _, out var timeoutError)) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = timeoutError, }); } if (!TryReadOptionalPositiveInt(settingsValues, "maxContentCharacters", out _, out var contentError)) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = contentError, }); } return Task.FromResult(null); } public async Task ExecuteAsync(JsonElement arguments, ToolExecutionContext context, CancellationToken token = default) { var urlText = ReadRequiredString(arguments, "url +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::TOOLCALLINGIMPLEMENTATIONS::READWEBPAGETOOL::T2860394705"] = "Maximum Content Characters\", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), _ => I18N.I.T(fieldDefinition.Title, typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), }; public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { \"timeoutSeconds\" => I18N.I.T(\"Optional HTTP timeout for loading a web page in seconds.\", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), \"maxContentCharacters\" => I18N.I.T(\"Optional global truncation limit for extracted Markdown returned to the model.\", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { if (!TryReadOptionalPositiveInt(settingsValues, \"timeoutSeconds\", out _, out var timeoutError)) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = timeoutError, }); } if (!TryReadOptionalPositiveInt(settingsValues, \"maxContentCharacters\", out _, out var contentError)) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = contentError, }); } return Task.FromResult(null); } public async Task ExecuteAsync(JsonElement arguments, ToolExecutionContext context, CancellationToken token = default) { var urlText = ReadRequiredString(arguments, \"url" + +-- Timeout Seconds", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), "maxContentCharacters" => I18N.I.T("Maximum Content Characters", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), _ => I18N.I.T(fieldDefinition.Title, typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), }; public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { "timeoutSeconds" => I18N.I.T("Optional HTTP timeout for loading a web page in seconds.", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), "maxContentCharacters" => I18N.I.T("Optional global truncation limit for extracted Markdown returned to the model.", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { if (!TryReadOptionalPositiveInt(settingsValues, "timeoutSeconds", out _, out var timeoutError)) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = timeoutError, }); } if (!TryReadOptionalPositiveInt(settingsValues, "maxContentCharacters", out _, out var contentError)) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = contentError, }); } return Task.FromResult(null); } public async Task ExecuteAsync(JsonElement arguments, ToolExecutionContext context, CancellationToken token = default) { var urlText = ReadRequiredString(arguments, "url +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::TOOLCALLINGIMPLEMENTATIONS::READWEBPAGETOOL::T3510104271"] = "Timeout Seconds\", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), \"maxContentCharacters\" => I18N.I.T(\"Maximum Content Characters\", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), _ => I18N.I.T(fieldDefinition.Title, typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), }; public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { \"timeoutSeconds\" => I18N.I.T(\"Optional HTTP timeout for loading a web page in seconds.\", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), \"maxContentCharacters\" => I18N.I.T(\"Optional global truncation limit for extracted Markdown returned to the model.\", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { if (!TryReadOptionalPositiveInt(settingsValues, \"timeoutSeconds\", out _, out var timeoutError)) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = timeoutError, }); } if (!TryReadOptionalPositiveInt(settingsValues, \"maxContentCharacters\", out _, out var contentError)) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = contentError, }); } return Task.FromResult(null); } public async Task ExecuteAsync(JsonElement arguments, ToolExecutionContext context, CancellationToken token = default) { var urlText = ReadRequiredString(arguments, \"url" + +-- Load a single web page, extract its main HTML content, and return structured working material for the model. Use the result to synthesize a natural-language answer instead of exposing the raw payload to the user.", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)); public string GetSettingsFieldLabel(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { "timeoutSeconds" => I18N.I.T("Timeout Seconds", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), "maxContentCharacters" => I18N.I.T("Maximum Content Characters", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), _ => I18N.I.T(fieldDefinition.Title, typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), }; public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { "timeoutSeconds" => I18N.I.T("Optional HTTP timeout for loading a web page in seconds.", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), "maxContentCharacters" => I18N.I.T("Optional global truncation limit for extracted Markdown returned to the model.", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { if (!TryReadOptionalPositiveInt(settingsValues, "timeoutSeconds", out _, out var timeoutError)) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = timeoutError, }); } if (!TryReadOptionalPositiveInt(settingsValues, "maxContentCharacters", out _, out var contentError)) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = contentError, }); } return Task.FromResult(null); } public async Task ExecuteAsync(JsonElement arguments, ToolExecutionContext context, CancellationToken token = default) { var urlText = ReadRequiredString(arguments, "url +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::TOOLCALLINGIMPLEMENTATIONS::READWEBPAGETOOL::T3614129091"] = "Load a single web page, extract its main HTML content, and return structured working material for the model. Use the result to synthesize a natural-language answer instead of exposing the raw payload to the user.\", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)); public string GetSettingsFieldLabel(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { \"timeoutSeconds\" => I18N.I.T(\"Timeout Seconds\", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), \"maxContentCharacters\" => I18N.I.T(\"Maximum Content Characters\", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), _ => I18N.I.T(fieldDefinition.Title, typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), }; public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { \"timeoutSeconds\" => I18N.I.T(\"Optional HTTP timeout for loading a web page in seconds.\", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), \"maxContentCharacters\" => I18N.I.T(\"Optional global truncation limit for extracted Markdown returned to the model.\", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { if (!TryReadOptionalPositiveInt(settingsValues, \"timeoutSeconds\", out _, out var timeoutError)) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = timeoutError, }); } if (!TryReadOptionalPositiveInt(settingsValues, \"maxContentCharacters\", out _, out var contentError)) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = contentError, }); } return Task.FromResult(null); } public async Task ExecuteAsync(JsonElement arguments, ToolExecutionContext context, CancellationToken token = default) { var urlText = ReadRequiredString(arguments, \"url" + +-- Default Categories", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultEngines" => I18N.I.T("Default Engines", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "maxResults" => I18N.I.T("Maximum Results", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "timeoutSeconds" => I18N.I.T("Timeout Seconds", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Title, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { "baseUrl" => I18N.I.T("Base URL of the SearXNG instance. You can enter either the instance root URL or the /search endpoint.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultLanguage" => I18N.I.T("Optional fallback language code when the model does not provide a language.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultSafeSearch" => I18N.I.T("Optional safe search policy sent to SearXNG when configured.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultCategories" => I18N.I.T("Optional comma-separated default categories. Do not set this together with default engines.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultEngines" => I18N.I.T("Optional comma-separated default engines. Do not set this together with default categories.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "maxResults" => I18N.I.T("Optional default maximum number of results returned to the model when the model does not provide a limit.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "timeoutSeconds" => I18N.I.T("Optional HTTP timeout for the search request in seconds.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { settingsValues.TryGetValue("baseUrl", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); if (!isValidBaseUrl) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = uriError, }); } var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault("defaultCategories +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::TOOLCALLINGIMPLEMENTATIONS::SEARXNGWEBSEARCHTOOL::T1254458306"] = "Default Categories\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultEngines\" => I18N.I.T(\"Default Engines\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"maxResults\" => I18N.I.T(\"Maximum Results\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"timeoutSeconds\" => I18N.I.T(\"Timeout Seconds\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Title, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { \"baseUrl\" => I18N.I.T(\"Base URL of the SearXNG instance. You can enter either the instance root URL or the /search endpoint.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultLanguage\" => I18N.I.T(\"Optional fallback language code when the model does not provide a language.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultSafeSearch\" => I18N.I.T(\"Optional safe search policy sent to SearXNG when configured.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultCategories\" => I18N.I.T(\"Optional comma-separated default categories. Do not set this together with default engines.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultEngines\" => I18N.I.T(\"Optional comma-separated default engines. Do not set this together with default categories.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"maxResults\" => I18N.I.T(\"Optional default maximum number of results returned to the model when the model does not provide a limit.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"timeoutSeconds\" => I18N.I.T(\"Optional HTTP timeout for the search request in seconds.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { settingsValues.TryGetValue(\"baseUrl\", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); if (!isValidBaseUrl) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = uriError, }); } var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault(\"defaultCategories" + +-- Optional safe search policy sent to SearXNG when configured.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultCategories" => I18N.I.T("Optional comma-separated default categories. Do not set this together with default engines.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultEngines" => I18N.I.T("Optional comma-separated default engines. Do not set this together with default categories.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "maxResults" => I18N.I.T("Optional default maximum number of results returned to the model when the model does not provide a limit.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "timeoutSeconds" => I18N.I.T("Optional HTTP timeout for the search request in seconds.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { settingsValues.TryGetValue("baseUrl", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); if (!isValidBaseUrl) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = uriError, }); } var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault("defaultCategories +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::TOOLCALLINGIMPLEMENTATIONS::SEARXNGWEBSEARCHTOOL::T1327402904"] = "Optional safe search policy sent to SearXNG when configured.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultCategories\" => I18N.I.T(\"Optional comma-separated default categories. Do not set this together with default engines.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultEngines\" => I18N.I.T(\"Optional comma-separated default engines. Do not set this together with default categories.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"maxResults\" => I18N.I.T(\"Optional default maximum number of results returned to the model when the model does not provide a limit.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"timeoutSeconds\" => I18N.I.T(\"Optional HTTP timeout for the search request in seconds.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { settingsValues.TryGetValue(\"baseUrl\", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); if (!isValidBaseUrl) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = uriError, }); } var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault(\"defaultCategories" + +-- Optional comma-separated default engines. Do not set this together with default categories.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "maxResults" => I18N.I.T("Optional default maximum number of results returned to the model when the model does not provide a limit.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "timeoutSeconds" => I18N.I.T("Optional HTTP timeout for the search request in seconds.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { settingsValues.TryGetValue("baseUrl", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); if (!isValidBaseUrl) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = uriError, }); } var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault("defaultCategories +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::TOOLCALLINGIMPLEMENTATIONS::SEARXNGWEBSEARCHTOOL::T1401266403"] = "Optional comma-separated default engines. Do not set this together with default categories.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"maxResults\" => I18N.I.T(\"Optional default maximum number of results returned to the model when the model does not provide a limit.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"timeoutSeconds\" => I18N.I.T(\"Optional HTTP timeout for the search request in seconds.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { settingsValues.TryGetValue(\"baseUrl\", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); if (!isValidBaseUrl) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = uriError, }); } var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault(\"defaultCategories" + +-- Optional comma-separated default categories. Do not set this together with default engines.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultEngines" => I18N.I.T("Optional comma-separated default engines. Do not set this together with default categories.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "maxResults" => I18N.I.T("Optional default maximum number of results returned to the model when the model does not provide a limit.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "timeoutSeconds" => I18N.I.T("Optional HTTP timeout for the search request in seconds.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { settingsValues.TryGetValue("baseUrl", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); if (!isValidBaseUrl) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = uriError, }); } var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault("defaultCategories +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::TOOLCALLINGIMPLEMENTATIONS::SEARXNGWEBSEARCHTOOL::T1539252250"] = "Optional comma-separated default categories. Do not set this together with default engines.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultEngines\" => I18N.I.T(\"Optional comma-separated default engines. Do not set this together with default categories.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"maxResults\" => I18N.I.T(\"Optional default maximum number of results returned to the model when the model does not provide a limit.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"timeoutSeconds\" => I18N.I.T(\"Optional HTTP timeout for the search request in seconds.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { settingsValues.TryGetValue(\"baseUrl\", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); if (!isValidBaseUrl) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = uriError, }); } var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault(\"defaultCategories" + +-- Timeout Seconds", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Title, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { "baseUrl" => I18N.I.T("Base URL of the SearXNG instance. You can enter either the instance root URL or the /search endpoint.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultLanguage" => I18N.I.T("Optional fallback language code when the model does not provide a language.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultSafeSearch" => I18N.I.T("Optional safe search policy sent to SearXNG when configured.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultCategories" => I18N.I.T("Optional comma-separated default categories. Do not set this together with default engines.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultEngines" => I18N.I.T("Optional comma-separated default engines. Do not set this together with default categories.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "maxResults" => I18N.I.T("Optional default maximum number of results returned to the model when the model does not provide a limit.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "timeoutSeconds" => I18N.I.T("Optional HTTP timeout for the search request in seconds.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { settingsValues.TryGetValue("baseUrl", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); if (!isValidBaseUrl) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = uriError, }); } var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault("defaultCategories +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::TOOLCALLINGIMPLEMENTATIONS::SEARXNGWEBSEARCHTOOL::T186659624"] = "Timeout Seconds\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Title, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { \"baseUrl\" => I18N.I.T(\"Base URL of the SearXNG instance. You can enter either the instance root URL or the /search endpoint.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultLanguage\" => I18N.I.T(\"Optional fallback language code when the model does not provide a language.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultSafeSearch\" => I18N.I.T(\"Optional safe search policy sent to SearXNG when configured.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultCategories\" => I18N.I.T(\"Optional comma-separated default categories. Do not set this together with default engines.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultEngines\" => I18N.I.T(\"Optional comma-separated default engines. Do not set this together with default categories.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"maxResults\" => I18N.I.T(\"Optional default maximum number of results returned to the model when the model does not provide a limit.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"timeoutSeconds\" => I18N.I.T(\"Optional HTTP timeout for the search request in seconds.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { settingsValues.TryGetValue(\"baseUrl\", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); if (!isValidBaseUrl) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = uriError, }); } var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault(\"defaultCategories" + +-- Default categories and default engines cannot both be set for the web search tool.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool))); var defaultLimit = ReadOptionalPositiveIntSetting(context.SettingsValues, "maxResults +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::TOOLCALLINGIMPLEMENTATIONS::SEARXNGWEBSEARCHTOOL::T2087438861"] = "Default categories and default engines cannot both be set for the web search tool.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool))); var defaultLimit = ReadOptionalPositiveIntSetting(context.SettingsValues, \"maxResults" + +-- The configured SearXNG URL must start with http:// or https://.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)); return false; } var basePath = parsedUri.AbsolutePath.TrimEnd('/'); if (basePath.EndsWith("/search", StringComparison.OrdinalIgnoreCase)) basePath = basePath[..^"/search".Length]; var normalizedPath = $"{basePath}/search"; var builder = new UriBuilder(parsedUri) { Path = normalizedPath, Query = string.Empty, Fragment = string.Empty, }; searchUri = builder.Uri; return true; } private static Uri BuildRequestUri(Uri searchUri, IEnumerable> queryParameters) { var builder = new StringBuilder(); foreach (var parameter in queryParameters) { if (builder.Length > 0) builder.Append('&'); builder.Append(WebUtility.UrlEncode(parameter.Key)); builder.Append('='); builder.Append(WebUtility.UrlEncode(parameter.Value)); } var uriBuilder = new UriBuilder(searchUri) { Query = builder.ToString(), }; return uriBuilder.Uri; } private static async Task SendAsync( HttpClient httpClient, HttpRequestMessage request, CancellationToken requestToken, int timeoutSeconds, CancellationToken callerToken) { try { return await httpClient.SendAsync(request, requestToken); } catch (OperationCanceledException) when (!callerToken.IsCancellationRequested) { throw new TimeoutException($"The SearXNG request timed out after {timeoutSeconds} seconds. +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::TOOLCALLINGIMPLEMENTATIONS::SEARXNGWEBSEARCHTOOL::T212620884"] = "The configured SearXNG URL must start with http:// or https://.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)); return false; } var basePath = parsedUri.AbsolutePath.TrimEnd('/'); if (basePath.EndsWith(\"/search\", StringComparison.OrdinalIgnoreCase)) basePath = basePath[..^\"/search\".Length]; var normalizedPath = $\"{basePath}/search\"; var builder = new UriBuilder(parsedUri) { Path = normalizedPath, Query = string.Empty, Fragment = string.Empty, }; searchUri = builder.Uri; return true; } private static Uri BuildRequestUri(Uri searchUri, IEnumerable> queryParameters) { var builder = new StringBuilder(); foreach (var parameter in queryParameters) { if (builder.Length > 0) builder.Append('&'); builder.Append(WebUtility.UrlEncode(parameter.Key)); builder.Append('='); builder.Append(WebUtility.UrlEncode(parameter.Value)); } var uriBuilder = new UriBuilder(searchUri) { Query = builder.ToString(), }; return uriBuilder.Uri; } private static async Task SendAsync( HttpClient httpClient, HttpRequestMessage request, CancellationToken requestToken, int timeoutSeconds, CancellationToken callerToken) { try { return await httpClient.SendAsync(request, requestToken); } catch (OperationCanceledException) when (!callerToken.IsCancellationRequested) { throw new TimeoutException($\"The SearXNG request timed out after {timeoutSeconds} seconds." + +-- Optional default maximum number of results returned to the model when the model does not provide a limit.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "timeoutSeconds" => I18N.I.T("Optional HTTP timeout for the search request in seconds.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { settingsValues.TryGetValue("baseUrl", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); if (!isValidBaseUrl) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = uriError, }); } var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault("defaultCategories +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::TOOLCALLINGIMPLEMENTATIONS::SEARXNGWEBSEARCHTOOL::T2170342710"] = "Optional default maximum number of results returned to the model when the model does not provide a limit.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"timeoutSeconds\" => I18N.I.T(\"Optional HTTP timeout for the search request in seconds.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { settingsValues.TryGetValue(\"baseUrl\", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); if (!isValidBaseUrl) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = uriError, }); } var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault(\"defaultCategories" + +-- A SearXNG URL is required.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)); return false; } if (!Uri.TryCreate(rawUrl.Trim(), UriKind.Absolute, out var parsedUri)) { error = I18N.I.T("The configured SearXNG URL is not a valid absolute URL.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)); return false; } if (parsedUri.Scheme is not ("http" or "https +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::TOOLCALLINGIMPLEMENTATIONS::SEARXNGWEBSEARCHTOOL::T2420801571"] = "A SearXNG URL is required.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)); return false; } if (!Uri.TryCreate(rawUrl.Trim(), UriKind.Absolute, out var parsedUri)) { error = I18N.I.T(\"The configured SearXNG URL is not a valid absolute URL.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)); return false; } if (parsedUri.Scheme is not (\"http\" or \"https" + +-- Default Engines", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "maxResults" => I18N.I.T("Maximum Results", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "timeoutSeconds" => I18N.I.T("Timeout Seconds", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Title, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { "baseUrl" => I18N.I.T("Base URL of the SearXNG instance. You can enter either the instance root URL or the /search endpoint.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultLanguage" => I18N.I.T("Optional fallback language code when the model does not provide a language.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultSafeSearch" => I18N.I.T("Optional safe search policy sent to SearXNG when configured.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultCategories" => I18N.I.T("Optional comma-separated default categories. Do not set this together with default engines.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultEngines" => I18N.I.T("Optional comma-separated default engines. Do not set this together with default categories.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "maxResults" => I18N.I.T("Optional default maximum number of results returned to the model when the model does not provide a limit.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "timeoutSeconds" => I18N.I.T("Optional HTTP timeout for the search request in seconds.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { settingsValues.TryGetValue("baseUrl", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); if (!isValidBaseUrl) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = uriError, }); } var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault("defaultCategories +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::TOOLCALLINGIMPLEMENTATIONS::SEARXNGWEBSEARCHTOOL::T2435794648"] = "Default Engines\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"maxResults\" => I18N.I.T(\"Maximum Results\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"timeoutSeconds\" => I18N.I.T(\"Timeout Seconds\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Title, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { \"baseUrl\" => I18N.I.T(\"Base URL of the SearXNG instance. You can enter either the instance root URL or the /search endpoint.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultLanguage\" => I18N.I.T(\"Optional fallback language code when the model does not provide a language.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultSafeSearch\" => I18N.I.T(\"Optional safe search policy sent to SearXNG when configured.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultCategories\" => I18N.I.T(\"Optional comma-separated default categories. Do not set this together with default engines.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultEngines\" => I18N.I.T(\"Optional comma-separated default engines. Do not set this together with default categories.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"maxResults\" => I18N.I.T(\"Optional default maximum number of results returned to the model when the model does not provide a limit.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"timeoutSeconds\" => I18N.I.T(\"Optional HTTP timeout for the search request in seconds.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { settingsValues.TryGetValue(\"baseUrl\", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); if (!isValidBaseUrl) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = uriError, }); } var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault(\"defaultCategories" + +-- Default categories and default engines cannot both be set for the web search tool.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }); } if (!TryReadOptionalPositiveInt(settingsValues, "maxResults", out _, out var maxResultsError)) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = maxResultsError, }); } if (!TryReadOptionalPositiveInt(settingsValues, "timeoutSeconds", out _, out var timeoutError)) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = timeoutError, }); } return Task.FromResult(null); } public async Task ExecuteAsync(JsonElement arguments, ToolExecutionContext context, CancellationToken token = default) { context.SettingsValues.TryGetValue("baseUrl", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out var searchUri, out var uriError); if (!isValidBaseUrl) throw new InvalidOperationException(uriError); var query = ReadRequiredString(arguments, "query +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::TOOLCALLINGIMPLEMENTATIONS::SEARXNGWEBSEARCHTOOL::T2471183191"] = "Default categories and default engines cannot both be set for the web search tool.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }); } if (!TryReadOptionalPositiveInt(settingsValues, \"maxResults\", out _, out var maxResultsError)) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = maxResultsError, }); } if (!TryReadOptionalPositiveInt(settingsValues, \"timeoutSeconds\", out _, out var timeoutError)) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = timeoutError, }); } return Task.FromResult(null); } public async Task ExecuteAsync(JsonElement arguments, ToolExecutionContext context, CancellationToken token = default) { context.SettingsValues.TryGetValue(\"baseUrl\", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out var searchUri, out var uriError); if (!isValidBaseUrl) throw new InvalidOperationException(uriError); var query = ReadRequiredString(arguments, \"query" + +-- SearXNG URL", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultLanguage" => I18N.I.T("Default Language", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultSafeSearch" => I18N.I.T("Default Safe Search", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultCategories" => I18N.I.T("Default Categories", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultEngines" => I18N.I.T("Default Engines", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "maxResults" => I18N.I.T("Maximum Results", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "timeoutSeconds" => I18N.I.T("Timeout Seconds", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Title, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { "baseUrl" => I18N.I.T("Base URL of the SearXNG instance. You can enter either the instance root URL or the /search endpoint.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultLanguage" => I18N.I.T("Optional fallback language code when the model does not provide a language.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultSafeSearch" => I18N.I.T("Optional safe search policy sent to SearXNG when configured.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultCategories" => I18N.I.T("Optional comma-separated default categories. Do not set this together with default engines.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultEngines" => I18N.I.T("Optional comma-separated default engines. Do not set this together with default categories.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "maxResults" => I18N.I.T("Optional default maximum number of results returned to the model when the model does not provide a limit.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "timeoutSeconds" => I18N.I.T("Optional HTTP timeout for the search request in seconds.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { settingsValues.TryGetValue("baseUrl", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); if (!isValidBaseUrl) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = uriError, }); } var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault("defaultCategories +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::TOOLCALLINGIMPLEMENTATIONS::SEARXNGWEBSEARCHTOOL::T2707478507"] = "SearXNG URL\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultLanguage\" => I18N.I.T(\"Default Language\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultSafeSearch\" => I18N.I.T(\"Default Safe Search\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultCategories\" => I18N.I.T(\"Default Categories\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultEngines\" => I18N.I.T(\"Default Engines\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"maxResults\" => I18N.I.T(\"Maximum Results\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"timeoutSeconds\" => I18N.I.T(\"Timeout Seconds\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Title, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { \"baseUrl\" => I18N.I.T(\"Base URL of the SearXNG instance. You can enter either the instance root URL or the /search endpoint.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultLanguage\" => I18N.I.T(\"Optional fallback language code when the model does not provide a language.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultSafeSearch\" => I18N.I.T(\"Optional safe search policy sent to SearXNG when configured.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultCategories\" => I18N.I.T(\"Optional comma-separated default categories. Do not set this together with default engines.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultEngines\" => I18N.I.T(\"Optional comma-separated default engines. Do not set this together with default categories.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"maxResults\" => I18N.I.T(\"Optional default maximum number of results returned to the model when the model does not provide a limit.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"timeoutSeconds\" => I18N.I.T(\"Optional HTTP timeout for the search request in seconds.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { settingsValues.TryGetValue(\"baseUrl\", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); if (!isValidBaseUrl) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = uriError, }); } var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault(\"defaultCategories" + +-- Optional fallback language code when the model does not provide a language.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultSafeSearch" => I18N.I.T("Optional safe search policy sent to SearXNG when configured.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultCategories" => I18N.I.T("Optional comma-separated default categories. Do not set this together with default engines.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultEngines" => I18N.I.T("Optional comma-separated default engines. Do not set this together with default categories.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "maxResults" => I18N.I.T("Optional default maximum number of results returned to the model when the model does not provide a limit.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "timeoutSeconds" => I18N.I.T("Optional HTTP timeout for the search request in seconds.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { settingsValues.TryGetValue("baseUrl", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); if (!isValidBaseUrl) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = uriError, }); } var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault("defaultCategories +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::TOOLCALLINGIMPLEMENTATIONS::SEARXNGWEBSEARCHTOOL::T2911071656"] = "Optional fallback language code when the model does not provide a language.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultSafeSearch\" => I18N.I.T(\"Optional safe search policy sent to SearXNG when configured.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultCategories\" => I18N.I.T(\"Optional comma-separated default categories. Do not set this together with default engines.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultEngines\" => I18N.I.T(\"Optional comma-separated default engines. Do not set this together with default categories.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"maxResults\" => I18N.I.T(\"Optional default maximum number of results returned to the model when the model does not provide a limit.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"timeoutSeconds\" => I18N.I.T(\"Optional HTTP timeout for the search request in seconds.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { settingsValues.TryGetValue(\"baseUrl\", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); if (!isValidBaseUrl) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = uriError, }); } var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault(\"defaultCategories" + +-- Optional HTTP timeout for the search request in seconds.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { settingsValues.TryGetValue("baseUrl", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); if (!isValidBaseUrl) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = uriError, }); } var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault("defaultCategories +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::TOOLCALLINGIMPLEMENTATIONS::SEARXNGWEBSEARCHTOOL::T2953585467"] = "Optional HTTP timeout for the search request in seconds.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { settingsValues.TryGetValue(\"baseUrl\", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); if (!isValidBaseUrl) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = uriError, }); } var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault(\"defaultCategories" + +-- Web Search", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)); public string GetDescription() => I18N.I.T("Search the web with a configured SearXNG instance and return candidate URLs for the model. Use Read Web Page on relevant result URLs before answering factual or detailed web questions.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)); public string GetSettingsFieldLabel(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { "baseUrl" => I18N.I.T("SearXNG URL", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultLanguage" => I18N.I.T("Default Language", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultSafeSearch" => I18N.I.T("Default Safe Search", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultCategories" => I18N.I.T("Default Categories", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultEngines" => I18N.I.T("Default Engines", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "maxResults" => I18N.I.T("Maximum Results", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "timeoutSeconds" => I18N.I.T("Timeout Seconds", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Title, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { "baseUrl" => I18N.I.T("Base URL of the SearXNG instance. You can enter either the instance root URL or the /search endpoint.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultLanguage" => I18N.I.T("Optional fallback language code when the model does not provide a language.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultSafeSearch" => I18N.I.T("Optional safe search policy sent to SearXNG when configured.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultCategories" => I18N.I.T("Optional comma-separated default categories. Do not set this together with default engines.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultEngines" => I18N.I.T("Optional comma-separated default engines. Do not set this together with default categories.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "maxResults" => I18N.I.T("Optional default maximum number of results returned to the model when the model does not provide a limit.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "timeoutSeconds" => I18N.I.T("Optional HTTP timeout for the search request in seconds.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { settingsValues.TryGetValue("baseUrl", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); if (!isValidBaseUrl) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = uriError, }); } var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault("defaultCategories +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::TOOLCALLINGIMPLEMENTATIONS::SEARXNGWEBSEARCHTOOL::T3158851812"] = "Web Search\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)); public string GetDescription() => I18N.I.T(\"Search the web with a configured SearXNG instance and return candidate URLs for the model. Use Read Web Page on relevant result URLs before answering factual or detailed web questions.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)); public string GetSettingsFieldLabel(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { \"baseUrl\" => I18N.I.T(\"SearXNG URL\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultLanguage\" => I18N.I.T(\"Default Language\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultSafeSearch\" => I18N.I.T(\"Default Safe Search\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultCategories\" => I18N.I.T(\"Default Categories\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultEngines\" => I18N.I.T(\"Default Engines\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"maxResults\" => I18N.I.T(\"Maximum Results\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"timeoutSeconds\" => I18N.I.T(\"Timeout Seconds\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Title, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { \"baseUrl\" => I18N.I.T(\"Base URL of the SearXNG instance. You can enter either the instance root URL or the /search endpoint.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultLanguage\" => I18N.I.T(\"Optional fallback language code when the model does not provide a language.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultSafeSearch\" => I18N.I.T(\"Optional safe search policy sent to SearXNG when configured.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultCategories\" => I18N.I.T(\"Optional comma-separated default categories. Do not set this together with default engines.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultEngines\" => I18N.I.T(\"Optional comma-separated default engines. Do not set this together with default categories.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"maxResults\" => I18N.I.T(\"Optional default maximum number of results returned to the model when the model does not provide a limit.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"timeoutSeconds\" => I18N.I.T(\"Optional HTTP timeout for the search request in seconds.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { settingsValues.TryGetValue(\"baseUrl\", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); if (!isValidBaseUrl) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = uriError, }); } var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault(\"defaultCategories" + +-- Maximum Results", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "timeoutSeconds" => I18N.I.T("Timeout Seconds", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Title, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { "baseUrl" => I18N.I.T("Base URL of the SearXNG instance. You can enter either the instance root URL or the /search endpoint.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultLanguage" => I18N.I.T("Optional fallback language code when the model does not provide a language.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultSafeSearch" => I18N.I.T("Optional safe search policy sent to SearXNG when configured.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultCategories" => I18N.I.T("Optional comma-separated default categories. Do not set this together with default engines.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultEngines" => I18N.I.T("Optional comma-separated default engines. Do not set this together with default categories.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "maxResults" => I18N.I.T("Optional default maximum number of results returned to the model when the model does not provide a limit.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "timeoutSeconds" => I18N.I.T("Optional HTTP timeout for the search request in seconds.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { settingsValues.TryGetValue("baseUrl", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); if (!isValidBaseUrl) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = uriError, }); } var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault("defaultCategories +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::TOOLCALLINGIMPLEMENTATIONS::SEARXNGWEBSEARCHTOOL::T3332435511"] = "Maximum Results\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"timeoutSeconds\" => I18N.I.T(\"Timeout Seconds\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Title, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { \"baseUrl\" => I18N.I.T(\"Base URL of the SearXNG instance. You can enter either the instance root URL or the /search endpoint.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultLanguage\" => I18N.I.T(\"Optional fallback language code when the model does not provide a language.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultSafeSearch\" => I18N.I.T(\"Optional safe search policy sent to SearXNG when configured.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultCategories\" => I18N.I.T(\"Optional comma-separated default categories. Do not set this together with default engines.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultEngines\" => I18N.I.T(\"Optional comma-separated default engines. Do not set this together with default categories.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"maxResults\" => I18N.I.T(\"Optional default maximum number of results returned to the model when the model does not provide a limit.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"timeoutSeconds\" => I18N.I.T(\"Optional HTTP timeout for the search request in seconds.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { settingsValues.TryGetValue(\"baseUrl\", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); if (!isValidBaseUrl) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = uriError, }); } var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault(\"defaultCategories" + +-- The configured SearXNG URL is not a valid absolute URL.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)); return false; } if (parsedUri.Scheme is not ("http" or "https +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::TOOLCALLINGIMPLEMENTATIONS::SEARXNGWEBSEARCHTOOL::T371406570"] = "The configured SearXNG URL is not a valid absolute URL.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)); return false; } if (parsedUri.Scheme is not (\"http\" or \"https" + +-- Default Safe Search", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultCategories" => I18N.I.T("Default Categories", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultEngines" => I18N.I.T("Default Engines", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "maxResults" => I18N.I.T("Maximum Results", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "timeoutSeconds" => I18N.I.T("Timeout Seconds", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Title, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { "baseUrl" => I18N.I.T("Base URL of the SearXNG instance. You can enter either the instance root URL or the /search endpoint.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultLanguage" => I18N.I.T("Optional fallback language code when the model does not provide a language.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultSafeSearch" => I18N.I.T("Optional safe search policy sent to SearXNG when configured.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultCategories" => I18N.I.T("Optional comma-separated default categories. Do not set this together with default engines.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultEngines" => I18N.I.T("Optional comma-separated default engines. Do not set this together with default categories.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "maxResults" => I18N.I.T("Optional default maximum number of results returned to the model when the model does not provide a limit.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "timeoutSeconds" => I18N.I.T("Optional HTTP timeout for the search request in seconds.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { settingsValues.TryGetValue("baseUrl", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); if (!isValidBaseUrl) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = uriError, }); } var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault("defaultCategories +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::TOOLCALLINGIMPLEMENTATIONS::SEARXNGWEBSEARCHTOOL::T3780386928"] = "Default Safe Search\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultCategories\" => I18N.I.T(\"Default Categories\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultEngines\" => I18N.I.T(\"Default Engines\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"maxResults\" => I18N.I.T(\"Maximum Results\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"timeoutSeconds\" => I18N.I.T(\"Timeout Seconds\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Title, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { \"baseUrl\" => I18N.I.T(\"Base URL of the SearXNG instance. You can enter either the instance root URL or the /search endpoint.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultLanguage\" => I18N.I.T(\"Optional fallback language code when the model does not provide a language.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultSafeSearch\" => I18N.I.T(\"Optional safe search policy sent to SearXNG when configured.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultCategories\" => I18N.I.T(\"Optional comma-separated default categories. Do not set this together with default engines.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultEngines\" => I18N.I.T(\"Optional comma-separated default engines. Do not set this together with default categories.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"maxResults\" => I18N.I.T(\"Optional default maximum number of results returned to the model when the model does not provide a limit.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"timeoutSeconds\" => I18N.I.T(\"Optional HTTP timeout for the search request in seconds.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { settingsValues.TryGetValue(\"baseUrl\", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); if (!isValidBaseUrl) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = uriError, }); } var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault(\"defaultCategories" + +-- Search the web with a configured SearXNG instance and return candidate URLs for the model. Use Read Web Page on relevant result URLs before answering factual or detailed web questions.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)); public string GetSettingsFieldLabel(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { "baseUrl" => I18N.I.T("SearXNG URL", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultLanguage" => I18N.I.T("Default Language", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultSafeSearch" => I18N.I.T("Default Safe Search", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultCategories" => I18N.I.T("Default Categories", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultEngines" => I18N.I.T("Default Engines", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "maxResults" => I18N.I.T("Maximum Results", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "timeoutSeconds" => I18N.I.T("Timeout Seconds", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Title, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { "baseUrl" => I18N.I.T("Base URL of the SearXNG instance. You can enter either the instance root URL or the /search endpoint.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultLanguage" => I18N.I.T("Optional fallback language code when the model does not provide a language.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultSafeSearch" => I18N.I.T("Optional safe search policy sent to SearXNG when configured.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultCategories" => I18N.I.T("Optional comma-separated default categories. Do not set this together with default engines.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultEngines" => I18N.I.T("Optional comma-separated default engines. Do not set this together with default categories.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "maxResults" => I18N.I.T("Optional default maximum number of results returned to the model when the model does not provide a limit.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "timeoutSeconds" => I18N.I.T("Optional HTTP timeout for the search request in seconds.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { settingsValues.TryGetValue("baseUrl", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); if (!isValidBaseUrl) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = uriError, }); } var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault("defaultCategories +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::TOOLCALLINGIMPLEMENTATIONS::SEARXNGWEBSEARCHTOOL::T4262764011"] = "Search the web with a configured SearXNG instance and return candidate URLs for the model. Use Read Web Page on relevant result URLs before answering factual or detailed web questions.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)); public string GetSettingsFieldLabel(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { \"baseUrl\" => I18N.I.T(\"SearXNG URL\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultLanguage\" => I18N.I.T(\"Default Language\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultSafeSearch\" => I18N.I.T(\"Default Safe Search\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultCategories\" => I18N.I.T(\"Default Categories\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultEngines\" => I18N.I.T(\"Default Engines\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"maxResults\" => I18N.I.T(\"Maximum Results\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"timeoutSeconds\" => I18N.I.T(\"Timeout Seconds\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Title, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { \"baseUrl\" => I18N.I.T(\"Base URL of the SearXNG instance. You can enter either the instance root URL or the /search endpoint.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultLanguage\" => I18N.I.T(\"Optional fallback language code when the model does not provide a language.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultSafeSearch\" => I18N.I.T(\"Optional safe search policy sent to SearXNG when configured.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultCategories\" => I18N.I.T(\"Optional comma-separated default categories. Do not set this together with default engines.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultEngines\" => I18N.I.T(\"Optional comma-separated default engines. Do not set this together with default categories.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"maxResults\" => I18N.I.T(\"Optional default maximum number of results returned to the model when the model does not provide a limit.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"timeoutSeconds\" => I18N.I.T(\"Optional HTTP timeout for the search request in seconds.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { settingsValues.TryGetValue(\"baseUrl\", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); if (!isValidBaseUrl) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = uriError, }); } var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault(\"defaultCategories" + +-- Default Language", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultSafeSearch" => I18N.I.T("Default Safe Search", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultCategories" => I18N.I.T("Default Categories", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultEngines" => I18N.I.T("Default Engines", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "maxResults" => I18N.I.T("Maximum Results", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "timeoutSeconds" => I18N.I.T("Timeout Seconds", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Title, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { "baseUrl" => I18N.I.T("Base URL of the SearXNG instance. You can enter either the instance root URL or the /search endpoint.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultLanguage" => I18N.I.T("Optional fallback language code when the model does not provide a language.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultSafeSearch" => I18N.I.T("Optional safe search policy sent to SearXNG when configured.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultCategories" => I18N.I.T("Optional comma-separated default categories. Do not set this together with default engines.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultEngines" => I18N.I.T("Optional comma-separated default engines. Do not set this together with default categories.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "maxResults" => I18N.I.T("Optional default maximum number of results returned to the model when the model does not provide a limit.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "timeoutSeconds" => I18N.I.T("Optional HTTP timeout for the search request in seconds.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { settingsValues.TryGetValue("baseUrl", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); if (!isValidBaseUrl) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = uriError, }); } var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault("defaultCategories +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::TOOLCALLINGIMPLEMENTATIONS::SEARXNGWEBSEARCHTOOL::T54221234"] = "Default Language\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultSafeSearch\" => I18N.I.T(\"Default Safe Search\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultCategories\" => I18N.I.T(\"Default Categories\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultEngines\" => I18N.I.T(\"Default Engines\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"maxResults\" => I18N.I.T(\"Maximum Results\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"timeoutSeconds\" => I18N.I.T(\"Timeout Seconds\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Title, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch { \"baseUrl\" => I18N.I.T(\"Base URL of the SearXNG instance. You can enter either the instance root URL or the /search endpoint.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultLanguage\" => I18N.I.T(\"Optional fallback language code when the model does not provide a language.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultSafeSearch\" => I18N.I.T(\"Optional safe search policy sent to SearXNG when configured.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultCategories\" => I18N.I.T(\"Optional comma-separated default categories. Do not set this together with default engines.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultEngines\" => I18N.I.T(\"Optional comma-separated default engines. Do not set this together with default categories.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"maxResults\" => I18N.I.T(\"Optional default maximum number of results returned to the model when the model does not provide a limit.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"timeoutSeconds\" => I18N.I.T(\"Optional HTTP timeout for the search request in seconds.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { settingsValues.TryGetValue(\"baseUrl\", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); if (!isValidBaseUrl) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = uriError, }); } var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault(\"defaultCategories" + +-- Base URL of the SearXNG instance. You can enter either the instance root URL or the /search endpoint.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultLanguage" => I18N.I.T("Optional fallback language code when the model does not provide a language.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultSafeSearch" => I18N.I.T("Optional safe search policy sent to SearXNG when configured.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultCategories" => I18N.I.T("Optional comma-separated default categories. Do not set this together with default engines.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "defaultEngines" => I18N.I.T("Optional comma-separated default engines. Do not set this together with default categories.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "maxResults" => I18N.I.T("Optional default maximum number of results returned to the model when the model does not provide a limit.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), "timeoutSeconds" => I18N.I.T("Optional HTTP timeout for the search request in seconds.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { settingsValues.TryGetValue("baseUrl", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); if (!isValidBaseUrl) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = uriError, }); } var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault("defaultCategories +UI_TEXT_CONTENT["AISTUDIO::TOOLS::TOOLCALLINGSYSTEM::TOOLCALLINGIMPLEMENTATIONS::SEARXNGWEBSEARCHTOOL::T54269506"] = "Base URL of the SearXNG instance. You can enter either the instance root URL or the /search endpoint.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultLanguage\" => I18N.I.T(\"Optional fallback language code when the model does not provide a language.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultSafeSearch\" => I18N.I.T(\"Optional safe search policy sent to SearXNG when configured.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultCategories\" => I18N.I.T(\"Optional comma-separated default categories. Do not set this together with default engines.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"defaultEngines\" => I18N.I.T(\"Optional comma-separated default engines. Do not set this together with default categories.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"maxResults\" => I18N.I.T(\"Optional default maximum number of results returned to the model when the model does not provide a limit.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), \"timeoutSeconds\" => I18N.I.T(\"Optional HTTP timeout for the search request in seconds.\", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), }; public Task ValidateConfigurationAsync( ToolDefinition definition, IReadOnlyDictionary settingsValues, CancellationToken token = default) { settingsValues.TryGetValue(\"baseUrl\", out var baseUrl); var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); if (!isValidBaseUrl) { return Task.FromResult(new ToolConfigurationState { IsConfigured = false, Message = uriError, }); } var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault(\"defaultCategories" + -- Pandoc Installation UI_TEXT_CONTENT["AISTUDIO::TOOLS::USERFILE::T185447014"] = "Pandoc Installation" diff --git a/app/MindWork AI Studio/Chat/ChatThread.cs b/app/MindWork AI Studio/Chat/ChatThread.cs index e8277cb5f..2ba1d7d53 100644 --- a/app/MindWork AI Studio/Chat/ChatThread.cs +++ b/app/MindWork AI Studio/Chat/ChatThread.cs @@ -1,8 +1,11 @@ using System.Globalization; +using System.Text.Json.Serialization; using AIStudio.Components; using AIStudio.Settings; using AIStudio.Settings.DataModel; +using AIStudio.Tools; +using AIStudio.Tools.ToolCallingSystem; using AIStudio.Tools.ERIClient.DataModel; namespace AIStudio.Chat; @@ -79,6 +82,12 @@ public sealed record ChatThread /// The content blocks of the chat thread. /// public List Blocks { get; init; } = []; + + [JsonIgnore] + public AIStudio.Tools.Components RuntimeComponent { get; set; } = AIStudio.Tools.Components.CHAT; + + [JsonIgnore] + public HashSet RuntimeSelectedToolIds { get; set; } = []; private bool allowProfile = true; @@ -185,6 +194,17 @@ public string PrepareSystemPrompt(SettingsManager settingsManager) } LOGGER.LogInformation(logMessage); + + var toolPolicy = this.BuildToolPolicyPrompt(); + if (!string.IsNullOrWhiteSpace(toolPolicy)) + { + systemPromptText = $""" + {systemPromptText} + + {toolPolicy} + """; + } + if(!this.IncludeDateTime) return systemPromptText; @@ -205,6 +225,28 @@ public string PrepareSystemPrompt(SettingsManager settingsManager) """; } + private string BuildToolPolicyPrompt() + { + var normalizedToolIds = ToolSelectionRules.NormalizeSelection(this.RuntimeSelectedToolIds); + var hasWebSearch = normalizedToolIds.Contains(ToolSelectionRules.WEB_SEARCH_TOOL_ID); + var hasReadWebPage = normalizedToolIds.Contains(ToolSelectionRules.READ_WEB_PAGE_TOOL_ID); + + if (hasWebSearch && hasReadWebPage) + return """ + Tool usage policy for web search: + - Use the `web_search`-tool to discover relevant candidate URLs. + - Do not answer substantive web questions from search snippets alone when `read_web_page` is available. + - Search snippets alone are only sufficient for simple link-finding or very high-level orientation. + - After `web_search`, use the `read_web_page`-tool on at least one relevant result before answering questions that require facts, summaries, comparisons, current information, or other page-level details. + - Prefer answering from the extracted page content when it is available. + - Summarize tool results in natural language. + - Treat `read_web_page` results as working material for synthesis, not as final answer text. + - Add a sources-section to the end of your answer, where you link the sources that you used. + """; + + return string.Empty; + } + /// /// Removes a content block from this chat thread. /// @@ -287,4 +329,4 @@ public void Remove(IContent content, bool removeForRegenerate = false) return new Tools.ERIClient.DataModel.ChatThread { ContentBlocks = contentBlocks }; } -} \ No newline at end of file +} diff --git a/app/MindWork AI Studio/Chat/ContentBlockComponent.razor b/app/MindWork AI Studio/Chat/ContentBlockComponent.razor index 8d0689da1..4e0402bce 100644 --- a/app/MindWork AI Studio/Chat/ContentBlockComponent.razor +++ b/app/MindWork AI Studio/Chat/ContentBlockComponent.razor @@ -11,9 +11,27 @@ - - @this.Role.ToName() (@this.Time.LocalDateTime) - + + + @this.Role.ToName() (@this.Time.LocalDateTime) + + @if (this.HasToolTrace) + { + + + + + + + + + } + @if (this.Content.FileAttachments.Count > 0) @@ -96,6 +114,67 @@ } else { + @if (this.HasToolTrace && this.showToolTrace) + { + + + @string.Format(T("Tool Calls ({0})"), textContent.ToolInvocations.Count) + + @foreach (var invocation in textContent.ToolInvocations.OrderBy(x => x.Order)) + { + + + + + + @($"{invocation.Order}. {invocation.ToolName}") + + @this.GetTraceStatusText(invocation) + + + + + + + @if (this.IsToolInvocationExpanded(invocation.Order)) + { + @if (!string.IsNullOrWhiteSpace(invocation.StatusMessage)) + { + @invocation.StatusMessage + } + + @T("Result") + + @this.GetToolInvocationResult(invocation) + + + @T("Arguments") + @if (invocation.Arguments.Count == 0) + { + @T("No arguments") + } + else + { + + @foreach (var argument in invocation.Arguments) + { + + @argument.Key: @argument.Value + + } + + } + } + + } + + } + var renderPlan = this.GetMarkdownRenderPlan(textContent.Text);
@foreach (var segment in renderPlan.Segments) @@ -115,6 +194,13 @@ }
+ + @if (this.Role is ChatRole.AI && !string.IsNullOrWhiteSpace(textContent.ToolRuntimeStatus.Message)) + { + + @textContent.ToolRuntimeStatus.Message + + } } } } diff --git a/app/MindWork AI Studio/Chat/ContentBlockComponent.razor.cs b/app/MindWork AI Studio/Chat/ContentBlockComponent.razor.cs index e0b035ce1..84b41232d 100644 --- a/app/MindWork AI Studio/Chat/ContentBlockComponent.razor.cs +++ b/app/MindWork AI Studio/Chat/ContentBlockComponent.razor.cs @@ -1,7 +1,9 @@ using AIStudio.Components; using AIStudio.Dialogs; using AIStudio.Tools.Services; +using AIStudio.Tools.ToolCallingSystem; using Microsoft.AspNetCore.Components; +using MudBlazor; namespace AIStudio.Chat; @@ -103,6 +105,8 @@ public partial class ContentBlockComponent : MSGComponentBase, IAsyncDisposable private string lastMathRenderSignature = string.Empty; private bool hasActiveMathContainer; private bool isDisposed; + private bool showToolTrace; + private readonly HashSet expandedToolInvocations = []; #region Overrides of ComponentBase @@ -199,6 +203,27 @@ private int CreateRenderHash() hash.Add(textValue.Length); hash.Add(textValue.GetHashCode(StringComparison.Ordinal)); hash.Add(text.Sources.Count); + hash.Add(text.ToolInvocations.Count); + hash.Add(text.ToolRuntimeStatus.IsRunning); + hash.Add(text.ToolRuntimeStatus.Message); + hash.Add(this.showToolTrace); + hash.Add(this.expandedToolInvocations.Count); + foreach (var expandedInvocation in this.expandedToolInvocations.Order()) + hash.Add(expandedInvocation); + foreach (var invocation in text.ToolInvocations) + { + hash.Add(invocation.Order); + hash.Add(invocation.ToolId); + hash.Add(invocation.Status); + hash.Add(invocation.StatusMessage); + hash.Add(invocation.Result); + hash.Add(invocation.Arguments.Count); + foreach (var argument in invocation.Arguments) + { + hash.Add(argument.Key); + hash.Add(argument.Value); + } + } break; case ContentImage image: @@ -214,8 +239,55 @@ private int CreateRenderHash() private string CardClasses => $"my-2 rounded-lg {this.Class}"; + private bool HasToolTrace => this.Role is ChatRole.AI && this.GetToolInvocations().Count > 0; + private CodeBlockTheme CodeColorPalette => this.SettingsManager.IsDarkMode ? CodeBlockTheme.Dark : CodeBlockTheme.Default; + private static Color GetTraceColor(ToolInvocationTraceStatus status) => status switch + { + ToolInvocationTraceStatus.SUCCESS => Color.Success, + ToolInvocationTraceStatus.ERROR => Color.Error, + ToolInvocationTraceStatus.BLOCKED => Color.Warning, + _ => Color.Default, + }; + + private string GetTraceStatusText(ToolInvocationTrace trace) => trace.Status switch + { + ToolInvocationTraceStatus.SUCCESS => this.T("Executed"), + ToolInvocationTraceStatus.ERROR => this.T("Failed"), + ToolInvocationTraceStatus.BLOCKED => this.T("Blocked"), + _ => this.T("Unknown"), + }; + + private IReadOnlyList GetToolInvocations() => this.Content is ContentText textContent + ? textContent.ToolInvocations.OrderBy(x => x.Order).ToList() + : []; + + private string GetToolTraceTooltip() + { + var invocations = this.GetToolInvocations(); + return invocations.Count switch + { + 0 => this.T("No tool calls"), + 1 => string.Format(this.T("Show tool call for {0}"), invocations[0].ToolName), + _ => string.Format(this.T("Show {0} tool calls"), invocations.Count), + }; + } + + private void ToggleToolTrace() => this.showToolTrace = !this.showToolTrace; + + private bool IsToolInvocationExpanded(int order) => this.expandedToolInvocations.Contains(order); + + private void ToggleToolInvocation(int order) + { + if (!this.expandedToolInvocations.Add(order)) + this.expandedToolInvocations.Remove(order); + } + + private string GetToolInvocationResult(ToolInvocationTrace invocation) => string.IsNullOrWhiteSpace(invocation.Result) + ? this.T("No result") + : invocation.Result; + private MudMarkdownStyling MarkdownStyling => new() { CodeBlock = { Theme = this.CodeColorPalette }, diff --git a/app/MindWork AI Studio/Chat/ContentText.cs b/app/MindWork AI Studio/Chat/ContentText.cs index 3a9b8f9d4..81a34e35c 100644 --- a/app/MindWork AI Studio/Chat/ContentText.cs +++ b/app/MindWork AI Studio/Chat/ContentText.cs @@ -4,6 +4,7 @@ using AIStudio.Provider; using AIStudio.Settings; using AIStudio.Tools.RAG.RAGProcesses; +using AIStudio.Tools.ToolCallingSystem; namespace AIStudio.Chat; @@ -44,6 +45,11 @@ public sealed class ContentText : IContent /// public List FileAttachments { get; set; } = []; + public List ToolInvocations { get; set; } = []; + + [JsonIgnore] + public ToolRuntimeStatus ToolRuntimeStatus { get; set; } = new(); + /// public async Task CreateFromProviderAsync(IProvider provider, Model chatModel, IContent? lastUserPrompt, ChatThread? chatThread, CancellationToken token = default) { @@ -145,6 +151,19 @@ await Task.Run(async () => IsStreaming = this.IsStreaming, Sources = [..this.Sources], FileAttachments = [..this.FileAttachments], + ToolInvocations = [..this.ToolInvocations.Select(x => new ToolInvocationTrace + { + Order = x.Order, + ToolId = x.ToolId, + ToolName = x.ToolName, + ToolIcon = x.ToolIcon, + ToolCallId = x.ToolCallId, + Status = x.Status, + WasExecuted = x.WasExecuted, + StatusMessage = x.StatusMessage, + Arguments = new Dictionary(x.Arguments, StringComparer.Ordinal), + Result = x.Result, + })], }; #endregion @@ -214,4 +233,4 @@ public async Task PrepareTextContentForAI() /// The text content. /// public string Text { get; set; } = string.Empty; -} \ No newline at end of file +} diff --git a/app/MindWork AI Studio/Components/ChatComponent.razor b/app/MindWork AI Studio/Components/ChatComponent.razor index 20bb5ec47..8f4fbf466 100644 --- a/app/MindWork AI Studio/Components/ChatComponent.razor +++ b/app/MindWork AI Studio/Components/ChatComponent.razor @@ -123,6 +123,8 @@ + + @if (PreviewFeatures.PRE_RAG_2024.IsEnabled(this.SettingsManager)) { diff --git a/app/MindWork AI Studio/Components/ChatComponent.razor.cs b/app/MindWork AI Studio/Components/ChatComponent.razor.cs index f734d620f..88e1ec20b 100644 --- a/app/MindWork AI Studio/Components/ChatComponent.razor.cs +++ b/app/MindWork AI Studio/Components/ChatComponent.razor.cs @@ -3,6 +3,7 @@ using AIStudio.Provider; using AIStudio.Settings; using AIStudio.Settings.DataModel; +using AIStudio.Tools.ToolCallingSystem; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; @@ -64,6 +65,7 @@ public partial class ChatComponent : MSGComponentBase, IAsyncDisposable private bool mustLoadChat; private LoadChat loadChat; private bool autoSaveEnabled; + private HashSet selectedToolIds = []; private string currentWorkspaceName = string.Empty; private Guid currentWorkspaceId = Guid.Empty; private Guid currentChatThreadId = Guid.Empty; @@ -91,6 +93,7 @@ protected override async Task OnInitializedAsync() // Get the preselected chat template: this.currentChatTemplate = this.SettingsManager.GetPreselectedChatTemplate(Tools.Components.CHAT); this.userInput = this.currentChatTemplate.PredefinedUserPrompt; + this.selectedToolIds = ToolSelectionRules.NormalizeSelection(this.SettingsManager.GetDefaultToolIds(Tools.Components.CHAT)); // Apply template's file attachments, if any: foreach (var attachment in this.currentChatTemplate.FileAttachments) @@ -607,6 +610,8 @@ private async Task SendMessage(bool reuseLastUserPrompt = false) using (this.cancellationTokenSource = new()) { this.StateHasChanged(); + this.ChatThread!.RuntimeComponent = Tools.Components.CHAT; + this.ChatThread.RuntimeSelectedToolIds = ToolSelectionRules.NormalizeSelection(this.selectedToolIds); // Use the selected provider to get the AI response. // By awaiting this line, we wait for the entire @@ -636,6 +641,12 @@ private async Task CancelStreaming() if(!this.cancellationTokenSource.IsCancellationRequested) await this.cancellationTokenSource.CancelAsync(); } + + private Task SelectedToolIdsChanged(HashSet updatedToolIds) + { + this.selectedToolIds = ToolSelectionRules.NormalizeSelection(updatedToolIds); + return Task.CompletedTask; + } private async Task SaveThread() { @@ -700,6 +711,7 @@ private async Task StartNewChat(bool useSameWorkspace = false, bool deletePrevio this.isStreaming = false; this.hasUnsavedChanges = false; this.userInput = string.Empty; + this.selectedToolIds = this.SettingsManager.GetDefaultToolIds(Tools.Components.CHAT); // // Reset the LLM provider considering the user's settings: diff --git a/app/MindWork AI Studio/Components/ConfigurationMultiSelect.razor.cs b/app/MindWork AI Studio/Components/ConfigurationMultiSelect.razor.cs index e924b4fda..8aebdc204 100644 --- a/app/MindWork AI Studio/Components/ConfigurationMultiSelect.razor.cs +++ b/app/MindWork AI Studio/Components/ConfigurationMultiSelect.razor.cs @@ -33,6 +33,15 @@ public partial class ConfigurationMultiSelect : ConfigurationBaseCore /// [Parameter] public Func IsItemLocked { get; set; } = _ => false; + + [Parameter] + public string EmptySelectionText { get; set; } = "No items selected."; + + [Parameter] + public string SingleSelectionText { get; set; } = "You have selected 1 item."; + + [Parameter] + public string MultipleSelectionText { get; set; } = "You have selected {0} items."; #region Overrides of ConfigurationBase @@ -61,12 +70,12 @@ private async Task OptionChanged(IEnumerable? updatedValues) private string GetMultiSelectionText(List? selectedValues) { if(selectedValues is null || selectedValues.Count == 0) - return T("No preview features selected."); + return T(this.EmptySelectionText); if(selectedValues.Count == 1) - return T("You have selected 1 preview feature."); + return T(this.SingleSelectionText); - return string.Format(T("You have selected {0} preview features."), selectedValues.Count); + return string.Format(T(this.MultipleSelectionText), selectedValues.Count); } private bool IsLockedValue(TData value) => this.IsItemLocked(value); @@ -76,4 +85,4 @@ private string LockedTooltip() => "This feature is managed by your organization and has therefore been disabled.", typeof(ConfigurationBase).Namespace, nameof(ConfigurationBase)); -} \ No newline at end of file +} diff --git a/app/MindWork AI Studio/Components/Settings/SettingsPanelTools.razor b/app/MindWork AI Studio/Components/Settings/SettingsPanelTools.razor new file mode 100644 index 000000000..c10c1983b --- /dev/null +++ b/app/MindWork AI Studio/Components/Settings/SettingsPanelTools.razor @@ -0,0 +1,44 @@ +@using AIStudio.Tools.ToolCallingSystem +@inherits SettingsPanelBase + + + + @T("Configure global settings for each tool. Tool defaults for chat and assistants are configured in the corresponding feature settings.") + + + + + @T("Icon") + @T("Name") + @T("Description") + @T("State") + @T("Settings") + + + + + + + @context.Implementation.GetDisplayName() + + + @context.Implementation.GetDescription() + + + @if (context.ConfigurationState.IsConfigured) + { + + } + else + { + + + + } + + + + + + + diff --git a/app/MindWork AI Studio/Components/Settings/SettingsPanelTools.razor.cs b/app/MindWork AI Studio/Components/Settings/SettingsPanelTools.razor.cs new file mode 100644 index 000000000..c978d16c7 --- /dev/null +++ b/app/MindWork AI Studio/Components/Settings/SettingsPanelTools.razor.cs @@ -0,0 +1,50 @@ +using AIStudio.Dialogs.Settings; +using AIStudio.Tools; +using AIStudio.Tools.ToolCallingSystem; + +using Microsoft.AspNetCore.Components; + +namespace AIStudio.Components.Settings; + +public partial class SettingsPanelTools : SettingsPanelBase +{ + [Inject] + private ToolRegistry ToolRegistry { get; init; } = null!; + + private IReadOnlyList items = []; + + protected override async Task OnInitializedAsync() + { + await base.OnInitializedAsync(); + this.items = await this.ToolRegistry.GetCatalogAsync(this.ToolRegistry.GetAllDefinitions()); + } + + private async Task OpenSettings(string toolId) + { + var parameters = new DialogParameters + { + { x => x.ToolId, toolId }, + }; + + var dialog = await this.DialogService.ShowAsync(null, parameters, Dialogs.DialogOptions.FULLSCREEN); + await dialog.Result; + this.items = await this.ToolRegistry.GetCatalogAsync(this.ToolRegistry.GetAllDefinitions()); + this.StateHasChanged(); + } + + private string GetConfigurationTooltip(ToolCatalogItem item) => item.ConfigurationState.MissingRequiredFields.Count switch + { + _ when !string.IsNullOrWhiteSpace(item.ConfigurationState.Message) => item.ConfigurationState.Message, + 0 => this.T("This tool still needs to be configured."), + _ => string.Format(this.T("Missing required settings: {0}"), string.Join(", ", item.ConfigurationState.MissingRequiredFields.Select(fieldName => this.GetFieldDisplayName(item, fieldName)))) + }; + + private string GetFieldDisplayName(ToolCatalogItem item, string fieldName) + { + var fieldDefinition = item.Definition.SettingsSchema.Properties.GetValueOrDefault(fieldName); + if (fieldDefinition is null) + return fieldName; + + return item.Implementation.GetSettingsFieldLabel(fieldName, fieldDefinition); + } +} diff --git a/app/MindWork AI Studio/Components/ToolDefaultsConfiguration.razor b/app/MindWork AI Studio/Components/ToolDefaultsConfiguration.razor new file mode 100644 index 000000000..be71c551d --- /dev/null +++ b/app/MindWork AI Studio/Components/ToolDefaultsConfiguration.razor @@ -0,0 +1,12 @@ +@using AIStudio.Tools +@using AIStudio.Tools.ToolCallingSystem +@inherits MSGComponentBase + +@if (this.availableTools.Count > 0) +{ + @if (this.Component is not Components.CHAT && this.IncludeVisibilityToggle) + { + + } + +} diff --git a/app/MindWork AI Studio/Components/ToolDefaultsConfiguration.razor.cs b/app/MindWork AI Studio/Components/ToolDefaultsConfiguration.razor.cs new file mode 100644 index 000000000..a63f8734d --- /dev/null +++ b/app/MindWork AI Studio/Components/ToolDefaultsConfiguration.razor.cs @@ -0,0 +1,43 @@ +using AIStudio.Settings; +using AIStudio.Tools; +using AIStudio.Tools.ToolCallingSystem; + +using Microsoft.AspNetCore.Components; + +namespace AIStudio.Components; + +public partial class ToolDefaultsConfiguration : MSGComponentBase +{ + [Parameter] + public AIStudio.Tools.Components Component { get; set; } = AIStudio.Tools.Components.CHAT; + + [Parameter] + public bool IncludeVisibilityToggle { get; set; } = true; + + [Inject] + private ToolRegistry ToolRegistry { get; init; } = null!; + + private List> availableTools = []; + + private string OptionTitle => this.Component is AIStudio.Tools.Components.CHAT ? this.T("Default tools for chat") : this.T("Default tools for this assistant"); + + private string OptionHelp => this.Component is AIStudio.Tools.Components.CHAT + ? this.T("Choose which tools should be preselected for new chats.") + : this.T("Choose which tools should be preselected for new runs of this assistant."); + + private bool AreDefaultToolsDisabled => + this.Component is not AIStudio.Tools.Components.CHAT && + !this.SettingsManager.IsToolSelectionVisible(this.Component); + + protected override async Task OnInitializedAsync() + { + this.availableTools = (await this.ToolRegistry.GetCatalogAsync(this.Component)) + .Select(x => new ConfigurationSelectData(x.Implementation.GetDisplayName(), x.Definition.Id)) + .ToList(); + await base.OnInitializedAsync(); + } + + private HashSet GetSelectedValues() => this.SettingsManager.GetDefaultToolIds(this.Component); + + private void UpdateSelection(HashSet values) => this.SettingsManager.ConfigurationData.Tools.DefaultToolIdsByComponent[this.Component.ToString()] = [..ToolSelectionRules.NormalizeSelection(values)]; +} diff --git a/app/MindWork AI Studio/Components/ToolSelection.razor b/app/MindWork AI Studio/Components/ToolSelection.razor new file mode 100644 index 000000000..a98f02e27 --- /dev/null +++ b/app/MindWork AI Studio/Components/ToolSelection.razor @@ -0,0 +1,72 @@ +@using AIStudio.Settings +@using AIStudio.Tools.ToolCallingSystem +@inherits MSGComponentBase + +
+ + + + + + + + + + @T("Tool Selection") + + + + + + @if (!this.SupportsTools) + { + @T("The selected provider or model does not support tool calling.") + } + else if (this.Disabled) + { + + @T("Tool changes are locked while a response is running. Your current selection is shown below and applies again from the next message once the run is finished.") + + } + else if (this.catalog.Count == 0) + { + @T("No tools are available in this context.") + } + + @if (this.SupportsTools && this.catalog.Count > 0) + { + @foreach (var item in this.catalog) + { + var isSelected = this.SelectedToolIds.Contains(item.Definition.Id); + var isConfigured = item.ConfigurationState.IsConfigured; + var dependencyHint = this.GetDependencyHint(item.Definition.Id); + + + + + + + @item.Implementation.GetDisplayName() + + + + + @if (!isConfigured) + { + @(string.IsNullOrWhiteSpace(item.ConfigurationState.Message) ? T("Required settings are missing. Configure this tool before enabling it.") : item.ConfigurationState.Message) + } + @if (!string.IsNullOrWhiteSpace(dependencyHint)) + { + @dependencyHint + } + + } + } + + + + @T("Close") + + + +
diff --git a/app/MindWork AI Studio/Components/ToolSelection.razor.cs b/app/MindWork AI Studio/Components/ToolSelection.razor.cs new file mode 100644 index 000000000..7f96fb069 --- /dev/null +++ b/app/MindWork AI Studio/Components/ToolSelection.razor.cs @@ -0,0 +1,98 @@ +using AIStudio.Dialogs.Settings; +using AIStudio.Provider; +using AIStudio.Settings; +using AIStudio.Tools; +using AIStudio.Tools.ToolCallingSystem; + +using Microsoft.AspNetCore.Components; + +namespace AIStudio.Components; + +public partial class ToolSelection : MSGComponentBase +{ + [Parameter] + public AIStudio.Tools.Components Component { get; set; } = AIStudio.Tools.Components.CHAT; + + [Parameter] + public required AIStudio.Settings.Provider LLMProvider { get; set; } + + [Parameter] + public HashSet SelectedToolIds { get; set; } = []; + + [Parameter] + public EventCallback> SelectedToolIdsChanged { get; set; } + + [Parameter] + public bool Disabled { get; set; } + + [Parameter] + public string PopoverButtonClasses { get; set; } = string.Empty; + + [Inject] + private ToolRegistry ToolRegistry { get; init; } = null!; + + [Inject] + private IDialogService DialogService { get; init; } = null!; + + private bool showSelection; + private IReadOnlyList catalog = []; + + protected override void OnParametersSet() + { + this.SelectedToolIds = ToolSelectionRules.NormalizeSelection(this.SelectedToolIds); + base.OnParametersSet(); + } + + private bool SupportsTools => + this.LLMProvider != AIStudio.Settings.Provider.NONE && + this.LLMProvider.GetModelCapabilities().Contains(Capability.CHAT_COMPLETION_API) && + this.LLMProvider.GetModelCapabilities().Contains(Capability.FUNCTION_CALLING); + + private async Task ToggleSelection() + { + this.showSelection = !this.showSelection; + if (this.showSelection) + this.catalog = await this.ToolRegistry.GetCatalogAsync(this.Component); + } + + private void Hide() => this.showSelection = false; + + private async Task ChangeSelection(string toolId, bool isSelected) + { + var updated = new HashSet(this.SelectedToolIds, StringComparer.Ordinal); + if (isSelected) + updated.Add(toolId); + else + updated.Remove(toolId); + + updated = ToolSelectionRules.NormalizeSelection(updated); + this.SelectedToolIds = updated; + await this.SelectedToolIdsChanged.InvokeAsync(updated); + } + + private bool IsSelectionLockedByDependency(string toolId) => ToolSelectionRules.IsRequiredBySelectedTools(toolId, this.SelectedToolIds); + + private string? GetDependencyHint(string toolId) + { + if (toolId == ToolSelectionRules.WEB_SEARCH_TOOL_ID) + return this.T("Enabling this tool also enables Read Web Page."); + + if (this.IsSelectionLockedByDependency(toolId)) + return this.T("This tool is currently required because Web Search is enabled."); + + return null; + } + + private async Task OpenSettings(string toolId) + { + var parameters = new DialogParameters + { + { x => x.ToolId, toolId }, + }; + + var dialog = await this.DialogService.ShowAsync(null, parameters, Dialogs.DialogOptions.FULLSCREEN); + await dialog.Result; + this.catalog = await this.ToolRegistry.GetCatalogAsync(this.Component); + this.StateHasChanged(); + } +} diff --git a/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogAgenda.razor b/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogAgenda.razor index dcaf18ff7..789d7a016 100644 --- a/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogAgenda.razor +++ b/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogAgenda.razor @@ -36,6 +36,7 @@ + diff --git a/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogAssistantBias.razor b/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogAssistantBias.razor index 40f3331f0..c00fe8d40 100644 --- a/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogAssistantBias.razor +++ b/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogAssistantBias.razor @@ -32,6 +32,7 @@ + diff --git a/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogChat.razor b/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogChat.razor index d9ed5a90a..94f9b0211 100644 --- a/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogChat.razor +++ b/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogChat.razor @@ -22,6 +22,8 @@ + + @if (PreviewFeatures.PRE_RAG_2024.IsEnabled(this.SettingsManager)) { diff --git a/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogCoding.razor b/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogCoding.razor index 6cfed1ac7..2dd954f65 100644 --- a/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogCoding.razor +++ b/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogCoding.razor @@ -22,6 +22,7 @@ + Close diff --git a/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogGrammarSpelling.razor b/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogGrammarSpelling.razor index 7130f3cf0..87a37a4f1 100644 --- a/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogGrammarSpelling.razor +++ b/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogGrammarSpelling.razor @@ -19,10 +19,11 @@ + @T("Close") - \ No newline at end of file + diff --git a/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogI18N.razor b/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogI18N.razor index a64528d09..6f7d87984 100644 --- a/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogI18N.razor +++ b/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogI18N.razor @@ -19,10 +19,11 @@ + @T("Close") - \ No newline at end of file + diff --git a/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogIconFinder.razor b/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogIconFinder.razor index 187e0523e..d4cb90fce 100644 --- a/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogIconFinder.razor +++ b/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogIconFinder.razor @@ -15,10 +15,11 @@ + @T("Close") - \ No newline at end of file + diff --git a/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogJobPostings.razor b/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogJobPostings.razor index 9d2c47bcd..563a7c347 100644 --- a/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogJobPostings.razor +++ b/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogJobPostings.razor @@ -26,10 +26,11 @@ + @T("Close") - \ No newline at end of file + diff --git a/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogLegalCheck.razor b/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogLegalCheck.razor index 71947b14f..817774ab4 100644 --- a/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogLegalCheck.razor +++ b/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogLegalCheck.razor @@ -17,6 +17,7 @@ + diff --git a/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogMyTasks.razor b/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogMyTasks.razor index 1fed1f083..dacffa8cc 100644 --- a/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogMyTasks.razor +++ b/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogMyTasks.razor @@ -20,6 +20,7 @@ + diff --git a/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogRewrite.razor b/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogRewrite.razor index 6cdfc96f6..5849256b4 100644 --- a/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogRewrite.razor +++ b/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogRewrite.razor @@ -21,10 +21,11 @@ + @T("Close") - \ No newline at end of file + diff --git a/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogSlideBuilder.razor b/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogSlideBuilder.razor index 18d512803..6d019598f 100644 --- a/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogSlideBuilder.razor +++ b/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogSlideBuilder.razor @@ -25,6 +25,7 @@ + diff --git a/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogSynonyms.razor b/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogSynonyms.razor index 0a78e616f..9dc1fdf2b 100644 --- a/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogSynonyms.razor +++ b/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogSynonyms.razor @@ -19,10 +19,11 @@ + @T("Close") - \ No newline at end of file + diff --git a/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogTextSummarizer.razor b/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogTextSummarizer.razor index 9e1e183b2..f17e57ad3 100644 --- a/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogTextSummarizer.razor +++ b/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogTextSummarizer.razor @@ -29,10 +29,11 @@ + @T("Close") - \ No newline at end of file + diff --git a/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogTranslation.razor b/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogTranslation.razor index f3db4a3c0..61187dc86 100644 --- a/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogTranslation.razor +++ b/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogTranslation.razor @@ -23,10 +23,11 @@ + @T("Close") - \ No newline at end of file + diff --git a/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogWritingEMails.razor b/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogWritingEMails.razor index ff96ced6b..2ff22788d 100644 --- a/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogWritingEMails.razor +++ b/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogWritingEMails.razor @@ -23,6 +23,7 @@ + diff --git a/app/MindWork AI Studio/Dialogs/Settings/ToolSettingsDialog.razor b/app/MindWork AI Studio/Dialogs/Settings/ToolSettingsDialog.razor new file mode 100644 index 000000000..b41ee6161 --- /dev/null +++ b/app/MindWork AI Studio/Dialogs/Settings/ToolSettingsDialog.razor @@ -0,0 +1,52 @@ +@using AIStudio.Tools.ToolCallingSystem +@inherits SettingsDialogBase + + + + + + @(this.implementation?.GetDisplayName() ?? T("Tool Settings")) + + + + @if (this.toolDefinition is null) + { + @T("The selected tool could not be loaded.") + } + else + { + + @this.implementation?.GetDescription() + + + + @foreach (var property in this.toolDefinition.SettingsSchema.Properties) + { + var fieldName = property.Key; + var field = property.Value; + if (field.EnumValues.Count > 0) + { + + @foreach (var option in field.EnumValues) + { + @option + } + + } + else + { + + } + } + + } + + + + @T("Cancel") + + + @T("Save") + + + diff --git a/app/MindWork AI Studio/Dialogs/Settings/ToolSettingsDialog.razor.cs b/app/MindWork AI Studio/Dialogs/Settings/ToolSettingsDialog.razor.cs new file mode 100644 index 000000000..e4cf432c5 --- /dev/null +++ b/app/MindWork AI Studio/Dialogs/Settings/ToolSettingsDialog.razor.cs @@ -0,0 +1,51 @@ +using AIStudio.Tools.ToolCallingSystem; + +using Microsoft.AspNetCore.Components; + +namespace AIStudio.Dialogs.Settings; + +public partial class ToolSettingsDialog : SettingsDialogBase +{ + [Parameter] + public string ToolId { get; set; } = string.Empty; + + [Inject] + private ToolRegistry ToolRegistry { get; init; } = null!; + + [Inject] + private ToolSettingsService ToolSettingsService { get; init; } = null!; + + private ToolDefinition? toolDefinition; + private IToolImplementation? implementation; + private Dictionary values = new(StringComparer.Ordinal); + + protected override async Task OnInitializedAsync() + { + await base.OnInitializedAsync(); + this.toolDefinition = this.ToolRegistry.GetDefinition(this.ToolId); + if (this.toolDefinition is not null) + { + this.implementation = this.ToolRegistry.GetImplementation(this.toolDefinition.ImplementationKey); + this.values = await this.ToolSettingsService.GetSettingsAsync(this.toolDefinition); + } + } + + private string GetValue(string fieldName) => this.values.GetValueOrDefault(fieldName, string.Empty); + + private string GetFieldLabel(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => + this.implementation?.GetSettingsFieldLabel(fieldName, fieldDefinition) ?? fieldDefinition.Title; + + private string GetFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => + this.implementation?.GetSettingsFieldDescription(fieldName, fieldDefinition) ?? fieldDefinition.Description; + + private void UpdateValue(string fieldName, string? value) => this.values[fieldName] = value ?? string.Empty; + + private async Task Save() + { + if (this.toolDefinition is null) + return; + + await this.ToolSettingsService.SaveSettingsAsync(this.toolDefinition, this.values); + this.MudDialog.Close(); + } +} diff --git a/app/MindWork AI Studio/Pages/Settings.razor b/app/MindWork AI Studio/Pages/Settings.razor index 702018075..56ec6e990 100644 --- a/app/MindWork AI Studio/Pages/Settings.razor +++ b/app/MindWork AI Studio/Pages/Settings.razor @@ -21,6 +21,8 @@ } + + @if (PreviewFeatures.PRE_RAG_2024.IsEnabled(this.SettingsManager)) { @@ -31,4 +33,4 @@ - \ No newline at end of file + diff --git a/app/MindWork AI Studio/Program.cs b/app/MindWork AI Studio/Program.cs index f19344d6f..0effbcc5c 100644 --- a/app/MindWork AI Studio/Program.cs +++ b/app/MindWork AI Studio/Program.cs @@ -1,10 +1,11 @@ using AIStudio.Agents; using AIStudio.Settings; +using AIStudio.Tools.ToolCallingSystem; using AIStudio.Tools.Databases; using AIStudio.Tools.Databases.Qdrant; using AIStudio.Tools.PluginSystem; using AIStudio.Tools.Services; - +using AIStudio.Tools.ToolCallingSystem.ToolCallingImplementations; using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.Extensions.Logging.Console; @@ -168,6 +169,12 @@ public static async Task Main() builder.Services.AddSingleton(rust); builder.Services.AddMudMarkdownClipboardService(); builder.Services.AddSingleton(); + builder.Services.AddSingleton(); + builder.Services.AddSingleton(); + builder.Services.AddSingleton(); + builder.Services.AddSingleton(); + builder.Services.AddSingleton(); + builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); diff --git a/app/MindWork AI Studio/Provider/AlibabaCloud/ProviderAlibabaCloud.cs b/app/MindWork AI Studio/Provider/AlibabaCloud/ProviderAlibabaCloud.cs index 3535809d0..24a6f4961 100644 --- a/app/MindWork AI Studio/Provider/AlibabaCloud/ProviderAlibabaCloud.cs +++ b/app/MindWork AI Studio/Provider/AlibabaCloud/ProviderAlibabaCloud.cs @@ -1,7 +1,5 @@ using System.Net.Http.Headers; using System.Runtime.CompilerServices; -using System.Text; -using System.Text.Json; using AIStudio.Chat; using AIStudio.Provider.OpenAI; @@ -24,52 +22,23 @@ public sealed class ProviderAlibabaCloud() : BaseProvider(LLMProviders.ALIBABA_C /// public override async IAsyncEnumerable StreamChatCompletion(Model chatModel, ChatThread chatThread, SettingsManager settingsManager, [EnumeratorCancellation] CancellationToken token = default) { - // Get the API key: - var requestedSecret = await RUST_SERVICE.GetAPIKey(this, SecretStoreType.LLM_PROVIDER); - if(!requestedSecret.Success) - yield break; - - // Prepare the system prompt: - var systemPrompt = new TextMessage - { - Role = "system", - Content = chatThread.PrepareSystemPrompt(settingsManager), - }; - - // Parse the API parameters: - var apiParameters = this.ParseAdditionalApiParameters(); - - // Build the list of messages: - var messages = await chatThread.Blocks.BuildMessagesUsingNestedImageUrlAsync(this.Provider, chatModel); - - // Prepare the AlibabaCloud HTTP chat request: - var alibabaCloudChatRequest = JsonSerializer.Serialize(new ChatCompletionAPIRequest - { - Model = chatModel.Id, - - // Build the messages: - // - First of all the system prompt - // - Then none-empty user and AI messages - Messages = [systemPrompt, ..messages], - - Stream = true, - AdditionalApiParameters = apiParameters - }, JSON_SERIALIZER_OPTIONS); - - async Task RequestBuilder() - { - // Build the HTTP post request: - var request = new HttpRequestMessage(HttpMethod.Post, "chat/completions"); - - // Set the authorization header: - request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await requestedSecret.Secret.Decrypt(ENCRYPTION)); - - // Set the content: - request.Content = new StringContent(alibabaCloudChatRequest, Encoding.UTF8, "application/json"); - return request; - } - - await foreach (var content in this.StreamChatCompletionInternal("AlibabaCloud", RequestBuilder, token)) + await foreach (var content in this.StreamOpenAICompatibleChatCompletion( + "AlibabaCloud", + chatModel, + chatThread, + settingsManager, + () => chatThread.Blocks.BuildMessagesUsingNestedImageUrlAsync(this.Provider, chatModel), + (systemPrompt, messages, apiParameters, stream, tools) => + Task.FromResult(new ChatCompletionAPIRequest + { + Model = chatModel.Id, + Messages = [systemPrompt, ..messages], + Stream = stream, + Tools = tools, + ParallelToolCalls = tools is null ? null : true, + AdditionalApiParameters = apiParameters + }), + token: token)) yield return content; } @@ -183,4 +152,4 @@ private async Task> LoadModels(string[] prefixes, SecretStore return modelResponse.Data.Where(model => prefixes.Any(prefix => model.Id.StartsWith(prefix, StringComparison.InvariantCulture))); } -} \ No newline at end of file +} diff --git a/app/MindWork AI Studio/Provider/BaseProvider.cs b/app/MindWork AI Studio/Provider/BaseProvider.cs index 9b7298241..2488db637 100644 --- a/app/MindWork AI Studio/Provider/BaseProvider.cs +++ b/app/MindWork AI Studio/Provider/BaseProvider.cs @@ -10,11 +10,14 @@ using AIStudio.Provider.OpenAI; using AIStudio.Provider.SelfHosted; using AIStudio.Settings; +using AIStudio.Tools.ToolCallingSystem; using AIStudio.Tools.MIME; using AIStudio.Tools.PluginSystem; using AIStudio.Tools.Rust; using AIStudio.Tools.Services; +using Microsoft.Extensions.DependencyInjection; + using Host = AIStudio.Provider.SelfHosted.Host; namespace AIStudio.Provider; @@ -565,6 +568,206 @@ protected async IAsyncEnumerable StreamResponsesInternal + /// Streams the chat completion from an OpenAI-compatible provider using the Chat Completion API. + /// + /// The provider name for logging and error reporting. + /// The selected chat model. + /// The current chat thread. + /// The settings manager. + /// Builds the provider-specific base messages. + /// Builds the provider-specific request body. + /// The secret store type. + /// Whether the API key is optional. + /// The system prompt role to use. + /// The request path, relative to the provider base URL. + /// Optional additional headers to add. + /// The cancellation token. + /// The delta stream line type. + /// The annotation stream line type. + /// The streamed content chunks. + protected async IAsyncEnumerable StreamOpenAICompatibleChatCompletion( + string providerName, + Model chatModel, + ChatThread chatThread, + SettingsManager settingsManager, + Func>> messagesFactory, + Func, IDictionary, bool, IList?, Task> requestFactory, + SecretStoreType storeType = SecretStoreType.LLM_PROVIDER, + bool isTryingSecret = false, + string systemPromptRole = "system", + string requestPath = "chat/completions", + Action? headersAction = null, + [EnumeratorCancellation] CancellationToken token = default) + where TDelta : IResponseStreamLine + where TAnnotation : IAnnotationStreamLine + { + // Get the API key: + var requestedSecret = await RUST_SERVICE.GetAPIKey(this, storeType, isTrying: isTryingSecret); + if(!requestedSecret.Success && !isTryingSecret) + yield break; + + // Prepare the system prompt: + var systemPrompt = new TextMessage + { + Role = systemPromptRole, + Content = chatThread.PrepareSystemPrompt(settingsManager), + }; + + // Parse the API parameters: + var apiParameters = this.ParseAdditionalApiParameters(); + + var baseMessages = await messagesFactory(); + var toolRegistry = Program.SERVICE_PROVIDER.GetService(); + var toolExecutor = Program.SERVICE_PROVIDER.GetService(); + var currentAssistantContent = chatThread.Blocks.LastOrDefault(x => x.Role is ChatRole.AI)?.Content as ContentText; + currentAssistantContent?.ToolInvocations.Clear(); + + if (toolRegistry is not null && toolExecutor is not null) + { + var runnableTools = await toolRegistry.GetRunnableToolsAsync( + chatThread.RuntimeComponent, + chatThread.RuntimeSelectedToolIds, + this.Provider.GetModelCapabilities(chatModel), + settingsManager.IsToolSelectionVisible(chatThread.RuntimeComponent)); + + if (runnableTools.Count > 0) + { + var providerTools = runnableTools.Select(x => (object)new + { + type = "function", + function = new + { + name = x.Definition.Function.Name, + description = x.Definition.Function.Description, + parameters = x.Definition.Function.Parameters, + strict = x.Definition.Function.Strict, + } + }).ToList(); + + var internalMessages = new List(); + var toolCallCount = 0; + while (true) + { + var requestDto = await requestFactory(systemPrompt, [..baseMessages, ..internalMessages], apiParameters, false, providerTools); + var response = await this.ExecuteChatCompletionRequest(requestDto, requestPath, requestedSecret, headersAction, token); + var responseMessage = response?.Choices.FirstOrDefault()?.Message; + if (responseMessage is null) + yield break; + + if (responseMessage.ToolCalls.Count == 0) + { + currentAssistantContent!.ToolRuntimeStatus = new(); + if (!string.IsNullOrWhiteSpace(responseMessage.Content)) + yield return new ContentStreamChunk(responseMessage.Content, []); + + yield break; + } + + currentAssistantContent!.ToolRuntimeStatus = new ToolRuntimeStatus + { + IsRunning = true, + ToolNames = responseMessage.ToolCalls + .Select(x => runnableTools.FirstOrDefault(tool => tool.Definition.Function.Name.Equals(x.Function.Name, StringComparison.Ordinal)).Implementation?.GetDisplayName() ?? x.Function.Name) + .ToList(), + }; + await currentAssistantContent.StreamingEvent(); + + internalMessages.Add(new AssistantToolCallMessage + { + Content = responseMessage.Content, + ToolCalls = responseMessage.ToolCalls, + }); + + foreach (var toolCall in responseMessage.ToolCalls) + { + toolCallCount++; + if (toolCallCount > 10) + { + var limitMessage = "Tool calling stopped because the maximum of 10 tool calls was reached."; + currentAssistantContent.ToolInvocations.Add(new ToolInvocationTrace + { + Order = toolCallCount, + ToolId = toolCall.Function.Name, + ToolName = toolCall.Function.Name, + ToolCallId = toolCall.Id, + Status = ToolInvocationTraceStatus.BLOCKED, + StatusMessage = limitMessage, + Result = limitMessage, + }); + currentAssistantContent.ToolRuntimeStatus = new(); + await currentAssistantContent.StreamingEvent(); + yield return new ContentStreamChunk(limitMessage, []); + yield break; + } + + var (toolContent, trace) = await toolExecutor.ExecuteAsync( + toolCall.Id, + toolCall.Function.Name, + toolCall.Function.Arguments, + runnableTools, + toolCallCount, + token); + + currentAssistantContent.ToolInvocations.Add(trace); + internalMessages.Add(new ToolResultMessage + { + Content = toolContent, + ToolCallId = toolCall.Id, + Name = toolCall.Function.Name, + }); + } + + await currentAssistantContent.StreamingEvent(); + } + } + } + + // Prepare the provider HTTP chat request: + var providerChatRequest = JsonSerializer.Serialize(await requestFactory(systemPrompt, baseMessages, apiParameters, true, null), JSON_SERIALIZER_OPTIONS); + + async Task RequestBuilder() + { + // Build the HTTP post request: + var request = new HttpRequestMessage(HttpMethod.Post, requestPath); + + // Set the authorization header: + if (requestedSecret.Success) + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await requestedSecret.Secret.Decrypt(ENCRYPTION)); + + // Set provider-specific headers: + headersAction?.Invoke(request.Headers); + + // Set the content: + request.Content = new StringContent(providerChatRequest, Encoding.UTF8, "application/json"); + return request; + } + + await foreach (var content in this.StreamChatCompletionInternal(providerName, RequestBuilder, token)) + yield return content; + } + + private async Task ExecuteChatCompletionRequest( + ChatCompletionAPIRequest requestDto, + string requestPath, + RequestedSecret requestedSecret, + Action? headersAction, + CancellationToken token) + { + using var request = new HttpRequestMessage(HttpMethod.Post, requestPath); + if (requestedSecret.Success) + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await requestedSecret.Secret.Decrypt(ENCRYPTION)); + + headersAction?.Invoke(request.Headers); + request.Content = new StringContent(JsonSerializer.Serialize(requestDto, JSON_SERIALIZER_OPTIONS), Encoding.UTF8, "application/json"); + + using var response = await this.httpClient.SendAsync(request, token); + if (!response.IsSuccessStatusCode) + return null; + + return await response.Content.ReadFromJsonAsync(JSON_SERIALIZER_OPTIONS, token); + } + protected async Task PerformStandardTranscriptionRequest(RequestedSecret requestedSecret, Model transcriptionModel, string audioFilePath, Host host = Host.NONE, CancellationToken token = default) { try diff --git a/app/MindWork AI Studio/Provider/DeepSeek/ProviderDeepSeek.cs b/app/MindWork AI Studio/Provider/DeepSeek/ProviderDeepSeek.cs index e1ae306a2..9ebce9240 100644 --- a/app/MindWork AI Studio/Provider/DeepSeek/ProviderDeepSeek.cs +++ b/app/MindWork AI Studio/Provider/DeepSeek/ProviderDeepSeek.cs @@ -1,7 +1,5 @@ using System.Net.Http.Headers; using System.Runtime.CompilerServices; -using System.Text; -using System.Text.Json; using AIStudio.Chat; using AIStudio.Provider.OpenAI; @@ -24,52 +22,23 @@ public sealed class ProviderDeepSeek() : BaseProvider(LLMProviders.DEEP_SEEK, "h /// public override async IAsyncEnumerable StreamChatCompletion(Model chatModel, ChatThread chatThread, SettingsManager settingsManager, [EnumeratorCancellation] CancellationToken token = default) { - // Get the API key: - var requestedSecret = await RUST_SERVICE.GetAPIKey(this, SecretStoreType.LLM_PROVIDER); - if(!requestedSecret.Success) - yield break; - - // Prepare the system prompt: - var systemPrompt = new TextMessage - { - Role = "system", - Content = chatThread.PrepareSystemPrompt(settingsManager), - }; - - // Parse the API parameters: - var apiParameters = this.ParseAdditionalApiParameters(); - - // Build the list of messages: - var messages = await chatThread.Blocks.BuildMessagesUsingDirectImageUrlAsync(this.Provider, chatModel); - - // Prepare the DeepSeek HTTP chat request: - var deepSeekChatRequest = JsonSerializer.Serialize(new ChatCompletionAPIRequest - { - Model = chatModel.Id, - - // Build the messages: - // - First of all the system prompt - // - Then none-empty user and AI messages - Messages = [systemPrompt, ..messages], - - Stream = true, - AdditionalApiParameters = apiParameters - }, JSON_SERIALIZER_OPTIONS); - - async Task RequestBuilder() - { - // Build the HTTP post request: - var request = new HttpRequestMessage(HttpMethod.Post, "chat/completions"); - - // Set the authorization header: - request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await requestedSecret.Secret.Decrypt(ENCRYPTION)); - - // Set the content: - request.Content = new StringContent(deepSeekChatRequest, Encoding.UTF8, "application/json"); - return request; - } - - await foreach (var content in this.StreamChatCompletionInternal("DeepSeek", RequestBuilder, token)) + await foreach (var content in this.StreamOpenAICompatibleChatCompletion( + "DeepSeek", + chatModel, + chatThread, + settingsManager, + () => chatThread.Blocks.BuildMessagesUsingDirectImageUrlAsync(this.Provider, chatModel), + (systemPrompt, messages, apiParameters, stream, tools) => + Task.FromResult(new ChatCompletionAPIRequest + { + Model = chatModel.Id, + Messages = [systemPrompt, ..messages], + Stream = stream, + Tools = tools, + ParallelToolCalls = tools is null ? null : true, + AdditionalApiParameters = apiParameters + }), + token: token)) yield return content; } @@ -144,4 +113,4 @@ private async Task> LoadModels(SecretStoreType storeType, Can var modelResponse = await response.Content.ReadFromJsonAsync(token); return modelResponse.Data; } -} \ No newline at end of file +} diff --git a/app/MindWork AI Studio/Provider/Fireworks/ChatRequest.cs b/app/MindWork AI Studio/Provider/Fireworks/ChatRequest.cs deleted file mode 100644 index 54963feb3..000000000 --- a/app/MindWork AI Studio/Provider/Fireworks/ChatRequest.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System.Text.Json.Serialization; - -namespace AIStudio.Provider.Fireworks; - -/// -/// The Fireworks chat request model. -/// -/// Which model to use for chat completion. -/// The chat messages. -/// Whether to stream the chat completion. -public readonly record struct ChatRequest( - string Model, - IList Messages, - bool Stream -) -{ - // Attention: The "required" modifier is not supported for [JsonExtensionData]. - [JsonExtensionData] - public IDictionary AdditionalApiParameters { get; init; } = new Dictionary(); -} \ No newline at end of file diff --git a/app/MindWork AI Studio/Provider/Fireworks/ProviderFireworks.cs b/app/MindWork AI Studio/Provider/Fireworks/ProviderFireworks.cs index 2254b7adf..0fbdcb7e5 100644 --- a/app/MindWork AI Studio/Provider/Fireworks/ProviderFireworks.cs +++ b/app/MindWork AI Studio/Provider/Fireworks/ProviderFireworks.cs @@ -1,7 +1,4 @@ -using System.Net.Http.Headers; using System.Runtime.CompilerServices; -using System.Text; -using System.Text.Json; using AIStudio.Chat; using AIStudio.Provider.OpenAI; @@ -24,53 +21,23 @@ public class ProviderFireworks() : BaseProvider(LLMProviders.FIREWORKS, "https:/ /// public override async IAsyncEnumerable StreamChatCompletion(Model chatModel, ChatThread chatThread, SettingsManager settingsManager, [EnumeratorCancellation] CancellationToken token = default) { - // Get the API key: - var requestedSecret = await RUST_SERVICE.GetAPIKey(this, SecretStoreType.LLM_PROVIDER); - if(!requestedSecret.Success) - yield break; - - // Prepare the system prompt: - var systemPrompt = new TextMessage - { - Role = "system", - Content = chatThread.PrepareSystemPrompt(settingsManager), - }; - - // Parse the API parameters: - var apiParameters = this.ParseAdditionalApiParameters(); - - // Build the list of messages: - var messages = await chatThread.Blocks.BuildMessagesUsingNestedImageUrlAsync(this.Provider, chatModel); - - // Prepare the Fireworks HTTP chat request: - var fireworksChatRequest = JsonSerializer.Serialize(new ChatRequest - { - Model = chatModel.Id, - - // Build the messages: - // - First of all the system prompt - // - Then none-empty user and AI messages - Messages = [systemPrompt, ..messages], - - // Right now, we only support streaming completions: - Stream = true, - AdditionalApiParameters = apiParameters - }, JSON_SERIALIZER_OPTIONS); - - async Task RequestBuilder() - { - // Build the HTTP post request: - var request = new HttpRequestMessage(HttpMethod.Post, "chat/completions"); - - // Set the authorization header: - request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await requestedSecret.Secret.Decrypt(ENCRYPTION)); - - // Set the content: - request.Content = new StringContent(fireworksChatRequest, Encoding.UTF8, "application/json"); - return request; - } - - await foreach (var content in this.StreamChatCompletionInternal("Fireworks", RequestBuilder, token)) + await foreach (var content in this.StreamOpenAICompatibleChatCompletion( + "Fireworks", + chatModel, + chatThread, + settingsManager, + () => chatThread.Blocks.BuildMessagesUsingNestedImageUrlAsync(this.Provider, chatModel), + (systemPrompt, messages, apiParameters, stream, tools) => + Task.FromResult(new ChatCompletionAPIRequest + { + Model = chatModel.Id, + Messages = [systemPrompt, ..messages], + Stream = stream, + Tools = tools, + ParallelToolCalls = tools is null ? null : true, + AdditionalApiParameters = apiParameters + }), + token: token)) yield return content; } @@ -126,4 +93,4 @@ public override Task> GetTranscriptionModels(string? apiKeyPr } #endregion -} \ No newline at end of file +} diff --git a/app/MindWork AI Studio/Provider/GWDG/ProviderGWDG.cs b/app/MindWork AI Studio/Provider/GWDG/ProviderGWDG.cs index 41e19fa9d..07bab5e85 100644 --- a/app/MindWork AI Studio/Provider/GWDG/ProviderGWDG.cs +++ b/app/MindWork AI Studio/Provider/GWDG/ProviderGWDG.cs @@ -1,7 +1,5 @@ using System.Net.Http.Headers; using System.Runtime.CompilerServices; -using System.Text; -using System.Text.Json; using AIStudio.Chat; using AIStudio.Provider.OpenAI; @@ -24,52 +22,23 @@ public sealed class ProviderGWDG() : BaseProvider(LLMProviders.GWDG, "https://ch /// public override async IAsyncEnumerable StreamChatCompletion(Model chatModel, ChatThread chatThread, SettingsManager settingsManager, [EnumeratorCancellation] CancellationToken token = default) { - // Get the API key: - var requestedSecret = await RUST_SERVICE.GetAPIKey(this, SecretStoreType.LLM_PROVIDER); - if(!requestedSecret.Success) - yield break; - - // Prepare the system prompt: - var systemPrompt = new TextMessage - { - Role = "system", - Content = chatThread.PrepareSystemPrompt(settingsManager), - }; - - // Parse the API parameters: - var apiParameters = this.ParseAdditionalApiParameters(); - - // Build the list of messages: - var messages = await chatThread.Blocks.BuildMessagesUsingNestedImageUrlAsync(this.Provider, chatModel); - - // Prepare the GWDG HTTP chat request: - var gwdgChatRequest = JsonSerializer.Serialize(new ChatCompletionAPIRequest - { - Model = chatModel.Id, - - // Build the messages: - // - First of all the system prompt - // - Then none-empty user and AI messages - Messages = [systemPrompt, ..messages], - - Stream = true, - AdditionalApiParameters = apiParameters - }, JSON_SERIALIZER_OPTIONS); - - async Task RequestBuilder() - { - // Build the HTTP post request: - var request = new HttpRequestMessage(HttpMethod.Post, "chat/completions"); - - // Set the authorization header: - request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await requestedSecret.Secret.Decrypt(ENCRYPTION)); - - // Set the content: - request.Content = new StringContent(gwdgChatRequest, Encoding.UTF8, "application/json"); - return request; - } - - await foreach (var content in this.StreamChatCompletionInternal("GWDG", RequestBuilder, token)) + await foreach (var content in this.StreamOpenAICompatibleChatCompletion( + "GWDG", + chatModel, + chatThread, + settingsManager, + () => chatThread.Blocks.BuildMessagesUsingNestedImageUrlAsync(this.Provider, chatModel), + (systemPrompt, messages, apiParameters, stream, tools) => + Task.FromResult(new ChatCompletionAPIRequest + { + Model = chatModel.Id, + Messages = [systemPrompt, ..messages], + Stream = stream, + Tools = tools, + ParallelToolCalls = tools is null ? null : true, + AdditionalApiParameters = apiParameters + }), + token: token)) yield return content; } @@ -152,4 +121,4 @@ private async Task> LoadModels(SecretStoreType storeType, Can var modelResponse = await response.Content.ReadFromJsonAsync(token); return modelResponse.Data; } -} \ No newline at end of file +} diff --git a/app/MindWork AI Studio/Provider/Google/ChatRequest.cs b/app/MindWork AI Studio/Provider/Google/ChatRequest.cs deleted file mode 100644 index 1a898c3a9..000000000 --- a/app/MindWork AI Studio/Provider/Google/ChatRequest.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System.Text.Json.Serialization; - -namespace AIStudio.Provider.Google; - -/// -/// The Google chat request model. -/// -/// Which model to use for chat completion. -/// The chat messages. -/// Whether to stream the chat completion. -public readonly record struct ChatRequest( - string Model, - IList Messages, - bool Stream -) -{ - // Attention: The "required" modifier is not supported for [JsonExtensionData]. - [JsonExtensionData] - public IDictionary AdditionalApiParameters { get; init; } = new Dictionary(); -} \ No newline at end of file diff --git a/app/MindWork AI Studio/Provider/Google/ProviderGoogle.cs b/app/MindWork AI Studio/Provider/Google/ProviderGoogle.cs index 8a86fcbec..6d3be3a11 100644 --- a/app/MindWork AI Studio/Provider/Google/ProviderGoogle.cs +++ b/app/MindWork AI Studio/Provider/Google/ProviderGoogle.cs @@ -24,53 +24,23 @@ public class ProviderGoogle() : BaseProvider(LLMProviders.GOOGLE, "https://gener /// public override async IAsyncEnumerable StreamChatCompletion(Model chatModel, ChatThread chatThread, SettingsManager settingsManager, [EnumeratorCancellation] CancellationToken token = default) { - // Get the API key: - var requestedSecret = await RUST_SERVICE.GetAPIKey(this, SecretStoreType.LLM_PROVIDER); - if(!requestedSecret.Success) - yield break; - - // Prepare the system prompt: - var systemPrompt = new TextMessage - { - Role = "system", - Content = chatThread.PrepareSystemPrompt(settingsManager), - }; - - // Parse the API parameters: - var apiParameters = this.ParseAdditionalApiParameters(); - - // Build the list of messages: - var messages = await chatThread.Blocks.BuildMessagesUsingNestedImageUrlAsync(this.Provider, chatModel); - - // Prepare the Google HTTP chat request: - var geminiChatRequest = JsonSerializer.Serialize(new ChatRequest - { - Model = chatModel.Id, - - // Build the messages: - // - First of all the system prompt - // - Then none-empty user and AI messages - Messages = [systemPrompt, ..messages], - - // Right now, we only support streaming completions: - Stream = true, - AdditionalApiParameters = apiParameters - }, JSON_SERIALIZER_OPTIONS); - - async Task RequestBuilder() - { - // Build the HTTP post request: - var request = new HttpRequestMessage(HttpMethod.Post, "chat/completions"); - - // Set the authorization header: - request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await requestedSecret.Secret.Decrypt(ENCRYPTION)); - - // Set the content: - request.Content = new StringContent(geminiChatRequest, Encoding.UTF8, "application/json"); - return request; - } - - await foreach (var content in this.StreamChatCompletionInternal("Google", RequestBuilder, token)) + await foreach (var content in this.StreamOpenAICompatibleChatCompletion( + "Google", + chatModel, + chatThread, + settingsManager, + () => chatThread.Blocks.BuildMessagesUsingNestedImageUrlAsync(this.Provider, chatModel), + (systemPrompt, messages, apiParameters, stream, tools) => + Task.FromResult(new ChatCompletionAPIRequest + { + Model = chatModel.Id, + Messages = [systemPrompt, ..messages], + Stream = stream, + Tools = tools, + ParallelToolCalls = tools is null ? null : true, + AdditionalApiParameters = apiParameters + }), + token: token)) yield return content; } @@ -256,4 +226,4 @@ private string NormalizeModelId(string modelId) ? modelId["models/".Length..] : modelId; } -} \ No newline at end of file +} diff --git a/app/MindWork AI Studio/Provider/Groq/ChatRequest.cs b/app/MindWork AI Studio/Provider/Groq/ChatRequest.cs deleted file mode 100644 index 2e7668f1e..000000000 --- a/app/MindWork AI Studio/Provider/Groq/ChatRequest.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System.Text.Json.Serialization; - -namespace AIStudio.Provider.Groq; - -/// -/// The Groq chat request model. -/// -/// Which model to use for chat completion. -/// The chat messages. -/// Whether to stream the chat completion. -/// The seed for the chat completion. -public readonly record struct ChatRequest( - string Model, - IList Messages, - bool Stream, - int Seed -) -{ - // Attention: The "required" modifier is not supported for [JsonExtensionData]. - [JsonExtensionData] - public IDictionary AdditionalApiParameters { get; init; } = new Dictionary(); -} \ No newline at end of file diff --git a/app/MindWork AI Studio/Provider/Groq/ProviderGroq.cs b/app/MindWork AI Studio/Provider/Groq/ProviderGroq.cs index 8f938667e..c7aac97bf 100644 --- a/app/MindWork AI Studio/Provider/Groq/ProviderGroq.cs +++ b/app/MindWork AI Studio/Provider/Groq/ProviderGroq.cs @@ -1,7 +1,5 @@ using System.Net.Http.Headers; using System.Runtime.CompilerServices; -using System.Text; -using System.Text.Json; using AIStudio.Chat; using AIStudio.Provider.OpenAI; @@ -24,53 +22,28 @@ public class ProviderGroq() : BaseProvider(LLMProviders.GROQ, "https://api.groq. /// public override async IAsyncEnumerable StreamChatCompletion(Model chatModel, ChatThread chatThread, SettingsManager settingsManager, [EnumeratorCancellation] CancellationToken token = default) { - // Get the API key: - var requestedSecret = await RUST_SERVICE.GetAPIKey(this, SecretStoreType.LLM_PROVIDER); - if(!requestedSecret.Success) - yield break; - - // Prepare the system prompt: - var systemPrompt = new TextMessage - { - Role = "system", - Content = chatThread.PrepareSystemPrompt(settingsManager), - }; - - // Parse the API parameters: - var apiParameters = this.ParseAdditionalApiParameters(); - - // Build the list of messages: - var messages = await chatThread.Blocks.BuildMessagesUsingNestedImageUrlAsync(this.Provider, chatModel); - - // Prepare the OpenAI HTTP chat request: - var groqChatRequest = JsonSerializer.Serialize(new ChatRequest - { - Model = chatModel.Id, - - // Build the messages: - // - First of all the system prompt - // - Then none-empty user and AI messages - Messages = [systemPrompt, ..messages], - - // Right now, we only support streaming completions: - Stream = true, - AdditionalApiParameters = apiParameters - }, JSON_SERIALIZER_OPTIONS); - - async Task RequestBuilder() - { - // Build the HTTP post request: - var request = new HttpRequestMessage(HttpMethod.Post, "chat/completions"); - - // Set the authorization header: - request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await requestedSecret.Secret.Decrypt(ENCRYPTION)); - - // Set the content: - request.Content = new StringContent(groqChatRequest, Encoding.UTF8, "application/json"); - return request; - } - - await foreach (var content in this.StreamChatCompletionInternal("Groq", RequestBuilder, token)) + await foreach (var content in this.StreamOpenAICompatibleChatCompletion( + "Groq", + chatModel, + chatThread, + settingsManager, + () => chatThread.Blocks.BuildMessagesUsingNestedImageUrlAsync(this.Provider, chatModel), + (systemPrompt, messages, apiParameters, stream, tools) => + { + if (TryPopIntParameter(apiParameters, "seed", out var parsedSeed)) + apiParameters["seed"] = parsedSeed; + + return Task.FromResult(new ChatCompletionAPIRequest + { + Model = chatModel.Id, + Messages = [systemPrompt, ..messages], + Stream = stream, + Tools = tools, + ParallelToolCalls = tools is null ? null : true, + AdditionalApiParameters = apiParameters + }); + }, + token: token)) yield return content; } @@ -148,4 +121,4 @@ private async Task> LoadModels(SecretStoreType storeType, Can !n.Id.StartsWith("distil-", StringComparison.OrdinalIgnoreCase) && !n.Id.Contains("-tts", StringComparison.OrdinalIgnoreCase)); } -} \ No newline at end of file +} diff --git a/app/MindWork AI Studio/Provider/Helmholtz/ProviderHelmholtz.cs b/app/MindWork AI Studio/Provider/Helmholtz/ProviderHelmholtz.cs index 070597a33..7f347a904 100644 --- a/app/MindWork AI Studio/Provider/Helmholtz/ProviderHelmholtz.cs +++ b/app/MindWork AI Studio/Provider/Helmholtz/ProviderHelmholtz.cs @@ -1,7 +1,5 @@ using System.Net.Http.Headers; using System.Runtime.CompilerServices; -using System.Text; -using System.Text.Json; using AIStudio.Chat; using AIStudio.Provider.OpenAI; @@ -24,52 +22,23 @@ public sealed class ProviderHelmholtz() : BaseProvider(LLMProviders.HELMHOLTZ, " /// public override async IAsyncEnumerable StreamChatCompletion(Model chatModel, ChatThread chatThread, SettingsManager settingsManager, [EnumeratorCancellation] CancellationToken token = default) { - // Get the API key: - var requestedSecret = await RUST_SERVICE.GetAPIKey(this, SecretStoreType.LLM_PROVIDER); - if(!requestedSecret.Success) - yield break; - - // Prepare the system prompt: - var systemPrompt = new TextMessage - { - Role = "system", - Content = chatThread.PrepareSystemPrompt(settingsManager), - }; - - // Parse the API parameters: - var apiParameters = this.ParseAdditionalApiParameters(); - - // Build the list of messages: - var messages = await chatThread.Blocks.BuildMessagesUsingNestedImageUrlAsync(this.Provider, chatModel); - - // Prepare the Helmholtz HTTP chat request: - var helmholtzChatRequest = JsonSerializer.Serialize(new ChatCompletionAPIRequest - { - Model = chatModel.Id, - - // Build the messages: - // - First of all the system prompt - // - Then none-empty user and AI messages - Messages = [systemPrompt, ..messages], - - Stream = true, - AdditionalApiParameters = apiParameters - }, JSON_SERIALIZER_OPTIONS); - - async Task RequestBuilder() - { - // Build the HTTP post request: - var request = new HttpRequestMessage(HttpMethod.Post, "chat/completions"); - - // Set the authorization header: - request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await requestedSecret.Secret.Decrypt(ENCRYPTION)); - - // Set the content: - request.Content = new StringContent(helmholtzChatRequest, Encoding.UTF8, "application/json"); - return request; - } - - await foreach (var content in this.StreamChatCompletionInternal("Helmholtz", RequestBuilder, token)) + await foreach (var content in this.StreamOpenAICompatibleChatCompletion( + "Helmholtz", + chatModel, + chatThread, + settingsManager, + () => chatThread.Blocks.BuildMessagesUsingNestedImageUrlAsync(this.Provider, chatModel), + (systemPrompt, messages, apiParameters, stream, tools) => + Task.FromResult(new ChatCompletionAPIRequest + { + Model = chatModel.Id, + Messages = [systemPrompt, ..messages], + Stream = stream, + Tools = tools, + ParallelToolCalls = tools is null ? null : true, + AdditionalApiParameters = apiParameters + }), + token: token)) yield return content; } @@ -151,4 +120,4 @@ private async Task> LoadModels(SecretStoreType storeType, Can var modelResponse = await response.Content.ReadFromJsonAsync(token); return modelResponse.Data; } -} \ No newline at end of file +} diff --git a/app/MindWork AI Studio/Provider/HuggingFace/ProviderHuggingFace.cs b/app/MindWork AI Studio/Provider/HuggingFace/ProviderHuggingFace.cs index f2e8c380c..6cfbd85f4 100644 --- a/app/MindWork AI Studio/Provider/HuggingFace/ProviderHuggingFace.cs +++ b/app/MindWork AI Studio/Provider/HuggingFace/ProviderHuggingFace.cs @@ -1,7 +1,4 @@ -using System.Net.Http.Headers; -using System.Runtime.CompilerServices; -using System.Text; -using System.Text.Json; +using System.Runtime.CompilerServices; using AIStudio.Chat; using AIStudio.Provider.OpenAI; @@ -29,52 +26,23 @@ public ProviderHuggingFace(HFInferenceProvider hfProvider, Model model) : base(L /// public override async IAsyncEnumerable StreamChatCompletion(Model chatModel, ChatThread chatThread, SettingsManager settingsManager, [EnumeratorCancellation] CancellationToken token = default) { - // Get the API key: - var requestedSecret = await RUST_SERVICE.GetAPIKey(this, SecretStoreType.LLM_PROVIDER); - if(!requestedSecret.Success) - yield break; - - // Prepare the system prompt: - var systemPrompt = new TextMessage - { - Role = "system", - Content = chatThread.PrepareSystemPrompt(settingsManager), - }; - - // Parse the API parameters: - var apiParameters = this.ParseAdditionalApiParameters(); - - // Build the list of messages: - var message = await chatThread.Blocks.BuildMessagesUsingNestedImageUrlAsync(this.Provider, chatModel); - - // Prepare the HuggingFace HTTP chat request: - var huggingfaceChatRequest = JsonSerializer.Serialize(new ChatCompletionAPIRequest - { - Model = chatModel.Id, - - // Build the messages: - // - First of all the system prompt - // - Then none-empty user and AI messages - Messages = [systemPrompt, ..message], - - Stream = true, - AdditionalApiParameters = apiParameters - }, JSON_SERIALIZER_OPTIONS); - - async Task RequestBuilder() - { - // Build the HTTP post request: - var request = new HttpRequestMessage(HttpMethod.Post, "chat/completions"); - - // Set the authorization header: - request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await requestedSecret.Secret.Decrypt(ENCRYPTION)); - - // Set the content: - request.Content = new StringContent(huggingfaceChatRequest, Encoding.UTF8, "application/json"); - return request; - } - - await foreach (var content in this.StreamChatCompletionInternal("HuggingFace", RequestBuilder, token)) + await foreach (var content in this.StreamOpenAICompatibleChatCompletion( + "HuggingFace", + chatModel, + chatThread, + settingsManager, + () => chatThread.Blocks.BuildMessagesUsingNestedImageUrlAsync(this.Provider, chatModel), + (systemPrompt, messages, apiParameters, stream, tools) => + Task.FromResult(new ChatCompletionAPIRequest + { + Model = chatModel.Id, + Messages = [systemPrompt, ..messages], + Stream = stream, + Tools = tools, + ParallelToolCalls = tools is null ? null : true, + AdditionalApiParameters = apiParameters + }), + token: token)) yield return content; } @@ -123,4 +91,4 @@ public override Task> GetTranscriptionModels(string? apiKeyPr } #endregion -} \ No newline at end of file +} diff --git a/app/MindWork AI Studio/Provider/Mistral/ChatRequest.cs b/app/MindWork AI Studio/Provider/Mistral/ChatRequest.cs deleted file mode 100644 index 1d42081fa..000000000 --- a/app/MindWork AI Studio/Provider/Mistral/ChatRequest.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System.Text.Json.Serialization; - -namespace AIStudio.Provider.Mistral; - -/// -/// The OpenAI chat request model. -/// -/// Which model to use for chat completion. -/// The chat messages. -/// Whether to stream the chat completion. -/// The seed for the chat completion. -/// Whether to inject a safety prompt before all conversations. -public readonly record struct ChatRequest( - string Model, - IList Messages, - bool Stream, - [property: JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] - int? RandomSeed, - bool SafePrompt = false -) -{ - // Attention: The "required" modifier is not supported for [JsonExtensionData]. - [JsonExtensionData] - public IDictionary AdditionalApiParameters { get; init; } = new Dictionary(); -} diff --git a/app/MindWork AI Studio/Provider/Mistral/ProviderMistral.cs b/app/MindWork AI Studio/Provider/Mistral/ProviderMistral.cs index 485729fb2..247352142 100644 --- a/app/MindWork AI Studio/Provider/Mistral/ProviderMistral.cs +++ b/app/MindWork AI Studio/Provider/Mistral/ProviderMistral.cs @@ -1,7 +1,5 @@ using System.Net.Http.Headers; using System.Runtime.CompilerServices; -using System.Text; -using System.Text.Json; using AIStudio.Chat; using AIStudio.Provider.OpenAI; @@ -22,58 +20,31 @@ public sealed class ProviderMistral() : BaseProvider(LLMProviders.MISTRAL, "http /// public override async IAsyncEnumerable StreamChatCompletion(Provider.Model chatModel, ChatThread chatThread, SettingsManager settingsManager, [EnumeratorCancellation] CancellationToken token = default) { - // Get the API key: - var requestedSecret = await RUST_SERVICE.GetAPIKey(this, SecretStoreType.LLM_PROVIDER); - if(!requestedSecret.Success) - yield break; - - // Prepare the system prompt: - var systemPrompt = new TextMessage - { - Role = "system", - Content = chatThread.PrepareSystemPrompt(settingsManager), - }; - - // Parse the API parameters: - var apiParameters = this.ParseAdditionalApiParameters(); - var safePrompt = TryPopBoolParameter(apiParameters, "safe_prompt", out var parsedSafePrompt) && parsedSafePrompt; - var randomSeed = TryPopIntParameter(apiParameters, "random_seed", out var parsedRandomSeed) ? parsedRandomSeed : (int?)null; - - // Build the list of messages: - var messages = await chatThread.Blocks.BuildMessagesUsingDirectImageUrlAsync(this.Provider, chatModel); - - // Prepare the Mistral HTTP chat request: - var mistralChatRequest = JsonSerializer.Serialize(new ChatRequest - { - Model = chatModel.Id, - - // Build the messages: - // - First of all the system prompt - // - Then none-empty user and AI messages - Messages = [systemPrompt, ..messages], - - // Right now, we only support streaming completions: - Stream = true, - RandomSeed = randomSeed, - SafePrompt = safePrompt, - AdditionalApiParameters = apiParameters - }, JSON_SERIALIZER_OPTIONS); - - - async Task RequestBuilder() - { - // Build the HTTP post request: - var request = new HttpRequestMessage(HttpMethod.Post, "chat/completions"); - - // Set the authorization header: - request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await requestedSecret.Secret.Decrypt(ENCRYPTION)); - - // Set the content: - request.Content = new StringContent(mistralChatRequest, Encoding.UTF8, "application/json"); - return request; - } - - await foreach (var content in this.StreamChatCompletionInternal("Mistral", RequestBuilder, token)) + await foreach (var content in this.StreamOpenAICompatibleChatCompletion( + "Mistral", + chatModel, + chatThread, + settingsManager, + () => chatThread.Blocks.BuildMessagesUsingDirectImageUrlAsync(this.Provider, chatModel), + (systemPrompt, messages, apiParameters, stream, tools) => + { + if (TryPopBoolParameter(apiParameters, "safe_prompt", out var parsedSafePrompt)) + apiParameters["safe_prompt"] = parsedSafePrompt; + + if (TryPopIntParameter(apiParameters, "random_seed", out var parsedRandomSeed)) + apiParameters["random_seed"] = parsedRandomSeed; + + return Task.FromResult(new ChatCompletionAPIRequest + { + Model = chatModel.Id, + Messages = [systemPrompt, ..messages], + Stream = stream, + Tools = tools, + ParallelToolCalls = tools is null ? null : true, + AdditionalApiParameters = apiParameters + }); + }, + token: token)) yield return content; } diff --git a/app/MindWork AI Studio/Provider/OpenAI/AssistantToolCallMessage.cs b/app/MindWork AI Studio/Provider/OpenAI/AssistantToolCallMessage.cs new file mode 100644 index 000000000..340d1e74d --- /dev/null +++ b/app/MindWork AI Studio/Provider/OpenAI/AssistantToolCallMessage.cs @@ -0,0 +1,10 @@ +namespace AIStudio.Provider.OpenAI; + +public sealed record AssistantToolCallMessage : IMessageBase +{ + public string Role { get; init; } = "assistant"; + + public string? Content { get; init; } + + public IList ToolCalls { get; init; } = []; +} diff --git a/app/MindWork AI Studio/Provider/OpenAI/ChatCompletionAPIRequest.cs b/app/MindWork AI Studio/Provider/OpenAI/ChatCompletionAPIRequest.cs index bd9c08e7c..c789385e9 100644 --- a/app/MindWork AI Studio/Provider/OpenAI/ChatCompletionAPIRequest.cs +++ b/app/MindWork AI Studio/Provider/OpenAI/ChatCompletionAPIRequest.cs @@ -17,8 +17,12 @@ bool Stream public ChatCompletionAPIRequest() : this(string.Empty, [], true) { } + + public IList? Tools { get; init; } + + public bool? ParallelToolCalls { get; init; } // Attention: The "required" modifier is not supported for [JsonExtensionData]. [JsonExtensionData] public IDictionary AdditionalApiParameters { get; init; } = new Dictionary(); -} \ No newline at end of file +} diff --git a/app/MindWork AI Studio/Provider/OpenAI/ChatCompletionResponse.cs b/app/MindWork AI Studio/Provider/OpenAI/ChatCompletionResponse.cs new file mode 100644 index 000000000..7c23d0ef0 --- /dev/null +++ b/app/MindWork AI Studio/Provider/OpenAI/ChatCompletionResponse.cs @@ -0,0 +1,10 @@ +namespace AIStudio.Provider.OpenAI; + +public sealed record ChatCompletionResponse +{ + public string Id { get; init; } = string.Empty; + + public string Model { get; init; } = string.Empty; + + public IList Choices { get; init; } = []; +} diff --git a/app/MindWork AI Studio/Provider/OpenAI/ChatCompletionResponseChoice.cs b/app/MindWork AI Studio/Provider/OpenAI/ChatCompletionResponseChoice.cs new file mode 100644 index 000000000..71887dc9d --- /dev/null +++ b/app/MindWork AI Studio/Provider/OpenAI/ChatCompletionResponseChoice.cs @@ -0,0 +1,10 @@ +namespace AIStudio.Provider.OpenAI; + +public sealed record ChatCompletionResponseChoice +{ + public int Index { get; init; } + + public string FinishReason { get; init; } = string.Empty; + + public ChatCompletionResponseMessage Message { get; init; } = new(); +} diff --git a/app/MindWork AI Studio/Provider/OpenAI/ChatCompletionResponseMessage.cs b/app/MindWork AI Studio/Provider/OpenAI/ChatCompletionResponseMessage.cs new file mode 100644 index 000000000..43fdbade2 --- /dev/null +++ b/app/MindWork AI Studio/Provider/OpenAI/ChatCompletionResponseMessage.cs @@ -0,0 +1,10 @@ +namespace AIStudio.Provider.OpenAI; + +public sealed record ChatCompletionResponseMessage +{ + public string Role { get; init; } = string.Empty; + + public string? Content { get; init; } + + public IList ToolCalls { get; init; } = []; +} diff --git a/app/MindWork AI Studio/Provider/OpenAI/ChatCompletionToolCall.cs b/app/MindWork AI Studio/Provider/OpenAI/ChatCompletionToolCall.cs new file mode 100644 index 000000000..4ba1ec597 --- /dev/null +++ b/app/MindWork AI Studio/Provider/OpenAI/ChatCompletionToolCall.cs @@ -0,0 +1,10 @@ +namespace AIStudio.Provider.OpenAI; + +public sealed record ChatCompletionToolCall +{ + public string Id { get; init; } = string.Empty; + + public string Type { get; init; } = "function"; + + public ChatCompletionToolFunction Function { get; init; } = new(); +} diff --git a/app/MindWork AI Studio/Provider/OpenAI/ChatCompletionToolFunction.cs b/app/MindWork AI Studio/Provider/OpenAI/ChatCompletionToolFunction.cs new file mode 100644 index 000000000..248b91f22 --- /dev/null +++ b/app/MindWork AI Studio/Provider/OpenAI/ChatCompletionToolFunction.cs @@ -0,0 +1,8 @@ +namespace AIStudio.Provider.OpenAI; + +public sealed record ChatCompletionToolFunction +{ + public string Name { get; init; } = string.Empty; + + public string Arguments { get; init; } = string.Empty; +} diff --git a/app/MindWork AI Studio/Provider/OpenAI/ProviderOpenAI.cs b/app/MindWork AI Studio/Provider/OpenAI/ProviderOpenAI.cs index e5b6ebfdd..5f717d8bd 100644 --- a/app/MindWork AI Studio/Provider/OpenAI/ProviderOpenAI.cs +++ b/app/MindWork AI Studio/Provider/OpenAI/ProviderOpenAI.cs @@ -63,25 +63,24 @@ public override async IAsyncEnumerable StreamChatCompletion( // Check if we are using the Responses API or the Chat Completion API: var usingResponsesAPI = modelCapabilities.Contains(Capability.RESPONSES_API); + var useChatCompletionsForTools = + chatThread.RuntimeSelectedToolIds.Count > 0 && + modelCapabilities.Contains(Capability.CHAT_COMPLETION_API) && + modelCapabilities.Contains(Capability.FUNCTION_CALLING); + if (useChatCompletionsForTools) + usingResponsesAPI = false; // Prepare the request path based on the API we are using: var requestPath = usingResponsesAPI ? "responses" : "chat/completions"; LOGGER.LogInformation("Using the system prompt role '{SystemPromptRole}' and the '{RequestPath}' API for model '{ChatModelId}'.", systemPromptRole, requestPath, chatModel.Id); - // Prepare the system prompt: - var systemPrompt = new TextMessage - { - Role = systemPromptRole, - Content = chatThread.PrepareSystemPrompt(settingsManager), - }; - // // Prepare the tools we want to use: // - IList tools = modelCapabilities.Contains(Capability.WEB_SEARCH) switch + IList providerTools = modelCapabilities.Contains(Capability.WEB_SEARCH) switch { - true => [ Tools.WEB_SEARCH ], + true => [ ProviderTools.WEB_SEARCH ], _ => [] }; @@ -89,60 +88,81 @@ public override async IAsyncEnumerable StreamChatCompletion( // Parse the API parameters: var apiParameters = this.ParseAdditionalApiParameters("input", "store", "tools"); + if (!usingResponsesAPI) + { + await foreach (var content in this.StreamOpenAICompatibleChatCompletion( + "OpenAI", + chatModel, + chatThread, + settingsManager, + () => chatThread.Blocks.BuildMessagesAsync( + this.Provider, + chatModel, + role => role switch + { + ChatRole.USER => "user", + ChatRole.AI => "assistant", + ChatRole.AGENT => "assistant", + ChatRole.SYSTEM => systemPromptRole, + _ => "user", + }, + text => new SubContentText + { + Text = text, + }, + async attachment => new SubContentImageUrlNested + { + ImageUrl = new SubContentImageUrlData + { + Url = await attachment.TryAsBase64(token: token) is (true, var base64Content) + ? $"data:{attachment.DetermineMimeType()};base64,{base64Content}" + : string.Empty, + }, + }), + (systemPrompt, messages, apiParameters, stream, tools) => Task.FromResult(new ChatCompletionAPIRequest + { + Model = chatModel.Id, + Messages = [systemPrompt, ..messages], + Stream = stream, + Tools = tools, + ParallelToolCalls = tools is null ? null : true, + AdditionalApiParameters = apiParameters, + }), + systemPromptRole: systemPromptRole, + requestPath: "chat/completions", + token: token)) + yield return content; + + yield break; + } + + // Prepare the system prompt: + var systemPrompt = new TextMessage + { + Role = systemPromptRole, + Content = chatThread.PrepareSystemPrompt(settingsManager), + }; + // Build the list of messages: var messages = await chatThread.Blocks.BuildMessagesAsync( this.Provider, chatModel, - - // OpenAI-specific role mapping: role => role switch { ChatRole.USER => "user", ChatRole.AI => "assistant", ChatRole.AGENT => "assistant", ChatRole.SYSTEM => systemPromptRole, - _ => "user", }, - - // OpenAI's text sub-content depends on the model, whether we are using - // the Responses API or the Chat Completion API: - text => usingResponsesAPI switch + text => new SubContentInputText { - // Responses API uses INPUT_TEXT: - true => new SubContentInputText - { - Text = text, - }, - - // Chat Completion API uses TEXT: - false => new SubContentText - { - Text = text, - }, + Text = text, }, - - // OpenAI's image sub-content depends on the model as well, - // whether we are using the Responses API or the Chat Completion API: - async attachment => usingResponsesAPI switch + async attachment => new SubContentInputImage { - // Responses API uses INPUT_IMAGE: - true => new SubContentInputImage - { - ImageUrl = await attachment.TryAsBase64(token: token) is (true, var base64Content) - ? $"data:{attachment.DetermineMimeType()};base64,{base64Content}" - : string.Empty, - }, - - // Chat Completion API uses IMAGE_URL: - false => new SubContentImageUrlNested - { - ImageUrl = new SubContentImageUrlData - { - Url = await attachment.TryAsBase64(token: token) is (true, var base64Content) - ? $"data:{attachment.DetermineMimeType()};base64,{base64Content}" - : string.Empty, - }, - } + ImageUrl = await attachment.TryAsBase64(token: token) is (true, var base64Content) + ? $"data:{attachment.DetermineMimeType()};base64,{base64Content}" + : string.Empty, }); // @@ -178,7 +198,7 @@ public override async IAsyncEnumerable StreamChatCompletion( Store = false, // Tools we want to use: - Tools = tools, + ProviderTools = providerTools, // Additional API parameters: AdditionalApiParameters = apiParameters @@ -290,4 +310,4 @@ private async Task> LoadModels(SecretStoreType storeType, str var modelResponse = await response.Content.ReadFromJsonAsync(token); return modelResponse.Data.Where(model => prefixes.Any(prefix => model.Id.StartsWith(prefix, StringComparison.InvariantCulture))); } -} \ No newline at end of file +} diff --git a/app/MindWork AI Studio/Provider/OpenAI/Tool.cs b/app/MindWork AI Studio/Provider/OpenAI/ProviderTool.cs similarity index 79% rename from app/MindWork AI Studio/Provider/OpenAI/Tool.cs rename to app/MindWork AI Studio/Provider/OpenAI/ProviderTool.cs index 782e6b604..61170af3d 100644 --- a/app/MindWork AI Studio/Provider/OpenAI/Tool.cs +++ b/app/MindWork AI Studio/Provider/OpenAI/ProviderTool.cs @@ -1,7 +1,7 @@ namespace AIStudio.Provider.OpenAI; /// -/// Represents a tool used by the AI model. +/// Represents a tool executed on the provider side. /// /// /// Right now, only our OpenAI provider is using tools. Thus, this class is located in the @@ -9,4 +9,4 @@ namespace AIStudio.Provider.OpenAI; /// be moved into the provider namespace. /// /// The type of the tool. -public record Tool(string Type); \ No newline at end of file +public record ProviderTool(string Type); diff --git a/app/MindWork AI Studio/Provider/OpenAI/Tools.cs b/app/MindWork AI Studio/Provider/OpenAI/ProviderTools.cs similarity index 67% rename from app/MindWork AI Studio/Provider/OpenAI/Tools.cs rename to app/MindWork AI Studio/Provider/OpenAI/ProviderTools.cs index 50d2b836c..359c781bd 100644 --- a/app/MindWork AI Studio/Provider/OpenAI/Tools.cs +++ b/app/MindWork AI Studio/Provider/OpenAI/ProviderTools.cs @@ -1,14 +1,14 @@ namespace AIStudio.Provider.OpenAI; /// -/// Known tools for LLM providers. +/// Known provider-side tools for LLM providers. /// /// /// Right now, only our OpenAI provider is using tools. Thus, this class is located in the /// OpenAI namespace. In the future, when other providers also support tools, this class can /// be moved into the provider namespace. /// -public static class Tools +public static class ProviderTools { - public static readonly Tool WEB_SEARCH = new("web_search"); -} \ No newline at end of file + public static readonly ProviderTool WEB_SEARCH = new("web_search"); +} diff --git a/app/MindWork AI Studio/Provider/OpenAI/ResponsesAPIRequest.cs b/app/MindWork AI Studio/Provider/OpenAI/ResponsesAPIRequest.cs index deb315d67..739ad7ade 100644 --- a/app/MindWork AI Studio/Provider/OpenAI/ResponsesAPIRequest.cs +++ b/app/MindWork AI Studio/Provider/OpenAI/ResponsesAPIRequest.cs @@ -9,13 +9,13 @@ namespace AIStudio.Provider.OpenAI; /// The chat messages. /// Whether to stream the response. /// Whether to store the response on the server (usually OpenAI's infrastructure). -/// The tools to use for the request. +/// The provider-side tools to use for the request. public record ResponsesAPIRequest( string Model, IList Input, bool Stream, bool Store, - IList Tools) + [property: JsonPropertyName("tools")] IList ProviderTools) { public ResponsesAPIRequest() : this(string.Empty, [], true, false, []) { @@ -24,4 +24,4 @@ public ResponsesAPIRequest() : this(string.Empty, [], true, false, []) // Attention: The "required" modifier is not supported for [JsonExtensionData]. [JsonExtensionData] public IDictionary AdditionalApiParameters { get; init; } = new Dictionary(); -} \ No newline at end of file +} diff --git a/app/MindWork AI Studio/Provider/OpenAI/ToolResultMessage.cs b/app/MindWork AI Studio/Provider/OpenAI/ToolResultMessage.cs new file mode 100644 index 000000000..feb698546 --- /dev/null +++ b/app/MindWork AI Studio/Provider/OpenAI/ToolResultMessage.cs @@ -0,0 +1,12 @@ +namespace AIStudio.Provider.OpenAI; + +public sealed record ToolResultMessage : IMessage +{ + public string Role { get; init; } = "tool"; + + public string Content { get; init; } = string.Empty; + + public string ToolCallId { get; init; } = string.Empty; + + public string Name { get; init; } = string.Empty; +} diff --git a/app/MindWork AI Studio/Provider/OpenRouter/ProviderOpenRouter.cs b/app/MindWork AI Studio/Provider/OpenRouter/ProviderOpenRouter.cs index 4995cca94..f66834f8e 100644 --- a/app/MindWork AI Studio/Provider/OpenRouter/ProviderOpenRouter.cs +++ b/app/MindWork AI Studio/Provider/OpenRouter/ProviderOpenRouter.cs @@ -1,7 +1,5 @@ using System.Net.Http.Headers; using System.Runtime.CompilerServices; -using System.Text; -using System.Text.Json; using AIStudio.Chat; using AIStudio.Provider.OpenAI; @@ -27,57 +25,29 @@ public sealed class ProviderOpenRouter() : BaseProvider(LLMProviders.OPEN_ROUTER /// public override async IAsyncEnumerable StreamChatCompletion(Model chatModel, ChatThread chatThread, SettingsManager settingsManager, [EnumeratorCancellation] CancellationToken token = default) { - // Get the API key: - var requestedSecret = await RUST_SERVICE.GetAPIKey(this, SecretStoreType.LLM_PROVIDER); - if(!requestedSecret.Success) - yield break; - - // Prepare the system prompt: - var systemPrompt = new TextMessage - { - Role = "system", - Content = chatThread.PrepareSystemPrompt(settingsManager), - }; - - // Parse the API parameters: - var apiParameters = this.ParseAdditionalApiParameters(); - - // Build the list of messages: - var messages = await chatThread.Blocks.BuildMessagesUsingNestedImageUrlAsync(this.Provider, chatModel); - - // Prepare the OpenRouter HTTP chat request: - var openRouterChatRequest = JsonSerializer.Serialize(new ChatCompletionAPIRequest - { - Model = chatModel.Id, - - // Build the messages: - // - First of all the system prompt - // - Then none-empty user and AI messages - Messages = [systemPrompt, ..messages], - - // Right now, we only support streaming completions: - Stream = true, - AdditionalApiParameters = apiParameters - }, JSON_SERIALIZER_OPTIONS); - - async Task RequestBuilder() - { - // Build the HTTP post request: - var request = new HttpRequestMessage(HttpMethod.Post, "chat/completions"); - - // Set the authorization header: - request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await requestedSecret.Secret.Decrypt(ENCRYPTION)); - - // Set custom headers for project identification: - request.Headers.Add("HTTP-Referer", PROJECT_WEBSITE); - request.Headers.Add("X-Title", PROJECT_NAME); - - // Set the content: - request.Content = new StringContent(openRouterChatRequest, Encoding.UTF8, "application/json"); - return request; - } - - await foreach (var content in this.StreamChatCompletionInternal("OpenRouter", RequestBuilder, token)) + await foreach (var content in this.StreamOpenAICompatibleChatCompletion( + "OpenRouter", + chatModel, + chatThread, + settingsManager, + () => chatThread.Blocks.BuildMessagesUsingNestedImageUrlAsync(this.Provider, chatModel), + (systemPrompt, messages, apiParameters, stream, tools) => + Task.FromResult(new ChatCompletionAPIRequest + { + Model = chatModel.Id, + Messages = [systemPrompt, ..messages], + Stream = stream, + Tools = tools, + ParallelToolCalls = tools is null ? null : true, + AdditionalApiParameters = apiParameters + }), + headersAction: headers => + { + // Set custom headers for project identification: + headers.Add("HTTP-Referer", PROJECT_WEBSITE); + headers.Add("X-Title", PROJECT_NAME); + }, + token: token)) yield return content; } diff --git a/app/MindWork AI Studio/Provider/Perplexity/ProviderPerplexity.cs b/app/MindWork AI Studio/Provider/Perplexity/ProviderPerplexity.cs index 4c73dc2df..a2b972731 100644 --- a/app/MindWork AI Studio/Provider/Perplexity/ProviderPerplexity.cs +++ b/app/MindWork AI Studio/Provider/Perplexity/ProviderPerplexity.cs @@ -1,7 +1,4 @@ -using System.Net.Http.Headers; using System.Runtime.CompilerServices; -using System.Text; -using System.Text.Json; using AIStudio.Chat; using AIStudio.Provider.OpenAI; @@ -33,51 +30,23 @@ public sealed class ProviderPerplexity() : BaseProvider(LLMProviders.PERPLEXITY, /// public override async IAsyncEnumerable StreamChatCompletion(Model chatModel, ChatThread chatThread, SettingsManager settingsManager, [EnumeratorCancellation] CancellationToken token = default) { - // Get the API key: - var requestedSecret = await RUST_SERVICE.GetAPIKey(this, SecretStoreType.LLM_PROVIDER); - if(!requestedSecret.Success) - yield break; - - // Prepare the system prompt: - var systemPrompt = new TextMessage - { - Role = "system", - Content = chatThread.PrepareSystemPrompt(settingsManager), - }; - - // Parse the API parameters: - var apiParameters = this.ParseAdditionalApiParameters(); - - // Build the list of messages: - var messages = await chatThread.Blocks.BuildMessagesUsingNestedImageUrlAsync(this.Provider, chatModel); - - // Prepare the Perplexity HTTP chat request: - var perplexityChatRequest = JsonSerializer.Serialize(new ChatCompletionAPIRequest - { - Model = chatModel.Id, - - // Build the messages: - // - First of all the system prompt - // - Then none-empty user and AI messages - Messages = [systemPrompt, ..messages], - Stream = true, - AdditionalApiParameters = apiParameters - }, JSON_SERIALIZER_OPTIONS); - - async Task RequestBuilder() - { - // Build the HTTP post request: - var request = new HttpRequestMessage(HttpMethod.Post, "chat/completions"); - - // Set the authorization header: - request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await requestedSecret.Secret.Decrypt(ENCRYPTION)); - - // Set the content: - request.Content = new StringContent(perplexityChatRequest, Encoding.UTF8, "application/json"); - return request; - } - - await foreach (var content in this.StreamChatCompletionInternal("Perplexity", RequestBuilder, token)) + await foreach (var content in this.StreamOpenAICompatibleChatCompletion( + "Perplexity", + chatModel, + chatThread, + settingsManager, + () => chatThread.Blocks.BuildMessagesUsingNestedImageUrlAsync(this.Provider, chatModel), + (systemPrompt, messages, apiParameters, stream, tools) => + Task.FromResult(new ChatCompletionAPIRequest + { + Model = chatModel.Id, + Messages = [systemPrompt, ..messages], + Stream = stream, + Tools = tools, + ParallelToolCalls = tools is null ? null : true, + AdditionalApiParameters = apiParameters + }), + token: token)) yield return content; } @@ -128,4 +97,4 @@ public override Task> GetTranscriptionModels(string? apiKeyPr #endregion private Task> LoadModels() => Task.FromResult>(KNOWN_MODELS); -} \ No newline at end of file +} diff --git a/app/MindWork AI Studio/Provider/SelfHosted/ChatRequest.cs b/app/MindWork AI Studio/Provider/SelfHosted/ChatRequest.cs deleted file mode 100644 index e1da56bd2..000000000 --- a/app/MindWork AI Studio/Provider/SelfHosted/ChatRequest.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System.Text.Json.Serialization; - -namespace AIStudio.Provider.SelfHosted; - -/// -/// The chat request model. -/// -/// Which model to use for chat completion. -/// The chat messages. -/// Whether to stream the chat completion. -public readonly record struct ChatRequest( - string Model, - IList Messages, - bool Stream -) -{ - // Attention: The "required" modifier is not supported for [JsonExtensionData]. - [JsonExtensionData] - public IDictionary AdditionalApiParameters { get; init; } = new Dictionary(); -} \ No newline at end of file diff --git a/app/MindWork AI Studio/Provider/SelfHosted/ProviderSelfHosted.cs b/app/MindWork AI Studio/Provider/SelfHosted/ProviderSelfHosted.cs index 8204fa6cd..948f88e77 100644 --- a/app/MindWork AI Studio/Provider/SelfHosted/ProviderSelfHosted.cs +++ b/app/MindWork AI Studio/Provider/SelfHosted/ProviderSelfHosted.cs @@ -1,7 +1,5 @@ using System.Net.Http.Headers; using System.Runtime.CompilerServices; -using System.Text; -using System.Text.Json; using AIStudio.Chat; using AIStudio.Provider.OpenAI; @@ -25,58 +23,29 @@ public sealed class ProviderSelfHosted(Host host, string hostname) : BaseProvide /// public override async IAsyncEnumerable StreamChatCompletion(Provider.Model chatModel, ChatThread chatThread, SettingsManager settingsManager, [EnumeratorCancellation] CancellationToken token = default) { - // Get the API key: - var requestedSecret = await RUST_SERVICE.GetAPIKey(this, SecretStoreType.LLM_PROVIDER, isTrying: true); - - // Prepare the system prompt: - var systemPrompt = new TextMessage - { - Role = "system", - Content = chatThread.PrepareSystemPrompt(settingsManager), - }; - - // Parse the API parameters: - var apiParameters = this.ParseAdditionalApiParameters(); - - // Build the list of messages. The image format depends on the host: - // - Ollama uses the direct image URL format: { "type": "image_url", "image_url": "data:..." } - // - LM Studio, vLLM, and llama.cpp use the nested image URL format: { "type": "image_url", "image_url": { "url": "data:..." } } - var messages = host switch - { - Host.OLLAMA => await chatThread.Blocks.BuildMessagesUsingDirectImageUrlAsync(this.Provider, chatModel), - _ => await chatThread.Blocks.BuildMessagesUsingNestedImageUrlAsync(this.Provider, chatModel), - }; - - // Prepare the OpenAI HTTP chat request: - var providerChatRequest = JsonSerializer.Serialize(new ChatRequest - { - Model = chatModel.Id, - - // Build the messages: - // - First of all the system prompt - // - Then none-empty user and AI messages - Messages = [systemPrompt, ..messages], - - // Right now, we only support streaming completions: - Stream = true, - AdditionalApiParameters = apiParameters - }, JSON_SERIALIZER_OPTIONS); - - async Task RequestBuilder() - { - // Build the HTTP post request: - var request = new HttpRequestMessage(HttpMethod.Post, host.ChatURL()); - - // Set the authorization header: - if (requestedSecret.Success) - request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await requestedSecret.Secret.Decrypt(ENCRYPTION)); - - // Set the content: - request.Content = new StringContent(providerChatRequest, Encoding.UTF8, "application/json"); - return request; - } - - await foreach (var content in this.StreamChatCompletionInternal("self-hosted provider", RequestBuilder, token)) + await foreach (var content in this.StreamOpenAICompatibleChatCompletion( + "self-hosted provider", + chatModel, + chatThread, + settingsManager, + () => host switch + { + Host.OLLAMA => chatThread.Blocks.BuildMessagesUsingDirectImageUrlAsync(this.Provider, chatModel), + _ => chatThread.Blocks.BuildMessagesUsingNestedImageUrlAsync(this.Provider, chatModel), + }, + (systemPrompt, messages, apiParameters, stream, tools) => + Task.FromResult(new ChatCompletionAPIRequest + { + Model = chatModel.Id, + Messages = [systemPrompt, ..messages], + Stream = stream, + Tools = tools, + ParallelToolCalls = tools is null ? null : true, + AdditionalApiParameters = apiParameters + }), + isTryingSecret: true, + requestPath: host.ChatURL(), + token: token)) yield return content; } @@ -211,4 +180,4 @@ public override async Task>> EmbedTextAsync(P filterPhrases.All( filter => model.Id.Contains(filter, StringComparison.InvariantCulture))) .Select(n => new Provider.Model(n.Id, null)); } -} \ No newline at end of file +} diff --git a/app/MindWork AI Studio/Provider/X/ProviderX.cs b/app/MindWork AI Studio/Provider/X/ProviderX.cs index 21d6e2ca0..1f3cd33ba 100644 --- a/app/MindWork AI Studio/Provider/X/ProviderX.cs +++ b/app/MindWork AI Studio/Provider/X/ProviderX.cs @@ -1,7 +1,5 @@ using System.Net.Http.Headers; using System.Runtime.CompilerServices; -using System.Text; -using System.Text.Json; using AIStudio.Chat; using AIStudio.Provider.OpenAI; @@ -24,53 +22,23 @@ public sealed class ProviderX() : BaseProvider(LLMProviders.X, "https://api.x.ai /// public override async IAsyncEnumerable StreamChatCompletion(Model chatModel, ChatThread chatThread, SettingsManager settingsManager, [EnumeratorCancellation] CancellationToken token = default) { - // Get the API key: - var requestedSecret = await RUST_SERVICE.GetAPIKey(this, SecretStoreType.LLM_PROVIDER); - if(!requestedSecret.Success) - yield break; - - // Prepare the system prompt: - var systemPrompt = new TextMessage - { - Role = "system", - Content = chatThread.PrepareSystemPrompt(settingsManager), - }; - - // Parse the API parameters: - var apiParameters = this.ParseAdditionalApiParameters(); - - // Build the list of messages: - var messages = await chatThread.Blocks.BuildMessagesUsingNestedImageUrlAsync(this.Provider, chatModel); - - // Prepare the xAI HTTP chat request: - var xChatRequest = JsonSerializer.Serialize(new ChatCompletionAPIRequest - { - Model = chatModel.Id, - - // Build the messages: - // - First of all the system prompt - // - Then none-empty user and AI messages - Messages = [systemPrompt, ..messages], - - // Right now, we only support streaming completions: - Stream = true, - AdditionalApiParameters = apiParameters - }, JSON_SERIALIZER_OPTIONS); - - async Task RequestBuilder() - { - // Build the HTTP post request: - var request = new HttpRequestMessage(HttpMethod.Post, "chat/completions"); - - // Set the authorization header: - request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await requestedSecret.Secret.Decrypt(ENCRYPTION)); - - // Set the content: - request.Content = new StringContent(xChatRequest, Encoding.UTF8, "application/json"); - return request; - } - - await foreach (var content in this.StreamChatCompletionInternal("xAI", RequestBuilder, token)) + await foreach (var content in this.StreamOpenAICompatibleChatCompletion( + "xAI", + chatModel, + chatThread, + settingsManager, + () => chatThread.Blocks.BuildMessagesUsingNestedImageUrlAsync(this.Provider, chatModel), + (systemPrompt, messages, apiParameters, stream, tools) => + Task.FromResult(new ChatCompletionAPIRequest + { + Model = chatModel.Id, + Messages = [systemPrompt, ..messages], + Stream = stream, + Tools = tools, + ParallelToolCalls = tools is null ? null : true, + AdditionalApiParameters = apiParameters + }), + token: token)) yield return content; } @@ -158,4 +126,4 @@ private async Task> LoadModels(SecretStoreType storeType, str } ]); } -} \ No newline at end of file +} diff --git a/app/MindWork AI Studio/Settings/DataModel/Data.cs b/app/MindWork AI Studio/Settings/DataModel/Data.cs index d6339739f..6affb20a2 100644 --- a/app/MindWork AI Studio/Settings/DataModel/Data.cs +++ b/app/MindWork AI Studio/Settings/DataModel/Data.cs @@ -136,4 +136,6 @@ public sealed class Data public DataBiasOfTheDay BiasOfTheDay { get; init; } = new(); public DataI18N I18N { get; init; } = new(); + + public DataTools Tools { get; init; } = new(); } \ No newline at end of file diff --git a/app/MindWork AI Studio/Settings/DataModel/DataTools.cs b/app/MindWork AI Studio/Settings/DataModel/DataTools.cs new file mode 100644 index 000000000..773ada7ef --- /dev/null +++ b/app/MindWork AI Studio/Settings/DataModel/DataTools.cs @@ -0,0 +1,10 @@ +namespace AIStudio.Settings.DataModel; + +public sealed class DataTools +{ + public Dictionary> Settings { get; set; } = []; + + public Dictionary> DefaultToolIdsByComponent { get; set; } = []; + + public HashSet VisibleToolSelectionComponents { get; set; } = []; +} diff --git a/app/MindWork AI Studio/Settings/SettingsManager.cs b/app/MindWork AI Studio/Settings/SettingsManager.cs index 50c8c03e7..25678efe7 100644 --- a/app/MindWork AI Studio/Settings/SettingsManager.cs +++ b/app/MindWork AI Studio/Settings/SettingsManager.cs @@ -4,6 +4,8 @@ using AIStudio.Provider; using AIStudio.Settings.DataModel; +using AIStudio.Tools; +using AIStudio.Tools.ToolCallingSystem; using AIStudio.Tools.PluginSystem; using AIStudio.Tools.Services; @@ -344,6 +346,33 @@ public ChatTemplate GetPreselectedChatTemplate(Tools.Components component) return preselection ?? ChatTemplate.NO_CHAT_TEMPLATE; } + public HashSet GetDefaultToolIds(AIStudio.Tools.Components component) + { + var key = component.ToString(); + if (this.ConfigurationData.Tools.DefaultToolIdsByComponent.TryGetValue(key, out var toolIds)) + return ToolSelectionRules.NormalizeSelection(toolIds); + + return []; + } + + public bool IsToolSelectionVisible(AIStudio.Tools.Components component) => component switch + { + AIStudio.Tools.Components.CHAT => true, + _ => this.ConfigurationData.Tools.VisibleToolSelectionComponents.Contains(component.ToString()), + }; + + public void SetToolSelectionVisibility(AIStudio.Tools.Components component, bool isVisible) + { + if (component is AIStudio.Tools.Components.CHAT) + return; + + var key = component.ToString(); + if (isVisible) + this.ConfigurationData.Tools.VisibleToolSelectionComponents.Add(key); + else + this.ConfigurationData.Tools.VisibleToolSelectionComponents.Remove(key); + } + public ConfidenceLevel GetConfiguredConfidenceLevel(LLMProviders llmProvider) { if(llmProvider is LLMProviders.NONE) diff --git a/app/MindWork AI Studio/Tools/HTMLParser.cs b/app/MindWork AI Studio/Tools/HTMLParser.cs index 4f9dca2a3..56aceee47 100644 --- a/app/MindWork AI Studio/Tools/HTMLParser.cs +++ b/app/MindWork AI Studio/Tools/HTMLParser.cs @@ -1,5 +1,6 @@ using System.Net; -using System.Text; +using System.Net.Http; +using System.Net.Http.Headers; using HtmlAgilityPack; @@ -9,6 +10,8 @@ namespace AIStudio.Tools; public sealed class HTMLParser { + private const string USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) MindWorkAIStudio/1.0"; + private static readonly Config MARKDOWN_PARSER_CONFIG = new() { UnknownTags = Config.UnknownTagsOption.Bypass, @@ -23,10 +26,8 @@ public sealed class HTMLParser /// The web content as text. public async Task LoadWebContentText(Uri url) { - var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30)); - var parser = new HtmlWeb(); - var doc = await parser.LoadFromWebAsync(url, Encoding.UTF8, new NetworkCredential(), cts.Token); - return doc.ParsedText; + var response = await this.LoadWebPageAsync(url); + return response.Document.ParsedText; } /// @@ -36,14 +37,66 @@ public async Task LoadWebContentText(Uri url) /// The web content as an HTML string. public async Task LoadWebContentHTML(Uri url) { - var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30)); - var parser = new HtmlWeb(); - var doc = await parser.LoadFromWebAsync(url, Encoding.UTF8, new NetworkCredential(), cts.Token); - var innerHtml = doc.DocumentNode.InnerHtml; + var response = await this.LoadWebPageAsync(url); + var innerHtml = response.Document.DocumentNode.InnerHtml; return innerHtml; } + public async Task LoadWebPageAsync(Uri url, CancellationToken token = default, int timeoutSeconds = 30) + { + using var handler = new HttpClientHandler + { + AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate | DecompressionMethods.Brotli, + }; + using var httpClient = new HttpClient(handler) + { + Timeout = Timeout.InfiniteTimeSpan, + }; + using var timeoutCts = CancellationTokenSource.CreateLinkedTokenSource(token); + timeoutCts.CancelAfter(TimeSpan.FromSeconds(timeoutSeconds)); + using var request = new HttpRequestMessage(HttpMethod.Get, url); + request.Headers.TryAddWithoutValidation("User-Agent", USER_AGENT); + request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/html")); + request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xhtml+xml")); + request.Headers.AcceptLanguage.Add(new StringWithQualityHeaderValue("en-US")); + request.Headers.AcceptLanguage.Add(new StringWithQualityHeaderValue("en", 0.9)); + request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip")); + request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate")); + request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("br")); + request.Headers.TryAddWithoutValidation("Upgrade-Insecure-Requests", "1"); + request.Headers.TryAddWithoutValidation("Sec-Fetch-Site", "none"); + request.Headers.TryAddWithoutValidation("Sec-Fetch-Mode", "navigate"); + request.Headers.TryAddWithoutValidation("Sec-Fetch-Dest", "document"); + request.Headers.TryAddWithoutValidation("Sec-Fetch-User", "?1"); + + using var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, timeoutCts.Token); + if (!response.IsSuccessStatusCode) + { + var statusCode = (int)response.StatusCode; + var reasonPhrase = string.IsNullOrWhiteSpace(response.ReasonPhrase) ? "Unknown" : response.ReasonPhrase; + throw new HttpRequestException($"The server returned HTTP {statusCode} ({reasonPhrase}) for '{url}'.", null, response.StatusCode); + } + + var html = await response.Content.ReadAsStringAsync(token); + var document = new HtmlDocument(); + document.LoadHtml(html); + + return new HTMLParserWebPage + { + RequestedUrl = url, + FinalUrl = response.RequestMessage?.RequestUri ?? url, + ContentType = response.Content.Headers.ContentType?.MediaType ?? string.Empty, + Document = document, + }; + } + + public string ExtractTitle(HtmlDocument document) + { + var title = document.DocumentNode.SelectSingleNode("//title")?.InnerText?.Trim(); + return WebUtility.HtmlDecode(title ?? string.Empty).Trim(); + } + /// /// Converts HTML content to the Markdown format. /// @@ -54,4 +107,15 @@ public string ParseToMarkdown(string html) var markdownConverter = new Converter(MARKDOWN_PARSER_CONFIG); return markdownConverter.Convert(html); } -} \ No newline at end of file +} + +public sealed class HTMLParserWebPage +{ + public required Uri RequestedUrl { get; init; } + + public required Uri FinalUrl { get; init; } + + public required string ContentType { get; init; } + + public required HtmlDocument Document { get; init; } +} diff --git a/app/MindWork AI Studio/Tools/ToolCallingSystem/IToolImplementation.cs b/app/MindWork AI Studio/Tools/ToolCallingSystem/IToolImplementation.cs new file mode 100644 index 000000000..9d0d86696 --- /dev/null +++ b/app/MindWork AI Studio/Tools/ToolCallingSystem/IToolImplementation.cs @@ -0,0 +1,35 @@ +using System.Text.Json; + +using AIStudio.Tools.PluginSystem; + +namespace AIStudio.Tools.ToolCallingSystem; + +public interface IToolImplementation +{ + public string ImplementationKey { get; } + + public string Icon => Icons.Material.Filled.Build; + + public IReadOnlySet SensitiveTraceArgumentNames { get; } + + public string GetDisplayName() => this.T("Tool"); + + public string GetDescription() => this.T("Tool description"); + + public string GetSettingsFieldLabel(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => + this.T(fieldDefinition.Title); + + public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => + this.T(fieldDefinition.Description); + + public Task ValidateConfigurationAsync( + ToolDefinition definition, + IReadOnlyDictionary settingsValues, + CancellationToken token = default) => Task.FromResult(null); + + public Task ExecuteAsync(JsonElement arguments, ToolExecutionContext context, CancellationToken token = default); + + public string FormatTraceResult(string rawResult) => rawResult; + + private string T(string fallbackEN) => I18N.I.T(fallbackEN, this.GetType().Namespace, this.GetType().Name); +} diff --git a/app/MindWork AI Studio/Tools/ToolCallingSystem/ToolCallingImplementations/GetCurrentWeatherTool.cs b/app/MindWork AI Studio/Tools/ToolCallingSystem/ToolCallingImplementations/GetCurrentWeatherTool.cs new file mode 100644 index 000000000..a767346e8 --- /dev/null +++ b/app/MindWork AI Studio/Tools/ToolCallingSystem/ToolCallingImplementations/GetCurrentWeatherTool.cs @@ -0,0 +1,44 @@ +using System.Text.Json; +using AIStudio.Tools.PluginSystem; + +namespace AIStudio.Tools.ToolCallingSystem.ToolCallingImplementations; + +public sealed class GetCurrentWeatherTool : IToolImplementation +{ + public string ImplementationKey => "get_current_weather"; + + public string Icon => Icons.Material.Filled.Cloud; + + public IReadOnlySet SensitiveTraceArgumentNames => new HashSet(StringComparer.Ordinal); + + public string GetDisplayName() => I18N.I.T("Current Weather", typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)); + + public string GetDescription() => I18N.I.T("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.", typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)); + + public string GetSettingsFieldLabel(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch + { + "demoLabel" => I18N.I.T("Demo Label", typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)), + _ => I18N.I.T(fieldDefinition.Title, typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)), + }; + + public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch + { + "demoLabel" => I18N.I.T("Required demo setting for validating tool settings in tests. It does not affect the weather result.", typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)), + _ => I18N.I.T(fieldDefinition.Description, typeof(GetCurrentWeatherTool).Namespace, nameof(GetCurrentWeatherTool)), + }; + + public Task 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.", + }); + } +} diff --git a/app/MindWork AI Studio/Tools/ToolCallingSystem/ToolCallingImplementations/ReadWebPageTool.cs b/app/MindWork AI Studio/Tools/ToolCallingSystem/ToolCallingImplementations/ReadWebPageTool.cs new file mode 100644 index 000000000..581cb8a6f --- /dev/null +++ b/app/MindWork AI Studio/Tools/ToolCallingSystem/ToolCallingImplementations/ReadWebPageTool.cs @@ -0,0 +1,222 @@ +using System.Text.Json; +using System.Text.Json.Nodes; +using AIStudio.Tools.PluginSystem; +using HtmlAgilityPack; + +namespace AIStudio.Tools.ToolCallingSystem.ToolCallingImplementations; + +public sealed class ReadWebPageTool(HTMLParser htmlParser) : IToolImplementation +{ + private const int DEFAULT_TIMEOUT_SECONDS = 30; + private const int DEFAULT_MAX_CONTENT_CHARACTERS = 12000; + private const int MAX_TRACE_LENGTH = 12000; + + private static readonly string[] REMOVED_NODE_XPATHS = + [ + "//script", + "//style", + "//noscript", + "//nav", + "//footer", + "//aside", + "//form", + "//iframe", + "//*[@role='navigation']", + "//*[@role='contentinfo']", + "//*[@role='complementary']" + ]; + + public string ImplementationKey => ToolSelectionRules.READ_WEB_PAGE_TOOL_ID; + + public string Icon => Icons.Material.Filled.Article; + + public IReadOnlySet SensitiveTraceArgumentNames => new HashSet(StringComparer.Ordinal); + + public string GetDisplayName() => I18N.I.T("Read Web Page", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)); + + public string GetDescription() => I18N.I.T("Load a single web page, extract its main HTML content, and return structured working material for the model. Use the result to synthesize a natural-language answer instead of exposing the raw payload to the user.", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)); + + public string GetSettingsFieldLabel(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch + { + "timeoutSeconds" => I18N.I.T("Timeout Seconds", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), + "maxContentCharacters" => I18N.I.T("Maximum Content Characters", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), + _ => I18N.I.T(fieldDefinition.Title, typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), + }; + + public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch + { + "timeoutSeconds" => I18N.I.T("Optional HTTP timeout for loading a web page in seconds.", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), + "maxContentCharacters" => I18N.I.T("Optional global truncation limit for extracted Markdown returned to the model.", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), + _ => I18N.I.T(fieldDefinition.Description, typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)), + }; + + public Task ValidateConfigurationAsync( + ToolDefinition definition, + IReadOnlyDictionary settingsValues, + CancellationToken token = default) + { + if (!TryReadOptionalPositiveInt(settingsValues, "timeoutSeconds", out _, out var timeoutError)) + { + return Task.FromResult(new ToolConfigurationState + { + IsConfigured = false, + Message = timeoutError, + }); + } + + if (!TryReadOptionalPositiveInt(settingsValues, "maxContentCharacters", out _, out var contentError)) + { + return Task.FromResult(new ToolConfigurationState + { + IsConfigured = false, + Message = contentError, + }); + } + + return Task.FromResult(null); + } + + public async Task ExecuteAsync(JsonElement arguments, ToolExecutionContext context, CancellationToken token = default) + { + var urlText = ReadRequiredString(arguments, "url"); + if (!Uri.TryCreate(urlText, UriKind.Absolute, out var url) || url is not { Scheme: "http" or "https" }) + throw new ArgumentException("Argument 'url' must be a valid HTTP or HTTPS URL."); + + var timeoutSeconds = ReadOptionalPositiveIntSetting(context.SettingsValues, "timeoutSeconds") ?? DEFAULT_TIMEOUT_SECONDS; + var maxContentCharacters = ReadOptionalPositiveIntSetting(context.SettingsValues, "maxContentCharacters") ?? DEFAULT_MAX_CONTENT_CHARACTERS; + + HTMLParserWebPage page; + try + { + page = await htmlParser.LoadWebPageAsync(url, token, timeoutSeconds); + } + catch (OperationCanceledException) when (!token.IsCancellationRequested) + { + throw new TimeoutException($"Loading the web page timed out after {timeoutSeconds} seconds."); + } + catch (HttpRequestException exception) + { + throw new InvalidOperationException($"Loading the web page failed: {exception.Message}", exception); + } + + if (!IsSupportedHtmlContentType(page.ContentType)) + throw new InvalidOperationException($"Unsupported content type '{page.ContentType}'. Only HTML pages are supported."); + + var document = page.Document; + var title = htmlParser.ExtractTitle(document); + var contentRoot = document.DocumentNode.SelectSingleNode("//article") ?? + document.DocumentNode.SelectSingleNode("//main") ?? + document.DocumentNode.SelectSingleNode("//body") ?? + document.DocumentNode; + + RemoveNoiseNodes(contentRoot); + + var markdown = htmlParser.ParseToMarkdown(contentRoot.InnerHtml).Trim(); + var warnings = new JsonArray(); + if (string.IsNullOrWhiteSpace(title)) + warnings.Add("No title could be extracted from the page."); + + if (string.IsNullOrWhiteSpace(markdown)) + warnings.Add("The extracted page content is empty."); + else if (markdown.Length < 200) + warnings.Add("The extracted page content is very short and may be incomplete."); + + if (markdown.Length > maxContentCharacters) + { + markdown = markdown[..maxContentCharacters].TrimEnd(); + warnings.Add($"The extracted page content was truncated to {maxContentCharacters} characters."); + } + + return new ToolExecutionResult + { + JsonContent = BuildResponseJson(page, title, markdown, warnings) + }; + } + + private static JsonObject BuildResponseJson(HTMLParserWebPage page, string title, string markdown, JsonArray warnings) + { + var response = new JsonObject + { + ["metadata"] = new JsonObject + { + ["url"] = page.RequestedUrl.ToString(), + ["final_url"] = page.FinalUrl.ToString(), + ["title"] = title, + }, + ["content_markdown"] = markdown, + }; + + if (warnings.Count > 0) + response["warnings"] = warnings; + + return response; + } + + public string FormatTraceResult(string rawResult) + { + if (rawResult.Length <= MAX_TRACE_LENGTH) + return rawResult; + + return $"{rawResult[..MAX_TRACE_LENGTH]}..."; + } + + private static void RemoveNoiseNodes(HtmlNode rootNode) + { + foreach (var xpath in REMOVED_NODE_XPATHS) + { + var nodes = rootNode.SelectNodes(xpath); + if (nodes is null) + continue; + + foreach (var node in nodes.ToList()) + node.Remove(); + } + } + + private static bool IsSupportedHtmlContentType(string? contentType) => + string.IsNullOrWhiteSpace(contentType) || + contentType.StartsWith("text/html", StringComparison.OrdinalIgnoreCase) || + contentType.StartsWith("application/xhtml+xml", StringComparison.OrdinalIgnoreCase); + + private static string ReadRequiredString(JsonElement arguments, string propertyName) + { + if (!arguments.TryGetProperty(propertyName, out var value) || value.ValueKind is not JsonValueKind.String) + throw new ArgumentException($"Missing required argument '{propertyName}'."); + + var text = value.GetString()?.Trim() ?? string.Empty; + if (string.IsNullOrWhiteSpace(text)) + throw new ArgumentException($"Missing required argument '{propertyName}'."); + + return text; + } + + private static int? ReadOptionalPositiveIntSetting(IReadOnlyDictionary 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; + } + + private static bool TryReadOptionalPositiveInt( + IReadOnlyDictionary settingsValues, + string key, + 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 = I18N.I.T($"The setting '{key}' must be a positive integer.", typeof(ReadWebPageTool).Namespace, nameof(ReadWebPageTool)); + return false; + } +} diff --git a/app/MindWork AI Studio/Tools/ToolCallingSystem/ToolCallingImplementations/SearXNGWebSearchTool.cs b/app/MindWork AI Studio/Tools/ToolCallingSystem/ToolCallingImplementations/SearXNGWebSearchTool.cs new file mode 100644 index 000000000..c3a13b903 --- /dev/null +++ b/app/MindWork AI Studio/Tools/ToolCallingSystem/ToolCallingImplementations/SearXNGWebSearchTool.cs @@ -0,0 +1,510 @@ +using System.Net; +using System.Text; +using System.Text.Json; +using System.Text.Json.Nodes; +using AIStudio.Tools.PluginSystem; + +namespace AIStudio.Tools.ToolCallingSystem.ToolCallingImplementations; + +public sealed class SearXNGWebSearchTool : IToolImplementation +{ + private const int DEFAULT_MAX_RESULTS = 5; + private const int DEFAULT_TIMEOUT_SECONDS = 20; + private const int MAX_TRACE_LENGTH = 4000; + + public string ImplementationKey => "web_search"; + + public string Icon => Icons.Material.Filled.Language; + + public IReadOnlySet SensitiveTraceArgumentNames => new HashSet(StringComparer.Ordinal); + + public string GetDisplayName() => I18N.I.T("Web Search", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)); + + public string GetDescription() => I18N.I.T("Search the web with a configured SearXNG instance and return candidate URLs for the model. Use Read Web Page on relevant result URLs before answering factual or detailed web questions.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)); + + public string GetSettingsFieldLabel(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch + { + "baseUrl" => I18N.I.T("SearXNG URL", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), + "defaultLanguage" => I18N.I.T("Default Language", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), + "defaultSafeSearch" => I18N.I.T("Default Safe Search", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), + "defaultCategories" => I18N.I.T("Default Categories", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), + "defaultEngines" => I18N.I.T("Default Engines", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), + "maxResults" => I18N.I.T("Maximum Results", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), + "timeoutSeconds" => I18N.I.T("Timeout Seconds", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), + _ => I18N.I.T(fieldDefinition.Title, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), + }; + + public string GetSettingsFieldDescription(string fieldName, ToolSettingsFieldDefinition fieldDefinition) => fieldName switch + { + "baseUrl" => I18N.I.T("Base URL of the SearXNG instance. You can enter either the instance root URL or the /search endpoint.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), + "defaultLanguage" => I18N.I.T("Optional fallback language code when the model does not provide a language.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), + "defaultSafeSearch" => I18N.I.T("Optional safe search policy sent to SearXNG when configured.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), + "defaultCategories" => I18N.I.T("Optional comma-separated default categories. Do not set this together with default engines.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), + "defaultEngines" => I18N.I.T("Optional comma-separated default engines. Do not set this together with default categories.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), + "maxResults" => I18N.I.T("Optional default maximum number of results returned to the model when the model does not provide a limit.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), + "timeoutSeconds" => I18N.I.T("Optional HTTP timeout for the search request in seconds.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), + _ => I18N.I.T(fieldDefinition.Description, typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), + }; + + public Task ValidateConfigurationAsync( + ToolDefinition definition, + IReadOnlyDictionary settingsValues, + CancellationToken token = default) + { + settingsValues.TryGetValue("baseUrl", out var baseUrl); + var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out _, out var uriError); + if (!isValidBaseUrl) + { + return Task.FromResult(new ToolConfigurationState + { + IsConfigured = false, + Message = uriError, + }); + } + + var hasDefaultCategories = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault("defaultCategories")); + var hasDefaultEngines = !string.IsNullOrWhiteSpace(settingsValues.GetValueOrDefault("defaultEngines")); + if (hasDefaultCategories && hasDefaultEngines) + { + return Task.FromResult(new ToolConfigurationState + { + IsConfigured = false, + Message = I18N.I.T("Default categories and default engines cannot both be set for the web search tool.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)), + }); + } + + if (!TryReadOptionalPositiveInt(settingsValues, "maxResults", out _, out var maxResultsError)) + { + return Task.FromResult(new ToolConfigurationState + { + IsConfigured = false, + Message = maxResultsError, + }); + } + + if (!TryReadOptionalPositiveInt(settingsValues, "timeoutSeconds", out _, out var timeoutError)) + { + return Task.FromResult(new ToolConfigurationState + { + IsConfigured = false, + Message = timeoutError, + }); + } + + return Task.FromResult(null); + } + + public async Task ExecuteAsync(JsonElement arguments, ToolExecutionContext context, CancellationToken token = default) + { + context.SettingsValues.TryGetValue("baseUrl", out var baseUrl); + var isValidBaseUrl = TryNormalizeSearchUri(baseUrl ?? string.Empty, out var searchUri, out var uriError); + if (!isValidBaseUrl) + throw new InvalidOperationException(uriError); + + var query = ReadRequiredString(arguments, "query"); + var categories = ReadOptionalStringArray(arguments, "categories"); + var engines = ReadOptionalStringArray(arguments, "engines"); + var language = ReadOptionalString(arguments, "language"); + var timeRange = ReadOptionalString(arguments, "time_range"); + var page = ReadOptionalPositiveInt(arguments, "page"); + var requestedLimit = ReadOptionalPositiveInt(arguments, "limit"); + + if (timeRange is not null && timeRange is not ("day" or "month" or "year")) + throw new ArgumentException($"Invalid time_range '{timeRange}'."); + + language = string.IsNullOrWhiteSpace(language) ? context.SettingsValues.GetValueOrDefault("defaultLanguage") : language; + var safeSearch = context.SettingsValues.GetValueOrDefault("defaultSafeSearch"); + + if (categories.Count == 0) + categories = SplitCommaSeparatedValues(context.SettingsValues.GetValueOrDefault("defaultCategories")); + + if (engines.Count == 0) + engines = SplitCommaSeparatedValues(context.SettingsValues.GetValueOrDefault("defaultEngines")); + + if (categories.Count > 0 && engines.Count > 0 && !string.IsNullOrWhiteSpace(context.SettingsValues.GetValueOrDefault("defaultCategories")) && !string.IsNullOrWhiteSpace(context.SettingsValues.GetValueOrDefault("defaultEngines"))) + throw new InvalidOperationException(I18N.I.T("Default categories and default engines cannot both be set for the web search tool.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool))); + + var defaultLimit = ReadOptionalPositiveIntSetting(context.SettingsValues, "maxResults") ?? DEFAULT_MAX_RESULTS; + var effectiveLimit = requestedLimit ?? defaultLimit; + var timeoutSeconds = ReadOptionalPositiveIntSetting(context.SettingsValues, "timeoutSeconds") ?? DEFAULT_TIMEOUT_SECONDS; + + var queryParameters = new List> + { + new("q", query), + new("format", "json"), + }; + + if (categories.Count > 0) + queryParameters.Add(new KeyValuePair("categories", string.Join(",", categories))); + + if (engines.Count > 0) + queryParameters.Add(new KeyValuePair("engines", string.Join(",", engines))); + + if (!string.IsNullOrWhiteSpace(language)) + queryParameters.Add(new KeyValuePair("language", language)); + + if (!string.IsNullOrWhiteSpace(timeRange)) + queryParameters.Add(new KeyValuePair("time_range", timeRange)); + + if (page is not null) + queryParameters.Add(new KeyValuePair("pageno", page.Value.ToString())); + + if (!string.IsNullOrWhiteSpace(safeSearch)) + queryParameters.Add(new KeyValuePair("safesearch", safeSearch)); + + using var httpClient = new HttpClient + { + Timeout = Timeout.InfiniteTimeSpan, + }; + using var request = new HttpRequestMessage(HttpMethod.Get, BuildRequestUri(searchUri, queryParameters)); + using var timeoutCts = CancellationTokenSource.CreateLinkedTokenSource(token); + timeoutCts.CancelAfter(TimeSpan.FromSeconds(timeoutSeconds)); + + using var response = await SendAsync(httpClient, request, timeoutCts.Token, timeoutSeconds, token); + var responseBody = await response.Content.ReadAsStringAsync(token); + if (!response.IsSuccessStatusCode) + { + var responseDetails = string.IsNullOrWhiteSpace(responseBody) ? string.Empty : $" Response body: {responseBody[..Math.Min(responseBody.Length, 400)]}"; + throw new InvalidOperationException($"The SearXNG request failed with status code {(int)response.StatusCode} ({response.StatusCode}).{responseDetails}"); + } + + JsonNode? responseJson; + try + { + responseJson = JsonNode.Parse(responseBody); + } + catch (JsonException exception) + { + throw new InvalidOperationException($"The SearXNG response was not valid JSON: {exception.Message}", exception); + } + + if (responseJson is not JsonObject responseObject) + throw new InvalidOperationException("The SearXNG response JSON must be an object."); + + responseObject = SanitizeResponse(responseObject, effectiveLimit); + + var requestJson = new JsonObject + { + ["query"] = query, + ["format"] = "json", + ["limit"] = effectiveLimit, + }; + + if (categories.Count > 0) + requestJson["categories"] = BuildJsonArray(categories); + + if (engines.Count > 0) + requestJson["engines"] = BuildJsonArray(engines); + + if (!string.IsNullOrWhiteSpace(language)) + requestJson["language"] = language; + + if (!string.IsNullOrWhiteSpace(timeRange)) + requestJson["time_range"] = timeRange; + + if (page is not null) + requestJson["page"] = page.Value; + + if (!string.IsNullOrWhiteSpace(safeSearch)) + requestJson["safesearch"] = safeSearch; + + return new ToolExecutionResult + { + JsonContent = new JsonObject + { + ["request"] = requestJson, + ["response"] = responseObject, + }, + }; + } + + public string FormatTraceResult(string rawResult) + { + if (rawResult.Length <= MAX_TRACE_LENGTH) + return rawResult; + + return $"{rawResult[..MAX_TRACE_LENGTH]}..."; + } + + private static string ReadRequiredString(JsonElement arguments, string propertyName) + { + var value = ReadOptionalString(arguments, propertyName); + if (string.IsNullOrWhiteSpace(value)) + throw new ArgumentException($"Missing required argument '{propertyName}'."); + + return value; + } + + private static string? ReadOptionalString(JsonElement arguments, string propertyName) + { + if (!arguments.TryGetProperty(propertyName, out var value)) + return null; + + return value.ValueKind switch + { + JsonValueKind.Null => null, + JsonValueKind.String => value.GetString()?.Trim(), + _ => throw new ArgumentException($"Argument '{propertyName}' must be a string."), + }; + } + + private static int? ReadOptionalPositiveInt(JsonElement arguments, string propertyName) + { + if (!arguments.TryGetProperty(propertyName, out var value)) + return null; + + if (value.ValueKind is JsonValueKind.Null) + return null; + + if (value.ValueKind is not JsonValueKind.Number || !value.TryGetInt32(out var intValue) || intValue <= 0) + throw new ArgumentException($"Argument '{propertyName}' must be a positive integer."); + + return intValue; + } + + private static List ReadOptionalStringArray(JsonElement arguments, string propertyName) + { + if (!arguments.TryGetProperty(propertyName, out var value) || value.ValueKind is JsonValueKind.Null) + return []; + + if (value.ValueKind is not JsonValueKind.Array) + throw new ArgumentException($"Argument '{propertyName}' must be an array of strings."); + + var values = new List(); + foreach (var element in value.EnumerateArray()) + { + if (element.ValueKind is not JsonValueKind.String) + throw new ArgumentException($"Argument '{propertyName}' must be an array of strings."); + + var item = element.GetString()?.Trim(); + if (!string.IsNullOrWhiteSpace(item)) + values.Add(item); + } + + return values; + } + + private static JsonArray BuildJsonArray(IEnumerable values) + { + var array = new JsonArray(); + foreach (var value in values) + array.Add(value); + + return array; + } + + private static JsonObject SanitizeResponse(JsonObject responseObject, int effectiveLimit) + { + var sanitizedResponse = new JsonObject(); + + var resultArray = responseObject["results"] as JsonArray; + var sanitizedResults = BuildSanitizedResults(resultArray, effectiveLimit); + sanitizedResponse["results"] = sanitizedResults; + + var suggestions = BuildSuggestions(responseObject["suggestions"] as JsonArray); + if (suggestions.Count > 0) + sanitizedResponse["suggestions"] = suggestions; + + return sanitizedResponse; + } + + private static JsonArray BuildSanitizedResults(JsonArray? resultArray, int effectiveLimit) + { + var sanitizedResults = new JsonArray(); + if (resultArray is null) + return sanitizedResults; + + var resultObjects = resultArray.OfType().ToList(); + var hasSortableScores = resultObjects.Any(result => TryGetScore(result, out _)); + IEnumerable orderedResults = hasSortableScores + ? resultObjects + .OrderByDescending(result => TryGetScore(result, out var score) ? score : double.MinValue) + .ThenBy(result => result["title"]?.ToString(), StringComparer.OrdinalIgnoreCase) + : resultObjects; + + foreach (var result in orderedResults.Take(effectiveLimit)) + sanitizedResults.Add(SanitizeResult(result)); + + return sanitizedResults; + } + + private static JsonObject SanitizeResult(JsonObject result) + { + var sanitizedResult = new JsonObject(); + CopyPropertyIfPresent(result, sanitizedResult, "title"); + CopyPropertyIfPresent(result, sanitizedResult, "url"); + CopyPropertyIfPresent(result, sanitizedResult, "content"); + CopyPropertyIfPresent(result, sanitizedResult, "score"); + CopyPropertyIfPresent(result, sanitizedResult, "engine"); + CopyPropertyIfPresent(result, sanitizedResult, "category"); + CopyPropertyIfPresent(result, sanitizedResult, "publishedDate"); + CopyPropertyIfPresent(result, sanitizedResult, "published_date"); + + return sanitizedResult; + } + + private static JsonArray BuildSuggestions(JsonArray? suggestionsArray) + { + var suggestions = new JsonArray(); + if (suggestionsArray is null) + return suggestions; + + foreach (var suggestionNode in suggestionsArray.Take(3)) + { + var suggestion = suggestionNode switch + { + JsonValue value => value.TryGetValue(out var stringSuggestion) ? stringSuggestion : null, + JsonObject suggestionObject when suggestionObject.TryGetPropertyValue("suggestion", out var suggestionValue) => suggestionValue?.ToString(), + JsonObject suggestionObject when suggestionObject.TryGetPropertyValue("title", out var titleValue) => titleValue?.ToString(), + _ => suggestionNode?.ToString(), + }; + + if (!string.IsNullOrWhiteSpace(suggestion)) + suggestions.Add(suggestion); + } + + return suggestions; + } + + private static void CopyPropertyIfPresent(JsonObject source, JsonObject target, string propertyName) + { + if (source.TryGetPropertyValue(propertyName, out var propertyValue) && propertyValue is not null) + target[propertyName] = propertyValue.DeepClone(); + } + + private static bool TryGetScore(JsonObject result, out double score) + { + score = double.MinValue; + if (!result.TryGetPropertyValue("score", out var scoreNode) || scoreNode is null) + return false; + + return scoreNode switch + { + JsonValue value when value.TryGetValue(out var doubleScore) => ReturnScore(doubleScore, out score), + JsonValue value when value.TryGetValue(out var decimalScore) => ReturnScore((double)decimalScore, out score), + JsonValue value when value.TryGetValue(out var intScore) => ReturnScore(intScore, out score), + _ => double.TryParse(scoreNode.ToString(), out var parsedScore) && ReturnScore(parsedScore, out score), + }; + } + + private static bool ReturnScore(double input, out double score) + { + score = input; + return true; + } + + private static List SplitCommaSeparatedValues(string? value) => value? + .Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) + .Where(x => !string.IsNullOrWhiteSpace(x)) + .Distinct(StringComparer.Ordinal) + .ToList() ?? []; + + private static int? ReadOptionalPositiveIntSetting(IReadOnlyDictionary 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; + } + + private static bool TryReadOptionalPositiveInt( + IReadOnlyDictionary settingsValues, + string key, + 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 = I18N.I.T($"The setting '{key}' must be a positive integer.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)); + return false; + } + + private static bool TryNormalizeSearchUri(string rawUrl, out Uri searchUri, out string error) + { + searchUri = null!; + error = string.Empty; + + if (string.IsNullOrWhiteSpace(rawUrl)) + { + error = I18N.I.T("A SearXNG URL is required.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)); + return false; + } + + if (!Uri.TryCreate(rawUrl.Trim(), UriKind.Absolute, out var parsedUri)) + { + error = I18N.I.T("The configured SearXNG URL is not a valid absolute URL.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)); + return false; + } + + if (parsedUri.Scheme is not ("http" or "https")) + { + error = I18N.I.T("The configured SearXNG URL must start with http:// or https://.", typeof(SearXNGWebSearchTool).Namespace, nameof(SearXNGWebSearchTool)); + return false; + } + + var basePath = parsedUri.AbsolutePath.TrimEnd('/'); + if (basePath.EndsWith("/search", StringComparison.OrdinalIgnoreCase)) + basePath = basePath[..^"/search".Length]; + + var normalizedPath = $"{basePath}/search"; + var builder = new UriBuilder(parsedUri) + { + Path = normalizedPath, + Query = string.Empty, + Fragment = string.Empty, + }; + searchUri = builder.Uri; + return true; + } + + private static Uri BuildRequestUri(Uri searchUri, IEnumerable> queryParameters) + { + var builder = new StringBuilder(); + foreach (var parameter in queryParameters) + { + if (builder.Length > 0) + builder.Append('&'); + + builder.Append(WebUtility.UrlEncode(parameter.Key)); + builder.Append('='); + builder.Append(WebUtility.UrlEncode(parameter.Value)); + } + + var uriBuilder = new UriBuilder(searchUri) + { + Query = builder.ToString(), + }; + return uriBuilder.Uri; + } + + private static async Task SendAsync( + HttpClient httpClient, + HttpRequestMessage request, + CancellationToken requestToken, + int timeoutSeconds, + CancellationToken callerToken) + { + try + { + return await httpClient.SendAsync(request, requestToken); + } + catch (OperationCanceledException) when (!callerToken.IsCancellationRequested) + { + throw new TimeoutException($"The SearXNG request timed out after {timeoutSeconds} seconds."); + } + catch (Exception exception) + { + throw new InvalidOperationException($"The SearXNG request failed: {exception.Message}", exception); + } + } +} diff --git a/app/MindWork AI Studio/Tools/ToolCallingSystem/ToolDefinition.cs b/app/MindWork AI Studio/Tools/ToolCallingSystem/ToolDefinition.cs new file mode 100644 index 000000000..126f9e9f3 --- /dev/null +++ b/app/MindWork AI Studio/Tools/ToolCallingSystem/ToolDefinition.cs @@ -0,0 +1,64 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace AIStudio.Tools.ToolCallingSystem; + +public sealed class ToolDefinition +{ + public int SchemaVersion { get; init; } = 1; + + public string Id { get; init; } = string.Empty; + + public string DisplayName { get; init; } = string.Empty; + + public string Icon { get; init; } = Icons.Material.Filled.Build; + + public string ImplementationKey { get; init; } = string.Empty; + + public ToolVisibilityDefinition VisibleIn { get; init; } = new(); + + public ToolSettingsSchema SettingsSchema { get; init; } = new(); + + public ToolFunctionDefinition Function { get; init; } = new(); +} + +public sealed class ToolVisibilityDefinition +{ + public bool Chat { get; init; } = true; + + public bool Assistants { get; init; } = true; +} + +public sealed class ToolFunctionDefinition +{ + public string Name { get; init; } = string.Empty; + + public string Description { get; init; } = string.Empty; + + public bool Strict { get; init; } = true; + + public JsonElement Parameters { get; init; } +} + +public sealed class ToolSettingsSchema +{ + public string Type { get; init; } = "object"; + + public Dictionary Properties { get; init; } = []; + + public HashSet Required { get; init; } = []; +} + +public sealed class ToolSettingsFieldDefinition +{ + public string Type { get; init; } = "string"; + + public string Title { get; init; } = string.Empty; + + public string Description { get; init; } = string.Empty; + + [JsonPropertyName("enum")] + public List EnumValues { get; init; } = []; + + public bool Secret { get; init; } +} diff --git a/app/MindWork AI Studio/Tools/ToolCallingSystem/ToolExecutionModels.cs b/app/MindWork AI Studio/Tools/ToolCallingSystem/ToolExecutionModels.cs new file mode 100644 index 000000000..f90cf5389 --- /dev/null +++ b/app/MindWork AI Studio/Tools/ToolCallingSystem/ToolExecutionModels.cs @@ -0,0 +1,98 @@ +using System.Text.Json; +using System.Text.Json.Nodes; + +using AIStudio.Settings; + +namespace AIStudio.Tools.ToolCallingSystem; + +public sealed class ToolExecutionContext +{ + public required ToolDefinition Definition { get; init; } + + public required SettingsManager SettingsManager { get; init; } + + public required IReadOnlyDictionary SettingsValues { get; init; } +} + +public sealed class ToolExecutionResult +{ + public string? TextContent { get; init; } + + public JsonNode? JsonContent { get; init; } + + public string ToModelContent() + { + if (this.JsonContent is not null) + return this.JsonContent.ToJsonString(); + + return this.TextContent ?? string.Empty; + } +} + +public enum ToolInvocationTraceStatus +{ + NONE = 0, + SUCCESS, + ERROR, + BLOCKED, +} + +public sealed class ToolInvocationTrace +{ + public int Order { get; set; } + + public string ToolId { get; set; } = string.Empty; + + public string ToolName { get; set; } = string.Empty; + + public string ToolIcon { get; set; } = Icons.Material.Filled.Build; + + public string ToolCallId { get; set; } = string.Empty; + + public ToolInvocationTraceStatus Status { get; set; } = ToolInvocationTraceStatus.NONE; + + public bool WasExecuted { get; set; } + + public string StatusMessage { get; set; } = string.Empty; + + public Dictionary Arguments { get; set; } = []; + + public string Result { get; set; } = string.Empty; +} + +public sealed class ToolRuntimeStatus +{ + public bool IsRunning { get; set; } + + public List ToolNames { get; set; } = []; + + public string Message => this.ToolNames.Count switch + { + 0 => string.Empty, + 1 => $"Using tool: {this.ToolNames[0]}", + _ => $"Using tools: {string.Join(", ", this.ToolNames)}", + }; +} + +public sealed class ToolConfigurationState +{ + public bool IsConfigured { get; init; } + + public List MissingRequiredFields { get; init; } = []; + + public string Message { get; init; } = string.Empty; +} + +public sealed class ToolCatalogItem +{ + public required ToolDefinition Definition { get; init; } + + public required IToolImplementation Implementation { get; init; } + + public required ToolConfigurationState ConfigurationState { get; init; } +} + +public sealed class ToolSelectionState +{ + public HashSet SelectedToolIds { get; init; } = []; +} diff --git a/app/MindWork AI Studio/Tools/ToolCallingSystem/ToolExecutor.cs b/app/MindWork AI Studio/Tools/ToolCallingSystem/ToolExecutor.cs new file mode 100644 index 000000000..4c8dd2f00 --- /dev/null +++ b/app/MindWork AI Studio/Tools/ToolCallingSystem/ToolExecutor.cs @@ -0,0 +1,107 @@ +using System.Text.Json; + +using Microsoft.Extensions.DependencyInjection; + +namespace AIStudio.Tools.ToolCallingSystem; + +public sealed class ToolExecutor(ToolSettingsService toolSettingsService) +{ + public async Task<(string Content, ToolInvocationTrace Trace)> ExecuteAsync( + string toolCallId, + string toolName, + string argumentsJson, + IReadOnlyList<(ToolDefinition Definition, IToolImplementation Implementation)> runnableTools, + int order, + CancellationToken token = default) + { + var runnableTool = runnableTools.FirstOrDefault(x => x.Definition.Function.Name.Equals(toolName, StringComparison.Ordinal)); + if (runnableTool.Definition is null || runnableTool.Implementation is null) + { + return (this.CreateError(toolName), new ToolInvocationTrace + { + Order = order, + ToolId = toolName, + ToolName = toolName, + ToolCallId = toolCallId, + Status = ToolInvocationTraceStatus.BLOCKED, + StatusMessage = "Tool is not available in the current context.", + Result = this.CreateError(toolName), + }); + } + + var definition = runnableTool.Definition; + var implementation = runnableTool.Implementation; + try + { + using var document = JsonDocument.Parse(string.IsNullOrWhiteSpace(argumentsJson) ? "{}" : argumentsJson); + var settingsValues = await toolSettingsService.GetSettingsAsync(definition); + var result = await implementation.ExecuteAsync(document.RootElement, new ToolExecutionContext + { + Definition = definition, + SettingsManager = Program.SERVICE_PROVIDER.GetRequiredService(), + SettingsValues = settingsValues, + }, token); + + return (result.ToModelContent(), new ToolInvocationTrace + { + Order = order, + ToolId = definition.Id, + ToolName = implementation.GetDisplayName(), + ToolIcon = implementation.Icon, + ToolCallId = toolCallId, + Status = ToolInvocationTraceStatus.SUCCESS, + WasExecuted = true, + Arguments = FormatArguments(document.RootElement, implementation.SensitiveTraceArgumentNames), + Result = implementation.FormatTraceResult(result.ToModelContent()), + }); + } + catch (Exception exception) + { + var error = $"Tool execution failed: {exception.Message}"; + Dictionary formattedArguments = []; + try + { + using var document = JsonDocument.Parse(string.IsNullOrWhiteSpace(argumentsJson) ? "{}" : argumentsJson); + formattedArguments = FormatArguments(document.RootElement, implementation.SensitiveTraceArgumentNames); + } + catch + { + } + + return (error, new ToolInvocationTrace + { + Order = order, + ToolId = definition.Id, + ToolName = implementation.GetDisplayName(), + ToolIcon = implementation.Icon, + ToolCallId = toolCallId, + Status = ToolInvocationTraceStatus.ERROR, + StatusMessage = error, + Arguments = formattedArguments, + Result = error, + }); + } + } + + private string CreateError(string toolName) => $"Tool '{toolName}' is not available."; + + private static Dictionary FormatArguments(JsonElement rootElement, IReadOnlySet sensitiveNames) + { + if (rootElement.ValueKind is not JsonValueKind.Object) + return []; + + var arguments = new Dictionary(StringComparer.Ordinal); + foreach (var property in rootElement.EnumerateObject()) + { + arguments[property.Name] = sensitiveNames.Contains(property.Name) + ? "*****" + : property.Value.ValueKind switch + { + JsonValueKind.String => property.Value.GetString() ?? string.Empty, + _ => property.Value.ToString(), + }; + } + + return arguments; + } +} diff --git a/app/MindWork AI Studio/Tools/ToolCallingSystem/ToolRegistry.cs b/app/MindWork AI Studio/Tools/ToolCallingSystem/ToolRegistry.cs new file mode 100644 index 000000000..ea4732d21 --- /dev/null +++ b/app/MindWork AI Studio/Tools/ToolCallingSystem/ToolRegistry.cs @@ -0,0 +1,139 @@ +using System.Text.Json; + +using AIStudio.Provider; + +using Microsoft.AspNetCore.Hosting; + +namespace AIStudio.Tools.ToolCallingSystem; + +public sealed class ToolRegistry +{ + private readonly ILogger logger; + private readonly ToolSettingsService toolSettingsService; + private readonly Dictionary definitionsById = new(StringComparer.Ordinal); + private readonly Dictionary implementationsByKey = new(StringComparer.Ordinal); + + public ToolRegistry( + IWebHostEnvironment webHostEnvironment, + IEnumerable implementations, + ToolSettingsService toolSettingsService, + ILogger logger) + { + this.logger = logger; + this.toolSettingsService = toolSettingsService; + + foreach (var implementation in implementations) + this.implementationsByKey[implementation.ImplementationKey] = implementation; + + var definitionsDirectory = webHostEnvironment.WebRootFileProvider.GetDirectoryContents("tool_definitions"); + if (!definitionsDirectory.Exists) + { + this.logger.LogWarning("The tool definitions directory was not found."); + return; + } + + var serializerOptions = new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true, + }; + + foreach (var file in definitionsDirectory.Where(x => !x.IsDirectory && x.Name.EndsWith(".json", StringComparison.OrdinalIgnoreCase))) + { + try + { + using var stream = file.CreateReadStream(); + var definition = JsonSerializer.Deserialize(stream, serializerOptions); + if (definition is null || string.IsNullOrWhiteSpace(definition.Id)) + { + this.logger.LogWarning("Skipping tool definition '{ToolFile}' because it could not be deserialized.", file.Name); + continue; + } + + if (!this.implementationsByKey.ContainsKey(definition.ImplementationKey)) + { + this.logger.LogWarning("Skipping tool definition '{ToolId}' because implementation key '{ImplementationKey}' is not registered.", definition.Id, definition.ImplementationKey); + continue; + } + + this.definitionsById[definition.Id] = definition; + } + catch (Exception exception) + { + this.logger.LogWarning(exception, "Skipping invalid tool definition file '{ToolFile}'.", file.Name); + } + } + } + + public IReadOnlyList GetDefinitionsForComponent(AIStudio.Tools.Components component) + { + var isChat = component is AIStudio.Tools.Components.CHAT; + return this.definitionsById.Values + .Where(x => isChat ? x.VisibleIn.Chat : x.VisibleIn.Assistants) + .OrderBy(x => this.implementationsByKey.GetValueOrDefault(x.ImplementationKey)?.GetDisplayName(), StringComparer.OrdinalIgnoreCase) + .ToList(); + } + + public IReadOnlyList GetAllDefinitions() => this.definitionsById.Values + .OrderBy(x => this.implementationsByKey.GetValueOrDefault(x.ImplementationKey)?.GetDisplayName(), StringComparer.OrdinalIgnoreCase) + .ToList(); + + public ToolDefinition? GetDefinition(string toolId) => this.definitionsById.GetValueOrDefault(toolId); + + public IToolImplementation? GetImplementation(string implementationKey) => this.implementationsByKey.GetValueOrDefault(implementationKey); + + public async Task> GetCatalogAsync(AIStudio.Tools.Components component) + { + var definitions = this.GetDefinitionsForComponent(component); + return await this.GetCatalogAsync(definitions); + } + + public async Task> GetCatalogAsync(IEnumerable definitions) + { + var definitionList = definitions.ToList(); + var items = new List(definitionList.Count); + foreach (var definition in definitionList) + { + if (!this.implementationsByKey.TryGetValue(definition.ImplementationKey, out var implementation)) + continue; + + items.Add(new ToolCatalogItem + { + Definition = definition, + Implementation = implementation, + ConfigurationState = await this.toolSettingsService.GetConfigurationStateAsync(definition, implementation), + }); + } + + return items; + } + + public async Task> GetRunnableToolsAsync( + AIStudio.Tools.Components component, + IEnumerable selectedToolIds, + IReadOnlyCollection modelCapabilities, + bool isToolSelectionVisible) + { + if (!isToolSelectionVisible) + return []; + + if (!modelCapabilities.Contains(Capability.CHAT_COMPLETION_API) || !modelCapabilities.Contains(Capability.FUNCTION_CALLING)) + return []; + + var selectedToolIdSet = ToolSelectionRules.NormalizeSelection(selectedToolIds); + var definitions = this.GetDefinitionsForComponent(component).Where(x => selectedToolIdSet.Contains(x.Id)).ToList(); + var result = new List<(ToolDefinition, IToolImplementation)>(definitions.Count); + foreach (var definition in definitions) + { + if (!this.implementationsByKey.TryGetValue(definition.ImplementationKey, out var implementation)) + continue; + + var configurationState = await this.toolSettingsService.GetConfigurationStateAsync(definition, implementation); + if (!configurationState.IsConfigured) + continue; + + result.Add((definition, implementation)); + } + + return result; + } +} diff --git a/app/MindWork AI Studio/Tools/ToolCallingSystem/ToolSelectionRules.cs b/app/MindWork AI Studio/Tools/ToolCallingSystem/ToolSelectionRules.cs new file mode 100644 index 000000000..fc5b9d394 --- /dev/null +++ b/app/MindWork AI Studio/Tools/ToolCallingSystem/ToolSelectionRules.cs @@ -0,0 +1,25 @@ +using System.Collections.Generic; +using System.Linq; + +namespace AIStudio.Tools.ToolCallingSystem; + +public static class ToolSelectionRules +{ + public const string WEB_SEARCH_TOOL_ID = "web_search"; + public const string READ_WEB_PAGE_TOOL_ID = "read_web_page"; + + public static HashSet NormalizeSelection(IEnumerable selectedToolIds) + { + var normalized = selectedToolIds.ToHashSet(StringComparer.Ordinal); + if (normalized.Contains(WEB_SEARCH_TOOL_ID)) + normalized.Add(READ_WEB_PAGE_TOOL_ID); + + return normalized; + } + + public static bool IsRequiredBySelectedTools(string toolId, IEnumerable selectedToolIds) + { + var normalized = NormalizeSelection(selectedToolIds); + return toolId == READ_WEB_PAGE_TOOL_ID && normalized.Contains(WEB_SEARCH_TOOL_ID); + } +} diff --git a/app/MindWork AI Studio/Tools/ToolCallingSystem/ToolSettingsSecretId.cs b/app/MindWork AI Studio/Tools/ToolCallingSystem/ToolSettingsSecretId.cs new file mode 100644 index 000000000..25b3c687e --- /dev/null +++ b/app/MindWork AI Studio/Tools/ToolCallingSystem/ToolSettingsSecretId.cs @@ -0,0 +1,10 @@ +using AIStudio.Tools; + +namespace AIStudio.Tools.ToolCallingSystem; + +internal sealed record ToolSettingsSecretId(string ToolId, string FieldName) : ISecretId +{ + public string SecretId => $"tool::{this.ToolId}"; + + public string SecretName => this.FieldName; +} diff --git a/app/MindWork AI Studio/Tools/ToolCallingSystem/ToolSettingsService.cs b/app/MindWork AI Studio/Tools/ToolCallingSystem/ToolSettingsService.cs new file mode 100644 index 000000000..fa1b592f3 --- /dev/null +++ b/app/MindWork AI Studio/Tools/ToolCallingSystem/ToolSettingsService.cs @@ -0,0 +1,99 @@ +using AIStudio.Settings; +using AIStudio.Tools.Services; + +namespace AIStudio.Tools.ToolCallingSystem; + +public sealed class ToolSettingsService(SettingsManager settingsManager, RustService rustService) +{ + public async Task> GetSettingsAsync(ToolDefinition definition) + { + var values = new Dictionary(StringComparer.Ordinal); + var storedValues = settingsManager.ConfigurationData.Tools.Settings.GetValueOrDefault(definition.Id); + foreach (var property in definition.SettingsSchema.Properties) + { + var fieldName = property.Key; + var fieldDefinition = property.Value; + if (fieldDefinition.Secret) + { + var response = await rustService.GetSecret(new ToolSettingsSecretId(definition.Id, fieldName), isTrying: true); + if (response.Success) + values[fieldName] = await response.Secret.Decrypt(Program.ENCRYPTION); + + continue; + } + + if (storedValues?.TryGetValue(fieldName, out var storedValue) is true) + values[fieldName] = storedValue; + } + + return values; + } + + public async Task GetConfigurationStateAsync( + ToolDefinition definition, + IToolImplementation? implementation = null, + CancellationToken token = default) + { + var values = await this.GetSettingsAsync(definition); + var missing = new List(); + foreach (var requiredField in definition.SettingsSchema.Required) + { + if (!values.TryGetValue(requiredField, out var value) || string.IsNullOrWhiteSpace(value)) + missing.Add(requiredField); + } + + if (missing.Count > 0) + { + return new ToolConfigurationState + { + IsConfigured = false, + MissingRequiredFields = missing, + }; + } + + if (implementation is not null) + { + var validationState = await implementation.ValidateConfigurationAsync(definition, values, token); + if (validationState is not null && !validationState.IsConfigured) + return validationState; + } + + return new ToolConfigurationState + { + IsConfigured = true, + }; + } + + public async Task SaveSettingsAsync(ToolDefinition definition, IReadOnlyDictionary values) + { + if (!settingsManager.ConfigurationData.Tools.Settings.TryGetValue(definition.Id, out var storedValues)) + { + storedValues = new Dictionary(StringComparer.Ordinal); + settingsManager.ConfigurationData.Tools.Settings[definition.Id] = storedValues; + } + + foreach (var property in definition.SettingsSchema.Properties) + { + var fieldName = property.Key; + var fieldDefinition = property.Value; + values.TryGetValue(fieldName, out var value); + value ??= string.Empty; + + if (fieldDefinition.Secret) + { + var secretId = new ToolSettingsSecretId(definition.Id, fieldName); + if (string.IsNullOrWhiteSpace(value)) + await rustService.DeleteSecret(secretId); + else + await rustService.SetSecret(secretId, value); + + continue; + } + + storedValues[fieldName] = value; + } + + await settingsManager.StoreSettings(); + await MessageBus.INSTANCE.SendMessage(null, Event.CONFIGURATION_CHANGED, null); + } +} diff --git a/app/MindWork AI Studio/wwwroot/tool_definitions/get_current_weather.json b/app/MindWork AI Studio/wwwroot/tool_definitions/get_current_weather.json new file mode 100644 index 000000000..47b93580e --- /dev/null +++ b/app/MindWork AI Studio/wwwroot/tool_definitions/get_current_weather.json @@ -0,0 +1,53 @@ +{ + "schemaVersion": 1, + "id": "get_current_weather", + "implementationKey": "get_current_weather", + "visibleIn": { + "chat": true, + "assistants": true + }, + "settingsSchema": { + "type": "object", + "properties": { + "demoLabel": { + "type": "string", + "secret": false + } + }, + "required": [ + "demoLabel" + ] + }, + "function": { + "name": "get_current_weather", + "description": "Get the current weather in a given location.", + "strict": true, + "parameters": { + "type": "object", + "properties": { + "city": { + "type": "string", + "description": "The city to find the weather for, e.g. 'San Francisco'." + }, + "state": { + "type": "string", + "description": "The two-letter abbreviation for the state, e.g. 'CA'." + }, + "unit": { + "type": "string", + "description": "The unit to fetch the temperature in.", + "enum": [ + "celsius", + "fahrenheit" + ] + } + }, + "required": [ + "city", + "state", + "unit" + ], + "additionalProperties": false + } + } +} diff --git a/app/MindWork AI Studio/wwwroot/tool_definitions/read_web_page.json b/app/MindWork AI Studio/wwwroot/tool_definitions/read_web_page.json new file mode 100644 index 000000000..e57d82bef --- /dev/null +++ b/app/MindWork AI Studio/wwwroot/tool_definitions/read_web_page.json @@ -0,0 +1,41 @@ +{ + "schemaVersion": 1, + "id": "read_web_page", + "implementationKey": "read_web_page", + "visibleIn": { + "chat": true, + "assistants": true + }, + "settingsSchema": { + "type": "object", + "properties": { + "timeoutSeconds": { + "type": "string", + "secret": false + }, + "maxContentCharacters": { + "type": "string", + "secret": false + } + }, + "required": [] + }, + "function": { + "name": "read_web_page", + "description": "Load a single HTTP or HTTPS web page, extract its main content as structured working material for the model, and use it to synthesize a natural-language answer for the user.", + "strict": true, + "parameters": { + "type": "object", + "properties": { + "url": { + "type": "string", + "description": "The full HTTP or HTTPS URL of the web page to read." + } + }, + "required": [ + "url" + ], + "additionalProperties": false + } + } +} diff --git a/app/MindWork AI Studio/wwwroot/tool_definitions/web_search.json b/app/MindWork AI Studio/wwwroot/tool_definitions/web_search.json new file mode 100644 index 000000000..e74e32464 --- /dev/null +++ b/app/MindWork AI Studio/wwwroot/tool_definitions/web_search.json @@ -0,0 +1,103 @@ +{ + "schemaVersion": 1, + "id": "web_search", + "implementationKey": "web_search", + "visibleIn": { + "chat": true, + "assistants": true + }, + "settingsSchema": { + "type": "object", + "properties": { + "baseUrl": { + "type": "string", + "secret": false + }, + "defaultLanguage": { + "type": "string", + "secret": false + }, + "defaultSafeSearch": { + "type": "string", + "enum": [ + "0", + "1", + "2" + ], + "secret": false + }, + "defaultCategories": { + "type": "string", + "secret": false + }, + "defaultEngines": { + "type": "string", + "secret": false + }, + "maxResults": { + "type": "string", + "secret": false + }, + "timeoutSeconds": { + "type": "string", + "secret": false + } + }, + "required": [ + "baseUrl" + ] + }, + "function": { + "name": "web_search", + "description": "Search the web via a configured SearXNG instance and return candidate result URLs. Prefer categories for broad search intent. Use engines only when the user explicitly asks for specific search engines. Do not answer detailed or factual web questions from search results alone when read_web_page is available. Use read_web_page on relevant URLs from response.results before answering with page-level facts or summaries.", + "strict": true, + "parameters": { + "type": "object", + "properties": { + "query": { + "type": "string", + "description": "The search query." + }, + "categories": { + "type": "array", + "description": "Optional list of SearXNG categories to use for the search.", + "items": { + "type": "string" + } + }, + "engines": { + "type": "array", + "description": "Optional list of specific SearXNG engines to use when the user requests them explicitly.", + "items": { + "type": "string" + } + }, + "language": { + "type": "string", + "description": "Optional language code for the search." + }, + "time_range": { + "type": "string", + "description": "Optional time range filter for engines that support it.", + "enum": [ + "day", + "month", + "year" + ] + }, + "page": { + "type": "integer", + "description": "Optional search result page number starting at 1." + }, + "limit": { + "type": "integer", + "description": "Optional maximum number of results to return to the model after local truncation." + } + }, + "required": [ + "query" + ], + "additionalProperties": false + } + } +}