Apply and undo reviewed vision function renames in MCP and WPF
This commit is contained in:
parent
1efee842d7
commit
2b66063b4d
@ -28,7 +28,7 @@ internal sealed class ProjectGateway(string root, bool allowWrite)
|
||||
return ProjectPipe.Call(root, new() { ["operation"] = operation, ["arguments"] = arguments }, () =>
|
||||
{
|
||||
using var project = ProjectDirectory.Open(root);
|
||||
if (operation is "preview" or "previewScript" or "previewRename" or "previewWorkflowRename" or "previewPointRename" or "previewUndo" or "apply" or "status")
|
||||
if (operation is "preview" or "previewScript" or "previewRename" or "previewWorkflowRename" or "previewPointRename" or "previewVisionRename" or "previewUndo" or "apply" or "status")
|
||||
return changes.Execute(project, operation, arguments, allowWrite, change => (change.Review == null && change.Rename == null) || approved);
|
||||
return ProjectInspection.Query(project, operation, arguments);
|
||||
}).ToJsonString();
|
||||
|
||||
@ -7,6 +7,10 @@ namespace PLCSharp.Mcp;
|
||||
[McpServerToolType]
|
||||
internal sealed class ProjectTools(ProjectGateway gateway)
|
||||
{
|
||||
[McpServerTool(Name = "preview_vision_function_rename"), Description("Preview renaming a vision function by ID. Compile rewritten recipe workflows and list dynamic or uncertain references before applying.")]
|
||||
public string PreviewVisionFunctionRename(string id, string newName) =>
|
||||
gateway.Call("previewVisionRename", new() { ["id"] = id, ["newName"] = newName });
|
||||
|
||||
[McpServerTool(Name = "preview_point_rename"), Description("Preview renaming an AxisPoints or RobotPoints ID, including bound workflow and matrix references. Review uncertain and dynamic hits before applying.")]
|
||||
public string PreviewPointRename(string kind, string id, string newName) =>
|
||||
gateway.Call("previewPointRename", new() { ["kind"] = kind, ["id"] = id, ["newName"] = newName });
|
||||
@ -17,7 +21,7 @@ internal sealed class ProjectTools(ProjectGateway gateway)
|
||||
[McpServerTool(Name = "preview_variable_rename"), Description("Preview renaming a recipe user variable by ID. Compile rewritten workflows, and report dynamic or uncertain references for review. Apply the returned draft ID after review.")]
|
||||
public string PreviewVariableRename(string id, string newName) => gateway.Call("previewRename", new() { ["id"] = id, ["newName"] = newName });
|
||||
|
||||
[McpServerTool(Name = "preview_undo"), Description("Preview reversal of a saved variable, point or workflow rename from this session. Apply the new draft ID to confirm undo.")]
|
||||
[McpServerTool(Name = "preview_undo"), Description("Preview reversal of a saved variable, vision function, point or workflow rename from this session. Apply the new draft ID to confirm undo.")]
|
||||
public string PreviewUndo(string draftId) => gateway.Call("previewUndo", new() { ["draftId"] = draftId });
|
||||
|
||||
[McpServerTool(Name = "preview_workflow_script"), Description("Preview replacement C# source for a workflow ID. Returns a line diff, Roslyn diagnostics, entry-point validation and draft ID. No code runs. Apply a valid draft with apply_changes; a local WPF confirmation is required.")]
|
||||
|
||||
@ -32,11 +32,11 @@ public partial class GlobalModel
|
||||
if (!AllowAiAccess) throw new InvalidOperationException("请在机种配方页面启用 AI 访问。");
|
||||
string operation = (string)request["operation"];
|
||||
var args = request["arguments"].AsObject();
|
||||
if (operation is "preview" or "previewScript" or "previewRename" or "previewWorkflowRename" or "previewPointRename" or "previewUndo" or "apply" or "status")
|
||||
if (operation is "preview" or "previewScript" or "previewRename" or "previewWorkflowRename" or "previewPointRename" or "previewVisionRename" or "previewUndo" or "apply" or "status")
|
||||
{
|
||||
if (operation == "apply" && (ModeState.State.Execute || WorkflowsModel.IsBusy))
|
||||
throw new InvalidOperationException("运行或切换配方期间无法保存 AI 修改。");
|
||||
Action updatePointView = null;
|
||||
Action updateEditorView = null;
|
||||
var resultChange = aiChanges.Execute(Project, operation, args, true, change =>
|
||||
{
|
||||
bool accepted = change.Review != null || change.Rename != null
|
||||
@ -49,21 +49,30 @@ public partial class GlobalModel
|
||||
(!VariablesModel.Variables.Any(v => v.ID == rename.VariableId && v.Name == rename.OldName) ||
|
||||
VariablesModel.Variables.Any(v => v.ID != rename.VariableId && (v.Name == rename.NewName || v.Name == rename.OldName))))
|
||||
throw new InvalidOperationException("编辑器变量列表存在更改,请先保存并重新预览。");
|
||||
if (accepted && change.Rename is { Kind: "VisionFunctions" } vision && CurrentRecipe.ID == vision.RecipeId)
|
||||
{
|
||||
var functions = VisionsModel.VisionFunctions;
|
||||
var target = functions.SingleOrDefault(v => v.ID == vision.ObjectId);
|
||||
if (target == null || target.Name != vision.OldName || functions.Any(v => v.ID != vision.ObjectId &&
|
||||
(v.Name == vision.OldName || v.Name == vision.NewName)))
|
||||
throw new ProjectConflictException("编辑器视觉功能列表有更改,请先保存并重新预览。");
|
||||
updateEditorView = () => target.SetReviewedName(vision.NewName);
|
||||
}
|
||||
if (accepted && change.Rename is { IsPoint: true } point && CurrentRecipe.ID == point.RecipeId)
|
||||
{
|
||||
if (point.Kind == "AxisPoints")
|
||||
updatePointView = Project.PreparePointView(point, ControllersModel.AxisPoints, ControllersModel.Matrices);
|
||||
updateEditorView = Project.PreparePointView(point, ControllersModel.AxisPoints, ControllersModel.Matrices);
|
||||
else
|
||||
{
|
||||
var owner = Project.Data.RobotPoints.Single(p => p.ID == point.ObjectId).RobotID;
|
||||
var robot = RobotModel.Robots.SingleOrDefault(r => r.ID == owner);
|
||||
if (robot != null)
|
||||
updatePointView = Project.PreparePointView(point, robot.Points, robot.RobotMatrices);
|
||||
updateEditorView = Project.PreparePointView(point, robot.Points, robot.RobotMatrices);
|
||||
}
|
||||
}
|
||||
return accepted;
|
||||
});
|
||||
if ((string)resultChange["Status"] == "saved") updatePointView?.Invoke();
|
||||
if ((string)resultChange["Status"] == "saved") updateEditorView?.Invoke();
|
||||
return resultChange;
|
||||
}
|
||||
var result = ProjectInspection.Query(Project, operation, args);
|
||||
|
||||
@ -26,10 +26,12 @@ public sealed class ProjectChanges
|
||||
public JsonNode Execute(ProjectDirectory project, string operation, JsonObject args, bool allowWrite,
|
||||
Func<ProjectChange, bool> confirm = null)
|
||||
{
|
||||
if (operation is "previewRename" or "previewWorkflowRename" or "previewPointRename" or "previewUndo")
|
||||
if (operation is "previewRename" or "previewWorkflowRename" or "previewPointRename" or "previewVisionRename" or "previewUndo")
|
||||
{
|
||||
ProjectRename plan;
|
||||
if (operation == "previewPointRename")
|
||||
if (operation == "previewVisionRename")
|
||||
plan = project.PlanVisionRename(Guid.Parse((string)args["id"]), (string)args["newName"]);
|
||||
else if (operation == "previewPointRename")
|
||||
plan = project.PlanPointRename((string)args["kind"], Guid.Parse((string)args["id"]), (string)args["newName"]);
|
||||
else if (operation == "previewWorkflowRename")
|
||||
plan = project.PlanWorkflowRename(Guid.Parse((string)args["id"]), (string)args["newName"]);
|
||||
|
||||
@ -21,6 +21,7 @@ public sealed record ProjectRename(Guid ObjectId, Guid RecipeId, string OldName,
|
||||
internal static string PathFor(string kind, Guid recipe) => kind switch
|
||||
{
|
||||
"AxisPoints" or "RobotPoints" => "configuration/points.json",
|
||||
"VisionFunctions" => "configuration/vision.json",
|
||||
"Workflows" => $"recipes/{recipe:D}/recipe.json",
|
||||
_ => ProjectDirectory.VariablesPath
|
||||
};
|
||||
@ -61,6 +62,23 @@ public sealed partial class ProjectDirectory
|
||||
return PlanReferences("Variables", id, variable.RecipeID, variable.Name, newName);
|
||||
}
|
||||
|
||||
internal ProjectRename PlanVisionRename(Guid id, string newName)
|
||||
{
|
||||
var target = Data.VisionFunctions.Single(v => v.ID == id);
|
||||
CheckVisionName(target.RecipeID, id, target.Name, newName);
|
||||
return PlanReferences("VisionFunctions", id, target.RecipeID, target.Name, newName);
|
||||
}
|
||||
|
||||
private void CheckVisionName(Guid recipe, Guid id, string before, string after)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(after) || after != after.Trim() || before == after)
|
||||
throw new ArgumentException("请输入不同的新名称,首尾不能有空白。");
|
||||
var rows = Data.VisionFunctions.Where(v => v.RecipeID == recipe);
|
||||
if (!rows.Any(v => v.ID == id && v.Name == before) ||
|
||||
rows.Any(v => v.ID != id && (v.Name == before || v.Name == after)))
|
||||
throw new ProjectConflictException("视觉功能已改变或配方内存在重名。");
|
||||
}
|
||||
|
||||
internal ProjectRename PlanWorkflowRename(Guid id, string newName)
|
||||
{
|
||||
var workflow = Recipes.SelectMany(r => Flows(r.ID)).Single(w => w.ID == id);
|
||||
@ -113,6 +131,7 @@ public sealed partial class ProjectDirectory
|
||||
private void CheckName(ProjectRename plan, string before, string after)
|
||||
{
|
||||
if (plan.Kind == "Workflows") CheckWorkflowName(plan.RecipeId, plan.ObjectId, before, after);
|
||||
else if (plan.Kind == "VisionFunctions") CheckVisionName(plan.RecipeId, plan.ObjectId, before, after);
|
||||
else if (plan.IsPoint) ValidatePoint(plan, before, after);
|
||||
else CheckVariableName(plan.RecipeId, plan.ObjectId, before, after);
|
||||
}
|
||||
@ -167,6 +186,9 @@ public sealed partial class ProjectDirectory
|
||||
PLCSharp.VVMs.Workflows.Workflow.SaveReviewedBatch(updates, () =>
|
||||
{
|
||||
using var transaction = workflowRename ? null : Data.Database.BeginTransaction();
|
||||
if (plan.Kind == "VisionFunctions" && Data.VisionFunctions.Where(v => v.ID == plan.ObjectId && v.Name == plan.OldName)
|
||||
.ExecuteUpdate(s => s.SetProperty(v => v.Name, plan.NewName)) != 1)
|
||||
throw new ProjectConflictException("视觉功能已发生变化。");
|
||||
if (plan.IsPoint) WritePointFields(plan);
|
||||
if (plan.Kind == "Variables" && Data.Variables.Where(v => v.ID == plan.ObjectId && v.Name == plan.OldName)
|
||||
.ExecuteUpdate(s => s.SetProperty(v => v.Name, plan.NewName)) != 1)
|
||||
@ -188,6 +210,13 @@ public sealed partial class ProjectDirectory
|
||||
AcceptPointFields(plan);
|
||||
return;
|
||||
}
|
||||
if (plan.Kind == "VisionFunctions")
|
||||
{
|
||||
var vision = Data.VisionFunctions.Find(plan.ObjectId);
|
||||
vision.SetReviewedName(plan.NewName);
|
||||
Data.Entry(vision).Property(v => v.Name).OriginalValue = plan.NewName;
|
||||
return;
|
||||
}
|
||||
var variable = Data.Variables.Local.Single(v => v.ID == plan.ObjectId);
|
||||
variable.Name = plan.NewName;
|
||||
Data.Entry(variable).Property(v => v.Name).OriginalValue = plan.NewName;
|
||||
|
||||
@ -53,6 +53,8 @@ namespace PLCSharp.VVMs.Vision
|
||||
}
|
||||
}
|
||||
|
||||
internal void SetReviewedName(string value) => SetProperty(ref _Name, value, nameof(Name));
|
||||
|
||||
private string _Prompt;
|
||||
|
||||
/// <summary>
|
||||
|
||||
@ -11,7 +11,8 @@ public partial class ScriptReviewWindow : Window
|
||||
InitializeComponent();
|
||||
if (change.Rename is { } rename)
|
||||
{
|
||||
var subject = rename.IsPoint ? "点位" : rename.Kind == "Workflows" ? "工作流" : "变量";
|
||||
var subject = rename.Kind switch { "VisionFunctions" => "视觉功能", "Workflows" => "工作流",
|
||||
"AxisPoints" or "RobotPoints" => "点位", _ => "变量" };
|
||||
Title = $"确认{(rename.UndoOf.HasValue ? "撤销" : "")}{subject}重命名";
|
||||
WorkflowTitle.Text = $"{rename.OldName} → {rename.NewName}";
|
||||
Location.Text = System.IO.Path.Combine(root, change.Path);
|
||||
|
||||
53
Tests/PLCSharp.Tests/VisionRenameTests.cs
Normal file
53
Tests/PLCSharp.Tests/VisionRenameTests.cs
Normal file
@ -0,0 +1,53 @@
|
||||
using PLCSharp.Models;
|
||||
using PLCSharp.VVMs.Vision;
|
||||
using PLCSharp.VVMs.Workflows;
|
||||
using System.IO;
|
||||
|
||||
namespace PLCSharp.Tests;
|
||||
|
||||
public class VisionRenameTests
|
||||
{
|
||||
[Fact]
|
||||
public void OnlyRecipeVisionLookupsAreRewrittenAndUndoRestoresFiles()
|
||||
{
|
||||
var root = Directory.CreateTempSubdirectory();
|
||||
try
|
||||
{
|
||||
using var p = ProjectDirectory.New();
|
||||
var vision = new VisionFunction { RecipeID = p.DefaultRecipeId, Name = "检查" };
|
||||
p.Data.AddRange(vision, new VisionFunction { RecipeID = vision.RecipeID, Name = "复检" });
|
||||
var caller = new Workflow { Name = "调度", RecipeID = p.DefaultRecipeId };
|
||||
caller.Code = $$"""
|
||||
using System.Linq;
|
||||
using PLCSharp.Models;
|
||||
using PLCSharp.Core.Common;
|
||||
public class Extension_{{caller.ID:N}} {
|
||||
public void Run(GlobalModel m, FlowModel f) {
|
||||
const string original = "检查";
|
||||
_ = m.GetVisionFunction(original);
|
||||
_ = m.VisionsModel.VisionFunctions.FirstOrDefault(v => v.Name == "检查");
|
||||
_ = m.GetVisionFunction(f.Step.ToString());
|
||||
_ = m.GetTask("检查");
|
||||
}
|
||||
}
|
||||
""";
|
||||
p.Flows(vision.RecipeID).Add(caller);
|
||||
p.Save(root.FullName);
|
||||
Assert.ThrowsAny<IOException>(() => p.PlanVisionRename(vision.ID, "复检"));
|
||||
var original = Directory.GetFiles(root.FullName, "*", SearchOption.AllDirectories).ToDictionary(f => f, File.ReadAllBytes);
|
||||
var plan = p.PlanVisionRename(vision.ID, "装配检查");
|
||||
Assert.True(plan.Compiled);
|
||||
Assert.Equal("dynamic", (string?)Assert.Single(plan.ManualReferences)!["confidence"]);
|
||||
p.ApplyRename(plan);
|
||||
Assert.Contains("GetVisionFunction(\"装配检查\")", caller.Code);
|
||||
Assert.Contains("v.Name == \"装配检查\"", caller.Code);
|
||||
Assert.Contains("GetTask(\"检查\")", caller.Code);
|
||||
using (var reopened = ProjectDirectory.Open(root.FullName))
|
||||
Assert.Equal("装配检查", reopened.Data.VisionFunctions.Find(vision.ID)!.Name);
|
||||
p.ApplyRename(p.PlanUndo(Guid.NewGuid(), plan));
|
||||
Assert.Equal("检查", vision.Name);
|
||||
Assert.All(original, file => Assert.Equal(file.Value, File.ReadAllBytes(file.Key)));
|
||||
}
|
||||
finally { root.Delete(true); }
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user