refactor: dedupe shared string predicates

This commit is contained in:
Peter Steinberger
2026-04-07 00:35:12 +01:00
parent f178a9dc41
commit 899f490c9c
6 changed files with 22 additions and 20 deletions

View File

@@ -1,10 +1,12 @@
import { asNullableRecord, isRecord } from "openclaw/plugin-sdk/text-runtime";
import {
asNullableRecord,
hasNonEmptyString as sharedHasNonEmptyString,
isRecord,
} from "openclaw/plugin-sdk/text-runtime";
export { asNullableRecord as asRecord, isRecord };
export function hasNonEmptyString(value: unknown): value is string {
return typeof value === "string" && value.trim().length > 0;
}
export const hasNonEmptyString = sharedHasNonEmptyString;
export function normalizeString(value: unknown): string | undefined {
if (typeof value === "string") {

View File

@@ -1,4 +1,8 @@
import { asOptionalRecord, normalizeOptionalString } from "openclaw/plugin-sdk/text-runtime";
import {
asOptionalRecord,
hasNonEmptyString as sharedHasNonEmptyString,
normalizeOptionalString,
} from "openclaw/plugin-sdk/text-runtime";
export function encodeQuery(params: Record<string, string | undefined>): string {
const query = new URLSearchParams();
@@ -24,9 +28,7 @@ export function isRecord(value: unknown): value is Record<string, unknown> {
export const asRecord = asOptionalRecord;
export function hasNonEmptyString(value: unknown): value is string {
return typeof value === "string" && value.trim().length > 0;
}
export const hasNonEmptyString = sharedHasNonEmptyString;
export function extractCommentElementText(element: unknown): string | undefined {
if (!isRecord(element)) {

View File

@@ -1,4 +1,5 @@
export { asNullableRecord as asRecord } from "openclaw/plugin-sdk/text-runtime";
export { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime";
export function normalizeTrimmedString(value: unknown): string | undefined {
if (typeof value !== "string") {
@@ -7,7 +8,3 @@ export function normalizeTrimmedString(value: unknown): string | undefined {
const trimmed = value.trim();
return trimmed.length > 0 ? trimmed : undefined;
}
export function formatErrorMessage(err: unknown): string {
return err instanceof Error ? err.message : String(err);
}

View File

@@ -1,3 +1,4 @@
import { formatErrorMessage as sharedFormatErrorMessage } from "openclaw/plugin-sdk/error-runtime";
import { asNullableObjectRecord } from "openclaw/plugin-sdk/text-runtime";
import { normalizeShip } from "../targets.js";
@@ -183,10 +184,7 @@ export async function resolveAuthorizedMessageText(params: {
}
export const asRecord = asNullableObjectRecord;
export function formatErrorMessage(error: unknown): string {
return error instanceof Error ? error.message : String(error);
}
export const formatErrorMessage = sharedFormatErrorMessage;
export function readString(
record: Record<string, unknown> | null,

View File

@@ -1,15 +1,14 @@
import { hasNonEmptyString as sharedHasNonEmptyString } from "../../shared/string-coerce.js";
import { MESSAGE_ACTION_TARGET_MODE } from "./message-action-spec.js";
export const hasNonEmptyString = sharedHasNonEmptyString;
export const CHANNEL_TARGET_DESCRIPTION =
"Recipient/channel: E.164 for WhatsApp/Signal, Telegram chat id/@username, Discord/Slack channel/user, or iMessage handle/chat_id";
export const CHANNEL_TARGETS_DESCRIPTION =
"Recipient/channel targets (same format as --target); accepts ids or names when the directory is available.";
export function hasNonEmptyString(value: unknown): value is string {
return typeof value === "string" && value.trim().length > 0;
}
export function applyTargetToParams(params: {
action: string;
args: Record<string, unknown>;

View File

@@ -9,3 +9,7 @@ export function normalizeNullableString(value: unknown): string | null {
export function normalizeOptionalString(value: unknown): string | undefined {
return normalizeNullableString(value) ?? undefined;
}
export function hasNonEmptyString(value: unknown): value is string {
return typeof value === "string" && value.trim().length > 0;
}