refactor: Standardize configuration and environment variable access

This commit centralizes configuration and environment variable access across various modules by consistently utilizing getters from scripts/modules/config-manager.js. This replaces direct access to process.env and the global CONFIG object, leading to improved consistency, maintainability, testability, and better handling of session-specific configurations within the MCP context.

Key changes include:

- Centralized Getters: Replaced numerous instances of process.env.* and CONFIG.* with corresponding getter functions (e.g., getLogLevel, getMainModelId, getResearchMaxTokens, getMainTemperature, isApiKeySet, getDebugFlag, getDefaultSubtasks).

- Session Awareness: Ensured that the session object is passed to config getters where necessary, particularly within AI service calls (ai-services.js, add-task.js) and error handling (ai-services.js), allowing for session-specific environment overrides.

- API Key Checks: Standardized API key availability checks using isApiKeySet() instead of directly checking process.env.* (e.g., for Perplexity in commands.js and ai-services.js).

- Client Instantiation Cleanup: Removed now-redundant/obsolete local client instantiation functions (getAnthropicClient, getPerplexityClient) from ai-services.js and the global Anthropic client initialization from dependency-manager.js. Client creation should now rely on the config manager and factory patterns.

- Consistent Debug Flag Usage: Standardized calls to getDebugFlag() in commands.js, removing potentially unnecessary null arguments.

- Accurate Progress Calculation: Updated AI stream progress reporting (ai-services.js, add-task.js) to use getMainMaxTokens(session) for more accurate calculations.

- Minor Cleanup: Removed unused  import from scripts/modules/commands.js.

Specific module updates:

- :

  - Uses getLogLevel() instead of process.env.LOG_LEVEL.

- :

  - Replaced direct env/config access for model IDs, tokens, temperature, API keys, and default subtasks with appropriate getters.

  - Passed session to handleClaudeError.

  - Removed local getPerplexityClient and getAnthropicClient functions.

  - Updated progress calculations to use getMainMaxTokens(session).

- :

  - Uses isApiKeySet('perplexity') for API key checks.

  - Uses getDebugFlag() consistently for debug checks.

  - Removed unused  import.

- :

  - Removed global Anthropic client initialization.

- :

  - Uses config getters (getResearch..., getMain...) for Perplexity and Claude API call parameters, preserving customEnv override logic.

This refactoring also resolves a potential SyntaxError: Identifier 'getPerplexityClient' has already been declared by removing the duplicated/obsolete function definition previously present in ai-services.js.
This commit is contained in:
Eyal Toledano
2025-04-21 21:30:12 -04:00
parent d46547a80f
commit 4a9f6cd5f5
13 changed files with 284 additions and 238 deletions

View File

@@ -13,7 +13,7 @@ import inquirer from 'inquirer';
import ora from 'ora';
import Table from 'cli-table3';
import { log, readJSON, writeJSON } from './utils.js';
import { log, readJSON } from './utils.js';
import {
parsePRD,
updateTasks,
@@ -347,7 +347,7 @@ function registerCommands(programInstance) {
if (useResearch) {
// Verify Perplexity API key exists if using research
if (!process.env.PERPLEXITY_API_KEY) {
if (!isApiKeySet('perplexity')) {
console.log(
chalk.yellow(
'Warning: PERPLEXITY_API_KEY environment variable is missing. Research-backed updates will not be available.'
@@ -400,7 +400,7 @@ function registerCommands(programInstance) {
}
// Use getDebugFlag getter instead of CONFIG.debug
if (getDebugFlag(null)) {
if (getDebugFlag()) {
console.error(error);
}
@@ -500,7 +500,7 @@ function registerCommands(programInstance) {
if (useResearch) {
// Verify Perplexity API key exists if using research
if (!process.env.PERPLEXITY_API_KEY) {
if (!isApiKeySet('perplexity')) {
console.log(
chalk.yellow(
'Warning: PERPLEXITY_API_KEY environment variable is missing. Research-backed updates will not be available.'
@@ -556,7 +556,7 @@ function registerCommands(programInstance) {
}
// Use getDebugFlag getter instead of CONFIG.debug
if (getDebugFlag(null)) {
if (getDebugFlag()) {
console.error(error);
}
@@ -923,7 +923,7 @@ function registerCommands(programInstance) {
console.log(chalk.gray('Next: Complete this task or add more tasks'));
} catch (error) {
console.error(chalk.red(`Error adding task: ${error.message}`));
if (error.stack && getDebugFlag(null)) {
if (error.stack && getDebugFlag()) {
console.error(error.stack);
}
process.exit(1);
@@ -2105,7 +2105,7 @@ function registerCommands(programInstance) {
}
} catch (error) {
log(`Error processing models command: ${error.message}`, 'error');
if (error.stack && getDebugFlag(null)) {
if (error.stack && getDebugFlag()) {
log(error.stack, 'debug');
}
process.exit(1);
@@ -2337,7 +2337,7 @@ async function runCLI(argv = process.argv) {
} catch (error) {
console.error(chalk.red(`Error: ${error.message}`));
if (getDebugFlag(null)) {
if (getDebugFlag()) {
console.error(error);
}