askeroo
Version:
A modern CLI prompt library with flow control, history navigation, and conditional prompts
185 lines • 5.67 kB
TypeScript
import * as React from "react";
import type { GroupOpts } from "../built-ins/group/index.js";
export type { GroupOpts };
export type Answers = Record<string, unknown>;
export type PromptKind = string;
export type BackToken = {
__back: true;
};
export type PromptRequest = {
type: string;
id: string;
label?: string;
groupName?: string;
flow?: "progressive" | "phased" | "static";
discoveredFields?: Array<{
id: string;
label: string;
type: string;
}>;
enableArrowNavigation?: boolean;
excludeFromCompleted?: boolean;
hideOnCompletion?: boolean;
allowBack?: boolean;
depth?: number;
autoSubmit?: boolean;
[key: string]: any;
};
export type GroupMeta = {
label?: string;
id?: string;
};
export type PromptOpts = {
message: string;
id?: string;
};
export type UI = {
showGroup(label: string | undefined, flow?: "progressive" | "phased" | "static", id?: string, discoveredFields?: Array<{
id: string;
label: string;
type: string;
}>, enableArrowNavigation?: boolean, depth?: number, parentGroup?: string, groupOptions?: any): Promise<void> | void;
clearGroup?(): void;
cleanup?(): void;
[key: string]: any;
};
export type Engine = {
step<T>(kind: PromptKind, opts: PromptOpts | (GroupMeta & GroupOpts), askFn: (id: string) => Promise<T | BackToken>): Promise<T>;
BACK: BackToken;
};
export interface FieldState {
values: Record<string, any>;
visited: Set<string>;
completed: Set<string>;
properties: Map<string, any>;
messages: Record<string, string>;
groupNames: Record<string, string>;
groupIds: Record<string, string>;
}
export interface GroupState {
progressive: Set<string>;
phased: Set<string>;
static: Set<string>;
completed: Set<string>;
order: string[];
arrowNavigation: Set<string>;
depths: Map<string, number>;
}
export interface PromptOrderState {
root: Array<{
id: string;
type: "field" | "group";
groupName?: string;
}>;
rootFieldHistory: Array<{
id: string;
label: string;
type: string;
hideOnCompletion?: boolean;
}>;
staticGroupFields: Map<string, Array<{
id: string;
label: string;
type: string;
hideOnCompletion?: boolean;
}>>;
groupFieldHistory: Map<string, Array<{
id: string;
label: string;
type: string;
hideOnCompletion?: boolean;
}>>;
}
export type PluginRenderProps = {
value?: any;
initialValue?: any;
completedValue?: any;
state: PluginState;
message?: string;
label?: string;
onSubmit: (value: any) => void;
onBack?: () => void;
flow?: "progressive" | "phased" | "static";
isFirstInGroup?: boolean;
isLastInGroup?: boolean;
isFirstRootPrompt?: boolean;
enableArrowNavigation?: boolean;
onHintChange?: (hint: React.ReactNode) => void;
allowBack?: boolean;
[key: string]: any;
};
export type PromptPlugin = {
type: string;
component?: React.ComponentType<any> | ((props: any) => React.ReactElement | null);
transform?: (opts: any, context: {
currentGroup?: string;
}, id: string) => any;
autoSubmit?: boolean;
isContainer?: boolean;
onEnter?: (runtime: any, opts: any) => Promise<void> | void;
onExit?: (runtime: any, opts: any) => Promise<void> | void;
execute?: (runtime: any, opts: any, body: () => Promise<any>) => Promise<any>;
};
export type FlowFunction<T> = (api: {
BACK: BackToken;
} & Record<string, any>) => Promise<T>;
export type ValidatorFunction<T = any> = (value: T) => string | null | Promise<string | null>;
export type PluginState = "active" | "completed" | "disabled";
/**
* Node properties - library-managed flow state
* These are shared across all plugins and managed by the framework
*/
export interface PluginNode {
state: PluginState;
flow?: "progressive" | "phased" | "static";
isFirstInGroup?: boolean;
isLastInGroup?: boolean;
isFirstRootPrompt?: boolean;
allowBack?: boolean;
completedValue?: any;
enableArrowNavigation?: boolean;
depth?: number;
children?: React.ReactNode;
}
/**
* Base event handlers available to all plugins
* Generic type T is the value type the plugin submits
*/
export interface PluginEvents<T = any> {
onSubmit?: (value: T | any) => void;
onBack?: () => void;
onHintChange?: (hint: React.ReactNode) => void;
onValidate?: ValidatorFunction<T>;
onNavigate?: (direction: "up" | "down") => void;
[key: string]: any;
}
/**
* Helper type for plugin component props
* Combines node, user options, and events into structured format
*
* @template TOptions - User-provided plugin options
* @template TValue - The value type the plugin submits
*/
export interface PluginComponentProps<TOptions = any, TValue = any> {
node: PluginNode;
options: TOptions;
events: PluginEvents<TValue>;
}
/**
* Helper type that combines user options with built-in properties
* This is what the plugin function accepts (e.g., text({ label: "...", allowBack: false }))
*
* @template TOptions - User-provided plugin options
* @template TValue - The value type the plugin submits
*/
export type PluginOptionsWithBuiltins<TOptions = any, TValue = any> = TOptions & {
id?: string;
excludeFromCompleted?: boolean;
hideOnCompletion?: boolean;
allowBack?: boolean;
autoSubmit?: boolean;
onValidate?: ValidatorFunction<TValue>;
onSubmit?: (value: TValue) => TValue | void;
meta?: Record<string, any>;
};
//# sourceMappingURL=index.d.ts.map