@@ -0,0 +1,151 @@
1+import { join, relative } from "node:path";
2+import { loadAgents } from "./agents.ts";
3+import { loadCustomCommands } from "./custom-commands.ts";
4+import { loadSkillsIndex } from "./skills.ts";
5+
6+// ─── Built-in commands ─────────────────────────────────────────────────────────
7+
8+const BUILTIN_COMMANDS = [
9+ "model",
10+ "models",
11+ "undo",
12+ "reasoning",
13+ "context",
14+ "cache",
15+ "agent",
16+ "mcp",
17+ "new",
18+ "help",
19+ "exit",
20+ "quit",
21+];
22+
23+// ─── Sub-command / parameter completions ───────────────────────────────────────
24+
25+const COMMAND_PARAMS: Record<string, string[] | ((cwd: string) => string[])> = {
26+ model: ["effort"],
27+ reasoning: ["on", "off"],
28+ context: ["prune", "cap"],
29+ cache: ["on", "off", "openai", "gemini"],
30+ mcp: ["list", "add", "remove", "rm"],
31+ agent: (cwd) => {
32+ const agents = loadAgents(cwd);
33+ return ["off", ...agents.keys()];
34+ },
35+};
36+
37+const NESTED_PARAMS: Record<string, Record<string, string[]>> = {
38+ model: { effort: ["low", "medium", "high", "xhigh", "off"] },
39+ context: {
40+ prune: ["off", "balanced", "aggressive"],
41+ cap: ["off"],
42+ },
43+ cache: {
44+ openai: ["in_memory", "24h"],
45+ gemini: ["off"],
46+ },
47+};
48+
49+// ─── Public API ────────────────────────────────────────────────────────────────
50+
51+const MAX = 10;
52+
53+/** Complete a `/`-prefixed command input. `text` includes the leading `/`. */
54+export function getCommandCompletions(text: string, cwd: string): string[] {
55+ const parts = text.slice(1).split(/\s+/);
56+ const p0 = (parts[0] ?? "").toLowerCase();
57+
58+ // Completing the command name itself: `/mod` → `/model`
59+ if (parts.length === 1) {
60+ const results: string[] = [];
61+
62+ // Built-in commands
63+ for (const cmd of BUILTIN_COMMANDS) {
64+ if (cmd.startsWith(p0)) results.push(`/${cmd}`);
65+ if (results.length >= MAX) break;
66+ }
67+
68+ // Custom commands
69+ if (results.length < MAX) {
70+ const custom = loadCustomCommands(cwd);
71+ for (const name of custom.keys()) {
72+ if (name.startsWith(p0)) results.push(`/${name}`);
73+ if (results.length >= MAX) break;
74+ }
75+ }
76+
77+ return results;
78+ }
79+
80+ // Completing second token: `/model eff` → `/model effort`
81+ if (parts.length === 2) {
82+ const query = (parts[1] ?? "").toLowerCase();
83+ const paramDef = COMMAND_PARAMS[p0];
84+ if (!paramDef) return [];
85+ const options = typeof paramDef === "function" ? paramDef(cwd) : paramDef;
86+ return options
87+ .filter((o) => o.startsWith(query))
88+ .slice(0, MAX)
89+ .map((o) => `/${p0} ${o}`);
90+ }
91+
92+ // Completing third token: `/model effort lo` → `/model effort low`
93+ if (parts.length === 3) {
94+ const sub = (parts[1] ?? "").toLowerCase();
95+ const query = (parts[2] ?? "").toLowerCase();
96+ const nested = NESTED_PARAMS[p0]?.[sub];
97+ if (!nested) return [];
98+ return nested
99+ .filter((o) => o.startsWith(query))
100+ .slice(0, MAX)
101+ .map((o) => `/${p0} ${sub} ${o}`);
102+ }
103+
104+ return [];
105+}
106+
107+/** Complete an `@`-prefixed reference (skills, files). */
108+
109+export async function getAtCompletions(
110+ prefix: string,
111+ cwd: string,
112+): Promise<string[]> {
113+ const query = prefix.startsWith("@") ? prefix.slice(1) : prefix;
114+ const results: string[] = [];
115+
116+ // Skills: @<skill-name>
117+ const skills = loadSkillsIndex(cwd);
118+ for (const [name] of skills) {
119+ if (results.length >= MAX) break;
120+ if (name.includes(query)) results.push(`@${name}`);
121+ }
122+
123+ // Files: @<relative-path> — fill remaining slots up to MAX
124+
125+ if (results.length < MAX) {
126+ const glob = new Bun.Glob(`**/*${query}*`);
127+ for await (const file of glob.scan({ cwd, onlyFiles: true })) {
128+ if (file.includes("node_modules") || file.includes(".git")) continue;
129+ results.push(`@${relative(cwd, join(cwd, file))}`);
130+ if (results.length >= MAX) break;
131+ }
132+ }
133+
134+ return results;
135+}
136+
137+/** Complete a bare file path (no `@` prefix). */
138+export async function getFilePathCompletions(
139+ query: string,
140+ cwd: string,
141+): Promise<string[]> {
142+ if (!query) return [];
143+ const results: string[] = [];
144+ const glob = new Bun.Glob(`${query}*`);
145+ for await (const file of glob.scan({ cwd })) {
146+ if (file.includes("node_modules") || file.includes(".git")) continue;
147+ results.push(file);
148+ if (results.length >= MAX) break;
149+ }
150+ return results;
151+}