refactor: dedupe core trimmed string readers

This commit is contained in:
Peter Steinberger
2026-04-07 22:49:49 +01:00
parent 67035a6af8
commit 7999767a0f
4 changed files with 78 additions and 86 deletions

View File

@@ -4,6 +4,7 @@ import JSON5 from "json5";
import type { ChannelConfigRuntimeSchema } from "../channels/plugins/types.plugin.js";
import { MANIFEST_KEY } from "../compat/legacy-names.js";
import { matchBoundaryFileOpenFailure, openBoundaryFileSync } from "../infra/boundary-file-read.js";
import { normalizeOptionalString } from "../shared/string-coerce.js";
import { normalizeTrimmedStringList } from "../shared/string-normalization.js";
import { isRecord } from "../utils.js";
import type { PluginConfigUiHint, PluginKind } from "./types.js";
@@ -184,7 +185,7 @@ function normalizeStringListRecord(value: unknown): Record<string, string[]> | u
}
const normalized: Record<string, string[]> = {};
for (const [key, rawValues] of Object.entries(value)) {
const providerId = typeof key === "string" ? key.trim() : "";
const providerId = normalizeOptionalString(key) ?? "";
if (!providerId) {
continue;
}
@@ -252,7 +253,7 @@ function normalizeManifestDangerousConfigFlags(
if (!isRecord(entry)) {
continue;
}
const path = typeof entry.path === "string" ? entry.path.trim() : "";
const path = normalizeOptionalString(entry.path) ?? "";
if (!path || !isManifestConfigLiteral(entry.equals)) {
continue;
}
@@ -272,7 +273,7 @@ function normalizeManifestSecretInputPaths(
if (!isRecord(entry)) {
continue;
}
const path = typeof entry.path === "string" ? entry.path.trim() : "";
const path = normalizeOptionalString(entry.path) ?? "";
if (!path) {
continue;
}
@@ -344,14 +345,14 @@ function normalizeProviderAuthChoices(
if (!isRecord(entry)) {
continue;
}
const provider = typeof entry.provider === "string" ? entry.provider.trim() : "";
const method = typeof entry.method === "string" ? entry.method.trim() : "";
const choiceId = typeof entry.choiceId === "string" ? entry.choiceId.trim() : "";
const provider = normalizeOptionalString(entry.provider) ?? "";
const method = normalizeOptionalString(entry.method) ?? "";
const choiceId = normalizeOptionalString(entry.choiceId) ?? "";
if (!provider || !method || !choiceId) {
continue;
}
const choiceLabel = typeof entry.choiceLabel === "string" ? entry.choiceLabel.trim() : "";
const choiceHint = typeof entry.choiceHint === "string" ? entry.choiceHint.trim() : "";
const choiceLabel = normalizeOptionalString(entry.choiceLabel) ?? "";
const choiceHint = normalizeOptionalString(entry.choiceHint) ?? "";
const assistantPriority =
typeof entry.assistantPriority === "number" && Number.isFinite(entry.assistantPriority)
? entry.assistantPriority
@@ -361,14 +362,13 @@ function normalizeProviderAuthChoices(
? entry.assistantVisibility
: undefined;
const deprecatedChoiceIds = normalizeTrimmedStringList(entry.deprecatedChoiceIds);
const groupId = typeof entry.groupId === "string" ? entry.groupId.trim() : "";
const groupLabel = typeof entry.groupLabel === "string" ? entry.groupLabel.trim() : "";
const groupHint = typeof entry.groupHint === "string" ? entry.groupHint.trim() : "";
const optionKey = typeof entry.optionKey === "string" ? entry.optionKey.trim() : "";
const cliFlag = typeof entry.cliFlag === "string" ? entry.cliFlag.trim() : "";
const cliOption = typeof entry.cliOption === "string" ? entry.cliOption.trim() : "";
const cliDescription =
typeof entry.cliDescription === "string" ? entry.cliDescription.trim() : "";
const groupId = normalizeOptionalString(entry.groupId) ?? "";
const groupLabel = normalizeOptionalString(entry.groupLabel) ?? "";
const groupHint = normalizeOptionalString(entry.groupHint) ?? "";
const optionKey = normalizeOptionalString(entry.optionKey) ?? "";
const cliFlag = normalizeOptionalString(entry.cliFlag) ?? "";
const cliOption = normalizeOptionalString(entry.cliOption) ?? "";
const cliDescription = normalizeOptionalString(entry.cliDescription) ?? "";
const onboardingScopes = normalizeTrimmedStringList(entry.onboardingScopes).filter(
(scope): scope is PluginManifestOnboardingScope =>
scope === "text-inference" || scope === "image-generation",
@@ -403,7 +403,7 @@ function normalizeChannelConfigs(
}
const normalized: Record<string, PluginManifestChannelConfig> = {};
for (const [key, rawEntry] of Object.entries(value)) {
const channelId = typeof key === "string" ? key.trim() : "";
const channelId = normalizeOptionalString(key) ?? "";
if (!channelId || !isRecord(rawEntry)) {
continue;
}
@@ -418,8 +418,8 @@ function normalizeChannelConfigs(
isRecord(rawEntry.runtime) && typeof rawEntry.runtime.safeParse === "function"
? (rawEntry.runtime as ChannelConfigRuntimeSchema)
: undefined;
const label = typeof rawEntry.label === "string" ? rawEntry.label.trim() : "";
const description = typeof rawEntry.description === "string" ? rawEntry.description.trim() : "";
const label = normalizeOptionalString(rawEntry.label) ?? "";
const description = normalizeOptionalString(rawEntry.description) ?? "";
const preferOver = normalizeTrimmedStringList(rawEntry.preferOver);
normalized[channelId] = {
schema,
@@ -493,7 +493,7 @@ export function loadPluginManifest(
if (!isRecord(raw)) {
return { ok: false, error: "plugin manifest must be an object", manifestPath };
}
const id = typeof raw.id === "string" ? raw.id.trim() : "";
const id = normalizeOptionalString(raw.id) ?? "";
if (!id) {
return { ok: false, error: "plugin manifest requires id", manifestPath };
}
@@ -508,9 +508,9 @@ export function loadPluginManifest(
const autoEnableWhenConfiguredProviders = normalizeTrimmedStringList(
raw.autoEnableWhenConfiguredProviders,
);
const name = typeof raw.name === "string" ? raw.name.trim() : undefined;
const description = typeof raw.description === "string" ? raw.description.trim() : undefined;
const version = typeof raw.version === "string" ? raw.version.trim() : undefined;
const name = normalizeOptionalString(raw.name);
const description = normalizeOptionalString(raw.description);
const version = normalizeOptionalString(raw.version);
const channels = normalizeTrimmedStringList(raw.channels);
const providers = normalizeTrimmedStringList(raw.providers);
const modelSupport = normalizeManifestModelSupport(raw.modelSupport);
@@ -656,9 +656,7 @@ export function resolvePackageExtensionEntries(
if (!Array.isArray(raw)) {
return { status: "missing", entries: [] };
}
const entries = raw
.map((entry) => (typeof entry === "string" ? entry.trim() : ""))
.filter(Boolean);
const entries = raw.map((entry) => normalizeOptionalString(entry) ?? "").filter(Boolean);
if (entries.length === 0) {
return { status: "empty", entries: [] };
}