fix(config): share json compatibility parsing

This commit is contained in:
Vincent Koc
2026-03-20 10:17:53 -07:00
parent 4838e3934b
commit a39c440d39
3 changed files with 13 additions and 20 deletions

View File

@@ -1,8 +1,8 @@
import fs from "node:fs";
import JSON5 from "json5";
import type { OpenClawConfig } from "../config/config.js";
import { resolveStorePath } from "../config/sessions/paths.js";
import { getSubagentDepth, parseAgentSessionKey } from "../sessions/session-key-utils.js";
import { parseJsonWithJson5Fallback } from "../utils/parse-json-compat.js";
import { resolveDefaultAgentId } from "./agent-scope.js";
type SessionDepthEntry = {
@@ -11,14 +11,6 @@ type SessionDepthEntry = {
spawnedBy?: unknown;
};
function parseSessionDepthStore(raw: string): unknown {
try {
return JSON.parse(raw);
} catch {
return JSON5.parse(raw);
}
}
function normalizeSpawnDepth(value: unknown): number | undefined {
if (typeof value === "number") {
return Number.isInteger(value) && value >= 0 ? value : undefined;
@@ -45,7 +37,7 @@ function normalizeSessionKey(value: unknown): string | undefined {
function readSessionStore(storePath: string): Record<string, SessionDepthEntry> {
try {
const raw = fs.readFileSync(storePath, "utf-8");
const parsed = parseSessionDepthStore(raw);
const parsed = parseJsonWithJson5Fallback(raw);
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
return parsed as Record<string, SessionDepthEntry>;
}