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
232 lines
6.8 KiB
TypeScript
232 lines
6.8 KiB
TypeScript
// Xai plugin module implements x search behavior.
|
|
import {
|
|
jsonResult,
|
|
readCache,
|
|
readStringArrayParam,
|
|
readStringParam,
|
|
resolveCacheTtlMs,
|
|
resolveTimeoutSeconds,
|
|
writeCache,
|
|
} from "openclaw/plugin-sdk/provider-web-search";
|
|
import { getRuntimeConfigSnapshot } from "openclaw/plugin-sdk/runtime-config-snapshot";
|
|
import {
|
|
isXaiToolEnabled,
|
|
resolveXaiToolApiKeyWithAuth,
|
|
type XaiToolAuthContext,
|
|
} from "./src/tool-auth-shared.js";
|
|
import { resolveEffectiveXSearchConfig } from "./src/x-search-config.js";
|
|
import {
|
|
buildXaiXSearchPayload,
|
|
requestXaiXSearch,
|
|
resolveXaiXSearchEndpoint,
|
|
resolveXaiXSearchInlineCitations,
|
|
resolveXaiXSearchMaxTurns,
|
|
resolveXaiXSearchModel,
|
|
type XaiXSearchOptions,
|
|
} from "./src/x-search-shared.js";
|
|
import {
|
|
buildMissingXSearchApiKeyPayload,
|
|
createXSearchToolDefinition,
|
|
} from "./x-search-tool-shared.js";
|
|
|
|
class PluginToolInputError extends Error {
|
|
constructor(message: string) {
|
|
super(message);
|
|
this.name = "ToolInputError";
|
|
}
|
|
}
|
|
|
|
const X_SEARCH_CACHE_KEY = Symbol.for("openclaw.xai.x-search.cache");
|
|
|
|
type XSearchCacheEntry = {
|
|
expiresAt: number;
|
|
insertedAt: number;
|
|
value: Record<string, unknown>;
|
|
};
|
|
|
|
function getSharedXSearchCache(): Map<string, XSearchCacheEntry> {
|
|
const root = globalThis as Record<PropertyKey, unknown>;
|
|
const existing = root[X_SEARCH_CACHE_KEY];
|
|
if (existing instanceof Map) {
|
|
return existing as Map<string, XSearchCacheEntry>;
|
|
}
|
|
const next = new Map<string, XSearchCacheEntry>();
|
|
root[X_SEARCH_CACHE_KEY] = next;
|
|
return next;
|
|
}
|
|
|
|
const X_SEARCH_CACHE = getSharedXSearchCache();
|
|
|
|
function resolveXSearchConfig(cfg?: unknown): Record<string, unknown> | undefined {
|
|
return resolveEffectiveXSearchConfig(cfg as never);
|
|
}
|
|
|
|
function resolveXSearchEnabled(params: {
|
|
cfg?: unknown;
|
|
config?: Record<string, unknown>;
|
|
runtimeConfig?: unknown;
|
|
auth?: XaiToolAuthContext;
|
|
}): boolean {
|
|
return isXaiToolEnabled({
|
|
enabled: params.config?.enabled as boolean | undefined,
|
|
runtimeConfig: params.runtimeConfig as never,
|
|
sourceConfig: params.cfg as never,
|
|
auth: params.auth,
|
|
});
|
|
}
|
|
|
|
async function resolveXSearchApiKey(params: {
|
|
sourceConfig?: unknown;
|
|
runtimeConfig?: unknown;
|
|
auth?: XaiToolAuthContext;
|
|
}): Promise<string | undefined> {
|
|
return await resolveXaiToolApiKeyWithAuth(params as never);
|
|
}
|
|
|
|
function normalizeOptionalIsoDate(value: string | undefined, label: string): string | undefined {
|
|
if (!value) {
|
|
return undefined;
|
|
}
|
|
const trimmed = value.trim();
|
|
if (!trimmed) {
|
|
return undefined;
|
|
}
|
|
if (!/^\d{4}-\d{2}-\d{2}$/.test(trimmed)) {
|
|
throw new PluginToolInputError(`${label} must use YYYY-MM-DD`);
|
|
}
|
|
const [year, month, day] = trimmed.split("-").map((entry) => Number.parseInt(entry, 10));
|
|
const date = new Date(Date.UTC(year, month - 1, day));
|
|
if (
|
|
date.getUTCFullYear() !== year ||
|
|
date.getUTCMonth() !== month - 1 ||
|
|
date.getUTCDate() !== day
|
|
) {
|
|
throw new PluginToolInputError(`${label} must be a valid calendar date`);
|
|
}
|
|
return trimmed;
|
|
}
|
|
|
|
function buildXSearchCacheKey(params: {
|
|
query: string;
|
|
model: string;
|
|
endpoint: string;
|
|
inlineCitations: boolean;
|
|
maxTurns?: number;
|
|
options: Omit<XaiXSearchOptions, "query">;
|
|
}) {
|
|
return JSON.stringify([
|
|
"x_search",
|
|
params.model,
|
|
params.endpoint,
|
|
params.query,
|
|
params.inlineCitations,
|
|
params.maxTurns ?? null,
|
|
params.options.allowedXHandles ?? null,
|
|
params.options.excludedXHandles ?? null,
|
|
params.options.fromDate ?? null,
|
|
params.options.toDate ?? null,
|
|
params.options.enableImageUnderstanding ?? false,
|
|
params.options.enableVideoUnderstanding ?? false,
|
|
]);
|
|
}
|
|
|
|
export function createXSearchTool(options?: {
|
|
config?: unknown;
|
|
runtimeConfig?: Record<string, unknown> | null;
|
|
auth?: XaiToolAuthContext;
|
|
}) {
|
|
const xSearchConfig = resolveXSearchConfig(options?.config);
|
|
const runtimeConfig = options?.runtimeConfig ?? getRuntimeConfigSnapshot();
|
|
if (
|
|
!resolveXSearchEnabled({
|
|
cfg: options?.config,
|
|
config: xSearchConfig,
|
|
runtimeConfig: runtimeConfig ?? undefined,
|
|
auth: options?.auth,
|
|
})
|
|
) {
|
|
return null;
|
|
}
|
|
|
|
return createXSearchToolDefinition(async (_toolCallId: string, args: Record<string, unknown>) => {
|
|
const apiKey = await resolveXSearchApiKey({
|
|
sourceConfig: options?.config,
|
|
runtimeConfig: runtimeConfig ?? undefined,
|
|
auth: options?.auth,
|
|
});
|
|
if (!apiKey) {
|
|
return jsonResult(buildMissingXSearchApiKeyPayload());
|
|
}
|
|
|
|
const query = readStringParam(args, "query", { required: true });
|
|
const allowedXHandles = readStringArrayParam(args, "allowed_x_handles");
|
|
const excludedXHandles = readStringArrayParam(args, "excluded_x_handles");
|
|
const fromDate = normalizeOptionalIsoDate(readStringParam(args, "from_date"), "from_date");
|
|
const toDate = normalizeOptionalIsoDate(readStringParam(args, "to_date"), "to_date");
|
|
if (fromDate && toDate && fromDate > toDate) {
|
|
throw new PluginToolInputError("from_date must be on or before to_date");
|
|
}
|
|
|
|
const xSearchOptions: XaiXSearchOptions = {
|
|
query,
|
|
allowedXHandles,
|
|
excludedXHandles,
|
|
fromDate,
|
|
toDate,
|
|
enableImageUnderstanding: args.enable_image_understanding === true,
|
|
enableVideoUnderstanding: args.enable_video_understanding === true,
|
|
};
|
|
const xSearchConfigRecord = xSearchConfig;
|
|
const model = resolveXaiXSearchModel(xSearchConfigRecord);
|
|
const endpoint = resolveXaiXSearchEndpoint(xSearchConfigRecord);
|
|
const inlineCitations = resolveXaiXSearchInlineCitations(xSearchConfigRecord);
|
|
const maxTurns = resolveXaiXSearchMaxTurns(xSearchConfigRecord);
|
|
const cacheKey = buildXSearchCacheKey({
|
|
query,
|
|
model,
|
|
endpoint,
|
|
inlineCitations,
|
|
maxTurns,
|
|
options: {
|
|
allowedXHandles,
|
|
excludedXHandles,
|
|
fromDate,
|
|
toDate,
|
|
enableImageUnderstanding: xSearchOptions.enableImageUnderstanding,
|
|
enableVideoUnderstanding: xSearchOptions.enableVideoUnderstanding,
|
|
},
|
|
});
|
|
const cached = readCache(X_SEARCH_CACHE, cacheKey);
|
|
if (cached) {
|
|
return jsonResult({ ...cached.value, cached: true });
|
|
}
|
|
|
|
const startedAt = Date.now();
|
|
const result = await requestXaiXSearch({
|
|
apiKey,
|
|
endpoint,
|
|
model,
|
|
timeoutSeconds: resolveTimeoutSeconds(xSearchConfig?.timeoutSeconds, 30),
|
|
inlineCitations,
|
|
maxTurns,
|
|
options: xSearchOptions,
|
|
});
|
|
const payload = buildXaiXSearchPayload({
|
|
query,
|
|
model,
|
|
tookMs: Date.now() - startedAt,
|
|
content: result.content,
|
|
citations: result.citations,
|
|
inlineCitations: result.inlineCitations,
|
|
options: xSearchOptions,
|
|
});
|
|
writeCache(
|
|
X_SEARCH_CACHE,
|
|
cacheKey,
|
|
payload,
|
|
resolveCacheTtlMs(xSearchConfig?.cacheTtlMinutes, 15),
|
|
);
|
|
return jsonResult(payload);
|
|
});
|
|
}
|