From 8fa1d5a2f9439b3d4078838b0fa1dbb638db6b3e Mon Sep 17 00:00:00 2001 From: sam hoang Date: Mon, 27 Jan 2025 15:30:35 +0700 Subject: [PATCH] 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 --- src/shared/experiments.ts | 19 +++++++++------ .../settings/ExperimentalFeature.tsx | 3 +-- .../src/components/settings/SettingsView.tsx | 23 +++++++++++++------ 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/shared/experiments.ts b/src/shared/experiments.ts index 0418334..71eda74 100644 --- a/src/shared/experiments.ts +++ b/src/shared/experiments.ts @@ -8,7 +8,6 @@ export type ExperimentKey = keyof typeof EXPERIMENT_IDS export type ExperimentId = valueof export interface ExperimentConfig { - id: ExperimentId name: string description: string enabled: boolean @@ -18,21 +17,18 @@ type valueof = X[keyof X] export const experimentConfigsMap: Record = { 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 = { } 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 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 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 diff --git a/webview-ui/src/components/settings/ExperimentalFeature.tsx b/webview-ui/src/components/settings/ExperimentalFeature.tsx index b5542b9..b896e8e 100644 --- a/webview-ui/src/components/settings/ExperimentalFeature.tsx +++ b/webview-ui/src/components/settings/ExperimentalFeature.tsx @@ -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 (
{ apiConfiguration, }) + console.log("Experiments", experiments) + vscode.postMessage({ type: "updateExperimental", values: experiments, @@ -646,14 +648,21 @@ const SettingsView = ({ onDone }: SettingsViewProps) => {

)} - {Object.values(experimentConfigsMap) - .filter((config) => config.id !== EXPERIMENT_IDS.DIFF_STRATEGY) + {Object.entries(experimentConfigsMap) + .filter((config) => config[0] !== "DIFF_STRATEGY") .map((config) => ( 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, + ) + } /> ))}