Strip all Hangman-Lab-specific content out of PrismFacet so it can be
reused by any project. Content (always router, pcexec-convention prompt,
fabric-chat-injector hook) moves to the new sibling plugin ClawPrompts.
Mechanism additions:
- `globalThis.__prismFacet` cross-plugin API installed at module-import
time (so consumers loaded before PrismFacet can still register):
.addRouter(name, resolveFn)
.addRule(router, key, { file })
- core/rule-store: tier rules into `persistent` (rules.json, mutated by
the prompt-rules admin tool) and `external` (in-memory, registered by
other plugins via the API). Persistent overrides external on conflict.
- core/router-loader: addExternalRouter() for programmatic registration
into the same map the file-based loader uses.
- index.ts: drops registerFabricChatInjector wiring, registerBeforePromptBuild
remains.
Removed (now shipped from ClawPrompts):
- plugin/routers/always.ts
- plugin/hooks/fabric-chat-injector.ts
- plugin/prompts/pcexec-convention.md
- plugin/rules.json: now `{}`; ClawPrompts registers its rule externally
What still lives in PrismFacet:
- before_prompt_build hook (the wiring between routers/rules and the
agent's system prompt)
- prompt-rules admin tool (lists + mutates persistent rules)
- file-based routersDir / rulesFile scanning (kept for operator ad-hoc
use; ClawPrompts uses the API instead)
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
93 lines
3.0 KiB
TypeScript
93 lines
3.0 KiB
TypeScript
/**
|
|
* Two-tier rule store: persistent (operator/admin rules loaded from
|
|
* rules.json, mutated by the prompt-rules tool) and external (in-memory
|
|
* rules registered by other plugins via the __prismFacet cross-plugin
|
|
* API). External rules are NOT saved to disk — they belong to the
|
|
* registering plugin and are re-registered on every gateway start.
|
|
*
|
|
* Lookup precedence: persistent > external (operator wins over plugin
|
|
* defaults so an explicit rules.json override is honored).
|
|
*/
|
|
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 PERSISTENT_KEY = "_prismFacetRules";
|
|
const EXTERNAL_KEY = "_prismFacetExternalRules";
|
|
const RULES_PATH_KEY = "_prismFacetRulesPath";
|
|
|
|
function getPersistent(): RuleStore {
|
|
if (!_G[PERSISTENT_KEY] || typeof _G[PERSISTENT_KEY] !== "object") {
|
|
_G[PERSISTENT_KEY] = {};
|
|
}
|
|
return _G[PERSISTENT_KEY] as RuleStore;
|
|
}
|
|
|
|
function getExternal(): RuleStore {
|
|
if (!_G[EXTERNAL_KEY] || typeof _G[EXTERNAL_KEY] !== "object") {
|
|
_G[EXTERNAL_KEY] = {};
|
|
}
|
|
return _G[EXTERNAL_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(getPersistent(), null, 2) + "\n", "utf8");
|
|
}
|
|
|
|
export function initRuleStore(filePath: string): void {
|
|
_G[RULES_PATH_KEY] = filePath;
|
|
try {
|
|
const raw = readFileSync(filePath, "utf8");
|
|
_G[PERSISTENT_KEY] = JSON.parse(raw) as RuleStore;
|
|
} catch {
|
|
_G[PERSISTENT_KEY] = {};
|
|
}
|
|
}
|
|
|
|
/** Operator-facing addRule (writes through to rules.json on disk). */
|
|
export function addRule(router: string, key: string, promptFile: string): void {
|
|
getPersistent()[`${router}:${key}`] = promptFile;
|
|
save();
|
|
}
|
|
|
|
/** Cross-plugin API: register a rule that lives in memory only. */
|
|
export function addExternalRule(router: string, key: string, promptFile: string): void {
|
|
getExternal()[`${router}:${key}`] = promptFile;
|
|
}
|
|
|
|
export function removeRule(router: string, key: string): boolean {
|
|
const rules = getPersistent();
|
|
const ruleKey = `${router}:${key}`;
|
|
if (!(ruleKey in rules)) return false;
|
|
delete rules[ruleKey];
|
|
save();
|
|
return true;
|
|
}
|
|
|
|
/** Lookup. Persistent (rules.json) overrides external (plugin defaults). */
|
|
export function getRule(router: string, key: string): string | undefined {
|
|
const persistentValue = getPersistent()[`${router}:${key}`];
|
|
if (persistentValue) return persistentValue;
|
|
return getExternal()[`${router}:${key}`];
|
|
}
|
|
|
|
/** Merged view for the admin tool. Persistent overrides external. */
|
|
export function listRules(): Record<string, string> {
|
|
return { ...getExternal(), ...getPersistent() };
|
|
}
|
|
|
|
/** Admin tool needs to know which tier a rule came from. */
|
|
export function listRulesTiered(): {
|
|
persistent: RuleStore;
|
|
external: RuleStore;
|
|
} {
|
|
return { persistent: { ...getPersistent() }, external: { ...getExternal() } };
|
|
}
|