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 type ExperimentId = valueof<typeof EXPERIMENT_IDS>
export interface ExperimentConfig { export interface ExperimentConfig {
id: ExperimentId
name: string name: string
description: string description: string
enabled: boolean enabled: boolean
@@ -18,21 +17,18 @@ type valueof<X> = X[keyof X]
export const experimentConfigsMap: Record<ExperimentKey, ExperimentConfig> = { export const experimentConfigsMap: Record<ExperimentKey, ExperimentConfig> = {
DIFF_STRATEGY: { DIFF_STRATEGY: {
id: EXPERIMENT_IDS.DIFF_STRATEGY,
name: "Use experimental unified diff strategy", name: "Use experimental unified diff strategy",
description: 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.", "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, enabled: false,
}, },
SEARCH_AND_REPLACE: { SEARCH_AND_REPLACE: {
id: EXPERIMENT_IDS.SEARCH_AND_REPLACE,
name: "Use experimental search and replace tool", name: "Use experimental search and replace tool",
description: description:
"Enable the experimental search and replace tool, allowing Roo to replace multiple instances of a search term in one request.", "Enable the experimental search and replace tool, allowing Roo to replace multiple instances of a search term in one request.",
enabled: false, enabled: false,
}, },
INSERT_BLOCK: { INSERT_BLOCK: {
id: EXPERIMENT_IDS.INSERT_BLOCK,
name: "Use experimental insert block tool", name: "Use experimental insert block tool",
description: description:
@@ -42,7 +38,10 @@ export const experimentConfigsMap: Record<ExperimentKey, ExperimentConfig> = {
} }
export const experimentDefault = Object.fromEntries( 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> ) as Record<ExperimentId, boolean>
export const experiments = { export const experiments = {
@@ -56,9 +55,15 @@ export const experiments = {
// Expose experiment details for UI - pre-compute from map for better performance // Expose experiment details for UI - pre-compute from map for better performance
export const experimentLabels = Object.fromEntries( 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> ) as Record<string, string>
export const experimentDescriptions = Object.fromEntries( 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> ) as Record<string, string>

View File

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

View File

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