Add profile management functions to API (#2251)

Co-authored-by: Greg Taylor <gregtaylor@netflix.com>
This commit is contained in:
Greg Taylor 2025-04-02 22:40:07 -07:00 committed by GitHub
parent 6534f5afe4
commit a3e6c912b0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 138 additions and 1 deletions

View file

@ -40,7 +40,7 @@ export class ProviderSettingsManager {
this.initialize().catch(console.error)
}
private generateId() {
public generateId() {
return Math.random().toString(36).substring(2, 15)
}

View file

@ -163,6 +163,82 @@ export class API extends EventEmitter<RooCodeEvents> implements RooCodeAPI {
await this.sidebarProvider.postStateToWebview()
}
public async createProfile(name: string): Promise<string> {
// Input validation
if (!name || !name.trim()) {
throw new Error("Profile name cannot be empty")
}
const currentSettings = this.getConfiguration()
const profiles = currentSettings.listApiConfigMeta || []
if (profiles.some((profile) => profile.name === name)) {
throw new Error(`A profile with the name "${name}" already exists`)
}
// Generate unique ID and create profile
const id = this.sidebarProvider.providerSettingsManager.generateId()
const newProfile = {
id,
name: name.trim(),
apiProvider: "openai" as const, // Type assertion for better type safety
}
// Update configuration with new profile
await this.setConfiguration({
...currentSettings,
listApiConfigMeta: [...profiles, newProfile],
})
return id
}
public getProfiles(): string[] {
const profiles = this.getConfiguration().listApiConfigMeta || []
return profiles.map((profile) => profile.name)
}
public async setActiveProfile(name: string): Promise<void> {
const currentSettings = this.getConfiguration()
const profiles = currentSettings.listApiConfigMeta || []
const profile = profiles.find((p) => p.name === name)
if (!profile) {
throw new Error(`Profile with name "${name}" does not exist`)
}
await this.setConfiguration({
...currentSettings,
currentApiConfigName: profile.name,
})
}
public getActiveProfile(): string | undefined {
return this.getConfiguration().currentApiConfigName
}
public async deleteProfile(name: string): Promise<void> {
const currentSettings = this.getConfiguration()
const profiles = currentSettings.listApiConfigMeta || []
const targetIndex = profiles.findIndex((p) => p.name === name)
if (targetIndex === -1) {
throw new Error(`Profile with name "${name}" does not exist`)
}
const profileToDelete = profiles[targetIndex]
profiles.splice(targetIndex, 1)
// If we're deleting the active profile, clear the currentApiConfigName
const newSettings: RooCodeSettings = {
...currentSettings,
listApiConfigMeta: profiles,
currentApiConfigName:
currentSettings.currentApiConfigName === profileToDelete.name
? undefined
: currentSettings.currentApiConfigName,
}
await this.setConfiguration(newSettings)
}
public isReady() {
return this.sidebarProvider.viewLaunched
}

View file

@ -72,6 +72,39 @@ export interface RooCodeAPI extends EventEmitter<RooCodeEvents> {
*/
setConfiguration(values: RooCodeSettings): Promise<void>
/**
* Creates a new API configuration profile
* @param name The name of the profile
* @returns The ID of the created profile
*/
createProfile(name: string): Promise<string>
/**
* Returns a list of all configured profile names
* @returns Array of profile names
*/
getProfiles(): string[]
/**
* Changes the active API configuration profile
* @param name The name of the profile to activate
* @throws Error if the profile does not exist
*/
setActiveProfile(name: string): Promise<void>
/**
* Returns the name of the currently active profile
* @returns The profile name, or undefined if no profile is active
*/
getActiveProfile(): string | undefined
/**
* Deletes a profile by name
* @param name The name of the profile to delete
* @throws Error if the profile does not exist
*/
deleteProfile(name: string): Promise<void>
/**
* Returns true if the API is ready to use.
*/

View file

@ -572,6 +572,34 @@ interface RooCodeAPI extends EventEmitter<RooCodeEvents> {
* @param values An object containing key-value pairs to set.
*/
setConfiguration(values: RooCodeSettings): Promise<void>
/**
* Creates a new API configuration profile
* @param name The name of the profile
* @returns The ID of the created profile
*/
createProfile(name: string): Promise<string>
/**
* Returns a list of all configured profile names
* @returns Array of profile names
*/
getProfiles(): string[]
/**
* Changes the active API configuration profile
* @param name The name of the profile to activate
* @throws Error if the profile does not exist
*/
setActiveProfile(name: string): Promise<void>
/**
* Returns the name of the currently active profile
* @returns The profile name, or undefined if no profile is active
*/
getActiveProfile(): string | undefined
/**
* Deletes a profile by name
* @param name The name of the profile to delete
* @throws Error if the profile does not exist
*/
deleteProfile(name: string): Promise<void>
/**
* Returns true if the API is ready to use.
*/