|
| 1 | +import { PlanWithSteps, Step, AgentType } from '@/models'; |
| 2 | +import { apiService } from '@/api'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Interface for processed plan data |
| 6 | + */ |
| 7 | +export interface ProcessedPlanData { |
| 8 | + plan: PlanWithSteps; |
| 9 | + agents: AgentType[]; |
| 10 | + steps: Step[]; |
| 11 | + hasHumanClarificationRequest: boolean; |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * Service for processing and managing plan data operations |
| 16 | + */ |
| 17 | +export class PlanDataService { /** |
| 18 | + * Fetch plan details by plan ID and process the data |
| 19 | + * @param planId Plan ID to fetch |
| 20 | + * @returns Promise with processed plan data |
| 21 | + */ |
| 22 | + static async fetchPlanData(planId: string): Promise<ProcessedPlanData> { |
| 23 | + try { |
| 24 | + // Use optimized getPlanById method for better performance |
| 25 | + const plan = await apiService.getPlanById(planId); |
| 26 | + return this.processPlanData(plan); |
| 27 | + } catch (error) { |
| 28 | + console.error('Failed to fetch plan data:', error); |
| 29 | + throw error; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Process plan data to extract agents, steps, and clarification status |
| 35 | + * @param plan PlanWithSteps object to process |
| 36 | + * @returns Processed plan data |
| 37 | + */ |
| 38 | + static processPlanData(plan: PlanWithSteps): ProcessedPlanData { |
| 39 | + // Extract unique agents from steps |
| 40 | + const uniqueAgents = new Set<AgentType>(); |
| 41 | + plan.steps.forEach(step => { |
| 42 | + if (step.agent) { |
| 43 | + uniqueAgents.add(step.agent); |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + // Convert Set to Array for easier handling |
| 48 | + const agents = Array.from(uniqueAgents); |
| 49 | + |
| 50 | + // Get all steps |
| 51 | + const steps = plan.steps; |
| 52 | + |
| 53 | + // Check if human_clarification_request is not null |
| 54 | + const hasHumanClarificationRequest = plan.human_clarification_request != null && |
| 55 | + plan.human_clarification_request.trim().length > 0; |
| 56 | + |
| 57 | + return { |
| 58 | + plan, |
| 59 | + agents, |
| 60 | + steps, |
| 61 | + hasHumanClarificationRequest |
| 62 | + }; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Get steps for a specific agent type |
| 67 | + * @param plan Plan with steps |
| 68 | + * @param agentType Agent type to filter by |
| 69 | + * @returns Array of steps for the specified agent |
| 70 | + */ |
| 71 | + static getStepsForAgent(plan: PlanWithSteps, agentType: AgentType): Step[] { |
| 72 | + return apiService.getStepsForAgent(plan, agentType); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Get steps that are awaiting human feedback |
| 77 | + * @param plan Plan with steps |
| 78 | + * @returns Array of steps awaiting feedback |
| 79 | + */ |
| 80 | + static getStepsAwaitingFeedback(plan: PlanWithSteps): Step[] { |
| 81 | + return apiService.getStepsAwaitingFeedback(plan); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Check if plan is complete |
| 86 | + * @param plan Plan with steps |
| 87 | + * @returns Boolean indicating if plan is complete |
| 88 | + */ |
| 89 | + static isPlanComplete(plan: PlanWithSteps): boolean { |
| 90 | + return apiService.isPlanComplete(plan); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Get plan completion percentage |
| 95 | + * @param plan Plan with steps |
| 96 | + * @returns Completion percentage (0-100) |
| 97 | + */ |
| 98 | + static getPlanCompletionPercentage(plan: PlanWithSteps): number { |
| 99 | + return apiService.getPlanCompletionPercentage(plan); |
| 100 | + } |
| 101 | +} |
0 commit comments