2025-04-29 08:29:09 -07:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2025-04-30 09:09:01 -07:00
|
|
|
import * as fs from 'fs/promises';
|
|
|
|
|
import * as path from 'path';
|
2025-04-29 08:29:09 -07:00
|
|
|
import { PartListUnion } from '@google/genai';
|
2025-05-01 18:02:04 -07:00
|
|
|
import {
|
|
|
|
|
Config,
|
|
|
|
|
getErrorMessage,
|
|
|
|
|
isNodeError,
|
|
|
|
|
unescapePath,
|
|
|
|
|
} from '@gemini-code/server';
|
2025-04-29 08:29:09 -07:00
|
|
|
import {
|
|
|
|
|
HistoryItem,
|
|
|
|
|
IndividualToolCallDisplay,
|
|
|
|
|
ToolCallStatus,
|
|
|
|
|
} from '../types.js';
|
2025-05-06 16:20:28 -07:00
|
|
|
import { UseHistoryManagerReturn } from './useHistoryManager.js';
|
2025-04-29 08:29:09 -07:00
|
|
|
|
|
|
|
|
interface HandleAtCommandParams {
|
2025-04-30 08:31:32 -07:00
|
|
|
query: string;
|
2025-04-29 08:29:09 -07:00
|
|
|
config: Config;
|
2025-05-06 16:20:28 -07:00
|
|
|
addItem: UseHistoryManagerReturn['addItem'];
|
2025-05-13 23:55:49 +00:00
|
|
|
onDebugMessage: (message: string) => void;
|
2025-05-06 16:20:28 -07:00
|
|
|
messageId: number;
|
2025-05-09 23:29:02 -07:00
|
|
|
signal: AbortSignal;
|
2025-04-29 08:29:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface HandleAtCommandResult {
|
2025-04-30 08:31:32 -07:00
|
|
|
processedQuery: PartListUnion | null;
|
|
|
|
|
shouldProceed: boolean;
|
2025-04-29 08:29:09 -07:00
|
|
|
}
|
|
|
|
|
|
2025-05-01 18:02:04 -07:00
|
|
|
/**
|
|
|
|
|
* 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) {
|
2025-05-06 16:20:28 -07:00
|
|
|
return null;
|
2025-05-01 18:02:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 };
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-29 08:29:09 -07:00
|
|
|
/**
|
2025-04-29 15:39:36 -07:00
|
|
|
* Processes user input potentially containing an '@<path>' command.
|
2025-05-06 16:20:28 -07:00
|
|
|
* 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.
|
2025-04-29 08:29:09 -07:00
|
|
|
*
|
2025-05-06 16:20:28 -07:00
|
|
|
* @returns An object indicating whether the main hook should proceed with an
|
|
|
|
|
* LLM call and the processed query parts (including file content).
|
2025-04-29 08:29:09 -07:00
|
|
|
*/
|
|
|
|
|
export async function handleAtCommand({
|
|
|
|
|
query,
|
|
|
|
|
config,
|
2025-05-07 12:57:19 -07:00
|
|
|
addItem,
|
2025-05-13 23:55:49 +00:00
|
|
|
onDebugMessage,
|
2025-05-06 16:20:28 -07:00
|
|
|
messageId: userMessageTimestamp,
|
2025-05-09 23:29:02 -07:00
|
|
|
signal,
|
2025-04-29 08:29:09 -07:00
|
|
|
}: HandleAtCommandParams): Promise<HandleAtCommandResult> {
|
|
|
|
|
const trimmedQuery = query.trim();
|
2025-05-01 18:02:04 -07:00
|
|
|
const parsedCommand = parseAtCommand(trimmedQuery);
|
2025-04-29 08:29:09 -07:00
|
|
|
|
2025-05-06 16:20:28 -07:00
|
|
|
// If no @ command, add user query normally and proceed to LLM
|
2025-05-01 18:02:04 -07:00
|
|
|
if (!parsedCommand) {
|
2025-05-06 16:20:28 -07:00
|
|
|
addItem({ type: 'user', text: query }, userMessageTimestamp);
|
2025-05-01 18:02:04 -07:00
|
|
|
return { processedQuery: [{ text: query }], shouldProceed: true };
|
2025-04-29 08:29:09 -07:00
|
|
|
}
|
|
|
|
|
|
2025-05-01 18:02:04 -07:00
|
|
|
const { textBefore, atPath, textAfter } = parsedCommand;
|
2025-04-29 15:39:36 -07:00
|
|
|
|
2025-05-06 16:20:28 -07:00
|
|
|
// Add the original user query to history first
|
|
|
|
|
addItem({ type: 'user', text: query }, userMessageTimestamp);
|
2025-04-29 15:39:36 -07:00
|
|
|
|
2025-05-07 12:30:32 -07:00
|
|
|
// If the atPath is just "@", pass the original query to the LLM
|
|
|
|
|
if (atPath === '@') {
|
2025-05-13 23:55:49 +00:00
|
|
|
onDebugMessage('Lone @ detected, passing directly to LLM.');
|
2025-05-07 12:30:32 -07:00
|
|
|
return { processedQuery: [{ text: query }], shouldProceed: true };
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-06 16:20:28 -07:00
|
|
|
const pathPart = atPath.substring(1); // Remove leading '@'
|
2025-04-29 08:29:09 -07:00
|
|
|
|
2025-05-07 12:30:32 -07:00
|
|
|
// 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.
|
2025-04-29 15:39:36 -07:00
|
|
|
if (!pathPart) {
|
2025-05-06 16:20:28 -07:00
|
|
|
addItem(
|
2025-05-07 12:30:32 -07:00
|
|
|
{ type: 'error', text: 'Error: No valid path specified after @ symbol.' },
|
2025-05-06 16:20:28 -07:00
|
|
|
userMessageTimestamp,
|
2025-04-29 08:29:09 -07:00
|
|
|
);
|
2025-04-29 15:39:36 -07:00
|
|
|
return { processedQuery: null, shouldProceed: false };
|
2025-04-29 08:29:09 -07:00
|
|
|
}
|
|
|
|
|
|
2025-05-07 12:30:32 -07:00
|
|
|
const contentLabel = pathPart;
|
2025-04-29 08:29:09 -07:00
|
|
|
const toolRegistry = config.getToolRegistry();
|
|
|
|
|
const readManyFilesTool = toolRegistry.getTool('read_many_files');
|
|
|
|
|
|
|
|
|
|
if (!readManyFilesTool) {
|
2025-05-06 16:20:28 -07:00
|
|
|
addItem(
|
2025-04-29 08:29:09 -07:00
|
|
|
{ type: 'error', text: 'Error: read_many_files tool not found.' },
|
2025-05-06 16:20:28 -07:00
|
|
|
userMessageTimestamp,
|
2025-04-29 08:29:09 -07:00
|
|
|
);
|
2025-04-30 08:31:32 -07:00
|
|
|
return { processedQuery: null, shouldProceed: false };
|
2025-04-29 08:29:09 -07:00
|
|
|
}
|
|
|
|
|
|
2025-05-06 16:20:28 -07:00
|
|
|
// Determine path spec (file or directory glob)
|
2025-04-30 08:31:32 -07:00
|
|
|
let pathSpec = pathPart;
|
2025-04-30 09:09:01 -07:00
|
|
|
try {
|
|
|
|
|
const absolutePath = path.resolve(config.getTargetDir(), pathPart);
|
|
|
|
|
const stats = await fs.stat(absolutePath);
|
|
|
|
|
if (stats.isDirectory()) {
|
|
|
|
|
pathSpec = pathPart.endsWith('/') ? `${pathPart}**` : `${pathPart}/**`;
|
2025-05-13 23:55:49 +00:00
|
|
|
onDebugMessage(`Path resolved to directory, using glob: ${pathSpec}`);
|
2025-04-30 09:09:01 -07:00
|
|
|
} else {
|
2025-05-13 23:55:49 +00:00
|
|
|
onDebugMessage(`Path resolved to file: ${pathSpec}`);
|
2025-04-30 09:09:01 -07:00
|
|
|
}
|
|
|
|
|
} catch (error) {
|
2025-05-06 16:20:28 -07:00
|
|
|
// If stat fails (e.g., not found), proceed with original path.
|
|
|
|
|
// The tool itself will handle the error during execution.
|
2025-04-30 09:09:01 -07:00
|
|
|
if (isNodeError(error) && error.code === 'ENOENT') {
|
2025-05-13 23:55:49 +00:00
|
|
|
onDebugMessage(`Path not found, proceeding with original: ${pathSpec}`);
|
2025-04-30 09:09:01 -07:00
|
|
|
} else {
|
|
|
|
|
console.error(`Error stating path ${pathPart}:`, error);
|
2025-05-13 23:55:49 +00:00
|
|
|
onDebugMessage(
|
2025-04-30 09:09:01 -07:00
|
|
|
`Error stating path, proceeding with original: ${pathSpec}`,
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-04-29 08:29:09 -07:00
|
|
|
}
|
2025-04-30 09:09:01 -07:00
|
|
|
|
2025-04-29 08:29:09 -07:00
|
|
|
const toolArgs = { paths: [pathSpec] };
|
|
|
|
|
let toolCallDisplay: IndividualToolCallDisplay;
|
|
|
|
|
|
|
|
|
|
try {
|
2025-05-09 23:29:02 -07:00
|
|
|
const result = await readManyFilesTool.execute(toolArgs, signal);
|
2025-04-29 08:29:09 -07:00
|
|
|
const fileContent = result.llmContent || '';
|
|
|
|
|
|
|
|
|
|
toolCallDisplay = {
|
|
|
|
|
callId: `client-read-${userMessageTimestamp}`,
|
|
|
|
|
name: readManyFilesTool.displayName,
|
|
|
|
|
description: readManyFilesTool.getDescription(toolArgs),
|
|
|
|
|
status: ToolCallStatus.Success,
|
|
|
|
|
resultDisplay: result.returnDisplay,
|
|
|
|
|
confirmationDetails: undefined,
|
|
|
|
|
};
|
|
|
|
|
|
2025-05-06 16:20:28 -07:00
|
|
|
// Prepare the query parts for the LLM
|
2025-04-29 15:39:36 -07:00
|
|
|
const processedQueryParts = [];
|
|
|
|
|
if (textBefore) {
|
|
|
|
|
processedQueryParts.push({ text: textBefore });
|
|
|
|
|
}
|
|
|
|
|
processedQueryParts.push({
|
|
|
|
|
text: `\n--- Content from: ${contentLabel} ---\n${fileContent}\n--- End Content ---`,
|
|
|
|
|
});
|
|
|
|
|
if (textAfter) {
|
|
|
|
|
processedQueryParts.push({ text: textAfter });
|
|
|
|
|
}
|
|
|
|
|
const processedQuery: PartListUnion = processedQueryParts;
|
2025-04-29 08:29:09 -07:00
|
|
|
|
2025-05-06 16:20:28 -07:00
|
|
|
// Add the successful tool result to history
|
|
|
|
|
addItem(
|
2025-04-29 08:29:09 -07:00
|
|
|
{ type: 'tool_group', tools: [toolCallDisplay] } as Omit<
|
|
|
|
|
HistoryItem,
|
|
|
|
|
'id'
|
|
|
|
|
>,
|
2025-05-06 16:20:28 -07:00
|
|
|
userMessageTimestamp,
|
2025-04-29 08:29:09 -07:00
|
|
|
);
|
|
|
|
|
|
2025-04-30 08:31:32 -07:00
|
|
|
return { processedQuery, shouldProceed: true };
|
2025-04-29 08:29:09 -07:00
|
|
|
} catch (error) {
|
2025-05-06 16:20:28 -07:00
|
|
|
// Handle errors during tool execution
|
2025-04-29 08:29:09 -07:00
|
|
|
toolCallDisplay = {
|
|
|
|
|
callId: `client-read-${userMessageTimestamp}`,
|
|
|
|
|
name: readManyFilesTool.displayName,
|
|
|
|
|
description: readManyFilesTool.getDescription(toolArgs),
|
|
|
|
|
status: ToolCallStatus.Error,
|
|
|
|
|
resultDisplay: `Error reading ${contentLabel}: ${getErrorMessage(error)}`,
|
|
|
|
|
confirmationDetails: undefined,
|
|
|
|
|
};
|
|
|
|
|
|
2025-05-06 16:20:28 -07:00
|
|
|
// Add the error tool result to history
|
|
|
|
|
addItem(
|
2025-04-29 08:29:09 -07:00
|
|
|
{ type: 'tool_group', tools: [toolCallDisplay] } as Omit<
|
|
|
|
|
HistoryItem,
|
|
|
|
|
'id'
|
|
|
|
|
>,
|
2025-05-06 16:20:28 -07:00
|
|
|
userMessageTimestamp,
|
2025-04-29 08:29:09 -07:00
|
|
|
);
|
|
|
|
|
|
2025-04-30 08:31:32 -07:00
|
|
|
return { processedQuery: null, shouldProceed: false };
|
2025-04-29 08:29:09 -07:00
|
|
|
}
|
|
|
|
|
}
|