From fd3ee24082e150c215054e69a99c29cea8d1c476 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Sun, 29 Nov 2026 12:06:06 -0500 Subject: [PATCH] refactor: share library record copies and MCP search page formatting Co-Authored-By: Claude Opus 5.5 --- OpenNest.Data/DrawingLibraryRecord.cs | 21 +++++++++++++++++ OpenNest.Data/DrawingLibrarySaveSession.cs | 26 +++++++--------------- OpenNest.Data/DrawingSetImport.cs | 21 ++--------------- OpenNest.Mcp/Tools/NestServerTools.cs | 22 +++++++++--------- 4 files changed, 43 insertions(+), 47 deletions(-) diff --git a/OpenNest.Data/DrawingLibraryRecord.cs b/OpenNest.Data/DrawingLibraryRecord.cs index 31fed42d..0cae37e7 100644 --- a/OpenNest.Data/DrawingLibraryRecord.cs +++ b/OpenNest.Data/DrawingLibraryRecord.cs @@ -59,4 +59,25 @@ public sealed class DrawingLibraryRecord /// measured it; null until then. Server-computed and ignored on upload. /// public DrawingGeometrySummary? Geometry { get; set; } + + /// + /// A copy of every field but the server-computed ones (package hash and size, saved time + /// and geometry), which keep their defaults. + /// + internal DrawingLibraryRecord CopyWithoutServerFields() => new() + { + Id = Id, + Revision = Revision, + Name = Name, + Customer = Customer, + MaterialName = MaterialName, + MaterialGrade = MaterialGrade, + Thickness = Thickness, + Units = Units, + CutType = CutType, + Notes = Notes, + Settings = Settings, + Archived = Archived, + SavedBy = SavedBy, + }; } diff --git a/OpenNest.Data/DrawingLibrarySaveSession.cs b/OpenNest.Data/DrawingLibrarySaveSession.cs index 227c2643..0feb96f7 100644 --- a/OpenNest.Data/DrawingLibrarySaveSession.cs +++ b/OpenNest.Data/DrawingLibrarySaveSession.cs @@ -351,25 +351,15 @@ public sealed class DrawingLibrarySaveSession return new DrawingLibrarySaveResult(saved, outcome, recoveredEarlierSave); } - private static DrawingLibraryRecord Copy(DrawingLibraryRecord record) => new() + // Everything but the measured geometry. + private static DrawingLibraryRecord Copy(DrawingLibraryRecord record) { - Id = record.Id, - Revision = record.Revision, - Name = record.Name, - Customer = record.Customer, - MaterialName = record.MaterialName, - MaterialGrade = record.MaterialGrade, - Thickness = record.Thickness, - Units = record.Units, - CutType = record.CutType, - Notes = record.Notes, - Settings = record.Settings, - Archived = record.Archived, - PackageSha256 = record.PackageSha256, - PackageSize = record.PackageSize, - SavedAt = record.SavedAt, - SavedBy = record.SavedBy, - }; + var copy = record.CopyWithoutServerFields(); + copy.PackageSha256 = record.PackageSha256; + copy.PackageSize = record.PackageSize; + copy.SavedAt = record.SavedAt; + return copy; + } private static string Sha256(byte[] bytes) => Convert.ToHexString(SHA256.HashData(bytes)).ToLowerInvariant(); diff --git a/OpenNest.Data/DrawingSetImport.cs b/OpenNest.Data/DrawingSetImport.cs index ed296b8e..250710d7 100644 --- a/OpenNest.Data/DrawingSetImport.cs +++ b/OpenNest.Data/DrawingSetImport.cs @@ -281,7 +281,7 @@ public sealed class DrawingSetImport private static DrawingLibraryRecord Created(DrawingSetImportEntry entry) { - var record = Copy(entry.Item.Metadata); + var record = entry.Item.Metadata.CopyWithoutServerFields(); record.Id = entry.NewId; record.Revision = 0; record.Thickness = DrawingLibraryRules.NormalizeThickness(record.Thickness); @@ -292,28 +292,11 @@ public sealed class DrawingSetImport // The identity already matches; keep the stored spelling, notes and settings. private static DrawingLibraryRecord Revised(DrawingLibraryRecord existing, string savedBy) { - var record = Copy(existing); + var record = existing.CopyWithoutServerFields(); record.Revision = 0; record.SavedBy = savedBy; return record; } - private static DrawingLibraryRecord Copy(DrawingLibraryRecord record) => new() - { - Id = record.Id, - Revision = record.Revision, - Name = record.Name, - Customer = record.Customer, - MaterialName = record.MaterialName, - MaterialGrade = record.MaterialGrade, - Thickness = record.Thickness, - Units = record.Units, - CutType = record.CutType, - Notes = record.Notes, - Settings = record.Settings, - Archived = record.Archived, - SavedBy = record.SavedBy, - }; - private static string Sha256(byte[] bytes) => Convert.ToHexString(SHA256.HashData(bytes)).ToLowerInvariant(); } diff --git a/OpenNest.Mcp/Tools/NestServerTools.cs b/OpenNest.Mcp/Tools/NestServerTools.cs index 824595dd..7bf57ad8 100644 --- a/OpenNest.Mcp/Tools/NestServerTools.cs +++ b/OpenNest.Mcp/Tools/NestServerTools.cs @@ -58,11 +58,7 @@ namespace OpenNest.Mcp.Tools Offset = System.Math.Max(0, offset), Limit = System.Math.Clamp(limit, 1, DrawingLibraryQuery.MaxLimit), }, cancellationToken); - var sb = new StringBuilder(); - sb.AppendLine($"{page.Items.Count} of {page.Total} drawings (offset {page.Offset}):"); - foreach (var drawing in page.Items) - sb.AppendLine("- " + DescribeDrawing(drawing)); - return sb.ToString(); + return FormatPage(page, page.Items, "drawings", DescribeDrawing); }); [McpServerTool(Name = "get_drawing")] @@ -111,11 +107,7 @@ namespace OpenNest.Mcp.Tools Offset = System.Math.Max(0, offset), Limit = System.Math.Clamp(limit, 1, DrawingSetQuery.MaxLimit), }, cancellationToken); - var sb = new StringBuilder(); - sb.AppendLine($"{page.Items.Count} of {page.Total} sets (offset {page.Offset}):"); - foreach (var set in page.Items) - sb.AppendLine("- " + DescribeSet(set)); - return sb.ToString(); + return FormatPage(page, page.Items, "sets", DescribeSet); }); [McpServerTool(Name = "get_set")] @@ -715,6 +707,16 @@ namespace OpenNest.Mcp.Tools return list.Count == 0 ? "" : " Similar: " + string.Join(", ", list) + "."; } + // A search result: the page's position in the matches, then one line per record. + private static string FormatPage(IBrowsePage page, IEnumerable items, string noun, Func describe) + { + var sb = new StringBuilder(); + sb.AppendLine($"{page.ItemCount} of {page.Total} {noun} (offset {page.Offset}):"); + foreach (var item in items) + sb.AppendLine("- " + describe(item)); + return sb.ToString(); + } + private static string DescribeDrawing(DrawingLibraryRecord d) => $"{d.Name} | id {d.Id} rev {d.Revision} | customer {Blank(d.Customer)} | " + $"{Material(d.MaterialName, d.MaterialGrade, d.Thickness, d.Units)} {d.CutType.ToString().ToLowerInvariant()}"