From b4094a628168682df58a185021b139c50293b4a1 Mon Sep 17 00:00:00 2001 From: sam hoang Date: Tue, 4 Mar 2025 20:26:22 +0700 Subject: [PATCH] refactor by pr comment --- src/core/webview/ClineProvider.ts | 46 ++++++------------------------- src/shared/api.ts | 43 +++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 37 deletions(-) diff --git a/src/core/webview/ClineProvider.ts b/src/core/webview/ClineProvider.ts index d9a1525730..ae53e58df9 100644 --- a/src/core/webview/ClineProvider.ts +++ b/src/core/webview/ClineProvider.ts @@ -8,7 +8,7 @@ import * as path from "path" import * as vscode from "vscode" import simpleGit from "simple-git" -import { ApiConfiguration, ApiProvider, ModelInfo } from "../../shared/api" +import { ApiConfiguration, ApiProvider, ModelInfo, API_CONFIG_KEYS } from "../../shared/api" import { findLast } from "../../shared/array" import { CustomSupportPrompts, supportPrompt } from "../../shared/support-prompt" import { GlobalFileNames } from "../../shared/globalFileNames" @@ -2114,47 +2114,19 @@ export class ClineProvider implements vscode.WebviewViewProvider { } // Build the apiConfiguration object combining state values and secrets + // Using the dynamic approach with API_CONFIG_KEYS const apiConfiguration: ApiConfiguration = { - apiProvider, - apiModelId: stateValues.apiModelId, - glamaModelId: stateValues.glamaModelId, - glamaModelInfo: stateValues.glamaModelInfo, - awsRegion: stateValues.awsRegion, - awsUseCrossRegionInference: stateValues.awsUseCrossRegionInference, - awsProfile: stateValues.awsProfile, - awsUseProfile: stateValues.awsUseProfile, - vertexProjectId: stateValues.vertexProjectId, - vertexRegion: stateValues.vertexRegion, - openAiBaseUrl: stateValues.openAiBaseUrl, - openAiModelId: stateValues.openAiModelId, - openAiCustomModelInfo: stateValues.openAiCustomModelInfo, - openAiUseAzure: stateValues.openAiUseAzure, - ollamaModelId: stateValues.ollamaModelId, - ollamaBaseUrl: stateValues.ollamaBaseUrl, - lmStudioModelId: stateValues.lmStudioModelId, - lmStudioBaseUrl: stateValues.lmStudioBaseUrl, - anthropicBaseUrl: stateValues.anthropicBaseUrl, - modelMaxThinkingTokens: stateValues.modelMaxThinkingTokens, - mistralCodestralUrl: stateValues.mistralCodestralUrl, - azureApiVersion: stateValues.azureApiVersion, - openAiStreamingEnabled: stateValues.openAiStreamingEnabled, - openRouterModelId: stateValues.openRouterModelId, - openRouterModelInfo: stateValues.openRouterModelInfo, - openRouterBaseUrl: stateValues.openRouterBaseUrl, - openRouterUseMiddleOutTransform: stateValues.openRouterUseMiddleOutTransform, - vsCodeLmModelSelector: stateValues.vsCodeLmModelSelector, - unboundModelId: stateValues.unboundModelId, - unboundModelInfo: stateValues.unboundModelInfo, - requestyModelId: stateValues.requestyModelId, - requestyModelInfo: stateValues.requestyModelInfo, - modelTemperature: stateValues.modelTemperature, - modelMaxTokens: stateValues.modelMaxTokens, - lmStudioSpeculativeDecodingEnabled: stateValues.lmStudioSpeculativeDecodingEnabled, - lmStudioDraftModelId: stateValues.lmStudioDraftModelId, + // Dynamically add all API-related keys from stateValues + ...Object.fromEntries(API_CONFIG_KEYS.map((key) => [key, stateValues[key]])), // Add all secrets ...secretValues, } + // Ensure apiProvider is set properly if not already in state + if (!apiConfiguration.apiProvider) { + apiConfiguration.apiProvider = apiProvider + } + // Return the same structure as before return { apiConfiguration, diff --git a/src/shared/api.ts b/src/shared/api.ts index 2ce7162640..9709ba79fc 100644 --- a/src/shared/api.ts +++ b/src/shared/api.ts @@ -78,6 +78,49 @@ export type ApiConfiguration = ApiHandlerOptions & { id?: string // stable unique identifier } +// Import GlobalStateKey type from globalState.ts +import { GlobalStateKey } from "./globalState" + +// Define API configuration keys for dynamic object building +export const API_CONFIG_KEYS: GlobalStateKey[] = [ + "apiProvider", + "apiModelId", + "glamaModelId", + "glamaModelInfo", + "awsRegion", + "awsUseCrossRegionInference", + "awsProfile", + "awsUseProfile", + "vertexProjectId", + "vertexRegion", + "openAiBaseUrl", + "openAiModelId", + "openAiCustomModelInfo", + "openAiUseAzure", + "ollamaModelId", + "ollamaBaseUrl", + "lmStudioModelId", + "lmStudioBaseUrl", + "anthropicBaseUrl", + "modelMaxThinkingTokens", + "mistralCodestralUrl", + "azureApiVersion", + "openAiStreamingEnabled", + "openRouterModelId", + "openRouterModelInfo", + "openRouterBaseUrl", + "openRouterUseMiddleOutTransform", + "vsCodeLmModelSelector", + "unboundModelId", + "unboundModelInfo", + "requestyModelId", + "requestyModelInfo", + "modelTemperature", + "modelMaxTokens", + "lmStudioSpeculativeDecodingEnabled", + "lmStudioDraftModelId" +] + // Models export interface ModelInfo {