| 1 | import { |
| 2 | ConfigResult, |
| 3 | ConfigValidationError, |
| 4 | FullSlug, |
| 5 | Policy, |
| 6 | } from "@continuedev/config-yaml"; |
| 7 | |
| 8 | import { |
| 9 | BrowserSerializedContinueConfig, |
| 10 | ContinueConfig, |
| 11 | IContextProvider, |
| 12 | IDE, |
| 13 | } from "../index.js"; |
| 14 | |
| 15 | import { Logger } from "../util/Logger.js"; |
| 16 | import { finalToBrowserConfig } from "./load.js"; |
| 17 | import { IProfileLoader } from "./profile/IProfileLoader.js"; |
| 18 | |
| 19 | export interface ProfileDescription { |
| 20 | fullSlug: FullSlug; |
| 21 | profileType: "control-plane" | "local" | "platform"; |
| 22 | title: string; |
| 23 | id: string; |
| 24 | iconUrl: string; |
| 25 | errors: ConfigValidationError[] | undefined; |
| 26 | uri: string; |
| 27 | rawYaml?: string; |
| 28 | } |
| 29 | |
| 30 | export interface OrganizationDescription { |
| 31 | id: string; |
| 32 | iconUrl: string; |
| 33 | name: string; |
| 34 | slug: string | undefined; // TODO: This doesn't need to be undefined, just doing while transitioning the backend |
| 35 | policy?: Policy; |
| 36 | } |
| 37 | |
| 38 | export type OrgWithProfiles = OrganizationDescription & { |
| 39 | profiles: ProfileLifecycleManager[]; |
| 40 | currentProfile: ProfileLifecycleManager | null; |
| 41 | }; |
| 42 | |
| 43 | export type SerializedOrgWithProfiles = OrganizationDescription & { |
| 44 | profiles: ProfileDescription[]; |
| 45 | selectedProfileId: string | null; |
| 46 | }; |
| 47 | |
| 48 | export class ProfileLifecycleManager { |
| 49 | private savedConfigResult: ConfigResult<ContinueConfig> | undefined; |
| 50 | private savedBrowserConfigResult?: ConfigResult<BrowserSerializedContinueConfig>; |
| 51 | private pendingConfigPromise?: Promise<ConfigResult<ContinueConfig>>; |
| 52 | |
| 53 | constructor( |
| 54 | private readonly profileLoader: IProfileLoader, |
| 55 | private readonly ide: IDE, |
| 56 | ) {} |
| 57 | |
| 58 | get profileDescription(): ProfileDescription { |
| 59 | return this.profileLoader.description; |
| 60 | } |
| 61 | |
| 62 | clearConfig() { |
| 63 | this.savedConfigResult = undefined; |
| 64 | this.savedBrowserConfigResult = undefined; |
| 65 | this.pendingConfigPromise = undefined; |
| 66 | } |
| 67 | |
| 68 | // Clear saved config and reload |
| 69 | async reloadConfig( |
| 70 | additionalContextProviders: IContextProvider[] = [], |
| 71 | ): Promise<ConfigResult<ContinueConfig>> { |
| 72 | this.savedConfigResult = undefined; |
| 73 | this.savedBrowserConfigResult = undefined; |
| 74 | this.pendingConfigPromise = undefined; |
| 75 | |
| 76 | return this.loadConfig(additionalContextProviders, true); |
| 77 | } |
| 78 | |
| 79 | async loadConfig( |
| 80 | additionalContextProviders: IContextProvider[], |
| 81 | forceReload: boolean = false, |
| 82 | ): Promise<ConfigResult<ContinueConfig>> { |
| 83 | // If we already have a config, return it |
| 84 | if (!forceReload) { |
| 85 | if (this.savedConfigResult) { |
| 86 | return this.savedConfigResult; |
| 87 | } else if (this.pendingConfigPromise) { |
| 88 | return this.pendingConfigPromise; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // Set pending config promise |
| 93 | this.pendingConfigPromise = new Promise((resolve) => { |
| 94 | void (async () => { |
| 95 | let result: ConfigResult<ContinueConfig>; |
| 96 | // This try catch is expected to catch high-level errors that aren't block-specific |
| 97 | // Like invalid json, invalid yaml, file read errors, etc. |
| 98 | // NOT block-specific loading errors |
| 99 | try { |
| 100 | result = await this.profileLoader.doLoadConfig(); |
| 101 | } catch (e) { |
| 102 | // Capture config loading system failures to Sentry |
| 103 | Logger.error(e, { |
| 104 | context: "profile_config_loading", |
| 105 | }); |
| 106 | |
| 107 | const message = |
| 108 | e instanceof Error |
| 109 | ? `${e.message}\n${e.stack ? e.stack : ""}` |
| 110 | : "Error loading config"; |
| 111 | result = { |
| 112 | errors: [ |
| 113 | { |
| 114 | fatal: true, |
| 115 | message, |
| 116 | }, |
| 117 | ], |
| 118 | config: undefined, |
| 119 | configLoadInterrupted: true, |
| 120 | }; |
| 121 | } |
| 122 | |
| 123 | if (result.config) { |
| 124 | // Add registered context providers |
| 125 | result.config.contextProviders = ( |
| 126 | result.config.contextProviders ?? [] |
| 127 | ).concat(additionalContextProviders); |
| 128 | } |
| 129 | |
| 130 | resolve(result); |
| 131 | })(); |
| 132 | }); |
| 133 | |
| 134 | // Wait for the config promise to resolve |
| 135 | this.savedConfigResult = await this.pendingConfigPromise; |
| 136 | this.pendingConfigPromise = undefined; |
| 137 | return this.savedConfigResult; |
| 138 | } |
| 139 | |
| 140 | async getSerializedConfig( |
| 141 | additionalContextProviders: IContextProvider[], |
| 142 | ): Promise<ConfigResult<BrowserSerializedContinueConfig>> { |
| 143 | if (this.savedBrowserConfigResult) { |
| 144 | return this.savedBrowserConfigResult; |
| 145 | } else { |
| 146 | const result = await this.loadConfig(additionalContextProviders); |
| 147 | if (!result.config) { |
| 148 | return { |
| 149 | ...result, |
| 150 | config: undefined, |
| 151 | }; |
| 152 | } |
| 153 | const serializedConfig = await finalToBrowserConfig( |
| 154 | result.config, |
| 155 | this.ide, |
| 156 | ); |
| 157 | return { |
| 158 | ...result, |
| 159 | config: serializedConfig, |
| 160 | }; |
| 161 | } |
| 162 | } |
| 163 | } |