mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
Add a bunch of shell tool commands
This commit is contained in:
19 files changed
+1710
-45
No files matched your search
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* Example usage of the ShellTool
|
||||
*
|
||||
*
|
||||
* This file demonstrates how to use the ShellTool to execute shell commands
|
||||
* in your application. The ShellTool provides a safe, sandboxed environment
|
||||
* for running basic file operations.
|
||||
@@ -19,36 +19,48 @@ export async function exampleShellUsage(fs: JSRuntimeFS, projectRoot: string) {
|
||||
console.log('Available commands:', shell.getAvailableCommands());
|
||||
console.log('Current directory:', shell.getCurrentWorkingDirectory());
|
||||
|
||||
// Example 1: Display a file
|
||||
console.log('\n--- Example 1: Display package.json ---');
|
||||
// Example 1: Display current directory
|
||||
console.log('\n--- Example 1: Show current directory ---');
|
||||
const pwdResult = await shell.execute({ command: 'pwd' });
|
||||
console.log(pwdResult);
|
||||
|
||||
// Example 2: List directory contents
|
||||
console.log('\n--- Example 2: List files ---');
|
||||
const lsResult = await shell.execute({ command: 'ls' });
|
||||
console.log(lsResult);
|
||||
|
||||
// Example 3: List with long format
|
||||
console.log('\n--- Example 3: List with details ---');
|
||||
const lsLongResult = await shell.execute({ command: 'ls -l' });
|
||||
console.log(lsLongResult);
|
||||
|
||||
// Example 4: Display a file
|
||||
console.log('\n--- Example 4: Display package.json ---');
|
||||
const catResult = await shell.execute({ command: 'cat package.json' });
|
||||
console.log(catResult);
|
||||
|
||||
// Example 2: Try to display multiple files
|
||||
console.log('\n--- Example 2: Display multiple files ---');
|
||||
const multiFileResult = await shell.execute({
|
||||
command: 'cat package.json README.md'
|
||||
});
|
||||
console.log(multiFileResult);
|
||||
// Example 5: Echo text
|
||||
console.log('\n--- Example 5: Echo message ---');
|
||||
const echoResult = await shell.execute({ command: 'echo Hello, World!' });
|
||||
console.log(echoResult);
|
||||
|
||||
// Example 3: Handle errors gracefully
|
||||
console.log('\n--- Example 3: Handle non-existent file ---');
|
||||
const errorResult = await shell.execute({
|
||||
command: 'cat nonexistent-file.txt'
|
||||
// Example 6: Change directory
|
||||
console.log('\n--- Example 6: Change directory ---');
|
||||
const cdResult = await shell.execute({ command: 'cd src' });
|
||||
console.log(cdResult);
|
||||
console.log('New directory:', shell.getCurrentWorkingDirectory());
|
||||
|
||||
// Example 7: Handle errors gracefully
|
||||
console.log('\n--- Example 7: Handle non-existent file ---');
|
||||
const errorResult = await shell.execute({
|
||||
command: 'cat nonexistent-file.txt'
|
||||
});
|
||||
console.log(errorResult);
|
||||
|
||||
// Example 4: Handle quoted filenames with spaces
|
||||
console.log('\n--- Example 4: Quoted filename ---');
|
||||
const quotedResult = await shell.execute({
|
||||
command: 'cat "file with spaces.txt"'
|
||||
});
|
||||
console.log(quotedResult);
|
||||
|
||||
// Example 5: Try unknown command
|
||||
console.log('\n--- Example 5: Unknown command ---');
|
||||
const unknownResult = await shell.execute({
|
||||
command: 'unknown-command arg1 arg2'
|
||||
// Example 8: Try unknown command
|
||||
console.log('\n--- Example 8: Unknown command ---');
|
||||
const unknownResult = await shell.execute({
|
||||
command: 'unknown-command arg1 arg2'
|
||||
});
|
||||
console.log(unknownResult);
|
||||
}
|
||||
@@ -128,7 +140,7 @@ export class SimpleCLI {
|
||||
|
||||
async executeCommand(command: string): Promise<string> {
|
||||
this.history.push(command);
|
||||
|
||||
|
||||
// Handle built-in commands
|
||||
if (command.trim() === 'help') {
|
||||
const commands = this.shell.getAvailableCommands();
|
||||
|
||||
@@ -28,7 +28,7 @@ describe('ShellTool', () => {
|
||||
});
|
||||
|
||||
it('should have correct tool properties', () => {
|
||||
expect(shellTool.description).toBe('Execute shell commands like cat, ls, cd, pwd, rm, cp, mv');
|
||||
expect(shellTool.description).toBe('Execute shell commands like cat, ls, cd, pwd, rm, cp, mv, echo');
|
||||
expect(shellTool.inputSchema).toBeDefined();
|
||||
});
|
||||
|
||||
@@ -45,7 +45,8 @@ describe('ShellTool', () => {
|
||||
it('should return error for unknown command', async () => {
|
||||
const result = await shellTool.execute({ command: 'unknowncommand' });
|
||||
expect(result).toContain("Command 'unknowncommand' not found");
|
||||
expect(result).toContain('Available commands: cat');
|
||||
expect(result).toContain('Available commands:');
|
||||
expect(result).toContain('cat');
|
||||
});
|
||||
|
||||
it('should execute cat command successfully', async () => {
|
||||
@@ -99,12 +100,25 @@ describe('ShellTool', () => {
|
||||
|
||||
it('should return available commands', () => {
|
||||
const commands = shellTool.getAvailableCommands();
|
||||
expect(commands).toHaveLength(1);
|
||||
expect(commands[0]).toEqual({
|
||||
expect(commands.length).toBeGreaterThan(1);
|
||||
|
||||
// Check that cat command is included
|
||||
const catCommand = commands.find(cmd => cmd.name === 'cat');
|
||||
expect(catCommand).toEqual({
|
||||
name: 'cat',
|
||||
description: 'Concatenate and display file contents',
|
||||
usage: 'cat [file...]',
|
||||
});
|
||||
|
||||
// Check that other commands are included
|
||||
const commandNames = commands.map(cmd => cmd.name);
|
||||
expect(commandNames).toContain('ls');
|
||||
expect(commandNames).toContain('cd');
|
||||
expect(commandNames).toContain('pwd');
|
||||
expect(commandNames).toContain('echo');
|
||||
expect(commandNames).toContain('rm');
|
||||
expect(commandNames).toContain('cp');
|
||||
expect(commandNames).toContain('mv');
|
||||
});
|
||||
|
||||
it('should handle command errors gracefully', async () => {
|
||||
@@ -122,4 +136,28 @@ describe('ShellTool', () => {
|
||||
expect(result).toContain('missing file operand');
|
||||
expect(result).toContain('Exit code: 1');
|
||||
});
|
||||
|
||||
it('should execute echo command', async () => {
|
||||
const result = await shellTool.execute({ command: 'echo hello world' });
|
||||
|
||||
expect(result).toBe('hello world\n');
|
||||
});
|
||||
|
||||
it('should execute pwd command', async () => {
|
||||
const result = await shellTool.execute({ command: 'pwd' });
|
||||
|
||||
expect(result).toBe(testCwd);
|
||||
});
|
||||
|
||||
it('should handle cd command and update working directory', async () => {
|
||||
vi.mocked(mockFS.stat).mockResolvedValue({
|
||||
isDirectory: () => true,
|
||||
isFile: () => false,
|
||||
});
|
||||
|
||||
const result = await shellTool.execute({ command: 'cd subdir' });
|
||||
|
||||
expect(result).toBe('(no output)');
|
||||
expect(shellTool.getCurrentWorkingDirectory()).toBe('/test/dir/subdir');
|
||||
});
|
||||
});
|
||||
@@ -2,7 +2,16 @@ import { z } from "zod";
|
||||
import type { Tool } from "./Tool";
|
||||
import type { JSRuntimeFS } from "../JSRuntime";
|
||||
import type { ShellCommand } from "../commands/ShellCommand";
|
||||
import { CatCommand } from "../commands/cat";
|
||||
import {
|
||||
CatCommand,
|
||||
CdCommand,
|
||||
CpCommand,
|
||||
EchoCommand,
|
||||
LsCommand,
|
||||
MvCommand,
|
||||
PwdCommand,
|
||||
RmCommand
|
||||
} from "../commands";
|
||||
|
||||
interface ShellToolParams {
|
||||
command: string;
|
||||
@@ -16,7 +25,7 @@ export class ShellTool implements Tool<ShellToolParams> {
|
||||
private cwd: string;
|
||||
private commands: Map<string, ShellCommand>;
|
||||
|
||||
readonly description = "Execute shell commands like cat, ls, cd, pwd, rm, cp, mv";
|
||||
readonly description = "Execute shell commands like cat, ls, cd, pwd, rm, cp, mv, echo";
|
||||
|
||||
readonly inputSchema = z.object({
|
||||
command: z.string().describe(
|
||||
@@ -28,9 +37,16 @@ export class ShellTool implements Tool<ShellToolParams> {
|
||||
this.fs = fs;
|
||||
this.cwd = cwd;
|
||||
this.commands = new Map();
|
||||
|
||||
|
||||
// Register available commands
|
||||
this.registerCommand(new CatCommand(fs));
|
||||
this.registerCommand(new CdCommand(fs));
|
||||
this.registerCommand(new CpCommand(fs));
|
||||
this.registerCommand(new EchoCommand());
|
||||
this.registerCommand(new LsCommand(fs));
|
||||
this.registerCommand(new MvCommand(fs));
|
||||
this.registerCommand(new PwdCommand());
|
||||
this.registerCommand(new RmCommand(fs));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,7 +68,7 @@ export class ShellTool implements Tool<ShellToolParams> {
|
||||
|
||||
for (let i = 0; i < commandStr.length; i++) {
|
||||
const char = commandStr[i];
|
||||
|
||||
|
||||
if (!inQuotes && (char === '"' || char === "'")) {
|
||||
inQuotes = true;
|
||||
quoteChar = char;
|
||||
@@ -108,11 +124,11 @@ export class ShellTool implements Tool<ShellToolParams> {
|
||||
|
||||
// Format output
|
||||
let output = '';
|
||||
|
||||
|
||||
if (result.stdout) {
|
||||
output += result.stdout;
|
||||
}
|
||||
|
||||
|
||||
if (result.stderr) {
|
||||
if (output) output += '\n';
|
||||
output += result.stderr;
|
||||
|
||||
Reference in new issue
Block a user