Skip to content

Commit

Permalink
Add suggestion to use /code post or /code copy after parameter in…
Browse files Browse the repository at this point in the history
…jection (#279)

- Update `IShell` to expose whether a channel to a shell application has been established.
- Add suggestion to use `/code post` or `/code copy` after placeholder replacement, depending on whether a connected shell is available.
  • Loading branch information
daxian-dbw authored Nov 6, 2024
1 parent 041921f commit 417ceb0
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
5 changes: 5 additions & 0 deletions shell/AIShell.Abstraction/IShell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ public interface IShell
/// </summary>
IHost Host { get; }

/// <summary>
/// Indicates whether the bi-directional channel with an application (e.g. a PowerShell session) has been established.
/// </summary>
bool ChannelEstablished { get; }

/// <summary>
/// The token to indicate cancellation when `Ctrl+c` is pressed by user.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions shell/AIShell.Kernel/Shell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ internal sealed class Shell : IShell
#region IShell implementation

IHost IShell.Host => Host;
bool IShell.ChannelEstablished => Channel is not null;
CancellationToken IShell.CancellationToken => _cancellationSource.Token;
List<CodeBlock> IShell.ExtractCodeBlocks(string text, out List<SourceInfo> sourceInfos) => Utils.ExtractCodeBlocks(text, out sourceInfos);

Expand Down
19 changes: 15 additions & 4 deletions shell/agents/Microsoft.Azure.Agent/AzureAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public async Task<bool> ChatAsync(string input, IShell shell)
ArgPlaceholder = new ArgumentPlaceholder(input, data, _httpClient);
}

string answer = data is null ? _copilotResponse.Text : GenerateAnswer(data);
string answer = data is null ? _copilotResponse.Text : GenerateAnswer(data, shell);
host.RenderFullResponse(answer);
}
}
Expand Down Expand Up @@ -503,8 +503,13 @@ internal void ReplaceKnownPlaceholders(ResponseData data)
}
}

internal string GenerateAnswer(ResponseData data)
internal string GenerateAnswer(ResponseData data, IShell shell)
{
// Use green (0,195,0) on grey (48,48,48) for rendering commands in the markdown.
// TODO: the color formatting should be exposed by the shell as utility method.
const string CommandVTColor = "\x1b[38;2;0;195;0;48;2;48;48;48m";
const string ResetVT = "\x1b[0m";

_buffer.Clear();
string text = data.Text;

Expand All @@ -528,7 +533,13 @@ internal string GenerateAnswer(ResponseData data)
_buffer.Append(text.AsSpan(index, text.Length - index));
}

if (data.PlaceholderSet is not null)
if (data.PlaceholderSet is null)
{
_buffer.Append(shell.ChannelEstablished
? $"\nRun {CommandVTColor} /code post {ResetVT} or press {CommandVTColor} Ctrl+d,Ctrl+d {ResetVT} to post the code to the connected shell.\n"
: $"\nRun {CommandVTColor} /code copy {ResetVT} or press {CommandVTColor} Ctrl+d,Ctrl+c {ResetVT} to copy the code to clipboard.\n");
}
else
{
// Construct text about the placeholders if we successfully stripped the placeholder
// section off from the original response.
Expand All @@ -547,7 +558,7 @@ internal string GenerateAnswer(ResponseData data)

// Use green (0,195,0) on grey (48,48,48) for rendering the command '/replace'.
// TODO: the color formatting should be exposed by the shell as utility method.
_buffer.Append("\nRun \x1b[38;2;0;195;0;48;2;48;48;48m /replace \x1b[0m to get assistance in placeholder replacement.\n");
_buffer.Append($"\nRun {CommandVTColor} /replace {ResetVT} to get assistance in placeholder replacement.\n");
}
}

Expand Down
2 changes: 1 addition & 1 deletion shell/agents/Microsoft.Azure.Agent/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,6 @@ private async Task<string> RegenerateAsync()
_agent.ResetArgumentPlaceholder();
}

return _agent.GenerateAnswer(data);
return _agent.GenerateAnswer(data, Shell);
}
}
2 changes: 1 addition & 1 deletion shell/shell.common.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>12.0</LangVersion>
<Version>0.1.0-alpha.19</Version>
<Version>0.1.0-alpha.20</Version>

<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down

0 comments on commit 417ceb0

Please sign in to comment.