refactor: dedupe gateway helper readers

This commit is contained in:
Peter Steinberger
2026-04-07 08:31:17 +01:00
parent 5eb6921a18
commit ce7ef626b8
8 changed files with 34 additions and 60 deletions

View File

@@ -17,7 +17,11 @@ import {
ssrfPolicyFromDangerouslyAllowPrivateNetwork,
type SsrFPolicy,
} from "openclaw/plugin-sdk/ssrf-runtime";
import { isRecord, resolveUserPath } from "openclaw/plugin-sdk/text-runtime";
import {
isRecord,
normalizeOptionalString,
resolveUserPath,
} from "openclaw/plugin-sdk/text-runtime";
const DEFAULT_COMFY_LOCAL_BASE_URL = "http://127.0.0.1:8188";
const DEFAULT_COMFY_CLOUD_BASE_URL = "https://cloud.comfy.org";
@@ -87,15 +91,6 @@ export function _setComfyFetchGuardForTesting(impl: typeof fetchWithSsrFGuard |
comfyFetchGuard = impl ?? fetchWithSsrFGuard;
}
function readConfigString(config: ComfyProviderConfig, key: string): string | undefined {
const value = config[key];
if (typeof value !== "string") {
return undefined;
}
const trimmed = value.trim();
return trimmed ? trimmed : undefined;
}
function readConfigBoolean(config: ComfyProviderConfig, key: string): boolean | undefined {
const value = config[key];
return typeof value === "boolean" ? value : undefined;
@@ -161,11 +156,11 @@ export function getComfyCapabilityConfig(
}
export function resolveComfyMode(config: ComfyProviderConfig): ComfyMode {
return readConfigString(config, "mode") === "cloud" ? "cloud" : "local";
return normalizeOptionalString(config.mode) === "cloud" ? "cloud" : "local";
}
function getRequiredConfigString(config: ComfyProviderConfig, key: string): string {
const value = readConfigString(config, key);
const value = normalizeOptionalString(config[key]);
if (!value) {
throw new Error(`models.providers.comfy.${key} is required`);
}
@@ -180,7 +175,7 @@ function resolveComfyWorkflowSource(config: ComfyProviderConfig): {
if (isRecord(workflow)) {
return { workflow: structuredClone(workflow) };
}
const workflowPath = readConfigString(config, "workflowPath");
const workflowPath = normalizeOptionalString(config.workflowPath);
return { workflowPath };
}
@@ -230,7 +225,7 @@ function resolveComfyNetworkPolicy(params: {
return {};
}
const hostname = parsed.hostname.trim().toLowerCase();
const hostname = normalizeOptionalString(parsed.hostname)?.toLowerCase() ?? "";
if (!hostname || !params.allowPrivateNetwork || !isPrivateOrLoopbackHost(hostname)) {
return {};
}