Files
bot-formal-models/scripts/extract-tool-groups.mjs
zooqueenandHanzo Dev f3900dd5f2 fix: accept BOT_REPO_DIR env var for renamed bot repo
The upstream repo was renamed from clawdbot to bot. Accept
BOT_REPO_DIR alongside the legacy OPENCLAW_REPO_DIR and
CLAWDBOT_REPO_DIR env vars, and update the default fallback
path from ../clawdbot to ../bot.

Co-authored-by: Hanzo Dev <[email protected]>
2026-02-22 14:03:59 -08:00

66 lines
2.3 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
const REPO_DIR = process.env.BOT_REPO_DIR || process.env.OPENCLAW_REPO_DIR || process.env.CLAWDBOT_REPO_DIR;
const SRC = REPO_DIR
? path.resolve(REPO_DIR, 'src/agents/tool-policy.ts')
: path.resolve('../bot/src/agents/tool-policy.ts');
const out = path.resolve('generated/tool-groups.json');
const text = fs.readFileSync(SRC, 'utf8');
// Very small extractor: grab the TOOL_GROUPS object literal and TOOL_NAME_ALIASES.
function extractObject(name) {
const re = new RegExp(`export const ${name}[^=]*=\\s*({[\\s\\S]*?^});`, 'm');
const m = text.match(re);
if (!m) throw new Error(`Could not find ${name} in ${SRC}`);
return m[1];
}
const groupsSrc = extractObject('TOOL_GROUPS');
const aliasesSrc = (() => {
const re = new RegExp(
`const TOOL_NAME_ALIASES\\s*:\\s*Record<[^>]+>\\s*=\\s*({[\\s\\S]*?^});`,
'm'
);
const m = text.match(re);
if (!m) throw new Error(`Could not find TOOL_NAME_ALIASES in ${SRC}`);
return m[1];
})();
// Evaluate safely-ish by converting to JSON-ish.
// Assumes keys/strings are quoted in the TS source (they are).
function evalObjectLiteral(src) {
// Replace trailing comments and TS-style multiline comments (best-effort)
const noComments = src
.replace(/\/\*[\s\S]*?\*\//g, '')
.replace(/(^|\s)\/\/.*$/gm, '');
// Wrap in parentheses so Node treats it as an expression.
// eslint-disable-next-line no-new-func
return Function(`"use strict"; return (${noComments});`)();
}
const groups = evalObjectLiteral(groupsSrc);
const toolNameAliases = evalObjectLiteral(aliasesSrc);
// Back-compat: project rename(s).
// Historically: `group:clawdbot`.
// Later: openclaw renamed this to `group:openclaw`.
// Current in this repo: `group:moltbot`.
// Keep all present aliases identical to avoid churn in downstream specs/tests
// and in cross-repo conformance checks.
const groupAliases = ['group:clawdbot', 'group:openclaw', 'group:moltbot'];
// Find the first defined alias as the canonical source.
const firstKey = groupAliases.find((k) => groups[k]);
if (firstKey) {
for (const k of groupAliases) {
if (!groups[k]) groups[k] = groups[firstKey];
}
}
fs.mkdirSync(path.dirname(out), { recursive: true });
fs.writeFileSync(out, JSON.stringify({ groups, aliases: toolNameAliases }, null, 2));
console.log(`Wrote ${out}`);