Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions apps/memos-local-openclaw/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import { SkillInstaller } from "./src/skill/installer";
import { Summarizer } from "./src/ingest/providers";
import { MEMORY_GUIDE_SKILL_MD } from "./src/skill/bundled-memory-guide";
import { Telemetry } from "./src/telemetry";
import { ensureToolsAllowEntry } from "./src/openclaw-config";
import { parseJsonOrJson5 } from "./src/shared/json5";
import { patchOpenclawAllowFile } from "./src/shared/openclaw-config";


/** Remove near-duplicate hits based on summary word overlap (>70%). Keeps first (highest-scored) hit. */
Expand Down Expand Up @@ -298,8 +299,8 @@ const memosLocalPlugin = {
const configPath = path.join(stateDir, "state", "memos-local", "config.json");
if (Object.keys(pluginCfg).length === 0 && fs.existsSync(configPath)) {
try {
const fileConfig = JSON.parse(fs.readFileSync(configPath, "utf-8"));
pluginCfg = fileConfig;
const fileConfig = parseJsonOrJson5(fs.readFileSync(configPath, "utf-8"));
pluginCfg = fileConfig as Record<string, unknown>;
api.logger.info(`memos-local: loaded config from ${configPath}`);
} catch (e) {
api.logger.warn(`memos-local: failed to load config from ${configPath}: ${e}`);
Expand Down Expand Up @@ -357,15 +358,17 @@ const memosLocalPlugin = {
ctx.log.warn(`memos-local: could not write to managed skills dir: ${e}`);
}

// Ensure plugin tools are enabled in openclaw.json tools.allow
// Ensure plugin tools are enabled in openclaw.json tools.allow.
// openclaw.json is JSON5 (supports // comments / single-quoted strings /
// trailing commas) — the patcher tolerates all of these (see issue #1543).
try {
const openclawJsonPath = path.join(stateDir, "openclaw.json");
if (fs.existsSync(openclawJsonPath)) {
const raw = fs.readFileSync(openclawJsonPath, "utf-8");
const patched = ensureToolsAllowEntry(raw, "group:plugins");
if (patched !== raw) {
fs.writeFileSync(openclawJsonPath, patched, "utf-8");
const result = patchOpenclawAllowFile(openclawJsonPath);
if (result.changed) {
ctx.log.info("memos-local: added 'group:plugins' to tools.allow in openclaw.json");
} else if (result.reason) {
ctx.log.debug(`memos-local: tools.allow not patched (${result.reason})`);
}
}
} catch (e) {
Expand Down
3 changes: 2 additions & 1 deletion apps/memos-local-openclaw/src/ingest/providers/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as fs from "fs";
import * as path from "path";
import type { SummarizerConfig, SummaryProvider, Logger, OpenClawAPI } from "../../types";
import { parseJsonOrJson5 } from "../../shared/json5";
import { summarizeOpenAI, summarizeTaskOpenAI, generateTaskTitleOpenAI, judgeNewTopicOpenAI, classifyTopicOpenAI, arbitrateTopicSplitOpenAI, filterRelevantOpenAI, judgeDedupOpenAI, parseFilterResult, parseDedupResult, parseTopicClassifyResult } from "./openai";
import type { FilterResult, DedupResult, TopicClassifyResult } from "./openai";
export type { FilterResult, DedupResult, TopicClassifyResult } from "./openai";
Expand Down Expand Up @@ -66,7 +67,7 @@ function loadOpenClawFallbackConfig(log: Logger): SummarizerConfig | undefined {
|| path.join(process.env.OPENCLAW_STATE_DIR || path.join(home, ".openclaw"), "openclaw.json");
if (!fs.existsSync(cfgPath)) return undefined;

const raw = JSON.parse(fs.readFileSync(cfgPath, "utf-8"));
const raw = parseJsonOrJson5(fs.readFileSync(cfgPath, "utf-8")) as any;

const agentModel: string | undefined = raw?.agents?.defaults?.model?.primary;
if (!agentModel) return undefined;
Expand Down
183 changes: 183 additions & 0 deletions apps/memos-local-openclaw/src/shared/json5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
/**
* Minimal JSON5-tolerant parser used to read `openclaw.json`.
*
* Background: OpenClaw stores its native config (`~/.openclaw/openclaw.json`)
* in JSON5 — it allows `//` line comments, `/* ... *​/` block comments,
* single-quoted strings, unquoted identifier keys, and trailing commas.
* Strict `JSON.parse` chokes on any of these (issue #1543).
*
* Bringing in the full `json5` npm package would be the obvious fix, but the
* plugin must avoid new runtime dependencies (it ships its `dependencies`
* one-by-one to keep install size small, and the install path runs in user
* machines where adding npm deps is sensitive). Instead, we normalize the raw
* text into strict JSON and hand it to the built-in `JSON.parse`.
*
* What is supported:
* • `//` line comments and `/* … *​/` block comments
* • single-quoted strings (`'foo'`)
* • unquoted identifier-style object keys (`tools: { ... }`)
* • trailing commas before `}` or `]`
* • UTF-8 BOM
*
* What is NOT supported (rare in practice for openclaw.json):
* • Hex numbers (`0xFF`), `+Infinity`, `NaN`, etc. — falls back to error.
* • Multi-line string continuation.
*
* The implementation walks the text character-by-character with a small state
* machine so that comment / quote rewriting never touches characters inside
* string literals (which is the classic regex-based-stripper bug).
*/

export class Json5ParseError extends Error {
constructor(message: string, public readonly originalError?: unknown) {
super(message);
this.name = "Json5ParseError";
}
}

/**
* Strip comments, normalize quotes/keys/trailing commas, then `JSON.parse`.
*
* Throws Json5ParseError if the normalized text still isn't valid JSON —
* callers should treat this the same as a malformed `JSON.parse`.
*/
export function parseJson5(text: string): unknown {
if (typeof text !== "string") {
throw new Json5ParseError(`parseJson5 expects a string, got ${typeof text}`);
}

const normalized = normalizeJson5(text);
try {
return JSON.parse(normalized);
} catch (err) {
throw new Json5ParseError(
`Failed to parse JSON5 content as JSON after normalization: ${(err as Error).message}`,
err,
);
}
}

/**
* Lenient variant: try strict `JSON.parse` first (fast path for files that
* happen to be plain JSON), fall back to the JSON5-tolerant path on failure.
*
* This is the recommended entry point for code that reads `openclaw.json`,
* because the user's config might or might not contain comments.
*/
export function parseJsonOrJson5(text: string): unknown {
// Fast path: strict JSON. Use it when possible so we don't pay normalization
// cost on every read.
try {
return JSON.parse(text);
} catch {
return parseJson5(text);
}
}

/**
* Normalize a JSON5 text into strict JSON-compatible text.
* Exposed for testing; production callers should use parseJson5 / parseJsonOrJson5.
*/
export function normalizeJson5(input: string): string {
// Strip UTF-8 BOM
let text = input.charCodeAt(0) === 0xfeff ? input.slice(1) : input;

const out: string[] = [];
const n = text.length;
let i = 0;

while (i < n) {
const c = text[i];
const next = i + 1 < n ? text[i + 1] : "";

// Line comment
if (c === "/" && next === "/") {
i += 2;
while (i < n && text[i] !== "\n") i++;
continue;
}
// Block comment
if (c === "/" && next === "*") {
i += 2;
while (i < n && !(text[i] === "*" && text[i + 1] === "/")) i++;
if (i < n) i += 2; // skip closing */
continue;
}

// Double-quoted string — passthrough verbatim, including escapes.
if (c === '"') {
out.push(c);
i++;
while (i < n) {
const ch = text[i];
out.push(ch);
if (ch === "\\" && i + 1 < n) {
out.push(text[i + 1]);
i += 2;
continue;
}
if (ch === '"') {
i++;
break;
}
i++;
}
continue;
}

// Single-quoted string — rewrite to double-quoted, escaping any embedded `"`.
if (c === "'") {
out.push('"');
i++;
while (i < n) {
const ch = text[i];
if (ch === "\\" && i + 1 < n) {
// Preserve escape sequence verbatim, except `\'` becomes a plain `'`
// (because the surrounding quotes are now double quotes).
const nx = text[i + 1];
if (nx === "'") {
out.push("'");
} else {
out.push("\\");
out.push(nx);
}
i += 2;
continue;
}
if (ch === "'") {
out.push('"');
i++;
break;
}
if (ch === '"') {
// Escape a bare `"` so the resulting double-quoted string stays valid.
out.push("\\\"");
i++;
continue;
}
out.push(ch);
i++;
}
continue;
}

out.push(c);
i++;
}

let stripped = out.join("");

// Unquoted identifier keys: ` foo: 1` → ` "foo": 1`.
// Matches an identifier that starts with a letter / `_` / `$` and is
// immediately followed by optional whitespace and `:`. The leading
// boundary ensures we don't match parts of other tokens.
stripped = stripped.replace(
/([\{,\s])([A-Za-z_$][A-Za-z0-9_$]*)(\s*:)/g,
(_, lead: string, key: string, tail: string) => `${lead}"${key}"${tail}`,
);

// Trailing commas before `}` or `]`.
stripped = stripped.replace(/,(\s*[}\]])/g, "$1");

return stripped;
}
3 changes: 2 additions & 1 deletion apps/memos-local-openclaw/src/shared/llm-call.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as fs from "fs";
import * as path from "path";
import type { SummarizerConfig, SummaryProvider, Logger, PluginContext, OpenClawAPI } from "../types";
import { parseJsonOrJson5 } from "./json5";

/**
* Resolve a SecretInput (string | SecretRef) to a plain string.
Expand Down Expand Up @@ -54,7 +55,7 @@ export function loadOpenClawFallbackConfig(log: Logger): SummarizerConfig | unde
|| path.join(process.env.OPENCLAW_STATE_DIR || path.join(home, ".openclaw"), "openclaw.json");
if (!fs.existsSync(cfgPath)) return undefined;

const raw = JSON.parse(fs.readFileSync(cfgPath, "utf-8"));
const raw = parseJsonOrJson5(fs.readFileSync(cfgPath, "utf-8")) as any;

const agentModel: string | undefined = raw?.agents?.defaults?.model?.primary;
if (!agentModel) return undefined;
Expand Down
96 changes: 96 additions & 0 deletions apps/memos-local-openclaw/src/shared/openclaw-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import * as fs from "fs";
import { parseJsonOrJson5 } from "./json5";

/**
* Ensure `"group:plugins"` is present in `tools.allow` inside `openclaw.json`.
*
* Strategy:
* 1. Read the file as JSON5 (the user may have // comments etc. — see issue #1543).
* 2. If `tools.allow` is missing, wildcarded (`"*"`), or already contains
* `"group:plugins"`, do nothing.
* 3. Otherwise patch the raw text by appending `"group:plugins"` after the
* last existing allow entry. We do textual insertion (rather than
* `JSON.stringify(cfg)`) so we preserve the user's comments and
* formatting in the file.
*
* Returns:
* - `{ changed: false }` when no edit was needed or the patch could not be
* anchored safely.
* - `{ changed: true, patched }` when the caller should `writeFileSync` the
* `patched` text back.
*/
export interface EnsureGroupPluginsAllowedResult {
changed: boolean;
patched?: string;
reason?: string;
}

const PATCH_VALUE = "group:plugins";

export function ensureGroupPluginsAllowed(
raw: string,
): EnsureGroupPluginsAllowedResult {
let cfg: any;
try {
cfg = parseJsonOrJson5(raw);
} catch (err) {
return { changed: false, reason: `parse failed: ${(err as Error).message}` };
}

const allow: unknown = cfg?.tools?.allow;
if (!Array.isArray(allow) || allow.length === 0) {
return { changed: false, reason: "tools.allow is missing or empty" };
}
if (allow.includes("*") || allow.includes(PATCH_VALUE)) {
return { changed: false, reason: "tools.allow already permissive enough" };
}

const lastEntry = allow[allow.length - 1];
if (typeof lastEntry !== "string") {
return { changed: false, reason: "tools.allow last entry is not a string" };
}

// Try anchoring on the last entry as either a double-quoted or single-quoted
// string — openclaw.json is JSON5 and the user may have used either. We also
// tolerate an optional trailing comma between the last entry and `]`.
const escaped = lastEntry.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
const candidates: Array<{ anchor: RegExp; insert: string }> = [
{
anchor: new RegExp(`("${escaped}")(\\s*,?\\s*\\])`),
insert: `$1,\n "${PATCH_VALUE}"$2`,
},
{
anchor: new RegExp(`('${escaped}')(\\s*,?\\s*\\])`),
insert: `$1,\n '${PATCH_VALUE}'$2`,
},
];

for (const { anchor, insert } of candidates) {
if (anchor.test(raw)) {
const patched = raw.replace(anchor, insert);
if (patched !== raw && patched.includes(PATCH_VALUE)) {
return { changed: true, patched };
}
}
}

return { changed: false, reason: "could not anchor last allow entry" };
}

/**
* File-level convenience wrapper. Reads `openclawJsonPath`, applies
* `ensureGroupPluginsAllowed`, writes it back if needed.
*
* Caller is expected to pre-check `fs.existsSync(openclawJsonPath)`.
* Returns the in-memory result so the caller can log.
*/
export function patchOpenclawAllowFile(
openclawJsonPath: string,
): EnsureGroupPluginsAllowedResult {
const raw = fs.readFileSync(openclawJsonPath, "utf-8");
const result = ensureGroupPluginsAllowed(raw);
if (result.changed && result.patched) {
fs.writeFileSync(openclawJsonPath, result.patched, "utf-8");
}
return result;
}
Loading
Loading