-
Notifications
You must be signed in to change notification settings - Fork 54
feature(desktop): start of naive autocomplete #519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
AviPeltz
wants to merge
2
commits into
main
Choose a base branch
from
autocomplete
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
240 changes: 240 additions & 0 deletions
240
apps/desktop/src/lib/trpc/routers/autocomplete/autocomplete.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,240 @@ | ||
| import fs from "node:fs/promises"; | ||
| import path from "node:path"; | ||
| import { commandHistoryManager } from "main/lib/command-history"; | ||
| import { z } from "zod"; | ||
| import { publicProcedure, router } from "../.."; | ||
|
|
||
| /** | ||
| * Autocomplete router for command history and file completion | ||
| * | ||
| * Provides: | ||
| * - Command history search with fuzzy matching | ||
| * - Recent command matching for ghost text suggestions | ||
| * - File/directory listing for path completion | ||
| */ | ||
| export const createAutocompleteRouter = () => { | ||
| return router({ | ||
| /** | ||
| * Search command history with fuzzy matching | ||
| * Uses FTS5 for efficient search across all recorded commands | ||
| */ | ||
| searchHistory: publicProcedure | ||
| .input( | ||
| z.object({ | ||
| query: z.string(), | ||
| limit: z.number().default(50), | ||
| workspaceId: z.string().optional(), | ||
| }), | ||
| ) | ||
| .query(({ input }) => { | ||
| return commandHistoryManager.search({ | ||
| query: input.query, | ||
| limit: input.limit, | ||
| workspaceId: input.workspaceId, | ||
| }); | ||
| }), | ||
|
|
||
| /** | ||
| * Get the most recent command matching a prefix | ||
| * Used for inline ghost text suggestions (Fish-style autosuggestions) | ||
| */ | ||
| getRecentMatch: publicProcedure | ||
| .input( | ||
| z.object({ | ||
| prefix: z.string(), | ||
| workspaceId: z.string().optional(), | ||
| }), | ||
| ) | ||
| .query(({ input }) => { | ||
| return commandHistoryManager.getRecentMatch({ | ||
| prefix: input.prefix, | ||
| workspaceId: input.workspaceId, | ||
| }); | ||
| }), | ||
|
|
||
| /** | ||
| * Record a command execution | ||
| * Called when shell emits OSC 133 sequences | ||
| */ | ||
| recordCommand: publicProcedure | ||
| .input( | ||
| z.object({ | ||
| command: z.string(), | ||
| workspaceId: z.string().optional(), | ||
| cwd: z.string().optional(), | ||
| exitCode: z.number().optional(), | ||
| }), | ||
| ) | ||
| .mutation(({ input }) => { | ||
| commandHistoryManager.record({ | ||
| command: input.command, | ||
| workspaceId: input.workspaceId, | ||
| cwd: input.cwd, | ||
| exitCode: input.exitCode, | ||
| }); | ||
| }), | ||
|
|
||
| /** | ||
| * Get recent commands (no search query) | ||
| * Used for initial history picker display | ||
| */ | ||
| getRecent: publicProcedure | ||
| .input( | ||
| z.object({ | ||
| limit: z.number().default(50), | ||
| workspaceId: z.string().optional(), | ||
| }), | ||
| ) | ||
| .query(({ input }) => { | ||
| return commandHistoryManager.getRecent({ | ||
| limit: input.limit, | ||
| workspaceId: input.workspaceId, | ||
| }); | ||
| }), | ||
|
|
||
| /** | ||
| * List files and directories for path completion | ||
| * Supports partial path matching (e.g., "src/comp" matches "src/components/") | ||
| */ | ||
| listCompletions: publicProcedure | ||
| .input( | ||
| z.object({ | ||
| partial: z.string(), | ||
| cwd: z.string(), | ||
| showHidden: z.boolean().default(false), | ||
| type: z.enum(["all", "files", "directories"]).default("all"), | ||
| }), | ||
| ) | ||
| .query(async ({ input }) => { | ||
| const { partial, cwd, showHidden, type } = input; | ||
|
|
||
| try { | ||
| // Resolve the partial path | ||
| const isAbsolute = path.isAbsolute(partial); | ||
| const basePath = isAbsolute ? partial : path.join(cwd, partial); | ||
|
|
||
| // Check if partial ends with separator (user is in directory) | ||
| const _endsWithSep = | ||
| partial.endsWith("/") || partial.endsWith(path.sep); | ||
|
|
||
| // Get directory and prefix for filtering | ||
| let dirPath: string; | ||
| let prefix: string; | ||
|
|
||
| try { | ||
| const stat = await fs.stat(basePath); | ||
| if (stat.isDirectory()) { | ||
| dirPath = basePath; | ||
| prefix = ""; | ||
| } else { | ||
| dirPath = path.dirname(basePath); | ||
| prefix = path.basename(basePath); | ||
| } | ||
| } catch { | ||
| // Path doesn't exist, treat as partial | ||
| dirPath = path.dirname(basePath); | ||
| prefix = path.basename(basePath); | ||
| } | ||
|
|
||
| // Read directory | ||
| const entries = await fs.readdir(dirPath, { withFileTypes: true }); | ||
|
|
||
| // Filter and map entries | ||
| const completions = entries | ||
| .filter((entry) => { | ||
| // Filter by prefix | ||
| if ( | ||
| prefix && | ||
| !entry.name.toLowerCase().startsWith(prefix.toLowerCase()) | ||
| ) { | ||
| return false; | ||
| } | ||
|
|
||
| // Filter hidden files | ||
| if (!showHidden && entry.name.startsWith(".")) { | ||
| return false; | ||
| } | ||
|
|
||
| // Filter by type | ||
| if (type === "files" && entry.isDirectory()) { | ||
| return false; | ||
| } | ||
| if (type === "directories" && !entry.isDirectory()) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| }) | ||
| .map((entry) => { | ||
| const isDirectory = entry.isDirectory(); | ||
| const name = entry.name; | ||
| const fullPath = path.join(dirPath, name); | ||
|
|
||
| // Build the completion text (what to insert) | ||
| // If user typed "src/comp", completion for "components" should be "onents/" | ||
| const completionSuffix = isDirectory ? "/" : ""; | ||
| const insertText = name.slice(prefix.length) + completionSuffix; | ||
|
|
||
| return { | ||
| name, | ||
| insertText, | ||
| fullPath, | ||
| isDirectory, | ||
| icon: isDirectory ? "folder" : getFileIcon(name), | ||
| }; | ||
| }) | ||
| .sort((a, b) => { | ||
| // Directories first, then alphabetical | ||
| if (a.isDirectory && !b.isDirectory) return -1; | ||
| if (!a.isDirectory && b.isDirectory) return 1; | ||
| return a.name.localeCompare(b.name); | ||
| }) | ||
| .slice(0, 50); // Limit results | ||
|
|
||
| return { | ||
| basePath: dirPath, | ||
| prefix, | ||
| completions, | ||
| }; | ||
| } catch (error) { | ||
| return { | ||
| basePath: cwd, | ||
| prefix: partial, | ||
| completions: [], | ||
| error: error instanceof Error ? error.message : "Unknown error", | ||
| }; | ||
| } | ||
| }), | ||
| }); | ||
| }; | ||
|
|
||
| /** | ||
| * Get a simple icon identifier based on file extension | ||
| */ | ||
| function getFileIcon(filename: string): string { | ||
| const ext = path.extname(filename).toLowerCase(); | ||
| const iconMap: Record<string, string> = { | ||
| ".ts": "typescript", | ||
| ".tsx": "react", | ||
| ".js": "javascript", | ||
| ".jsx": "react", | ||
| ".json": "json", | ||
| ".md": "markdown", | ||
| ".css": "css", | ||
| ".scss": "css", | ||
| ".html": "html", | ||
| ".py": "python", | ||
| ".rs": "rust", | ||
| ".go": "go", | ||
| ".sh": "shell", | ||
| ".bash": "shell", | ||
| ".zsh": "shell", | ||
| ".yml": "yaml", | ||
| ".yaml": "yaml", | ||
| ".toml": "config", | ||
| ".env": "config", | ||
| ".gitignore": "git", | ||
| ".git": "git", | ||
| }; | ||
| return iconMap[ext] || "file"; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { createAutocompleteRouter } from "./autocomplete"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical: OSC 133 sequence order is incorrect for bash.
The bash implementation emits the C (command start) sequence in
PROMPT_COMMAND(line 95), which runs after the command completes. According to OSC 133 semantics, C should be emitted before the command executes, and D should be emitted after.The correct flow should be:
Currently, both C and D are emitted in PROMPT_COMMAND after the command completes, which will produce incorrect command tracking timestamps and ordering.
🔎 Proposed fix
_superset_last_cmd="" _superset_trap_debug() { # Capture the command before execution _superset_last_cmd="$BASH_COMMAND" + # Emit command start when we have a command + if [[ -n "$_superset_last_cmd" && "$_superset_last_cmd" != "_superset_prompt_command" ]]; then + printf '\033]133;C;%s\033\\' "${_superset_last_cmd//[[:cntrl:]]}" + fi } _superset_prompt_command() { local exit_code=$? # Emit command done with exit code printf '\033]133;D;%d\033\\' "$exit_code" - # Emit command start when we have a captured command - if [[ -n "$_superset_last_cmd" && "$_superset_last_cmd" != "_superset_prompt_command" ]]; then - printf '\033]133;C;%s\033\\' "${_superset_last_cmd//[[:cntrl:]]}" - fi _superset_last_cmd="" }📝 Committable suggestion
🤖 Prompt for AI Agents