Skip to content

Commit e7e64b6

Browse files
committed
More elegant way to generate arrays of keys from a type with full type safety
1 parent 1384077 commit e7e64b6

File tree

4 files changed

+193
-196
lines changed

4 files changed

+193
-196
lines changed

packages/types/src/__tests__/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// npx vitest run src/__tests__/index.test.ts
1+
// npx vitest run --globals src/__tests__/index.test.ts
22

33
import { GLOBAL_STATE_KEYS } from "../index.js"
44

packages/types/src/global-settings.ts

Lines changed: 106 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { z } from "zod"
22

3-
import type { Keys } from "./type-fu.js"
3+
import { type Keys, keysOf } from "./type-fu.js"
44
import {
55
type ProviderSettings,
66
PROVIDER_SETTINGS_KEYS,
@@ -34,8 +34,6 @@ export const globalSettingsSchema = z.object({
3434
autoApprovalEnabled: z.boolean().optional(),
3535
alwaysAllowReadOnly: z.boolean().optional(),
3636
alwaysAllowReadOnlyOutsideWorkspace: z.boolean().optional(),
37-
codebaseIndexModels: codebaseIndexModelsSchema.optional(),
38-
codebaseIndexConfig: codebaseIndexConfigSchema.optional(),
3937
alwaysAllowWrite: z.boolean().optional(),
4038
alwaysAllowWriteOutsideWorkspace: z.boolean().optional(),
4139
writeDelayMs: z.number().optional(),
@@ -85,6 +83,9 @@ export const globalSettingsSchema = z.object({
8583
fuzzyMatchThreshold: z.number().optional(),
8684
experiments: experimentsSchema.optional(),
8785

86+
codebaseIndexModels: codebaseIndexModelsSchema.optional(),
87+
codebaseIndexConfig: codebaseIndexConfigSchema.optional(),
88+
8889
language: languagesSchema.optional(),
8990

9091
telemetrySetting: telemetrySettingsSchema.optional(),
@@ -103,91 +104,87 @@ export const globalSettingsSchema = z.object({
103104

104105
export type GlobalSettings = z.infer<typeof globalSettingsSchema>
105106

106-
type GlobalSettingsRecord = Record<Keys<GlobalSettings>, undefined>
107-
108-
const globalSettingsRecord: GlobalSettingsRecord = {
109-
codebaseIndexModels: undefined,
110-
codebaseIndexConfig: undefined,
111-
currentApiConfigName: undefined,
112-
listApiConfigMeta: undefined,
113-
pinnedApiConfigs: undefined,
114-
115-
lastShownAnnouncementId: undefined,
116-
customInstructions: undefined,
117-
taskHistory: undefined,
118-
119-
condensingApiConfigId: undefined,
120-
customCondensingPrompt: undefined,
121-
122-
autoApprovalEnabled: undefined,
123-
alwaysAllowReadOnly: undefined,
124-
alwaysAllowReadOnlyOutsideWorkspace: undefined,
125-
alwaysAllowWrite: undefined,
126-
alwaysAllowWriteOutsideWorkspace: undefined,
127-
writeDelayMs: undefined,
128-
alwaysAllowBrowser: undefined,
129-
alwaysApproveResubmit: undefined,
130-
requestDelaySeconds: undefined,
131-
alwaysAllowMcp: undefined,
132-
alwaysAllowModeSwitch: undefined,
133-
alwaysAllowSubtasks: undefined,
134-
alwaysAllowExecute: undefined,
135-
allowedCommands: undefined,
136-
allowedMaxRequests: undefined,
137-
autoCondenseContextPercent: undefined,
138-
139-
browserToolEnabled: undefined,
140-
browserViewportSize: undefined,
141-
screenshotQuality: undefined,
142-
remoteBrowserEnabled: undefined,
143-
remoteBrowserHost: undefined,
144-
145-
enableCheckpoints: undefined,
146-
147-
ttsEnabled: undefined,
148-
ttsSpeed: undefined,
149-
soundEnabled: undefined,
150-
soundVolume: undefined,
151-
152-
maxOpenTabsContext: undefined,
153-
maxWorkspaceFiles: undefined,
154-
showRooIgnoredFiles: undefined,
155-
maxReadFileLine: undefined,
156-
157-
terminalOutputLineLimit: undefined,
158-
terminalShellIntegrationTimeout: undefined,
159-
terminalShellIntegrationDisabled: undefined,
160-
terminalCommandDelay: undefined,
161-
terminalPowershellCounter: undefined,
162-
terminalZshClearEolMark: undefined,
163-
terminalZshOhMy: undefined,
164-
terminalZshP10k: undefined,
165-
terminalZdotdir: undefined,
166-
terminalCompressProgressBar: undefined,
167-
168-
rateLimitSeconds: undefined,
169-
diffEnabled: undefined,
170-
fuzzyMatchThreshold: undefined,
171-
experiments: undefined,
172-
173-
language: undefined,
174-
175-
telemetrySetting: undefined,
176-
177-
mcpEnabled: undefined,
178-
enableMcpServerCreation: undefined,
179-
180-
mode: undefined,
181-
modeApiConfigs: undefined,
182-
customModes: undefined,
183-
customModePrompts: undefined,
184-
customSupportPrompts: undefined,
185-
enhancementApiConfigId: undefined,
186-
cachedChromeHostUrl: undefined,
187-
historyPreviewCollapsed: undefined,
188-
}
189-
190-
export const GLOBAL_SETTINGS_KEYS = Object.keys(globalSettingsRecord) as Keys<GlobalSettings>[]
107+
export const GLOBAL_SETTINGS_KEYS = keysOf<GlobalSettings>()([
108+
"currentApiConfigName",
109+
"listApiConfigMeta",
110+
"pinnedApiConfigs",
111+
112+
"lastShownAnnouncementId",
113+
"customInstructions",
114+
"taskHistory",
115+
116+
"condensingApiConfigId",
117+
"customCondensingPrompt",
118+
119+
"autoApprovalEnabled",
120+
"alwaysAllowReadOnly",
121+
"alwaysAllowReadOnlyOutsideWorkspace",
122+
"alwaysAllowWrite",
123+
"alwaysAllowWriteOutsideWorkspace",
124+
"writeDelayMs",
125+
"alwaysAllowBrowser",
126+
"alwaysApproveResubmit",
127+
"requestDelaySeconds",
128+
"alwaysAllowMcp",
129+
"alwaysAllowModeSwitch",
130+
"alwaysAllowSubtasks",
131+
"alwaysAllowExecute",
132+
"allowedCommands",
133+
"allowedMaxRequests",
134+
"autoCondenseContextPercent",
135+
136+
"browserToolEnabled",
137+
"browserViewportSize",
138+
"screenshotQuality",
139+
"remoteBrowserEnabled",
140+
"remoteBrowserHost",
141+
142+
"enableCheckpoints",
143+
144+
"ttsEnabled",
145+
"ttsSpeed",
146+
"soundEnabled",
147+
"soundVolume",
148+
149+
"maxOpenTabsContext",
150+
"maxWorkspaceFiles",
151+
"showRooIgnoredFiles",
152+
"maxReadFileLine",
153+
154+
"terminalOutputLineLimit",
155+
"terminalShellIntegrationTimeout",
156+
"terminalShellIntegrationDisabled",
157+
"terminalCommandDelay",
158+
"terminalPowershellCounter",
159+
"terminalZshClearEolMark",
160+
"terminalZshOhMy",
161+
"terminalZshP10k",
162+
"terminalZdotdir",
163+
"terminalCompressProgressBar",
164+
165+
"rateLimitSeconds",
166+
"diffEnabled",
167+
"fuzzyMatchThreshold",
168+
"experiments",
169+
170+
"codebaseIndexModels",
171+
"codebaseIndexConfig",
172+
173+
"language",
174+
175+
"telemetrySetting",
176+
"mcpEnabled",
177+
"enableMcpServerCreation",
178+
179+
"mode",
180+
"modeApiConfigs",
181+
"customModes",
182+
"customModePrompts",
183+
"customSupportPrompts",
184+
"enhancementApiConfigId",
185+
"cachedChromeHostUrl",
186+
"historyPreviewCollapsed",
187+
])
191188

192189
/**
193190
* RooCodeSettings
@@ -224,33 +221,27 @@ export type SecretState = Pick<
224221
| "codeIndexQdrantApiKey"
225222
>
226223

227-
export type CodeIndexSecrets = "codeIndexOpenAiKey" | "codeIndexQdrantApiKey"
228-
229-
type SecretStateRecord = Record<Keys<SecretState>, undefined>
230-
231-
const secretStateRecord: SecretStateRecord = {
232-
apiKey: undefined,
233-
glamaApiKey: undefined,
234-
openRouterApiKey: undefined,
235-
awsAccessKey: undefined,
236-
awsSecretKey: undefined,
237-
awsSessionToken: undefined,
238-
openAiApiKey: undefined,
239-
geminiApiKey: undefined,
240-
openAiNativeApiKey: undefined,
241-
deepSeekApiKey: undefined,
242-
mistralApiKey: undefined,
243-
unboundApiKey: undefined,
244-
requestyApiKey: undefined,
245-
xaiApiKey: undefined,
246-
groqApiKey: undefined,
247-
chutesApiKey: undefined,
248-
litellmApiKey: undefined,
249-
codeIndexOpenAiKey: undefined,
250-
codeIndexQdrantApiKey: undefined,
251-
}
252-
253-
export const SECRET_STATE_KEYS = Object.keys(secretStateRecord) as Keys<SecretState>[]
224+
export const SECRET_STATE_KEYS = keysOf<SecretState>()([
225+
"apiKey",
226+
"glamaApiKey",
227+
"openRouterApiKey",
228+
"awsAccessKey",
229+
"awsSecretKey",
230+
"awsSessionToken",
231+
"openAiApiKey",
232+
"geminiApiKey",
233+
"openAiNativeApiKey",
234+
"deepSeekApiKey",
235+
"mistralApiKey",
236+
"unboundApiKey",
237+
"requestyApiKey",
238+
"xaiApiKey",
239+
"groqApiKey",
240+
"chutesApiKey",
241+
"litellmApiKey",
242+
"codeIndexOpenAiKey",
243+
"codeIndexQdrantApiKey",
244+
])
254245

255246
export const isSecretStateKey = (key: string): key is Keys<SecretState> =>
256247
SECRET_STATE_KEYS.includes(key as Keys<SecretState>)

0 commit comments

Comments
 (0)