Add the built-in tool run_command_in_terminal for AI to execute commands in the connected PowerShell session
#398
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Summary
Allow AIShell to run command in the connected PowerShell session and collect all output and error.
Add
Invoke-AICommandcmdlet (alias:airun) toAIShellmodule. Commands sent from the sidecar AIShell will be executed through this command in the form ofairun { <command> }. This command is designed to collect all output and error messages as they are displayed in the terminal, while preserving the streaming behavior as expected.Add
RunCommandandPostResultmessages to the protocol.Update the
Channelclass inAIShellmodule to support theOnRunCommandaction. We already support posting command to the PowerShell's prompt, but it turns out not easy to make the command be accepted. On Windows, we have to callAcceptLinewithin anOnIdleevent handler and it also requires changes toPSReadLine.AcceptLineonly set a flag inPSReadLineto indicate the line was accepted. The flag is checked inInputLoop, however, whenPSReadLineis waiting for input, it's blocked in theReadKeycall withinInputLoop, so even if the flag is set,InputLoopwon't be able to check the flag until afterReadKeycall is returned.OnIdleevent, it checks if the_lineAcceptedflag is set. If it's set, it meansAcceptLinegot called within theOnIdlehandler, and it throws aLineAcceptedExceptionto break out fromReadKey. I catch this exception inInputLoopto continue with the flag check.Console.ReadKeywhen the command is returned to PowerShell to execute. On Windows, this could cause minor issues if the command also callsConsole.ReadKey-- 2 threads callingConsole.ReadKeyin parallel, so it's uncertain which will get the next keystroke input. On macOS and Linux, the problem is way much bigger -- any subsequent writing to the terminal may be blocked, because on Unix platforms, reading cursor position will be blocked if another thread is callingConsole.ReadKey.iTerm2, which has a Python API server that allows to send keystrokes to a tab using the Python API, so we could possibly use that for macOS. But Windows Terminal doesn't support that, and thus we will have to use the above approach to accept the command on Windows.PostCodeaction.Add
run_command_in_terminalandget_command_outputtools and expose them to agents.