createPowerShellTool function
Builds the tool that runs a PowerShell command when PowerShell is available.
Implementation
Tool<JsonMap, String> createPowerShellTool(
Genkit ai, {
required ToolRuntime runtime,
}) {
const toolName = 'powershell';
return ai.defineTool(
name: toolName,
description: 'Executes a PowerShell command.',
inputSchema: createJsonObjectSchema(
description: 'Input for the powershell tool.',
properties: {
'command': $Schema.string(
description: 'The PowerShell command to execute.',
),
},
required: ['command'],
),
outputSchema: stringOutputSchema,
fn: (input, _) async {
final command = readString(input, 'command')?.trim();
if (command == null || command.isEmpty) {
const error = 'Error: `command` is required.';
runtime.endTool(toolName, error);
return error;
}
runtime.startTool(toolName, command);
ProcessResult result;
try {
result = await Process.run('pwsh', ['-Command', command]);
} on ProcessException {
try {
result = await Process.run('powershell', ['-Command', command]);
} on ProcessException {
const error = 'Error: PowerShell is not available on this machine.';
runtime.endTool(toolName, error);
return error;
}
}
final output = '${result.stdout}${result.stderr}'.trimRight();
runtime.endTool(toolName, output);
return output;
},
);
}