Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(desktop): 命令面板支持非前缀匹配 (#2116)

fmfsaisaie0f5f18f8bac
diff --git a/apps/desktop/help-knowledge/commands.md b/apps/desktop/help-knowledge/commands.md
index 84f7fc22c3..8743b2c7fc 100644
--- a/apps/desktop/help-knowledge/commands.md
+++ b/apps/desktop/help-knowledge/commands.md
@@ -13,7 +13,7 @@ Type `/` at the start of the composer to open the command palette.
**Using the palette:**
-- Start typing to filter. Matching is **prefix-only** (case-insensitive `startsWith` on the command name) — not fuzzy.
+- Start typing to filter. Matching is **contains-based** (case-insensitive substring matching on the command name) — not fuzzy.
- Up to 25 matches shown at a time.
- **↑ / ↓** move focus, **Enter** runs the focused command, **Esc** closes the palette.
diff --git a/apps/desktop/src/main/maker-ipc/helpKnowledge.generated.ts b/apps/desktop/src/main/maker-ipc/helpKnowledge.generated.ts
index 80a52b9424..746aba03cf 100644
--- a/apps/desktop/src/main/maker-ipc/helpKnowledge.generated.ts
+++ b/apps/desktop/src/main/maker-ipc/helpKnowledge.generated.ts
@@ -34,7 +34,7 @@ export const HELP_KNOWLEDGE: HelpKnowledgeDoc[] = [
id: "commands",
title: "Slash commands",
summary: "Type \"/\" in the composer to open a palette merging your skills, built-in app commands, and the agent's own commands.",
- content: "Type `/` at the start of the composer to open the command palette.\n\n**What's in the palette (priority order on collisions):**\n\n1. **Your installed skills** (highest priority) — anything in your global or project skills directory (see the Skills topic).\n2. **Built-in app commands** — `/help` (show available commands), `/clear` (clear the current session context — resets the conversation in place without creating a new session), `/cmd` (run a shell command in the working directory), `/issue` (file an issue), `/goal` (start a goal-driven run), `/learn`, `/workflows`, and `/jump-session`.\n3. **The agent's own commands** — e.g. `/compact`, `/agents`, `/memory` (the exact list depends on the agent; Claude Code and Codex each contribute their own).\n\n**Using the palette:**\n\n- Start typing to filter. Matching is **prefix-only** (case-insensitive `startsWith` on the command name) — not fuzzy.\n- Up to 25 matches shown at a time.\n- **↑ / ↓** move focus, **Enter** runs the focused command, **Esc** closes the palette.\n\n**Notes:**\n\n- If a name collides across sources (same command in a skill and in a built-in), the higher-priority source wins; the lower-priority one is silently skipped (logged as a warning).\n- Most built-in app commands run inside the desktop app (a few, like `/workflows` and `/jump-session`, are handled directly in the UI); agent commands are sent as prompt prefixes to the agent.",
+ content: "Type `/` at the start of the composer to open the command palette.\n\n**What's in the palette (priority order on collisions):**\n\n1. **Your installed skills** (highest priority) — anything in your global or project skills directory (see the Skills topic).\n2. **Built-in app commands** — `/help` (show available commands), `/clear` (clear the current session context — resets the conversation in place without creating a new session), `/cmd` (run a shell command in the working directory), `/issue` (file an issue), `/goal` (start a goal-driven run), `/learn`, `/workflows`, and `/jump-session`.\n3. **The agent's own commands** — e.g. `/compact`, `/agents`, `/memory` (the exact list depends on the agent; Claude Code and Codex each contribute their own).\n\n**Using the palette:**\n\n- Start typing to filter. Matching is **contains-based** (case-insensitive substring matching on the command name) — not fuzzy.\n- Up to 25 matches shown at a time.\n- **↑ / ↓** move focus, **Enter** runs the focused command, **Esc** closes the palette.\n\n**Notes:**\n\n- If a name collides across sources (same command in a skill and in a built-in), the higher-priority source wins; the lower-priority one is silently skipped (logged as a warning).\n- Most built-in app commands run inside the desktop app (a few, like `/workflows` and `/jump-session`, are handled directly in the UI); agent commands are sent as prompt prefixes to the agent.",
},
{
id: "connections",
diff --git a/apps/desktop/src/renderer/lib/__tests__/slashCommands.test.ts b/apps/desktop/src/renderer/lib/__tests__/slashCommands.test.ts
new file mode 100644
index 0000000000..7ce4d94e15
--- /dev/null
+++ b/apps/desktop/src/renderer/lib/__tests__/slashCommands.test.ts
@@ -0,0 +1,49 @@
+import { describe, expect, it } from 'vitest';
+
+import { filterSlashCommands } from '@/lib/slashCommands';
+
+describe('filterSlashCommands', () => {
+ it('matches command names by case-insensitive containment', () => {
+ const commands = [
+ { kind: 'desktop' as const, name: 'lark-drive', description: 'Drive' },
+ { kind: 'desktop' as const, name: 'github', description: 'GitHub' },
+ ];
+
+ expect(filterSlashCommands(commands, 'DRIVE').map((command) => command.name)).toEqual([
+ 'lark-drive',
+ ]);
+ });
+
+ it('ranks exact and prefix matches before ordinary contains matches', () => {
+ const commands = [
+ { kind: 'desktop' as const, name: 'my-drive-tool', description: '' },
+ { kind: 'desktop' as const, name: 'drive-sync', description: '' },
+ { kind: 'desktop' as const, name: 'archive-drive', description: '' },
+ { kind: 'desktop' as const, name: 'drive', description: '' },
+ { kind: 'desktop' as const, name: 'lark-drive', description: '' },
+ ];
+
+ expect(filterSlashCommands(commands, 'drive').map((command) => command.name)).toEqual([
+ 'drive',
+ 'drive-sync',
+ 'my-drive-tool',
+ 'archive-drive',
+ 'lark-drive',
+ ]);
+ });
+
+ it('keeps an exact match visible when contains matches exceed the limit', () => {
+ const containsMatches = Array.from({ length: 25 }, (_, index) => ({
+ kind: 'desktop' as const,
+ name: `plugin-${index}-drive`,
+ description: '',
+ }));
+
+ expect(
+ filterSlashCommands([
+ ...containsMatches,
+ { kind: 'desktop' as const, name: 'drive', description: '' },
+ ], 'drive', 25).map((command) => command.name),
+ ).toContain('drive');
+ });
+});
diff --git a/apps/desktop/src/renderer/lib/slashCommands.ts b/apps/desktop/src/renderer/lib/slashCommands.ts
index 6a1260f5ef..bbcb80a6de 100644
--- a/apps/desktop/src/renderer/lib/slashCommands.ts
+++ b/apps/desktop/src/renderer/lib/slashCommands.ts
@@ -67,7 +67,10 @@ export function mergeCommands(
}
/**
- * 前缀过滤(case-insensitive); 与 F1 spec 对齐 —— 不做 fuzzy match, 只做 startsWith。
+ * 包含过滤(case-insensitive); 精确匹配优先,其次是前缀匹配,最后是普通包含匹配。
+ *
+ * `/`、`$` 两类命令都复用这套筛选,输入命令中间的关键词也能命中,
+ * 与 `@` 资源面板的搜索体验保持一致。
*/
export function filterSlashCommands(
commands: UnifiedCommand[],
@@ -76,7 +79,15 @@ export function filterSlashCommands(
): UnifiedCommand[] {
const q = query.trim().toLowerCase();
const filtered = q
- ? commands.filter((c) => c.name.toLowerCase().startsWith(q))
+ ? commands
+ .map((command, index) => {
+ const name = command.name.toLowerCase();
+ const rank = name === q ? 0 : name.startsWith(q) ? 1 : name.includes(q) ? 2 : -1;
+ return { command, index, rank };
+ })
+ .filter((entry) => entry.rank >= 0)
+ .sort((a, b) => a.rank - b.rank || a.index - b.index)
+ .map((entry) => entry.command)
: commands;
return filtered.length > limit ? filtered.slice(0, limit) : filtered;
}