Sections manifest: parse chatTabs (server default chat tabs)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Daniel Wallace 2026-11-15 08:22:56 -08:00
commit f2d7f0464c
2 changed files with 120 additions and 0 deletions

View file

@ -61,6 +61,17 @@
// command, or with a duplicate id, is dropped; at most MaxBarMenuItems (6)
// are kept, and a "barMenu" that is not an array is ignored (the window is
// kept). Engines before v95 ignore the key.
// chatTabs (2026-11-15) default RynthChat tabs a server's plugin declares:
// [ { "server": "dreamweave", "id": "dereth-network", "name": "Network",
// "pinned": true, "globalRoomFallback": "Roleplay",
// "rules": [ { "action": "copy", "when": "type:3", "mode": "",
// "pattern": "...", "globalRoom": false } ] } ].
// "server" and "id" are ids (the owner key is "server/id"); "action" is copy
// or move; "globalRoomFallback" is General, Trade, LFG or Roleplay (the
// room a "globalRoom": true rule matches until chat shows another). An
// entry with a bad field is dropped and reported in the parse error (the
// other keys still parse); at most MaxChatTabs entries of MaxChatRules
// rules each. Engines before this ignore the key.
//
// Everything from a plugin is checked here: ids are short lower-case words,
// text is capped, a command must be a single "/..." chat command, and the lists
@ -145,6 +156,12 @@ internal sealed record SectionWindowDecl(
public SectionBarMenuItem[] Menu => BarMenu ?? System.Array.Empty<SectionBarMenuItem>();
}
/// <summary>One rule of a plugin-declared chat tab ("chatTabs"); a GlobalRoom rule's pattern follows the room discovered from chat.</summary>
internal sealed record SectionChatRuleDecl(string Action, string When, string Mode, string Pattern, bool GlobalRoom);
/// <summary>A default chat tab a server's plugin declares ("chatTabs", 2026-11-15); owner key = Server + "/" + Id.</summary>
internal sealed record SectionChatTabDecl(string Server, string Id, string Name, bool Pinned, string GlobalRoomFallback, SectionChatRuleDecl[] Rules);
/// <summary>One plugin's parsed manifest.</summary>
internal sealed record SectionManifest(
string Plugin,
@ -157,6 +174,9 @@ internal sealed record SectionManifest(
/// <summary>The windows the plugin draws (plugin API v89 engines host them; older ones ignore the key).</summary>
public SectionWindowDecl[] Windows { get; init; } = Array.Empty<SectionWindowDecl>();
/// <summary>The default chat tabs the plugin declares for its server (2026-11-15; older engines ignore the key).</summary>
public SectionChatTabDecl[] ChatTabs { get; init; } = Array.Empty<SectionChatTabDecl>();
}
internal static class SectionManifestParser
@ -178,6 +198,9 @@ internal static class SectionManifestParser
public const int MaxWindowTitleLength = 32;
/// <summary>Most items kept in one window's bar-icon dropdown (v95).</summary>
public const int MaxBarMenuItems = 6;
/// <summary>Most chat tab entries kept per manifest, and rules per entry (2026-11-15).</summary>
public const int MaxChatTabs = 8;
public const int MaxChatRules = 16;
public const float DefaultWindowWidth = 420f, DefaultWindowHeight = 300f, MinWindowSize = 120f, MaxWindowSize = 2000f;
/// <summary>
@ -278,9 +301,20 @@ internal static class SectionManifestParser
BarMenu: barMenu.Count > 0 ? barMenu.ToArray() : null));
}
var chatTabs = new List<SectionChatTabDecl>();
foreach (JsonElement e in Array(root, "chatTabs"))
{
if (chatTabs.Count >= MaxChatTabs) { Note(ref error, $"chatTabs: more than {MaxChatTabs} entries, the rest dropped"); break; }
SectionChatTabDecl? decl = ChatTab(e, out string why);
if (decl == null) { Note(ref error, "chatTabs: entry dropped, " + why); continue; }
if (chatTabs.Exists(c => c.Server == decl.Server && c.Id == decl.Id)) { Note(ref error, "chatTabs: duplicate " + decl.Server + "/" + decl.Id); continue; }
chatTabs.Add(decl);
}
return new SectionManifest(plugin, servers.ToArray(), sections.ToArray(), contributions.ToArray())
{
Windows = windows.ToArray(),
ChatTabs = chatTabs.ToArray(),
};
}
catch (JsonException ex)
@ -290,6 +324,39 @@ internal static class SectionManifestParser
}
}
/// <summary>Keeps the first note; a dropped chatTabs entry is reported without discarding the rest of the manifest.</summary>
private static void Note(ref string error, string note)
{
if (error.Length == 0) error = note;
}
/// <summary>One chatTabs entry, or null (with the reason) when a field is bad.</summary>
private static SectionChatTabDecl? ChatTab(JsonElement e, out string why)
{
why = string.Empty;
string server = Id(Str(e, "server"));
if (server.Length == 0) { why = "missing or bad server"; return null; }
string id = Id(Str(e, "id"));
if (id.Length == 0) { why = "missing or bad id"; return null; }
string name = Text(Str(e, "name"), MaxLabelLength);
if (name.Length == 0) { why = "missing name"; return null; }
string fallback = Str(e, "globalRoomFallback").Trim();
string room = fallback.Length == 0 ? "General" : GlobalRooms.FirstOrDefault(r => r.Equals(fallback, StringComparison.OrdinalIgnoreCase)) ?? string.Empty;
if (room.Length == 0) { why = "globalRoomFallback is not General, Trade, LFG or Roleplay"; return null; }
var rules = new List<SectionChatRuleDecl>();
foreach (JsonElement r in Array(e, "rules"))
{
if (rules.Count >= MaxChatRules) { why = $"more than {MaxChatRules} rules"; return null; }
string action = Str(r, "action").Trim().ToLowerInvariant();
if (action is not ("copy" or "move")) { why = "rule action is not copy or move"; return null; }
rules.Add(new SectionChatRuleDecl(action, Text(Str(r, "when"), MaxLabelLength), Text(Str(r, "mode"), MaxLabelLength),
Text(Str(r, "pattern"), MaxTextLength), Bool(r, "globalRoom", false)));
}
return new SectionChatTabDecl(server, id, name, Bool(e, "pinned", false), room, rules.ToArray());
}
private static readonly string[] GlobalRooms = { "General", "Trade", "LFG", "Roleplay" };
// ── Field rules ─────────────────────────────────────────────────────
/// <summary>A lower-case id of letters, digits, '-' and '_' (at most MaxIdLength), else "".</summary>