refactor: dedupe legacy config record helpers

This commit is contained in:
Peter Steinberger
2026-04-06 18:03:55 +01:00
parent b4785525df
commit 09fc834c75
3 changed files with 39 additions and 48 deletions

View File

@@ -0,0 +1,25 @@
type JsonRecord = Record<string, unknown>;
export type { JsonRecord };
export function isRecord(value: unknown): value is JsonRecord {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
export function cloneRecord<T extends JsonRecord>(value: T | undefined): T {
return { ...value } as T;
}
export function ensureRecord(target: JsonRecord, key: string): JsonRecord {
const current = target[key];
if (isRecord(current)) {
return current;
}
const next: JsonRecord = {};
target[key] = next;
return next;
}
export function hasOwnKey(target: JsonRecord, key: string): boolean {
return Object.prototype.hasOwnProperty.call(target, key);
}

View File

@@ -1,27 +1,14 @@
import type { OpenClawConfig } from "../../../config/config.js";
import { mergeMissing } from "../../../config/legacy.shared.js";
type JsonRecord = Record<string, unknown>;
import {
cloneRecord,
ensureRecord,
hasOwnKey,
isRecord,
type JsonRecord,
} from "./legacy-config-record-shared.js";
const DANGEROUS_RECORD_KEYS = new Set(["__proto__", "prototype", "constructor"]);
function isRecord(value: unknown): value is JsonRecord {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function cloneRecord<T extends JsonRecord>(value: T | undefined): T {
return { ...value } as T;
}
function ensureRecord(target: JsonRecord, key: string): JsonRecord {
const current = target[key];
if (isRecord(current)) {
return current;
}
const next: JsonRecord = {};
target[key] = next;
return next;
}
function resolveLegacyFetchConfig(raw: unknown): JsonRecord | undefined {
if (!isRecord(raw)) {
return undefined;
@@ -31,10 +18,6 @@ function resolveLegacyFetchConfig(raw: unknown): JsonRecord | undefined {
return isRecord(web?.fetch) ? web.fetch : undefined;
}
function hasOwnKey(target: JsonRecord, key: string): boolean {
return Object.prototype.hasOwnProperty.call(target, key);
}
function copyLegacyFirecrawlFetchConfig(fetch: JsonRecord): JsonRecord | undefined {
const current = fetch.firecrawl;
if (!isRecord(current)) {

View File

@@ -4,8 +4,13 @@ import {
loadPluginManifestRegistry,
resolveManifestContractOwnerPluginId,
} from "../../../plugins/manifest-registry.js";
type JsonRecord = Record<string, unknown>;
import {
cloneRecord,
ensureRecord,
hasOwnKey,
isRecord,
type JsonRecord,
} from "./legacy-config-record-shared.js";
const MODERN_SCOPED_WEB_SEARCH_KEYS = new Set(["openaiCodex"]);
@@ -20,24 +25,6 @@ const LEGACY_WEB_SEARCH_PROVIDER_IDS = loadPluginManifestRegistry({ cache: true
const LEGACY_WEB_SEARCH_PROVIDER_ID_SET = new Set(LEGACY_WEB_SEARCH_PROVIDER_IDS);
const LEGACY_GLOBAL_WEB_SEARCH_PROVIDER_ID = "brave";
function isRecord(value: unknown): value is JsonRecord {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function cloneRecord<T extends JsonRecord>(value: T | undefined): T {
return { ...value } as T;
}
function ensureRecord(target: JsonRecord, key: string): JsonRecord {
const current = target[key];
if (isRecord(current)) {
return current;
}
const next: JsonRecord = {};
target[key] = next;
return next;
}
function resolveLegacySearchConfig(raw: unknown): JsonRecord | undefined {
if (!isRecord(raw)) {
return undefined;
@@ -52,10 +39,6 @@ function copyLegacyProviderConfig(search: JsonRecord, providerKey: string): Json
return isRecord(current) ? cloneRecord(current) : undefined;
}
function hasOwnKey(target: JsonRecord, key: string): boolean {
return Object.prototype.hasOwnProperty.call(target, key);
}
function hasMappedLegacyWebSearchConfig(raw: unknown): boolean {
const search = resolveLegacySearchConfig(raw);
if (!search) {