refactor: dedupe provider string readers

This commit is contained in:
Peter Steinberger
2026-04-07 04:47:57 +01:00
parent b059328f60
commit 8b501986aa
11 changed files with 36 additions and 24 deletions

View File

@@ -8,6 +8,7 @@ import {
streamWithPayloadPatch,
} from "openclaw/plugin-sdk/provider-stream-shared";
import { createSubsystemLogger } from "openclaw/plugin-sdk/runtime-env";
import { readStringValue } from "openclaw/plugin-sdk/text-runtime";
const log = createSubsystemLogger("anthropic-stream");
@@ -157,9 +158,9 @@ export function createAnthropicFastModeWrapper(
}
const payloadPolicy = resolveAnthropicPayloadPolicy({
provider: typeof model.provider === "string" ? model.provider : undefined,
api: typeof model.api === "string" ? model.api : undefined,
baseUrl: typeof model.baseUrl === "string" ? model.baseUrl : undefined,
provider: readStringValue(model.provider),
api: readStringValue(model.api),
baseUrl: readStringValue(model.baseUrl),
serviceTier,
});
if (!payloadPolicy.allowsServiceTier) {
@@ -183,9 +184,9 @@ export function createAnthropicServiceTierWrapper(
}
const payloadPolicy = resolveAnthropicPayloadPolicy({
provider: typeof model.provider === "string" ? model.provider : undefined,
api: typeof model.api === "string" ? model.api : undefined,
baseUrl: typeof model.baseUrl === "string" ? model.baseUrl : undefined,
provider: readStringValue(model.provider),
api: readStringValue(model.api),
baseUrl: readStringValue(model.baseUrl),
serviceTier,
});
if (!payloadPolicy.allowsServiceTier) {

View File

@@ -7,6 +7,7 @@ import {
import { buildOauthProviderAuthResult } from "openclaw/plugin-sdk/provider-auth";
import { createProviderApiKeyAuthMethod } from "openclaw/plugin-sdk/provider-auth-api-key";
import { loginChutes } from "openclaw/plugin-sdk/provider-auth-login";
import { readStringValue } from "openclaw/plugin-sdk/text-runtime";
import {
CHUTES_DEFAULT_MODEL_REF,
applyChutesApiKeyConfig,
@@ -82,7 +83,7 @@ async function runChutesOAuth(ctx: ProviderAuthContext): Promise<ProviderAuthRes
access: creds.access,
refresh: creds.refresh,
expires: creds.expires,
email: typeof creds.email === "string" ? creds.email : undefined,
email: readStringValue(creds.email),
credentialExtra: {
clientId,
...("accountId" in creds && typeof creds.accountId === "string"

View File

@@ -1,4 +1,5 @@
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-runtime";
import { readStringValue } from "openclaw/plugin-sdk/text-runtime";
import { runFirecrawlScrape } from "./src/firecrawl-client.js";
export type FetchFirecrawlContentParams = {
@@ -58,9 +59,9 @@ export async function fetchFirecrawlContent(
return {
text: typeof result.text === "string" ? result.text : "",
title: typeof result.title === "string" ? result.title : undefined,
finalUrl: typeof result.finalUrl === "string" ? result.finalUrl : undefined,
title: readStringValue(result.title),
finalUrl: readStringValue(result.finalUrl),
status: typeof result.status === "number" ? result.status : undefined,
warning: typeof result.warning === "string" ? result.warning : undefined,
warning: readStringValue(result.warning),
};
}

View File

@@ -1,3 +1,5 @@
import { readStringValue } from "openclaw/plugin-sdk/text-runtime";
type GoogleOauthApiKeyCredential = {
type?: string;
access?: string;
@@ -11,8 +13,8 @@ export function parseGoogleOauthApiKey(apiKey: string): {
try {
const parsed = JSON.parse(apiKey) as { token?: unknown; projectId?: unknown };
return {
token: typeof parsed.token === "string" ? parsed.token : undefined,
projectId: typeof parsed.projectId === "string" ? parsed.projectId : undefined,
token: readStringValue(parsed.token),
projectId: readStringValue(parsed.projectId),
};
} catch {
return null;

View File

@@ -1,5 +1,6 @@
import { defineSingleProviderPluginEntry } from "openclaw/plugin-sdk/provider-entry";
import { resolveProviderRequestCapabilities } from "openclaw/plugin-sdk/provider-http";
import { readStringValue } from "openclaw/plugin-sdk/text-runtime";
import { applyMistralModelCompat, MISTRAL_MODEL_COMPAT_PATCH } from "./api.js";
import { mistralMediaUnderstandingProvider } from "./media-understanding-provider.js";
import { applyMistralConfig, MISTRAL_DEFAULT_MODEL_REF } from "./onboard.js";
@@ -36,9 +37,9 @@ function shouldContributeMistralCompat(params: {
}
const capabilities = resolveProviderRequestCapabilities({
provider: typeof params.model.provider === "string" ? params.model.provider : undefined,
provider: readStringValue(params.model.provider),
api: "openai-completions",
baseUrl: typeof params.model.baseUrl === "string" ? params.model.baseUrl : undefined,
baseUrl: readStringValue(params.model.baseUrl),
capability: "llm",
transport: "stream",
modelId: params.modelId,

View File

@@ -7,6 +7,7 @@ import {
type ProviderDiscoveryContext,
} from "openclaw/plugin-sdk/plugin-entry";
import { buildProviderReplayFamilyHooks } from "openclaw/plugin-sdk/provider-model-shared";
import { readStringValue } from "openclaw/plugin-sdk/text-runtime";
import {
buildOllamaProvider,
configureOllamaNonInteractive,
@@ -118,7 +119,7 @@ export default definePluginEntry({
return null;
}
const ollamaKey = ctx.resolveProviderApiKey(PROVIDER_ID).apiKey;
const explicitApiKey = typeof explicit?.apiKey === "string" ? explicit.apiKey : undefined;
const explicitApiKey = readStringValue(explicit?.apiKey);
if (hasExplicitModels && explicit) {
return {
provider: {

View File

@@ -26,6 +26,7 @@ import {
streamWithPayloadPatch,
} from "openclaw/plugin-sdk/provider-stream-shared";
import { createSubsystemLogger } from "openclaw/plugin-sdk/runtime-env";
import { readStringValue } from "openclaw/plugin-sdk/text-runtime";
import { OLLAMA_DEFAULT_BASE_URL } from "./defaults.js";
import {
parseJsonObjectPreservingUnsafeIntegers,
@@ -759,7 +760,7 @@ export function createConfiguredOllamaStreamFn(params: {
}): StreamFn {
return createOllamaStreamFn(
resolveOllamaBaseUrlForRun({
modelBaseUrl: typeof params.model.baseUrl === "string" ? params.model.baseUrl : undefined,
modelBaseUrl: readStringValue(params.model.baseUrl),
providerBaseUrl: params.providerBaseUrl,
}),
resolveOllamaModelHeaders(params.model),

View File

@@ -19,6 +19,7 @@ import {
} from "openclaw/plugin-sdk/provider-model-shared";
import { buildProviderStreamFamilyHooks } from "openclaw/plugin-sdk/provider-stream-family";
import { fetchCodexUsage } from "openclaw/plugin-sdk/provider-usage";
import { readStringValue } from "openclaw/plugin-sdk/text-runtime";
import { OPENAI_CODEX_DEFAULT_MODEL } from "./default-models.js";
import { resolveCodexAuthIdentity } from "./openai-codex-auth-identity.js";
import { buildOpenAICodexProvider } from "./openai-codex-catalog.js";
@@ -214,7 +215,7 @@ async function runOpenAICodexOAuth(ctx: ProviderAuthContext) {
const identity = resolveCodexAuthIdentity({
accessToken: creds.access,
email: typeof creds.email === "string" ? creds.email : undefined,
email: readStringValue(creds.email),
});
return buildOauthProviderAuthResult({

View File

@@ -8,6 +8,7 @@ import {
applyXaiModelCompat,
resolveXaiModelCompatPatch,
} from "@openclaw/plugin-sdk/provider-tools";
import { readStringValue } from "openclaw/plugin-sdk/text-runtime";
export { buildXaiProvider } from "./provider-catalog.js";
export { applyXaiConfig, applyXaiProviderConfig } from "./onboard.js";
@@ -75,6 +76,6 @@ export function resolveXaiTransport(params: {
}
return {
api: "openai-responses",
baseUrl: typeof params.baseUrl === "string" ? params.baseUrl : undefined,
baseUrl: readStringValue(params.baseUrl),
};
}

View File

@@ -1,3 +1,4 @@
import { readStringValue } from "../shared/string-coerce.js";
import { resolveProviderRequestPolicyConfig } from "./provider-request-config.js";
type OpenAIResponsesPayloadModel = {
@@ -93,9 +94,9 @@ export function resolveOpenAIResponsesPayloadPolicy(
options: OpenAIResponsesPayloadPolicyOptions = {},
): OpenAIResponsesPayloadPolicy {
const capabilities = resolveProviderRequestPolicyConfig({
provider: typeof model.provider === "string" ? model.provider : undefined,
api: typeof model.api === "string" ? model.api : undefined,
baseUrl: typeof model.baseUrl === "string" ? model.baseUrl : undefined,
provider: readStringValue(model.provider),
api: readStringValue(model.api),
baseUrl: readStringValue(model.baseUrl),
compat: model.compat,
capability: "llm",
transport: "stream",

View File

@@ -1,4 +1,5 @@
import type { StreamFn } from "@mariozechner/pi-agent-core";
import { readStringValue } from "../shared/string-coerce.js";
import type {
FunctionToolDefinition,
InputItem,
@@ -94,9 +95,9 @@ export function buildOpenAIWebSocketResponseCreatePayload(params: {
}
const supportsResponsesStoreField = resolveProviderRequestPolicyConfig({
provider: typeof params.model.provider === "string" ? params.model.provider : undefined,
api: typeof params.model.api === "string" ? params.model.api : undefined,
baseUrl: typeof params.model.baseUrl === "string" ? params.model.baseUrl : undefined,
provider: readStringValue(params.model.provider),
api: readStringValue(params.model.api),
baseUrl: readStringValue(params.model.baseUrl),
compat: (params.model as { compat?: { supportsStore?: boolean } }).compat,
capability: "llm",
transport: "websocket",