You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kev/Drawer/AI/Service/FormRequestNotifier.cs

72 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
using AI.Interface;
using AI.Models;
using AI.Models.Form;
using AI.ViewModels;
using Avalonia.Threading;
namespace AI.Service
{
/// <summary>
/// 表单请求通知实现:收到请求后在 UI 线程向当前会话插入表单消息。
/// </summary>
public class FormRequestNotifier : IFormRequestNotifier
{
private readonly MainWindowViewModel _mainViewModel;
private readonly IFormRegistry _registry;
private readonly Dictionary<string, Action<ChatSession?>> _handlers;
public const string FormIdLoadXyz = "gridding-load-xyz";
public const string FormIdGriddingParams = "gridding-parameters";
public FormRequestNotifier(MainWindowViewModel mainViewModel, IFormRegistry registry)
{
_mainViewModel = mainViewModel ?? throw new ArgumentNullException(nameof(mainViewModel));
_registry = registry ?? throw new ArgumentNullException(nameof(registry));
_handlers = new Dictionary<string, Action<ChatSession?>>(StringComparer.OrdinalIgnoreCase)
{
[FormIdLoadXyz] = ShowXyzLoadCard,
[FormIdGriddingParams] = ShowGriddingParamCard,
};
}
/// <inheritdoc />
public void RequestForm(string formId)
{
if (string.IsNullOrWhiteSpace(formId))
{
return;
}
var targetSession = CurrentSessionContext.Current;
if (_handlers.TryGetValue(formId, out var handler))
{
handler(targetSession);
return;
}
var definition = _registry.GetForm(formId);
if (definition == null)
{
return;
}
Dispatcher.UIThread.Post(() => _mainViewModel.AddFormMessage(definition, targetSession));
}
private void ShowXyzLoadCard(ChatSession? session)
{
Dispatcher.UIThread.Post(() => _mainViewModel.AddXyzLoadCardMessage(session));
}
private void ShowGriddingParamCard(ChatSession? session)
{
Dispatcher.UIThread.Post(() => _mainViewModel.AddGriddingParamCardMessage(session));
}
}
}