All files / core/config squad-config.service.ts

100% Statements 39/39
96.42% Branches 54/56
100% Functions 4/4
100% Lines 36/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 1416x       6x       121x 359x 359x 2272x 2272x 3x   2269x 4x     352x     121x 118x 118x       121x   121x   121x 1x             120x         120x         120x 120x 2x               118x       118x 118x         118x     118x         1x             117x         117x   117x               117x 1x           116x                                                         134x        
import { Injectable } from '@nestjs/common';
import { ISquadConfig, SquadStateMode } from './squad-config.interface';
 
@Injectable()
export class SquadConfigService {
  private readonly config: ISquadConfig;
 
  constructor() {
    const getArgValue = (flagName: string): string | undefined => {
      const argv = process.argv || [];
      for (let i = 0; i < argv.length; i += 1) {
        const arg = argv[i];
        if (arg.startsWith(`--${flagName}=`)) {
          return arg.slice(flagName.length + 3);
        }
        if (arg === `--${flagName}`) {
          return argv[i + 1];
        }
      }
      return undefined;
    };
 
    const hasFlag = (flagName: string): boolean => {
      const argv = process.argv || [];
      return argv.includes(`--${flagName}`);
    };
 
    const stateModeArg =
      getArgValue('state-mode') as SquadStateMode | undefined;
    const stateMode =
      (stateModeArg || (process.env.STATE_MODE as SquadStateMode))
      || 'stateless';
    if (stateMode !== 'stateless' && stateMode !== 'stateful') {
      throw new Error(
        `Invalid STATE_MODE: ${stateMode}. ` +
        'Must be \'stateless\' or \'stateful\''
      );
    }
 
    // Resolve engine from CLI flag or env, default to cursor-agent
    const engineFromArg = getArgValue('engine') as
      | 'cursor-agent'
      | 'claude'
      | 'codex'
      | undefined;
    const engineEnv = process.env.ENGINE as
      | 'cursor-agent'
      | 'claude'
      | 'codex'
      | undefined;
    const engine = engineFromArg || engineEnv || 'cursor-agent';
    if (!([ 'cursor-agent', 'claude', 'codex' ] as const).includes(engine)) {
      throw new Error(
        'Invalid ENGINE: ' +
        engine +
        '. Must be \'cursor-agent\', \'claude\', or \'codex\''
      );
    }
 
    // Execution mode (used when custom template provided)
    const execModeArg = getArgValue('execution-mode') as
      | 'sequential'
      | 'parallel'
      | undefined;
    const sequentialFlag = hasFlag('sequential');
    const execModeEnv = process.env.EXECUTION_MODE as
      | 'sequential'
      | 'parallel'
      | undefined;
    const executionMode: 'sequential' | 'parallel' | undefined =
      execModeArg
      || (sequentialFlag ? 'sequential' : undefined)
      || execModeEnv;
    if (
      executionMode &&
      executionMode !== 'sequential' &&
      executionMode !== 'parallel'
    ) {
      throw new Error(
        'Invalid EXECUTION_MODE: ' +
        executionMode +
        '. Must be \'sequential\' or \'parallel\''
      );
    }
 
    const sequentialDelayRaw = parseInt(
      process.env.SEQUENTIAL_DELAY_MS || '100',
      10
    );
 
    const providedRunTemplatePath = process.env.RUN_TEMPLATE_PATH;
    const resolvedRunTemplatePath =
      providedRunTemplatePath
      || (engine === 'cursor-agent'
        ? 'templates/run-cursor-agent.template'
        : engine === 'claude'
          ? 'templates/run-claude.template'
          : 'templates/run-codex.template');
 
    // If user provided a custom template explicitly, require execution mode
    if (providedRunTemplatePath && !executionMode) {
      throw new Error(
        'EXECUTION_MODE is required when providing RUN_TEMPLATE_PATH. ' +
        'Set EXECUTION_MODE=sequential|parallel or pass --execution-mode.'
      );
    }
 
    this.config = {
      stateMode,
      engine,
      executionMode,
      runTemplatePath: resolvedRunTemplatePath,
      createChatTemplatePath:
        process.env.CREATE_CHAT_TEMPLATE_PATH
        || (stateMode === 'stateful'
          ? (engine === 'cursor-agent'
              ? 'templates/create-chat-cursor-agent.template'
              : engine === 'claude'
                ? 'templates/create-chat-claude.template'
                : 'templates/create-chat-codex.template')
          : undefined),
      agentsDirectoryPath:
        process.env.AGENTS_DIRECTORY_PATH || 'agents',
      processTimeoutMs: parseInt(
        process.env.PROCESS_TIMEOUT_MS || '600000',
        10
      ),
      sequentialDelayMs: Number.isNaN(sequentialDelayRaw)
        ? 1000
        : Math.max(0, sequentialDelayRaw)
    };
 
    // When stateful, createChatTemplatePath is set (provided or defaulted)
  }
 
  getConfig(): ISquadConfig {
    return { ...this.config };
  }
}