Skip to content

Commit

Permalink
Auto Inmport
Browse files Browse the repository at this point in the history
  • Loading branch information
Jun-Murakami committed Jan 3, 2024
1 parent 44691e7 commit e0714d5
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Assets/lang/en-US.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ Please check your mailbox. Once your address is verified, you can log in.</syste
After logging in with your browser, please set a new password from the menu.</system:String>
<system:String x:Key="My.Strings.PasswordChanged">Your password has been changed.</system:String>
<system:String x:Key="My.Strings.EmailLoginFailed">Login failed. Please check your email address and password.</system:String>
<system:String x:Key="My.Strings.OverWriteLog">A chat log with the same name exists.
<system:String x:Key="My.Strings.OverWriteLog">A chat log of the same name with different content exists.
Do you want to overwrite it?</system:String>
</ResourceDictionary>
2 changes: 1 addition & 1 deletion Assets/lang/ja-JP.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@
メール記載のリンクをクリックしてブラウザでログインし、メニューから新しいパスワードに変更してください。</system:String>
<system:String x:Key="My.Strings.PasswordChanged">パスワードが変更されました。</system:String>
<system:String x:Key="My.Strings.EmailLoginFailed">ログインに失敗しました。メールアドレスとパスワードを確認してもう一度お試しください。</system:String>
<system:String x:Key="My.Strings.OverWriteLog">同名のチャットログが存在します。上書きしますか?</system:String>
<system:String x:Key="My.Strings.OverWriteLog">内容の異なる同名のチャットログが存在します。上書きしますか?</system:String>
</ResourceDictionary>
2 changes: 2 additions & 0 deletions src/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public bool SyncIsOn
}
}
}
public bool IsAutoImporting { get; set; }
public string? Provider { get; set; }
public string? Email { get; set; }
public string? Password { get; set; }
Expand Down Expand Up @@ -99,6 +100,7 @@ public AppSettings()
SeparatorMode = 5;
Session = null;
SyncIsOn = false;
IsAutoImporting = false;
Provider = "";
Email = "";
Password = "";
Expand Down
154 changes: 124 additions & 30 deletions src/Models/DatabaseProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System.Text.RegularExpressions;
using static TmCGPTD.Models.PostgreSqlModels;
using static Postgrest.Constants;
using Avalonia.Threading;

namespace TmCGPTD.Models
{
Expand Down Expand Up @@ -849,14 +850,17 @@ await SupabaseStates.Instance.Supabase.From<ChatRoom>()
await command.ExecuteNonQueryAsync();
}

//削除履歴を追加
using (var command = new SqliteCommand("INSERT INTO management (user_id, delete_table, delete_id, date) VALUES (@user_id, @delete_table, @delete_id, @date)", connection, (SqliteTransaction)transaction))
if (AppSettings.Instance.SyncIsOn && SupabaseStates.Instance.Supabase != null && Uid != null)
{
command.Parameters.AddWithValue("@user_id", Uid);
command.Parameters.AddWithValue("@delete_table", "chatlog");
command.Parameters.AddWithValue("@delete_id", chatId);
command.Parameters.AddWithValue("@date", date.ToString("s"));
await command.ExecuteNonQueryAsync();
//削除履歴を追加
using (var command = new SqliteCommand("INSERT INTO management (user_id, delete_table, delete_id, date) VALUES (@user_id, @delete_table, @delete_id, @date)", connection, (SqliteTransaction)transaction))
{
command.Parameters.AddWithValue("@user_id", Uid);
command.Parameters.AddWithValue("@delete_table", "chatlog");
command.Parameters.AddWithValue("@delete_id", chatId);
command.Parameters.AddWithValue("@date", date.ToString("s"));
await command.ExecuteNonQueryAsync();
}
}

await transaction.CommitAsync();
Expand Down Expand Up @@ -950,16 +954,23 @@ public async Task UpdateCategoryDatabaseAsync(long chatId, string category)
}

// Webチャットログのインポート--------------------------------------------------------------

public async Task<string> InsertWebChatLogDatabaseAsync(string webChatTitle, List<Dictionary<string, object>> webConversationHistory, string webLog, string chatService)
{
if (string.IsNullOrEmpty(webLog))
{
return "No chat log found.";
}

if (VMLocator.WebChatViewModel.IsImportRunning)
{
return "Through";
}

int? matchingId = null;
string query = "";

// 既存のチャットログとタイトルが一致するものがあるか検索
using (var command = new SqliteCommand { Connection = memoryConnection })
{
command.CommandText = "SELECT id FROM chatlog WHERE title = @webChatTitle LIMIT 1";
Expand All @@ -979,10 +990,57 @@ public async Task<string> InsertWebChatLogDatabaseAsync(string webChatTitle, Lis

if (matchingId.HasValue)
{
// 既存のチャットログとタイトルが一致するテキストログをデータベースから取得
string webLogFromDb;
using (var command = new SqliteCommand { Connection = memoryConnection })
{
command.CommandText = "SELECT text FROM chatlog WHERE id = @matchingId LIMIT 1";
command.Parameters.AddWithValue("@matchingId", matchingId.Value);

using var reader = await command.ExecuteReaderAsync();
if (await reader.ReadAsync())
{
webLogFromDb = reader.GetString(0);
}
else
{
return "No matching chat log found.";
}
}

// 既存のチャットログとタイトルが一致するテキストログがWebチャットログのテキストに含まれているか検索
bool isWebLogFromDbInWebLog = webLog.Contains(webLogFromDb);

Application.Current!.TryFindResource("My.Strings.OverWriteLog", out object? resource1);
var dialog = new ContentDialog() { Title = $"{resource1}{Environment.NewLine}{Environment.NewLine}'{webChatTitle}'", PrimaryButtonText = "Overwrite", SecondaryButtonText = "Rename", CloseButtonText = "Cancel" };
var dialogResult = await VMLocator.MainViewModel.ContentDialogShowAsync(dialog);
if (dialogResult == ContentDialogResult.Primary)

var dialogResult = ContentDialogResult.None;

if (!isWebLogFromDbInWebLog)
{
VMLocator.WebChatViewModel.IsImportRunning = true;

if (Dispatcher.UIThread.CheckAccess())
{
var dialog = new ContentDialog() { Title = $"{resource1}{Environment.NewLine}{Environment.NewLine}'{webChatTitle}'", PrimaryButtonText = "Overwrite", SecondaryButtonText = "Rename", CloseButtonText = "Cancel" };
dialogResult = await VMLocator.MainViewModel.ContentDialogShowAsync(dialog);
}
else
{
await Dispatcher.UIThread.InvokeAsync(async () =>
{
var dialog = new ContentDialog() { Title = $"{resource1}{Environment.NewLine}{Environment.NewLine}'{webChatTitle}'", PrimaryButtonText = "Overwrite", SecondaryButtonText = "Rename", CloseButtonText = "Cancel" };
dialogResult = await VMLocator.MainViewModel.ContentDialogShowAsync(dialog);
});
}

VMLocator.WebChatViewModel.IsImportRunning = false;
}

if (isWebLogFromDbInWebLog && webLog == webLogFromDb)
{
return "Through";
}
else if (!VMLocator.WebChatViewModel.IsImportRunning && (isWebLogFromDbInWebLog || dialogResult == ContentDialogResult.Primary))
{
if (AppSettings.Instance.SyncIsOn && SupabaseStates.Instance.Supabase != null && Uid != null)
{
Expand All @@ -1003,32 +1061,68 @@ public async Task<string> InsertWebChatLogDatabaseAsync(string webChatTitle, Lis
}
else if (dialogResult == ContentDialogResult.Secondary)
{
dialog = new ContentDialog()
if (Dispatcher.UIThread.CheckAccess())
{
Title = "Please enter a new chat name.",
PrimaryButtonText = "OK",
CloseButtonText = "Cancel"
};
var dialog = new ContentDialog()
{
Title = "Please enter a new chat name.",
PrimaryButtonText = "OK",
CloseButtonText = "Cancel"
};

var viewModel = new PhrasePresetsNameInputViewModel(dialog);
dialog.Content = new PhrasePresetsNameInput()
{
DataContext = viewModel
};
dialogResult = await VMLocator.MainViewModel.ContentDialogShowAsync(dialog);
if (dialogResult != ContentDialogResult.Primary || string.IsNullOrWhiteSpace(viewModel.UserInput))
{
return "Cancel";
var viewModel = new PhrasePresetsNameInputViewModel(dialog);
dialog.Content = new PhrasePresetsNameInput()
{
DataContext = viewModel
};
dialogResult = await VMLocator.MainViewModel.ContentDialogShowAsync(dialog);
if (dialogResult != ContentDialogResult.Primary || string.IsNullOrWhiteSpace(viewModel.UserInput))
{
return "Cancel";
}
else
{
webChatTitle = viewModel.UserInput;
var msg = await InsertWebChatLogDatabaseAsync(webChatTitle, webConversationHistory, webLog, chatService);
if (msg == "Cancel")
{
return "Cancel";
}
return "OK";
}
}
else
{
webChatTitle = viewModel.UserInput;
var msg = await InsertWebChatLogDatabaseAsync(webChatTitle, webConversationHistory, webLog, chatService);
if (msg == "Cancel")
await Dispatcher.UIThread.InvokeAsync(async () =>
{
return "Cancel";
}
return "OK";
var dialog = new ContentDialog()
{
Title = "Please enter a new chat name.",
PrimaryButtonText = "OK",
CloseButtonText = "Cancel"
};

var viewModel = new PhrasePresetsNameInputViewModel(dialog);
dialog.Content = new PhrasePresetsNameInput()
{
DataContext = viewModel
};
dialogResult = await VMLocator.MainViewModel.ContentDialogShowAsync(dialog);
if (dialogResult != ContentDialogResult.Primary || string.IsNullOrWhiteSpace(viewModel.UserInput))
{
return "Cancel";
}
else
{
webChatTitle = viewModel.UserInput;
var msg = await InsertWebChatLogDatabaseAsync(webChatTitle, webConversationHistory, webLog, chatService);
if (msg == "Cancel")
{
return "Cancel";
}
return "OK";
}
});
}
}
else
Expand Down
10 changes: 9 additions & 1 deletion src/Models/HtmlProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,23 @@ public async Task<string> GetWebChatLogAsync(string htmlSource)
if (titleNode != null)
{
string titleText = titleNode.InnerText;
if (titleText == "New chat" || titleText == "" || titleText == "ChatGPT")
if (!AppSettings.Instance.IsAutoImporting && ( titleText == "New chat" || titleText == "" || titleText == "ChatGPT" ) )
{
return resourceString;
}
else if (AppSettings.Instance.IsAutoImporting && (titleText == "New chat" || titleText == "" || titleText == "ChatGPT"))
{
return "Through";
}
else
{
webChatTitle = ReplaceEntities(titleText);
}
}
else if (AppSettings.Instance.IsAutoImporting)
{
return "Through";
}
else
{
return resourceString;
Expand Down
44 changes: 42 additions & 2 deletions src/ViewModels/WebChatViewmodel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public WebChatViewModel()
ImportWebChatLogCommand = new AsyncRelayCommand(async () => await ImportWebChatLog());
UpdateBrowserCommand = new RelayCommand(UpdateBrowser);

AutoImportIsOn = AppSettings.Instance.IsAutoImporting;

WebChatViewIsVisible = true;
}

Expand Down Expand Up @@ -63,9 +65,27 @@ public async Task PostWebChat()
}
public async Task ImportWebChatLog()
{
var htmlSource = await _browser!.EvaluateJavaScript<string>("return document.documentElement.outerHTML;");
string htmlSource = await _browser!.EvaluateJavaScript<string>("return document.documentElement.outerHTML;");

if (AppSettings.Instance.IsAutoImporting)
{
//htmlに"conversation-turn-"が含まれていない場合、20回htmlを読み込み直す
string tempHtmlSource = htmlSource;
for (int i = 0; i < 20; i++)
{
if (htmlSource.Contains("conversation-turn-") && htmlSource == tempHtmlSource)
{
await Task.Delay(1000);
htmlSource = await _browser.EvaluateJavaScript<string>("return document.documentElement.outerHTML;");
break;
}
htmlSource = await _browser.EvaluateJavaScript<string>("return document.documentElement.outerHTML;");
await Task.Delay(1000);
tempHtmlSource = await _browser.EvaluateJavaScript<string>("return document.documentElement.outerHTML;");
}
}
var msg = await _htmlProcess.GetWebChatLogAsync(htmlSource);
if (msg == "Cancel" || msg == "OK")
if (msg == "Cancel" || msg == "OK" || msg == "Through")
{
return;
}
Expand Down Expand Up @@ -310,5 +330,25 @@ public bool WebChatViewIsVisible//ダイアログ表示用
get => _webChatViewIsVisible;
set => SetProperty(ref _webChatViewIsVisible, value);
}

private bool _autoImportIsOn;
public bool AutoImportIsOn
{
get => _autoImportIsOn;
set
{
if (SetProperty(ref _autoImportIsOn, value))
{
AppSettings.Instance.IsAutoImporting = value;
}
}
}

private bool _isImportRunning;
public bool IsImportRunning
{
get => _isImportRunning;
set => SetProperty(ref _isImportRunning, value);
}
}
}
1 change: 1 addition & 0 deletions src/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ private async void MainWindow_Loaded(object? sender, RoutedEventArgs e)
AppSettings.Instance.Provider = settings.Provider;
AppSettings.Instance.Session = settings.Session;
AppSettings.Instance.SyncIsOn = settings.SyncIsOn;
AppSettings.Instance.IsAutoImporting = settings.IsAutoImporting;
AppSettings.Instance.IsAutoGenerateChatTitle = settings.IsAutoGenerateChatTitle;
VMLocator.CloudLoginViewModel.Email = settings.Email;
VMLocator.CloudLoginViewModel.Password = settings.Password;
Expand Down
11 changes: 9 additions & 2 deletions src/Views/WebChatView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@
<Grid Grid.Column="1"
VerticalAlignment="Center"
Margin="2,0,0,0">
<Button Content="Import"
<Button Content="Import "
ToolTip.Tip="Import Web Chat Log to API Chat"
VerticalAlignment="Center"
HorizontalAlignment="Right"
Width="130"
Width="140"
Command="{Binding ImportWebChatLogCommand}" />
<Image Source="/Assets/iconImport.png"
VerticalAlignment="Center"
Expand All @@ -56,6 +56,13 @@
Height="22"
Width="22"
Margin="10,0,0,0" />
<ToggleSwitch ToolTip.Tip="Auto import"
IsChecked="{Binding AutoImportIsOn}"
OnContent=""
OffContent=""
VerticalAlignment="Center"
HorizontalAlignment="Right"
Margin="0,-4,-10,0" />
</Grid>
<Grid Grid.Column="2">
<Button Command="{Binding UpdateBrowserCommand}"
Expand Down
10 changes: 10 additions & 0 deletions src/Views/WebChatView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public WebChatView()
WebChatViewModel.SetBrowser(browser);
//browser.Focusable = false;
browser.LoadEnd += Browser_LoadEnd;
browser.TitleChanged += Browser_TitleChanged;

_searchBox = this.FindControl<TextBox>("SearchBox")!;
}
Expand All @@ -54,8 +55,17 @@ public void Dispose()
browser.Dispose();
}

private void Browser_TitleChanged(object? sender, string title)
{
if (AppSettings.Instance.IsAutoImporting)
{
_ = VMLocator.WebChatViewModel.ImportWebChatLog();
}
}

private void Browser_LoadEnd(object? sender, LoadEndEventArgs e)
{

string addElementsCode = @"
var button;
var style;
Expand Down

0 comments on commit e0714d5

Please sign in to comment.