execute method

  1. @override
Future<ToolResult> execute(
  1. Map<String, dynamic> input
)
override

Execute the tool with the given input.

Implementation

@override
Future<ToolResult> execute(Map<String, dynamic> input) async {
  final parsed = PowerShellInput.fromJson(input);
  final timeout = parsed.timeoutMs != null
      ? Duration(milliseconds: parsed.timeoutMs!)
      : defaultTimeout;

  final sw = Stopwatch()..start();
  try {
    final result = await Process.run('powershell', [
      '-NoProfile',
      '-NonInteractive',
      '-Command',
      parsed.command,
    ]).timeout(timeout);
    sw.stop();

    final out = PowerShellOutput(
      stdout: (result.stdout as String).trim(),
      stderr: (result.stderr as String).trim(),
      exitCode: result.exitCode,
      durationMs: sw.elapsedMilliseconds,
    );

    return result.exitCode != 0
        ? ToolResult(content: out.toString(), isError: true)
        : ToolResult.success(out.toString());
  } on ProcessException catch (e) {
    return ToolResult.error('PowerShell error: ${e.message}');
  } catch (e) {
    return ToolResult.error('Command timed out after ${timeout.inSeconds}s');
  }
}