summaryrefslogtreecommitdiff
path: root/packages/server/src/tools/shell.ts
blob: bf4cf81075b4b04e6c12fea19a4b8c077748526f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import fs from 'fs';
import { Config } from '../config/config.js';
import {
  BaseTool,
  ToolResult,
  ToolCallConfirmationDetails,
  ToolExecuteConfirmationDetails,
  ToolConfirmationOutcome,
} from './tools.js';
import toolParameterSchema from './shell.json' with { type: 'json' };

export interface ShellToolParams {
  command: string;
  description?: string;
}

export class ShellTool extends BaseTool<ShellToolParams, ToolResult> {
  static Name: string = 'execute_bash_command';
  private readonly config: Config;
  private cwd: string;
  private whitelist: Set<string> = new Set();

  constructor(config: Config) {
    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;
    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;
  }

  async execute(_params: ShellToolParams): Promise<ToolResult> {
    return {
      llmContent: 'hello',
      returnDisplay: 'hello',
    };
  }
}