Some checks failed
ClawSweeper Dispatch / dispatch (push) Has been cancelled
CodeQL / Security High (actions) (push) Has been cancelled
CodeQL / Security High (channel-runtime-boundary) (push) Has been cancelled
CodeQL / Security High (core-auth-secrets) (push) Has been cancelled
CodeQL / Security High (mcp-process-tool-boundary) (push) Has been cancelled
CodeQL / Security High (network-ssrf-boundary) (push) Has been cancelled
CodeQL / Security High (plugin-trust-boundary) (push) Has been cancelled
CodeQL / Security High (process-exec-boundary) (push) Has been cancelled
Docs Sync Publish Repo / sync-publish-repo (push) Has been cancelled
Docs / docs (push) Has been cancelled
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Has been cancelled
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / generated-doc-baselines (push) Has been cancelled
CI / runner-admission (push) Has been cancelled
CI / preflight (push) Has been cancelled
CI / security-fast (push) Has been cancelled
CI / pnpm-store-warmup (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / native-i18n (push) Has been cancelled
CI / ${{ matrix.check_name }} (push) Has been cancelled
CI / ${{ matrix.checkName }} (push) Has been cancelled
CI / checks-node-compat-node22 (push) Has been cancelled
CI / check-bundled-channel-config-metadata (push) Has been cancelled
CI / check-dependencies (push) Has been cancelled
CI / check-guards (push) Has been cancelled
CI / check-lint (push) Has been cancelled
CI / check-prod-types (push) Has been cancelled
CI / check-shrinkwrap (push) Has been cancelled
CI / check-test-types (push) Has been cancelled
CI / check-additional-boundaries-a (push) Has been cancelled
CI / check-additional-boundaries-bcd (push) Has been cancelled
CI / check-additional-extension-bundled (push) Has been cancelled
CI / check-additional-extension-channels (push) Has been cancelled
CI / check-additional-extension-package-boundary (push) Has been cancelled
CI / check-additional-runtime-topology-architecture (push) Has been cancelled
CI / check-session-accessor-boundary (push) Has been cancelled
CI / check-session-transcript-reader-boundary (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / macos-swift (push) Has been cancelled
CI / ios-build (push) Has been cancelled
CI / ci-timings-summary (push) Has been cancelled
Native App Locale Refresh / Refresh native fa (push) Has been cancelled
Native App Locale Refresh / Refresh native fr (push) Has been cancelled
Native App Locale Refresh / Refresh native hi (push) Has been cancelled
Native App Locale Refresh / Refresh native id (push) Has been cancelled
Native App Locale Refresh / Refresh native it (push) Has been cancelled
Native App Locale Refresh / Refresh native ja-JP (push) Has been cancelled
Control UI Locale Refresh / plan (push) Has been cancelled
Control UI Locale Refresh / Refresh ${{ matrix.locale }} (push) Has been cancelled
Control UI Locale Refresh / Commit control UI locale refresh (push) Has been cancelled
Live Media Runner Image / Build live media runner image (push) Has been cancelled
Native App Locale Refresh / Refresh native ar (push) Has been cancelled
Native App Locale Refresh / Refresh native de (push) Has been cancelled
Native App Locale Refresh / Refresh native es (push) Has been cancelled
Native App Locale Refresh / Refresh native ko (push) Has been cancelled
Native App Locale Refresh / Refresh native nl (push) Has been cancelled
Native App Locale Refresh / Refresh native pl (push) Has been cancelled
Native App Locale Refresh / Refresh native pt-BR (push) Has been cancelled
Native App Locale Refresh / Refresh native ru (push) Has been cancelled
Native App Locale Refresh / Refresh native sv (push) Has been cancelled
Native App Locale Refresh / Refresh native th (push) Has been cancelled
Native App Locale Refresh / Refresh native tr (push) Has been cancelled
Native App Locale Refresh / Refresh native uk (push) Has been cancelled
Native App Locale Refresh / Refresh native vi (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-CN (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-TW (push) Has been cancelled
Native App Locale Refresh / Commit native locale refresh (push) Has been cancelled
Plugin Init Scaffold Validation / Validate provider scaffold (push) Has been cancelled
Plugin NPM Release / preview_plugins_npm (push) Has been cancelled
Plugin NPM Release / Validate release publish approval (push) Has been cancelled
Plugin NPM Release / preview_plugin_pack (push) Has been cancelled
Plugin NPM Release / publish_plugins_npm (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Website Installer Sync / static (push) Has been cancelled
Website Installer Sync / linux-docker (push) Has been cancelled
Website Installer Sync / macos-installer (push) Has been cancelled
Website Installer Sync / windows-installer (push) Has been cancelled
Website Installer Sync / sync-website (push) Has been cancelled
Adolf is a fork/vendored clone of github.com/openclaw/openclaw (v2026.6.11), free to diverge. Tree copied sans upstream .git; upstream remote added for future syncs. Node pinned to 24 (.nvmrc); engines already require >=22.19. Preserves docs/ARCHITECTURE.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LeqyaxJF2nbRXJtae2kNB2
516 lines
15 KiB
TypeScript
516 lines
15 KiB
TypeScript
// Ollama tests cover web search provider plugin behavior.
|
|
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { createStreamingResponse } from "../../test-support/streaming-error-response.js";
|
|
import { createOllamaWebSearchProvider as createContractOllamaWebSearchProvider } from "../web-search-contract-api.js";
|
|
import {
|
|
testing,
|
|
createOllamaWebSearchProvider,
|
|
runOllamaWebSearch,
|
|
} from "./web-search-provider.js";
|
|
|
|
const { fetchWithSsrFGuardMock } = vi.hoisted(() => ({
|
|
fetchWithSsrFGuardMock: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("openclaw/plugin-sdk/ssrf-runtime", () => ({
|
|
fetchWithSsrFGuard: fetchWithSsrFGuardMock,
|
|
}));
|
|
|
|
type OllamaProviderConfigOverride = Partial<{
|
|
api: "ollama";
|
|
apiKey: string;
|
|
baseUrl: string;
|
|
baseURL: string;
|
|
models: NonNullable<
|
|
NonNullable<NonNullable<OpenClawConfig["models"]>["providers"]>[string]
|
|
>["models"];
|
|
}>;
|
|
|
|
function createOllamaConfig(provider: OllamaProviderConfigOverride = {}): OpenClawConfig {
|
|
return {
|
|
models: {
|
|
providers: {
|
|
ollama: {
|
|
baseUrl: "http://ollama.local:11434/v1",
|
|
api: "ollama",
|
|
models: [],
|
|
...provider,
|
|
},
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
function createOllamaConfigWithWebSearchBaseUrl(baseUrl: string): OpenClawConfig {
|
|
return {
|
|
...createOllamaConfig(),
|
|
plugins: {
|
|
entries: {
|
|
ollama: {
|
|
config: {
|
|
webSearch: {
|
|
baseUrl,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
function createSetupNotes() {
|
|
const notes: Array<{ title?: string; message: string }> = [];
|
|
return {
|
|
notes,
|
|
prompter: {
|
|
note: async (message: string, title?: string) => {
|
|
notes.push({ title, message });
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
function expectOllamaWebSearchRequest(
|
|
call: unknown[] | undefined,
|
|
params: {
|
|
url: string;
|
|
query?: string;
|
|
maxResults?: number;
|
|
headers?: Record<string, string>;
|
|
policy: Record<string, unknown>;
|
|
},
|
|
) {
|
|
if (!call?.[0] || typeof call[0] !== "object") {
|
|
throw new Error("Expected fetchWithSsrFGuard call");
|
|
}
|
|
const request = call[0] as {
|
|
url: string;
|
|
init: {
|
|
method: string;
|
|
headers: Record<string, string>;
|
|
body: string;
|
|
signal: AbortSignal;
|
|
};
|
|
policy: Record<string, unknown>;
|
|
auditContext: string;
|
|
};
|
|
expect(request).toEqual({
|
|
url: params.url,
|
|
init: {
|
|
method: "POST",
|
|
headers: params.headers ?? { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
query: params.query ?? "openclaw",
|
|
max_results: params.maxResults ?? 5,
|
|
}),
|
|
signal: request.init.signal,
|
|
},
|
|
policy: params.policy,
|
|
auditContext: "ollama-web-search.search",
|
|
});
|
|
expect(request.init.signal).toBeInstanceOf(AbortSignal);
|
|
}
|
|
|
|
function fetchCall(index = 0): unknown[] {
|
|
const call = fetchWithSsrFGuardMock.mock.calls.at(index);
|
|
if (!call) {
|
|
throw new Error(`expected guarded fetch call ${index}`);
|
|
}
|
|
return call;
|
|
}
|
|
|
|
function fetchRequest(index = 0): {
|
|
init?: { headers?: Record<string, string> };
|
|
url?: string;
|
|
} {
|
|
const request = fetchCall(index).at(0);
|
|
if (!request || typeof request !== "object") {
|
|
throw new Error(`expected guarded fetch request ${index}`);
|
|
}
|
|
return request as {
|
|
init?: { headers?: Record<string, string> };
|
|
url?: string;
|
|
};
|
|
}
|
|
|
|
function expectSingleSearchResultUrl(results: unknown, url: string) {
|
|
if (!Array.isArray(results)) {
|
|
throw new Error("Expected search results array");
|
|
}
|
|
expect(results).toHaveLength(1);
|
|
const [result] = results;
|
|
if (!result || typeof result !== "object") {
|
|
throw new Error("Expected search result object");
|
|
}
|
|
expect((result as { url?: unknown }).url).toBe(url);
|
|
}
|
|
|
|
describe("ollama web search provider", () => {
|
|
beforeEach(() => {
|
|
fetchWithSsrFGuardMock.mockReset();
|
|
});
|
|
|
|
it("registers a keyless web search provider", () => {
|
|
const provider = createContractOllamaWebSearchProvider();
|
|
|
|
expect(provider.id).toBe("ollama");
|
|
expect(provider.label).toBe("Ollama Web Search");
|
|
expect(provider.requiresCredential).toBe(false);
|
|
expect(provider.envVars).toEqual([]);
|
|
});
|
|
|
|
it("uses the configured Ollama host and enables the plugin in config", () => {
|
|
const provider = createOllamaWebSearchProvider();
|
|
if (!provider.applySelectionConfig) {
|
|
throw new Error("Expected applySelectionConfig to be defined");
|
|
}
|
|
|
|
const applied = provider.applySelectionConfig({});
|
|
|
|
expect(provider.credentialPath).toBe("");
|
|
expect(applied.plugins?.entries?.ollama?.enabled).toBe(true);
|
|
expect(
|
|
testing.resolveOllamaWebSearchBaseUrl({
|
|
models: {
|
|
providers: {
|
|
ollama: {
|
|
baseUrl: "http://ollama.local:11434/v1",
|
|
api: "ollama",
|
|
models: [],
|
|
},
|
|
},
|
|
},
|
|
}),
|
|
).toBe("http://ollama.local:11434");
|
|
});
|
|
|
|
it("prefers the plugin web search base URL over the model provider host", () => {
|
|
expect(
|
|
testing.resolveOllamaWebSearchBaseUrl(
|
|
createOllamaConfigWithWebSearchBaseUrl("http://localhost:11434/v1"),
|
|
),
|
|
).toBe("http://localhost:11434");
|
|
});
|
|
|
|
it("uses the configured Ollama Cloud host for web search", () => {
|
|
expect(
|
|
testing.resolveOllamaWebSearchBaseUrl(
|
|
createOllamaConfig({
|
|
baseUrl: "https://ollama.com",
|
|
}),
|
|
),
|
|
).toBe("https://ollama.com");
|
|
});
|
|
|
|
it("uses the model provider baseURL alias for web search", () => {
|
|
expect(
|
|
testing.resolveOllamaWebSearchBaseUrl(
|
|
createOllamaConfig({
|
|
baseUrl: undefined,
|
|
baseURL: "http://remote-ollama:11434/v1",
|
|
} as OllamaProviderConfigOverride),
|
|
),
|
|
).toBe("http://remote-ollama:11434");
|
|
});
|
|
|
|
it("maps generic search args into the local Ollama proxy endpoint", async () => {
|
|
const release = vi.fn(async () => {});
|
|
fetchWithSsrFGuardMock.mockResolvedValue({
|
|
response: new Response(
|
|
JSON.stringify({
|
|
results: [
|
|
{
|
|
title: "OpenClaw",
|
|
url: "https://openclaw.ai/docs",
|
|
content: "Gateway docs and setup details",
|
|
},
|
|
],
|
|
}),
|
|
{
|
|
status: 200,
|
|
headers: { "Content-Type": "application/json" },
|
|
},
|
|
),
|
|
release,
|
|
});
|
|
|
|
const provider = createOllamaWebSearchProvider();
|
|
const tool = provider.createTool({
|
|
config: createOllamaConfig(),
|
|
} as never);
|
|
if (!tool) {
|
|
throw new Error("Expected tool definition");
|
|
}
|
|
const result = await tool.execute({ query: "openclaw docs", count: 3 });
|
|
|
|
expectOllamaWebSearchRequest(fetchCall(), {
|
|
url: "http://ollama.local:11434/api/experimental/web_search",
|
|
query: "openclaw docs",
|
|
maxResults: 3,
|
|
policy: {
|
|
allowPrivateNetwork: true,
|
|
hostnameAllowlist: ["ollama.local"],
|
|
},
|
|
});
|
|
expect(result.query).toBe("openclaw docs");
|
|
expect(result.provider).toBe("ollama");
|
|
expect(result.count).toBe(1);
|
|
expectSingleSearchResultUrl(result.results, "https://openclaw.ai/docs");
|
|
expect(release).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("tries the future local direct endpoint when the local proxy endpoint is missing", async () => {
|
|
fetchWithSsrFGuardMock
|
|
.mockResolvedValueOnce({
|
|
response: new Response("not found", { status: 404 }),
|
|
release: vi.fn(async () => {}),
|
|
})
|
|
.mockResolvedValueOnce({
|
|
response: new Response(
|
|
JSON.stringify({
|
|
results: [{ title: "Legacy", url: "https://example.com", content: "result" }],
|
|
}),
|
|
{
|
|
status: 200,
|
|
headers: { "Content-Type": "application/json" },
|
|
},
|
|
),
|
|
release: vi.fn(async () => {}),
|
|
});
|
|
|
|
const result = await runOllamaWebSearch({
|
|
config: createOllamaConfig(),
|
|
query: "openclaw",
|
|
});
|
|
|
|
expect(result.count).toBe(1);
|
|
expectSingleSearchResultUrl(result.results, "https://example.com");
|
|
|
|
expect(fetchWithSsrFGuardMock.mock.calls.map((call) => call[0].url)).toEqual([
|
|
"http://ollama.local:11434/api/experimental/web_search",
|
|
"http://ollama.local:11434/api/web_search",
|
|
]);
|
|
});
|
|
|
|
it("uses only the hosted endpoint for Ollama Cloud base URLs", async () => {
|
|
fetchWithSsrFGuardMock.mockResolvedValueOnce({
|
|
response: new Response(
|
|
JSON.stringify({
|
|
results: [{ title: "Cloud", url: "https://example.com", content: "result" }],
|
|
}),
|
|
{
|
|
status: 200,
|
|
headers: { "Content-Type": "application/json" },
|
|
},
|
|
),
|
|
release: vi.fn(async () => {}),
|
|
});
|
|
|
|
const result = await runOllamaWebSearch({
|
|
config: createOllamaConfig({
|
|
baseUrl: "https://ollama.com",
|
|
apiKey: "cloud-config-secret",
|
|
}),
|
|
query: "openclaw",
|
|
});
|
|
|
|
expect(result.count).toBe(1);
|
|
expect(fetchWithSsrFGuardMock.mock.calls).toHaveLength(1);
|
|
expect(fetchRequest().url).toBe("https://ollama.com/api/web_search");
|
|
expectOllamaWebSearchRequest(fetchCall(), {
|
|
url: "https://ollama.com/api/web_search",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: "Bearer cloud-config-secret",
|
|
},
|
|
policy: {
|
|
allowPrivateNetwork: true,
|
|
hostnameAllowlist: ["ollama.com"],
|
|
},
|
|
});
|
|
});
|
|
|
|
it("uses an env Ollama key only for the cloud fallback from a local host", async () => {
|
|
const original = process.env.OLLAMA_API_KEY;
|
|
try {
|
|
process.env.OLLAMA_API_KEY = "cloud-secret";
|
|
fetchWithSsrFGuardMock
|
|
.mockResolvedValueOnce({
|
|
response: new Response("not found", { status: 404 }),
|
|
release: vi.fn(async () => {}),
|
|
})
|
|
.mockResolvedValueOnce({
|
|
response: new Response("not found", { status: 404 }),
|
|
release: vi.fn(async () => {}),
|
|
})
|
|
.mockResolvedValueOnce({
|
|
response: new Response(
|
|
JSON.stringify({
|
|
results: [{ title: "Cloud", url: "https://example.com", content: "result" }],
|
|
}),
|
|
{
|
|
status: 200,
|
|
headers: { "Content-Type": "application/json" },
|
|
},
|
|
),
|
|
release: vi.fn(async () => {}),
|
|
});
|
|
|
|
const result = await runOllamaWebSearch({
|
|
config: createOllamaConfig(),
|
|
query: "openclaw",
|
|
});
|
|
|
|
expect(result.count).toBe(1);
|
|
const firstHeaders = fetchRequest().init?.headers;
|
|
const cloudHeaders = fetchRequest(2).init?.headers;
|
|
expect(firstHeaders?.Authorization).toBeUndefined();
|
|
expect(cloudHeaders?.Authorization).toBe("Bearer cloud-secret");
|
|
expect(fetchWithSsrFGuardMock.mock.calls.map((call) => call[0].url)).toEqual([
|
|
"http://ollama.local:11434/api/experimental/web_search",
|
|
"http://ollama.local:11434/api/web_search",
|
|
"https://ollama.com/api/web_search",
|
|
]);
|
|
expect(fetchRequest(2).url).toBe("https://ollama.com/api/web_search");
|
|
} finally {
|
|
if (original === undefined) {
|
|
delete process.env.OLLAMA_API_KEY;
|
|
} else {
|
|
process.env.OLLAMA_API_KEY = original;
|
|
}
|
|
}
|
|
});
|
|
|
|
it("surfaces Ollama signin guidance for 401 responses", async () => {
|
|
fetchWithSsrFGuardMock.mockResolvedValue({
|
|
response: new Response("", { status: 401 }),
|
|
release: vi.fn(async () => {}),
|
|
});
|
|
|
|
await expect(runOllamaWebSearch({ query: "latest openclaw release" })).rejects.toThrow(
|
|
"ollama signin",
|
|
);
|
|
});
|
|
|
|
it("reports malformed Ollama web search JSON with a stable provider error", async () => {
|
|
fetchWithSsrFGuardMock.mockResolvedValueOnce({
|
|
response: new Response("{ nope", { status: 200 }),
|
|
release: vi.fn(async () => {}),
|
|
});
|
|
|
|
await expect(
|
|
runOllamaWebSearch({
|
|
config: createOllamaConfig(),
|
|
query: "openclaw",
|
|
}),
|
|
).rejects.toThrow("Ollama web search: malformed JSON response");
|
|
});
|
|
|
|
it("bounds successful Ollama web search JSON bodies before parsing", async () => {
|
|
const streamed = createStreamingResponse({
|
|
chunkCount: 32,
|
|
chunkSize: 1024 * 1024,
|
|
text: "x",
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
const jsonSpy = vi.spyOn(streamed.response, "json").mockRejectedValue(new Error("unbounded"));
|
|
fetchWithSsrFGuardMock.mockResolvedValueOnce({
|
|
response: streamed.response,
|
|
release: vi.fn(async () => {}),
|
|
});
|
|
|
|
await expect(
|
|
runOllamaWebSearch({
|
|
config: createOllamaConfig(),
|
|
query: "openclaw",
|
|
}),
|
|
).rejects.toThrow("Ollama web search: JSON response exceeds 16777216 bytes");
|
|
|
|
expect(streamed.getReadCount()).toBeLessThan(32);
|
|
expect(streamed.wasCanceled()).toBe(true);
|
|
expect(jsonSpy).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("warns when Ollama is not reachable during setup without cancelling", async () => {
|
|
fetchWithSsrFGuardMock.mockRejectedValueOnce(new Error("connect failed"));
|
|
|
|
const config = createOllamaConfig();
|
|
const { notes, prompter } = createSetupNotes();
|
|
|
|
const next = await testing.warnOllamaWebSearchPrereqs({
|
|
config,
|
|
prompter,
|
|
});
|
|
|
|
expect(next).toBe(config);
|
|
expect(notes).toEqual([
|
|
{
|
|
title: "Ollama Web Search",
|
|
message: [
|
|
"Ollama Web Search requires Ollama to be running.",
|
|
"Expected host: http://ollama.local:11434",
|
|
"Start Ollama before using this provider.",
|
|
].join("\n"),
|
|
},
|
|
]);
|
|
});
|
|
|
|
it("resolves env var when config apiKey is a marker string", () => {
|
|
const original = process.env.OLLAMA_API_KEY;
|
|
try {
|
|
process.env.OLLAMA_API_KEY = "real-secret-from-env";
|
|
const key = testing.resolveOllamaWebSearchApiKey(
|
|
createOllamaConfig({
|
|
apiKey: "OLLAMA_API_KEY",
|
|
baseUrl: "http://localhost:11434",
|
|
}),
|
|
);
|
|
expect(key).toBe("real-secret-from-env");
|
|
} finally {
|
|
if (original === undefined) {
|
|
delete process.env.OLLAMA_API_KEY;
|
|
} else {
|
|
process.env.OLLAMA_API_KEY = original;
|
|
}
|
|
}
|
|
});
|
|
|
|
it("warns when ollama signin is missing during setup without cancelling", async () => {
|
|
fetchWithSsrFGuardMock
|
|
.mockResolvedValueOnce({
|
|
response: new Response(JSON.stringify({ models: [] }), {
|
|
status: 200,
|
|
headers: { "Content-Type": "application/json" },
|
|
}),
|
|
release: vi.fn(async () => {}),
|
|
})
|
|
.mockResolvedValueOnce({
|
|
response: new Response(
|
|
JSON.stringify({ error: "not signed in", signin_url: "https://ollama.com/signin" }),
|
|
{
|
|
status: 401,
|
|
headers: { "Content-Type": "application/json" },
|
|
},
|
|
),
|
|
release: vi.fn(async () => {}),
|
|
});
|
|
|
|
const config = createOllamaConfig();
|
|
const { notes, prompter } = createSetupNotes();
|
|
|
|
const next = await testing.warnOllamaWebSearchPrereqs({
|
|
config,
|
|
prompter,
|
|
});
|
|
|
|
expect(next).toBe(config);
|
|
expect(notes).toEqual([
|
|
{
|
|
title: "Ollama Web Search",
|
|
message: "Ollama Web Search requires `ollama signin`.\nhttps://ollama.com/signin",
|
|
},
|
|
]);
|
|
});
|
|
});
|