Files
opencode-manager/frontend/src/hooks/useCommands.ts
T
Chris Scott 7e93eb4c34 feat(commands): align built-in slash commands with OpenCode v2
Replace the split built-in command name list and switch with one typed
registry, so a registered command without a handler is a compile error.
Remove the broken themes/share/unshare/editor entries and the hardcoded
init; server and custom commands now win on a name clash and built-ins
dispatch by registry identity.

Add /copy, /btw, /rename, /fork (message picker), /timeline, /model,
/agent, /variants, /mcp, /skills, /settings and /connect. Open the model
picker through state so /model and the select-model shortcut work while a
response is running, load full session history in the fork picker, and
keep attachments when a built-in command is submitted.

Extract shared clipboard and primary-agent helpers, reuse CopyButton in
the manager token settings, and update the Quick Start and chat docs.
2026-10-02 09:53:57 -04:00

61 lines
1.9 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { listCommands } from '@/api/opencode'
import type { CommandInfo } from '@opencode-manager/shared/opencode'
import { BUILTIN_COMMANDS } from '@/lib/builtinCommands'
function sortCommandsByName(commands: CommandInfo[]): CommandInfo[] {
return [...commands].sort((a, b) => a.name.localeCompare(b.name))
}
function rankCommandMatch(command: CommandInfo, searchTerm: string): number {
const name = command.name.toLowerCase()
if (name === searchTerm) return 0
if (name.startsWith(searchTerm)) return 1
return 2
}
const SORTED_BUILTIN_COMMANDS = sortCommandsByName([...BUILTIN_COMMANDS])
interface UseCommandsOptions {
directory?: string
enabled?: boolean
}
export function useCommands(options: UseCommandsOptions = {}) {
const { directory, enabled = true } = options
const { data: commands, isLoading: loading, error } = useQuery({
queryKey: ['opencode', 'commands', directory ?? null],
queryFn: async () => {
const loaded = await listCommands(directory)
const allCommands = [...loaded, ...BUILTIN_COMMANDS]
const uniqueCommands = allCommands.filter((command, index, self) =>
index === self.findIndex((c) => c.name === command.name)
)
return sortCommandsByName(uniqueCommands)
},
enabled,
initialData: SORTED_BUILTIN_COMMANDS,
})
const filterCommands = (query: string) => {
if (!query.trim()) return commands
const searchTerm = query.toLowerCase()
return commands
.filter(command => command.name.toLowerCase().includes(searchTerm))
.sort((a, b) => {
const rankDifference = rankCommandMatch(a, searchTerm) - rankCommandMatch(b, searchTerm)
if (rankDifference !== 0) return rankDifference
return a.name.localeCompare(b.name)
})
}
return {
commands,
loading,
error: error ? 'Failed to load commands' : null,
filterCommands
}
}