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

修复输入卡顿的问题 #76

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public sealed partial class ChatSessionViewModel
private readonly IStorageService _storageService;
private readonly ILogger<ChatSessionViewModel> _logger;
private CancellationTokenSource _cancellationTokenSource;
private DateTimeOffset? _lastInputTime;

private int _baseTokenCount;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,8 @@ private void Initialize(ChatSession data)
/// 在进入视图开始显示时执行.
/// </summary>
[RelayCommand]
private async Task EnterViewAsync()
{
await CalcTotalTokenCountAsync();
}
private void EnterView()
=> CalcTotalTokenCountCommand.Execute(default);

[RelayCommand]
private void NewSession()
Expand Down Expand Up @@ -251,5 +249,5 @@ partial void OnModelChanged(string value)
=> CheckCurrentModelStatus();

partial void OnUserInputChanged(string value)
=> CalcUserInputTokenCountCommand.Execute(default);
=> _lastInputTime = DateTimeOffset.Now;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public sealed partial class ChatServicePageViewModel
private readonly ILogger<ChatServicePageViewModel> _logger;
private readonly ChatPresetModuleViewModel _chatPresetModuleVM;
private readonly GroupPresetModuleViewModel _groupPresetModuleVM;
private readonly DispatcherTimer? _tokenTimer;
private bool _isPluginInitialized;

[ObservableProperty]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,7 @@ private void ExitGroupChat()
CurrentGroup?.SaveSessionToDatabaseCommand.ExecuteAsync(default);
CurrentGroup = default;
}

private void OnTokenTimerTick(object? sender, object e)
=> CurrentSession?.TryAutoCalcUserInputTokenCommand.Execute(default);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

/// <inheritdoc/>
Expand Down
Loading