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-AICommand
cmdlet (alias:airun
) toAIShell
module. 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
RunCommand
andPostResult
messages to the protocol.Update the
Channel
class inAIShell
module to support theOnRunCommand
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 callAcceptLine
within anOnIdle
event handler and it also requires changes toPSReadLine
.AcceptLine
only set a flag inPSReadLine
to indicate the line was accepted. The flag is checked inInputLoop
, however, whenPSReadLine
is waiting for input, it's blocked in theReadKey
call withinInputLoop
, so even if the flag is set,InputLoop
won't be able to check the flag until afterReadKey
call is returned.OnIdle
event, it checks if the_lineAccepted
flag is set. If it's set, it meansAcceptLine
got called within theOnIdle
handler, and it throws aLineAcceptedException
to break out fromReadKey
. I catch this exception inInputLoop
to continue with the flag check.Console.ReadKey
when the command is returned to PowerShell to execute. On Windows, this could cause minor issues if the command also callsConsole.ReadKey
-- 2 threads callingConsole.ReadKey
in 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.PostCode
action.Add
run_command_in_terminal
andget_command_output
tools and expose them to agents.