insert try cathc method at save permissions function

This commit is contained in:
duffyduck 2026-03-14 17:26:36 +01:00
parent 800a57d28a
commit 9f2d898d82
1 changed files with 28 additions and 24 deletions

View File

@ -1542,25 +1542,26 @@ async function handleSavePermissions(clientWs, allowedTools) {
`const tools=${JSON.stringify(allowedTools)};`,
// Claude Code Format: Tool-Namen mit (*) Glob-Pattern
'const ccAllow=tools.map(t=>t+"(*)");',
'for(const f of paths){',
'const ok=[];const fail=[];',
'for(const f of paths){try{',
'let s={};try{s=JSON.parse(fs.readFileSync(f,"utf8"));}catch(e){}',
// OpenClaw Format
's.allowedTools=tools;',
// Claude Code Format
'if(!s.permissions)s.permissions={};',
's.permissions.allow=ccAllow;',
's.permissions.deny=[];',
'const dir=path.dirname(f);',
'try{fs.mkdirSync(dir,{recursive:true});}catch(e){}',
'fs.writeFileSync(f,JSON.stringify(s,null,2));',
'}',
// Verify: alle Pfade zuruecklesen
'const result={};',
'for(const f of paths){',
'ok.push(f);',
'}catch(e){fail.push(f+": "+e.message);}}',
// Verify: nur erfolgreiche Pfade zuruecklesen
'const result={ok:[],fail:fail,verified:{}};',
'for(const f of ok){',
'try{const d=JSON.parse(fs.readFileSync(f,"utf8"));',
'result[f]={allowedTools:d.allowedTools||[],permsAllow:d.permissions&&d.permissions.allow||[]};}',
'catch(e){result[f]={error:e.message};}',
'result.verified[f]={allowedTools:d.allowedTools||[],permsAllow:(d.permissions&&d.permissions.allow)||[]};}',
'catch(e){result.fail.push(f+": verify-read "+e.message);}',
'}',
'result.ok=ok;',
'process.stdout.write(JSON.stringify(result));',
].join("");
const b64 = Buffer.from(script).toString("base64");
@ -1572,21 +1573,20 @@ async function handleSavePermissions(clientWs, allowedTools) {
let verifyDetails = [];
try {
verifyResult = JSON.parse(verifyRaw.trim());
verified = true;
for (const [fpath, data] of Object.entries(verifyResult)) {
if (data.error) {
verifyDetails.push(`${fpath}: FEHLER ${data.error}`);
verified = false;
} else {
const ok = data.allowedTools.length === allowedTools.length
&& allowedTools.every(t => data.allowedTools.includes(t));
const ccOk = data.permsAllow.length === allowedTools.length;
verifyDetails.push(`${fpath}: allowedTools=${ok?"OK":"FEHLER"} permissions.allow=${ccOk?"OK":"FEHLER"} (${data.allowedTools.length}/${data.permsAllow.length} Tools)`);
if (!ok) verified = false;
}
// Mindestens ein Pfad muss erfolgreich sein
verified = verifyResult.ok && verifyResult.ok.length > 0;
for (const f of (verifyResult.fail || [])) {
verifyDetails.push(`FEHLER: ${f}`);
}
for (const [fpath, data] of Object.entries(verifyResult.verified || {})) {
const ok = data.allowedTools.length === allowedTools.length
&& allowedTools.every(t => data.allowedTools.includes(t));
const ccOk = data.permsAllow.length === allowedTools.length;
verifyDetails.push(`${fpath}: allowedTools=${ok?"OK":"FEHLER"} permissions.allow=${ccOk?"OK":"FEHLER"} (${data.allowedTools.length}/${data.permsAllow.length} Tools)`);
if (!ok) verified = false;
}
} catch (e) {
verifyDetails.push(`Parse-Fehler: ${e.message}`);
verifyDetails.push(`Parse-Fehler: ${e.message} — Raw: ${verifyRaw.substring(0, 200)}`);
}
log("info", "server", `Verify:\n${verifyDetails.join("\n")}`);
@ -1598,12 +1598,16 @@ async function handleSavePermissions(clientWs, allowedTools) {
return;
}
const okCount = (verifyResult.ok || []).length;
const failCount = (verifyResult.fail || []).length;
const infoMsg = `${allowedTools.length} Tools gespeichert (${okCount} Pfade OK` + (failCount ? `, ${failCount} fehlgeschlagen` : '') + ')';
clientWs.send(JSON.stringify({
type: "permissions_saved", ok: true,
info: `${allowedTools.length} Tools gespeichert und verifiziert (alle ${OPENCLAW_SETTINGS_PATHS.length} Pfade)`,
info: infoMsg,
details: verifyDetails,
needsRestart: true,
}));
log("info", "server", `Berechtigungen gespeichert und verifiziert (${allowedTools.length} Tools)`);
log("info", "server", infoMsg);
} catch (err) {
log("error", "server", `Berechtigungen speichern fehlgeschlagen: ${err.message}`);
clientWs.send(JSON.stringify({ type: "permissions_saved", ok: false, error: err.message }));