Files
PrismFacet/plugin/core/rule-store.ts
zhi d5c057a3f9 refactor: restructure PrismFacet per OpenClaw plugin spec
- plugin/ directory structure: hooks/, tools/, core/
- export default { id, name, register } entry format
- globalThis state management with lifecycle protection
- WeakSet dedup on before_prompt_build hook
- Tool uses inputSchema + execute (not parameters + handler)
- additionalProperties: false in config schema
- Core logic in plugin/core/ (no plugin-sdk dependency)
- Install/uninstall script (scripts/install.mjs)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-18 17:22:17 +00:00

57 lines
1.4 KiB
TypeScript

import { readFileSync, writeFileSync } from "node:fs";
export type RuleStore = Record<string, string>; // "router:key" → prompt file path
const _G = globalThis as Record<string, unknown>;
const RULES_KEY = "_prismFacetRules";
const RULES_PATH_KEY = "_prismFacetRulesPath";
function getRules(): RuleStore {
if (!_G[RULES_KEY] || typeof _G[RULES_KEY] !== "object") {
_G[RULES_KEY] = {};
}
return _G[RULES_KEY] as RuleStore;
}
function getRulesPath(): string {
return (_G[RULES_PATH_KEY] as string) || "";
}
function save(): void {
const p = getRulesPath();
if (!p) return;
writeFileSync(p, JSON.stringify(getRules(), null, 2) + "\n", "utf8");
}
export function initRuleStore(filePath: string): void {
_G[RULES_PATH_KEY] = filePath;
try {
const raw = readFileSync(filePath, "utf8");
_G[RULES_KEY] = JSON.parse(raw) as RuleStore;
} catch {
_G[RULES_KEY] = {};
}
}
export function addRule(router: string, key: string, promptFile: string): void {
getRules()[`${router}:${key}`] = promptFile;
save();
}
export function removeRule(router: string, key: string): boolean {
const rules = getRules();
const ruleKey = `${router}:${key}`;
if (!(ruleKey in rules)) return false;
delete rules[ruleKey];
save();
return true;
}
export function getRule(router: string, key: string): string | undefined {
return getRules()[`${router}:${key}`];
}
export function listRules(): Record<string, string> {
return { ...getRules() };
}