feat(websearch): settings UI overhaul and quota improvements
- Remove Priority field, auto load-balance by quota remaining - Replace QuotaRefreshInterval (daily/weekly/monthly) with SubscribedAt (subscription date, monthly lazy refresh via Redis TTL) - Add collapsible provider cards, API key show/copy, usage progress bar - Add test endpoint (POST /web-search-emulation/test) bypassing quota - Wire WebSearchManagerBuilder on startup (was never called before) - Fix nextMonthlyReset day-of-month overflow (Jan 31 → Feb 28) - Fix non-deterministic sort in selectByQuotaWeight - Map ProxyID in builder for provider-level proxy tracking - Fix frontend timezone drift in subscribed_at date picker - Fix provider deletion index shift for expandedProviders state
This commit is contained in:
@@ -22,15 +22,14 @@ type WebSearchEmulationConfig struct {
|
||||
|
||||
// WebSearchProviderConfig describes a single search provider (Brave or Tavily).
|
||||
type WebSearchProviderConfig struct {
|
||||
Type string `json:"type"` // websearch.ProviderTypeBrave | Tavily
|
||||
APIKey string `json:"api_key,omitempty"` // secret — omitted in API responses
|
||||
APIKeyConfigured bool `json:"api_key_configured"` // read-only mask
|
||||
Priority int `json:"priority"` // lower = higher priority
|
||||
QuotaLimit int64 `json:"quota_limit"` // 0 = unlimited
|
||||
QuotaRefreshInterval string `json:"quota_refresh_interval"` // websearch.QuotaRefresh*
|
||||
QuotaUsed int64 `json:"quota_used,omitempty"` // read-only: current period usage
|
||||
ProxyID *int64 `json:"proxy_id"` // optional proxy association
|
||||
ExpiresAt *int64 `json:"expires_at,omitempty"` // optional expiration timestamp
|
||||
Type string `json:"type"` // websearch.ProviderTypeBrave | Tavily
|
||||
APIKey string `json:"api_key,omitempty"` // secret — omitted in API responses
|
||||
APIKeyConfigured bool `json:"api_key_configured"` // read-only mask
|
||||
QuotaLimit int64 `json:"quota_limit"` // 0 = unlimited
|
||||
SubscribedAt *int64 `json:"subscribed_at,omitempty"` // subscription start (unix seconds); quota resets monthly
|
||||
QuotaUsed int64 `json:"quota_used,omitempty"` // read-only: current usage from Redis
|
||||
ProxyID *int64 `json:"proxy_id"` // optional proxy association
|
||||
ExpiresAt *int64 `json:"expires_at,omitempty"` // optional expiration timestamp
|
||||
}
|
||||
|
||||
// --- Validation ---
|
||||
@@ -42,13 +41,6 @@ var validProviderTypes = map[string]bool{
|
||||
websearch.ProviderTypeTavily: true,
|
||||
}
|
||||
|
||||
var validQuotaIntervals = map[string]bool{
|
||||
websearch.QuotaRefreshDaily: true,
|
||||
websearch.QuotaRefreshWeekly: true,
|
||||
websearch.QuotaRefreshMonthly: true,
|
||||
"": true, // defaults to monthly
|
||||
}
|
||||
|
||||
func validateWebSearchConfig(cfg *WebSearchEmulationConfig) error {
|
||||
if cfg == nil {
|
||||
return nil
|
||||
@@ -61,9 +53,6 @@ func validateWebSearchConfig(cfg *WebSearchEmulationConfig) error {
|
||||
if !validProviderTypes[p.Type] {
|
||||
return fmt.Errorf("provider[%d]: invalid type %q", i, p.Type)
|
||||
}
|
||||
if !validQuotaIntervals[p.QuotaRefreshInterval] {
|
||||
return fmt.Errorf("provider[%d]: invalid quota_refresh_interval %q", i, p.QuotaRefreshInterval)
|
||||
}
|
||||
if p.QuotaLimit < 0 {
|
||||
return fmt.Errorf("provider[%d]: quota_limit must be >= 0", i)
|
||||
}
|
||||
@@ -237,17 +226,55 @@ func (s *SettingService) RebuildWebSearchManager(ctx context.Context) {
|
||||
slog.Info("websearch: manager rebuilt", "provider_count", len(providerConfigs))
|
||||
}
|
||||
|
||||
// SanitizeWebSearchConfig returns a copy with api_key fields masked for API responses.
|
||||
func SanitizeWebSearchConfig(cfg *WebSearchEmulationConfig) *WebSearchEmulationConfig {
|
||||
// WebSearchTestResult holds the result of a search test.
|
||||
type WebSearchTestResult struct {
|
||||
Provider string `json:"provider"`
|
||||
Results []websearch.SearchResult `json:"results"`
|
||||
Query string `json:"query"`
|
||||
}
|
||||
|
||||
// TestWebSearch executes a test search using the currently configured Manager.
|
||||
// Uses Manager.TestSearch which bypasses quota tracking.
|
||||
func TestWebSearch(ctx context.Context, query string) (*WebSearchTestResult, error) {
|
||||
mgr := getWebSearchManager()
|
||||
if mgr == nil {
|
||||
return nil, fmt.Errorf("web search: manager not initialized, save config first")
|
||||
}
|
||||
resp, providerName, err := mgr.TestSearch(ctx, websearch.SearchRequest{
|
||||
Query: query,
|
||||
MaxResults: webSearchDefaultMaxResults,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &WebSearchTestResult{
|
||||
Provider: providerName,
|
||||
Results: resp.Results,
|
||||
Query: resp.Query,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// SanitizeWebSearchConfig returns a copy with api_key fields masked and quota usage populated.
|
||||
func SanitizeWebSearchConfig(ctx context.Context, cfg *WebSearchEmulationConfig) *WebSearchEmulationConfig {
|
||||
if cfg == nil {
|
||||
return nil
|
||||
}
|
||||
out := *cfg
|
||||
out.Providers = make([]WebSearchProviderConfig, len(cfg.Providers))
|
||||
|
||||
// Load usage from the global Manager (reads from Redis)
|
||||
mgr := getWebSearchManager()
|
||||
|
||||
for i, p := range cfg.Providers {
|
||||
out.Providers[i] = p
|
||||
out.Providers[i].APIKeyConfigured = p.APIKey != ""
|
||||
out.Providers[i].APIKey = "" // never return the secret
|
||||
|
||||
// Populate quota usage from Redis
|
||||
if mgr != nil {
|
||||
used, _ := mgr.GetUsage(ctx, p.Type)
|
||||
out.Providers[i].QuotaUsed = used
|
||||
}
|
||||
}
|
||||
return &out
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -16,8 +17,8 @@ func TestValidateWebSearchConfig_Valid(t *testing.T) {
|
||||
cfg := &WebSearchEmulationConfig{
|
||||
Enabled: true,
|
||||
Providers: []WebSearchProviderConfig{
|
||||
{Type: "brave", Priority: 1, QuotaLimit: 1000, QuotaRefreshInterval: "monthly"},
|
||||
{Type: "tavily", Priority: 2, QuotaLimit: 500, QuotaRefreshInterval: "daily"},
|
||||
{Type: "brave", QuotaLimit: 1000},
|
||||
{Type: "tavily", QuotaLimit: 500},
|
||||
},
|
||||
}
|
||||
require.NoError(t, validateWebSearchConfig(cfg))
|
||||
@@ -39,13 +40,6 @@ func TestValidateWebSearchConfig_InvalidType(t *testing.T) {
|
||||
require.ErrorContains(t, validateWebSearchConfig(cfg), "invalid type")
|
||||
}
|
||||
|
||||
func TestValidateWebSearchConfig_InvalidQuotaInterval(t *testing.T) {
|
||||
cfg := &WebSearchEmulationConfig{
|
||||
Providers: []WebSearchProviderConfig{{Type: "brave", QuotaRefreshInterval: "hourly"}},
|
||||
}
|
||||
require.ErrorContains(t, validateWebSearchConfig(cfg), "invalid quota_refresh_interval")
|
||||
}
|
||||
|
||||
func TestValidateWebSearchConfig_NegativeQuotaLimit(t *testing.T) {
|
||||
cfg := &WebSearchEmulationConfig{
|
||||
Providers: []WebSearchProviderConfig{{Type: "brave", QuotaLimit: -1}},
|
||||
@@ -56,20 +50,13 @@ func TestValidateWebSearchConfig_NegativeQuotaLimit(t *testing.T) {
|
||||
func TestValidateWebSearchConfig_DuplicateType(t *testing.T) {
|
||||
cfg := &WebSearchEmulationConfig{
|
||||
Providers: []WebSearchProviderConfig{
|
||||
{Type: "brave", Priority: 1},
|
||||
{Type: "brave", Priority: 2},
|
||||
{Type: "brave"},
|
||||
{Type: "brave"},
|
||||
},
|
||||
}
|
||||
require.ErrorContains(t, validateWebSearchConfig(cfg), "duplicate type")
|
||||
}
|
||||
|
||||
func TestValidateWebSearchConfig_EmptyQuotaInterval(t *testing.T) {
|
||||
cfg := &WebSearchEmulationConfig{
|
||||
Providers: []WebSearchProviderConfig{{Type: "brave", QuotaRefreshInterval: ""}},
|
||||
}
|
||||
require.NoError(t, validateWebSearchConfig(cfg))
|
||||
}
|
||||
|
||||
func TestValidateWebSearchConfig_ZeroQuotaLimit(t *testing.T) {
|
||||
cfg := &WebSearchEmulationConfig{
|
||||
Providers: []WebSearchProviderConfig{{Type: "brave", QuotaLimit: 0}},
|
||||
@@ -99,6 +86,15 @@ func TestParseWebSearchConfigJSON_InvalidJSON(t *testing.T) {
|
||||
require.Empty(t, cfg.Providers)
|
||||
}
|
||||
|
||||
func TestParseWebSearchConfigJSON_BackwardCompatibility(t *testing.T) {
|
||||
// Old config with priority and quota_refresh_interval should parse without error
|
||||
raw := `{"enabled":true,"providers":[{"type":"brave","priority":1,"quota_refresh_interval":"monthly","quota_limit":1000}]}`
|
||||
cfg := parseWebSearchConfigJSON(raw)
|
||||
require.True(t, cfg.Enabled)
|
||||
require.Len(t, cfg.Providers, 1)
|
||||
require.Equal(t, int64(1000), cfg.Providers[0].QuotaLimit)
|
||||
}
|
||||
|
||||
// --- SanitizeWebSearchConfig ---
|
||||
|
||||
func TestSanitizeWebSearchConfig_MaskAPIKey(t *testing.T) {
|
||||
@@ -108,7 +104,7 @@ func TestSanitizeWebSearchConfig_MaskAPIKey(t *testing.T) {
|
||||
{Type: "brave", APIKey: "sk-secret-xxx"},
|
||||
},
|
||||
}
|
||||
out := SanitizeWebSearchConfig(cfg)
|
||||
out := SanitizeWebSearchConfig(context.Background(), cfg)
|
||||
require.Equal(t, "", out.Providers[0].APIKey)
|
||||
require.True(t, out.Providers[0].APIKeyConfigured)
|
||||
}
|
||||
@@ -117,25 +113,24 @@ func TestSanitizeWebSearchConfig_NoAPIKey(t *testing.T) {
|
||||
cfg := &WebSearchEmulationConfig{
|
||||
Providers: []WebSearchProviderConfig{{Type: "brave", APIKey: ""}},
|
||||
}
|
||||
out := SanitizeWebSearchConfig(cfg)
|
||||
out := SanitizeWebSearchConfig(context.Background(), cfg)
|
||||
require.Equal(t, "", out.Providers[0].APIKey)
|
||||
require.False(t, out.Providers[0].APIKeyConfigured)
|
||||
}
|
||||
|
||||
func TestSanitizeWebSearchConfig_Nil(t *testing.T) {
|
||||
require.Nil(t, SanitizeWebSearchConfig(nil))
|
||||
require.Nil(t, SanitizeWebSearchConfig(context.Background(), nil))
|
||||
}
|
||||
|
||||
func TestSanitizeWebSearchConfig_PreservesOtherFields(t *testing.T) {
|
||||
cfg := &WebSearchEmulationConfig{
|
||||
Enabled: true,
|
||||
Providers: []WebSearchProviderConfig{
|
||||
{Type: "brave", APIKey: "secret", Priority: 10, QuotaLimit: 1000},
|
||||
{Type: "brave", APIKey: "secret", QuotaLimit: 1000},
|
||||
},
|
||||
}
|
||||
out := SanitizeWebSearchConfig(cfg)
|
||||
out := SanitizeWebSearchConfig(context.Background(), cfg)
|
||||
require.True(t, out.Enabled)
|
||||
require.Equal(t, 10, out.Providers[0].Priority)
|
||||
require.Equal(t, int64(1000), out.Providers[0].QuotaLimit)
|
||||
}
|
||||
|
||||
@@ -143,6 +138,6 @@ func TestSanitizeWebSearchConfig_DoesNotMutateOriginal(t *testing.T) {
|
||||
cfg := &WebSearchEmulationConfig{
|
||||
Providers: []WebSearchProviderConfig{{Type: "brave", APIKey: "secret"}},
|
||||
}
|
||||
_ = SanitizeWebSearchConfig(cfg)
|
||||
_ = SanitizeWebSearchConfig(context.Background(), cfg)
|
||||
require.Equal(t, "secret", cfg.Providers[0].APIKey)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user