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>
This commit is contained in:
h z
2026-05-25 10:19:39 +01:00
parent b0fd02c50b
commit 241cced780
11 changed files with 175 additions and 146 deletions

View File

@@ -1,85 +0,0 @@
/**
* Inject a "start the chat workflow" hint into the agent's system prompt
* when this turn was triggered by a message in a fabric channel.
*
* Two cases:
* - no prior chat journal for this (agent, channel) → suggest
* `workflow_start chat` (fresh).
* - mapping exists → suggest `workflow_start chat from <journalId>`
* so the conversation continues in the same linear journal.
*
* The (channel → journal) mapping is owned by Meridian and exposed via
* globalThis.__meridian.getChatJournalForChannel. Meridian writes the
* entry the first time the agent starts a chat workflow on a channel
* with no `from` argument.
*
* TODO(phase-2): once Fabric.OpenclawPlugin exposes per-channel type
* info (DM / group / triage) via a cross-plugin API, narrow this hook
* to xType === 'dm' only. Today we inject for any fabric channel — chat
* workflow itself is a no-op outside DMs, but the suggestion is noise.
*/
const _G = globalThis as Record<string, unknown>;
const DEDUP_KEY = '_prismFacetFabricChatDedup';
interface MeridianBridge {
getChatJournalForChannel?: (agentId: string, channelId: string) => Promise<string | null>;
}
interface PromptCtx {
agentId?: string;
channelId?: string;
messageProvider?: string;
}
export function registerFabricChatInjector(api: {
on(hook: string, handler: (...args: any[]) => any): void;
logger: { info(msg: string): void; warn(msg: string): void };
}): void {
if (!(_G[DEDUP_KEY] instanceof WeakSet)) _G[DEDUP_KEY] = new WeakSet<object>();
const dedup = _G[DEDUP_KEY] as WeakSet<object>;
api.on('before_prompt_build', async (event: unknown, ctx: PromptCtx) => {
if (dedup.has(event as object)) return;
dedup.add(event as object);
const agentId = ctx.agentId || '';
const channelId = (ctx.channelId || '').trim();
const provider = (ctx.messageProvider || '').toLowerCase();
if (!agentId || !channelId) return;
if (provider && provider !== 'fabric') return;
// Empty provider also accepted — gateway sometimes omits the field
// even when the trigger was a fabric channel; channelId presence is
// the load-bearing signal.
let journalId: string | null = null;
const meridian = _G['__meridian'] as MeridianBridge | undefined;
if (typeof meridian?.getChatJournalForChannel === 'function') {
try {
journalId = await meridian.getChatJournalForChannel(agentId, channelId);
} catch (err) {
api.logger.warn(
`[prism-facet] fabric-chat-injector: meridian lookup failed for ` +
`agent=${agentId} channel=${channelId}: ${String(err)}`,
);
}
}
const cmd = journalId
? `\`workflow_start\` with \`workflow="chat"\` and \`from="${journalId}"\``
: `\`workflow_start\` with \`workflow="chat"\``;
const continuationLine = journalId
? `This channel already has an open chat journal (\`${journalId}\`). Resume it with the \`from\` argument so the conversation history stays in one file.`
: `No prior chat journal exists for this channel yet — Meridian will create a fresh one and remember the channel→journal mapping for future turns.`;
const segment =
`# Chat channel context\n` +
`\n` +
`This turn was triggered by a message in a fabric channel (\`${channelId}\`).\n` +
`${continuationLine}\n` +
`\n` +
`**Next action:** call ${cmd} to enter the chat workflow before doing anything else.\n`;
return { appendSystemContext: segment };
});
}