UNPKG

askeroo

Version:

A modern CLI prompt library with flow control, history navigation, and conditional prompts

155 lines 5.84 kB
import { PromptRequest } from "../types/index.js"; export interface PromptNode { id: string; type: "field" | "group"; label?: string; fieldType?: string; value?: any; completed: boolean; visited: boolean; active: boolean; hideOnCompletion?: boolean; excludeFromCompleted?: boolean; depth: number; flow?: "progressive" | "phased" | "static"; enableArrowNavigation?: boolean; discoveredFields?: Array<{ id: string; label: string; type: string; }>; children: PromptNode[]; parent?: PromptNode; properties: Record<string, any>; allowBack?: boolean; groupName?: string; submissionType?: "manual" | "auto" | "skipped" | "programmatic"; autoSubmit?: boolean; frozen?: boolean; isCancelPrompt?: boolean; } export interface PromptTree { root: PromptNode; nodeIndex: Map<string, PromptNode>; history: PromptNode[]; } export type NavigationDirection = "back" | "forward" | "next"; export interface NavigationResult { success: boolean; node?: PromptNode; reason?: string; } export declare class PromptTreeManager { private tree; constructor(); getTree(): PromptTree; setTree(tree: PromptTree): void; getActiveNode(): PromptNode | null; getNode(id: string): PromptNode | undefined; addNode(node: Omit<PromptNode, "children" | "parent">, parentId?: string): PromptNode; updateNode(id: string, updates: Partial<PromptNode>): boolean; navigateTo(nodeId: string): NavigationResult; goBack(): NavigationResult; /** Determine if we should clear state when navigating to a node */ private shouldClearStateOnNavigateTo; /** Check if navigating to this node is forward navigation */ private isForwardNavigation; /** Check if node is a sibling of an ancestor of active */ private isAncestorSibling; /** Activate a node (handles deactivation and history tracking) */ private activateNode; /** * Clear all fields in a group and go back * Used when user wants to clear a group and navigate back */ clearGroupAndGoBack(fieldId: string): NavigationResult; canGoBack(): boolean; findNextActiveNode(fromNode?: PromptNode): PromptNode | null; private findNextSibling; private findNextInParentChain; private removeNodeFromTree; /** * Clear future state from a node - keeps the node and everything before it in history * Used for back navigation and field value changes */ private clearFutureStateFrom; /** * Clear all nodes added after a field's first visit (for value changes) */ clearAllNodesAddedAfterField(field: PromptNode): void; /** Add a node and all its ancestors to the keep set */ private addPathToRoot; findParentGroup(node: PromptNode): PromptNode | null; getGroupChildren(groupId: string): PromptNode[]; traverseDepthFirst(visitor: (node: PromptNode) => void, startNode?: PromptNode): void; findNodes(predicate: (node: PromptNode) => boolean): PromptNode[]; getCompletedNodes(): PromptNode[]; getVisitedNodes(): PromptNode[]; getNodesByType(type: "field" | "group"): PromptNode[]; getNodesByFieldType(fieldType: string): PromptNode[]; getNavigationPath(): PromptNode[]; /** * Get the count of answers stored in the tree */ getAnswerCount(): number; /** * Clear future answers (for back navigation) * Removes answers from nodes that come after the current step */ clearFutureAnswers(currentStep: number): void; /** * Clear unreachable answers (after replay) * Removes answers from nodes that are not in the current flow */ clearUnreachableAnswers(): void; getGroupFlow(groupId: string): "progressive" | "phased" | "static" | undefined; getGroupDepth(groupId: string): number | undefined; clearHistoryAfter(nodeId: string): void; /** * Add a prompt request (field or group) to the tree * @param request - The prompt request * @param explicitParentId - Explicit parent group ID from runtime (preferred) */ addPromptRequest(request: PromptRequest, explicitParentId?: string | null): PromptNode; /** * Add a group to the tree * @param request - Group request * @param explicitParentId - Explicit parent from runtime (which knows groupStack) */ private addGroupRequest; /** * Add a field to the tree * @param request - Field request * @param explicitParentId - Explicit parent from runtime (which knows groupStack) */ private addFieldRequest; getCurrentGroupId(): string | null; /** * Mark a node as submitted with a specific submission type * @param nodeId - The node ID to mark * @param submissionType - How the node was submitted */ markNodeSubmitted(nodeId: string, submissionType: "manual" | "auto" | "skipped" | "programmatic"): boolean; /** * Get the submission type of a node * @param nodeId - The node ID to query * @returns The submission type, or undefined if not set */ getNodeSubmissionType(nodeId: string): "manual" | "auto" | "skipped" | "programmatic" | undefined; /** * Get the previous node in history (before the current active node) * @returns The previous node, or null if there is none */ getPreviousNode(): PromptNode | null; /** * Check if back navigation should be allowed based on previous node's submission type * @returns true if back navigation is allowed, false otherwise */ canGoBackBasedOnSubmissionType(): boolean; /** * Enhanced canGoBack that considers submission types * @returns true if back navigation is allowed */ canGoBackEnhanced(): boolean; } //# sourceMappingURL=prompt-tree.d.ts.map