Skip to content

Commit d79b94d

Browse files
committed
More type safety
1 parent 421deb8 commit d79b94d

File tree

1 file changed

+84
-44
lines changed

1 file changed

+84
-44
lines changed

src/shared/globalState.ts

Lines changed: 84 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@ import { z } from "zod"
22

33
import type {
44
ProviderName,
5-
ModelInfo,
6-
ExperimentId,
75
CheckpointStorage,
86
ToolGroup,
97
Language,
108
TelemetrySetting,
9+
ProviderSettingsKey,
1110
SecretStateKey,
1211
GlobalStateKey,
13-
GlobalSettings,
12+
ModelInfo,
13+
ApiConfigMeta,
14+
HistoryItem,
15+
GroupEntry,
16+
ModeConfig,
17+
ExperimentId,
1418
ProviderSettings,
15-
ProviderSettingsKey,
19+
GlobalSettings,
1620
} from "../exports/roo-code"
1721

1822
import { Keys, AssertEqual, Equals } from "../utils/type-fu"
@@ -103,7 +107,7 @@ const languages: Record<Language, true> = {
103107
"zh-TW": true,
104108
}
105109

106-
export const LANGUAGES = Object.keys(languages) as Language[]
110+
const LANGUAGES = Object.keys(languages) as Language[]
107111

108112
const languagesEnum: [Language, ...Language[]] = [LANGUAGES[0], ...LANGUAGES.slice(1).map((p) => p)]
109113

@@ -119,7 +123,7 @@ const telemetrySettings: Record<TelemetrySetting, true> = {
119123
disabled: true,
120124
}
121125

122-
export const TELEMETRY_SETTINGS = Object.keys(telemetrySettings) as TelemetrySetting[]
126+
const TELEMETRY_SETTINGS = Object.keys(telemetrySettings) as TelemetrySetting[]
123127

124128
const telemetrySettingsEnum: [TelemetrySetting, ...TelemetrySetting[]] = [
125129
TELEMETRY_SETTINGS[0],
@@ -242,7 +246,7 @@ export const isSecretStateKey = (key: string): key is SecretStateKey =>
242246
* GlobalStateKey
243247
*/
244248

245-
export const globalStateKeys: Record<GlobalStateKey, true> = {
249+
const globalStateKeys: Record<GlobalStateKey, true> = {
246250
apiProvider: true,
247251
apiModelId: true,
248252
glamaModelId: true,
@@ -344,20 +348,63 @@ export const globalStateKeys: Record<GlobalStateKey, true> = {
344348

345349
export const GLOBAL_STATE_KEYS = Object.keys(globalStateKeys) as GlobalStateKey[]
346350

347-
export const isGlobalStateKey = (key: string): key is GlobalStateKey =>
348-
GLOBAL_STATE_KEYS.includes(key as GlobalStateKey)
351+
/**
352+
* PassThroughStateKey
353+
*
354+
* TODO: Why is this necessary?
355+
*/
356+
357+
const PASS_THROUGH_STATE_KEYS = ["taskHistory"] as const
358+
359+
type PassThroughStateKey = (typeof PASS_THROUGH_STATE_KEYS)[number]
360+
361+
export const isPassThroughStateKey = (key: string): key is PassThroughStateKey =>
362+
PASS_THROUGH_STATE_KEYS.includes(key as PassThroughStateKey)
349363

350364
/**
351365
* Schemas
352366
*/
353367

368+
/**
369+
* ModelInfo
370+
*/
371+
372+
const modelInfoSchema = z.object({
373+
maxTokens: z.number().optional(),
374+
contextWindow: z.number(),
375+
supportsImages: z.boolean().optional(),
376+
supportsComputerUse: z.boolean().optional(),
377+
supportsPromptCache: z.boolean(),
378+
inputPrice: z.number().optional(),
379+
outputPrice: z.number().optional(),
380+
cacheWritesPrice: z.number().optional(),
381+
cacheReadsPrice: z.number().optional(),
382+
description: z.string().optional(),
383+
reasoningEffort: z.enum(["low", "medium", "high"]).optional(),
384+
thinking: z.boolean().optional(),
385+
})
386+
387+
// Throws a type error if the inferred type of the modelInfoSchema is not equal
388+
// to ModelInfo.
389+
type _AssertModelInfo = AssertEqual<Equals<ModelInfo, z.infer<typeof modelInfoSchema>>>
390+
391+
/**
392+
* ApiConfigMeta
393+
*/
394+
354395
const apiConfigMetaSchema = z.object({
355396
id: z.string(),
356397
name: z.string(),
357398
apiProvider: z.enum(providerNamesEnum).optional(),
358399
})
359400

360-
const taskHistorySchema = z.object({
401+
type _AssertApiConfigMeta = AssertEqual<Equals<ApiConfigMeta, z.infer<typeof apiConfigMetaSchema>>>
402+
403+
/**
404+
* HistoryItem
405+
*/
406+
407+
const historyItemSchema = z.object({
361408
id: z.string(),
362409
number: z.number(),
363410
ts: z.number(),
@@ -370,13 +417,17 @@ const taskHistorySchema = z.object({
370417
size: z.number().optional(),
371418
})
372419

373-
const toolGroupSchema = z.enum(toolGroupsEnum)
420+
type _AssertHistoryItem = AssertEqual<Equals<HistoryItem, z.infer<typeof historyItemSchema>>>
421+
422+
/**
423+
* GroupEntry
424+
*/
374425

375426
const groupEntrySchema = z.union([
376-
toolGroupSchema,
427+
z.enum(toolGroupsEnum),
377428
z
378429
.tuple([
379-
toolGroupSchema,
430+
z.enum(toolGroupsEnum),
380431
z.object({
381432
fileRegex: z.string().optional(),
382433
description: z.string().optional(),
@@ -385,6 +436,12 @@ const groupEntrySchema = z.union([
385436
.readonly(),
386437
])
387438

439+
type _AssertGroupEntry = AssertEqual<Equals<GroupEntry, z.infer<typeof groupEntrySchema>>>
440+
441+
/**
442+
* ModeConfig
443+
*/
444+
388445
const modeConfigSchema = z.object({
389446
slug: z.string(),
390447
name: z.string(),
@@ -394,6 +451,12 @@ const modeConfigSchema = z.object({
394451
source: z.enum(["global", "project"]).optional(),
395452
})
396453

454+
type _AssertModeConfig = AssertEqual<Equals<ModeConfig, z.infer<typeof modeConfigSchema>>>
455+
456+
/**
457+
* ExperimentId
458+
*/
459+
397460
const experimentsSchema = z.object({
398461
experimentalDiffStrategy: z.boolean(),
399462
search_and_replace: z.boolean(),
@@ -402,6 +465,10 @@ const experimentsSchema = z.object({
402465
multi_search_and_replace: z.boolean(),
403466
})
404467

468+
/**
469+
* GlobalSettings
470+
*/
471+
405472
// Throws a type error if the inferred type of the experimentsSchema is not
406473
// equal to ExperimentId.
407474
type _AssertExperiments = AssertEqual<Equals<ExperimentId, Keys<z.infer<typeof experimentsSchema>>>>
@@ -411,7 +478,7 @@ export const globalSettingsSchema = z.object({
411478
listApiConfigMeta: z.array(apiConfigMetaSchema).optional(),
412479
lastShownAnnouncementId: z.string().optional(),
413480
customInstructions: z.string().optional(),
414-
taskHistory: z.array(taskHistorySchema).optional(),
481+
taskHistory: z.array(historyItemSchema).optional(),
415482

416483
autoApprovalEnabled: z.boolean().optional(),
417484
alwaysAllowReadOnly: z.boolean().optional(),
@@ -484,24 +551,9 @@ export const globalSettingsSchema = z.object({
484551
// equal to GlobalSettings.
485552
type _AssertGlobalSettings = AssertEqual<Equals<GlobalSettings, z.infer<typeof globalSettingsSchema>>>
486553

487-
export const modelInfoSchema = z.object({
488-
maxTokens: z.number().optional(),
489-
contextWindow: z.number(),
490-
supportsImages: z.boolean().optional(),
491-
supportsComputerUse: z.boolean().optional(),
492-
supportsPromptCache: z.boolean(),
493-
inputPrice: z.number().optional(),
494-
outputPrice: z.number().optional(),
495-
cacheWritesPrice: z.number().optional(),
496-
cacheReadsPrice: z.number().optional(),
497-
description: z.string().optional(),
498-
reasoningEffort: z.enum(["low", "medium", "high"]).optional(),
499-
thinking: z.boolean().optional(),
500-
})
501-
502-
// Throws a type error if the inferred type of the modelInfoSchema is not equal
503-
// to ModelInfo.
504-
type _AssertModelInfo = AssertEqual<Equals<ModelInfo, z.infer<typeof modelInfoSchema>>>
554+
/**
555+
* ProviderSettings
556+
*/
505557

506558
export const providerSettingsSchema = z.object({
507559
apiProvider: z.enum(providerNamesEnum).optional(),
@@ -596,15 +648,3 @@ export const providerSettingsSchema = z.object({
596648
// Throws a type error if the inferred type of the providerSettingsSchema is not
597649
// equal to ProviderSettings.
598650
type _AssertProviderSettings = AssertEqual<Equals<ProviderSettings, z.infer<typeof providerSettingsSchema>>>
599-
600-
export const rooCodeSettingsSchema = globalSettingsSchema.merge(providerSettingsSchema)
601-
602-
/**
603-
* Pass-through state keys.
604-
* TODO: What are these?
605-
*/
606-
607-
export const PASS_THROUGH_STATE_KEYS = ["taskHistory"] as const
608-
609-
export const isPassThroughStateKey = (key: string): key is (typeof PASS_THROUGH_STATE_KEYS)[number] =>
610-
PASS_THROUGH_STATE_KEYS.includes(key as (typeof PASS_THROUGH_STATE_KEYS)[number])

0 commit comments

Comments
 (0)