Skip to content

7.4 billion tokens per month. 34 free LLM providers. 635 free model endpoints. All behind one /v1 endpoint, plus any custom OpenAI-compatible endpoint. Smart routing, automatic failover, encrypted keys. Personal experimentation only.

TypeScript

fix(search): a served search marks its key as working (#1612)

Tashfeen8fd4ef99caad
diff --git a/server/src/__tests__/services/search.test.ts b/server/src/__tests__/services/search.test.ts
index f2d8cfc8..f4f3db90 100644
--- a/server/src/__tests__/services/search.test.ts
+++ b/server/src/__tests__/services/search.test.ts
@@ -14,6 +14,7 @@ import {
import { SEARCH_PLATFORMS as SHARED_SEARCH_PLATFORMS } from '../../../../shared/types.js';
import { SEARCH_PLATFORMS } from '../../lib/search-platforms.js';
import { seedSearchEngines } from '../helpers/search-engines.js';
+import { clearLastHealthyStampState } from '../../services/health.js';
const realFetch = globalThis.fetch;
@@ -108,6 +109,23 @@ describe('search service', () => {
expect(logRows()).toEqual([{ engine_id: 'exa-search', platform: 'exa', status: 'ok', result_count: 2 }]);
});
+ it('a served search marks the key as working; a failed one does not (#1493)', async () => {
+ clearLastHealthyStampState();
+ const good = addKey('exa');
+ getDb().prepare("UPDATE api_keys SET status = 'unknown' WHERE id = ?").run(good);
+ mockFetch(() => json({ results: [{ title: 'A', url: 'https://a', text: 'alpha' }] }));
+ await runSearch('q');
+ expect(getDb().prepare('SELECT status, last_healthy_at FROM api_keys WHERE id = ?').get(good))
+ .toMatchObject({ status: 'healthy', last_healthy_at: expect.any(String) });
+
+ getDb().prepare('DELETE FROM api_keys').run();
+ clearAllCooldowns(Number.MAX_SAFE_INTEGER);
+ const bad = addKey('exa');
+ mockFetch(() => json({ error: 'boom' }, 500));
+ await expect(runSearch('q')).rejects.toBeInstanceOf(SearchError);
+ expect(getDb().prepare('SELECT last_healthy_at FROM api_keys WHERE id = ?').get(bad)).toEqual({ last_healthy_at: null });
+ });
+
it('passes Tavily topic/answer through and keeps its scores', async () => {
addKey('tavily');
const fn = mockFetch(() => json({ answer: '42', results: [{ title: 'T', url: 'https://t', content: 'c', score: 0.83 }] }));
diff --git a/server/src/services/search.ts b/server/src/services/search.ts
index c795a136..3193f33b 100644
--- a/server/src/services/search.ts
+++ b/server/src/services/search.ts
@@ -15,6 +15,7 @@ import { randomUUID } from 'node:crypto';
import { secondsUntilNextMonth } from './key-budget.js';
import { reserveProviderCredential, type ReservedProviderCredential } from './provider-credential.js';
import { isOnCooldown, setCooldown } from './ratelimit.js';
+import { markKeyHealthyFromRequest } from './health.js';
// Web search over the key pool (#1174).
//
@@ -513,6 +514,11 @@ export async function runSearch(query: string, opts: SearchOptions = {}): Promis
const out = await ADAPTERS[row.platform]!(credential, query, opts, maxResults);
if (out.results.length === 0) throw new SearchError('upstream returned no valid search hits', 502);
logSearch(row, credential.id, 'ok', Date.now() - started, out.results.length, clientAgent, null);
+ // A served search is the evidence the key works, the same as a served
+ // chat request: it stamps last_healthy_at and lifts an 'unknown' or
+ // 'error' key. For SerpSearch, Exa and Parallel it is the only evidence
+ // after their one-time check (#1599, #1493).
+ markKeyHealthyFromRequest(credential.id);
return {
query,
...(out.answer ? { answer: out.answer } : {}),