|
1 | | -import * as vscode from "vscode" |
2 | | - |
3 | | -import { |
4 | | - ORGANIZATION_ALLOW_ALL, |
5 | | - OrganizationAllowList, |
6 | | - OrganizationSettings, |
7 | | - organizationSettingsSchema, |
8 | | -} from "@roo-code/types" |
9 | | - |
10 | | -import { getRooCodeApiUrl } from "./Config" |
11 | | -import type { AuthService } from "./auth" |
12 | | -import { RefreshTimer } from "./RefreshTimer" |
13 | | - |
14 | | -const ORGANIZATION_SETTINGS_CACHE_KEY = "organization-settings" |
15 | | - |
16 | | -export class SettingsService { |
17 | | - private context: vscode.ExtensionContext |
18 | | - private authService: AuthService |
19 | | - private settings: OrganizationSettings | undefined = undefined |
20 | | - private timer: RefreshTimer |
21 | | - private log: (...args: unknown[]) => void |
22 | | - |
23 | | - constructor( |
24 | | - context: vscode.ExtensionContext, |
25 | | - authService: AuthService, |
26 | | - callback: () => void, |
27 | | - log?: (...args: unknown[]) => void, |
28 | | - ) { |
29 | | - this.context = context |
30 | | - this.authService = authService |
31 | | - this.log = log || console.log |
32 | | - |
33 | | - this.timer = new RefreshTimer({ |
34 | | - callback: async () => { |
35 | | - return await this.fetchSettings(callback) |
36 | | - }, |
37 | | - successInterval: 30000, |
38 | | - initialBackoffMs: 1000, |
39 | | - maxBackoffMs: 30000, |
40 | | - }) |
41 | | - } |
42 | | - |
43 | | - public initialize(): void { |
44 | | - this.loadCachedSettings() |
45 | | - |
46 | | - // Clear cached settings if we have missed a log out. |
47 | | - if (this.authService.getState() == "logged-out" && this.settings) { |
48 | | - this.removeSettings() |
49 | | - } |
50 | | - |
51 | | - this.authService.on("active-session", () => { |
52 | | - this.timer.start() |
53 | | - }) |
54 | | - |
55 | | - this.authService.on("logged-out", () => { |
56 | | - this.timer.stop() |
57 | | - this.removeSettings() |
58 | | - }) |
59 | | - |
60 | | - if (this.authService.hasActiveSession()) { |
61 | | - this.timer.start() |
62 | | - } |
63 | | - } |
64 | | - |
65 | | - private async fetchSettings(callback: () => void): Promise<boolean> { |
66 | | - const token = this.authService.getSessionToken() |
67 | | - |
68 | | - if (!token) { |
69 | | - return false |
70 | | - } |
71 | | - |
72 | | - try { |
73 | | - const response = await fetch(`${getRooCodeApiUrl()}/api/organization-settings`, { |
74 | | - headers: { |
75 | | - Authorization: `Bearer ${token}`, |
76 | | - }, |
77 | | - }) |
78 | | - |
79 | | - if (!response.ok) { |
80 | | - this.log( |
81 | | - "[cloud-settings] Failed to fetch organization settings:", |
82 | | - response.status, |
83 | | - response.statusText, |
84 | | - ) |
85 | | - return false |
86 | | - } |
87 | | - |
88 | | - const data = await response.json() |
89 | | - const result = organizationSettingsSchema.safeParse(data) |
90 | | - |
91 | | - if (!result.success) { |
92 | | - this.log("[cloud-settings] Invalid organization settings format:", result.error) |
93 | | - return false |
94 | | - } |
95 | | - |
96 | | - const newSettings = result.data |
97 | | - |
98 | | - if (!this.settings || this.settings.version !== newSettings.version) { |
99 | | - this.settings = newSettings |
100 | | - await this.cacheSettings() |
101 | | - callback() |
102 | | - } |
103 | | - |
104 | | - return true |
105 | | - } catch (error) { |
106 | | - this.log("[cloud-settings] Error fetching organization settings:", error) |
107 | | - return false |
108 | | - } |
109 | | - } |
110 | | - |
111 | | - private async cacheSettings(): Promise<void> { |
112 | | - await this.context.globalState.update(ORGANIZATION_SETTINGS_CACHE_KEY, this.settings) |
113 | | - } |
114 | | - |
115 | | - private loadCachedSettings(): void { |
116 | | - this.settings = this.context.globalState.get<OrganizationSettings>(ORGANIZATION_SETTINGS_CACHE_KEY) |
117 | | - } |
118 | | - |
119 | | - public getAllowList(): OrganizationAllowList { |
120 | | - return this.settings?.allowList || ORGANIZATION_ALLOW_ALL |
121 | | - } |
122 | | - |
123 | | - public getSettings(): OrganizationSettings | undefined { |
124 | | - return this.settings |
125 | | - } |
126 | | - |
127 | | - public async removeSettings(): Promise<void> { |
128 | | - this.settings = undefined |
129 | | - await this.cacheSettings() |
130 | | - } |
131 | | - |
132 | | - public dispose(): void { |
133 | | - this.timer.stop() |
134 | | - } |
| 1 | +import type { OrganizationAllowList, OrganizationSettings } from "@roo-code/types" |
| 2 | + |
| 3 | +/** |
| 4 | + * Interface for settings services that provide organization settings |
| 5 | + */ |
| 6 | +export interface SettingsService { |
| 7 | + /** |
| 8 | + * Get the organization allow list |
| 9 | + * @returns The organization allow list or default if none available |
| 10 | + */ |
| 11 | + getAllowList(): OrganizationAllowList |
| 12 | + |
| 13 | + /** |
| 14 | + * Get the current organization settings |
| 15 | + * @returns The organization settings or undefined if none available |
| 16 | + */ |
| 17 | + getSettings(): OrganizationSettings | undefined |
| 18 | + |
| 19 | + /** |
| 20 | + * Dispose of the settings service and clean up resources |
| 21 | + */ |
| 22 | + dispose(): void |
135 | 23 | } |
0 commit comments