Added a check for characters which an API key cannot contain (#1227)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-11-24 17:20:13 +01:00
1 parent c72a1948b3
commit e5a7f5423f
40 files changed
+1179 -71

No files matched your search

@@ -1,4 +1,5 @@
using AIStudio.Tools.PluginSystem;
using AIStudio.Tools.Validation;
namespace AIStudio.Tools.ToolCallingSystem.ToolCallingImplementations.WebSearch.Staan;
@@ -65,7 +66,7 @@ public sealed class StaanSearchBackend : IWebSearchBackend
public void DeclareSettings(ToolSettingsSchemaBuilder builder) => builder
.InGroup(SETTINGS_GROUP)
.OptionalSecret(API_KEY_SETTING)
.OptionalAPIKey(API_KEY_SETTING)
.OptionalEnum(MARKET_SETTING, SUPPORTED_MARKETS)
.InGroup(string.Empty);
@@ -125,6 +126,13 @@ public sealed class StaanSearchBackend : IWebSearchBackend
if (string.IsNullOrWhiteSpace(apiKey))
throw new InvalidOperationException(TB("A Staan API key is required."));
//
// A key stored before AI Studio checked keys, or one the organization provides, may hold a
// character the header cannot carry. The dispatcher asks the other services all the same:
//
if (!APIKeyValidation.TryPrepareForHeader(apiKey, out var headerAPIKey))
throw new InvalidOperationException(APIKeyValidation.GetUnusableKeyMessage("Staan"));
//
// Staan refuses a longer query outright. Shortening it here would search for something
// other than what was asked, so the model is told and can search again instead:
@@ -141,7 +149,7 @@ public sealed class StaanSearchBackend : IWebSearchBackend
var market = ResolveMarket(query.Language, settingsValues, notes);
var searchRequest = new StaanSearchRequest { Query = query.Query, Market = market, Offset = ReadOffset(query.Page) };
var response = await this.searchClient.SearchAsync(apiKey.Trim(), searchRequest, query.TimeoutSeconds, token);
var response = await this.searchClient.SearchAsync(headerAPIKey, searchRequest, query.TimeoutSeconds, token);
//
// Staan corrects an obvious mistake in a query and says so. Which query actually ran is
@@ -1,4 +1,5 @@
using AIStudio.Tools.PluginSystem;
using AIStudio.Tools.Validation;
namespace AIStudio.Tools.ToolCallingSystem.ToolCallingImplementations.WebSearch.Tavily;
@@ -58,7 +59,7 @@ public sealed class TavilySearchBackend : IWebSearchBackend
public void DeclareSettings(ToolSettingsSchemaBuilder builder) => builder
.InGroup(SETTINGS_GROUP)
.OptionalSecret(API_KEY_SETTING)
.OptionalAPIKey(API_KEY_SETTING)
.OptionalEnum(SEARCH_DEPTH_SETTING, SUPPORTED_SEARCH_DEPTHS)
.InGroup(string.Empty);
@@ -118,6 +119,13 @@ public sealed class TavilySearchBackend : IWebSearchBackend
if (string.IsNullOrWhiteSpace(apiKey))
throw new InvalidOperationException(TB("A Tavily API key is required."));
//
// A key stored before AI Studio checked keys, or one the organization provides, may hold a
// character the header cannot carry. The dispatcher asks the other services all the same:
//
if (!APIKeyValidation.TryPrepareForHeader(apiKey, out var headerAPIKey))
throw new InvalidOperationException(APIKeyValidation.GetUnusableKeyMessage("Tavily"));
var notes = new List<string>();
var language = ResolveLanguage(query.Language, notes);
var searchRequest = new TavilySearchRequest
@@ -138,7 +146,7 @@ public sealed class TavilySearchBackend : IWebSearchBackend
SafeSearch = query.SafeSearch?.TavilyValue,
};
var response = await this.searchClient.SearchAsync(apiKey.Trim(), searchRequest, query.TimeoutSeconds, token);
var response = await this.searchClient.SearchAsync(headerAPIKey, searchRequest, query.TimeoutSeconds, token);
var hits = response.Results.Select(result => new SearchHit(result.Url, result.Title, result.Content));
var candidates = SearchCandidateCollector.Collect(WebSearchBackend.TAVILY, hits, query.Limit, out var candidateCount);
return new WebSearchBackendResult(WebSearchBackend.TAVILY, candidates, candidateCount, notes);