-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue 42: LmKitService requests handling refactoring (#44)
- Loading branch information
1 parent
03b8e23
commit 1ccd9c6
Showing
12 changed files
with
375 additions
and
360 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using LMKit.TextGeneration; | ||
using System.ComponentModel; | ||
|
||
namespace LMKit.Maestro.Services; | ||
|
||
public partial class LMKitService : INotifyPropertyChanged | ||
{ | ||
private sealed class LMKitRequest | ||
{ | ||
public ManualResetEvent CanBeExecutedSignal { get; } = new ManualResetEvent(false); | ||
public CancellationTokenSource CancellationTokenSource { get; } | ||
public TaskCompletionSource<LMKitResult> ResponseTask { get; } = new TaskCompletionSource<LMKitResult>(); | ||
public object? Parameters { get; } | ||
|
||
public LMKitRequestType RequestType { get; } | ||
|
||
public LMKitRequest(LMKitRequestType requestType, object? parameter, int requestTimeout) | ||
{ | ||
RequestType = requestType; | ||
Parameters = parameter; | ||
CancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(requestTimeout)); | ||
} | ||
|
||
public void CancelAndAwaitTermination() | ||
{ | ||
CancellationTokenSource.Cancel(); | ||
ResponseTask.Task.Wait(); | ||
} | ||
|
||
public enum LMKitRequestType | ||
{ | ||
Prompt, | ||
RegenerateResponse, | ||
GenerateTitle, | ||
Translate | ||
} | ||
|
||
public sealed class PromptRequestParameters | ||
{ | ||
public Conversation Conversation { get; set; } | ||
|
||
public string Prompt { get; set; } | ||
|
||
public PromptRequestParameters(Conversation conversation, string prompt) | ||
{ | ||
Conversation = conversation; | ||
Prompt = prompt; | ||
} | ||
} | ||
|
||
public sealed class TranslationRequestParameters | ||
{ | ||
public string InputText { get; set; } | ||
|
||
public Language Language { get; set; } | ||
|
||
public TranslationRequestParameters(string inputText, Language language) | ||
{ | ||
InputText = inputText; | ||
Language = language; | ||
} | ||
} | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
LM-Kit-Maestro/Services/LmKitService.RequestSchedule.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
using System.ComponentModel; | ||
using LMKit.TextGeneration; | ||
|
||
namespace LMKit.Maestro.Services; | ||
|
||
public partial class LMKitService : INotifyPropertyChanged | ||
{ | ||
private sealed class RequestSchedule | ||
{ | ||
private readonly object _locker = new object(); | ||
|
||
private List<LMKitRequest> _scheduledPrompts = new List<LMKitRequest>(); | ||
|
||
public int Count | ||
{ | ||
get | ||
{ | ||
lock (_locker) | ||
{ | ||
return _scheduledPrompts.Count; | ||
} | ||
} | ||
} | ||
|
||
public LMKitRequest? Next | ||
{ | ||
get | ||
{ | ||
lock (_locker) | ||
{ | ||
if (_scheduledPrompts.Count > 0) | ||
{ | ||
var scheduledPrompt = _scheduledPrompts[0]; | ||
|
||
return scheduledPrompt; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} | ||
|
||
public LMKitRequest? RunningPromptRequest { get; set; } | ||
|
||
public void Schedule(LMKitRequest promptRequest) | ||
{ | ||
lock (_locker) | ||
{ | ||
_scheduledPrompts.Add(promptRequest); | ||
|
||
if (Count == 1) | ||
{ | ||
promptRequest.CanBeExecutedSignal.Set(); | ||
} | ||
} | ||
} | ||
|
||
public bool Contains(LMKitRequest scheduledPrompt) | ||
{ | ||
lock (_locker) | ||
{ | ||
return _scheduledPrompts.Contains(scheduledPrompt); | ||
} | ||
} | ||
|
||
public void Remove(LMKitRequest scheduledPrompt) | ||
{ | ||
lock (_locker) | ||
{ | ||
HandleScheduledPromptRemoval(scheduledPrompt); | ||
} | ||
} | ||
|
||
public LMKitRequest? Unschedule(Conversation conversation) | ||
{ | ||
LMKitRequest? prompt = null; | ||
|
||
lock (_locker) | ||
{ | ||
foreach (var scheduledPrompt in _scheduledPrompts) | ||
{ | ||
if (scheduledPrompt.Parameters is LMKitRequest.PromptRequestParameters parameter && parameter.Conversation == conversation) | ||
{ | ||
prompt = scheduledPrompt; | ||
break; | ||
} | ||
} | ||
|
||
if (prompt != null) | ||
{ | ||
HandleScheduledPromptRemoval(prompt); | ||
} | ||
} | ||
|
||
return prompt; | ||
} | ||
|
||
private void HandleScheduledPromptRemoval(LMKitRequest scheduledPrompt) | ||
{ | ||
bool wasFirstInLine = scheduledPrompt == _scheduledPrompts[0]; | ||
|
||
_scheduledPrompts.Remove(scheduledPrompt); | ||
|
||
if (wasFirstInLine && Next != null) | ||
{ | ||
Next.CanBeExecutedSignal.Set(); | ||
} | ||
else | ||
{ | ||
scheduledPrompt.CanBeExecutedSignal.Set(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.