refactor: dedupe plugin string helpers

This commit is contained in:
Peter Steinberger
2026-04-07 00:27:30 +01:00
parent 59ccea334d
commit 560a7aecd0
5 changed files with 14 additions and 25 deletions

View File

@@ -1,6 +1,7 @@
import crypto from "node:crypto";
import fs from "node:fs/promises";
import path from "node:path";
import { normalizeOptionalString } from "openclaw/plugin-sdk/text-runtime";
import type { PluginLogger } from "../api.js";
import type { DiffArtifactContext, DiffArtifactMeta, DiffOutputFormat } from "./types.js";
@@ -379,7 +380,3 @@ function normalizeArtifactContext(value: unknown): DiffArtifactContext | undefin
return Object.values(context).some((entry) => entry !== undefined) ? context : undefined;
}
function normalizeOptionalString(value: unknown): string | undefined {
return typeof value === "string" && value.trim().length > 0 ? value.trim() : undefined;
}

View File

@@ -1,7 +1,7 @@
import { existsSync, readFileSync } from "node:fs";
import { homedir } from "node:os";
import { join } from "node:path";
import { isRecord } from "openclaw/plugin-sdk/text-runtime";
import { isRecord, normalizeOptionalString } from "openclaw/plugin-sdk/text-runtime";
type OAuthSettingsFs = {
existsSync: (path: Parameters<typeof existsSync>[0]) => ReturnType<typeof existsSync>;
@@ -28,10 +28,6 @@ type GeminiCliAuthSettings = {
enforcedAuthType?: unknown;
};
function readString(value: unknown): string | undefined {
return typeof value === "string" && value.trim() ? value.trim() : undefined;
}
function readSettingsFile(): GeminiCliAuthSettings | null {
const settingsPath = join(oauthSettingsFs.homedir(), ".gemini", "settings.json");
if (!oauthSettingsFs.existsSync(settingsPath)) {
@@ -55,10 +51,10 @@ export function resolveGeminiCliSelectedAuthType(): string | undefined {
const security = isRecord(settings.security) ? settings.security : undefined;
const auth = isRecord(security?.auth) ? security.auth : undefined;
const selectedAuthType =
readString(auth?.selectedType) ??
readString(auth?.enforcedType) ??
readString(settings.selectedAuthType) ??
readString(settings.enforcedAuthType);
normalizeOptionalString(auth?.selectedType) ??
normalizeOptionalString(auth?.enforcedType) ??
normalizeOptionalString(settings.selectedAuthType) ??
normalizeOptionalString(settings.enforcedAuthType);
if (selectedAuthType) {
return selectedAuthType;
}

View File

@@ -1,4 +1,5 @@
import path from "node:path";
import { normalizeOptionalString } from "openclaw/plugin-sdk/text-runtime";
import YAML from "yaml";
export const WIKI_PAGE_KINDS = ["entity", "concept", "source", "synthesis", "report"] as const;
@@ -34,10 +35,6 @@ export type WikiPageSummary = {
updatedAt?: string;
};
function normalizeOptionalString(value: unknown): string | undefined {
return typeof value === "string" && value.trim() ? value.trim() : undefined;
}
const FRONTMATTER_PATTERN = /^---\n([\s\S]*?)\n---\n?/;
const OBSIDIAN_LINK_PATTERN = /\[\[([^\]|]+)(?:\|[^\]]+)?\]\]/g;
const MARKDOWN_LINK_PATTERN = /\[[^\]]+\]\(([^)]+)\)/g;

View File

@@ -1,3 +1,5 @@
import { normalizeOptionalString } from "openclaw/plugin-sdk/text-runtime";
export type SlackModalPrivateMetadata = {
sessionKey?: string;
channelId?: string;
@@ -7,10 +9,6 @@ export type SlackModalPrivateMetadata = {
const SLACK_PRIVATE_METADATA_MAX = 3000;
function normalizeString(value: unknown) {
return typeof value === "string" && value.trim().length > 0 ? value.trim() : undefined;
}
export function parseSlackModalPrivateMetadata(raw: unknown): SlackModalPrivateMetadata {
if (typeof raw !== "string" || raw.trim().length === 0) {
return {};
@@ -18,10 +16,10 @@ export function parseSlackModalPrivateMetadata(raw: unknown): SlackModalPrivateM
try {
const parsed = JSON.parse(raw) as Record<string, unknown>;
return {
sessionKey: normalizeString(parsed.sessionKey),
channelId: normalizeString(parsed.channelId),
channelType: normalizeString(parsed.channelType),
userId: normalizeString(parsed.userId),
sessionKey: normalizeOptionalString(parsed.sessionKey),
channelId: normalizeOptionalString(parsed.channelId),
channelType: normalizeOptionalString(parsed.channelType),
userId: normalizeOptionalString(parsed.userId),
};
} catch {
return {};

View File

@@ -11,6 +11,7 @@ export * from "../markdown/render.js";
export * from "../markdown/tables.js";
export * from "../shared/global-singleton.js";
export * from "../shared/scoped-expiring-id-cache.js";
export * from "../shared/string-coerce.js";
export * from "../shared/string-normalization.js";
export * from "../shared/string-sample.js";
export * from "../shared/text/assistant-visible-text.js";