mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-03 14:40:22 +00:00
feat(plugins): add web search runtime capability
This commit is contained in:
@@ -2,14 +2,8 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { DEFAULT_MODEL, DEFAULT_PROVIDER } from "../../agents/defaults.js";
|
||||
import { onAgentEvent } from "../../infra/agent-events.js";
|
||||
import { requestHeartbeatNow } from "../../infra/heartbeat-wake.js";
|
||||
import * as execModule from "../../process/exec.js";
|
||||
import { onSessionTranscriptUpdate } from "../../sessions/transcript-events.js";
|
||||
|
||||
const runCommandWithTimeoutMock = vi.hoisted(() => vi.fn());
|
||||
|
||||
vi.mock("../../process/exec.js", () => ({
|
||||
runCommandWithTimeout: (...args: unknown[]) => runCommandWithTimeoutMock(...args),
|
||||
}));
|
||||
|
||||
import {
|
||||
clearGatewaySubagentRuntime,
|
||||
createPluginRuntime,
|
||||
@@ -18,20 +12,24 @@ import {
|
||||
|
||||
describe("plugin runtime command execution", () => {
|
||||
beforeEach(() => {
|
||||
runCommandWithTimeoutMock.mockClear();
|
||||
vi.restoreAllMocks();
|
||||
clearGatewaySubagentRuntime();
|
||||
});
|
||||
|
||||
it("exposes runtime.system.runCommandWithTimeout by default", async () => {
|
||||
const commandResult = {
|
||||
pid: 12345,
|
||||
stdout: "hello\n",
|
||||
stderr: "",
|
||||
code: 0,
|
||||
signal: null,
|
||||
killed: false,
|
||||
noOutputTimedOut: false,
|
||||
termination: "exit" as const,
|
||||
};
|
||||
runCommandWithTimeoutMock.mockResolvedValue(commandResult);
|
||||
const runCommandWithTimeoutMock = vi
|
||||
.spyOn(execModule, "runCommandWithTimeout")
|
||||
.mockResolvedValue(commandResult);
|
||||
|
||||
const runtime = createPluginRuntime();
|
||||
await expect(
|
||||
@@ -41,7 +39,9 @@ describe("plugin runtime command execution", () => {
|
||||
});
|
||||
|
||||
it("forwards runtime.system.runCommandWithTimeout errors", async () => {
|
||||
runCommandWithTimeoutMock.mockRejectedValue(new Error("boom"));
|
||||
const runCommandWithTimeoutMock = vi
|
||||
.spyOn(execModule, "runCommandWithTimeout")
|
||||
.mockRejectedValue(new Error("boom"));
|
||||
const runtime = createPluginRuntime();
|
||||
await expect(
|
||||
runtime.system.runCommandWithTimeout(["echo", "hello"], { timeoutMs: 1000 }),
|
||||
@@ -63,6 +63,12 @@ describe("plugin runtime command execution", () => {
|
||||
expect(runtime.mediaUnderstanding.transcribeAudioFile).toBe(runtime.stt.transcribeAudioFile);
|
||||
});
|
||||
|
||||
it("exposes runtime.webSearch helpers", () => {
|
||||
const runtime = createPluginRuntime();
|
||||
expect(typeof runtime.webSearch.listProviders).toBe("function");
|
||||
expect(typeof runtime.webSearch.search).toBe("function");
|
||||
});
|
||||
|
||||
it("exposes runtime.system.requestHeartbeatNow", () => {
|
||||
const runtime = createPluginRuntime();
|
||||
expect(runtime.system.requestHeartbeatNow).toBe(requestHeartbeatNow);
|
||||
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
transcribeAudioFile,
|
||||
} from "../../media-understanding/runtime.js";
|
||||
import { listSpeechVoices, textToSpeech, textToSpeechTelephony } from "../../tts/tts.js";
|
||||
import { listWebSearchProviders, runWebSearch } from "../../web-search/runtime.js";
|
||||
import { createRuntimeAgent } from "./runtime-agent.js";
|
||||
import { createRuntimeChannel } from "./runtime-channel.js";
|
||||
import { createRuntimeConfig } from "./runtime-config.js";
|
||||
@@ -147,6 +148,10 @@ export function createPluginRuntime(_options: CreatePluginRuntimeOptions = {}):
|
||||
describeVideoFile,
|
||||
transcribeAudioFile,
|
||||
},
|
||||
webSearch: {
|
||||
listProviders: listWebSearchProviders,
|
||||
search: runWebSearch,
|
||||
},
|
||||
stt: { transcribeAudioFile },
|
||||
tools: createRuntimeTools(),
|
||||
channel: createRuntimeChannel(),
|
||||
|
||||
@@ -57,6 +57,10 @@ export type PluginRuntimeCore = {
|
||||
describeVideoFile: typeof import("../../media-understanding/runtime.js").describeVideoFile;
|
||||
transcribeAudioFile: typeof import("../../media-understanding/runtime.js").transcribeAudioFile;
|
||||
};
|
||||
webSearch: {
|
||||
listProviders: typeof import("../../web-search/runtime.js").listWebSearchProviders;
|
||||
search: typeof import("../../web-search/runtime.js").runWebSearch;
|
||||
};
|
||||
stt: {
|
||||
transcribeAudioFile: typeof import("../../media-understanding/transcribe-audio.js").transcribeAudioFile;
|
||||
};
|
||||
|
||||
@@ -1,7 +1,16 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { resolvePluginWebSearchProviders } from "./web-search-providers.js";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { createEmptyPluginRegistry } from "./registry.js";
|
||||
import { setActivePluginRegistry } from "./runtime.js";
|
||||
import {
|
||||
resolvePluginWebSearchProviders,
|
||||
resolveRuntimeWebSearchProviders,
|
||||
} from "./web-search-providers.js";
|
||||
|
||||
describe("resolvePluginWebSearchProviders", () => {
|
||||
afterEach(() => {
|
||||
setActivePluginRegistry(createEmptyPluginRegistry());
|
||||
});
|
||||
|
||||
it("returns bundled providers in auto-detect order", () => {
|
||||
const providers = resolvePluginWebSearchProviders({});
|
||||
|
||||
@@ -72,4 +81,36 @@ describe("resolvePluginWebSearchProviders", () => {
|
||||
|
||||
expect(providers).toEqual([]);
|
||||
});
|
||||
|
||||
it("prefers the active plugin registry for runtime resolution", () => {
|
||||
const registry = createEmptyPluginRegistry();
|
||||
registry.webSearchProviders.push({
|
||||
pluginId: "custom-search",
|
||||
pluginName: "Custom Search",
|
||||
provider: {
|
||||
id: "custom",
|
||||
label: "Custom Search",
|
||||
hint: "Custom runtime provider",
|
||||
envVars: ["CUSTOM_SEARCH_API_KEY"],
|
||||
placeholder: "custom-...",
|
||||
signupUrl: "https://example.com/signup",
|
||||
autoDetectOrder: 1,
|
||||
getCredentialValue: () => "configured",
|
||||
setCredentialValue: () => {},
|
||||
createTool: () => ({
|
||||
description: "custom",
|
||||
parameters: {},
|
||||
execute: async () => ({}),
|
||||
}),
|
||||
},
|
||||
source: "test",
|
||||
});
|
||||
setActivePluginRegistry(registry);
|
||||
|
||||
const providers = resolveRuntimeWebSearchProviders({});
|
||||
|
||||
expect(providers.map((provider) => `${provider.pluginId}:${provider.id}`)).toEqual([
|
||||
"custom-search:custom",
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -12,6 +12,7 @@ import {
|
||||
} from "./bundled-compat.js";
|
||||
import { normalizePluginsConfig, resolveEffectiveEnableState } from "./config-state.js";
|
||||
import type { PluginLoadOptions } from "./loader.js";
|
||||
import { getActivePluginRegistry } from "./runtime.js";
|
||||
import type { PluginWebSearchProviderEntry } from "./types.js";
|
||||
|
||||
const BUNDLED_WEB_SEARCH_ALLOWLIST_COMPAT_PLUGIN_IDS = [
|
||||
@@ -127,25 +128,47 @@ export function resolvePluginWebSearchProviders(params: {
|
||||
});
|
||||
const normalizedPlugins = normalizePluginsConfig(config?.plugins);
|
||||
|
||||
return BUNDLED_WEB_SEARCH_PROVIDER_REGISTRY.filter(
|
||||
({ pluginId }) =>
|
||||
resolveEffectiveEnableState({
|
||||
id: pluginId,
|
||||
origin: "bundled",
|
||||
config: normalizedPlugins,
|
||||
rootConfig: config,
|
||||
}).enabled,
|
||||
)
|
||||
.map((entry) => ({
|
||||
return sortWebSearchProviders(
|
||||
BUNDLED_WEB_SEARCH_PROVIDER_REGISTRY.filter(
|
||||
({ pluginId }) =>
|
||||
resolveEffectiveEnableState({
|
||||
id: pluginId,
|
||||
origin: "bundled",
|
||||
config: normalizedPlugins,
|
||||
rootConfig: config,
|
||||
}).enabled,
|
||||
).map((entry) => ({
|
||||
...entry.provider,
|
||||
pluginId: entry.pluginId,
|
||||
}))
|
||||
.toSorted((a, b) => {
|
||||
const aOrder = a.autoDetectOrder ?? Number.MAX_SAFE_INTEGER;
|
||||
const bOrder = b.autoDetectOrder ?? Number.MAX_SAFE_INTEGER;
|
||||
if (aOrder !== bOrder) {
|
||||
return aOrder - bOrder;
|
||||
}
|
||||
return a.id.localeCompare(b.id);
|
||||
});
|
||||
})),
|
||||
);
|
||||
}
|
||||
|
||||
function sortWebSearchProviders(
|
||||
providers: PluginWebSearchProviderEntry[],
|
||||
): PluginWebSearchProviderEntry[] {
|
||||
return providers.toSorted((a, b) => {
|
||||
const aOrder = a.autoDetectOrder ?? Number.MAX_SAFE_INTEGER;
|
||||
const bOrder = b.autoDetectOrder ?? Number.MAX_SAFE_INTEGER;
|
||||
if (aOrder !== bOrder) {
|
||||
return aOrder - bOrder;
|
||||
}
|
||||
return a.id.localeCompare(b.id);
|
||||
});
|
||||
}
|
||||
|
||||
export function resolveRuntimeWebSearchProviders(params: {
|
||||
config?: PluginLoadOptions["config"];
|
||||
bundledAllowlistCompat?: boolean;
|
||||
}): PluginWebSearchProviderEntry[] {
|
||||
const runtimeProviders = getActivePluginRegistry()?.webSearchProviders ?? [];
|
||||
if (runtimeProviders.length > 0) {
|
||||
return sortWebSearchProviders(
|
||||
runtimeProviders.map((entry) => ({
|
||||
...entry.provider,
|
||||
pluginId: entry.pluginId,
|
||||
})),
|
||||
);
|
||||
}
|
||||
return resolvePluginWebSearchProviders(params);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user