UNPKG

askeroo

Version:

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

179 lines 5.47 kB
/** * RuntimeState - Manages all runtime state for the prompt flow * * Consolidated state management with clear, descriptive method names. * Handles answers, navigation, group context, and flow control. */ export class RuntimeState { // ===== ANSWER STORAGE ===== answers = new Map(); // ===== NAVIGATION STATE ===== interactivePrompts = []; currentPromptIndex = 0; // ===== FLOW CONTROL ===== isInFlowExecution = false; // ===== GROUP CONTEXT ===== groupHierarchy = []; processedGroups = new Set(); // ========== ANSWER MANAGEMENT ========== storeAnswer(promptId, value) { this.answers.set(promptId, value); } retrieveAnswer(promptId) { return this.answers.get(promptId); } hasStoredAnswer(promptId) { return this.answers.has(promptId); } getAllAnswers() { return Object.fromEntries(this.answers); } getAnswerCount() { return this.answers.size; } removeAnswer(promptId) { this.answers.delete(promptId); } clearAllAnswers() { this.answers.clear(); } // ========== PROMPT TRACKING ========== registerPrompt(promptId) { this.interactivePrompts.push(promptId); } getPromptHistory() { return [...this.interactivePrompts]; } getTotalPromptCount() { return this.interactivePrompts.length; } clearPromptHistory() { this.interactivePrompts = []; } // ========== NAVIGATION ========== getCurrentPromptIndex() { return this.currentPromptIndex; } advanceToNextPrompt() { this.currentPromptIndex++; return this.currentPromptIndex; } returnToPreviousPrompt() { if (this.currentPromptIndex > 0) { this.currentPromptIndex--; } return this.currentPromptIndex; } setPromptIndex(index) { this.currentPromptIndex = Math.max(0, index); } isReplayingAnswers() { return this.currentPromptIndex > 0; } hasReachedEndOfFlow() { return this.currentPromptIndex >= this.interactivePrompts.length; } // ========== GROUP MANAGEMENT ========== enterGroup(groupId) { this.groupHierarchy.push(groupId); } exitGroup() { return this.groupHierarchy.pop(); } getCurrentGroupId() { return this.groupHierarchy[this.groupHierarchy.length - 1]; } getGroupHierarchy() { return [...this.groupHierarchy]; } getGroupDepth() { return this.groupHierarchy.length; } clearGroupHierarchy() { this.groupHierarchy = []; } // ========== PROCESSED GROUPS ========== markGroupAsProcessed(groupId) { this.processedGroups.add(groupId); } isGroupProcessed(groupId) { return this.processedGroups.has(groupId); } clearProcessedGroups() { this.processedGroups.clear(); } // ========== FLOW EXECUTION STATE ========== beginFlowExecution() { this.isInFlowExecution = true; } endFlowExecution() { this.isInFlowExecution = false; } isExecutingFlow() { return this.isInFlowExecution; } // ========== ANSWER CLEANUP ========== /** * Remove answers for prompts that come after the current position * Used for back navigation to clear future answers */ clearAnswersAfterCurrentPosition() { const currentPrompts = new Set(this.interactivePrompts.slice(0, this.currentPromptIndex)); const answerIds = Array.from(this.answers.keys()); for (const id of answerIds) { if (!currentPrompts.has(id)) { this.answers.delete(id); } } } /** * Remove answers for prompts that are no longer in the flow * Used after replay to clean up unreachable answers */ clearUnreachableAnswers() { const reachablePrompts = new Set(this.interactivePrompts); const answerIds = Array.from(this.answers.keys()); for (const id of answerIds) { if (!reachablePrompts.has(id)) { this.answers.delete(id); } } } // ========== RESET OPERATIONS ========== /** * Reset state for a new replay cycle * Clears prompts, groups, and processed groups tracking * Keeps answers and currentPromptIndex intact */ prepareForReplay() { this.interactivePrompts = []; this.groupHierarchy = []; this.processedGroups.clear(); } /** * Complete reset of all state * Use with caution - typically only needed when creating a fresh runtime */ resetAll() { this.answers.clear(); this.interactivePrompts = []; this.currentPromptIndex = 0; this.isInFlowExecution = false; this.groupHierarchy = []; this.processedGroups.clear(); } // ========== DEBUG/INSPECTION ========== getDebugInfo() { return { answerCount: this.answers.size, totalPrompts: this.interactivePrompts.length, currentIndex: this.currentPromptIndex, isExecuting: this.isInFlowExecution, isReplaying: this.isReplayingAnswers(), groupDepth: this.groupHierarchy.length, currentGroup: this.getCurrentGroupId(), processedGroupsCount: this.processedGroups.size, }; } } //# sourceMappingURL=runtime-state.js.map