Files
PrismFacet/plugin/core/cross-plugin-api.ts
hzhang 241cced780 refactor(prism-facet): become a pure registration framework
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>
2026-05-25 10:19:39 +01:00

84 lines
3.2 KiB
TypeScript

/**
* Cross-plugin API: globalThis.__prismFacet
*
* PrismFacet is the registration mechanism (routers, rules, the
* before_prompt_build hook that resolves them); other plugins (notably
* ClawPrompts) supply the actual content by calling into this API at
* their own register() time.
*
* Installation idempotence: safe to call install() multiple times — the
* function attaches the same set of methods to globalThis.__prismFacet
* if not already present. Plugin reload doesn't break consumers.
*
* If a consumer plugin (ClawPrompts) loads BEFORE PrismFacet, the
* `globalThis.__prismFacet` slot will be undefined at the consumer's
* register() time. To make load-order independence work, this module
* pre-creates the slot at module-import time too — consumers can call
* .addRouter / .addRule even if PrismFacet's register() hasn't run yet
* (the registration still lands in the shared maps).
*/
import { addExternalRouter, type RouterContext } from "./router-loader.js";
import { addExternalRule } from "./rule-store.js";
const _G = globalThis as Record<string, unknown>;
export interface PrismFacetCrossPluginApi {
/**
* Register a router programmatically.
* @param name router name (matches the "router:" prefix of rule keys)
* @param resolve function returning a key string for the given context
* (or empty/null to skip this router for this context)
*/
addRouter(
name: string,
resolve: (ctx: RouterContext) => string | Promise<string>,
): void;
/**
* Register a rule programmatically (in-memory; persistent rules.json
* entries set by the prompt-rules tool take precedence on conflict).
* @param routerName router this rule binds to
* @param key value the router must resolve to for this rule to fire
* @param prompt either { file: "/abs/path.md" } or { text: "..." }
*/
addRule(
routerName: string,
key: string,
prompt: { file?: string; text?: string },
): void;
}
/** Idempotent install. Called from PrismFacet's register(). Also runs
* at module-import time below so that consumer plugins loading before
* PrismFacet still find a usable API. */
export function installCrossPluginApi(): PrismFacetCrossPluginApi {
const existing = _G["__prismFacet"] as PrismFacetCrossPluginApi | undefined;
if (existing && typeof existing.addRouter === "function") return existing;
const api: PrismFacetCrossPluginApi = {
addRouter(name, resolve) {
addExternalRouter(name, resolve);
},
addRule(routerName, key, prompt) {
// Phase 1: only file-based prompts are wired through the existing
// injector. `text` inline prompts will need a tmpfile-or-store
// backend; surfaced as a TODO so an early caller failing loudly
// beats silently dropping the prompt.
if (!prompt.file) {
throw new Error(
"__prismFacet.addRule: only { file: '/abs/path.md' } is supported today; " +
"inline { text } pending injector update",
);
}
addExternalRule(routerName, key, prompt.file);
},
};
_G["__prismFacet"] = api;
return api;
}
// Eager install on module load so a consumer plugin importing into the
// same gateway process can register even if it runs before PrismFacet's
// register() does.
installCrossPluginApi();