Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

feat(teamhub): list web search backends without reading quota

LeoXing5eceb7c85abb
diff --git a/apps/teamhub/internal/app/contract_server_agent_web_search.go b/apps/teamhub/internal/app/contract_server_agent_web_search.go
index 53439a18..100f23dd 100644
--- a/apps/teamhub/internal/app/contract_server_agent_web_search.go
+++ b/apps/teamhub/internal/app/contract_server_agent_web_search.go
@@ -221,7 +221,7 @@ func (a *App) listAgentWebBackends(c *gin.Context) {
if _, ok := a.authorizeWebSearch(c, &input, true); !ok {
return
}
- list, err := a.webSearch.List(c.Request.Context())
+ list, err := a.webSearch.List(c.Request.Context(), true)
if err != nil {
a.logger.Error("web_search.list_failed", "request_id", c.GetString("request_id"), "error_type", fmt.Sprintf("%T", err))
a.writeAgentAPIError(c, http.StatusInternalServerError, api.AgentApiErrorCodeInternalError, "Web search backends could not be listed")
diff --git a/apps/teamhub/internal/app/feature_web_search.go b/apps/teamhub/internal/app/feature_web_search.go
index 06123c71..239f5d49 100644
--- a/apps/teamhub/internal/app/feature_web_search.go
+++ b/apps/teamhub/internal/app/feature_web_search.go
@@ -29,7 +29,8 @@ func (a *App) registerWebSearch(cfg config.Config, deps Deps) error {
}
func (a *App) listWebSearchBackends(c *gin.Context) {
- list, err := a.webSearch.List(c.Request.Context())
+ params := contractParams[httpapi.ListWebSearchBackendsParams](c)
+ list, err := a.webSearch.List(c.Request.Context(), params.IncludeQuota == nil || *params.IncludeQuota)
if err != nil {
a.internalError(c, "list web search backends", err)
return
diff --git a/apps/teamhub/internal/app/routes_public.go b/apps/teamhub/internal/app/routes_public.go
index 3aabf0e3..2da8d999 100644
--- a/apps/teamhub/internal/app/routes_public.go
+++ b/apps/teamhub/internal/app/routes_public.go
@@ -234,7 +234,7 @@ func (a *App) publicRoutes() []contractRoute {
route(http.MethodGet, "/v0/voice-sessions/:voiceSessionId/events", httpapi.ListVoiceEventsParams{}, session, a.listVoiceEvents),
route(http.MethodGet, "/v0/voice-sessions/:voiceSessionId/events/stream", httpapi.StreamVoiceEventsParams{}, session, a.streamVoiceEvents),
route(http.MethodGet, "/v0/voice-sessions/:voiceSessionId/transcript-events", httpapi.ListVoiceTranscriptEventsParams{}, session, a.listVoiceTranscriptEvents),
- route(http.MethodGet, "/v0/web-search/backends", nil, session, a.listWebSearchBackends),
+ route(http.MethodGet, "/v0/web-search/backends", httpapi.ListWebSearchBackendsParams{}, session, a.listWebSearchBackends),
route(http.MethodDelete, "/v0/web-search/backends/:backend/key", nil, session, a.deleteWebSearchKey),
route(http.MethodPut, "/v0/web-search/backends/:backend/key", nil, session, a.saveWebSearchKey),
route(http.MethodDelete, "/v0/web-search/default", nil, session, a.clearWebSearchDefault),
diff --git a/apps/teamhub/internal/websearch/websearch.go b/apps/teamhub/internal/websearch/websearch.go
index c7bb63ea..4586870b 100644
--- a/apps/teamhub/internal/websearch/websearch.go
+++ b/apps/teamhub/internal/websearch/websearch.go
@@ -225,7 +225,7 @@ type BackendView struct {
KeyURL string `json:"key_url,omitempty"`
Status string `json:"status"`
KeyState string `json:"key_state"`
- Quota Quota `json:"quota"`
+ Quota Quota `json:"quota,omitzero"`
}
// List is every backend as the contract's WebSearchBackendList.
@@ -374,8 +374,9 @@ func defaultSearch(ring keyring, chosen Backend) Backend {
return You
}
-// List returns every backend with its live quota.
-func (s *Service) List(ctx context.Context) (List, error) {
+// List returns every backend, with its live quota when withQuota is set;
+// without it no provider is called and every quota is left out.
+func (s *Service) List(ctx context.Context, withQuota bool) (List, error) {
ring, err := s.keys(ctx)
if err != nil {
return List{}, err
@@ -391,6 +392,10 @@ func (s *Service) List(ctx context.Context) (List, error) {
defer cancel()
for i, b := range order {
views[i] = s.view(ring, b)
+ if !withQuota {
+ views[i].Quota = Quota{}
+ continue
+ }
if !ring.usable(b) || b == Brave {
continue
}
@@ -456,12 +461,13 @@ func braveStoredQuota(row store.ListWebSearchKeysRow) Quota {
return quota
}
-// Backend returns one backend with its live quota.
+// Backend returns one backend's state without asking any provider for its
+// quota.
func (s *Service) Backend(ctx context.Context, b Backend) (BackendView, error) {
if _, ok := definitions[b]; !ok {
return BackendView{}, ErrInvalid
}
- list, err := s.List(ctx)
+ list, err := s.List(ctx, false)
if err != nil {
return BackendView{}, err
}
diff --git a/apps/teamhub/internal/websearch/websearch_test.go b/apps/teamhub/internal/websearch/websearch_test.go
index 7b31065d..5376a179 100644
--- a/apps/teamhub/internal/websearch/websearch_test.go
+++ b/apps/teamhub/internal/websearch/websearch_test.go
@@ -464,7 +464,7 @@ func TestDefaultSearchUsesTheChosenDefaultWithoutFallingBack(t *testing.T) {
failure.Backend != Firecrawl || failure.Suggested {
t.Fatalf("Run with a rejected chosen default = %#v", err)
}
- if list, err := s.List(t.Context()); err != nil || list.DefaultSearch != Firecrawl || list.ChosenDefault != Firecrawl {
+ if list, err := s.List(t.Context(), false); err != nil || list.DefaultSearch != Firecrawl || list.ChosenDefault != Firecrawl {
t.Fatalf("List with a rejected chosen default = %+v, %v", list, err)
}
if err := s.RemoveKey(t.Context(), Firecrawl); err != nil {
@@ -638,7 +638,7 @@ func TestListReportsTiersStatesAndLiveQuota(t *testing.T) {
saveRaw(t, s, keys, Firecrawl, "provider-secret", "verified")
saveRaw(t, s, keys, Tavily, "provider-secret", "rejected")
saveRaw(t, s, keys, Brave, "provider-secret", "unverified")
- list, err := s.List(t.Context())
+ list, err := s.List(t.Context(), true)
if err != nil || list.DefaultSearch != Brave || list.DefaultOpen != Direct || !list.CanSaveKeys || len(list.Backends) != 5 {
t.Fatalf("List = %+v, %v", list, err)
}
@@ -661,7 +661,7 @@ func TestListReportsTiersStatesAndLiveQuota(t *testing.T) {
}
saveRaw(t, s, keys, You, "provider-secret", "verified")
start := time.Now()
- list, err = s.List(t.Context())
+ list, err = s.List(t.Context(), true)
if err != nil || time.Since(start) > quotaTimeout+500*time.Millisecond {
t.Fatalf("List took %s, %v", time.Since(start), err)
}
@@ -671,3 +671,35 @@ func TestListReportsTiersStatesAndLiveQuota(t *testing.T) {
}
}
}
+
+func TestListWithoutQuotaCallsNoProvider(t *testing.T) {
+ fake := newFakeProviders(t, func(w http.ResponseWriter, r seen) {
+ t.Errorf("unexpected %s", r.path)
+ })
+ s, keys := newTestService(t, fake)
+ saveRaw(t, s, keys, Firecrawl, "provider-secret", "verified")
+ saveRaw(t, s, keys, You, "provider-secret", "verified")
+ saveRaw(t, s, keys, Tavily, "provider-secret", "verified")
+ list, err := s.List(t.Context(), false)
+ if err != nil || len(list.Backends) != 5 || len(fake.seen()) != 0 {
+ t.Fatalf("List = %+v, %v, calls %d", list, err, len(fake.seen()))
+ }
+ for _, view := range list.Backends {
+ raw, err := json.Marshal(view)
+ if err != nil || view.Name == "" || view.Status == "" || strings.Contains(string(raw), `"quota"`) {
+ t.Fatalf("backend without quota = %s, %v", raw, err)
+ }
+ }
+}
+
+func TestBackendReadsItsStatusWithoutCallingAProvider(t *testing.T) {
+ fake := newFakeProviders(t, func(w http.ResponseWriter, r seen) {
+ t.Errorf("unexpected %s", r.path)
+ })
+ s, keys := newTestService(t, fake)
+ saveRaw(t, s, keys, Tavily, "provider-secret", "verified")
+ view, err := s.Backend(t.Context(), Tavily)
+ if err != nil || view.ID != Tavily || view.Status != "ready" || len(fake.seen()) != 0 {
+ t.Fatalf("Backend = %+v, %v, calls %d", view, err, len(fake.seen()))
+ }
+}
diff --git a/apps/teamhub/tests/integration/web_search_test.go b/apps/teamhub/tests/integration/web_search_test.go
index 1c92ba96..fc2f18a3 100644
--- a/apps/teamhub/tests/integration/web_search_test.go
+++ b/apps/teamhub/tests/integration/web_search_test.go
@@ -122,6 +122,10 @@ func TestWebSearchAsksForAKeyAndReplaysTheSearchWithIt(t *testing.T) {
list.Backends[3].ID != websearch.You || list.Backends[3].Tier != websearch.TierFreeTrial {
t.Fatalf("backends: %d %s", code, listed)
}
+ code, _, listed = env.request(alice, http.MethodGet, "/v0/web-search/backends?include_quota=false", nil, "")
+ if code != http.StatusOK || strings.Contains(string(listed), `"quota"`) || !strings.Contains(string(listed), `"free_trial"`) {
+ t.Fatalf("backends without quota: %d %s", code, listed)
+ }
saveKey := func(backend, key, decisionID string) (int, []byte) {
t.Helper()
diff --git a/contracts/generated/go/teamhubagentapi/openapi.gen.go b/contracts/generated/go/teamhubagentapi/openapi.gen.go
index 0afd7085..9e9ade46 100644
--- a/contracts/generated/go/teamhubagentapi/openapi.gen.go
+++ b/contracts/generated/go/teamhubagentapi/openapi.gen.go
@@ -3483,8 +3483,8 @@ type WebSearchBackend struct {
// OfficialKeyCheck Whether the provider has an official endpoint TeamHub checks a key with before saving it. Without one the key is saved unverified and proven by its first call.
OfficialKeyCheck bool `json:"official_key_check"`
- // Quota What is left on the backend's account. from says where the numbers came from: provider is the provider's official usage endpoint read for this response; last_response is the rate-limit headers of the last call (Brave), as of observed_at; unavailable carries no numbers and reason says why.
- Quota WebSearchQuota `json:"quota"`
+ // Quota Left out when the caller asked for the list without quota.
+ Quota *WebSearchQuota `json:"quota,omitempty"`
Status WebSearchBackendStatus `json:"status"`
// Tier How a call to the backend runs now. key: with an API key saved in the product (or one it still needs); free_trial: You.com without a key, a limited trial with lower quality results that TeamHub does not count; none: direct, which needs no account.
diff --git a/contracts/generated/go/teamhubpublic/openapi.gen.go b/contracts/generated/go/teamhubpublic/openapi.gen.go
index 4a82cc97..4aa94a95 100644
--- a/contracts/generated/go/teamhubpublic/openapi.gen.go
+++ b/contracts/generated/go/teamhubpublic/openapi.gen.go
@@ -9619,8 +9619,8 @@ type WebSearchBackend struct {
// OfficialKeyCheck Whether the provider has an official endpoint TeamHub checks a key with before saving it. Without one the key is saved unverified and proven by its first call.
OfficialKeyCheck bool `json:"official_key_check"`
- // Quota What is left on the backend's account. from says where the numbers came from: provider is the provider's official usage endpoint read for this response; last_response is the rate-limit headers of the last call (Brave), as of observed_at; unavailable carries no numbers and reason says why.
- Quota WebSearchQuota `json:"quota"`
+ // Quota Left out when the caller asked for the list without quota.
+ Quota *WebSearchQuota `json:"quota,omitempty"`
Status WebSearchBackendStatus `json:"status"`
// Tier How a call to the backend runs now. key: with an API key saved in the product (or one it still needs); free_trial: You.com without a key, a limited trial with lower quality results that TeamHub does not count; none: direct, which needs no account.
@@ -10418,6 +10418,12 @@ type ListVoiceTranscriptEventsParams struct {
AfterId *Id `form:"after_id,omitempty" json:"after_id,omitempty"`
}
+// ListWebSearchBackendsParams defines parameters for ListWebSearchBackends.
+type ListWebSearchBackendsParams struct {
+ // IncludeQuota Read each backend's live quota from its provider.
+ IncludeQuota *bool `form:"include_quota,omitempty" json:"include_quota,omitempty"`
+}
+
// UpdateWorkItemParams defines parameters for UpdateWorkItem.
type UpdateWorkItemParams struct {
// IdempotencyKey Reusing the same key with the same canonical request returns the original result. Reusing it with different input returns 409 IDEMPOTENCY_KEY_REUSED.
diff --git a/contracts/generated/typescript/src/generated/teamhub-agent-api/types.ts b/contracts/generated/typescript/src/generated/teamhub-agent-api/types.ts
index e394b50e..a9493a88 100644
--- a/contracts/generated/typescript/src/generated/teamhub-agent-api/types.ts
+++ b/contracts/generated/typescript/src/generated/teamhub-agent-api/types.ts
@@ -2811,7 +2811,8 @@ export interface components {
status: "ready" | "key_required" | "key_rejected";
/** @enum {string} */
key_state: "none" | "verified" | "unverified" | "rejected";
- quota: components["schemas"]["WebSearchQuota"];
+ /** @description Left out when the caller asked for the list without quota. */
+ quota?: components["schemas"]["WebSearchQuota"];
};
/** @description Every backend in TeamHub's default order. default_search is the backend a search without one uses now: the default a Human saved (chosen_default_search) even when its key stopped working, else the first ready backend in that order. */
WebSearchBackendList: {
diff --git a/contracts/generated/typescript/src/generated/teamhub-public/types.ts b/contracts/generated/typescript/src/generated/teamhub-public/types.ts
index d44b579c..808b056b 100644
--- a/contracts/generated/typescript/src/generated/teamhub-public/types.ts
+++ b/contracts/generated/typescript/src/generated/teamhub-public/types.ts
@@ -2518,7 +2518,7 @@ export interface paths {
};
/**
* List the web search backends with their display text, state and live quota.
- * @description Every backend in TeamHub's default order, with what a client needs to render one row each: name, commands, filters, free tier text, where to get a key, the key state and live quota. Quota is read from each provider's official usage endpoint for this request, in parallel and briefly.
+ * @description Every backend in TeamHub's default order, with what a client needs to render one row each: name, commands, filters, free tier text, where to get a key, the key state and live quota. Quota is read from each provider's official usage endpoint for this request, in parallel and briefly; with include_quota=false TeamHub calls no provider and leaves quota out of every backend.
*/
get: operations["listWebSearchBackends"];
put?: never;
@@ -7468,7 +7468,8 @@ export interface components {
status: "ready" | "key_required" | "key_rejected";
/** @enum {string} */
key_state: "none" | "verified" | "unverified" | "rejected";
- quota: components["schemas"]["WebSearchQuota"];
+ /** @description Left out when the caller asked for the list without quota. */
+ quota?: components["schemas"]["WebSearchQuota"];
};
/** @description Every backend in TeamHub's default order. default_search is the backend a search without one uses now: the default a Human saved (chosen_default_search) even when its key stopped working, else the first ready backend in that order. */
WebSearchBackendList: {
@@ -12686,7 +12687,10 @@ export interface operations {
};
listWebSearchBackends: {
parameters: {
- query?: never;
+ query?: {
+ /** @description Read each backend's live quota from its provider. */
+ include_quota?: boolean;
+ };
header?: never;
path?: never;
cookie?: never;
diff --git a/contracts/openapi/components/web-search.yaml b/contracts/openapi/components/web-search.yaml
index b9403196..495ac0ad 100644
--- a/contracts/openapi/components/web-search.yaml
+++ b/contracts/openapi/components/web-search.yaml
@@ -129,7 +129,6 @@ components:
official_key_check,
status,
key_state,
- quota,
]
properties:
id:
@@ -215,7 +214,10 @@ components:
WebSearchBackendKeyStateRejected,
]
quota:
- $ref: "#/components/schemas/WebSearchQuota"
+ description: >-
+ Left out when the caller asked for the list without quota.
+ allOf:
+ - $ref: "#/components/schemas/WebSearchQuota"
WebSearchBackendList:
type: object
additionalProperties: false
diff --git a/contracts/openapi/paths/teamhub-public-web-search.yaml b/contracts/openapi/paths/teamhub-public-web-search.yaml
index 0ecea717..67d36db4 100644
--- a/contracts/openapi/paths/teamhub-public-web-search.yaml
+++ b/contracts/openapi/paths/teamhub-public-web-search.yaml
@@ -8,7 +8,15 @@ backends:
render one row each: name, commands, filters, free tier text, where to
get a key, the key state and live quota. Quota is read from each
provider's official usage endpoint for this request, in parallel and
- briefly.
+ briefly; with include_quota=false TeamHub calls no provider and leaves
+ quota out of every backend.
+ parameters:
+ - name: include_quota
+ in: query
+ description: Read each backend's live quota from its provider.
+ schema:
+ type: boolean
+ default: true
responses:
"200":
description: The backends.