2025-04-29 08:29:09 -07:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2025-10-21 16:35:22 -04:00
|
|
|
import { debugLogger } from '@google/gemini-cli-core';
|
2025-11-17 15:48:33 -08:00
|
|
|
import clipboardy from 'clipboardy';
|
2025-12-01 12:29:03 -05:00
|
|
|
import type { SlashCommand } from '../commands/types.js';
|
2025-07-20 20:57:41 +02:00
|
|
|
|
2025-04-29 08:29:09 -07:00
|
|
|
/**
|
|
|
|
|
* Checks if a query string potentially represents an '@' command.
|
|
|
|
|
* It triggers if the query starts with '@' or contains '@' preceded by whitespace
|
|
|
|
|
* and followed by a non-whitespace character.
|
|
|
|
|
*
|
|
|
|
|
* @param query The input query string.
|
|
|
|
|
* @returns True if the query looks like an '@' command, false otherwise.
|
|
|
|
|
*/
|
2025-04-29 15:39:36 -07:00
|
|
|
export const isAtCommand = (query: string): boolean =>
|
2025-05-07 12:30:32 -07:00
|
|
|
// Check if starts with @ OR has a space, then @
|
|
|
|
|
query.startsWith('@') || /\s@/.test(query);
|
2025-04-29 13:29:57 -07:00
|
|
|
|
2025-05-01 00:52:01 +00:00
|
|
|
/**
|
|
|
|
|
* Checks if a query string potentially represents an '/' command.
|
2025-08-26 11:51:27 +08:00
|
|
|
* It triggers if the query starts with '/' but excludes code comments like '//' and '/*'.
|
2025-05-01 00:52:01 +00:00
|
|
|
*
|
|
|
|
|
* @param query The input query string.
|
|
|
|
|
* @returns True if the query looks like an '/' command, false otherwise.
|
|
|
|
|
*/
|
2025-08-26 11:51:27 +08:00
|
|
|
export const isSlashCommand = (query: string): boolean => {
|
|
|
|
|
if (!query.startsWith('/')) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Exclude line comments that start with '//'
|
|
|
|
|
if (query.startsWith('//')) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Exclude block comments that start with '/*'
|
|
|
|
|
if (query.startsWith('/*')) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
};
|
2025-07-20 20:57:41 +02:00
|
|
|
|
2025-11-17 15:48:33 -08:00
|
|
|
// Copies a string snippet to the clipboard
|
2025-07-20 20:57:41 +02:00
|
|
|
export const copyToClipboard = async (text: string): Promise<void> => {
|
2025-11-17 15:48:33 -08:00
|
|
|
await clipboardy.write(text);
|
2025-07-20 20:57:41 +02:00
|
|
|
};
|
2025-08-07 12:00:46 -04:00
|
|
|
|
|
|
|
|
export const getUrlOpenCommand = (): string => {
|
|
|
|
|
// --- Determine the OS-specific command to open URLs ---
|
|
|
|
|
let openCmd: string;
|
|
|
|
|
switch (process.platform) {
|
|
|
|
|
case 'darwin':
|
|
|
|
|
openCmd = 'open';
|
|
|
|
|
break;
|
|
|
|
|
case 'win32':
|
|
|
|
|
openCmd = 'start';
|
|
|
|
|
break;
|
|
|
|
|
case 'linux':
|
|
|
|
|
openCmd = 'xdg-open';
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
// Default to xdg-open, which appears to be supported for the less popular operating systems.
|
|
|
|
|
openCmd = 'xdg-open';
|
2025-10-21 16:35:22 -04:00
|
|
|
debugLogger.warn(
|
2025-08-07 12:00:46 -04:00
|
|
|
`Unknown platform: ${process.platform}. Attempting to open URLs with: ${openCmd}.`,
|
|
|
|
|
);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return openCmd;
|
|
|
|
|
};
|
2025-12-01 12:29:03 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determines if a slash command should auto-execute when selected.
|
|
|
|
|
*
|
|
|
|
|
* All built-in commands have autoExecute explicitly set to true or false.
|
|
|
|
|
* Custom commands (.toml files) and extension commands without this flag
|
|
|
|
|
* will default to false (safe default - won't auto-execute).
|
|
|
|
|
*
|
|
|
|
|
* @param command The slash command to check
|
|
|
|
|
* @returns true if the command should auto-execute on Enter
|
|
|
|
|
*/
|
|
|
|
|
export function isAutoExecutableCommand(
|
|
|
|
|
command: SlashCommand | undefined,
|
|
|
|
|
): boolean {
|
|
|
|
|
if (!command) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Simply return the autoExecute flag value, defaulting to false if undefined
|
|
|
|
|
return command.autoExecute ?? false;
|
|
|
|
|
}
|