Skip to content

Commit d299739

Browse files
committed
Add profile management functions to API
1 parent 6534f5a commit d299739

File tree

4 files changed

+138
-1
lines changed

4 files changed

+138
-1
lines changed

src/core/config/ProviderSettingsManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class ProviderSettingsManager {
4040
this.initialize().catch(console.error)
4141
}
4242

43-
private generateId() {
43+
public generateId() {
4444
return Math.random().toString(36).substring(2, 15)
4545
}
4646

src/exports/api.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,82 @@ export class API extends EventEmitter<RooCodeEvents> implements RooCodeAPI {
163163
await this.sidebarProvider.postStateToWebview()
164164
}
165165

166+
public async createProfile(name: string): Promise<string> {
167+
// Input validation
168+
if (!name || !name.trim()) {
169+
throw new Error("Profile name cannot be empty")
170+
}
171+
172+
const currentSettings = this.getConfiguration()
173+
const profiles = currentSettings.listApiConfigMeta || []
174+
175+
if (profiles.some((profile) => profile.name === name)) {
176+
throw new Error(`A profile with the name "${name}" already exists`)
177+
}
178+
179+
// Generate unique ID and create profile
180+
const id = this.sidebarProvider.providerSettingsManager.generateId()
181+
const newProfile = {
182+
id,
183+
name: name.trim(),
184+
apiProvider: "openai" as const, // Type assertion for better type safety
185+
}
186+
187+
// Update configuration with new profile
188+
await this.setConfiguration({
189+
...currentSettings,
190+
listApiConfigMeta: [...profiles, newProfile],
191+
})
192+
return id
193+
}
194+
195+
public getProfiles(): string[] {
196+
const profiles = this.getConfiguration().listApiConfigMeta || []
197+
return profiles.map((profile) => profile.name)
198+
}
199+
200+
public async setActiveProfile(name: string): Promise<void> {
201+
const currentSettings = this.getConfiguration()
202+
const profiles = currentSettings.listApiConfigMeta || []
203+
204+
const profile = profiles.find((p) => p.name === name)
205+
if (!profile) {
206+
throw new Error(`Profile with name "${name}" does not exist`)
207+
}
208+
209+
await this.setConfiguration({
210+
...currentSettings,
211+
currentApiConfigName: profile.name,
212+
})
213+
}
214+
215+
public getActiveProfile(): string | undefined {
216+
return this.getConfiguration().currentApiConfigName
217+
}
218+
219+
public async deleteProfile(name: string): Promise<void> {
220+
const currentSettings = this.getConfiguration()
221+
const profiles = currentSettings.listApiConfigMeta || []
222+
const targetIndex = profiles.findIndex((p) => p.name === name)
223+
if (targetIndex === -1) {
224+
throw new Error(`Profile with name "${name}" does not exist`)
225+
}
226+
227+
const profileToDelete = profiles[targetIndex]
228+
profiles.splice(targetIndex, 1)
229+
230+
// If we're deleting the active profile, clear the currentApiConfigName
231+
const newSettings: RooCodeSettings = {
232+
...currentSettings,
233+
listApiConfigMeta: profiles,
234+
currentApiConfigName:
235+
currentSettings.currentApiConfigName === profileToDelete.name
236+
? undefined
237+
: currentSettings.currentApiConfigName,
238+
}
239+
await this.setConfiguration(newSettings)
240+
}
241+
166242
public isReady() {
167243
return this.sidebarProvider.viewLaunched
168244
}

src/exports/interface.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,39 @@ export interface RooCodeAPI extends EventEmitter<RooCodeEvents> {
7272
*/
7373
setConfiguration(values: RooCodeSettings): Promise<void>
7474

75+
/**
76+
* Creates a new API configuration profile
77+
* @param name The name of the profile
78+
* @returns The ID of the created profile
79+
*/
80+
createProfile(name: string): Promise<string>
81+
82+
/**
83+
* Returns a list of all configured profile names
84+
* @returns Array of profile names
85+
*/
86+
getProfiles(): string[]
87+
88+
/**
89+
* Changes the active API configuration profile
90+
* @param name The name of the profile to activate
91+
* @throws Error if the profile does not exist
92+
*/
93+
setActiveProfile(name: string): Promise<void>
94+
95+
/**
96+
* Returns the name of the currently active profile
97+
* @returns The profile name, or undefined if no profile is active
98+
*/
99+
getActiveProfile(): string | undefined
100+
101+
/**
102+
* Deletes a profile by name
103+
* @param name The name of the profile to delete
104+
* @throws Error if the profile does not exist
105+
*/
106+
deleteProfile(name: string): Promise<void>
107+
75108
/**
76109
* Returns true if the API is ready to use.
77110
*/

src/exports/roo-code.d.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,34 @@ interface RooCodeAPI extends EventEmitter<RooCodeEvents> {
572572
* @param values An object containing key-value pairs to set.
573573
*/
574574
setConfiguration(values: RooCodeSettings): Promise<void>
575+
/**
576+
* Creates a new API configuration profile
577+
* @param name The name of the profile
578+
* @returns The ID of the created profile
579+
*/
580+
createProfile(name: string): Promise<string>
581+
/**
582+
* Returns a list of all configured profile names
583+
* @returns Array of profile names
584+
*/
585+
getProfiles(): string[]
586+
/**
587+
* Changes the active API configuration profile
588+
* @param name The name of the profile to activate
589+
* @throws Error if the profile does not exist
590+
*/
591+
setActiveProfile(name: string): Promise<void>
592+
/**
593+
* Returns the name of the currently active profile
594+
* @returns The profile name, or undefined if no profile is active
595+
*/
596+
getActiveProfile(): string | undefined
597+
/**
598+
* Deletes a profile by name
599+
* @param name The name of the profile to delete
600+
* @throws Error if the profile does not exist
601+
*/
602+
deleteProfile(name: string): Promise<void>
575603
/**
576604
* Returns true if the API is ready to use.
577605
*/

0 commit comments

Comments
 (0)