/**
* Execute Command Tool
* Executes commands in the OpenClaw environment
*/
import type { OpenClawClient } from '../openclaw-client.js';
import type { ExecuteCommandParams } from '../types.js';
/**
* Input schema for execute_command tool
*/
export const ExecuteCommandInputSchema = {
type: 'object',
properties: {
command: {
type: 'string',
description: 'Command to execute (e.g., "ls -la", "npm install")',
},
timeout: {
type: 'number',
description: 'Timeout in seconds (default: 30)',
default: 30,
},
async: {
type: 'boolean',
description: 'Execute asynchronously (default: false)',
default: false,
},
},
required: ['command'],
};
/**
* Execute the execute_command tool
*/
export async function executeExecuteCommand(
client: OpenClawClient,
args: ExecuteCommandParams
) {
try {
// Validate inputs
if (!args.command || args.command.trim() === '') {
return {
content: [
{
type: 'text',
text: JSON.stringify({ error: 'Command is required' }, null, 2),
},
],
isError: true,
};
}
// Validate timeout
const timeout = args.timeout ?? 30;
if (timeout < 1 || timeout > 300) {
return {
content: [
{
type: 'text',
text: JSON.stringify(
{ error: 'Timeout must be between 1 and 300 seconds' },
null,
2
),
},
],
isError: true,
};
}
// Call the OpenClaw API
const response = await client.executeCommand({
command: args.command,
timeout,
async: args.async ?? false,
});
if (response.success) {
const result: Record<string, unknown> = {
success: true,
command: args.command,
async: args.async ?? false,
};
if (response.taskId) {
result.taskId = response.taskId;
result.message = response.async
? 'Command started asynchronously'
: 'Command executed successfully';
}
if (response.output !== undefined) {
result.output = response.output;
}
if (response.exitCode !== undefined) {
result.exitCode = response.exitCode;
}
return {
content: [
{
type: 'text',
text: JSON.stringify(result, null, 2),
},
],
};
} else {
return {
content: [
{
type: 'text',
text: JSON.stringify(
{
success: false,
command: args.command,
error: response.error || 'Failed to execute command',
},
null,
2
),
},
],
isError: true,
};
}
} catch (error) {
return {
content: [
{
type: 'text',
text: JSON.stringify(
{
error: 'Unexpected error occurred',
details: error instanceof Error ? error.message : String(error),
},
null,
2
),
},
],
isError: true,
};
}
}
/**
* Tool definition for execute_command
*/
export const ExecuteCommandTool = {
name: 'execute_command',
description: 'Execute a command in the OpenClaw environment. Commands can run synchronously or asynchronously.',
inputSchema: ExecuteCommandInputSchema,
};