askeroo
Version:
A modern CLI prompt library with flow control, history navigation, and conditional prompts
76 lines • 2.7 kB
JavaScript
class PromptRegistry {
plugins = new Map();
register(plugin) {
this.plugins.set(plugin.type, plugin);
}
get(type) {
return this.plugins.get(type);
}
getAll() {
return Array.from(this.plugins.values());
}
getComponent(type) {
const plugin = this.plugins.get(type);
return plugin?.component;
}
shouldAutoSubmit(type) {
const plugin = this.plugins.get(type);
return plugin?.autoSubmit === true; // Default to false if not specified
}
isContainer(type) {
const plugin = this.plugins.get(type);
return plugin?.isContainer === true;
}
getComponents() {
const components = {};
for (const [type, plugin] of this.plugins.entries()) {
if (plugin.component) {
components[type] = plugin.component;
}
}
return components;
}
}
// Global singleton registry
export const globalRegistry = new PromptRegistry();
// Manual plugin registration function for external developers
export function registerPlugin(plugin) {
globalRegistry.register(plugin);
}
// Import runtime context management
import { getPluginRuntime } from "./runtime-context.js";
// Plugin creation function that auto-registers
export function createPrompt(config) {
// Validate that either component or execute is provided (not required for containers with execute)
if (!config.component && !config.execute) {
throw new Error(`Plugin "${config.type}" must provide either a component or execute function`);
}
// Use component directly
const component = config.component;
const plugin = {
type: config.type,
component: component,
transform: config.transform,
autoSubmit: config.autoSubmit,
isContainer: config.isContainer,
execute: config.execute,
onEnter: config.onEnter,
onExit: config.onExit,
};
// Auto-register the plugin
globalRegistry.register(plugin);
// Return the prompt function that users will call
// Make opts optional with empty object as default
return async function (opts = {}) {
const runtime = getPluginRuntime(config.type);
// For container plugins with custom execute, call it directly
if (config.isContainer && config.execute) {
const body = opts.body; // Extract body function for containers
return await config.execute(runtime, opts, body);
}
// Call the dynamically created prompt function from the runtime
const dynamicPrompt = runtime[config.type];
return dynamicPrompt(opts);
};
}
//# sourceMappingURL=registry.js.map