refactor: consolidate duplicate utility functions (#12439)

* refactor: consolidate duplicate utility functions

- Add escapeRegExp to src/utils.ts and remove 10 local duplicates
- Rename bash-tools clampNumber to clampWithDefault (different signature)
- Centralize formatError calls to use formatErrorMessage from infra/errors.ts
- Re-export formatErrorMessage from cli/cli-utils.ts to preserve API

* refactor: consolidate remaining escapeRegExp duplicates

* refactor: consolidate sleep, stripAnsi, and clamp duplicates
This commit is contained in:
max
2026-02-08 23:59:43 -08:00
committed by GitHub
parent 8968d9a339
commit ec910a235e
29 changed files with 67 additions and 146 deletions

View File

@@ -6,6 +6,7 @@ import {
type MSTeamsReplyStyle,
type ReplyPayload,
SILENT_REPLY_TOKEN,
sleep,
} from "openclaw/plugin-sdk";
import type { MSTeamsAccessTokenProvider } from "./attachments/types.js";
import type { StoredConversationReference } from "./conversation-store.js";
@@ -166,16 +167,6 @@ function clampMs(value: number, maxMs: number): number {
return Math.min(value, maxMs);
}
async function sleep(ms: number): Promise<void> {
const delay = Math.max(0, ms);
if (delay === 0) {
return;
}
await new Promise<void>((resolve) => {
setTimeout(resolve, delay);
});
}
function resolveRetryOptions(
retry: false | MSTeamsSendRetryOptions | undefined,
): Required<MSTeamsSendRetryOptions> & { enabled: boolean } {

View File

@@ -2,6 +2,7 @@ import type { Command } from "commander";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { sleep } from "openclaw/plugin-sdk";
import type { VoiceCallConfig } from "./config.js";
import type { VoiceCallRuntime } from "./runtime.js";
import { resolveUserPath } from "./utils.js";
@@ -40,10 +41,6 @@ function resolveDefaultStorePath(config: VoiceCallConfig): string {
return path.join(base, "calls.jsonl");
}
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export function registerVoiceCallCli(params: {
program: Command;
config: VoiceCallConfig;

View File

@@ -4,6 +4,7 @@ import {
collectWhatsAppStatusIssues,
createActionGate,
DEFAULT_ACCOUNT_ID,
escapeRegExp,
formatPairingApproveHint,
getChatChannelMeta,
isWhatsAppGroupJid,
@@ -33,8 +34,6 @@ import { getWhatsAppRuntime } from "./runtime.js";
const meta = getChatChannelMeta("whatsapp");
const escapeRegExp = (value: string) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
export const whatsappPlugin: ChannelPlugin<ResolvedWhatsAppAccount> = {
id: "whatsapp",
meta: {

View File

@@ -1,4 +1,5 @@
import { spawn, type SpawnOptions } from "node:child_process";
import { stripAnsi } from "openclaw/plugin-sdk";
import type { ZcaResult, ZcaRunOptions } from "./types.js";
const ZCA_BINARY = "zca";
@@ -107,11 +108,6 @@ export function runZcaInteractive(args: string[], options?: ZcaRunOptions): Prom
});
}
function stripAnsi(str: string): string {
// oxlint-disable-next-line no-control-regex
return str.replace(/\x1B\[[0-9;]*[a-zA-Z]/g, "");
}
export function parseJsonOutput<T>(stdout: string): T | null {
try {
return JSON.parse(stdout) as T;