@openclaw/brave-plugin 2026.10.10 → 2026.11.5-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/.setup/brave-web-search-provider-PfEIXTwN.mjs +85 -0
- package/dist/.setup/{brave-web-search-provider.runtime-C-yoQ8ge.mjs → brave-web-search-provider.runtime-BJOE5Gqg.mjs} +99 -175
- package/dist/.source-module-urls/c56d5ead937f37db9ad473f25208bbdd773d745f5be1ef3863e9582e6bf06315.json +1 -1
- package/dist/index.js +1 -2
- package/dist/web-search-contract-api.js +1 -1
- package/dist/web-search-provider.js +1 -1
- package/dist/web-search-shared.js +1 -28
- package/package.json +4 -4
- package/dist/.setup/brave-web-search-provider-BqnjCSjg.mjs +0 -78
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { resolveBraveMode } from "../web-search-shared.js";
|
|
2
|
+
import { isDiagnosticFlagEnabled } from "openclaw/plugin-sdk/diagnostic-flags";
|
|
3
|
+
import { createLazyRuntimeModule } from "openclaw/plugin-sdk/lazy-runtime";
|
|
4
|
+
import { WEB_SEARCH_QUERY_COUNT_PROPERTIES, createWebSearchProviderContractFields, mergeScopedSearchConfig, resolveProviderWebSearchPluginConfig } from "openclaw/plugin-sdk/provider-web-search-config-contract";
|
|
5
|
+
import { asOptionalRecord } from "openclaw/plugin-sdk/string-coerce-runtime";
|
|
6
|
+
// extensions/brave/src/brave-web-search-provider.ts
|
|
7
|
+
/**
|
|
8
|
+
* Brave web-search provider factory. It builds the agent tool definition and
|
|
9
|
+
* lazy-loads HTTP execution only when a search is run.
|
|
10
|
+
*/
|
|
11
|
+
const BRAVE_CREDENTIAL_PATH = "plugins.entries.brave.config.webSearch.apiKey";
|
|
12
|
+
const loadBraveWebSearchRuntime = createLazyRuntimeModule(() => import("./brave-web-search-provider.runtime-BJOE5Gqg.mjs"));
|
|
13
|
+
const BraveSearchSchema = {
|
|
14
|
+
type: "object",
|
|
15
|
+
properties: {
|
|
16
|
+
...WEB_SEARCH_QUERY_COUNT_PROPERTIES,
|
|
17
|
+
country: {
|
|
18
|
+
type: "string",
|
|
19
|
+
description: "2-letter country code for region-specific results (e.g., 'DE', 'US', 'ALL'). Default: 'US'."
|
|
20
|
+
},
|
|
21
|
+
language: {
|
|
22
|
+
type: "string",
|
|
23
|
+
description: "ISO 639-1 language code for results (e.g., 'en', 'de', 'fr')."
|
|
24
|
+
},
|
|
25
|
+
freshness: {
|
|
26
|
+
type: "string",
|
|
27
|
+
description: "Filter by time: 'day' (24h), 'week', 'month', or 'year'."
|
|
28
|
+
},
|
|
29
|
+
date_after: {
|
|
30
|
+
type: "string",
|
|
31
|
+
description: "Only results published after this date (YYYY-MM-DD)."
|
|
32
|
+
},
|
|
33
|
+
date_before: {
|
|
34
|
+
type: "string",
|
|
35
|
+
description: "Only results published before this date (YYYY-MM-DD)."
|
|
36
|
+
},
|
|
37
|
+
search_lang: {
|
|
38
|
+
type: "string",
|
|
39
|
+
description: "Brave language code for search results (e.g., 'en', 'de', 'en-gb', 'zh-hans', 'zh-hant', 'pt-br')."
|
|
40
|
+
},
|
|
41
|
+
ui_lang: {
|
|
42
|
+
type: "string",
|
|
43
|
+
description: "Locale code for UI elements in language-region format (e.g., 'en-US', 'de-DE', 'fr-FR', 'tr-TR'). Must include region subtag."
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
function createBraveWebSearchProvider() {
|
|
48
|
+
return {
|
|
49
|
+
id: "brave",
|
|
50
|
+
label: "Brave Search",
|
|
51
|
+
hint: "Structured results · country/language/time filters",
|
|
52
|
+
onboardingScopes: ["text-inference"],
|
|
53
|
+
credentialLabel: "Brave Search API key",
|
|
54
|
+
envVars: ["BRAVE_API_KEY"],
|
|
55
|
+
placeholder: "BSA...",
|
|
56
|
+
signupUrl: "https://brave.com/search/api/",
|
|
57
|
+
docsUrl: "https://docs.openclaw.ai/tools/brave-search",
|
|
58
|
+
autoDetectOrder: 10,
|
|
59
|
+
credentialPath: BRAVE_CREDENTIAL_PATH,
|
|
60
|
+
...createWebSearchProviderContractFields({
|
|
61
|
+
credentialPath: BRAVE_CREDENTIAL_PATH,
|
|
62
|
+
searchCredential: { type: "top-level" },
|
|
63
|
+
configuredCredential: { pluginId: "brave" }
|
|
64
|
+
}),
|
|
65
|
+
createTool: (ctx) => {
|
|
66
|
+
const searchConfig = mergeScopedSearchConfig(ctx.searchConfig, "brave", resolveProviderWebSearchPluginConfig(ctx.config, "brave"), { mirrorApiKeyToTopLevel: true });
|
|
67
|
+
const config = ctx.config;
|
|
68
|
+
const braveMode = resolveBraveMode(asOptionalRecord(searchConfig?.brave));
|
|
69
|
+
const diagnosticsEnabled = isDiagnosticFlagEnabled("brave.http", config);
|
|
70
|
+
return {
|
|
71
|
+
description: braveMode === "llm-context" ? "Search the web using Brave Search LLM Context API. Returns pre-extracted page content (text chunks, tables, code blocks) optimized for LLM grounding." : "Search the web using Brave Search API. Supports region-specific and localized search via country and language parameters. Returns titles, URLs, and snippets for fast research.",
|
|
72
|
+
parameters: BraveSearchSchema,
|
|
73
|
+
execute: async (args, context) => {
|
|
74
|
+
context?.signal?.throwIfAborted();
|
|
75
|
+
const { executeBraveSearch } = await loadBraveWebSearchRuntime();
|
|
76
|
+
return await executeBraveSearch(args, searchConfig, {
|
|
77
|
+
diagnosticsEnabled,
|
|
78
|
+
signal: context?.signal
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
export { createBraveWebSearchProvider as t };
|
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import { resolveBraveMode } from "../web-search-shared.js";
|
|
2
2
|
import { asNonArrayRecord, normalizeLowercaseStringOrEmpty, normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime";
|
|
3
3
|
import { buildTimeoutAbortSignal } from "openclaw/plugin-sdk/extension-shared";
|
|
4
|
+
import { sha256Hex } from "openclaw/plugin-sdk/node-crypto-runtime";
|
|
4
5
|
import { assertOkOrThrowProviderError, readProviderJsonResponse } from "openclaw/plugin-sdk/provider-http";
|
|
5
|
-
import { DEFAULT_SEARCH_COUNT,
|
|
6
|
+
import { DEFAULT_SEARCH_COUNT, buildSearchCacheKey, externalContentStamp, formatCliCommand, parseWebSearchTimeFilters, readCachedSearchPayload, readConfiguredSecretString, readStringParam, readWebSearchApiKey, readWebSearchQueryAndCount, resolveSearchCacheTtlMs, resolveSearchCount, resolveSearchTimeoutSeconds, resolveSiteName, withSelfHostedWebToolsEndpoint, withTrustedWebToolsEndpoint, wrapWebContent, writeCachedSearchPayload } from "openclaw/plugin-sdk/provider-web-search";
|
|
6
7
|
import { createSubsystemLogger } from "openclaw/plugin-sdk/runtime-env";
|
|
7
8
|
import { assertHttpUrlTargetsPrivateNetwork, isBlockedHostnameOrIp, isPrivateIpAddress, resolvePinnedHostnameWithPolicy } from "openclaw/plugin-sdk/ssrf-runtime";
|
|
8
|
-
// extensions/brave/src/brave-web-search-provider.
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
// extensions/brave/src/brave-web-search-provider.runtime.ts
|
|
10
|
+
const DEFAULT_BRAVE_BASE_URL = "https://api.search.brave.com";
|
|
11
|
+
const BRAVE_SEARCH_ENDPOINT_PATH = "/res/v1/web/search";
|
|
12
|
+
const BRAVE_LLM_CONTEXT_ENDPOINT_PATH = "/res/v1/llm/context";
|
|
13
|
+
const braveHttpLogger = createSubsystemLogger("brave/http");
|
|
13
14
|
const BRAVE_COUNTRY_CODES = /* @__PURE__ */ new Set([
|
|
14
15
|
"AR",
|
|
15
16
|
"AU",
|
|
@@ -117,13 +118,6 @@ function normalizeBraveSearchLang(value) {
|
|
|
117
118
|
const canonical = BRAVE_SEARCH_LANG_ALIASES[lower] ?? lower;
|
|
118
119
|
return BRAVE_SEARCH_LANG_CODES.has(canonical) ? canonical : void 0;
|
|
119
120
|
}
|
|
120
|
-
/** Normalize Brave country filter values. */
|
|
121
|
-
function normalizeBraveCountry(value) {
|
|
122
|
-
const trimmed = normalizeOptionalString(value);
|
|
123
|
-
if (!trimmed) return;
|
|
124
|
-
const canonical = trimmed.toUpperCase();
|
|
125
|
-
return BRAVE_COUNTRY_CODES.has(canonical) ? canonical : "ALL";
|
|
126
|
-
}
|
|
127
121
|
function normalizeBraveUiLang(value) {
|
|
128
122
|
const match = normalizeOptionalString(value)?.match(BRAVE_UI_LANG_LOCALE);
|
|
129
123
|
if (!match) return;
|
|
@@ -131,50 +125,10 @@ function normalizeBraveUiLang(value) {
|
|
|
131
125
|
if (!language || !region) return;
|
|
132
126
|
return `${normalizeLowercaseStringOrEmpty(language)}-${region.toUpperCase()}`;
|
|
133
127
|
}
|
|
134
|
-
/** Normalize Brave search and UI language params, detecting swapped fields. */
|
|
135
|
-
function normalizeBraveLanguageParams(params) {
|
|
136
|
-
const rawSearchLang = normalizeOptionalString(params.search_lang);
|
|
137
|
-
const rawUiLang = normalizeOptionalString(params.ui_lang);
|
|
138
|
-
let searchLangCandidate = rawSearchLang;
|
|
139
|
-
let uiLangCandidate = rawUiLang;
|
|
140
|
-
if (normalizeBraveUiLang(rawSearchLang) && normalizeBraveSearchLang(rawUiLang)) {
|
|
141
|
-
searchLangCandidate = rawUiLang;
|
|
142
|
-
uiLangCandidate = rawSearchLang;
|
|
143
|
-
}
|
|
144
|
-
const search_lang = normalizeBraveSearchLang(searchLangCandidate);
|
|
145
|
-
if (searchLangCandidate && !search_lang) return { invalidField: "search_lang" };
|
|
146
|
-
const ui_lang = normalizeBraveUiLang(uiLangCandidate);
|
|
147
|
-
if (uiLangCandidate && !ui_lang) return { invalidField: "ui_lang" };
|
|
148
|
-
return {
|
|
149
|
-
search_lang,
|
|
150
|
-
ui_lang
|
|
151
|
-
};
|
|
152
|
-
}
|
|
153
|
-
/** Map Brave LLM Context API grounding results into web-search result rows. */
|
|
154
|
-
function mapBraveLlmContextResults(data) {
|
|
155
|
-
return (Array.isArray(data.grounding?.generic) ? data.grounding.generic : []).map((entry) => ({
|
|
156
|
-
url: entry.url ?? "",
|
|
157
|
-
title: entry.title ?? "",
|
|
158
|
-
snippets: (entry.snippets ?? []).filter((snippet) => typeof snippet === "string" && snippet.length > 0),
|
|
159
|
-
siteName: resolveSiteName(entry.url) || void 0,
|
|
160
|
-
published: data.sources?.[entry.url]?.age?.[3] || data.sources?.[entry.url]?.age?.[1] || void 0
|
|
161
|
-
}));
|
|
162
|
-
}
|
|
163
|
-
// extensions/brave/src/brave-web-search-provider.runtime.ts
|
|
164
|
-
const DEFAULT_BRAVE_BASE_URL = "https://api.search.brave.com";
|
|
165
|
-
const BRAVE_SEARCH_ENDPOINT_PATH = "/res/v1/web/search";
|
|
166
|
-
const BRAVE_LLM_CONTEXT_ENDPOINT_PATH = "/res/v1/llm/context";
|
|
167
|
-
const braveHttpLogger = createSubsystemLogger("brave/http");
|
|
168
128
|
function logBraveHttp(diagnosticsEnabled, event, meta) {
|
|
169
129
|
if (!diagnosticsEnabled) return;
|
|
170
130
|
braveHttpLogger.info(`brave http ${event}`, meta);
|
|
171
131
|
}
|
|
172
|
-
function resolveBraveApiKey(searchConfig) {
|
|
173
|
-
return readConfiguredSecretString(searchConfig?.apiKey, "plugins.entries.brave.config.webSearch.apiKey") ?? readProviderEnvValue(["BRAVE_API_KEY"]);
|
|
174
|
-
}
|
|
175
|
-
function resolveBraveBaseUrl(braveConfig) {
|
|
176
|
-
return readConfiguredSecretString(braveConfig?.baseUrl, "plugins.entries.brave.config.webSearch.baseUrl")?.replace(/\/+$/u, "") || DEFAULT_BRAVE_BASE_URL;
|
|
177
|
-
}
|
|
178
132
|
async function braveEndpointTargetsPrivateNetwork(url, signal) {
|
|
179
133
|
if (isBlockedHostnameOrIp(url.hostname)) return true;
|
|
180
134
|
try {
|
|
@@ -208,75 +162,8 @@ async function validateBraveBaseUrl(baseUrl, signal) {
|
|
|
208
162
|
}
|
|
209
163
|
return await braveEndpointTargetsPrivateNetwork(parsed, signal) ? "selfHosted" : "strict";
|
|
210
164
|
}
|
|
211
|
-
async function runBraveSearch(params) {
|
|
212
|
-
const url = new URL(params.baseUrl);
|
|
213
|
-
url.pathname = `${url.pathname.replace(/\/+$/u, "")}${params.mode === "llm-context" ? BRAVE_LLM_CONTEXT_ENDPOINT_PATH : BRAVE_SEARCH_ENDPOINT_PATH}`;
|
|
214
|
-
url.search = "";
|
|
215
|
-
url.searchParams.set("q", params.query);
|
|
216
|
-
if (params.country) url.searchParams.set("country", params.country);
|
|
217
|
-
if (params.search_lang) url.searchParams.set("search_lang", params.search_lang);
|
|
218
|
-
if (params.freshness) url.searchParams.set("freshness", params.freshness);
|
|
219
|
-
else if (params.dateAfter && params.dateBefore) url.searchParams.set("freshness", `${params.dateAfter}to${params.dateBefore}`);
|
|
220
|
-
else if (params.dateAfter) url.searchParams.set("freshness", `${params.dateAfter}to${(/* @__PURE__ */ new Date()).toISOString().slice(0, 10)}`);
|
|
221
|
-
else if (params.mode === "web" && params.dateBefore) url.searchParams.set("freshness", `1970-01-01to${params.dateBefore}`);
|
|
222
|
-
if (params.mode === "web") {
|
|
223
|
-
url.searchParams.set("count", String(params.count));
|
|
224
|
-
if (params.ui_lang) url.searchParams.set("ui_lang", params.ui_lang);
|
|
225
|
-
}
|
|
226
|
-
logBraveHttp(params.diagnosticsEnabled, "request", {
|
|
227
|
-
mode: params.mode,
|
|
228
|
-
url: url.toString(),
|
|
229
|
-
query: url.searchParams.get("q") ?? "",
|
|
230
|
-
params: Object.fromEntries(url.searchParams.entries())
|
|
231
|
-
});
|
|
232
|
-
const startedAt = Date.now();
|
|
233
|
-
return (params.endpointMode === "selfHosted" ? withSelfHostedWebToolsEndpoint : withTrustedWebToolsEndpoint)({
|
|
234
|
-
url: url.toString(),
|
|
235
|
-
signal: params.signal,
|
|
236
|
-
init: {
|
|
237
|
-
method: "GET",
|
|
238
|
-
headers: {
|
|
239
|
-
Accept: "application/json",
|
|
240
|
-
"X-Subscription-Token": params.apiKey
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
}, async ({ response }) => {
|
|
244
|
-
logBraveHttp(params.diagnosticsEnabled, "response", {
|
|
245
|
-
mode: params.mode,
|
|
246
|
-
status: response.status,
|
|
247
|
-
ok: response.ok,
|
|
248
|
-
durationMs: Date.now() - startedAt
|
|
249
|
-
});
|
|
250
|
-
const errorLabel = params.mode === "llm-context" ? "Brave LLM Context API error" : "Brave Search API error";
|
|
251
|
-
await assertOkOrThrowProviderError(response, errorLabel);
|
|
252
|
-
if (params.mode === "llm-context") {
|
|
253
|
-
const data = await readProviderJsonResponse(response, errorLabel);
|
|
254
|
-
return {
|
|
255
|
-
mode: "llm-context",
|
|
256
|
-
results: mapBraveLlmContextResults(data),
|
|
257
|
-
sources: data.sources
|
|
258
|
-
};
|
|
259
|
-
}
|
|
260
|
-
const data = await readProviderJsonResponse(response, errorLabel);
|
|
261
|
-
return {
|
|
262
|
-
mode: "web",
|
|
263
|
-
results: (Array.isArray(data.web?.results) ? data.web.results : []).slice(0, params.count).map((entry) => {
|
|
264
|
-
const description = entry.description ?? "";
|
|
265
|
-
const title = entry.title ?? "";
|
|
266
|
-
const resultUrl = entry.url ?? "";
|
|
267
|
-
return {
|
|
268
|
-
title: title ? wrapWebContent(title, "web_search") : "",
|
|
269
|
-
url: resultUrl,
|
|
270
|
-
description: description ? wrapWebContent(description, "web_search") : "",
|
|
271
|
-
published: entry.page_age || void 0,
|
|
272
|
-
siteName: resolveSiteName(resultUrl) || void 0
|
|
273
|
-
};
|
|
274
|
-
})
|
|
275
|
-
};
|
|
276
|
-
});
|
|
277
|
-
}
|
|
278
165
|
async function executeBraveSearch(args, searchConfig, options) {
|
|
279
|
-
const apiKey =
|
|
166
|
+
const apiKey = readWebSearchApiKey(searchConfig?.apiKey, "plugins.entries.brave.config.webSearch.apiKey", ["BRAVE_API_KEY"]);
|
|
280
167
|
if (!apiKey) return {
|
|
281
168
|
error: "missing_brave_api_key",
|
|
282
169
|
message: `web_search (brave) needs a Brave Search API key. Run \`${formatCliCommand("openclaw configure --section web")}\` to store it, or set BRAVE_API_KEY in the Gateway environment. If you do not want to configure a search API key, use web_fetch for a specific URL or the browser tool for interactive pages.`,
|
|
@@ -284,7 +171,7 @@ async function executeBraveSearch(args, searchConfig, options) {
|
|
|
284
171
|
};
|
|
285
172
|
const braveConfig = asNonArrayRecord(searchConfig?.brave);
|
|
286
173
|
const braveMode = resolveBraveMode(braveConfig);
|
|
287
|
-
const braveBaseUrl =
|
|
174
|
+
const braveBaseUrl = readConfiguredSecretString(braveConfig?.baseUrl, "plugins.entries.brave.config.webSearch.baseUrl")?.replace(/\/+$/u, "") || DEFAULT_BRAVE_BASE_URL;
|
|
288
175
|
const { signal, cleanup } = buildTimeoutAbortSignal({
|
|
289
176
|
timeoutMs: resolveSearchTimeoutSeconds(searchConfig) * 1e3,
|
|
290
177
|
signal: options?.signal,
|
|
@@ -295,30 +182,33 @@ async function executeBraveSearch(args, searchConfig, options) {
|
|
|
295
182
|
signal?.throwIfAborted();
|
|
296
183
|
const braveEndpointMode = await validateBraveBaseUrl(braveBaseUrl, signal);
|
|
297
184
|
signal?.throwIfAborted();
|
|
298
|
-
const query =
|
|
299
|
-
const
|
|
300
|
-
|
|
301
|
-
message: `count must be an integer from 1 to ${MAX_SEARCH_COUNT}.`
|
|
302
|
-
}) ?? searchConfig?.maxResults ?? void 0;
|
|
303
|
-
const country = normalizeBraveCountry(readStringParam(args, "country"));
|
|
185
|
+
const { query, count } = readWebSearchQueryAndCount(args, searchConfig);
|
|
186
|
+
const canonicalCountry = normalizeOptionalString(readStringParam(args, "country"))?.toUpperCase();
|
|
187
|
+
const country = canonicalCountry ? BRAVE_COUNTRY_CODES.has(canonicalCountry) ? canonicalCountry : "ALL" : void 0;
|
|
304
188
|
const language = readStringParam(args, "language");
|
|
305
189
|
const search_lang = readStringParam(args, "search_lang");
|
|
306
190
|
const ui_lang = readStringParam(args, "ui_lang");
|
|
307
|
-
const
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
if (
|
|
191
|
+
const rawSearchLang = normalizeOptionalString(search_lang || language);
|
|
192
|
+
const rawUiLang = normalizeOptionalString(ui_lang);
|
|
193
|
+
let searchLangCandidate = rawSearchLang;
|
|
194
|
+
let uiLangCandidate = rawUiLang;
|
|
195
|
+
if (normalizeBraveUiLang(rawSearchLang) && normalizeBraveSearchLang(rawUiLang)) {
|
|
196
|
+
searchLangCandidate = rawUiLang;
|
|
197
|
+
uiLangCandidate = rawSearchLang;
|
|
198
|
+
}
|
|
199
|
+
const normalizedSearchLang = normalizeBraveSearchLang(searchLangCandidate);
|
|
200
|
+
if (searchLangCandidate && !normalizedSearchLang) return {
|
|
312
201
|
error: "invalid_search_lang",
|
|
313
202
|
message: "search_lang must be a Brave-supported language code like 'en', 'en-gb', 'zh-hans', or 'zh-hant'.",
|
|
314
203
|
docs: "https://docs.openclaw.ai/tools/web"
|
|
315
204
|
};
|
|
316
|
-
|
|
205
|
+
const normalizedUiLang = normalizeBraveUiLang(uiLangCandidate);
|
|
206
|
+
if (uiLangCandidate && !normalizedUiLang) return {
|
|
317
207
|
error: "invalid_ui_lang",
|
|
318
208
|
message: "ui_lang must be a language-region locale like 'en-US'.",
|
|
319
209
|
docs: "https://docs.openclaw.ai/tools/web"
|
|
320
210
|
};
|
|
321
|
-
if (
|
|
211
|
+
if (normalizedUiLang && braveMode === "llm-context") return {
|
|
322
212
|
error: "unsupported_ui_lang",
|
|
323
213
|
message: "ui_lang is not supported by Brave llm-context mode. Remove ui_lang or use Brave web mode for locale-based UI hints.",
|
|
324
214
|
docs: "https://docs.openclaw.ai/tools/web"
|
|
@@ -355,13 +245,14 @@ async function executeBraveSearch(args, searchConfig, options) {
|
|
|
355
245
|
const requestedCount = resolveSearchCount(count, DEFAULT_SEARCH_COUNT);
|
|
356
246
|
const cacheKey = buildSearchCacheKey([
|
|
357
247
|
"brave",
|
|
248
|
+
sha256Hex(apiKey),
|
|
358
249
|
braveMode,
|
|
359
250
|
braveBaseUrl,
|
|
360
251
|
query,
|
|
361
252
|
requestedCount,
|
|
362
253
|
country,
|
|
363
|
-
|
|
364
|
-
...braveMode === "web" ? [
|
|
254
|
+
normalizedSearchLang,
|
|
255
|
+
...braveMode === "web" ? [normalizedUiLang] : [],
|
|
365
256
|
freshness,
|
|
366
257
|
dateAfter,
|
|
367
258
|
llmContextDateEnd
|
|
@@ -370,63 +261,96 @@ async function executeBraveSearch(args, searchConfig, options) {
|
|
|
370
261
|
const cacheTtlMs = resolveSearchCacheTtlMs(searchConfig);
|
|
371
262
|
const cached = readCachedSearchPayload(cacheKey, cacheTtlMs);
|
|
372
263
|
if (cached) {
|
|
373
|
-
logBraveHttp(diagnosticsEnabled, "cache hit", {
|
|
374
|
-
mode: braveMode,
|
|
375
|
-
query,
|
|
376
|
-
cacheKey
|
|
377
|
-
});
|
|
264
|
+
logBraveHttp(diagnosticsEnabled, "cache hit", { mode: braveMode });
|
|
378
265
|
return cached;
|
|
379
266
|
}
|
|
380
|
-
logBraveHttp(diagnosticsEnabled, "cache miss", {
|
|
267
|
+
logBraveHttp(diagnosticsEnabled, "cache miss", { mode: braveMode });
|
|
268
|
+
const start = Date.now();
|
|
269
|
+
const url = new URL(braveBaseUrl);
|
|
270
|
+
url.pathname = `${url.pathname.replace(/\/+$/u, "")}${braveMode === "llm-context" ? BRAVE_LLM_CONTEXT_ENDPOINT_PATH : BRAVE_SEARCH_ENDPOINT_PATH}`;
|
|
271
|
+
url.search = "";
|
|
272
|
+
url.searchParams.set("q", query);
|
|
273
|
+
if (country) url.searchParams.set("country", country);
|
|
274
|
+
if (normalizedSearchLang) url.searchParams.set("search_lang", normalizedSearchLang);
|
|
275
|
+
if (freshness) url.searchParams.set("freshness", freshness);
|
|
276
|
+
else if (dateAfter && dateBefore) url.searchParams.set("freshness", `${dateAfter}to${dateBefore}`);
|
|
277
|
+
else if (dateAfter) url.searchParams.set("freshness", `${dateAfter}to${(/* @__PURE__ */ new Date()).toISOString().slice(0, 10)}`);
|
|
278
|
+
else if (braveMode === "web" && dateBefore) url.searchParams.set("freshness", `1970-01-01to${dateBefore}`);
|
|
279
|
+
if (braveMode === "web") {
|
|
280
|
+
url.searchParams.set("count", String(requestedCount));
|
|
281
|
+
if (normalizedUiLang) url.searchParams.set("ui_lang", normalizedUiLang);
|
|
282
|
+
}
|
|
283
|
+
logBraveHttp(diagnosticsEnabled, "request", {
|
|
381
284
|
mode: braveMode,
|
|
382
|
-
|
|
383
|
-
|
|
285
|
+
endpoint: `${url.origin}${url.pathname}`,
|
|
286
|
+
count: requestedCount
|
|
384
287
|
});
|
|
385
|
-
const
|
|
386
|
-
const response = await
|
|
387
|
-
|
|
388
|
-
endpointMode: braveEndpointMode,
|
|
389
|
-
query,
|
|
390
|
-
apiKey,
|
|
391
|
-
diagnosticsEnabled,
|
|
288
|
+
const requestStartedAt = Date.now();
|
|
289
|
+
const response = await (braveEndpointMode === "selfHosted" ? withSelfHostedWebToolsEndpoint : withTrustedWebToolsEndpoint)({
|
|
290
|
+
url: url.toString(),
|
|
392
291
|
signal,
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
292
|
+
init: {
|
|
293
|
+
method: "GET",
|
|
294
|
+
headers: {
|
|
295
|
+
Accept: "application/json",
|
|
296
|
+
"X-Subscription-Token": apiKey
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}, async ({ response: httpResponse }) => {
|
|
300
|
+
logBraveHttp(diagnosticsEnabled, "response", {
|
|
301
|
+
mode: braveMode,
|
|
302
|
+
status: httpResponse.status,
|
|
303
|
+
ok: httpResponse.ok,
|
|
304
|
+
durationMs: Date.now() - requestStartedAt
|
|
305
|
+
});
|
|
306
|
+
const errorLabel = braveMode === "llm-context" ? "Brave LLM Context API error" : "Brave Search API error";
|
|
307
|
+
await assertOkOrThrowProviderError(httpResponse, errorLabel);
|
|
308
|
+
if (braveMode === "llm-context") {
|
|
309
|
+
const data = await readProviderJsonResponse(httpResponse, errorLabel);
|
|
310
|
+
return {
|
|
311
|
+
mode: "llm-context",
|
|
312
|
+
results: (Array.isArray(data.grounding?.generic) ? data.grounding.generic : []).slice(0, requestedCount).map((entry) => ({
|
|
313
|
+
url: entry.url ?? "",
|
|
314
|
+
title: entry.title ? wrapWebContent(entry.title, "web_search") : "",
|
|
315
|
+
snippets: (entry.snippets ?? []).filter((snippet) => typeof snippet === "string" && snippet.length > 0).map((snippet) => wrapWebContent(snippet, "web_search")),
|
|
316
|
+
siteName: resolveSiteName(entry.url) || void 0,
|
|
317
|
+
published: data.sources?.[entry.url]?.age?.[3] || data.sources?.[entry.url]?.age?.[1] || void 0
|
|
318
|
+
})),
|
|
319
|
+
sources: data.sources
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
const data = await readProviderJsonResponse(httpResponse, errorLabel);
|
|
323
|
+
return {
|
|
324
|
+
mode: "web",
|
|
325
|
+
results: (Array.isArray(data.web?.results) ? data.web.results : []).slice(0, requestedCount).map((entry) => {
|
|
326
|
+
const description = entry.description ?? "";
|
|
327
|
+
const title = entry.title ?? "";
|
|
328
|
+
const resultUrl = entry.url ?? "";
|
|
329
|
+
return {
|
|
330
|
+
title: title ? wrapWebContent(title, "web_search") : "",
|
|
331
|
+
url: resultUrl,
|
|
332
|
+
description: description ? wrapWebContent(description, "web_search") : "",
|
|
333
|
+
published: entry.page_age || void 0,
|
|
334
|
+
siteName: resolveSiteName(resultUrl) || void 0
|
|
335
|
+
};
|
|
336
|
+
})
|
|
337
|
+
};
|
|
401
338
|
});
|
|
402
339
|
signal?.throwIfAborted();
|
|
403
|
-
const results = response.
|
|
404
|
-
title: entry.title ? wrapWebContent(entry.title, "web_search") : "",
|
|
405
|
-
url: entry.url,
|
|
406
|
-
snippets: entry.snippets.map((snippet) => wrapWebContent(snippet, "web_search")),
|
|
407
|
-
siteName: entry.siteName,
|
|
408
|
-
published: entry.published
|
|
409
|
-
})) : response.results;
|
|
340
|
+
const results = response.results;
|
|
410
341
|
const payload = {
|
|
411
342
|
query,
|
|
412
343
|
provider: "brave",
|
|
413
344
|
...response.mode === "llm-context" ? { mode: response.mode } : {},
|
|
414
345
|
count: results.length,
|
|
415
346
|
tookMs: Date.now() - start,
|
|
416
|
-
externalContent:
|
|
417
|
-
untrusted: true,
|
|
418
|
-
source: "web_search",
|
|
419
|
-
provider: "brave",
|
|
420
|
-
wrapped: true
|
|
421
|
-
},
|
|
347
|
+
externalContent: externalContentStamp("brave"),
|
|
422
348
|
results,
|
|
423
349
|
...response.mode === "llm-context" ? { sources: response.sources } : {}
|
|
424
350
|
};
|
|
425
351
|
writeCachedSearchPayload(cacheKey, payload, cacheTtlMs);
|
|
426
352
|
logBraveHttp(diagnosticsEnabled, "cache write", {
|
|
427
353
|
mode: response.mode,
|
|
428
|
-
query,
|
|
429
|
-
cacheKey,
|
|
430
354
|
ttlMs: cacheTtlMs,
|
|
431
355
|
count: results.length
|
|
432
356
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"entries":[{"output":"index.js","source":"extensions/brave/index.ts"},{"output":"web-search-contract-api.js","source":"extensions/brave/web-search-contract-api.ts"},{"output":"web-search-provider.js","source":"extensions/brave/web-search-provider.ts"},{"output":"web-search-shared.js","source":"extensions/brave/web-search-shared.ts"}],"chunks":[{"output":".setup/brave-web-search-provider-
|
|
1
|
+
{"version":3,"entries":[{"output":"index.js","source":"extensions/brave/index.ts"},{"output":"web-search-contract-api.js","source":"extensions/brave/web-search-contract-api.ts"},{"output":"web-search-provider.js","source":"extensions/brave/web-search-provider.ts"},{"output":"web-search-shared.js","source":"extensions/brave/web-search-shared.ts"}],"chunks":[{"output":".setup/brave-web-search-provider-PfEIXTwN.mjs","sha256":"62ad7f5aedff0032604ddbb1ea7c4f7e5b0f044d1b3ca1889294b35c73869785","facadeSource":null,"sources":["extensions/brave/src/brave-web-search-provider.ts"]},{"output":".setup/brave-web-search-provider.runtime-BJOE5Gqg.mjs","sha256":"13284f02094a7a23d262244dfebace464a84dab04577718475c25247be8ec035","facadeSource":"extensions/brave/src/brave-web-search-provider.runtime.ts","sources":["extensions/brave/src/brave-web-search-provider.runtime.ts"]},{"output":"index.js","sha256":"3e01d3d332d944c9f8cd963c74be2ab3f254d81738d79e97a1f1661f3ce278fe","facadeSource":"extensions/brave/index.ts","sources":["extensions/brave/index.ts"]},{"output":"web-search-contract-api.js","sha256":"73eb3e58db2ce458714b7c3b21620ae4d221f3d9af0ad6a6a89a1169880bf380","facadeSource":"extensions/brave/web-search-contract-api.ts","sources":[]},{"output":"web-search-provider.js","sha256":"cff3d14ecf6b681d2d10d032d7765709925f91819b9a21ae15c13450ade8fa84","facadeSource":"extensions/brave/web-search-provider.ts","sources":[]},{"output":"web-search-shared.js","sha256":"3f7697de3c5fa16fd4cd02aa5a7cd915b291112bf0aa940c25f69a8691273930","facadeSource":"extensions/brave/web-search-shared.ts","sources":["extensions/brave/web-search-shared.ts"]}],"unrenderedSources":["extensions/brave/web-search-contract-api.ts","extensions/brave/web-search-provider.ts"]}
|
package/dist/index.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { t as createBraveWebSearchProvider } from "./.setup/brave-web-search-provider-
|
|
1
|
+
import { t as createBraveWebSearchProvider } from "./.setup/brave-web-search-provider-PfEIXTwN.mjs";
|
|
2
2
|
import { definePluginEntry } from "openclaw/plugin-sdk/plugin-entry";
|
|
3
3
|
// extensions/brave/index.ts
|
|
4
4
|
/**
|
|
5
5
|
* Brave Search plugin entry. It registers the Brave web-search provider and
|
|
6
6
|
* keeps runtime HTTP execution lazy.
|
|
7
7
|
*/
|
|
8
|
-
/** Plugin entry for Brave Search. */
|
|
9
8
|
var brave_default = definePluginEntry({
|
|
10
9
|
id: "brave",
|
|
11
10
|
name: "Brave Plugin",
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { t as createBraveWebSearchProvider } from "./.setup/brave-web-search-provider-
|
|
1
|
+
export { t as createBraveWebSearchProvider } from "./.setup/brave-web-search-provider-PfEIXTwN.mjs";
|
|
2
2
|
import "./web-search-provider.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { t as createBraveWebSearchProvider } from "./.setup/brave-web-search-provider-
|
|
1
|
+
export { t as createBraveWebSearchProvider } from "./.setup/brave-web-search-provider-PfEIXTwN.mjs";
|
|
@@ -1,32 +1,5 @@
|
|
|
1
|
-
import { createWebSearchProviderContractFields } from "openclaw/plugin-sdk/provider-web-search-config-contract";
|
|
2
1
|
// extensions/brave/web-search-shared.ts
|
|
3
|
-
/**
|
|
4
|
-
* Shared Brave Search provider metadata and credential lookup. Contract tests
|
|
5
|
-
* and runtime provider creation both use this lightweight descriptor.
|
|
6
|
-
*/
|
|
7
|
-
const BRAVE_CREDENTIAL_PATH = "plugins.entries.brave.config.webSearch.apiKey";
|
|
8
2
|
function resolveBraveMode(brave) {
|
|
9
3
|
return brave?.mode === "llm-context" ? "llm-context" : "web";
|
|
10
4
|
}
|
|
11
|
-
|
|
12
|
-
function buildBraveWebSearchProviderBase() {
|
|
13
|
-
return {
|
|
14
|
-
id: "brave",
|
|
15
|
-
label: "Brave Search",
|
|
16
|
-
hint: "Structured results · country/language/time filters",
|
|
17
|
-
onboardingScopes: ["text-inference"],
|
|
18
|
-
credentialLabel: "Brave Search API key",
|
|
19
|
-
envVars: ["BRAVE_API_KEY"],
|
|
20
|
-
placeholder: "BSA...",
|
|
21
|
-
signupUrl: "https://brave.com/search/api/",
|
|
22
|
-
docsUrl: "https://docs.openclaw.ai/tools/brave-search",
|
|
23
|
-
autoDetectOrder: 10,
|
|
24
|
-
credentialPath: BRAVE_CREDENTIAL_PATH,
|
|
25
|
-
...createWebSearchProviderContractFields({
|
|
26
|
-
credentialPath: BRAVE_CREDENTIAL_PATH,
|
|
27
|
-
searchCredential: { type: "top-level" },
|
|
28
|
-
configuredCredential: { pluginId: "brave" }
|
|
29
|
-
})
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
export { buildBraveWebSearchProviderBase, resolveBraveMode };
|
|
5
|
+
export { resolveBraveMode };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openclaw/brave-plugin",
|
|
3
|
-
"version": "2026.
|
|
3
|
+
"version": "2026.11.5-beta.1",
|
|
4
4
|
"description": "OpenClaw Brave Search provider plugin for web search.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -19,10 +19,10 @@
|
|
|
19
19
|
"allowInvalidConfigRecovery": true
|
|
20
20
|
},
|
|
21
21
|
"compat": {
|
|
22
|
-
"pluginApi": ">=2026.
|
|
22
|
+
"pluginApi": ">=2026.11.5-beta.1"
|
|
23
23
|
},
|
|
24
24
|
"build": {
|
|
25
|
-
"openclawVersion": "2026.
|
|
25
|
+
"openclawVersion": "2026.11.5-beta.1"
|
|
26
26
|
},
|
|
27
27
|
"release": {
|
|
28
28
|
"publishToClawHub": true,
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"assets/activity.svg"
|
|
41
41
|
],
|
|
42
42
|
"peerDependencies": {
|
|
43
|
-
"openclaw": ">=2026.
|
|
43
|
+
"openclaw": ">=2026.11.5-beta.1"
|
|
44
44
|
},
|
|
45
45
|
"peerDependenciesMeta": {
|
|
46
46
|
"openclaw": {
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import { buildBraveWebSearchProviderBase, resolveBraveMode } from "../web-search-shared.js";
|
|
2
|
-
import { isDiagnosticFlagEnabled } from "openclaw/plugin-sdk/diagnostic-flags";
|
|
3
|
-
import { createLazyRuntimeModule } from "openclaw/plugin-sdk/lazy-runtime";
|
|
4
|
-
import { mergeScopedSearchConfig, resolveProviderWebSearchPluginConfig } from "openclaw/plugin-sdk/provider-web-search-config-contract";
|
|
5
|
-
import { asOptionalRecord } from "openclaw/plugin-sdk/string-coerce-runtime";
|
|
6
|
-
// extensions/brave/src/brave-web-search-provider.ts
|
|
7
|
-
/**
|
|
8
|
-
* Brave web-search provider factory. It builds the agent tool definition and
|
|
9
|
-
* lazy-loads HTTP execution only when a search is run.
|
|
10
|
-
*/
|
|
11
|
-
const loadBraveWebSearchRuntime = createLazyRuntimeModule(() => import("./brave-web-search-provider.runtime-C-yoQ8ge.mjs"));
|
|
12
|
-
const BraveSearchSchema = {
|
|
13
|
-
type: "object",
|
|
14
|
-
properties: {
|
|
15
|
-
query: {
|
|
16
|
-
type: "string",
|
|
17
|
-
description: "Search query string."
|
|
18
|
-
},
|
|
19
|
-
count: {
|
|
20
|
-
type: "integer",
|
|
21
|
-
description: "Number of results to return (1-10).",
|
|
22
|
-
minimum: 1,
|
|
23
|
-
maximum: 10
|
|
24
|
-
},
|
|
25
|
-
country: {
|
|
26
|
-
type: "string",
|
|
27
|
-
description: "2-letter country code for region-specific results (e.g., 'DE', 'US', 'ALL'). Default: 'US'."
|
|
28
|
-
},
|
|
29
|
-
language: {
|
|
30
|
-
type: "string",
|
|
31
|
-
description: "ISO 639-1 language code for results (e.g., 'en', 'de', 'fr')."
|
|
32
|
-
},
|
|
33
|
-
freshness: {
|
|
34
|
-
type: "string",
|
|
35
|
-
description: "Filter by time: 'day' (24h), 'week', 'month', or 'year'."
|
|
36
|
-
},
|
|
37
|
-
date_after: {
|
|
38
|
-
type: "string",
|
|
39
|
-
description: "Only results published after this date (YYYY-MM-DD)."
|
|
40
|
-
},
|
|
41
|
-
date_before: {
|
|
42
|
-
type: "string",
|
|
43
|
-
description: "Only results published before this date (YYYY-MM-DD)."
|
|
44
|
-
},
|
|
45
|
-
search_lang: {
|
|
46
|
-
type: "string",
|
|
47
|
-
description: "Brave language code for search results (e.g., 'en', 'de', 'en-gb', 'zh-hans', 'zh-hant', 'pt-br')."
|
|
48
|
-
},
|
|
49
|
-
ui_lang: {
|
|
50
|
-
type: "string",
|
|
51
|
-
description: "Locale code for UI elements in language-region format (e.g., 'en-US', 'de-DE', 'fr-FR', 'tr-TR'). Must include region subtag."
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
function createBraveToolDefinition(searchConfig, config) {
|
|
56
|
-
const braveMode = resolveBraveMode(asOptionalRecord(searchConfig?.brave));
|
|
57
|
-
const diagnosticsEnabled = isDiagnosticFlagEnabled("brave.http", config);
|
|
58
|
-
return {
|
|
59
|
-
description: braveMode === "llm-context" ? "Search the web using Brave Search LLM Context API. Returns pre-extracted page content (text chunks, tables, code blocks) optimized for LLM grounding." : "Search the web using Brave Search API. Supports region-specific and localized search via country and language parameters. Returns titles, URLs, and snippets for fast research.",
|
|
60
|
-
parameters: BraveSearchSchema,
|
|
61
|
-
execute: async (args, context) => {
|
|
62
|
-
context?.signal?.throwIfAborted();
|
|
63
|
-
const { executeBraveSearch } = await loadBraveWebSearchRuntime();
|
|
64
|
-
return await executeBraveSearch(args, searchConfig, {
|
|
65
|
-
diagnosticsEnabled,
|
|
66
|
-
signal: context?.signal
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
/** Create the runtime Brave Search provider descriptor. */
|
|
72
|
-
function createBraveWebSearchProvider() {
|
|
73
|
-
return {
|
|
74
|
-
...buildBraveWebSearchProviderBase(),
|
|
75
|
-
createTool: (ctx) => createBraveToolDefinition(mergeScopedSearchConfig(ctx.searchConfig, "brave", resolveProviderWebSearchPluginConfig(ctx.config, "brave"), { mirrorApiKeyToTopLevel: true }), ctx.config)
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
export { createBraveWebSearchProvider as t };
|