|
| 1 | +import { Plan, PlanWithSteps, Step } from '../models'; |
| 2 | + |
| 3 | +// Base API URL - update this to point to your backend API |
| 4 | +const API_BASE_URL = process.env.REACT_APP_API_BASE_URL || 'http://localhost:5000/api'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Service for interacting with Plans and Steps |
| 8 | + */ |
| 9 | +export const PlanService = { |
| 10 | + /** |
| 11 | + * Get a plan by ID |
| 12 | + * @param sessionId Session identifier |
| 13 | + * @param planId Plan identifier |
| 14 | + * @returns Promise with the plan |
| 15 | + */ |
| 16 | + async getPlan(sessionId: string, planId: string): Promise<Plan> { |
| 17 | + const response = await fetch(`${API_BASE_URL}/sessions/${sessionId}/plans/${planId}`); |
| 18 | + if (!response.ok) { |
| 19 | + throw new Error(`Failed to get plan: ${response.statusText}`); |
| 20 | + } |
| 21 | + return await response.json() as Plan; |
| 22 | + }, |
| 23 | + |
| 24 | + /** |
| 25 | + * Get a plan with its steps |
| 26 | + * @param sessionId Session identifier |
| 27 | + * @param planId Plan identifier |
| 28 | + * @returns Promise with the plan and its steps |
| 29 | + */ |
| 30 | + async getPlanWithSteps(sessionId: string, planId: string): Promise<PlanWithSteps> { |
| 31 | + const response = await fetch(`${API_BASE_URL}/sessions/${sessionId}/plans/${planId}/with-steps`); |
| 32 | + if (!response.ok) { |
| 33 | + throw new Error(`Failed to get plan with steps: ${response.statusText}`); |
| 34 | + } |
| 35 | + return await response.json() as PlanWithSteps; |
| 36 | + }, |
| 37 | + |
| 38 | + /** |
| 39 | + * Get all plans for a session |
| 40 | + * @param sessionId Session identifier |
| 41 | + * @returns Promise with an array of plans |
| 42 | + */ |
| 43 | + async getPlans(sessionId: string): Promise<Plan[]> { |
| 44 | + const response = await fetch(`${API_BASE_URL}/sessions/${sessionId}/plans`); |
| 45 | + if (!response.ok) { |
| 46 | + throw new Error(`Failed to get plans: ${response.statusText}`); |
| 47 | + } |
| 48 | + return await response.json() as Plan[]; |
| 49 | + }, |
| 50 | + |
| 51 | + /** |
| 52 | + * Create a new plan |
| 53 | + * @param plan Plan to create |
| 54 | + * @returns Promise with the created plan |
| 55 | + */ |
| 56 | + async createPlan(plan: Omit<Plan, 'id' | 'timestamp'>): Promise<Plan> { |
| 57 | + const response = await fetch(`${API_BASE_URL}/sessions/${plan.session_id}/plans`, { |
| 58 | + method: 'POST', |
| 59 | + headers: { |
| 60 | + 'Content-Type': 'application/json', |
| 61 | + }, |
| 62 | + body: JSON.stringify(plan), |
| 63 | + }); |
| 64 | + if (!response.ok) { |
| 65 | + throw new Error(`Failed to create plan: ${response.statusText}`); |
| 66 | + } |
| 67 | + return await response.json() as Plan; |
| 68 | + }, |
| 69 | + |
| 70 | + /** |
| 71 | + * Get a step by ID |
| 72 | + * @param sessionId Session identifier |
| 73 | + * @param planId Plan identifier |
| 74 | + * @param stepId Step identifier |
| 75 | + * @returns Promise with the step |
| 76 | + */ |
| 77 | + async getStep(sessionId: string, planId: string, stepId: string): Promise<Step> { |
| 78 | + const response = await fetch(`${API_BASE_URL}/sessions/${sessionId}/plans/${planId}/steps/${stepId}`); |
| 79 | + if (!response.ok) { |
| 80 | + throw new Error(`Failed to get step: ${response.statusText}`); |
| 81 | + } |
| 82 | + return await response.json() as Step; |
| 83 | + }, |
| 84 | + |
| 85 | + /** |
| 86 | + * Get all steps for a plan |
| 87 | + * @param sessionId Session identifier |
| 88 | + * @param planId Plan identifier |
| 89 | + * @returns Promise with an array of steps |
| 90 | + */ |
| 91 | + async getSteps(sessionId: string, planId: string): Promise<Step[]> { |
| 92 | + const response = await fetch(`${API_BASE_URL}/sessions/${sessionId}/plans/${planId}/steps`); |
| 93 | + if (!response.ok) { |
| 94 | + throw new Error(`Failed to get steps: ${response.statusText}`); |
| 95 | + } |
| 96 | + return await response.json() as Step[]; |
| 97 | + }, |
| 98 | + |
| 99 | + /** |
| 100 | + * Update step status and provide feedback |
| 101 | + * @param sessionId Session identifier |
| 102 | + * @param planId Plan identifier |
| 103 | + * @param stepId Step identifier |
| 104 | + * @param update Update data |
| 105 | + * @returns Promise with the updated step |
| 106 | + */ |
| 107 | + async updateStep( |
| 108 | + sessionId: string, |
| 109 | + planId: string, |
| 110 | + stepId: string, |
| 111 | + update: { |
| 112 | + status?: StepStatus, |
| 113 | + human_feedback?: string, |
| 114 | + updated_action?: string, |
| 115 | + } |
| 116 | + ): Promise<Step> { |
| 117 | + const response = await fetch(`${API_BASE_URL}/sessions/${sessionId}/plans/${planId}/steps/${stepId}`, { |
| 118 | + method: 'PATCH', |
| 119 | + headers: { |
| 120 | + 'Content-Type': 'application/json', |
| 121 | + }, |
| 122 | + body: JSON.stringify(update), |
| 123 | + }); |
| 124 | + if (!response.ok) { |
| 125 | + throw new Error(`Failed to update step: ${response.statusText}`); |
| 126 | + } |
| 127 | + return await response.json() as Step; |
| 128 | + } |
| 129 | +}; |
| 130 | + |
| 131 | +export default PlanService; |
0 commit comments