refactor: share library record copies and MCP search page formatting

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Opus 5.5 committed 2026-11-29 12:06:37 -05:00
1 parent 70ac1c7d6f
commit fd3ee24082
4 files changed
+43 -47

No files matched your search

+21
View File
@@ -59,4 +59,25 @@ public sealed class DrawingLibraryRecord
/// measured it; null until then. Server-computed and ignored on upload.
/// </summary>
public DrawingGeometrySummary? Geometry { get; set; }
/// <summary>
/// A copy of every field but the server-computed ones (package hash and size, saved time
/// and geometry), which keep their defaults.
/// </summary>
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,
};
}
+8 -18
View File
@@ -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();
+2 -19
View File
@@ -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();
}
+12 -10
View File
@@ -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<T>(IBrowsePage page, IEnumerable<T> items, string noun, Func<T, string> 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()}"