Skip to content

Commit 302dc2d

Browse files
authored
Export more types to the external API (#3383)
1 parent c21aa23 commit 302dc2d

File tree

6 files changed

+492
-403
lines changed

6 files changed

+492
-403
lines changed

src/core/config/ProviderSettingsManager.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ export class ProviderSettingsManager {
247247
* Preserves the ID from the input 'config' object if it exists,
248248
* otherwise generates a new one (for creation scenarios).
249249
*/
250-
public async saveConfig(name: string, config: ProviderSettingsWithId) {
250+
public async saveConfig(name: string, config: ProviderSettingsWithId): Promise<string> {
251251
try {
252252
return await this.lock(async () => {
253253
const providerProfiles = await this.load()
@@ -259,13 +259,16 @@ export class ProviderSettingsManager {
259259
const filteredConfig = providerSettingsSchemaDiscriminated.parse(config)
260260
providerProfiles.apiConfigs[name] = { ...filteredConfig, id }
261261
await this.store(providerProfiles)
262+
return id
262263
})
263264
} catch (error) {
264265
throw new Error(`Failed to save config: ${error}`)
265266
}
266267
}
267268

268-
public async getProfile(params: { name: string } | { id: string }) {
269+
public async getProfile(
270+
params: { name: string } | { id: string },
271+
): Promise<ProviderSettingsWithId & { name: string }> {
269272
try {
270273
return await this.lock(async () => {
271274
const providerProfiles = await this.load()

src/exports/api.ts

Lines changed: 88 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -178,90 +178,6 @@ export class API extends EventEmitter<RooCodeEvents> implements RooCodeAPI {
178178
await this.sidebarProvider.postMessageToWebview({ type: "invoke", invoke: "secondaryButtonClick" })
179179
}
180180

181-
public getConfiguration() {
182-
return this.sidebarProvider.getValues()
183-
}
184-
185-
public async setConfiguration(values: RooCodeSettings) {
186-
await this.sidebarProvider.setValues(values)
187-
await this.sidebarProvider.providerSettingsManager.saveConfig(values.currentApiConfigName || "default", values)
188-
await this.sidebarProvider.postStateToWebview()
189-
}
190-
191-
public async createProfile(name: string) {
192-
if (!name || !name.trim()) {
193-
throw new Error("Profile name cannot be empty")
194-
}
195-
196-
const currentSettings = this.getConfiguration()
197-
const profiles = currentSettings.listApiConfigMeta || []
198-
199-
if (profiles.some((profile) => profile.name === name)) {
200-
throw new Error(`A profile with the name "${name}" already exists`)
201-
}
202-
203-
const id = this.sidebarProvider.providerSettingsManager.generateId()
204-
205-
await this.setConfiguration({
206-
...currentSettings,
207-
listApiConfigMeta: [
208-
...profiles,
209-
{
210-
id,
211-
name: name.trim(),
212-
apiProvider: "openai" as const,
213-
},
214-
],
215-
})
216-
217-
return id
218-
}
219-
220-
private getProfilesMeta() {
221-
return this.getConfiguration().listApiConfigMeta || []
222-
}
223-
224-
public getProfiles() {
225-
return this.getProfilesMeta().map((profile) => profile.name)
226-
}
227-
228-
public hasProfile(name: string): boolean {
229-
return !!(this.getConfiguration().listApiConfigMeta || []).find((profile) => profile.name === name)
230-
}
231-
232-
public async setActiveProfile(name: string) {
233-
if (!this.hasProfile(name)) {
234-
throw new Error(`Profile with name "${name}" does not exist`)
235-
}
236-
237-
await this.sidebarProvider.activateProviderProfile({ name })
238-
}
239-
240-
public getActiveProfile() {
241-
return this.getConfiguration().currentApiConfigName
242-
}
243-
244-
public async deleteProfile(name: string) {
245-
const currentSettings = this.getConfiguration()
246-
const listApiConfigMeta = this.getProfilesMeta()
247-
const targetIndex = listApiConfigMeta.findIndex((p) => p.name === name)
248-
249-
if (targetIndex === -1) {
250-
throw new Error(`Profile with name "${name}" does not exist`)
251-
}
252-
253-
const profileToDelete = listApiConfigMeta[targetIndex]
254-
listApiConfigMeta.splice(targetIndex, 1)
255-
256-
// If we're deleting the active profile, clear the currentApiConfigName.
257-
const currentApiConfigName =
258-
currentSettings.currentApiConfigName === profileToDelete.name
259-
? undefined
260-
: currentSettings.currentApiConfigName
261-
262-
await this.setConfiguration({ ...currentSettings, listApiConfigMeta, currentApiConfigName })
263-
}
264-
265181
public isReady() {
266182
return this.sidebarProvider.viewLaunched
267183
}
@@ -327,4 +243,92 @@ export class API extends EventEmitter<RooCodeEvents> implements RooCodeAPI {
327243
this.logfile = undefined
328244
}
329245
}
246+
247+
// Global Settings Management
248+
249+
public getConfiguration() {
250+
return this.sidebarProvider.getValues()
251+
}
252+
253+
public async setConfiguration(values: RooCodeSettings) {
254+
await this.sidebarProvider.contextProxy.setValues(values)
255+
await this.sidebarProvider.providerSettingsManager.saveConfig(values.currentApiConfigName || "default", values)
256+
await this.sidebarProvider.postStateToWebview()
257+
}
258+
259+
// Provider Profile Management
260+
261+
private getProfilesMeta() {
262+
return this.getConfiguration().listApiConfigMeta || []
263+
}
264+
265+
public getProfiles() {
266+
return this.getProfilesMeta().map((profile) => profile.name)
267+
}
268+
269+
public hasProfile(name: string): boolean {
270+
return !!(this.getConfiguration().listApiConfigMeta || []).find((profile) => profile.name === name)
271+
}
272+
273+
public async createProfile(name: string) {
274+
if (!name || !name.trim()) {
275+
throw new Error("Profile name cannot be empty")
276+
}
277+
278+
const currentSettings = this.getConfiguration()
279+
const profiles = currentSettings.listApiConfigMeta || []
280+
281+
if (profiles.some((profile) => profile.name === name)) {
282+
throw new Error(`A profile with the name "${name}" already exists`)
283+
}
284+
285+
const id = this.sidebarProvider.providerSettingsManager.generateId()
286+
287+
await this.setConfiguration({
288+
...currentSettings,
289+
listApiConfigMeta: [
290+
...profiles,
291+
{
292+
id,
293+
name: name.trim(),
294+
apiProvider: "openai" as const,
295+
},
296+
],
297+
})
298+
299+
return id
300+
}
301+
302+
public async deleteProfile(name: string) {
303+
const currentSettings = this.getConfiguration()
304+
const listApiConfigMeta = this.getProfilesMeta()
305+
const targetIndex = listApiConfigMeta.findIndex((p) => p.name === name)
306+
307+
if (targetIndex === -1) {
308+
throw new Error(`Profile with name "${name}" does not exist`)
309+
}
310+
311+
const profileToDelete = listApiConfigMeta[targetIndex]
312+
listApiConfigMeta.splice(targetIndex, 1)
313+
314+
// If we're deleting the active profile, clear the currentApiConfigName.
315+
const currentApiConfigName =
316+
currentSettings.currentApiConfigName === profileToDelete.name
317+
? undefined
318+
: currentSettings.currentApiConfigName
319+
320+
await this.setConfiguration({ ...currentSettings, listApiConfigMeta, currentApiConfigName })
321+
}
322+
323+
public getActiveProfile(): string | undefined {
324+
return this.getConfiguration().currentApiConfigName
325+
}
326+
327+
public async setActiveProfile(name: string) {
328+
if (!this.hasProfile(name)) {
329+
throw new Error(`Profile with name "${name}" does not exist`)
330+
}
331+
332+
await this.sidebarProvider.activateProviderProfile({ name })
333+
}
330334
}

src/exports/interface.ts

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
import { EventEmitter } from "events"
22

3-
import type { ProviderSettings, GlobalSettings, ClineMessage, TokenUsage, RooCodeEvents } from "./types"
4-
export type { RooCodeSettings, ProviderSettings, GlobalSettings, ClineMessage, TokenUsage, RooCodeEvents }
3+
import type {
4+
GlobalSettings,
5+
ProviderSettings,
6+
ProviderSettingsEntry,
7+
ClineMessage,
8+
TokenUsage,
9+
RooCodeEvents,
10+
} from "./types"
11+
12+
export type {
13+
RooCodeSettings,
14+
GlobalSettings,
15+
ProviderSettings,
16+
ProviderSettingsEntry,
17+
ClineMessage,
18+
TokenUsage,
19+
RooCodeEvents,
20+
}
521

622
import { RooCodeEventName } from "../schemas"
723
export type { RooCodeEventName }
@@ -74,6 +90,11 @@ export interface RooCodeAPI extends EventEmitter<RooCodeEvents> {
7490
*/
7591
pressSecondaryButton(): Promise<void>
7692

93+
/**
94+
* Returns true if the API is ready to use.
95+
*/
96+
isReady(): boolean
97+
7798
/**
7899
* Returns the current configuration.
79100
* @returns The current configuration.
@@ -86,25 +107,26 @@ export interface RooCodeAPI extends EventEmitter<RooCodeEvents> {
86107
*/
87108
setConfiguration(values: RooCodeSettings): Promise<void>
88109

89-
/**
90-
* Creates a new API configuration profile
91-
* @param name The name of the profile
92-
* @returns The ID of the created profile
93-
*/
94-
createProfile(name: string): Promise<string>
95-
96110
/**
97111
* Returns a list of all configured profile names
98112
* @returns Array of profile names
99113
*/
100114
getProfiles(): string[]
101115

102116
/**
103-
* Changes the active API configuration profile
104-
* @param name The name of the profile to activate
117+
* Creates a new API configuration profile
118+
* @param name The name of the profile
119+
* @returns The ID of the created profile
120+
* @throws Error if the profile already exists
121+
*/
122+
createProfile(name: string, profile?: ProviderSettings): Promise<string>
123+
124+
/**
125+
* Deletes a profile by name
126+
* @param name The name of the profile to delete
105127
* @throws Error if the profile does not exist
106128
*/
107-
setActiveProfile(name: string): Promise<void>
129+
deleteProfile(name: string): Promise<void>
108130

109131
/**
110132
* Returns the name of the currently active profile
@@ -113,14 +135,9 @@ export interface RooCodeAPI extends EventEmitter<RooCodeEvents> {
113135
getActiveProfile(): string | undefined
114136

115137
/**
116-
* Deletes a profile by name
117-
* @param name The name of the profile to delete
138+
* Changes the active API configuration profile
139+
* @param name The name of the profile to activate
118140
* @throws Error if the profile does not exist
119141
*/
120-
deleteProfile(name: string): Promise<void>
121-
122-
/**
123-
* Returns true if the API is ready to use.
124-
*/
125-
isReady(): boolean
142+
setActiveProfile(name: string): Promise<void>
126143
}

0 commit comments

Comments
 (0)