mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-22 02:54:31 -07:00
feat(plan): add core logic and exit_plan_mode tool definition (#18110)
This commit is contained in:
@@ -228,6 +228,55 @@ export function isWithinRoot(
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely resolves a path to its real path if it exists, otherwise returns the absolute resolved path.
|
||||
*/
|
||||
export function getRealPath(filePath: string): string {
|
||||
try {
|
||||
return fs.realpathSync(filePath);
|
||||
} catch {
|
||||
return path.resolve(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a file's content is empty or contains only whitespace.
|
||||
* Efficiently checks file size first, and only samples the beginning of the file.
|
||||
* Honors Unicode BOM encodings.
|
||||
*/
|
||||
export async function isEmpty(filePath: string): Promise<boolean> {
|
||||
try {
|
||||
const stats = await fsPromises.stat(filePath);
|
||||
if (stats.size === 0) return true;
|
||||
|
||||
// Sample up to 1KB to check for non-whitespace content.
|
||||
// If a file is larger than 1KB and contains only whitespace,
|
||||
// it's an extreme edge case we can afford to read slightly more of if needed,
|
||||
// but for most valid plans/files, this is sufficient.
|
||||
const fd = await fsPromises.open(filePath, 'r');
|
||||
try {
|
||||
const { buffer } = await fd.read({
|
||||
buffer: Buffer.alloc(Math.min(1024, stats.size)),
|
||||
offset: 0,
|
||||
length: Math.min(1024, stats.size),
|
||||
position: 0,
|
||||
});
|
||||
|
||||
const bom = detectBOM(buffer);
|
||||
const content = bom
|
||||
? buffer.subarray(bom.bomLength).toString('utf8')
|
||||
: buffer.toString('utf8');
|
||||
|
||||
return content.trim().length === 0;
|
||||
} finally {
|
||||
await fd.close();
|
||||
}
|
||||
} catch {
|
||||
// If file is unreadable, we treat it as empty/invalid for validation purposes
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Heuristic: determine if a file is likely binary.
|
||||
* Now BOM-aware: if a Unicode BOM is detected, we treat it as text.
|
||||
|
||||
Reference in New Issue
Block a user