From 181abde2ffa8f1f68b6c540a5600346116cb5145 Mon Sep 17 00:00:00 2001 From: Marat Boshernitsan Date: Thu, 12 Jun 2025 17:17:29 -0700 Subject: Reduce coupling between core and cli packages (#961) Co-authored-by: Marat Boshernitsan --- packages/core/src/config/config.ts | 56 +++++++++++--------------------------- 1 file changed, 16 insertions(+), 40 deletions(-) (limited to 'packages/core/src/config/config.ts') diff --git a/packages/core/src/config/config.ts b/packages/core/src/config/config.ts index b94a88a4..3bb9815f 100644 --- a/packages/core/src/config/config.ts +++ b/packages/core/src/config/config.ts @@ -4,11 +4,8 @@ * SPDX-License-Identifier: Apache-2.0 */ -import * as dotenv from 'dotenv'; -import * as fs from 'node:fs'; import * as path from 'node:path'; import process from 'node:process'; -import * as os from 'node:os'; import { ContentGeneratorConfig } from '../core/contentGenerator.js'; import { ToolRegistry } from '../tools/tool-registry.js'; import { LSTool } from '../tools/ls.js'; @@ -79,9 +76,12 @@ export interface ConfigParameters { accessibility?: AccessibilitySettings; telemetry?: boolean; telemetryLogUserPromptsEnabled?: boolean; + telemetryOtlpEndpoint?: string; fileFilteringRespectGitIgnore?: boolean; fileFilteringAllowBuildArtifacts?: boolean; checkpoint?: boolean; + proxy?: string; + cwd: string; } export class Config { @@ -115,6 +115,8 @@ export class Config { private fileDiscoveryService: FileDiscoveryService | null = null; private gitService: GitService | undefined = undefined; private readonly checkpoint: boolean; + private readonly proxy: string | undefined; + private readonly cwd: string; constructor(params: ConfigParameters) { this.sessionId = params.sessionId; @@ -140,12 +142,14 @@ export class Config { this.telemetryLogUserPromptsEnabled = params.telemetryLogUserPromptsEnabled ?? true; this.telemetryOtlpEndpoint = - process.env.OTEL_EXPORTER_OTLP_ENDPOINT ?? 'http://localhost:4317'; + params.telemetryOtlpEndpoint ?? 'http://localhost:4317'; this.fileFilteringRespectGitIgnore = params.fileFilteringRespectGitIgnore ?? true; this.fileFilteringAllowBuildArtifacts = params.fileFilteringAllowBuildArtifacts ?? false; this.checkpoint = params.checkpoint ?? false; + this.proxy = params.proxy; + this.cwd = params.cwd ?? process.cwd(); if (params.contextFileName) { setGeminiMdFilename(params.contextFileName); @@ -297,6 +301,14 @@ export class Config { return this.checkpoint; } + getProxy(): string | undefined { + return this.proxy; + } + + getWorkingDir(): string { + return this.cwd; + } + async getFileService(): Promise { if (!this.fileDiscoveryService) { this.fileDiscoveryService = new FileDiscoveryService(this.targetDir); @@ -317,42 +329,6 @@ export class Config { } } -function findEnvFile(startDir: string): string | null { - let currentDir = path.resolve(startDir); - while (true) { - // prefer gemini-specific .env under GEMINI_DIR - 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) { - // check .env under home as fallback, again preferring gemini-specific .env - const homeGeminiEnvPath = path.join(os.homedir(), GEMINI_DIR, '.env'); - if (fs.existsSync(homeGeminiEnvPath)) { - return homeGeminiEnvPath; - } - const homeEnvPath = path.join(os.homedir(), '.env'); - if (fs.existsSync(homeEnvPath)) { - return homeEnvPath; - } - return null; - } - currentDir = parentDir; - } -} - -export function loadEnvironment(): void { - const envFilePath = findEnvFile(process.cwd()); - if (envFilePath) { - dotenv.config({ path: envFilePath }); - } -} - export function createToolRegistry(config: Config): Promise { const registry = new ToolRegistry(config); const targetDir = config.getTargetDir(); -- cgit v1.2.3