2025-04-24 18:03:33 -07:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import fs from 'fs';
|
|
|
|
|
import { Config } from '../config/config.js';
|
2025-04-25 14:05:58 -07:00
|
|
|
import {
|
|
|
|
|
BaseTool,
|
|
|
|
|
ToolResult,
|
|
|
|
|
ToolCallConfirmationDetails,
|
|
|
|
|
ToolExecuteConfirmationDetails,
|
|
|
|
|
ToolConfirmationOutcome,
|
|
|
|
|
} from './tools.js';
|
2025-04-24 18:30:19 -07:00
|
|
|
import toolParameterSchema from './shell.json' with { type: 'json' };
|
2025-04-24 18:03:33 -07:00
|
|
|
|
|
|
|
|
export interface ShellToolParams {
|
|
|
|
|
command: string;
|
|
|
|
|
description?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class ShellTool extends BaseTool<ShellToolParams, ToolResult> {
|
|
|
|
|
static Name: string = 'execute_bash_command';
|
|
|
|
|
private readonly config: Config;
|
2025-04-25 14:05:58 -07:00
|
|
|
private cwd: string;
|
|
|
|
|
private whitelist: Set<string> = new Set();
|
2025-04-24 18:03:33 -07:00
|
|
|
|
2025-04-25 14:05:58 -07:00
|
|
|
constructor(config: Config) {
|
2025-04-24 18:03:33 -07:00
|
|
|
const toolDisplayName = 'Shell';
|
|
|
|
|
const descriptionUrl = new URL('shell.md', import.meta.url);
|
|
|
|
|
const toolDescription = fs.readFileSync(descriptionUrl, 'utf-8');
|
|
|
|
|
super(
|
|
|
|
|
ShellTool.Name,
|
|
|
|
|
toolDisplayName,
|
|
|
|
|
toolDescription,
|
|
|
|
|
toolParameterSchema,
|
|
|
|
|
);
|
|
|
|
|
this.config = config;
|
2025-04-25 14:05:58 -07:00
|
|
|
this.cwd = config.getTargetDir();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getDescription(params: ShellToolParams): string {
|
|
|
|
|
return params.description || `Execute \`${params.command}\` in ${this.cwd}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
validateToolParams(_params: ShellToolParams): string | null {
|
|
|
|
|
// TODO: validate the command here
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async shouldConfirmExecute(
|
|
|
|
|
params: ShellToolParams,
|
|
|
|
|
): Promise<ToolCallConfirmationDetails | false> {
|
|
|
|
|
const rootCommand =
|
|
|
|
|
params.command
|
|
|
|
|
.trim()
|
|
|
|
|
.split(/[\s;&&|]+/)[0]
|
|
|
|
|
?.split(/[/\\]/)
|
|
|
|
|
.pop() || 'unknown';
|
|
|
|
|
if (this.whitelist.has(rootCommand)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
const confirmationDetails: ToolExecuteConfirmationDetails = {
|
|
|
|
|
title: 'Confirm Shell Command',
|
|
|
|
|
command: params.command,
|
|
|
|
|
rootCommand,
|
|
|
|
|
onConfirm: async (outcome: ToolConfirmationOutcome) => {
|
|
|
|
|
if (outcome === ToolConfirmationOutcome.ProceedAlways) {
|
|
|
|
|
this.whitelist.add(rootCommand);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
return confirmationDetails;
|
2025-04-24 18:03:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async execute(_params: ShellToolParams): Promise<ToolResult> {
|
|
|
|
|
return {
|
|
|
|
|
llmContent: 'hello',
|
|
|
|
|
returnDisplay: 'hello',
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|