Files
openclaw/extensions/linux-node/src/command-utils.ts
T
Peter Steinberger 964c8c84c1 refactor: consolidate coercion ownership (#122299)
* refactor: consolidate coercion ownership

Centralize four canonical coercion helpers, migrate exact core and plugin duplicates through narrow Plugin SDK facades, and enforce declaration and plugin-normalization ownership boundaries.

The sweep adds eight focused SDK exports while deleting more production and tooling code than it adds. User-visible behavior is unchanged except for safer equivalent object and UI parsing at existing boundaries.

* fix: guard integer option ownership

Register resolveIntegerOption with the canonical function owner and extend the declaration-guard fixture so future local duplicates fail validation.

* fix: keep integer helpers on numeric facade

Remove the unshipped duplicate string-coerce exports and route every affected plugin consumer through the existing number-runtime contract.

* fix: point numeric coercion to number runtime

Make boundary and declaration diagnostics recommend the canonical numeric facade, with failing-before coverage for both guidance paths.
2026-08-11 17:14:53 -07:00

52 lines
1.8 KiB
TypeScript

import type { OpenClawPluginNodeHostCommandAvailabilityContext } from "openclaw/plugin-sdk/plugin-entry";
import type { CommandOptions, SpawnResult } from "openclaw/plugin-sdk/process-runtime";
import { asFiniteNumber, asNonArrayRecord } from "openclaw/plugin-sdk/string-coerce-runtime";
import { truncateUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime";
import {
resolveLinuxNodePluginConfigFromHost,
type ResolvedLinuxNodePluginConfig,
} from "./config.js";
export type RunCommand = (argv: string[], options: CommandOptions) => Promise<SpawnResult>;
export function parseParams(paramsJSON: string | null | undefined): Record<string, unknown> {
if (!paramsJSON) {
return {};
}
try {
const parsed = JSON.parse(paramsJSON) as unknown;
return asNonArrayRecord(parsed);
} catch {
return {};
}
}
export { asFiniteNumber as readFiniteNumber };
export function clamp(value: number, minimum: number, maximum: number): number {
return Math.min(maximum, Math.max(minimum, value));
}
export function formatToolError(result: SpawnResult): string {
const detail = result.stderr.trim() || result.stdout.trim();
return detail
? truncateUtf16Safe(detail.replaceAll(/\s+/gu, " "), 300)
: `exit ${result.code ?? "unknown"}`;
}
export function assertToolResult(result: SpawnResult, code: string): void {
if (result.termination === "timeout" || result.termination === "no-output-timeout") {
throw new Error(`${code}: command timed out`);
}
if (result.code !== 0) {
throw new Error(`${code}: ${formatToolError(result)}`);
}
}
export function isCapabilityEnabledForHost(
context: OpenClawPluginNodeHostCommandAvailabilityContext,
capability: keyof ResolvedLinuxNodePluginConfig,
): boolean {
return resolveLinuxNodePluginConfigFromHost(context.config)?.[capability].enabled === true;
}