Files
openclaw/src/plugins/runtime.ts
Glucksberg 4ee808dbcb feat: add plugin command API for LLM-free auto-reply commands
This adds a new `api.registerCommand()` method to the plugin API, allowing
plugins to register slash commands that execute without invoking the AI agent.

Features:
- Plugin commands are processed before built-in commands and the agent
- Commands can optionally require authorization
- Commands can accept arguments
- Async handlers are supported

Use case: plugins can implement toggle commands (like /tts_on, /tts_off)
that respond immediately without consuming LLM API calls.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-24 06:28:22 +00:00

57 lines
1.3 KiB
TypeScript

import type { PluginRegistry } from "./registry.js";
const createEmptyRegistry = (): PluginRegistry => ({
plugins: [],
tools: [],
hooks: [],
typedHooks: [],
channels: [],
providers: [],
gatewayHandlers: {},
httpHandlers: [],
cliRegistrars: [],
services: [],
commands: [],
diagnostics: [],
});
const REGISTRY_STATE = Symbol.for("clawdbot.pluginRegistryState");
type RegistryState = {
registry: PluginRegistry | null;
key: string | null;
};
const state: RegistryState = (() => {
const globalState = globalThis as typeof globalThis & {
[REGISTRY_STATE]?: RegistryState;
};
if (!globalState[REGISTRY_STATE]) {
globalState[REGISTRY_STATE] = {
registry: createEmptyRegistry(),
key: null,
};
}
return globalState[REGISTRY_STATE] as RegistryState;
})();
export function setActivePluginRegistry(registry: PluginRegistry, cacheKey?: string) {
state.registry = registry;
state.key = cacheKey ?? null;
}
export function getActivePluginRegistry(): PluginRegistry | null {
return state.registry;
}
export function requireActivePluginRegistry(): PluginRegistry {
if (!state.registry) {
state.registry = createEmptyRegistry();
}
return state.registry;
}
export function getActivePluginRegistryKey(): string | null {
return state.key;
}