|
1 | | -import { PlanWithSteps, Step, AgentType, ProcessedPlanData, PlanMessage } from '@/models'; |
2 | | -import { apiService } from '@/api'; |
3 | | - |
| 1 | +import { |
| 2 | + PlanWithSteps, |
| 3 | + Step, |
| 4 | + AgentType, |
| 5 | + ProcessedPlanData, |
| 6 | + PlanMessage, |
| 7 | +} from "@/models"; |
| 8 | +import { apiService } from "@/api"; |
4 | 9 |
|
5 | 10 | /** |
6 | 11 | * Service for processing and managing plan data operations |
7 | 12 | */ |
8 | | -export class PlanDataService { /** |
9 | | - * Fetch plan details by plan ID and process the data |
10 | | - * @param planId Plan ID to fetch |
11 | | - * @returns Promise with processed plan data |
12 | | - */ |
13 | | - static async fetchPlanData(planId: string): Promise<ProcessedPlanData> { |
14 | | - try { |
15 | | - // Use optimized getPlanById method for better performance |
16 | | - const planBody = await apiService.getPlanById(planId); |
17 | | - return this.processPlanData(planBody.plan_with_steps, planBody.messages || []); |
18 | | - } catch (error) { |
19 | | - console.log('Failed to fetch plan data:', error); |
20 | | - throw error; |
21 | | - } |
| 13 | +export class PlanDataService { |
| 14 | + /** |
| 15 | + * Fetch plan details by plan ID and process the data |
| 16 | + * @param planId Plan ID to fetch |
| 17 | + * @returns Promise with processed plan data |
| 18 | + */ |
| 19 | + static async fetchPlanData( |
| 20 | + planId: string, |
| 21 | + useCache: boolean |
| 22 | + ): Promise<ProcessedPlanData> { |
| 23 | + try { |
| 24 | + // Use optimized getPlanById method for better performance |
| 25 | + const planBody = await apiService.getPlanById(planId, useCache); |
| 26 | + return this.processPlanData( |
| 27 | + planBody.plan_with_steps, |
| 28 | + planBody.messages || [] |
| 29 | + ); |
| 30 | + } catch (error) { |
| 31 | + console.log("Failed to fetch plan data:", error); |
| 32 | + throw error; |
22 | 33 | } |
| 34 | + } |
23 | 35 |
|
24 | | - /** |
25 | | - * Process plan data to extract agents, steps, and clarification status |
26 | | - * @param plan PlanWithSteps object to process |
27 | | - * @returns Processed plan data |
28 | | - */ |
29 | | - static processPlanData(plan: PlanWithSteps, messages: PlanMessage[]): ProcessedPlanData { |
30 | | - // Extract unique agents from steps |
31 | | - const uniqueAgents = new Set<AgentType>(); |
32 | | - plan.steps.forEach(step => { |
33 | | - if (step.agent) { |
34 | | - uniqueAgents.add(step.agent); |
35 | | - } |
36 | | - }); |
| 36 | + /** |
| 37 | + * Process plan data to extract agents, steps, and clarification status |
| 38 | + * @param plan PlanWithSteps object to process |
| 39 | + * @returns Processed plan data |
| 40 | + */ |
| 41 | + static processPlanData( |
| 42 | + plan: PlanWithSteps, |
| 43 | + messages: PlanMessage[] |
| 44 | + ): ProcessedPlanData { |
| 45 | + // Extract unique agents from steps |
| 46 | + const uniqueAgents = new Set<AgentType>(); |
| 47 | + plan.steps.forEach((step) => { |
| 48 | + if (step.agent) { |
| 49 | + uniqueAgents.add(step.agent); |
| 50 | + } |
| 51 | + }); |
37 | 52 |
|
38 | | - // Convert Set to Array for easier handling |
39 | | - const agents = Array.from(uniqueAgents); |
| 53 | + // Convert Set to Array for easier handling |
| 54 | + const agents = Array.from(uniqueAgents); |
40 | 55 |
|
41 | | - // Get all steps |
42 | | - const steps = plan.steps; |
| 56 | + // Get all steps |
| 57 | + const steps = plan.steps; |
43 | 58 |
|
44 | | - // Check if human_clarification_request is not null |
45 | | - const hasClarificationRequest = plan.human_clarification_request != null && plan.human_clarification_request.trim().length > 0; |
46 | | - const hasClarificationResponse = plan.human_clarification_response != null && plan.human_clarification_response.trim().length > 0; |
47 | | - const enableChat = hasClarificationRequest && !hasClarificationResponse; |
48 | | - const enableStepButtons = (hasClarificationRequest && hasClarificationResponse) || (!hasClarificationRequest && !hasClarificationResponse); |
49 | | - return { |
50 | | - plan, |
51 | | - agents, |
52 | | - steps, |
53 | | - hasClarificationRequest, |
54 | | - hasClarificationResponse, |
55 | | - enableChat, |
56 | | - enableStepButtons, |
57 | | - messages |
58 | | - }; |
59 | | - } |
| 59 | + // Check if human_clarification_request is not null |
| 60 | + const hasClarificationRequest = |
| 61 | + plan.human_clarification_request != null && |
| 62 | + plan.human_clarification_request.trim().length > 0; |
| 63 | + const hasClarificationResponse = |
| 64 | + plan.human_clarification_response != null && |
| 65 | + plan.human_clarification_response.trim().length > 0; |
| 66 | + const enableChat = hasClarificationRequest && !hasClarificationResponse; |
| 67 | + const enableStepButtons = |
| 68 | + (hasClarificationRequest && hasClarificationResponse) || |
| 69 | + (!hasClarificationRequest && !hasClarificationResponse); |
| 70 | + return { |
| 71 | + plan, |
| 72 | + agents, |
| 73 | + steps, |
| 74 | + hasClarificationRequest, |
| 75 | + hasClarificationResponse, |
| 76 | + enableChat, |
| 77 | + enableStepButtons, |
| 78 | + messages, |
| 79 | + }; |
| 80 | + } |
60 | 81 |
|
61 | | - /** |
62 | | - * Get steps for a specific agent type |
63 | | - * @param plan Plan with steps |
64 | | - * @param agentType Agent type to filter by |
65 | | - * @returns Array of steps for the specified agent |
66 | | - */ |
67 | | - static getStepsForAgent(plan: PlanWithSteps, agentType: AgentType): Step[] { |
68 | | - return apiService.getStepsForAgent(plan, agentType); |
69 | | - } |
| 82 | + /** |
| 83 | + * Get steps for a specific agent type |
| 84 | + * @param plan Plan with steps |
| 85 | + * @param agentType Agent type to filter by |
| 86 | + * @returns Array of steps for the specified agent |
| 87 | + */ |
| 88 | + static getStepsForAgent(plan: PlanWithSteps, agentType: AgentType): Step[] { |
| 89 | + return apiService.getStepsForAgent(plan, agentType); |
| 90 | + } |
70 | 91 |
|
71 | | - /** |
72 | | - * Get steps that are awaiting human feedback |
73 | | - * @param plan Plan with steps |
74 | | - * @returns Array of steps awaiting feedback |
75 | | - */ |
76 | | - static getStepsAwaitingFeedback(plan: PlanWithSteps): Step[] { |
77 | | - return apiService.getStepsAwaitingFeedback(plan); |
78 | | - } |
| 92 | + /** |
| 93 | + * Get steps that are awaiting human feedback |
| 94 | + * @param plan Plan with steps |
| 95 | + * @returns Array of steps awaiting feedback |
| 96 | + */ |
| 97 | + static getStepsAwaitingFeedback(plan: PlanWithSteps): Step[] { |
| 98 | + return apiService.getStepsAwaitingFeedback(plan); |
| 99 | + } |
79 | 100 |
|
80 | | - /** |
81 | | - * Check if plan is complete |
82 | | - * @param plan Plan with steps |
83 | | - * @returns Boolean indicating if plan is complete |
84 | | - */ |
85 | | - static isPlanComplete(plan: PlanWithSteps): boolean { |
86 | | - return apiService.isPlanComplete(plan); |
87 | | - } |
| 101 | + /** |
| 102 | + * Check if plan is complete |
| 103 | + * @param plan Plan with steps |
| 104 | + * @returns Boolean indicating if plan is complete |
| 105 | + */ |
| 106 | + static isPlanComplete(plan: PlanWithSteps): boolean { |
| 107 | + return apiService.isPlanComplete(plan); |
| 108 | + } |
88 | 109 |
|
89 | | - /** |
90 | | - * Get plan completion percentage |
91 | | - * @param plan Plan with steps |
92 | | - * @returns Completion percentage (0-100) |
93 | | - */ |
94 | | - static getPlanCompletionPercentage(plan: PlanWithSteps): number { |
95 | | - return apiService.getPlanCompletionPercentage(plan); |
96 | | - } |
97 | | - |
98 | | - /** |
99 | | - * Approve a plan step |
100 | | - * @param step Step to approve |
101 | | - * @returns Promise with API response |
102 | | - */ |
103 | | - static async stepStatus(step: Step, action: boolean): Promise<{ status: string }> { |
104 | | - try { |
| 110 | + /** |
| 111 | + * Get plan completion percentage |
| 112 | + * @param plan Plan with steps |
| 113 | + * @returns Completion percentage (0-100) |
| 114 | + */ |
| 115 | + static getPlanCompletionPercentage(plan: PlanWithSteps): number { |
| 116 | + return apiService.getPlanCompletionPercentage(plan); |
| 117 | + } |
105 | 118 |
|
106 | | - |
107 | | - return apiService.stepStatus( |
108 | | - step.plan_id, |
109 | | - step.session_id, |
110 | | - action, // approved |
111 | | - step.id |
112 | | - ); |
113 | | - } catch (error) { |
114 | | - console.log('Failed to change step status:', error); |
115 | | - throw error; |
116 | | - } |
| 119 | + /** |
| 120 | + * Approve a plan step |
| 121 | + * @param step Step to approve |
| 122 | + * @returns Promise with API response |
| 123 | + */ |
| 124 | + static async stepStatus( |
| 125 | + step: Step, |
| 126 | + action: boolean |
| 127 | + ): Promise<{ status: string }> { |
| 128 | + try { |
| 129 | + return apiService.stepStatus( |
| 130 | + step.plan_id, |
| 131 | + step.session_id, |
| 132 | + action, // approved |
| 133 | + step.id |
| 134 | + ); |
| 135 | + } catch (error) { |
| 136 | + console.log("Failed to change step status:", error); |
| 137 | + throw error; |
117 | 138 | } |
| 139 | + } |
118 | 140 |
|
119 | | - |
120 | | - /** |
121 | | - * Submit human clarification for a plan |
122 | | - * @param planId Plan ID |
123 | | - * @param sessionId Session ID |
124 | | - * @param clarification Clarification text |
125 | | - * @returns Promise with API response |
126 | | - */ |
127 | | - static async submitClarification(planId: string, sessionId: string, clarification: string) { |
128 | | - try { |
129 | | - return apiService.submitClarification(planId, sessionId, clarification); |
130 | | - } catch (error) { |
131 | | - console.log('Failed to submit clarification:', error); |
132 | | - throw error; |
133 | | - } |
| 141 | + /** |
| 142 | + * Submit human clarification for a plan |
| 143 | + * @param planId Plan ID |
| 144 | + * @param sessionId Session ID |
| 145 | + * @param clarification Clarification text |
| 146 | + * @returns Promise with API response |
| 147 | + */ |
| 148 | + static async submitClarification( |
| 149 | + planId: string, |
| 150 | + sessionId: string, |
| 151 | + clarification: string |
| 152 | + ) { |
| 153 | + try { |
| 154 | + return apiService.submitClarification(planId, sessionId, clarification); |
| 155 | + } catch (error) { |
| 156 | + console.log("Failed to submit clarification:", error); |
| 157 | + throw error; |
134 | 158 | } |
| 159 | + } |
135 | 160 | } |
0 commit comments