|
| 1 | +// Applies immutable path removals to config-like objects. |
| 2 | +import { isDeepStrictEqual } from "node:util"; |
| 3 | +import { expectDefined } from "@openclaw/normalization-core"; |
| 4 | +import { isBlockedObjectKey } from "../infra/prototype-keys.js"; |
| 5 | +import { parseConfigPathArrayIndex } from "../shared/path-array-index.js"; |
| 6 | +import type { OpenClawConfig } from "./types.js"; |
| 7 | + |
| 8 | +const MANAGED_CONFIG_UNSET_PATHS = [["plugins", "installs"]] as const; |
| 9 | +const WRITE_PRUNED_OBJECT = Symbol("write-pruned-object"); |
| 10 | + |
| 11 | +function isWritePlainObject(value: unknown): value is Record<string, unknown> { |
| 12 | + return Boolean(value) && typeof value === "object" && !Array.isArray(value); |
| 13 | +} |
| 14 | + |
| 15 | +function coerceConfig(value: unknown): OpenClawConfig { |
| 16 | + if (!value || typeof value !== "object" || Array.isArray(value)) { |
| 17 | + return {}; |
| 18 | + } |
| 19 | + return value as OpenClawConfig; |
| 20 | +} |
| 21 | + |
| 22 | +function unsetPathForWriteAt( |
| 23 | + value: unknown, |
| 24 | + pathSegments: string[], |
| 25 | + depth: number, |
| 26 | +): { changed: boolean; value: unknown } { |
| 27 | + if (depth >= pathSegments.length) { |
| 28 | + return { changed: false, value }; |
| 29 | + } |
| 30 | + const segment = expectDefined(pathSegments[depth], "path segments entry at depth"); |
| 31 | + const isLeaf = depth === pathSegments.length - 1; |
| 32 | + |
| 33 | + if (Array.isArray(value)) { |
| 34 | + const index = parseConfigPathArrayIndex(segment); |
| 35 | + if (index === undefined || index >= value.length) { |
| 36 | + return { changed: false, value }; |
| 37 | + } |
| 38 | + if (isLeaf) { |
| 39 | + const next = value.slice(); |
| 40 | + next.splice(index, 1); |
| 41 | + return { changed: true, value: next }; |
| 42 | + } |
| 43 | + const child = unsetPathForWriteAt(value[index], pathSegments, depth + 1); |
| 44 | + if (!child.changed) { |
| 45 | + return { changed: false, value }; |
| 46 | + } |
| 47 | + const next = value.slice(); |
| 48 | + if (child.value === WRITE_PRUNED_OBJECT) { |
| 49 | + next.splice(index, 1); |
| 50 | + } else { |
| 51 | + next[index] = child.value; |
| 52 | + } |
| 53 | + return { changed: true, value: next }; |
| 54 | + } |
| 55 | + |
| 56 | + if (isBlockedObjectKey(segment) || !isWritePlainObject(value) || !Object.hasOwn(value, segment)) { |
| 57 | + return { changed: false, value }; |
| 58 | + } |
| 59 | + if (isLeaf) { |
| 60 | + const next: Record<string, unknown> = { ...value }; |
| 61 | + delete next[segment]; |
| 62 | + return { |
| 63 | + changed: true, |
| 64 | + value: Object.keys(next).length === 0 ? WRITE_PRUNED_OBJECT : next, |
| 65 | + }; |
| 66 | + } |
| 67 | + |
| 68 | + const child = unsetPathForWriteAt(value[segment], pathSegments, depth + 1); |
| 69 | + if (!child.changed) { |
| 70 | + return { changed: false, value }; |
| 71 | + } |
| 72 | + const next: Record<string, unknown> = { ...value }; |
| 73 | + if (child.value === WRITE_PRUNED_OBJECT) { |
| 74 | + delete next[segment]; |
| 75 | + } else { |
| 76 | + next[segment] = child.value; |
| 77 | + } |
| 78 | + return { |
| 79 | + changed: true, |
| 80 | + value: Object.keys(next).length === 0 ? WRITE_PRUNED_OBJECT : next, |
| 81 | + }; |
| 82 | +} |
| 83 | + |
| 84 | +function unsetPathForWrite( |
| 85 | + root: OpenClawConfig, |
| 86 | + pathSegments: string[], |
| 87 | +): { changed: boolean; next: OpenClawConfig } { |
| 88 | + if (pathSegments.length === 0) { |
| 89 | + return { changed: false, next: root }; |
| 90 | + } |
| 91 | + const result = unsetPathForWriteAt(root, pathSegments, 0); |
| 92 | + if (!result.changed) { |
| 93 | + return { changed: false, next: root }; |
| 94 | + } |
| 95 | + if (result.value === WRITE_PRUNED_OBJECT) { |
| 96 | + return { changed: true, next: {} }; |
| 97 | + } |
| 98 | + if (isWritePlainObject(result.value)) { |
| 99 | + return { changed: true, next: coerceConfig(result.value) }; |
| 100 | + } |
| 101 | + return { changed: false, next: root }; |
| 102 | +} |
| 103 | + |
| 104 | +export function applyUnsetPathsForWrite( |
| 105 | + root: OpenClawConfig, |
| 106 | + unsetPaths: readonly string[][] | undefined, |
| 107 | +): OpenClawConfig { |
| 108 | + let next = root; |
| 109 | + for (const unsetPath of unsetPaths ?? []) { |
| 110 | + if (!Array.isArray(unsetPath) || unsetPath.length === 0) { |
| 111 | + continue; |
| 112 | + } |
| 113 | + const unsetResult = unsetPathForWrite(next, unsetPath); |
| 114 | + if (unsetResult.changed) { |
| 115 | + next = unsetResult.next; |
| 116 | + } |
| 117 | + } |
| 118 | + return next; |
| 119 | +} |
| 120 | + |
| 121 | +export function resolveManagedUnsetPathsForWrite( |
| 122 | + unsetPaths: readonly string[][] | undefined, |
| 123 | +): string[][] { |
| 124 | + const next: string[][] = []; |
| 125 | + for (const managedPath of MANAGED_CONFIG_UNSET_PATHS) { |
| 126 | + next.push(Array.from(managedPath)); |
| 127 | + } |
| 128 | + for (const unsetPath of unsetPaths ?? []) { |
| 129 | + if (!Array.isArray(unsetPath) || unsetPath.length === 0) { |
| 130 | + continue; |
| 131 | + } |
| 132 | + if (next.some((existing) => isDeepStrictEqual(existing, unsetPath))) { |
| 133 | + continue; |
| 134 | + } |
| 135 | + next.push([...unsetPath]); |
| 136 | + } |
| 137 | + return next; |
| 138 | +} |
0 commit comments