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