summaryrefslogtreecommitdiff
path: root/packages/cli/src/config/config.ts
blob: 0db07d2b3bc59f408881b49cb509405d754cbf82 (plain)
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
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import yargs from 'yargs/yargs';
import { hideBin } from 'yargs/helpers';
import process from 'node:process';
import {
  Config,
  loadEnvironment,
  createServerConfig,
} from '@gemini-code/server';

const DEFAULT_GEMINI_MODEL = 'gemini-2.5-flash-preview-04-17';

// Keep CLI-specific argument parsing
interface CliArgs {
  model: string | undefined;
  debug_mode: boolean | undefined;
  question: string | undefined;
  full_context: boolean | undefined;
}

function parseArguments(): CliArgs {
  const argv = yargs(hideBin(process.argv))
    .option('model', {
      alias: 'm',
      type: 'string',
      description: `The Gemini model to use. Defaults to ${DEFAULT_GEMINI_MODEL}.`,
      default: process.env.GEMINI_CODE_MODEL || DEFAULT_GEMINI_MODEL,
    })
    .option('debug_mode', {
      alias: 'z',
      type: 'boolean',
      description: 'Whether to run in debug mode. Defaults to false.',
      default: false,
    })
    .option('question', {
      alias: 'q',
      type: 'string',
      description:
        'The question to pass to the command when using piped input.',
    })
    .option('full_context', {
      alias: 'f',
      type: 'boolean',
      description:
        'Recursively include all files within the current directory as context.',
      default: false,
    })
    .help()
    .alias('h', 'help')
    .strict().argv;
  return argv as unknown as CliArgs;
}

// Renamed function for clarity
export function loadCliConfig(): Config {
  // Load .env file using logic from server package
  loadEnvironment();

  // Check API key (CLI responsibility)
  if (!process.env.GEMINI_API_KEY) {
    console.log(
      'GEMINI_API_KEY is not set. See https://ai.google.dev/gemini-api/docs/api-key to obtain one. ' +
        'Please set it in your .env file or as an environment variable.',
    );
    process.exit(1);
  }

  // Parse CLI arguments
  const argv = parseArguments();

  // Create config using factory from server package
  return createServerConfig(
    process.env.GEMINI_API_KEY,
    argv.model || DEFAULT_GEMINI_MODEL,
    process.cwd(),
    argv.debug_mode || false,
    argv.question || '',
    undefined, // TODO: load passthroughCommands from .env file
    argv.full_context || false,
  );
}