From cc3e18040476e9a99304ade9944449274ee7a660 Mon Sep 17 00:00:00 2001 From: Richasy Date: Sun, 25 May 2025 15:18:30 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor:=20=E6=8E=A7?= =?UTF-8?q?=E4=BB=B6=E5=8D=B8=E8=BD=BD=E5=90=8E=E6=89=8B=E5=8A=A8=E9=87=8D?= =?UTF-8?q?=E7=BD=AE=E6=95=B0=E6=8D=AE=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controls/Audio/AudioSideBody.xaml | 2 +- .../Controls/Audio/AudioSideBody.xaml.cs | 3 +++ .../Controls/Chat/ChatGroupConfigPanel.xaml | 2 +- .../Chat/ChatGroupConfigPanel.xaml.cs | 3 +++ .../Controls/Chat/ChatHistoryPanel.xaml | 5 ++++- .../Controls/Chat/ChatHistoryPanel.xaml.cs | 3 +++ .../Controls/Chat/ChatSessionMainFooter.xaml | 10 ++++++++-- .../Chat/ChatSessionMainFooter.xaml.cs | 2 ++ .../Controls/Chat/ChatSideBody.xaml | 20 +++++++++++++++---- .../Controls/Chat/ChatSideBody.xaml.cs | 8 ++++++++ .../Controls/Chat/GroupAgentsPanel.xaml | 5 ++++- .../Controls/Chat/GroupAgentsPanel.xaml.cs | 3 +++ .../Controls/Chat/McpConfigDialog.xaml | 2 +- .../Controls/Chat/McpConfigDialog.xaml.cs | 12 ++++++++++- .../Controls/Chat/McpServerItemControl.xaml | 5 ++++- .../Chat/McpServerItemControl.xaml.cs | 2 ++ .../Controls/Draw/DrawSideBody.xaml | 2 +- .../Controls/Draw/DrawSideBody.xaml.cs | 3 +++ .../RodelAgent.UI/Controls/EmojiPanel.xaml.cs | 11 +++++++--- .../Settings/AboutSettingControl.xaml | 2 ++ .../Settings/AboutSettingControl.xaml.cs | 6 ++++++ .../RodelAgent.UI/Pages/ChatPage.xaml.cs | 6 +----- .../ChatSessionViewModel.Interop.cs | 5 +++++ .../ChatSessionViewModel.cs | 6 +----- src/Directory.Packages.props | 10 +++++----- 25 files changed, 106 insertions(+), 32 deletions(-) diff --git a/src/Desktop/RodelAgent.UI/Controls/Audio/AudioSideBody.xaml b/src/Desktop/RodelAgent.UI/Controls/Audio/AudioSideBody.xaml index 48d0d52b..adf7b787 100644 --- a/src/Desktop/RodelAgent.UI/Controls/Audio/AudioSideBody.xaml +++ b/src/Desktop/RodelAgent.UI/Controls/Audio/AudioSideBody.xaml @@ -19,7 +19,7 @@ Style="{StaticResource PageScrollViewerStyle}" Visibility="{x:Bind ViewModel.IsHistoryEmpty, Mode=OneWay, Converter={StaticResource BoolToVisibilityReverseConverter}}"> - + diff --git a/src/Desktop/RodelAgent.UI/Controls/Audio/AudioSideBody.xaml.cs b/src/Desktop/RodelAgent.UI/Controls/Audio/AudioSideBody.xaml.cs index 8506666b..2f90a835 100644 --- a/src/Desktop/RodelAgent.UI/Controls/Audio/AudioSideBody.xaml.cs +++ b/src/Desktop/RodelAgent.UI/Controls/Audio/AudioSideBody.xaml.cs @@ -11,4 +11,7 @@ public sealed partial class AudioSideBody : AudioPageControlBase /// Initializes a new instance of the class. /// public AudioSideBody() => InitializeComponent(); + + protected override void OnControlUnloaded() + => HistoryRepeater.ItemsSource = null; } diff --git a/src/Desktop/RodelAgent.UI/Controls/Chat/ChatGroupConfigPanel.xaml b/src/Desktop/RodelAgent.UI/Controls/Chat/ChatGroupConfigPanel.xaml index adaea7d6..16c2d39c 100644 --- a/src/Desktop/RodelAgent.UI/Controls/Chat/ChatGroupConfigPanel.xaml +++ b/src/Desktop/RodelAgent.UI/Controls/Chat/ChatGroupConfigPanel.xaml @@ -210,7 +210,7 @@ - + AgentRepeater.ItemsSource = null; + private string GetEmoji() => _selectedEmoji?.Unicode ?? string.Empty; diff --git a/src/Desktop/RodelAgent.UI/Controls/Chat/ChatHistoryPanel.xaml b/src/Desktop/RodelAgent.UI/Controls/Chat/ChatHistoryPanel.xaml index 93245102..5c1702d8 100644 --- a/src/Desktop/RodelAgent.UI/Controls/Chat/ChatHistoryPanel.xaml +++ b/src/Desktop/RodelAgent.UI/Controls/Chat/ChatHistoryPanel.xaml @@ -49,7 +49,10 @@ Grid.Row="1" Padding="8,0" Style="{StaticResource PageScrollViewerStyle}"> - + diff --git a/src/Desktop/RodelAgent.UI/Controls/Chat/ChatHistoryPanel.xaml.cs b/src/Desktop/RodelAgent.UI/Controls/Chat/ChatHistoryPanel.xaml.cs index 5fad8f75..3e6a8f37 100644 --- a/src/Desktop/RodelAgent.UI/Controls/Chat/ChatHistoryPanel.xaml.cs +++ b/src/Desktop/RodelAgent.UI/Controls/Chat/ChatHistoryPanel.xaml.cs @@ -11,4 +11,7 @@ public sealed partial class ChatHistoryPanel : ChatSessionControlBase /// Initializes a new instance of the class. /// public ChatHistoryPanel() => InitializeComponent(); + + protected override void OnControlUnloaded() + => HistoryRepeater.ItemsSource = null; } diff --git a/src/Desktop/RodelAgent.UI/Controls/Chat/ChatSessionMainFooter.xaml b/src/Desktop/RodelAgent.UI/Controls/Chat/ChatSessionMainFooter.xaml index d933904e..b3628d01 100644 --- a/src/Desktop/RodelAgent.UI/Controls/Chat/ChatSessionMainFooter.xaml +++ b/src/Desktop/RodelAgent.UI/Controls/Chat/ChatSessionMainFooter.xaml @@ -46,7 +46,10 @@ MaxHeight="400" Padding="12,8" Style="{StaticResource PageScrollViewerStyle}"> - + @@ -89,7 +92,10 @@ Padding="12,0" Style="{StaticResource PageScrollViewerStyle}" Visibility="{x:Bind ViewModel.IsServerEmpty, Mode=OneWay, Converter={StaticResource BoolToVisibilityReverseConverter}}"> - + diff --git a/src/Desktop/RodelAgent.UI/Controls/Chat/ChatSessionMainFooter.xaml.cs b/src/Desktop/RodelAgent.UI/Controls/Chat/ChatSessionMainFooter.xaml.cs index 9e453894..d1d70d09 100644 --- a/src/Desktop/RodelAgent.UI/Controls/Chat/ChatSessionMainFooter.xaml.cs +++ b/src/Desktop/RodelAgent.UI/Controls/Chat/ChatSessionMainFooter.xaml.cs @@ -32,6 +32,8 @@ protected override void OnControlLoaded() /// protected override void OnControlUnloaded() { + ModelRepeater.ItemsSource = null; + ServerRepeater.ItemsSource = null; ViewModel.RequestFocusInput -= OnRequestFocusInput; ViewModel.RequestCloseFlyout -= OnRequestCloseFlyout; } diff --git a/src/Desktop/RodelAgent.UI/Controls/Chat/ChatSideBody.xaml b/src/Desktop/RodelAgent.UI/Controls/Chat/ChatSideBody.xaml index 631acfd5..a95af23c 100644 --- a/src/Desktop/RodelAgent.UI/Controls/Chat/ChatSideBody.xaml +++ b/src/Desktop/RodelAgent.UI/Controls/Chat/ChatSideBody.xaml @@ -29,7 +29,10 @@ - + @@ -42,7 +45,10 @@ - + @@ -76,7 +82,10 @@ - + @@ -95,7 +104,10 @@ Style="{StaticResource PageScrollViewerStyle}" Visibility="{x:Bind ViewModel.IsServerEmpty, Mode=OneWay, Converter={StaticResource BoolToVisibilityReverseConverter}}"> - + diff --git a/src/Desktop/RodelAgent.UI/Controls/Chat/ChatSideBody.xaml.cs b/src/Desktop/RodelAgent.UI/Controls/Chat/ChatSideBody.xaml.cs index 09fdd9a9..150d6a04 100644 --- a/src/Desktop/RodelAgent.UI/Controls/Chat/ChatSideBody.xaml.cs +++ b/src/Desktop/RodelAgent.UI/Controls/Chat/ChatSideBody.xaml.cs @@ -11,6 +11,14 @@ public sealed partial class ChatSideBody : ChatPageControlBase { public ChatSideBody() => InitializeComponent(); + protected override void OnControlUnloaded() + { + ServiceRepeater.ItemsSource = null; + AgentRepeater.ItemsSource = null; + GroupRepeater.ItemsSource = null; + ServerRepeater.ItemsSource = null; + } + private void OnAgentItemClick(object sender, EventArgs e) { var data = (sender as ChatAgentItemControl)?.ViewModel; diff --git a/src/Desktop/RodelAgent.UI/Controls/Chat/GroupAgentsPanel.xaml b/src/Desktop/RodelAgent.UI/Controls/Chat/GroupAgentsPanel.xaml index d00f8b6c..e659e44e 100644 --- a/src/Desktop/RodelAgent.UI/Controls/Chat/GroupAgentsPanel.xaml +++ b/src/Desktop/RodelAgent.UI/Controls/Chat/GroupAgentsPanel.xaml @@ -11,7 +11,10 @@ - + diff --git a/src/Desktop/RodelAgent.UI/Controls/Chat/GroupAgentsPanel.xaml.cs b/src/Desktop/RodelAgent.UI/Controls/Chat/GroupAgentsPanel.xaml.cs index e0c6f926..b59e1afe 100644 --- a/src/Desktop/RodelAgent.UI/Controls/Chat/GroupAgentsPanel.xaml.cs +++ b/src/Desktop/RodelAgent.UI/Controls/Chat/GroupAgentsPanel.xaml.cs @@ -11,4 +11,7 @@ public sealed partial class GroupAgentsPanel : ChatSessionControlBase /// Initializes a new instance of the class. /// public GroupAgentsPanel() => InitializeComponent(); + + protected override void OnControlUnloaded() + => AgentRepeater.ItemsSource = null; } \ No newline at end of file diff --git a/src/Desktop/RodelAgent.UI/Controls/Chat/McpConfigDialog.xaml b/src/Desktop/RodelAgent.UI/Controls/Chat/McpConfigDialog.xaml index d1156e0d..36f1a8bb 100644 --- a/src/Desktop/RodelAgent.UI/Controls/Chat/McpConfigDialog.xaml +++ b/src/Desktop/RodelAgent.UI/Controls/Chat/McpConfigDialog.xaml @@ -121,7 +121,7 @@ Style="{StaticResource PageScrollViewerStyle}" VerticalScrollBarVisibility="Hidden" Visibility="Collapsed"> - + diff --git a/src/Desktop/RodelAgent.UI/Controls/Chat/McpConfigDialog.xaml.cs b/src/Desktop/RodelAgent.UI/Controls/Chat/McpConfigDialog.xaml.cs index ac88bcc4..753a3b18 100644 --- a/src/Desktop/RodelAgent.UI/Controls/Chat/McpConfigDialog.xaml.cs +++ b/src/Desktop/RodelAgent.UI/Controls/Chat/McpConfigDialog.xaml.cs @@ -14,7 +14,11 @@ public sealed partial class McpConfigDialog : AppDialog private readonly McpServerItemViewModel? _source; internal ObservableCollection Variables { get; } = []; - public McpConfigDialog() => InitializeComponent(); + public McpConfigDialog() + { + InitializeComponent(); + Unloaded += OnUnloaded; + } public McpConfigDialog(McpServerItemViewModel vm) : this() @@ -28,6 +32,12 @@ public McpConfigDialog(McpServerItemViewModel vm) CheckVariablesVisibility(); } + private void OnUnloaded(object sender, RoutedEventArgs e) + { + VariableRepeater.ItemsSource = null; + Unloaded -= OnUnloaded; + } + private void OnPrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args) { var name = NameBox.Text; diff --git a/src/Desktop/RodelAgent.UI/Controls/Chat/McpServerItemControl.xaml b/src/Desktop/RodelAgent.UI/Controls/Chat/McpServerItemControl.xaml index 70b35774..fd48678b 100644 --- a/src/Desktop/RodelAgent.UI/Controls/Chat/McpServerItemControl.xaml +++ b/src/Desktop/RodelAgent.UI/Controls/Chat/McpServerItemControl.xaml @@ -109,7 +109,10 @@ HorizontalAlignment="Left" Text="{ext:Locale Name=Methods}" /> - + diff --git a/src/Desktop/RodelAgent.UI/Controls/Chat/McpServerItemControl.xaml.cs b/src/Desktop/RodelAgent.UI/Controls/Chat/McpServerItemControl.xaml.cs index 59efa41c..e86944d9 100644 --- a/src/Desktop/RodelAgent.UI/Controls/Chat/McpServerItemControl.xaml.cs +++ b/src/Desktop/RodelAgent.UI/Controls/Chat/McpServerItemControl.xaml.cs @@ -23,6 +23,8 @@ protected override void OnControlUnloaded() { ViewModel.PropertyChanged += OnViewModelPropertyChanged; } + + FunctionRepeater.ItemsSource = null; } private void OnViewModelPropertyChanged(object? sender, PropertyChangedEventArgs e) diff --git a/src/Desktop/RodelAgent.UI/Controls/Draw/DrawSideBody.xaml b/src/Desktop/RodelAgent.UI/Controls/Draw/DrawSideBody.xaml index a5dafff9..f0a00b7d 100644 --- a/src/Desktop/RodelAgent.UI/Controls/Draw/DrawSideBody.xaml +++ b/src/Desktop/RodelAgent.UI/Controls/Draw/DrawSideBody.xaml @@ -19,7 +19,7 @@ Style="{StaticResource PageScrollViewerStyle}" Visibility="{x:Bind ViewModel.IsHistoryEmpty, Mode=OneWay, Converter={StaticResource BoolToVisibilityReverseConverter}}"> - + diff --git a/src/Desktop/RodelAgent.UI/Controls/Draw/DrawSideBody.xaml.cs b/src/Desktop/RodelAgent.UI/Controls/Draw/DrawSideBody.xaml.cs index 3487c3d9..c9ed8578 100644 --- a/src/Desktop/RodelAgent.UI/Controls/Draw/DrawSideBody.xaml.cs +++ b/src/Desktop/RodelAgent.UI/Controls/Draw/DrawSideBody.xaml.cs @@ -11,4 +11,7 @@ public sealed partial class DrawSideBody : DrawPageControlBase /// Initializes a new instance of the class. /// public DrawSideBody() => InitializeComponent(); + + protected override void OnControlUnloaded() + => HistoryRepeater.ItemsSource = null; } diff --git a/src/Desktop/RodelAgent.UI/Controls/EmojiPanel.xaml.cs b/src/Desktop/RodelAgent.UI/Controls/EmojiPanel.xaml.cs index e61c7cbc..bef04273 100644 --- a/src/Desktop/RodelAgent.UI/Controls/EmojiPanel.xaml.cs +++ b/src/Desktop/RodelAgent.UI/Controls/EmojiPanel.xaml.cs @@ -9,7 +9,7 @@ namespace RodelAgent.UI.Controls; /// public sealed partial class EmojiPanel : LayoutUserControlBase { - private readonly ObservableCollection _items = new(); + private readonly ObservableCollection _items = []; /// /// Initializes a new instance of the class. @@ -28,14 +28,19 @@ protected override void OnControlLoaded() { if (_items.Count == 0) { - var emojis = EmojiStatics.GetEmojis().OrderBy(p => p.Group).ThenBy(p => p.Unicode); - foreach (var emoji in emojis) + foreach (var emoji in EmojiStatics.GetEmojis().OrderBy(p => p.Group).ThenBy(p => p.Unicode)) { _items.Add(emoji); } } } + protected override void OnControlUnloaded() + { + EmojiRepeater.ItemsSource = null; + _items.Clear(); + } + private void OnEmojiButtonClick(object sender, RoutedEventArgs e) { var context = (sender as Button)?.DataContext as EmojiItem; diff --git a/src/Desktop/RodelAgent.UI/Controls/Settings/AboutSettingControl.xaml b/src/Desktop/RodelAgent.UI/Controls/Settings/AboutSettingControl.xaml index 20c8bad7..e5977fc1 100644 --- a/src/Desktop/RodelAgent.UI/Controls/Settings/AboutSettingControl.xaml +++ b/src/Desktop/RodelAgent.UI/Controls/Settings/AboutSettingControl.xaml @@ -37,6 +37,7 @@ @@ -55,6 +56,7 @@ diff --git a/src/Desktop/RodelAgent.UI/Controls/Settings/AboutSettingControl.xaml.cs b/src/Desktop/RodelAgent.UI/Controls/Settings/AboutSettingControl.xaml.cs index 87cd3609..dee49c17 100644 --- a/src/Desktop/RodelAgent.UI/Controls/Settings/AboutSettingControl.xaml.cs +++ b/src/Desktop/RodelAgent.UI/Controls/Settings/AboutSettingControl.xaml.cs @@ -11,4 +11,10 @@ public sealed partial class AboutSettingControl : SettingsPageControlBase /// Initializes a new instance of the class. /// public AboutSettingControl() => InitializeComponent(); + + protected override void OnControlUnloaded() + { + LinkRepeater.ItemsSource = null; + LibraryRepeater.ItemsSource = null; + } } diff --git a/src/Desktop/RodelAgent.UI/Pages/ChatPage.xaml.cs b/src/Desktop/RodelAgent.UI/Pages/ChatPage.xaml.cs index 771b7ac6..782c7ad8 100644 --- a/src/Desktop/RodelAgent.UI/Pages/ChatPage.xaml.cs +++ b/src/Desktop/RodelAgent.UI/Pages/ChatPage.xaml.cs @@ -12,11 +12,7 @@ public sealed partial class ChatPage : ChatPageBase /// /// Initializes a new instance of the class. /// - public ChatPage() - { - InitializeComponent(); - NavigationCacheMode = NavigationCacheMode.Required; - } + public ChatPage() => InitializeComponent(); /// protected override void OnPageLoaded() diff --git a/src/Desktop/RodelAgent.UI/ViewModels/Core/ChatSessionViewModel/ChatSessionViewModel.Interop.cs b/src/Desktop/RodelAgent.UI/ViewModels/Core/ChatSessionViewModel/ChatSessionViewModel.Interop.cs index 03e76dd5..d04bc4b0 100644 --- a/src/Desktop/RodelAgent.UI/ViewModels/Core/ChatSessionViewModel/ChatSessionViewModel.Interop.cs +++ b/src/Desktop/RodelAgent.UI/ViewModels/Core/ChatSessionViewModel/ChatSessionViewModel.Interop.cs @@ -239,6 +239,11 @@ private async void OnWebMessageReceived(CoreWebView2 sender, CoreWebView2WebMess ReloadWebThemeCommand.Execute(default); SetInteropResourcesCommand.Execute(default); + if (_currentConversation != null) + { + SetCurrentConversation(_currentConversation); + SetInitialInteropHistoryCommand.Execute(_currentConversation?.History ?? []); + } } } else if (msg.StartsWith("data:", StringComparison.OrdinalIgnoreCase)) diff --git a/src/Desktop/RodelAgent.UI/ViewModels/Core/ChatSessionViewModel/ChatSessionViewModel.cs b/src/Desktop/RodelAgent.UI/ViewModels/Core/ChatSessionViewModel/ChatSessionViewModel.cs index 0579fe79..2fd4a973 100644 --- a/src/Desktop/RodelAgent.UI/ViewModels/Core/ChatSessionViewModel/ChatSessionViewModel.cs +++ b/src/Desktop/RodelAgent.UI/ViewModels/Core/ChatSessionViewModel/ChatSessionViewModel.cs @@ -65,11 +65,6 @@ public void InjectFunc(Func? maxRounds) public async Task InitializeAsync(WebView2 view) { - if (_webView is not null) - { - return; - } - if (_tokenTimer is null) { _tokenTimer = new DispatcherTimer @@ -79,6 +74,7 @@ public async Task InitializeAsync(WebView2 view) _tokenTimer.Tick += OnTokenTimerTick; } + _webView?.Close(); _webView = view; IsWebInitializing = true; IsWebInitialized = false; diff --git a/src/Directory.Packages.props b/src/Directory.Packages.props index 640e1a4d..16de0a6e 100644 --- a/src/Directory.Packages.props +++ b/src/Directory.Packages.props @@ -55,15 +55,15 @@ - + - - + + @@ -86,8 +86,8 @@ - - + +