/** * Shell completion generators for Cortex CLI */ export type Shell = 'bash' | 'zsh' | 'fish' | 'powershell'; const COMMANDS = [ 'add', 'query', 'show', 'list', 'update', 'remove', 'link', 'graph', 'children', 'decay', 'serve', 'history', 'diff', 'restore', 'capture', 'context', 'config', 'index', 'journal', 'ingest', 'clip', 'export', 'viz', 'import', 'backup', 'restore-backup', 'list-backups', 'graphs', 'use', 'init', 'smart-search', 'ss', 'what', 'now', 'tui', 'ui', ]; const KINDS = ['memory', 'component', 'task', 'decision']; const STATUSES = ['active', 'todo', 'in_progress', 'done', 'deprecated']; const EDGE_TYPES = ['depends_on', 'contains', 'implements', 'blocked_by', 'subtask_of', 'relates_to', 'supersedes', 'about']; export function generateCompletions(shell: Shell): string { switch (shell) { case 'bash': return generateBashCompletions(); case 'zsh': return generateZshCompletions(); case 'fish': return generateFishCompletions(); case 'powershell': return generatePowerShellCompletions(); } } function generateBashCompletions(): string { return `# Cortex CLI completions for Bash # Add to ~/.bashrc or /etc/bash_completion.d/cortex _cortex_completions() { local cur prev words cword _init_completion -n : || return local commands="${COMMANDS.join(' ')}" local kinds="${KINDS.join(' ')}" local statuses="${STATUSES.join(' ')}" local edge_types="${EDGE_TYPES.join(' ')}" case "\${prev}" in cortex) COMPREPLY=($(compgen -W "\${commands}" -- "\${cur}")) return 0 ;; add) COMPREPLY=($(compgen -W "\${kinds}" -- "\${cur}")) return 0 ;; --kind|-k) COMPREPLY=($(compgen -W "\${kinds}" -- "\${cur}")) return 0 ;; --status|-s) COMPREPLY=($(compgen -W "\${statuses}" -- "\${cur}")) return 0 ;; --type) COMPREPLY=($(compgen -W "\${edge_types}" -- "\${cur}")) return 0 ;; --tags|-t) local tags=$(cortex --get-tags 2>/dev/null) COMPREPLY=($(compgen -W "\${tags}" -- "\${cur}")) return 0 ;; --graph) local graphs=$(cortex --get-graphs 2>/dev/null) COMPREPLY=($(compgen -W "\${graphs}" -- "\${cur}")) return 0 ;; show|update|remove|children|history|diff|restore) local nodes=$(cortex --get-nodes "\${cur}" 2>/dev/null) COMPREPLY=($(compgen -W "\${nodes}" -- "\${cur}")) return 0 ;; link) local nodes=$(cortex --get-nodes "\${cur}" 2>/dev/null) COMPREPLY=($(compgen -W "\${nodes}" -- "\${cur}")) return 0 ;; use) local graphs=$(cortex --get-graphs 2>/dev/null) COMPREPLY=($(compgen -W "\${graphs}" -- "\${cur}")) return 0 ;; import) COMPREPLY=($(compgen -W "obsidian markdown" -- "\${cur}")) return 0 ;; export|--format|-f) COMPREPLY=($(compgen -W "html svg mermaid markdown jsonld" -- "\${cur}")) return 0 ;; esac if [[ "\${cur}" == -* ]]; then local opts="--help --version --kind --status --tags --limit --format --output --graph --all-graphs" COMPREPLY=($(compgen -W "\${opts}" -- "\${cur}")) return 0 fi } complete -F _cortex_completions cortex `; } function generateZshCompletions(): string { return `#compdef cortex # Cortex CLI completions for Zsh # Add to ~/.zsh/completions/_cortex _cortex() { local -a commands commands=( 'add:Add a node to the knowledge graph' 'query:Search the knowledge graph' 'show:Show a node and its connections' 'list:List nodes' 'update:Update a node' 'remove:Remove a node' 'link:Create a link between nodes' 'graph:Display graph structure' 'children:List child nodes' 'decay:Mark old nodes as stale' 'serve:Start web server' 'history:View node version history' 'diff:Compare node versions' 'restore:Restore node to previous version' 'capture:Capture conversation as memory' 'context:Show context for session' 'config:Configure capture settings' 'index:Index project codebase' 'journal:Daily journal' 'ingest:Ingest content from URL' 'clip:Quick clip a URL' 'export:Export graph as HTML/SVG/Mermaid' 'viz:Export interactive visualization' 'import:Import from Obsidian/Markdown' 'backup:Create database backup' 'restore-backup:Restore from backup' 'list-backups:List backup files' 'graphs:Manage knowledge graphs' 'use:Switch active graph' 'init:Initialize project with graph' 'smart-search:Context-aware search' 'ss:Alias for smart-search' 'what:What should I know right now?' 'now:Show current context' 'tui:Launch interactive TUI' 'ui:Alias for tui' ) local -a kinds=(memory component task decision) local -a statuses=(active todo in_progress done deprecated) local -a edge_types=(depends_on contains implements blocked_by subtask_of relates_to supersedes about) _arguments -C \\ '1:command:->command' \\ '*::arg:->args' case "$state" in command) _describe -t commands 'cortex commands' commands ;; args) case "$words[1]" in add) _arguments \\ '1:kind:(memory component task decision)' \\ '--title[Node title]:title:' \\ '--content[Node content]:content:' \\ '--tags[Tags]:tags:->tags' \\ '--status[Status]:status:(${STATUSES.join(' ')})' ;; show|update|remove|children|history) _arguments '1:node:->nodes' ;; link) _arguments \\ '1:from:->nodes' \\ '2:to:->nodes' \\ '--type[Edge type]:type:(${EDGE_TYPES.join(' ')})' ;; list|query) _arguments \\ '--kind[Filter by kind]:kind:(${KINDS.join(' ')})' \\ '--status[Filter by status]:status:(${STATUSES.join(' ')})' \\ '--tags[Filter by tags]:tags:->tags' \\ '--limit[Max results]:limit:' \\ '--graph[Query specific graph]:graph:->graphs' \\ '--all-graphs[Search all graphs]' ;; use) _arguments '1:graph:->graphs' ;; import) _arguments \\ '1:source:(obsidian markdown)' \\ '2:path:_files -/' ;; export) _arguments \\ '--format[Export format]:format:(html svg mermaid markdown jsonld)' \\ '--output[Output file]:file:_files' \\ '--kind[Filter by kind]:kind:(${KINDS.join(' ')})' ;; esac ;; esac case "$state" in nodes) local -a nodes nodes=(\${(f)"$(cortex --get-nodes 2>/dev/null)"}) _describe -t nodes 'nodes' nodes ;; tags) local -a tags tags=(\${(f)"$(cortex --get-tags 2>/dev/null)"}) _describe -t tags 'tags' tags ;; graphs) local -a graphs graphs=(\${(f)"$(cortex --get-graphs 2>/dev/null)"}) _describe -t graphs 'graphs' graphs ;; esac } _cortex `; } function generateFishCompletions(): string { return `# Cortex CLI completions for Fish # Add to ~/.config/fish/completions/cortex.fish # Disable file completions for cortex complete -c cortex -f # Main commands complete -c cortex -n __fish_use_subcommand -a add -d 'Add a node' complete -c cortex -n __fish_use_subcommand -a query -d 'Search the graph' complete -c cortex -n __fish_use_subcommand -a show -d 'Show a node' complete -c cortex -n __fish_use_subcommand -a list -d 'List nodes' complete -c cortex -n __fish_use_subcommand -a update -d 'Update a node' complete -c cortex -n __fish_use_subcommand -a remove -d 'Remove a node' complete -c cortex -n __fish_use_subcommand -a link -d 'Link nodes' complete -c cortex -n __fish_use_subcommand -a graph -d 'Display graph' complete -c cortex -n __fish_use_subcommand -a children -d 'List children' complete -c cortex -n __fish_use_subcommand -a decay -d 'Mark stale nodes' complete -c cortex -n __fish_use_subcommand -a serve -d 'Start server' complete -c cortex -n __fish_use_subcommand -a history -d 'View history' complete -c cortex -n __fish_use_subcommand -a diff -d 'Compare versions' complete -c cortex -n __fish_use_subcommand -a restore -d 'Restore version' complete -c cortex -n __fish_use_subcommand -a capture -d 'Capture memory' complete -c cortex -n __fish_use_subcommand -a context -d 'Show context' complete -c cortex -n __fish_use_subcommand -a config -d 'Configure' complete -c cortex -n __fish_use_subcommand -a index -d 'Index project' complete -c cortex -n __fish_use_subcommand -a journal -d 'Daily journal' complete -c cortex -n __fish_use_subcommand -a ingest -d 'Ingest URL' complete -c cortex -n __fish_use_subcommand -a clip -d 'Clip URL' complete -c cortex -n __fish_use_subcommand -a export -d 'Export graph' complete -c cortex -n __fish_use_subcommand -a viz -d 'Visualize' complete -c cortex -n __fish_use_subcommand -a import -d 'Import data' complete -c cortex -n __fish_use_subcommand -a backup -d 'Backup database' complete -c cortex -n __fish_use_subcommand -a restore-backup -d 'Restore backup' complete -c cortex -n __fish_use_subcommand -a list-backups -d 'List backups' complete -c cortex -n __fish_use_subcommand -a graphs -d 'Manage graphs' complete -c cortex -n __fish_use_subcommand -a use -d 'Switch graph' complete -c cortex -n __fish_use_subcommand -a init -d 'Init project' complete -c cortex -n __fish_use_subcommand -a smart-search -d 'Smart search' complete -c cortex -n __fish_use_subcommand -a ss -d 'Smart search alias' complete -c cortex -n __fish_use_subcommand -a what -d 'What should I know?' complete -c cortex -n __fish_use_subcommand -a now -d 'Current context' complete -c cortex -n __fish_use_subcommand -a tui -d 'Interactive TUI' complete -c cortex -n __fish_use_subcommand -a ui -d 'TUI alias' # Kind completions complete -c cortex -n '__fish_seen_subcommand_from add' -a 'memory component task decision' # Node ID completions complete -c cortex -n '__fish_seen_subcommand_from show update remove children history' -a '(cortex --get-nodes 2>/dev/null)' # Graph completions complete -c cortex -n '__fish_seen_subcommand_from use' -a '(cortex --get-graphs 2>/dev/null)' # Import source completions complete -c cortex -n '__fish_seen_subcommand_from import' -a 'obsidian markdown' # Export format completions complete -c cortex -n '__fish_seen_subcommand_from export' -l format -a 'html svg mermaid markdown jsonld' # Common options complete -c cortex -l kind -s k -d 'Filter by kind' -a 'memory component task decision' complete -c cortex -l status -s s -d 'Filter by status' -a 'active todo in_progress done deprecated' complete -c cortex -l tags -s t -d 'Filter by tags' -a '(cortex --get-tags 2>/dev/null)' complete -c cortex -l limit -s l -d 'Max results' complete -c cortex -l graph -d 'Specific graph' -a '(cortex --get-graphs 2>/dev/null)' complete -c cortex -l all-graphs -d 'Search all graphs' complete -c cortex -l help -s h -d 'Show help' `; } function generatePowerShellCompletions(): string { return `# Cortex CLI completions for PowerShell # Add to your $PROFILE Register-ArgumentCompleter -Native -CommandName cortex -ScriptBlock { param($wordToComplete, $commandAst, $cursorPosition) $commands = @('add', 'query', 'show', 'list', 'update', 'remove', 'link', 'graph', 'children', 'decay', 'serve', 'history', 'diff', 'restore', 'capture', 'context', 'config', 'index', 'journal', 'ingest', 'clip', 'export', 'viz', 'import', 'backup', 'restore-backup', 'list-backups', 'graphs', 'use', 'init', 'smart-search', 'ss', 'what', 'now', 'tui', 'ui') $kinds = @('memory', 'component', 'task', 'decision') $statuses = @('active', 'todo', 'in_progress', 'done', 'deprecated') $edgeTypes = @('depends_on', 'contains', 'implements', 'blocked_by', 'subtask_of', 'relates_to', 'supersedes', 'about') $elements = $commandAst.CommandElements $command = if ($elements.Count -gt 1) { $elements[1].Value } else { $null } # Command completion if ($elements.Count -le 2) { $commands | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) } return } # Sub-command argument completion switch ($command) { 'add' { if ($elements.Count -eq 3) { $kinds | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) } } } { $_ -in @('show', 'update', 'remove', 'children', 'history') } { $nodes = cortex --get-nodes $wordToComplete 2>$null if ($nodes) { $nodes -split "\\n" | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) } } } 'use' { $graphs = cortex --get-graphs 2>$null if ($graphs) { $graphs -split "\\n" | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) } } } 'import' { if ($elements.Count -eq 3) { @('obsidian', 'markdown') | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) } } } } # Option completion $prevElement = if ($elements.Count -gt 2) { $elements[-2].Value } else { $null } switch ($prevElement) { { $_ -in @('--kind', '-k') } { $kinds | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) } } { $_ -in @('--status', '-s') } { $statuses | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) } } '--type' { $edgeTypes | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) } } { $_ -in @('--format', '-f') } { @('html', 'svg', 'mermaid', 'markdown', 'jsonld') | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) } } } } `; }