1. 14 Apr, 2026 4 commits
    • erio's avatar
      test: add unit tests for billing, websearch, and notify systems · 9028d208
      erio authored
      Billing (25 tests):
      - CalculateCostUnified: nil resolver fallback, token/per_request/image modes
      - GetModelPricingWithChannel: nil/partial/full channel overrides
      - resolveAccountStatsCost: four-level priority chain integration tests
      
      WebSearch (18 tests):
      - PopulateWebSearchUsage: nil input, manager states, QuotaLimit nil/*int64
      - ResetWebSearchUsage: nil manager error
      - Manager.ResetUsage: nil Redis
      - shouldEmulateWebSearch: full decision chain (8 scenarios)
      
      Notify (36 tests):
      - ParseNotifyEmails/MarshalNotifyEmails: old/new format, roundtrip
      - crossedDownward: boundary values, threshold semantics
      - checkQuotaDimCrossings: mixed dimensions, disabled/zero skip
      9028d208
    • erio's avatar
      feat(websearch): settings UI overhaul and quota improvements · d0674e0f
      erio authored
      - 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
      d0674e0f
    • erio's avatar
      fix(websearch): improve isProxyError detection and add manager tests · 60b0fa81
      erio authored
      - Add TLS error detection to isProxyError (RecordHeaderError, handshake)
      - Case-insensitive error string matching
      - Add 19 unit tests for: isProviderAvailable, resolveProxyID,
        isProxyError, isProxyAvailable, selectByQuotaWeight, newHTTPClient
      60b0fa81
    • erio's avatar
      feat(gateway): add web search emulation for Anthropic API Key accounts · 1b53ffca
      erio authored
      Inject web search capability for Claude Console (API Key) accounts that
      don't natively support Anthropic's web_search tool. When a pure
      web_search request is detected, the gateway calls Brave Search or Tavily
      API directly and constructs an Anthropic-protocol-compliant SSE/JSON
      response without forwarding to upstream.
      
      Backend:
      - New `pkg/websearch/` SDK: Brave and Tavily provider implementations
        with io.LimitReader, proxy support, and Redis-based quota tracking
        (Lua atomic INCR + TTL, DECR rollback on failure)
      - Global config via `settings.web_search_emulation_config` (JSON) with
        in-process cache + singleflight, input validation, API key merge on
        save, and sanitized API responses
      - Channel-level toggle via `channels.features_config` JSONB column
        (DB migration 101)
      - Account-level toggle via `accounts.extra.web_search_emulation`
      - Request interception in `Forward()` with SSE streaming response
        construction using json.Marshal (no manual string concatenation)
      - Manager hot-reload: `RebuildWebSearchManager()` called on config save
        and startup via `SetWebSearchRedisClient()`
      - 70 unit tests covering providers, manager, config validation,
        sanitization, tool detection, query extraction, and response building
      
      Frontend:
      - Settings → Gateway tab: Web Search Emulation config card with global
        toggle, provider list (add/remove, API key, priority, quota, proxy)
      - Channels → Anthropic tab: web search emulation toggle with global
        state linkage (disabled when global off)
      - Account Create/Edit modals: web search emulation toggle for API Key
        type with Toggle component
      - Full i18n coverage (zh + en)
      1b53ffca