Skip to content

Commit aa98e19

Browse files
committed
move select profile and org logic to thunks
1 parent c829825 commit aa98e19

File tree

29 files changed

+754
-870
lines changed

29 files changed

+754
-870
lines changed

core/config/ConfigHandler.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe.skip("Test the ConfigHandler and E2E config loading", () => {
1414
expect(profiles[0].id).toBe("local");
1515

1616
const currentProfile = testConfigHandler.currentProfile;
17-
expect(currentProfile.profileDescription.id).toBe("local");
17+
expect(currentProfile?.profileDescription.id).toBe("local");
1818
});
1919

2020
test("should load the default config successfully", async () => {

core/config/ConfigHandler.ts

+62-39
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ export class ConfigHandler {
4848
private readonly globalContext = new GlobalContext();
4949
private additionalContextProviders: IContextProvider[] = [];
5050
private profiles: ProfileLifecycleManager[];
51-
private selectedProfileId: string;
51+
private selectedProfileId: string | null;
5252
private selectedOrgId: string | null;
53+
private localProfileManager: ProfileLifecycleManager;
5354

5455
constructor(
5556
private readonly ide: IDE,
@@ -68,21 +69,25 @@ export class ConfigHandler {
6869
controlPlaneClient,
6970
writeLog,
7071
);
71-
this.profiles = [new ProfileLifecycleManager(localProfileLoader, this.ide)];
72+
this.localProfileManager = new ProfileLifecycleManager(
73+
localProfileLoader,
74+
this.ide,
75+
);
76+
this.profiles = [this.localProfileManager];
7277
this.selectedProfileId = localProfileLoader.description.id;
7378
this.selectedOrgId = null;
7479

80+
void this.init();
81+
}
82+
83+
private async init() {
7584
// Always load local profile immediately in case control plane doesn't load
7685
try {
77-
void this.loadConfig();
86+
await this.loadConfig();
7887
} catch (e) {
7988
console.error("Failed to load config: ", e);
8089
}
8190

82-
void this.init();
83-
}
84-
85-
private async init() {
8691
const workspaceId = await this.getWorkspaceId();
8792
const lastSelectedOrgIds =
8893
this.globalContext.get("lastSelectedOrgIdForWorkspace") ?? {};
@@ -97,16 +102,17 @@ export class ConfigHandler {
97102
void this.fetchControlPlaneProfiles();
98103
}
99104

100-
// This will be the local profile
101-
private get fallbackProfile() {
102-
return this.profiles[0];
103-
}
104-
105105
get currentProfile() {
106+
if (!this.selectedProfileId) {
107+
return null;
108+
}
109+
// IMPORTANT
110+
// We must fall back to null, not the first or local profiles
111+
// Because GUI must be the source of truth for selected profile
106112
return (
107113
this.profiles.find(
108114
(p) => p.profileDescription.id === this.selectedProfileId,
109-
) ?? this.fallbackProfile
115+
) ?? null
110116
);
111117
}
112118

@@ -165,32 +171,30 @@ export class ConfigHandler {
165171
}),
166172
);
167173

168-
this.profiles = [
169-
...this.profiles.filter(
170-
(profile) => profile.profileDescription.id === "local",
171-
),
172-
...hubProfiles,
173-
];
174+
this.profiles =
175+
this.selectedOrgId === null
176+
? [this.localProfileManager, ...hubProfiles]
177+
: hubProfiles;
174178

175179
this.notifyProfileListeners(
176180
this.profiles.map((profile) => profile.profileDescription),
177181
);
178182

179183
// Check the last selected workspace, and reload if it isn't local
180184
const workspaceId = await this.getWorkspaceId();
181-
const lastSelectedWorkspaceIds =
185+
const lastSelectedIds =
182186
this.globalContext.get("lastSelectedProfileForWorkspace") ?? {};
183187

184-
const selectedWorkspaceId = lastSelectedWorkspaceIds[workspaceId];
185-
if (selectedWorkspaceId) {
186-
this.selectedProfileId = selectedWorkspaceId;
188+
const selectedProfileId = lastSelectedIds[workspaceId];
189+
if (selectedProfileId) {
190+
this.selectedProfileId = selectedProfileId;
187191
await this.loadConfig();
188192
} else {
189193
// Otherwise we stick with local profile, and record choice
190-
lastSelectedWorkspaceIds[workspaceId] = this.selectedProfileId;
194+
lastSelectedIds[workspaceId] = this.selectedProfileId;
191195
this.globalContext.update(
192196
"lastSelectedProfileForWorkspace",
193-
lastSelectedWorkspaceIds,
197+
lastSelectedIds,
194198
);
195199
}
196200
})
@@ -249,9 +253,7 @@ export class ConfigHandler {
249253
this.controlPlaneClient
250254
.listWorkspaces()
251255
.then(async (workspaces) => {
252-
this.profiles = this.profiles.filter(
253-
(profile) => profile.profileDescription.id === "local",
254-
);
256+
this.profiles = [this.localProfileManager];
255257
workspaces.forEach((workspace) => {
256258
const profileLoader = new ControlPlaneProfileLoader(
257259
workspace.id,
@@ -273,18 +275,18 @@ export class ConfigHandler {
273275

274276
// Check the last selected workspace, and reload if it isn't local
275277
const workspaceId = await this.getWorkspaceId();
276-
const lastSelectedWorkspaceIds =
278+
const lastSelectedIds =
277279
this.globalContext.get("lastSelectedProfileForWorkspace") ?? {};
278-
const selectedWorkspaceId = lastSelectedWorkspaceIds[workspaceId];
279-
if (selectedWorkspaceId) {
280-
this.selectedProfileId = selectedWorkspaceId;
280+
const selectedProfileId = lastSelectedIds[workspaceId];
281+
if (selectedProfileId) {
282+
this.selectedProfileId = selectedProfileId;
281283
await this.loadConfig();
282284
} else {
283285
// Otherwise we stick with local profile, and record choice
284-
lastSelectedWorkspaceIds[workspaceId] = this.selectedProfileId;
286+
lastSelectedIds[workspaceId] = this.selectedProfileId;
285287
this.globalContext.update(
286288
"lastSelectedProfileForWorkspace",
287-
lastSelectedWorkspaceIds,
289+
lastSelectedIds,
288290
);
289291
}
290292
})
@@ -302,7 +304,7 @@ export class ConfigHandler {
302304
this.globalContext.update("lastSelectedOrgIdForWorkspace", selectedOrgs);
303305
}
304306

305-
async setSelectedProfile(profileId: string) {
307+
async setSelectedProfile(profileId: string | null) {
306308
this.selectedProfileId = profileId;
307309
const result = await this.loadConfig();
308310
this.notifyConfigListeners(result);
@@ -365,8 +367,15 @@ export class ConfigHandler {
365367
this.updateListeners.push(listener);
366368
}
367369

370+
// TODO: this isn't right, there are two different senses in which you want to "reload"
368371
async reloadConfig() {
369-
// TODO: this isn't right, there are two different senses in which you want to "reload"
372+
if (!this.currentProfile) {
373+
return {
374+
config: undefined,
375+
errors: [],
376+
configLoadInterrupted: true,
377+
};
378+
}
370379

371380
const { config, errors, configLoadInterrupted } =
372381
await this.currentProfile.reloadConfig(this.additionalContextProviders);
@@ -376,13 +385,20 @@ export class ConfigHandler {
376385
}
377386

378387
this.notifyConfigListeners({ config, errors, configLoadInterrupted });
379-
return { config, errors };
388+
return { config, errors, configLoadInterrupted };
380389
}
381390

382-
getSerializedConfig(): Promise<
391+
async getSerializedConfig(): Promise<
383392
ConfigResult<BrowserSerializedContinueConfig>
384393
> {
385-
return this.currentProfile.getSerializedConfig(
394+
if (!this.currentProfile) {
395+
return {
396+
config: undefined,
397+
errors: [],
398+
configLoadInterrupted: true,
399+
};
400+
}
401+
return await this.currentProfile.getSerializedConfig(
386402
this.additionalContextProviders,
387403
);
388404
}
@@ -392,6 +408,13 @@ export class ConfigHandler {
392408
}
393409

394410
async loadConfig(): Promise<ConfigResult<ContinueConfig>> {
411+
if (!this.currentProfile) {
412+
return {
413+
config: undefined,
414+
errors: [],
415+
configLoadInterrupted: true,
416+
};
417+
}
395418
return await this.currentProfile.loadConfig(
396419
this.additionalContextProviders,
397420
);

core/core.ts

+11-14
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,6 @@ export class Core {
7373

7474
private abortedMessageIds: Set<string> = new Set();
7575

76-
private async config() {
77-
return (await this.configHandler.loadConfig()).config;
78-
}
79-
8076
invoke<T extends keyof ToCoreProtocol>(
8177
messageType: T,
8278
data: ToCoreProtocol[T][0],
@@ -136,7 +132,8 @@ export class Core {
136132
const serializedResult = await this.configHandler.getSerializedConfig();
137133
this.messenger.send("configUpdate", {
138134
result: serializedResult,
139-
profileId: this.configHandler.currentProfile.profileDescription.id,
135+
profileId:
136+
this.configHandler.currentProfile?.profileDescription.id ?? null,
140137
});
141138
});
142139

@@ -281,10 +278,8 @@ export class Core {
281278
});
282279

283280
on("config/newPromptFile", async (msg) => {
284-
await createNewPromptFileV2(
285-
this.ide,
286-
(await this.config())?.experimental?.promptPath,
287-
);
281+
const { config } = await this.configHandler.loadConfig();
282+
await createNewPromptFileV2(this.ide, config?.experimental?.promptPath);
288283
await this.configHandler.reloadConfig();
289284
});
290285

@@ -341,7 +336,7 @@ export class Core {
341336
});
342337

343338
on("context/loadSubmenuItems", async (msg) => {
344-
const config = await this.config();
339+
const { config } = await this.configHandler.loadConfig();
345340
if (!config) {
346341
return [];
347342
}
@@ -358,13 +353,14 @@ export class Core {
358353
});
359354

360355
on("context/getContextItems", async (msg) => {
361-
const { name, query, fullInput, selectedCode, selectedModelTitle } =
362-
msg.data;
363-
const config = await this.config();
356+
const { config } = await this.configHandler.loadConfig();
364357
if (!config) {
365358
return [];
366359
}
367360

361+
const { name, query, fullInput, selectedCode, selectedModelTitle } =
362+
msg.data;
363+
368364
const llm = await this.configHandler.llmFromTitle(selectedModelTitle);
369365
const provider =
370366
config.contextProviders?.find(
@@ -454,7 +450,8 @@ export class Core {
454450
on("config/getSerializedProfileInfo", async (msg) => {
455451
return {
456452
result: await this.configHandler.getSerializedConfig(),
457-
profileId: this.configHandler.currentProfile.profileDescription.id,
453+
profileId:
454+
this.configHandler.currentProfile?.profileDescription.id ?? null,
458455
};
459456
});
460457

core/protocol/core.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export type ToCoreFromIdeOrWebviewProtocol = {
6262
undefined,
6363
{
6464
result: ConfigResult<BrowserSerializedContinueConfig>;
65-
profileId: string;
65+
profileId: string | null;
6666
},
6767
];
6868
"config/deleteModel": [{ title: string }, void];
@@ -184,8 +184,6 @@ export type ToCoreFromIdeOrWebviewProtocol = {
184184
"docs/getDetails": [{ startUrl: string }, DocsIndexingDetails];
185185
addAutocompleteModel: [{ model: ModelDescription }, void];
186186

187-
"profiles/switch": [{ id: string }, undefined];
188-
189187
"auth/getAuthUrl": [{ useOnboarding: boolean }, { url: string }];
190188
"tools/call": [
191189
{ toolCall: ToolCall; selectedModelTitle: string },

core/protocol/coreWebview.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ToCoreFromIdeOrWebviewProtocol } from "./core.js";
44
import { ToWebviewFromIdeOrCoreProtocol } from "./webview.js";
55

66
export type ToCoreFromWebviewProtocol = ToCoreFromIdeOrWebviewProtocol & {
7-
didChangeSelectedProfile: [{ id: string }, void];
7+
didChangeSelectedProfile: [{ id: string | null }, void];
88
didChangeSelectedOrg: [{ id: string | null }, void];
99
};
1010
export type ToWebviewFromCoreProtocol = ToWebviewFromIdeOrCoreProtocol & {

core/protocol/passThrough.ts

-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ export const WEBVIEW_TO_CORE_PASS_THROUGH: (keyof ToCoreFromWebviewProtocol)[] =
5757
//
5858
"completeOnboarding",
5959
"addAutocompleteModel",
60-
"profiles/switch",
6160
"didChangeSelectedProfile",
6261
"didChangeSelectedOrg",
6362
"tools/call",
@@ -82,7 +81,5 @@ export const CORE_TO_WEBVIEW_PASS_THROUGH: (keyof ToWebviewFromCoreProtocol)[] =
8281
"setTTSActive",
8382
"getWebviewHistoryLength",
8483
"getCurrentSessionId",
85-
"signInToControlPlane",
86-
"openDialogMessage",
8784
"docs/suggestions",
8885
];

core/protocol/webview.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export type ToWebviewFromIdeOrCoreProtocol = {
1212
configUpdate: [
1313
{
1414
result: ConfigResult<BrowserSerializedContinueConfig>;
15-
profileId: string;
15+
profileId: string | null;
1616
},
1717
void,
1818
];
@@ -37,8 +37,6 @@ export type ToWebviewFromIdeOrCoreProtocol = {
3737
setTTSActive: [boolean, void];
3838
getWebviewHistoryLength: [undefined, number];
3939
getCurrentSessionId: [undefined, string];
40-
signInToControlPlane: [undefined, void];
41-
openDialogMessage: ["account", void];
4240
"docs/suggestions": [PackageDocsResult[], void];
4341
"jetbrains/setColors": [Record<string, string>, void];
4442
};

core/util/GlobalContext.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import { SiteIndexingConfig } from "..";
1212
export type GlobalContextType = {
1313
indexingPaused: boolean;
1414
selectedTabAutocompleteModel: string;
15-
lastSelectedProfileForWorkspace: { [workspaceIdentifier: string]: string };
15+
lastSelectedProfileForWorkspace: {
16+
[workspaceIdentifier: string]: string | null;
17+
};
1618
lastSelectedOrgIdForWorkspace: {
1719
[workspaceIdentifier: string]: string | null;
1820
};

extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/actions/ContinuePluginActions.kt

-7
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,4 @@ class OpenMorePageAction : AnAction() {
134134
val params = mapOf("path" to "/more", "toggle" to true)
135135
continuePluginService.sendToWebview("navigateTo", params)
136136
}
137-
}
138-
139-
class OpenAccountDialogAction : AnAction() {
140-
override fun actionPerformed(e: AnActionEvent) {
141-
val continuePluginService = getContinuePluginService(e.project) ?: return
142-
continuePluginService.sendToWebview("openDialogMessage", "account")
143-
}
144137
}

extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/constants/MessageTypes.kt

-3
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ class MessageTypes {
7070
"setTTSActive",
7171
"getWebviewHistoryLength",
7272
"getCurrentSessionId",
73-
"signInToControlPlane",
74-
"openDialogMessage",
7573
"docs/suggestions",
7674
)
7775

@@ -126,7 +124,6 @@ class MessageTypes {
126124
//
127125
"completeOnboarding",
128126
"addAutocompleteModel",
129-
"profiles/switch",
130127
"didChangeSelectedProfile",
131128
"didChangeSelectedOrg",
132129
"tools/call",

0 commit comments

Comments
 (0)