diff --git a/src/BraveAPI/index.ts b/src/BraveAPI/index.ts index 0c7db6b..24d8ab8 100644 --- a/src/BraveAPI/index.ts +++ b/src/BraveAPI/index.ts @@ -21,7 +21,7 @@ const getDefaultRequestHeaders = (): Record => { }; }; -const isValidGoggleURL = (url: string) => { +const isValidGoggleURL = (url: string): boolean => { try { // Only allow HTTPS URLs return new URL(url).protocol === 'https:'; @@ -30,6 +30,16 @@ const isValidGoggleURL = (url: string) => { } }; +const normalizeGoggle = (value: unknown): string | null => { + if (typeof value !== 'string') return null; + const trimmed = value.trim(); + if (trimmed.length === 0) return null; + if (/^https?:\/\//i.test(trimmed)) { + return isValidGoggleURL(trimmed) ? trimmed : null; + } + return trimmed; +}; + async function issueRequest( endpoint: T, parameters: Endpoints[T]['params'], @@ -81,11 +91,11 @@ async function issueRequest( // Handle `goggles` parameter(s) if (key === 'goggles') { - if (typeof value === 'string') { - queryParams.set(key, value); - } else if (Array.isArray(value)) { - for (const url of value.filter(isValidGoggleURL)) { - queryParams.append(key, url); + const candidates = Array.isArray(value) ? value : [value]; + for (const candidate of candidates) { + const normalized = normalizeGoggle(candidate); + if (normalized !== null) { + queryParams.append(key, normalized); } } continue; diff --git a/src/tools/llm_context/schemas/input.ts b/src/tools/llm_context/schemas/input.ts index e2cf0d2..2dff31a 100644 --- a/src/tools/llm_context/schemas/input.ts +++ b/src/tools/llm_context/schemas/input.ts @@ -191,7 +191,9 @@ export const RequestParamsSchema = z.object({ .optional(), goggles: z .union([z.string(), z.array(z.string())]) - .describe('The goggle url(s) or definition(s) to rerank search results.') + .describe( + "Goggles act as a custom re-ranking on top of Brave's search index. The parameter supports both a url where the Goggle is hosted or the definition of the Goggle. Multiple goggle URLs and/or definitions can be provided in an array. For more details, refer to the Goggles repository (i.e., https://github.com/brave/goggles-quickstart)." + ) .optional(), freshness: FreshnessSchema.optional(), enable_local: z diff --git a/src/tools/news/params.ts b/src/tools/news/params.ts index c2168f2..c85666d 100644 --- a/src/tools/news/params.ts +++ b/src/tools/news/params.ts @@ -75,9 +75,9 @@ export const params = z.object({ ) .optional(), goggles: z - .array(z.string()) + .union([z.string(), z.array(z.string())]) .describe( - "Goggles act as a custom re-ranking on top of Brave's search index. The parameter supports both a url where the Goggle is hosted or the definition of the Goggle. For more details, refer to the Goggles repository (i.e., https://github.com/brave/goggles-quickstart)." + "Goggles act as a custom re-ranking on top of Brave's search index. The parameter supports both a url where the Goggle is hosted or the definition of the Goggle. Multiple goggle URLs and/or definitions can be provided in an array. For more details, refer to the Goggles repository (i.e., https://github.com/brave/goggles-quickstart)." ) .optional(), }); diff --git a/src/tools/web/params.ts b/src/tools/web/params.ts index e474961..cd2df89 100644 --- a/src/tools/web/params.ts +++ b/src/tools/web/params.ts @@ -223,9 +223,9 @@ export const params = z.object({ .describe("Result filter (default ['web', 'query'])") .optional(), goggles: z - .array(z.string()) + .union([z.string(), z.array(z.string())]) .describe( - "Goggles act as a custom re-ranking on top of Brave's search index. The parameter supports both a url where the Goggle is hosted or the definition of the Goggle. For more details, refer to the Goggles repository (i.e., https://github.com/brave/goggles-quickstart)." + "Goggles act as a custom re-ranking on top of Brave's search index. The parameter supports both a url where the Goggle is hosted or the definition of the Goggle. Multiple goggle URLs and/or definitions can be provided in an array. For more details, refer to the Goggles repository (i.e., https://github.com/brave/goggles-quickstart)." ) .optional(), units: z