summaryrefslogtreecommitdiff
path: root/packages/cli/src/ui/hooks/shellCommandProcessor.ts
blob: d0615ce5b87a01f6a9d629ba230a1fd3059b1aa0 (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
87
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { exec as _exec } from 'child_process';
import { useCallback } from 'react';
import { Config } from '@gemini-code/server';
import { type PartListUnion } from '@google/genai';
import { StreamingState } from '../types.js';
import { getCommandFromQuery } from '../utils/commandUtils.js';
import { UseHistoryManagerReturn } from './useHistoryManager.js';

/**
 * Hook to process shell commands (e.g., !ls, $pwd).
 * Executes the command in the target directory and adds output/errors to history.
 */
export const useShellCommandProcessor = (
  addItemToHistory: UseHistoryManagerReturn['addItem'],
  setStreamingState: React.Dispatch<React.SetStateAction<StreamingState>>,
  onDebugMessage: (message: string) => void,
  config: Config,
) => {
  /**
   * Checks if the query is a shell command, executes it, and adds results to history.
   * @returns True if the query was handled as a shell command, false otherwise.
   */
  const handleShellCommand = useCallback(
    (rawQuery: PartListUnion): boolean => {
      if (typeof rawQuery !== 'string') {
        return false;
      }

      const [symbol] = getCommandFromQuery(rawQuery);
      if (symbol !== '!' && symbol !== '$') {
        return false;
      }
      const commandToExecute = rawQuery.trim().slice(1).trimStart();

      const userMessageTimestamp = Date.now();
      addItemToHistory({ type: 'user', text: rawQuery }, userMessageTimestamp);

      if (!commandToExecute) {
        addItemToHistory(
          { type: 'error', text: 'Empty shell command.' },
          userMessageTimestamp,
        );
        return true; // Handled (by showing error)
      }

      const targetDir = config.getTargetDir();
      onDebugMessage(
        `Executing shell command in ${targetDir}: ${commandToExecute}`,
      );
      const execOptions = {
        cwd: targetDir,
      };

      setStreamingState(StreamingState.Responding);

      _exec(commandToExecute, execOptions, (error, stdout, stderr) => {
        if (error) {
          addItemToHistory(
            { type: 'error', text: error.message },
            userMessageTimestamp,
          );
        } else {
          let output = '';
          if (stdout) output += stdout;
          if (stderr) output += (output ? '\n' : '') + stderr; // Include stderr as info

          addItemToHistory(
            { type: 'info', text: output || '(Command produced no output)' },
            userMessageTimestamp,
          );
        }
        setStreamingState(StreamingState.Idle);
      });

      return true; // Command was initiated
    },
    [config, onDebugMessage, addItemToHistory, setStreamingState],
  );

  return { handleShellCommand };
};