refactor(experiments): simplify experiment config structure

- Remove redundant id field from ExperimentConfig interface
- Update UI components to use experiment keys directly
- Improve type safety by using key-based mapping instead of object values
This commit is contained in:
sam hoang
2025-01-27 15:30:35 +07:00
parent 7dd161824b
commit 8fa1d5a2f9
3 changed files with 29 additions and 16 deletions

View File

@@ -8,7 +8,6 @@ export type ExperimentKey = keyof typeof EXPERIMENT_IDS
export type ExperimentId = valueof<typeof EXPERIMENT_IDS>
export interface ExperimentConfig {
id: ExperimentId
name: string
description: string
enabled: boolean
@@ -18,21 +17,18 @@ type valueof<X> = X[keyof X]
export const experimentConfigsMap: Record<ExperimentKey, ExperimentConfig> = {
DIFF_STRATEGY: {
id: EXPERIMENT_IDS.DIFF_STRATEGY,
name: "Use experimental unified diff strategy",
description:
"Enable the experimental unified diff strategy. This strategy might reduce the number of retries caused by model errors but may cause unexpected behavior or incorrect edits. Only enable if you understand the risks and are willing to carefully review all changes.",
enabled: false,
},
SEARCH_AND_REPLACE: {
id: EXPERIMENT_IDS.SEARCH_AND_REPLACE,
name: "Use experimental search and replace tool",
description:
"Enable the experimental search and replace tool, allowing Roo to replace multiple instances of a search term in one request.",
enabled: false,
},
INSERT_BLOCK: {
id: EXPERIMENT_IDS.INSERT_BLOCK,
name: "Use experimental insert block tool",
description:
@@ -42,7 +38,10 @@ export const experimentConfigsMap: Record<ExperimentKey, ExperimentConfig> = {
}
export const experimentDefault = Object.fromEntries(
Object.entries(experimentConfigsMap).map(([_, config]) => [config.id, config.enabled]),
Object.entries(experimentConfigsMap).map(([_, config]) => [
EXPERIMENT_IDS[_ as keyof typeof EXPERIMENT_IDS] as ExperimentId,
config.enabled,
]),
) as Record<ExperimentId, boolean>
export const experiments = {
@@ -56,9 +55,15 @@ export const experiments = {
// Expose experiment details for UI - pre-compute from map for better performance
export const experimentLabels = Object.fromEntries(
Object.values(experimentConfigsMap).map((config) => [config.id, config.name]),
Object.entries(experimentConfigsMap).map(([_, config]) => [
EXPERIMENT_IDS[_ as keyof typeof EXPERIMENT_IDS] as ExperimentId,
config.name,
]),
) as Record<string, string>
export const experimentDescriptions = Object.fromEntries(
Object.values(experimentConfigsMap).map((config) => [config.id, config.description]),
Object.entries(experimentConfigsMap).map(([_, config]) => [
EXPERIMENT_IDS[_ as keyof typeof EXPERIMENT_IDS] as ExperimentId,
config.description,
]),
) as Record<string, string>

View File

@@ -1,14 +1,13 @@
import { VSCodeCheckbox } from "@vscode/webview-ui-toolkit/react"
interface ExperimentalFeatureProps {
id: string
name: string
description: string
enabled: boolean
onChange: (value: boolean) => void
}
const ExperimentalFeature = ({ id, name, description, enabled, onChange }: ExperimentalFeatureProps) => {
const ExperimentalFeature = ({ name, description, enabled, onChange }: ExperimentalFeatureProps) => {
return (
<div
style={{

View File

@@ -5,7 +5,7 @@ import { validateApiConfiguration, validateModelId } from "../../utils/validate"
import { vscode } from "../../utils/vscode"
import ApiOptions from "./ApiOptions"
import ExperimentalFeature from "./ExperimentalFeature"
import { EXPERIMENT_IDS, experimentConfigsMap } from "../../../../src/shared/experiments"
import { EXPERIMENT_IDS, experimentConfigsMap, ExperimentId, ExperimentKey } from "../../../../src/shared/experiments"
import ApiConfigManager from "./ApiConfigManager"
type SettingsViewProps = {
@@ -97,6 +97,8 @@ const SettingsView = ({ onDone }: SettingsViewProps) => {
apiConfiguration,
})
console.log("Experiments", experiments)
vscode.postMessage({
type: "updateExperimental",
values: experiments,
@@ -646,14 +648,21 @@ const SettingsView = ({ onDone }: SettingsViewProps) => {
</p>
</div>
)}
{Object.values(experimentConfigsMap)
.filter((config) => config.id !== EXPERIMENT_IDS.DIFF_STRATEGY)
{Object.entries(experimentConfigsMap)
.filter((config) => config[0] !== "DIFF_STRATEGY")
.map((config) => (
<ExperimentalFeature
key={config.id}
{...config}
enabled={experiments[config.id] ?? false}
onChange={(enabled) => setExperimentEnabled(config.id, enabled)}
key={config[0]}
{...config[1]}
enabled={
experiments[EXPERIMENT_IDS[config[0] as keyof typeof EXPERIMENT_IDS]] ?? false
}
onChange={(enabled) =>
setExperimentEnabled(
EXPERIMENT_IDS[config[0] as keyof typeof EXPERIMENT_IDS],
enabled,
)
}
/>
))}
</div>