Skip to content

Commit 0b8ba41

Browse files
bkudiessCopilotCopilotshanselman
authored
Chat: surface the full / command catalog, grouped by Mac buckets with styled rows (#893)
* Chat: group the / command palette by Mac buckets with styled rows Enhance the inline "/" slash command palette (added in #890) so it matches Mac's grouped, styled command menu: - Group commands under Mac's Session / Model / Tools / Agents display buckets with uppercase category subheadings; the run options (verbose/reasoning/fast/trace/usage/elevated/exec/think/model) surface under "Model" instead of being truncated off a flat alphabetical list. - Mac-style rows: icon, /name (bold, default font), [args] hint (mono, muted), right-aligned description, and an accent-tinted "N options" badge. - The palette filters and ranks live as you type; relevance order is preserved within each group so the top match stays the default Enter target. - Auto-scroll the keyboard-selected row into view during arrow navigation. Bucket mapping mirrors Mac's mapCategory + CATEGORY_OVERRIDES; ordering mirrors CATEGORY_ORDER. No item cap (the popup ScrollViewer bounds height, so no bucket is silently dropped). No custom "Run options" menu (Mac has none) and no sessions.patch UI plumbing. Co-authored-by: Copilot <[email protected]> * Chat palette: match highlight, opaque flyout, global-best selection UI polish for the grouped "/" command palette plus the Codex/ClawSweeper review fix: - Highlight the matched query substring in the command name + description with a soft accent tint and white (primary) text. Uses TextHighlighter (rectangular, layout-safe); the alpha is baked into the Color because TextHighlighter ignores SolidColorBrush.Opacity. - Make the slash flyout background opaque, matching the composer textbox (TextControlBackground composited over the base surface) instead of a lighter floating surface that showed chat content through it. - Hide the flyout entirely when nothing matches (drop the "No matching commands" hint). - Preserve the GLOBAL best Search match as the default Enter/Tab target across grouped buckets. Grouping renders by Mac bucket order, but the weak first-bucket row is no longer the default selection; keyboard selection now defaults to the top-ranked match via ChatCommandCatalogView.GroupForPalette / GroupedPalette.DefaultSelectionIndex (addresses ClawSweeper review at OpenClawComposer.cs:474). Adds cross-bucket regression tests. Co-authored-by: Copilot <[email protected]> * Fix hidden slash palette key handling Only trap composer keys while the slash command catalog is still loading and the loading popup is visible. Once the catalog is loaded with no matching command, let no-match slash input behave like ordinary text. Co-authored-by: Copilot App <[email protected]> --------- Co-authored-by: Copilot <[email protected]> Co-authored-by: Copilot <[email protected]> Co-authored-by: Scott Hanselman <[email protected]>
1 parent a13d683 commit 0b8ba41

4 files changed

Lines changed: 568 additions & 57 deletions

File tree

‎src/OpenClaw.Shared/CommandCatalogPresentation.cs‎

Lines changed: 190 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,19 @@ private static string Normalize(string value)
110110
/// <summary>A named group of commands sharing a category, in display order.</summary>
111111
public sealed record CommandCategoryGroup(string Category, IReadOnlyList<GatewayCommand> Commands);
112112

113+
/// <summary>
114+
/// A grouped command palette: the display <see cref="Groups"/>; the same commands
115+
/// <see cref="Flattened"/> in group/display order (the keyboard-navigation list);
116+
/// and <see cref="DefaultSelectionIndex"/> — the index in <see cref="Flattened"/>
117+
/// of the GLOBAL best search match, i.e. the row keyboard selection should default
118+
/// to so that display grouping never demotes a strong later-bucket match behind a
119+
/// weak earlier-bucket one for Enter/Tab.
120+
/// </summary>
121+
public sealed record GroupedPalette(
122+
IReadOnlyList<CommandCategoryGroup> Groups,
123+
IReadOnlyList<GatewayCommand> Flattened,
124+
int DefaultSelectionIndex);
125+
113126
/// <summary>
114127
/// Ranked search + category grouping over a set of gateway commands for the chat
115128
/// command palette. Distinct from <see cref="CommandCatalogQuery"/> (a boolean
@@ -161,20 +174,79 @@ public IReadOnlyList<GatewayCommand> Search(string? query)
161174
}
162175

163176
/// <summary>
164-
/// Groups commands by category (falling back to source label, then "Other"),
165-
/// optionally filtered by the same search used in <see cref="Search"/>.
166-
/// Groups and their members are returned in a stable, display-friendly order.
177+
/// Groups commands by their raw category (falling back to source label, then
178+
/// "Other"), optionally filtered/ordered. See <see cref="GroupBy"/>.
179+
/// </summary>
180+
public IReadOnlyList<CommandCategoryGroup> GroupByCategory(
181+
string? query = null, IReadOnlyList<string>? categoryOrder = null)
182+
=> GroupBy(CategoryKey, query, categoryOrder);
183+
184+
/// <summary>
185+
/// Groups commands using a caller-supplied category selector, optionally
186+
/// filtered by the same search used in <see cref="Search"/>. Members within a
187+
/// group preserve the relevance order produced by <see cref="Search"/>
188+
/// (alphabetical when the query is empty), so the top match stays first. When
189+
/// <paramref name="categoryOrder"/> is supplied, groups are ordered by that
190+
/// sequence first (unlisted categories sort last, alphabetically); otherwise
191+
/// groups are ordered alphabetically. The selector lets the palette group by a
192+
/// mapped bucket (e.g. Mac's Session/Model/Tools/Agents) rather than the raw
193+
/// wire category.
167194
/// </summary>
168-
public IReadOnlyList<CommandCategoryGroup> GroupByCategory(string? query = null)
195+
public IReadOnlyList<CommandCategoryGroup> GroupBy(
196+
Func<GatewayCommand, string> categorySelector,
197+
string? query = null,
198+
IReadOnlyList<string>? categoryOrder = null)
169199
{
170200
var matched = Search(query);
171-
return matched
172-
.GroupBy(CategoryKey, StringComparer.OrdinalIgnoreCase)
173-
.Select(g => new CommandCategoryGroup(g.Key, Ordered(g).ToList()))
201+
// GroupBy preserves source (relevance) order within each group; do NOT
202+
// re-sort the members — that would demote a strong match below a weaker
203+
// one and change the default Enter target.
204+
var groups = matched
205+
.GroupBy(categorySelector, StringComparer.OrdinalIgnoreCase)
206+
.Select(g => new CommandCategoryGroup(g.Key, g.ToList()));
207+
208+
if (categoryOrder is { Count: > 0 })
209+
{
210+
int Rank(string category)
211+
{
212+
for (int i = 0; i < categoryOrder.Count; i++)
213+
if (string.Equals(categoryOrder[i], category, StringComparison.OrdinalIgnoreCase))
214+
return i;
215+
return int.MaxValue;
216+
}
217+
218+
return groups
219+
.OrderBy(g => Rank(g.Category))
220+
.ThenBy(g => g.Category, StringComparer.OrdinalIgnoreCase)
221+
.ToList();
222+
}
223+
224+
return groups
174225
.OrderBy(g => g.Category, StringComparer.OrdinalIgnoreCase)
175226
.ToList();
176227
}
177228

229+
/// <summary>
230+
/// Groups commands for the chat command palette (visual grouping) and also
231+
/// computes <see cref="GroupedPalette.DefaultSelectionIndex"/> — the index,
232+
/// within the flattened group order, of the GLOBAL best <see cref="Search"/>
233+
/// match. Display grouping orders by bucket, which can render a strong
234+
/// later-bucket match after a weak earlier-bucket one; keyboard selection
235+
/// should default to this index (not 0) to preserve the flat-search Enter/Tab
236+
/// target.
237+
/// </summary>
238+
public GroupedPalette GroupForPalette(
239+
Func<GatewayCommand, string> categorySelector,
240+
string? query = null,
241+
IReadOnlyList<string>? categoryOrder = null)
242+
{
243+
var groups = GroupBy(categorySelector, query, categoryOrder);
244+
var flattened = groups.SelectMany(g => g.Commands).ToList();
245+
var top = Search(query).FirstOrDefault();
246+
var defaultIndex = top is null ? 0 : flattened.IndexOf(top);
247+
return new GroupedPalette(groups, flattened, defaultIndex < 0 ? 0 : defaultIndex);
248+
}
249+
178250
private static string CategoryKey(GatewayCommand cmd)
179251
{
180252
if (!string.IsNullOrWhiteSpace(cmd.Category)) return cmd.Category!.Trim();
@@ -216,3 +288,114 @@ void Consider(string? token, int exact, int prefix, int contains)
216288
return best;
217289
}
218290
}
291+
292+
/// <summary>
293+
/// Mac/web parity for the slash command palette grouping (slash-commands.ts):
294+
/// maps each command into one of four display buckets — Session, Model, Tools,
295+
/// Agents — via per-command name overrides first, then a raw-category fallback
296+
/// (session→Session, options→Model, management→Tools, everything else→Tools).
297+
/// </summary>
298+
public static class CommandCategories
299+
{
300+
/// <summary>Bucket display order, mirroring Mac's CATEGORY_ORDER.</summary>
301+
public static readonly IReadOnlyList<string> DisplayOrder = new[]
302+
{
303+
"session", "model", "tools", "agents",
304+
};
305+
306+
private static readonly Dictionary<string, string> Labels = new(StringComparer.OrdinalIgnoreCase)
307+
{
308+
["session"] = "Session",
309+
["model"] = "Model",
310+
["tools"] = "Tools",
311+
["agents"] = "Agents",
312+
};
313+
314+
// Per-command bucket overrides keyed by normalized command name. Mirrors
315+
// Mac's CATEGORY_OVERRIDES, applied before the raw-category fallback so e.g.
316+
// /usage (raw category "options") lands in Tools rather than Model. This is a
317+
// deliberate SUPERSET of Mac's map: it additionally pins export/kill/focus/
318+
// unfocus, which Mac leaves to its raw-category fallback.
319+
private static readonly Dictionary<string, string> BucketOverrides = new(StringComparer.OrdinalIgnoreCase)
320+
{
321+
["help"] = "tools",
322+
["commands"] = "tools",
323+
["tools"] = "tools",
324+
["skill"] = "tools",
325+
["status"] = "tools",
326+
["export_session"] = "tools",
327+
["export"] = "tools",
328+
["usage"] = "tools",
329+
["tts"] = "tools",
330+
["agents"] = "agents",
331+
["subagents"] = "agents",
332+
["kill"] = "agents",
333+
["steer"] = "agents",
334+
["redirect"] = "agents",
335+
["session"] = "session",
336+
["stop"] = "session",
337+
["reset"] = "session",
338+
["new"] = "session",
339+
["compact"] = "session",
340+
["focus"] = "session",
341+
["unfocus"] = "session",
342+
["model"] = "model",
343+
["models"] = "model",
344+
["think"] = "model",
345+
["verbose"] = "model",
346+
["fast"] = "model",
347+
["reasoning"] = "model",
348+
["elevated"] = "model",
349+
["queue"] = "model",
350+
};
351+
352+
/// <summary>
353+
/// Display bucket (session/model/tools/agents) for a command, mirroring Mac's
354+
/// mapCategory: name override first, then raw-category fallback.
355+
/// </summary>
356+
public static string Bucket(GatewayCommand command)
357+
{
358+
if (command is null) return "tools";
359+
var key = NormalizeKey(command);
360+
if (!string.IsNullOrEmpty(key) && BucketOverrides.TryGetValue(key, out var bucket))
361+
return bucket;
362+
363+
return (command.Category?.Trim().ToLowerInvariant()) switch
364+
{
365+
"session" => "session",
366+
"options" => "model",
367+
"management" => "tools",
368+
_ => "tools",
369+
};
370+
}
371+
372+
/// <summary>Friendly heading for a bucket key; Title-cases unknowns.</summary>
373+
public static string Label(string? bucket)
374+
{
375+
if (string.IsNullOrWhiteSpace(bucket)) return "Other";
376+
var key = bucket!.Trim();
377+
if (Labels.TryGetValue(key, out var label)) return label;
378+
return char.ToUpperInvariant(key[0]) + (key.Length > 1 ? key[1..] : "");
379+
}
380+
381+
// Normalizes a command name to an override key the way Mac's normalizeUiKey
382+
// does (lowercase, strip a leading slash, ':' '.' '-' → '_'). Mac keys off
383+
// command.key; the wire command carries no key here, and the catalog is
384+
// requested with text scope, so the text-surface Name is the closest analog —
385+
// fall back to the first text alias, then the native name.
386+
private static string NormalizeKey(GatewayCommand command)
387+
{
388+
var raw = !string.IsNullOrWhiteSpace(command.Name)
389+
? command.Name
390+
: command.TextAliases?.FirstOrDefault(a => !string.IsNullOrWhiteSpace(a));
391+
if (string.IsNullOrWhiteSpace(raw)) raw = command.NativeName;
392+
raw = (raw ?? "").Trim();
393+
if (raw.Length == 0) return "";
394+
if (raw[0] == '/') raw = raw[1..];
395+
raw = raw.ToLowerInvariant();
396+
var chars = raw.ToCharArray();
397+
for (int i = 0; i < chars.Length; i++)
398+
if (chars[i] is ':' or '.' or '-') chars[i] = '_';
399+
return new string(chars);
400+
}
401+
}

0 commit comments

Comments
 (0)