From 42bad19889aed1d03e4e6d4f6f799ddbb5ea7b8b Mon Sep 17 00:00:00 2001 From: celestial-vault <58194240+celestial-vault@users.noreply.github.com> Date: Sat, 11 Oct 2025 20:29:40 -0700 Subject: [PATCH] remote config state setting (#6776) * set remote config state and make it override task and global state * remove console logs * remove console log --- src/core/storage/StateManager.ts | 80 +++++++++++++--- src/core/storage/disk.ts | 5 +- .../storage}/remote-config/fetch.ts | 19 ++-- src/core/storage/remote-config/utils.ts | 93 +++++++++++++++++++ src/shared/remote-config/index.ts | 1 - 5 files changed, 178 insertions(+), 20 deletions(-) rename src/{shared => core/storage}/remote-config/fetch.ts (83%) create mode 100644 src/core/storage/remote-config/utils.ts diff --git a/src/core/storage/StateManager.ts b/src/core/storage/StateManager.ts index 70476611..83f9bb11 100644 --- a/src/core/storage/StateManager.ts +++ b/src/core/storage/StateManager.ts @@ -37,6 +37,7 @@ export class StateManager { private globalStateCache: GlobalStateAndSettings = {} as GlobalStateAndSettings private taskStateCache: Partial = {} + private remoteConfigCache: Partial = {} as GlobalStateAndSettings private secretsCache: Secrets = {} as Secrets private workspaceStateCache: LocalState = {} as LocalState private context: ExtensionContext @@ -309,6 +310,31 @@ export class StateManager { this.scheduleDebouncedPersistence() } + /** + * Set method for remote config field - updates cache immediately (no persistence) + * Remote config is read-only from the extension's perspective and only stored in memory + */ + setRemoteConfigField(key: K, value: GlobalStateAndSettings[K]): void { + if (!this.isInitialized) { + throw new Error(STATE_MANAGER_NOT_INITIALIZED) + } + + // Update cache immediately for instant access (no persistence needed) + this.remoteConfigCache[key] = value + } + + /** + * Clear remote config cache + * Used when switching organizations or when remote config is no longer applicable + */ + clearRemoteConfig(): void { + if (!this.isInitialized) { + throw new Error(STATE_MANAGER_NOT_INITIALIZED) + } + + this.remoteConfigCache = {} as GlobalStateAndSettings + } + /** * Initialize chokidar watcher for the taskHistory.json file * Updates in-memory cache on external changes without writing back to disk. @@ -679,11 +705,15 @@ export class StateManager { /** * Get method for global settings keys - reads from in-memory cache + * Precedence: remote config > task settings > global settings */ getGlobalSettingsKey(key: K): Settings[K] { if (!this.isInitialized) { throw new Error(STATE_MANAGER_NOT_INITIALIZED) } + if (this.remoteConfigCache[key] !== undefined) { + return this.remoteConfigCache[key] + } if (this.taskStateCache[key] !== undefined) { return this.taskStateCache[key] } @@ -760,6 +790,7 @@ export class StateManager { this.secretsCache = {} as Secrets this.workspaceStateCache = {} as LocalState this.taskStateCache = {} + this.remoteConfigCache = {} as GlobalStateAndSettings this.isInitialized = false } @@ -938,22 +969,40 @@ export class StateManager { vercelAiGatewayApiKey: this.secretsCache["vercelAiGatewayApiKey"], zaiApiKey: this.secretsCache["zaiApiKey"], - // Global state - awsRegion: this.taskStateCache["awsRegion"] || this.globalStateCache["awsRegion"], + // Global state (with remote config precedence for applicable fields) + awsRegion: + this.remoteConfigCache["awsRegion"] || this.taskStateCache["awsRegion"] || this.globalStateCache["awsRegion"], awsUseCrossRegionInference: - this.taskStateCache["awsUseCrossRegionInference"] || this.globalStateCache["awsUseCrossRegionInference"], - awsUseGlobalInference: this.taskStateCache["awsUseGlobalInference"] || this.globalStateCache["awsUseGlobalInference"], + this.remoteConfigCache["awsUseCrossRegionInference"] || + this.taskStateCache["awsUseCrossRegionInference"] || + this.globalStateCache["awsUseCrossRegionInference"], + awsUseGlobalInference: + this.remoteConfigCache["awsUseGlobalInference"] || + this.taskStateCache["awsUseGlobalInference"] || + this.globalStateCache["awsUseGlobalInference"], awsBedrockUsePromptCache: - this.taskStateCache["awsBedrockUsePromptCache"] || this.globalStateCache["awsBedrockUsePromptCache"], - awsBedrockEndpoint: this.taskStateCache["awsBedrockEndpoint"] || this.globalStateCache["awsBedrockEndpoint"], + this.remoteConfigCache["awsBedrockUsePromptCache"] || + this.taskStateCache["awsBedrockUsePromptCache"] || + this.globalStateCache["awsBedrockUsePromptCache"], + awsBedrockEndpoint: + this.remoteConfigCache["awsBedrockEndpoint"] || + this.taskStateCache["awsBedrockEndpoint"] || + this.globalStateCache["awsBedrockEndpoint"], awsProfile: this.taskStateCache["awsProfile"] || this.globalStateCache["awsProfile"], awsUseProfile: this.taskStateCache["awsUseProfile"] || this.globalStateCache["awsUseProfile"], awsAuthentication: this.taskStateCache["awsAuthentication"] || this.globalStateCache["awsAuthentication"], vertexProjectId: this.taskStateCache["vertexProjectId"] || this.globalStateCache["vertexProjectId"], vertexRegion: this.taskStateCache["vertexRegion"] || this.globalStateCache["vertexRegion"], requestyBaseUrl: this.taskStateCache["requestyBaseUrl"] || this.globalStateCache["requestyBaseUrl"], - openAiBaseUrl: this.taskStateCache["openAiBaseUrl"] || this.globalStateCache["openAiBaseUrl"], - openAiHeaders: this.taskStateCache["openAiHeaders"] || this.globalStateCache["openAiHeaders"] || {}, + openAiBaseUrl: + this.remoteConfigCache["openAiBaseUrl"] || + this.taskStateCache["openAiBaseUrl"] || + this.globalStateCache["openAiBaseUrl"], + openAiHeaders: + this.remoteConfigCache["openAiHeaders"] || + this.taskStateCache["openAiHeaders"] || + this.globalStateCache["openAiHeaders"] || + {}, ollamaBaseUrl: this.taskStateCache["ollamaBaseUrl"] || this.globalStateCache["ollamaBaseUrl"], ollamaApiOptionsCtxNum: this.taskStateCache["ollamaApiOptionsCtxNum"] || this.globalStateCache["ollamaApiOptionsCtxNum"], @@ -961,7 +1010,10 @@ export class StateManager { lmStudioMaxTokens: this.taskStateCache["lmStudioMaxTokens"] || this.globalStateCache["lmStudioMaxTokens"], anthropicBaseUrl: this.taskStateCache["anthropicBaseUrl"] || this.globalStateCache["anthropicBaseUrl"], geminiBaseUrl: this.taskStateCache["geminiBaseUrl"] || this.globalStateCache["geminiBaseUrl"], - azureApiVersion: this.taskStateCache["azureApiVersion"] || this.globalStateCache["azureApiVersion"], + azureApiVersion: + this.remoteConfigCache["azureApiVersion"] || + this.taskStateCache["azureApiVersion"] || + this.globalStateCache["azureApiVersion"], openRouterProviderSorting: this.taskStateCache["openRouterProviderSorting"] || this.globalStateCache["openRouterProviderSorting"], liteLlmBaseUrl: this.taskStateCache["liteLlmBaseUrl"] || this.globalStateCache["liteLlmBaseUrl"], @@ -988,7 +1040,10 @@ export class StateManager { ocaMode: this.globalStateCache["ocaMode"], // Plan mode configurations - planModeApiProvider: this.taskStateCache["planModeApiProvider"] || this.globalStateCache["planModeApiProvider"], + planModeApiProvider: + this.remoteConfigCache["planModeApiProvider"] || + this.taskStateCache["planModeApiProvider"] || + this.globalStateCache["planModeApiProvider"], planModeApiModelId: this.taskStateCache["planModeApiModelId"] || this.globalStateCache["planModeApiModelId"], planModeThinkingBudgetTokens: this.taskStateCache["planModeThinkingBudgetTokens"] || this.globalStateCache["planModeThinkingBudgetTokens"], @@ -1052,7 +1107,10 @@ export class StateManager { planModeOcaModelInfo: this.globalStateCache["planModeOcaModelInfo"], // Act mode configurations - actModeApiProvider: this.taskStateCache["actModeApiProvider"] || this.globalStateCache["actModeApiProvider"], + actModeApiProvider: + this.remoteConfigCache["actModeApiProvider"] || + this.taskStateCache["actModeApiProvider"] || + this.globalStateCache["actModeApiProvider"], actModeApiModelId: this.taskStateCache["actModeApiModelId"] || this.globalStateCache["actModeApiModelId"], actModeThinkingBudgetTokens: this.taskStateCache["actModeThinkingBudgetTokens"] || this.globalStateCache["actModeThinkingBudgetTokens"], diff --git a/src/core/storage/disk.ts b/src/core/storage/disk.ts index 6d834413..b1db37f6 100644 --- a/src/core/storage/disk.ts +++ b/src/core/storage/disk.ts @@ -3,6 +3,7 @@ import { TaskMetadata } from "@core/context/context-tracking/ContextTrackerTypes import { execa } from "@packages/execa" import { ClineMessage } from "@shared/ExtensionMessage" import { HistoryItem } from "@shared/HistoryItem" +import { RemoteConfig } from "@shared/remote-config/schema" import { fileExistsAtPath, isDirectory } from "@utils/fs" import fs from "fs/promises" import os from "os" @@ -290,7 +291,7 @@ export async function writeTaskSettingsToStorage(taskId: string, settings: Parti } } -export async function readRemoteConfigFromCache(organizationId: string): Promise | undefined> { +export async function readRemoteConfigFromCache(organizationId: string): Promise { try { const remoteConfigFilePath = path.join(await ensureCacheDirectoryExists(), GlobalFileNames.remoteConfig(organizationId)) const fileExists = await fileExistsAtPath(remoteConfigFilePath) @@ -305,7 +306,7 @@ export async function readRemoteConfigFromCache(organizationId: string): Promise } } -export async function writeRemoteConfigToCache(organizationId: string, config: Record): Promise { +export async function writeRemoteConfigToCache(organizationId: string, config: RemoteConfig): Promise { try { const remoteConfigFilePath = path.join(await ensureCacheDirectoryExists(), GlobalFileNames.remoteConfig(organizationId)) await fs.writeFile(remoteConfigFilePath, JSON.stringify(config)) diff --git a/src/shared/remote-config/fetch.ts b/src/core/storage/remote-config/fetch.ts similarity index 83% rename from src/shared/remote-config/fetch.ts rename to src/core/storage/remote-config/fetch.ts index e56ae279..b3524f2f 100644 --- a/src/shared/remote-config/fetch.ts +++ b/src/core/storage/remote-config/fetch.ts @@ -1,9 +1,10 @@ import axios, { AxiosRequestConfig, AxiosResponse } from "axios" -import { clineEnvConfig } from "@/config" -import { readRemoteConfigFromCache, writeRemoteConfigToCache } from "@/core/storage/disk" -import { AuthService } from "@/services/auth/AuthService" -import { CLINE_API_ENDPOINT } from "@/shared/cline/api" -import { RemoteConfig, RemoteConfigSchema } from "./schema" +import { clineEnvConfig } from "../../../config" +import { AuthService } from "../../../services/auth/AuthService" +import { CLINE_API_ENDPOINT } from "../../../shared/cline/api" +import { RemoteConfig, RemoteConfigSchema } from "../../../shared/remote-config/schema" +import { readRemoteConfigFromCache, writeRemoteConfigToCache } from "../disk" +import { applyRemoteConfig } from "./utils" /** * Fetches remote configuration for the active organization from the API. @@ -85,6 +86,9 @@ export async function fetchRemoteConfig(): Promise { // Write to cache await writeRemoteConfigToCache(organizationId, validatedConfig) + // Apply config to StateManager + applyRemoteConfig(validatedConfig) + return validatedConfig } catch (error) { console.error("Failed to fetch remote config from API:", error) @@ -93,7 +97,10 @@ export async function fetchRemoteConfig(): Promise { const cachedConfig = await readRemoteConfigFromCache(organizationId) if (cachedConfig) { // Validate cached config against schema - return RemoteConfigSchema.parse(cachedConfig) + const validatedCachedConfig = RemoteConfigSchema.parse(cachedConfig) + // Apply config to StateManager + applyRemoteConfig(validatedCachedConfig) + return validatedCachedConfig } // Both API and cache failed diff --git a/src/core/storage/remote-config/utils.ts b/src/core/storage/remote-config/utils.ts new file mode 100644 index 00000000..fe9a671e --- /dev/null +++ b/src/core/storage/remote-config/utils.ts @@ -0,0 +1,93 @@ +import { RemoteConfig } from "../../../shared/remote-config/schema" +import { StateManager } from "../StateManager" +import { GlobalStateAndSettings } from "../state-keys" + +/** + * Transforms RemoteConfig schema to GlobalStateAndSettings shape + * @param remoteConfig The remote configuration object + * @returns Partial containing only the fields present in remote config + */ +export function transformRemoteConfigToStateShape(remoteConfig: RemoteConfig): Partial { + const transformed: Partial = {} + + // Map top-level settings + if (remoteConfig.telemetryEnabled !== undefined) { + transformed.telemetrySetting = remoteConfig.telemetryEnabled ? "enabled" : "disabled" + } + if (remoteConfig.mcpMarketplaceEnabled !== undefined) { + transformed.mcpMarketplaceEnabled = remoteConfig.mcpMarketplaceEnabled + } + if (remoteConfig.yoloModeAllowed !== undefined) { + // only set the yoloModeToggled field if yolo mode is not allowed. Otherwise, we let the user toggle it. + if (remoteConfig.yoloModeAllowed === false) { + transformed.yoloModeToggled = false + } + } + + // Map OpenAiCompatible provider settings + const openAiSettings = remoteConfig.providerSettings?.OpenAiCompatible + if (openAiSettings) { + transformed.planModeApiProvider = "openai" + transformed.actModeApiProvider = "openai" + + if (openAiSettings.openAiBaseUrl !== undefined) { + transformed.openAiBaseUrl = openAiSettings.openAiBaseUrl + } + if (openAiSettings.openAiHeaders !== undefined) { + transformed.openAiHeaders = openAiSettings.openAiHeaders + } + if (openAiSettings.azureApiVersion !== undefined) { + transformed.azureApiVersion = openAiSettings.azureApiVersion + } + } + + // Map AwsBedrock provider settings + const awsBedrockSettings = remoteConfig.providerSettings?.AwsBedrock + if (awsBedrockSettings) { + transformed.planModeApiProvider = "bedrock" + transformed.actModeApiProvider = "bedrock" + + if (awsBedrockSettings.awsRegion !== undefined) { + transformed.awsRegion = awsBedrockSettings.awsRegion + } + if (awsBedrockSettings.awsUseCrossRegionInference !== undefined) { + transformed.awsUseCrossRegionInference = awsBedrockSettings.awsUseCrossRegionInference + } + if (awsBedrockSettings.awsUseGlobalInference !== undefined) { + transformed.awsUseGlobalInference = awsBedrockSettings.awsUseGlobalInference + } + if (awsBedrockSettings.awsBedrockUsePromptCache !== undefined) { + transformed.awsBedrockUsePromptCache = awsBedrockSettings.awsBedrockUsePromptCache + } + if (awsBedrockSettings.awsBedrockEndpoint !== undefined) { + transformed.awsBedrockEndpoint = awsBedrockSettings.awsBedrockEndpoint + } + } + + return transformed +} + +/** + * Applies remote config to the StateManager's remote config cache + * @param remoteConfig The remote configuration object to apply + */ +export function applyRemoteConfig(remoteConfig?: RemoteConfig): void { + const stateManager = StateManager.get() + + // If no remote config provided, clear the cache + if (!remoteConfig) { + stateManager.clearRemoteConfig() + return + } + + // Transform remote config to state shape + const transformed = transformRemoteConfigToStateShape(remoteConfig) + + // Clear existing remote config cache + stateManager.clearRemoteConfig() + + // Populate remote config cache with transformed values + for (const [key, value] of Object.entries(transformed)) { + stateManager.setRemoteConfigField(key as keyof GlobalStateAndSettings, value) + } +} diff --git a/src/shared/remote-config/index.ts b/src/shared/remote-config/index.ts index 0597d352..06861aae 100644 --- a/src/shared/remote-config/index.ts +++ b/src/shared/remote-config/index.ts @@ -1,2 +1 @@ -export * from "./fetch" export * from "./schema"