
























@@ -41,8 +41,8 @@ const OLLAMA_WEB_SEARCH_SCHEMA = Type.Object(
4141{ additionalProperties: false },
4242);
434344-const OLLAMA_WEB_SEARCH_PATH = "/api/web_search";
45-const OLLAMA_LEGACY_WEB_SEARCH_PATH = "/api/experimental/web_search";
44+const OLLAMA_HOSTED_WEB_SEARCH_PATH = "/api/web_search";
45+const OLLAMA_LOCAL_WEB_SEARCH_PROXY_PATH = "/api/experimental/web_search";
4646const OLLAMA_CLOUD_BASE_URL = "https://ollama.com";
4747const DEFAULT_OLLAMA_WEB_SEARCH_COUNT = 5;
4848const DEFAULT_OLLAMA_WEB_SEARCH_TIMEOUT_MS = 15_000;
@@ -58,6 +58,12 @@ type OllamaWebSearchResponse = {
5858results?: OllamaWebSearchResult[];
5959};
606061+type OllamaWebSearchAttempt = {
62+baseUrl: string;
63+path: string;
64+apiKey?: string;
65+};
66+6167function isOllamaCloudBaseUrl(baseUrl: string): boolean {
6268try {
6369const parsed = new URL(baseUrl);
@@ -111,6 +117,43 @@ function normalizeOllamaWebSearchResult(
111117};
112118}
113119120+function buildOllamaWebSearchAttempts(params: {
121+baseUrl: string;
122+configuredApiKey?: string;
123+envApiKey?: string;
124+}): OllamaWebSearchAttempt[] {
125+if (isOllamaCloudBaseUrl(params.baseUrl)) {
126+return [
127+{
128+baseUrl: params.baseUrl,
129+path: OLLAMA_HOSTED_WEB_SEARCH_PATH,
130+apiKey: params.configuredApiKey ?? params.envApiKey,
131+},
132+];
133+}
134+135+const attempts: OllamaWebSearchAttempt[] = [
136+{
137+baseUrl: params.baseUrl,
138+path: OLLAMA_LOCAL_WEB_SEARCH_PROXY_PATH,
139+apiKey: params.configuredApiKey,
140+},
141+{
142+baseUrl: params.baseUrl,
143+path: OLLAMA_HOSTED_WEB_SEARCH_PATH,
144+apiKey: params.configuredApiKey,
145+},
146+];
147+if (params.envApiKey) {
148+attempts.push({
149+baseUrl: OLLAMA_CLOUD_BASE_URL,
150+path: OLLAMA_HOSTED_WEB_SEARCH_PATH,
151+apiKey: params.envApiKey,
152+});
153+}
154+return attempts;
155+}
156+114157export async function runOllamaWebSearch(params: {
115158config?: OpenClawConfig;
116159query: string;
@@ -127,27 +170,7 @@ export async function runOllamaWebSearch(params: {
127170const count = resolveSearchCount(params.count, DEFAULT_OLLAMA_WEB_SEARCH_COUNT);
128171const startedAt = Date.now();
129172const body = JSON.stringify({ query, max_results: count });
130-const attempts = [
131-{
132- baseUrl,
133-path: OLLAMA_WEB_SEARCH_PATH,
134-apiKey: isOllamaCloudBaseUrl(baseUrl) ? (configuredApiKey ?? envApiKey) : configuredApiKey,
135-},
136-{
137- baseUrl,
138-path: OLLAMA_LEGACY_WEB_SEARCH_PATH,
139-apiKey: isOllamaCloudBaseUrl(baseUrl) ? (configuredApiKey ?? envApiKey) : configuredApiKey,
140-},
141- ...(!isOllamaCloudBaseUrl(baseUrl) && envApiKey
142- ? [
143-{
144-baseUrl: OLLAMA_CLOUD_BASE_URL,
145-path: OLLAMA_WEB_SEARCH_PATH,
146-apiKey: envApiKey,
147-},
148-]
149- : []),
150-];
173+const attempts = buildOllamaWebSearchAttempts({ baseUrl, configuredApiKey, envApiKey });
151174152175let payload: OllamaWebSearchResponse | undefined;
153176let lastError: Error | undefined;
@@ -305,6 +328,7 @@ export function createOllamaWebSearchProvider(): WebSearchProviderPlugin {
305328}
306329307330export const __testing = {
331+ buildOllamaWebSearchAttempts,
308332 normalizeOllamaWebSearchResult,
309333 resolveConfiguredOllamaWebSearchApiKey,
310334 resolveEnvOllamaWebSearchApiKey,
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。