diff --git a/src/Desktop/RodelAgent.UI/Controls/Chat/SystemInstructionPanel.xaml.cs b/src/Desktop/RodelAgent.UI/Controls/Chat/SystemInstructionPanel.xaml.cs index f3f3aae..957810c 100644 --- a/src/Desktop/RodelAgent.UI/Controls/Chat/SystemInstructionPanel.xaml.cs +++ b/src/Desktop/RodelAgent.UI/Controls/Chat/SystemInstructionPanel.xaml.cs @@ -38,7 +38,7 @@ private void OnSystemBoxTextChanged(object sender, TextChangedEventArgs e) } ViewModel.Data.SystemInstruction = SystemBox.Text; - ViewModel.CalcTotalTokenCountCommand.Execute(default); + ViewModel.ResetLastInputTimeCommand.Execute(default); _textChanged = true; } diff --git a/src/Desktop/RodelAgent.UI/ViewModels/Components/ChatSessionViewModel/ChatSessionViewModel.Messages.cs b/src/Desktop/RodelAgent.UI/ViewModels/Components/ChatSessionViewModel/ChatSessionViewModel.Messages.cs index 3b232f6..3a8bf59 100644 --- a/src/Desktop/RodelAgent.UI/ViewModels/Components/ChatSessionViewModel/ChatSessionViewModel.Messages.cs +++ b/src/Desktop/RodelAgent.UI/ViewModels/Components/ChatSessionViewModel/ChatSessionViewModel.Messages.cs @@ -306,4 +306,18 @@ await Task.Run(() => }); }); } + + [RelayCommand] + private void TryAutoCalcUserInputToken() + { + if (_lastInputTime is not null && DateTimeOffset.Now - _lastInputTime >= TimeSpan.FromSeconds(1)) + { + _lastInputTime = default; + CalcTotalTokenCountCommand.Execute(default); + } + } + + [RelayCommand] + private void ResetLastInputTime() + => _lastInputTime = DateTimeOffset.Now; } diff --git a/src/Desktop/RodelAgent.UI/ViewModels/Components/ChatSessionViewModel/ChatSessionViewModel.Properties.cs b/src/Desktop/RodelAgent.UI/ViewModels/Components/ChatSessionViewModel/ChatSessionViewModel.Properties.cs index c970e64..b492f0c 100644 --- a/src/Desktop/RodelAgent.UI/ViewModels/Components/ChatSessionViewModel/ChatSessionViewModel.Properties.cs +++ b/src/Desktop/RodelAgent.UI/ViewModels/Components/ChatSessionViewModel/ChatSessionViewModel.Properties.cs @@ -18,6 +18,7 @@ public sealed partial class ChatSessionViewModel private readonly IStorageService _storageService; private readonly ILogger _logger; private CancellationTokenSource _cancellationTokenSource; + private DateTimeOffset? _lastInputTime; private int _baseTokenCount; diff --git a/src/Desktop/RodelAgent.UI/ViewModels/Components/ChatSessionViewModel/ChatSessionViewModel.cs b/src/Desktop/RodelAgent.UI/ViewModels/Components/ChatSessionViewModel/ChatSessionViewModel.cs index eedd3a3..0bc23d0 100644 --- a/src/Desktop/RodelAgent.UI/ViewModels/Components/ChatSessionViewModel/ChatSessionViewModel.cs +++ b/src/Desktop/RodelAgent.UI/ViewModels/Components/ChatSessionViewModel/ChatSessionViewModel.cs @@ -74,10 +74,8 @@ private void Initialize(ChatSession data) /// 在进入视图开始显示时执行. /// [RelayCommand] - private async Task EnterViewAsync() - { - await CalcTotalTokenCountAsync(); - } + private void EnterView() + => CalcTotalTokenCountCommand.Execute(default); [RelayCommand] private void NewSession() @@ -251,5 +249,5 @@ partial void OnModelChanged(string value) => CheckCurrentModelStatus(); partial void OnUserInputChanged(string value) - => CalcUserInputTokenCountCommand.Execute(default); + => _lastInputTime = DateTimeOffset.Now; } diff --git a/src/Desktop/RodelAgent.UI/ViewModels/Pages/ChatServicePageViewModel/ChatServicePageViewModel.Properties.cs b/src/Desktop/RodelAgent.UI/ViewModels/Pages/ChatServicePageViewModel/ChatServicePageViewModel.Properties.cs index 77d40a9..5b08a6e 100644 --- a/src/Desktop/RodelAgent.UI/ViewModels/Pages/ChatServicePageViewModel/ChatServicePageViewModel.Properties.cs +++ b/src/Desktop/RodelAgent.UI/ViewModels/Pages/ChatServicePageViewModel/ChatServicePageViewModel.Properties.cs @@ -20,6 +20,7 @@ public sealed partial class ChatServicePageViewModel private readonly ILogger _logger; private readonly ChatPresetModuleViewModel _chatPresetModuleVM; private readonly GroupPresetModuleViewModel _groupPresetModuleVM; + private readonly DispatcherTimer? _tokenTimer; private bool _isPluginInitialized; [ObservableProperty] diff --git a/src/Desktop/RodelAgent.UI/ViewModels/Pages/ChatServicePageViewModel/ChatServicePageViewModel.Sessions.cs b/src/Desktop/RodelAgent.UI/ViewModels/Pages/ChatServicePageViewModel/ChatServicePageViewModel.Sessions.cs index 40bce45..008fc53 100644 --- a/src/Desktop/RodelAgent.UI/ViewModels/Pages/ChatServicePageViewModel/ChatServicePageViewModel.Sessions.cs +++ b/src/Desktop/RodelAgent.UI/ViewModels/Pages/ChatServicePageViewModel/ChatServicePageViewModel.Sessions.cs @@ -314,4 +314,7 @@ private void ExitGroupChat() CurrentGroup?.SaveSessionToDatabaseCommand.ExecuteAsync(default); CurrentGroup = default; } + + private void OnTokenTimerTick(object? sender, object e) + => CurrentSession?.TryAutoCalcUserInputTokenCommand.Execute(default); } diff --git a/src/Desktop/RodelAgent.UI/ViewModels/Pages/ChatServicePageViewModel/ChatServicePageViewModel.cs b/src/Desktop/RodelAgent.UI/ViewModels/Pages/ChatServicePageViewModel/ChatServicePageViewModel.cs index af04446..55eb2d7 100644 --- a/src/Desktop/RodelAgent.UI/ViewModels/Pages/ChatServicePageViewModel/ChatServicePageViewModel.cs +++ b/src/Desktop/RodelAgent.UI/ViewModels/Pages/ChatServicePageViewModel/ChatServicePageViewModel.cs @@ -50,6 +50,14 @@ public ChatServicePageViewModel( HistoryGroupSessions.CollectionChanged += OnHistorySessionsCountChanged; Plugins.CollectionChanged += OnPluginsCountChanged; CheckPluginsCount(); + + if (_tokenTimer is null) + { + _tokenTimer = new DispatcherTimer(); + _tokenTimer.Interval = TimeSpan.FromMilliseconds(400); + _tokenTimer.Tick += OnTokenTimerTick; + _tokenTimer.Start(); + } } ///