refactor: share self hosted provider auth flow

This commit is contained in:
Peter Steinberger 2026-03-13 22:48:40 +00:00
parent 46d4fe2fa1
commit 66979bcc2f
3 changed files with 40 additions and 37 deletions

View File

@ -2,11 +2,9 @@ import {
buildSglangProvider,
configureOpenAICompatibleSelfHostedProviderNonInteractive,
emptyPluginConfigSchema,
promptAndConfigureOpenAICompatibleSelfHostedProvider,
promptAndConfigureOpenAICompatibleSelfHostedProviderAuth,
type OpenClawPluginApi,
type ProviderAuthContext,
type ProviderAuthMethodNonInteractiveContext,
type ProviderAuthResult,
type ProviderDiscoveryContext,
} from "openclaw/plugin-sdk/core";
@ -30,8 +28,8 @@ const sglangPlugin = {
label: "SGLang",
hint: "Fast self-hosted OpenAI-compatible server",
kind: "custom",
run: async (ctx: ProviderAuthContext): Promise<ProviderAuthResult> => {
const result = await promptAndConfigureOpenAICompatibleSelfHostedProvider({
run: (ctx) =>
promptAndConfigureOpenAICompatibleSelfHostedProviderAuth({
cfg: ctx.config,
prompter: ctx.prompter,
providerId: PROVIDER_ID,
@ -39,18 +37,7 @@ const sglangPlugin = {
defaultBaseUrl: DEFAULT_BASE_URL,
defaultApiKeyEnvVar: "SGLANG_API_KEY",
modelPlaceholder: "Qwen/Qwen3-8B",
});
return {
profiles: [
{
profileId: result.profileId,
credential: result.credential,
},
],
configPatch: result.config,
defaultModel: result.modelRef,
};
},
}),
runNonInteractive: async (ctx: ProviderAuthMethodNonInteractiveContext) =>
configureOpenAICompatibleSelfHostedProviderNonInteractive({
ctx,

View File

@ -2,11 +2,9 @@ import {
buildVllmProvider,
configureOpenAICompatibleSelfHostedProviderNonInteractive,
emptyPluginConfigSchema,
promptAndConfigureOpenAICompatibleSelfHostedProvider,
promptAndConfigureOpenAICompatibleSelfHostedProviderAuth,
type OpenClawPluginApi,
type ProviderAuthContext,
type ProviderAuthMethodNonInteractiveContext,
type ProviderAuthResult,
type ProviderDiscoveryContext,
} from "openclaw/plugin-sdk/core";
@ -30,8 +28,8 @@ const vllmPlugin = {
label: "vLLM",
hint: "Local/self-hosted OpenAI-compatible server",
kind: "custom",
run: async (ctx: ProviderAuthContext): Promise<ProviderAuthResult> => {
const result = await promptAndConfigureOpenAICompatibleSelfHostedProvider({
run: (ctx) =>
promptAndConfigureOpenAICompatibleSelfHostedProviderAuth({
cfg: ctx.config,
prompter: ctx.prompter,
providerId: PROVIDER_ID,
@ -39,18 +37,7 @@ const vllmPlugin = {
defaultBaseUrl: DEFAULT_BASE_URL,
defaultApiKeyEnvVar: "VLLM_API_KEY",
modelPlaceholder: "meta-llama/Meta-Llama-3-8B-Instruct",
});
return {
profiles: [
{
profileId: result.profileId,
credential: result.credential,
},
],
configPatch: result.config,
defaultModel: result.modelRef,
};
},
}),
runNonInteractive: async (ctx: ProviderAuthMethodNonInteractiveContext) =>
configureOpenAICompatibleSelfHostedProviderNonInteractive({
ctx,

View File

@ -2,6 +2,7 @@ import { upsertAuthProfileWithLock } from "../agents/auth-profiles.js";
import type { ApiKeyCredential, AuthProfileCredential } from "../agents/auth-profiles/types.js";
import type { OpenClawConfig } from "../config/config.js";
import type {
ProviderAuthResult,
ProviderAuthMethodNonInteractiveContext,
ProviderNonInteractiveApiKeyResult,
} from "../plugins/types.js";
@ -85,7 +86,7 @@ function buildOpenAICompatibleSelfHostedProviderConfig(params: {
};
}
export async function promptAndConfigureOpenAICompatibleSelfHostedProvider(params: {
type OpenAICompatibleSelfHostedProviderSetupParams = {
cfg: OpenClawConfig;
prompter: WizardPrompter;
providerId: string;
@ -97,13 +98,34 @@ export async function promptAndConfigureOpenAICompatibleSelfHostedProvider(param
reasoning?: boolean;
contextWindow?: number;
maxTokens?: number;
}): Promise<{
};
type OpenAICompatibleSelfHostedProviderPromptResult = {
config: OpenClawConfig;
credential: AuthProfileCredential;
modelId: string;
modelRef: string;
profileId: string;
}> {
};
function buildSelfHostedProviderAuthResult(
result: OpenAICompatibleSelfHostedProviderPromptResult,
): ProviderAuthResult {
return {
profiles: [
{
profileId: result.profileId,
credential: result.credential,
},
],
configPatch: result.config,
defaultModel: result.modelRef,
};
}
export async function promptAndConfigureOpenAICompatibleSelfHostedProvider(
params: OpenAICompatibleSelfHostedProviderSetupParams,
): Promise<OpenAICompatibleSelfHostedProviderPromptResult> {
const baseUrlRaw = await params.prompter.text({
message: `${params.providerLabel} base URL`,
initialValue: params.defaultBaseUrl,
@ -152,6 +174,13 @@ export async function promptAndConfigureOpenAICompatibleSelfHostedProvider(param
};
}
export async function promptAndConfigureOpenAICompatibleSelfHostedProviderAuth(
params: OpenAICompatibleSelfHostedProviderSetupParams,
): Promise<ProviderAuthResult> {
const result = await promptAndConfigureOpenAICompatibleSelfHostedProvider(params);
return buildSelfHostedProviderAuthResult(result);
}
function buildMissingNonInteractiveModelIdMessage(params: {
authChoice: string;
providerLabel: string;