add: proxy config for brave search api
This commit is contained in:
parent
6af205a13a
commit
6b331916ec
@ -1,4 +1,5 @@
|
|||||||
import { Type } from "@sinclair/typebox";
|
import { Type } from "@sinclair/typebox";
|
||||||
|
import { ProxyAgent } from "undici";
|
||||||
|
|
||||||
import type { OpenClawConfig } from "../../config/config.js";
|
import type { OpenClawConfig } from "../../config/config.js";
|
||||||
import { formatCliCommand } from "../../cli/command-format.js";
|
import { formatCliCommand } from "../../cli/command-format.js";
|
||||||
@ -122,6 +123,11 @@ function resolveSearchApiKey(search?: WebSearchConfig): string | undefined {
|
|||||||
return fromConfig || fromEnv || undefined;
|
return fromConfig || fromEnv || undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function resolveProxy(search?: WebSearchConfig): string | undefined {
|
||||||
|
const fromConfig = search && "proxy" in search && typeof search.proxy === "string" ? search.proxy.trim() : "";
|
||||||
|
return fromConfig || undefined;
|
||||||
|
}
|
||||||
|
|
||||||
function missingSearchKeyPayload(provider: (typeof SEARCH_PROVIDERS)[number]) {
|
function missingSearchKeyPayload(provider: (typeof SEARCH_PROVIDERS)[number]) {
|
||||||
if (provider === "perplexity") {
|
if (provider === "perplexity") {
|
||||||
return {
|
return {
|
||||||
@ -271,9 +277,12 @@ async function runPerplexitySearch(params: {
|
|||||||
baseUrl: string;
|
baseUrl: string;
|
||||||
model: string;
|
model: string;
|
||||||
timeoutSeconds: number;
|
timeoutSeconds: number;
|
||||||
|
proxy?: string;
|
||||||
}): Promise<{ content: string; citations: string[] }> {
|
}): Promise<{ content: string; citations: string[] }> {
|
||||||
const endpoint = `${params.baseUrl.replace(/\/$/, "")}/chat/completions`;
|
const endpoint = `${params.baseUrl.replace(/\/$/, "")}/chat/completions`;
|
||||||
|
|
||||||
|
const dispatcher = params.proxy ? new ProxyAgent(params.proxy) : undefined;
|
||||||
|
|
||||||
const res = await fetch(endpoint, {
|
const res = await fetch(endpoint, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
@ -292,6 +301,7 @@ async function runPerplexitySearch(params: {
|
|||||||
],
|
],
|
||||||
}),
|
}),
|
||||||
signal: withTimeout(undefined, params.timeoutSeconds * 1000),
|
signal: withTimeout(undefined, params.timeoutSeconds * 1000),
|
||||||
|
dispatcher,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
@ -319,6 +329,7 @@ async function runWebSearch(params: {
|
|||||||
freshness?: string;
|
freshness?: string;
|
||||||
perplexityBaseUrl?: string;
|
perplexityBaseUrl?: string;
|
||||||
perplexityModel?: string;
|
perplexityModel?: string;
|
||||||
|
proxy?: string;
|
||||||
}): Promise<Record<string, unknown>> {
|
}): Promise<Record<string, unknown>> {
|
||||||
const cacheKey = normalizeCacheKey(
|
const cacheKey = normalizeCacheKey(
|
||||||
params.provider === "brave"
|
params.provider === "brave"
|
||||||
@ -337,6 +348,7 @@ async function runWebSearch(params: {
|
|||||||
baseUrl: params.perplexityBaseUrl ?? DEFAULT_PERPLEXITY_BASE_URL,
|
baseUrl: params.perplexityBaseUrl ?? DEFAULT_PERPLEXITY_BASE_URL,
|
||||||
model: params.perplexityModel ?? DEFAULT_PERPLEXITY_MODEL,
|
model: params.perplexityModel ?? DEFAULT_PERPLEXITY_MODEL,
|
||||||
timeoutSeconds: params.timeoutSeconds,
|
timeoutSeconds: params.timeoutSeconds,
|
||||||
|
proxy: params.proxy,
|
||||||
});
|
});
|
||||||
|
|
||||||
const payload = {
|
const payload = {
|
||||||
@ -371,6 +383,8 @@ async function runWebSearch(params: {
|
|||||||
url.searchParams.set("freshness", params.freshness);
|
url.searchParams.set("freshness", params.freshness);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const dispatcher = params.proxy ? new ProxyAgent(params.proxy) : undefined;
|
||||||
|
|
||||||
const res = await fetch(url.toString(), {
|
const res = await fetch(url.toString(), {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: {
|
headers: {
|
||||||
@ -378,6 +392,7 @@ async function runWebSearch(params: {
|
|||||||
"X-Subscription-Token": params.apiKey,
|
"X-Subscription-Token": params.apiKey,
|
||||||
},
|
},
|
||||||
signal: withTimeout(undefined, params.timeoutSeconds * 1000),
|
signal: withTimeout(undefined, params.timeoutSeconds * 1000),
|
||||||
|
dispatcher,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
@ -476,6 +491,7 @@ export function createWebSearchTool(options?: {
|
|||||||
perplexityAuth?.apiKey,
|
perplexityAuth?.apiKey,
|
||||||
),
|
),
|
||||||
perplexityModel: resolvePerplexityModel(perplexityConfig),
|
perplexityModel: resolvePerplexityModel(perplexityConfig),
|
||||||
|
proxy: resolveProxy(search),
|
||||||
});
|
});
|
||||||
return jsonResult(result);
|
return jsonResult(result);
|
||||||
},
|
},
|
||||||
|
|||||||
@ -170,6 +170,7 @@ export const ToolsWebSearchSchema = z
|
|||||||
maxResults: z.number().int().positive().optional(),
|
maxResults: z.number().int().positive().optional(),
|
||||||
timeoutSeconds: z.number().int().positive().optional(),
|
timeoutSeconds: z.number().int().positive().optional(),
|
||||||
cacheTtlMinutes: z.number().nonnegative().optional(),
|
cacheTtlMinutes: z.number().nonnegative().optional(),
|
||||||
|
proxy: z.string().optional(),
|
||||||
perplexity: z
|
perplexity: z
|
||||||
.object({
|
.object({
|
||||||
apiKey: z.string().optional(),
|
apiKey: z.string().optional(),
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user