fix(search): consolidate duplicate Brave API helpers (#1010)

* fix(search): consolidate duplicate Brave API helper functions

getBraveApiKey() and braveHeaders() were duplicated across provider.ts,
tool-llm-context.ts, and tool-search.ts. Export both from provider.ts
and import in the tool files.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix(test): update provider export count to include braveHeaders

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
TÂCHES 2026-03-17 18:06:23 -06:00 committed by GitHub
parent 818d77d5a2
commit 13f9d5585d
4 changed files with 14 additions and 28 deletions

View file

@ -35,6 +35,15 @@ export function getBraveApiKey(): string {
return process.env.BRAVE_API_KEY || ''
}
/** Standard headers for Brave Search API requests. */
export function braveHeaders(): Record<string, string> {
return {
"Accept": "application/json",
"Accept-Encoding": "gzip",
"X-Subscription-Token": getBraveApiKey(),
}
}
/** Returns the Ollama API key from the environment, or empty string if not set. */
export function getOllamaApiKey(): string {
return process.env.OLLAMA_API_KEY || ''

View file

@ -27,7 +27,7 @@ import { normalizeQuery, extractDomain } from "./url-utils.js";
import { formatLLMContext, type LLMContextSnippet, type LLMContextSource } from "./format.js";
import type { TavilyResult, TavilySearchResponse } from "./tavily.js";
import { publishedDateToAge } from "./tavily.js";
import { getTavilyApiKey, getOllamaApiKey, resolveSearchProvider } from "./provider.js";
import { getTavilyApiKey, getOllamaApiKey, getBraveApiKey, braveHeaders, resolveSearchProvider } from "./provider.js";
// =============================================================================
// Types
@ -94,18 +94,6 @@ contextCache.startPurgeInterval(60_000);
// Helpers
// =============================================================================
function getBraveApiKey(): string {
return process.env.BRAVE_API_KEY || "";
}
function braveHeaders(): Record<string, string> {
return {
"Accept": "application/json",
"Accept-Encoding": "gzip",
"X-Subscription-Token": getBraveApiKey(),
};
}
/** Rough token estimate: ~4 chars per token for English text. */
function estimateTokens(text: string): number {
return Math.ceil(text.length / 4);

View file

@ -20,7 +20,7 @@ import { LRUTTLCache } from "./cache.js";
import { fetchWithRetryTimed, fetchWithRetry, classifyError, type RateLimitInfo } from "./http.js";
import { normalizeQuery, toDedupeKey, detectFreshness } from "./url-utils.js";
import { formatSearchResults, type SearchResultFormatted, type FormatSearchOptions } from "./format.js";
import { getTavilyApiKey, getOllamaApiKey, resolveSearchProvider } from "./provider.js";
import { getTavilyApiKey, getOllamaApiKey, getBraveApiKey, braveHeaders, resolveSearchProvider } from "./provider.js";
import { normalizeTavilyResult, mapFreshnessToTavily, type TavilySearchResponse } from "./tavily.js";
// =============================================================================
@ -117,18 +117,6 @@ const summarizerCache = new LRUTTLCache<string>({ max: 50, ttlMs: 900_000 });
// Brave API helpers
// =============================================================================
function getBraveApiKey(): string {
return process.env.BRAVE_API_KEY || "";
}
function braveHeaders(): Record<string, string> {
return {
"Accept": "application/json",
"Accept-Encoding": "gzip",
"X-Subscription-Token": getBraveApiKey(),
};
}
/**
* Normalize a Brave result into our formatted result type.
*/

View file

@ -245,7 +245,7 @@ test('getBraveApiKey reads from process.env.BRAVE_API_KEY', async () => {
// 4. Boundary contract — S01→S02 public API surface
// ═══════════════════════════════════════════════════════════════════════════
test('provider.ts exports exactly the 6 expected functions', async () => {
test('provider.ts exports exactly the 7 expected functions', async () => {
const provider = await import(
'../resources/extensions/search-the-web/provider.ts'
)
@ -254,6 +254,7 @@ test('provider.ts exports exactly the 6 expected functions', async () => {
'resolveSearchProvider',
'getTavilyApiKey',
'getBraveApiKey',
'braveHeaders',
'getOllamaApiKey',
'getSearchProviderPreference',
'setSearchProviderPreference',
@ -271,6 +272,6 @@ test('provider.ts exports exactly the 6 expected functions', async () => {
assert.deepEqual(
actualFunctions.sort(),
[...expectedExports].sort(),
'provider.ts should export exactly the 6 expected functions (no extra function exports)',
'provider.ts should export exactly the 7 expected functions (no extra function exports)',
)
})