mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-20 05:31:30 +00:00
refactor: dedupe nullable string helper
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { resolveSystemRunApprovalRuntimeContext } from "../infra/system-run-approval-context.js";
|
||||
import { resolveSystemRunCommandRequest } from "../infra/system-run-command.js";
|
||||
import { asNullableRecord } from "../shared/record-coerce.js";
|
||||
import { normalizeNullableString } from "../shared/string-coerce.js";
|
||||
import type { ExecApprovalRecord } from "./exec-approval-manager.js";
|
||||
import {
|
||||
systemRunApprovalGuardError,
|
||||
@@ -40,16 +41,8 @@ type ApprovalClient = {
|
||||
} | null;
|
||||
};
|
||||
|
||||
function normalizeString(value: unknown): string | null {
|
||||
if (typeof value !== "string") {
|
||||
return null;
|
||||
}
|
||||
const trimmed = value.trim();
|
||||
return trimmed ? trimmed : null;
|
||||
}
|
||||
|
||||
function normalizeApprovalDecision(value: unknown): "allow-once" | "allow-always" | null {
|
||||
const s = normalizeString(value);
|
||||
const s = normalizeNullableString(value);
|
||||
return s === "allow-once" || s === "allow-always" ? s : null;
|
||||
}
|
||||
|
||||
@@ -125,7 +118,7 @@ export function sanitizeSystemRunParamsForForwarding(opts: {
|
||||
return { ok: true, params: next };
|
||||
}
|
||||
|
||||
const runId = normalizeString(p.runId);
|
||||
const runId = normalizeNullableString(p.runId);
|
||||
if (!runId) {
|
||||
return systemRunApprovalGuardError({
|
||||
code: "MISSING_RUN_ID",
|
||||
@@ -159,7 +152,7 @@ export function sanitizeSystemRunParamsForForwarding(opts: {
|
||||
});
|
||||
}
|
||||
|
||||
const targetNodeId = normalizeString(opts.nodeId);
|
||||
const targetNodeId = normalizeNullableString(opts.nodeId);
|
||||
if (!targetNodeId) {
|
||||
return systemRunApprovalGuardError({
|
||||
code: "MISSING_NODE_ID",
|
||||
@@ -167,7 +160,7 @@ export function sanitizeSystemRunParamsForForwarding(opts: {
|
||||
details: { runId },
|
||||
});
|
||||
}
|
||||
const approvalNodeId = normalizeString(snapshot.request.nodeId);
|
||||
const approvalNodeId = normalizeNullableString(snapshot.request.nodeId);
|
||||
if (!approvalNodeId) {
|
||||
return systemRunApprovalGuardError({
|
||||
code: "APPROVAL_NODE_BINDING_MISSING",
|
||||
|
||||
@@ -19,6 +19,7 @@ import {
|
||||
resolveInlineCommandMatch,
|
||||
} from "../infra/shell-inline-command.js";
|
||||
import { formatExecCommand, resolveSystemRunCommandRequest } from "../infra/system-run-command.js";
|
||||
import { normalizeNullableString } from "../shared/string-coerce.js";
|
||||
import { splitShellArgs } from "../utils/shell-argv.js";
|
||||
|
||||
export type ApprovedCwdSnapshot = {
|
||||
@@ -195,14 +196,6 @@ type FileOperandCollection = {
|
||||
sawOptionValueFile: boolean;
|
||||
};
|
||||
|
||||
function normalizeString(value: unknown): string | null {
|
||||
if (typeof value !== "string") {
|
||||
return null;
|
||||
}
|
||||
const trimmed = value.trim();
|
||||
return trimmed ? trimmed : null;
|
||||
}
|
||||
|
||||
function pathComponentsFromRootSync(targetPath: string): string[] {
|
||||
const absolute = path.resolve(targetPath);
|
||||
const parts: string[] = [];
|
||||
@@ -1178,7 +1171,7 @@ export function buildSystemRunApprovalPlan(params: {
|
||||
approvedByAsk: true,
|
||||
argv: command.argv,
|
||||
shellCommand: command.shellPayload,
|
||||
cwd: normalizeString(params.cwd) ?? undefined,
|
||||
cwd: normalizeNullableString(params.cwd) ?? undefined,
|
||||
});
|
||||
if (!hardening.ok) {
|
||||
return { ok: false, message: hardening.message };
|
||||
@@ -1203,8 +1196,8 @@ export function buildSystemRunApprovalPlan(params: {
|
||||
cwd: hardening.cwd ?? null,
|
||||
commandText,
|
||||
commandPreview,
|
||||
agentId: normalizeString(params.agentId),
|
||||
sessionKey: normalizeString(params.sessionKey),
|
||||
agentId: normalizeNullableString(params.agentId),
|
||||
sessionKey: normalizeNullableString(params.sessionKey),
|
||||
mutableFileOperand: mutableFileOperand.snapshot ?? undefined,
|
||||
},
|
||||
};
|
||||
|
||||
7
src/shared/string-coerce.ts
Normal file
7
src/shared/string-coerce.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export function normalizeNullableString(value: unknown): string | null {
|
||||
if (typeof value !== "string") {
|
||||
return null;
|
||||
}
|
||||
const trimmed = value.trim();
|
||||
return trimmed ? trimmed : null;
|
||||
}
|
||||
Reference in New Issue
Block a user