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.
276 lines
10 KiB
C#
276 lines
10 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Linq;
|
|
using System.Security.Policy;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.SemanticKernel;
|
|
using Newtonsoft.Json;
|
|
using AI.AgentIntegration;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
namespace AI.Plugin
|
|
{
|
|
/// <summary>
|
|
/// 应用程序状态插件类,提供对应用程序状态的访问和控制功能
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// 初始化 AppStatePlugin 类的新实例
|
|
/// </remarks>
|
|
/// <param name="controller">应用程序控制器实例</param>
|
|
public class AppStatePlugin(IAppController controller)
|
|
{
|
|
/// <summary>
|
|
/// 应用程序控制器实例
|
|
/// </summary>
|
|
private readonly IAppController controller = controller;
|
|
|
|
// ======= 状态读取 =======
|
|
|
|
/// <summary>
|
|
/// 获取当前应用状态的 JSON 快照
|
|
/// </summary>
|
|
/// <returns>表示当前应用状态的 JSON 字符串</returns>
|
|
[KernelFunction]
|
|
[Description("获取当前应用状态的 JSON 快照")]
|
|
public Task<string> GetSnapshotAsync()
|
|
{
|
|
return Task.FromResult(controller.GetCurrentState().ToJson());
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取所有打开的标签页列表
|
|
/// </summary>
|
|
/// <returns>包含所有打开标签页的 JSON 数组字符串</returns>
|
|
[KernelFunction]
|
|
[Description("获取所有打开的标签页列表")]
|
|
public Task<string> GetOpenTabsAsync()
|
|
{
|
|
var tabs = controller.GetCurrentState().UI.OpenTabs;
|
|
return Task.FromResult(JsonConvert.SerializeObject(tabs, Formatting.Indented));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 输出当前状态摘要信息
|
|
/// </summary>
|
|
/// <returns>包含当前视图、活动标签和未保存状态的摘要信息</returns>
|
|
[KernelFunction]
|
|
[Description("输出当前状态摘要")]
|
|
public Task<string> DescribeStateAsync()
|
|
{
|
|
var state = controller.GetCurrentState();
|
|
string summary = $"当前视图: {state.Navigation.CurrentView}, 活动标签: {state.UI.ActiveTab ?? "无"}, 未保存: {state.File.HasUnsavedChanges}";
|
|
return Task.FromResult(summary);
|
|
}
|
|
|
|
// ======= 文件/标签页操作 =======
|
|
|
|
/// <summary>
|
|
/// 打开指定路径的文件
|
|
/// </summary>
|
|
/// <param name="path">要打开的文件路径</param>
|
|
/// <returns>表示操作结果的 JSON 字符串</returns>
|
|
[KernelFunction]
|
|
[Description("打开新文件")]
|
|
public async Task<string> OpenFileAsync(string path)
|
|
{
|
|
var result = await controller.ExecuteAsync(AppAction.CreateOpenFile(path));
|
|
return JsonConvert.SerializeObject(result, Formatting.Indented);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 关闭当前活动文件
|
|
/// </summary>
|
|
/// <returns>表示操作结果的 JSON 字符串</returns>
|
|
[KernelFunction]
|
|
[Description("关闭当前活动文件")]
|
|
public async Task<string> CloseFileAsync()
|
|
{
|
|
var result = await controller.ExecuteAsync(AppAction.CreateAction(AppActionType.CloseFile));
|
|
return JsonConvert.SerializeObject(result, Formatting.Indented);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 关闭所有打开的文件
|
|
/// </summary>
|
|
/// <returns>表示操作结果的 JSON 字符串</returns>
|
|
[KernelFunction]
|
|
[Description("关闭所有文件")]
|
|
public async Task<string> CloseAllFilesAsync()
|
|
{
|
|
var result = await controller.ExecuteAsync(AppAction.CreateAction(AppActionType.CloseAllFiles));
|
|
return JsonConvert.SerializeObject(result, Formatting.Indented);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存当前文件
|
|
/// </summary>
|
|
/// <returns>表示操作结果的 JSON 字符串</returns>
|
|
[KernelFunction]
|
|
[Description("保存当前文件")]
|
|
public async Task<string> SaveFileAsync()
|
|
{
|
|
var result = await controller.ExecuteAsync(AppAction.CreateAction(AppActionType.SaveFile));
|
|
return JsonConvert.SerializeObject(result, Formatting.Indented);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存所有打开的文件
|
|
/// </summary>
|
|
/// <returns>表示操作结果的 JSON 字符串</returns>
|
|
[KernelFunction]
|
|
[Description("保存所有文件")]
|
|
public async Task<string> SaveAllAsync()
|
|
{
|
|
var result = await controller.ExecuteAsync(AppAction.CreateAction(AppActionType.SaveAll));
|
|
return JsonConvert.SerializeObject(result, Formatting.Indented);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 重新加载文件
|
|
/// </summary>
|
|
/// <returns>表示操作结果的 JSON 字符串</returns>
|
|
[KernelFunction]
|
|
[Description("重新加载文件")]
|
|
public async Task<string> ReloadFileAsync()
|
|
{
|
|
var result = await controller.ExecuteAsync(AppAction.CreateAction(AppActionType.ReloadFile));
|
|
return JsonConvert.SerializeObject(result, Formatting.Indented);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 切换到指定标签页
|
|
/// </summary>
|
|
/// <param name="tab">要切换到的标签页标识符</param>
|
|
/// <returns>表示操作结果的 JSON 字符串</returns>
|
|
[KernelFunction]
|
|
[Description("切换标签页")]
|
|
public async Task<string> SwitchTabAsync(string tab)
|
|
{
|
|
var result = await controller.ExecuteAsync(AppAction.CreateAction(AppActionType.SwitchTab, new Dictionary<string, object> { ["tab"] = tab }));
|
|
return JsonConvert.SerializeObject(result, Formatting.Indented);
|
|
}
|
|
|
|
// ======= 导航 =======
|
|
|
|
/// <summary>
|
|
/// 导航到指定视图
|
|
/// </summary>
|
|
/// <param name="view">目标视图名称</param>
|
|
/// <returns>表示操作结果的 JSON 字符串</returns>
|
|
[KernelFunction]
|
|
[Description("导航到指定视图")]
|
|
public async Task<string> NavigateAsync(string view)
|
|
{
|
|
var result = await controller.ExecuteAsync(AppAction.CreateAction(AppActionType.Navigate, new Dictionary<string, object> { ["view"] = view }));
|
|
return JsonConvert.SerializeObject(result, Formatting.Indented);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 执行后退导航操作
|
|
/// </summary>
|
|
/// <returns>表示操作结果的 JSON 字符串</returns>
|
|
[KernelFunction]
|
|
[Description("后退导航")]
|
|
public async Task<string> NavigateBackAsync()
|
|
{
|
|
var result = await controller.ExecuteAsync(AppAction.CreateAction(AppActionType.NavigateBack));
|
|
return JsonConvert.SerializeObject(result, Formatting.Indented);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 执行前进导航操作
|
|
/// </summary>
|
|
/// <returns>表示操作结果的 JSON 字符串</returns>
|
|
[KernelFunction]
|
|
[Description("前进导航")]
|
|
public async Task<string> NavigateForwardAsync()
|
|
{
|
|
var result = await controller.ExecuteAsync(AppAction.CreateAction(AppActionType.NavigateForward));
|
|
return JsonConvert.SerializeObject(result, Formatting.Indented);
|
|
}
|
|
|
|
// ======= 程序状态控制 =======
|
|
|
|
/// <summary>
|
|
/// 设置应用程序的忙碌状态
|
|
/// </summary>
|
|
/// <param name="isBusy">指示应用程序是否处于忙碌状态的布尔值</param>
|
|
/// <returns>表示操作结果的 JSON 字符串</returns>
|
|
[KernelFunction]
|
|
[Description("设置应用忙碌状态")]
|
|
public async Task<string> SetBusyAsync(bool isBusy)
|
|
{
|
|
var result = await controller.ExecuteAsync(AppAction.CreateAction(AppActionType.SetBusy, new Dictionary<string, object> { ["isBusy"] = isBusy }));
|
|
return JsonConvert.SerializeObject(result, Formatting.Indented);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 刷新当前视图
|
|
/// </summary>
|
|
/// <returns>表示操作结果的 JSON 字符串</returns>
|
|
[KernelFunction]
|
|
[Description("刷新当前视图")]
|
|
public async Task<string> RefreshAsync()
|
|
{
|
|
var result = await controller.ExecuteAsync(AppAction.CreateAction(AppActionType.Refresh));
|
|
return JsonConvert.SerializeObject(result, Formatting.Indented);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 退出应用程序
|
|
/// </summary>
|
|
/// <returns>表示操作结果的 JSON 字符串</returns>
|
|
[KernelFunction]
|
|
[Description("退出程序")]
|
|
public async Task<string> ExitAsync()
|
|
{
|
|
var result = await controller.ExecuteAsync(AppAction.CreateAction(AppActionType.Exit));
|
|
return JsonConvert.SerializeObject(result, Formatting.Indented);
|
|
}
|
|
|
|
// ======= 编辑操作 =======
|
|
|
|
/// <summary>
|
|
/// 撤销上一次操作
|
|
/// </summary>
|
|
/// <returns>表示操作结果的 JSON 字符串</returns>
|
|
[KernelFunction]
|
|
[Description("撤销操作")]
|
|
public async Task<string> UndoAsync()
|
|
{
|
|
var result = await controller.ExecuteAsync(AppAction.CreateAction(AppActionType.Undo));
|
|
return JsonConvert.SerializeObject(result, Formatting.Indented);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 重做被撤销的操作
|
|
/// </summary>
|
|
/// <returns>表示操作结果的 JSON 字符串</returns>
|
|
[KernelFunction]
|
|
[Description("重做操作")]
|
|
public async Task<string> RedoAsync()
|
|
{
|
|
var result = await controller.ExecuteAsync(AppAction.CreateAction(AppActionType.Redo));
|
|
return JsonConvert.SerializeObject(result, Formatting.Indented);
|
|
}
|
|
|
|
// ======= 编辑操作 =======
|
|
|
|
[KernelFunction]
|
|
[Description("添加比例尺")]
|
|
public async Task<string> AddScaleBarAsync()
|
|
{
|
|
var result = await controller.ExecuteAsync(AppAction.CreateAction(AppActionType.AddScaleBar));
|
|
return JsonConvert.SerializeObject(result, Formatting.Indented);
|
|
}
|
|
|
|
[KernelFunction]
|
|
[Description("添加图例")]
|
|
public async Task<string> AddLegendAsync()
|
|
{
|
|
var result = await controller.ExecuteAsync(AppAction.CreateAction(AppActionType.AddLegend));
|
|
return JsonConvert.SerializeObject(result, Formatting.Indented);
|
|
}
|
|
}
|
|
} |