Files
SenMeshNet/electron/bridges/aiBridge/sdk/claudeConfig.cjs
zhaolei 2f9f8da48e
Some checks failed
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
Initial commit: NetMesh smart ops platform with 3D topology, inspection reports, RCA and AI ops assistant
2026-10-15 13:53:04 +08:00

75 lines
2.2 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use strict";
/**
* Repair ~/.claude.json before the claude-agent-sdk subprocess reads it.
* 1:1 port of craft options.ts ensureClaudeConfig(): a missing/empty/BOM-
* prefixed/corrupted config (or a stale .backup / .corrupted.* sibling) makes
* the Claude Code binary write plain-text recovery messages to stdout, which
* the SDK transport rejects as "CLI output was not valid JSON".
*/
const { join } = require("node:path");
const { homedir } = require("node:os");
const { existsSync, readFileSync, writeFileSync, unlinkSync, readdirSync } = require("node:fs");
const UTF8_BOM = "";
let claudeConfigChecked = false;
function writeConfigSafe(configPath, content) {
try {
writeFileSync(configPath, content, "utf-8");
} catch (err) {
const code = err && err.code;
if (process.platform === "win32" && (code === "EBUSY" || code === "EPERM")) {
const start = Date.now();
while (Date.now() - start < 100) { /* brief busy wait, runs once at startup */ }
try { writeFileSync(configPath, content, "utf-8"); } catch { /* best effort */ }
}
}
}
function ensureClaudeConfig() {
if (claudeConfigChecked) return;
claudeConfigChecked = true;
const configPath = join(homedir(), ".claude.json");
const backupPath = `${configPath}.backup`;
if (existsSync(backupPath)) {
try { unlinkSync(backupPath); } catch { /* best effort */ }
}
try {
const homeDir = homedir();
for (const file of readdirSync(homeDir)) {
if (file.startsWith(".claude.json.corrupted.")) {
try { unlinkSync(join(homeDir, file)); } catch { /* best effort */ }
}
}
} catch { /* ignore — main repair below still runs */ }
if (!existsSync(configPath)) {
writeConfigSafe(configPath, "{}");
return;
}
try {
const raw = readFileSync(configPath, "utf-8");
const content = raw.startsWith(UTF8_BOM) ? raw.slice(1) : raw;
const hasBom = raw !== content;
if (content.trim().length === 0) {
writeConfigSafe(configPath, "{}");
return;
}
JSON.parse(content);
if (hasBom) writeConfigSafe(configPath, content);
} catch {
writeConfigSafe(configPath, "{}");
}
}
function resetClaudeConfigCheck() {
claudeConfigChecked = false;
}
module.exports = { ensureClaudeConfig, resetClaudeConfigCheck };