|
| 1 | +# API Service Documentation |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The `apiService` is a unified service for interacting with the backend `app_kernel.py`. It provides a centralized interface for all API calls, with additional features like caching, request tracking, and utility methods. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- **Unified Interface**: All API endpoints are accessible from a single service. |
| 10 | +- **Caching**: Automatic caching of responses for improved performance. |
| 11 | +- **Request Tracking**: Prevents duplicate requests for the same resource. |
| 12 | +- **Error Handling**: Consistent error handling across all endpoints. |
| 13 | +- **Utility Methods**: Helper methods for common operations. |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +### Basic Usage |
| 18 | + |
| 19 | +```tsx |
| 20 | +import { apiService } from "../api/apiService"; |
| 21 | + |
| 22 | +// Get all plans |
| 23 | +const plans = await apiService.getPlans(); |
| 24 | + |
| 25 | +// Submit a new task |
| 26 | +const response = await apiService.submitInputTask({ |
| 27 | + description: "My new task", |
| 28 | +}); |
| 29 | +``` |
| 30 | + |
| 31 | +### Caching |
| 32 | + |
| 33 | +The service includes built-in caching for improved performance. By default, cached data expires after 30 seconds. |
| 34 | + |
| 35 | +```tsx |
| 36 | +// Use cached data if available (default) |
| 37 | +const plans = await apiService.getPlans(sessionId, true); |
| 38 | + |
| 39 | +// Force fresh data from server |
| 40 | +const freshPlans = await apiService.getPlans(sessionId, false); |
| 41 | + |
| 42 | +// Clear all cached data |
| 43 | +apiService.clearCache(); |
| 44 | +``` |
| 45 | + |
| 46 | +### Utility Methods |
| 47 | + |
| 48 | +The service provides utility methods for common operations: |
| 49 | + |
| 50 | +```tsx |
| 51 | +// Check if a plan is complete |
| 52 | +const isComplete = apiService.isPlanComplete(plan); |
| 53 | + |
| 54 | +// Get steps awaiting feedback |
| 55 | +const pendingSteps = apiService.getStepsAwaitingFeedback(plan); |
| 56 | + |
| 57 | +// Get plan completion percentage |
| 58 | +const progress = apiService.getPlanCompletionPercentage(plan); |
| 59 | +``` |
| 60 | + |
| 61 | +## API Reference |
| 62 | + |
| 63 | +### Task Methods |
| 64 | + |
| 65 | +- `submitInputTask(inputTask: InputTask): Promise<InputTaskResponse>` |
| 66 | + |
| 67 | +### Plan Methods |
| 68 | + |
| 69 | +- `getPlans(sessionId?: string, useCache = true): Promise<PlanWithSteps[]>` |
| 70 | +- `getPlanWithSteps(sessionId: string, planId: string, useCache = true): Promise<PlanWithSteps>` |
| 71 | +- `getSteps(planId: string, useCache = true): Promise<Step[]>` |
| 72 | +- `updateStep(sessionId: string, planId: string, stepId: string, update: {...}): Promise<Step>` |
| 73 | + |
| 74 | +### Feedback Methods |
| 75 | + |
| 76 | +- `provideStepFeedback(stepId: string, planId: string, sessionId: string, approved: boolean, ...): Promise<...>` |
| 77 | +- `approveSteps(planId: string, sessionId: string, approved: boolean, ...): Promise<{ status: string }>` |
| 78 | +- `submitClarification(planId: string, sessionId: string, clarification: string): Promise<...>` |
| 79 | + |
| 80 | +### Message Methods |
| 81 | + |
| 82 | +- `getAgentMessages(sessionId: string, useCache = true): Promise<AgentMessage[]>` |
| 83 | +- `deleteAllMessages(): Promise<{ status: string }>` |
| 84 | +- `getAllMessages(useCache = true): Promise<any[]>` |
| 85 | + |
| 86 | +### Utility Methods |
| 87 | + |
| 88 | +- `isPlanComplete(plan: PlanWithSteps): boolean` |
| 89 | +- `getStepsAwaitingFeedback(plan: PlanWithSteps): Step[]` |
| 90 | +- `getStepsForAgent(plan: PlanWithSteps, agentType: AgentType): Step[]` |
| 91 | +- `clearCache(): void` |
| 92 | +- `getPlanProgressStatus(plan: PlanWithSteps): Record<StepStatus, number>` |
| 93 | +- `getPlanCompletionPercentage(plan: PlanWithSteps): number` |
| 94 | + |
| 95 | +## Architecture |
| 96 | + |
| 97 | +The service is built on top of the `apiClient` which handles the HTTP requests and error handling. The service layer adds additional functionality like caching and request tracking. |
| 98 | + |
| 99 | +### Caching |
| 100 | + |
| 101 | +Responses are cached using a simple in-memory cache with expiration. Cache keys are based on the endpoint and parameters. |
| 102 | + |
| 103 | +### Request Tracking |
| 104 | + |
| 105 | +The service tracks in-progress requests to prevent duplicate requests for the same resource, which helps avoid race conditions and unnecessary network traffic. |
| 106 | + |
| 107 | +## Migration Guide |
| 108 | + |
| 109 | +If you're migrating from the individual services (taskService, planService, etc.), here's how to update your code: |
| 110 | + |
| 111 | +Before: |
| 112 | + |
| 113 | +```tsx |
| 114 | +import { planService } from "../api"; |
| 115 | + |
| 116 | +const plans = await planService.getPlans(sessionId); |
| 117 | +``` |
| 118 | + |
| 119 | +After: |
| 120 | + |
| 121 | +```tsx |
| 122 | +import { apiService } from "../api"; |
| 123 | + |
| 124 | +const plans = await apiService.getPlans(sessionId); |
| 125 | +``` |
| 126 | + |
| 127 | +Most methods have compatible signatures, so migration should be straightforward. |
0 commit comments