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

Refactor PromptClientInput to accept string array for prompt #336

Merged
merged 2 commits into from
Aug 5, 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
16 changes: 16 additions & 0 deletions SharedLibraryCore/Helpers/ParsedInputResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections.Generic;

namespace SharedLibraryCore.Helpers;

public class ParsedInputResult<TResult>
{
public TResult? Result { get; set; }

Check warning on line 7 in SharedLibraryCore/Helpers/ParsedInputResult.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 7 in SharedLibraryCore/Helpers/ParsedInputResult.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 7 in SharedLibraryCore/Helpers/ParsedInputResult.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 7 in SharedLibraryCore/Helpers/ParsedInputResult.cs

View workflow job for this annotation

GitHub Actions / build_pack

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 7 in SharedLibraryCore/Helpers/ParsedInputResult.cs

View workflow job for this annotation

GitHub Actions / build_pack

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
public string? RawInput { get; set; }

Check warning on line 8 in SharedLibraryCore/Helpers/ParsedInputResult.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 8 in SharedLibraryCore/Helpers/ParsedInputResult.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 8 in SharedLibraryCore/Helpers/ParsedInputResult.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 8 in SharedLibraryCore/Helpers/ParsedInputResult.cs

View workflow job for this annotation

GitHub Actions / build_pack

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 8 in SharedLibraryCore/Helpers/ParsedInputResult.cs

View workflow job for this annotation

GitHub Actions / build_pack

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
public List<string> ErrorMessages { get; set; } = [];

public ParsedInputResult<TResult> WithError(string errorMessage)
{
ErrorMessages.Add(errorMessage);
return this;
}
}
29 changes: 17 additions & 12 deletions SharedLibraryCore/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1375,28 +1375,32 @@
public static void ExecuteAfterDelay(this Func<CancellationToken, Task> action, int delayMs,
CancellationToken token = default) => ExecuteAfterDelay(delayMs, action, token);

public static async Task<string> PromptClientInput(this EFClient client, string prompt, Func<string, Task<bool>> validator,
CancellationToken token = default)
public static async Task<ParsedInputResult<TResult>> PromptClientInput<TResult>(this EFClient client, string[] prompts,
Func<string, Task<ParsedInputResult<TResult>>> parser, string tokenExpiredMessage, CancellationToken token = default)
{
var clientResponse = new ManualResetEventSlim(false);
string response = null;
ParsedInputResult<TResult>? response = null;

Check warning on line 1382 in SharedLibraryCore/Utilities.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 1382 in SharedLibraryCore/Utilities.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 1382 in SharedLibraryCore/Utilities.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 1382 in SharedLibraryCore/Utilities.cs

View workflow job for this annotation

GitHub Actions / build_pack

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 1382 in SharedLibraryCore/Utilities.cs

View workflow job for this annotation

GitHub Actions / build_pack

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

try
{
IGameEventSubscriptions.ClientMessaged += OnResponse;
await client.TellAsync([prompt], token);
await client.TellAsync(prompts, token);

using var tokenSource = new CancellationTokenSource(DefaultCommandTimeout);
using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(tokenSource.Token, token);

clientResponse.Wait(linkedTokenSource.Token);
try
{
clientResponse.Wait(linkedTokenSource.Token);
}
catch (OperationCanceledException)
{
await client.TellAsync([tokenExpiredMessage], token);
return new ParsedInputResult<TResult> { ErrorMessages = [tokenExpiredMessage] };
}

return response;
}
catch (OperationCanceledException)
{
return null;
}
finally
{
IGameEventSubscriptions.ClientMessaged -= OnResponse;
Expand All @@ -1410,16 +1414,17 @@
return;
}

response = messageEvent.Message;
response = await parser(messageEvent.Message);
response.RawInput = messageEvent.Message;

if (await validator(response))
if (response.ErrorMessages.Count is 0)
{
// ReSharper disable once AccessToDisposedClosure
clientResponse.Set();
}
else
{
await client.TellAsync([prompt], cancellationToken);
await client.TellAsync(response.ErrorMessages.Concat(prompts), cancellationToken);
}
}
}
Expand Down
Loading