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

Fix saving #27

Merged
merged 1 commit into from
Nov 3, 2023
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
9 changes: 5 additions & 4 deletions ChatRPG/ChatRPG/Data/FileUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ public record MessagePair(string PlayerMessage, string AssistantMessage);

public class FileUtility
{
private readonly string _currentUser;
private readonly string _path;
private readonly string _saveDir;
private readonly string _filenamePrefix = "conversation";

// Define "special" keywords for determining the author of a message.
private readonly string _playerKeyword = "#<Player>: ";
private readonly string _gameKeyword = "#<Game>: ";

public FileUtility(string saveDir = "Saves/")
public FileUtility(string currentUser, string saveDir = "Saves/")
{
_currentUser = currentUser;
_saveDir = Path.Join(AppDomain.CurrentDomain.BaseDirectory, saveDir);
_path = SetPath(DateTime.Now);
}
Expand All @@ -38,7 +39,7 @@ public async Task UpdateSaveFileAsync(MessagePair messages)

public async Task<List<string>> GetMostRecentConversationAsync(string playerTag, string assistantTag)
{
string filePath = GetMostRecentFile(Directory.GetFiles(_saveDir, $"{_filenamePrefix}*"));
string filePath = GetMostRecentFile(Directory.GetFiles(_saveDir, $"{_currentUser}*"));
damniko marked this conversation as resolved.
Show resolved Hide resolved
return await GetConversationsStringFromSaveFileAsync(filePath, playerTag, assistantTag);
}

Expand Down Expand Up @@ -132,7 +133,7 @@ private List<string> ConvertConversationStringToList(string conversation, string
private string SetPath(DateTime timestamp)
{
// Add save directory and file name to path
string fileName = $"{_filenamePrefix} {timestamp:dd-MM-yyyy_HH-mm-ss}.txt";
string fileName = $"{_currentUser} {timestamp:dd-MM-yyyy_HH-mm-ss}.txt";
return Path.Join(_saveDir, fileName);
}
}
9 changes: 7 additions & 2 deletions ChatRPG/ChatRPG/Pages/Campaign.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
@inject IConfiguration Configuration
@inject IOpenAiLlmClient OpenAiLlmClient;
@inject IJSRuntime JsRuntime
@inject AuthenticationStateProvider AuthenticationStateProvider

<PageTitle>Campaign</PageTitle>

Expand Down Expand Up @@ -38,14 +39,18 @@
<span id="bottom-id"></span>

@code {
private string? _loggedInUsername;
private bool _shouldSave;
private IJSObjectReference? _scrollJsScript;
private readonly FileUtility _fileUtil = new FileUtility();
private FileUtility? _fileUtil;
readonly List<string> _conversation = new List<string>();
private string _userInput = "";

protected override async Task OnInitializedAsync()
{
AuthenticationState authenticationState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
_loggedInUsername = authenticationState.User.Identity?.Name;
if (_loggedInUsername != null) _fileUtil = new FileUtility(_loggedInUsername);
_shouldSave = Configuration.GetValue<bool>("SaveConversationsToFile");
_scrollJsScript = await JsRuntime.InvokeAsync<IJSObjectReference>("import", "./js/scroll.js");
}
Expand All @@ -66,7 +71,7 @@
_conversation.Add($"Player: {_userInput}");
string response = await ProcessUserInput(_userInput);
_conversation.Add($"Assistant: {response}");
if (_shouldSave)
if (_shouldSave && _fileUtil != null)
{
await _fileUtil.UpdateSaveFileAsync(new MessagePair(_userInput, response));
}
Expand Down