
















@@ -18,6 +18,7 @@ import {
1818wrapWebContent,
1919writeCachedSearchPayload,
2020} from "openclaw/plugin-sdk/provider-web-search";
21+import { createSubsystemLogger } from "openclaw/plugin-sdk/runtime-env";
2122import {
2223type BraveLlmContextResponse,
2324mapBraveLlmContextResults,
@@ -29,6 +30,7 @@ import {
29303031const BRAVE_SEARCH_ENDPOINT = "https://api.search.brave.com/res/v1/web/search";
3132const BRAVE_LLM_CONTEXT_ENDPOINT = "https://api.search.brave.com/res/v1/llm/context";
33+const braveHttpLogger = createSubsystemLogger("brave/http");
32343335type BraveSearchResult = {
3436title?: string;
@@ -43,6 +45,33 @@ type BraveSearchResponse = {
4345};
4446};
454748+type BraveHttpDiagnostics = {
49+enabled?: boolean;
50+};
51+52+function logBraveHttp(
53+diagnostics: BraveHttpDiagnostics | undefined,
54+event: string,
55+meta?: Record<string, unknown>,
56+): void {
57+if (!diagnostics?.enabled) {
58+return;
59+}
60+braveHttpLogger.info(`brave http ${event}`, meta);
61+}
62+63+function describeBraveRequestUrl(url: URL): {
64+url: string;
65+query: string;
66+params: Record<string, string>;
67+} {
68+return {
69+url: url.toString(),
70+query: url.searchParams.get("q") ?? "",
71+params: Object.fromEntries(url.searchParams.entries()),
72+};
73+}
74+4675function resolveBraveApiKey(searchConfig?: SearchConfigRecord): string | undefined {
4776return (
4877readConfiguredSecretString(searchConfig?.apiKey, "tools.web.search.apiKey") ??
@@ -62,6 +91,7 @@ async function runBraveLlmContextSearch(params: {
6291query: string;
6392apiKey: string;
6493timeoutSeconds: number;
94+diagnostics?: BraveHttpDiagnostics;
6595country?: string;
6696search_lang?: string;
6797freshness?: string;
@@ -95,6 +125,11 @@ async function runBraveLlmContextSearch(params: {
95125);
96126}
97127128+logBraveHttp(params.diagnostics, "request", {
129+mode: "llm-context",
130+ ...describeBraveRequestUrl(url),
131+});
132+const startedAt = Date.now();
98133return withTrustedWebSearchEndpoint(
99134{
100135url: url.toString(),
@@ -108,6 +143,12 @@ async function runBraveLlmContextSearch(params: {
108143},
109144},
110145async (response) => {
146+logBraveHttp(params.diagnostics, "response", {
147+mode: "llm-context",
148+status: response.status,
149+ok: response.ok,
150+durationMs: Date.now() - startedAt,
151+});
111152if (!response.ok) {
112153const detail = await response.text();
113154throw new Error(
@@ -126,6 +167,7 @@ async function runBraveWebSearch(params: {
126167count: number;
127168apiKey: string;
128169timeoutSeconds: number;
170+diagnostics?: BraveHttpDiagnostics;
129171country?: string;
130172search_lang?: string;
131173ui_lang?: string;
@@ -158,6 +200,11 @@ async function runBraveWebSearch(params: {
158200url.searchParams.set("freshness", `1970-01-01to${params.dateBefore}`);
159201}
160202203+logBraveHttp(params.diagnostics, "request", {
204+mode: "web",
205+ ...describeBraveRequestUrl(url),
206+});
207+const startedAt = Date.now();
161208return withTrustedWebSearchEndpoint(
162209{
163210url: url.toString(),
@@ -171,6 +218,12 @@ async function runBraveWebSearch(params: {
171218},
172219},
173220async (response) => {
221+logBraveHttp(params.diagnostics, "response", {
222+mode: "web",
223+status: response.status,
224+ok: response.ok,
225+durationMs: Date.now() - startedAt,
226+});
174227if (!response.ok) {
175228const detail = await response.text();
176229throw new Error(
@@ -199,6 +252,9 @@ async function runBraveWebSearch(params: {
199252export async function executeBraveSearch(
200253args: Record<string, unknown>,
201254searchConfig?: SearchConfigRecord,
255+options?: {
256+diagnosticsEnabled?: boolean;
257+},
202258): Promise<Record<string, unknown>> {
203259const apiKey = resolveBraveApiKey(searchConfig);
204260if (!apiKey) {
@@ -322,10 +378,13 @@ export async function executeBraveSearch(
322378dateBefore,
323379],
324380);
381+const diagnostics: BraveHttpDiagnostics = { enabled: options?.diagnosticsEnabled === true };
325382const cached = readCachedSearchPayload(cacheKey);
326383if (cached) {
384+logBraveHttp(diagnostics, "cache hit", { mode: braveMode, query, cacheKey });
327385return cached;
328386}
387+logBraveHttp(diagnostics, "cache miss", { mode: braveMode, query, cacheKey });
329388330389const start = Date.now();
331390const timeoutSeconds = resolveSearchTimeoutSeconds(searchConfig);
@@ -336,6 +395,7 @@ export async function executeBraveSearch(
336395 query,
337396 apiKey,
338397 timeoutSeconds,
398+ diagnostics,
339399country: country ?? undefined,
340400search_lang: normalizedLanguage.search_lang,
341401 freshness,
@@ -363,6 +423,13 @@ export async function executeBraveSearch(
363423 sources,
364424};
365425writeCachedSearchPayload(cacheKey, payload, cacheTtlMs);
426+logBraveHttp(diagnostics, "cache write", {
427+mode: "llm-context",
428+ query,
429+ cacheKey,
430+ttlMs: cacheTtlMs,
431+count: results.length,
432+});
366433return payload;
367434}
368435@@ -371,6 +438,7 @@ export async function executeBraveSearch(
371438count: resolveSearchCount(count, DEFAULT_SEARCH_COUNT),
372439 apiKey,
373440 timeoutSeconds,
441+ diagnostics,
374442country: country ?? undefined,
375443search_lang: normalizedLanguage.search_lang,
376444ui_lang: normalizedLanguage.ui_lang,
@@ -392,5 +460,12 @@ export async function executeBraveSearch(
392460 results,
393461};
394462writeCachedSearchPayload(cacheKey, payload, cacheTtlMs);
463+logBraveHttp(diagnostics, "cache write", {
464+mode: "web",
465+ query,
466+ cacheKey,
467+ttlMs: cacheTtlMs,
468+count: results.length,
469+});
395470return payload;
396471}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。