remote config state setting (#6776)

* set remote config state and make it override task and global state

* remove console logs

* remove console log
This commit is contained in:
celestial-vault authored and GitHub committed 2025-10-11 20:29:40 -07:00
1 parent 5c7bb7c5db
commit 42bad19889
5 files changed
+178 -20

No files matched your search

+69 -11
View File
@@ -37,6 +37,7 @@ export class StateManager {
private globalStateCache: GlobalStateAndSettings = {} as GlobalStateAndSettings
private taskStateCache: Partial<Settings> = {}
private remoteConfigCache: Partial<GlobalStateAndSettings> = {} 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<K extends keyof GlobalStateAndSettings>(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<K extends keyof Settings>(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"],
+3 -2
View File
@@ -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<Record<string, any> | undefined> {
export async function readRemoteConfigFromCache(organizationId: string): Promise<RemoteConfig | undefined> {
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<string, any>): Promise<void> {
export async function writeRemoteConfigToCache(organizationId: string, config: RemoteConfig): Promise<void> {
try {
const remoteConfigFilePath = path.join(await ensureCacheDirectoryExists(), GlobalFileNames.remoteConfig(organizationId))
await fs.writeFile(remoteConfigFilePath, JSON.stringify(config))
@@ -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<RemoteConfig | undefined> {
// 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<RemoteConfig | undefined> {
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
+93
View File
@@ -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<GlobalStateAndSettings> containing only the fields present in remote config
*/
export function transformRemoteConfigToStateShape(remoteConfig: RemoteConfig): Partial<GlobalStateAndSettings> {
const transformed: Partial<GlobalStateAndSettings> = {}
// 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)
}
}
-1
View File
@@ -1,2 +1 @@
export * from "./fetch"
export * from "./schema"