| | |
| | |
| | |
| | |
| | |
| |
|
| | import * as fs from 'fs'; |
| | import * as path from 'path'; |
| | import { homedir, platform } from 'os'; |
| | import * as dotenv from 'dotenv'; |
| | import { |
| | GEMINI_CONFIG_DIR as GEMINI_DIR, |
| | getErrorMessage, |
| | } from '@qwen-code/qwen-code-core'; |
| | import stripJsonComments from 'strip-json-comments'; |
| | import { DefaultLight } from '../ui/themes/default-light.js'; |
| | import { DefaultDark } from '../ui/themes/default.js'; |
| | import { Settings, MemoryImportFormat } from './settingsSchema.js'; |
| |
|
| | export type { Settings, MemoryImportFormat }; |
| |
|
| | export const SETTINGS_DIRECTORY_NAME = '.qwen'; |
| | export const USER_SETTINGS_DIR = path.join(homedir(), SETTINGS_DIRECTORY_NAME); |
| | export const USER_SETTINGS_PATH = path.join(USER_SETTINGS_DIR, 'settings.json'); |
| | export const DEFAULT_EXCLUDED_ENV_VARS = ['DEBUG', 'DEBUG_MODE']; |
| |
|
| | export function getSystemSettingsPath(): string { |
| | if (process.env['GEMINI_CLI_SYSTEM_SETTINGS_PATH']) { |
| | return process.env['GEMINI_CLI_SYSTEM_SETTINGS_PATH']; |
| | } |
| | if (platform() === 'darwin') { |
| | return '/Library/Application Support/QwenCode/settings.json'; |
| | } else if (platform() === 'win32') { |
| | return 'C:\\ProgramData\\qwen-code\\settings.json'; |
| | } else { |
| | return '/etc/qwen-code/settings.json'; |
| | } |
| | } |
| |
|
| | export function getWorkspaceSettingsPath(workspaceDir: string): string { |
| | return path.join(workspaceDir, SETTINGS_DIRECTORY_NAME, 'settings.json'); |
| | } |
| |
|
| | export type { DnsResolutionOrder } from './settingsSchema.js'; |
| |
|
| | export enum SettingScope { |
| | User = 'User', |
| | Workspace = 'Workspace', |
| | System = 'System', |
| | } |
| |
|
| | export interface CheckpointingSettings { |
| | enabled?: boolean; |
| | } |
| |
|
| | export interface SummarizeToolOutputSettings { |
| | tokenBudget?: number; |
| | } |
| |
|
| | export interface AccessibilitySettings { |
| | disableLoadingPhrases?: boolean; |
| | } |
| |
|
| | export interface SettingsError { |
| | message: string; |
| | path: string; |
| | } |
| |
|
| | export interface SettingsFile { |
| | settings: Settings; |
| | path: string; |
| | } |
| |
|
| | function mergeSettings( |
| | system: Settings, |
| | user: Settings, |
| | workspace: Settings, |
| | ): Settings { |
| | |
| | |
| | const { folderTrust, ...workspaceWithoutFolderTrust } = workspace; |
| |
|
| | return { |
| | ...user, |
| | ...workspaceWithoutFolderTrust, |
| | ...system, |
| | customThemes: { |
| | ...(user.customThemes || {}), |
| | ...(workspace.customThemes || {}), |
| | ...(system.customThemes || {}), |
| | }, |
| | mcpServers: { |
| | ...(user.mcpServers || {}), |
| | ...(workspace.mcpServers || {}), |
| | ...(system.mcpServers || {}), |
| | }, |
| | includeDirectories: [ |
| | ...(system.includeDirectories || []), |
| | ...(user.includeDirectories || []), |
| | ...(workspace.includeDirectories || []), |
| | ], |
| | chatCompression: { |
| | ...(system.chatCompression || {}), |
| | ...(user.chatCompression || {}), |
| | ...(workspace.chatCompression || {}), |
| | }, |
| | }; |
| | } |
| |
|
| | export class LoadedSettings { |
| | constructor( |
| | system: SettingsFile, |
| | user: SettingsFile, |
| | workspace: SettingsFile, |
| | errors: SettingsError[], |
| | ) { |
| | this.system = system; |
| | this.user = user; |
| | this.workspace = workspace; |
| | this.errors = errors; |
| | this._merged = this.computeMergedSettings(); |
| | } |
| |
|
| | readonly system: SettingsFile; |
| | readonly user: SettingsFile; |
| | readonly workspace: SettingsFile; |
| | readonly errors: SettingsError[]; |
| |
|
| | private _merged: Settings; |
| |
|
| | get merged(): Settings { |
| | return this._merged; |
| | } |
| |
|
| | private computeMergedSettings(): Settings { |
| | return mergeSettings( |
| | this.system.settings, |
| | this.user.settings, |
| | this.workspace.settings, |
| | ); |
| | } |
| |
|
| | forScope(scope: SettingScope): SettingsFile { |
| | switch (scope) { |
| | case SettingScope.User: |
| | return this.user; |
| | case SettingScope.Workspace: |
| | return this.workspace; |
| | case SettingScope.System: |
| | return this.system; |
| | default: |
| | throw new Error(`Invalid scope: ${scope}`); |
| | } |
| | } |
| |
|
| | setValue<K extends keyof Settings>( |
| | scope: SettingScope, |
| | key: K, |
| | value: Settings[K], |
| | ): void { |
| | const settingsFile = this.forScope(scope); |
| | settingsFile.settings[key] = value; |
| | this._merged = this.computeMergedSettings(); |
| | saveSettings(settingsFile); |
| | } |
| | } |
| |
|
| | function resolveEnvVarsInString(value: string): string { |
| | const envVarRegex = /\$(?:(\w+)|{([^}]+)})/g; |
| | return value.replace(envVarRegex, (match, varName1, varName2) => { |
| | const varName = varName1 || varName2; |
| | if (process && process.env && typeof process.env[varName] === 'string') { |
| | return process.env[varName]!; |
| | } |
| | return match; |
| | }); |
| | } |
| |
|
| | function resolveEnvVarsInObject<T>(obj: T): T { |
| | if ( |
| | obj === null || |
| | obj === undefined || |
| | typeof obj === 'boolean' || |
| | typeof obj === 'number' |
| | ) { |
| | return obj; |
| | } |
| |
|
| | if (typeof obj === 'string') { |
| | return resolveEnvVarsInString(obj) as unknown as T; |
| | } |
| |
|
| | if (Array.isArray(obj)) { |
| | return obj.map((item) => resolveEnvVarsInObject(item)) as unknown as T; |
| | } |
| |
|
| | if (typeof obj === 'object') { |
| | const newObj = { ...obj } as T; |
| | for (const key in newObj) { |
| | if (Object.prototype.hasOwnProperty.call(newObj, key)) { |
| | newObj[key] = resolveEnvVarsInObject(newObj[key]); |
| | } |
| | } |
| | return newObj; |
| | } |
| |
|
| | return obj; |
| | } |
| |
|
| | function findEnvFile(startDir: string): string | null { |
| | let currentDir = path.resolve(startDir); |
| | while (true) { |
| | |
| | const geminiEnvPath = path.join(currentDir, GEMINI_DIR, '.env'); |
| | if (fs.existsSync(geminiEnvPath)) { |
| | return geminiEnvPath; |
| | } |
| | const envPath = path.join(currentDir, '.env'); |
| | if (fs.existsSync(envPath)) { |
| | return envPath; |
| | } |
| | const parentDir = path.dirname(currentDir); |
| | if (parentDir === currentDir || !parentDir) { |
| | |
| | const homeGeminiEnvPath = path.join(homedir(), GEMINI_DIR, '.env'); |
| | if (fs.existsSync(homeGeminiEnvPath)) { |
| | return homeGeminiEnvPath; |
| | } |
| | const homeEnvPath = path.join(homedir(), '.env'); |
| | if (fs.existsSync(homeEnvPath)) { |
| | return homeEnvPath; |
| | } |
| | return null; |
| | } |
| | currentDir = parentDir; |
| | } |
| | } |
| |
|
| | export function setUpCloudShellEnvironment(envFilePath: string | null): void { |
| | |
| | |
| | |
| | |
| | |
| | if (envFilePath && fs.existsSync(envFilePath)) { |
| | const envFileContent = fs.readFileSync(envFilePath); |
| | const parsedEnv = dotenv.parse(envFileContent); |
| | if (parsedEnv['GOOGLE_CLOUD_PROJECT']) { |
| | |
| | process.env['GOOGLE_CLOUD_PROJECT'] = parsedEnv['GOOGLE_CLOUD_PROJECT']; |
| | } else { |
| | |
| | process.env['GOOGLE_CLOUD_PROJECT'] = 'cloudshell-gca'; |
| | } |
| | } else { |
| | |
| | process.env['GOOGLE_CLOUD_PROJECT'] = 'cloudshell-gca'; |
| | } |
| | } |
| |
|
| | export function loadEnvironment(settings?: Settings): void { |
| | const envFilePath = findEnvFile(process.cwd()); |
| |
|
| | |
| | if (process.env['CLOUD_SHELL'] === 'true') { |
| | setUpCloudShellEnvironment(envFilePath); |
| | } |
| |
|
| | |
| | let resolvedSettings = settings; |
| | if (!resolvedSettings) { |
| | const workspaceSettingsPath = getWorkspaceSettingsPath(process.cwd()); |
| | try { |
| | if (fs.existsSync(workspaceSettingsPath)) { |
| | const workspaceContent = fs.readFileSync( |
| | workspaceSettingsPath, |
| | 'utf-8', |
| | ); |
| | const parsedWorkspaceSettings = JSON.parse( |
| | stripJsonComments(workspaceContent), |
| | ) as Settings; |
| | resolvedSettings = resolveEnvVarsInObject(parsedWorkspaceSettings); |
| | } |
| | } catch (_e) { |
| | |
| | } |
| | } |
| |
|
| | if (envFilePath) { |
| | |
| | |
| | try { |
| | const envFileContent = fs.readFileSync(envFilePath, 'utf-8'); |
| | const parsedEnv = dotenv.parse(envFileContent); |
| |
|
| | const excludedVars = |
| | resolvedSettings?.excludedProjectEnvVars || DEFAULT_EXCLUDED_ENV_VARS; |
| | const isProjectEnvFile = !envFilePath.includes(GEMINI_DIR); |
| |
|
| | for (const key in parsedEnv) { |
| | if (Object.hasOwn(parsedEnv, key)) { |
| | |
| | if (isProjectEnvFile && excludedVars.includes(key)) { |
| | continue; |
| | } |
| |
|
| | |
| | if (!Object.hasOwn(process.env, key)) { |
| | process.env[key] = parsedEnv[key]; |
| | } |
| | } |
| | } |
| | } catch (_e) { |
| | |
| | } |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | export function loadSettings(workspaceDir: string): LoadedSettings { |
| | let systemSettings: Settings = {}; |
| | let userSettings: Settings = {}; |
| | let workspaceSettings: Settings = {}; |
| | const settingsErrors: SettingsError[] = []; |
| | const systemSettingsPath = getSystemSettingsPath(); |
| |
|
| | |
| | const resolvedWorkspaceDir = path.resolve(workspaceDir); |
| | const resolvedHomeDir = path.resolve(homedir()); |
| |
|
| | let realWorkspaceDir = resolvedWorkspaceDir; |
| | try { |
| | |
| | realWorkspaceDir = fs.realpathSync(resolvedWorkspaceDir); |
| | } catch (_e) { |
| | |
| | } |
| |
|
| | |
| | const realHomeDir = fs.realpathSync(resolvedHomeDir); |
| |
|
| | const workspaceSettingsPath = getWorkspaceSettingsPath(workspaceDir); |
| |
|
| | |
| | try { |
| | if (fs.existsSync(systemSettingsPath)) { |
| | const systemContent = fs.readFileSync(systemSettingsPath, 'utf-8'); |
| | systemSettings = JSON.parse(stripJsonComments(systemContent)) as Settings; |
| | } |
| | } catch (error: unknown) { |
| | settingsErrors.push({ |
| | message: getErrorMessage(error), |
| | path: systemSettingsPath, |
| | }); |
| | } |
| |
|
| | |
| | try { |
| | if (fs.existsSync(USER_SETTINGS_PATH)) { |
| | const userContent = fs.readFileSync(USER_SETTINGS_PATH, 'utf-8'); |
| | userSettings = JSON.parse(stripJsonComments(userContent)) as Settings; |
| | |
| | if (userSettings.theme && userSettings.theme === 'VS') { |
| | userSettings.theme = DefaultLight.name; |
| | } else if (userSettings.theme && userSettings.theme === 'VS2015') { |
| | userSettings.theme = DefaultDark.name; |
| | } |
| | } |
| | } catch (error: unknown) { |
| | settingsErrors.push({ |
| | message: getErrorMessage(error), |
| | path: USER_SETTINGS_PATH, |
| | }); |
| | } |
| |
|
| | if (realWorkspaceDir !== realHomeDir) { |
| | |
| | try { |
| | if (fs.existsSync(workspaceSettingsPath)) { |
| | const projectContent = fs.readFileSync(workspaceSettingsPath, 'utf-8'); |
| | workspaceSettings = JSON.parse( |
| | stripJsonComments(projectContent), |
| | ) as Settings; |
| | if (workspaceSettings.theme && workspaceSettings.theme === 'VS') { |
| | workspaceSettings.theme = DefaultLight.name; |
| | } else if ( |
| | workspaceSettings.theme && |
| | workspaceSettings.theme === 'VS2015' |
| | ) { |
| | workspaceSettings.theme = DefaultDark.name; |
| | } |
| | } |
| | } catch (error: unknown) { |
| | settingsErrors.push({ |
| | message: getErrorMessage(error), |
| | path: workspaceSettingsPath, |
| | }); |
| | } |
| | } |
| |
|
| | |
| | const tempMergedSettings = mergeSettings( |
| | systemSettings, |
| | userSettings, |
| | workspaceSettings, |
| | ); |
| |
|
| | |
| | |
| | loadEnvironment(tempMergedSettings); |
| |
|
| | |
| | systemSettings = resolveEnvVarsInObject(systemSettings); |
| | userSettings = resolveEnvVarsInObject(userSettings); |
| | workspaceSettings = resolveEnvVarsInObject(workspaceSettings); |
| |
|
| | |
| | const loadedSettings = new LoadedSettings( |
| | { |
| | path: systemSettingsPath, |
| | settings: systemSettings, |
| | }, |
| | { |
| | path: USER_SETTINGS_PATH, |
| | settings: userSettings, |
| | }, |
| | { |
| | path: workspaceSettingsPath, |
| | settings: workspaceSettings, |
| | }, |
| | settingsErrors, |
| | ); |
| |
|
| | |
| | const chatCompression = loadedSettings.merged.chatCompression; |
| | const threshold = chatCompression?.contextPercentageThreshold; |
| | if ( |
| | threshold != null && |
| | (typeof threshold !== 'number' || threshold < 0 || threshold > 1) |
| | ) { |
| | console.warn( |
| | `Invalid value for chatCompression.contextPercentageThreshold: "${threshold}". Please use a value between 0 and 1. Using default compression settings.`, |
| | ); |
| | delete loadedSettings.merged.chatCompression; |
| | } |
| |
|
| | return loadedSettings; |
| | } |
| |
|
| | export function saveSettings(settingsFile: SettingsFile): void { |
| | try { |
| | |
| | const dirPath = path.dirname(settingsFile.path); |
| | if (!fs.existsSync(dirPath)) { |
| | fs.mkdirSync(dirPath, { recursive: true }); |
| | } |
| |
|
| | fs.writeFileSync( |
| | settingsFile.path, |
| | JSON.stringify(settingsFile.settings, null, 2), |
| | 'utf-8', |
| | ); |
| | } catch (error) { |
| | console.error('Error saving user settings file:', error); |
| | } |
| | } |
| |
|