@openclaw/brave-plugin 2026.10.10-beta.5 → 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-Pv20V5ro.mjs → brave-web-search-provider.runtime-BJOE5Gqg.mjs} +99 -177
- package/dist/.source-module-urls/c56d5ead937f37db9ad473f25208bbdd773d745f5be1ef3863e9582e6bf06315.json +1 -1
- package/dist/index.js +2 -4
- package/dist/web-search-contract-api.js +1 -1
- package/dist/web-search-provider.js +1 -1
- package/dist/web-search-shared.js +2 -30
- package/package.json +4 -4
- package/dist/.setup/brave-web-search-provider-m9mxf4kg.mjs +0 -79
|
@@ -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
|
-
|
|
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,51 +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
|
-
//#endregion
|
|
164
|
-
//#region extensions/brave/src/brave-web-search-provider.runtime.ts
|
|
165
|
-
const DEFAULT_BRAVE_BASE_URL = "https://api.search.brave.com";
|
|
166
|
-
const BRAVE_SEARCH_ENDPOINT_PATH = "/res/v1/web/search";
|
|
167
|
-
const BRAVE_LLM_CONTEXT_ENDPOINT_PATH = "/res/v1/llm/context";
|
|
168
|
-
const braveHttpLogger = createSubsystemLogger("brave/http");
|
|
169
128
|
function logBraveHttp(diagnosticsEnabled, event, meta) {
|
|
170
129
|
if (!diagnosticsEnabled) return;
|
|
171
130
|
braveHttpLogger.info(`brave http ${event}`, meta);
|
|
172
131
|
}
|
|
173
|
-
function resolveBraveApiKey(searchConfig) {
|
|
174
|
-
return readConfiguredSecretString(searchConfig?.apiKey, "plugins.entries.brave.config.webSearch.apiKey") ?? readProviderEnvValue(["BRAVE_API_KEY"]);
|
|
175
|
-
}
|
|
176
|
-
function resolveBraveBaseUrl(braveConfig) {
|
|
177
|
-
return readConfiguredSecretString(braveConfig?.baseUrl, "plugins.entries.brave.config.webSearch.baseUrl")?.replace(/\/+$/u, "") || DEFAULT_BRAVE_BASE_URL;
|
|
178
|
-
}
|
|
179
132
|
async function braveEndpointTargetsPrivateNetwork(url, signal) {
|
|
180
133
|
if (isBlockedHostnameOrIp(url.hostname)) return true;
|
|
181
134
|
try {
|
|
@@ -209,75 +162,8 @@ async function validateBraveBaseUrl(baseUrl, signal) {
|
|
|
209
162
|
}
|
|
210
163
|
return await braveEndpointTargetsPrivateNetwork(parsed, signal) ? "selfHosted" : "strict";
|
|
211
164
|
}
|
|
212
|
-
async function runBraveSearch(params) {
|
|
213
|
-
const url = new URL(params.baseUrl);
|
|
214
|
-
url.pathname = `${url.pathname.replace(/\/+$/u, "")}${params.mode === "llm-context" ? BRAVE_LLM_CONTEXT_ENDPOINT_PATH : BRAVE_SEARCH_ENDPOINT_PATH}`;
|
|
215
|
-
url.search = "";
|
|
216
|
-
url.searchParams.set("q", params.query);
|
|
217
|
-
if (params.country) url.searchParams.set("country", params.country);
|
|
218
|
-
if (params.search_lang) url.searchParams.set("search_lang", params.search_lang);
|
|
219
|
-
if (params.freshness) url.searchParams.set("freshness", params.freshness);
|
|
220
|
-
else if (params.dateAfter && params.dateBefore) url.searchParams.set("freshness", `${params.dateAfter}to${params.dateBefore}`);
|
|
221
|
-
else if (params.dateAfter) url.searchParams.set("freshness", `${params.dateAfter}to${(/* @__PURE__ */ new Date()).toISOString().slice(0, 10)}`);
|
|
222
|
-
else if (params.mode === "web" && params.dateBefore) url.searchParams.set("freshness", `1970-01-01to${params.dateBefore}`);
|
|
223
|
-
if (params.mode === "web") {
|
|
224
|
-
url.searchParams.set("count", String(params.count));
|
|
225
|
-
if (params.ui_lang) url.searchParams.set("ui_lang", params.ui_lang);
|
|
226
|
-
}
|
|
227
|
-
logBraveHttp(params.diagnosticsEnabled, "request", {
|
|
228
|
-
mode: params.mode,
|
|
229
|
-
url: url.toString(),
|
|
230
|
-
query: url.searchParams.get("q") ?? "",
|
|
231
|
-
params: Object.fromEntries(url.searchParams.entries())
|
|
232
|
-
});
|
|
233
|
-
const startedAt = Date.now();
|
|
234
|
-
return (params.endpointMode === "selfHosted" ? withSelfHostedWebToolsEndpoint : withTrustedWebToolsEndpoint)({
|
|
235
|
-
url: url.toString(),
|
|
236
|
-
signal: params.signal,
|
|
237
|
-
init: {
|
|
238
|
-
method: "GET",
|
|
239
|
-
headers: {
|
|
240
|
-
Accept: "application/json",
|
|
241
|
-
"X-Subscription-Token": params.apiKey
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
}, async ({ response }) => {
|
|
245
|
-
logBraveHttp(params.diagnosticsEnabled, "response", {
|
|
246
|
-
mode: params.mode,
|
|
247
|
-
status: response.status,
|
|
248
|
-
ok: response.ok,
|
|
249
|
-
durationMs: Date.now() - startedAt
|
|
250
|
-
});
|
|
251
|
-
const errorLabel = params.mode === "llm-context" ? "Brave LLM Context API error" : "Brave Search API error";
|
|
252
|
-
await assertOkOrThrowProviderError(response, errorLabel);
|
|
253
|
-
if (params.mode === "llm-context") {
|
|
254
|
-
const data = await readProviderJsonResponse(response, errorLabel);
|
|
255
|
-
return {
|
|
256
|
-
mode: "llm-context",
|
|
257
|
-
results: mapBraveLlmContextResults(data),
|
|
258
|
-
sources: data.sources
|
|
259
|
-
};
|
|
260
|
-
}
|
|
261
|
-
const data = await readProviderJsonResponse(response, errorLabel);
|
|
262
|
-
return {
|
|
263
|
-
mode: "web",
|
|
264
|
-
results: (Array.isArray(data.web?.results) ? data.web.results : []).slice(0, params.count).map((entry) => {
|
|
265
|
-
const description = entry.description ?? "";
|
|
266
|
-
const title = entry.title ?? "";
|
|
267
|
-
const resultUrl = entry.url ?? "";
|
|
268
|
-
return {
|
|
269
|
-
title: title ? wrapWebContent(title, "web_search") : "",
|
|
270
|
-
url: resultUrl,
|
|
271
|
-
description: description ? wrapWebContent(description, "web_search") : "",
|
|
272
|
-
published: entry.page_age || void 0,
|
|
273
|
-
siteName: resolveSiteName(resultUrl) || void 0
|
|
274
|
-
};
|
|
275
|
-
})
|
|
276
|
-
};
|
|
277
|
-
});
|
|
278
|
-
}
|
|
279
165
|
async function executeBraveSearch(args, searchConfig, options) {
|
|
280
|
-
const apiKey =
|
|
166
|
+
const apiKey = readWebSearchApiKey(searchConfig?.apiKey, "plugins.entries.brave.config.webSearch.apiKey", ["BRAVE_API_KEY"]);
|
|
281
167
|
if (!apiKey) return {
|
|
282
168
|
error: "missing_brave_api_key",
|
|
283
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.`,
|
|
@@ -285,7 +171,7 @@ async function executeBraveSearch(args, searchConfig, options) {
|
|
|
285
171
|
};
|
|
286
172
|
const braveConfig = asNonArrayRecord(searchConfig?.brave);
|
|
287
173
|
const braveMode = resolveBraveMode(braveConfig);
|
|
288
|
-
const braveBaseUrl =
|
|
174
|
+
const braveBaseUrl = readConfiguredSecretString(braveConfig?.baseUrl, "plugins.entries.brave.config.webSearch.baseUrl")?.replace(/\/+$/u, "") || DEFAULT_BRAVE_BASE_URL;
|
|
289
175
|
const { signal, cleanup } = buildTimeoutAbortSignal({
|
|
290
176
|
timeoutMs: resolveSearchTimeoutSeconds(searchConfig) * 1e3,
|
|
291
177
|
signal: options?.signal,
|
|
@@ -296,30 +182,33 @@ async function executeBraveSearch(args, searchConfig, options) {
|
|
|
296
182
|
signal?.throwIfAborted();
|
|
297
183
|
const braveEndpointMode = await validateBraveBaseUrl(braveBaseUrl, signal);
|
|
298
184
|
signal?.throwIfAborted();
|
|
299
|
-
const query =
|
|
300
|
-
const
|
|
301
|
-
|
|
302
|
-
message: `count must be an integer from 1 to ${MAX_SEARCH_COUNT}.`
|
|
303
|
-
}) ?? searchConfig?.maxResults ?? void 0;
|
|
304
|
-
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;
|
|
305
188
|
const language = readStringParam(args, "language");
|
|
306
189
|
const search_lang = readStringParam(args, "search_lang");
|
|
307
190
|
const ui_lang = readStringParam(args, "ui_lang");
|
|
308
|
-
const
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
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 {
|
|
313
201
|
error: "invalid_search_lang",
|
|
314
202
|
message: "search_lang must be a Brave-supported language code like 'en', 'en-gb', 'zh-hans', or 'zh-hant'.",
|
|
315
203
|
docs: "https://docs.openclaw.ai/tools/web"
|
|
316
204
|
};
|
|
317
|
-
|
|
205
|
+
const normalizedUiLang = normalizeBraveUiLang(uiLangCandidate);
|
|
206
|
+
if (uiLangCandidate && !normalizedUiLang) return {
|
|
318
207
|
error: "invalid_ui_lang",
|
|
319
208
|
message: "ui_lang must be a language-region locale like 'en-US'.",
|
|
320
209
|
docs: "https://docs.openclaw.ai/tools/web"
|
|
321
210
|
};
|
|
322
|
-
if (
|
|
211
|
+
if (normalizedUiLang && braveMode === "llm-context") return {
|
|
323
212
|
error: "unsupported_ui_lang",
|
|
324
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.",
|
|
325
214
|
docs: "https://docs.openclaw.ai/tools/web"
|
|
@@ -356,13 +245,14 @@ async function executeBraveSearch(args, searchConfig, options) {
|
|
|
356
245
|
const requestedCount = resolveSearchCount(count, DEFAULT_SEARCH_COUNT);
|
|
357
246
|
const cacheKey = buildSearchCacheKey([
|
|
358
247
|
"brave",
|
|
248
|
+
sha256Hex(apiKey),
|
|
359
249
|
braveMode,
|
|
360
250
|
braveBaseUrl,
|
|
361
251
|
query,
|
|
362
252
|
requestedCount,
|
|
363
253
|
country,
|
|
364
|
-
|
|
365
|
-
...braveMode === "web" ? [
|
|
254
|
+
normalizedSearchLang,
|
|
255
|
+
...braveMode === "web" ? [normalizedUiLang] : [],
|
|
366
256
|
freshness,
|
|
367
257
|
dateAfter,
|
|
368
258
|
llmContextDateEnd
|
|
@@ -371,63 +261,96 @@ async function executeBraveSearch(args, searchConfig, options) {
|
|
|
371
261
|
const cacheTtlMs = resolveSearchCacheTtlMs(searchConfig);
|
|
372
262
|
const cached = readCachedSearchPayload(cacheKey, cacheTtlMs);
|
|
373
263
|
if (cached) {
|
|
374
|
-
logBraveHttp(diagnosticsEnabled, "cache hit", {
|
|
375
|
-
mode: braveMode,
|
|
376
|
-
query,
|
|
377
|
-
cacheKey
|
|
378
|
-
});
|
|
264
|
+
logBraveHttp(diagnosticsEnabled, "cache hit", { mode: braveMode });
|
|
379
265
|
return cached;
|
|
380
266
|
}
|
|
381
|
-
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", {
|
|
382
284
|
mode: braveMode,
|
|
383
|
-
|
|
384
|
-
|
|
285
|
+
endpoint: `${url.origin}${url.pathname}`,
|
|
286
|
+
count: requestedCount
|
|
385
287
|
});
|
|
386
|
-
const
|
|
387
|
-
const response = await
|
|
388
|
-
|
|
389
|
-
endpointMode: braveEndpointMode,
|
|
390
|
-
query,
|
|
391
|
-
apiKey,
|
|
392
|
-
diagnosticsEnabled,
|
|
288
|
+
const requestStartedAt = Date.now();
|
|
289
|
+
const response = await (braveEndpointMode === "selfHosted" ? withSelfHostedWebToolsEndpoint : withTrustedWebToolsEndpoint)({
|
|
290
|
+
url: url.toString(),
|
|
393
291
|
signal,
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
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
|
+
};
|
|
402
338
|
});
|
|
403
339
|
signal?.throwIfAborted();
|
|
404
|
-
const results = response.
|
|
405
|
-
title: entry.title ? wrapWebContent(entry.title, "web_search") : "",
|
|
406
|
-
url: entry.url,
|
|
407
|
-
snippets: entry.snippets.map((snippet) => wrapWebContent(snippet, "web_search")),
|
|
408
|
-
siteName: entry.siteName,
|
|
409
|
-
published: entry.published
|
|
410
|
-
})) : response.results;
|
|
340
|
+
const results = response.results;
|
|
411
341
|
const payload = {
|
|
412
342
|
query,
|
|
413
343
|
provider: "brave",
|
|
414
344
|
...response.mode === "llm-context" ? { mode: response.mode } : {},
|
|
415
345
|
count: results.length,
|
|
416
346
|
tookMs: Date.now() - start,
|
|
417
|
-
externalContent:
|
|
418
|
-
untrusted: true,
|
|
419
|
-
source: "web_search",
|
|
420
|
-
provider: "brave",
|
|
421
|
-
wrapped: true
|
|
422
|
-
},
|
|
347
|
+
externalContent: externalContentStamp("brave"),
|
|
423
348
|
results,
|
|
424
349
|
...response.mode === "llm-context" ? { sources: response.sources } : {}
|
|
425
350
|
};
|
|
426
351
|
writeCachedSearchPayload(cacheKey, payload, cacheTtlMs);
|
|
427
352
|
logBraveHttp(diagnosticsEnabled, "cache write", {
|
|
428
353
|
mode: response.mode,
|
|
429
|
-
query,
|
|
430
|
-
cacheKey,
|
|
431
354
|
ttlMs: cacheTtlMs,
|
|
432
355
|
count: results.length
|
|
433
356
|
});
|
|
@@ -439,5 +362,4 @@ async function executeBraveSearch(args, searchConfig, options) {
|
|
|
439
362
|
cleanup();
|
|
440
363
|
}
|
|
441
364
|
}
|
|
442
|
-
//#endregion
|
|
443
365
|
export { executeBraveSearch };
|
|
@@ -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",
|
|
@@ -14,5 +13,4 @@ var brave_default = definePluginEntry({
|
|
|
14
13
|
api.registerWebSearchProvider(createBraveWebSearchProvider());
|
|
15
14
|
}
|
|
16
15
|
});
|
|
17
|
-
//#endregion
|
|
18
16
|
export { brave_default as default };
|
|
@@ -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,33 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
//#region 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";
|
|
1
|
+
// extensions/brave/web-search-shared.ts
|
|
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
|
-
//#endregion
|
|
33
|
-
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,79 +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
|
-
//#region 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-Pv20V5ro.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
|
-
//#endregion
|
|
79
|
-
export { createBraveWebSearchProvider as t };
|