summaryrefslogtreecommitdiff
path: root/packages/cli/src/ui/hooks/atCommandProcessor.ts
blob: dd97a0d65b6fd9e32b28966aabd9dbe727fd8988 (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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import * as fs from 'fs/promises';
import * as path from 'path';
import { PartListUnion, PartUnion } from '@google/genai';
import {
  Config,
  getErrorMessage,
  isNodeError,
  unescapePath,
} from '@gemini-code/server';
import {
  HistoryItem,
  IndividualToolCallDisplay,
  ToolCallStatus,
} from '../types.js';
import { UseHistoryManagerReturn } from './useHistoryManager.js';

interface HandleAtCommandParams {
  query: string;
  config: Config;
  addItem: UseHistoryManagerReturn['addItem'];
  onDebugMessage: (message: string) => void;
  messageId: number;
  signal: AbortSignal;
}

interface HandleAtCommandResult {
  processedQuery: PartListUnion | null;
  shouldProceed: boolean;
}

/**
 * Parses a query string to find the first '@<path>' command,
 * handling \ escaped spaces within the path.
 */
function parseAtCommand(
  query: string,
): { textBefore: string; atPath: string; textAfter: string } | null {
  let atIndex = -1;
  for (let i = 0; i < query.length; i++) {
    if (query[i] === '@' && (i === 0 || query[i - 1] !== '\\')) {
      atIndex = i;
      break;
    }
  }

  if (atIndex === -1) {
    return null;
  }

  const textBefore = query.substring(0, atIndex).trim();
  let pathEndIndex = atIndex + 1;
  let inEscape = false;

  while (pathEndIndex < query.length) {
    const char = query[pathEndIndex];
    if (inEscape) {
      inEscape = false;
    } else if (char === '\\') {
      inEscape = true;
    } else if (/\s/.test(char)) {
      break;
    }
    pathEndIndex++;
  }

  const rawAtPath = query.substring(atIndex, pathEndIndex);
  const textAfter = query.substring(pathEndIndex).trim();
  const atPath = unescapePath(rawAtPath);

  return { textBefore, atPath, textAfter };
}

/**
 * Processes user input potentially containing an '@<path>' command.
 * If found, it attempts to read the specified file/directory using the
 * 'read_many_files' tool, adds the user query and tool result/error to history,
 * and prepares the content for the LLM.
 *
 * @returns An object indicating whether the main hook should proceed with an
 *          LLM call and the processed query parts (including file content).
 */
export async function handleAtCommand({
  query,
  config,
  addItem,
  onDebugMessage,
  messageId: userMessageTimestamp,
  signal,
}: HandleAtCommandParams): Promise<HandleAtCommandResult> {
  const trimmedQuery = query.trim();
  const parsedCommand = parseAtCommand(trimmedQuery);

  // If no @ command, add user query normally and proceed to LLM
  if (!parsedCommand) {
    addItem({ type: 'user', text: query }, userMessageTimestamp);
    return { processedQuery: [{ text: query }], shouldProceed: true };
  }

  const { textBefore, atPath, textAfter } = parsedCommand;

  // Add the original user query to history first
  addItem({ type: 'user', text: query }, userMessageTimestamp);

  // If the atPath is just "@", pass the original query to the LLM
  if (atPath === '@') {
    onDebugMessage('Lone @ detected, passing directly to LLM.');
    return { processedQuery: [{ text: query }], shouldProceed: true };
  }

  const pathPart = atPath.substring(1); // Remove leading '@'

  // This error condition is for cases where pathPart becomes empty *after* the initial "@" check,
  // which is unlikely with the current parser but good for robustness.
  if (!pathPart) {
    addItem(
      { type: 'error', text: 'Error: No valid path specified after @ symbol.' },
      userMessageTimestamp,
    );
    return { processedQuery: null, shouldProceed: false };
  }

  const contentLabel = pathPart;

  const toolRegistry = config.getToolRegistry();
  const readManyFilesTool = toolRegistry.getTool('read_many_files');

  if (!readManyFilesTool) {
    addItem(
      { type: 'error', text: 'Error: read_many_files tool not found.' },
      userMessageTimestamp,
    );
    return { processedQuery: null, shouldProceed: false };
  }

  // Determine path spec (file or directory glob)
  let pathSpec = pathPart;
  try {
    const absolutePath = path.resolve(config.getTargetDir(), pathPart);
    const stats = await fs.stat(absolutePath);
    if (stats.isDirectory()) {
      pathSpec = pathPart.endsWith('/') ? `${pathPart}**` : `${pathPart}/**`;
      onDebugMessage(`Path resolved to directory, using glob: ${pathSpec}`);
    } else {
      onDebugMessage(`Path resolved to file: ${pathSpec}`);
    }
  } catch (error) {
    if (isNodeError(error) && error.code === 'ENOENT') {
      onDebugMessage(
        `Path ${pathPart} not found directly, attempting glob search.`,
      );
      const globTool = toolRegistry.getTool('glob');
      if (globTool) {
        try {
          const globResult = await globTool.execute(
            {
              pattern: `**/*${pathPart}*`,
              path: config.getTargetDir(), // Ensure glob searches from the root
            },
            signal,
          );
          // Assuming llmContent contains the list of files or a "no files found" message.
          // And that paths are absolute.
          if (
            globResult.llmContent &&
            typeof globResult.llmContent === 'string' &&
            !globResult.llmContent.startsWith('No files found') &&
            !globResult.llmContent.startsWith('Error:')
          ) {
            // Extract the first line after the header
            const lines = globResult.llmContent.split('\n');
            if (lines.length > 1 && lines[1]) {
              const firstMatchAbsolute = lines[1].trim();
              // Convert absolute path from glob to relative path for read_many_files
              pathSpec = path.relative(
                config.getTargetDir(),
                firstMatchAbsolute,
              );
              onDebugMessage(
                `Glob search found ${firstMatchAbsolute}, using relative path: ${pathSpec}`,
              );
            } else {
              onDebugMessage(
                `Glob search for '**/*${pathPart}*' did not return a usable path. Proceeding with original: ${pathPart}`,
              );
              // pathSpec remains pathPart
            }
          } else {
            onDebugMessage(
              `Glob search for '**/*${pathPart}*' found no files or an error occurred. Proceeding with original: ${pathPart}`,
            );
            // pathSpec remains pathPart
          }
        } catch (globError) {
          console.error(
            `Error during glob search: ${getErrorMessage(globError)}`,
          );
          onDebugMessage(
            `Error during glob search. Proceeding with original: ${pathPart}`,
          );
          // pathSpec remains pathPart
        }
      } else {
        onDebugMessage(
          'Glob tool not found. Proceeding with original path: ${pathPart}',
        );
        // pathSpec remains pathPart
      }
    } else {
      console.error(
        `Error stating path ${pathPart}: ${getErrorMessage(error)}`,
      );
      onDebugMessage(
        `Error stating path, proceeding with original: ${pathSpec}`,
      );
    }
  }

  const toolArgs = { paths: [pathSpec] };
  let toolCallDisplay: IndividualToolCallDisplay;

  try {
    const result = await readManyFilesTool.execute(toolArgs, signal);

    toolCallDisplay = {
      callId: `client-read-${userMessageTimestamp}`,
      name: readManyFilesTool.displayName,
      description: readManyFilesTool.getDescription(toolArgs),
      status: ToolCallStatus.Success,
      resultDisplay: result.returnDisplay,
      confirmationDetails: undefined,
    };

    // Prepare the query parts for the LLM
    const processedQueryParts: PartUnion[] = [];
    if (textBefore) {
      processedQueryParts.push({ text: textBefore });
    }

    // Process the result from the tool
    processedQueryParts.push('\n--- Content from: ${contentLabel} ---\n');
    if (Array.isArray(result.llmContent)) {
      for (const part of result.llmContent) {
        processedQueryParts.push(part);
      }
    } else {
      processedQueryParts.push(result.llmContent);
    }
    processedQueryParts.push('\n--- End of content ---\n');

    if (textAfter) {
      processedQueryParts.push({ text: textAfter });
    }
    const processedQuery: PartListUnion = processedQueryParts;

    // Add the successful tool result to history
    addItem(
      { type: 'tool_group', tools: [toolCallDisplay] } as Omit<
        HistoryItem,
        'id'
      >,
      userMessageTimestamp,
    );

    return { processedQuery, shouldProceed: true };
  } catch (error: unknown) {
    // Handle errors during tool execution
    toolCallDisplay = {
      callId: `client-read-${userMessageTimestamp}`,
      name: readManyFilesTool.displayName,
      description: readManyFilesTool.getDescription(toolArgs),
      status: ToolCallStatus.Error,
      resultDisplay: `Error reading ${contentLabel}: ${getErrorMessage(error)}`,
      confirmationDetails: undefined,
    };

    // Add the error tool result to history
    addItem(
      { type: 'tool_group', tools: [toolCallDisplay] } as Omit<
        HistoryItem,
        'id'
      >,
      userMessageTimestamp,
    );

    return { processedQuery: null, shouldProceed: false };
  }
}