refactor: share self hosted provider plugin helpers

This commit is contained in:
Peter Steinberger
2026-03-14 00:40:00 +00:00
parent 66aabf5eaa
commit 81ea997d40
4 changed files with 47 additions and 66 deletions

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 {
ProviderDiscoveryContext,
ProviderAuthResult,
ProviderAuthMethodNonInteractiveContext,
ProviderNonInteractiveApiKeyResult,
@@ -181,6 +182,28 @@ export async function promptAndConfigureOpenAICompatibleSelfHostedProviderAuth(
return buildSelfHostedProviderAuthResult(result);
}
export async function discoverOpenAICompatibleSelfHostedProvider<
T extends Record<string, unknown>,
>(params: {
ctx: ProviderDiscoveryContext;
providerId: string;
buildProvider: (params: { apiKey?: string }) => Promise<T>;
}): Promise<{ provider: T & { apiKey: string } } | null> {
if (params.ctx.config.models?.providers?.[params.providerId]) {
return null;
}
const { apiKey, discoveryApiKey } = params.ctx.resolveProviderApiKey(params.providerId);
if (!apiKey) {
return null;
}
return {
provider: {
...(await params.buildProvider({ apiKey: discoveryApiKey })),
apiKey,
},
};
}
function buildMissingNonInteractiveModelIdMessage(params: {
authChoice: string;
providerLabel: string;

View File

@@ -17,7 +17,9 @@ export { buildOauthProviderAuthResult } from "./provider-auth-result.js";
export {
applyProviderDefaultModel,
configureOpenAICompatibleSelfHostedProviderNonInteractive,
discoverOpenAICompatibleSelfHostedProvider,
promptAndConfigureOpenAICompatibleSelfHostedProvider,
promptAndConfigureOpenAICompatibleSelfHostedProviderAuth,
SELF_HOSTED_DEFAULT_CONTEXT_WINDOW,
SELF_HOSTED_DEFAULT_COST,
SELF_HOSTED_DEFAULT_MAX_TOKENS,