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