Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve parsing of responses from the AI #18220

Open
wants to merge 11 commits into
base: feature/llm
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions OpenConsole.sln
Original file line number Diff line number Diff line change
Expand Up @@ -2334,10 +2334,14 @@ Global
{6515F03F-E56D-4DB4-B23D-AC4FB80DB36F}.Release|x64.Build.0 = Release|x64
{6515F03F-E56D-4DB4-B23D-AC4FB80DB36F}.Release|x86.ActiveCfg = Release|Win32
{6515F03F-E56D-4DB4-B23D-AC4FB80DB36F}.Release|x86.Build.0 = Release|Win32
{7615F03F-E56D-4DB4-B23D-BD4FB80DB36F}.AuditMode|Any CPU.ActiveCfg = Debug|Win32
{7615F03F-E56D-4DB4-B23D-BD4FB80DB36F}.AuditMode|ARM64.ActiveCfg = Release|ARM64
{7615F03F-E56D-4DB4-B23D-BD4FB80DB36F}.AuditMode|x64.ActiveCfg = Release|x64
{7615F03F-E56D-4DB4-B23D-BD4FB80DB36F}.AuditMode|x86.ActiveCfg = Release|Win32
{7615F03F-E56D-4DB4-B23D-BD4FB80DB36F}.AuditMode|Any CPU.ActiveCfg = AuditMode|x64
{7615F03F-E56D-4DB4-B23D-BD4FB80DB36F}.AuditMode|Any CPU.Build.0 = AuditMode|x64
{7615F03F-E56D-4DB4-B23D-BD4FB80DB36F}.AuditMode|ARM64.ActiveCfg = AuditMode|ARM64
{7615F03F-E56D-4DB4-B23D-BD4FB80DB36F}.AuditMode|ARM64.Build.0 = AuditMode|ARM64
{7615F03F-E56D-4DB4-B23D-BD4FB80DB36F}.AuditMode|x64.ActiveCfg = AuditMode|x64
{7615F03F-E56D-4DB4-B23D-BD4FB80DB36F}.AuditMode|x64.Build.0 = AuditMode|x64
{7615F03F-E56D-4DB4-B23D-BD4FB80DB36F}.AuditMode|x86.ActiveCfg = AuditMode|Win32
{7615F03F-E56D-4DB4-B23D-BD4FB80DB36F}.AuditMode|x86.Build.0 = AuditMode|Win32
{7615F03F-E56D-4DB4-B23D-BD4FB80DB36F}.Debug|Any CPU.ActiveCfg = Debug|x64
{7615F03F-E56D-4DB4-B23D-BD4FB80DB36F}.Debug|ARM64.ActiveCfg = Debug|ARM64
{7615F03F-E56D-4DB4-B23D-BD4FB80DB36F}.Debug|ARM64.Build.0 = Debug|ARM64
Expand Down
96 changes: 57 additions & 39 deletions src/cascadia/QueryExtension/ExtensionPalette.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,48 +197,15 @@ namespace winrt::Microsoft::Terminal::Query::Extension::implementation

void ExtensionPalette::_splitResponseAndAddToChatHelper(const IResponse response)
{
// this function is dependent on the AI response separating code blocks with
// newlines and "```". OpenAI seems to naturally conform to this, though
// we could probably engineer the prompt to specify this if we need to.
std::wstringstream ss(response.Message().c_str());
std::wstring line;
std::wstring codeBlock;
bool inCodeBlock = false;
const auto time = _getCurrentLocalTimeHelper();
std::vector<IInspectable> messageParts;

while (std::getline(ss, line))
{
if (!line.empty())
{
if (!inCodeBlock && line.find(L"```") == 0)
{
inCodeBlock = true;
continue;
}
if (inCodeBlock && line.find(L"```") == 0)
{
inCodeBlock = false;
const auto chatMsg = winrt::make<ChatMessage>(winrt::hstring{ std::move(codeBlock) }, false, true);
messageParts.push_back(chatMsg);
codeBlock.clear();
continue;
}
if (inCodeBlock)
{
if (!codeBlock.empty())
{
codeBlock += L'\n';
}
codeBlock += line;
}
else
{
const auto chatMsg = winrt::make<ChatMessage>(winrt::hstring{ line }, false, false);
messageParts.push_back(chatMsg);
}
}
}
const auto chatMsg = winrt::make<ChatMessage>(winrt::to_hstring(response.Message()), false, false);
chatMsg.RunCommandClicked([this](auto&&, const auto commandlines) {
_InputSuggestionRequestedHandlers(*this, commandlines);
_close();
});
messageParts.push_back(chatMsg);

const auto brandingData = _lmProvider ? _lmProvider.BrandingData() : nullptr;
const auto responseAttribution = response.ResponseAttribution().empty() ? _ProfileName : response.ResponseAttribution();
Expand Down Expand Up @@ -489,4 +456,55 @@ namespace winrt::Microsoft::Terminal::Query::Extension::implementation
// Clear the text box each time we close the dialog. This is consistent with VsCode.
_queryBox().Text(winrt::hstring{});
}

ChatMessage::ChatMessage(winrt::hstring content, bool isQuery, bool isCode) :
_messageContent{ content },
_isQuery{ isQuery },
_isCode{ isCode }
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
{
_richBlock = Microsoft::Terminal::UI::Markdown::Builder::Convert(_messageContent, L"");
const auto resources = Application::Current().Resources();
if (_isQuery)
{
if (const auto textBrush = resources.Lookup(box_value(L"TextOnAccentFillColorPrimaryBrush")).try_as<Windows::UI::Xaml::Media::SolidColorBrush>())
{
_richBlock.Foreground(textBrush);
}
}
else
{
for (const auto& b : _richBlock.Blocks())
{
if (const auto& p{ b.try_as<Windows::UI::Xaml::Documents::Paragraph>() })
{
for (const auto& line : p.Inlines())
{
if (const auto& otherContent{ line.try_as<Windows::UI::Xaml::Documents::InlineUIContainer>() })
{
if (const auto& codeBlock{ otherContent.Child().try_as<Microsoft::Terminal::UI::Markdown::CodeBlock>() })
{
codeBlock.Margin({ 0, 8, 0, 8 });
codeBlock.PlayButtonVisibility(Windows::UI::Xaml::Visibility::Visible);
if (const auto backgroundBrush = resources.Lookup(box_value(L"ControlAltFillColorSecondaryBrush")).try_as<Windows::UI::Xaml::Media::SolidColorBrush>())
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
{
codeBlock.Background(backgroundBrush);
}
if (const auto foregroundBrush = resources.Lookup(box_value(L"AccentTextFillColorPrimaryBrush")).try_as<Windows::UI::Xaml::Media::SolidColorBrush>())
{
codeBlock.Foreground(foregroundBrush);
}
codeBlock.RequestRunCommands([this, commandlines = codeBlock.Commandlines()](auto&&, auto&&) {
_RunCommandClickedHandlers(*this, commandlines);
});
}
}
}
}
}
if (const auto textBrush = resources.Lookup(box_value(L"TextFillColorPrimaryBrush")).try_as<Windows::UI::Xaml::Media::SolidColorBrush>())
{
_richBlock.Foreground(textBrush);
}
}
}
}
9 changes: 5 additions & 4 deletions src/cascadia/QueryExtension/ExtensionPalette.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,20 @@ namespace winrt::Microsoft::Terminal::Query::Extension::implementation

struct ChatMessage : ChatMessageT<ChatMessage>
{
ChatMessage(winrt::hstring content, bool isQuery, bool isCode) :
_messageContent{ content },
_isQuery{ isQuery },
_isCode{ isCode } {}
ChatMessage(winrt::hstring content, bool isQuery, bool isCode);

bool IsQuery() const { return _isQuery; };
bool IsCode() const { return _isCode; };
winrt::hstring MessageContent() const { return _messageContent; };
winrt::Windows::UI::Xaml::Controls::RichTextBlock RichBlock() const { return _richBlock; };

TYPED_EVENT(RunCommandClicked, winrt::Microsoft::Terminal::Query::Extension::ChatMessage, winrt::hstring);

private:
bool _isQuery;
bool _isCode;
winrt::hstring _messageContent;
Windows::UI::Xaml::Controls::RichTextBlock _richBlock;
};

struct GroupedChatMessages : GroupedChatMessagesT<GroupedChatMessages>
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/QueryExtension/ExtensionPalette.idl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace Microsoft.Terminal.Query.Extension
String MessageContent { get; };
Boolean IsQuery { get; };
Boolean IsCode { get; };
Windows.UI.Xaml.Controls.RichTextBlock RichBlock { get; };
event Windows.Foundation.TypedEventHandler<ChatMessage, String> RunCommandClicked;
}

runtimeclass GroupedChatMessages : Windows.Foundation.Collections.IVector<IInspectable>
Expand Down
56 changes: 6 additions & 50 deletions src/cascadia/QueryExtension/ExtensionPalette.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<mux:StackLayout x:Name="VerticalStackLayout"
Orientation="Vertical"
Spacing="16" />
<DataTemplate x:Key="QueryMessageTemplate"
<DataTemplate x:Key="RichQueryMessageTemplate"
x:DataType="local:ChatMessage">
<Grid Height="Auto"
Margin="4"
Expand All @@ -62,14 +62,11 @@
BorderBrush="{ThemeResource MessageBorderBrush}"
BorderThickness="{ThemeResource MessageBorderThickness}"
CornerRadius="8">
<TextBlock FontSize="14"
Foreground="{ThemeResource TextOnAccentFillColorPrimaryBrush}"
Text="{x:Bind MessageContent}"
TextWrapping="WrapWholeWords" />
<ContentPresenter Content="{x:Bind RichBlock}" />
</StackPanel>
</Grid>
</DataTemplate>
<DataTemplate x:Key="TextResponseMessageTemplate"
<DataTemplate x:Key="RichResponseMessageTemplate"
x:DataType="local:ChatMessage">
<Grid Height="Auto"
Margin="4"
Expand All @@ -85,54 +82,13 @@
BorderBrush="{ThemeResource MessageBorderBrush}"
BorderThickness="{ThemeResource MessageBorderThickness}"
CornerRadius="8">
<TextBlock FontSize="14"
Foreground="{ThemeResource TextFillColorPrimaryBrush}"
IsTextSelectionEnabled="False"
Text="{x:Bind MessageContent}"
TextWrapping="WrapWholeWords" />
</StackPanel>
</Grid>
</DataTemplate>
<DataTemplate x:Key="CodeResponseMessageTemplate"
x:DataType="local:ChatMessage">
<Grid Height="Auto"
Margin="4"
HorizontalAlignment="Left">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="1"
Padding="16,8,16,8"
Background="{ThemeResource ControlAltFillColorQuarternaryBrush}"
BorderBrush="{ThemeResource MessageBorderBrush}"
BorderThickness="{ThemeResource MessageBorderThickness}"
CornerRadius="8">
<Border Padding="8,4,8,4"
Background="{ThemeResource ControlAltFillColorSecondaryBrush}"
CornerRadius="4">
<StackPanel Orientation="Horizontal"
Spacing="8">
<TextBlock MaxWidth="400"
FontFamily="Cascadia Code"
FontSize="12"
Foreground="{ThemeResource AccentTextFillColorPrimaryBrush}"
IsTextSelectionEnabled="False"
Text="{x:Bind MessageContent}"
TextWrapping="Wrap" />
<FontIcon VerticalAlignment="Center"
FontSize="16"
Foreground="{ThemeResource TextFillColorPrimaryBrush}"
Glyph="&#xE8C8;" />
</StackPanel>
</Border>
<ContentPresenter Content="{x:Bind RichBlock}" />
</StackPanel>
</Grid>
</DataTemplate>
<local:ExtensionPaletteMessageTemplateSelector x:Key="ChatMessageTemplateSelector"
CodeResponseMessageTemplate="{StaticResource CodeResponseMessageTemplate}"
QueryMessageTemplate="{StaticResource QueryMessageTemplate}"
TextResponseMessageTemplate="{StaticResource TextResponseMessageTemplate}" />
RichQueryMessageTemplate="{StaticResource RichQueryMessageTemplate}"
RichResponseMessageTemplate="{StaticResource RichResponseMessageTemplate}" />
<Style TargetType="ListViewHeaderItem">
<Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
<Setter Property="FontSize" Value="{ThemeResource ListViewHeaderItemThemeFontSize}" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,10 @@ namespace winrt::Microsoft::Terminal::Query::Extension::implementation
{
if (!message.IsQuery())
{
if (message.IsCode())
{
return CodeResponseMessageTemplate();
}
else
{
return TextResponseMessageTemplate();
}
return RichResponseMessageTemplate();
}
}
return QueryMessageTemplate();
return RichQueryMessageTemplate();
}

Windows::UI::Xaml::DataTemplate ExtensionPaletteGroupedMessagesHeaderTemplateSelector::SelectTemplateCore(const winrt::Windows::Foundation::IInspectable& item, const winrt::Windows::UI::Xaml::DependencyObject& /*container*/)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ namespace winrt::Microsoft::Terminal::Query::Extension::implementation
Windows::UI::Xaml::DataTemplate SelectTemplateCore(const winrt::Windows::Foundation::IInspectable&, const winrt::Windows::UI::Xaml::DependencyObject&);
Windows::UI::Xaml::DataTemplate SelectTemplateCore(const winrt::Windows::Foundation::IInspectable&);

WINRT_PROPERTY(winrt::Windows::UI::Xaml::DataTemplate, QueryMessageTemplate);
WINRT_PROPERTY(winrt::Windows::UI::Xaml::DataTemplate, TextResponseMessageTemplate);
WINRT_PROPERTY(winrt::Windows::UI::Xaml::DataTemplate, CodeResponseMessageTemplate);
WINRT_PROPERTY(winrt::Windows::UI::Xaml::DataTemplate, RichQueryMessageTemplate);
WINRT_PROPERTY(winrt::Windows::UI::Xaml::DataTemplate, RichResponseMessageTemplate);
};

struct ExtensionPaletteGroupedMessagesHeaderTemplateSelector : ExtensionPaletteGroupedMessagesHeaderTemplateSelectorT<ExtensionPaletteGroupedMessagesHeaderTemplateSelector>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ namespace Microsoft.Terminal.Query.Extension
{
ExtensionPaletteMessageTemplateSelector();

Windows.UI.Xaml.DataTemplate QueryMessageTemplate;
Windows.UI.Xaml.DataTemplate TextResponseMessageTemplate;
Windows.UI.Xaml.DataTemplate CodeResponseMessageTemplate;
Windows.UI.Xaml.DataTemplate RichQueryMessageTemplate;
Windows.UI.Xaml.DataTemplate RichResponseMessageTemplate;
}

[default_interface] runtimeclass ExtensionPaletteGroupedMessagesHeaderTemplateSelector : Windows.UI.Xaml.Controls.DataTemplateSelector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
<ProjectReference Include="$(OpenConsoleDir)src\cascadia\TerminalSettingsModel\dll\Microsoft.Terminal.Settings.Model.vcxproj">
<Private>false</Private>
</ProjectReference>
<ProjectReference Include="$(OpenConsoleDir)src\cascadia\UIMarkdown\UIMarkdown.vcxproj" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/QueryExtension/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ TRACELOGGING_DECLARE_PROVIDER(g_hQueryExtensionProvider);

#include <winrt/Microsoft.Terminal.Settings.Model.h>
#include <winrt/Microsoft.Terminal.UI.h>
#include <winrt/Microsoft.Terminal.UI.Markdown.h>

#include <winrt/Windows.Web.Http.h>
#include <winrt/Windows.Web.Http.Headers.h>
Expand Down
Loading