|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { promises as fs } from 'node:fs'; |
|
|
import path from 'node:path'; |
|
|
import toml from '@iarna/toml'; |
|
|
import { glob } from 'glob'; |
|
|
import { z } from 'zod'; |
|
|
import type { Config } from '@google/gemini-cli-core'; |
|
|
import { Storage } from '@google/gemini-cli-core'; |
|
|
import type { ICommandLoader } from './types.js'; |
|
|
import type { |
|
|
CommandContext, |
|
|
SlashCommand, |
|
|
SlashCommandActionReturn, |
|
|
} from '../ui/commands/types.js'; |
|
|
import { CommandKind } from '../ui/commands/types.js'; |
|
|
import { DefaultArgumentProcessor } from './prompt-processors/argumentProcessor.js'; |
|
|
import type { |
|
|
IPromptProcessor, |
|
|
PromptPipelineContent, |
|
|
} from './prompt-processors/types.js'; |
|
|
import { |
|
|
SHORTHAND_ARGS_PLACEHOLDER, |
|
|
SHELL_INJECTION_TRIGGER, |
|
|
AT_FILE_INJECTION_TRIGGER, |
|
|
} from './prompt-processors/types.js'; |
|
|
import { |
|
|
ConfirmationRequiredError, |
|
|
ShellProcessor, |
|
|
} from './prompt-processors/shellProcessor.js'; |
|
|
import { AtFileProcessor } from './prompt-processors/atFileProcessor.js'; |
|
|
|
|
|
interface CommandDirectory { |
|
|
path: string; |
|
|
extensionName?: string; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const TomlCommandDefSchema = z.object({ |
|
|
prompt: z.string({ |
|
|
required_error: "The 'prompt' field is required.", |
|
|
invalid_type_error: "The 'prompt' field must be a string.", |
|
|
}), |
|
|
description: z.string().optional(), |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class FileCommandLoader implements ICommandLoader { |
|
|
private readonly projectRoot: string; |
|
|
private readonly folderTrustEnabled: boolean; |
|
|
private readonly folderTrust: boolean; |
|
|
|
|
|
constructor(private readonly config: Config | null) { |
|
|
this.folderTrustEnabled = !!config?.getFolderTrustFeature(); |
|
|
this.folderTrust = !!config?.getFolderTrust(); |
|
|
this.projectRoot = config?.getProjectRoot() || process.cwd(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async loadCommands(signal: AbortSignal): Promise<SlashCommand[]> { |
|
|
const allCommands: SlashCommand[] = []; |
|
|
const globOptions = { |
|
|
nodir: true, |
|
|
dot: true, |
|
|
signal, |
|
|
follow: true, |
|
|
}; |
|
|
|
|
|
|
|
|
const commandDirs = this.getCommandDirectories(); |
|
|
for (const dirInfo of commandDirs) { |
|
|
try { |
|
|
const files = await glob('**/*.toml', { |
|
|
...globOptions, |
|
|
cwd: dirInfo.path, |
|
|
}); |
|
|
|
|
|
if (this.folderTrustEnabled && !this.folderTrust) { |
|
|
return []; |
|
|
} |
|
|
|
|
|
const commandPromises = files.map((file) => |
|
|
this.parseAndAdaptFile( |
|
|
path.join(dirInfo.path, file), |
|
|
dirInfo.path, |
|
|
dirInfo.extensionName, |
|
|
), |
|
|
); |
|
|
|
|
|
const commands = (await Promise.all(commandPromises)).filter( |
|
|
(cmd): cmd is SlashCommand => cmd !== null, |
|
|
); |
|
|
|
|
|
|
|
|
allCommands.push(...commands); |
|
|
} catch (error) { |
|
|
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') { |
|
|
console.error( |
|
|
`[FileCommandLoader] Error loading commands from ${dirInfo.path}:`, |
|
|
error, |
|
|
); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
return allCommands; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private getCommandDirectories(): CommandDirectory[] { |
|
|
const dirs: CommandDirectory[] = []; |
|
|
|
|
|
const storage = this.config?.storage ?? new Storage(this.projectRoot); |
|
|
|
|
|
|
|
|
dirs.push({ path: Storage.getUserCommandsDir() }); |
|
|
|
|
|
|
|
|
dirs.push({ path: storage.getProjectCommandsDir() }); |
|
|
|
|
|
|
|
|
if (this.config) { |
|
|
const activeExtensions = this.config |
|
|
.getExtensions() |
|
|
.filter((ext) => ext.isActive) |
|
|
.sort((a, b) => a.name.localeCompare(b.name)); |
|
|
|
|
|
const extensionCommandDirs = activeExtensions.map((ext) => ({ |
|
|
path: path.join(ext.path, 'commands'), |
|
|
extensionName: ext.name, |
|
|
})); |
|
|
|
|
|
dirs.push(...extensionCommandDirs); |
|
|
} |
|
|
|
|
|
return dirs; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async parseAndAdaptFile( |
|
|
filePath: string, |
|
|
baseDir: string, |
|
|
extensionName?: string, |
|
|
): Promise<SlashCommand | null> { |
|
|
let fileContent: string; |
|
|
try { |
|
|
fileContent = await fs.readFile(filePath, 'utf-8'); |
|
|
} catch (error: unknown) { |
|
|
console.error( |
|
|
`[FileCommandLoader] Failed to read file ${filePath}:`, |
|
|
error instanceof Error ? error.message : String(error), |
|
|
); |
|
|
return null; |
|
|
} |
|
|
|
|
|
let parsed: unknown; |
|
|
try { |
|
|
parsed = toml.parse(fileContent); |
|
|
} catch (error: unknown) { |
|
|
console.error( |
|
|
`[FileCommandLoader] Failed to parse TOML file ${filePath}:`, |
|
|
error instanceof Error ? error.message : String(error), |
|
|
); |
|
|
return null; |
|
|
} |
|
|
|
|
|
const validationResult = TomlCommandDefSchema.safeParse(parsed); |
|
|
|
|
|
if (!validationResult.success) { |
|
|
console.error( |
|
|
`[FileCommandLoader] Skipping invalid command file: ${filePath}. Validation errors:`, |
|
|
validationResult.error.flatten(), |
|
|
); |
|
|
return null; |
|
|
} |
|
|
|
|
|
const validDef = validationResult.data; |
|
|
|
|
|
const relativePathWithExt = path.relative(baseDir, filePath); |
|
|
const relativePath = relativePathWithExt.substring( |
|
|
0, |
|
|
relativePathWithExt.length - 5, |
|
|
); |
|
|
const baseCommandName = relativePath |
|
|
.split(path.sep) |
|
|
|
|
|
|
|
|
|
|
|
.map((segment) => segment.replaceAll(':', '_')) |
|
|
.join(':'); |
|
|
|
|
|
|
|
|
const defaultDescription = `Custom command from ${path.basename(filePath)}`; |
|
|
let description = validDef.description || defaultDescription; |
|
|
if (extensionName) { |
|
|
description = `[${extensionName}] ${description}`; |
|
|
} |
|
|
|
|
|
const processors: IPromptProcessor[] = []; |
|
|
const usesArgs = validDef.prompt.includes(SHORTHAND_ARGS_PLACEHOLDER); |
|
|
const usesShellInjection = validDef.prompt.includes( |
|
|
SHELL_INJECTION_TRIGGER, |
|
|
); |
|
|
const usesAtFileInjection = validDef.prompt.includes( |
|
|
AT_FILE_INJECTION_TRIGGER, |
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (usesAtFileInjection) { |
|
|
processors.push(new AtFileProcessor(baseCommandName)); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (usesShellInjection || usesArgs) { |
|
|
processors.push(new ShellProcessor(baseCommandName)); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!usesArgs) { |
|
|
processors.push(new DefaultArgumentProcessor()); |
|
|
} |
|
|
|
|
|
return { |
|
|
name: baseCommandName, |
|
|
description, |
|
|
kind: CommandKind.FILE, |
|
|
extensionName, |
|
|
action: async ( |
|
|
context: CommandContext, |
|
|
_args: string, |
|
|
): Promise<SlashCommandActionReturn> => { |
|
|
if (!context.invocation) { |
|
|
console.error( |
|
|
`[FileCommandLoader] Critical error: Command '${baseCommandName}' was executed without invocation context.`, |
|
|
); |
|
|
return { |
|
|
type: 'submit_prompt', |
|
|
content: [{ text: validDef.prompt }], |
|
|
}; |
|
|
} |
|
|
|
|
|
try { |
|
|
let processedContent: PromptPipelineContent = [ |
|
|
{ text: validDef.prompt }, |
|
|
]; |
|
|
for (const processor of processors) { |
|
|
processedContent = await processor.process( |
|
|
processedContent, |
|
|
context, |
|
|
); |
|
|
} |
|
|
|
|
|
return { |
|
|
type: 'submit_prompt', |
|
|
content: processedContent, |
|
|
}; |
|
|
} catch (e) { |
|
|
|
|
|
if (e instanceof ConfirmationRequiredError) { |
|
|
|
|
|
return { |
|
|
type: 'confirm_shell_commands', |
|
|
commandsToConfirm: e.commandsToConfirm, |
|
|
originalInvocation: { |
|
|
raw: context.invocation.raw, |
|
|
}, |
|
|
}; |
|
|
} |
|
|
|
|
|
throw e; |
|
|
} |
|
|
}, |
|
|
}; |
|
|
} |
|
|
} |
|
|
|