Files
BeWoPlaner/BeWo/AI/View/AiTranskriptionView.xaml.cs

323 lines
8.9 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Threading;
using BeWo.Annotations;
using BeWo.Core;
using BeWo.ServiceProxy;
using BeWo.Validation;
using BeWo.View;
using BeWo.View.Controls;
using BeWo.ViewModel;
using BeWo.ViewModel.ListViewModel;
using BeWo.ViewModel.View.AI;
using BS.Shared;
using BS.Shared.Core;
using BS.Shared.DataContracts;
using BS.Shared.Extensions;
using BS.Shared.Translation;
using DevExpress.Mvvm;
using DevExpress.Utils;
using DevExpress.Xpf.Editors;
using DevExpress.Xpf.Grid;
using MessageBox = System.Windows.MessageBox;
namespace BeWo.AI.View
{
public partial class AiTranskriptionView : BeWoView
{
public event EventHandler ApplyClicked;
public event EventHandler CancelClicked;
bool timerActive = false;
AiVoiceDataDC currentVoiceData = null;
public ICommand OpenAiPromptSelectionCommand { get; set; }
public AiTranskriptionView()
{
InitializeComponent();
//InitAiPrompt();
}
//private void InitAiPrompt()
//{
// var vm_doku = new AiPromptbausteinSelectionComplexViewModel(AiActionType.ServiceRecordDocumentation);
// vm_doku.PromptAccepted += PromptAccepted;
// OpenAiPromptSelectionCommand = new DelegateCommand(() =>
// {
// BeWoApp.MainControl.WindowService.Show(vm_doku);
// }, () => !string.IsNullOrWhiteSpace(TextBoxErgebnis.Text));
//}
//private void PromptAccepted(object sender, AiPromptbausteinPromptVM prompt)
//{
// if (prompt is null)
// throw new NotImplementedException();
// // 1. Information aufbereiten
// // 1.1 Prompt
// var prompt_oid = prompt.DataContract.Oid.Value;
// var prompt_version = prompt.DataContract.Version;
// // 1.2 Hilfeplan
// var support_concept = SelectedCustomerNode.SupportConceptTreeNodeDC.SupportConcept;
// var support_concept_oid = support_concept.SupportConceptOid;
// var support_concept_version = support_concept.SupportConceptVersion;
// // 1.3 Aktuelles Doku Feld
// var doku_feld = TextBoxErgebnis.Text;
// // 1.4 Sichtbaren Zeiterfassungseinträge
// var service_records = CurrentVisibleVMs;
// var sr_refs = service_records.Select(x => new BeWoRefDC(x.DataContract.ServiceRecordOid.Value, x.DataContract.ServiceRecordVersion));
// // 1.5 Costbearer
// var costbearer = SelectedCustomerNode.SupportConceptTreeNodeDC.CostBearer;
// var cb_oid = costbearer.CostBearerOid.Value;
// var cb_ver = costbearer.CostBearerVersion;
// // Service Aufruf
// var req = new AiFunctionDocRequest()
// {
// AiPromptbausteinPrompt = new BeWoRefDC(prompt_oid, prompt_version),
// SupportConcept = new BeWoRefDC(support_concept_oid, support_concept_version),
// ServiceRecords = sr_refs,
// CostBearer = new BeWoRefDC(cb_oid, cb_ver),
// Dokumentation = doku_feld,
// };
// ServiceFacade.DoAiEnhancedServiceAsnyc(x => x.ExecuteAiFunctionServiceRecordDocumentation(req),
// callback =>
// {
// var viewModel = new AiPromptbausteinActionResultViewModel();
// viewModel.Result = callback.Result;
// viewModel.Accept_Button_Clicked += (s, e) =>
// {
// var sb = new StringBuilder();
// sb.AppendLine("AI Antwort:");
// sb.AppendLine(viewModel.Result);
// sb.AppendLine();
// sb.AppendLine("Original:");
// sb.AppendLine(AktuellerDokutext);
// var str = sb.ToString();
// var idx = _AiActionHistoryIndex;
// var count = _AiActionHistory.Count;
// if (count - 1 > idx)
// {
// _AiActionHistory.RemoveRange(idx + 1, count - idx - 1);
// }
// if (AktuellerDokutext != _AiActionHistory[_AiActionHistoryIndex])
// {
// _AiActionHistory.Add(AktuellerDokutext);
// _AiActionHistoryIndex++;
// }
// _AiActionHistory.Add(str);
// _AiActionHistoryIndex++;
// AktuellerDokutext = str;
// };
// viewModel.Replace_Button_Clicked += (s, e) =>
// {
// var str = viewModel.Result;
// var idx = _AiActionHistoryIndex;
// var count = _AiActionHistory.Count;
// if (count - 1 > idx)
// {
// _AiActionHistory.RemoveRange(idx + 1, count - idx - 1);
// }
// if (AktuellerDokutext != _AiActionHistory[_AiActionHistoryIndex])
// {
// _AiActionHistory.Add(AktuellerDokutext);
// _AiActionHistoryIndex++;
// }
// _AiActionHistory.Add(str);
// _AiActionHistoryIndex++;
// AktuellerDokutext = str;
// };
// viewModel.Retry_Button_Clicked += (s, e) =>
// {
// PromptAccepted(sender, prompt);
// };
// BeWoApp.MainControl.WindowService.Show(viewModel);
// }
// );
//}
public void InitView()
{
}
public string GetText()
{
return TextBoxErgebnis.Text;
}
private void StartAufnahme()
{
timerActive = true;
//lblStatus.Content = "Um die Aufnahme zu starten, klicken Sie auf den Link, den Sie in ownChat erhalten haben.";
StartTimer();
ButtonText.Text = "Aufnahme abbrechen";
currentVoiceData = ServiceFacade.DoOperationsServiceSync(s => s.StartAIVoice());
PanelAnimation.Visibility = Visibility.Visible;
VBPlay.Visibility = Visibility.Hidden;
VBStop.Visibility = Visibility.Visible;
}
private void StopAufnahme()
{
timerActive = false;
VBPlay.Visibility = Visibility.Visible;
VBStop.Visibility = Visibility.Hidden;
}
private void StartTimer()
{
var sec = BeWoApp.AppSettings.AiVoiceRequestInterval;
if (sec <= 0)
{
sec = 10;
}
new DispatcherTimer(
TimeSpan.FromMilliseconds(sec * 1000),
DispatcherPriority.Background,
(s1, e1) =>
{
CheckTranskriptionStatus();
if (!timerActive)
{
((DispatcherTimer)s1).Stop();
PanelAnimation.Visibility = Visibility.Hidden;
ButtonText.Text = "Neue Aufnahme starten";
lblStatus.Content = "";
}
},
this.Dispatcher).Start();
}
private void CheckTranskriptionStatus()
{
currentVoiceData = ServiceFacade.DoOperationsServiceSync(s => s.GetAIVoiceTranscript(currentVoiceData));
if (!String.IsNullOrEmpty(currentVoiceData.TranskriptionText))
{
InsertTextAtSelection(currentVoiceData.TranskriptionText);
lblStatus.Content = Translator.Translate("Transkription erfolgreich durchgeführt");
StopAufnahme();
}
else if (!String.IsNullOrEmpty(currentVoiceData.Error))
{
lblStatus.Content = currentVoiceData.Error;
StopAufnahme();
}
else if (!String.IsNullOrEmpty(currentVoiceData.StatusMessage))
{
lblStatus.Content = currentVoiceData.StatusMessage;
}
}
private void InsertTextAtSelection(string neuerText)
{
int start = TextBoxErgebnis.SelectionStart;
int length = TextBoxErgebnis.SelectionLength;
TextBoxErgebnis.Text =
TextBoxErgebnis.Text.Remove(start, length)
.Insert(start, neuerText);
TextBoxErgebnis.CaretIndex = start + neuerText.Length;
}
private void ButtonStartStop_Click(object sender, RoutedEventArgs e)
{
if (!timerActive)
{
StartAufnahme();
}
else
{
ButtonText.Text = "Bitte warten...";
lblStatus.Content = "Aufnahme wird abgebrochen. Bitte warten...";
StopAufnahme();
}
}
private void ButtonApply_Click(object sender, RoutedEventArgs e)
{
Save(() => ApplyClicked?.Invoke(this, EventArgs.Empty));
}
private void ButtonClose_Click(object sender, RoutedEventArgs e)
{
Save(() => CancelClicked?.Invoke(this, EventArgs.Empty));
}
private void Save(Action finish = null)
{
var userSettings = DataContext as AiUserSettingVM;
if (userSettings.IsDirty)
{
var dc = userSettings.CommitToDataContract();
ServiceFacade.DoAiEnhancedServiceAsnyc(x => x.UpdateUserSetting(dc),
st =>
{
userSettings.UpdateByDataContract(st);
finish();
});
}
else
{
finish();
}
}
private void BeWoView_Loaded(object sender, RoutedEventArgs e)
{
CheckForAutoStartRecording();
}
private void CheckForAutoStartRecording()
{
var viewmodel = DataContext as AiUserSettingVM;
if (viewmodel is null)
return;
if (viewmodel.AutoStartRecording)
ButtonStartStop_Click(this, new RoutedEventArgs());
}
}
}