mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-11-25 02:53:48 +00:00
Added a check for characters which an API key cannot contain (#1227)
This commit is contained in:
1 parent
c72a1948b3
commit
e5a7f5423f
40 files changed
+1179
-71
No files matched your search
+10
-2
@@ -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
|
||||
|
||||
+10
-2
@@ -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);
|
||||
|
||||
@@ -26,6 +26,18 @@ public sealed class ToolSettingsFieldDefinition
|
||||
|
||||
public bool Secret { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Whether the field holds an API key, which is always a secret as well.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// An API key goes into an HTTP header, which carries nothing but ASCII. The settings dialog
|
||||
/// therefore checks what the user enters there. Settings validation leaves the key alone on
|
||||
/// purpose, since it decides whether the tool is offered at all: a key which was stored before,
|
||||
/// or which the organization provides, would switch off the whole tool without a word. The
|
||||
/// tool checks a stored key again before sending it.
|
||||
/// </remarks>
|
||||
public bool IsAPIKey { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Name of the group this field belongs to, or empty when it stands on its own.
|
||||
/// </summary>
|
||||
|
||||
@@ -68,19 +68,29 @@ public sealed class ToolSettingsSchemaBuilder
|
||||
|
||||
public ToolSettingsSchemaBuilder RequiredSecret(string name) => this.Add(name, isRequired: true, isSecret: true);
|
||||
|
||||
/// <summary>
|
||||
/// An optional API key, kept in the operating system's keyring like any secret.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Use this instead of a plain secret for every key which goes into an HTTP header, so that the
|
||||
/// settings dialog tells the user about a character the header cannot carry.
|
||||
/// </remarks>
|
||||
public ToolSettingsSchemaBuilder OptionalAPIKey(string name) => this.Add(name, isRequired: false, isSecret: true, isAPIKey: true);
|
||||
|
||||
public ToolSettingsSchema Build() => new()
|
||||
{
|
||||
Properties = new(this.properties, StringComparer.Ordinal),
|
||||
Required = [..this.requiredNames],
|
||||
};
|
||||
|
||||
private ToolSettingsSchemaBuilder Add(string name, bool isRequired, string optionSource = "", bool isSecret = false, IReadOnlyList<string>? enumValues = null)
|
||||
private ToolSettingsSchemaBuilder Add(string name, bool isRequired, string optionSource = "", bool isSecret = false, IReadOnlyList<string>? enumValues = null, bool isAPIKey = false)
|
||||
{
|
||||
this.properties[name] = new ToolSettingsFieldDefinition
|
||||
{
|
||||
OptionSource = optionSource,
|
||||
EnumValues = enumValues?.ToList() ?? [],
|
||||
Secret = isSecret,
|
||||
IsAPIKey = isAPIKey,
|
||||
Group = this.currentGroup,
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user