diff --git a/core/config/ConfigHandler.ts b/core/config/ConfigHandler.ts index 34632085b..e43664098 100644 --- a/core/config/ConfigHandler.ts +++ b/core/config/ConfigHandler.ts @@ -26,19 +26,16 @@ import LocalProfileLoader from "./profile/LocalProfileLoader.js"; import PlatformProfileLoader from "./profile/PlatformProfileLoader.js"; import { OrganizationDescription, + OrgWithProfiles, ProfileDescription, ProfileLifecycleManager, + SerializedOrgWithProfiles, } from "./ProfileLifecycleManager.js"; export type { ProfileDescription }; type ConfigUpdateFunction = (payload: ConfigResult) => void; -type OrgWithProfiles = OrganizationDescription & { - profiles: ProfileLifecycleManager[]; - currentProfile: ProfileLifecycleManager | null; -}; - export class ConfigHandler { controlPlaneClient: ControlPlaneClient; private readonly globalContext = new GlobalContext(); @@ -134,6 +131,7 @@ export class ConfigHandler { [workspaceId]: selectedOrg.id, }); + this.organizations = orgs; this.currentOrg = selectedOrg; this.currentProfile = selectedOrg.currentProfile; await this.reloadConfig(); @@ -150,13 +148,25 @@ export class ConfigHandler { ); return [personalHubOrg, ...hubOrgs]; } else { - return [await this.getTeamsOrg(orgDescs[0])]; + // Should only ever be one teams org. Will be removed soon anyways + return await Promise.all(orgDescs.map((org) => this.getTeamsOrg(org))); } } else { return [await this.getLocalOrg()]; } } + getSerializedOrgs(): SerializedOrgWithProfiles[] { + return this.organizations.map((org) => ({ + iconUrl: org.iconUrl, + id: org.id, + name: org.name, + slug: org.slug, + profiles: org.profiles.map((profile) => profile.profileDescription), + selectedProfileId: org.currentProfile?.profileDescription.id || null, + })); + } + private async getHubProfiles(orgScopeId: string | null) { const assistants = await this.controlPlaneClient.listAssistants(orgScopeId); diff --git a/core/config/ProfileLifecycleManager.ts b/core/config/ProfileLifecycleManager.ts index 9d4bd2842..d93ab9236 100644 --- a/core/config/ProfileLifecycleManager.ts +++ b/core/config/ProfileLifecycleManager.ts @@ -32,6 +32,16 @@ export interface OrganizationDescription { slug: string | undefined; // TODO: This doesn't need to be undefined, just doing while transitioning the backend } +export type OrgWithProfiles = OrganizationDescription & { + profiles: ProfileLifecycleManager[]; + currentProfile: ProfileLifecycleManager | null; +}; + +export type SerializedOrgWithProfiles = OrganizationDescription & { + profiles: ProfileDescription[]; + selectedProfileId: string | null; +}; + export class ProfileLifecycleManager { private savedConfigResult: ConfigResult | undefined; private savedBrowserConfigResult?: ConfigResult; diff --git a/core/core.ts b/core/core.ts index 9ff5c9d08..3610f3dab 100644 --- a/core/core.ts +++ b/core/core.ts @@ -126,10 +126,9 @@ export class Core { this.messenger.send("configUpdate", { result: serializedResult, profileId: - this.configHandler.currentProfile?.profileDescription.id ?? null, - selectedOrgId: this.configHandler.selectedOrgId, - availableOrgProfiles: this.configHandler.listProfileDescriptions(), - availableOrgs: this.configHandler.organizations, + this.configHandler.currentProfile?.profileDescription.id || null, + organizations: this.configHandler.getSerializedOrgs(), + selectedOrgId: this.configHandler.currentOrg.id, }); // update additional submenu context providers registered via VSCode API @@ -297,7 +296,7 @@ export class Core { }); on("config/refreshProfiles", async (msg) => { - await this.configHandler.refreshAssistantsForSelectedOrg(); + await this.configHandler.refreshAll(); }); on("config/updateSharedConfig", async (msg) => { @@ -457,6 +456,8 @@ export class Core { result: await this.configHandler.getSerializedConfig(), profileId: this.configHandler.currentProfile?.profileDescription.id ?? null, + organizations: this.configHandler.getSerializedOrgs(), + selectedOrgId: this.configHandler.currentOrg.id, }; }); @@ -752,11 +753,15 @@ export class Core { void refreshIfNotIgnored(data.uris); // If it's a local assistant being created, we want to reload all assistants so it shows up in the list + let localAssistantCreated = false; for (const uri of data.uris) { if (isLocalAssistantFile(uri)) { - await this.configHandler.refreshAssistantsForSelectedOrg(); + localAssistantCreated = true; } } + if (localAssistantCreated) { + await this.configHandler.refreshAll(); + } } }); diff --git a/core/protocol/core.ts b/core/protocol/core.ts index d7a5fdd01..83ad67a85 100644 --- a/core/protocol/core.ts +++ b/core/protocol/core.ts @@ -30,6 +30,7 @@ import type { SlashCommandDescription, ToolCall, } from "../"; +import { SerializedOrgWithProfiles } from "../config/ProfileLifecycleManager"; import { ControlPlaneSessionInfo } from "../control-plane/client"; export type OnboardingModes = "Local" | "Best" | "Custom" | "Quickstart"; @@ -65,6 +66,8 @@ export type ToCoreFromIdeOrWebviewProtocol = { { result: ConfigResult; profileId: string | null; + organizations: SerializedOrgWithProfiles[]; + selectedOrgId: string; }, ]; "config/deleteModel": [{ title: string }, void]; diff --git a/core/protocol/webview.ts b/core/protocol/webview.ts index d8142a9ed..74ce31559 100644 --- a/core/protocol/webview.ts +++ b/core/protocol/webview.ts @@ -1,8 +1,5 @@ import { ConfigResult } from "@continuedev/config-yaml"; -import { - OrganizationDescription, - ProfileDescription, -} from "../config/ProfileLifecycleManager.js"; +import { SerializedOrgWithProfiles } from "../config/ProfileLifecycleManager.js"; import type { BrowserSerializedContinueConfig, ContextItemWithId, @@ -16,9 +13,8 @@ export type ToWebviewFromIdeOrCoreProtocol = { { result: ConfigResult; profileId: string | null; - availableOrgs: OrganizationDescription[]; + organizations: SerializedOrgWithProfiles[]; selectedOrgId: string | null; - availableOrgProfiles: ProfileDescription[]; }, void, ]; diff --git a/gui/src/components/mainInput/Lump/sections/docs/DocsSection.tsx b/gui/src/components/mainInput/Lump/sections/docs/DocsSection.tsx index cbe7e6e72..e7913d1ef 100644 --- a/gui/src/components/mainInput/Lump/sections/docs/DocsSection.tsx +++ b/gui/src/components/mainInput/Lump/sections/docs/DocsSection.tsx @@ -1,14 +1,12 @@ import { parseConfigYaml } from "@continuedev/config-yaml"; import { IndexingStatus } from "core"; import { useMemo } from "react"; -import { useDispatch } from "react-redux"; import { useAuth } from "../../../../../context/Auth"; import { useAppSelector } from "../../../../../redux/hooks"; import { ExploreBlocksButton } from "../ExploreBlocksButton"; import DocsIndexingStatus from "./DocsIndexingStatus"; function DocsIndexingStatuses() { - const dispatch = useDispatch(); const config = useAppSelector((store) => store.config.config); const indexingStatuses = useAppSelector( (store) => store.indexing.indexing.statuses, diff --git a/gui/src/components/modelSelection/platform/AssistantSelect.tsx b/gui/src/components/modelSelection/platform/AssistantSelect.tsx index 0d82f4183..6877876c1 100644 --- a/gui/src/components/modelSelection/platform/AssistantSelect.tsx +++ b/gui/src/components/modelSelection/platform/AssistantSelect.tsx @@ -9,7 +9,7 @@ import { import { useContext, useEffect, useMemo, useRef } from "react"; import { useAuth } from "../../../context/Auth"; import { IdeMessengerContext } from "../../../context/IdeMessenger"; -import { setSelectedProfile } from "../../../redux"; +import { selectCurrentOrg, setSelectedProfile } from "../../../redux"; import { useAppDispatch, useAppSelector } from "../../../redux/hooks"; import { fontSize, @@ -154,9 +154,7 @@ export default function AssistantSelect() { const dispatch = useAppDispatch(); const buttonRef = useRef(null); const { selectedProfile, selectedOrganization } = useAuth(); - const availableProfiles = useAppSelector( - (store) => store.profiles.availableProfiles, - ); + const currentOrg = useAppSelector(selectCurrentOrg); const ideMessenger = useContext(IdeMessengerContext); const { isToolbarExpanded } = useLump(); @@ -178,7 +176,7 @@ export default function AssistantSelect() { useEffect(() => { let lastToggleTime = 0; - const DEBOUNCE_MS = 500; + const DEBOUNCE_MS = 800; const handleKeyDown = (event: KeyboardEvent) => { if ( @@ -192,7 +190,7 @@ export default function AssistantSelect() { lastToggleTime = now; const profileIds = - availableProfiles?.map((profile) => profile.id) ?? []; + currentOrg?.profiles.map((profile) => profile.id) ?? []; // In case of 1 or 0 profiles just does nothing if (profileIds.length < 2) { return; @@ -203,6 +201,7 @@ export default function AssistantSelect() { const nextIndex = (curIndex + 1) % profileIds.length; nextId = profileIds[nextIndex]; } + // Optimistic update dispatch(setSelectedProfile(nextId)); ideMessenger.post("didChangeSelectedProfile", { id: nextId, @@ -215,7 +214,7 @@ export default function AssistantSelect() { return () => { window.removeEventListener("keydown", handleKeyDown); }; - }, [availableProfiles, selectedProfile]); + }, [currentOrg, selectedProfile]); const tinyFont = useFontSize(-4); const smallFont = useFontSize(-3); diff --git a/gui/src/context/Auth.tsx b/gui/src/context/Auth.tsx index 4b6b68fb8..870d9acca 100644 --- a/gui/src/context/Auth.tsx +++ b/gui/src/context/Auth.tsx @@ -11,7 +11,7 @@ import React, { useState, } from "react"; import ConfirmationDialog from "../components/dialogs/ConfirmationDialog"; -import { selectSelectedProfile } from "../redux/"; +import { selectCurrentOrg, selectSelectedProfile } from "../redux/"; import { useAppDispatch, useAppSelector } from "../redux/hooks"; import { setDialogMessage, setShowDialog } from "../redux/slices/uiSlice"; import { IdeMessengerContext } from "./IdeMessenger"; @@ -42,9 +42,9 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ ); // Orgs - const orgs = useAppSelector((store) => store.organizations.organizations); + const orgs = useAppSelector((store) => store.profiles.organizations); const selectedOrgId = useAppSelector( - (store) => store.organizations.selectedOrganizationId, + (store) => store.profiles.selectedOrganizationId, ); const selectedOrganization = useMemo(() => { if (!selectedOrgId) { @@ -54,7 +54,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ }, [orgs, selectedOrgId]); // Profiles - const profiles = useAppSelector((store) => store.profiles.availableProfiles); + const currentOrg = useAppSelector(selectCurrentOrg); const selectedProfile = useAppSelector(selectSelectedProfile); const login: AuthContextType["login"] = (useOnboarding: boolean) => { @@ -150,7 +150,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ logout, login, selectedProfile, - profiles, + profiles: currentOrg?.profiles ?? [], refreshProfiles, selectedOrganization, organizations: orgs, diff --git a/gui/src/hooks/useSetup.ts b/gui/src/hooks/useSetup.ts index 60fbdcd9b..5b3e665ad 100644 --- a/gui/src/hooks/useSetup.ts +++ b/gui/src/hooks/useSetup.ts @@ -2,11 +2,11 @@ import { useCallback, useContext, useEffect, useRef } from "react"; import { VSC_THEME_COLOR_VARS } from "../components"; import { IdeMessengerContext } from "../context/IdeMessenger"; -import { ConfigResult } from "@continuedev/config-yaml"; -import { BrowserSerializedContinueConfig } from "core"; +import { FromCoreProtocol } from "core/protocol"; import { initializeProfilePreferences, - selectSelectedProfileId, + setOrganizations, + setSelectedOrgId, setSelectedProfile, } from "../redux"; import { useAppDispatch, useAppSelector } from "../redux/hooks"; @@ -36,25 +36,28 @@ function useSetup() { const ideMessenger = useContext(IdeMessengerContext); const history = useAppSelector((store) => store.session.history); const defaultModel = useAppSelector(selectDefaultModel); - const selectedProfileId = useAppSelector(selectSelectedProfileId); + const selectedProfileId = useAppSelector( + (store) => store.profiles.selectedProfileId, + ); const hasDoneInitialConfigLoad = useRef(false); const handleConfigUpdate = useCallback( - async ( - isInitial: boolean, - result: { - result: ConfigResult; - profileId: string | null; - }, - ) => { - const { result: configResult, profileId } = result; + async (isInitial: boolean, result: FromCoreProtocol["configUpdate"][0]) => { + const { + result: configResult, + profileId, + organizations, + selectedOrgId, + } = result; if (isInitial && hasDoneInitialConfigLoad.current) { return; } hasDoneInitialConfigLoad.current = true; - dispatch(setConfigResult(configResult)); + dispatch(setOrganizations(organizations)); + dispatch(setSelectedOrgId(selectedOrgId)); dispatch(setSelectedProfile(profileId)); + dispatch(setConfigResult(configResult)); const isNewProfileId = profileId && profileId !== selectedProfileId; diff --git a/gui/src/pages/config/AccountButton.tsx b/gui/src/pages/config/AccountButton.tsx index 34dfe2f29..05f50cc2a 100644 --- a/gui/src/pages/config/AccountButton.tsx +++ b/gui/src/pages/config/AccountButton.tsx @@ -8,13 +8,13 @@ import { Transition, } from "../../components/ui"; import { useAuth } from "../../context/Auth"; -import { selectSelectedOrganization } from "../../redux"; +import { selectCurrentOrg } from "../../redux"; import { useAppSelector } from "../../redux/hooks"; import { ScopeSelect } from "./ScopeSelect"; export function AccountButton() { const { session, logout, login, organizations } = useAuth(); - const selectedOrg = useAppSelector(selectSelectedOrganization); + const selectedOrg = useAppSelector(selectCurrentOrg); if (!session) { return ( diff --git a/gui/src/pages/config/ScopeSelect.tsx b/gui/src/pages/config/ScopeSelect.tsx index 94dfe4039..19abd78f5 100644 --- a/gui/src/pages/config/ScopeSelect.tsx +++ b/gui/src/pages/config/ScopeSelect.tsx @@ -12,14 +12,14 @@ import { } from "../../components/ui/Listbox"; import { useAuth } from "../../context/Auth"; import { IdeMessengerContext } from "../../context/IdeMessenger"; -import { setSelectedOrgId } from "../../redux"; import { useAppDispatch, useAppSelector } from "../../redux/hooks"; +import { setSelectedOrgId } from "../../redux/slices/profiles/slice"; export function ScopeSelect() { const { organizations, selectedOrganization } = useAuth(); const ideMessenger = useContext(IdeMessengerContext); const selectedOrgId = useAppSelector( - (state) => state.organizations.selectedOrganizationId, + (state) => state.profiles.selectedOrganizationId, ); const dispatch = useAppDispatch(); diff --git a/gui/src/pages/config/UserSettingsForm.tsx b/gui/src/pages/config/UserSettingsForm.tsx index d9a5cfd27..152d701ae 100644 --- a/gui/src/pages/config/UserSettingsForm.tsx +++ b/gui/src/pages/config/UserSettingsForm.tsx @@ -17,7 +17,7 @@ import { getFontSize } from "../../util"; export function UserSettingsForm() { /////// User settings section ////// const dispatch = useAppDispatch(); - const { selectedProfile, controlServerBetaEnabled } = useAuth(); + const { controlServerBetaEnabled } = useAuth(); const ideMessenger = useContext(IdeMessengerContext); const config = useAppSelector((state) => state.config.config); diff --git a/gui/src/redux/slices/organizations/index.ts b/gui/src/redux/slices/organizations/index.ts deleted file mode 100644 index 22be336b5..000000000 --- a/gui/src/redux/slices/organizations/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from "./slice"; -export * from "./thunks"; diff --git a/gui/src/redux/slices/organizations/slice.ts b/gui/src/redux/slices/organizations/slice.ts deleted file mode 100644 index 097b35a58..000000000 --- a/gui/src/redux/slices/organizations/slice.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { createSlice, PayloadAction } from "@reduxjs/toolkit"; -import { OrganizationDescription } from "core/config/ProfileLifecycleManager"; - -export interface OrganizationsState { - organizations: OrganizationDescription[]; - selectedOrganizationId: string | null; -} - -const initialState: OrganizationsState = { - organizations: [], - selectedOrganizationId: null, -}; - -export const organizationsSlice = createSlice({ - name: "organizations", - initialState, - reducers: { - setOrganizations: ( - state, - { payload }: PayloadAction, - ) => { - state.organizations = payload; - }, - setSelectedOrgId: (state, { payload }: PayloadAction) => { - state.selectedOrganizationId = payload; - }, - }, - selectors: { - selectSelectedOrganization: (state) => - state.organizations.find( - (org) => org.id === state.selectedOrganizationId, - ) ?? null, - }, -}); - -export const { setOrganizations } = organizationsSlice.actions; - -export const { selectSelectedOrganization } = organizationsSlice.selectors; - -export const { reducer: organizationsReducer } = organizationsSlice; diff --git a/gui/src/redux/slices/profiles/index.ts b/gui/src/redux/slices/profiles/index.ts index 22be336b5..52fbee8e7 100644 --- a/gui/src/redux/slices/profiles/index.ts +++ b/gui/src/redux/slices/profiles/index.ts @@ -1,2 +1 @@ export * from "./slice"; -export * from "./thunks"; diff --git a/gui/src/redux/slices/profiles/slice.ts b/gui/src/redux/slices/profiles/slice.ts index c8eabf168..6f3144736 100644 --- a/gui/src/redux/slices/profiles/slice.ts +++ b/gui/src/redux/slices/profiles/slice.ts @@ -1,6 +1,6 @@ import { createSlice, PayloadAction } from "@reduxjs/toolkit"; import { SlashCommandDescription } from "core"; -import { ProfileDescription } from "core/config/ConfigHandler"; +import { SerializedOrgWithProfiles } from "core/config/ProfileLifecycleManager"; const DEFAULT_SLASH_COMMANDS_BOOKMARKS_COUNT = 5; @@ -13,15 +13,40 @@ export interface PreferencesState { } export interface ProfilesState { - availableProfiles: ProfileDescription[] | null; + organizations: SerializedOrgWithProfiles[]; selectedProfileId: string | null; + selectedOrganizationId: string | null; preferencesByProfileId: Record; } const initialState: ProfilesState = { - availableProfiles: null, - selectedProfileId: null, preferencesByProfileId: {}, + selectedProfileId: null, + selectedOrganizationId: null, + organizations: [ + { + id: "personal", + profiles: [ + { + title: "Local Assistant", + id: "local", + errors: [], + profileType: "local", + uri: "", + iconUrl: "", + fullSlug: { + ownerSlug: "", + packageSlug: "", + versionSlug: "", + }, + }, + ], + slug: "", + selectedProfileId: "local", + name: "Personal", + iconUrl: "", + }, + ], }; export const profilesSlice = createSlice({ @@ -30,12 +55,27 @@ export const profilesSlice = createSlice({ reducers: { setSelectedProfile: (state, { payload }: PayloadAction) => { state.selectedProfileId = payload; + const currentOrg = state.organizations.find( + (o) => o.id === state.selectedOrganizationId, + ); + if (currentOrg) { + currentOrg.selectedProfileId = payload; + } }, - setAvailableProfiles: ( + setOrganizations: ( state, - { payload }: PayloadAction, + { payload }: PayloadAction, ) => { - state.availableProfiles = payload; + state.organizations = payload; + }, + setSelectedOrgId: (state, { payload }: PayloadAction) => { + state.selectedOrganizationId = payload; + const org = state.organizations.find((o) => o.id === payload); + if (org) { + state.selectedProfileId = org.selectedProfileId; + } else { + state.selectedProfileId = null; + } }, initializeProfilePreferences: ( state, @@ -102,15 +142,27 @@ export const profilesSlice = createSlice({ }, selectors: { selectSelectedProfile: (state) => { - return ( - state.availableProfiles?.find( + const selectedOrg = state.organizations.find( + (org) => org.id === state.selectedOrganizationId, + ); + if (selectedOrg) { + const profile = selectedOrg.profiles.find( (profile) => profile.id === state.selectedProfileId, + ); + return profile ?? null; + } else { + return null; + } + }, + + selectCurrentOrg: (state) => { + return ( + state.organizations.find( + (org) => org.id === state.selectedOrganizationId, ) ?? null ); }, - selectSelectedProfileId: (state) => state.selectedProfileId, - selectBookmarkedSlashCommands: (state) => { if (!state.selectedProfileId) return []; const preferences = state.preferencesByProfileId[state.selectedProfileId]; @@ -122,18 +174,19 @@ export const profilesSlice = createSlice({ }); export const { - setAvailableProfiles, setSelectedProfile, bookmarkSlashCommand, unbookmarkSlashCommand, initializeProfilePreferences, + setOrganizations, + setSelectedOrgId, } = profilesSlice.actions; export const { selectSelectedProfile, - selectSelectedProfileId, selectBookmarkedSlashCommands, selectPreferencesByProfileId, + selectCurrentOrg, } = profilesSlice.selectors; export const { reducer: profilesReducer } = profilesSlice; diff --git a/gui/src/redux/store.ts b/gui/src/redux/store.ts index a008c6669..c69e65315 100644 --- a/gui/src/redux/store.ts +++ b/gui/src/redux/store.ts @@ -10,7 +10,7 @@ import { createFilter } from "redux-persist-transform-filter"; import autoMergeLevel2 from "redux-persist/lib/stateReconciler/autoMergeLevel2"; import storage from "redux-persist/lib/storage"; import { IdeMessenger, IIdeMessenger } from "../context/IdeMessenger"; -import { organizationsReducer, profilesReducer } from "./slices"; +import { profilesReducer } from "./slices"; import configReducer from "./slices/configSlice"; import editModeStateReducer from "./slices/editModeState"; import indexingReducer from "./slices/indexingSlice"; @@ -31,7 +31,6 @@ const rootReducer = combineReducers({ indexing: indexingReducer, tabs: tabsReducer, profiles: profilesReducer, - organizations: organizationsReducer, }); const saveSubsetFilters = [ @@ -56,8 +55,12 @@ const saveSubsetFilters = [ createFilter("ui", ["toolSettings", "toolGroupSettings"]), createFilter("indexing", []), createFilter("tabs", ["tabs"]), - createFilter("organizations", ["selectedOrganizationId"]), - createFilter("profiles", ["preferencesByProfileId", "selectedProfileId"]), + createFilter("profiles", [ + "preferencesByProfileId", + "selectedProfileId", + "selectedOrganizationId", + "organizations", + ]), ]; const migrations: MigrationManifest = {