This repository was archived by the owner on Jan 30, 2026. It is now read-only.
Add the built-in tool run_command_in_terminal for AI to execute commands in the connected PowerShell session#398
Merged
daxian-dbw merged 6 commits intoPowerShell:mainfrom Jul 24, 2025
Conversation
…collect all output and error.
1. Add 'Invoke-AICommand' cmdlet (alias: 'airun') to 'AIShell' module. Commands sent from the side-car AIShell will be executed through this command in the form of `airun { <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.
2. Add 'RunCommand' and 'PostResult' messages to the protocol.
3. Update the 'Channel' class in 'AIShell' module to support the 'OnRunCommand' action. 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 call 'AcceptLine' within an 'OnIdle' event handler and it also requires changes to PsReadLine.
- 'AcceptLine' only set a flag in PSReadLine to indicate the line was accepted. The flag is checked in 'InputLoop', however, when PSReadLine is waiting for input, it's blocked in the 'ReadKey' call within 'InputLoop', so even if the flag is set, 'InputLoop' won't be able to check it until after 'ReadKey' call is returned.
- I need to change PSReadLine a bit: After it finishes handling the 'OnIdle' event, it checks if the '_lineAccepted' flag is set. If it's set, it means 'AcceptLine' got called within the 'OnIdle' handler, and it throws a 'LineAcceptedException' to break out from 'ReadKey'. I catch this exception in 'InputLoop' to continue with the flag check.
- However, a problem with this change is: the "readkey thread" is still blocked on 'Console.ReadKey' when the command is returned to PowerShell to execute. On Windows, this could cause minor issues if the command also calls 'Console.ReadKey' -- 2 threads calling 'Console.ReadKey' in parallel, so it's uncertian 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 non-Windows, reading cursor position will be blocked if another thread is calling 'Console.ReadKey'.
- So, this approach can only work on Windows. On macOS, we depend on iTerm2, which has a Python API server and it's possible to send keystrokes to a tab using the Python API, so we could 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.
- On macOS, if the Python API approach works fine, then we could even consider using it for the 'PostCode' action.
4. Add '/code run <command>' to test out the 'RunCommand' functionality end-to-end.
…e connected PS session
StevenBucher98
approved these changes
Jul 23, 2025
StevenBucher98
approved these changes
Jul 23, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.