| 1 | import { AuthConfig } from "../auth/workos.js"; |
| 2 | import { getApiClient } from "../config.js"; |
| 3 | import { logger } from "../util/logger.js"; |
| 4 | |
| 5 | import { BaseService, ServiceWithDependencies } from "./BaseService.js"; |
| 6 | import { ApiClientServiceState } from "./types.js"; |
| 7 | |
| 8 | /** |
| 9 | * Service for managing API client state |
| 10 | * Provides access to the Continue SDK API client |
| 11 | */ |
| 12 | export class ApiClientService |
| 13 | extends BaseService<ApiClientServiceState> |
| 14 | implements ServiceWithDependencies |
| 15 | { |
| 16 | constructor() { |
| 17 | super("ApiClientService", { |
| 18 | apiClient: null, |
| 19 | }); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Declare dependencies on other services |
| 24 | */ |
| 25 | getDependencies(): string[] { |
| 26 | return ["auth"]; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Initialize the API client service |
| 31 | */ |
| 32 | async doInitialize(authConfig: AuthConfig): Promise<ApiClientServiceState> { |
| 33 | const apiClient = getApiClient(authConfig?.accessToken); |
| 34 | |
| 35 | return { |
| 36 | apiClient, |
| 37 | }; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Update the API client with new auth config |
| 42 | */ |
| 43 | async update(authConfig: AuthConfig): Promise<ApiClientServiceState> { |
| 44 | logger.debug("Updating ApiClientService"); |
| 45 | |
| 46 | try { |
| 47 | const apiClient = getApiClient(authConfig?.accessToken); |
| 48 | |
| 49 | this.setState({ |
| 50 | apiClient, |
| 51 | }); |
| 52 | |
| 53 | logger.debug("ApiClientService updated successfully"); |
| 54 | return this.getState(); |
| 55 | } catch (error: any) { |
| 56 | logger.error("Failed to update ApiClientService:", error); |
| 57 | this.emit("error", error); |
| 58 | throw error; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Override isReady to check for API client |
| 64 | */ |
| 65 | override isReady(): boolean { |
| 66 | return super.isReady() && this.currentState.apiClient !== null; |
| 67 | } |
| 68 | } |