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
267 lines
9.5 KiB
TypeScript
267 lines
9.5 KiB
TypeScript
import { createLazyRuntimeModule } from "openclaw/plugin-sdk/lazy-runtime";
|
|
// Xai plugin entrypoint registers its OpenClaw integration.
|
|
import { defineSingleProviderPluginEntry } from "openclaw/plugin-sdk/provider-entry";
|
|
import { OPENAI_COMPATIBLE_REPLAY_HOOKS } from "openclaw/plugin-sdk/provider-model-shared";
|
|
import { defaultToolStreamExtraParams } from "openclaw/plugin-sdk/provider-stream-shared";
|
|
import { jsonResult } from "openclaw/plugin-sdk/provider-web-search";
|
|
import {
|
|
applyXaiRuntimeModelCompat,
|
|
buildXaiImageGenerationProvider,
|
|
normalizeXaiModelId,
|
|
resolveXaiTransport,
|
|
} from "./api.js";
|
|
import {
|
|
buildMissingCodeExecutionApiKeyPayload,
|
|
createCodeExecutionToolDefinition,
|
|
} from "./code-execution-tool-shared.js";
|
|
import { applyXaiConfig, XAI_DEFAULT_MODEL_REF } from "./onboard.js";
|
|
import {
|
|
buildLiveXaiOAuthProvider,
|
|
buildLiveXaiProvider,
|
|
buildXaiProvider,
|
|
} from "./provider-catalog.js";
|
|
import { isModernXaiModel, resolveXaiForwardCompatModel } from "./provider-models.js";
|
|
import { resolveThinkingProfile } from "./provider-policy-api.js";
|
|
import { buildXaiRealtimeTranscriptionProvider } from "./realtime-transcription-provider.js";
|
|
import { buildXaiSpeechProvider } from "./speech-provider.js";
|
|
import {
|
|
readPluginCodeExecutionConfig,
|
|
resolveCodeExecutionEnabled,
|
|
} from "./src/code-execution-config.js";
|
|
import {
|
|
isXaiToolEnabled,
|
|
resolveFallbackXaiAuth,
|
|
type XaiToolAuthContext,
|
|
} from "./src/tool-auth-shared.js";
|
|
import { resolveEffectiveXSearchConfig } from "./src/x-search-config.js";
|
|
import { wrapXaiProviderStream } from "./stream.js";
|
|
import { buildXaiMediaUnderstandingProvider } from "./stt.js";
|
|
import { buildXaiVideoGenerationProvider } from "./video-generation-provider.js";
|
|
import { createXaiWebSearchProvider } from "./web-search.js";
|
|
import {
|
|
buildMissingXSearchApiKeyPayload,
|
|
createXSearchToolDefinition,
|
|
} from "./x-search-tool-shared.js";
|
|
import {
|
|
createXaiDeviceCodeAuthMethod,
|
|
createXaiOAuthAuthMethod,
|
|
refreshXaiOAuthCredential,
|
|
} from "./xai-oauth.js";
|
|
|
|
const PROVIDER_ID = "xai";
|
|
|
|
const XAI_CREDIT_OR_SPENDING_LIMIT_RE =
|
|
/\b(?:used all available credits|monthly spending limit|purchase more credits|raise your spending limit)\b/i;
|
|
const XAI_RATE_LIMIT_RE = /\b(?:rate limit exceeded|too many requests)\b/i;
|
|
|
|
const loadCodeExecutionModule = createLazyRuntimeModule(() => import("./code-execution.js"));
|
|
|
|
const loadXSearchModule = createLazyRuntimeModule(() => import("./x-search.js"));
|
|
|
|
function classifyXaiFailoverReason(errorMessage: string) {
|
|
if (XAI_CREDIT_OR_SPENDING_LIMIT_RE.test(errorMessage)) {
|
|
return "billing" as const;
|
|
}
|
|
if (XAI_RATE_LIMIT_RE.test(errorMessage)) {
|
|
return "rate_limit" as const;
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
function hasResolvableXaiApiKey(config: unknown, auth?: XaiToolAuthContext): boolean {
|
|
return isXaiToolEnabled({ sourceConfig: config as never, auth });
|
|
}
|
|
|
|
function isCodeExecutionEnabled(config: unknown, auth?: XaiToolAuthContext): boolean {
|
|
return resolveCodeExecutionEnabled({
|
|
sourceConfig: config,
|
|
runtimeConfig: config,
|
|
config: readPluginCodeExecutionConfig(config),
|
|
auth,
|
|
});
|
|
}
|
|
|
|
function isXSearchEnabled(config: unknown, auth?: XaiToolAuthContext): boolean {
|
|
const resolved =
|
|
config && typeof config === "object"
|
|
? resolveEffectiveXSearchConfig(config as never)
|
|
: undefined;
|
|
if (resolved?.enabled === false) {
|
|
return false;
|
|
}
|
|
return hasResolvableXaiApiKey(config, auth);
|
|
}
|
|
|
|
function createLazyCodeExecutionTool(ctx: {
|
|
config?: Record<string, unknown>;
|
|
runtimeConfig?: Record<string, unknown>;
|
|
hasAuthForProvider?: XaiToolAuthContext["hasAuthForProvider"];
|
|
resolveApiKeyForProvider?: XaiToolAuthContext["resolveApiKeyForProvider"];
|
|
}) {
|
|
const effectiveConfig = ctx.runtimeConfig ?? ctx.config;
|
|
if (!isCodeExecutionEnabled(effectiveConfig, ctx)) {
|
|
return null;
|
|
}
|
|
|
|
return createCodeExecutionToolDefinition(
|
|
async (toolCallId: string, args: Record<string, unknown>) => {
|
|
const { createCodeExecutionTool } = await loadCodeExecutionModule();
|
|
const tool = createCodeExecutionTool({
|
|
config: ctx.config as never,
|
|
runtimeConfig: (ctx.runtimeConfig as never) ?? null,
|
|
auth: ctx,
|
|
});
|
|
if (!tool) {
|
|
return jsonResult(buildMissingCodeExecutionApiKeyPayload());
|
|
}
|
|
return await tool.execute(toolCallId, args);
|
|
},
|
|
);
|
|
}
|
|
|
|
function createLazyXSearchTool(ctx: {
|
|
config?: Record<string, unknown>;
|
|
runtimeConfig?: Record<string, unknown>;
|
|
hasAuthForProvider?: XaiToolAuthContext["hasAuthForProvider"];
|
|
resolveApiKeyForProvider?: XaiToolAuthContext["resolveApiKeyForProvider"];
|
|
}) {
|
|
const effectiveConfig = ctx.runtimeConfig ?? ctx.config;
|
|
if (!isXSearchEnabled(effectiveConfig, ctx)) {
|
|
return null;
|
|
}
|
|
|
|
return createXSearchToolDefinition(async (toolCallId: string, args: Record<string, unknown>) => {
|
|
const { createXSearchTool } = await loadXSearchModule();
|
|
const tool = createXSearchTool({
|
|
config: ctx.config as never,
|
|
runtimeConfig: (ctx.runtimeConfig as never) ?? null,
|
|
auth: ctx,
|
|
});
|
|
if (!tool) {
|
|
return jsonResult(buildMissingXSearchApiKeyPayload());
|
|
}
|
|
return await tool.execute(toolCallId, args);
|
|
});
|
|
}
|
|
|
|
export default defineSingleProviderPluginEntry({
|
|
id: "xai",
|
|
name: "xAI Plugin",
|
|
description: "Bundled xAI plugin",
|
|
provider: {
|
|
label: "xAI",
|
|
aliases: ["x-ai"],
|
|
docsPath: "/providers/xai",
|
|
auth: [
|
|
{
|
|
methodId: "api-key",
|
|
label: "xAI API key",
|
|
hint: "API key",
|
|
optionKey: "xaiApiKey",
|
|
flagName: "--xai-api-key",
|
|
envVar: "XAI_API_KEY",
|
|
promptMessage: "Enter xAI API key",
|
|
defaultModel: XAI_DEFAULT_MODEL_REF,
|
|
applyConfig: (cfg) => applyXaiConfig(cfg),
|
|
wizard: {
|
|
groupLabel: "xAI (Grok)",
|
|
},
|
|
},
|
|
],
|
|
extraAuth: [createXaiOAuthAuthMethod(), createXaiDeviceCodeAuthMethod()],
|
|
catalog: {
|
|
order: "simple",
|
|
run: async (ctx) => {
|
|
const auth = ctx.resolveProviderAuth(PROVIDER_ID);
|
|
try {
|
|
const { resolveApiKeyForProvider } =
|
|
await import("openclaw/plugin-sdk/provider-auth-runtime");
|
|
const runtimeAuth = await resolveApiKeyForProvider({
|
|
provider: PROVIDER_ID,
|
|
cfg: ctx.config,
|
|
...(ctx.agentDir ? { agentDir: ctx.agentDir } : {}),
|
|
...(ctx.workspaceDir ? { workspaceDir: ctx.workspaceDir } : {}),
|
|
...(auth.profileId
|
|
? {
|
|
profileId: auth.profileId,
|
|
lockedProfile: true,
|
|
}
|
|
: {}),
|
|
});
|
|
if (runtimeAuth?.mode === "oauth" && runtimeAuth.apiKey) {
|
|
return {
|
|
provider: await buildLiveXaiOAuthProvider({
|
|
discoveryApiKey: runtimeAuth.apiKey,
|
|
}),
|
|
};
|
|
}
|
|
} catch {
|
|
if (auth.mode === "oauth") {
|
|
// OAuth discovery is advisory; fall through so configured API-key
|
|
// auth can still publish the standard xAI catalog.
|
|
}
|
|
}
|
|
if (auth.apiKey) {
|
|
return {
|
|
provider: await buildLiveXaiProvider({
|
|
apiKey: auth.apiKey,
|
|
discoveryApiKey: auth.discoveryApiKey,
|
|
}),
|
|
};
|
|
}
|
|
|
|
const apiKey = ctx.resolveProviderApiKey(PROVIDER_ID);
|
|
if (!apiKey.apiKey) {
|
|
return null;
|
|
}
|
|
return {
|
|
provider: await buildLiveXaiProvider({
|
|
apiKey: apiKey.apiKey,
|
|
discoveryApiKey: apiKey.discoveryApiKey,
|
|
}),
|
|
};
|
|
},
|
|
staticRun: async () => ({
|
|
provider: buildXaiProvider(),
|
|
}),
|
|
},
|
|
...OPENAI_COMPATIBLE_REPLAY_HOOKS,
|
|
prepareExtraParams: (ctx) => defaultToolStreamExtraParams(ctx.extraParams),
|
|
wrapStreamFn: wrapXaiProviderStream,
|
|
// Provider-specific fallback auth stays owned by the xAI plugin so core
|
|
// auth/discovery code can consume it generically without parsing xAI's
|
|
// private config layout. Callers may receive a real key from the active
|
|
// runtime snapshot or a non-secret SecretRef marker from source config.
|
|
resolveSyntheticAuth: ({ config }) => {
|
|
const fallbackAuth = resolveFallbackXaiAuth(config);
|
|
if (!fallbackAuth) {
|
|
return undefined;
|
|
}
|
|
return {
|
|
apiKey: fallbackAuth.apiKey,
|
|
source: fallbackAuth.source,
|
|
mode: "api-key" as const,
|
|
};
|
|
},
|
|
normalizeResolvedModel: ({ model }) => applyXaiRuntimeModelCompat(model),
|
|
normalizeTransport: ({ provider, api, baseUrl }) =>
|
|
resolveXaiTransport({ provider, api, baseUrl }),
|
|
normalizeModelId: ({ modelId }) => normalizeXaiModelId(modelId),
|
|
resolveDynamicModel: (ctx) => resolveXaiForwardCompatModel({ providerId: PROVIDER_ID, ctx }),
|
|
refreshOAuth: refreshXaiOAuthCredential,
|
|
resolveThinkingProfile,
|
|
isModernModelRef: ({ modelId }) => isModernXaiModel(modelId),
|
|
classifyFailoverReason: ({ errorMessage }) => classifyXaiFailoverReason(errorMessage),
|
|
},
|
|
register(api) {
|
|
api.registerWebSearchProvider(createXaiWebSearchProvider());
|
|
api.registerMediaUnderstandingProvider(buildXaiMediaUnderstandingProvider());
|
|
api.registerVideoGenerationProvider(buildXaiVideoGenerationProvider());
|
|
api.registerImageGenerationProvider(buildXaiImageGenerationProvider());
|
|
api.registerSpeechProvider(buildXaiSpeechProvider());
|
|
api.registerRealtimeTranscriptionProvider(buildXaiRealtimeTranscriptionProvider());
|
|
api.registerTool((ctx) => createLazyCodeExecutionTool(ctx), { name: "code_execution" });
|
|
api.registerTool((ctx) => createLazyXSearchTool(ctx), { name: "x_search" });
|
|
},
|
|
});
|