From a894dd80eacb94584380ce7a7a76d26b8f176b45 Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Fri, 19 Jul 2024 16:37:01 -0700 Subject: [PATCH] update workspaces on session info change --- core/config/ConfigHandler.ts | 82 +++++++------ core/core.ts | 21 +++- .../vscode/src/extension/VsCodeExtension.ts | 5 +- gui/src/components/ProfileSwitcher.tsx | 108 +++++++++--------- 4 files changed, 124 insertions(+), 92 deletions(-) diff --git a/core/config/ConfigHandler.ts b/core/config/ConfigHandler.ts index e01b41e0e..64669f68d 100644 --- a/core/config/ConfigHandler.ts +++ b/core/config/ConfigHandler.ts @@ -1,4 +1,7 @@ -import { ControlPlaneClient } from "../control-plane/client.js"; +import { + ControlPlaneClient, + ControlPlaneSessionInfo, +} from "../control-plane/client.js"; import { BrowserSerializedContinueConfig, ContinueConfig, @@ -118,44 +121,20 @@ export class ConfigHandler { return this.profiles.filter((p) => p.profileId !== this.selectedProfileId); } - constructor( - private readonly ide: IDE, - private ideSettingsPromise: Promise, - private readonly writeLog: (text: string) => Promise, - private readonly controlPlaneClient: ControlPlaneClient, - ) { - this.ide = ide; - this.ideSettingsPromise = ideSettingsPromise; - this.writeLog = writeLog; - - // Set local profile as default - const localProfileLoader = new LocalProfileLoader( - ide, - ideSettingsPromise, - writeLog, - ); - this.profiles = [new ProfileLifecycleManager(localProfileLoader)]; - this.selectedProfileId = localProfileLoader.profileId; - - // Always load local profile immediately in case control plane doesn't load - try { - this.loadConfig(); - } catch (e) { - console.error("Failed to load config: ", e); - } - - // Load control plane profiles - // TODO + private async fetchControlPlaneProfiles() { // Get the profiles and create their lifecycle managers this.controlPlaneClient.listWorkspaces().then(async (workspaces) => { + this.profiles = this.profiles.filter( + (profile) => profile.profileId === "local", + ); workspaces.forEach((workspace) => { const profileLoader = new ControlPlaneProfileLoader( workspace.id, workspace.name, this.controlPlaneClient, - ide, - ideSettingsPromise, - writeLog, + this.ide, + this.ideSettingsPromise, + this.writeLog, this.reloadConfig.bind(this), ); this.profiles.push(new ProfileLifecycleManager(profileLoader)); @@ -184,6 +163,36 @@ export class ConfigHandler { }); } + constructor( + private readonly ide: IDE, + private ideSettingsPromise: Promise, + private readonly writeLog: (text: string) => Promise, + private controlPlaneClient: ControlPlaneClient, + ) { + this.ide = ide; + this.ideSettingsPromise = ideSettingsPromise; + this.writeLog = writeLog; + + // Set local profile as default + const localProfileLoader = new LocalProfileLoader( + ide, + ideSettingsPromise, + writeLog, + ); + this.profiles = [new ProfileLifecycleManager(localProfileLoader)]; + this.selectedProfileId = localProfileLoader.profileId; + + // Always load local profile immediately in case control plane doesn't load + try { + this.loadConfig(); + } catch (e) { + console.error("Failed to load config: ", e); + } + + // Load control plane profiles + this.fetchControlPlaneProfiles(); + } + async setSelectedProfile(profileId: string) { this.selectedProfileId = profileId; const newConfig = await this.loadConfig(); @@ -209,6 +218,15 @@ export class ConfigHandler { this.reloadConfig(); } + updateControlPlaneSessionInfo( + sessionInfo: ControlPlaneSessionInfo | undefined, + ) { + this.controlPlaneClient = new ControlPlaneClient( + Promise.resolve(sessionInfo), + ); + this.fetchControlPlaneProfiles(); + } + private profilesListeners: ((profiles: ProfileDescription[]) => void)[] = []; onDidChangeAvailableProfiles( listener: (profiles: ProfileDescription[]) => void, diff --git a/core/core.ts b/core/core.ts index 25e44a849..3244ae9ef 100644 --- a/core/core.ts +++ b/core/core.ts @@ -248,9 +248,9 @@ export class Core { (provider) => provider.description.title === "docs", ); - const siteIndexingOptions: SiteIndexingConfig[] = provider ? - (mProvider => mProvider?.options?.sites || [])({ ...provider }) : - []; + const siteIndexingOptions: SiteIndexingConfig[] = provider + ? ((mProvider) => mProvider?.options?.sites || [])({ ...provider }) + : []; await this.indexDocs(siteIndexingOptions, msg.data.reIndex); this.ide.infoPopup("Docs indexing completed"); @@ -619,6 +619,9 @@ export class Core { on("didChangeSelectedProfile", (msg) => { this.configHandler.setSelectedProfile(msg.data.id); }); + on("didChangeControlPlaneSessionInfo", async (msg) => { + this.configHandler.updateControlPlaneSessionInfo(msg.data.sessionInfo); + }); } private indexingCancellationController: AbortController | undefined; @@ -637,12 +640,18 @@ export class Core { } } - private async indexDocs(sites: SiteIndexingConfig[], reIndex: boolean): Promise { + private async indexDocs( + sites: SiteIndexingConfig[], + reIndex: boolean, + ): Promise { for (const site of sites) { - for await (const update of this.docsService.indexAndAdd(site, new TransformersJsEmbeddingsProvider(), reIndex)) { + for await (const update of this.docsService.indexAndAdd( + site, + new TransformersJsEmbeddingsProvider(), + reIndex, + )) { // Temporary disabled posting progress updates to the UI due to // possible collision with code indexing progress updates. - // this.messenger.request("indexProgress", update); // this.indexingState = update; } diff --git a/extensions/vscode/src/extension/VsCodeExtension.ts b/extensions/vscode/src/extension/VsCodeExtension.ts index d0b406a8a..a4db8ed6d 100644 --- a/extensions/vscode/src/extension/VsCodeExtension.ts +++ b/extensions/vscode/src/extension/VsCodeExtension.ts @@ -257,12 +257,12 @@ export class VsCodeExtension { }); // When GitHub sign-in status changes, reload config - vscode.authentication.onDidChangeSessions((e) => { + vscode.authentication.onDidChangeSessions(async (e) => { if (e.provider.id === "github") { this.configHandler.reloadConfig(); } else if (e.provider.id === "continue") { + const sessionInfo = await getControlPlaneSessionInfo(true); this.webviewProtocolPromise.then(async (webviewProtocol) => { - const sessionInfo = await getControlPlaneSessionInfo(true); webviewProtocol.request("didChangeControlPlaneSessionInfo", { sessionInfo, }); @@ -270,6 +270,7 @@ export class VsCodeExtension { // To make sure continue-proxy models and anything else requiring it get updated access token this.configHandler.reloadConfig(); }); + this.core.invoke("didChangeControlPlaneSessionInfo", { sessionInfo }); } }); diff --git a/gui/src/components/ProfileSwitcher.tsx b/gui/src/components/ProfileSwitcher.tsx index e1545ba5a..24fc99b31 100644 --- a/gui/src/components/ProfileSwitcher.tsx +++ b/gui/src/components/ProfileSwitcher.tsx @@ -160,58 +160,62 @@ function ProfileSwitcher(props: {}) { return ( <> - {controlServerBetaEnabled && profiles.length > 0 && ( - { - ideMessenger.request("didChangeSelectedProfile", { id }); - }} - > -
- -
{selectedProfile()?.title}
-
-
-
- {topDiv && - ReactDOM.createPortal( - - - {profiles.map((option, idx) => ( - 1} - /> - ))} -
- {profiles.length === 0 ? ( - No profiles found - ) : ( - "Select profile" - )} -
-
-
, - topDiv, - )} -
-
- )} + {controlServerBetaEnabled && + profiles.some((profile) => profile.id !== "local") && ( + { + ideMessenger.request("didChangeSelectedProfile", { id }); + }} + > +
+ +
{selectedProfile()?.title}
+
+
+
+ {topDiv && + ReactDOM.createPortal( + + + {profiles.map((option, idx) => ( + 1} + /> + ))} +
+ {profiles.length === 0 ? ( + No profiles found + ) : ( + "Select profile" + )} +
+
+
, + topDiv, + )} +
+
+ )} {/* Settings button (either opens config.json or /settings page in control plane) */}