refactor: share perplexity web search provider base

This commit is contained in:
Peter Steinberger
2026-04-20 22:04:00 +01:00
parent 1680c86b6c
commit e96a4e8fc3
3 changed files with 58 additions and 68 deletions

View File

@@ -1,5 +1,13 @@
import {
createWebSearchProviderContractFields,
mergeScopedSearchConfig,
resolveProviderWebSearchPluginConfig,
type WebSearchProviderPlugin,
} from "openclaw/plugin-sdk/provider-web-search-config-contract";
export const DEFAULT_PERPLEXITY_BASE_URL = "https://openrouter.ai/api/v1";
export const PERPLEXITY_DIRECT_BASE_URL = "https://api.perplexity.ai";
export const PERPLEXITY_CREDENTIAL_PATH = "plugins.entries.perplexity.config.webSearch.apiKey";
const PERPLEXITY_KEY_PREFIXES = ["pplx-"];
const OPENROUTER_KEY_PREFIXES = ["sk-or-"];
@@ -13,6 +21,44 @@ export type PerplexityRuntimeTransportContext = {
fallbackEnvVar?: string;
};
export function createPerplexityWebSearchProviderBase() {
return {
id: "perplexity",
label: "Perplexity Search",
hint: "Requires Perplexity API key or OpenRouter API key · structured results",
onboardingScopes: ["text-inference"],
credentialLabel: "Perplexity API key",
envVars: ["PERPLEXITY_API_KEY", "OPENROUTER_API_KEY"],
placeholder: "pplx-...",
signupUrl: "https://www.perplexity.ai/settings/api",
docsUrl: "https://docs.openclaw.ai/perplexity",
autoDetectOrder: 50,
credentialPath: PERPLEXITY_CREDENTIAL_PATH,
...createWebSearchProviderContractFields({
credentialPath: PERPLEXITY_CREDENTIAL_PATH,
searchCredential: { type: "scoped", scopeId: "perplexity" },
configuredCredential: { pluginId: "perplexity" },
}),
};
}
export function resolvePerplexityWebSearchRuntimeMetadata(
ctx: Parameters<NonNullable<WebSearchProviderPlugin["resolveRuntimeMetadata"]>>[0],
) {
return {
perplexityTransport: resolvePerplexityRuntimeTransport({
searchConfig: mergeScopedSearchConfig(
ctx.searchConfig,
"perplexity",
resolveProviderWebSearchPluginConfig(ctx.config, "perplexity"),
),
resolvedKey: ctx.resolvedCredential?.value,
keySource: ctx.resolvedCredential?.source ?? "missing",
fallbackEnvVar: ctx.resolvedCredential?.fallbackEnvVar,
}),
};
}
function trimToUndefined(value: unknown): string | undefined {
return typeof value === "string" && value.trim().length > 0 ? value.trim() : undefined;
}

View File

@@ -1,13 +1,13 @@
import {
createWebSearchProviderContractFields,
mergeScopedSearchConfig,
resolveProviderWebSearchPluginConfig,
type WebSearchProviderPlugin,
type WebSearchProviderToolDefinition,
} from "openclaw/plugin-sdk/provider-web-search-config-contract";
import { resolvePerplexityRuntimeTransport } from "./perplexity-web-search-provider.shared.js";
const PERPLEXITY_CREDENTIAL_PATH = "plugins.entries.perplexity.config.webSearch.apiKey";
import {
createPerplexityWebSearchProviderBase,
resolvePerplexityWebSearchRuntimeMetadata,
} from "./perplexity-web-search-provider.shared.js";
type PerplexityWebSearchRuntime = typeof import("./perplexity-web-search-provider.runtime.js");
@@ -112,34 +112,8 @@ function createPerplexityToolDefinition(
export function createPerplexityWebSearchProvider(): WebSearchProviderPlugin {
return {
id: "perplexity",
label: "Perplexity Search",
hint: "Requires Perplexity API key or OpenRouter API key · structured results",
onboardingScopes: ["text-inference"],
credentialLabel: "Perplexity API key",
envVars: ["PERPLEXITY_API_KEY", "OPENROUTER_API_KEY"],
placeholder: "pplx-...",
signupUrl: "https://www.perplexity.ai/settings/api",
docsUrl: "https://docs.openclaw.ai/perplexity",
autoDetectOrder: 50,
credentialPath: PERPLEXITY_CREDENTIAL_PATH,
...createWebSearchProviderContractFields({
credentialPath: PERPLEXITY_CREDENTIAL_PATH,
searchCredential: { type: "scoped", scopeId: "perplexity" },
configuredCredential: { pluginId: "perplexity" },
}),
resolveRuntimeMetadata: (ctx) => ({
perplexityTransport: resolvePerplexityRuntimeTransport({
searchConfig: mergeScopedSearchConfig(
ctx.searchConfig,
"perplexity",
resolveProviderWebSearchPluginConfig(ctx.config, "perplexity"),
),
resolvedKey: ctx.resolvedCredential?.value,
keySource: ctx.resolvedCredential?.source ?? "missing",
fallbackEnvVar: ctx.resolvedCredential?.fallbackEnvVar,
}),
}),
...createPerplexityWebSearchProviderBase(),
resolveRuntimeMetadata: resolvePerplexityWebSearchRuntimeMetadata,
createTool: (ctx) =>
createPerplexityToolDefinition(
mergeScopedSearchConfig(

View File

@@ -1,43 +1,13 @@
import { type WebSearchProviderPlugin } from "openclaw/plugin-sdk/provider-web-search-config-contract";
import {
createWebSearchProviderContractFields,
mergeScopedSearchConfig,
resolveProviderWebSearchPluginConfig,
type WebSearchProviderPlugin,
} from "openclaw/plugin-sdk/provider-web-search-config-contract";
import { resolvePerplexityRuntimeTransport } from "./src/perplexity-web-search-provider.shared.js";
createPerplexityWebSearchProviderBase,
resolvePerplexityWebSearchRuntimeMetadata,
} from "./src/perplexity-web-search-provider.shared.js";
export function createPerplexityWebSearchProvider(): WebSearchProviderPlugin {
const credentialPath = "plugins.entries.perplexity.config.webSearch.apiKey";
return {
id: "perplexity",
label: "Perplexity Search",
hint: "Requires Perplexity API key or OpenRouter API key · structured results",
onboardingScopes: ["text-inference"],
credentialLabel: "Perplexity API key",
envVars: ["PERPLEXITY_API_KEY", "OPENROUTER_API_KEY"],
placeholder: "pplx-...",
signupUrl: "https://www.perplexity.ai/settings/api",
docsUrl: "https://docs.openclaw.ai/perplexity",
autoDetectOrder: 50,
credentialPath,
...createWebSearchProviderContractFields({
credentialPath,
searchCredential: { type: "scoped", scopeId: "perplexity" },
configuredCredential: { pluginId: "perplexity" },
}),
resolveRuntimeMetadata: (ctx) => ({
perplexityTransport: resolvePerplexityRuntimeTransport({
searchConfig: mergeScopedSearchConfig(
ctx.searchConfig,
"perplexity",
resolveProviderWebSearchPluginConfig(ctx.config, "perplexity"),
),
resolvedKey: ctx.resolvedCredential?.value,
keySource: ctx.resolvedCredential?.source ?? "missing",
fallbackEnvVar: ctx.resolvedCredential?.fallbackEnvVar,
}),
}),
...createPerplexityWebSearchProviderBase(),
resolveRuntimeMetadata: resolvePerplexityWebSearchRuntimeMetadata,
createTool: () => null,
};
}